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