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