1 //===- ASTImporter.cpp - Importing ASTs from other Contexts ---------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the ASTImporter class which imports AST nodes from one 11 // context into another context. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/AST/ASTImporter.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/ASTDiagnostic.h" 18 #include "clang/AST/ASTStructuralEquivalence.h" 19 #include "clang/AST/Attr.h" 20 #include "clang/AST/Decl.h" 21 #include "clang/AST/DeclAccessPair.h" 22 #include "clang/AST/DeclBase.h" 23 #include "clang/AST/DeclCXX.h" 24 #include "clang/AST/DeclFriend.h" 25 #include "clang/AST/DeclGroup.h" 26 #include "clang/AST/DeclObjC.h" 27 #include "clang/AST/DeclTemplate.h" 28 #include "clang/AST/DeclVisitor.h" 29 #include "clang/AST/DeclarationName.h" 30 #include "clang/AST/Expr.h" 31 #include "clang/AST/ExprCXX.h" 32 #include "clang/AST/ExprObjC.h" 33 #include "clang/AST/ExternalASTSource.h" 34 #include "clang/AST/LambdaCapture.h" 35 #include "clang/AST/NestedNameSpecifier.h" 36 #include "clang/AST/OperationKinds.h" 37 #include "clang/AST/Stmt.h" 38 #include "clang/AST/StmtCXX.h" 39 #include "clang/AST/StmtObjC.h" 40 #include "clang/AST/StmtVisitor.h" 41 #include "clang/AST/TemplateBase.h" 42 #include "clang/AST/TemplateName.h" 43 #include "clang/AST/Type.h" 44 #include "clang/AST/TypeLoc.h" 45 #include "clang/AST/TypeVisitor.h" 46 #include "clang/AST/UnresolvedSet.h" 47 #include "clang/Basic/ExceptionSpecificationType.h" 48 #include "clang/Basic/FileManager.h" 49 #include "clang/Basic/IdentifierTable.h" 50 #include "clang/Basic/LLVM.h" 51 #include "clang/Basic/LangOptions.h" 52 #include "clang/Basic/SourceLocation.h" 53 #include "clang/Basic/SourceManager.h" 54 #include "clang/Basic/Specifiers.h" 55 #include "llvm/ADT/APSInt.h" 56 #include "llvm/ADT/ArrayRef.h" 57 #include "llvm/ADT/DenseMap.h" 58 #include "llvm/ADT/None.h" 59 #include "llvm/ADT/Optional.h" 60 #include "llvm/ADT/STLExtras.h" 61 #include "llvm/ADT/SmallVector.h" 62 #include "llvm/Support/Casting.h" 63 #include "llvm/Support/ErrorHandling.h" 64 #include "llvm/Support/MemoryBuffer.h" 65 #include <algorithm> 66 #include <cassert> 67 #include <cstddef> 68 #include <memory> 69 #include <type_traits> 70 #include <utility> 71 72 namespace clang { 73 74 unsigned ASTImporter::getFieldIndex(Decl *F) { 75 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) && 76 "Try to get field index for non-field."); 77 78 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext()); 79 if (!Owner) 80 return 0; 81 82 unsigned Index = 1; 83 for (const auto *D : Owner->decls()) { 84 if (D == F) 85 return Index; 86 87 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) 88 ++Index; 89 } 90 91 llvm_unreachable("Field was not found in its parent context."); 92 93 return 0; 94 } 95 96 template <class T> 97 SmallVector<Decl*, 2> 98 getCanonicalForwardRedeclChain(Redeclarable<T>* D) { 99 SmallVector<Decl*, 2> Redecls; 100 for (auto *R : D->getFirstDecl()->redecls()) { 101 if (R != D->getFirstDecl()) 102 Redecls.push_back(R); 103 } 104 Redecls.push_back(D->getFirstDecl()); 105 std::reverse(Redecls.begin(), Redecls.end()); 106 return Redecls; 107 } 108 109 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) { 110 // Currently only FunctionDecl is supported 111 auto FD = cast<FunctionDecl>(D); 112 return getCanonicalForwardRedeclChain<FunctionDecl>(FD); 113 } 114 115 void updateFlags(const Decl *From, Decl *To) { 116 // Check if some flags or attrs are new in 'From' and copy into 'To'. 117 // FIXME: Other flags or attrs? 118 if (From->isUsed(false) && !To->isUsed(false)) 119 To->setIsUsed(); 120 } 121 122 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>, 123 public DeclVisitor<ASTNodeImporter, Decl *>, 124 public StmtVisitor<ASTNodeImporter, Stmt *> { 125 ASTImporter &Importer; 126 127 // Wrapper for an overload set. 128 template <typename ToDeclT> struct CallOverloadedCreateFun { 129 template <typename... Args> 130 auto operator()(Args &&... args) 131 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) { 132 return ToDeclT::Create(std::forward<Args>(args)...); 133 } 134 }; 135 136 // Always use these functions to create a Decl during import. There are 137 // certain tasks which must be done after the Decl was created, e.g. we 138 // must immediately register that as an imported Decl. The parameter `ToD` 139 // will be set to the newly created Decl or if had been imported before 140 // then to the already imported Decl. Returns a bool value set to true if 141 // the `FromD` had been imported before. 142 template <typename ToDeclT, typename FromDeclT, typename... Args> 143 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD, 144 Args &&... args) { 145 // There may be several overloads of ToDeclT::Create. We must make sure 146 // to call the one which would be chosen by the arguments, thus we use a 147 // wrapper for the overload set. 148 CallOverloadedCreateFun<ToDeclT> OC; 149 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD, 150 std::forward<Args>(args)...); 151 } 152 // Use this overload if a special Type is needed to be created. E.g if we 153 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl` 154 // then: 155 // TypedefNameDecl *ToTypedef; 156 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...); 157 template <typename NewDeclT, typename ToDeclT, typename FromDeclT, 158 typename... Args> 159 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD, 160 Args &&... args) { 161 CallOverloadedCreateFun<NewDeclT> OC; 162 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD, 163 std::forward<Args>(args)...); 164 } 165 // Use this version if a special create function must be 166 // used, e.g. CXXRecordDecl::CreateLambda . 167 template <typename ToDeclT, typename CreateFunT, typename FromDeclT, 168 typename... Args> 169 LLVM_NODISCARD bool 170 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun, 171 FromDeclT *FromD, Args &&... args) { 172 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD)); 173 if (ToD) 174 return true; // Already imported. 175 ToD = CreateFun(std::forward<Args>(args)...); 176 InitializeImportedDecl(FromD, ToD); 177 return false; // A new Decl is created. 178 } 179 180 void InitializeImportedDecl(Decl *FromD, Decl *ToD) { 181 Importer.MapImported(FromD, ToD); 182 ToD->IdentifierNamespace = FromD->IdentifierNamespace; 183 if (FromD->hasAttrs()) 184 for (const Attr *FromAttr : FromD->getAttrs()) 185 ToD->addAttr(Importer.Import(FromAttr)); 186 if (FromD->isUsed()) 187 ToD->setIsUsed(); 188 if (FromD->isImplicit()) 189 ToD->setImplicit(); 190 } 191 192 public: 193 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {} 194 195 using TypeVisitor<ASTNodeImporter, QualType>::Visit; 196 using DeclVisitor<ASTNodeImporter, Decl *>::Visit; 197 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit; 198 199 // Importing types 200 QualType VisitType(const Type *T); 201 QualType VisitAtomicType(const AtomicType *T); 202 QualType VisitBuiltinType(const BuiltinType *T); 203 QualType VisitDecayedType(const DecayedType *T); 204 QualType VisitComplexType(const ComplexType *T); 205 QualType VisitPointerType(const PointerType *T); 206 QualType VisitBlockPointerType(const BlockPointerType *T); 207 QualType VisitLValueReferenceType(const LValueReferenceType *T); 208 QualType VisitRValueReferenceType(const RValueReferenceType *T); 209 QualType VisitMemberPointerType(const MemberPointerType *T); 210 QualType VisitConstantArrayType(const ConstantArrayType *T); 211 QualType VisitIncompleteArrayType(const IncompleteArrayType *T); 212 QualType VisitVariableArrayType(const VariableArrayType *T); 213 QualType VisitDependentSizedArrayType(const DependentSizedArrayType *T); 214 // FIXME: DependentSizedExtVectorType 215 QualType VisitVectorType(const VectorType *T); 216 QualType VisitExtVectorType(const ExtVectorType *T); 217 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T); 218 QualType VisitFunctionProtoType(const FunctionProtoType *T); 219 QualType VisitUnresolvedUsingType(const UnresolvedUsingType *T); 220 QualType VisitParenType(const ParenType *T); 221 QualType VisitTypedefType(const TypedefType *T); 222 QualType VisitTypeOfExprType(const TypeOfExprType *T); 223 // FIXME: DependentTypeOfExprType 224 QualType VisitTypeOfType(const TypeOfType *T); 225 QualType VisitDecltypeType(const DecltypeType *T); 226 QualType VisitUnaryTransformType(const UnaryTransformType *T); 227 QualType VisitAutoType(const AutoType *T); 228 QualType VisitInjectedClassNameType(const InjectedClassNameType *T); 229 // FIXME: DependentDecltypeType 230 QualType VisitRecordType(const RecordType *T); 231 QualType VisitEnumType(const EnumType *T); 232 QualType VisitAttributedType(const AttributedType *T); 233 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T); 234 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T); 235 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T); 236 QualType VisitElaboratedType(const ElaboratedType *T); 237 QualType VisitDependentNameType(const DependentNameType *T); 238 QualType VisitPackExpansionType(const PackExpansionType *T); 239 QualType VisitDependentTemplateSpecializationType( 240 const DependentTemplateSpecializationType *T); 241 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T); 242 QualType VisitObjCObjectType(const ObjCObjectType *T); 243 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T); 244 245 // Importing declarations 246 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC, 247 DeclContext *&LexicalDC, DeclarationName &Name, 248 NamedDecl *&ToD, SourceLocation &Loc); 249 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); 250 void ImportDeclarationNameLoc(const DeclarationNameInfo &From, 251 DeclarationNameInfo& To); 252 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); 253 void ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To); 254 255 bool ImportCastPath(CastExpr *E, CXXCastPath &Path); 256 257 using Designator = DesignatedInitExpr::Designator; 258 259 Designator ImportDesignator(const Designator &D); 260 261 Optional<LambdaCapture> ImportLambdaCapture(const LambdaCapture &From); 262 263 /// What we should import from the definition. 264 enum ImportDefinitionKind { 265 /// Import the default subset of the definition, which might be 266 /// nothing (if minimal import is set) or might be everything (if minimal 267 /// import is not set). 268 IDK_Default, 269 270 /// Import everything. 271 IDK_Everything, 272 273 /// Import only the bare bones needed to establish a valid 274 /// DeclContext. 275 IDK_Basic 276 }; 277 278 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { 279 return IDK == IDK_Everything || 280 (IDK == IDK_Default && !Importer.isMinimalImport()); 281 } 282 283 bool ImportDefinition(RecordDecl *From, RecordDecl *To, 284 ImportDefinitionKind Kind = IDK_Default); 285 bool ImportDefinition(VarDecl *From, VarDecl *To, 286 ImportDefinitionKind Kind = IDK_Default); 287 bool ImportDefinition(EnumDecl *From, EnumDecl *To, 288 ImportDefinitionKind Kind = IDK_Default); 289 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, 290 ImportDefinitionKind Kind = IDK_Default); 291 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To, 292 ImportDefinitionKind Kind = IDK_Default); 293 TemplateParameterList *ImportTemplateParameterList( 294 TemplateParameterList *Params); 295 TemplateArgument ImportTemplateArgument(const TemplateArgument &From); 296 Optional<TemplateArgumentLoc> ImportTemplateArgumentLoc( 297 const TemplateArgumentLoc &TALoc); 298 bool ImportTemplateArguments(const TemplateArgument *FromArgs, 299 unsigned NumFromArgs, 300 SmallVectorImpl<TemplateArgument> &ToArgs); 301 302 template <typename InContainerTy> 303 bool ImportTemplateArgumentListInfo(const InContainerTy &Container, 304 TemplateArgumentListInfo &ToTAInfo); 305 306 template<typename InContainerTy> 307 bool ImportTemplateArgumentListInfo(SourceLocation FromLAngleLoc, 308 SourceLocation FromRAngleLoc, 309 const InContainerTy &Container, 310 TemplateArgumentListInfo &Result); 311 312 using TemplateArgsTy = SmallVector<TemplateArgument, 8>; 313 using OptionalTemplateArgsTy = Optional<TemplateArgsTy>; 314 std::tuple<FunctionTemplateDecl *, OptionalTemplateArgsTy> 315 ImportFunctionTemplateWithTemplateArgsFromSpecialization( 316 FunctionDecl *FromFD); 317 318 bool ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD); 319 320 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain); 321 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord, 322 bool Complain = true); 323 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, 324 bool Complain = true); 325 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord); 326 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC); 327 bool IsStructuralMatch(FunctionTemplateDecl *From, 328 FunctionTemplateDecl *To); 329 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To); 330 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To); 331 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To); 332 Decl *VisitDecl(Decl *D); 333 Decl *VisitEmptyDecl(EmptyDecl *D); 334 Decl *VisitAccessSpecDecl(AccessSpecDecl *D); 335 Decl *VisitStaticAssertDecl(StaticAssertDecl *D); 336 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); 337 Decl *VisitNamespaceDecl(NamespaceDecl *D); 338 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D); 339 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); 340 Decl *VisitTypedefDecl(TypedefDecl *D); 341 Decl *VisitTypeAliasDecl(TypeAliasDecl *D); 342 Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D); 343 Decl *VisitLabelDecl(LabelDecl *D); 344 Decl *VisitEnumDecl(EnumDecl *D); 345 Decl *VisitRecordDecl(RecordDecl *D); 346 Decl *VisitEnumConstantDecl(EnumConstantDecl *D); 347 Decl *VisitFunctionDecl(FunctionDecl *D); 348 Decl *VisitCXXMethodDecl(CXXMethodDecl *D); 349 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); 350 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); 351 Decl *VisitCXXConversionDecl(CXXConversionDecl *D); 352 Decl *VisitFieldDecl(FieldDecl *D); 353 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D); 354 Decl *VisitFriendDecl(FriendDecl *D); 355 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D); 356 Decl *VisitVarDecl(VarDecl *D); 357 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D); 358 Decl *VisitParmVarDecl(ParmVarDecl *D); 359 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D); 360 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); 361 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D); 362 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D); 363 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D); 364 Decl *VisitUsingDecl(UsingDecl *D); 365 Decl *VisitUsingShadowDecl(UsingShadowDecl *D); 366 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D); 367 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); 368 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); 369 370 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list); 371 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); 372 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); 373 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D); 374 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D); 375 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); 376 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); 377 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); 378 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); 379 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); 380 Decl *VisitClassTemplateSpecializationDecl( 381 ClassTemplateSpecializationDecl *D); 382 Decl *VisitVarTemplateDecl(VarTemplateDecl *D); 383 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); 384 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D); 385 386 // Importing statements 387 DeclGroupRef ImportDeclGroup(DeclGroupRef DG); 388 389 Stmt *VisitStmt(Stmt *S); 390 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S); 391 Stmt *VisitDeclStmt(DeclStmt *S); 392 Stmt *VisitNullStmt(NullStmt *S); 393 Stmt *VisitCompoundStmt(CompoundStmt *S); 394 Stmt *VisitCaseStmt(CaseStmt *S); 395 Stmt *VisitDefaultStmt(DefaultStmt *S); 396 Stmt *VisitLabelStmt(LabelStmt *S); 397 Stmt *VisitAttributedStmt(AttributedStmt *S); 398 Stmt *VisitIfStmt(IfStmt *S); 399 Stmt *VisitSwitchStmt(SwitchStmt *S); 400 Stmt *VisitWhileStmt(WhileStmt *S); 401 Stmt *VisitDoStmt(DoStmt *S); 402 Stmt *VisitForStmt(ForStmt *S); 403 Stmt *VisitGotoStmt(GotoStmt *S); 404 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S); 405 Stmt *VisitContinueStmt(ContinueStmt *S); 406 Stmt *VisitBreakStmt(BreakStmt *S); 407 Stmt *VisitReturnStmt(ReturnStmt *S); 408 // FIXME: MSAsmStmt 409 // FIXME: SEHExceptStmt 410 // FIXME: SEHFinallyStmt 411 // FIXME: SEHTryStmt 412 // FIXME: SEHLeaveStmt 413 // FIXME: CapturedStmt 414 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S); 415 Stmt *VisitCXXTryStmt(CXXTryStmt *S); 416 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S); 417 // FIXME: MSDependentExistsStmt 418 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); 419 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); 420 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S); 421 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S); 422 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); 423 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); 424 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); 425 426 // Importing expressions 427 Expr *VisitExpr(Expr *E); 428 Expr *VisitVAArgExpr(VAArgExpr *E); 429 Expr *VisitGNUNullExpr(GNUNullExpr *E); 430 Expr *VisitPredefinedExpr(PredefinedExpr *E); 431 Expr *VisitDeclRefExpr(DeclRefExpr *E); 432 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE); 433 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E); 434 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); 435 Expr *VisitIntegerLiteral(IntegerLiteral *E); 436 Expr *VisitFloatingLiteral(FloatingLiteral *E); 437 Expr *VisitCharacterLiteral(CharacterLiteral *E); 438 Expr *VisitStringLiteral(StringLiteral *E); 439 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E); 440 Expr *VisitAtomicExpr(AtomicExpr *E); 441 Expr *VisitAddrLabelExpr(AddrLabelExpr *E); 442 Expr *VisitParenExpr(ParenExpr *E); 443 Expr *VisitParenListExpr(ParenListExpr *E); 444 Expr *VisitStmtExpr(StmtExpr *E); 445 Expr *VisitUnaryOperator(UnaryOperator *E); 446 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); 447 Expr *VisitBinaryOperator(BinaryOperator *E); 448 Expr *VisitConditionalOperator(ConditionalOperator *E); 449 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E); 450 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E); 451 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); 452 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E); 453 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E); 454 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E); 455 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E); 456 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E); 457 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE); 458 Expr *VisitCXXThrowExpr(CXXThrowExpr *E); 459 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E); 460 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E); 461 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); 462 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); 463 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE); 464 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E); 465 Expr *VisitPackExpansionExpr(PackExpansionExpr *E); 466 Expr *VisitSizeOfPackExpr(SizeOfPackExpr *E); 467 Expr *VisitCXXNewExpr(CXXNewExpr *CE); 468 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E); 469 Expr *VisitCXXConstructExpr(CXXConstructExpr *E); 470 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E); 471 Expr *VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); 472 Expr *VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); 473 Expr *VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *CE); 474 Expr *VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E); 475 Expr *VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E); 476 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC); 477 Expr *VisitCXXThisExpr(CXXThisExpr *E); 478 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); 479 Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); 480 Expr *VisitMemberExpr(MemberExpr *E); 481 Expr *VisitCallExpr(CallExpr *E); 482 Expr *VisitLambdaExpr(LambdaExpr *LE); 483 Expr *VisitInitListExpr(InitListExpr *E); 484 Expr *VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E); 485 Expr *VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E); 486 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E); 487 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E); 488 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E); 489 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E); 490 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E); 491 Expr *VisitTypeTraitExpr(TypeTraitExpr *E); 492 Expr *VisitCXXTypeidExpr(CXXTypeidExpr *E); 493 494 template<typename IIter, typename OIter> 495 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) { 496 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type; 497 498 ASTImporter &ImporterRef = Importer; 499 std::transform(Ibegin, Iend, Obegin, 500 [&ImporterRef](ItemT From) -> ItemT { 501 return ImporterRef.Import(From); 502 }); 503 } 504 505 template<typename IIter, typename OIter> 506 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) { 507 using ItemT = typename std::remove_reference<decltype(**Obegin)>::type; 508 509 ASTImporter &ImporterRef = Importer; 510 bool Failed = false; 511 std::transform(Ibegin, Iend, Obegin, 512 [&ImporterRef, &Failed](ItemT *From) -> ItemT * { 513 auto *To = cast_or_null<ItemT>(ImporterRef.Import(From)); 514 if (!To && From) 515 Failed = true; 516 return To; 517 }); 518 return Failed; 519 } 520 521 template<typename InContainerTy, typename OutContainerTy> 522 bool ImportContainerChecked(const InContainerTy &InContainer, 523 OutContainerTy &OutContainer) { 524 return ImportArrayChecked(InContainer.begin(), InContainer.end(), 525 OutContainer.begin()); 526 } 527 528 template<typename InContainerTy, typename OIter> 529 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) { 530 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin); 531 } 532 533 // Importing overrides. 534 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod); 535 536 FunctionDecl *FindFunctionTemplateSpecialization(FunctionDecl *FromFD); 537 }; 538 539 template <typename InContainerTy> 540 bool ASTNodeImporter::ImportTemplateArgumentListInfo( 541 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc, 542 const InContainerTy &Container, TemplateArgumentListInfo &Result) { 543 TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc), 544 Importer.Import(FromRAngleLoc)); 545 if (ImportTemplateArgumentListInfo(Container, ToTAInfo)) 546 return true; 547 Result = ToTAInfo; 548 return false; 549 } 550 551 template <> 552 bool ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>( 553 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) { 554 return ImportTemplateArgumentListInfo( 555 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result); 556 } 557 558 template <> 559 bool ASTNodeImporter::ImportTemplateArgumentListInfo< 560 ASTTemplateArgumentListInfo>(const ASTTemplateArgumentListInfo &From, 561 TemplateArgumentListInfo &Result) { 562 return ImportTemplateArgumentListInfo(From.LAngleLoc, From.RAngleLoc, 563 From.arguments(), Result); 564 } 565 566 std::tuple<FunctionTemplateDecl *, ASTNodeImporter::OptionalTemplateArgsTy> 567 ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization( 568 FunctionDecl *FromFD) { 569 assert(FromFD->getTemplatedKind() == 570 FunctionDecl::TK_FunctionTemplateSpecialization); 571 auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); 572 auto *Template = cast_or_null<FunctionTemplateDecl>( 573 Importer.Import(FTSInfo->getTemplate())); 574 575 // Import template arguments. 576 auto TemplArgs = FTSInfo->TemplateArguments->asArray(); 577 TemplateArgsTy ToTemplArgs; 578 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(), 579 ToTemplArgs)) // Error during import. 580 return std::make_tuple(Template, OptionalTemplateArgsTy()); 581 582 return std::make_tuple(Template, ToTemplArgs); 583 } 584 585 } // namespace clang 586 587 //---------------------------------------------------------------------------- 588 // Import Types 589 //---------------------------------------------------------------------------- 590 591 using namespace clang; 592 593 QualType ASTNodeImporter::VisitType(const Type *T) { 594 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) 595 << T->getTypeClassName(); 596 return {}; 597 } 598 599 QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){ 600 QualType UnderlyingType = Importer.Import(T->getValueType()); 601 if(UnderlyingType.isNull()) 602 return {}; 603 604 return Importer.getToContext().getAtomicType(UnderlyingType); 605 } 606 607 QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { 608 switch (T->getKind()) { 609 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 610 case BuiltinType::Id: \ 611 return Importer.getToContext().SingletonId; 612 #include "clang/Basic/OpenCLImageTypes.def" 613 #define SHARED_SINGLETON_TYPE(Expansion) 614 #define BUILTIN_TYPE(Id, SingletonId) \ 615 case BuiltinType::Id: return Importer.getToContext().SingletonId; 616 #include "clang/AST/BuiltinTypes.def" 617 618 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" 619 // context supports C++. 620 621 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" 622 // context supports ObjC. 623 624 case BuiltinType::Char_U: 625 // The context we're importing from has an unsigned 'char'. If we're 626 // importing into a context with a signed 'char', translate to 627 // 'unsigned char' instead. 628 if (Importer.getToContext().getLangOpts().CharIsSigned) 629 return Importer.getToContext().UnsignedCharTy; 630 631 return Importer.getToContext().CharTy; 632 633 case BuiltinType::Char_S: 634 // The context we're importing from has an unsigned 'char'. If we're 635 // importing into a context with a signed 'char', translate to 636 // 'unsigned char' instead. 637 if (!Importer.getToContext().getLangOpts().CharIsSigned) 638 return Importer.getToContext().SignedCharTy; 639 640 return Importer.getToContext().CharTy; 641 642 case BuiltinType::WChar_S: 643 case BuiltinType::WChar_U: 644 // FIXME: If not in C++, shall we translate to the C equivalent of 645 // wchar_t? 646 return Importer.getToContext().WCharTy; 647 } 648 649 llvm_unreachable("Invalid BuiltinType Kind!"); 650 } 651 652 QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) { 653 QualType OrigT = Importer.Import(T->getOriginalType()); 654 if (OrigT.isNull()) 655 return {}; 656 657 return Importer.getToContext().getDecayedType(OrigT); 658 } 659 660 QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) { 661 QualType ToElementType = Importer.Import(T->getElementType()); 662 if (ToElementType.isNull()) 663 return {}; 664 665 return Importer.getToContext().getComplexType(ToElementType); 666 } 667 668 QualType ASTNodeImporter::VisitPointerType(const PointerType *T) { 669 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 670 if (ToPointeeType.isNull()) 671 return {}; 672 673 return Importer.getToContext().getPointerType(ToPointeeType); 674 } 675 676 QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { 677 // FIXME: Check for blocks support in "to" context. 678 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 679 if (ToPointeeType.isNull()) 680 return {}; 681 682 return Importer.getToContext().getBlockPointerType(ToPointeeType); 683 } 684 685 QualType 686 ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { 687 // FIXME: Check for C++ support in "to" context. 688 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); 689 if (ToPointeeType.isNull()) 690 return {}; 691 692 return Importer.getToContext().getLValueReferenceType(ToPointeeType); 693 } 694 695 QualType 696 ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { 697 // FIXME: Check for C++0x support in "to" context. 698 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); 699 if (ToPointeeType.isNull()) 700 return {}; 701 702 return Importer.getToContext().getRValueReferenceType(ToPointeeType); 703 } 704 705 QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { 706 // FIXME: Check for C++ support in "to" context. 707 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 708 if (ToPointeeType.isNull()) 709 return {}; 710 711 QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); 712 return Importer.getToContext().getMemberPointerType(ToPointeeType, 713 ClassType.getTypePtr()); 714 } 715 716 QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { 717 QualType ToElementType = Importer.Import(T->getElementType()); 718 if (ToElementType.isNull()) 719 return {}; 720 721 return Importer.getToContext().getConstantArrayType(ToElementType, 722 T->getSize(), 723 T->getSizeModifier(), 724 T->getIndexTypeCVRQualifiers()); 725 } 726 727 QualType 728 ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { 729 QualType ToElementType = Importer.Import(T->getElementType()); 730 if (ToElementType.isNull()) 731 return {}; 732 733 return Importer.getToContext().getIncompleteArrayType(ToElementType, 734 T->getSizeModifier(), 735 T->getIndexTypeCVRQualifiers()); 736 } 737 738 QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { 739 QualType ToElementType = Importer.Import(T->getElementType()); 740 if (ToElementType.isNull()) 741 return {}; 742 743 Expr *Size = Importer.Import(T->getSizeExpr()); 744 if (!Size) 745 return {}; 746 747 SourceRange Brackets = Importer.Import(T->getBracketsRange()); 748 return Importer.getToContext().getVariableArrayType(ToElementType, Size, 749 T->getSizeModifier(), 750 T->getIndexTypeCVRQualifiers(), 751 Brackets); 752 } 753 754 QualType ASTNodeImporter::VisitDependentSizedArrayType( 755 const DependentSizedArrayType *T) { 756 QualType ToElementType = Importer.Import(T->getElementType()); 757 if (ToElementType.isNull()) 758 return {}; 759 760 // SizeExpr may be null if size is not specified directly. 761 // For example, 'int a[]'. 762 Expr *Size = Importer.Import(T->getSizeExpr()); 763 if (!Size && T->getSizeExpr()) 764 return {}; 765 766 SourceRange Brackets = Importer.Import(T->getBracketsRange()); 767 return Importer.getToContext().getDependentSizedArrayType( 768 ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(), 769 Brackets); 770 } 771 772 QualType ASTNodeImporter::VisitVectorType(const VectorType *T) { 773 QualType ToElementType = Importer.Import(T->getElementType()); 774 if (ToElementType.isNull()) 775 return {}; 776 777 return Importer.getToContext().getVectorType(ToElementType, 778 T->getNumElements(), 779 T->getVectorKind()); 780 } 781 782 QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { 783 QualType ToElementType = Importer.Import(T->getElementType()); 784 if (ToElementType.isNull()) 785 return {}; 786 787 return Importer.getToContext().getExtVectorType(ToElementType, 788 T->getNumElements()); 789 } 790 791 QualType 792 ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { 793 // FIXME: What happens if we're importing a function without a prototype 794 // into C++? Should we make it variadic? 795 QualType ToResultType = Importer.Import(T->getReturnType()); 796 if (ToResultType.isNull()) 797 return {}; 798 799 return Importer.getToContext().getFunctionNoProtoType(ToResultType, 800 T->getExtInfo()); 801 } 802 803 QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { 804 QualType ToResultType = Importer.Import(T->getReturnType()); 805 if (ToResultType.isNull()) 806 return {}; 807 808 // Import argument types 809 SmallVector<QualType, 4> ArgTypes; 810 for (const auto &A : T->param_types()) { 811 QualType ArgType = Importer.Import(A); 812 if (ArgType.isNull()) 813 return {}; 814 ArgTypes.push_back(ArgType); 815 } 816 817 // Import exception types 818 SmallVector<QualType, 4> ExceptionTypes; 819 for (const auto &E : T->exceptions()) { 820 QualType ExceptionType = Importer.Import(E); 821 if (ExceptionType.isNull()) 822 return {}; 823 ExceptionTypes.push_back(ExceptionType); 824 } 825 826 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); 827 FunctionProtoType::ExtProtoInfo ToEPI; 828 829 ToEPI.ExtInfo = FromEPI.ExtInfo; 830 ToEPI.Variadic = FromEPI.Variadic; 831 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; 832 ToEPI.TypeQuals = FromEPI.TypeQuals; 833 ToEPI.RefQualifier = FromEPI.RefQualifier; 834 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; 835 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; 836 ToEPI.ExceptionSpec.NoexceptExpr = 837 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr); 838 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>( 839 Importer.Import(FromEPI.ExceptionSpec.SourceDecl)); 840 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>( 841 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate)); 842 843 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI); 844 } 845 846 QualType ASTNodeImporter::VisitUnresolvedUsingType( 847 const UnresolvedUsingType *T) { 848 const auto *ToD = 849 cast_or_null<UnresolvedUsingTypenameDecl>(Importer.Import(T->getDecl())); 850 if (!ToD) 851 return {}; 852 853 auto *ToPrevD = 854 cast_or_null<UnresolvedUsingTypenameDecl>( 855 Importer.Import(T->getDecl()->getPreviousDecl())); 856 if (!ToPrevD && T->getDecl()->getPreviousDecl()) 857 return {}; 858 859 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD); 860 } 861 862 QualType ASTNodeImporter::VisitParenType(const ParenType *T) { 863 QualType ToInnerType = Importer.Import(T->getInnerType()); 864 if (ToInnerType.isNull()) 865 return {}; 866 867 return Importer.getToContext().getParenType(ToInnerType); 868 } 869 870 QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { 871 auto *ToDecl = 872 dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl())); 873 if (!ToDecl) 874 return {}; 875 876 return Importer.getToContext().getTypeDeclType(ToDecl); 877 } 878 879 QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { 880 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); 881 if (!ToExpr) 882 return {}; 883 884 return Importer.getToContext().getTypeOfExprType(ToExpr); 885 } 886 887 QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { 888 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); 889 if (ToUnderlyingType.isNull()) 890 return {}; 891 892 return Importer.getToContext().getTypeOfType(ToUnderlyingType); 893 } 894 895 QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { 896 // FIXME: Make sure that the "to" context supports C++0x! 897 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); 898 if (!ToExpr) 899 return {}; 900 901 QualType UnderlyingType = Importer.Import(T->getUnderlyingType()); 902 if (UnderlyingType.isNull()) 903 return {}; 904 905 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType); 906 } 907 908 QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { 909 QualType ToBaseType = Importer.Import(T->getBaseType()); 910 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); 911 if (ToBaseType.isNull() || ToUnderlyingType.isNull()) 912 return {}; 913 914 return Importer.getToContext().getUnaryTransformType(ToBaseType, 915 ToUnderlyingType, 916 T->getUTTKind()); 917 } 918 919 QualType ASTNodeImporter::VisitAutoType(const AutoType *T) { 920 // FIXME: Make sure that the "to" context supports C++11! 921 QualType FromDeduced = T->getDeducedType(); 922 QualType ToDeduced; 923 if (!FromDeduced.isNull()) { 924 ToDeduced = Importer.Import(FromDeduced); 925 if (ToDeduced.isNull()) 926 return {}; 927 } 928 929 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(), 930 /*IsDependent*/false); 931 } 932 933 QualType ASTNodeImporter::VisitInjectedClassNameType( 934 const InjectedClassNameType *T) { 935 auto *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl())); 936 if (!D) 937 return {}; 938 939 QualType InjType = Importer.Import(T->getInjectedSpecializationType()); 940 if (InjType.isNull()) 941 return {}; 942 943 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading 944 // See comments in InjectedClassNameType definition for details 945 // return Importer.getToContext().getInjectedClassNameType(D, InjType); 946 enum { 947 TypeAlignmentInBits = 4, 948 TypeAlignment = 1 << TypeAlignmentInBits 949 }; 950 951 return QualType(new (Importer.getToContext(), TypeAlignment) 952 InjectedClassNameType(D, InjType), 0); 953 } 954 955 QualType ASTNodeImporter::VisitRecordType(const RecordType *T) { 956 auto *ToDecl = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); 957 if (!ToDecl) 958 return {}; 959 960 return Importer.getToContext().getTagDeclType(ToDecl); 961 } 962 963 QualType ASTNodeImporter::VisitEnumType(const EnumType *T) { 964 auto *ToDecl = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl())); 965 if (!ToDecl) 966 return {}; 967 968 return Importer.getToContext().getTagDeclType(ToDecl); 969 } 970 971 QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { 972 QualType FromModifiedType = T->getModifiedType(); 973 QualType FromEquivalentType = T->getEquivalentType(); 974 QualType ToModifiedType; 975 QualType ToEquivalentType; 976 977 if (!FromModifiedType.isNull()) { 978 ToModifiedType = Importer.Import(FromModifiedType); 979 if (ToModifiedType.isNull()) 980 return {}; 981 } 982 if (!FromEquivalentType.isNull()) { 983 ToEquivalentType = Importer.Import(FromEquivalentType); 984 if (ToEquivalentType.isNull()) 985 return {}; 986 } 987 988 return Importer.getToContext().getAttributedType(T->getAttrKind(), 989 ToModifiedType, ToEquivalentType); 990 } 991 992 QualType ASTNodeImporter::VisitTemplateTypeParmType( 993 const TemplateTypeParmType *T) { 994 auto *ParmDecl = 995 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl())); 996 if (!ParmDecl && T->getDecl()) 997 return {}; 998 999 return Importer.getToContext().getTemplateTypeParmType( 1000 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl); 1001 } 1002 1003 QualType ASTNodeImporter::VisitSubstTemplateTypeParmType( 1004 const SubstTemplateTypeParmType *T) { 1005 const auto *Replaced = 1006 cast_or_null<TemplateTypeParmType>(Importer.Import( 1007 QualType(T->getReplacedParameter(), 0)).getTypePtr()); 1008 if (!Replaced) 1009 return {}; 1010 1011 QualType Replacement = Importer.Import(T->getReplacementType()); 1012 if (Replacement.isNull()) 1013 return {}; 1014 Replacement = Replacement.getCanonicalType(); 1015 1016 return Importer.getToContext().getSubstTemplateTypeParmType( 1017 Replaced, Replacement); 1018 } 1019 1020 QualType ASTNodeImporter::VisitTemplateSpecializationType( 1021 const TemplateSpecializationType *T) { 1022 TemplateName ToTemplate = Importer.Import(T->getTemplateName()); 1023 if (ToTemplate.isNull()) 1024 return {}; 1025 1026 SmallVector<TemplateArgument, 2> ToTemplateArgs; 1027 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs)) 1028 return {}; 1029 1030 QualType ToCanonType; 1031 if (!QualType(T, 0).isCanonical()) { 1032 QualType FromCanonType 1033 = Importer.getFromContext().getCanonicalType(QualType(T, 0)); 1034 ToCanonType =Importer.Import(FromCanonType); 1035 if (ToCanonType.isNull()) 1036 return {}; 1037 } 1038 return Importer.getToContext().getTemplateSpecializationType(ToTemplate, 1039 ToTemplateArgs, 1040 ToCanonType); 1041 } 1042 1043 QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { 1044 NestedNameSpecifier *ToQualifier = nullptr; 1045 // Note: the qualifier in an ElaboratedType is optional. 1046 if (T->getQualifier()) { 1047 ToQualifier = Importer.Import(T->getQualifier()); 1048 if (!ToQualifier) 1049 return {}; 1050 } 1051 1052 QualType ToNamedType = Importer.Import(T->getNamedType()); 1053 if (ToNamedType.isNull()) 1054 return {}; 1055 1056 TagDecl *OwnedTagDecl = 1057 cast_or_null<TagDecl>(Importer.Import(T->getOwnedTagDecl())); 1058 if (!OwnedTagDecl && T->getOwnedTagDecl()) 1059 return {}; 1060 1061 return Importer.getToContext().getElaboratedType(T->getKeyword(), 1062 ToQualifier, ToNamedType, 1063 OwnedTagDecl); 1064 } 1065 1066 QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) { 1067 QualType Pattern = Importer.Import(T->getPattern()); 1068 if (Pattern.isNull()) 1069 return {}; 1070 1071 return Importer.getToContext().getPackExpansionType(Pattern, 1072 T->getNumExpansions()); 1073 } 1074 1075 QualType ASTNodeImporter::VisitDependentTemplateSpecializationType( 1076 const DependentTemplateSpecializationType *T) { 1077 NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier()); 1078 if (!Qualifier && T->getQualifier()) 1079 return {}; 1080 1081 IdentifierInfo *Name = Importer.Import(T->getIdentifier()); 1082 if (!Name && T->getIdentifier()) 1083 return {}; 1084 1085 SmallVector<TemplateArgument, 2> ToPack; 1086 ToPack.reserve(T->getNumArgs()); 1087 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack)) 1088 return {}; 1089 1090 return Importer.getToContext().getDependentTemplateSpecializationType( 1091 T->getKeyword(), Qualifier, Name, ToPack); 1092 } 1093 1094 QualType ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) { 1095 NestedNameSpecifier *NNS = Importer.Import(T->getQualifier()); 1096 if (!NNS && T->getQualifier()) 1097 return QualType(); 1098 1099 IdentifierInfo *Name = Importer.Import(T->getIdentifier()); 1100 if (!Name && T->getIdentifier()) 1101 return QualType(); 1102 1103 QualType Canon = (T == T->getCanonicalTypeInternal().getTypePtr()) 1104 ? QualType() 1105 : Importer.Import(T->getCanonicalTypeInternal()); 1106 if (!Canon.isNull()) 1107 Canon = Canon.getCanonicalType(); 1108 1109 return Importer.getToContext().getDependentNameType(T->getKeyword(), NNS, 1110 Name, Canon); 1111 } 1112 1113 QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { 1114 auto *Class = 1115 dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl())); 1116 if (!Class) 1117 return {}; 1118 1119 return Importer.getToContext().getObjCInterfaceType(Class); 1120 } 1121 1122 QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { 1123 QualType ToBaseType = Importer.Import(T->getBaseType()); 1124 if (ToBaseType.isNull()) 1125 return {}; 1126 1127 SmallVector<QualType, 4> TypeArgs; 1128 for (auto TypeArg : T->getTypeArgsAsWritten()) { 1129 QualType ImportedTypeArg = Importer.Import(TypeArg); 1130 if (ImportedTypeArg.isNull()) 1131 return {}; 1132 1133 TypeArgs.push_back(ImportedTypeArg); 1134 } 1135 1136 SmallVector<ObjCProtocolDecl *, 4> Protocols; 1137 for (auto *P : T->quals()) { 1138 auto *Protocol = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P)); 1139 if (!Protocol) 1140 return {}; 1141 Protocols.push_back(Protocol); 1142 } 1143 1144 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs, 1145 Protocols, 1146 T->isKindOfTypeAsWritten()); 1147 } 1148 1149 QualType 1150 ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { 1151 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1152 if (ToPointeeType.isNull()) 1153 return {}; 1154 1155 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType); 1156 } 1157 1158 //---------------------------------------------------------------------------- 1159 // Import Declarations 1160 //---------------------------------------------------------------------------- 1161 bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC, 1162 DeclContext *&LexicalDC, 1163 DeclarationName &Name, 1164 NamedDecl *&ToD, 1165 SourceLocation &Loc) { 1166 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop. 1167 // example: int struct_in_proto(struct data_t{int a;int b;} *d); 1168 DeclContext *OrigDC = D->getDeclContext(); 1169 FunctionDecl *FunDecl; 1170 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) && 1171 FunDecl->hasBody()) { 1172 auto getLeafPointeeType = [](const Type *T) { 1173 while (T->isPointerType() || T->isArrayType()) { 1174 T = T->getPointeeOrArrayElementType(); 1175 } 1176 return T; 1177 }; 1178 for (const ParmVarDecl *P : FunDecl->parameters()) { 1179 const Type *LeafT = 1180 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr()); 1181 auto *RT = dyn_cast<RecordType>(LeafT); 1182 if (RT && RT->getDecl() == D) { 1183 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) 1184 << D->getDeclKindName(); 1185 return true; 1186 } 1187 } 1188 } 1189 1190 // Import the context of this declaration. 1191 DC = Importer.ImportContext(OrigDC); 1192 if (!DC) 1193 return true; 1194 1195 LexicalDC = DC; 1196 if (D->getDeclContext() != D->getLexicalDeclContext()) { 1197 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 1198 if (!LexicalDC) 1199 return true; 1200 } 1201 1202 // Import the name of this declaration. 1203 Name = Importer.Import(D->getDeclName()); 1204 if (D->getDeclName() && !Name) 1205 return true; 1206 1207 // Import the location of this declaration. 1208 Loc = Importer.Import(D->getLocation()); 1209 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D)); 1210 return false; 1211 } 1212 1213 void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { 1214 if (!FromD) 1215 return; 1216 1217 if (!ToD) { 1218 ToD = Importer.Import(FromD); 1219 if (!ToD) 1220 return; 1221 } 1222 1223 if (auto *FromRecord = dyn_cast<RecordDecl>(FromD)) { 1224 if (auto *ToRecord = cast_or_null<RecordDecl>(ToD)) { 1225 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) { 1226 ImportDefinition(FromRecord, ToRecord); 1227 } 1228 } 1229 return; 1230 } 1231 1232 if (auto *FromEnum = dyn_cast<EnumDecl>(FromD)) { 1233 if (auto *ToEnum = cast_or_null<EnumDecl>(ToD)) { 1234 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { 1235 ImportDefinition(FromEnum, ToEnum); 1236 } 1237 } 1238 return; 1239 } 1240 } 1241 1242 void 1243 ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From, 1244 DeclarationNameInfo& To) { 1245 // NOTE: To.Name and To.Loc are already imported. 1246 // We only have to import To.LocInfo. 1247 switch (To.getName().getNameKind()) { 1248 case DeclarationName::Identifier: 1249 case DeclarationName::ObjCZeroArgSelector: 1250 case DeclarationName::ObjCOneArgSelector: 1251 case DeclarationName::ObjCMultiArgSelector: 1252 case DeclarationName::CXXUsingDirective: 1253 case DeclarationName::CXXDeductionGuideName: 1254 return; 1255 1256 case DeclarationName::CXXOperatorName: { 1257 SourceRange Range = From.getCXXOperatorNameRange(); 1258 To.setCXXOperatorNameRange(Importer.Import(Range)); 1259 return; 1260 } 1261 case DeclarationName::CXXLiteralOperatorName: { 1262 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc(); 1263 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc)); 1264 return; 1265 } 1266 case DeclarationName::CXXConstructorName: 1267 case DeclarationName::CXXDestructorName: 1268 case DeclarationName::CXXConversionFunctionName: { 1269 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo(); 1270 To.setNamedTypeInfo(Importer.Import(FromTInfo)); 1271 return; 1272 } 1273 } 1274 llvm_unreachable("Unknown name kind."); 1275 } 1276 1277 void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { 1278 if (Importer.isMinimalImport() && !ForceImport) { 1279 Importer.ImportContext(FromDC); 1280 return; 1281 } 1282 1283 for (auto *From : FromDC->decls()) 1284 Importer.Import(From); 1285 } 1286 1287 void ASTNodeImporter::ImportImplicitMethods( 1288 const CXXRecordDecl *From, CXXRecordDecl *To) { 1289 assert(From->isCompleteDefinition() && To->getDefinition() == To && 1290 "Import implicit methods to or from non-definition"); 1291 1292 for (CXXMethodDecl *FromM : From->methods()) 1293 if (FromM->isImplicit()) 1294 Importer.Import(FromM); 1295 } 1296 1297 static void setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To, 1298 ASTImporter &Importer) { 1299 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) { 1300 auto *ToTypedef = 1301 cast_or_null<TypedefNameDecl>(Importer.Import(FromTypedef)); 1302 assert (ToTypedef && "Failed to import typedef of an anonymous structure"); 1303 1304 To->setTypedefNameForAnonDecl(ToTypedef); 1305 } 1306 } 1307 1308 bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To, 1309 ImportDefinitionKind Kind) { 1310 if (To->getDefinition() || To->isBeingDefined()) { 1311 if (Kind == IDK_Everything) 1312 ImportDeclContext(From, /*ForceImport=*/true); 1313 1314 return false; 1315 } 1316 1317 To->startDefinition(); 1318 1319 setTypedefNameForAnonDecl(From, To, Importer); 1320 1321 // Add base classes. 1322 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) { 1323 auto *FromCXX = cast<CXXRecordDecl>(From); 1324 1325 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); 1326 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); 1327 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor; 1328 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers; 1329 ToData.Aggregate = FromData.Aggregate; 1330 ToData.PlainOldData = FromData.PlainOldData; 1331 ToData.Empty = FromData.Empty; 1332 ToData.Polymorphic = FromData.Polymorphic; 1333 ToData.Abstract = FromData.Abstract; 1334 ToData.IsStandardLayout = FromData.IsStandardLayout; 1335 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout; 1336 ToData.HasBasesWithFields = FromData.HasBasesWithFields; 1337 ToData.HasBasesWithNonStaticDataMembers = 1338 FromData.HasBasesWithNonStaticDataMembers; 1339 ToData.HasPrivateFields = FromData.HasPrivateFields; 1340 ToData.HasProtectedFields = FromData.HasProtectedFields; 1341 ToData.HasPublicFields = FromData.HasPublicFields; 1342 ToData.HasMutableFields = FromData.HasMutableFields; 1343 ToData.HasVariantMembers = FromData.HasVariantMembers; 1344 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers; 1345 ToData.HasInClassInitializer = FromData.HasInClassInitializer; 1346 ToData.HasUninitializedReferenceMember 1347 = FromData.HasUninitializedReferenceMember; 1348 ToData.HasUninitializedFields = FromData.HasUninitializedFields; 1349 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor; 1350 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment; 1351 ToData.NeedOverloadResolutionForCopyConstructor 1352 = FromData.NeedOverloadResolutionForCopyConstructor; 1353 ToData.NeedOverloadResolutionForMoveConstructor 1354 = FromData.NeedOverloadResolutionForMoveConstructor; 1355 ToData.NeedOverloadResolutionForMoveAssignment 1356 = FromData.NeedOverloadResolutionForMoveAssignment; 1357 ToData.NeedOverloadResolutionForDestructor 1358 = FromData.NeedOverloadResolutionForDestructor; 1359 ToData.DefaultedCopyConstructorIsDeleted 1360 = FromData.DefaultedCopyConstructorIsDeleted; 1361 ToData.DefaultedMoveConstructorIsDeleted 1362 = FromData.DefaultedMoveConstructorIsDeleted; 1363 ToData.DefaultedMoveAssignmentIsDeleted 1364 = FromData.DefaultedMoveAssignmentIsDeleted; 1365 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted; 1366 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers; 1367 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor; 1368 ToData.HasConstexprNonCopyMoveConstructor 1369 = FromData.HasConstexprNonCopyMoveConstructor; 1370 ToData.HasDefaultedDefaultConstructor 1371 = FromData.HasDefaultedDefaultConstructor; 1372 ToData.DefaultedDefaultConstructorIsConstexpr 1373 = FromData.DefaultedDefaultConstructorIsConstexpr; 1374 ToData.HasConstexprDefaultConstructor 1375 = FromData.HasConstexprDefaultConstructor; 1376 ToData.HasNonLiteralTypeFieldsOrBases 1377 = FromData.HasNonLiteralTypeFieldsOrBases; 1378 // ComputedVisibleConversions not imported. 1379 ToData.UserProvidedDefaultConstructor 1380 = FromData.UserProvidedDefaultConstructor; 1381 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers; 1382 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase 1383 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase; 1384 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase 1385 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase; 1386 ToData.ImplicitCopyAssignmentHasConstParam 1387 = FromData.ImplicitCopyAssignmentHasConstParam; 1388 ToData.HasDeclaredCopyConstructorWithConstParam 1389 = FromData.HasDeclaredCopyConstructorWithConstParam; 1390 ToData.HasDeclaredCopyAssignmentWithConstParam 1391 = FromData.HasDeclaredCopyAssignmentWithConstParam; 1392 1393 SmallVector<CXXBaseSpecifier *, 4> Bases; 1394 for (const auto &Base1 : FromCXX->bases()) { 1395 QualType T = Importer.Import(Base1.getType()); 1396 if (T.isNull()) 1397 return true; 1398 1399 SourceLocation EllipsisLoc; 1400 if (Base1.isPackExpansion()) 1401 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc()); 1402 1403 // Ensure that we have a definition for the base. 1404 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()); 1405 1406 Bases.push_back( 1407 new (Importer.getToContext()) 1408 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()), 1409 Base1.isVirtual(), 1410 Base1.isBaseOfClass(), 1411 Base1.getAccessSpecifierAsWritten(), 1412 Importer.Import(Base1.getTypeSourceInfo()), 1413 EllipsisLoc)); 1414 } 1415 if (!Bases.empty()) 1416 ToCXX->setBases(Bases.data(), Bases.size()); 1417 } 1418 1419 if (shouldForceImportDeclContext(Kind)) 1420 ImportDeclContext(From, /*ForceImport=*/true); 1421 1422 To->completeDefinition(); 1423 return false; 1424 } 1425 1426 bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To, 1427 ImportDefinitionKind Kind) { 1428 if (To->getAnyInitializer()) 1429 return false; 1430 1431 // FIXME: Can we really import any initializer? Alternatively, we could force 1432 // ourselves to import every declaration of a variable and then only use 1433 // getInit() here. 1434 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer()))); 1435 1436 // FIXME: Other bits to merge? 1437 1438 return false; 1439 } 1440 1441 bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To, 1442 ImportDefinitionKind Kind) { 1443 if (To->getDefinition() || To->isBeingDefined()) { 1444 if (Kind == IDK_Everything) 1445 ImportDeclContext(From, /*ForceImport=*/true); 1446 return false; 1447 } 1448 1449 To->startDefinition(); 1450 1451 setTypedefNameForAnonDecl(From, To, Importer); 1452 1453 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From)); 1454 if (T.isNull()) 1455 return true; 1456 1457 QualType ToPromotionType = Importer.Import(From->getPromotionType()); 1458 if (ToPromotionType.isNull()) 1459 return true; 1460 1461 if (shouldForceImportDeclContext(Kind)) 1462 ImportDeclContext(From, /*ForceImport=*/true); 1463 1464 // FIXME: we might need to merge the number of positive or negative bits 1465 // if the enumerator lists don't match. 1466 To->completeDefinition(T, ToPromotionType, 1467 From->getNumPositiveBits(), 1468 From->getNumNegativeBits()); 1469 return false; 1470 } 1471 1472 TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList( 1473 TemplateParameterList *Params) { 1474 SmallVector<NamedDecl *, 4> ToParams(Params->size()); 1475 if (ImportContainerChecked(*Params, ToParams)) 1476 return nullptr; 1477 1478 Expr *ToRequiresClause; 1479 if (Expr *const R = Params->getRequiresClause()) { 1480 ToRequiresClause = Importer.Import(R); 1481 if (!ToRequiresClause) 1482 return nullptr; 1483 } else { 1484 ToRequiresClause = nullptr; 1485 } 1486 1487 return TemplateParameterList::Create(Importer.getToContext(), 1488 Importer.Import(Params->getTemplateLoc()), 1489 Importer.Import(Params->getLAngleLoc()), 1490 ToParams, 1491 Importer.Import(Params->getRAngleLoc()), 1492 ToRequiresClause); 1493 } 1494 1495 TemplateArgument 1496 ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { 1497 switch (From.getKind()) { 1498 case TemplateArgument::Null: 1499 return TemplateArgument(); 1500 1501 case TemplateArgument::Type: { 1502 QualType ToType = Importer.Import(From.getAsType()); 1503 if (ToType.isNull()) 1504 return {}; 1505 return TemplateArgument(ToType); 1506 } 1507 1508 case TemplateArgument::Integral: { 1509 QualType ToType = Importer.Import(From.getIntegralType()); 1510 if (ToType.isNull()) 1511 return {}; 1512 return TemplateArgument(From, ToType); 1513 } 1514 1515 case TemplateArgument::Declaration: { 1516 auto *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl())); 1517 QualType ToType = Importer.Import(From.getParamTypeForDecl()); 1518 if (!To || ToType.isNull()) 1519 return {}; 1520 return TemplateArgument(To, ToType); 1521 } 1522 1523 case TemplateArgument::NullPtr: { 1524 QualType ToType = Importer.Import(From.getNullPtrType()); 1525 if (ToType.isNull()) 1526 return {}; 1527 return TemplateArgument(ToType, /*isNullPtr*/true); 1528 } 1529 1530 case TemplateArgument::Template: { 1531 TemplateName ToTemplate = Importer.Import(From.getAsTemplate()); 1532 if (ToTemplate.isNull()) 1533 return {}; 1534 1535 return TemplateArgument(ToTemplate); 1536 } 1537 1538 case TemplateArgument::TemplateExpansion: { 1539 TemplateName ToTemplate 1540 = Importer.Import(From.getAsTemplateOrTemplatePattern()); 1541 if (ToTemplate.isNull()) 1542 return {}; 1543 1544 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions()); 1545 } 1546 1547 case TemplateArgument::Expression: 1548 if (Expr *ToExpr = Importer.Import(From.getAsExpr())) 1549 return TemplateArgument(ToExpr); 1550 return TemplateArgument(); 1551 1552 case TemplateArgument::Pack: { 1553 SmallVector<TemplateArgument, 2> ToPack; 1554 ToPack.reserve(From.pack_size()); 1555 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack)) 1556 return {}; 1557 1558 return TemplateArgument( 1559 llvm::makeArrayRef(ToPack).copy(Importer.getToContext())); 1560 } 1561 } 1562 1563 llvm_unreachable("Invalid template argument kind"); 1564 } 1565 1566 Optional<TemplateArgumentLoc> 1567 ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) { 1568 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument()); 1569 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo(); 1570 TemplateArgumentLocInfo ToInfo; 1571 if (Arg.getKind() == TemplateArgument::Expression) { 1572 Expr *E = Importer.Import(FromInfo.getAsExpr()); 1573 ToInfo = TemplateArgumentLocInfo(E); 1574 if (!E) 1575 return None; 1576 } else if (Arg.getKind() == TemplateArgument::Type) { 1577 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo())) 1578 ToInfo = TemplateArgumentLocInfo(TSI); 1579 else 1580 return None; 1581 } else { 1582 ToInfo = TemplateArgumentLocInfo( 1583 Importer.Import(FromInfo.getTemplateQualifierLoc()), 1584 Importer.Import(FromInfo.getTemplateNameLoc()), 1585 Importer.Import(FromInfo.getTemplateEllipsisLoc())); 1586 } 1587 return TemplateArgumentLoc(Arg, ToInfo); 1588 } 1589 1590 bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs, 1591 unsigned NumFromArgs, 1592 SmallVectorImpl<TemplateArgument> &ToArgs) { 1593 for (unsigned I = 0; I != NumFromArgs; ++I) { 1594 TemplateArgument To = ImportTemplateArgument(FromArgs[I]); 1595 if (To.isNull() && !FromArgs[I].isNull()) 1596 return true; 1597 1598 ToArgs.push_back(To); 1599 } 1600 1601 return false; 1602 } 1603 1604 // We cannot use Optional<> pattern here and below because 1605 // TemplateArgumentListInfo's operator new is declared as deleted so it cannot 1606 // be stored in Optional. 1607 template <typename InContainerTy> 1608 bool ASTNodeImporter::ImportTemplateArgumentListInfo( 1609 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) { 1610 for (const auto &FromLoc : Container) { 1611 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc)) 1612 ToTAInfo.addArgument(*ToLoc); 1613 else 1614 return true; 1615 } 1616 return false; 1617 } 1618 1619 static StructuralEquivalenceKind 1620 getStructuralEquivalenceKind(const ASTImporter &Importer) { 1621 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal 1622 : StructuralEquivalenceKind::Default; 1623 } 1624 1625 bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) { 1626 StructuralEquivalenceContext Ctx( 1627 Importer.getFromContext(), Importer.getToContext(), 1628 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer), 1629 false, Complain); 1630 return Ctx.IsEquivalent(From, To); 1631 } 1632 1633 bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, 1634 RecordDecl *ToRecord, bool Complain) { 1635 // Eliminate a potential failure point where we attempt to re-import 1636 // something we're trying to import while completing ToRecord. 1637 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord); 1638 if (ToOrigin) { 1639 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin); 1640 if (ToOriginRecord) 1641 ToRecord = ToOriginRecord; 1642 } 1643 1644 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 1645 ToRecord->getASTContext(), 1646 Importer.getNonEquivalentDecls(), 1647 getStructuralEquivalenceKind(Importer), 1648 false, Complain); 1649 return Ctx.IsEquivalent(FromRecord, ToRecord); 1650 } 1651 1652 bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, 1653 bool Complain) { 1654 StructuralEquivalenceContext Ctx( 1655 Importer.getFromContext(), Importer.getToContext(), 1656 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer), 1657 false, Complain); 1658 return Ctx.IsEquivalent(FromVar, ToVar); 1659 } 1660 1661 bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { 1662 StructuralEquivalenceContext Ctx( 1663 Importer.getFromContext(), Importer.getToContext(), 1664 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer)); 1665 return Ctx.IsEquivalent(FromEnum, ToEnum); 1666 } 1667 1668 bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From, 1669 FunctionTemplateDecl *To) { 1670 StructuralEquivalenceContext Ctx( 1671 Importer.getFromContext(), Importer.getToContext(), 1672 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer), 1673 false, false); 1674 return Ctx.IsEquivalent(From, To); 1675 } 1676 1677 bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) { 1678 StructuralEquivalenceContext Ctx( 1679 Importer.getFromContext(), Importer.getToContext(), 1680 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer), 1681 false, false); 1682 return Ctx.IsEquivalent(From, To); 1683 } 1684 1685 bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC, 1686 EnumConstantDecl *ToEC) { 1687 const llvm::APSInt &FromVal = FromEC->getInitVal(); 1688 const llvm::APSInt &ToVal = ToEC->getInitVal(); 1689 1690 return FromVal.isSigned() == ToVal.isSigned() && 1691 FromVal.getBitWidth() == ToVal.getBitWidth() && 1692 FromVal == ToVal; 1693 } 1694 1695 bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From, 1696 ClassTemplateDecl *To) { 1697 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 1698 Importer.getToContext(), 1699 Importer.getNonEquivalentDecls(), 1700 getStructuralEquivalenceKind(Importer)); 1701 return Ctx.IsEquivalent(From, To); 1702 } 1703 1704 bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From, 1705 VarTemplateDecl *To) { 1706 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 1707 Importer.getToContext(), 1708 Importer.getNonEquivalentDecls(), 1709 getStructuralEquivalenceKind(Importer)); 1710 return Ctx.IsEquivalent(From, To); 1711 } 1712 1713 Decl *ASTNodeImporter::VisitDecl(Decl *D) { 1714 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) 1715 << D->getDeclKindName(); 1716 return nullptr; 1717 } 1718 1719 Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) { 1720 // Import the context of this declaration. 1721 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 1722 if (!DC) 1723 return nullptr; 1724 1725 DeclContext *LexicalDC = DC; 1726 if (D->getDeclContext() != D->getLexicalDeclContext()) { 1727 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 1728 if (!LexicalDC) 1729 return nullptr; 1730 } 1731 1732 // Import the location of this declaration. 1733 SourceLocation Loc = Importer.Import(D->getLocation()); 1734 1735 EmptyDecl *ToD; 1736 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, Loc)) 1737 return ToD; 1738 1739 ToD->setLexicalDeclContext(LexicalDC); 1740 LexicalDC->addDeclInternal(ToD); 1741 return ToD; 1742 } 1743 1744 Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 1745 TranslationUnitDecl *ToD = 1746 Importer.getToContext().getTranslationUnitDecl(); 1747 1748 Importer.MapImported(D, ToD); 1749 1750 return ToD; 1751 } 1752 1753 Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { 1754 SourceLocation Loc = Importer.Import(D->getLocation()); 1755 SourceLocation ColonLoc = Importer.Import(D->getColonLoc()); 1756 1757 // Import the context of this declaration. 1758 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 1759 if (!DC) 1760 return nullptr; 1761 1762 AccessSpecDecl *ToD; 1763 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(), 1764 DC, Loc, ColonLoc)) 1765 return ToD; 1766 1767 // Lexical DeclContext and Semantic DeclContext 1768 // is always the same for the accessSpec. 1769 ToD->setLexicalDeclContext(DC); 1770 DC->addDeclInternal(ToD); 1771 1772 return ToD; 1773 } 1774 1775 Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) { 1776 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 1777 if (!DC) 1778 return nullptr; 1779 1780 DeclContext *LexicalDC = DC; 1781 1782 // Import the location of this declaration. 1783 SourceLocation Loc = Importer.Import(D->getLocation()); 1784 1785 Expr *AssertExpr = Importer.Import(D->getAssertExpr()); 1786 if (!AssertExpr) 1787 return nullptr; 1788 1789 StringLiteral *FromMsg = D->getMessage(); 1790 auto *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg)); 1791 if (!ToMsg && FromMsg) 1792 return nullptr; 1793 1794 StaticAssertDecl *ToD; 1795 if (GetImportedOrCreateDecl( 1796 ToD, D, Importer.getToContext(), DC, Loc, AssertExpr, ToMsg, 1797 Importer.Import(D->getRParenLoc()), D->isFailed())) 1798 return ToD; 1799 1800 ToD->setLexicalDeclContext(LexicalDC); 1801 LexicalDC->addDeclInternal(ToD); 1802 return ToD; 1803 } 1804 1805 Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { 1806 // Import the major distinguishing characteristics of this namespace. 1807 DeclContext *DC, *LexicalDC; 1808 DeclarationName Name; 1809 SourceLocation Loc; 1810 NamedDecl *ToD; 1811 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 1812 return nullptr; 1813 if (ToD) 1814 return ToD; 1815 1816 NamespaceDecl *MergeWithNamespace = nullptr; 1817 if (!Name) { 1818 // This is an anonymous namespace. Adopt an existing anonymous 1819 // namespace if we can. 1820 // FIXME: Not testable. 1821 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC)) 1822 MergeWithNamespace = TU->getAnonymousNamespace(); 1823 else 1824 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace(); 1825 } else { 1826 SmallVector<NamedDecl *, 4> ConflictingDecls; 1827 SmallVector<NamedDecl *, 2> FoundDecls; 1828 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 1829 for (auto *FoundDecl : FoundDecls) { 1830 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace)) 1831 continue; 1832 1833 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) { 1834 MergeWithNamespace = FoundNS; 1835 ConflictingDecls.clear(); 1836 break; 1837 } 1838 1839 ConflictingDecls.push_back(FoundDecl); 1840 } 1841 1842 if (!ConflictingDecls.empty()) { 1843 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace, 1844 ConflictingDecls.data(), 1845 ConflictingDecls.size()); 1846 } 1847 } 1848 1849 // Create the "to" namespace, if needed. 1850 NamespaceDecl *ToNamespace = MergeWithNamespace; 1851 if (!ToNamespace) { 1852 if (GetImportedOrCreateDecl( 1853 ToNamespace, D, Importer.getToContext(), DC, D->isInline(), 1854 Importer.Import(D->getLocStart()), Loc, Name.getAsIdentifierInfo(), 1855 /*PrevDecl=*/nullptr)) 1856 return ToNamespace; 1857 ToNamespace->setLexicalDeclContext(LexicalDC); 1858 LexicalDC->addDeclInternal(ToNamespace); 1859 1860 // If this is an anonymous namespace, register it as the anonymous 1861 // namespace within its context. 1862 if (!Name) { 1863 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC)) 1864 TU->setAnonymousNamespace(ToNamespace); 1865 else 1866 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace); 1867 } 1868 } 1869 Importer.MapImported(D, ToNamespace); 1870 1871 ImportDeclContext(D); 1872 1873 return ToNamespace; 1874 } 1875 1876 Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 1877 // Import the major distinguishing characteristics of this namespace. 1878 DeclContext *DC, *LexicalDC; 1879 DeclarationName Name; 1880 SourceLocation Loc; 1881 NamedDecl *LookupD; 1882 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc)) 1883 return nullptr; 1884 if (LookupD) 1885 return LookupD; 1886 1887 // NOTE: No conflict resolution is done for namespace aliases now. 1888 1889 auto *TargetDecl = cast_or_null<NamespaceDecl>( 1890 Importer.Import(D->getNamespace())); 1891 if (!TargetDecl) 1892 return nullptr; 1893 1894 IdentifierInfo *ToII = Importer.Import(D->getIdentifier()); 1895 if (!ToII) 1896 return nullptr; 1897 1898 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc()); 1899 if (D->getQualifierLoc() && !ToQLoc) 1900 return nullptr; 1901 1902 NamespaceAliasDecl *ToD; 1903 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, 1904 Importer.Import(D->getNamespaceLoc()), 1905 Importer.Import(D->getAliasLoc()), ToII, ToQLoc, 1906 Importer.Import(D->getTargetNameLoc()), 1907 TargetDecl)) 1908 return ToD; 1909 1910 ToD->setLexicalDeclContext(LexicalDC); 1911 LexicalDC->addDeclInternal(ToD); 1912 1913 return ToD; 1914 } 1915 1916 Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { 1917 // Import the major distinguishing characteristics of this typedef. 1918 DeclContext *DC, *LexicalDC; 1919 DeclarationName Name; 1920 SourceLocation Loc; 1921 NamedDecl *ToD; 1922 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 1923 return nullptr; 1924 if (ToD) 1925 return ToD; 1926 1927 // If this typedef is not in block scope, determine whether we've 1928 // seen a typedef with the same name (that we can merge with) or any 1929 // other entity by that name (which name lookup could conflict with). 1930 if (!DC->isFunctionOrMethod()) { 1931 SmallVector<NamedDecl *, 4> ConflictingDecls; 1932 unsigned IDNS = Decl::IDNS_Ordinary; 1933 SmallVector<NamedDecl *, 2> FoundDecls; 1934 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 1935 for (auto *FoundDecl : FoundDecls) { 1936 if (!FoundDecl->isInIdentifierNamespace(IDNS)) 1937 continue; 1938 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) { 1939 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(), 1940 FoundTypedef->getUnderlyingType())) 1941 return Importer.MapImported(D, FoundTypedef); 1942 } 1943 1944 ConflictingDecls.push_back(FoundDecl); 1945 } 1946 1947 if (!ConflictingDecls.empty()) { 1948 Name = Importer.HandleNameConflict(Name, DC, IDNS, 1949 ConflictingDecls.data(), 1950 ConflictingDecls.size()); 1951 if (!Name) 1952 return nullptr; 1953 } 1954 } 1955 1956 // Import the underlying type of this typedef; 1957 QualType T = Importer.Import(D->getUnderlyingType()); 1958 if (T.isNull()) 1959 return nullptr; 1960 1961 // Create the new typedef node. 1962 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 1963 SourceLocation StartL = Importer.Import(D->getLocStart()); 1964 1965 TypedefNameDecl *ToTypedef; 1966 if (IsAlias) { 1967 if (GetImportedOrCreateDecl<TypeAliasDecl>( 1968 ToTypedef, D, Importer.getToContext(), DC, StartL, Loc, 1969 Name.getAsIdentifierInfo(), TInfo)) 1970 return ToTypedef; 1971 } else if (GetImportedOrCreateDecl<TypedefDecl>( 1972 ToTypedef, D, Importer.getToContext(), DC, StartL, Loc, 1973 Name.getAsIdentifierInfo(), TInfo)) 1974 return ToTypedef; 1975 1976 ToTypedef->setAccess(D->getAccess()); 1977 ToTypedef->setLexicalDeclContext(LexicalDC); 1978 1979 // Templated declarations should not appear in DeclContext. 1980 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr; 1981 if (!FromAlias || !FromAlias->getDescribedAliasTemplate()) 1982 LexicalDC->addDeclInternal(ToTypedef); 1983 1984 return ToTypedef; 1985 } 1986 1987 Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { 1988 return VisitTypedefNameDecl(D, /*IsAlias=*/false); 1989 } 1990 1991 Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { 1992 return VisitTypedefNameDecl(D, /*IsAlias=*/true); 1993 } 1994 1995 Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { 1996 // Import the major distinguishing characteristics of this typedef. 1997 DeclContext *DC, *LexicalDC; 1998 DeclarationName Name; 1999 SourceLocation Loc; 2000 NamedDecl *FoundD; 2001 if (ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc)) 2002 return nullptr; 2003 if (FoundD) 2004 return FoundD; 2005 2006 // If this typedef is not in block scope, determine whether we've 2007 // seen a typedef with the same name (that we can merge with) or any 2008 // other entity by that name (which name lookup could conflict with). 2009 if (!DC->isFunctionOrMethod()) { 2010 SmallVector<NamedDecl *, 4> ConflictingDecls; 2011 unsigned IDNS = Decl::IDNS_Ordinary; 2012 SmallVector<NamedDecl *, 2> FoundDecls; 2013 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2014 for (auto *FoundDecl : FoundDecls) { 2015 if (!FoundDecl->isInIdentifierNamespace(IDNS)) 2016 continue; 2017 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl)) 2018 return Importer.MapImported(D, FoundAlias); 2019 ConflictingDecls.push_back(FoundDecl); 2020 } 2021 2022 if (!ConflictingDecls.empty()) { 2023 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2024 ConflictingDecls.data(), 2025 ConflictingDecls.size()); 2026 if (!Name) 2027 return nullptr; 2028 } 2029 } 2030 2031 TemplateParameterList *Params = ImportTemplateParameterList( 2032 D->getTemplateParameters()); 2033 if (!Params) 2034 return nullptr; 2035 2036 auto *TemplDecl = cast_or_null<TypeAliasDecl>( 2037 Importer.Import(D->getTemplatedDecl())); 2038 if (!TemplDecl) 2039 return nullptr; 2040 2041 TypeAliasTemplateDecl *ToAlias; 2042 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc, 2043 Name, Params, TemplDecl)) 2044 return ToAlias; 2045 2046 TemplDecl->setDescribedAliasTemplate(ToAlias); 2047 2048 ToAlias->setAccess(D->getAccess()); 2049 ToAlias->setLexicalDeclContext(LexicalDC); 2050 LexicalDC->addDeclInternal(ToAlias); 2051 return ToAlias; 2052 } 2053 2054 Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) { 2055 // Import the major distinguishing characteristics of this label. 2056 DeclContext *DC, *LexicalDC; 2057 DeclarationName Name; 2058 SourceLocation Loc; 2059 NamedDecl *ToD; 2060 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2061 return nullptr; 2062 if (ToD) 2063 return ToD; 2064 2065 assert(LexicalDC->isFunctionOrMethod()); 2066 2067 LabelDecl *ToLabel; 2068 if (D->isGnuLocal() 2069 ? GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, 2070 Importer.Import(D->getLocation()), 2071 Name.getAsIdentifierInfo(), 2072 Importer.Import(D->getLocStart())) 2073 : GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, 2074 Importer.Import(D->getLocation()), 2075 Name.getAsIdentifierInfo())) 2076 return ToLabel; 2077 2078 auto *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt())); 2079 if (!Label) 2080 return nullptr; 2081 2082 ToLabel->setStmt(Label); 2083 ToLabel->setLexicalDeclContext(LexicalDC); 2084 LexicalDC->addDeclInternal(ToLabel); 2085 return ToLabel; 2086 } 2087 2088 Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { 2089 // Import the major distinguishing characteristics of this enum. 2090 DeclContext *DC, *LexicalDC; 2091 DeclarationName Name; 2092 SourceLocation Loc; 2093 NamedDecl *ToD; 2094 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2095 return nullptr; 2096 if (ToD) 2097 return ToD; 2098 2099 // Figure out what enum name we're looking for. 2100 unsigned IDNS = Decl::IDNS_Tag; 2101 DeclarationName SearchName = Name; 2102 if (!SearchName && D->getTypedefNameForAnonDecl()) { 2103 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); 2104 IDNS = Decl::IDNS_Ordinary; 2105 } else if (Importer.getToContext().getLangOpts().CPlusPlus) 2106 IDNS |= Decl::IDNS_Ordinary; 2107 2108 // We may already have an enum of the same name; try to find and match it. 2109 if (!DC->isFunctionOrMethod() && SearchName) { 2110 SmallVector<NamedDecl *, 4> ConflictingDecls; 2111 SmallVector<NamedDecl *, 2> FoundDecls; 2112 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls); 2113 for (auto *FoundDecl : FoundDecls) { 2114 if (!FoundDecl->isInIdentifierNamespace(IDNS)) 2115 continue; 2116 2117 Decl *Found = FoundDecl; 2118 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) { 2119 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) 2120 Found = Tag->getDecl(); 2121 } 2122 2123 if (auto *FoundEnum = dyn_cast<EnumDecl>(Found)) { 2124 if (IsStructuralMatch(D, FoundEnum)) 2125 return Importer.MapImported(D, FoundEnum); 2126 } 2127 2128 ConflictingDecls.push_back(FoundDecl); 2129 } 2130 2131 if (!ConflictingDecls.empty()) { 2132 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2133 ConflictingDecls.data(), 2134 ConflictingDecls.size()); 2135 } 2136 } 2137 2138 // Create the enum declaration. 2139 EnumDecl *D2; 2140 if (GetImportedOrCreateDecl( 2141 D2, D, Importer.getToContext(), DC, Importer.Import(D->getLocStart()), 2142 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(), 2143 D->isScopedUsingClassTag(), D->isFixed())) 2144 return D2; 2145 2146 // Import the qualifier, if any. 2147 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 2148 D2->setAccess(D->getAccess()); 2149 D2->setLexicalDeclContext(LexicalDC); 2150 LexicalDC->addDeclInternal(D2); 2151 2152 // Import the integer type. 2153 QualType ToIntegerType = Importer.Import(D->getIntegerType()); 2154 if (ToIntegerType.isNull()) 2155 return nullptr; 2156 D2->setIntegerType(ToIntegerType); 2157 2158 // Import the definition 2159 if (D->isCompleteDefinition() && ImportDefinition(D, D2)) 2160 return nullptr; 2161 2162 return D2; 2163 } 2164 2165 Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { 2166 // If this record has a definition in the translation unit we're coming from, 2167 // but this particular declaration is not that definition, import the 2168 // definition and map to that. 2169 TagDecl *Definition = D->getDefinition(); 2170 if (Definition && Definition != D && 2171 // In contrast to a normal CXXRecordDecl, the implicit 2172 // CXXRecordDecl of ClassTemplateSpecializationDecl is its redeclaration. 2173 // The definition of the implicit CXXRecordDecl in this case is the 2174 // ClassTemplateSpecializationDecl itself. Thus, we start with an extra 2175 // condition in order to be able to import the implict Decl. 2176 !D->isImplicit()) { 2177 Decl *ImportedDef = Importer.Import(Definition); 2178 if (!ImportedDef) 2179 return nullptr; 2180 2181 return Importer.MapImported(D, ImportedDef); 2182 } 2183 2184 // Import the major distinguishing characteristics of this record. 2185 DeclContext *DC, *LexicalDC; 2186 DeclarationName Name; 2187 SourceLocation Loc; 2188 NamedDecl *ToD; 2189 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2190 return nullptr; 2191 if (ToD) 2192 return ToD; 2193 2194 // Figure out what structure name we're looking for. 2195 unsigned IDNS = Decl::IDNS_Tag; 2196 DeclarationName SearchName = Name; 2197 if (!SearchName && D->getTypedefNameForAnonDecl()) { 2198 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); 2199 IDNS = Decl::IDNS_Ordinary; 2200 } else if (Importer.getToContext().getLangOpts().CPlusPlus) 2201 IDNS |= Decl::IDNS_Ordinary; 2202 2203 // We may already have a record of the same name; try to find and match it. 2204 RecordDecl *AdoptDecl = nullptr; 2205 RecordDecl *PrevDecl = nullptr; 2206 if (!DC->isFunctionOrMethod()) { 2207 SmallVector<NamedDecl *, 4> ConflictingDecls; 2208 SmallVector<NamedDecl *, 2> FoundDecls; 2209 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls); 2210 2211 if (!FoundDecls.empty()) { 2212 // We're going to have to compare D against potentially conflicting Decls, so complete it. 2213 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition()) 2214 D->getASTContext().getExternalSource()->CompleteType(D); 2215 } 2216 2217 for (auto *FoundDecl : FoundDecls) { 2218 if (!FoundDecl->isInIdentifierNamespace(IDNS)) 2219 continue; 2220 2221 Decl *Found = FoundDecl; 2222 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) { 2223 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) 2224 Found = Tag->getDecl(); 2225 } 2226 2227 if (D->getDescribedTemplate()) { 2228 if (auto *Template = dyn_cast<ClassTemplateDecl>(Found)) 2229 Found = Template->getTemplatedDecl(); 2230 else 2231 continue; 2232 } 2233 2234 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) { 2235 if (!SearchName) { 2236 if (!IsStructuralMatch(D, FoundRecord, false)) 2237 continue; 2238 } 2239 2240 PrevDecl = FoundRecord; 2241 2242 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { 2243 if ((SearchName && !D->isCompleteDefinition()) 2244 || (D->isCompleteDefinition() && 2245 D->isAnonymousStructOrUnion() 2246 == FoundDef->isAnonymousStructOrUnion() && 2247 IsStructuralMatch(D, FoundDef))) { 2248 // The record types structurally match, or the "from" translation 2249 // unit only had a forward declaration anyway; call it the same 2250 // function. 2251 // FIXME: Structural equivalence check should check for same 2252 // user-defined methods. 2253 Importer.MapImported(D, FoundDef); 2254 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) { 2255 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef); 2256 assert(FoundCXX && "Record type mismatch"); 2257 2258 if (D->isCompleteDefinition() && !Importer.isMinimalImport()) 2259 // FoundDef may not have every implicit method that D has 2260 // because implicit methods are created only if they are used. 2261 ImportImplicitMethods(DCXX, FoundCXX); 2262 } 2263 return FoundDef; 2264 } 2265 } else if (!D->isCompleteDefinition()) { 2266 // We have a forward declaration of this type, so adopt that forward 2267 // declaration rather than building a new one. 2268 2269 // If one or both can be completed from external storage then try one 2270 // last time to complete and compare them before doing this. 2271 2272 if (FoundRecord->hasExternalLexicalStorage() && 2273 !FoundRecord->isCompleteDefinition()) 2274 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord); 2275 if (D->hasExternalLexicalStorage()) 2276 D->getASTContext().getExternalSource()->CompleteType(D); 2277 2278 if (FoundRecord->isCompleteDefinition() && 2279 D->isCompleteDefinition() && 2280 !IsStructuralMatch(D, FoundRecord)) 2281 continue; 2282 2283 AdoptDecl = FoundRecord; 2284 continue; 2285 } else if (!SearchName) { 2286 continue; 2287 } 2288 } 2289 2290 ConflictingDecls.push_back(FoundDecl); 2291 } 2292 2293 if (!ConflictingDecls.empty() && SearchName) { 2294 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2295 ConflictingDecls.data(), 2296 ConflictingDecls.size()); 2297 } 2298 } 2299 2300 // Create the record declaration. 2301 RecordDecl *D2 = AdoptDecl; 2302 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 2303 if (!D2) { 2304 CXXRecordDecl *D2CXX = nullptr; 2305 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) { 2306 if (DCXX->isLambda()) { 2307 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo()); 2308 if (GetImportedOrCreateSpecialDecl( 2309 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(), 2310 DC, TInfo, Loc, DCXX->isDependentLambda(), 2311 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault())) 2312 return D2CXX; 2313 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl()); 2314 if (DCXX->getLambdaContextDecl() && !CDecl) 2315 return nullptr; 2316 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl); 2317 } else if (DCXX->isInjectedClassName()) { 2318 // We have to be careful to do a similar dance to the one in 2319 // Sema::ActOnStartCXXMemberDeclarations 2320 CXXRecordDecl *const PrevDecl = nullptr; 2321 const bool DelayTypeCreation = true; 2322 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(), 2323 D->getTagKind(), DC, StartLoc, Loc, 2324 Name.getAsIdentifierInfo(), PrevDecl, 2325 DelayTypeCreation)) 2326 return D2CXX; 2327 Importer.getToContext().getTypeDeclType( 2328 D2CXX, dyn_cast<CXXRecordDecl>(DC)); 2329 } else { 2330 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(), 2331 D->getTagKind(), DC, StartLoc, Loc, 2332 Name.getAsIdentifierInfo(), 2333 cast_or_null<CXXRecordDecl>(PrevDecl))) 2334 return D2CXX; 2335 } 2336 2337 D2 = D2CXX; 2338 D2->setAccess(D->getAccess()); 2339 D2->setLexicalDeclContext(LexicalDC); 2340 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit()) 2341 LexicalDC->addDeclInternal(D2); 2342 2343 if (ClassTemplateDecl *FromDescribed = 2344 DCXX->getDescribedClassTemplate()) { 2345 auto *ToDescribed = cast_or_null<ClassTemplateDecl>( 2346 Importer.Import(FromDescribed)); 2347 if (!ToDescribed) 2348 return nullptr; 2349 D2CXX->setDescribedClassTemplate(ToDescribed); 2350 if (!DCXX->isInjectedClassName()) { 2351 // In a record describing a template the type should be an 2352 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the 2353 // previously set type to the correct value here (ToDescribed is not 2354 // available at record create). 2355 // FIXME: The previous type is cleared but not removed from 2356 // ASTContext's internal storage. 2357 CXXRecordDecl *Injected = nullptr; 2358 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) { 2359 auto *Record = dyn_cast<CXXRecordDecl>(Found); 2360 if (Record && Record->isInjectedClassName()) { 2361 Injected = Record; 2362 break; 2363 } 2364 } 2365 D2CXX->setTypeForDecl(nullptr); 2366 Importer.getToContext().getInjectedClassNameType(D2CXX, 2367 ToDescribed->getInjectedClassNameSpecialization()); 2368 if (Injected) { 2369 Injected->setTypeForDecl(nullptr); 2370 Importer.getToContext().getTypeDeclType(Injected, D2CXX); 2371 } 2372 } 2373 } else if (MemberSpecializationInfo *MemberInfo = 2374 DCXX->getMemberSpecializationInfo()) { 2375 TemplateSpecializationKind SK = 2376 MemberInfo->getTemplateSpecializationKind(); 2377 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass(); 2378 auto *ToInst = 2379 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst)); 2380 if (FromInst && !ToInst) 2381 return nullptr; 2382 D2CXX->setInstantiationOfMemberClass(ToInst, SK); 2383 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation( 2384 Importer.Import(MemberInfo->getPointOfInstantiation())); 2385 } 2386 } else { 2387 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), 2388 D->getTagKind(), DC, StartLoc, Loc, 2389 Name.getAsIdentifierInfo(), PrevDecl)) 2390 return D2; 2391 D2->setLexicalDeclContext(LexicalDC); 2392 LexicalDC->addDeclInternal(D2); 2393 } 2394 2395 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 2396 if (D->isAnonymousStructOrUnion()) 2397 D2->setAnonymousStructOrUnion(true); 2398 } 2399 2400 Importer.MapImported(D, D2); 2401 2402 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default)) 2403 return nullptr; 2404 2405 return D2; 2406 } 2407 2408 Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { 2409 // Import the major distinguishing characteristics of this enumerator. 2410 DeclContext *DC, *LexicalDC; 2411 DeclarationName Name; 2412 SourceLocation Loc; 2413 NamedDecl *ToD; 2414 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2415 return nullptr; 2416 if (ToD) 2417 return ToD; 2418 2419 QualType T = Importer.Import(D->getType()); 2420 if (T.isNull()) 2421 return nullptr; 2422 2423 // Determine whether there are any other declarations with the same name and 2424 // in the same context. 2425 if (!LexicalDC->isFunctionOrMethod()) { 2426 SmallVector<NamedDecl *, 4> ConflictingDecls; 2427 unsigned IDNS = Decl::IDNS_Ordinary; 2428 SmallVector<NamedDecl *, 2> FoundDecls; 2429 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2430 for (auto *FoundDecl : FoundDecls) { 2431 if (!FoundDecl->isInIdentifierNamespace(IDNS)) 2432 continue; 2433 2434 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) { 2435 if (IsStructuralMatch(D, FoundEnumConstant)) 2436 return Importer.MapImported(D, FoundEnumConstant); 2437 } 2438 2439 ConflictingDecls.push_back(FoundDecl); 2440 } 2441 2442 if (!ConflictingDecls.empty()) { 2443 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2444 ConflictingDecls.data(), 2445 ConflictingDecls.size()); 2446 if (!Name) 2447 return nullptr; 2448 } 2449 } 2450 2451 Expr *Init = Importer.Import(D->getInitExpr()); 2452 if (D->getInitExpr() && !Init) 2453 return nullptr; 2454 2455 EnumConstantDecl *ToEnumerator; 2456 if (GetImportedOrCreateDecl( 2457 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc, 2458 Name.getAsIdentifierInfo(), T, Init, D->getInitVal())) 2459 return ToEnumerator; 2460 2461 ToEnumerator->setAccess(D->getAccess()); 2462 ToEnumerator->setLexicalDeclContext(LexicalDC); 2463 LexicalDC->addDeclInternal(ToEnumerator); 2464 return ToEnumerator; 2465 } 2466 2467 bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD, 2468 FunctionDecl *ToFD) { 2469 switch (FromFD->getTemplatedKind()) { 2470 case FunctionDecl::TK_NonTemplate: 2471 case FunctionDecl::TK_FunctionTemplate: 2472 return false; 2473 2474 case FunctionDecl::TK_MemberSpecialization: { 2475 auto *InstFD = cast_or_null<FunctionDecl>( 2476 Importer.Import(FromFD->getInstantiatedFromMemberFunction())); 2477 if (!InstFD) 2478 return true; 2479 2480 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind(); 2481 SourceLocation POI = Importer.Import( 2482 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()); 2483 ToFD->setInstantiationOfMemberFunction(InstFD, TSK); 2484 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI); 2485 return false; 2486 } 2487 2488 case FunctionDecl::TK_FunctionTemplateSpecialization: { 2489 FunctionTemplateDecl* Template; 2490 OptionalTemplateArgsTy ToTemplArgs; 2491 std::tie(Template, ToTemplArgs) = 2492 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); 2493 if (!Template || !ToTemplArgs) 2494 return true; 2495 2496 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy( 2497 Importer.getToContext(), *ToTemplArgs); 2498 2499 auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); 2500 TemplateArgumentListInfo ToTAInfo; 2501 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten; 2502 if (FromTAArgsAsWritten) 2503 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo)) 2504 return true; 2505 2506 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation()); 2507 2508 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind(); 2509 ToFD->setFunctionTemplateSpecialization( 2510 Template, ToTAList, /* InsertPos= */ nullptr, 2511 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI); 2512 return false; 2513 } 2514 2515 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { 2516 auto *FromInfo = FromFD->getDependentSpecializationInfo(); 2517 UnresolvedSet<8> TemplDecls; 2518 unsigned NumTemplates = FromInfo->getNumTemplates(); 2519 for (unsigned I = 0; I < NumTemplates; I++) { 2520 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>( 2521 Importer.Import(FromInfo->getTemplate(I)))) 2522 TemplDecls.addDecl(ToFTD); 2523 else 2524 return true; 2525 } 2526 2527 // Import TemplateArgumentListInfo. 2528 TemplateArgumentListInfo ToTAInfo; 2529 if (ImportTemplateArgumentListInfo( 2530 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(), 2531 llvm::makeArrayRef(FromInfo->getTemplateArgs(), 2532 FromInfo->getNumTemplateArgs()), 2533 ToTAInfo)) 2534 return true; 2535 2536 ToFD->setDependentTemplateSpecialization(Importer.getToContext(), 2537 TemplDecls, ToTAInfo); 2538 return false; 2539 } 2540 } 2541 llvm_unreachable("All cases should be covered!"); 2542 } 2543 2544 FunctionDecl * 2545 ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) { 2546 FunctionTemplateDecl* Template; 2547 OptionalTemplateArgsTy ToTemplArgs; 2548 std::tie(Template, ToTemplArgs) = 2549 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); 2550 if (!Template || !ToTemplArgs) 2551 return nullptr; 2552 2553 void *InsertPos = nullptr; 2554 auto *FoundSpec = Template->findSpecialization(*ToTemplArgs, InsertPos); 2555 return FoundSpec; 2556 } 2557 2558 Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { 2559 2560 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D); 2561 auto RedeclIt = Redecls.begin(); 2562 // Import the first part of the decl chain. I.e. import all previous 2563 // declarations starting from the canonical decl. 2564 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) 2565 if (!Importer.Import(*RedeclIt)) 2566 return nullptr; 2567 assert(*RedeclIt == D); 2568 2569 // Import the major distinguishing characteristics of this function. 2570 DeclContext *DC, *LexicalDC; 2571 DeclarationName Name; 2572 SourceLocation Loc; 2573 NamedDecl *ToD; 2574 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2575 return nullptr; 2576 if (ToD) 2577 return ToD; 2578 2579 const FunctionDecl *FoundByLookup = nullptr; 2580 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate(); 2581 2582 // If this is a function template specialization, then try to find the same 2583 // existing specialization in the "to" context. The localUncachedLookup 2584 // below will not find any specialization, but would find the primary 2585 // template; thus, we have to skip normal lookup in case of specializations. 2586 // FIXME handle member function templates (TK_MemberSpecialization) similarly? 2587 if (D->getTemplatedKind() == 2588 FunctionDecl::TK_FunctionTemplateSpecialization) { 2589 if (FunctionDecl *FoundFunction = FindFunctionTemplateSpecialization(D)) { 2590 if (D->doesThisDeclarationHaveABody() && 2591 FoundFunction->hasBody()) 2592 return Importer.Imported(D, FoundFunction); 2593 FoundByLookup = FoundFunction; 2594 } 2595 } 2596 // Try to find a function in our own ("to") context with the same name, same 2597 // type, and in the same context as the function we're importing. 2598 else if (!LexicalDC->isFunctionOrMethod()) { 2599 SmallVector<NamedDecl *, 4> ConflictingDecls; 2600 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; 2601 SmallVector<NamedDecl *, 2> FoundDecls; 2602 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2603 for (auto *FoundDecl : FoundDecls) { 2604 if (!FoundDecl->isInIdentifierNamespace(IDNS)) 2605 continue; 2606 2607 // If template was found, look at the templated function. 2608 if (FromFT) { 2609 if (auto *Template = dyn_cast<FunctionTemplateDecl>(FoundDecl)) 2610 FoundDecl = Template->getTemplatedDecl(); 2611 else 2612 continue; 2613 } 2614 2615 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) { 2616 if (FoundFunction->hasExternalFormalLinkage() && 2617 D->hasExternalFormalLinkage()) { 2618 if (IsStructuralMatch(D, FoundFunction)) { 2619 const FunctionDecl *Definition = nullptr; 2620 if (D->doesThisDeclarationHaveABody() && 2621 FoundFunction->hasBody(Definition)) { 2622 return Importer.MapImported( 2623 D, const_cast<FunctionDecl *>(Definition)); 2624 } 2625 FoundByLookup = FoundFunction; 2626 break; 2627 } 2628 2629 // FIXME: Check for overloading more carefully, e.g., by boosting 2630 // Sema::IsOverload out to the AST library. 2631 2632 // Function overloading is okay in C++. 2633 if (Importer.getToContext().getLangOpts().CPlusPlus) 2634 continue; 2635 2636 // Complain about inconsistent function types. 2637 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) 2638 << Name << D->getType() << FoundFunction->getType(); 2639 Importer.ToDiag(FoundFunction->getLocation(), 2640 diag::note_odr_value_here) 2641 << FoundFunction->getType(); 2642 } 2643 } 2644 2645 ConflictingDecls.push_back(FoundDecl); 2646 } 2647 2648 if (!ConflictingDecls.empty()) { 2649 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2650 ConflictingDecls.data(), 2651 ConflictingDecls.size()); 2652 if (!Name) 2653 return nullptr; 2654 } 2655 } 2656 2657 DeclarationNameInfo NameInfo(Name, Loc); 2658 // Import additional name location/type info. 2659 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); 2660 2661 QualType FromTy = D->getType(); 2662 bool usedDifferentExceptionSpec = false; 2663 2664 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) { 2665 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); 2666 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the 2667 // FunctionDecl that we are importing the FunctionProtoType for. 2668 // To avoid an infinite recursion when importing, create the FunctionDecl 2669 // with a simplified function type and update it afterwards. 2670 if (FromEPI.ExceptionSpec.SourceDecl || 2671 FromEPI.ExceptionSpec.SourceTemplate || 2672 FromEPI.ExceptionSpec.NoexceptExpr) { 2673 FunctionProtoType::ExtProtoInfo DefaultEPI; 2674 FromTy = Importer.getFromContext().getFunctionType( 2675 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI); 2676 usedDifferentExceptionSpec = true; 2677 } 2678 } 2679 2680 // Import the type. 2681 QualType T = Importer.Import(FromTy); 2682 if (T.isNull()) 2683 return nullptr; 2684 2685 // Import the function parameters. 2686 SmallVector<ParmVarDecl *, 8> Parameters; 2687 for (auto P : D->parameters()) { 2688 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P)); 2689 if (!ToP) 2690 return nullptr; 2691 2692 Parameters.push_back(ToP); 2693 } 2694 2695 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 2696 if (D->getTypeSourceInfo() && !TInfo) 2697 return nullptr; 2698 2699 // Create the imported function. 2700 FunctionDecl *ToFunction = nullptr; 2701 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart()); 2702 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { 2703 if (GetImportedOrCreateDecl<CXXConstructorDecl>( 2704 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC), 2705 InnerLocStart, NameInfo, T, TInfo, FromConstructor->isExplicit(), 2706 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr())) 2707 return ToFunction; 2708 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) { 2709 SmallVector<CXXCtorInitializer *, 4> CtorInitializers; 2710 for (auto *I : FromConstructor->inits()) { 2711 auto *ToI = cast_or_null<CXXCtorInitializer>(Importer.Import(I)); 2712 if (!ToI && I) 2713 return nullptr; 2714 CtorInitializers.push_back(ToI); 2715 } 2716 auto **Memory = 2717 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers]; 2718 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory); 2719 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction); 2720 ToCtor->setCtorInitializers(Memory); 2721 ToCtor->setNumCtorInitializers(NumInitializers); 2722 } 2723 } else if (isa<CXXDestructorDecl>(D)) { 2724 if (GetImportedOrCreateDecl<CXXDestructorDecl>( 2725 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC), 2726 InnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(), 2727 D->isImplicit())) 2728 return ToFunction; 2729 } else if (CXXConversionDecl *FromConversion = 2730 dyn_cast<CXXConversionDecl>(D)) { 2731 if (GetImportedOrCreateDecl<CXXConversionDecl>( 2732 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC), 2733 InnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(), 2734 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation())) 2735 return ToFunction; 2736 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) { 2737 if (GetImportedOrCreateDecl<CXXMethodDecl>( 2738 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC), 2739 InnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(), 2740 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation())) 2741 return ToFunction; 2742 } else { 2743 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC, 2744 InnerLocStart, NameInfo, T, TInfo, 2745 D->getStorageClass(), D->isInlineSpecified(), 2746 D->hasWrittenPrototype(), D->isConstexpr())) 2747 return ToFunction; 2748 } 2749 2750 // Import the qualifier, if any. 2751 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 2752 ToFunction->setAccess(D->getAccess()); 2753 ToFunction->setLexicalDeclContext(LexicalDC); 2754 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); 2755 ToFunction->setTrivial(D->isTrivial()); 2756 ToFunction->setPure(D->isPure()); 2757 ToFunction->setRangeEnd(Importer.Import(D->getLocEnd())); 2758 2759 // Set the parameters. 2760 for (auto *Param : Parameters) { 2761 Param->setOwningFunction(ToFunction); 2762 ToFunction->addDeclInternal(Param); 2763 } 2764 ToFunction->setParams(Parameters); 2765 2766 if (FoundByLookup) { 2767 auto *Recent = const_cast<FunctionDecl *>( 2768 FoundByLookup->getMostRecentDecl()); 2769 ToFunction->setPreviousDecl(Recent); 2770 } 2771 2772 // We need to complete creation of FunctionProtoTypeLoc manually with setting 2773 // params it refers to. 2774 if (TInfo) { 2775 if (auto ProtoLoc = 2776 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) { 2777 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) 2778 ProtoLoc.setParam(I, Parameters[I]); 2779 } 2780 } 2781 2782 if (usedDifferentExceptionSpec) { 2783 // Update FunctionProtoType::ExtProtoInfo. 2784 QualType T = Importer.Import(D->getType()); 2785 if (T.isNull()) 2786 return nullptr; 2787 ToFunction->setType(T); 2788 } 2789 2790 // Import the describing template function, if any. 2791 if (FromFT) 2792 if (!Importer.Import(FromFT)) 2793 return nullptr; 2794 2795 if (D->doesThisDeclarationHaveABody()) { 2796 if (Stmt *FromBody = D->getBody()) { 2797 if (Stmt *ToBody = Importer.Import(FromBody)) { 2798 ToFunction->setBody(ToBody); 2799 } 2800 } 2801 } 2802 2803 // FIXME: Other bits to merge? 2804 2805 // If it is a template, import all related things. 2806 if (ImportTemplateInformation(D, ToFunction)) 2807 return nullptr; 2808 2809 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend); 2810 2811 // TODO Can we generalize this approach to other AST nodes as well? 2812 if (D->getDeclContext()->containsDeclAndLoad(D)) 2813 DC->addDeclInternal(ToFunction); 2814 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D)) 2815 LexicalDC->addDeclInternal(ToFunction); 2816 2817 // Friend declaration's lexical context is the befriending class, but the 2818 // semantic context is the enclosing scope of the befriending class. 2819 // We want the friend functions to be found in the semantic context by lookup. 2820 // FIXME should we handle this generically in VisitFriendDecl? 2821 // In Other cases when LexicalDC != DC we don't want it to be added, 2822 // e.g out-of-class definitions like void B::f() {} . 2823 if (LexicalDC != DC && IsFriend) { 2824 DC->makeDeclVisibleInContext(ToFunction); 2825 } 2826 2827 // Import the rest of the chain. I.e. import all subsequent declarations. 2828 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) 2829 if (!Importer.Import(*RedeclIt)) 2830 return nullptr; 2831 2832 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D)) 2833 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod); 2834 2835 return ToFunction; 2836 } 2837 2838 Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { 2839 return VisitFunctionDecl(D); 2840 } 2841 2842 Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { 2843 return VisitCXXMethodDecl(D); 2844 } 2845 2846 Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { 2847 return VisitCXXMethodDecl(D); 2848 } 2849 2850 Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { 2851 return VisitCXXMethodDecl(D); 2852 } 2853 2854 Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { 2855 // Import the major distinguishing characteristics of a variable. 2856 DeclContext *DC, *LexicalDC; 2857 DeclarationName Name; 2858 SourceLocation Loc; 2859 NamedDecl *ToD; 2860 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2861 return nullptr; 2862 if (ToD) 2863 return ToD; 2864 2865 // Determine whether we've already imported this field. 2866 SmallVector<NamedDecl *, 2> FoundDecls; 2867 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2868 for (auto *FoundDecl : FoundDecls) { 2869 if (auto *FoundField = dyn_cast<FieldDecl>(FoundDecl)) { 2870 // For anonymous fields, match up by index. 2871 if (!Name && 2872 ASTImporter::getFieldIndex(D) != 2873 ASTImporter::getFieldIndex(FoundField)) 2874 continue; 2875 2876 if (Importer.IsStructurallyEquivalent(D->getType(), 2877 FoundField->getType())) { 2878 Importer.MapImported(D, FoundField); 2879 return FoundField; 2880 } 2881 2882 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) 2883 << Name << D->getType() << FoundField->getType(); 2884 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) 2885 << FoundField->getType(); 2886 return nullptr; 2887 } 2888 } 2889 2890 // Import the type. 2891 QualType T = Importer.Import(D->getType()); 2892 if (T.isNull()) 2893 return nullptr; 2894 2895 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 2896 Expr *BitWidth = Importer.Import(D->getBitWidth()); 2897 if (!BitWidth && D->getBitWidth()) 2898 return nullptr; 2899 2900 FieldDecl *ToField; 2901 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC, 2902 Importer.Import(D->getInnerLocStart()), Loc, 2903 Name.getAsIdentifierInfo(), T, TInfo, BitWidth, 2904 D->isMutable(), D->getInClassInitStyle())) 2905 return ToField; 2906 2907 ToField->setAccess(D->getAccess()); 2908 ToField->setLexicalDeclContext(LexicalDC); 2909 if (Expr *FromInitializer = D->getInClassInitializer()) { 2910 Expr *ToInitializer = Importer.Import(FromInitializer); 2911 if (ToInitializer) 2912 ToField->setInClassInitializer(ToInitializer); 2913 else 2914 return nullptr; 2915 } 2916 ToField->setImplicit(D->isImplicit()); 2917 LexicalDC->addDeclInternal(ToField); 2918 return ToField; 2919 } 2920 2921 Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { 2922 // Import the major distinguishing characteristics of a variable. 2923 DeclContext *DC, *LexicalDC; 2924 DeclarationName Name; 2925 SourceLocation Loc; 2926 NamedDecl *ToD; 2927 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2928 return nullptr; 2929 if (ToD) 2930 return ToD; 2931 2932 // Determine whether we've already imported this field. 2933 SmallVector<NamedDecl *, 2> FoundDecls; 2934 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2935 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2936 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) { 2937 // For anonymous indirect fields, match up by index. 2938 if (!Name && 2939 ASTImporter::getFieldIndex(D) != 2940 ASTImporter::getFieldIndex(FoundField)) 2941 continue; 2942 2943 if (Importer.IsStructurallyEquivalent(D->getType(), 2944 FoundField->getType(), 2945 !Name.isEmpty())) { 2946 Importer.MapImported(D, FoundField); 2947 return FoundField; 2948 } 2949 2950 // If there are more anonymous fields to check, continue. 2951 if (!Name && I < N-1) 2952 continue; 2953 2954 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) 2955 << Name << D->getType() << FoundField->getType(); 2956 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) 2957 << FoundField->getType(); 2958 return nullptr; 2959 } 2960 } 2961 2962 // Import the type. 2963 QualType T = Importer.Import(D->getType()); 2964 if (T.isNull()) 2965 return nullptr; 2966 2967 auto **NamedChain = 2968 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()]; 2969 2970 unsigned i = 0; 2971 for (auto *PI : D->chain()) { 2972 Decl *D = Importer.Import(PI); 2973 if (!D) 2974 return nullptr; 2975 NamedChain[i++] = cast<NamedDecl>(D); 2976 } 2977 2978 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()}; 2979 IndirectFieldDecl *ToIndirectField; 2980 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC, 2981 Loc, Name.getAsIdentifierInfo(), T, CH)) 2982 // FIXME here we leak `NamedChain` which is allocated before 2983 return ToIndirectField; 2984 2985 for (const auto *A : D->attrs()) 2986 ToIndirectField->addAttr(Importer.Import(A)); 2987 2988 ToIndirectField->setAccess(D->getAccess()); 2989 ToIndirectField->setLexicalDeclContext(LexicalDC); 2990 LexicalDC->addDeclInternal(ToIndirectField); 2991 return ToIndirectField; 2992 } 2993 2994 Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) { 2995 // Import the major distinguishing characteristics of a declaration. 2996 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 2997 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext() 2998 ? DC : Importer.ImportContext(D->getLexicalDeclContext()); 2999 if (!DC || !LexicalDC) 3000 return nullptr; 3001 3002 // Determine whether we've already imported this decl. 3003 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup. 3004 auto *RD = cast<CXXRecordDecl>(DC); 3005 FriendDecl *ImportedFriend = RD->getFirstFriend(); 3006 3007 while (ImportedFriend) { 3008 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) { 3009 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(), 3010 /*Complain=*/false)) 3011 return Importer.MapImported(D, ImportedFriend); 3012 3013 } else if (D->getFriendType() && ImportedFriend->getFriendType()) { 3014 if (Importer.IsStructurallyEquivalent( 3015 D->getFriendType()->getType(), 3016 ImportedFriend->getFriendType()->getType(), true)) 3017 return Importer.MapImported(D, ImportedFriend); 3018 } 3019 ImportedFriend = ImportedFriend->getNextFriend(); 3020 } 3021 3022 // Not found. Create it. 3023 FriendDecl::FriendUnion ToFU; 3024 if (NamedDecl *FriendD = D->getFriendDecl()) { 3025 auto *ToFriendD = cast_or_null<NamedDecl>(Importer.Import(FriendD)); 3026 if (ToFriendD && FriendD->getFriendObjectKind() != Decl::FOK_None && 3027 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator))) 3028 ToFriendD->setObjectOfFriendDecl(false); 3029 3030 ToFU = ToFriendD; 3031 } else // The friend is a type, not a decl. 3032 ToFU = Importer.Import(D->getFriendType()); 3033 if (!ToFU) 3034 return nullptr; 3035 3036 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists); 3037 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>(); 3038 for (unsigned I = 0; I < D->NumTPLists; I++) { 3039 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]); 3040 if (!List) 3041 return nullptr; 3042 ToTPLists[I] = List; 3043 } 3044 3045 FriendDecl *FrD; 3046 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC, 3047 Importer.Import(D->getLocation()), ToFU, 3048 Importer.Import(D->getFriendLoc()), ToTPLists)) 3049 return FrD; 3050 3051 FrD->setAccess(D->getAccess()); 3052 FrD->setLexicalDeclContext(LexicalDC); 3053 LexicalDC->addDeclInternal(FrD); 3054 return FrD; 3055 } 3056 3057 Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { 3058 // Import the major distinguishing characteristics of an ivar. 3059 DeclContext *DC, *LexicalDC; 3060 DeclarationName Name; 3061 SourceLocation Loc; 3062 NamedDecl *ToD; 3063 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3064 return nullptr; 3065 if (ToD) 3066 return ToD; 3067 3068 // Determine whether we've already imported this ivar 3069 SmallVector<NamedDecl *, 2> FoundDecls; 3070 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3071 for (auto *FoundDecl : FoundDecls) { 3072 if (auto *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) { 3073 if (Importer.IsStructurallyEquivalent(D->getType(), 3074 FoundIvar->getType())) { 3075 Importer.MapImported(D, FoundIvar); 3076 return FoundIvar; 3077 } 3078 3079 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent) 3080 << Name << D->getType() << FoundIvar->getType(); 3081 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) 3082 << FoundIvar->getType(); 3083 return nullptr; 3084 } 3085 } 3086 3087 // Import the type. 3088 QualType T = Importer.Import(D->getType()); 3089 if (T.isNull()) 3090 return nullptr; 3091 3092 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3093 Expr *BitWidth = Importer.Import(D->getBitWidth()); 3094 if (!BitWidth && D->getBitWidth()) 3095 return nullptr; 3096 3097 ObjCIvarDecl *ToIvar; 3098 if (GetImportedOrCreateDecl( 3099 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC), 3100 Importer.Import(D->getInnerLocStart()), Loc, 3101 Name.getAsIdentifierInfo(), T, TInfo, D->getAccessControl(), BitWidth, 3102 D->getSynthesize())) 3103 return ToIvar; 3104 3105 ToIvar->setLexicalDeclContext(LexicalDC); 3106 LexicalDC->addDeclInternal(ToIvar); 3107 return ToIvar; 3108 } 3109 3110 Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { 3111 // Import the major distinguishing characteristics of a variable. 3112 DeclContext *DC, *LexicalDC; 3113 DeclarationName Name; 3114 SourceLocation Loc; 3115 NamedDecl *ToD; 3116 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3117 return nullptr; 3118 if (ToD) 3119 return ToD; 3120 3121 // Try to find a variable in our own ("to") context with the same name and 3122 // in the same context as the variable we're importing. 3123 if (D->isFileVarDecl()) { 3124 VarDecl *MergeWithVar = nullptr; 3125 SmallVector<NamedDecl *, 4> ConflictingDecls; 3126 unsigned IDNS = Decl::IDNS_Ordinary; 3127 SmallVector<NamedDecl *, 2> FoundDecls; 3128 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3129 for (auto *FoundDecl : FoundDecls) { 3130 if (!FoundDecl->isInIdentifierNamespace(IDNS)) 3131 continue; 3132 3133 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) { 3134 // We have found a variable that we may need to merge with. Check it. 3135 if (FoundVar->hasExternalFormalLinkage() && 3136 D->hasExternalFormalLinkage()) { 3137 if (Importer.IsStructurallyEquivalent(D->getType(), 3138 FoundVar->getType())) { 3139 MergeWithVar = FoundVar; 3140 break; 3141 } 3142 3143 const ArrayType *FoundArray 3144 = Importer.getToContext().getAsArrayType(FoundVar->getType()); 3145 const ArrayType *TArray 3146 = Importer.getToContext().getAsArrayType(D->getType()); 3147 if (FoundArray && TArray) { 3148 if (isa<IncompleteArrayType>(FoundArray) && 3149 isa<ConstantArrayType>(TArray)) { 3150 // Import the type. 3151 QualType T = Importer.Import(D->getType()); 3152 if (T.isNull()) 3153 return nullptr; 3154 3155 FoundVar->setType(T); 3156 MergeWithVar = FoundVar; 3157 break; 3158 } else if (isa<IncompleteArrayType>(TArray) && 3159 isa<ConstantArrayType>(FoundArray)) { 3160 MergeWithVar = FoundVar; 3161 break; 3162 } 3163 } 3164 3165 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) 3166 << Name << D->getType() << FoundVar->getType(); 3167 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) 3168 << FoundVar->getType(); 3169 } 3170 } 3171 3172 ConflictingDecls.push_back(FoundDecl); 3173 } 3174 3175 if (MergeWithVar) { 3176 // An equivalent variable with external linkage has been found. Link 3177 // the two declarations, then merge them. 3178 Importer.MapImported(D, MergeWithVar); 3179 updateFlags(D, MergeWithVar); 3180 3181 if (VarDecl *DDef = D->getDefinition()) { 3182 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { 3183 Importer.ToDiag(ExistingDef->getLocation(), 3184 diag::err_odr_variable_multiple_def) 3185 << Name; 3186 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); 3187 } else { 3188 Expr *Init = Importer.Import(DDef->getInit()); 3189 MergeWithVar->setInit(Init); 3190 if (DDef->isInitKnownICE()) { 3191 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt(); 3192 Eval->CheckedICE = true; 3193 Eval->IsICE = DDef->isInitICE(); 3194 } 3195 } 3196 } 3197 3198 return MergeWithVar; 3199 } 3200 3201 if (!ConflictingDecls.empty()) { 3202 Name = Importer.HandleNameConflict(Name, DC, IDNS, 3203 ConflictingDecls.data(), 3204 ConflictingDecls.size()); 3205 if (!Name) 3206 return nullptr; 3207 } 3208 } 3209 3210 // Import the type. 3211 QualType T = Importer.Import(D->getType()); 3212 if (T.isNull()) 3213 return nullptr; 3214 3215 // Create the imported variable. 3216 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3217 VarDecl *ToVar; 3218 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC, 3219 Importer.Import(D->getInnerLocStart()), Loc, 3220 Name.getAsIdentifierInfo(), T, TInfo, 3221 D->getStorageClass())) 3222 return ToVar; 3223 3224 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 3225 ToVar->setAccess(D->getAccess()); 3226 ToVar->setLexicalDeclContext(LexicalDC); 3227 3228 // Templated declarations should never appear in the enclosing DeclContext. 3229 if (!D->getDescribedVarTemplate()) 3230 LexicalDC->addDeclInternal(ToVar); 3231 3232 // Merge the initializer. 3233 if (ImportDefinition(D, ToVar)) 3234 return nullptr; 3235 3236 if (D->isConstexpr()) 3237 ToVar->setConstexpr(true); 3238 3239 return ToVar; 3240 } 3241 3242 Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { 3243 // Parameters are created in the translation unit's context, then moved 3244 // into the function declaration's context afterward. 3245 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); 3246 3247 // Import the name of this declaration. 3248 DeclarationName Name = Importer.Import(D->getDeclName()); 3249 if (D->getDeclName() && !Name) 3250 return nullptr; 3251 3252 // Import the location of this declaration. 3253 SourceLocation Loc = Importer.Import(D->getLocation()); 3254 3255 // Import the parameter's type. 3256 QualType T = Importer.Import(D->getType()); 3257 if (T.isNull()) 3258 return nullptr; 3259 3260 // Create the imported parameter. 3261 ImplicitParamDecl *ToParm = nullptr; 3262 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC, Loc, 3263 Name.getAsIdentifierInfo(), T, 3264 D->getParameterKind())) 3265 return ToParm; 3266 return ToParm; 3267 } 3268 3269 Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { 3270 // Parameters are created in the translation unit's context, then moved 3271 // into the function declaration's context afterward. 3272 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); 3273 3274 // Import the name of this declaration. 3275 DeclarationName Name = Importer.Import(D->getDeclName()); 3276 if (D->getDeclName() && !Name) 3277 return nullptr; 3278 3279 // Import the location of this declaration. 3280 SourceLocation Loc = Importer.Import(D->getLocation()); 3281 3282 // Import the parameter's type. 3283 QualType T = Importer.Import(D->getType()); 3284 if (T.isNull()) 3285 return nullptr; 3286 3287 // Create the imported parameter. 3288 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3289 ParmVarDecl *ToParm; 3290 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC, 3291 Importer.Import(D->getInnerLocStart()), Loc, 3292 Name.getAsIdentifierInfo(), T, TInfo, 3293 D->getStorageClass(), 3294 /*DefaultArg*/ nullptr)) 3295 return ToParm; 3296 3297 // Set the default argument. 3298 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg()); 3299 ToParm->setKNRPromoted(D->isKNRPromoted()); 3300 3301 Expr *ToDefArg = nullptr; 3302 Expr *FromDefArg = nullptr; 3303 if (D->hasUninstantiatedDefaultArg()) { 3304 FromDefArg = D->getUninstantiatedDefaultArg(); 3305 ToDefArg = Importer.Import(FromDefArg); 3306 ToParm->setUninstantiatedDefaultArg(ToDefArg); 3307 } else if (D->hasUnparsedDefaultArg()) { 3308 ToParm->setUnparsedDefaultArg(); 3309 } else if (D->hasDefaultArg()) { 3310 FromDefArg = D->getDefaultArg(); 3311 ToDefArg = Importer.Import(FromDefArg); 3312 ToParm->setDefaultArg(ToDefArg); 3313 } 3314 if (FromDefArg && !ToDefArg) 3315 return nullptr; 3316 3317 if (D->isObjCMethodParameter()) { 3318 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex()); 3319 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier()); 3320 } else { 3321 ToParm->setScopeInfo(D->getFunctionScopeDepth(), 3322 D->getFunctionScopeIndex()); 3323 } 3324 3325 return ToParm; 3326 } 3327 3328 Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { 3329 // Import the major distinguishing characteristics of a method. 3330 DeclContext *DC, *LexicalDC; 3331 DeclarationName Name; 3332 SourceLocation Loc; 3333 NamedDecl *ToD; 3334 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3335 return nullptr; 3336 if (ToD) 3337 return ToD; 3338 3339 SmallVector<NamedDecl *, 2> FoundDecls; 3340 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3341 for (auto *FoundDecl : FoundDecls) { 3342 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) { 3343 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) 3344 continue; 3345 3346 // Check return types. 3347 if (!Importer.IsStructurallyEquivalent(D->getReturnType(), 3348 FoundMethod->getReturnType())) { 3349 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent) 3350 << D->isInstanceMethod() << Name << D->getReturnType() 3351 << FoundMethod->getReturnType(); 3352 Importer.ToDiag(FoundMethod->getLocation(), 3353 diag::note_odr_objc_method_here) 3354 << D->isInstanceMethod() << Name; 3355 return nullptr; 3356 } 3357 3358 // Check the number of parameters. 3359 if (D->param_size() != FoundMethod->param_size()) { 3360 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent) 3361 << D->isInstanceMethod() << Name 3362 << D->param_size() << FoundMethod->param_size(); 3363 Importer.ToDiag(FoundMethod->getLocation(), 3364 diag::note_odr_objc_method_here) 3365 << D->isInstanceMethod() << Name; 3366 return nullptr; 3367 } 3368 3369 // Check parameter types. 3370 for (ObjCMethodDecl::param_iterator P = D->param_begin(), 3371 PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); 3372 P != PEnd; ++P, ++FoundP) { 3373 if (!Importer.IsStructurallyEquivalent((*P)->getType(), 3374 (*FoundP)->getType())) { 3375 Importer.FromDiag((*P)->getLocation(), 3376 diag::err_odr_objc_method_param_type_inconsistent) 3377 << D->isInstanceMethod() << Name 3378 << (*P)->getType() << (*FoundP)->getType(); 3379 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) 3380 << (*FoundP)->getType(); 3381 return nullptr; 3382 } 3383 } 3384 3385 // Check variadic/non-variadic. 3386 // Check the number of parameters. 3387 if (D->isVariadic() != FoundMethod->isVariadic()) { 3388 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent) 3389 << D->isInstanceMethod() << Name; 3390 Importer.ToDiag(FoundMethod->getLocation(), 3391 diag::note_odr_objc_method_here) 3392 << D->isInstanceMethod() << Name; 3393 return nullptr; 3394 } 3395 3396 // FIXME: Any other bits we need to merge? 3397 return Importer.MapImported(D, FoundMethod); 3398 } 3399 } 3400 3401 // Import the result type. 3402 QualType ResultTy = Importer.Import(D->getReturnType()); 3403 if (ResultTy.isNull()) 3404 return nullptr; 3405 3406 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo()); 3407 3408 ObjCMethodDecl *ToMethod; 3409 if (GetImportedOrCreateDecl( 3410 ToMethod, D, Importer.getToContext(), Loc, 3411 Importer.Import(D->getLocEnd()), Name.getObjCSelector(), ResultTy, 3412 ReturnTInfo, DC, D->isInstanceMethod(), D->isVariadic(), 3413 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(), 3414 D->getImplementationControl(), D->hasRelatedResultType())) 3415 return ToMethod; 3416 3417 // FIXME: When we decide to merge method definitions, we'll need to 3418 // deal with implicit parameters. 3419 3420 // Import the parameters 3421 SmallVector<ParmVarDecl *, 5> ToParams; 3422 for (auto *FromP : D->parameters()) { 3423 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP)); 3424 if (!ToP) 3425 return nullptr; 3426 3427 ToParams.push_back(ToP); 3428 } 3429 3430 // Set the parameters. 3431 for (auto *ToParam : ToParams) { 3432 ToParam->setOwningFunction(ToMethod); 3433 ToMethod->addDeclInternal(ToParam); 3434 } 3435 3436 SmallVector<SourceLocation, 12> SelLocs; 3437 D->getSelectorLocs(SelLocs); 3438 for (auto &Loc : SelLocs) 3439 Loc = Importer.Import(Loc); 3440 3441 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs); 3442 3443 ToMethod->setLexicalDeclContext(LexicalDC); 3444 LexicalDC->addDeclInternal(ToMethod); 3445 return ToMethod; 3446 } 3447 3448 Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { 3449 // Import the major distinguishing characteristics of a category. 3450 DeclContext *DC, *LexicalDC; 3451 DeclarationName Name; 3452 SourceLocation Loc; 3453 NamedDecl *ToD; 3454 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3455 return nullptr; 3456 if (ToD) 3457 return ToD; 3458 3459 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo()); 3460 if (!BoundInfo) 3461 return nullptr; 3462 3463 ObjCTypeParamDecl *Result; 3464 if (GetImportedOrCreateDecl( 3465 Result, D, Importer.getToContext(), DC, D->getVariance(), 3466 Importer.Import(D->getVarianceLoc()), D->getIndex(), 3467 Importer.Import(D->getLocation()), Name.getAsIdentifierInfo(), 3468 Importer.Import(D->getColonLoc()), BoundInfo)) 3469 return Result; 3470 3471 Result->setLexicalDeclContext(LexicalDC); 3472 return Result; 3473 } 3474 3475 Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { 3476 // Import the major distinguishing characteristics of a category. 3477 DeclContext *DC, *LexicalDC; 3478 DeclarationName Name; 3479 SourceLocation Loc; 3480 NamedDecl *ToD; 3481 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3482 return nullptr; 3483 if (ToD) 3484 return ToD; 3485 3486 auto *ToInterface = 3487 cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface())); 3488 if (!ToInterface) 3489 return nullptr; 3490 3491 // Determine if we've already encountered this category. 3492 ObjCCategoryDecl *MergeWithCategory 3493 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); 3494 ObjCCategoryDecl *ToCategory = MergeWithCategory; 3495 if (!ToCategory) { 3496 3497 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC, 3498 Importer.Import(D->getAtStartLoc()), Loc, 3499 Importer.Import(D->getCategoryNameLoc()), 3500 Name.getAsIdentifierInfo(), ToInterface, 3501 /*TypeParamList=*/nullptr, 3502 Importer.Import(D->getIvarLBraceLoc()), 3503 Importer.Import(D->getIvarRBraceLoc()))) 3504 return ToCategory; 3505 3506 ToCategory->setLexicalDeclContext(LexicalDC); 3507 LexicalDC->addDeclInternal(ToCategory); 3508 // Import the type parameter list after calling Imported, to avoid 3509 // loops when bringing in their DeclContext. 3510 ToCategory->setTypeParamList(ImportObjCTypeParamList( 3511 D->getTypeParamList())); 3512 3513 // Import protocols 3514 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3515 SmallVector<SourceLocation, 4> ProtocolLocs; 3516 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc 3517 = D->protocol_loc_begin(); 3518 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), 3519 FromProtoEnd = D->protocol_end(); 3520 FromProto != FromProtoEnd; 3521 ++FromProto, ++FromProtoLoc) { 3522 auto *ToProto = 3523 cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3524 if (!ToProto) 3525 return nullptr; 3526 Protocols.push_back(ToProto); 3527 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3528 } 3529 3530 // FIXME: If we're merging, make sure that the protocol list is the same. 3531 ToCategory->setProtocolList(Protocols.data(), Protocols.size(), 3532 ProtocolLocs.data(), Importer.getToContext()); 3533 } else { 3534 Importer.MapImported(D, ToCategory); 3535 } 3536 3537 // Import all of the members of this category. 3538 ImportDeclContext(D); 3539 3540 // If we have an implementation, import it as well. 3541 if (D->getImplementation()) { 3542 auto *Impl = 3543 cast_or_null<ObjCCategoryImplDecl>( 3544 Importer.Import(D->getImplementation())); 3545 if (!Impl) 3546 return nullptr; 3547 3548 ToCategory->setImplementation(Impl); 3549 } 3550 3551 return ToCategory; 3552 } 3553 3554 bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From, 3555 ObjCProtocolDecl *To, 3556 ImportDefinitionKind Kind) { 3557 if (To->getDefinition()) { 3558 if (shouldForceImportDeclContext(Kind)) 3559 ImportDeclContext(From); 3560 return false; 3561 } 3562 3563 // Start the protocol definition 3564 To->startDefinition(); 3565 3566 // Import protocols 3567 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3568 SmallVector<SourceLocation, 4> ProtocolLocs; 3569 ObjCProtocolDecl::protocol_loc_iterator 3570 FromProtoLoc = From->protocol_loc_begin(); 3571 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), 3572 FromProtoEnd = From->protocol_end(); 3573 FromProto != FromProtoEnd; 3574 ++FromProto, ++FromProtoLoc) { 3575 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3576 if (!ToProto) 3577 return true; 3578 Protocols.push_back(ToProto); 3579 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3580 } 3581 3582 // FIXME: If we're merging, make sure that the protocol list is the same. 3583 To->setProtocolList(Protocols.data(), Protocols.size(), 3584 ProtocolLocs.data(), Importer.getToContext()); 3585 3586 if (shouldForceImportDeclContext(Kind)) { 3587 // Import all of the members of this protocol. 3588 ImportDeclContext(From, /*ForceImport=*/true); 3589 } 3590 return false; 3591 } 3592 3593 Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { 3594 // If this protocol has a definition in the translation unit we're coming 3595 // from, but this particular declaration is not that definition, import the 3596 // definition and map to that. 3597 ObjCProtocolDecl *Definition = D->getDefinition(); 3598 if (Definition && Definition != D) { 3599 Decl *ImportedDef = Importer.Import(Definition); 3600 if (!ImportedDef) 3601 return nullptr; 3602 3603 return Importer.MapImported(D, ImportedDef); 3604 } 3605 3606 // Import the major distinguishing characteristics of a protocol. 3607 DeclContext *DC, *LexicalDC; 3608 DeclarationName Name; 3609 SourceLocation Loc; 3610 NamedDecl *ToD; 3611 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3612 return nullptr; 3613 if (ToD) 3614 return ToD; 3615 3616 ObjCProtocolDecl *MergeWithProtocol = nullptr; 3617 SmallVector<NamedDecl *, 2> FoundDecls; 3618 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3619 for (auto *FoundDecl : FoundDecls) { 3620 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) 3621 continue; 3622 3623 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl))) 3624 break; 3625 } 3626 3627 ObjCProtocolDecl *ToProto = MergeWithProtocol; 3628 if (!ToProto) { 3629 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC, 3630 Name.getAsIdentifierInfo(), Loc, 3631 Importer.Import(D->getAtStartLoc()), 3632 /*PrevDecl=*/nullptr)) 3633 return ToProto; 3634 ToProto->setLexicalDeclContext(LexicalDC); 3635 LexicalDC->addDeclInternal(ToProto); 3636 } 3637 3638 Importer.MapImported(D, ToProto); 3639 3640 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto)) 3641 return nullptr; 3642 3643 return ToProto; 3644 } 3645 3646 Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 3647 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 3648 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 3649 3650 SourceLocation ExternLoc = Importer.Import(D->getExternLoc()); 3651 SourceLocation LangLoc = Importer.Import(D->getLocation()); 3652 3653 bool HasBraces = D->hasBraces(); 3654 3655 LinkageSpecDecl *ToLinkageSpec; 3656 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC, 3657 ExternLoc, LangLoc, D->getLanguage(), HasBraces)) 3658 return ToLinkageSpec; 3659 3660 if (HasBraces) { 3661 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc()); 3662 ToLinkageSpec->setRBraceLoc(RBraceLoc); 3663 } 3664 3665 ToLinkageSpec->setLexicalDeclContext(LexicalDC); 3666 LexicalDC->addDeclInternal(ToLinkageSpec); 3667 3668 return ToLinkageSpec; 3669 } 3670 3671 Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) { 3672 DeclContext *DC, *LexicalDC; 3673 DeclarationName Name; 3674 SourceLocation Loc; 3675 NamedDecl *ToD = nullptr; 3676 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3677 return nullptr; 3678 if (ToD) 3679 return ToD; 3680 3681 DeclarationNameInfo NameInfo(Name, 3682 Importer.Import(D->getNameInfo().getLoc())); 3683 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); 3684 3685 UsingDecl *ToUsing; 3686 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC, 3687 Importer.Import(D->getUsingLoc()), 3688 Importer.Import(D->getQualifierLoc()), NameInfo, 3689 D->hasTypename())) 3690 return ToUsing; 3691 3692 ToUsing->setLexicalDeclContext(LexicalDC); 3693 LexicalDC->addDeclInternal(ToUsing); 3694 3695 if (NamedDecl *FromPattern = 3696 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) { 3697 if (auto *ToPattern = 3698 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern))) 3699 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern); 3700 else 3701 return nullptr; 3702 } 3703 3704 for (auto *FromShadow : D->shadows()) { 3705 if (auto *ToShadow = 3706 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow))) 3707 ToUsing->addShadowDecl(ToShadow); 3708 else 3709 // FIXME: We return a nullptr here but the definition is already created 3710 // and available with lookups. How to fix this?.. 3711 return nullptr; 3712 } 3713 return ToUsing; 3714 } 3715 3716 Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) { 3717 DeclContext *DC, *LexicalDC; 3718 DeclarationName Name; 3719 SourceLocation Loc; 3720 NamedDecl *ToD = nullptr; 3721 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3722 return nullptr; 3723 if (ToD) 3724 return ToD; 3725 3726 auto *ToUsing = dyn_cast_or_null<UsingDecl>( 3727 Importer.Import(D->getUsingDecl())); 3728 if (!ToUsing) 3729 return nullptr; 3730 3731 auto *ToTarget = dyn_cast_or_null<NamedDecl>( 3732 Importer.Import(D->getTargetDecl())); 3733 if (!ToTarget) 3734 return nullptr; 3735 3736 UsingShadowDecl *ToShadow; 3737 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc, 3738 ToUsing, ToTarget)) 3739 return ToShadow; 3740 3741 ToShadow->setLexicalDeclContext(LexicalDC); 3742 ToShadow->setAccess(D->getAccess()); 3743 3744 if (UsingShadowDecl *FromPattern = 3745 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) { 3746 if (auto *ToPattern = 3747 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern))) 3748 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow, 3749 ToPattern); 3750 else 3751 // FIXME: We return a nullptr here but the definition is already created 3752 // and available with lookups. How to fix this?.. 3753 return nullptr; 3754 } 3755 3756 LexicalDC->addDeclInternal(ToShadow); 3757 3758 return ToShadow; 3759 } 3760 3761 Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 3762 DeclContext *DC, *LexicalDC; 3763 DeclarationName Name; 3764 SourceLocation Loc; 3765 NamedDecl *ToD = nullptr; 3766 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3767 return nullptr; 3768 if (ToD) 3769 return ToD; 3770 3771 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor()); 3772 if (!ToComAncestor) 3773 return nullptr; 3774 3775 auto *ToNominated = cast_or_null<NamespaceDecl>( 3776 Importer.Import(D->getNominatedNamespace())); 3777 if (!ToNominated) 3778 return nullptr; 3779 3780 UsingDirectiveDecl *ToUsingDir; 3781 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC, 3782 Importer.Import(D->getUsingLoc()), 3783 Importer.Import(D->getNamespaceKeyLocation()), 3784 Importer.Import(D->getQualifierLoc()), 3785 Importer.Import(D->getIdentLocation()), 3786 ToNominated, ToComAncestor)) 3787 return ToUsingDir; 3788 3789 ToUsingDir->setLexicalDeclContext(LexicalDC); 3790 LexicalDC->addDeclInternal(ToUsingDir); 3791 3792 return ToUsingDir; 3793 } 3794 3795 Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl( 3796 UnresolvedUsingValueDecl *D) { 3797 DeclContext *DC, *LexicalDC; 3798 DeclarationName Name; 3799 SourceLocation Loc; 3800 NamedDecl *ToD = nullptr; 3801 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3802 return nullptr; 3803 if (ToD) 3804 return ToD; 3805 3806 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc())); 3807 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); 3808 3809 UnresolvedUsingValueDecl *ToUsingValue; 3810 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC, 3811 Importer.Import(D->getUsingLoc()), 3812 Importer.Import(D->getQualifierLoc()), NameInfo, 3813 Importer.Import(D->getEllipsisLoc()))) 3814 return ToUsingValue; 3815 3816 ToUsingValue->setAccess(D->getAccess()); 3817 ToUsingValue->setLexicalDeclContext(LexicalDC); 3818 LexicalDC->addDeclInternal(ToUsingValue); 3819 3820 return ToUsingValue; 3821 } 3822 3823 Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl( 3824 UnresolvedUsingTypenameDecl *D) { 3825 DeclContext *DC, *LexicalDC; 3826 DeclarationName Name; 3827 SourceLocation Loc; 3828 NamedDecl *ToD = nullptr; 3829 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3830 return nullptr; 3831 if (ToD) 3832 return ToD; 3833 3834 UnresolvedUsingTypenameDecl *ToUsing; 3835 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC, 3836 Importer.Import(D->getUsingLoc()), 3837 Importer.Import(D->getTypenameLoc()), 3838 Importer.Import(D->getQualifierLoc()), Loc, Name, 3839 Importer.Import(D->getEllipsisLoc()))) 3840 return ToUsing; 3841 3842 ToUsing->setAccess(D->getAccess()); 3843 ToUsing->setLexicalDeclContext(LexicalDC); 3844 LexicalDC->addDeclInternal(ToUsing); 3845 3846 return ToUsing; 3847 } 3848 3849 bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From, 3850 ObjCInterfaceDecl *To, 3851 ImportDefinitionKind Kind) { 3852 if (To->getDefinition()) { 3853 // Check consistency of superclass. 3854 ObjCInterfaceDecl *FromSuper = From->getSuperClass(); 3855 if (FromSuper) { 3856 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper)); 3857 if (!FromSuper) 3858 return true; 3859 } 3860 3861 ObjCInterfaceDecl *ToSuper = To->getSuperClass(); 3862 if ((bool)FromSuper != (bool)ToSuper || 3863 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { 3864 Importer.ToDiag(To->getLocation(), 3865 diag::err_odr_objc_superclass_inconsistent) 3866 << To->getDeclName(); 3867 if (ToSuper) 3868 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) 3869 << To->getSuperClass()->getDeclName(); 3870 else 3871 Importer.ToDiag(To->getLocation(), 3872 diag::note_odr_objc_missing_superclass); 3873 if (From->getSuperClass()) 3874 Importer.FromDiag(From->getSuperClassLoc(), 3875 diag::note_odr_objc_superclass) 3876 << From->getSuperClass()->getDeclName(); 3877 else 3878 Importer.FromDiag(From->getLocation(), 3879 diag::note_odr_objc_missing_superclass); 3880 } 3881 3882 if (shouldForceImportDeclContext(Kind)) 3883 ImportDeclContext(From); 3884 return false; 3885 } 3886 3887 // Start the definition. 3888 To->startDefinition(); 3889 3890 // If this class has a superclass, import it. 3891 if (From->getSuperClass()) { 3892 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo()); 3893 if (!SuperTInfo) 3894 return true; 3895 3896 To->setSuperClass(SuperTInfo); 3897 } 3898 3899 // Import protocols 3900 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3901 SmallVector<SourceLocation, 4> ProtocolLocs; 3902 ObjCInterfaceDecl::protocol_loc_iterator 3903 FromProtoLoc = From->protocol_loc_begin(); 3904 3905 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), 3906 FromProtoEnd = From->protocol_end(); 3907 FromProto != FromProtoEnd; 3908 ++FromProto, ++FromProtoLoc) { 3909 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3910 if (!ToProto) 3911 return true; 3912 Protocols.push_back(ToProto); 3913 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3914 } 3915 3916 // FIXME: If we're merging, make sure that the protocol list is the same. 3917 To->setProtocolList(Protocols.data(), Protocols.size(), 3918 ProtocolLocs.data(), Importer.getToContext()); 3919 3920 // Import categories. When the categories themselves are imported, they'll 3921 // hook themselves into this interface. 3922 for (auto *Cat : From->known_categories()) 3923 Importer.Import(Cat); 3924 3925 // If we have an @implementation, import it as well. 3926 if (From->getImplementation()) { 3927 auto *Impl = cast_or_null<ObjCImplementationDecl>( 3928 Importer.Import(From->getImplementation())); 3929 if (!Impl) 3930 return true; 3931 3932 To->setImplementation(Impl); 3933 } 3934 3935 if (shouldForceImportDeclContext(Kind)) { 3936 // Import all of the members of this class. 3937 ImportDeclContext(From, /*ForceImport=*/true); 3938 } 3939 return false; 3940 } 3941 3942 ObjCTypeParamList * 3943 ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { 3944 if (!list) 3945 return nullptr; 3946 3947 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams; 3948 for (auto fromTypeParam : *list) { 3949 auto *toTypeParam = cast_or_null<ObjCTypeParamDecl>( 3950 Importer.Import(fromTypeParam)); 3951 if (!toTypeParam) 3952 return nullptr; 3953 3954 toTypeParams.push_back(toTypeParam); 3955 } 3956 3957 return ObjCTypeParamList::create(Importer.getToContext(), 3958 Importer.Import(list->getLAngleLoc()), 3959 toTypeParams, 3960 Importer.Import(list->getRAngleLoc())); 3961 } 3962 3963 Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { 3964 // If this class has a definition in the translation unit we're coming from, 3965 // but this particular declaration is not that definition, import the 3966 // definition and map to that. 3967 ObjCInterfaceDecl *Definition = D->getDefinition(); 3968 if (Definition && Definition != D) { 3969 Decl *ImportedDef = Importer.Import(Definition); 3970 if (!ImportedDef) 3971 return nullptr; 3972 3973 return Importer.MapImported(D, ImportedDef); 3974 } 3975 3976 // Import the major distinguishing characteristics of an @interface. 3977 DeclContext *DC, *LexicalDC; 3978 DeclarationName Name; 3979 SourceLocation Loc; 3980 NamedDecl *ToD; 3981 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3982 return nullptr; 3983 if (ToD) 3984 return ToD; 3985 3986 // Look for an existing interface with the same name. 3987 ObjCInterfaceDecl *MergeWithIface = nullptr; 3988 SmallVector<NamedDecl *, 2> FoundDecls; 3989 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3990 for (auto *FoundDecl : FoundDecls) { 3991 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 3992 continue; 3993 3994 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl))) 3995 break; 3996 } 3997 3998 // Create an interface declaration, if one does not already exist. 3999 ObjCInterfaceDecl *ToIface = MergeWithIface; 4000 if (!ToIface) { 4001 if (GetImportedOrCreateDecl( 4002 ToIface, D, Importer.getToContext(), DC, 4003 Importer.Import(D->getAtStartLoc()), Name.getAsIdentifierInfo(), 4004 /*TypeParamList=*/nullptr, 4005 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl())) 4006 return ToIface; 4007 ToIface->setLexicalDeclContext(LexicalDC); 4008 LexicalDC->addDeclInternal(ToIface); 4009 } 4010 Importer.MapImported(D, ToIface); 4011 // Import the type parameter list after calling Imported, to avoid 4012 // loops when bringing in their DeclContext. 4013 ToIface->setTypeParamList(ImportObjCTypeParamList( 4014 D->getTypeParamListAsWritten())); 4015 4016 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface)) 4017 return nullptr; 4018 4019 return ToIface; 4020 } 4021 4022 Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { 4023 auto *Category = cast_or_null<ObjCCategoryDecl>( 4024 Importer.Import(D->getCategoryDecl())); 4025 if (!Category) 4026 return nullptr; 4027 4028 ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); 4029 if (!ToImpl) { 4030 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 4031 if (!DC) 4032 return nullptr; 4033 4034 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc()); 4035 if (GetImportedOrCreateDecl( 4036 ToImpl, D, Importer.getToContext(), DC, 4037 Importer.Import(D->getIdentifier()), Category->getClassInterface(), 4038 Importer.Import(D->getLocation()), 4039 Importer.Import(D->getAtStartLoc()), CategoryNameLoc)) 4040 return ToImpl; 4041 4042 DeclContext *LexicalDC = DC; 4043 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4044 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4045 if (!LexicalDC) 4046 return nullptr; 4047 4048 ToImpl->setLexicalDeclContext(LexicalDC); 4049 } 4050 4051 LexicalDC->addDeclInternal(ToImpl); 4052 Category->setImplementation(ToImpl); 4053 } 4054 4055 Importer.MapImported(D, ToImpl); 4056 ImportDeclContext(D); 4057 return ToImpl; 4058 } 4059 4060 Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { 4061 // Find the corresponding interface. 4062 auto *Iface = cast_or_null<ObjCInterfaceDecl>( 4063 Importer.Import(D->getClassInterface())); 4064 if (!Iface) 4065 return nullptr; 4066 4067 // Import the superclass, if any. 4068 ObjCInterfaceDecl *Super = nullptr; 4069 if (D->getSuperClass()) { 4070 Super = cast_or_null<ObjCInterfaceDecl>( 4071 Importer.Import(D->getSuperClass())); 4072 if (!Super) 4073 return nullptr; 4074 } 4075 4076 ObjCImplementationDecl *Impl = Iface->getImplementation(); 4077 if (!Impl) { 4078 // We haven't imported an implementation yet. Create a new @implementation 4079 // now. 4080 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(), 4081 Importer.ImportContext(D->getDeclContext()), 4082 Iface, Super, Importer.Import(D->getLocation()), 4083 Importer.Import(D->getAtStartLoc()), 4084 Importer.Import(D->getSuperClassLoc()), 4085 Importer.Import(D->getIvarLBraceLoc()), 4086 Importer.Import(D->getIvarRBraceLoc()))) 4087 return Impl; 4088 4089 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4090 DeclContext *LexicalDC 4091 = Importer.ImportContext(D->getLexicalDeclContext()); 4092 if (!LexicalDC) 4093 return nullptr; 4094 Impl->setLexicalDeclContext(LexicalDC); 4095 } 4096 4097 // Associate the implementation with the class it implements. 4098 Iface->setImplementation(Impl); 4099 Importer.MapImported(D, Iface->getImplementation()); 4100 } else { 4101 Importer.MapImported(D, Iface->getImplementation()); 4102 4103 // Verify that the existing @implementation has the same superclass. 4104 if ((Super && !Impl->getSuperClass()) || 4105 (!Super && Impl->getSuperClass()) || 4106 (Super && Impl->getSuperClass() && 4107 !declaresSameEntity(Super->getCanonicalDecl(), 4108 Impl->getSuperClass()))) { 4109 Importer.ToDiag(Impl->getLocation(), 4110 diag::err_odr_objc_superclass_inconsistent) 4111 << Iface->getDeclName(); 4112 // FIXME: It would be nice to have the location of the superclass 4113 // below. 4114 if (Impl->getSuperClass()) 4115 Importer.ToDiag(Impl->getLocation(), 4116 diag::note_odr_objc_superclass) 4117 << Impl->getSuperClass()->getDeclName(); 4118 else 4119 Importer.ToDiag(Impl->getLocation(), 4120 diag::note_odr_objc_missing_superclass); 4121 if (D->getSuperClass()) 4122 Importer.FromDiag(D->getLocation(), 4123 diag::note_odr_objc_superclass) 4124 << D->getSuperClass()->getDeclName(); 4125 else 4126 Importer.FromDiag(D->getLocation(), 4127 diag::note_odr_objc_missing_superclass); 4128 return nullptr; 4129 } 4130 } 4131 4132 // Import all of the members of this @implementation. 4133 ImportDeclContext(D); 4134 4135 return Impl; 4136 } 4137 4138 Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { 4139 // Import the major distinguishing characteristics of an @property. 4140 DeclContext *DC, *LexicalDC; 4141 DeclarationName Name; 4142 SourceLocation Loc; 4143 NamedDecl *ToD; 4144 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4145 return nullptr; 4146 if (ToD) 4147 return ToD; 4148 4149 // Check whether we have already imported this property. 4150 SmallVector<NamedDecl *, 2> FoundDecls; 4151 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4152 for (auto *FoundDecl : FoundDecls) { 4153 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) { 4154 // Check property types. 4155 if (!Importer.IsStructurallyEquivalent(D->getType(), 4156 FoundProp->getType())) { 4157 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent) 4158 << Name << D->getType() << FoundProp->getType(); 4159 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) 4160 << FoundProp->getType(); 4161 return nullptr; 4162 } 4163 4164 // FIXME: Check property attributes, getters, setters, etc.? 4165 4166 // Consider these properties to be equivalent. 4167 Importer.MapImported(D, FoundProp); 4168 return FoundProp; 4169 } 4170 } 4171 4172 // Import the type. 4173 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo()); 4174 if (!TSI) 4175 return nullptr; 4176 4177 // Create the new property. 4178 ObjCPropertyDecl *ToProperty; 4179 if (GetImportedOrCreateDecl( 4180 ToProperty, D, Importer.getToContext(), DC, Loc, 4181 Name.getAsIdentifierInfo(), Importer.Import(D->getAtLoc()), 4182 Importer.Import(D->getLParenLoc()), Importer.Import(D->getType()), 4183 TSI, D->getPropertyImplementation())) 4184 return ToProperty; 4185 4186 ToProperty->setLexicalDeclContext(LexicalDC); 4187 LexicalDC->addDeclInternal(ToProperty); 4188 4189 ToProperty->setPropertyAttributes(D->getPropertyAttributes()); 4190 ToProperty->setPropertyAttributesAsWritten( 4191 D->getPropertyAttributesAsWritten()); 4192 ToProperty->setGetterName(Importer.Import(D->getGetterName()), 4193 Importer.Import(D->getGetterNameLoc())); 4194 ToProperty->setSetterName(Importer.Import(D->getSetterName()), 4195 Importer.Import(D->getSetterNameLoc())); 4196 ToProperty->setGetterMethodDecl( 4197 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl()))); 4198 ToProperty->setSetterMethodDecl( 4199 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl()))); 4200 ToProperty->setPropertyIvarDecl( 4201 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl()))); 4202 return ToProperty; 4203 } 4204 4205 Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { 4206 auto *Property = cast_or_null<ObjCPropertyDecl>( 4207 Importer.Import(D->getPropertyDecl())); 4208 if (!Property) 4209 return nullptr; 4210 4211 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 4212 if (!DC) 4213 return nullptr; 4214 4215 // Import the lexical declaration context. 4216 DeclContext *LexicalDC = DC; 4217 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4218 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4219 if (!LexicalDC) 4220 return nullptr; 4221 } 4222 4223 auto *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC); 4224 if (!InImpl) 4225 return nullptr; 4226 4227 // Import the ivar (for an @synthesize). 4228 ObjCIvarDecl *Ivar = nullptr; 4229 if (D->getPropertyIvarDecl()) { 4230 Ivar = cast_or_null<ObjCIvarDecl>( 4231 Importer.Import(D->getPropertyIvarDecl())); 4232 if (!Ivar) 4233 return nullptr; 4234 } 4235 4236 ObjCPropertyImplDecl *ToImpl 4237 = InImpl->FindPropertyImplDecl(Property->getIdentifier(), 4238 Property->getQueryKind()); 4239 if (!ToImpl) { 4240 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC, 4241 Importer.Import(D->getLocStart()), 4242 Importer.Import(D->getLocation()), Property, 4243 D->getPropertyImplementation(), Ivar, 4244 Importer.Import(D->getPropertyIvarDeclLoc()))) 4245 return ToImpl; 4246 4247 ToImpl->setLexicalDeclContext(LexicalDC); 4248 LexicalDC->addDeclInternal(ToImpl); 4249 } else { 4250 // Check that we have the same kind of property implementation (@synthesize 4251 // vs. @dynamic). 4252 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { 4253 Importer.ToDiag(ToImpl->getLocation(), 4254 diag::err_odr_objc_property_impl_kind_inconsistent) 4255 << Property->getDeclName() 4256 << (ToImpl->getPropertyImplementation() 4257 == ObjCPropertyImplDecl::Dynamic); 4258 Importer.FromDiag(D->getLocation(), 4259 diag::note_odr_objc_property_impl_kind) 4260 << D->getPropertyDecl()->getDeclName() 4261 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); 4262 return nullptr; 4263 } 4264 4265 // For @synthesize, check that we have the same 4266 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && 4267 Ivar != ToImpl->getPropertyIvarDecl()) { 4268 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), 4269 diag::err_odr_objc_synthesize_ivar_inconsistent) 4270 << Property->getDeclName() 4271 << ToImpl->getPropertyIvarDecl()->getDeclName() 4272 << Ivar->getDeclName(); 4273 Importer.FromDiag(D->getPropertyIvarDeclLoc(), 4274 diag::note_odr_objc_synthesize_ivar_here) 4275 << D->getPropertyIvarDecl()->getDeclName(); 4276 return nullptr; 4277 } 4278 4279 // Merge the existing implementation with the new implementation. 4280 Importer.MapImported(D, ToImpl); 4281 } 4282 4283 return ToImpl; 4284 } 4285 4286 Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { 4287 // For template arguments, we adopt the translation unit as our declaration 4288 // context. This context will be fixed when the actual template declaration 4289 // is created. 4290 4291 // FIXME: Import default argument. 4292 TemplateTypeParmDecl *ToD = nullptr; 4293 (void)GetImportedOrCreateDecl( 4294 ToD, D, Importer.getToContext(), 4295 Importer.getToContext().getTranslationUnitDecl(), 4296 Importer.Import(D->getLocStart()), Importer.Import(D->getLocation()), 4297 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()), 4298 D->wasDeclaredWithTypename(), D->isParameterPack()); 4299 return ToD; 4300 } 4301 4302 Decl * 4303 ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { 4304 // Import the name of this declaration. 4305 DeclarationName Name = Importer.Import(D->getDeclName()); 4306 if (D->getDeclName() && !Name) 4307 return nullptr; 4308 4309 // Import the location of this declaration. 4310 SourceLocation Loc = Importer.Import(D->getLocation()); 4311 4312 // Import the type of this declaration. 4313 QualType T = Importer.Import(D->getType()); 4314 if (T.isNull()) 4315 return nullptr; 4316 4317 // Import type-source information. 4318 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 4319 if (D->getTypeSourceInfo() && !TInfo) 4320 return nullptr; 4321 4322 // FIXME: Import default argument. 4323 4324 NonTypeTemplateParmDecl *ToD = nullptr; 4325 (void)GetImportedOrCreateDecl( 4326 ToD, D, Importer.getToContext(), 4327 Importer.getToContext().getTranslationUnitDecl(), 4328 Importer.Import(D->getInnerLocStart()), Loc, D->getDepth(), 4329 D->getPosition(), Name.getAsIdentifierInfo(), T, D->isParameterPack(), 4330 TInfo); 4331 return ToD; 4332 } 4333 4334 Decl * 4335 ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { 4336 // Import the name of this declaration. 4337 DeclarationName Name = Importer.Import(D->getDeclName()); 4338 if (D->getDeclName() && !Name) 4339 return nullptr; 4340 4341 // Import the location of this declaration. 4342 SourceLocation Loc = Importer.Import(D->getLocation()); 4343 4344 // Import template parameters. 4345 TemplateParameterList *TemplateParams 4346 = ImportTemplateParameterList(D->getTemplateParameters()); 4347 if (!TemplateParams) 4348 return nullptr; 4349 4350 // FIXME: Import default argument. 4351 4352 TemplateTemplateParmDecl *ToD = nullptr; 4353 (void)GetImportedOrCreateDecl( 4354 ToD, D, Importer.getToContext(), 4355 Importer.getToContext().getTranslationUnitDecl(), Loc, D->getDepth(), 4356 D->getPosition(), D->isParameterPack(), Name.getAsIdentifierInfo(), 4357 TemplateParams); 4358 return ToD; 4359 } 4360 4361 // Returns the definition for a (forward) declaration of a ClassTemplateDecl, if 4362 // it has any definition in the redecl chain. 4363 static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) { 4364 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition(); 4365 if (!ToTemplatedDef) 4366 return nullptr; 4367 ClassTemplateDecl *TemplateWithDef = 4368 ToTemplatedDef->getDescribedClassTemplate(); 4369 return TemplateWithDef; 4370 } 4371 4372 Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 4373 // If this record has a definition in the translation unit we're coming from, 4374 // but this particular declaration is not that definition, import the 4375 // definition and map to that. 4376 auto *Definition = 4377 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition()); 4378 if (Definition && Definition != D->getTemplatedDecl()) { 4379 Decl *ImportedDef 4380 = Importer.Import(Definition->getDescribedClassTemplate()); 4381 if (!ImportedDef) 4382 return nullptr; 4383 4384 return Importer.MapImported(D, ImportedDef); 4385 } 4386 4387 // Import the major distinguishing characteristics of this class template. 4388 DeclContext *DC, *LexicalDC; 4389 DeclarationName Name; 4390 SourceLocation Loc; 4391 NamedDecl *ToD; 4392 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4393 return nullptr; 4394 if (ToD) 4395 return ToD; 4396 4397 // We may already have a template of the same name; try to find and match it. 4398 if (!DC->isFunctionOrMethod()) { 4399 SmallVector<NamedDecl *, 4> ConflictingDecls; 4400 SmallVector<NamedDecl *, 2> FoundDecls; 4401 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4402 for (auto *FoundDecl : FoundDecls) { 4403 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 4404 continue; 4405 4406 Decl *Found = FoundDecl; 4407 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) { 4408 4409 // The class to be imported is a definition. 4410 if (D->isThisDeclarationADefinition()) { 4411 // Lookup will find the fwd decl only if that is more recent than the 4412 // definition. So, try to get the definition if that is available in 4413 // the redecl chain. 4414 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate); 4415 if (!TemplateWithDef) 4416 continue; 4417 FoundTemplate = TemplateWithDef; // Continue with the definition. 4418 } 4419 4420 if (IsStructuralMatch(D, FoundTemplate)) { 4421 // The class templates structurally match; call it the same template. 4422 4423 Importer.MapImported(D->getTemplatedDecl(), 4424 FoundTemplate->getTemplatedDecl()); 4425 return Importer.MapImported(D, FoundTemplate); 4426 } 4427 } 4428 4429 ConflictingDecls.push_back(FoundDecl); 4430 } 4431 4432 if (!ConflictingDecls.empty()) { 4433 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, 4434 ConflictingDecls.data(), 4435 ConflictingDecls.size()); 4436 } 4437 4438 if (!Name) 4439 return nullptr; 4440 } 4441 4442 CXXRecordDecl *FromTemplated = D->getTemplatedDecl(); 4443 4444 // Create the declaration that is being templated. 4445 auto *ToTemplated = cast_or_null<CXXRecordDecl>( 4446 Importer.Import(FromTemplated)); 4447 if (!ToTemplated) 4448 return nullptr; 4449 4450 // Create the class template declaration itself. 4451 TemplateParameterList *TemplateParams = 4452 ImportTemplateParameterList(D->getTemplateParameters()); 4453 if (!TemplateParams) 4454 return nullptr; 4455 4456 ClassTemplateDecl *D2; 4457 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name, 4458 TemplateParams, ToTemplated)) 4459 return D2; 4460 4461 ToTemplated->setDescribedClassTemplate(D2); 4462 4463 D2->setAccess(D->getAccess()); 4464 D2->setLexicalDeclContext(LexicalDC); 4465 LexicalDC->addDeclInternal(D2); 4466 4467 if (FromTemplated->isCompleteDefinition() && 4468 !ToTemplated->isCompleteDefinition()) { 4469 // FIXME: Import definition! 4470 } 4471 4472 return D2; 4473 } 4474 4475 Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl( 4476 ClassTemplateSpecializationDecl *D) { 4477 // If this record has a definition in the translation unit we're coming from, 4478 // but this particular declaration is not that definition, import the 4479 // definition and map to that. 4480 TagDecl *Definition = D->getDefinition(); 4481 if (Definition && Definition != D) { 4482 Decl *ImportedDef = Importer.Import(Definition); 4483 if (!ImportedDef) 4484 return nullptr; 4485 4486 return Importer.MapImported(D, ImportedDef); 4487 } 4488 4489 auto *ClassTemplate = 4490 cast_or_null<ClassTemplateDecl>(Importer.Import( 4491 D->getSpecializedTemplate())); 4492 if (!ClassTemplate) 4493 return nullptr; 4494 4495 // Import the context of this declaration. 4496 DeclContext *DC = ClassTemplate->getDeclContext(); 4497 if (!DC) 4498 return nullptr; 4499 4500 DeclContext *LexicalDC = DC; 4501 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4502 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4503 if (!LexicalDC) 4504 return nullptr; 4505 } 4506 4507 // Import the location of this declaration. 4508 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 4509 SourceLocation IdLoc = Importer.Import(D->getLocation()); 4510 4511 // Import template arguments. 4512 SmallVector<TemplateArgument, 2> TemplateArgs; 4513 if (ImportTemplateArguments(D->getTemplateArgs().data(), 4514 D->getTemplateArgs().size(), 4515 TemplateArgs)) 4516 return nullptr; 4517 4518 // Try to find an existing specialization with these template arguments. 4519 void *InsertPos = nullptr; 4520 ClassTemplateSpecializationDecl *D2 4521 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos); 4522 if (D2) { 4523 // We already have a class template specialization with these template 4524 // arguments. 4525 4526 // FIXME: Check for specialization vs. instantiation errors. 4527 4528 if (RecordDecl *FoundDef = D2->getDefinition()) { 4529 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) { 4530 // The record types structurally match, or the "from" translation 4531 // unit only had a forward declaration anyway; call it the same 4532 // function. 4533 return Importer.MapImported(D, FoundDef); 4534 } 4535 } 4536 } else { 4537 // Create a new specialization. 4538 if (auto *PartialSpec = 4539 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) { 4540 // Import TemplateArgumentListInfo 4541 TemplateArgumentListInfo ToTAInfo; 4542 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten(); 4543 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo)) 4544 return nullptr; 4545 4546 QualType CanonInjType = Importer.Import( 4547 PartialSpec->getInjectedSpecializationType()); 4548 if (CanonInjType.isNull()) 4549 return nullptr; 4550 CanonInjType = CanonInjType.getCanonicalType(); 4551 4552 TemplateParameterList *ToTPList = ImportTemplateParameterList( 4553 PartialSpec->getTemplateParameters()); 4554 if (!ToTPList && PartialSpec->getTemplateParameters()) 4555 return nullptr; 4556 4557 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>( 4558 D2, D, Importer.getToContext(), D->getTagKind(), DC, StartLoc, 4559 IdLoc, ToTPList, ClassTemplate, 4560 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()), 4561 ToTAInfo, CanonInjType, nullptr)) 4562 return D2; 4563 4564 } else { 4565 if (GetImportedOrCreateDecl( 4566 D2, D, Importer.getToContext(), D->getTagKind(), DC, StartLoc, 4567 IdLoc, ClassTemplate, TemplateArgs, /*PrevDecl=*/nullptr)) 4568 return D2; 4569 } 4570 4571 D2->setSpecializationKind(D->getSpecializationKind()); 4572 4573 // Add this specialization to the class template. 4574 ClassTemplate->AddSpecialization(D2, InsertPos); 4575 4576 // Import the qualifier, if any. 4577 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 4578 4579 if (auto *TSI = D->getTypeAsWritten()) { 4580 TypeSourceInfo *TInfo = Importer.Import(TSI); 4581 if (!TInfo) 4582 return nullptr; 4583 D2->setTypeAsWritten(TInfo); 4584 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc())); 4585 D2->setExternLoc(Importer.Import(D->getExternLoc())); 4586 } 4587 4588 SourceLocation POI = Importer.Import(D->getPointOfInstantiation()); 4589 if (POI.isValid()) 4590 D2->setPointOfInstantiation(POI); 4591 else if (D->getPointOfInstantiation().isValid()) 4592 return nullptr; 4593 4594 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind()); 4595 4596 // Set the context of this specialization/instantiation. 4597 D2->setLexicalDeclContext(LexicalDC); 4598 4599 // Add to the DC only if it was an explicit specialization/instantiation. 4600 if (D2->isExplicitInstantiationOrSpecialization()) { 4601 LexicalDC->addDeclInternal(D2); 4602 } 4603 } 4604 if (D->isCompleteDefinition() && ImportDefinition(D, D2)) 4605 return nullptr; 4606 4607 return D2; 4608 } 4609 4610 Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { 4611 // If this variable has a definition in the translation unit we're coming 4612 // from, 4613 // but this particular declaration is not that definition, import the 4614 // definition and map to that. 4615 auto *Definition = 4616 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition()); 4617 if (Definition && Definition != D->getTemplatedDecl()) { 4618 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate()); 4619 if (!ImportedDef) 4620 return nullptr; 4621 4622 return Importer.MapImported(D, ImportedDef); 4623 } 4624 4625 // Import the major distinguishing characteristics of this variable template. 4626 DeclContext *DC, *LexicalDC; 4627 DeclarationName Name; 4628 SourceLocation Loc; 4629 NamedDecl *ToD; 4630 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4631 return nullptr; 4632 if (ToD) 4633 return ToD; 4634 4635 // We may already have a template of the same name; try to find and match it. 4636 assert(!DC->isFunctionOrMethod() && 4637 "Variable templates cannot be declared at function scope"); 4638 SmallVector<NamedDecl *, 4> ConflictingDecls; 4639 SmallVector<NamedDecl *, 2> FoundDecls; 4640 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4641 for (auto *FoundDecl : FoundDecls) { 4642 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 4643 continue; 4644 4645 Decl *Found = FoundDecl; 4646 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) { 4647 if (IsStructuralMatch(D, FoundTemplate)) { 4648 // The variable templates structurally match; call it the same template. 4649 Importer.MapImported(D->getTemplatedDecl(), 4650 FoundTemplate->getTemplatedDecl()); 4651 return Importer.MapImported(D, FoundTemplate); 4652 } 4653 } 4654 4655 ConflictingDecls.push_back(FoundDecl); 4656 } 4657 4658 if (!ConflictingDecls.empty()) { 4659 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, 4660 ConflictingDecls.data(), 4661 ConflictingDecls.size()); 4662 } 4663 4664 if (!Name) 4665 return nullptr; 4666 4667 VarDecl *DTemplated = D->getTemplatedDecl(); 4668 4669 // Import the type. 4670 QualType T = Importer.Import(DTemplated->getType()); 4671 if (T.isNull()) 4672 return nullptr; 4673 4674 // Create the declaration that is being templated. 4675 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated)); 4676 if (!ToTemplated) 4677 return nullptr; 4678 4679 // Create the variable template declaration itself. 4680 TemplateParameterList *TemplateParams = 4681 ImportTemplateParameterList(D->getTemplateParameters()); 4682 if (!TemplateParams) 4683 return nullptr; 4684 4685 VarTemplateDecl *ToVarTD; 4686 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc, 4687 Name, TemplateParams, ToTemplated)) 4688 return ToVarTD; 4689 4690 ToTemplated->setDescribedVarTemplate(ToVarTD); 4691 4692 ToVarTD->setAccess(D->getAccess()); 4693 ToVarTD->setLexicalDeclContext(LexicalDC); 4694 LexicalDC->addDeclInternal(ToVarTD); 4695 4696 if (DTemplated->isThisDeclarationADefinition() && 4697 !ToTemplated->isThisDeclarationADefinition()) { 4698 // FIXME: Import definition! 4699 } 4700 4701 return ToVarTD; 4702 } 4703 4704 Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl( 4705 VarTemplateSpecializationDecl *D) { 4706 // If this record has a definition in the translation unit we're coming from, 4707 // but this particular declaration is not that definition, import the 4708 // definition and map to that. 4709 VarDecl *Definition = D->getDefinition(); 4710 if (Definition && Definition != D) { 4711 Decl *ImportedDef = Importer.Import(Definition); 4712 if (!ImportedDef) 4713 return nullptr; 4714 4715 return Importer.MapImported(D, ImportedDef); 4716 } 4717 4718 auto *VarTemplate = cast_or_null<VarTemplateDecl>( 4719 Importer.Import(D->getSpecializedTemplate())); 4720 if (!VarTemplate) 4721 return nullptr; 4722 4723 // Import the context of this declaration. 4724 DeclContext *DC = VarTemplate->getDeclContext(); 4725 if (!DC) 4726 return nullptr; 4727 4728 DeclContext *LexicalDC = DC; 4729 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4730 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4731 if (!LexicalDC) 4732 return nullptr; 4733 } 4734 4735 // Import the location of this declaration. 4736 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 4737 SourceLocation IdLoc = Importer.Import(D->getLocation()); 4738 4739 // Import template arguments. 4740 SmallVector<TemplateArgument, 2> TemplateArgs; 4741 if (ImportTemplateArguments(D->getTemplateArgs().data(), 4742 D->getTemplateArgs().size(), TemplateArgs)) 4743 return nullptr; 4744 4745 // Try to find an existing specialization with these template arguments. 4746 void *InsertPos = nullptr; 4747 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization( 4748 TemplateArgs, InsertPos); 4749 if (D2) { 4750 // We already have a variable template specialization with these template 4751 // arguments. 4752 4753 // FIXME: Check for specialization vs. instantiation errors. 4754 4755 if (VarDecl *FoundDef = D2->getDefinition()) { 4756 if (!D->isThisDeclarationADefinition() || 4757 IsStructuralMatch(D, FoundDef)) { 4758 // The record types structurally match, or the "from" translation 4759 // unit only had a forward declaration anyway; call it the same 4760 // variable. 4761 return Importer.MapImported(D, FoundDef); 4762 } 4763 } 4764 } else { 4765 // Import the type. 4766 QualType T = Importer.Import(D->getType()); 4767 if (T.isNull()) 4768 return nullptr; 4769 4770 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 4771 if (D->getTypeSourceInfo() && !TInfo) 4772 return nullptr; 4773 4774 TemplateArgumentListInfo ToTAInfo; 4775 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo)) 4776 return nullptr; 4777 4778 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl; 4779 // Create a new specialization. 4780 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) { 4781 // Import TemplateArgumentListInfo 4782 TemplateArgumentListInfo ArgInfos; 4783 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten(); 4784 // NOTE: FromTAArgsAsWritten and template parameter list are non-null. 4785 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos)) 4786 return nullptr; 4787 4788 TemplateParameterList *ToTPList = ImportTemplateParameterList( 4789 FromPartial->getTemplateParameters()); 4790 if (!ToTPList) 4791 return nullptr; 4792 4793 PartVarSpecDecl *ToPartial; 4794 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC, 4795 StartLoc, IdLoc, ToTPList, VarTemplate, T, 4796 TInfo, D->getStorageClass(), TemplateArgs, 4797 ArgInfos)) 4798 return ToPartial; 4799 4800 auto *FromInst = FromPartial->getInstantiatedFromMember(); 4801 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst)); 4802 if (FromInst && !ToInst) 4803 return nullptr; 4804 4805 ToPartial->setInstantiatedFromMember(ToInst); 4806 if (FromPartial->isMemberSpecialization()) 4807 ToPartial->setMemberSpecialization(); 4808 4809 D2 = ToPartial; 4810 } else { // Full specialization 4811 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, StartLoc, 4812 IdLoc, VarTemplate, T, TInfo, 4813 D->getStorageClass(), TemplateArgs)) 4814 return D2; 4815 } 4816 4817 SourceLocation POI = D->getPointOfInstantiation(); 4818 if (POI.isValid()) 4819 D2->setPointOfInstantiation(Importer.Import(POI)); 4820 4821 D2->setSpecializationKind(D->getSpecializationKind()); 4822 D2->setTemplateArgsInfo(ToTAInfo); 4823 4824 // Add this specialization to the class template. 4825 VarTemplate->AddSpecialization(D2, InsertPos); 4826 4827 // Import the qualifier, if any. 4828 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 4829 4830 if (D->isConstexpr()) 4831 D2->setConstexpr(true); 4832 4833 // Add the specialization to this context. 4834 D2->setLexicalDeclContext(LexicalDC); 4835 LexicalDC->addDeclInternal(D2); 4836 4837 D2->setAccess(D->getAccess()); 4838 } 4839 4840 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if 4841 // declaration has initializer. Should this be fixed in the AST?.. Anyway, 4842 // we have to check the declaration for initializer - otherwise, it won't be 4843 // imported. 4844 if ((D->isThisDeclarationADefinition() || D->hasInit()) && 4845 ImportDefinition(D, D2)) 4846 return nullptr; 4847 4848 return D2; 4849 } 4850 4851 Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 4852 DeclContext *DC, *LexicalDC; 4853 DeclarationName Name; 4854 SourceLocation Loc; 4855 NamedDecl *ToD; 4856 4857 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4858 return nullptr; 4859 4860 if (ToD) 4861 return ToD; 4862 4863 // Try to find a function in our own ("to") context with the same name, same 4864 // type, and in the same context as the function we're importing. 4865 if (!LexicalDC->isFunctionOrMethod()) { 4866 unsigned IDNS = Decl::IDNS_Ordinary; 4867 SmallVector<NamedDecl *, 2> FoundDecls; 4868 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4869 for (auto *FoundDecl : FoundDecls) { 4870 if (!FoundDecl->isInIdentifierNamespace(IDNS)) 4871 continue; 4872 4873 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) { 4874 if (FoundFunction->hasExternalFormalLinkage() && 4875 D->hasExternalFormalLinkage()) { 4876 if (IsStructuralMatch(D, FoundFunction)) { 4877 Importer.MapImported(D, FoundFunction); 4878 // FIXME: Actually try to merge the body and other attributes. 4879 return FoundFunction; 4880 } 4881 } 4882 } 4883 } 4884 } 4885 4886 TemplateParameterList *Params = 4887 ImportTemplateParameterList(D->getTemplateParameters()); 4888 if (!Params) 4889 return nullptr; 4890 4891 auto *TemplatedFD = 4892 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl())); 4893 if (!TemplatedFD) 4894 return nullptr; 4895 4896 FunctionTemplateDecl *ToFunc; 4897 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name, 4898 Params, TemplatedFD)) 4899 return ToFunc; 4900 4901 TemplatedFD->setDescribedFunctionTemplate(ToFunc); 4902 ToFunc->setAccess(D->getAccess()); 4903 ToFunc->setLexicalDeclContext(LexicalDC); 4904 4905 LexicalDC->addDeclInternal(ToFunc); 4906 return ToFunc; 4907 } 4908 4909 //---------------------------------------------------------------------------- 4910 // Import Statements 4911 //---------------------------------------------------------------------------- 4912 4913 DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) { 4914 if (DG.isNull()) 4915 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0); 4916 size_t NumDecls = DG.end() - DG.begin(); 4917 SmallVector<Decl *, 1> ToDecls(NumDecls); 4918 auto &_Importer = this->Importer; 4919 std::transform(DG.begin(), DG.end(), ToDecls.begin(), 4920 [&_Importer](Decl *D) -> Decl * { 4921 return _Importer.Import(D); 4922 }); 4923 return DeclGroupRef::Create(Importer.getToContext(), 4924 ToDecls.begin(), 4925 NumDecls); 4926 } 4927 4928 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) { 4929 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node) 4930 << S->getStmtClassName(); 4931 return nullptr; 4932 } 4933 4934 Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) { 4935 SmallVector<IdentifierInfo *, 4> Names; 4936 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { 4937 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I)); 4938 // ToII is nullptr when no symbolic name is given for output operand 4939 // see ParseStmtAsm::ParseAsmOperandsOpt 4940 if (!ToII && S->getOutputIdentifier(I)) 4941 return nullptr; 4942 Names.push_back(ToII); 4943 } 4944 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { 4945 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I)); 4946 // ToII is nullptr when no symbolic name is given for input operand 4947 // see ParseStmtAsm::ParseAsmOperandsOpt 4948 if (!ToII && S->getInputIdentifier(I)) 4949 return nullptr; 4950 Names.push_back(ToII); 4951 } 4952 4953 SmallVector<StringLiteral *, 4> Clobbers; 4954 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) { 4955 auto *Clobber = cast_or_null<StringLiteral>( 4956 Importer.Import(S->getClobberStringLiteral(I))); 4957 if (!Clobber) 4958 return nullptr; 4959 Clobbers.push_back(Clobber); 4960 } 4961 4962 SmallVector<StringLiteral *, 4> Constraints; 4963 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { 4964 auto *Output = cast_or_null<StringLiteral>( 4965 Importer.Import(S->getOutputConstraintLiteral(I))); 4966 if (!Output) 4967 return nullptr; 4968 Constraints.push_back(Output); 4969 } 4970 4971 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { 4972 auto *Input = cast_or_null<StringLiteral>( 4973 Importer.Import(S->getInputConstraintLiteral(I))); 4974 if (!Input) 4975 return nullptr; 4976 Constraints.push_back(Input); 4977 } 4978 4979 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs()); 4980 if (ImportContainerChecked(S->outputs(), Exprs)) 4981 return nullptr; 4982 4983 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs())) 4984 return nullptr; 4985 4986 auto *AsmStr = cast_or_null<StringLiteral>( 4987 Importer.Import(S->getAsmString())); 4988 if (!AsmStr) 4989 return nullptr; 4990 4991 return new (Importer.getToContext()) GCCAsmStmt( 4992 Importer.getToContext(), 4993 Importer.Import(S->getAsmLoc()), 4994 S->isSimple(), 4995 S->isVolatile(), 4996 S->getNumOutputs(), 4997 S->getNumInputs(), 4998 Names.data(), 4999 Constraints.data(), 5000 Exprs.data(), 5001 AsmStr, 5002 S->getNumClobbers(), 5003 Clobbers.data(), 5004 Importer.Import(S->getRParenLoc())); 5005 } 5006 5007 Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) { 5008 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup()); 5009 for (auto *ToD : ToDG) { 5010 if (!ToD) 5011 return nullptr; 5012 } 5013 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc()); 5014 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc()); 5015 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc); 5016 } 5017 5018 Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) { 5019 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc()); 5020 return new (Importer.getToContext()) NullStmt(ToSemiLoc, 5021 S->hasLeadingEmptyMacro()); 5022 } 5023 5024 Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) { 5025 SmallVector<Stmt *, 8> ToStmts(S->size()); 5026 5027 if (ImportContainerChecked(S->body(), ToStmts)) 5028 return nullptr; 5029 5030 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc()); 5031 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc()); 5032 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc, 5033 ToRBraceLoc); 5034 } 5035 5036 Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) { 5037 Expr *ToLHS = Importer.Import(S->getLHS()); 5038 if (!ToLHS) 5039 return nullptr; 5040 Expr *ToRHS = Importer.Import(S->getRHS()); 5041 if (!ToRHS && S->getRHS()) 5042 return nullptr; 5043 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 5044 if (!ToSubStmt && S->getSubStmt()) 5045 return nullptr; 5046 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc()); 5047 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc()); 5048 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 5049 auto *ToStmt = new (Importer.getToContext()) 5050 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc); 5051 ToStmt->setSubStmt(ToSubStmt); 5052 return ToStmt; 5053 } 5054 5055 Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) { 5056 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc()); 5057 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 5058 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 5059 if (!ToSubStmt && S->getSubStmt()) 5060 return nullptr; 5061 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc, 5062 ToSubStmt); 5063 } 5064 5065 Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) { 5066 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc()); 5067 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl())); 5068 if (!ToLabelDecl && S->getDecl()) 5069 return nullptr; 5070 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 5071 if (!ToSubStmt && S->getSubStmt()) 5072 return nullptr; 5073 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl, 5074 ToSubStmt); 5075 } 5076 5077 Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) { 5078 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc()); 5079 ArrayRef<const Attr*> FromAttrs(S->getAttrs()); 5080 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size()); 5081 if (ImportContainerChecked(FromAttrs, ToAttrs)) 5082 return nullptr; 5083 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 5084 if (!ToSubStmt && S->getSubStmt()) 5085 return nullptr; 5086 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc, 5087 ToAttrs, ToSubStmt); 5088 } 5089 5090 Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) { 5091 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc()); 5092 Stmt *ToInit = Importer.Import(S->getInit()); 5093 if (!ToInit && S->getInit()) 5094 return nullptr; 5095 VarDecl *ToConditionVariable = nullptr; 5096 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 5097 ToConditionVariable = 5098 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 5099 if (!ToConditionVariable) 5100 return nullptr; 5101 } 5102 Expr *ToCondition = Importer.Import(S->getCond()); 5103 if (!ToCondition && S->getCond()) 5104 return nullptr; 5105 Stmt *ToThenStmt = Importer.Import(S->getThen()); 5106 if (!ToThenStmt && S->getThen()) 5107 return nullptr; 5108 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc()); 5109 Stmt *ToElseStmt = Importer.Import(S->getElse()); 5110 if (!ToElseStmt && S->getElse()) 5111 return nullptr; 5112 return new (Importer.getToContext()) IfStmt(Importer.getToContext(), 5113 ToIfLoc, S->isConstexpr(), 5114 ToInit, 5115 ToConditionVariable, 5116 ToCondition, ToThenStmt, 5117 ToElseLoc, ToElseStmt); 5118 } 5119 5120 Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) { 5121 Stmt *ToInit = Importer.Import(S->getInit()); 5122 if (!ToInit && S->getInit()) 5123 return nullptr; 5124 VarDecl *ToConditionVariable = nullptr; 5125 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 5126 ToConditionVariable = 5127 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 5128 if (!ToConditionVariable) 5129 return nullptr; 5130 } 5131 Expr *ToCondition = Importer.Import(S->getCond()); 5132 if (!ToCondition && S->getCond()) 5133 return nullptr; 5134 auto *ToStmt = new (Importer.getToContext()) SwitchStmt( 5135 Importer.getToContext(), ToInit, 5136 ToConditionVariable, ToCondition); 5137 Stmt *ToBody = Importer.Import(S->getBody()); 5138 if (!ToBody && S->getBody()) 5139 return nullptr; 5140 ToStmt->setBody(ToBody); 5141 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc())); 5142 // Now we have to re-chain the cases. 5143 SwitchCase *LastChainedSwitchCase = nullptr; 5144 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr; 5145 SC = SC->getNextSwitchCase()) { 5146 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC)); 5147 if (!ToSC) 5148 return nullptr; 5149 if (LastChainedSwitchCase) 5150 LastChainedSwitchCase->setNextSwitchCase(ToSC); 5151 else 5152 ToStmt->setSwitchCaseList(ToSC); 5153 LastChainedSwitchCase = ToSC; 5154 } 5155 return ToStmt; 5156 } 5157 5158 Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) { 5159 VarDecl *ToConditionVariable = nullptr; 5160 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 5161 ToConditionVariable = 5162 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 5163 if (!ToConditionVariable) 5164 return nullptr; 5165 } 5166 Expr *ToCondition = Importer.Import(S->getCond()); 5167 if (!ToCondition && S->getCond()) 5168 return nullptr; 5169 Stmt *ToBody = Importer.Import(S->getBody()); 5170 if (!ToBody && S->getBody()) 5171 return nullptr; 5172 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); 5173 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(), 5174 ToConditionVariable, 5175 ToCondition, ToBody, 5176 ToWhileLoc); 5177 } 5178 5179 Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) { 5180 Stmt *ToBody = Importer.Import(S->getBody()); 5181 if (!ToBody && S->getBody()) 5182 return nullptr; 5183 Expr *ToCondition = Importer.Import(S->getCond()); 5184 if (!ToCondition && S->getCond()) 5185 return nullptr; 5186 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc()); 5187 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); 5188 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5189 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition, 5190 ToDoLoc, ToWhileLoc, 5191 ToRParenLoc); 5192 } 5193 5194 Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) { 5195 Stmt *ToInit = Importer.Import(S->getInit()); 5196 if (!ToInit && S->getInit()) 5197 return nullptr; 5198 Expr *ToCondition = Importer.Import(S->getCond()); 5199 if (!ToCondition && S->getCond()) 5200 return nullptr; 5201 VarDecl *ToConditionVariable = nullptr; 5202 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 5203 ToConditionVariable = 5204 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 5205 if (!ToConditionVariable) 5206 return nullptr; 5207 } 5208 Expr *ToInc = Importer.Import(S->getInc()); 5209 if (!ToInc && S->getInc()) 5210 return nullptr; 5211 Stmt *ToBody = Importer.Import(S->getBody()); 5212 if (!ToBody && S->getBody()) 5213 return nullptr; 5214 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 5215 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc()); 5216 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5217 return new (Importer.getToContext()) ForStmt(Importer.getToContext(), 5218 ToInit, ToCondition, 5219 ToConditionVariable, 5220 ToInc, ToBody, 5221 ToForLoc, ToLParenLoc, 5222 ToRParenLoc); 5223 } 5224 5225 Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) { 5226 LabelDecl *ToLabel = nullptr; 5227 if (LabelDecl *FromLabel = S->getLabel()) { 5228 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel)); 5229 if (!ToLabel) 5230 return nullptr; 5231 } 5232 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); 5233 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc()); 5234 return new (Importer.getToContext()) GotoStmt(ToLabel, 5235 ToGotoLoc, ToLabelLoc); 5236 } 5237 5238 Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { 5239 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); 5240 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc()); 5241 Expr *ToTarget = Importer.Import(S->getTarget()); 5242 if (!ToTarget && S->getTarget()) 5243 return nullptr; 5244 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc, 5245 ToTarget); 5246 } 5247 5248 Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) { 5249 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc()); 5250 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc); 5251 } 5252 5253 Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) { 5254 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc()); 5255 return new (Importer.getToContext()) BreakStmt(ToBreakLoc); 5256 } 5257 5258 Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) { 5259 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc()); 5260 Expr *ToRetExpr = Importer.Import(S->getRetValue()); 5261 if (!ToRetExpr && S->getRetValue()) 5262 return nullptr; 5263 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate()); 5264 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate)); 5265 if (!ToNRVOCandidate && NRVOCandidate) 5266 return nullptr; 5267 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr, 5268 ToNRVOCandidate); 5269 } 5270 5271 Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) { 5272 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc()); 5273 VarDecl *ToExceptionDecl = nullptr; 5274 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) { 5275 ToExceptionDecl = 5276 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); 5277 if (!ToExceptionDecl) 5278 return nullptr; 5279 } 5280 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock()); 5281 if (!ToHandlerBlock && S->getHandlerBlock()) 5282 return nullptr; 5283 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc, 5284 ToExceptionDecl, 5285 ToHandlerBlock); 5286 } 5287 5288 Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) { 5289 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc()); 5290 Stmt *ToTryBlock = Importer.Import(S->getTryBlock()); 5291 if (!ToTryBlock && S->getTryBlock()) 5292 return nullptr; 5293 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers()); 5294 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) { 5295 CXXCatchStmt *FromHandler = S->getHandler(HI); 5296 if (Stmt *ToHandler = Importer.Import(FromHandler)) 5297 ToHandlers[HI] = ToHandler; 5298 else 5299 return nullptr; 5300 } 5301 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock, 5302 ToHandlers); 5303 } 5304 5305 Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { 5306 auto *ToRange = 5307 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt())); 5308 if (!ToRange && S->getRangeStmt()) 5309 return nullptr; 5310 auto *ToBegin = 5311 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt())); 5312 if (!ToBegin && S->getBeginStmt()) 5313 return nullptr; 5314 auto *ToEnd = 5315 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt())); 5316 if (!ToEnd && S->getEndStmt()) 5317 return nullptr; 5318 Expr *ToCond = Importer.Import(S->getCond()); 5319 if (!ToCond && S->getCond()) 5320 return nullptr; 5321 Expr *ToInc = Importer.Import(S->getInc()); 5322 if (!ToInc && S->getInc()) 5323 return nullptr; 5324 auto *ToLoopVar = 5325 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt())); 5326 if (!ToLoopVar && S->getLoopVarStmt()) 5327 return nullptr; 5328 Stmt *ToBody = Importer.Import(S->getBody()); 5329 if (!ToBody && S->getBody()) 5330 return nullptr; 5331 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 5332 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc()); 5333 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 5334 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5335 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd, 5336 ToCond, ToInc, 5337 ToLoopVar, ToBody, 5338 ToForLoc, ToCoawaitLoc, 5339 ToColonLoc, ToRParenLoc); 5340 } 5341 5342 Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { 5343 Stmt *ToElem = Importer.Import(S->getElement()); 5344 if (!ToElem && S->getElement()) 5345 return nullptr; 5346 Expr *ToCollect = Importer.Import(S->getCollection()); 5347 if (!ToCollect && S->getCollection()) 5348 return nullptr; 5349 Stmt *ToBody = Importer.Import(S->getBody()); 5350 if (!ToBody && S->getBody()) 5351 return nullptr; 5352 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 5353 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5354 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem, 5355 ToCollect, 5356 ToBody, ToForLoc, 5357 ToRParenLoc); 5358 } 5359 5360 Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { 5361 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc()); 5362 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5363 VarDecl *ToExceptionDecl = nullptr; 5364 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) { 5365 ToExceptionDecl = 5366 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); 5367 if (!ToExceptionDecl) 5368 return nullptr; 5369 } 5370 Stmt *ToBody = Importer.Import(S->getCatchBody()); 5371 if (!ToBody && S->getCatchBody()) 5372 return nullptr; 5373 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc, 5374 ToRParenLoc, 5375 ToExceptionDecl, 5376 ToBody); 5377 } 5378 5379 Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { 5380 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc()); 5381 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody()); 5382 if (!ToAtFinallyStmt && S->getFinallyBody()) 5383 return nullptr; 5384 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc, 5385 ToAtFinallyStmt); 5386 } 5387 5388 Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { 5389 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc()); 5390 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody()); 5391 if (!ToAtTryStmt && S->getTryBody()) 5392 return nullptr; 5393 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts()); 5394 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) { 5395 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI); 5396 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt)) 5397 ToCatchStmts[CI] = ToCatchStmt; 5398 else 5399 return nullptr; 5400 } 5401 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt()); 5402 if (!ToAtFinallyStmt && S->getFinallyStmt()) 5403 return nullptr; 5404 return ObjCAtTryStmt::Create(Importer.getToContext(), 5405 ToAtTryLoc, ToAtTryStmt, 5406 ToCatchStmts.begin(), ToCatchStmts.size(), 5407 ToAtFinallyStmt); 5408 } 5409 5410 Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt 5411 (ObjCAtSynchronizedStmt *S) { 5412 SourceLocation ToAtSynchronizedLoc = 5413 Importer.Import(S->getAtSynchronizedLoc()); 5414 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr()); 5415 if (!ToSynchExpr && S->getSynchExpr()) 5416 return nullptr; 5417 Stmt *ToSynchBody = Importer.Import(S->getSynchBody()); 5418 if (!ToSynchBody && S->getSynchBody()) 5419 return nullptr; 5420 return new (Importer.getToContext()) ObjCAtSynchronizedStmt( 5421 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody); 5422 } 5423 5424 Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { 5425 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc()); 5426 Expr *ToThrow = Importer.Import(S->getThrowExpr()); 5427 if (!ToThrow && S->getThrowExpr()) 5428 return nullptr; 5429 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow); 5430 } 5431 5432 Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt 5433 (ObjCAutoreleasePoolStmt *S) { 5434 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc()); 5435 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 5436 if (!ToSubStmt && S->getSubStmt()) 5437 return nullptr; 5438 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc, 5439 ToSubStmt); 5440 } 5441 5442 //---------------------------------------------------------------------------- 5443 // Import Expressions 5444 //---------------------------------------------------------------------------- 5445 Expr *ASTNodeImporter::VisitExpr(Expr *E) { 5446 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node) 5447 << E->getStmtClassName(); 5448 return nullptr; 5449 } 5450 5451 Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) { 5452 QualType T = Importer.Import(E->getType()); 5453 if (T.isNull()) 5454 return nullptr; 5455 5456 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5457 if (!SubExpr && E->getSubExpr()) 5458 return nullptr; 5459 5460 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo()); 5461 if (!TInfo) 5462 return nullptr; 5463 5464 return new (Importer.getToContext()) VAArgExpr( 5465 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo, 5466 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI()); 5467 } 5468 5469 Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) { 5470 QualType T = Importer.Import(E->getType()); 5471 if (T.isNull()) 5472 return nullptr; 5473 5474 return new (Importer.getToContext()) GNUNullExpr( 5475 T, Importer.Import(E->getLocStart())); 5476 } 5477 5478 Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) { 5479 QualType T = Importer.Import(E->getType()); 5480 if (T.isNull()) 5481 return nullptr; 5482 5483 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName())); 5484 if (!SL && E->getFunctionName()) 5485 return nullptr; 5486 5487 return new (Importer.getToContext()) PredefinedExpr( 5488 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL); 5489 } 5490 5491 Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { 5492 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl())); 5493 if (!ToD) 5494 return nullptr; 5495 5496 NamedDecl *FoundD = nullptr; 5497 if (E->getDecl() != E->getFoundDecl()) { 5498 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl())); 5499 if (!FoundD) 5500 return nullptr; 5501 } 5502 5503 QualType T = Importer.Import(E->getType()); 5504 if (T.isNull()) 5505 return nullptr; 5506 5507 TemplateArgumentListInfo ToTAInfo; 5508 TemplateArgumentListInfo *ResInfo = nullptr; 5509 if (E->hasExplicitTemplateArgs()) { 5510 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo)) 5511 return nullptr; 5512 ResInfo = &ToTAInfo; 5513 } 5514 5515 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(), 5516 Importer.Import(E->getQualifierLoc()), 5517 Importer.Import(E->getTemplateKeywordLoc()), 5518 ToD, 5519 E->refersToEnclosingVariableOrCapture(), 5520 Importer.Import(E->getLocation()), 5521 T, E->getValueKind(), 5522 FoundD, ResInfo); 5523 if (E->hadMultipleCandidates()) 5524 DRE->setHadMultipleCandidates(true); 5525 return DRE; 5526 } 5527 5528 Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { 5529 QualType T = Importer.Import(E->getType()); 5530 if (T.isNull()) 5531 return nullptr; 5532 5533 return new (Importer.getToContext()) ImplicitValueInitExpr(T); 5534 } 5535 5536 ASTNodeImporter::Designator 5537 ASTNodeImporter::ImportDesignator(const Designator &D) { 5538 if (D.isFieldDesignator()) { 5539 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName()); 5540 // Caller checks for import error 5541 return Designator(ToFieldName, Importer.Import(D.getDotLoc()), 5542 Importer.Import(D.getFieldLoc())); 5543 } 5544 if (D.isArrayDesignator()) 5545 return Designator(D.getFirstExprIndex(), 5546 Importer.Import(D.getLBracketLoc()), 5547 Importer.Import(D.getRBracketLoc())); 5548 5549 assert(D.isArrayRangeDesignator()); 5550 return Designator(D.getFirstExprIndex(), 5551 Importer.Import(D.getLBracketLoc()), 5552 Importer.Import(D.getEllipsisLoc()), 5553 Importer.Import(D.getRBracketLoc())); 5554 } 5555 5556 5557 Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) { 5558 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit())); 5559 if (!Init) 5560 return nullptr; 5561 5562 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1); 5563 // List elements from the second, the first is Init itself 5564 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) { 5565 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I)))) 5566 IndexExprs[I - 1] = Arg; 5567 else 5568 return nullptr; 5569 } 5570 5571 SmallVector<Designator, 4> Designators(DIE->size()); 5572 llvm::transform(DIE->designators(), Designators.begin(), 5573 [this](const Designator &D) -> Designator { 5574 return ImportDesignator(D); 5575 }); 5576 5577 for (const auto &D : DIE->designators()) 5578 if (D.isFieldDesignator() && !D.getFieldName()) 5579 return nullptr; 5580 5581 return DesignatedInitExpr::Create( 5582 Importer.getToContext(), Designators, 5583 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()), 5584 DIE->usesGNUSyntax(), Init); 5585 } 5586 5587 Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) { 5588 QualType T = Importer.Import(E->getType()); 5589 if (T.isNull()) 5590 return nullptr; 5591 5592 return new (Importer.getToContext()) 5593 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation())); 5594 } 5595 5596 Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { 5597 QualType T = Importer.Import(E->getType()); 5598 if (T.isNull()) 5599 return nullptr; 5600 5601 return IntegerLiteral::Create(Importer.getToContext(), 5602 E->getValue(), T, 5603 Importer.Import(E->getLocation())); 5604 } 5605 5606 Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) { 5607 QualType T = Importer.Import(E->getType()); 5608 if (T.isNull()) 5609 return nullptr; 5610 5611 return FloatingLiteral::Create(Importer.getToContext(), 5612 E->getValue(), E->isExact(), T, 5613 Importer.Import(E->getLocation())); 5614 } 5615 5616 Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { 5617 QualType T = Importer.Import(E->getType()); 5618 if (T.isNull()) 5619 return nullptr; 5620 5621 return new (Importer.getToContext()) CharacterLiteral(E->getValue(), 5622 E->getKind(), T, 5623 Importer.Import(E->getLocation())); 5624 } 5625 5626 Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) { 5627 QualType T = Importer.Import(E->getType()); 5628 if (T.isNull()) 5629 return nullptr; 5630 5631 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated()); 5632 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin()); 5633 5634 return StringLiteral::Create(Importer.getToContext(), E->getBytes(), 5635 E->getKind(), E->isPascal(), T, 5636 Locations.data(), Locations.size()); 5637 } 5638 5639 Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 5640 QualType T = Importer.Import(E->getType()); 5641 if (T.isNull()) 5642 return nullptr; 5643 5644 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo()); 5645 if (!TInfo) 5646 return nullptr; 5647 5648 Expr *Init = Importer.Import(E->getInitializer()); 5649 if (!Init) 5650 return nullptr; 5651 5652 return new (Importer.getToContext()) CompoundLiteralExpr( 5653 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(), 5654 Init, E->isFileScope()); 5655 } 5656 5657 Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) { 5658 QualType T = Importer.Import(E->getType()); 5659 if (T.isNull()) 5660 return nullptr; 5661 5662 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs()); 5663 if (ImportArrayChecked( 5664 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(), 5665 Exprs.begin())) 5666 return nullptr; 5667 5668 return new (Importer.getToContext()) AtomicExpr( 5669 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(), 5670 Importer.Import(E->getRParenLoc())); 5671 } 5672 5673 Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) { 5674 QualType T = Importer.Import(E->getType()); 5675 if (T.isNull()) 5676 return nullptr; 5677 5678 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel())); 5679 if (!ToLabel) 5680 return nullptr; 5681 5682 return new (Importer.getToContext()) AddrLabelExpr( 5683 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()), 5684 ToLabel, T); 5685 } 5686 5687 Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) { 5688 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5689 if (!SubExpr) 5690 return nullptr; 5691 5692 return new (Importer.getToContext()) 5693 ParenExpr(Importer.Import(E->getLParen()), 5694 Importer.Import(E->getRParen()), 5695 SubExpr); 5696 } 5697 5698 Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) { 5699 SmallVector<Expr *, 4> Exprs(E->getNumExprs()); 5700 if (ImportContainerChecked(E->exprs(), Exprs)) 5701 return nullptr; 5702 5703 return new (Importer.getToContext()) ParenListExpr( 5704 Importer.getToContext(), Importer.Import(E->getLParenLoc()), 5705 Exprs, Importer.Import(E->getLParenLoc())); 5706 } 5707 5708 Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) { 5709 QualType T = Importer.Import(E->getType()); 5710 if (T.isNull()) 5711 return nullptr; 5712 5713 auto *ToSubStmt = cast_or_null<CompoundStmt>( 5714 Importer.Import(E->getSubStmt())); 5715 if (!ToSubStmt && E->getSubStmt()) 5716 return nullptr; 5717 5718 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T, 5719 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc())); 5720 } 5721 5722 Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { 5723 QualType T = Importer.Import(E->getType()); 5724 if (T.isNull()) 5725 return nullptr; 5726 5727 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5728 if (!SubExpr) 5729 return nullptr; 5730 5731 return new (Importer.getToContext()) UnaryOperator( 5732 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(), 5733 Importer.Import(E->getOperatorLoc()), E->canOverflow()); 5734 } 5735 5736 Expr * 5737 ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) { 5738 QualType ResultType = Importer.Import(E->getType()); 5739 5740 if (E->isArgumentType()) { 5741 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo()); 5742 if (!TInfo) 5743 return nullptr; 5744 5745 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), 5746 TInfo, ResultType, 5747 Importer.Import(E->getOperatorLoc()), 5748 Importer.Import(E->getRParenLoc())); 5749 } 5750 5751 Expr *SubExpr = Importer.Import(E->getArgumentExpr()); 5752 if (!SubExpr) 5753 return nullptr; 5754 5755 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), 5756 SubExpr, ResultType, 5757 Importer.Import(E->getOperatorLoc()), 5758 Importer.Import(E->getRParenLoc())); 5759 } 5760 5761 Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { 5762 QualType T = Importer.Import(E->getType()); 5763 if (T.isNull()) 5764 return nullptr; 5765 5766 Expr *LHS = Importer.Import(E->getLHS()); 5767 if (!LHS) 5768 return nullptr; 5769 5770 Expr *RHS = Importer.Import(E->getRHS()); 5771 if (!RHS) 5772 return nullptr; 5773 5774 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(), 5775 T, E->getValueKind(), 5776 E->getObjectKind(), 5777 Importer.Import(E->getOperatorLoc()), 5778 E->getFPFeatures()); 5779 } 5780 5781 Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) { 5782 QualType T = Importer.Import(E->getType()); 5783 if (T.isNull()) 5784 return nullptr; 5785 5786 Expr *ToLHS = Importer.Import(E->getLHS()); 5787 if (!ToLHS) 5788 return nullptr; 5789 5790 Expr *ToRHS = Importer.Import(E->getRHS()); 5791 if (!ToRHS) 5792 return nullptr; 5793 5794 Expr *ToCond = Importer.Import(E->getCond()); 5795 if (!ToCond) 5796 return nullptr; 5797 5798 return new (Importer.getToContext()) ConditionalOperator( 5799 ToCond, Importer.Import(E->getQuestionLoc()), 5800 ToLHS, Importer.Import(E->getColonLoc()), 5801 ToRHS, T, E->getValueKind(), E->getObjectKind()); 5802 } 5803 5804 Expr *ASTNodeImporter::VisitBinaryConditionalOperator( 5805 BinaryConditionalOperator *E) { 5806 QualType T = Importer.Import(E->getType()); 5807 if (T.isNull()) 5808 return nullptr; 5809 5810 Expr *Common = Importer.Import(E->getCommon()); 5811 if (!Common) 5812 return nullptr; 5813 5814 Expr *Cond = Importer.Import(E->getCond()); 5815 if (!Cond) 5816 return nullptr; 5817 5818 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>( 5819 Importer.Import(E->getOpaqueValue())); 5820 if (!OpaqueValue) 5821 return nullptr; 5822 5823 Expr *TrueExpr = Importer.Import(E->getTrueExpr()); 5824 if (!TrueExpr) 5825 return nullptr; 5826 5827 Expr *FalseExpr = Importer.Import(E->getFalseExpr()); 5828 if (!FalseExpr) 5829 return nullptr; 5830 5831 return new (Importer.getToContext()) BinaryConditionalOperator( 5832 Common, OpaqueValue, Cond, TrueExpr, FalseExpr, 5833 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()), 5834 T, E->getValueKind(), E->getObjectKind()); 5835 } 5836 5837 Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 5838 QualType T = Importer.Import(E->getType()); 5839 if (T.isNull()) 5840 return nullptr; 5841 5842 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo()); 5843 if (!ToQueried) 5844 return nullptr; 5845 5846 Expr *Dim = Importer.Import(E->getDimensionExpression()); 5847 if (!Dim && E->getDimensionExpression()) 5848 return nullptr; 5849 5850 return new (Importer.getToContext()) ArrayTypeTraitExpr( 5851 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried, 5852 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T); 5853 } 5854 5855 Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 5856 QualType T = Importer.Import(E->getType()); 5857 if (T.isNull()) 5858 return nullptr; 5859 5860 Expr *ToQueried = Importer.Import(E->getQueriedExpression()); 5861 if (!ToQueried) 5862 return nullptr; 5863 5864 return new (Importer.getToContext()) ExpressionTraitExpr( 5865 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried, 5866 E->getValue(), Importer.Import(E->getLocEnd()), T); 5867 } 5868 5869 Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) { 5870 QualType T = Importer.Import(E->getType()); 5871 if (T.isNull()) 5872 return nullptr; 5873 5874 Expr *SourceExpr = Importer.Import(E->getSourceExpr()); 5875 if (!SourceExpr && E->getSourceExpr()) 5876 return nullptr; 5877 5878 return new (Importer.getToContext()) OpaqueValueExpr( 5879 Importer.Import(E->getLocation()), T, E->getValueKind(), 5880 E->getObjectKind(), SourceExpr); 5881 } 5882 5883 Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 5884 QualType T = Importer.Import(E->getType()); 5885 if (T.isNull()) 5886 return nullptr; 5887 5888 Expr *ToLHS = Importer.Import(E->getLHS()); 5889 if (!ToLHS) 5890 return nullptr; 5891 5892 Expr *ToRHS = Importer.Import(E->getRHS()); 5893 if (!ToRHS) 5894 return nullptr; 5895 5896 return new (Importer.getToContext()) ArraySubscriptExpr( 5897 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(), 5898 Importer.Import(E->getRBracketLoc())); 5899 } 5900 5901 Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { 5902 QualType T = Importer.Import(E->getType()); 5903 if (T.isNull()) 5904 return nullptr; 5905 5906 QualType CompLHSType = Importer.Import(E->getComputationLHSType()); 5907 if (CompLHSType.isNull()) 5908 return nullptr; 5909 5910 QualType CompResultType = Importer.Import(E->getComputationResultType()); 5911 if (CompResultType.isNull()) 5912 return nullptr; 5913 5914 Expr *LHS = Importer.Import(E->getLHS()); 5915 if (!LHS) 5916 return nullptr; 5917 5918 Expr *RHS = Importer.Import(E->getRHS()); 5919 if (!RHS) 5920 return nullptr; 5921 5922 return new (Importer.getToContext()) 5923 CompoundAssignOperator(LHS, RHS, E->getOpcode(), 5924 T, E->getValueKind(), 5925 E->getObjectKind(), 5926 CompLHSType, CompResultType, 5927 Importer.Import(E->getOperatorLoc()), 5928 E->getFPFeatures()); 5929 } 5930 5931 bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) { 5932 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) { 5933 if (CXXBaseSpecifier *Spec = Importer.Import(*I)) 5934 Path.push_back(Spec); 5935 else 5936 return true; 5937 } 5938 return false; 5939 } 5940 5941 Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { 5942 QualType T = Importer.Import(E->getType()); 5943 if (T.isNull()) 5944 return nullptr; 5945 5946 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5947 if (!SubExpr) 5948 return nullptr; 5949 5950 CXXCastPath BasePath; 5951 if (ImportCastPath(E, BasePath)) 5952 return nullptr; 5953 5954 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(), 5955 SubExpr, &BasePath, E->getValueKind()); 5956 } 5957 5958 Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) { 5959 QualType T = Importer.Import(E->getType()); 5960 if (T.isNull()) 5961 return nullptr; 5962 5963 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5964 if (!SubExpr) 5965 return nullptr; 5966 5967 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten()); 5968 if (!TInfo && E->getTypeInfoAsWritten()) 5969 return nullptr; 5970 5971 CXXCastPath BasePath; 5972 if (ImportCastPath(E, BasePath)) 5973 return nullptr; 5974 5975 switch (E->getStmtClass()) { 5976 case Stmt::CStyleCastExprClass: { 5977 auto *CCE = cast<CStyleCastExpr>(E); 5978 return CStyleCastExpr::Create(Importer.getToContext(), T, 5979 E->getValueKind(), E->getCastKind(), 5980 SubExpr, &BasePath, TInfo, 5981 Importer.Import(CCE->getLParenLoc()), 5982 Importer.Import(CCE->getRParenLoc())); 5983 } 5984 5985 case Stmt::CXXFunctionalCastExprClass: { 5986 auto *FCE = cast<CXXFunctionalCastExpr>(E); 5987 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T, 5988 E->getValueKind(), TInfo, 5989 E->getCastKind(), SubExpr, &BasePath, 5990 Importer.Import(FCE->getLParenLoc()), 5991 Importer.Import(FCE->getRParenLoc())); 5992 } 5993 5994 case Stmt::ObjCBridgedCastExprClass: { 5995 auto *OCE = cast<ObjCBridgedCastExpr>(E); 5996 return new (Importer.getToContext()) ObjCBridgedCastExpr( 5997 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(), 5998 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()), 5999 TInfo, SubExpr); 6000 } 6001 default: 6002 break; // just fall through 6003 } 6004 6005 auto *Named = cast<CXXNamedCastExpr>(E); 6006 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()), 6007 RParenLoc = Importer.Import(Named->getRParenLoc()); 6008 SourceRange Brackets = Importer.Import(Named->getAngleBrackets()); 6009 6010 switch (E->getStmtClass()) { 6011 case Stmt::CXXStaticCastExprClass: 6012 return CXXStaticCastExpr::Create(Importer.getToContext(), T, 6013 E->getValueKind(), E->getCastKind(), 6014 SubExpr, &BasePath, TInfo, 6015 ExprLoc, RParenLoc, Brackets); 6016 6017 case Stmt::CXXDynamicCastExprClass: 6018 return CXXDynamicCastExpr::Create(Importer.getToContext(), T, 6019 E->getValueKind(), E->getCastKind(), 6020 SubExpr, &BasePath, TInfo, 6021 ExprLoc, RParenLoc, Brackets); 6022 6023 case Stmt::CXXReinterpretCastExprClass: 6024 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T, 6025 E->getValueKind(), E->getCastKind(), 6026 SubExpr, &BasePath, TInfo, 6027 ExprLoc, RParenLoc, Brackets); 6028 6029 case Stmt::CXXConstCastExprClass: 6030 return CXXConstCastExpr::Create(Importer.getToContext(), T, 6031 E->getValueKind(), SubExpr, TInfo, ExprLoc, 6032 RParenLoc, Brackets); 6033 default: 6034 llvm_unreachable("Cast expression of unsupported type!"); 6035 return nullptr; 6036 } 6037 } 6038 6039 Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) { 6040 QualType T = Importer.Import(OE->getType()); 6041 if (T.isNull()) 6042 return nullptr; 6043 6044 SmallVector<OffsetOfNode, 4> Nodes; 6045 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) { 6046 const OffsetOfNode &Node = OE->getComponent(I); 6047 6048 switch (Node.getKind()) { 6049 case OffsetOfNode::Array: 6050 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), 6051 Node.getArrayExprIndex(), 6052 Importer.Import(Node.getLocEnd()))); 6053 break; 6054 6055 case OffsetOfNode::Base: { 6056 CXXBaseSpecifier *BS = Importer.Import(Node.getBase()); 6057 if (!BS && Node.getBase()) 6058 return nullptr; 6059 Nodes.push_back(OffsetOfNode(BS)); 6060 break; 6061 } 6062 case OffsetOfNode::Field: { 6063 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField())); 6064 if (!FD) 6065 return nullptr; 6066 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD, 6067 Importer.Import(Node.getLocEnd()))); 6068 break; 6069 } 6070 case OffsetOfNode::Identifier: { 6071 IdentifierInfo *ToII = Importer.Import(Node.getFieldName()); 6072 if (!ToII) 6073 return nullptr; 6074 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII, 6075 Importer.Import(Node.getLocEnd()))); 6076 break; 6077 } 6078 } 6079 } 6080 6081 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions()); 6082 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) { 6083 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I)); 6084 if (!ToIndexExpr) 6085 return nullptr; 6086 Exprs[I] = ToIndexExpr; 6087 } 6088 6089 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo()); 6090 if (!TInfo && OE->getTypeSourceInfo()) 6091 return nullptr; 6092 6093 return OffsetOfExpr::Create(Importer.getToContext(), T, 6094 Importer.Import(OE->getOperatorLoc()), 6095 TInfo, Nodes, Exprs, 6096 Importer.Import(OE->getRParenLoc())); 6097 } 6098 6099 Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 6100 QualType T = Importer.Import(E->getType()); 6101 if (T.isNull()) 6102 return nullptr; 6103 6104 Expr *Operand = Importer.Import(E->getOperand()); 6105 if (!Operand) 6106 return nullptr; 6107 6108 CanThrowResult CanThrow; 6109 if (E->isValueDependent()) 6110 CanThrow = CT_Dependent; 6111 else 6112 CanThrow = E->getValue() ? CT_Can : CT_Cannot; 6113 6114 return new (Importer.getToContext()) CXXNoexceptExpr( 6115 T, Operand, CanThrow, 6116 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd())); 6117 } 6118 6119 Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) { 6120 QualType T = Importer.Import(E->getType()); 6121 if (T.isNull()) 6122 return nullptr; 6123 6124 Expr *SubExpr = Importer.Import(E->getSubExpr()); 6125 if (!SubExpr && E->getSubExpr()) 6126 return nullptr; 6127 6128 return new (Importer.getToContext()) CXXThrowExpr( 6129 SubExpr, T, Importer.Import(E->getThrowLoc()), 6130 E->isThrownVariableInScope()); 6131 } 6132 6133 Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { 6134 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam())); 6135 if (!Param) 6136 return nullptr; 6137 6138 return CXXDefaultArgExpr::Create( 6139 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param); 6140 } 6141 6142 Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { 6143 QualType T = Importer.Import(E->getType()); 6144 if (T.isNull()) 6145 return nullptr; 6146 6147 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo()); 6148 if (!TypeInfo) 6149 return nullptr; 6150 6151 return new (Importer.getToContext()) CXXScalarValueInitExpr( 6152 T, TypeInfo, Importer.Import(E->getRParenLoc())); 6153 } 6154 6155 Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { 6156 Expr *SubExpr = Importer.Import(E->getSubExpr()); 6157 if (!SubExpr) 6158 return nullptr; 6159 6160 auto *Dtor = cast_or_null<CXXDestructorDecl>( 6161 Importer.Import(const_cast<CXXDestructorDecl *>( 6162 E->getTemporary()->getDestructor()))); 6163 if (!Dtor) 6164 return nullptr; 6165 6166 ASTContext &ToCtx = Importer.getToContext(); 6167 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor); 6168 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr); 6169 } 6170 6171 Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) { 6172 QualType T = Importer.Import(CE->getType()); 6173 if (T.isNull()) 6174 return nullptr; 6175 6176 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo()); 6177 if (!TInfo) 6178 return nullptr; 6179 6180 SmallVector<Expr *, 8> Args(CE->getNumArgs()); 6181 if (ImportContainerChecked(CE->arguments(), Args)) 6182 return nullptr; 6183 6184 auto *Ctor = cast_or_null<CXXConstructorDecl>( 6185 Importer.Import(CE->getConstructor())); 6186 if (!Ctor) 6187 return nullptr; 6188 6189 return new (Importer.getToContext()) CXXTemporaryObjectExpr( 6190 Importer.getToContext(), Ctor, T, TInfo, Args, 6191 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(), 6192 CE->isListInitialization(), CE->isStdInitListInitialization(), 6193 CE->requiresZeroInitialization()); 6194 } 6195 6196 Expr * 6197 ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) { 6198 QualType T = Importer.Import(E->getType()); 6199 if (T.isNull()) 6200 return nullptr; 6201 6202 Expr *TempE = Importer.Import(E->GetTemporaryExpr()); 6203 if (!TempE) 6204 return nullptr; 6205 6206 auto *ExtendedBy = cast_or_null<ValueDecl>( 6207 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl()))); 6208 if (!ExtendedBy && E->getExtendingDecl()) 6209 return nullptr; 6210 6211 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr( 6212 T, TempE, E->isBoundToLvalueReference()); 6213 6214 // FIXME: Should ManglingNumber get numbers associated with 'to' context? 6215 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber()); 6216 return ToMTE; 6217 } 6218 6219 Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) { 6220 QualType T = Importer.Import(E->getType()); 6221 if (T.isNull()) 6222 return nullptr; 6223 6224 Expr *Pattern = Importer.Import(E->getPattern()); 6225 if (!Pattern) 6226 return nullptr; 6227 6228 return new (Importer.getToContext()) PackExpansionExpr( 6229 T, Pattern, Importer.Import(E->getEllipsisLoc()), 6230 E->getNumExpansions()); 6231 } 6232 6233 Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 6234 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack())); 6235 if (!Pack) 6236 return nullptr; 6237 6238 Optional<unsigned> Length; 6239 6240 if (!E->isValueDependent()) 6241 Length = E->getPackLength(); 6242 6243 SmallVector<TemplateArgument, 8> PartialArguments; 6244 if (E->isPartiallySubstituted()) { 6245 if (ImportTemplateArguments(E->getPartialArguments().data(), 6246 E->getPartialArguments().size(), 6247 PartialArguments)) 6248 return nullptr; 6249 } 6250 6251 return SizeOfPackExpr::Create( 6252 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack, 6253 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()), 6254 Length, PartialArguments); 6255 } 6256 6257 Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) { 6258 QualType T = Importer.Import(CE->getType()); 6259 if (T.isNull()) 6260 return nullptr; 6261 6262 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs()); 6263 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs)) 6264 return nullptr; 6265 6266 auto *OperatorNewDecl = cast_or_null<FunctionDecl>( 6267 Importer.Import(CE->getOperatorNew())); 6268 if (!OperatorNewDecl && CE->getOperatorNew()) 6269 return nullptr; 6270 6271 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>( 6272 Importer.Import(CE->getOperatorDelete())); 6273 if (!OperatorDeleteDecl && CE->getOperatorDelete()) 6274 return nullptr; 6275 6276 Expr *ToInit = Importer.Import(CE->getInitializer()); 6277 if (!ToInit && CE->getInitializer()) 6278 return nullptr; 6279 6280 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo()); 6281 if (!TInfo) 6282 return nullptr; 6283 6284 Expr *ToArrSize = Importer.Import(CE->getArraySize()); 6285 if (!ToArrSize && CE->getArraySize()) 6286 return nullptr; 6287 6288 return new (Importer.getToContext()) CXXNewExpr( 6289 Importer.getToContext(), 6290 CE->isGlobalNew(), 6291 OperatorNewDecl, OperatorDeleteDecl, 6292 CE->passAlignment(), 6293 CE->doesUsualArrayDeleteWantSize(), 6294 PlacementArgs, 6295 Importer.Import(CE->getTypeIdParens()), 6296 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo, 6297 Importer.Import(CE->getSourceRange()), 6298 Importer.Import(CE->getDirectInitRange())); 6299 } 6300 6301 Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 6302 QualType T = Importer.Import(E->getType()); 6303 if (T.isNull()) 6304 return nullptr; 6305 6306 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>( 6307 Importer.Import(E->getOperatorDelete())); 6308 if (!OperatorDeleteDecl && E->getOperatorDelete()) 6309 return nullptr; 6310 6311 Expr *ToArg = Importer.Import(E->getArgument()); 6312 if (!ToArg && E->getArgument()) 6313 return nullptr; 6314 6315 return new (Importer.getToContext()) CXXDeleteExpr( 6316 T, E->isGlobalDelete(), 6317 E->isArrayForm(), 6318 E->isArrayFormAsWritten(), 6319 E->doesUsualArrayDeleteWantSize(), 6320 OperatorDeleteDecl, 6321 ToArg, 6322 Importer.Import(E->getLocStart())); 6323 } 6324 6325 Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) { 6326 QualType T = Importer.Import(E->getType()); 6327 if (T.isNull()) 6328 return nullptr; 6329 6330 auto *ToCCD = 6331 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor())); 6332 if (!ToCCD) 6333 return nullptr; 6334 6335 SmallVector<Expr *, 6> ToArgs(E->getNumArgs()); 6336 if (ImportContainerChecked(E->arguments(), ToArgs)) 6337 return nullptr; 6338 6339 return CXXConstructExpr::Create(Importer.getToContext(), T, 6340 Importer.Import(E->getLocation()), 6341 ToCCD, E->isElidable(), 6342 ToArgs, E->hadMultipleCandidates(), 6343 E->isListInitialization(), 6344 E->isStdInitListInitialization(), 6345 E->requiresZeroInitialization(), 6346 E->getConstructionKind(), 6347 Importer.Import(E->getParenOrBraceRange())); 6348 } 6349 6350 Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) { 6351 Expr *SubExpr = Importer.Import(EWC->getSubExpr()); 6352 if (!SubExpr && EWC->getSubExpr()) 6353 return nullptr; 6354 6355 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects()); 6356 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++) 6357 if (ExprWithCleanups::CleanupObject Obj = 6358 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I)))) 6359 Objs[I] = Obj; 6360 else 6361 return nullptr; 6362 6363 return ExprWithCleanups::Create(Importer.getToContext(), 6364 SubExpr, EWC->cleanupsHaveSideEffects(), 6365 Objs); 6366 } 6367 6368 Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { 6369 QualType T = Importer.Import(E->getType()); 6370 if (T.isNull()) 6371 return nullptr; 6372 6373 Expr *ToFn = Importer.Import(E->getCallee()); 6374 if (!ToFn) 6375 return nullptr; 6376 6377 SmallVector<Expr *, 4> ToArgs(E->getNumArgs()); 6378 if (ImportContainerChecked(E->arguments(), ToArgs)) 6379 return nullptr; 6380 6381 return new (Importer.getToContext()) CXXMemberCallExpr( 6382 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(), 6383 Importer.Import(E->getRParenLoc())); 6384 } 6385 6386 Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) { 6387 QualType T = Importer.Import(E->getType()); 6388 if (T.isNull()) 6389 return nullptr; 6390 6391 return new (Importer.getToContext()) 6392 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit()); 6393 } 6394 6395 Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { 6396 QualType T = Importer.Import(E->getType()); 6397 if (T.isNull()) 6398 return nullptr; 6399 6400 return new (Importer.getToContext()) 6401 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation())); 6402 } 6403 6404 6405 Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) { 6406 QualType T = Importer.Import(E->getType()); 6407 if (T.isNull()) 6408 return nullptr; 6409 6410 Expr *ToBase = Importer.Import(E->getBase()); 6411 if (!ToBase && E->getBase()) 6412 return nullptr; 6413 6414 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl())); 6415 if (!ToMember && E->getMemberDecl()) 6416 return nullptr; 6417 6418 auto *ToDecl = 6419 dyn_cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())); 6420 if (!ToDecl && E->getFoundDecl().getDecl()) 6421 return nullptr; 6422 6423 DeclAccessPair ToFoundDecl = 6424 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess()); 6425 6426 DeclarationNameInfo ToMemberNameInfo( 6427 Importer.Import(E->getMemberNameInfo().getName()), 6428 Importer.Import(E->getMemberNameInfo().getLoc())); 6429 6430 if (E->hasExplicitTemplateArgs()) { 6431 return nullptr; // FIXME: handle template arguments 6432 } 6433 6434 return MemberExpr::Create(Importer.getToContext(), ToBase, 6435 E->isArrow(), 6436 Importer.Import(E->getOperatorLoc()), 6437 Importer.Import(E->getQualifierLoc()), 6438 Importer.Import(E->getTemplateKeywordLoc()), 6439 ToMember, ToFoundDecl, ToMemberNameInfo, 6440 nullptr, T, E->getValueKind(), 6441 E->getObjectKind()); 6442 } 6443 6444 Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr( 6445 CXXPseudoDestructorExpr *E) { 6446 Expr *BaseE = Importer.Import(E->getBase()); 6447 if (!BaseE) 6448 return nullptr; 6449 6450 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo()); 6451 if (!ScopeInfo && E->getScopeTypeInfo()) 6452 return nullptr; 6453 6454 PseudoDestructorTypeStorage Storage; 6455 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) { 6456 IdentifierInfo *ToII = Importer.Import(FromII); 6457 if (!ToII) 6458 return nullptr; 6459 Storage = PseudoDestructorTypeStorage( 6460 ToII, Importer.Import(E->getDestroyedTypeLoc())); 6461 } else { 6462 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo()); 6463 if (!TI) 6464 return nullptr; 6465 Storage = PseudoDestructorTypeStorage(TI); 6466 } 6467 6468 return new (Importer.getToContext()) CXXPseudoDestructorExpr( 6469 Importer.getToContext(), BaseE, E->isArrow(), 6470 Importer.Import(E->getOperatorLoc()), 6471 Importer.Import(E->getQualifierLoc()), 6472 ScopeInfo, Importer.Import(E->getColonColonLoc()), 6473 Importer.Import(E->getTildeLoc()), Storage); 6474 } 6475 6476 Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr( 6477 CXXDependentScopeMemberExpr *E) { 6478 Expr *Base = nullptr; 6479 if (!E->isImplicitAccess()) { 6480 Base = Importer.Import(E->getBase()); 6481 if (!Base) 6482 return nullptr; 6483 } 6484 6485 QualType BaseType = Importer.Import(E->getBaseType()); 6486 if (BaseType.isNull()) 6487 return nullptr; 6488 6489 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; 6490 if (E->hasExplicitTemplateArgs()) { 6491 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(), 6492 E->template_arguments(), ToTAInfo)) 6493 return nullptr; 6494 ResInfo = &ToTAInfo; 6495 } 6496 6497 DeclarationName Name = Importer.Import(E->getMember()); 6498 if (!E->getMember().isEmpty() && Name.isEmpty()) 6499 return nullptr; 6500 6501 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc())); 6502 // Import additional name location/type info. 6503 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo); 6504 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope()); 6505 if (!ToFQ && E->getFirstQualifierFoundInScope()) 6506 return nullptr; 6507 6508 return CXXDependentScopeMemberExpr::Create( 6509 Importer.getToContext(), Base, BaseType, E->isArrow(), 6510 Importer.Import(E->getOperatorLoc()), 6511 Importer.Import(E->getQualifierLoc()), 6512 Importer.Import(E->getTemplateKeywordLoc()), 6513 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo); 6514 } 6515 6516 Expr * 6517 ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { 6518 DeclarationName Name = Importer.Import(E->getDeclName()); 6519 if (!E->getDeclName().isEmpty() && Name.isEmpty()) 6520 return nullptr; 6521 6522 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getExprLoc())); 6523 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo); 6524 6525 TemplateArgumentListInfo ToTAInfo(Importer.Import(E->getLAngleLoc()), 6526 Importer.Import(E->getRAngleLoc())); 6527 TemplateArgumentListInfo *ResInfo = nullptr; 6528 if (E->hasExplicitTemplateArgs()) { 6529 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo)) 6530 return nullptr; 6531 ResInfo = &ToTAInfo; 6532 } 6533 6534 return DependentScopeDeclRefExpr::Create( 6535 Importer.getToContext(), Importer.Import(E->getQualifierLoc()), 6536 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo); 6537 } 6538 6539 Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr( 6540 CXXUnresolvedConstructExpr *CE) { 6541 unsigned NumArgs = CE->arg_size(); 6542 6543 SmallVector<Expr *, 8> ToArgs(NumArgs); 6544 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin())) 6545 return nullptr; 6546 6547 return CXXUnresolvedConstructExpr::Create( 6548 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()), 6549 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs), 6550 Importer.Import(CE->getRParenLoc())); 6551 } 6552 6553 Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) { 6554 auto *NamingClass = 6555 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass())); 6556 if (E->getNamingClass() && !NamingClass) 6557 return nullptr; 6558 6559 DeclarationName Name = Importer.Import(E->getName()); 6560 if (E->getName() && !Name) 6561 return nullptr; 6562 6563 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc())); 6564 // Import additional name location/type info. 6565 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo); 6566 6567 UnresolvedSet<8> ToDecls; 6568 for (auto *D : E->decls()) { 6569 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D))) 6570 ToDecls.addDecl(To); 6571 else 6572 return nullptr; 6573 } 6574 6575 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; 6576 if (E->hasExplicitTemplateArgs()) { 6577 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(), 6578 E->template_arguments(), ToTAInfo)) 6579 return nullptr; 6580 ResInfo = &ToTAInfo; 6581 } 6582 6583 if (ResInfo || E->getTemplateKeywordLoc().isValid()) 6584 return UnresolvedLookupExpr::Create( 6585 Importer.getToContext(), NamingClass, 6586 Importer.Import(E->getQualifierLoc()), 6587 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(), 6588 ResInfo, ToDecls.begin(), ToDecls.end()); 6589 6590 return UnresolvedLookupExpr::Create( 6591 Importer.getToContext(), NamingClass, 6592 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(), 6593 E->isOverloaded(), ToDecls.begin(), ToDecls.end()); 6594 } 6595 6596 Expr *ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) { 6597 DeclarationName Name = Importer.Import(E->getName()); 6598 if (!E->getName().isEmpty() && Name.isEmpty()) 6599 return nullptr; 6600 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc())); 6601 // Import additional name location/type info. 6602 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo); 6603 6604 QualType BaseType = Importer.Import(E->getType()); 6605 if (!E->getType().isNull() && BaseType.isNull()) 6606 return nullptr; 6607 6608 UnresolvedSet<8> ToDecls; 6609 for (Decl *D : E->decls()) { 6610 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D))) 6611 ToDecls.addDecl(To); 6612 else 6613 return nullptr; 6614 } 6615 6616 TemplateArgumentListInfo ToTAInfo; 6617 TemplateArgumentListInfo *ResInfo = nullptr; 6618 if (E->hasExplicitTemplateArgs()) { 6619 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo)) 6620 return nullptr; 6621 ResInfo = &ToTAInfo; 6622 } 6623 6624 Expr *BaseE = E->isImplicitAccess() ? nullptr : Importer.Import(E->getBase()); 6625 if (!BaseE && !E->isImplicitAccess() && E->getBase()) { 6626 return nullptr; 6627 } 6628 6629 return UnresolvedMemberExpr::Create( 6630 Importer.getToContext(), E->hasUnresolvedUsing(), BaseE, BaseType, 6631 E->isArrow(), Importer.Import(E->getOperatorLoc()), 6632 Importer.Import(E->getQualifierLoc()), 6633 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo, 6634 ToDecls.begin(), ToDecls.end()); 6635 } 6636 6637 Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) { 6638 QualType T = Importer.Import(E->getType()); 6639 if (T.isNull()) 6640 return nullptr; 6641 6642 Expr *ToCallee = Importer.Import(E->getCallee()); 6643 if (!ToCallee && E->getCallee()) 6644 return nullptr; 6645 6646 unsigned NumArgs = E->getNumArgs(); 6647 SmallVector<Expr *, 2> ToArgs(NumArgs); 6648 if (ImportContainerChecked(E->arguments(), ToArgs)) 6649 return nullptr; 6650 6651 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs]; 6652 6653 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) 6654 ToArgs_Copied[ai] = ToArgs[ai]; 6655 6656 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) { 6657 return new (Importer.getToContext()) CXXOperatorCallExpr( 6658 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T, 6659 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()), 6660 OCE->getFPFeatures()); 6661 } 6662 6663 return new (Importer.getToContext()) 6664 CallExpr(Importer.getToContext(), ToCallee, 6665 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(), 6666 Importer.Import(E->getRParenLoc())); 6667 } 6668 6669 Optional<LambdaCapture> 6670 ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) { 6671 VarDecl *Var = nullptr; 6672 if (From.capturesVariable()) { 6673 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar())); 6674 if (!Var) 6675 return None; 6676 } 6677 6678 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(), 6679 From.getCaptureKind(), Var, 6680 From.isPackExpansion() 6681 ? Importer.Import(From.getEllipsisLoc()) 6682 : SourceLocation()); 6683 } 6684 6685 Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) { 6686 CXXRecordDecl *FromClass = LE->getLambdaClass(); 6687 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass)); 6688 if (!ToClass) 6689 return nullptr; 6690 6691 // NOTE: lambda classes are created with BeingDefined flag set up. 6692 // It means that ImportDefinition doesn't work for them and we should fill it 6693 // manually. 6694 if (ToClass->isBeingDefined()) { 6695 for (auto FromField : FromClass->fields()) { 6696 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField)); 6697 if (!ToField) 6698 return nullptr; 6699 } 6700 } 6701 6702 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>( 6703 Importer.Import(LE->getCallOperator())); 6704 if (!ToCallOp) 6705 return nullptr; 6706 6707 ToClass->completeDefinition(); 6708 6709 unsigned NumCaptures = LE->capture_size(); 6710 SmallVector<LambdaCapture, 8> Captures; 6711 Captures.reserve(NumCaptures); 6712 for (const auto &FromCapture : LE->captures()) { 6713 if (auto ToCapture = ImportLambdaCapture(FromCapture)) 6714 Captures.push_back(*ToCapture); 6715 else 6716 return nullptr; 6717 } 6718 6719 SmallVector<Expr *, 8> InitCaptures(NumCaptures); 6720 if (ImportContainerChecked(LE->capture_inits(), InitCaptures)) 6721 return nullptr; 6722 6723 return LambdaExpr::Create(Importer.getToContext(), ToClass, 6724 Importer.Import(LE->getIntroducerRange()), 6725 LE->getCaptureDefault(), 6726 Importer.Import(LE->getCaptureDefaultLoc()), 6727 Captures, 6728 LE->hasExplicitParameters(), 6729 LE->hasExplicitResultType(), 6730 InitCaptures, 6731 Importer.Import(LE->getLocEnd()), 6732 LE->containsUnexpandedParameterPack()); 6733 } 6734 6735 Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) { 6736 QualType T = Importer.Import(ILE->getType()); 6737 if (T.isNull()) 6738 return nullptr; 6739 6740 SmallVector<Expr *, 4> Exprs(ILE->getNumInits()); 6741 if (ImportContainerChecked(ILE->inits(), Exprs)) 6742 return nullptr; 6743 6744 ASTContext &ToCtx = Importer.getToContext(); 6745 InitListExpr *To = new (ToCtx) InitListExpr( 6746 ToCtx, Importer.Import(ILE->getLBraceLoc()), 6747 Exprs, Importer.Import(ILE->getLBraceLoc())); 6748 To->setType(T); 6749 6750 if (ILE->hasArrayFiller()) { 6751 Expr *Filler = Importer.Import(ILE->getArrayFiller()); 6752 if (!Filler) 6753 return nullptr; 6754 To->setArrayFiller(Filler); 6755 } 6756 6757 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) { 6758 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD)); 6759 if (!ToFD) 6760 return nullptr; 6761 To->setInitializedFieldInUnion(ToFD); 6762 } 6763 6764 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) { 6765 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm)); 6766 if (!ToSyntForm) 6767 return nullptr; 6768 To->setSyntacticForm(ToSyntForm); 6769 } 6770 6771 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator()); 6772 To->setValueDependent(ILE->isValueDependent()); 6773 To->setInstantiationDependent(ILE->isInstantiationDependent()); 6774 6775 return To; 6776 } 6777 6778 Expr *ASTNodeImporter::VisitCXXStdInitializerListExpr( 6779 CXXStdInitializerListExpr *E) { 6780 QualType T = Importer.Import(E->getType()); 6781 if (T.isNull()) 6782 return nullptr; 6783 6784 Expr *SE = Importer.Import(E->getSubExpr()); 6785 if (!SE) 6786 return nullptr; 6787 6788 return new (Importer.getToContext()) CXXStdInitializerListExpr(T, SE); 6789 } 6790 6791 Expr *ASTNodeImporter::VisitCXXInheritedCtorInitExpr( 6792 CXXInheritedCtorInitExpr *E) { 6793 QualType T = Importer.Import(E->getType()); 6794 if (T.isNull()) 6795 return nullptr; 6796 6797 auto *Ctor = cast_or_null<CXXConstructorDecl>(Importer.Import( 6798 E->getConstructor())); 6799 if (!Ctor) 6800 return nullptr; 6801 6802 return new (Importer.getToContext()) CXXInheritedCtorInitExpr( 6803 Importer.Import(E->getLocation()), T, Ctor, 6804 E->constructsVBase(), E->inheritedFromVBase()); 6805 } 6806 6807 Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) { 6808 QualType ToType = Importer.Import(E->getType()); 6809 if (ToType.isNull()) 6810 return nullptr; 6811 6812 Expr *ToCommon = Importer.Import(E->getCommonExpr()); 6813 if (!ToCommon && E->getCommonExpr()) 6814 return nullptr; 6815 6816 Expr *ToSubExpr = Importer.Import(E->getSubExpr()); 6817 if (!ToSubExpr && E->getSubExpr()) 6818 return nullptr; 6819 6820 return new (Importer.getToContext()) 6821 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr); 6822 } 6823 6824 Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) { 6825 QualType ToType = Importer.Import(E->getType()); 6826 if (ToType.isNull()) 6827 return nullptr; 6828 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType); 6829 } 6830 6831 Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) { 6832 auto *ToField = dyn_cast_or_null<FieldDecl>(Importer.Import(DIE->getField())); 6833 if (!ToField && DIE->getField()) 6834 return nullptr; 6835 6836 return CXXDefaultInitExpr::Create( 6837 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField); 6838 } 6839 6840 Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { 6841 QualType ToType = Importer.Import(E->getType()); 6842 if (ToType.isNull() && !E->getType().isNull()) 6843 return nullptr; 6844 ExprValueKind VK = E->getValueKind(); 6845 CastKind CK = E->getCastKind(); 6846 Expr *ToOp = Importer.Import(E->getSubExpr()); 6847 if (!ToOp && E->getSubExpr()) 6848 return nullptr; 6849 CXXCastPath BasePath; 6850 if (ImportCastPath(E, BasePath)) 6851 return nullptr; 6852 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten()); 6853 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc()); 6854 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc()); 6855 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets()); 6856 6857 if (isa<CXXStaticCastExpr>(E)) { 6858 return CXXStaticCastExpr::Create( 6859 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, 6860 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); 6861 } else if (isa<CXXDynamicCastExpr>(E)) { 6862 return CXXDynamicCastExpr::Create( 6863 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, 6864 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); 6865 } else if (isa<CXXReinterpretCastExpr>(E)) { 6866 return CXXReinterpretCastExpr::Create( 6867 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, 6868 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); 6869 } else { 6870 return nullptr; 6871 } 6872 } 6873 6874 Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr( 6875 SubstNonTypeTemplateParmExpr *E) { 6876 QualType T = Importer.Import(E->getType()); 6877 if (T.isNull()) 6878 return nullptr; 6879 6880 auto *Param = cast_or_null<NonTypeTemplateParmDecl>( 6881 Importer.Import(E->getParameter())); 6882 if (!Param) 6883 return nullptr; 6884 6885 Expr *Replacement = Importer.Import(E->getReplacement()); 6886 if (!Replacement) 6887 return nullptr; 6888 6889 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr( 6890 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param, 6891 Replacement); 6892 } 6893 6894 Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) { 6895 QualType ToType = Importer.Import(E->getType()); 6896 if (ToType.isNull()) 6897 return nullptr; 6898 6899 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs()); 6900 if (ImportContainerChecked(E->getArgs(), ToArgs)) 6901 return nullptr; 6902 6903 // According to Sema::BuildTypeTrait(), if E is value-dependent, 6904 // Value is always false. 6905 bool ToValue = false; 6906 if (!E->isValueDependent()) 6907 ToValue = E->getValue(); 6908 6909 return TypeTraitExpr::Create( 6910 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()), 6911 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue); 6912 } 6913 6914 Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) { 6915 QualType ToType = Importer.Import(E->getType()); 6916 if (ToType.isNull()) 6917 return nullptr; 6918 6919 if (E->isTypeOperand()) { 6920 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo()); 6921 if (!TSI) 6922 return nullptr; 6923 6924 return new (Importer.getToContext()) 6925 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange())); 6926 } 6927 6928 Expr *Op = Importer.Import(E->getExprOperand()); 6929 if (!Op) 6930 return nullptr; 6931 6932 return new (Importer.getToContext()) 6933 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange())); 6934 } 6935 6936 void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod, 6937 CXXMethodDecl *FromMethod) { 6938 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) 6939 ToMethod->addOverriddenMethod( 6940 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>( 6941 FromOverriddenMethod)))); 6942 } 6943 6944 ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, 6945 ASTContext &FromContext, FileManager &FromFileManager, 6946 bool MinimalImport) 6947 : ToContext(ToContext), FromContext(FromContext), 6948 ToFileManager(ToFileManager), FromFileManager(FromFileManager), 6949 Minimal(MinimalImport) { 6950 ImportedDecls[FromContext.getTranslationUnitDecl()] 6951 = ToContext.getTranslationUnitDecl(); 6952 } 6953 6954 ASTImporter::~ASTImporter() = default; 6955 6956 QualType ASTImporter::Import(QualType FromT) { 6957 if (FromT.isNull()) 6958 return {}; 6959 6960 const Type *fromTy = FromT.getTypePtr(); 6961 6962 // Check whether we've already imported this type. 6963 llvm::DenseMap<const Type *, const Type *>::iterator Pos 6964 = ImportedTypes.find(fromTy); 6965 if (Pos != ImportedTypes.end()) 6966 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers()); 6967 6968 // Import the type 6969 ASTNodeImporter Importer(*this); 6970 QualType ToT = Importer.Visit(fromTy); 6971 if (ToT.isNull()) 6972 return ToT; 6973 6974 // Record the imported type. 6975 ImportedTypes[fromTy] = ToT.getTypePtr(); 6976 6977 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers()); 6978 } 6979 6980 TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { 6981 if (!FromTSI) 6982 return FromTSI; 6983 6984 // FIXME: For now we just create a "trivial" type source info based 6985 // on the type and a single location. Implement a real version of this. 6986 QualType T = Import(FromTSI->getType()); 6987 if (T.isNull()) 6988 return nullptr; 6989 6990 return ToContext.getTrivialTypeSourceInfo(T, 6991 Import(FromTSI->getTypeLoc().getLocStart())); 6992 } 6993 6994 Attr *ASTImporter::Import(const Attr *FromAttr) { 6995 Attr *ToAttr = FromAttr->clone(ToContext); 6996 ToAttr->setRange(Import(FromAttr->getRange())); 6997 return ToAttr; 6998 } 6999 7000 Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) { 7001 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); 7002 if (Pos != ImportedDecls.end()) { 7003 Decl *ToD = Pos->second; 7004 // FIXME: move this call to ImportDeclParts(). 7005 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD); 7006 return ToD; 7007 } else { 7008 return nullptr; 7009 } 7010 } 7011 7012 Decl *ASTImporter::Import(Decl *FromD) { 7013 if (!FromD) 7014 return nullptr; 7015 7016 ASTNodeImporter Importer(*this); 7017 7018 // Check whether we've already imported this declaration. 7019 Decl *ToD = GetAlreadyImportedOrNull(FromD); 7020 if (ToD) { 7021 // If FromD has some updated flags after last import, apply it 7022 updateFlags(FromD, ToD); 7023 return ToD; 7024 } 7025 7026 // Import the type. 7027 ToD = Importer.Visit(FromD); 7028 if (!ToD) 7029 return nullptr; 7030 7031 // Notify subclasses. 7032 Imported(FromD, ToD); 7033 7034 return ToD; 7035 } 7036 7037 DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) { 7038 if (!FromDC) 7039 return FromDC; 7040 7041 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC))); 7042 if (!ToDC) 7043 return nullptr; 7044 7045 // When we're using a record/enum/Objective-C class/protocol as a context, we 7046 // need it to have a definition. 7047 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) { 7048 auto *FromRecord = cast<RecordDecl>(FromDC); 7049 if (ToRecord->isCompleteDefinition()) { 7050 // Do nothing. 7051 } else if (FromRecord->isCompleteDefinition()) { 7052 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord, 7053 ASTNodeImporter::IDK_Basic); 7054 } else { 7055 CompleteDecl(ToRecord); 7056 } 7057 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) { 7058 auto *FromEnum = cast<EnumDecl>(FromDC); 7059 if (ToEnum->isCompleteDefinition()) { 7060 // Do nothing. 7061 } else if (FromEnum->isCompleteDefinition()) { 7062 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum, 7063 ASTNodeImporter::IDK_Basic); 7064 } else { 7065 CompleteDecl(ToEnum); 7066 } 7067 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) { 7068 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC); 7069 if (ToClass->getDefinition()) { 7070 // Do nothing. 7071 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { 7072 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass, 7073 ASTNodeImporter::IDK_Basic); 7074 } else { 7075 CompleteDecl(ToClass); 7076 } 7077 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) { 7078 auto *FromProto = cast<ObjCProtocolDecl>(FromDC); 7079 if (ToProto->getDefinition()) { 7080 // Do nothing. 7081 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { 7082 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto, 7083 ASTNodeImporter::IDK_Basic); 7084 } else { 7085 CompleteDecl(ToProto); 7086 } 7087 } 7088 7089 return ToDC; 7090 } 7091 7092 Expr *ASTImporter::Import(Expr *FromE) { 7093 if (!FromE) 7094 return nullptr; 7095 7096 return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); 7097 } 7098 7099 Stmt *ASTImporter::Import(Stmt *FromS) { 7100 if (!FromS) 7101 return nullptr; 7102 7103 // Check whether we've already imported this declaration. 7104 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); 7105 if (Pos != ImportedStmts.end()) 7106 return Pos->second; 7107 7108 // Import the type 7109 ASTNodeImporter Importer(*this); 7110 Stmt *ToS = Importer.Visit(FromS); 7111 if (!ToS) 7112 return nullptr; 7113 7114 // Record the imported declaration. 7115 ImportedStmts[FromS] = ToS; 7116 return ToS; 7117 } 7118 7119 NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { 7120 if (!FromNNS) 7121 return nullptr; 7122 7123 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix()); 7124 7125 switch (FromNNS->getKind()) { 7126 case NestedNameSpecifier::Identifier: 7127 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) { 7128 return NestedNameSpecifier::Create(ToContext, prefix, II); 7129 } 7130 return nullptr; 7131 7132 case NestedNameSpecifier::Namespace: 7133 if (auto *NS = 7134 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) { 7135 return NestedNameSpecifier::Create(ToContext, prefix, NS); 7136 } 7137 return nullptr; 7138 7139 case NestedNameSpecifier::NamespaceAlias: 7140 if (auto *NSAD = 7141 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) { 7142 return NestedNameSpecifier::Create(ToContext, prefix, NSAD); 7143 } 7144 return nullptr; 7145 7146 case NestedNameSpecifier::Global: 7147 return NestedNameSpecifier::GlobalSpecifier(ToContext); 7148 7149 case NestedNameSpecifier::Super: 7150 if (auto *RD = 7151 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) { 7152 return NestedNameSpecifier::SuperSpecifier(ToContext, RD); 7153 } 7154 return nullptr; 7155 7156 case NestedNameSpecifier::TypeSpec: 7157 case NestedNameSpecifier::TypeSpecWithTemplate: { 7158 QualType T = Import(QualType(FromNNS->getAsType(), 0u)); 7159 if (!T.isNull()) { 7160 bool bTemplate = FromNNS->getKind() == 7161 NestedNameSpecifier::TypeSpecWithTemplate; 7162 return NestedNameSpecifier::Create(ToContext, prefix, 7163 bTemplate, T.getTypePtr()); 7164 } 7165 } 7166 return nullptr; 7167 } 7168 7169 llvm_unreachable("Invalid nested name specifier kind"); 7170 } 7171 7172 NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { 7173 // Copied from NestedNameSpecifier mostly. 7174 SmallVector<NestedNameSpecifierLoc , 8> NestedNames; 7175 NestedNameSpecifierLoc NNS = FromNNS; 7176 7177 // Push each of the nested-name-specifiers's onto a stack for 7178 // serialization in reverse order. 7179 while (NNS) { 7180 NestedNames.push_back(NNS); 7181 NNS = NNS.getPrefix(); 7182 } 7183 7184 NestedNameSpecifierLocBuilder Builder; 7185 7186 while (!NestedNames.empty()) { 7187 NNS = NestedNames.pop_back_val(); 7188 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier()); 7189 if (!Spec) 7190 return NestedNameSpecifierLoc(); 7191 7192 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind(); 7193 switch (Kind) { 7194 case NestedNameSpecifier::Identifier: 7195 Builder.Extend(getToContext(), 7196 Spec->getAsIdentifier(), 7197 Import(NNS.getLocalBeginLoc()), 7198 Import(NNS.getLocalEndLoc())); 7199 break; 7200 7201 case NestedNameSpecifier::Namespace: 7202 Builder.Extend(getToContext(), 7203 Spec->getAsNamespace(), 7204 Import(NNS.getLocalBeginLoc()), 7205 Import(NNS.getLocalEndLoc())); 7206 break; 7207 7208 case NestedNameSpecifier::NamespaceAlias: 7209 Builder.Extend(getToContext(), 7210 Spec->getAsNamespaceAlias(), 7211 Import(NNS.getLocalBeginLoc()), 7212 Import(NNS.getLocalEndLoc())); 7213 break; 7214 7215 case NestedNameSpecifier::TypeSpec: 7216 case NestedNameSpecifier::TypeSpecWithTemplate: { 7217 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo( 7218 QualType(Spec->getAsType(), 0)); 7219 Builder.Extend(getToContext(), 7220 Import(NNS.getLocalBeginLoc()), 7221 TSI->getTypeLoc(), 7222 Import(NNS.getLocalEndLoc())); 7223 break; 7224 } 7225 7226 case NestedNameSpecifier::Global: 7227 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc())); 7228 break; 7229 7230 case NestedNameSpecifier::Super: { 7231 SourceRange ToRange = Import(NNS.getSourceRange()); 7232 Builder.MakeSuper(getToContext(), 7233 Spec->getAsRecordDecl(), 7234 ToRange.getBegin(), 7235 ToRange.getEnd()); 7236 } 7237 } 7238 } 7239 7240 return Builder.getWithLocInContext(getToContext()); 7241 } 7242 7243 TemplateName ASTImporter::Import(TemplateName From) { 7244 switch (From.getKind()) { 7245 case TemplateName::Template: 7246 if (auto *ToTemplate = 7247 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) 7248 return TemplateName(ToTemplate); 7249 7250 return {}; 7251 7252 case TemplateName::OverloadedTemplate: { 7253 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); 7254 UnresolvedSet<2> ToTemplates; 7255 for (auto *I : *FromStorage) { 7256 if (auto *To = cast_or_null<NamedDecl>(Import(I))) 7257 ToTemplates.addDecl(To); 7258 else 7259 return {}; 7260 } 7261 return ToContext.getOverloadedTemplateName(ToTemplates.begin(), 7262 ToTemplates.end()); 7263 } 7264 7265 case TemplateName::QualifiedTemplate: { 7266 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); 7267 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier()); 7268 if (!Qualifier) 7269 return {}; 7270 7271 if (auto *ToTemplate = 7272 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) 7273 return ToContext.getQualifiedTemplateName(Qualifier, 7274 QTN->hasTemplateKeyword(), 7275 ToTemplate); 7276 7277 return {}; 7278 } 7279 7280 case TemplateName::DependentTemplate: { 7281 DependentTemplateName *DTN = From.getAsDependentTemplateName(); 7282 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier()); 7283 if (!Qualifier) 7284 return {}; 7285 7286 if (DTN->isIdentifier()) { 7287 return ToContext.getDependentTemplateName(Qualifier, 7288 Import(DTN->getIdentifier())); 7289 } 7290 7291 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator()); 7292 } 7293 7294 case TemplateName::SubstTemplateTemplateParm: { 7295 SubstTemplateTemplateParmStorage *subst 7296 = From.getAsSubstTemplateTemplateParm(); 7297 auto *param = 7298 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter())); 7299 if (!param) 7300 return {}; 7301 7302 TemplateName replacement = Import(subst->getReplacement()); 7303 if (replacement.isNull()) 7304 return {}; 7305 7306 return ToContext.getSubstTemplateTemplateParm(param, replacement); 7307 } 7308 7309 case TemplateName::SubstTemplateTemplateParmPack: { 7310 SubstTemplateTemplateParmPackStorage *SubstPack 7311 = From.getAsSubstTemplateTemplateParmPack(); 7312 auto *Param = 7313 cast_or_null<TemplateTemplateParmDecl>( 7314 Import(SubstPack->getParameterPack())); 7315 if (!Param) 7316 return {}; 7317 7318 ASTNodeImporter Importer(*this); 7319 TemplateArgument ArgPack 7320 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); 7321 if (ArgPack.isNull()) 7322 return {}; 7323 7324 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack); 7325 } 7326 } 7327 7328 llvm_unreachable("Invalid template name kind"); 7329 } 7330 7331 SourceLocation ASTImporter::Import(SourceLocation FromLoc) { 7332 if (FromLoc.isInvalid()) 7333 return {}; 7334 7335 SourceManager &FromSM = FromContext.getSourceManager(); 7336 7337 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); 7338 FileID ToFileID = Import(Decomposed.first); 7339 if (ToFileID.isInvalid()) 7340 return {}; 7341 SourceManager &ToSM = ToContext.getSourceManager(); 7342 return ToSM.getComposedLoc(ToFileID, Decomposed.second); 7343 } 7344 7345 SourceRange ASTImporter::Import(SourceRange FromRange) { 7346 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); 7347 } 7348 7349 FileID ASTImporter::Import(FileID FromID) { 7350 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID); 7351 if (Pos != ImportedFileIDs.end()) 7352 return Pos->second; 7353 7354 SourceManager &FromSM = FromContext.getSourceManager(); 7355 SourceManager &ToSM = ToContext.getSourceManager(); 7356 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); 7357 7358 // Map the FromID to the "to" source manager. 7359 FileID ToID; 7360 if (FromSLoc.isExpansion()) { 7361 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion(); 7362 SourceLocation ToSpLoc = Import(FromEx.getSpellingLoc()); 7363 SourceLocation ToExLocS = Import(FromEx.getExpansionLocStart()); 7364 unsigned TokenLen = FromSM.getFileIDSize(FromID); 7365 SourceLocation MLoc; 7366 if (FromEx.isMacroArgExpansion()) { 7367 MLoc = ToSM.createMacroArgExpansionLoc(ToSpLoc, ToExLocS, TokenLen); 7368 } else { 7369 SourceLocation ToExLocE = Import(FromEx.getExpansionLocEnd()); 7370 MLoc = ToSM.createExpansionLoc(ToSpLoc, ToExLocS, ToExLocE, TokenLen, 7371 FromEx.isExpansionTokenRange()); 7372 } 7373 ToID = ToSM.getFileID(MLoc); 7374 } else { 7375 // Include location of this file. 7376 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); 7377 7378 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); 7379 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { 7380 // FIXME: We probably want to use getVirtualFile(), so we don't hit the 7381 // disk again 7382 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather 7383 // than mmap the files several times. 7384 const FileEntry *Entry = 7385 ToFileManager.getFile(Cache->OrigEntry->getName()); 7386 if (!Entry) 7387 return {}; 7388 ToID = ToSM.createFileID(Entry, ToIncludeLoc, 7389 FromSLoc.getFile().getFileCharacteristic()); 7390 } else { 7391 // FIXME: We want to re-use the existing MemoryBuffer! 7392 const llvm::MemoryBuffer *FromBuf = 7393 Cache->getBuffer(FromContext.getDiagnostics(), FromSM); 7394 std::unique_ptr<llvm::MemoryBuffer> ToBuf = 7395 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(), 7396 FromBuf->getBufferIdentifier()); 7397 ToID = ToSM.createFileID(std::move(ToBuf), 7398 FromSLoc.getFile().getFileCharacteristic()); 7399 } 7400 } 7401 7402 ImportedFileIDs[FromID] = ToID; 7403 return ToID; 7404 } 7405 7406 CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) { 7407 Expr *ToExpr = Import(From->getInit()); 7408 if (!ToExpr && From->getInit()) 7409 return nullptr; 7410 7411 if (From->isBaseInitializer()) { 7412 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo()); 7413 if (!ToTInfo && From->getTypeSourceInfo()) 7414 return nullptr; 7415 7416 return new (ToContext) CXXCtorInitializer( 7417 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()), 7418 ToExpr, Import(From->getRParenLoc()), 7419 From->isPackExpansion() ? Import(From->getEllipsisLoc()) 7420 : SourceLocation()); 7421 } else if (From->isMemberInitializer()) { 7422 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember())); 7423 if (!ToField && From->getMember()) 7424 return nullptr; 7425 7426 return new (ToContext) CXXCtorInitializer( 7427 ToContext, ToField, Import(From->getMemberLocation()), 7428 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc())); 7429 } else if (From->isIndirectMemberInitializer()) { 7430 auto *ToIField = cast_or_null<IndirectFieldDecl>( 7431 Import(From->getIndirectMember())); 7432 if (!ToIField && From->getIndirectMember()) 7433 return nullptr; 7434 7435 return new (ToContext) CXXCtorInitializer( 7436 ToContext, ToIField, Import(From->getMemberLocation()), 7437 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc())); 7438 } else if (From->isDelegatingInitializer()) { 7439 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo()); 7440 if (!ToTInfo && From->getTypeSourceInfo()) 7441 return nullptr; 7442 7443 return new (ToContext) 7444 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()), 7445 ToExpr, Import(From->getRParenLoc())); 7446 } else { 7447 return nullptr; 7448 } 7449 } 7450 7451 CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) { 7452 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec); 7453 if (Pos != ImportedCXXBaseSpecifiers.end()) 7454 return Pos->second; 7455 7456 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier( 7457 Import(BaseSpec->getSourceRange()), 7458 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(), 7459 BaseSpec->getAccessSpecifierAsWritten(), 7460 Import(BaseSpec->getTypeSourceInfo()), 7461 Import(BaseSpec->getEllipsisLoc())); 7462 ImportedCXXBaseSpecifiers[BaseSpec] = Imported; 7463 return Imported; 7464 } 7465 7466 void ASTImporter::ImportDefinition(Decl *From) { 7467 Decl *To = Import(From); 7468 if (!To) 7469 return; 7470 7471 if (auto *FromDC = cast<DeclContext>(From)) { 7472 ASTNodeImporter Importer(*this); 7473 7474 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) { 7475 if (!ToRecord->getDefinition()) { 7476 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord, 7477 ASTNodeImporter::IDK_Everything); 7478 return; 7479 } 7480 } 7481 7482 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) { 7483 if (!ToEnum->getDefinition()) { 7484 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum, 7485 ASTNodeImporter::IDK_Everything); 7486 return; 7487 } 7488 } 7489 7490 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) { 7491 if (!ToIFace->getDefinition()) { 7492 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace, 7493 ASTNodeImporter::IDK_Everything); 7494 return; 7495 } 7496 } 7497 7498 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) { 7499 if (!ToProto->getDefinition()) { 7500 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto, 7501 ASTNodeImporter::IDK_Everything); 7502 return; 7503 } 7504 } 7505 7506 Importer.ImportDeclContext(FromDC, true); 7507 } 7508 } 7509 7510 DeclarationName ASTImporter::Import(DeclarationName FromName) { 7511 if (!FromName) 7512 return {}; 7513 7514 switch (FromName.getNameKind()) { 7515 case DeclarationName::Identifier: 7516 return Import(FromName.getAsIdentifierInfo()); 7517 7518 case DeclarationName::ObjCZeroArgSelector: 7519 case DeclarationName::ObjCOneArgSelector: 7520 case DeclarationName::ObjCMultiArgSelector: 7521 return Import(FromName.getObjCSelector()); 7522 7523 case DeclarationName::CXXConstructorName: { 7524 QualType T = Import(FromName.getCXXNameType()); 7525 if (T.isNull()) 7526 return {}; 7527 7528 return ToContext.DeclarationNames.getCXXConstructorName( 7529 ToContext.getCanonicalType(T)); 7530 } 7531 7532 case DeclarationName::CXXDestructorName: { 7533 QualType T = Import(FromName.getCXXNameType()); 7534 if (T.isNull()) 7535 return {}; 7536 7537 return ToContext.DeclarationNames.getCXXDestructorName( 7538 ToContext.getCanonicalType(T)); 7539 } 7540 7541 case DeclarationName::CXXDeductionGuideName: { 7542 auto *Template = cast_or_null<TemplateDecl>( 7543 Import(FromName.getCXXDeductionGuideTemplate())); 7544 if (!Template) 7545 return {}; 7546 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template); 7547 } 7548 7549 case DeclarationName::CXXConversionFunctionName: { 7550 QualType T = Import(FromName.getCXXNameType()); 7551 if (T.isNull()) 7552 return {}; 7553 7554 return ToContext.DeclarationNames.getCXXConversionFunctionName( 7555 ToContext.getCanonicalType(T)); 7556 } 7557 7558 case DeclarationName::CXXOperatorName: 7559 return ToContext.DeclarationNames.getCXXOperatorName( 7560 FromName.getCXXOverloadedOperator()); 7561 7562 case DeclarationName::CXXLiteralOperatorName: 7563 return ToContext.DeclarationNames.getCXXLiteralOperatorName( 7564 Import(FromName.getCXXLiteralIdentifier())); 7565 7566 case DeclarationName::CXXUsingDirective: 7567 // FIXME: STATICS! 7568 return DeclarationName::getUsingDirectiveName(); 7569 } 7570 7571 llvm_unreachable("Invalid DeclarationName Kind!"); 7572 } 7573 7574 IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { 7575 if (!FromId) 7576 return nullptr; 7577 7578 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName()); 7579 7580 if (!ToId->getBuiltinID() && FromId->getBuiltinID()) 7581 ToId->setBuiltinID(FromId->getBuiltinID()); 7582 7583 return ToId; 7584 } 7585 7586 Selector ASTImporter::Import(Selector FromSel) { 7587 if (FromSel.isNull()) 7588 return {}; 7589 7590 SmallVector<IdentifierInfo *, 4> Idents; 7591 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); 7592 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) 7593 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); 7594 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); 7595 } 7596 7597 DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name, 7598 DeclContext *DC, 7599 unsigned IDNS, 7600 NamedDecl **Decls, 7601 unsigned NumDecls) { 7602 return Name; 7603 } 7604 7605 DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { 7606 if (LastDiagFromFrom) 7607 ToContext.getDiagnostics().notePriorDiagnosticFrom( 7608 FromContext.getDiagnostics()); 7609 LastDiagFromFrom = false; 7610 return ToContext.getDiagnostics().Report(Loc, DiagID); 7611 } 7612 7613 DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { 7614 if (!LastDiagFromFrom) 7615 FromContext.getDiagnostics().notePriorDiagnosticFrom( 7616 ToContext.getDiagnostics()); 7617 LastDiagFromFrom = true; 7618 return FromContext.getDiagnostics().Report(Loc, DiagID); 7619 } 7620 7621 void ASTImporter::CompleteDecl (Decl *D) { 7622 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) { 7623 if (!ID->getDefinition()) 7624 ID->startDefinition(); 7625 } 7626 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) { 7627 if (!PD->getDefinition()) 7628 PD->startDefinition(); 7629 } 7630 else if (auto *TD = dyn_cast<TagDecl>(D)) { 7631 if (!TD->getDefinition() && !TD->isBeingDefined()) { 7632 TD->startDefinition(); 7633 TD->setCompleteDefinition(true); 7634 } 7635 } 7636 else { 7637 assert(0 && "CompleteDecl called on a Decl that can't be completed"); 7638 } 7639 } 7640 7641 Decl *ASTImporter::MapImported(Decl *From, Decl *To) { 7642 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From); 7643 assert((Pos == ImportedDecls.end() || Pos->second == To) && 7644 "Try to import an already imported Decl"); 7645 if (Pos != ImportedDecls.end()) 7646 return Pos->second; 7647 ImportedDecls[From] = To; 7648 return To; 7649 } 7650 7651 bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, 7652 bool Complain) { 7653 llvm::DenseMap<const Type *, const Type *>::iterator Pos 7654 = ImportedTypes.find(From.getTypePtr()); 7655 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To)) 7656 return true; 7657 7658 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls, 7659 getStructuralEquivalenceKind(*this), false, 7660 Complain); 7661 return Ctx.IsEquivalent(From, To); 7662 } 7663