clang  7.0.0svn
ASTImporter.cpp
Go to the documentation of this file.
1 //===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the ASTImporter class which imports AST nodes from one
11 // context into another context.
12 //
13 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTImporter.h"
15 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "clang/AST/DeclVisitor.h"
21 #include "clang/AST/StmtVisitor.h"
22 #include "clang/AST/TypeVisitor.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 
27 namespace clang {
28  class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
29  public DeclVisitor<ASTNodeImporter, Decl *>,
30  public StmtVisitor<ASTNodeImporter, Stmt *> {
31  ASTImporter &Importer;
32 
33  public:
34  explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
35 
39 
40  // Importing types
41  QualType VisitType(const Type *T);
55  // FIXME: DependentSizedExtVectorType
64  // FIXME: DependentTypeOfExprType
70  // FIXME: DependentDecltypeType
78  // FIXME: DependentNameType
85 
86  // Importing declarations
87  bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
88  DeclContext *&LexicalDC, DeclarationName &Name,
89  NamedDecl *&ToD, SourceLocation &Loc);
90  void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
93  void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
94 
95  bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
96 
98  Designator ImportDesignator(const Designator &D);
99 
101 
102 
103  /// \brief What we should import from the definition.
105  /// \brief Import the default subset of the definition, which might be
106  /// nothing (if minimal import is set) or might be everything (if minimal
107  /// import is not set).
109  /// \brief Import everything.
111  /// \brief Import only the bare bones needed to establish a valid
112  /// DeclContext.
114  };
115 
117  return IDK == IDK_Everything ||
118  (IDK == IDK_Default && !Importer.isMinimalImport());
119  }
120 
121  bool ImportDefinition(RecordDecl *From, RecordDecl *To,
123  bool ImportDefinition(VarDecl *From, VarDecl *To,
125  bool ImportDefinition(EnumDecl *From, EnumDecl *To,
132  TemplateParameterList *Params);
135  const TemplateArgumentLoc &TALoc);
136  bool ImportTemplateArguments(const TemplateArgument *FromArgs,
137  unsigned NumFromArgs,
139 
140  template <typename InContainerTy>
141  bool ImportTemplateArgumentListInfo(const InContainerTy &Container,
142  TemplateArgumentListInfo &ToTAInfo);
143 
144  template<typename InContainerTy>
146  SourceLocation FromRAngleLoc,
147  const InContainerTy &Container,
149 
151 
152  bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
153  bool Complain = true);
154  bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
155  bool Complain = true);
156  bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
162  Decl *VisitDecl(Decl *D);
169  Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
199 
200 
216 
217  // Importing statements
219 
220  Stmt *VisitStmt(Stmt *S);
229  Stmt *VisitIfStmt(IfStmt *S);
232  Stmt *VisitDoStmt(DoStmt *S);
239  // FIXME: MSAsmStmt
240  // FIXME: SEHExceptStmt
241  // FIXME: SEHFinallyStmt
242  // FIXME: SEHTryStmt
243  // FIXME: SEHLeaveStmt
244  // FIXME: CapturedStmt
248  // FIXME: MSDependentExistsStmt
256 
257  // Importing expressions
258  Expr *VisitExpr(Expr *E);
320 
321 
322  template<typename IIter, typename OIter>
323  void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
324  typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT;
325  ASTImporter &ImporterRef = Importer;
326  std::transform(Ibegin, Iend, Obegin,
327  [&ImporterRef](ItemT From) -> ItemT {
328  return ImporterRef.Import(From);
329  });
330  }
331 
332  template<typename IIter, typename OIter>
333  bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
335  ASTImporter &ImporterRef = Importer;
336  bool Failed = false;
337  std::transform(Ibegin, Iend, Obegin,
338  [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
339  ItemT *To = cast_or_null<ItemT>(
340  ImporterRef.Import(From));
341  if (!To && From)
342  Failed = true;
343  return To;
344  });
345  return Failed;
346  }
347 
348  template<typename InContainerTy, typename OutContainerTy>
349  bool ImportContainerChecked(const InContainerTy &InContainer,
350  OutContainerTy &OutContainer) {
351  return ImportArrayChecked(InContainer.begin(), InContainer.end(),
352  OutContainer.begin());
353  }
354 
355  template<typename InContainerTy, typename OIter>
356  bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
357  return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
358  }
359 
360  // Importing overrides.
361  void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
362  };
363 
364 
365 template <typename InContainerTy>
367  SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
368  const InContainerTy &Container, TemplateArgumentListInfo &Result) {
369  TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc),
370  Importer.Import(FromRAngleLoc));
371  if (ImportTemplateArgumentListInfo(Container, ToTAInfo))
372  return true;
373  Result = ToTAInfo;
374  return false;
375 }
376 
377 template <>
378 bool ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
381  From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
382 }
383 
384 template <>
388  return ImportTemplateArgumentListInfo(From.LAngleLoc, From.RAngleLoc,
389  From.arguments(), Result);
390 }
391 
392 } // end namespace clang
393 
394 //----------------------------------------------------------------------------
395 // Import Types
396 //----------------------------------------------------------------------------
397 
398 using namespace clang;
399 
401  Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
402  << T->getTypeClassName();
403  return QualType();
404 }
405 
407  QualType UnderlyingType = Importer.Import(T->getValueType());
408  if(UnderlyingType.isNull())
409  return QualType();
410 
411  return Importer.getToContext().getAtomicType(UnderlyingType);
412 }
413 
415  switch (T->getKind()) {
416 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
417  case BuiltinType::Id: \
418  return Importer.getToContext().SingletonId;
419 #include "clang/Basic/OpenCLImageTypes.def"
420 #define SHARED_SINGLETON_TYPE(Expansion)
421 #define BUILTIN_TYPE(Id, SingletonId) \
422  case BuiltinType::Id: return Importer.getToContext().SingletonId;
423 #include "clang/AST/BuiltinTypes.def"
424 
425  // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
426  // context supports C++.
427 
428  // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
429  // context supports ObjC.
430 
431  case BuiltinType::Char_U:
432  // The context we're importing from has an unsigned 'char'. If we're
433  // importing into a context with a signed 'char', translate to
434  // 'unsigned char' instead.
435  if (Importer.getToContext().getLangOpts().CharIsSigned)
436  return Importer.getToContext().UnsignedCharTy;
437 
438  return Importer.getToContext().CharTy;
439 
440  case BuiltinType::Char_S:
441  // The context we're importing from has an unsigned 'char'. If we're
442  // importing into a context with a signed 'char', translate to
443  // 'unsigned char' instead.
444  if (!Importer.getToContext().getLangOpts().CharIsSigned)
445  return Importer.getToContext().SignedCharTy;
446 
447  return Importer.getToContext().CharTy;
448 
449  case BuiltinType::WChar_S:
450  case BuiltinType::WChar_U:
451  // FIXME: If not in C++, shall we translate to the C equivalent of
452  // wchar_t?
453  return Importer.getToContext().WCharTy;
454  }
455 
456  llvm_unreachable("Invalid BuiltinType Kind!");
457 }
458 
460  QualType OrigT = Importer.Import(T->getOriginalType());
461  if (OrigT.isNull())
462  return QualType();
463 
464  return Importer.getToContext().getDecayedType(OrigT);
465 }
466 
468  QualType ToElementType = Importer.Import(T->getElementType());
469  if (ToElementType.isNull())
470  return QualType();
471 
472  return Importer.getToContext().getComplexType(ToElementType);
473 }
474 
476  QualType ToPointeeType = Importer.Import(T->getPointeeType());
477  if (ToPointeeType.isNull())
478  return QualType();
479 
480  return Importer.getToContext().getPointerType(ToPointeeType);
481 }
482 
484  // FIXME: Check for blocks support in "to" context.
485  QualType ToPointeeType = Importer.Import(T->getPointeeType());
486  if (ToPointeeType.isNull())
487  return QualType();
488 
489  return Importer.getToContext().getBlockPointerType(ToPointeeType);
490 }
491 
492 QualType
494  // FIXME: Check for C++ support in "to" context.
495  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
496  if (ToPointeeType.isNull())
497  return QualType();
498 
499  return Importer.getToContext().getLValueReferenceType(ToPointeeType);
500 }
501 
502 QualType
504  // FIXME: Check for C++0x support in "to" context.
505  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
506  if (ToPointeeType.isNull())
507  return QualType();
508 
509  return Importer.getToContext().getRValueReferenceType(ToPointeeType);
510 }
511 
513  // FIXME: Check for C++ support in "to" context.
514  QualType ToPointeeType = Importer.Import(T->getPointeeType());
515  if (ToPointeeType.isNull())
516  return QualType();
517 
518  QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
519  return Importer.getToContext().getMemberPointerType(ToPointeeType,
520  ClassType.getTypePtr());
521 }
522 
524  QualType ToElementType = Importer.Import(T->getElementType());
525  if (ToElementType.isNull())
526  return QualType();
527 
528  return Importer.getToContext().getConstantArrayType(ToElementType,
529  T->getSize(),
530  T->getSizeModifier(),
532 }
533 
534 QualType
536  QualType ToElementType = Importer.Import(T->getElementType());
537  if (ToElementType.isNull())
538  return QualType();
539 
540  return Importer.getToContext().getIncompleteArrayType(ToElementType,
541  T->getSizeModifier(),
543 }
544 
546  QualType ToElementType = Importer.Import(T->getElementType());
547  if (ToElementType.isNull())
548  return QualType();
549 
550  Expr *Size = Importer.Import(T->getSizeExpr());
551  if (!Size)
552  return QualType();
553 
554  SourceRange Brackets = Importer.Import(T->getBracketsRange());
555  return Importer.getToContext().getVariableArrayType(ToElementType, Size,
556  T->getSizeModifier(),
558  Brackets);
559 }
560 
562  const DependentSizedArrayType *T) {
563  QualType ToElementType = Importer.Import(T->getElementType());
564  if (ToElementType.isNull())
565  return QualType();
566 
567  // SizeExpr may be null if size is not specified directly.
568  // For example, 'int a[]'.
569  Expr *Size = Importer.Import(T->getSizeExpr());
570  if (!Size && T->getSizeExpr())
571  return QualType();
572 
573  SourceRange Brackets = Importer.Import(T->getBracketsRange());
574  return Importer.getToContext().getDependentSizedArrayType(
575  ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(),
576  Brackets);
577 }
578 
580  QualType ToElementType = Importer.Import(T->getElementType());
581  if (ToElementType.isNull())
582  return QualType();
583 
584  return Importer.getToContext().getVectorType(ToElementType,
585  T->getNumElements(),
586  T->getVectorKind());
587 }
588 
590  QualType ToElementType = Importer.Import(T->getElementType());
591  if (ToElementType.isNull())
592  return QualType();
593 
594  return Importer.getToContext().getExtVectorType(ToElementType,
595  T->getNumElements());
596 }
597 
598 QualType
600  // FIXME: What happens if we're importing a function without a prototype
601  // into C++? Should we make it variadic?
602  QualType ToResultType = Importer.Import(T->getReturnType());
603  if (ToResultType.isNull())
604  return QualType();
605 
606  return Importer.getToContext().getFunctionNoProtoType(ToResultType,
607  T->getExtInfo());
608 }
609 
611  QualType ToResultType = Importer.Import(T->getReturnType());
612  if (ToResultType.isNull())
613  return QualType();
614 
615  // Import argument types
616  SmallVector<QualType, 4> ArgTypes;
617  for (const auto &A : T->param_types()) {
618  QualType ArgType = Importer.Import(A);
619  if (ArgType.isNull())
620  return QualType();
621  ArgTypes.push_back(ArgType);
622  }
623 
624  // Import exception types
625  SmallVector<QualType, 4> ExceptionTypes;
626  for (const auto &E : T->exceptions()) {
627  QualType ExceptionType = Importer.Import(E);
628  if (ExceptionType.isNull())
629  return QualType();
630  ExceptionTypes.push_back(ExceptionType);
631  }
632 
635 
636  ToEPI.ExtInfo = FromEPI.ExtInfo;
637  ToEPI.Variadic = FromEPI.Variadic;
638  ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
639  ToEPI.TypeQuals = FromEPI.TypeQuals;
640  ToEPI.RefQualifier = FromEPI.RefQualifier;
641  ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
642  ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
644  Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
645  ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
646  Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
647  ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
648  Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
649 
650  return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
651 }
652 
654  const UnresolvedUsingType *T) {
655  UnresolvedUsingTypenameDecl *ToD = cast_or_null<UnresolvedUsingTypenameDecl>(
656  Importer.Import(T->getDecl()));
657  if (!ToD)
658  return QualType();
659 
660  UnresolvedUsingTypenameDecl *ToPrevD =
661  cast_or_null<UnresolvedUsingTypenameDecl>(
662  Importer.Import(T->getDecl()->getPreviousDecl()));
663  if (!ToPrevD && T->getDecl()->getPreviousDecl())
664  return QualType();
665 
666  return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
667 }
668 
670  QualType ToInnerType = Importer.Import(T->getInnerType());
671  if (ToInnerType.isNull())
672  return QualType();
673 
674  return Importer.getToContext().getParenType(ToInnerType);
675 }
676 
678  TypedefNameDecl *ToDecl
679  = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
680  if (!ToDecl)
681  return QualType();
682 
683  return Importer.getToContext().getTypeDeclType(ToDecl);
684 }
685 
687  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
688  if (!ToExpr)
689  return QualType();
690 
691  return Importer.getToContext().getTypeOfExprType(ToExpr);
692 }
693 
695  QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
696  if (ToUnderlyingType.isNull())
697  return QualType();
698 
699  return Importer.getToContext().getTypeOfType(ToUnderlyingType);
700 }
701 
703  // FIXME: Make sure that the "to" context supports C++0x!
704  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
705  if (!ToExpr)
706  return QualType();
707 
708  QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
709  if (UnderlyingType.isNull())
710  return QualType();
711 
712  return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
713 }
714 
716  QualType ToBaseType = Importer.Import(T->getBaseType());
717  QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
718  if (ToBaseType.isNull() || ToUnderlyingType.isNull())
719  return QualType();
720 
721  return Importer.getToContext().getUnaryTransformType(ToBaseType,
722  ToUnderlyingType,
723  T->getUTTKind());
724 }
725 
727  // FIXME: Make sure that the "to" context supports C++11!
728  QualType FromDeduced = T->getDeducedType();
729  QualType ToDeduced;
730  if (!FromDeduced.isNull()) {
731  ToDeduced = Importer.Import(FromDeduced);
732  if (ToDeduced.isNull())
733  return QualType();
734  }
735 
736  return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
737  /*IsDependent*/false);
738 }
739 
741  const InjectedClassNameType *T) {
742  CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
743  if (!D)
744  return QualType();
745 
746  QualType InjType = Importer.Import(T->getInjectedSpecializationType());
747  if (InjType.isNull())
748  return QualType();
749 
750  // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
751  // See comments in InjectedClassNameType definition for details
752  // return Importer.getToContext().getInjectedClassNameType(D, InjType);
753  enum {
756  };
757 
758  return QualType(new (Importer.getToContext(), TypeAlignment)
759  InjectedClassNameType(D, InjType), 0);
760 }
761 
763  RecordDecl *ToDecl
764  = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
765  if (!ToDecl)
766  return QualType();
767 
768  return Importer.getToContext().getTagDeclType(ToDecl);
769 }
770 
772  EnumDecl *ToDecl
773  = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
774  if (!ToDecl)
775  return QualType();
776 
777  return Importer.getToContext().getTagDeclType(ToDecl);
778 }
779 
781  QualType FromModifiedType = T->getModifiedType();
782  QualType FromEquivalentType = T->getEquivalentType();
783  QualType ToModifiedType;
784  QualType ToEquivalentType;
785 
786  if (!FromModifiedType.isNull()) {
787  ToModifiedType = Importer.Import(FromModifiedType);
788  if (ToModifiedType.isNull())
789  return QualType();
790  }
791  if (!FromEquivalentType.isNull()) {
792  ToEquivalentType = Importer.Import(FromEquivalentType);
793  if (ToEquivalentType.isNull())
794  return QualType();
795  }
796 
797  return Importer.getToContext().getAttributedType(T->getAttrKind(),
798  ToModifiedType, ToEquivalentType);
799 }
800 
801 
803  const TemplateTypeParmType *T) {
804  TemplateTypeParmDecl *ParmDecl =
805  cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
806  if (!ParmDecl && T->getDecl())
807  return QualType();
808 
809  return Importer.getToContext().getTemplateTypeParmType(
810  T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
811 }
812 
814  const SubstTemplateTypeParmType *T) {
815  const TemplateTypeParmType *Replaced =
816  cast_or_null<TemplateTypeParmType>(Importer.Import(
817  QualType(T->getReplacedParameter(), 0)).getTypePtr());
818  if (!Replaced)
819  return QualType();
820 
821  QualType Replacement = Importer.Import(T->getReplacementType());
822  if (Replacement.isNull())
823  return QualType();
824  Replacement = Replacement.getCanonicalType();
825 
826  return Importer.getToContext().getSubstTemplateTypeParmType(
827  Replaced, Replacement);
828 }
829 
831  const TemplateSpecializationType *T) {
832  TemplateName ToTemplate = Importer.Import(T->getTemplateName());
833  if (ToTemplate.isNull())
834  return QualType();
835 
836  SmallVector<TemplateArgument, 2> ToTemplateArgs;
837  if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
838  return QualType();
839 
840  QualType ToCanonType;
841  if (!QualType(T, 0).isCanonical()) {
842  QualType FromCanonType
843  = Importer.getFromContext().getCanonicalType(QualType(T, 0));
844  ToCanonType =Importer.Import(FromCanonType);
845  if (ToCanonType.isNull())
846  return QualType();
847  }
848  return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
849  ToTemplateArgs,
850  ToCanonType);
851 }
852 
854  NestedNameSpecifier *ToQualifier = nullptr;
855  // Note: the qualifier in an ElaboratedType is optional.
856  if (T->getQualifier()) {
857  ToQualifier = Importer.Import(T->getQualifier());
858  if (!ToQualifier)
859  return QualType();
860  }
861 
862  QualType ToNamedType = Importer.Import(T->getNamedType());
863  if (ToNamedType.isNull())
864  return QualType();
865 
866  return Importer.getToContext().getElaboratedType(T->getKeyword(),
867  ToQualifier, ToNamedType);
868 }
869 
871  QualType Pattern = Importer.Import(T->getPattern());
872  if (Pattern.isNull())
873  return QualType();
874 
875  return Importer.getToContext().getPackExpansionType(Pattern,
876  T->getNumExpansions());
877 }
878 
881  NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier());
882  if (!Qualifier && T->getQualifier())
883  return QualType();
884 
885  IdentifierInfo *Name = Importer.Import(T->getIdentifier());
886  if (!Name && T->getIdentifier())
887  return QualType();
888 
890  ToPack.reserve(T->getNumArgs());
891  if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack))
892  return QualType();
893 
894  return Importer.getToContext().getDependentTemplateSpecializationType(
895  T->getKeyword(), Qualifier, Name, ToPack);
896 }
897 
899  ObjCInterfaceDecl *Class
900  = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
901  if (!Class)
902  return QualType();
903 
904  return Importer.getToContext().getObjCInterfaceType(Class);
905 }
906 
908  QualType ToBaseType = Importer.Import(T->getBaseType());
909  if (ToBaseType.isNull())
910  return QualType();
911 
912  SmallVector<QualType, 4> TypeArgs;
913  for (auto TypeArg : T->getTypeArgsAsWritten()) {
914  QualType ImportedTypeArg = Importer.Import(TypeArg);
915  if (ImportedTypeArg.isNull())
916  return QualType();
917 
918  TypeArgs.push_back(ImportedTypeArg);
919  }
920 
922  for (auto *P : T->quals()) {
923  ObjCProtocolDecl *Protocol
924  = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
925  if (!Protocol)
926  return QualType();
927  Protocols.push_back(Protocol);
928  }
929 
930  return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
931  Protocols,
932  T->isKindOfTypeAsWritten());
933 }
934 
935 QualType
937  QualType ToPointeeType = Importer.Import(T->getPointeeType());
938  if (ToPointeeType.isNull())
939  return QualType();
940 
941  return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
942 }
943 
944 //----------------------------------------------------------------------------
945 // Import Declarations
946 //----------------------------------------------------------------------------
948  DeclContext *&LexicalDC,
949  DeclarationName &Name,
950  NamedDecl *&ToD,
951  SourceLocation &Loc) {
952  // Import the context of this declaration.
953  DC = Importer.ImportContext(D->getDeclContext());
954  if (!DC)
955  return true;
956 
957  LexicalDC = DC;
958  if (D->getDeclContext() != D->getLexicalDeclContext()) {
959  LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
960  if (!LexicalDC)
961  return true;
962  }
963 
964  // Import the name of this declaration.
965  Name = Importer.Import(D->getDeclName());
966  if (D->getDeclName() && !Name)
967  return true;
968 
969  // Import the location of this declaration.
970  Loc = Importer.Import(D->getLocation());
971  ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
972  return false;
973 }
974 
976  if (!FromD)
977  return;
978 
979  if (!ToD) {
980  ToD = Importer.Import(FromD);
981  if (!ToD)
982  return;
983  }
984 
985  if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
986  if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
987  if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
988  ImportDefinition(FromRecord, ToRecord);
989  }
990  }
991  return;
992  }
993 
994  if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
995  if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
996  if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
997  ImportDefinition(FromEnum, ToEnum);
998  }
999  }
1000  return;
1001  }
1002 }
1003 
1004 void
1006  DeclarationNameInfo& To) {
1007  // NOTE: To.Name and To.Loc are already imported.
1008  // We only have to import To.LocInfo.
1009  switch (To.getName().getNameKind()) {
1016  return;
1017 
1019  SourceRange Range = From.getCXXOperatorNameRange();
1020  To.setCXXOperatorNameRange(Importer.Import(Range));
1021  return;
1022  }
1025  To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1026  return;
1027  }
1031  TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1032  To.setNamedTypeInfo(Importer.Import(FromTInfo));
1033  return;
1034  }
1035  }
1036  llvm_unreachable("Unknown name kind.");
1037 }
1038 
1039 void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1040  if (Importer.isMinimalImport() && !ForceImport) {
1041  Importer.ImportContext(FromDC);
1042  return;
1043  }
1044 
1045  for (auto *From : FromDC->decls())
1046  Importer.Import(From);
1047 }
1048 
1051  if (To->getDefinition() || To->isBeingDefined()) {
1052  if (Kind == IDK_Everything)
1053  ImportDeclContext(From, /*ForceImport=*/true);
1054 
1055  return false;
1056  }
1057 
1058  To->startDefinition();
1059 
1060  // Add base classes.
1061  if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1062  CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1063 
1064  struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1065  struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1066  ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1067  ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
1068  ToData.Aggregate = FromData.Aggregate;
1069  ToData.PlainOldData = FromData.PlainOldData;
1070  ToData.Empty = FromData.Empty;
1071  ToData.Polymorphic = FromData.Polymorphic;
1072  ToData.Abstract = FromData.Abstract;
1073  ToData.IsStandardLayout = FromData.IsStandardLayout;
1074  ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1075  ToData.HasPrivateFields = FromData.HasPrivateFields;
1076  ToData.HasProtectedFields = FromData.HasProtectedFields;
1077  ToData.HasPublicFields = FromData.HasPublicFields;
1078  ToData.HasMutableFields = FromData.HasMutableFields;
1079  ToData.HasVariantMembers = FromData.HasVariantMembers;
1080  ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
1081  ToData.HasInClassInitializer = FromData.HasInClassInitializer;
1082  ToData.HasUninitializedReferenceMember
1083  = FromData.HasUninitializedReferenceMember;
1084  ToData.HasUninitializedFields = FromData.HasUninitializedFields;
1085  ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1086  ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
1087  ToData.NeedOverloadResolutionForCopyConstructor
1088  = FromData.NeedOverloadResolutionForCopyConstructor;
1089  ToData.NeedOverloadResolutionForMoveConstructor
1090  = FromData.NeedOverloadResolutionForMoveConstructor;
1091  ToData.NeedOverloadResolutionForMoveAssignment
1092  = FromData.NeedOverloadResolutionForMoveAssignment;
1093  ToData.NeedOverloadResolutionForDestructor
1094  = FromData.NeedOverloadResolutionForDestructor;
1095  ToData.DefaultedCopyConstructorIsDeleted
1096  = FromData.DefaultedCopyConstructorIsDeleted;
1097  ToData.DefaultedMoveConstructorIsDeleted
1098  = FromData.DefaultedMoveConstructorIsDeleted;
1099  ToData.DefaultedMoveAssignmentIsDeleted
1100  = FromData.DefaultedMoveAssignmentIsDeleted;
1101  ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
1102  ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1103  ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
1104  ToData.HasConstexprNonCopyMoveConstructor
1105  = FromData.HasConstexprNonCopyMoveConstructor;
1106  ToData.HasDefaultedDefaultConstructor
1107  = FromData.HasDefaultedDefaultConstructor;
1108  ToData.CanPassInRegisters = FromData.CanPassInRegisters;
1109  ToData.DefaultedDefaultConstructorIsConstexpr
1110  = FromData.DefaultedDefaultConstructorIsConstexpr;
1111  ToData.HasConstexprDefaultConstructor
1112  = FromData.HasConstexprDefaultConstructor;
1113  ToData.HasNonLiteralTypeFieldsOrBases
1114  = FromData.HasNonLiteralTypeFieldsOrBases;
1115  // ComputedVisibleConversions not imported.
1116  ToData.UserProvidedDefaultConstructor
1117  = FromData.UserProvidedDefaultConstructor;
1118  ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
1119  ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1120  = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1121  ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1122  = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
1123  ToData.ImplicitCopyAssignmentHasConstParam
1124  = FromData.ImplicitCopyAssignmentHasConstParam;
1125  ToData.HasDeclaredCopyConstructorWithConstParam
1126  = FromData.HasDeclaredCopyConstructorWithConstParam;
1127  ToData.HasDeclaredCopyAssignmentWithConstParam
1128  = FromData.HasDeclaredCopyAssignmentWithConstParam;
1129 
1131  for (const auto &Base1 : FromCXX->bases()) {
1132  QualType T = Importer.Import(Base1.getType());
1133  if (T.isNull())
1134  return true;
1135 
1136  SourceLocation EllipsisLoc;
1137  if (Base1.isPackExpansion())
1138  EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
1139 
1140  // Ensure that we have a definition for the base.
1141  ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
1142 
1143  Bases.push_back(
1144  new (Importer.getToContext())
1145  CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1146  Base1.isVirtual(),
1147  Base1.isBaseOfClass(),
1148  Base1.getAccessSpecifierAsWritten(),
1149  Importer.Import(Base1.getTypeSourceInfo()),
1150  EllipsisLoc));
1151  }
1152  if (!Bases.empty())
1153  ToCXX->setBases(Bases.data(), Bases.size());
1154  }
1155 
1156  if (shouldForceImportDeclContext(Kind))
1157  ImportDeclContext(From, /*ForceImport=*/true);
1158 
1159  To->completeDefinition();
1160  return false;
1161 }
1162 
1165  if (To->getAnyInitializer())
1166  return false;
1167 
1168  // FIXME: Can we really import any initializer? Alternatively, we could force
1169  // ourselves to import every declaration of a variable and then only use
1170  // getInit() here.
1171  To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1172 
1173  // FIXME: Other bits to merge?
1174 
1175  return false;
1176 }
1177 
1180  if (To->getDefinition() || To->isBeingDefined()) {
1181  if (Kind == IDK_Everything)
1182  ImportDeclContext(From, /*ForceImport=*/true);
1183  return false;
1184  }
1185 
1186  To->startDefinition();
1187 
1188  QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1189  if (T.isNull())
1190  return true;
1191 
1192  QualType ToPromotionType = Importer.Import(From->getPromotionType());
1193  if (ToPromotionType.isNull())
1194  return true;
1195 
1196  if (shouldForceImportDeclContext(Kind))
1197  ImportDeclContext(From, /*ForceImport=*/true);
1198 
1199  // FIXME: we might need to merge the number of positive or negative bits
1200  // if the enumerator lists don't match.
1201  To->completeDefinition(T, ToPromotionType,
1202  From->getNumPositiveBits(),
1203  From->getNumNegativeBits());
1204  return false;
1205 }
1206 
1208  TemplateParameterList *Params) {
1209  SmallVector<NamedDecl *, 4> ToParams(Params->size());
1210  if (ImportContainerChecked(*Params, ToParams))
1211  return nullptr;
1212 
1213  Expr *ToRequiresClause;
1214  if (Expr *const R = Params->getRequiresClause()) {
1215  ToRequiresClause = Importer.Import(R);
1216  if (!ToRequiresClause)
1217  return nullptr;
1218  } else {
1219  ToRequiresClause = nullptr;
1220  }
1221 
1222  return TemplateParameterList::Create(Importer.getToContext(),
1223  Importer.Import(Params->getTemplateLoc()),
1224  Importer.Import(Params->getLAngleLoc()),
1225  ToParams,
1226  Importer.Import(Params->getRAngleLoc()),
1227  ToRequiresClause);
1228 }
1229 
1232  switch (From.getKind()) {
1234  return TemplateArgument();
1235 
1236  case TemplateArgument::Type: {
1237  QualType ToType = Importer.Import(From.getAsType());
1238  if (ToType.isNull())
1239  return TemplateArgument();
1240  return TemplateArgument(ToType);
1241  }
1242 
1244  QualType ToType = Importer.Import(From.getIntegralType());
1245  if (ToType.isNull())
1246  return TemplateArgument();
1247  return TemplateArgument(From, ToType);
1248  }
1249 
1251  ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
1252  QualType ToType = Importer.Import(From.getParamTypeForDecl());
1253  if (!To || ToType.isNull())
1254  return TemplateArgument();
1255  return TemplateArgument(To, ToType);
1256  }
1257 
1259  QualType ToType = Importer.Import(From.getNullPtrType());
1260  if (ToType.isNull())
1261  return TemplateArgument();
1262  return TemplateArgument(ToType, /*isNullPtr*/true);
1263  }
1264 
1266  TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1267  if (ToTemplate.isNull())
1268  return TemplateArgument();
1269 
1270  return TemplateArgument(ToTemplate);
1271  }
1272 
1274  TemplateName ToTemplate
1275  = Importer.Import(From.getAsTemplateOrTemplatePattern());
1276  if (ToTemplate.isNull())
1277  return TemplateArgument();
1278 
1279  return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
1280  }
1281 
1283  if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1284  return TemplateArgument(ToExpr);
1285  return TemplateArgument();
1286 
1287  case TemplateArgument::Pack: {
1289  ToPack.reserve(From.pack_size());
1290  if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1291  return TemplateArgument();
1292 
1293  return TemplateArgument(
1294  llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
1295  }
1296  }
1297 
1298  llvm_unreachable("Invalid template argument kind");
1299 }
1300 
1304  TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1305  TemplateArgumentLocInfo ToInfo;
1306  if (Arg.getKind() == TemplateArgument::Expression) {
1307  Expr *E = Importer.Import(FromInfo.getAsExpr());
1308  ToInfo = TemplateArgumentLocInfo(E);
1309  if (!E)
1310  return None;
1311  } else if (Arg.getKind() == TemplateArgument::Type) {
1312  if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1313  ToInfo = TemplateArgumentLocInfo(TSI);
1314  else
1315  return None;
1316  } else {
1317  ToInfo = TemplateArgumentLocInfo(
1318  Importer.Import(FromInfo.getTemplateQualifierLoc()),
1319  Importer.Import(FromInfo.getTemplateNameLoc()),
1320  Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1321  }
1322  return TemplateArgumentLoc(Arg, ToInfo);
1323 }
1324 
1326  unsigned NumFromArgs,
1328  for (unsigned I = 0; I != NumFromArgs; ++I) {
1329  TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1330  if (To.isNull() && !FromArgs[I].isNull())
1331  return true;
1332 
1333  ToArgs.push_back(To);
1334  }
1335 
1336  return false;
1337 }
1338 
1339 // We cannot use Optional<> pattern here and below because
1340 // TemplateArgumentListInfo's operator new is declared as deleted so it cannot
1341 // be stored in Optional.
1342 template <typename InContainerTy>
1344  const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1345  for (const auto &FromLoc : Container) {
1346  if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1347  ToTAInfo.addArgument(*ToLoc);
1348  else
1349  return true;
1350  }
1351  return false;
1352 }
1353 
1355  RecordDecl *ToRecord, bool Complain) {
1356  // Eliminate a potential failure point where we attempt to re-import
1357  // something we're trying to import while completing ToRecord.
1358  Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1359  if (ToOrigin) {
1360  RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
1361  if (ToOriginRecord)
1362  ToRecord = ToOriginRecord;
1363  }
1364 
1365  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1366  ToRecord->getASTContext(),
1367  Importer.getNonEquivalentDecls(),
1368  false, Complain);
1369  return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
1370 }
1371 
1373  bool Complain) {
1375  Importer.getFromContext(), Importer.getToContext(),
1376  Importer.getNonEquivalentDecls(), false, Complain);
1377  return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1378 }
1379 
1381  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1382  Importer.getToContext(),
1383  Importer.getNonEquivalentDecls());
1384  return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
1385 }
1386 
1388  FunctionTemplateDecl *To) {
1390  Importer.getFromContext(), Importer.getToContext(),
1391  Importer.getNonEquivalentDecls(), false, false);
1392  return Ctx.IsStructurallyEquivalent(From, To);
1393 }
1394 
1396  EnumConstantDecl *ToEC)
1397 {
1398  const llvm::APSInt &FromVal = FromEC->getInitVal();
1399  const llvm::APSInt &ToVal = ToEC->getInitVal();
1400 
1401  return FromVal.isSigned() == ToVal.isSigned() &&
1402  FromVal.getBitWidth() == ToVal.getBitWidth() &&
1403  FromVal == ToVal;
1404 }
1405 
1407  ClassTemplateDecl *To) {
1408  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1409  Importer.getToContext(),
1410  Importer.getNonEquivalentDecls());
1411  return Ctx.IsStructurallyEquivalent(From, To);
1412 }
1413 
1415  VarTemplateDecl *To) {
1416  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1417  Importer.getToContext(),
1418  Importer.getNonEquivalentDecls());
1419  return Ctx.IsStructurallyEquivalent(From, To);
1420 }
1421 
1423  Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1424  << D->getDeclKindName();
1425  return nullptr;
1426 }
1427 
1429  // Import the context of this declaration.
1430  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1431  if (!DC)
1432  return nullptr;
1433 
1434  DeclContext *LexicalDC = DC;
1435  if (D->getDeclContext() != D->getLexicalDeclContext()) {
1436  LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1437  if (!LexicalDC)
1438  return nullptr;
1439  }
1440 
1441  // Import the location of this declaration.
1442  SourceLocation Loc = Importer.Import(D->getLocation());
1443 
1444  EmptyDecl *ToD = EmptyDecl::Create(Importer.getToContext(), DC, Loc);
1445  ToD->setLexicalDeclContext(LexicalDC);
1446  Importer.Imported(D, ToD);
1447  LexicalDC->addDeclInternal(ToD);
1448  return ToD;
1449 }
1450 
1452  TranslationUnitDecl *ToD =
1453  Importer.getToContext().getTranslationUnitDecl();
1454 
1455  Importer.Imported(D, ToD);
1456 
1457  return ToD;
1458 }
1459 
1461 
1462  SourceLocation Loc = Importer.Import(D->getLocation());
1463  SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1464 
1465  // Import the context of this declaration.
1466  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1467  if (!DC)
1468  return nullptr;
1469 
1471  = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1472  DC, Loc, ColonLoc);
1473 
1474  if (!accessSpecDecl)
1475  return nullptr;
1476 
1477  // Lexical DeclContext and Semantic DeclContext
1478  // is always the same for the accessSpec.
1479  accessSpecDecl->setLexicalDeclContext(DC);
1480  DC->addDeclInternal(accessSpecDecl);
1481 
1482  return accessSpecDecl;
1483 }
1484 
1486  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1487  if (!DC)
1488  return nullptr;
1489 
1490  DeclContext *LexicalDC = DC;
1491 
1492  // Import the location of this declaration.
1493  SourceLocation Loc = Importer.Import(D->getLocation());
1494 
1495  Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1496  if (!AssertExpr)
1497  return nullptr;
1498 
1499  StringLiteral *FromMsg = D->getMessage();
1500  StringLiteral *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
1501  if (!ToMsg && FromMsg)
1502  return nullptr;
1503 
1505  Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1506  Importer.Import(D->getRParenLoc()), D->isFailed());
1507 
1508  ToD->setLexicalDeclContext(LexicalDC);
1509  LexicalDC->addDeclInternal(ToD);
1510  Importer.Imported(D, ToD);
1511  return ToD;
1512 }
1513 
1515  // Import the major distinguishing characteristics of this namespace.
1516  DeclContext *DC, *LexicalDC;
1517  DeclarationName Name;
1518  SourceLocation Loc;
1519  NamedDecl *ToD;
1520  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1521  return nullptr;
1522  if (ToD)
1523  return ToD;
1524 
1525  NamespaceDecl *MergeWithNamespace = nullptr;
1526  if (!Name) {
1527  // This is an anonymous namespace. Adopt an existing anonymous
1528  // namespace if we can.
1529  // FIXME: Not testable.
1530  if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1531  MergeWithNamespace = TU->getAnonymousNamespace();
1532  else
1533  MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1534  } else {
1535  SmallVector<NamedDecl *, 4> ConflictingDecls;
1536  SmallVector<NamedDecl *, 2> FoundDecls;
1537  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1538  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1539  if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
1540  continue;
1541 
1542  if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
1543  MergeWithNamespace = FoundNS;
1544  ConflictingDecls.clear();
1545  break;
1546  }
1547 
1548  ConflictingDecls.push_back(FoundDecls[I]);
1549  }
1550 
1551  if (!ConflictingDecls.empty()) {
1552  Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
1553  ConflictingDecls.data(),
1554  ConflictingDecls.size());
1555  }
1556  }
1557 
1558  // Create the "to" namespace, if needed.
1559  NamespaceDecl *ToNamespace = MergeWithNamespace;
1560  if (!ToNamespace) {
1561  ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
1562  D->isInline(),
1563  Importer.Import(D->getLocStart()),
1564  Loc, Name.getAsIdentifierInfo(),
1565  /*PrevDecl=*/nullptr);
1566  ToNamespace->setLexicalDeclContext(LexicalDC);
1567  LexicalDC->addDeclInternal(ToNamespace);
1568 
1569  // If this is an anonymous namespace, register it as the anonymous
1570  // namespace within its context.
1571  if (!Name) {
1572  if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1573  TU->setAnonymousNamespace(ToNamespace);
1574  else
1575  cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1576  }
1577  }
1578  Importer.Imported(D, ToNamespace);
1579 
1580  ImportDeclContext(D);
1581 
1582  return ToNamespace;
1583 }
1584 
1586  // Import the major distinguishing characteristics of this namespace.
1587  DeclContext *DC, *LexicalDC;
1588  DeclarationName Name;
1589  SourceLocation Loc;
1590  NamedDecl *LookupD;
1591  if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1592  return nullptr;
1593  if (LookupD)
1594  return LookupD;
1595 
1596  // NOTE: No conflict resolution is done for namespace aliases now.
1597 
1598  NamespaceDecl *TargetDecl = cast_or_null<NamespaceDecl>(
1599  Importer.Import(D->getNamespace()));
1600  if (!TargetDecl)
1601  return nullptr;
1602 
1603  IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1604  if (!ToII)
1605  return nullptr;
1606 
1607  NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1608  if (D->getQualifierLoc() && !ToQLoc)
1609  return nullptr;
1610 
1612  Importer.getToContext(), DC, Importer.Import(D->getNamespaceLoc()),
1613  Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1614  Importer.Import(D->getTargetNameLoc()), TargetDecl);
1615 
1616  ToD->setLexicalDeclContext(LexicalDC);
1617  Importer.Imported(D, ToD);
1618  LexicalDC->addDeclInternal(ToD);
1619 
1620  return ToD;
1621 }
1622 
1624  // Import the major distinguishing characteristics of this typedef.
1625  DeclContext *DC, *LexicalDC;
1626  DeclarationName Name;
1627  SourceLocation Loc;
1628  NamedDecl *ToD;
1629  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1630  return nullptr;
1631  if (ToD)
1632  return ToD;
1633 
1634  // If this typedef is not in block scope, determine whether we've
1635  // seen a typedef with the same name (that we can merge with) or any
1636  // other entity by that name (which name lookup could conflict with).
1637  if (!DC->isFunctionOrMethod()) {
1638  SmallVector<NamedDecl *, 4> ConflictingDecls;
1639  unsigned IDNS = Decl::IDNS_Ordinary;
1640  SmallVector<NamedDecl *, 2> FoundDecls;
1641  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1642  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1643  if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1644  continue;
1645  if (TypedefNameDecl *FoundTypedef =
1646  dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
1647  if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1648  FoundTypedef->getUnderlyingType()))
1649  return Importer.Imported(D, FoundTypedef);
1650  }
1651 
1652  ConflictingDecls.push_back(FoundDecls[I]);
1653  }
1654 
1655  if (!ConflictingDecls.empty()) {
1656  Name = Importer.HandleNameConflict(Name, DC, IDNS,
1657  ConflictingDecls.data(),
1658  ConflictingDecls.size());
1659  if (!Name)
1660  return nullptr;
1661  }
1662  }
1663 
1664  // Import the underlying type of this typedef;
1665  QualType T = Importer.Import(D->getUnderlyingType());
1666  if (T.isNull())
1667  return nullptr;
1668 
1669  // Create the new typedef node.
1670  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1671  SourceLocation StartL = Importer.Import(D->getLocStart());
1672  TypedefNameDecl *ToTypedef;
1673  if (IsAlias)
1674  ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, StartL, Loc,
1675  Name.getAsIdentifierInfo(), TInfo);
1676  else
1677  ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1678  StartL, Loc,
1679  Name.getAsIdentifierInfo(),
1680  TInfo);
1681 
1682  ToTypedef->setAccess(D->getAccess());
1683  ToTypedef->setLexicalDeclContext(LexicalDC);
1684  Importer.Imported(D, ToTypedef);
1685 
1686  // Templated declarations should not appear in DeclContext.
1687  TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
1688  if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
1689  LexicalDC->addDeclInternal(ToTypedef);
1690 
1691  return ToTypedef;
1692 }
1693 
1695  return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1696 }
1697 
1699  return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1700 }
1701 
1703  // Import the major distinguishing characteristics of this typedef.
1704  DeclContext *DC, *LexicalDC;
1705  DeclarationName Name;
1706  SourceLocation Loc;
1707  NamedDecl *FoundD;
1708  if (ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
1709  return nullptr;
1710  if (FoundD)
1711  return FoundD;
1712 
1713  // If this typedef is not in block scope, determine whether we've
1714  // seen a typedef with the same name (that we can merge with) or any
1715  // other entity by that name (which name lookup could conflict with).
1716  if (!DC->isFunctionOrMethod()) {
1717  SmallVector<NamedDecl *, 4> ConflictingDecls;
1718  unsigned IDNS = Decl::IDNS_Ordinary;
1719  SmallVector<NamedDecl *, 2> FoundDecls;
1720  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1721  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1722  if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1723  continue;
1724  if (auto *FoundAlias =
1725  dyn_cast<TypeAliasTemplateDecl>(FoundDecls[I]))
1726  return Importer.Imported(D, FoundAlias);
1727  ConflictingDecls.push_back(FoundDecls[I]);
1728  }
1729 
1730  if (!ConflictingDecls.empty()) {
1731  Name = Importer.HandleNameConflict(Name, DC, IDNS,
1732  ConflictingDecls.data(),
1733  ConflictingDecls.size());
1734  if (!Name)
1735  return nullptr;
1736  }
1737  }
1738 
1740  D->getTemplateParameters());
1741  if (!Params)
1742  return nullptr;
1743 
1744  auto *TemplDecl = cast_or_null<TypeAliasDecl>(
1745  Importer.Import(D->getTemplatedDecl()));
1746  if (!TemplDecl)
1747  return nullptr;
1748 
1750  Importer.getToContext(), DC, Loc, Name, Params, TemplDecl);
1751 
1752  TemplDecl->setDescribedAliasTemplate(ToAlias);
1753 
1754  ToAlias->setAccess(D->getAccess());
1755  ToAlias->setLexicalDeclContext(LexicalDC);
1756  Importer.Imported(D, ToAlias);
1757  LexicalDC->addDeclInternal(ToAlias);
1758  return ToAlias;
1759 }
1760 
1762  // Import the major distinguishing characteristics of this label.
1763  DeclContext *DC, *LexicalDC;
1764  DeclarationName Name;
1765  SourceLocation Loc;
1766  NamedDecl *ToD;
1767  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1768  return nullptr;
1769  if (ToD)
1770  return ToD;
1771 
1772  assert(LexicalDC->isFunctionOrMethod());
1773 
1774  LabelDecl *ToLabel = D->isGnuLocal()
1775  ? LabelDecl::Create(Importer.getToContext(),
1776  DC, Importer.Import(D->getLocation()),
1777  Name.getAsIdentifierInfo(),
1778  Importer.Import(D->getLocStart()))
1779  : LabelDecl::Create(Importer.getToContext(),
1780  DC, Importer.Import(D->getLocation()),
1781  Name.getAsIdentifierInfo());
1782  Importer.Imported(D, ToLabel);
1783 
1784  LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
1785  if (!Label)
1786  return nullptr;
1787 
1788  ToLabel->setStmt(Label);
1789  ToLabel->setLexicalDeclContext(LexicalDC);
1790  LexicalDC->addDeclInternal(ToLabel);
1791  return ToLabel;
1792 }
1793 
1795  // Import the major distinguishing characteristics of this enum.
1796  DeclContext *DC, *LexicalDC;
1797  DeclarationName Name;
1798  SourceLocation Loc;
1799  NamedDecl *ToD;
1800  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1801  return nullptr;
1802  if (ToD)
1803  return ToD;
1804 
1805  // Figure out what enum name we're looking for.
1806  unsigned IDNS = Decl::IDNS_Tag;
1807  DeclarationName SearchName = Name;
1808  if (!SearchName && D->getTypedefNameForAnonDecl()) {
1809  SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
1810  IDNS = Decl::IDNS_Ordinary;
1811  } else if (Importer.getToContext().getLangOpts().CPlusPlus)
1812  IDNS |= Decl::IDNS_Ordinary;
1813 
1814  // We may already have an enum of the same name; try to find and match it.
1815  if (!DC->isFunctionOrMethod() && SearchName) {
1816  SmallVector<NamedDecl *, 4> ConflictingDecls;
1817  SmallVector<NamedDecl *, 2> FoundDecls;
1818  DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
1819  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1820  if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1821  continue;
1822 
1823  Decl *Found = FoundDecls[I];
1824  if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
1825  if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1826  Found = Tag->getDecl();
1827  }
1828 
1829  if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
1830  if (IsStructuralMatch(D, FoundEnum))
1831  return Importer.Imported(D, FoundEnum);
1832  }
1833 
1834  ConflictingDecls.push_back(FoundDecls[I]);
1835  }
1836 
1837  if (!ConflictingDecls.empty()) {
1838  Name = Importer.HandleNameConflict(Name, DC, IDNS,
1839  ConflictingDecls.data(),
1840  ConflictingDecls.size());
1841  }
1842  }
1843 
1844  // Create the enum declaration.
1845  EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1846  Importer.Import(D->getLocStart()),
1847  Loc, Name.getAsIdentifierInfo(), nullptr,
1848  D->isScoped(), D->isScopedUsingClassTag(),
1849  D->isFixed());
1850  // Import the qualifier, if any.
1851  D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
1852  D2->setAccess(D->getAccess());
1853  D2->setLexicalDeclContext(LexicalDC);
1854  Importer.Imported(D, D2);
1855  LexicalDC->addDeclInternal(D2);
1856 
1857  // Import the integer type.
1858  QualType ToIntegerType = Importer.Import(D->getIntegerType());
1859  if (ToIntegerType.isNull())
1860  return nullptr;
1861  D2->setIntegerType(ToIntegerType);
1862 
1863  // Import the definition
1864  if (D->isCompleteDefinition() && ImportDefinition(D, D2))
1865  return nullptr;
1866 
1867  return D2;
1868 }
1869 
1871  // If this record has a definition in the translation unit we're coming from,
1872  // but this particular declaration is not that definition, import the
1873  // definition and map to that.
1874  TagDecl *Definition = D->getDefinition();
1875  if (Definition && Definition != D) {
1876  Decl *ImportedDef = Importer.Import(Definition);
1877  if (!ImportedDef)
1878  return nullptr;
1879 
1880  return Importer.Imported(D, ImportedDef);
1881  }
1882 
1883  // Import the major distinguishing characteristics of this record.
1884  DeclContext *DC, *LexicalDC;
1885  DeclarationName Name;
1886  SourceLocation Loc;
1887  NamedDecl *ToD;
1888  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1889  return nullptr;
1890  if (ToD)
1891  return ToD;
1892 
1893  // Figure out what structure name we're looking for.
1894  unsigned IDNS = Decl::IDNS_Tag;
1895  DeclarationName SearchName = Name;
1896  if (!SearchName && D->getTypedefNameForAnonDecl()) {
1897  SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
1898  IDNS = Decl::IDNS_Ordinary;
1899  } else if (Importer.getToContext().getLangOpts().CPlusPlus)
1900  IDNS |= Decl::IDNS_Ordinary;
1901 
1902  // We may already have a record of the same name; try to find and match it.
1903  RecordDecl *AdoptDecl = nullptr;
1904  RecordDecl *PrevDecl = nullptr;
1905  if (!DC->isFunctionOrMethod()) {
1906  SmallVector<NamedDecl *, 4> ConflictingDecls;
1907  SmallVector<NamedDecl *, 2> FoundDecls;
1908  DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
1909 
1910  if (!FoundDecls.empty()) {
1911  // We're going to have to compare D against potentially conflicting Decls, so complete it.
1914  }
1915 
1916  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1917  if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1918  continue;
1919 
1920  Decl *Found = FoundDecls[I];
1921  if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
1922  if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1923  Found = Tag->getDecl();
1924  }
1925 
1926  if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
1927  if (D->isAnonymousStructOrUnion() &&
1928  FoundRecord->isAnonymousStructOrUnion()) {
1929  // If both anonymous structs/unions are in a record context, make sure
1930  // they occur in the same location in the context records.
1931  if (Optional<unsigned> Index1 =
1933  D)) {
1935  findUntaggedStructOrUnionIndex(FoundRecord)) {
1936  if (*Index1 != *Index2)
1937  continue;
1938  }
1939  }
1940  }
1941 
1942  PrevDecl = FoundRecord;
1943 
1944  if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1945  if ((SearchName && !D->isCompleteDefinition())
1946  || (D->isCompleteDefinition() &&
1948  == FoundDef->isAnonymousStructOrUnion() &&
1949  IsStructuralMatch(D, FoundDef))) {
1950  // The record types structurally match, or the "from" translation
1951  // unit only had a forward declaration anyway; call it the same
1952  // function.
1953  // FIXME: For C++, we should also merge methods here.
1954  return Importer.Imported(D, FoundDef);
1955  }
1956  } else if (!D->isCompleteDefinition()) {
1957  // We have a forward declaration of this type, so adopt that forward
1958  // declaration rather than building a new one.
1959 
1960  // If one or both can be completed from external storage then try one
1961  // last time to complete and compare them before doing this.
1962 
1963  if (FoundRecord->hasExternalLexicalStorage() &&
1964  !FoundRecord->isCompleteDefinition())
1965  FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
1966  if (D->hasExternalLexicalStorage())
1968 
1969  if (FoundRecord->isCompleteDefinition() &&
1970  D->isCompleteDefinition() &&
1971  !IsStructuralMatch(D, FoundRecord))
1972  continue;
1973 
1974  AdoptDecl = FoundRecord;
1975  continue;
1976  } else if (!SearchName) {
1977  continue;
1978  }
1979  }
1980 
1981  ConflictingDecls.push_back(FoundDecls[I]);
1982  }
1983 
1984  if (!ConflictingDecls.empty() && SearchName) {
1985  Name = Importer.HandleNameConflict(Name, DC, IDNS,
1986  ConflictingDecls.data(),
1987  ConflictingDecls.size());
1988  }
1989  }
1990 
1991  // Create the record declaration.
1992  RecordDecl *D2 = AdoptDecl;
1993  SourceLocation StartLoc = Importer.Import(D->getLocStart());
1994  if (!D2) {
1995  CXXRecordDecl *D2CXX = nullptr;
1996  if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
1997  if (DCXX->isLambda()) {
1998  TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
1999  D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
2000  DC, TInfo, Loc,
2001  DCXX->isDependentLambda(),
2002  DCXX->isGenericLambda(),
2003  DCXX->getLambdaCaptureDefault());
2004  Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2005  if (DCXX->getLambdaContextDecl() && !CDecl)
2006  return nullptr;
2007  D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
2008  } else if (DCXX->isInjectedClassName()) {
2009  // We have to be careful to do a similar dance to the one in
2010  // Sema::ActOnStartCXXMemberDeclarations
2011  CXXRecordDecl *const PrevDecl = nullptr;
2012  const bool DelayTypeCreation = true;
2013  D2CXX = CXXRecordDecl::Create(
2014  Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
2015  Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
2016  Importer.getToContext().getTypeDeclType(
2017  D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));
2018  } else {
2019  D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2020  D->getTagKind(),
2021  DC, StartLoc, Loc,
2022  Name.getAsIdentifierInfo());
2023  }
2024  D2 = D2CXX;
2025  D2->setAccess(D->getAccess());
2026  D2->setLexicalDeclContext(LexicalDC);
2027  if (!DCXX->getDescribedClassTemplate())
2028  LexicalDC->addDeclInternal(D2);
2029 
2030  Importer.Imported(D, D2);
2031 
2032  if (ClassTemplateDecl *FromDescribed =
2033  DCXX->getDescribedClassTemplate()) {
2034  ClassTemplateDecl *ToDescribed = cast_or_null<ClassTemplateDecl>(
2035  Importer.Import(FromDescribed));
2036  if (!ToDescribed)
2037  return nullptr;
2038  D2CXX->setDescribedClassTemplate(ToDescribed);
2039 
2040  } else if (MemberSpecializationInfo *MemberInfo =
2041  DCXX->getMemberSpecializationInfo()) {
2043  MemberInfo->getTemplateSpecializationKind();
2044  CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
2045  CXXRecordDecl *ToInst =
2046  cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
2047  if (FromInst && !ToInst)
2048  return nullptr;
2049  D2CXX->setInstantiationOfMemberClass(ToInst, SK);
2051  Importer.Import(MemberInfo->getPointOfInstantiation()));
2052  }
2053 
2054  } else {
2055  D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
2056  DC, StartLoc, Loc, Name.getAsIdentifierInfo());
2057  D2->setLexicalDeclContext(LexicalDC);
2058  LexicalDC->addDeclInternal(D2);
2059  }
2060 
2061  D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2062  if (D->isAnonymousStructOrUnion())
2063  D2->setAnonymousStructOrUnion(true);
2064  if (PrevDecl) {
2065  // FIXME: do this for all Redeclarables, not just RecordDecls.
2066  D2->setPreviousDecl(PrevDecl);
2067  }
2068  }
2069 
2070  Importer.Imported(D, D2);
2071 
2073  return nullptr;
2074 
2075  return D2;
2076 }
2077 
2079  // Import the major distinguishing characteristics of this enumerator.
2080  DeclContext *DC, *LexicalDC;
2081  DeclarationName Name;
2082  SourceLocation Loc;
2083  NamedDecl *ToD;
2084  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2085  return nullptr;
2086  if (ToD)
2087  return ToD;
2088 
2089  QualType T = Importer.Import(D->getType());
2090  if (T.isNull())
2091  return nullptr;
2092 
2093  // Determine whether there are any other declarations with the same name and
2094  // in the same context.
2095  if (!LexicalDC->isFunctionOrMethod()) {
2096  SmallVector<NamedDecl *, 4> ConflictingDecls;
2097  unsigned IDNS = Decl::IDNS_Ordinary;
2098  SmallVector<NamedDecl *, 2> FoundDecls;
2099  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2100  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2101  if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2102  continue;
2103 
2104  if (EnumConstantDecl *FoundEnumConstant
2105  = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2106  if (IsStructuralMatch(D, FoundEnumConstant))
2107  return Importer.Imported(D, FoundEnumConstant);
2108  }
2109 
2110  ConflictingDecls.push_back(FoundDecls[I]);
2111  }
2112 
2113  if (!ConflictingDecls.empty()) {
2114  Name = Importer.HandleNameConflict(Name, DC, IDNS,
2115  ConflictingDecls.data(),
2116  ConflictingDecls.size());
2117  if (!Name)
2118  return nullptr;
2119  }
2120  }
2121 
2122  Expr *Init = Importer.Import(D->getInitExpr());
2123  if (D->getInitExpr() && !Init)
2124  return nullptr;
2125 
2126  EnumConstantDecl *ToEnumerator
2127  = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2128  Name.getAsIdentifierInfo(), T,
2129  Init, D->getInitVal());
2130  ToEnumerator->setAccess(D->getAccess());
2131  ToEnumerator->setLexicalDeclContext(LexicalDC);
2132  Importer.Imported(D, ToEnumerator);
2133  LexicalDC->addDeclInternal(ToEnumerator);
2134  return ToEnumerator;
2135 }
2136 
2138  FunctionDecl *ToFD) {
2139  switch (FromFD->getTemplatedKind()) {
2142  return false;
2143 
2145  auto *InstFD = cast_or_null<FunctionDecl>(
2146  Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2147  if (!InstFD)
2148  return true;
2149 
2151  SourceLocation POI = Importer.Import(
2153  ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2155  return false;
2156  }
2157 
2159  auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
2160  auto *Template = cast_or_null<FunctionTemplateDecl>(
2161  Importer.Import(FTSInfo->getTemplate()));
2162  if (!Template)
2163  return true;
2164  TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
2165 
2166  // Import template arguments.
2167  auto TemplArgs = FTSInfo->TemplateArguments->asArray();
2169  if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
2170  ToTemplArgs))
2171  return true;
2172 
2174  Importer.getToContext(), ToTemplArgs);
2175 
2176  TemplateArgumentListInfo ToTAInfo;
2177  const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
2178  if (FromTAArgsAsWritten)
2179  if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo))
2180  return true;
2181 
2182  SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2183 
2184  ToFD->setFunctionTemplateSpecialization(
2185  Template, ToTAList, /* InsertPos= */ nullptr,
2186  TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
2187  return false;
2188  }
2189 
2191  auto *FromInfo = FromFD->getDependentSpecializationInfo();
2192  UnresolvedSet<8> TemplDecls;
2193  unsigned NumTemplates = FromInfo->getNumTemplates();
2194  for (unsigned I = 0; I < NumTemplates; I++) {
2195  if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2196  Importer.Import(FromInfo->getTemplate(I))))
2197  TemplDecls.addDecl(ToFTD);
2198  else
2199  return true;
2200  }
2201 
2202  // Import TemplateArgumentListInfo.
2203  TemplateArgumentListInfo ToTAInfo;
2205  FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2206  llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2207  FromInfo->getNumTemplateArgs()),
2208  ToTAInfo))
2209  return true;
2210 
2211  ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2212  TemplDecls, ToTAInfo);
2213  return false;
2214  }
2215  }
2216  llvm_unreachable("All cases should be covered!");
2217 }
2218 
2220  // Import the major distinguishing characteristics of this function.
2221  DeclContext *DC, *LexicalDC;
2222  DeclarationName Name;
2223  SourceLocation Loc;
2224  NamedDecl *ToD;
2225  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2226  return nullptr;
2227  if (ToD)
2228  return ToD;
2229 
2230  const FunctionDecl *FoundWithoutBody = nullptr;
2231 
2232  // Try to find a function in our own ("to") context with the same name, same
2233  // type, and in the same context as the function we're importing.
2234  if (!LexicalDC->isFunctionOrMethod()) {
2235  SmallVector<NamedDecl *, 4> ConflictingDecls;
2236  unsigned IDNS = Decl::IDNS_Ordinary;
2237  SmallVector<NamedDecl *, 2> FoundDecls;
2238  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2239  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2240  if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2241  continue;
2242 
2243  if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
2244  if (FoundFunction->hasExternalFormalLinkage() &&
2245  D->hasExternalFormalLinkage()) {
2246  if (Importer.IsStructurallyEquivalent(D->getType(),
2247  FoundFunction->getType())) {
2248  // FIXME: Actually try to merge the body and other attributes.
2249  const FunctionDecl *FromBodyDecl = nullptr;
2250  D->hasBody(FromBodyDecl);
2251  if (D == FromBodyDecl && !FoundFunction->hasBody()) {
2252  // This function is needed to merge completely.
2253  FoundWithoutBody = FoundFunction;
2254  break;
2255  }
2256  return Importer.Imported(D, FoundFunction);
2257  }
2258 
2259  // FIXME: Check for overloading more carefully, e.g., by boosting
2260  // Sema::IsOverload out to the AST library.
2261 
2262  // Function overloading is okay in C++.
2263  if (Importer.getToContext().getLangOpts().CPlusPlus)
2264  continue;
2265 
2266  // Complain about inconsistent function types.
2267  Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
2268  << Name << D->getType() << FoundFunction->getType();
2269  Importer.ToDiag(FoundFunction->getLocation(),
2270  diag::note_odr_value_here)
2271  << FoundFunction->getType();
2272  }
2273  }
2274 
2275  ConflictingDecls.push_back(FoundDecls[I]);
2276  }
2277 
2278  if (!ConflictingDecls.empty()) {
2279  Name = Importer.HandleNameConflict(Name, DC, IDNS,
2280  ConflictingDecls.data(),
2281  ConflictingDecls.size());
2282  if (!Name)
2283  return nullptr;
2284  }
2285  }
2286 
2287  DeclarationNameInfo NameInfo(Name, Loc);
2288  // Import additional name location/type info.
2289  ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2290 
2291  QualType FromTy = D->getType();
2292  bool usedDifferentExceptionSpec = false;
2293 
2294  if (const FunctionProtoType *
2295  FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2296  FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2297  // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2298  // FunctionDecl that we are importing the FunctionProtoType for.
2299  // To avoid an infinite recursion when importing, create the FunctionDecl
2300  // with a simplified function type and update it afterwards.
2301  if (FromEPI.ExceptionSpec.SourceDecl ||
2302  FromEPI.ExceptionSpec.SourceTemplate ||
2303  FromEPI.ExceptionSpec.NoexceptExpr) {
2305  FromTy = Importer.getFromContext().getFunctionType(
2306  FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
2307  usedDifferentExceptionSpec = true;
2308  }
2309  }
2310 
2311  // Import the type.
2312  QualType T = Importer.Import(FromTy);
2313  if (T.isNull())
2314  return nullptr;
2315 
2316  // Import the function parameters.
2317  SmallVector<ParmVarDecl *, 8> Parameters;
2318  for (auto P : D->parameters()) {
2319  ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
2320  if (!ToP)
2321  return nullptr;
2322 
2323  Parameters.push_back(ToP);
2324  }
2325 
2326  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2327  if (D->getTypeSourceInfo() && !TInfo)
2328  return nullptr;
2329 
2330  // Create the imported function.
2331  FunctionDecl *ToFunction = nullptr;
2332  SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
2333  if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2334  ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2335  cast<CXXRecordDecl>(DC),
2336  InnerLocStart,
2337  NameInfo, T, TInfo,
2338  FromConstructor->isExplicit(),
2339  D->isInlineSpecified(),
2340  D->isImplicit(),
2341  D->isConstexpr());
2342  if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2343  SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
2344  for (CXXCtorInitializer *I : FromConstructor->inits()) {
2345  CXXCtorInitializer *ToI =
2346  cast_or_null<CXXCtorInitializer>(Importer.Import(I));
2347  if (!ToI && I)
2348  return nullptr;
2349  CtorInitializers.push_back(ToI);
2350  }
2351  CXXCtorInitializer **Memory =
2352  new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2353  std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
2354  CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
2355  ToCtor->setCtorInitializers(Memory);
2356  ToCtor->setNumCtorInitializers(NumInitializers);
2357  }
2358  } else if (isa<CXXDestructorDecl>(D)) {
2359  ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2360  cast<CXXRecordDecl>(DC),
2361  InnerLocStart,
2362  NameInfo, T, TInfo,
2363  D->isInlineSpecified(),
2364  D->isImplicit());
2365  } else if (CXXConversionDecl *FromConversion
2366  = dyn_cast<CXXConversionDecl>(D)) {
2367  ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2368  cast<CXXRecordDecl>(DC),
2369  InnerLocStart,
2370  NameInfo, T, TInfo,
2371  D->isInlineSpecified(),
2372  FromConversion->isExplicit(),
2373  D->isConstexpr(),
2374  Importer.Import(D->getLocEnd()));
2375  } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2376  ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2377  cast<CXXRecordDecl>(DC),
2378  InnerLocStart,
2379  NameInfo, T, TInfo,
2380  Method->getStorageClass(),
2381  Method->isInlineSpecified(),
2382  D->isConstexpr(),
2383  Importer.Import(D->getLocEnd()));
2384  } else {
2385  ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2386  InnerLocStart,
2387  NameInfo, T, TInfo, D->getStorageClass(),
2388  D->isInlineSpecified(),
2389  D->hasWrittenPrototype(),
2390  D->isConstexpr());
2391  }
2392 
2393  // Import the qualifier, if any.
2394  ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2395  ToFunction->setAccess(D->getAccess());
2396  ToFunction->setLexicalDeclContext(LexicalDC);
2397  ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2398  ToFunction->setTrivial(D->isTrivial());
2399  ToFunction->setPure(D->isPure());
2400  Importer.Imported(D, ToFunction);
2401 
2402  // Set the parameters.
2403  for (ParmVarDecl *Param : Parameters) {
2404  Param->setOwningFunction(ToFunction);
2405  ToFunction->addDeclInternal(Param);
2406  }
2407  ToFunction->setParams(Parameters);
2408 
2409  if (FoundWithoutBody) {
2410  auto *Recent = const_cast<FunctionDecl *>(
2411  FoundWithoutBody->getMostRecentDecl());
2412  ToFunction->setPreviousDecl(Recent);
2413  }
2414 
2415  // We need to complete creation of FunctionProtoTypeLoc manually with setting
2416  // params it refers to.
2417  if (TInfo) {
2418  if (auto ProtoLoc =
2420  for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2421  ProtoLoc.setParam(I, Parameters[I]);
2422  }
2423  }
2424 
2425  if (usedDifferentExceptionSpec) {
2426  // Update FunctionProtoType::ExtProtoInfo.
2427  QualType T = Importer.Import(D->getType());
2428  if (T.isNull())
2429  return nullptr;
2430  ToFunction->setType(T);
2431  }
2432 
2433  // Import the body, if any.
2434  if (Stmt *FromBody = D->getBody()) {
2435  if (Stmt *ToBody = Importer.Import(FromBody)) {
2436  ToFunction->setBody(ToBody);
2437  }
2438  }
2439 
2440  // FIXME: Other bits to merge?
2441 
2442  // If it is a template, import all related things.
2443  if (ImportTemplateInformation(D, ToFunction))
2444  return nullptr;
2445 
2446  // Add this function to the lexical context.
2447  // NOTE: If the function is templated declaration, it should be not added into
2448  // LexicalDC. But described template is imported during import of
2449  // FunctionTemplateDecl (it happens later). So, we use source declaration
2450  // to determine if we should add the result function.
2451  if (!D->getDescribedFunctionTemplate())
2452  LexicalDC->addDeclInternal(ToFunction);
2453 
2454  if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2455  ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2456 
2457  return ToFunction;
2458 }
2459 
2461  return VisitFunctionDecl(D);
2462 }
2463 
2465  return VisitCXXMethodDecl(D);
2466 }
2467 
2469  return VisitCXXMethodDecl(D);
2470 }
2471 
2473  return VisitCXXMethodDecl(D);
2474 }
2475 
2476 static unsigned getFieldIndex(Decl *F) {
2477  RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2478  if (!Owner)
2479  return 0;
2480 
2481  unsigned Index = 1;
2482  for (const auto *D : Owner->noload_decls()) {
2483  if (D == F)
2484  return Index;
2485 
2486  if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2487  ++Index;
2488  }
2489 
2490  return Index;
2491 }
2492 
2494  // Import the major distinguishing characteristics of a variable.
2495  DeclContext *DC, *LexicalDC;
2496  DeclarationName Name;
2497  SourceLocation Loc;
2498  NamedDecl *ToD;
2499  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2500  return nullptr;
2501  if (ToD)
2502  return ToD;
2503 
2504  // Determine whether we've already imported this field.
2505  SmallVector<NamedDecl *, 2> FoundDecls;
2506  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2507  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2508  if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
2509  // For anonymous fields, match up by index.
2510  if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2511  continue;
2512 
2513  if (Importer.IsStructurallyEquivalent(D->getType(),
2514  FoundField->getType())) {
2515  Importer.Imported(D, FoundField);
2516  return FoundField;
2517  }
2518 
2519  Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2520  << Name << D->getType() << FoundField->getType();
2521  Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2522  << FoundField->getType();
2523  return nullptr;
2524  }
2525  }
2526 
2527  // Import the type.
2528  QualType T = Importer.Import(D->getType());
2529  if (T.isNull())
2530  return nullptr;
2531 
2532  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2533  Expr *BitWidth = Importer.Import(D->getBitWidth());
2534  if (!BitWidth && D->getBitWidth())
2535  return nullptr;
2536 
2537  FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2538  Importer.Import(D->getInnerLocStart()),
2539  Loc, Name.getAsIdentifierInfo(),
2540  T, TInfo, BitWidth, D->isMutable(),
2541  D->getInClassInitStyle());
2542  ToField->setAccess(D->getAccess());
2543  ToField->setLexicalDeclContext(LexicalDC);
2544  if (Expr *FromInitializer = D->getInClassInitializer()) {
2545  Expr *ToInitializer = Importer.Import(FromInitializer);
2546  if (ToInitializer)
2547  ToField->setInClassInitializer(ToInitializer);
2548  else
2549  return nullptr;
2550  }
2551  ToField->setImplicit(D->isImplicit());
2552  Importer.Imported(D, ToField);
2553  LexicalDC->addDeclInternal(ToField);
2554  return ToField;
2555 }
2556 
2558  // Import the major distinguishing characteristics of a variable.
2559  DeclContext *DC, *LexicalDC;
2560  DeclarationName Name;
2561  SourceLocation Loc;
2562  NamedDecl *ToD;
2563  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2564  return nullptr;
2565  if (ToD)
2566  return ToD;
2567 
2568  // Determine whether we've already imported this field.
2569  SmallVector<NamedDecl *, 2> FoundDecls;
2570  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2571  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2572  if (IndirectFieldDecl *FoundField
2573  = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
2574  // For anonymous indirect fields, match up by index.
2575  if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2576  continue;
2577 
2578  if (Importer.IsStructurallyEquivalent(D->getType(),
2579  FoundField->getType(),
2580  !Name.isEmpty())) {
2581  Importer.Imported(D, FoundField);
2582  return FoundField;
2583  }
2584 
2585  // If there are more anonymous fields to check, continue.
2586  if (!Name && I < N-1)
2587  continue;
2588 
2589  Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2590  << Name << D->getType() << FoundField->getType();
2591  Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2592  << FoundField->getType();
2593  return nullptr;
2594  }
2595  }
2596 
2597  // Import the type.
2598  QualType T = Importer.Import(D->getType());
2599  if (T.isNull())
2600  return nullptr;
2601 
2602  NamedDecl **NamedChain =
2603  new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2604 
2605  unsigned i = 0;
2606  for (auto *PI : D->chain()) {
2607  Decl *D = Importer.Import(PI);
2608  if (!D)
2609  return nullptr;
2610  NamedChain[i++] = cast<NamedDecl>(D);
2611  }
2612 
2613  IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2614  Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
2615  {NamedChain, D->getChainingSize()});
2616 
2617  for (const auto *Attr : D->attrs())
2618  ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
2619 
2620  ToIndirectField->setAccess(D->getAccess());
2621  ToIndirectField->setLexicalDeclContext(LexicalDC);
2622  Importer.Imported(D, ToIndirectField);
2623  LexicalDC->addDeclInternal(ToIndirectField);
2624  return ToIndirectField;
2625 }
2626 
2628  // Import the major distinguishing characteristics of a declaration.
2629  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2630  DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2631  ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2632  if (!DC || !LexicalDC)
2633  return nullptr;
2634 
2635  // Determine whether we've already imported this decl.
2636  // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2637  auto *RD = cast<CXXRecordDecl>(DC);
2638  FriendDecl *ImportedFriend = RD->getFirstFriend();
2640  Importer.getFromContext(), Importer.getToContext(),
2641  Importer.getNonEquivalentDecls(), false, false);
2642 
2643  while (ImportedFriend) {
2644  if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2645  if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2646  ImportedFriend->getFriendDecl()))
2647  return Importer.Imported(D, ImportedFriend);
2648 
2649  } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2650  if (Importer.IsStructurallyEquivalent(
2651  D->getFriendType()->getType(),
2652  ImportedFriend->getFriendType()->getType(), true))
2653  return Importer.Imported(D, ImportedFriend);
2654  }
2655  ImportedFriend = ImportedFriend->getNextFriend();
2656  }
2657 
2658  // Not found. Create it.
2660  if (NamedDecl *FriendD = D->getFriendDecl())
2661  ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2662  else
2663  ToFU = Importer.Import(D->getFriendType());
2664  if (!ToFU)
2665  return nullptr;
2666 
2667  SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
2668  TemplateParameterList **FromTPLists =
2669  D->getTrailingObjects<TemplateParameterList *>();
2670  for (unsigned I = 0; I < D->NumTPLists; I++) {
2671  TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2672  if (!List)
2673  return nullptr;
2674  ToTPLists[I] = List;
2675  }
2676 
2677  FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2678  Importer.Import(D->getLocation()),
2679  ToFU, Importer.Import(D->getFriendLoc()),
2680  ToTPLists);
2681 
2682  Importer.Imported(D, FrD);
2683  RD->pushFriendDecl(FrD);
2684 
2685  FrD->setAccess(D->getAccess());
2686  FrD->setLexicalDeclContext(LexicalDC);
2687  LexicalDC->addDeclInternal(FrD);
2688  return FrD;
2689 }
2690 
2692  // Import the major distinguishing characteristics of an ivar.
2693  DeclContext *DC, *LexicalDC;
2694  DeclarationName Name;
2695  SourceLocation Loc;
2696  NamedDecl *ToD;
2697  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2698  return nullptr;
2699  if (ToD)
2700  return ToD;
2701 
2702  // Determine whether we've already imported this ivar
2703  SmallVector<NamedDecl *, 2> FoundDecls;
2704  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2705  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2706  if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
2707  if (Importer.IsStructurallyEquivalent(D->getType(),
2708  FoundIvar->getType())) {
2709  Importer.Imported(D, FoundIvar);
2710  return FoundIvar;
2711  }
2712 
2713  Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2714  << Name << D->getType() << FoundIvar->getType();
2715  Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2716  << FoundIvar->getType();
2717  return nullptr;
2718  }
2719  }
2720 
2721  // Import the type.
2722  QualType T = Importer.Import(D->getType());
2723  if (T.isNull())
2724  return nullptr;
2725 
2726  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2727  Expr *BitWidth = Importer.Import(D->getBitWidth());
2728  if (!BitWidth && D->getBitWidth())
2729  return nullptr;
2730 
2731  ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2732  cast<ObjCContainerDecl>(DC),
2733  Importer.Import(D->getInnerLocStart()),
2734  Loc, Name.getAsIdentifierInfo(),
2735  T, TInfo, D->getAccessControl(),
2736  BitWidth, D->getSynthesize());
2737  ToIvar->setLexicalDeclContext(LexicalDC);
2738  Importer.Imported(D, ToIvar);
2739  LexicalDC->addDeclInternal(ToIvar);
2740  return ToIvar;
2741 
2742 }
2743 
2745  // Import the major distinguishing characteristics of a variable.
2746  DeclContext *DC, *LexicalDC;
2747  DeclarationName Name;
2748  SourceLocation Loc;
2749  NamedDecl *ToD;
2750  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2751  return nullptr;
2752  if (ToD)
2753  return ToD;
2754 
2755  // Try to find a variable in our own ("to") context with the same name and
2756  // in the same context as the variable we're importing.
2757  if (D->isFileVarDecl()) {
2758  VarDecl *MergeWithVar = nullptr;
2759  SmallVector<NamedDecl *, 4> ConflictingDecls;
2760  unsigned IDNS = Decl::IDNS_Ordinary;
2761  SmallVector<NamedDecl *, 2> FoundDecls;
2762  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2763  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2764  if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
2765  continue;
2766 
2767  if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
2768  // We have found a variable that we may need to merge with. Check it.
2769  if (FoundVar->hasExternalFormalLinkage() &&
2770  D->hasExternalFormalLinkage()) {
2771  if (Importer.IsStructurallyEquivalent(D->getType(),
2772  FoundVar->getType())) {
2773  MergeWithVar = FoundVar;
2774  break;
2775  }
2776 
2777  const ArrayType *FoundArray
2778  = Importer.getToContext().getAsArrayType(FoundVar->getType());
2779  const ArrayType *TArray
2780  = Importer.getToContext().getAsArrayType(D->getType());
2781  if (FoundArray && TArray) {
2782  if (isa<IncompleteArrayType>(FoundArray) &&
2783  isa<ConstantArrayType>(TArray)) {
2784  // Import the type.
2785  QualType T = Importer.Import(D->getType());
2786  if (T.isNull())
2787  return nullptr;
2788 
2789  FoundVar->setType(T);
2790  MergeWithVar = FoundVar;
2791  break;
2792  } else if (isa<IncompleteArrayType>(TArray) &&
2793  isa<ConstantArrayType>(FoundArray)) {
2794  MergeWithVar = FoundVar;
2795  break;
2796  }
2797  }
2798 
2799  Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
2800  << Name << D->getType() << FoundVar->getType();
2801  Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2802  << FoundVar->getType();
2803  }
2804  }
2805 
2806  ConflictingDecls.push_back(FoundDecls[I]);
2807  }
2808 
2809  if (MergeWithVar) {
2810  // An equivalent variable with external linkage has been found. Link
2811  // the two declarations, then merge them.
2812  Importer.Imported(D, MergeWithVar);
2813 
2814  if (VarDecl *DDef = D->getDefinition()) {
2815  if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2816  Importer.ToDiag(ExistingDef->getLocation(),
2817  diag::err_odr_variable_multiple_def)
2818  << Name;
2819  Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2820  } else {
2821  Expr *Init = Importer.Import(DDef->getInit());
2822  MergeWithVar->setInit(Init);
2823  if (DDef->isInitKnownICE()) {
2824  EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2825  Eval->CheckedICE = true;
2826  Eval->IsICE = DDef->isInitICE();
2827  }
2828  }
2829  }
2830 
2831  return MergeWithVar;
2832  }
2833 
2834  if (!ConflictingDecls.empty()) {
2835  Name = Importer.HandleNameConflict(Name, DC, IDNS,
2836  ConflictingDecls.data(),
2837  ConflictingDecls.size());
2838  if (!Name)
2839  return nullptr;
2840  }
2841  }
2842 
2843  // Import the type.
2844  QualType T = Importer.Import(D->getType());
2845  if (T.isNull())
2846  return nullptr;
2847 
2848  // Create the imported variable.
2849  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2850  VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2851  Importer.Import(D->getInnerLocStart()),
2852  Loc, Name.getAsIdentifierInfo(),
2853  T, TInfo,
2854  D->getStorageClass());
2855  ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2856  ToVar->setAccess(D->getAccess());
2857  ToVar->setLexicalDeclContext(LexicalDC);
2858  Importer.Imported(D, ToVar);
2859 
2860  // Templated declarations should never appear in the enclosing DeclContext.
2861  if (!D->getDescribedVarTemplate())
2862  LexicalDC->addDeclInternal(ToVar);
2863 
2864  if (!D->isFileVarDecl() &&
2865  D->isUsed())
2866  ToVar->setIsUsed();
2867 
2868  // Merge the initializer.
2869  if (ImportDefinition(D, ToVar))
2870  return nullptr;
2871 
2872  if (D->isConstexpr())
2873  ToVar->setConstexpr(true);
2874 
2875  return ToVar;
2876 }
2877 
2879  // Parameters are created in the translation unit's context, then moved
2880  // into the function declaration's context afterward.
2881  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2882 
2883  // Import the name of this declaration.
2884  DeclarationName Name = Importer.Import(D->getDeclName());
2885  if (D->getDeclName() && !Name)
2886  return nullptr;
2887 
2888  // Import the location of this declaration.
2889  SourceLocation Loc = Importer.Import(D->getLocation());
2890 
2891  // Import the parameter's type.
2892  QualType T = Importer.Import(D->getType());
2893  if (T.isNull())
2894  return nullptr;
2895 
2896  // Create the imported parameter.
2897  auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
2898  Name.getAsIdentifierInfo(), T,
2899  D->getParameterKind());
2900  return Importer.Imported(D, ToParm);
2901 }
2902 
2904  // Parameters are created in the translation unit's context, then moved
2905  // into the function declaration's context afterward.
2906  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2907 
2908  // Import the name of this declaration.
2909  DeclarationName Name = Importer.Import(D->getDeclName());
2910  if (D->getDeclName() && !Name)
2911  return nullptr;
2912 
2913  // Import the location of this declaration.
2914  SourceLocation Loc = Importer.Import(D->getLocation());
2915 
2916  // Import the parameter's type.
2917  QualType T = Importer.Import(D->getType());
2918  if (T.isNull())
2919  return nullptr;
2920 
2921  // Create the imported parameter.
2922  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2923  ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2924  Importer.Import(D->getInnerLocStart()),
2925  Loc, Name.getAsIdentifierInfo(),
2926  T, TInfo, D->getStorageClass(),
2927  /*DefaultArg*/ nullptr);
2928 
2929  // Set the default argument.
2931  ToParm->setKNRPromoted(D->isKNRPromoted());
2932 
2933  Expr *ToDefArg = nullptr;
2934  Expr *FromDefArg = nullptr;
2935  if (D->hasUninstantiatedDefaultArg()) {
2936  FromDefArg = D->getUninstantiatedDefaultArg();
2937  ToDefArg = Importer.Import(FromDefArg);
2938  ToParm->setUninstantiatedDefaultArg(ToDefArg);
2939  } else if (D->hasUnparsedDefaultArg()) {
2940  ToParm->setUnparsedDefaultArg();
2941  } else if (D->hasDefaultArg()) {
2942  FromDefArg = D->getDefaultArg();
2943  ToDefArg = Importer.Import(FromDefArg);
2944  ToParm->setDefaultArg(ToDefArg);
2945  }
2946  if (FromDefArg && !ToDefArg)
2947  return nullptr;
2948 
2949  if (D->isObjCMethodParameter()) {
2952  } else {
2953  ToParm->setScopeInfo(D->getFunctionScopeDepth(),
2954  D->getFunctionScopeIndex());
2955  }
2956 
2957  if (D->isUsed())
2958  ToParm->setIsUsed();
2959 
2960  return Importer.Imported(D, ToParm);
2961 }
2962 
2964  // Import the major distinguishing characteristics of a method.
2965  DeclContext *DC, *LexicalDC;
2966  DeclarationName Name;
2967  SourceLocation Loc;
2968  NamedDecl *ToD;
2969  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2970  return nullptr;
2971  if (ToD)
2972  return ToD;
2973 
2974  SmallVector<NamedDecl *, 2> FoundDecls;
2975  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
2976  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2977  if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
2978  if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2979  continue;
2980 
2981  // Check return types.
2982  if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
2983  FoundMethod->getReturnType())) {
2984  Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2985  << D->isInstanceMethod() << Name << D->getReturnType()
2986  << FoundMethod->getReturnType();
2987  Importer.ToDiag(FoundMethod->getLocation(),
2988  diag::note_odr_objc_method_here)
2989  << D->isInstanceMethod() << Name;
2990  return nullptr;
2991  }
2992 
2993  // Check the number of parameters.
2994  if (D->param_size() != FoundMethod->param_size()) {
2995  Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2996  << D->isInstanceMethod() << Name
2997  << D->param_size() << FoundMethod->param_size();
2998  Importer.ToDiag(FoundMethod->getLocation(),
2999  diag::note_odr_objc_method_here)
3000  << D->isInstanceMethod() << Name;
3001  return nullptr;
3002  }
3003 
3004  // Check parameter types.
3006  PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3007  P != PEnd; ++P, ++FoundP) {
3008  if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3009  (*FoundP)->getType())) {
3010  Importer.FromDiag((*P)->getLocation(),
3011  diag::err_odr_objc_method_param_type_inconsistent)
3012  << D->isInstanceMethod() << Name
3013  << (*P)->getType() << (*FoundP)->getType();
3014  Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3015  << (*FoundP)->getType();
3016  return nullptr;
3017  }
3018  }
3019 
3020  // Check variadic/non-variadic.
3021  // Check the number of parameters.
3022  if (D->isVariadic() != FoundMethod->isVariadic()) {
3023  Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3024  << D->isInstanceMethod() << Name;
3025  Importer.ToDiag(FoundMethod->getLocation(),
3026  diag::note_odr_objc_method_here)
3027  << D->isInstanceMethod() << Name;
3028  return nullptr;
3029  }
3030 
3031  // FIXME: Any other bits we need to merge?
3032  return Importer.Imported(D, FoundMethod);
3033  }
3034  }
3035 
3036  // Import the result type.
3037  QualType ResultTy = Importer.Import(D->getReturnType());
3038  if (ResultTy.isNull())
3039  return nullptr;
3040 
3041  TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
3042 
3044  Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3045  Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3046  D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3048 
3049  // FIXME: When we decide to merge method definitions, we'll need to
3050  // deal with implicit parameters.
3051 
3052  // Import the parameters
3054  for (auto *FromP : D->parameters()) {
3055  ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
3056  if (!ToP)
3057  return nullptr;
3058 
3059  ToParams.push_back(ToP);
3060  }
3061 
3062  // Set the parameters.
3063  for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3064  ToParams[I]->setOwningFunction(ToMethod);
3065  ToMethod->addDeclInternal(ToParams[I]);
3066  }
3067 
3069  D->getSelectorLocs(SelLocs);
3070  for (SourceLocation &Loc : SelLocs)
3071  Loc = Importer.Import(Loc);
3072 
3073  ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
3074 
3075  ToMethod->setLexicalDeclContext(LexicalDC);
3076  Importer.Imported(D, ToMethod);
3077  LexicalDC->addDeclInternal(ToMethod);
3078  return ToMethod;
3079 }
3080 
3082  // Import the major distinguishing characteristics of a category.
3083  DeclContext *DC, *LexicalDC;
3084  DeclarationName Name;
3085  SourceLocation Loc;
3086  NamedDecl *ToD;
3087  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3088  return nullptr;
3089  if (ToD)
3090  return ToD;
3091 
3092  TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3093  if (!BoundInfo)
3094  return nullptr;
3095 
3097  Importer.getToContext(), DC,
3098  D->getVariance(),
3099  Importer.Import(D->getVarianceLoc()),
3100  D->getIndex(),
3101  Importer.Import(D->getLocation()),
3102  Name.getAsIdentifierInfo(),
3103  Importer.Import(D->getColonLoc()),
3104  BoundInfo);
3105  Importer.Imported(D, Result);
3106  Result->setLexicalDeclContext(LexicalDC);
3107  return Result;
3108 }
3109 
3111  // Import the major distinguishing characteristics of a category.
3112  DeclContext *DC, *LexicalDC;
3113  DeclarationName Name;
3114  SourceLocation Loc;
3115  NamedDecl *ToD;
3116  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3117  return nullptr;
3118  if (ToD)
3119  return ToD;
3120 
3121  ObjCInterfaceDecl *ToInterface
3122  = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3123  if (!ToInterface)
3124  return nullptr;
3125 
3126  // Determine if we've already encountered this category.
3127  ObjCCategoryDecl *MergeWithCategory
3128  = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3129  ObjCCategoryDecl *ToCategory = MergeWithCategory;
3130  if (!ToCategory) {
3131  ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
3132  Importer.Import(D->getAtStartLoc()),
3133  Loc,
3134  Importer.Import(D->getCategoryNameLoc()),
3135  Name.getAsIdentifierInfo(),
3136  ToInterface,
3137  /*TypeParamList=*/nullptr,
3138  Importer.Import(D->getIvarLBraceLoc()),
3139  Importer.Import(D->getIvarRBraceLoc()));
3140  ToCategory->setLexicalDeclContext(LexicalDC);
3141  LexicalDC->addDeclInternal(ToCategory);
3142  Importer.Imported(D, ToCategory);
3143  // Import the type parameter list after calling Imported, to avoid
3144  // loops when bringing in their DeclContext.
3146  D->getTypeParamList()));
3147 
3148  // Import protocols
3150  SmallVector<SourceLocation, 4> ProtocolLocs;
3152  = D->protocol_loc_begin();
3154  FromProtoEnd = D->protocol_end();
3155  FromProto != FromProtoEnd;
3156  ++FromProto, ++FromProtoLoc) {
3157  ObjCProtocolDecl *ToProto
3158  = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3159  if (!ToProto)
3160  return nullptr;
3161  Protocols.push_back(ToProto);
3162  ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3163  }
3164 
3165  // FIXME: If we're merging, make sure that the protocol list is the same.
3166  ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3167  ProtocolLocs.data(), Importer.getToContext());
3168 
3169  } else {
3170  Importer.Imported(D, ToCategory);
3171  }
3172 
3173  // Import all of the members of this category.
3174  ImportDeclContext(D);
3175 
3176  // If we have an implementation, import it as well.
3177  if (D->getImplementation()) {
3178  ObjCCategoryImplDecl *Impl
3179  = cast_or_null<ObjCCategoryImplDecl>(
3180  Importer.Import(D->getImplementation()));
3181  if (!Impl)
3182  return nullptr;
3183 
3184  ToCategory->setImplementation(Impl);
3185  }
3186 
3187  return ToCategory;
3188 }
3189 
3191  ObjCProtocolDecl *To,
3193  if (To->getDefinition()) {
3194  if (shouldForceImportDeclContext(Kind))
3195  ImportDeclContext(From);
3196  return false;
3197  }
3198 
3199  // Start the protocol definition
3200  To->startDefinition();
3201 
3202  // Import protocols
3204  SmallVector<SourceLocation, 4> ProtocolLocs;
3206  FromProtoLoc = From->protocol_loc_begin();
3207  for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3208  FromProtoEnd = From->protocol_end();
3209  FromProto != FromProtoEnd;
3210  ++FromProto, ++FromProtoLoc) {
3211  ObjCProtocolDecl *ToProto
3212  = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3213  if (!ToProto)
3214  return true;
3215  Protocols.push_back(ToProto);
3216  ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3217  }
3218 
3219  // FIXME: If we're merging, make sure that the protocol list is the same.
3220  To->setProtocolList(Protocols.data(), Protocols.size(),
3221  ProtocolLocs.data(), Importer.getToContext());
3222 
3223  if (shouldForceImportDeclContext(Kind)) {
3224  // Import all of the members of this protocol.
3225  ImportDeclContext(From, /*ForceImport=*/true);
3226  }
3227  return false;
3228 }
3229 
3231  // If this protocol has a definition in the translation unit we're coming
3232  // from, but this particular declaration is not that definition, import the
3233  // definition and map to that.
3234  ObjCProtocolDecl *Definition = D->getDefinition();
3235  if (Definition && Definition != D) {
3236  Decl *ImportedDef = Importer.Import(Definition);
3237  if (!ImportedDef)
3238  return nullptr;
3239 
3240  return Importer.Imported(D, ImportedDef);
3241  }
3242 
3243  // Import the major distinguishing characteristics of a protocol.
3244  DeclContext *DC, *LexicalDC;
3245  DeclarationName Name;
3246  SourceLocation Loc;
3247  NamedDecl *ToD;
3248  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3249  return nullptr;
3250  if (ToD)
3251  return ToD;
3252 
3253  ObjCProtocolDecl *MergeWithProtocol = nullptr;
3254  SmallVector<NamedDecl *, 2> FoundDecls;
3255  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3256  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3257  if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
3258  continue;
3259 
3260  if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
3261  break;
3262  }
3263 
3264  ObjCProtocolDecl *ToProto = MergeWithProtocol;
3265  if (!ToProto) {
3266  ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3267  Name.getAsIdentifierInfo(), Loc,
3268  Importer.Import(D->getAtStartLoc()),
3269  /*PrevDecl=*/nullptr);
3270  ToProto->setLexicalDeclContext(LexicalDC);
3271  LexicalDC->addDeclInternal(ToProto);
3272  }
3273 
3274  Importer.Imported(D, ToProto);
3275 
3276  if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3277  return nullptr;
3278 
3279  return ToProto;
3280 }
3281 
3283  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3284  DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3285 
3286  SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3287  SourceLocation LangLoc = Importer.Import(D->getLocation());
3288 
3289  bool HasBraces = D->hasBraces();
3290 
3291  LinkageSpecDecl *ToLinkageSpec =
3292  LinkageSpecDecl::Create(Importer.getToContext(),
3293  DC,
3294  ExternLoc,
3295  LangLoc,
3296  D->getLanguage(),
3297  HasBraces);
3298 
3299  if (HasBraces) {
3300  SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3301  ToLinkageSpec->setRBraceLoc(RBraceLoc);
3302  }
3303 
3304  ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3305  LexicalDC->addDeclInternal(ToLinkageSpec);
3306 
3307  Importer.Imported(D, ToLinkageSpec);
3308 
3309  return ToLinkageSpec;
3310 }
3311 
3313  DeclContext *DC, *LexicalDC;
3314  DeclarationName Name;
3315  SourceLocation Loc;
3316  NamedDecl *ToD = nullptr;
3317  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3318  return nullptr;
3319  if (ToD)
3320  return ToD;
3321 
3322  DeclarationNameInfo NameInfo(Name,
3323  Importer.Import(D->getNameInfo().getLoc()));
3324  ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3325 
3326  UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC,
3327  Importer.Import(D->getUsingLoc()),
3328  Importer.Import(D->getQualifierLoc()),
3329  NameInfo, D->hasTypename());
3330  ToUsing->setLexicalDeclContext(LexicalDC);
3331  LexicalDC->addDeclInternal(ToUsing);
3332  Importer.Imported(D, ToUsing);
3333 
3334  if (NamedDecl *FromPattern =
3335  Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
3336  if (NamedDecl *ToPattern =
3337  dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
3338  Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3339  else
3340  return nullptr;
3341  }
3342 
3343  for (UsingShadowDecl *FromShadow : D->shadows()) {
3344  if (UsingShadowDecl *ToShadow =
3345  dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
3346  ToUsing->addShadowDecl(ToShadow);
3347  else
3348  // FIXME: We return a nullptr here but the definition is already created
3349  // and available with lookups. How to fix this?..
3350  return nullptr;
3351  }
3352  return ToUsing;
3353 }
3354 
3356  DeclContext *DC, *LexicalDC;
3357  DeclarationName Name;
3358  SourceLocation Loc;
3359  NamedDecl *ToD = nullptr;
3360  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3361  return nullptr;
3362  if (ToD)
3363  return ToD;
3364 
3365  UsingDecl *ToUsing = dyn_cast_or_null<UsingDecl>(
3366  Importer.Import(D->getUsingDecl()));
3367  if (!ToUsing)
3368  return nullptr;
3369 
3370  NamedDecl *ToTarget = dyn_cast_or_null<NamedDecl>(
3371  Importer.Import(D->getTargetDecl()));
3372  if (!ToTarget)
3373  return nullptr;
3374 
3376  Importer.getToContext(), DC, Loc, ToUsing, ToTarget);
3377 
3378  ToShadow->setLexicalDeclContext(LexicalDC);
3379  ToShadow->setAccess(D->getAccess());
3380  Importer.Imported(D, ToShadow);
3381 
3382  if (UsingShadowDecl *FromPattern =
3383  Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
3384  if (UsingShadowDecl *ToPattern =
3385  dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
3386  Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3387  ToPattern);
3388  else
3389  // FIXME: We return a nullptr here but the definition is already created
3390  // and available with lookups. How to fix this?..
3391  return nullptr;
3392  }
3393 
3394  LexicalDC->addDeclInternal(ToShadow);
3395 
3396  return ToShadow;
3397 }
3398 
3399 
3401  DeclContext *DC, *LexicalDC;
3402  DeclarationName Name;
3403  SourceLocation Loc;
3404  NamedDecl *ToD = nullptr;
3405  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3406  return nullptr;
3407  if (ToD)
3408  return ToD;
3409 
3410  DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3411  if (!ToComAncestor)
3412  return nullptr;
3413 
3414  NamespaceDecl *ToNominated = cast_or_null<NamespaceDecl>(
3415  Importer.Import(D->getNominatedNamespace()));
3416  if (!ToNominated)
3417  return nullptr;
3418 
3420  Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3421  Importer.Import(D->getNamespaceKeyLocation()),
3422  Importer.Import(D->getQualifierLoc()),
3423  Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor);
3424  ToUsingDir->setLexicalDeclContext(LexicalDC);
3425  LexicalDC->addDeclInternal(ToUsingDir);
3426  Importer.Imported(D, ToUsingDir);
3427 
3428  return ToUsingDir;
3429 }
3430 
3433  DeclContext *DC, *LexicalDC;
3434  DeclarationName Name;
3435  SourceLocation Loc;
3436  NamedDecl *ToD = nullptr;
3437  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3438  return nullptr;
3439  if (ToD)
3440  return ToD;
3441 
3442  DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3443  ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3444 
3446  Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3447  Importer.Import(D->getQualifierLoc()), NameInfo,
3448  Importer.Import(D->getEllipsisLoc()));
3449 
3450  Importer.Imported(D, ToUsingValue);
3451  ToUsingValue->setAccess(D->getAccess());
3452  ToUsingValue->setLexicalDeclContext(LexicalDC);
3453  LexicalDC->addDeclInternal(ToUsingValue);
3454 
3455  return ToUsingValue;
3456 }
3457 
3460  DeclContext *DC, *LexicalDC;
3461  DeclarationName Name;
3462  SourceLocation Loc;
3463  NamedDecl *ToD = nullptr;
3464  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3465  return nullptr;
3466  if (ToD)
3467  return ToD;
3468 
3470  Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3471  Importer.Import(D->getTypenameLoc()),
3472  Importer.Import(D->getQualifierLoc()), Loc, Name,
3473  Importer.Import(D->getEllipsisLoc()));
3474 
3475  Importer.Imported(D, ToUsing);
3476  ToUsing->setAccess(D->getAccess());
3477  ToUsing->setLexicalDeclContext(LexicalDC);
3478  LexicalDC->addDeclInternal(ToUsing);
3479 
3480  return ToUsing;
3481 }
3482 
3483 
3485  ObjCInterfaceDecl *To,
3487  if (To->getDefinition()) {
3488  // Check consistency of superclass.
3489  ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3490  if (FromSuper) {
3491  FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3492  if (!FromSuper)
3493  return true;
3494  }
3495 
3496  ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3497  if ((bool)FromSuper != (bool)ToSuper ||
3498  (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3499  Importer.ToDiag(To->getLocation(),
3500  diag::err_odr_objc_superclass_inconsistent)
3501  << To->getDeclName();
3502  if (ToSuper)
3503  Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3504  << To->getSuperClass()->getDeclName();
3505  else
3506  Importer.ToDiag(To->getLocation(),
3507  diag::note_odr_objc_missing_superclass);
3508  if (From->getSuperClass())
3509  Importer.FromDiag(From->getSuperClassLoc(),
3510  diag::note_odr_objc_superclass)
3511  << From->getSuperClass()->getDeclName();
3512  else
3513  Importer.FromDiag(From->getLocation(),
3514  diag::note_odr_objc_missing_superclass);
3515  }
3516 
3517  if (shouldForceImportDeclContext(Kind))
3518  ImportDeclContext(From);
3519  return false;
3520  }
3521 
3522  // Start the definition.
3523  To->startDefinition();
3524 
3525  // If this class has a superclass, import it.
3526  if (From->getSuperClass()) {
3527  TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3528  if (!SuperTInfo)
3529  return true;
3530 
3531  To->setSuperClass(SuperTInfo);
3532  }
3533 
3534  // Import protocols
3536  SmallVector<SourceLocation, 4> ProtocolLocs;
3538  FromProtoLoc = From->protocol_loc_begin();
3539 
3540  for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3541  FromProtoEnd = From->protocol_end();
3542  FromProto != FromProtoEnd;
3543  ++FromProto, ++FromProtoLoc) {
3544  ObjCProtocolDecl *ToProto
3545  = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3546  if (!ToProto)
3547  return true;
3548  Protocols.push_back(ToProto);
3549  ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3550  }
3551 
3552  // FIXME: If we're merging, make sure that the protocol list is the same.
3553  To->setProtocolList(Protocols.data(), Protocols.size(),
3554  ProtocolLocs.data(), Importer.getToContext());
3555 
3556  // Import categories. When the categories themselves are imported, they'll
3557  // hook themselves into this interface.
3558  for (auto *Cat : From->known_categories())
3559  Importer.Import(Cat);
3560 
3561  // If we have an @implementation, import it as well.
3562  if (From->getImplementation()) {
3563  ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3564  Importer.Import(From->getImplementation()));
3565  if (!Impl)
3566  return true;
3567 
3568  To->setImplementation(Impl);
3569  }
3570 
3571  if (shouldForceImportDeclContext(Kind)) {
3572  // Import all of the members of this class.
3573  ImportDeclContext(From, /*ForceImport=*/true);
3574  }
3575  return false;
3576 }
3577 
3580  if (!list)
3581  return nullptr;
3582 
3584  for (auto fromTypeParam : *list) {
3585  auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3586  Importer.Import(fromTypeParam));
3587  if (!toTypeParam)
3588  return nullptr;
3589 
3590  toTypeParams.push_back(toTypeParam);
3591  }
3592 
3593  return ObjCTypeParamList::create(Importer.getToContext(),
3594  Importer.Import(list->getLAngleLoc()),
3595  toTypeParams,
3596  Importer.Import(list->getRAngleLoc()));
3597 }
3598 
3600  // If this class has a definition in the translation unit we're coming from,
3601  // but this particular declaration is not that definition, import the
3602  // definition and map to that.
3603  ObjCInterfaceDecl *Definition = D->getDefinition();
3604  if (Definition && Definition != D) {
3605  Decl *ImportedDef = Importer.Import(Definition);
3606  if (!ImportedDef)
3607  return nullptr;
3608 
3609  return Importer.Imported(D, ImportedDef);
3610  }
3611 
3612  // Import the major distinguishing characteristics of an @interface.
3613  DeclContext *DC, *LexicalDC;
3614  DeclarationName Name;
3615  SourceLocation Loc;
3616  NamedDecl *ToD;
3617  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3618  return nullptr;
3619  if (ToD)
3620  return ToD;
3621 
3622  // Look for an existing interface with the same name.
3623  ObjCInterfaceDecl *MergeWithIface = nullptr;
3624  SmallVector<NamedDecl *, 2> FoundDecls;
3625  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3626  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3627  if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3628  continue;
3629 
3630  if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
3631  break;
3632  }
3633 
3634  // Create an interface declaration, if one does not already exist.
3635  ObjCInterfaceDecl *ToIface = MergeWithIface;
3636  if (!ToIface) {
3637  ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3638  Importer.Import(D->getAtStartLoc()),
3639  Name.getAsIdentifierInfo(),
3640  /*TypeParamList=*/nullptr,
3641  /*PrevDecl=*/nullptr, Loc,
3643  ToIface->setLexicalDeclContext(LexicalDC);
3644  LexicalDC->addDeclInternal(ToIface);
3645  }
3646  Importer.Imported(D, ToIface);
3647  // Import the type parameter list after calling Imported, to avoid
3648  // loops when bringing in their DeclContext.
3651 
3652  if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3653  return nullptr;
3654 
3655  return ToIface;
3656 }
3657 
3659  ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3660  Importer.Import(D->getCategoryDecl()));
3661  if (!Category)
3662  return nullptr;
3663 
3664  ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3665  if (!ToImpl) {
3666  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3667  if (!DC)
3668  return nullptr;
3669 
3670  SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
3671  ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3672  Importer.Import(D->getIdentifier()),
3673  Category->getClassInterface(),
3674  Importer.Import(D->getLocation()),
3675  Importer.Import(D->getAtStartLoc()),
3676  CategoryNameLoc);
3677 
3678  DeclContext *LexicalDC = DC;
3679  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3680  LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3681  if (!LexicalDC)
3682  return nullptr;
3683 
3684  ToImpl->setLexicalDeclContext(LexicalDC);
3685  }
3686 
3687  LexicalDC->addDeclInternal(ToImpl);
3688  Category->setImplementation(ToImpl);
3689  }
3690 
3691  Importer.Imported(D, ToImpl);
3692  ImportDeclContext(D);
3693  return ToImpl;
3694 }
3695 
3697  // Find the corresponding interface.
3698  ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3699  Importer.Import(D->getClassInterface()));
3700  if (!Iface)
3701  return nullptr;
3702 
3703  // Import the superclass, if any.
3704  ObjCInterfaceDecl *Super = nullptr;
3705  if (D->getSuperClass()) {
3706  Super = cast_or_null<ObjCInterfaceDecl>(
3707  Importer.Import(D->getSuperClass()));
3708  if (!Super)
3709  return nullptr;
3710  }
3711 
3712  ObjCImplementationDecl *Impl = Iface->getImplementation();
3713  if (!Impl) {
3714  // We haven't imported an implementation yet. Create a new @implementation
3715  // now.
3716  Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3717  Importer.ImportContext(D->getDeclContext()),
3718  Iface, Super,
3719  Importer.Import(D->getLocation()),
3720  Importer.Import(D->getAtStartLoc()),
3721  Importer.Import(D->getSuperClassLoc()),
3722  Importer.Import(D->getIvarLBraceLoc()),
3723  Importer.Import(D->getIvarRBraceLoc()));
3724 
3725  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3726  DeclContext *LexicalDC
3727  = Importer.ImportContext(D->getLexicalDeclContext());
3728  if (!LexicalDC)
3729  return nullptr;
3730  Impl->setLexicalDeclContext(LexicalDC);
3731  }
3732 
3733  // Associate the implementation with the class it implements.
3734  Iface->setImplementation(Impl);
3735  Importer.Imported(D, Iface->getImplementation());
3736  } else {
3737  Importer.Imported(D, Iface->getImplementation());
3738 
3739  // Verify that the existing @implementation has the same superclass.
3740  if ((Super && !Impl->getSuperClass()) ||
3741  (!Super && Impl->getSuperClass()) ||
3742  (Super && Impl->getSuperClass() &&
3744  Impl->getSuperClass()))) {
3745  Importer.ToDiag(Impl->getLocation(),
3746  diag::err_odr_objc_superclass_inconsistent)
3747  << Iface->getDeclName();
3748  // FIXME: It would be nice to have the location of the superclass
3749  // below.
3750  if (Impl->getSuperClass())
3751  Importer.ToDiag(Impl->getLocation(),
3752  diag::note_odr_objc_superclass)
3753  << Impl->getSuperClass()->getDeclName();
3754  else
3755  Importer.ToDiag(Impl->getLocation(),
3756  diag::note_odr_objc_missing_superclass);
3757  if (D->getSuperClass())
3758  Importer.FromDiag(D->getLocation(),
3759  diag::note_odr_objc_superclass)
3760  << D->getSuperClass()->getDeclName();
3761  else
3762  Importer.FromDiag(D->getLocation(),
3763  diag::note_odr_objc_missing_superclass);
3764  return nullptr;
3765  }
3766  }
3767 
3768  // Import all of the members of this @implementation.
3769  ImportDeclContext(D);
3770 
3771  return Impl;
3772 }
3773 
3775  // Import the major distinguishing characteristics of an @property.
3776  DeclContext *DC, *LexicalDC;
3777  DeclarationName Name;
3778  SourceLocation Loc;
3779  NamedDecl *ToD;
3780  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3781  return nullptr;
3782  if (ToD)
3783  return ToD;
3784 
3785  // Check whether we have already imported this property.
3786  SmallVector<NamedDecl *, 2> FoundDecls;
3787  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
3788  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3789  if (ObjCPropertyDecl *FoundProp
3790  = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
3791  // Check property types.
3792  if (!Importer.IsStructurallyEquivalent(D->getType(),
3793  FoundProp->getType())) {
3794  Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3795  << Name << D->getType() << FoundProp->getType();
3796  Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3797  << FoundProp->getType();
3798  return nullptr;
3799  }
3800 
3801  // FIXME: Check property attributes, getters, setters, etc.?
3802 
3803  // Consider these properties to be equivalent.
3804  Importer.Imported(D, FoundProp);
3805  return FoundProp;
3806  }
3807  }
3808 
3809  // Import the type.
3810  TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3811  if (!TSI)
3812  return nullptr;
3813 
3814  // Create the new property.
3815  ObjCPropertyDecl *ToProperty
3816  = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3817  Name.getAsIdentifierInfo(),
3818  Importer.Import(D->getAtLoc()),
3819  Importer.Import(D->getLParenLoc()),
3820  Importer.Import(D->getType()),
3821  TSI,
3823  Importer.Imported(D, ToProperty);
3824  ToProperty->setLexicalDeclContext(LexicalDC);
3825  LexicalDC->addDeclInternal(ToProperty);
3826 
3827  ToProperty->setPropertyAttributes(D->getPropertyAttributes());
3828  ToProperty->setPropertyAttributesAsWritten(
3830  ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3831  Importer.Import(D->getGetterNameLoc()));
3832  ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3833  Importer.Import(D->getSetterNameLoc()));
3834  ToProperty->setGetterMethodDecl(
3835  cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3836  ToProperty->setSetterMethodDecl(
3837  cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3838  ToProperty->setPropertyIvarDecl(
3839  cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3840  return ToProperty;
3841 }
3842 
3844  ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3845  Importer.Import(D->getPropertyDecl()));
3846  if (!Property)
3847  return nullptr;
3848 
3849  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3850  if (!DC)
3851  return nullptr;
3852 
3853  // Import the lexical declaration context.
3854  DeclContext *LexicalDC = DC;
3855  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3856  LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3857  if (!LexicalDC)
3858  return nullptr;
3859  }
3860 
3861  ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3862  if (!InImpl)
3863  return nullptr;
3864 
3865  // Import the ivar (for an @synthesize).
3866  ObjCIvarDecl *Ivar = nullptr;
3867  if (D->getPropertyIvarDecl()) {
3868  Ivar = cast_or_null<ObjCIvarDecl>(
3869  Importer.Import(D->getPropertyIvarDecl()));
3870  if (!Ivar)
3871  return nullptr;
3872  }
3873 
3874  ObjCPropertyImplDecl *ToImpl
3875  = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3876  Property->getQueryKind());
3877  if (!ToImpl) {
3878  ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3879  Importer.Import(D->getLocStart()),
3880  Importer.Import(D->getLocation()),
3881  Property,
3883  Ivar,
3884  Importer.Import(D->getPropertyIvarDeclLoc()));
3885  ToImpl->setLexicalDeclContext(LexicalDC);
3886  Importer.Imported(D, ToImpl);
3887  LexicalDC->addDeclInternal(ToImpl);
3888  } else {
3889  // Check that we have the same kind of property implementation (@synthesize
3890  // vs. @dynamic).
3891  if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3892  Importer.ToDiag(ToImpl->getLocation(),
3893  diag::err_odr_objc_property_impl_kind_inconsistent)
3894  << Property->getDeclName()
3895  << (ToImpl->getPropertyImplementation()
3897  Importer.FromDiag(D->getLocation(),
3898  diag::note_odr_objc_property_impl_kind)
3899  << D->getPropertyDecl()->getDeclName()
3901  return nullptr;
3902  }
3903 
3904  // For @synthesize, check that we have the same
3906  Ivar != ToImpl->getPropertyIvarDecl()) {
3907  Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3908  diag::err_odr_objc_synthesize_ivar_inconsistent)
3909  << Property->getDeclName()
3910  << ToImpl->getPropertyIvarDecl()->getDeclName()
3911  << Ivar->getDeclName();
3912  Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3913  diag::note_odr_objc_synthesize_ivar_here)
3914  << D->getPropertyIvarDecl()->getDeclName();
3915  return nullptr;
3916  }
3917 
3918  // Merge the existing implementation with the new implementation.
3919  Importer.Imported(D, ToImpl);
3920  }
3921 
3922  return ToImpl;
3923 }
3924 
3926  // For template arguments, we adopt the translation unit as our declaration
3927  // context. This context will be fixed when the actual template declaration
3928  // is created.
3929 
3930  // FIXME: Import default argument.
3931  return TemplateTypeParmDecl::Create(Importer.getToContext(),
3932  Importer.getToContext().getTranslationUnitDecl(),
3933  Importer.Import(D->getLocStart()),
3934  Importer.Import(D->getLocation()),
3935  D->getDepth(),
3936  D->getIndex(),
3937  Importer.Import(D->getIdentifier()),
3939  D->isParameterPack());
3940 }
3941 
3942 Decl *
3944  // Import the name of this declaration.
3945  DeclarationName Name = Importer.Import(D->getDeclName());
3946  if (D->getDeclName() && !Name)
3947  return nullptr;
3948 
3949  // Import the location of this declaration.
3950  SourceLocation Loc = Importer.Import(D->getLocation());
3951 
3952  // Import the type of this declaration.
3953  QualType T = Importer.Import(D->getType());
3954  if (T.isNull())
3955  return nullptr;
3956 
3957  // Import type-source information.
3958  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3959  if (D->getTypeSourceInfo() && !TInfo)
3960  return nullptr;
3961 
3962  // FIXME: Import default argument.
3963 
3964  return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3965  Importer.getToContext().getTranslationUnitDecl(),
3966  Importer.Import(D->getInnerLocStart()),
3967  Loc, D->getDepth(), D->getPosition(),
3968  Name.getAsIdentifierInfo(),
3969  T, D->isParameterPack(), TInfo);
3970 }
3971 
3972 Decl *
3974  // Import the name of this declaration.
3975  DeclarationName Name = Importer.Import(D->getDeclName());
3976  if (D->getDeclName() && !Name)
3977  return nullptr;
3978 
3979  // Import the location of this declaration.
3980  SourceLocation Loc = Importer.Import(D->getLocation());
3981 
3982  // Import template parameters.
3983  TemplateParameterList *TemplateParams
3985  if (!TemplateParams)
3986  return nullptr;
3987 
3988  // FIXME: Import default argument.
3989 
3990  return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3991  Importer.getToContext().getTranslationUnitDecl(),
3992  Loc, D->getDepth(), D->getPosition(),
3993  D->isParameterPack(),
3994  Name.getAsIdentifierInfo(),
3995  TemplateParams);
3996 }
3997 
3999  // If this record has a definition in the translation unit we're coming from,
4000  // but this particular declaration is not that definition, import the
4001  // definition and map to that.
4002  CXXRecordDecl *Definition
4003  = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4004  if (Definition && Definition != D->getTemplatedDecl()) {
4005  Decl *ImportedDef
4006  = Importer.Import(Definition->getDescribedClassTemplate());
4007  if (!ImportedDef)
4008  return nullptr;
4009 
4010  return Importer.Imported(D, ImportedDef);
4011  }
4012 
4013  // Import the major distinguishing characteristics of this class template.
4014  DeclContext *DC, *LexicalDC;
4015  DeclarationName Name;
4016  SourceLocation Loc;
4017  NamedDecl *ToD;
4018  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4019  return nullptr;
4020  if (ToD)
4021  return ToD;
4022 
4023  // We may already have a template of the same name; try to find and match it.
4024  if (!DC->isFunctionOrMethod()) {
4025  SmallVector<NamedDecl *, 4> ConflictingDecls;
4026  SmallVector<NamedDecl *, 2> FoundDecls;
4027  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
4028  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4029  if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4030  continue;
4031 
4032  Decl *Found = FoundDecls[I];
4033  if (ClassTemplateDecl *FoundTemplate
4034  = dyn_cast<ClassTemplateDecl>(Found)) {
4035  if (IsStructuralMatch(D, FoundTemplate)) {
4036  // The class templates structurally match; call it the same template.
4037  // FIXME: We may be filling in a forward declaration here. Handle
4038  // this case!
4039  Importer.Imported(D->getTemplatedDecl(),
4040  FoundTemplate->getTemplatedDecl());
4041  return Importer.Imported(D, FoundTemplate);
4042  }
4043  }
4044 
4045  ConflictingDecls.push_back(FoundDecls[I]);
4046  }
4047 
4048  if (!ConflictingDecls.empty()) {
4049  Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4050  ConflictingDecls.data(),
4051  ConflictingDecls.size());
4052  }
4053 
4054  if (!Name)
4055  return nullptr;
4056  }
4057 
4058  CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4059 
4060  // Create the declaration that is being templated.
4061  CXXRecordDecl *ToTemplated = cast_or_null<CXXRecordDecl>(
4062  Importer.Import(FromTemplated));
4063  if (!ToTemplated)
4064  return nullptr;
4065 
4066  // Resolve possible cyclic import.
4067  if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4068  return AlreadyImported;
4069 
4070  // Create the class template declaration itself.
4071  TemplateParameterList *TemplateParams =
4073  if (!TemplateParams)
4074  return nullptr;
4075 
4076  ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4077  Loc, Name, TemplateParams,
4078  ToTemplated);
4079  ToTemplated->setDescribedClassTemplate(D2);
4080 
4081  D2->setAccess(D->getAccess());
4082  D2->setLexicalDeclContext(LexicalDC);
4083  LexicalDC->addDeclInternal(D2);
4084 
4085  // Note the relationship between the class templates.
4086  Importer.Imported(D, D2);
4087  Importer.Imported(FromTemplated, ToTemplated);
4088 
4089  if (FromTemplated->isCompleteDefinition() &&
4090  !ToTemplated->isCompleteDefinition()) {
4091  // FIXME: Import definition!
4092  }
4093 
4094  return D2;
4095 }
4096 
4099  // If this record has a definition in the translation unit we're coming from,
4100  // but this particular declaration is not that definition, import the
4101  // definition and map to that.
4102  TagDecl *Definition = D->getDefinition();
4103  if (Definition && Definition != D) {
4104  Decl *ImportedDef = Importer.Import(Definition);
4105  if (!ImportedDef)
4106  return nullptr;
4107 
4108  return Importer.Imported(D, ImportedDef);
4109  }
4110 
4111  ClassTemplateDecl *ClassTemplate
4112  = cast_or_null<ClassTemplateDecl>(Importer.Import(
4113  D->getSpecializedTemplate()));
4114  if (!ClassTemplate)
4115  return nullptr;
4116 
4117  // Import the context of this declaration.
4118  DeclContext *DC = ClassTemplate->getDeclContext();
4119  if (!DC)
4120  return nullptr;
4121 
4122  DeclContext *LexicalDC = DC;
4123  if (D->getDeclContext() != D->getLexicalDeclContext()) {
4124  LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4125  if (!LexicalDC)
4126  return nullptr;
4127  }
4128 
4129  // Import the location of this declaration.
4130  SourceLocation StartLoc = Importer.Import(D->getLocStart());
4131  SourceLocation IdLoc = Importer.Import(D->getLocation());
4132 
4133  // Import template arguments.
4134  SmallVector<TemplateArgument, 2> TemplateArgs;
4136  D->getTemplateArgs().size(),
4137  TemplateArgs))
4138  return nullptr;
4139 
4140  // Try to find an existing specialization with these template arguments.
4141  void *InsertPos = nullptr;
4143  = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
4144  if (D2) {
4145  // We already have a class template specialization with these template
4146  // arguments.
4147 
4148  // FIXME: Check for specialization vs. instantiation errors.
4149 
4150  if (RecordDecl *FoundDef = D2->getDefinition()) {
4151  if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
4152  // The record types structurally match, or the "from" translation
4153  // unit only had a forward declaration anyway; call it the same
4154  // function.
4155  return Importer.Imported(D, FoundDef);
4156  }
4157  }
4158  } else {
4159  // Create a new specialization.
4160  if (ClassTemplatePartialSpecializationDecl *PartialSpec =
4161  dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
4162 
4163  // Import TemplateArgumentListInfo
4164  TemplateArgumentListInfo ToTAInfo;
4165  const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4166  if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
4167  return nullptr;
4168 
4169  QualType CanonInjType = Importer.Import(
4170  PartialSpec->getInjectedSpecializationType());
4171  if (CanonInjType.isNull())
4172  return nullptr;
4173  CanonInjType = CanonInjType.getCanonicalType();
4174 
4176  PartialSpec->getTemplateParameters());
4177  if (!ToTPList && PartialSpec->getTemplateParameters())
4178  return nullptr;
4179 
4181  Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
4182  ToTPList, ClassTemplate,
4183  llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4184  ToTAInfo, CanonInjType, nullptr);
4185 
4186  } else {
4187  D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4188  D->getTagKind(), DC,
4189  StartLoc, IdLoc,
4190  ClassTemplate,
4191  TemplateArgs,
4192  /*PrevDecl=*/nullptr);
4193  }
4194 
4196 
4197  // Add this specialization to the class template.
4198  ClassTemplate->AddSpecialization(D2, InsertPos);
4199 
4200  // Import the qualifier, if any.
4201  D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4202 
4203  Importer.Imported(D, D2);
4204 
4205  if (auto *TSI = D->getTypeAsWritten()) {
4206  TypeSourceInfo *TInfo = Importer.Import(TSI);
4207  if (!TInfo)
4208  return nullptr;
4209  D2->setTypeAsWritten(TInfo);
4210  D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4211  D2->setExternLoc(Importer.Import(D->getExternLoc()));
4212  }
4213 
4214  SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4215  if (POI.isValid())
4216  D2->setPointOfInstantiation(POI);
4217  else if (D->getPointOfInstantiation().isValid())
4218  return nullptr;
4219 
4221 
4222  // Add the specialization to this context.
4223  D2->setLexicalDeclContext(LexicalDC);
4224  LexicalDC->addDeclInternal(D2);
4225  }
4226  Importer.Imported(D, D2);
4227  if (D->isCompleteDefinition() && ImportDefinition(D, D2))
4228  return nullptr;
4229 
4230  return D2;
4231 }
4232 
4234  // If this variable has a definition in the translation unit we're coming
4235  // from,
4236  // but this particular declaration is not that definition, import the
4237  // definition and map to that.
4238  VarDecl *Definition =
4239  cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4240  if (Definition && Definition != D->getTemplatedDecl()) {
4241  Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4242  if (!ImportedDef)
4243  return nullptr;
4244 
4245  return Importer.Imported(D, ImportedDef);
4246  }
4247 
4248  // Import the major distinguishing characteristics of this variable template.
4249  DeclContext *DC, *LexicalDC;
4250  DeclarationName Name;
4251  SourceLocation Loc;
4252  NamedDecl *ToD;
4253  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4254  return nullptr;
4255  if (ToD)
4256  return ToD;
4257 
4258  // We may already have a template of the same name; try to find and match it.
4259  assert(!DC->isFunctionOrMethod() &&
4260  "Variable templates cannot be declared at function scope");
4261  SmallVector<NamedDecl *, 4> ConflictingDecls;
4262  SmallVector<NamedDecl *, 2> FoundDecls;
4263  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
4264  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4265  if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4266  continue;
4267 
4268  Decl *Found = FoundDecls[I];
4269  if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4270  if (IsStructuralMatch(D, FoundTemplate)) {
4271  // The variable templates structurally match; call it the same template.
4272  Importer.Imported(D->getTemplatedDecl(),
4273  FoundTemplate->getTemplatedDecl());
4274  return Importer.Imported(D, FoundTemplate);
4275  }
4276  }
4277 
4278  ConflictingDecls.push_back(FoundDecls[I]);
4279  }
4280 
4281  if (!ConflictingDecls.empty()) {
4282  Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4283  ConflictingDecls.data(),
4284  ConflictingDecls.size());
4285  }
4286 
4287  if (!Name)
4288  return nullptr;
4289 
4290  VarDecl *DTemplated = D->getTemplatedDecl();
4291 
4292  // Import the type.
4293  QualType T = Importer.Import(DTemplated->getType());
4294  if (T.isNull())
4295  return nullptr;
4296 
4297  // Create the declaration that is being templated.
4298  auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4299  if (!ToTemplated)
4300  return nullptr;
4301 
4302  // Create the variable template declaration itself.
4303  TemplateParameterList *TemplateParams =
4305  if (!TemplateParams)
4306  return nullptr;
4307 
4309  Importer.getToContext(), DC, Loc, Name, TemplateParams, ToTemplated);
4310  ToTemplated->setDescribedVarTemplate(ToVarTD);
4311 
4312  ToVarTD->setAccess(D->getAccess());
4313  ToVarTD->setLexicalDeclContext(LexicalDC);
4314  LexicalDC->addDeclInternal(ToVarTD);
4315 
4316  // Note the relationship between the variable templates.
4317  Importer.Imported(D, ToVarTD);
4318  Importer.Imported(DTemplated, ToTemplated);
4319 
4320  if (DTemplated->isThisDeclarationADefinition() &&
4321  !ToTemplated->isThisDeclarationADefinition()) {
4322  // FIXME: Import definition!
4323  }
4324 
4325  return ToVarTD;
4326 }
4327 
4330  // If this record has a definition in the translation unit we're coming from,
4331  // but this particular declaration is not that definition, import the
4332  // definition and map to that.
4333  VarDecl *Definition = D->getDefinition();
4334  if (Definition && Definition != D) {
4335  Decl *ImportedDef = Importer.Import(Definition);
4336  if (!ImportedDef)
4337  return nullptr;
4338 
4339  return Importer.Imported(D, ImportedDef);
4340  }
4341 
4342  VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4343  Importer.Import(D->getSpecializedTemplate()));
4344  if (!VarTemplate)
4345  return nullptr;
4346 
4347  // Import the context of this declaration.
4348  DeclContext *DC = VarTemplate->getDeclContext();
4349  if (!DC)
4350  return nullptr;
4351 
4352  DeclContext *LexicalDC = DC;
4353  if (D->getDeclContext() != D->getLexicalDeclContext()) {
4354  LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4355  if (!LexicalDC)
4356  return nullptr;
4357  }
4358 
4359  // Import the location of this declaration.
4360  SourceLocation StartLoc = Importer.Import(D->getLocStart());
4361  SourceLocation IdLoc = Importer.Import(D->getLocation());
4362 
4363  // Import template arguments.
4364  SmallVector<TemplateArgument, 2> TemplateArgs;
4366  D->getTemplateArgs().size(), TemplateArgs))
4367  return nullptr;
4368 
4369  // Try to find an existing specialization with these template arguments.
4370  void *InsertPos = nullptr;
4372  TemplateArgs, InsertPos);
4373  if (D2) {
4374  // We already have a variable template specialization with these template
4375  // arguments.
4376 
4377  // FIXME: Check for specialization vs. instantiation errors.
4378 
4379  if (VarDecl *FoundDef = D2->getDefinition()) {
4380  if (!D->isThisDeclarationADefinition() ||
4381  IsStructuralMatch(D, FoundDef)) {
4382  // The record types structurally match, or the "from" translation
4383  // unit only had a forward declaration anyway; call it the same
4384  // variable.
4385  return Importer.Imported(D, FoundDef);
4386  }
4387  }
4388  } else {
4389 
4390  // Import the type.
4391  QualType T = Importer.Import(D->getType());
4392  if (T.isNull())
4393  return nullptr;
4394 
4395  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4396  if (D->getTypeSourceInfo() && !TInfo)
4397  return nullptr;
4398 
4399  TemplateArgumentListInfo ToTAInfo;
4401  return nullptr;
4402 
4403  using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
4404  // Create a new specialization.
4405  if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4406  // Import TemplateArgumentListInfo
4407  TemplateArgumentListInfo ArgInfos;
4408  const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4409  // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4410  if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4411  return nullptr;
4412 
4414  FromPartial->getTemplateParameters());
4415  if (!ToTPList)
4416  return nullptr;
4417 
4418  auto *ToPartial = PartVarSpecDecl::Create(
4419  Importer.getToContext(), DC, StartLoc, IdLoc, ToTPList, VarTemplate,
4420  T, TInfo, D->getStorageClass(), TemplateArgs, ArgInfos);
4421 
4422  auto *FromInst = FromPartial->getInstantiatedFromMember();
4423  auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4424  if (FromInst && !ToInst)
4425  return nullptr;
4426 
4427  ToPartial->setInstantiatedFromMember(ToInst);
4428  if (FromPartial->isMemberSpecialization())
4429  ToPartial->setMemberSpecialization();
4430 
4431  D2 = ToPartial;
4432 
4433  } else { // Full specialization
4435  Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4436  D->getStorageClass(), TemplateArgs);
4437  }
4438 
4440  if (POI.isValid())
4441  D2->setPointOfInstantiation(Importer.Import(POI));
4442 
4444  D2->setTemplateArgsInfo(ToTAInfo);
4445 
4446  // Add this specialization to the class template.
4447  VarTemplate->AddSpecialization(D2, InsertPos);
4448 
4449  // Import the qualifier, if any.
4450  D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4451 
4452  if (D->isConstexpr())
4453  D2->setConstexpr(true);
4454 
4455  // Add the specialization to this context.
4456  D2->setLexicalDeclContext(LexicalDC);
4457  LexicalDC->addDeclInternal(D2);
4458 
4459  D2->setAccess(D->getAccess());
4460  }
4461 
4462  Importer.Imported(D, D2);
4463 
4464  // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4465  // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4466  // we have to check the declaration for initializer - otherwise, it won't be
4467  // imported.
4468  if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4469  ImportDefinition(D, D2))
4470  return nullptr;
4471 
4472  return D2;
4473 }
4474 
4476  DeclContext *DC, *LexicalDC;
4477  DeclarationName Name;
4478  SourceLocation Loc;
4479  NamedDecl *ToD;
4480 
4481  if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4482  return nullptr;
4483 
4484  if (ToD)
4485  return ToD;
4486 
4487  // Try to find a function in our own ("to") context with the same name, same
4488  // type, and in the same context as the function we're importing.
4489  if (!LexicalDC->isFunctionOrMethod()) {
4490  unsigned IDNS = Decl::IDNS_Ordinary;
4491  SmallVector<NamedDecl *, 2> FoundDecls;
4492  DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
4493  for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4494  if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
4495  continue;
4496 
4497  if (FunctionTemplateDecl *FoundFunction =
4498  dyn_cast<FunctionTemplateDecl>(FoundDecls[I])) {
4499  if (FoundFunction->hasExternalFormalLinkage() &&
4500  D->hasExternalFormalLinkage()) {
4501  if (IsStructuralMatch(D, FoundFunction)) {
4502  Importer.Imported(D, FoundFunction);
4503  // FIXME: Actually try to merge the body and other attributes.
4504  return FoundFunction;
4505  }
4506  }
4507  }
4508  }
4509  }
4510 
4511  TemplateParameterList *Params =
4513  if (!Params)
4514  return nullptr;
4515 
4516  FunctionDecl *TemplatedFD =
4517  cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4518  if (!TemplatedFD)
4519  return nullptr;
4520 
4522  Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD);
4523 
4524  TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4525  ToFunc->setAccess(D->getAccess());
4526  ToFunc->setLexicalDeclContext(LexicalDC);
4527  Importer.Imported(D, ToFunc);
4528 
4529  LexicalDC->addDeclInternal(ToFunc);
4530  return ToFunc;
4531 }
4532 
4533 //----------------------------------------------------------------------------
4534 // Import Statements
4535 //----------------------------------------------------------------------------
4536 
4538  if (DG.isNull())
4539  return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4540  size_t NumDecls = DG.end() - DG.begin();
4541  SmallVector<Decl *, 1> ToDecls(NumDecls);
4542  auto &_Importer = this->Importer;
4543  std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4544  [&_Importer](Decl *D) -> Decl * {
4545  return _Importer.Import(D);
4546  });
4547  return DeclGroupRef::Create(Importer.getToContext(),
4548  ToDecls.begin(),
4549  NumDecls);
4550 }
4551 
4553  Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4554  << S->getStmtClassName();
4555  return nullptr;
4556  }
4557 
4558 
4561  for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4562  IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
4563  // ToII is nullptr when no symbolic name is given for output operand
4564  // see ParseStmtAsm::ParseAsmOperandsOpt
4565  if (!ToII && S->getOutputIdentifier(I))
4566  return nullptr;
4567  Names.push_back(ToII);
4568  }
4569  for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4570  IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
4571  // ToII is nullptr when no symbolic name is given for input operand
4572  // see ParseStmtAsm::ParseAsmOperandsOpt
4573  if (!ToII && S->getInputIdentifier(I))
4574  return nullptr;
4575  Names.push_back(ToII);
4576  }
4577 
4579  for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
4580  StringLiteral *Clobber = cast_or_null<StringLiteral>(
4581  Importer.Import(S->getClobberStringLiteral(I)));
4582  if (!Clobber)
4583  return nullptr;
4584  Clobbers.push_back(Clobber);
4585  }
4586 
4587  SmallVector<StringLiteral *, 4> Constraints;
4588  for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4589  StringLiteral *Output = cast_or_null<StringLiteral>(
4590  Importer.Import(S->getOutputConstraintLiteral(I)));
4591  if (!Output)
4592  return nullptr;
4593  Constraints.push_back(Output);
4594  }
4595 
4596  for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4597  StringLiteral *Input = cast_or_null<StringLiteral>(
4598  Importer.Import(S->getInputConstraintLiteral(I)));
4599  if (!Input)
4600  return nullptr;
4601  Constraints.push_back(Input);
4602  }
4603 
4605  if (ImportContainerChecked(S->outputs(), Exprs))
4606  return nullptr;
4607 
4608  if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
4609  return nullptr;
4610 
4611  StringLiteral *AsmStr = cast_or_null<StringLiteral>(
4612  Importer.Import(S->getAsmString()));
4613  if (!AsmStr)
4614  return nullptr;
4615 
4616  return new (Importer.getToContext()) GCCAsmStmt(
4617  Importer.getToContext(),
4618  Importer.Import(S->getAsmLoc()),
4619  S->isSimple(),
4620  S->isVolatile(),
4621  S->getNumOutputs(),
4622  S->getNumInputs(),
4623  Names.data(),
4624  Constraints.data(),
4625  Exprs.data(),
4626  AsmStr,
4627  S->getNumClobbers(),
4628  Clobbers.data(),
4629  Importer.Import(S->getRParenLoc()));
4630 }
4631 
4634  for (Decl *ToD : ToDG) {
4635  if (!ToD)
4636  return nullptr;
4637  }
4638  SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4639  SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4640  return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4641 }
4642 
4644  SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4645  return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4646  S->hasLeadingEmptyMacro());
4647 }
4648 
4650  llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
4651 
4652  if (ImportContainerChecked(S->body(), ToStmts))
4653  return nullptr;
4654 
4655  SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4656  SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4657  return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4658  ToRBraceLoc);
4659 }
4660 
4662  Expr *ToLHS = Importer.Import(S->getLHS());
4663  if (!ToLHS)
4664  return nullptr;
4665  Expr *ToRHS = Importer.Import(S->getRHS());
4666  if (!ToRHS && S->getRHS())
4667  return nullptr;
4668  Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4669  if (!ToSubStmt && S->getSubStmt())
4670  return nullptr;
4671  SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4672  SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4673  SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4674  CaseStmt *ToStmt = new (Importer.getToContext())
4675  CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4676  ToStmt->setSubStmt(ToSubStmt);
4677  return ToStmt;
4678 }
4679 
4681  SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4682  SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4683  Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4684  if (!ToSubStmt && S->getSubStmt())
4685  return nullptr;
4686  return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4687  ToSubStmt);
4688 }
4689 
4691  SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4692  LabelDecl *ToLabelDecl =
4693  cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4694  if (!ToLabelDecl && S->getDecl())
4695  return nullptr;
4696  Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4697  if (!ToSubStmt && S->getSubStmt())
4698  return nullptr;
4699  return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4700  ToSubStmt);
4701 }
4702 
4704  SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4705  ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4706  SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4707  ASTContext &_ToContext = Importer.getToContext();
4708  std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4709  [&_ToContext](const Attr *A) -> const Attr * {
4710  return A->clone(_ToContext);
4711  });
4712  for (const Attr *ToA : ToAttrs) {
4713  if (!ToA)
4714  return nullptr;
4715  }
4716  Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4717  if (!ToSubStmt && S->getSubStmt())
4718  return nullptr;
4719  return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4720  ToAttrs, ToSubStmt);
4721 }
4722 
4724  SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4725  Stmt *ToInit = Importer.Import(S->getInit());
4726  if (!ToInit && S->getInit())
4727  return nullptr;
4728  VarDecl *ToConditionVariable = nullptr;
4729  if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4730  ToConditionVariable =
4731  dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4732  if (!ToConditionVariable)
4733  return nullptr;
4734  }
4735  Expr *ToCondition = Importer.Import(S->getCond());
4736  if (!ToCondition && S->getCond())
4737  return nullptr;
4738  Stmt *ToThenStmt = Importer.Import(S->getThen());
4739  if (!ToThenStmt && S->getThen())
4740  return nullptr;
4741  SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4742  Stmt *ToElseStmt = Importer.Import(S->getElse());
4743  if (!ToElseStmt && S->getElse())
4744  return nullptr;
4745  return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4746  ToIfLoc, S->isConstexpr(),
4747  ToInit,
4748  ToConditionVariable,
4749  ToCondition, ToThenStmt,
4750  ToElseLoc, ToElseStmt);
4751 }
4752 
4754  Stmt *ToInit = Importer.Import(S->getInit());
4755  if (!ToInit && S->getInit())
4756  return nullptr;
4757  VarDecl *ToConditionVariable = nullptr;
4758  if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4759  ToConditionVariable =
4760  dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4761  if (!ToConditionVariable)
4762  return nullptr;
4763  }
4764  Expr *ToCondition = Importer.Import(S->getCond());
4765  if (!ToCondition && S->getCond())
4766  return nullptr;
4767  SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
4768  Importer.getToContext(), ToInit,
4769  ToConditionVariable, ToCondition);
4770  Stmt *ToBody = Importer.Import(S->getBody());
4771  if (!ToBody && S->getBody())
4772  return nullptr;
4773  ToStmt->setBody(ToBody);
4774  ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4775  // Now we have to re-chain the cases.
4776  SwitchCase *LastChainedSwitchCase = nullptr;
4777  for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4778  SC = SC->getNextSwitchCase()) {
4779  SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4780  if (!ToSC)
4781  return nullptr;
4782  if (LastChainedSwitchCase)
4783  LastChainedSwitchCase->setNextSwitchCase(ToSC);
4784  else
4785  ToStmt->setSwitchCaseList(ToSC);
4786  LastChainedSwitchCase = ToSC;
4787  }
4788  return ToStmt;
4789 }
4790 
4792  VarDecl *ToConditionVariable = nullptr;
4793  if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4794  ToConditionVariable =
4795  dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4796  if (!ToConditionVariable)
4797  return nullptr;
4798  }
4799  Expr *ToCondition = Importer.Import(S->getCond());
4800  if (!ToCondition && S->getCond())
4801  return nullptr;
4802  Stmt *ToBody = Importer.Import(S->getBody());
4803  if (!ToBody && S->getBody())
4804  return nullptr;
4805  SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4806  return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4807  ToConditionVariable,
4808  ToCondition, ToBody,
4809  ToWhileLoc);
4810 }
4811 
4813  Stmt *ToBody = Importer.Import(S->getBody());
4814  if (!ToBody && S->getBody())
4815  return nullptr;
4816  Expr *ToCondition = Importer.Import(S->getCond());
4817  if (!ToCondition && S->getCond())
4818  return nullptr;
4819  SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4820  SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4821  SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4822  return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4823  ToDoLoc, ToWhileLoc,
4824  ToRParenLoc);
4825 }
4826 
4828  Stmt *ToInit = Importer.Import(S->getInit());
4829  if (!ToInit && S->getInit())
4830  return nullptr;
4831  Expr *ToCondition = Importer.Import(S->getCond());
4832  if (!ToCondition && S->getCond())
4833  return nullptr;
4834  VarDecl *ToConditionVariable = nullptr;
4835  if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4836  ToConditionVariable =
4837  dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4838  if (!ToConditionVariable)
4839  return nullptr;
4840  }
4841  Expr *ToInc = Importer.Import(S->getInc());
4842  if (!ToInc && S->getInc())
4843  return nullptr;
4844  Stmt *ToBody = Importer.Import(S->getBody());
4845  if (!ToBody && S->getBody())
4846  return nullptr;
4847  SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4848  SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4849  SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4850  return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4851  ToInit, ToCondition,
4852  ToConditionVariable,
4853  ToInc, ToBody,
4854  ToForLoc, ToLParenLoc,
4855  ToRParenLoc);
4856 }
4857 
4859  LabelDecl *ToLabel = nullptr;
4860  if (LabelDecl *FromLabel = S->getLabel()) {
4861  ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4862  if (!ToLabel)
4863  return nullptr;
4864  }
4865  SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4866  SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4867  return new (Importer.getToContext()) GotoStmt(ToLabel,
4868  ToGotoLoc, ToLabelLoc);
4869 }
4870 
4872  SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4873  SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4874  Expr *ToTarget = Importer.Import(S->getTarget());
4875  if (!ToTarget && S->getTarget())
4876  return nullptr;
4877  return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4878  ToTarget);
4879 }
4880 
4882  SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4883  return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4884 }
4885 
4887  SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4888  return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4889 }
4890 
4892  SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4893  Expr *ToRetExpr = Importer.Import(S->getRetValue());
4894  if (!ToRetExpr && S->getRetValue())
4895  return nullptr;
4896  VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4897  VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4898  if (!ToNRVOCandidate && NRVOCandidate)
4899  return nullptr;
4900  return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4901  ToNRVOCandidate);
4902 }
4903 
4905  SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4906  VarDecl *ToExceptionDecl = nullptr;
4907  if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4908  ToExceptionDecl =
4909  dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4910  if (!ToExceptionDecl)
4911  return nullptr;
4912  }
4913  Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4914  if (!ToHandlerBlock && S->getHandlerBlock())
4915  return nullptr;
4916  return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4917  ToExceptionDecl,
4918  ToHandlerBlock);
4919 }
4920 
4922  SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4923  Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4924  if (!ToTryBlock && S->getTryBlock())
4925  return nullptr;
4926  SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4927  for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4928  CXXCatchStmt *FromHandler = S->getHandler(HI);
4929  if (Stmt *ToHandler = Importer.Import(FromHandler))
4930  ToHandlers[HI] = ToHandler;
4931  else
4932  return nullptr;
4933  }
4934  return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4935  ToHandlers);
4936 }
4937 
4939  DeclStmt *ToRange =
4940  dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4941  if (!ToRange && S->getRangeStmt())
4942  return nullptr;
4943  DeclStmt *ToBegin =
4944  dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4945  if (!ToBegin && S->getBeginStmt())
4946  return nullptr;
4947  DeclStmt *ToEnd =
4948  dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4949  if (!ToEnd && S->getEndStmt())
4950  return nullptr;
4951  Expr *ToCond = Importer.Import(S->getCond());
4952  if (!ToCond && S->getCond())
4953  return nullptr;
4954  Expr *ToInc = Importer.Import(S->getInc());
4955  if (!ToInc && S->getInc())
4956  return nullptr;
4957  DeclStmt *ToLoopVar =
4958  dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4959  if (!ToLoopVar && S->getLoopVarStmt())
4960  return nullptr;
4961  Stmt *ToBody = Importer.Import(S->getBody());
4962  if (!ToBody && S->getBody())
4963  return nullptr;
4964  SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4965  SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
4966  SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4967  SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4968  return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
4969  ToCond, ToInc,
4970  ToLoopVar, ToBody,
4971  ToForLoc, ToCoawaitLoc,
4972  ToColonLoc, ToRParenLoc);
4973 }
4974 
4976  Stmt *ToElem = Importer.Import(S->getElement());
4977  if (!ToElem && S->getElement())
4978  return nullptr;
4979  Expr *ToCollect = Importer.Import(S->getCollection());
4980  if (!ToCollect && S->getCollection())
4981  return nullptr;
4982  Stmt *ToBody = Importer.Import(S->getBody());
4983  if (!ToBody && S->getBody())
4984  return nullptr;
4985  SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4986  SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4987  return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4988  ToCollect,
4989  ToBody, ToForLoc,
4990  ToRParenLoc);
4991 }
4992 
4994  SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4995  SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4996  VarDecl *ToExceptionDecl = nullptr;
4997  if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4998  ToExceptionDecl =
4999  dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5000  if (!ToExceptionDecl)
5001  return nullptr;
5002  }
5003  Stmt *ToBody = Importer.Import(S->getCatchBody());
5004  if (!ToBody && S->getCatchBody())
5005  return nullptr;
5006  return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5007  ToRParenLoc,
5008  ToExceptionDecl,
5009  ToBody);
5010 }
5011 
5013  SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5014  Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5015  if (!ToAtFinallyStmt && S->getFinallyBody())
5016  return nullptr;
5017  return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5018  ToAtFinallyStmt);
5019 }
5020 
5022  SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5023  Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5024  if (!ToAtTryStmt && S->getTryBody())
5025  return nullptr;
5026  SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5027  for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5028  ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5029  if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5030  ToCatchStmts[CI] = ToCatchStmt;
5031  else
5032  return nullptr;
5033  }
5034  Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5035  if (!ToAtFinallyStmt && S->getFinallyStmt())
5036  return nullptr;
5037  return ObjCAtTryStmt::Create(Importer.getToContext(),
5038  ToAtTryLoc, ToAtTryStmt,
5039  ToCatchStmts.begin(), ToCatchStmts.size(),
5040  ToAtFinallyStmt);
5041 }
5042 
5045  SourceLocation ToAtSynchronizedLoc =
5046  Importer.Import(S->getAtSynchronizedLoc());
5047  Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5048  if (!ToSynchExpr && S->getSynchExpr())
5049  return nullptr;
5050  Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5051  if (!ToSynchBody && S->getSynchBody())
5052  return nullptr;
5053  return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5054  ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5055 }
5056 
5058  SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5059  Expr *ToThrow = Importer.Import(S->getThrowExpr());
5060  if (!ToThrow && S->getThrowExpr())
5061  return nullptr;
5062  return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5063 }
5064 
5067  SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5068  Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5069  if (!ToSubStmt && S->getSubStmt())
5070  return nullptr;
5071  return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5072  ToSubStmt);
5073 }
5074 
5075 //----------------------------------------------------------------------------
5076 // Import Expressions
5077 //----------------------------------------------------------------------------
5079  Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5080  << E->getStmtClassName();
5081  return nullptr;
5082 }
5083 
5085  QualType T = Importer.Import(E->getType());
5086  if (T.isNull())
5087  return nullptr;
5088 
5089  Expr *SubExpr = Importer.Import(E->getSubExpr());
5090  if (!SubExpr && E->getSubExpr())
5091  return nullptr;
5092 
5093  TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5094  if (!TInfo)
5095  return nullptr;
5096 
5097  return new (Importer.getToContext()) VAArgExpr(
5098  Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5099  Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5100 }
5101 
5102 
5104  QualType T = Importer.Import(E->getType());
5105  if (T.isNull())
5106  return nullptr;
5107 
5108  return new (Importer.getToContext()) GNUNullExpr(
5109  T, Importer.Import(E->getLocStart()));
5110 }
5111 
5113  QualType T = Importer.Import(E->getType());
5114  if (T.isNull())
5115  return nullptr;
5116 
5117  StringLiteral *SL = cast_or_null<StringLiteral>(
5118  Importer.Import(E->getFunctionName()));
5119  if (!SL && E->getFunctionName())
5120  return nullptr;
5121 
5122  return new (Importer.getToContext()) PredefinedExpr(
5123  Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
5124 }
5125 
5127  ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5128  if (!ToD)
5129  return nullptr;
5130 
5131  NamedDecl *FoundD = nullptr;
5132  if (E->getDecl() != E->getFoundDecl()) {
5133  FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5134  if (!FoundD)
5135  return nullptr;
5136  }
5137 
5138  QualType T = Importer.Import(E->getType());
5139  if (T.isNull())
5140  return nullptr;
5141 
5142 
5143  TemplateArgumentListInfo ToTAInfo;
5144  TemplateArgumentListInfo *ResInfo = nullptr;
5145  if (E->hasExplicitTemplateArgs()) {
5147  return nullptr;
5148  ResInfo = &ToTAInfo;
5149  }
5150 
5151  DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5152  Importer.Import(E->getQualifierLoc()),
5153  Importer.Import(E->getTemplateKeywordLoc()),
5154  ToD,
5156  Importer.Import(E->getLocation()),
5157  T, E->getValueKind(),
5158  FoundD, ResInfo);
5159  if (E->hadMultipleCandidates())
5160  DRE->setHadMultipleCandidates(true);
5161  return DRE;
5162 }
5163 
5165  QualType T = Importer.Import(E->getType());
5166  if (T.isNull())
5167  return nullptr;
5168 
5169  return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5170 }
5171 
5174  if (D.isFieldDesignator()) {
5175  IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5176  // Caller checks for import error
5177  return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5178  Importer.Import(D.getFieldLoc()));
5179  }
5180  if (D.isArrayDesignator())
5181  return Designator(D.getFirstExprIndex(),
5182  Importer.Import(D.getLBracketLoc()),
5183  Importer.Import(D.getRBracketLoc()));
5184 
5185  assert(D.isArrayRangeDesignator());
5186  return Designator(D.getFirstExprIndex(),
5187  Importer.Import(D.getLBracketLoc()),
5188  Importer.Import(D.getEllipsisLoc()),
5189  Importer.Import(D.getRBracketLoc()));
5190 }
5191 
5192 
5194  Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
5195  if (!Init)
5196  return nullptr;
5197 
5198  SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5199  // List elements from the second, the first is Init itself
5200  for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
5201  if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
5202  IndexExprs[I - 1] = Arg;
5203  else
5204  return nullptr;
5205  }
5206 
5207  SmallVector<Designator, 4> Designators(DIE->size());
5208  llvm::transform(DIE->designators(), Designators.begin(),
5209  [this](const Designator &D) -> Designator {
5210  return ImportDesignator(D);
5211  });
5212 
5213  for (const Designator &D : DIE->designators())
5214  if (D.isFieldDesignator() && !D.getFieldName())
5215  return nullptr;
5216 
5218  Importer.getToContext(), Designators,
5219  IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5220  DIE->usesGNUSyntax(), Init);
5221 }
5222 
5224  QualType T = Importer.Import(E->getType());
5225  if (T.isNull())
5226  return nullptr;
5227 
5228  return new (Importer.getToContext())
5229  CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5230 }
5231 
5233  QualType T = Importer.Import(E->getType());
5234  if (T.isNull())
5235  return nullptr;
5236 
5237  return IntegerLiteral::Create(Importer.getToContext(),
5238  E->getValue(), T,
5239  Importer.Import(E->getLocation()));
5240 }
5241 
5243  QualType T = Importer.Import(E->getType());
5244  if (T.isNull())
5245  return nullptr;
5246 
5247  return FloatingLiteral::Create(Importer.getToContext(),
5248  E->getValue(), E->isExact(), T,
5249  Importer.Import(E->getLocation()));
5250 }
5251 
5253  QualType T = Importer.Import(E->getType());
5254  if (T.isNull())
5255  return nullptr;
5256 
5257  return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5258  E->getKind(), T,
5259  Importer.Import(E->getLocation()));
5260 }
5261 
5263  QualType T = Importer.Import(E->getType());
5264  if (T.isNull())
5265  return nullptr;
5266 
5268  ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5269 
5270  return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5271  E->getKind(), E->isPascal(), T,
5272  Locations.data(), Locations.size());
5273 }
5274 
5276  QualType T = Importer.Import(E->getType());
5277  if (T.isNull())
5278  return nullptr;
5279 
5280  TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5281  if (!TInfo)
5282  return nullptr;
5283 
5284  Expr *Init = Importer.Import(E->getInitializer());
5285  if (!Init)
5286  return nullptr;
5287 
5288  return new (Importer.getToContext()) CompoundLiteralExpr(
5289  Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5290  Init, E->isFileScope());
5291 }
5292 
5294  QualType T = Importer.Import(E->getType());
5295  if (T.isNull())
5296  return nullptr;
5297 
5299  if (ImportArrayChecked(
5300  E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5301  Exprs.begin()))
5302  return nullptr;
5303 
5304  return new (Importer.getToContext()) AtomicExpr(
5305  Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5306  Importer.Import(E->getRParenLoc()));
5307 }
5308 
5310  QualType T = Importer.Import(E->getType());
5311  if (T.isNull())
5312  return nullptr;
5313 
5314  LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
5315  if (!ToLabel)
5316  return nullptr;
5317 
5318  return new (Importer.getToContext()) AddrLabelExpr(
5319  Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5320  ToLabel, T);
5321 }
5322 
5324  Expr *SubExpr = Importer.Import(E->getSubExpr());
5325  if (!SubExpr)
5326  return nullptr;
5327 
5328  return new (Importer.getToContext())
5329  ParenExpr(Importer.Import(E->getLParen()),
5330  Importer.Import(E->getRParen()),
5331  SubExpr);
5332 }
5333 
5335  SmallVector<Expr *, 4> Exprs(E->getNumExprs());
5336  if (ImportContainerChecked(E->exprs(), Exprs))
5337  return nullptr;
5338 
5339  return new (Importer.getToContext()) ParenListExpr(
5340  Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5341  Exprs, Importer.Import(E->getLParenLoc()));
5342 }
5343 
5345  QualType T = Importer.Import(E->getType());
5346  if (T.isNull())
5347  return nullptr;
5348 
5349  CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
5350  Importer.Import(E->getSubStmt()));
5351  if (!ToSubStmt && E->getSubStmt())
5352  return nullptr;
5353 
5354  return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5355  Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5356 }
5357