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