1 //===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the ASTImporter class which imports AST nodes from one 11 // context into another context. 12 // 13 //===----------------------------------------------------------------------===// 14 #include "clang/AST/ASTImporter.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/ASTDiagnostic.h" 17 #include "clang/AST/DeclCXX.h" 18 #include "clang/AST/DeclObjC.h" 19 #include "clang/AST/DeclVisitor.h" 20 #include "clang/AST/StmtVisitor.h" 21 #include "clang/AST/TypeVisitor.h" 22 #include "clang/Basic/FileManager.h" 23 #include "clang/Basic/SourceManager.h" 24 #include "llvm/Support/MemoryBuffer.h" 25 #include <deque> 26 27 namespace clang { 28 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>, 29 public DeclVisitor<ASTNodeImporter, Decl *>, 30 public StmtVisitor<ASTNodeImporter, Stmt *> { 31 ASTImporter &Importer; 32 33 public: 34 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { } 35 36 using TypeVisitor<ASTNodeImporter, QualType>::Visit; 37 using DeclVisitor<ASTNodeImporter, Decl *>::Visit; 38 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit; 39 40 // Importing types 41 QualType VisitType(const Type *T); 42 QualType VisitBuiltinType(const BuiltinType *T); 43 QualType VisitComplexType(const ComplexType *T); 44 QualType VisitPointerType(const PointerType *T); 45 QualType VisitBlockPointerType(const BlockPointerType *T); 46 QualType VisitLValueReferenceType(const LValueReferenceType *T); 47 QualType VisitRValueReferenceType(const RValueReferenceType *T); 48 QualType VisitMemberPointerType(const MemberPointerType *T); 49 QualType VisitConstantArrayType(const ConstantArrayType *T); 50 QualType VisitIncompleteArrayType(const IncompleteArrayType *T); 51 QualType VisitVariableArrayType(const VariableArrayType *T); 52 // FIXME: DependentSizedArrayType 53 // FIXME: DependentSizedExtVectorType 54 QualType VisitVectorType(const VectorType *T); 55 QualType VisitExtVectorType(const ExtVectorType *T); 56 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T); 57 QualType VisitFunctionProtoType(const FunctionProtoType *T); 58 // FIXME: UnresolvedUsingType 59 QualType VisitParenType(const ParenType *T); 60 QualType VisitTypedefType(const TypedefType *T); 61 QualType VisitTypeOfExprType(const TypeOfExprType *T); 62 // FIXME: DependentTypeOfExprType 63 QualType VisitTypeOfType(const TypeOfType *T); 64 QualType VisitDecltypeType(const DecltypeType *T); 65 QualType VisitUnaryTransformType(const UnaryTransformType *T); 66 QualType VisitAutoType(const AutoType *T); 67 // FIXME: DependentDecltypeType 68 QualType VisitRecordType(const RecordType *T); 69 QualType VisitEnumType(const EnumType *T); 70 QualType VisitAttributedType(const AttributedType *T); 71 // FIXME: TemplateTypeParmType 72 // FIXME: SubstTemplateTypeParmType 73 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T); 74 QualType VisitElaboratedType(const ElaboratedType *T); 75 // FIXME: DependentNameType 76 // FIXME: DependentTemplateSpecializationType 77 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T); 78 QualType VisitObjCObjectType(const ObjCObjectType *T); 79 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T); 80 81 // Importing declarations 82 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC, 83 DeclContext *&LexicalDC, DeclarationName &Name, 84 NamedDecl *&ToD, SourceLocation &Loc); 85 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); 86 void ImportDeclarationNameLoc(const DeclarationNameInfo &From, 87 DeclarationNameInfo& To); 88 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); 89 90 /// \brief What we should import from the definition. 91 enum ImportDefinitionKind { 92 /// \brief Import the default subset of the definition, which might be 93 /// nothing (if minimal import is set) or might be everything (if minimal 94 /// import is not set). 95 IDK_Default, 96 /// \brief Import everything. 97 IDK_Everything, 98 /// \brief Import only the bare bones needed to establish a valid 99 /// DeclContext. 100 IDK_Basic 101 }; 102 103 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { 104 return IDK == IDK_Everything || 105 (IDK == IDK_Default && !Importer.isMinimalImport()); 106 } 107 108 bool ImportDefinition(RecordDecl *From, RecordDecl *To, 109 ImportDefinitionKind Kind = IDK_Default); 110 bool ImportDefinition(VarDecl *From, VarDecl *To, 111 ImportDefinitionKind Kind = IDK_Default); 112 bool ImportDefinition(EnumDecl *From, EnumDecl *To, 113 ImportDefinitionKind Kind = IDK_Default); 114 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, 115 ImportDefinitionKind Kind = IDK_Default); 116 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To, 117 ImportDefinitionKind Kind = IDK_Default); 118 TemplateParameterList *ImportTemplateParameterList( 119 TemplateParameterList *Params); 120 TemplateArgument ImportTemplateArgument(const TemplateArgument &From); 121 bool ImportTemplateArguments(const TemplateArgument *FromArgs, 122 unsigned NumFromArgs, 123 SmallVectorImpl<TemplateArgument> &ToArgs); 124 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord, 125 bool Complain = true); 126 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, 127 bool Complain = true); 128 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord); 129 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC); 130 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To); 131 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To); 132 Decl *VisitDecl(Decl *D); 133 Decl *VisitAccessSpecDecl(AccessSpecDecl *D); 134 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); 135 Decl *VisitNamespaceDecl(NamespaceDecl *D); 136 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); 137 Decl *VisitTypedefDecl(TypedefDecl *D); 138 Decl *VisitTypeAliasDecl(TypeAliasDecl *D); 139 Decl *VisitEnumDecl(EnumDecl *D); 140 Decl *VisitRecordDecl(RecordDecl *D); 141 Decl *VisitEnumConstantDecl(EnumConstantDecl *D); 142 Decl *VisitFunctionDecl(FunctionDecl *D); 143 Decl *VisitCXXMethodDecl(CXXMethodDecl *D); 144 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); 145 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); 146 Decl *VisitCXXConversionDecl(CXXConversionDecl *D); 147 Decl *VisitFieldDecl(FieldDecl *D); 148 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D); 149 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D); 150 Decl *VisitVarDecl(VarDecl *D); 151 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D); 152 Decl *VisitParmVarDecl(ParmVarDecl *D); 153 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D); 154 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); 155 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D); 156 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D); 157 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D); 158 159 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list); 160 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); 161 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); 162 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D); 163 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D); 164 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); 165 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); 166 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); 167 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); 168 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); 169 Decl *VisitClassTemplateSpecializationDecl( 170 ClassTemplateSpecializationDecl *D); 171 Decl *VisitVarTemplateDecl(VarTemplateDecl *D); 172 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); 173 174 // Importing statements 175 DeclGroupRef ImportDeclGroup(DeclGroupRef DG); 176 177 Stmt *VisitStmt(Stmt *S); 178 Stmt *VisitDeclStmt(DeclStmt *S); 179 Stmt *VisitNullStmt(NullStmt *S); 180 Stmt *VisitCompoundStmt(CompoundStmt *S); 181 Stmt *VisitCaseStmt(CaseStmt *S); 182 Stmt *VisitDefaultStmt(DefaultStmt *S); 183 Stmt *VisitLabelStmt(LabelStmt *S); 184 Stmt *VisitAttributedStmt(AttributedStmt *S); 185 Stmt *VisitIfStmt(IfStmt *S); 186 Stmt *VisitSwitchStmt(SwitchStmt *S); 187 Stmt *VisitWhileStmt(WhileStmt *S); 188 Stmt *VisitDoStmt(DoStmt *S); 189 Stmt *VisitForStmt(ForStmt *S); 190 Stmt *VisitGotoStmt(GotoStmt *S); 191 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S); 192 Stmt *VisitContinueStmt(ContinueStmt *S); 193 Stmt *VisitBreakStmt(BreakStmt *S); 194 Stmt *VisitReturnStmt(ReturnStmt *S); 195 // FIXME: GCCAsmStmt 196 // FIXME: MSAsmStmt 197 // FIXME: SEHExceptStmt 198 // FIXME: SEHFinallyStmt 199 // FIXME: SEHTryStmt 200 // FIXME: SEHLeaveStmt 201 // FIXME: CapturedStmt 202 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S); 203 Stmt *VisitCXXTryStmt(CXXTryStmt *S); 204 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S); 205 // FIXME: MSDependentExistsStmt 206 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); 207 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); 208 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S); 209 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S); 210 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); 211 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); 212 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); 213 214 // Importing expressions 215 Expr *VisitExpr(Expr *E); 216 Expr *VisitDeclRefExpr(DeclRefExpr *E); 217 Expr *VisitIntegerLiteral(IntegerLiteral *E); 218 Expr *VisitCharacterLiteral(CharacterLiteral *E); 219 Expr *VisitParenExpr(ParenExpr *E); 220 Expr *VisitUnaryOperator(UnaryOperator *E); 221 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); 222 Expr *VisitBinaryOperator(BinaryOperator *E); 223 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E); 224 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E); 225 Expr *VisitCStyleCastExpr(CStyleCastExpr *E); 226 Expr *VisitCXXConstructExpr(CXXConstructExpr *E); 227 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E); 228 Expr *VisitCXXThisExpr(CXXThisExpr *E); 229 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); 230 Expr *VisitMemberExpr(MemberExpr *E); 231 Expr *VisitCallExpr(CallExpr *E); 232 Expr *VisitInitListExpr(InitListExpr *E); 233 234 template <typename T, typename Iter> bool ImportArray(Iter B, Iter E, llvm::ArrayRef<T*> &ToArray) { 235 size_t NumElements = E - B; 236 SmallVector<T *, 1> ImportedElements(NumElements); 237 ASTImporter &_Importer = Importer; 238 239 bool Failed = false; 240 std::transform(B, E, ImportedElements.begin(), 241 [&_Importer, &Failed](T *Element) -> T* { 242 T *ToElement = _Importer.Import(Element); 243 if (Element && !ToElement) 244 Failed = true; 245 return ToElement; 246 }); 247 248 if (Failed) 249 return false; 250 251 T **CopiedElements = new (Importer.getToContext()) T*[NumElements]; 252 std::copy(ImportedElements.begin(), ImportedElements.end(), &CopiedElements[0]); 253 ToArray = llvm::ArrayRef<T*>(CopiedElements, NumElements); 254 255 return true; 256 } 257 }; 258 } 259 using namespace clang; 260 261 //---------------------------------------------------------------------------- 262 // Structural Equivalence 263 //---------------------------------------------------------------------------- 264 265 namespace { 266 struct StructuralEquivalenceContext { 267 /// \brief AST contexts for which we are checking structural equivalence. 268 ASTContext &C1, &C2; 269 270 /// \brief The set of "tentative" equivalences between two canonical 271 /// declarations, mapping from a declaration in the first context to the 272 /// declaration in the second context that we believe to be equivalent. 273 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences; 274 275 /// \brief Queue of declarations in the first context whose equivalence 276 /// with a declaration in the second context still needs to be verified. 277 std::deque<Decl *> DeclsToCheck; 278 279 /// \brief Declaration (from, to) pairs that are known not to be equivalent 280 /// (which we have already complained about). 281 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls; 282 283 /// \brief Whether we're being strict about the spelling of types when 284 /// unifying two types. 285 bool StrictTypeSpelling; 286 287 /// \brief Whether to complain about failures. 288 bool Complain; 289 290 /// \brief \c true if the last diagnostic came from C2. 291 bool LastDiagFromC2; 292 293 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2, 294 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls, 295 bool StrictTypeSpelling = false, 296 bool Complain = true) 297 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls), 298 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain), 299 LastDiagFromC2(false) {} 300 301 /// \brief Determine whether the two declarations are structurally 302 /// equivalent. 303 bool IsStructurallyEquivalent(Decl *D1, Decl *D2); 304 305 /// \brief Determine whether the two types are structurally equivalent. 306 bool IsStructurallyEquivalent(QualType T1, QualType T2); 307 308 private: 309 /// \brief Finish checking all of the structural equivalences. 310 /// 311 /// \returns true if an error occurred, false otherwise. 312 bool Finish(); 313 314 public: 315 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) { 316 assert(Complain && "Not allowed to complain"); 317 if (LastDiagFromC2) 318 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics()); 319 LastDiagFromC2 = false; 320 return C1.getDiagnostics().Report(Loc, DiagID); 321 } 322 323 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) { 324 assert(Complain && "Not allowed to complain"); 325 if (!LastDiagFromC2) 326 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics()); 327 LastDiagFromC2 = true; 328 return C2.getDiagnostics().Report(Loc, DiagID); 329 } 330 }; 331 } 332 333 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 334 QualType T1, QualType T2); 335 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 336 Decl *D1, Decl *D2); 337 338 /// \brief Determine structural equivalence of two expressions. 339 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 340 Expr *E1, Expr *E2) { 341 if (!E1 || !E2) 342 return E1 == E2; 343 344 // FIXME: Actually perform a structural comparison! 345 return true; 346 } 347 348 /// \brief Determine whether two identifiers are equivalent. 349 static bool IsStructurallyEquivalent(const IdentifierInfo *Name1, 350 const IdentifierInfo *Name2) { 351 if (!Name1 || !Name2) 352 return Name1 == Name2; 353 354 return Name1->getName() == Name2->getName(); 355 } 356 357 /// \brief Determine whether two nested-name-specifiers are equivalent. 358 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 359 NestedNameSpecifier *NNS1, 360 NestedNameSpecifier *NNS2) { 361 // FIXME: Implement! 362 return true; 363 } 364 365 /// \brief Determine whether two template arguments are equivalent. 366 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 367 const TemplateArgument &Arg1, 368 const TemplateArgument &Arg2) { 369 if (Arg1.getKind() != Arg2.getKind()) 370 return false; 371 372 switch (Arg1.getKind()) { 373 case TemplateArgument::Null: 374 return true; 375 376 case TemplateArgument::Type: 377 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType()); 378 379 case TemplateArgument::Integral: 380 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(), 381 Arg2.getIntegralType())) 382 return false; 383 384 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral()); 385 386 case TemplateArgument::Declaration: 387 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl()); 388 389 case TemplateArgument::NullPtr: 390 return true; // FIXME: Is this correct? 391 392 case TemplateArgument::Template: 393 return IsStructurallyEquivalent(Context, 394 Arg1.getAsTemplate(), 395 Arg2.getAsTemplate()); 396 397 case TemplateArgument::TemplateExpansion: 398 return IsStructurallyEquivalent(Context, 399 Arg1.getAsTemplateOrTemplatePattern(), 400 Arg2.getAsTemplateOrTemplatePattern()); 401 402 case TemplateArgument::Expression: 403 return IsStructurallyEquivalent(Context, 404 Arg1.getAsExpr(), Arg2.getAsExpr()); 405 406 case TemplateArgument::Pack: 407 if (Arg1.pack_size() != Arg2.pack_size()) 408 return false; 409 410 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I) 411 if (!IsStructurallyEquivalent(Context, 412 Arg1.pack_begin()[I], 413 Arg2.pack_begin()[I])) 414 return false; 415 416 return true; 417 } 418 419 llvm_unreachable("Invalid template argument kind"); 420 } 421 422 /// \brief Determine structural equivalence for the common part of array 423 /// types. 424 static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context, 425 const ArrayType *Array1, 426 const ArrayType *Array2) { 427 if (!IsStructurallyEquivalent(Context, 428 Array1->getElementType(), 429 Array2->getElementType())) 430 return false; 431 if (Array1->getSizeModifier() != Array2->getSizeModifier()) 432 return false; 433 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers()) 434 return false; 435 436 return true; 437 } 438 439 /// \brief Determine structural equivalence of two types. 440 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 441 QualType T1, QualType T2) { 442 if (T1.isNull() || T2.isNull()) 443 return T1.isNull() && T2.isNull(); 444 445 if (!Context.StrictTypeSpelling) { 446 // We aren't being strict about token-to-token equivalence of types, 447 // so map down to the canonical type. 448 T1 = Context.C1.getCanonicalType(T1); 449 T2 = Context.C2.getCanonicalType(T2); 450 } 451 452 if (T1.getQualifiers() != T2.getQualifiers()) 453 return false; 454 455 Type::TypeClass TC = T1->getTypeClass(); 456 457 if (T1->getTypeClass() != T2->getTypeClass()) { 458 // Compare function types with prototypes vs. without prototypes as if 459 // both did not have prototypes. 460 if (T1->getTypeClass() == Type::FunctionProto && 461 T2->getTypeClass() == Type::FunctionNoProto) 462 TC = Type::FunctionNoProto; 463 else if (T1->getTypeClass() == Type::FunctionNoProto && 464 T2->getTypeClass() == Type::FunctionProto) 465 TC = Type::FunctionNoProto; 466 else 467 return false; 468 } 469 470 switch (TC) { 471 case Type::Builtin: 472 // FIXME: Deal with Char_S/Char_U. 473 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind()) 474 return false; 475 break; 476 477 case Type::Complex: 478 if (!IsStructurallyEquivalent(Context, 479 cast<ComplexType>(T1)->getElementType(), 480 cast<ComplexType>(T2)->getElementType())) 481 return false; 482 break; 483 484 case Type::Adjusted: 485 case Type::Decayed: 486 if (!IsStructurallyEquivalent(Context, 487 cast<AdjustedType>(T1)->getOriginalType(), 488 cast<AdjustedType>(T2)->getOriginalType())) 489 return false; 490 break; 491 492 case Type::Pointer: 493 if (!IsStructurallyEquivalent(Context, 494 cast<PointerType>(T1)->getPointeeType(), 495 cast<PointerType>(T2)->getPointeeType())) 496 return false; 497 break; 498 499 case Type::BlockPointer: 500 if (!IsStructurallyEquivalent(Context, 501 cast<BlockPointerType>(T1)->getPointeeType(), 502 cast<BlockPointerType>(T2)->getPointeeType())) 503 return false; 504 break; 505 506 case Type::LValueReference: 507 case Type::RValueReference: { 508 const ReferenceType *Ref1 = cast<ReferenceType>(T1); 509 const ReferenceType *Ref2 = cast<ReferenceType>(T2); 510 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue()) 511 return false; 512 if (Ref1->isInnerRef() != Ref2->isInnerRef()) 513 return false; 514 if (!IsStructurallyEquivalent(Context, 515 Ref1->getPointeeTypeAsWritten(), 516 Ref2->getPointeeTypeAsWritten())) 517 return false; 518 break; 519 } 520 521 case Type::MemberPointer: { 522 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1); 523 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2); 524 if (!IsStructurallyEquivalent(Context, 525 MemPtr1->getPointeeType(), 526 MemPtr2->getPointeeType())) 527 return false; 528 if (!IsStructurallyEquivalent(Context, 529 QualType(MemPtr1->getClass(), 0), 530 QualType(MemPtr2->getClass(), 0))) 531 return false; 532 break; 533 } 534 535 case Type::ConstantArray: { 536 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1); 537 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2); 538 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize())) 539 return false; 540 541 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) 542 return false; 543 break; 544 } 545 546 case Type::IncompleteArray: 547 if (!IsArrayStructurallyEquivalent(Context, 548 cast<ArrayType>(T1), 549 cast<ArrayType>(T2))) 550 return false; 551 break; 552 553 case Type::VariableArray: { 554 const VariableArrayType *Array1 = cast<VariableArrayType>(T1); 555 const VariableArrayType *Array2 = cast<VariableArrayType>(T2); 556 if (!IsStructurallyEquivalent(Context, 557 Array1->getSizeExpr(), Array2->getSizeExpr())) 558 return false; 559 560 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) 561 return false; 562 563 break; 564 } 565 566 case Type::DependentSizedArray: { 567 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1); 568 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2); 569 if (!IsStructurallyEquivalent(Context, 570 Array1->getSizeExpr(), Array2->getSizeExpr())) 571 return false; 572 573 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) 574 return false; 575 576 break; 577 } 578 579 case Type::DependentSizedExtVector: { 580 const DependentSizedExtVectorType *Vec1 581 = cast<DependentSizedExtVectorType>(T1); 582 const DependentSizedExtVectorType *Vec2 583 = cast<DependentSizedExtVectorType>(T2); 584 if (!IsStructurallyEquivalent(Context, 585 Vec1->getSizeExpr(), Vec2->getSizeExpr())) 586 return false; 587 if (!IsStructurallyEquivalent(Context, 588 Vec1->getElementType(), 589 Vec2->getElementType())) 590 return false; 591 break; 592 } 593 594 case Type::Vector: 595 case Type::ExtVector: { 596 const VectorType *Vec1 = cast<VectorType>(T1); 597 const VectorType *Vec2 = cast<VectorType>(T2); 598 if (!IsStructurallyEquivalent(Context, 599 Vec1->getElementType(), 600 Vec2->getElementType())) 601 return false; 602 if (Vec1->getNumElements() != Vec2->getNumElements()) 603 return false; 604 if (Vec1->getVectorKind() != Vec2->getVectorKind()) 605 return false; 606 break; 607 } 608 609 case Type::FunctionProto: { 610 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1); 611 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2); 612 if (Proto1->getNumParams() != Proto2->getNumParams()) 613 return false; 614 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) { 615 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I), 616 Proto2->getParamType(I))) 617 return false; 618 } 619 if (Proto1->isVariadic() != Proto2->isVariadic()) 620 return false; 621 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType()) 622 return false; 623 if (Proto1->getExceptionSpecType() == EST_Dynamic) { 624 if (Proto1->getNumExceptions() != Proto2->getNumExceptions()) 625 return false; 626 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) { 627 if (!IsStructurallyEquivalent(Context, 628 Proto1->getExceptionType(I), 629 Proto2->getExceptionType(I))) 630 return false; 631 } 632 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) { 633 if (!IsStructurallyEquivalent(Context, 634 Proto1->getNoexceptExpr(), 635 Proto2->getNoexceptExpr())) 636 return false; 637 } 638 if (Proto1->getTypeQuals() != Proto2->getTypeQuals()) 639 return false; 640 641 // Fall through to check the bits common with FunctionNoProtoType. 642 } 643 644 case Type::FunctionNoProto: { 645 const FunctionType *Function1 = cast<FunctionType>(T1); 646 const FunctionType *Function2 = cast<FunctionType>(T2); 647 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(), 648 Function2->getReturnType())) 649 return false; 650 if (Function1->getExtInfo() != Function2->getExtInfo()) 651 return false; 652 break; 653 } 654 655 case Type::UnresolvedUsing: 656 if (!IsStructurallyEquivalent(Context, 657 cast<UnresolvedUsingType>(T1)->getDecl(), 658 cast<UnresolvedUsingType>(T2)->getDecl())) 659 return false; 660 661 break; 662 663 case Type::Attributed: 664 if (!IsStructurallyEquivalent(Context, 665 cast<AttributedType>(T1)->getModifiedType(), 666 cast<AttributedType>(T2)->getModifiedType())) 667 return false; 668 if (!IsStructurallyEquivalent(Context, 669 cast<AttributedType>(T1)->getEquivalentType(), 670 cast<AttributedType>(T2)->getEquivalentType())) 671 return false; 672 break; 673 674 case Type::Paren: 675 if (!IsStructurallyEquivalent(Context, 676 cast<ParenType>(T1)->getInnerType(), 677 cast<ParenType>(T2)->getInnerType())) 678 return false; 679 break; 680 681 case Type::Typedef: 682 if (!IsStructurallyEquivalent(Context, 683 cast<TypedefType>(T1)->getDecl(), 684 cast<TypedefType>(T2)->getDecl())) 685 return false; 686 break; 687 688 case Type::TypeOfExpr: 689 if (!IsStructurallyEquivalent(Context, 690 cast<TypeOfExprType>(T1)->getUnderlyingExpr(), 691 cast<TypeOfExprType>(T2)->getUnderlyingExpr())) 692 return false; 693 break; 694 695 case Type::TypeOf: 696 if (!IsStructurallyEquivalent(Context, 697 cast<TypeOfType>(T1)->getUnderlyingType(), 698 cast<TypeOfType>(T2)->getUnderlyingType())) 699 return false; 700 break; 701 702 case Type::UnaryTransform: 703 if (!IsStructurallyEquivalent(Context, 704 cast<UnaryTransformType>(T1)->getUnderlyingType(), 705 cast<UnaryTransformType>(T1)->getUnderlyingType())) 706 return false; 707 break; 708 709 case Type::Decltype: 710 if (!IsStructurallyEquivalent(Context, 711 cast<DecltypeType>(T1)->getUnderlyingExpr(), 712 cast<DecltypeType>(T2)->getUnderlyingExpr())) 713 return false; 714 break; 715 716 case Type::Auto: 717 if (!IsStructurallyEquivalent(Context, 718 cast<AutoType>(T1)->getDeducedType(), 719 cast<AutoType>(T2)->getDeducedType())) 720 return false; 721 break; 722 723 case Type::Record: 724 case Type::Enum: 725 if (!IsStructurallyEquivalent(Context, 726 cast<TagType>(T1)->getDecl(), 727 cast<TagType>(T2)->getDecl())) 728 return false; 729 break; 730 731 case Type::TemplateTypeParm: { 732 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1); 733 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2); 734 if (Parm1->getDepth() != Parm2->getDepth()) 735 return false; 736 if (Parm1->getIndex() != Parm2->getIndex()) 737 return false; 738 if (Parm1->isParameterPack() != Parm2->isParameterPack()) 739 return false; 740 741 // Names of template type parameters are never significant. 742 break; 743 } 744 745 case Type::SubstTemplateTypeParm: { 746 const SubstTemplateTypeParmType *Subst1 747 = cast<SubstTemplateTypeParmType>(T1); 748 const SubstTemplateTypeParmType *Subst2 749 = cast<SubstTemplateTypeParmType>(T2); 750 if (!IsStructurallyEquivalent(Context, 751 QualType(Subst1->getReplacedParameter(), 0), 752 QualType(Subst2->getReplacedParameter(), 0))) 753 return false; 754 if (!IsStructurallyEquivalent(Context, 755 Subst1->getReplacementType(), 756 Subst2->getReplacementType())) 757 return false; 758 break; 759 } 760 761 case Type::SubstTemplateTypeParmPack: { 762 const SubstTemplateTypeParmPackType *Subst1 763 = cast<SubstTemplateTypeParmPackType>(T1); 764 const SubstTemplateTypeParmPackType *Subst2 765 = cast<SubstTemplateTypeParmPackType>(T2); 766 if (!IsStructurallyEquivalent(Context, 767 QualType(Subst1->getReplacedParameter(), 0), 768 QualType(Subst2->getReplacedParameter(), 0))) 769 return false; 770 if (!IsStructurallyEquivalent(Context, 771 Subst1->getArgumentPack(), 772 Subst2->getArgumentPack())) 773 return false; 774 break; 775 } 776 case Type::TemplateSpecialization: { 777 const TemplateSpecializationType *Spec1 778 = cast<TemplateSpecializationType>(T1); 779 const TemplateSpecializationType *Spec2 780 = cast<TemplateSpecializationType>(T2); 781 if (!IsStructurallyEquivalent(Context, 782 Spec1->getTemplateName(), 783 Spec2->getTemplateName())) 784 return false; 785 if (Spec1->getNumArgs() != Spec2->getNumArgs()) 786 return false; 787 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { 788 if (!IsStructurallyEquivalent(Context, 789 Spec1->getArg(I), Spec2->getArg(I))) 790 return false; 791 } 792 break; 793 } 794 795 case Type::Elaborated: { 796 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1); 797 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2); 798 // CHECKME: what if a keyword is ETK_None or ETK_typename ? 799 if (Elab1->getKeyword() != Elab2->getKeyword()) 800 return false; 801 if (!IsStructurallyEquivalent(Context, 802 Elab1->getQualifier(), 803 Elab2->getQualifier())) 804 return false; 805 if (!IsStructurallyEquivalent(Context, 806 Elab1->getNamedType(), 807 Elab2->getNamedType())) 808 return false; 809 break; 810 } 811 812 case Type::InjectedClassName: { 813 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1); 814 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2); 815 if (!IsStructurallyEquivalent(Context, 816 Inj1->getInjectedSpecializationType(), 817 Inj2->getInjectedSpecializationType())) 818 return false; 819 break; 820 } 821 822 case Type::DependentName: { 823 const DependentNameType *Typename1 = cast<DependentNameType>(T1); 824 const DependentNameType *Typename2 = cast<DependentNameType>(T2); 825 if (!IsStructurallyEquivalent(Context, 826 Typename1->getQualifier(), 827 Typename2->getQualifier())) 828 return false; 829 if (!IsStructurallyEquivalent(Typename1->getIdentifier(), 830 Typename2->getIdentifier())) 831 return false; 832 833 break; 834 } 835 836 case Type::DependentTemplateSpecialization: { 837 const DependentTemplateSpecializationType *Spec1 = 838 cast<DependentTemplateSpecializationType>(T1); 839 const DependentTemplateSpecializationType *Spec2 = 840 cast<DependentTemplateSpecializationType>(T2); 841 if (!IsStructurallyEquivalent(Context, 842 Spec1->getQualifier(), 843 Spec2->getQualifier())) 844 return false; 845 if (!IsStructurallyEquivalent(Spec1->getIdentifier(), 846 Spec2->getIdentifier())) 847 return false; 848 if (Spec1->getNumArgs() != Spec2->getNumArgs()) 849 return false; 850 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { 851 if (!IsStructurallyEquivalent(Context, 852 Spec1->getArg(I), Spec2->getArg(I))) 853 return false; 854 } 855 break; 856 } 857 858 case Type::PackExpansion: 859 if (!IsStructurallyEquivalent(Context, 860 cast<PackExpansionType>(T1)->getPattern(), 861 cast<PackExpansionType>(T2)->getPattern())) 862 return false; 863 break; 864 865 case Type::ObjCInterface: { 866 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1); 867 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2); 868 if (!IsStructurallyEquivalent(Context, 869 Iface1->getDecl(), Iface2->getDecl())) 870 return false; 871 break; 872 } 873 874 case Type::ObjCObject: { 875 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1); 876 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2); 877 if (!IsStructurallyEquivalent(Context, 878 Obj1->getBaseType(), 879 Obj2->getBaseType())) 880 return false; 881 if (Obj1->getNumProtocols() != Obj2->getNumProtocols()) 882 return false; 883 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) { 884 if (!IsStructurallyEquivalent(Context, 885 Obj1->getProtocol(I), 886 Obj2->getProtocol(I))) 887 return false; 888 } 889 break; 890 } 891 892 case Type::ObjCObjectPointer: { 893 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1); 894 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2); 895 if (!IsStructurallyEquivalent(Context, 896 Ptr1->getPointeeType(), 897 Ptr2->getPointeeType())) 898 return false; 899 break; 900 } 901 902 case Type::Atomic: { 903 if (!IsStructurallyEquivalent(Context, 904 cast<AtomicType>(T1)->getValueType(), 905 cast<AtomicType>(T2)->getValueType())) 906 return false; 907 break; 908 } 909 910 case Type::Pipe: { 911 if (!IsStructurallyEquivalent(Context, 912 cast<PipeType>(T1)->getElementType(), 913 cast<PipeType>(T2)->getElementType())) 914 return false; 915 break; 916 } 917 918 } // end switch 919 920 return true; 921 } 922 923 /// \brief Determine structural equivalence of two fields. 924 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 925 FieldDecl *Field1, FieldDecl *Field2) { 926 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext()); 927 928 // For anonymous structs/unions, match up the anonymous struct/union type 929 // declarations directly, so that we don't go off searching for anonymous 930 // types 931 if (Field1->isAnonymousStructOrUnion() && 932 Field2->isAnonymousStructOrUnion()) { 933 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl(); 934 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl(); 935 return IsStructurallyEquivalent(Context, D1, D2); 936 } 937 938 // Check for equivalent field names. 939 IdentifierInfo *Name1 = Field1->getIdentifier(); 940 IdentifierInfo *Name2 = Field2->getIdentifier(); 941 if (!::IsStructurallyEquivalent(Name1, Name2)) 942 return false; 943 944 if (!IsStructurallyEquivalent(Context, 945 Field1->getType(), Field2->getType())) { 946 if (Context.Complain) { 947 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) 948 << Context.C2.getTypeDeclType(Owner2); 949 Context.Diag2(Field2->getLocation(), diag::note_odr_field) 950 << Field2->getDeclName() << Field2->getType(); 951 Context.Diag1(Field1->getLocation(), diag::note_odr_field) 952 << Field1->getDeclName() << Field1->getType(); 953 } 954 return false; 955 } 956 957 if (Field1->isBitField() != Field2->isBitField()) { 958 if (Context.Complain) { 959 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) 960 << Context.C2.getTypeDeclType(Owner2); 961 if (Field1->isBitField()) { 962 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) 963 << Field1->getDeclName() << Field1->getType() 964 << Field1->getBitWidthValue(Context.C1); 965 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field) 966 << Field2->getDeclName(); 967 } else { 968 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) 969 << Field2->getDeclName() << Field2->getType() 970 << Field2->getBitWidthValue(Context.C2); 971 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field) 972 << Field1->getDeclName(); 973 } 974 } 975 return false; 976 } 977 978 if (Field1->isBitField()) { 979 // Make sure that the bit-fields are the same length. 980 unsigned Bits1 = Field1->getBitWidthValue(Context.C1); 981 unsigned Bits2 = Field2->getBitWidthValue(Context.C2); 982 983 if (Bits1 != Bits2) { 984 if (Context.Complain) { 985 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) 986 << Context.C2.getTypeDeclType(Owner2); 987 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) 988 << Field2->getDeclName() << Field2->getType() << Bits2; 989 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) 990 << Field1->getDeclName() << Field1->getType() << Bits1; 991 } 992 return false; 993 } 994 } 995 996 return true; 997 } 998 999 /// \brief Find the index of the given anonymous struct/union within its 1000 /// context. 1001 /// 1002 /// \returns Returns the index of this anonymous struct/union in its context, 1003 /// including the next assigned index (if none of them match). Returns an 1004 /// empty option if the context is not a record, i.e.. if the anonymous 1005 /// struct/union is at namespace or block scope. 1006 static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) { 1007 ASTContext &Context = Anon->getASTContext(); 1008 QualType AnonTy = Context.getRecordType(Anon); 1009 1010 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext()); 1011 if (!Owner) 1012 return None; 1013 1014 unsigned Index = 0; 1015 for (const auto *D : Owner->noload_decls()) { 1016 const auto *F = dyn_cast<FieldDecl>(D); 1017 if (!F || !F->isAnonymousStructOrUnion()) 1018 continue; 1019 1020 if (Context.hasSameType(F->getType(), AnonTy)) 1021 break; 1022 1023 ++Index; 1024 } 1025 1026 return Index; 1027 } 1028 1029 /// \brief Determine structural equivalence of two records. 1030 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1031 RecordDecl *D1, RecordDecl *D2) { 1032 if (D1->isUnion() != D2->isUnion()) { 1033 if (Context.Complain) { 1034 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1035 << Context.C2.getTypeDeclType(D2); 1036 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here) 1037 << D1->getDeclName() << (unsigned)D1->getTagKind(); 1038 } 1039 return false; 1040 } 1041 1042 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) { 1043 // If both anonymous structs/unions are in a record context, make sure 1044 // they occur in the same location in the context records. 1045 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) { 1046 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) { 1047 if (*Index1 != *Index2) 1048 return false; 1049 } 1050 } 1051 } 1052 1053 // If both declarations are class template specializations, we know 1054 // the ODR applies, so check the template and template arguments. 1055 ClassTemplateSpecializationDecl *Spec1 1056 = dyn_cast<ClassTemplateSpecializationDecl>(D1); 1057 ClassTemplateSpecializationDecl *Spec2 1058 = dyn_cast<ClassTemplateSpecializationDecl>(D2); 1059 if (Spec1 && Spec2) { 1060 // Check that the specialized templates are the same. 1061 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(), 1062 Spec2->getSpecializedTemplate())) 1063 return false; 1064 1065 // Check that the template arguments are the same. 1066 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size()) 1067 return false; 1068 1069 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I) 1070 if (!IsStructurallyEquivalent(Context, 1071 Spec1->getTemplateArgs().get(I), 1072 Spec2->getTemplateArgs().get(I))) 1073 return false; 1074 } 1075 // If one is a class template specialization and the other is not, these 1076 // structures are different. 1077 else if (Spec1 || Spec2) 1078 return false; 1079 1080 // Compare the definitions of these two records. If either or both are 1081 // incomplete, we assume that they are equivalent. 1082 D1 = D1->getDefinition(); 1083 D2 = D2->getDefinition(); 1084 if (!D1 || !D2) 1085 return true; 1086 1087 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) { 1088 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) { 1089 if (D1CXX->getNumBases() != D2CXX->getNumBases()) { 1090 if (Context.Complain) { 1091 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1092 << Context.C2.getTypeDeclType(D2); 1093 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases) 1094 << D2CXX->getNumBases(); 1095 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases) 1096 << D1CXX->getNumBases(); 1097 } 1098 return false; 1099 } 1100 1101 // Check the base classes. 1102 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(), 1103 BaseEnd1 = D1CXX->bases_end(), 1104 Base2 = D2CXX->bases_begin(); 1105 Base1 != BaseEnd1; 1106 ++Base1, ++Base2) { 1107 if (!IsStructurallyEquivalent(Context, 1108 Base1->getType(), Base2->getType())) { 1109 if (Context.Complain) { 1110 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1111 << Context.C2.getTypeDeclType(D2); 1112 Context.Diag2(Base2->getLocStart(), diag::note_odr_base) 1113 << Base2->getType() 1114 << Base2->getSourceRange(); 1115 Context.Diag1(Base1->getLocStart(), diag::note_odr_base) 1116 << Base1->getType() 1117 << Base1->getSourceRange(); 1118 } 1119 return false; 1120 } 1121 1122 // Check virtual vs. non-virtual inheritance mismatch. 1123 if (Base1->isVirtual() != Base2->isVirtual()) { 1124 if (Context.Complain) { 1125 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1126 << Context.C2.getTypeDeclType(D2); 1127 Context.Diag2(Base2->getLocStart(), 1128 diag::note_odr_virtual_base) 1129 << Base2->isVirtual() << Base2->getSourceRange(); 1130 Context.Diag1(Base1->getLocStart(), diag::note_odr_base) 1131 << Base1->isVirtual() 1132 << Base1->getSourceRange(); 1133 } 1134 return false; 1135 } 1136 } 1137 } else if (D1CXX->getNumBases() > 0) { 1138 if (Context.Complain) { 1139 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1140 << Context.C2.getTypeDeclType(D2); 1141 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin(); 1142 Context.Diag1(Base1->getLocStart(), diag::note_odr_base) 1143 << Base1->getType() 1144 << Base1->getSourceRange(); 1145 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base); 1146 } 1147 return false; 1148 } 1149 } 1150 1151 // Check the fields for consistency. 1152 RecordDecl::field_iterator Field2 = D2->field_begin(), 1153 Field2End = D2->field_end(); 1154 for (RecordDecl::field_iterator Field1 = D1->field_begin(), 1155 Field1End = D1->field_end(); 1156 Field1 != Field1End; 1157 ++Field1, ++Field2) { 1158 if (Field2 == Field2End) { 1159 if (Context.Complain) { 1160 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1161 << Context.C2.getTypeDeclType(D2); 1162 Context.Diag1(Field1->getLocation(), diag::note_odr_field) 1163 << Field1->getDeclName() << Field1->getType(); 1164 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field); 1165 } 1166 return false; 1167 } 1168 1169 if (!IsStructurallyEquivalent(Context, *Field1, *Field2)) 1170 return false; 1171 } 1172 1173 if (Field2 != Field2End) { 1174 if (Context.Complain) { 1175 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1176 << Context.C2.getTypeDeclType(D2); 1177 Context.Diag2(Field2->getLocation(), diag::note_odr_field) 1178 << Field2->getDeclName() << Field2->getType(); 1179 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field); 1180 } 1181 return false; 1182 } 1183 1184 return true; 1185 } 1186 1187 /// \brief Determine structural equivalence of two enums. 1188 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1189 EnumDecl *D1, EnumDecl *D2) { 1190 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(), 1191 EC2End = D2->enumerator_end(); 1192 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(), 1193 EC1End = D1->enumerator_end(); 1194 EC1 != EC1End; ++EC1, ++EC2) { 1195 if (EC2 == EC2End) { 1196 if (Context.Complain) { 1197 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1198 << Context.C2.getTypeDeclType(D2); 1199 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) 1200 << EC1->getDeclName() 1201 << EC1->getInitVal().toString(10); 1202 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator); 1203 } 1204 return false; 1205 } 1206 1207 llvm::APSInt Val1 = EC1->getInitVal(); 1208 llvm::APSInt Val2 = EC2->getInitVal(); 1209 if (!llvm::APSInt::isSameValue(Val1, Val2) || 1210 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) { 1211 if (Context.Complain) { 1212 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1213 << Context.C2.getTypeDeclType(D2); 1214 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) 1215 << EC2->getDeclName() 1216 << EC2->getInitVal().toString(10); 1217 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) 1218 << EC1->getDeclName() 1219 << EC1->getInitVal().toString(10); 1220 } 1221 return false; 1222 } 1223 } 1224 1225 if (EC2 != EC2End) { 1226 if (Context.Complain) { 1227 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1228 << Context.C2.getTypeDeclType(D2); 1229 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) 1230 << EC2->getDeclName() 1231 << EC2->getInitVal().toString(10); 1232 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator); 1233 } 1234 return false; 1235 } 1236 1237 return true; 1238 } 1239 1240 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1241 TemplateParameterList *Params1, 1242 TemplateParameterList *Params2) { 1243 if (Params1->size() != Params2->size()) { 1244 if (Context.Complain) { 1245 Context.Diag2(Params2->getTemplateLoc(), 1246 diag::err_odr_different_num_template_parameters) 1247 << Params1->size() << Params2->size(); 1248 Context.Diag1(Params1->getTemplateLoc(), 1249 diag::note_odr_template_parameter_list); 1250 } 1251 return false; 1252 } 1253 1254 for (unsigned I = 0, N = Params1->size(); I != N; ++I) { 1255 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) { 1256 if (Context.Complain) { 1257 Context.Diag2(Params2->getParam(I)->getLocation(), 1258 diag::err_odr_different_template_parameter_kind); 1259 Context.Diag1(Params1->getParam(I)->getLocation(), 1260 diag::note_odr_template_parameter_here); 1261 } 1262 return false; 1263 } 1264 1265 if (!Context.IsStructurallyEquivalent(Params1->getParam(I), 1266 Params2->getParam(I))) { 1267 1268 return false; 1269 } 1270 } 1271 1272 return true; 1273 } 1274 1275 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1276 TemplateTypeParmDecl *D1, 1277 TemplateTypeParmDecl *D2) { 1278 if (D1->isParameterPack() != D2->isParameterPack()) { 1279 if (Context.Complain) { 1280 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) 1281 << D2->isParameterPack(); 1282 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) 1283 << D1->isParameterPack(); 1284 } 1285 return false; 1286 } 1287 1288 return true; 1289 } 1290 1291 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1292 NonTypeTemplateParmDecl *D1, 1293 NonTypeTemplateParmDecl *D2) { 1294 if (D1->isParameterPack() != D2->isParameterPack()) { 1295 if (Context.Complain) { 1296 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) 1297 << D2->isParameterPack(); 1298 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) 1299 << D1->isParameterPack(); 1300 } 1301 return false; 1302 } 1303 1304 // Check types. 1305 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) { 1306 if (Context.Complain) { 1307 Context.Diag2(D2->getLocation(), 1308 diag::err_odr_non_type_parameter_type_inconsistent) 1309 << D2->getType() << D1->getType(); 1310 Context.Diag1(D1->getLocation(), diag::note_odr_value_here) 1311 << D1->getType(); 1312 } 1313 return false; 1314 } 1315 1316 return true; 1317 } 1318 1319 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1320 TemplateTemplateParmDecl *D1, 1321 TemplateTemplateParmDecl *D2) { 1322 if (D1->isParameterPack() != D2->isParameterPack()) { 1323 if (Context.Complain) { 1324 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) 1325 << D2->isParameterPack(); 1326 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) 1327 << D1->isParameterPack(); 1328 } 1329 return false; 1330 } 1331 1332 // Check template parameter lists. 1333 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(), 1334 D2->getTemplateParameters()); 1335 } 1336 1337 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1338 ClassTemplateDecl *D1, 1339 ClassTemplateDecl *D2) { 1340 // Check template parameters. 1341 if (!IsStructurallyEquivalent(Context, 1342 D1->getTemplateParameters(), 1343 D2->getTemplateParameters())) 1344 return false; 1345 1346 // Check the templated declaration. 1347 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(), 1348 D2->getTemplatedDecl()); 1349 } 1350 1351 /// \brief Determine structural equivalence of two declarations. 1352 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1353 Decl *D1, Decl *D2) { 1354 // FIXME: Check for known structural equivalences via a callback of some sort. 1355 1356 // Check whether we already know that these two declarations are not 1357 // structurally equivalent. 1358 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(), 1359 D2->getCanonicalDecl()))) 1360 return false; 1361 1362 // Determine whether we've already produced a tentative equivalence for D1. 1363 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()]; 1364 if (EquivToD1) 1365 return EquivToD1 == D2->getCanonicalDecl(); 1366 1367 // Produce a tentative equivalence D1 <-> D2, which will be checked later. 1368 EquivToD1 = D2->getCanonicalDecl(); 1369 Context.DeclsToCheck.push_back(D1->getCanonicalDecl()); 1370 return true; 1371 } 1372 1373 bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1, 1374 Decl *D2) { 1375 if (!::IsStructurallyEquivalent(*this, D1, D2)) 1376 return false; 1377 1378 return !Finish(); 1379 } 1380 1381 bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1, 1382 QualType T2) { 1383 if (!::IsStructurallyEquivalent(*this, T1, T2)) 1384 return false; 1385 1386 return !Finish(); 1387 } 1388 1389 bool StructuralEquivalenceContext::Finish() { 1390 while (!DeclsToCheck.empty()) { 1391 // Check the next declaration. 1392 Decl *D1 = DeclsToCheck.front(); 1393 DeclsToCheck.pop_front(); 1394 1395 Decl *D2 = TentativeEquivalences[D1]; 1396 assert(D2 && "Unrecorded tentative equivalence?"); 1397 1398 bool Equivalent = true; 1399 1400 // FIXME: Switch on all declaration kinds. For now, we're just going to 1401 // check the obvious ones. 1402 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) { 1403 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) { 1404 // Check for equivalent structure names. 1405 IdentifierInfo *Name1 = Record1->getIdentifier(); 1406 if (!Name1 && Record1->getTypedefNameForAnonDecl()) 1407 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier(); 1408 IdentifierInfo *Name2 = Record2->getIdentifier(); 1409 if (!Name2 && Record2->getTypedefNameForAnonDecl()) 1410 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier(); 1411 if (!::IsStructurallyEquivalent(Name1, Name2) || 1412 !::IsStructurallyEquivalent(*this, Record1, Record2)) 1413 Equivalent = false; 1414 } else { 1415 // Record/non-record mismatch. 1416 Equivalent = false; 1417 } 1418 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) { 1419 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) { 1420 // Check for equivalent enum names. 1421 IdentifierInfo *Name1 = Enum1->getIdentifier(); 1422 if (!Name1 && Enum1->getTypedefNameForAnonDecl()) 1423 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier(); 1424 IdentifierInfo *Name2 = Enum2->getIdentifier(); 1425 if (!Name2 && Enum2->getTypedefNameForAnonDecl()) 1426 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier(); 1427 if (!::IsStructurallyEquivalent(Name1, Name2) || 1428 !::IsStructurallyEquivalent(*this, Enum1, Enum2)) 1429 Equivalent = false; 1430 } else { 1431 // Enum/non-enum mismatch 1432 Equivalent = false; 1433 } 1434 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) { 1435 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) { 1436 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(), 1437 Typedef2->getIdentifier()) || 1438 !::IsStructurallyEquivalent(*this, 1439 Typedef1->getUnderlyingType(), 1440 Typedef2->getUnderlyingType())) 1441 Equivalent = false; 1442 } else { 1443 // Typedef/non-typedef mismatch. 1444 Equivalent = false; 1445 } 1446 } else if (ClassTemplateDecl *ClassTemplate1 1447 = dyn_cast<ClassTemplateDecl>(D1)) { 1448 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) { 1449 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(), 1450 ClassTemplate2->getIdentifier()) || 1451 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2)) 1452 Equivalent = false; 1453 } else { 1454 // Class template/non-class-template mismatch. 1455 Equivalent = false; 1456 } 1457 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) { 1458 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) { 1459 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) 1460 Equivalent = false; 1461 } else { 1462 // Kind mismatch. 1463 Equivalent = false; 1464 } 1465 } else if (NonTypeTemplateParmDecl *NTTP1 1466 = dyn_cast<NonTypeTemplateParmDecl>(D1)) { 1467 if (NonTypeTemplateParmDecl *NTTP2 1468 = dyn_cast<NonTypeTemplateParmDecl>(D2)) { 1469 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2)) 1470 Equivalent = false; 1471 } else { 1472 // Kind mismatch. 1473 Equivalent = false; 1474 } 1475 } else if (TemplateTemplateParmDecl *TTP1 1476 = dyn_cast<TemplateTemplateParmDecl>(D1)) { 1477 if (TemplateTemplateParmDecl *TTP2 1478 = dyn_cast<TemplateTemplateParmDecl>(D2)) { 1479 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) 1480 Equivalent = false; 1481 } else { 1482 // Kind mismatch. 1483 Equivalent = false; 1484 } 1485 } 1486 1487 if (!Equivalent) { 1488 // Note that these two declarations are not equivalent (and we already 1489 // know about it). 1490 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(), 1491 D2->getCanonicalDecl())); 1492 return true; 1493 } 1494 // FIXME: Check other declaration kinds! 1495 } 1496 1497 return false; 1498 } 1499 1500 //---------------------------------------------------------------------------- 1501 // Import Types 1502 //---------------------------------------------------------------------------- 1503 1504 QualType ASTNodeImporter::VisitType(const Type *T) { 1505 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) 1506 << T->getTypeClassName(); 1507 return QualType(); 1508 } 1509 1510 QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { 1511 switch (T->getKind()) { 1512 #define SHARED_SINGLETON_TYPE(Expansion) 1513 #define BUILTIN_TYPE(Id, SingletonId) \ 1514 case BuiltinType::Id: return Importer.getToContext().SingletonId; 1515 #include "clang/AST/BuiltinTypes.def" 1516 1517 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" 1518 // context supports C++. 1519 1520 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" 1521 // context supports ObjC. 1522 1523 case BuiltinType::Char_U: 1524 // The context we're importing from has an unsigned 'char'. If we're 1525 // importing into a context with a signed 'char', translate to 1526 // 'unsigned char' instead. 1527 if (Importer.getToContext().getLangOpts().CharIsSigned) 1528 return Importer.getToContext().UnsignedCharTy; 1529 1530 return Importer.getToContext().CharTy; 1531 1532 case BuiltinType::Char_S: 1533 // The context we're importing from has an unsigned 'char'. If we're 1534 // importing into a context with a signed 'char', translate to 1535 // 'unsigned char' instead. 1536 if (!Importer.getToContext().getLangOpts().CharIsSigned) 1537 return Importer.getToContext().SignedCharTy; 1538 1539 return Importer.getToContext().CharTy; 1540 1541 case BuiltinType::WChar_S: 1542 case BuiltinType::WChar_U: 1543 // FIXME: If not in C++, shall we translate to the C equivalent of 1544 // wchar_t? 1545 return Importer.getToContext().WCharTy; 1546 } 1547 1548 llvm_unreachable("Invalid BuiltinType Kind!"); 1549 } 1550 1551 QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) { 1552 QualType ToElementType = Importer.Import(T->getElementType()); 1553 if (ToElementType.isNull()) 1554 return QualType(); 1555 1556 return Importer.getToContext().getComplexType(ToElementType); 1557 } 1558 1559 QualType ASTNodeImporter::VisitPointerType(const PointerType *T) { 1560 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1561 if (ToPointeeType.isNull()) 1562 return QualType(); 1563 1564 return Importer.getToContext().getPointerType(ToPointeeType); 1565 } 1566 1567 QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { 1568 // FIXME: Check for blocks support in "to" context. 1569 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1570 if (ToPointeeType.isNull()) 1571 return QualType(); 1572 1573 return Importer.getToContext().getBlockPointerType(ToPointeeType); 1574 } 1575 1576 QualType 1577 ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { 1578 // FIXME: Check for C++ support in "to" context. 1579 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); 1580 if (ToPointeeType.isNull()) 1581 return QualType(); 1582 1583 return Importer.getToContext().getLValueReferenceType(ToPointeeType); 1584 } 1585 1586 QualType 1587 ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { 1588 // FIXME: Check for C++0x support in "to" context. 1589 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); 1590 if (ToPointeeType.isNull()) 1591 return QualType(); 1592 1593 return Importer.getToContext().getRValueReferenceType(ToPointeeType); 1594 } 1595 1596 QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { 1597 // FIXME: Check for C++ support in "to" context. 1598 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1599 if (ToPointeeType.isNull()) 1600 return QualType(); 1601 1602 QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); 1603 return Importer.getToContext().getMemberPointerType(ToPointeeType, 1604 ClassType.getTypePtr()); 1605 } 1606 1607 QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { 1608 QualType ToElementType = Importer.Import(T->getElementType()); 1609 if (ToElementType.isNull()) 1610 return QualType(); 1611 1612 return Importer.getToContext().getConstantArrayType(ToElementType, 1613 T->getSize(), 1614 T->getSizeModifier(), 1615 T->getIndexTypeCVRQualifiers()); 1616 } 1617 1618 QualType 1619 ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { 1620 QualType ToElementType = Importer.Import(T->getElementType()); 1621 if (ToElementType.isNull()) 1622 return QualType(); 1623 1624 return Importer.getToContext().getIncompleteArrayType(ToElementType, 1625 T->getSizeModifier(), 1626 T->getIndexTypeCVRQualifiers()); 1627 } 1628 1629 QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { 1630 QualType ToElementType = Importer.Import(T->getElementType()); 1631 if (ToElementType.isNull()) 1632 return QualType(); 1633 1634 Expr *Size = Importer.Import(T->getSizeExpr()); 1635 if (!Size) 1636 return QualType(); 1637 1638 SourceRange Brackets = Importer.Import(T->getBracketsRange()); 1639 return Importer.getToContext().getVariableArrayType(ToElementType, Size, 1640 T->getSizeModifier(), 1641 T->getIndexTypeCVRQualifiers(), 1642 Brackets); 1643 } 1644 1645 QualType ASTNodeImporter::VisitVectorType(const VectorType *T) { 1646 QualType ToElementType = Importer.Import(T->getElementType()); 1647 if (ToElementType.isNull()) 1648 return QualType(); 1649 1650 return Importer.getToContext().getVectorType(ToElementType, 1651 T->getNumElements(), 1652 T->getVectorKind()); 1653 } 1654 1655 QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { 1656 QualType ToElementType = Importer.Import(T->getElementType()); 1657 if (ToElementType.isNull()) 1658 return QualType(); 1659 1660 return Importer.getToContext().getExtVectorType(ToElementType, 1661 T->getNumElements()); 1662 } 1663 1664 QualType 1665 ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { 1666 // FIXME: What happens if we're importing a function without a prototype 1667 // into C++? Should we make it variadic? 1668 QualType ToResultType = Importer.Import(T->getReturnType()); 1669 if (ToResultType.isNull()) 1670 return QualType(); 1671 1672 return Importer.getToContext().getFunctionNoProtoType(ToResultType, 1673 T->getExtInfo()); 1674 } 1675 1676 QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { 1677 QualType ToResultType = Importer.Import(T->getReturnType()); 1678 if (ToResultType.isNull()) 1679 return QualType(); 1680 1681 // Import argument types 1682 SmallVector<QualType, 4> ArgTypes; 1683 for (const auto &A : T->param_types()) { 1684 QualType ArgType = Importer.Import(A); 1685 if (ArgType.isNull()) 1686 return QualType(); 1687 ArgTypes.push_back(ArgType); 1688 } 1689 1690 // Import exception types 1691 SmallVector<QualType, 4> ExceptionTypes; 1692 for (const auto &E : T->exceptions()) { 1693 QualType ExceptionType = Importer.Import(E); 1694 if (ExceptionType.isNull()) 1695 return QualType(); 1696 ExceptionTypes.push_back(ExceptionType); 1697 } 1698 1699 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); 1700 FunctionProtoType::ExtProtoInfo ToEPI; 1701 1702 ToEPI.ExtInfo = FromEPI.ExtInfo; 1703 ToEPI.Variadic = FromEPI.Variadic; 1704 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; 1705 ToEPI.TypeQuals = FromEPI.TypeQuals; 1706 ToEPI.RefQualifier = FromEPI.RefQualifier; 1707 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; 1708 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; 1709 ToEPI.ExceptionSpec.NoexceptExpr = 1710 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr); 1711 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>( 1712 Importer.Import(FromEPI.ExceptionSpec.SourceDecl)); 1713 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>( 1714 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate)); 1715 1716 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI); 1717 } 1718 1719 QualType ASTNodeImporter::VisitParenType(const ParenType *T) { 1720 QualType ToInnerType = Importer.Import(T->getInnerType()); 1721 if (ToInnerType.isNull()) 1722 return QualType(); 1723 1724 return Importer.getToContext().getParenType(ToInnerType); 1725 } 1726 1727 QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { 1728 TypedefNameDecl *ToDecl 1729 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl())); 1730 if (!ToDecl) 1731 return QualType(); 1732 1733 return Importer.getToContext().getTypeDeclType(ToDecl); 1734 } 1735 1736 QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { 1737 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); 1738 if (!ToExpr) 1739 return QualType(); 1740 1741 return Importer.getToContext().getTypeOfExprType(ToExpr); 1742 } 1743 1744 QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { 1745 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); 1746 if (ToUnderlyingType.isNull()) 1747 return QualType(); 1748 1749 return Importer.getToContext().getTypeOfType(ToUnderlyingType); 1750 } 1751 1752 QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { 1753 // FIXME: Make sure that the "to" context supports C++0x! 1754 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); 1755 if (!ToExpr) 1756 return QualType(); 1757 1758 QualType UnderlyingType = Importer.Import(T->getUnderlyingType()); 1759 if (UnderlyingType.isNull()) 1760 return QualType(); 1761 1762 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType); 1763 } 1764 1765 QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { 1766 QualType ToBaseType = Importer.Import(T->getBaseType()); 1767 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); 1768 if (ToBaseType.isNull() || ToUnderlyingType.isNull()) 1769 return QualType(); 1770 1771 return Importer.getToContext().getUnaryTransformType(ToBaseType, 1772 ToUnderlyingType, 1773 T->getUTTKind()); 1774 } 1775 1776 QualType ASTNodeImporter::VisitAutoType(const AutoType *T) { 1777 // FIXME: Make sure that the "to" context supports C++11! 1778 QualType FromDeduced = T->getDeducedType(); 1779 QualType ToDeduced; 1780 if (!FromDeduced.isNull()) { 1781 ToDeduced = Importer.Import(FromDeduced); 1782 if (ToDeduced.isNull()) 1783 return QualType(); 1784 } 1785 1786 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(), 1787 /*IsDependent*/false); 1788 } 1789 1790 QualType ASTNodeImporter::VisitRecordType(const RecordType *T) { 1791 RecordDecl *ToDecl 1792 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); 1793 if (!ToDecl) 1794 return QualType(); 1795 1796 return Importer.getToContext().getTagDeclType(ToDecl); 1797 } 1798 1799 QualType ASTNodeImporter::VisitEnumType(const EnumType *T) { 1800 EnumDecl *ToDecl 1801 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl())); 1802 if (!ToDecl) 1803 return QualType(); 1804 1805 return Importer.getToContext().getTagDeclType(ToDecl); 1806 } 1807 1808 QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { 1809 QualType FromModifiedType = T->getModifiedType(); 1810 QualType FromEquivalentType = T->getEquivalentType(); 1811 QualType ToModifiedType; 1812 QualType ToEquivalentType; 1813 1814 if (!FromModifiedType.isNull()) { 1815 ToModifiedType = Importer.Import(FromModifiedType); 1816 if (ToModifiedType.isNull()) 1817 return QualType(); 1818 } 1819 if (!FromEquivalentType.isNull()) { 1820 ToEquivalentType = Importer.Import(FromEquivalentType); 1821 if (ToEquivalentType.isNull()) 1822 return QualType(); 1823 } 1824 1825 return Importer.getToContext().getAttributedType(T->getAttrKind(), 1826 ToModifiedType, ToEquivalentType); 1827 } 1828 1829 QualType ASTNodeImporter::VisitTemplateSpecializationType( 1830 const TemplateSpecializationType *T) { 1831 TemplateName ToTemplate = Importer.Import(T->getTemplateName()); 1832 if (ToTemplate.isNull()) 1833 return QualType(); 1834 1835 SmallVector<TemplateArgument, 2> ToTemplateArgs; 1836 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs)) 1837 return QualType(); 1838 1839 QualType ToCanonType; 1840 if (!QualType(T, 0).isCanonical()) { 1841 QualType FromCanonType 1842 = Importer.getFromContext().getCanonicalType(QualType(T, 0)); 1843 ToCanonType =Importer.Import(FromCanonType); 1844 if (ToCanonType.isNull()) 1845 return QualType(); 1846 } 1847 return Importer.getToContext().getTemplateSpecializationType(ToTemplate, 1848 ToTemplateArgs.data(), 1849 ToTemplateArgs.size(), 1850 ToCanonType); 1851 } 1852 1853 QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { 1854 NestedNameSpecifier *ToQualifier = nullptr; 1855 // Note: the qualifier in an ElaboratedType is optional. 1856 if (T->getQualifier()) { 1857 ToQualifier = Importer.Import(T->getQualifier()); 1858 if (!ToQualifier) 1859 return QualType(); 1860 } 1861 1862 QualType ToNamedType = Importer.Import(T->getNamedType()); 1863 if (ToNamedType.isNull()) 1864 return QualType(); 1865 1866 return Importer.getToContext().getElaboratedType(T->getKeyword(), 1867 ToQualifier, ToNamedType); 1868 } 1869 1870 QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { 1871 ObjCInterfaceDecl *Class 1872 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl())); 1873 if (!Class) 1874 return QualType(); 1875 1876 return Importer.getToContext().getObjCInterfaceType(Class); 1877 } 1878 1879 QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { 1880 QualType ToBaseType = Importer.Import(T->getBaseType()); 1881 if (ToBaseType.isNull()) 1882 return QualType(); 1883 1884 SmallVector<QualType, 4> TypeArgs; 1885 for (auto TypeArg : T->getTypeArgsAsWritten()) { 1886 QualType ImportedTypeArg = Importer.Import(TypeArg); 1887 if (ImportedTypeArg.isNull()) 1888 return QualType(); 1889 1890 TypeArgs.push_back(ImportedTypeArg); 1891 } 1892 1893 SmallVector<ObjCProtocolDecl *, 4> Protocols; 1894 for (auto *P : T->quals()) { 1895 ObjCProtocolDecl *Protocol 1896 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P)); 1897 if (!Protocol) 1898 return QualType(); 1899 Protocols.push_back(Protocol); 1900 } 1901 1902 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs, 1903 Protocols, 1904 T->isKindOfTypeAsWritten()); 1905 } 1906 1907 QualType 1908 ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { 1909 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1910 if (ToPointeeType.isNull()) 1911 return QualType(); 1912 1913 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType); 1914 } 1915 1916 //---------------------------------------------------------------------------- 1917 // Import Declarations 1918 //---------------------------------------------------------------------------- 1919 bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC, 1920 DeclContext *&LexicalDC, 1921 DeclarationName &Name, 1922 NamedDecl *&ToD, 1923 SourceLocation &Loc) { 1924 // Import the context of this declaration. 1925 DC = Importer.ImportContext(D->getDeclContext()); 1926 if (!DC) 1927 return true; 1928 1929 LexicalDC = DC; 1930 if (D->getDeclContext() != D->getLexicalDeclContext()) { 1931 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 1932 if (!LexicalDC) 1933 return true; 1934 } 1935 1936 // Import the name of this declaration. 1937 Name = Importer.Import(D->getDeclName()); 1938 if (D->getDeclName() && !Name) 1939 return true; 1940 1941 // Import the location of this declaration. 1942 Loc = Importer.Import(D->getLocation()); 1943 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D)); 1944 return false; 1945 } 1946 1947 void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { 1948 if (!FromD) 1949 return; 1950 1951 if (!ToD) { 1952 ToD = Importer.Import(FromD); 1953 if (!ToD) 1954 return; 1955 } 1956 1957 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) { 1958 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) { 1959 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) { 1960 ImportDefinition(FromRecord, ToRecord); 1961 } 1962 } 1963 return; 1964 } 1965 1966 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) { 1967 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) { 1968 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { 1969 ImportDefinition(FromEnum, ToEnum); 1970 } 1971 } 1972 return; 1973 } 1974 } 1975 1976 void 1977 ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From, 1978 DeclarationNameInfo& To) { 1979 // NOTE: To.Name and To.Loc are already imported. 1980 // We only have to import To.LocInfo. 1981 switch (To.getName().getNameKind()) { 1982 case DeclarationName::Identifier: 1983 case DeclarationName::ObjCZeroArgSelector: 1984 case DeclarationName::ObjCOneArgSelector: 1985 case DeclarationName::ObjCMultiArgSelector: 1986 case DeclarationName::CXXUsingDirective: 1987 return; 1988 1989 case DeclarationName::CXXOperatorName: { 1990 SourceRange Range = From.getCXXOperatorNameRange(); 1991 To.setCXXOperatorNameRange(Importer.Import(Range)); 1992 return; 1993 } 1994 case DeclarationName::CXXLiteralOperatorName: { 1995 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc(); 1996 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc)); 1997 return; 1998 } 1999 case DeclarationName::CXXConstructorName: 2000 case DeclarationName::CXXDestructorName: 2001 case DeclarationName::CXXConversionFunctionName: { 2002 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo(); 2003 To.setNamedTypeInfo(Importer.Import(FromTInfo)); 2004 return; 2005 } 2006 } 2007 llvm_unreachable("Unknown name kind."); 2008 } 2009 2010 void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { 2011 if (Importer.isMinimalImport() && !ForceImport) { 2012 Importer.ImportContext(FromDC); 2013 return; 2014 } 2015 2016 for (auto *From : FromDC->decls()) 2017 Importer.Import(From); 2018 } 2019 2020 bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To, 2021 ImportDefinitionKind Kind) { 2022 if (To->getDefinition() || To->isBeingDefined()) { 2023 if (Kind == IDK_Everything) 2024 ImportDeclContext(From, /*ForceImport=*/true); 2025 2026 return false; 2027 } 2028 2029 To->startDefinition(); 2030 2031 // Add base classes. 2032 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) { 2033 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From); 2034 2035 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); 2036 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); 2037 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor; 2038 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers; 2039 ToData.Aggregate = FromData.Aggregate; 2040 ToData.PlainOldData = FromData.PlainOldData; 2041 ToData.Empty = FromData.Empty; 2042 ToData.Polymorphic = FromData.Polymorphic; 2043 ToData.Abstract = FromData.Abstract; 2044 ToData.IsStandardLayout = FromData.IsStandardLayout; 2045 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases; 2046 ToData.HasPrivateFields = FromData.HasPrivateFields; 2047 ToData.HasProtectedFields = FromData.HasProtectedFields; 2048 ToData.HasPublicFields = FromData.HasPublicFields; 2049 ToData.HasMutableFields = FromData.HasMutableFields; 2050 ToData.HasVariantMembers = FromData.HasVariantMembers; 2051 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers; 2052 ToData.HasInClassInitializer = FromData.HasInClassInitializer; 2053 ToData.HasUninitializedReferenceMember 2054 = FromData.HasUninitializedReferenceMember; 2055 ToData.HasUninitializedFields = FromData.HasUninitializedFields; 2056 ToData.NeedOverloadResolutionForMoveConstructor 2057 = FromData.NeedOverloadResolutionForMoveConstructor; 2058 ToData.NeedOverloadResolutionForMoveAssignment 2059 = FromData.NeedOverloadResolutionForMoveAssignment; 2060 ToData.NeedOverloadResolutionForDestructor 2061 = FromData.NeedOverloadResolutionForDestructor; 2062 ToData.DefaultedMoveConstructorIsDeleted 2063 = FromData.DefaultedMoveConstructorIsDeleted; 2064 ToData.DefaultedMoveAssignmentIsDeleted 2065 = FromData.DefaultedMoveAssignmentIsDeleted; 2066 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted; 2067 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers; 2068 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor; 2069 ToData.HasConstexprNonCopyMoveConstructor 2070 = FromData.HasConstexprNonCopyMoveConstructor; 2071 ToData.HasDefaultedDefaultConstructor 2072 = FromData.HasDefaultedDefaultConstructor; 2073 ToData.DefaultedDefaultConstructorIsConstexpr 2074 = FromData.DefaultedDefaultConstructorIsConstexpr; 2075 ToData.HasConstexprDefaultConstructor 2076 = FromData.HasConstexprDefaultConstructor; 2077 ToData.HasNonLiteralTypeFieldsOrBases 2078 = FromData.HasNonLiteralTypeFieldsOrBases; 2079 // ComputedVisibleConversions not imported. 2080 ToData.UserProvidedDefaultConstructor 2081 = FromData.UserProvidedDefaultConstructor; 2082 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers; 2083 ToData.ImplicitCopyConstructorHasConstParam 2084 = FromData.ImplicitCopyConstructorHasConstParam; 2085 ToData.ImplicitCopyAssignmentHasConstParam 2086 = FromData.ImplicitCopyAssignmentHasConstParam; 2087 ToData.HasDeclaredCopyConstructorWithConstParam 2088 = FromData.HasDeclaredCopyConstructorWithConstParam; 2089 ToData.HasDeclaredCopyAssignmentWithConstParam 2090 = FromData.HasDeclaredCopyAssignmentWithConstParam; 2091 ToData.IsLambda = FromData.IsLambda; 2092 2093 SmallVector<CXXBaseSpecifier *, 4> Bases; 2094 for (const auto &Base1 : FromCXX->bases()) { 2095 QualType T = Importer.Import(Base1.getType()); 2096 if (T.isNull()) 2097 return true; 2098 2099 SourceLocation EllipsisLoc; 2100 if (Base1.isPackExpansion()) 2101 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc()); 2102 2103 // Ensure that we have a definition for the base. 2104 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()); 2105 2106 Bases.push_back( 2107 new (Importer.getToContext()) 2108 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()), 2109 Base1.isVirtual(), 2110 Base1.isBaseOfClass(), 2111 Base1.getAccessSpecifierAsWritten(), 2112 Importer.Import(Base1.getTypeSourceInfo()), 2113 EllipsisLoc)); 2114 } 2115 if (!Bases.empty()) 2116 ToCXX->setBases(Bases.data(), Bases.size()); 2117 } 2118 2119 if (shouldForceImportDeclContext(Kind)) 2120 ImportDeclContext(From, /*ForceImport=*/true); 2121 2122 To->completeDefinition(); 2123 return false; 2124 } 2125 2126 bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To, 2127 ImportDefinitionKind Kind) { 2128 if (To->getAnyInitializer()) 2129 return false; 2130 2131 // FIXME: Can we really import any initializer? Alternatively, we could force 2132 // ourselves to import every declaration of a variable and then only use 2133 // getInit() here. 2134 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer()))); 2135 2136 // FIXME: Other bits to merge? 2137 2138 return false; 2139 } 2140 2141 bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To, 2142 ImportDefinitionKind Kind) { 2143 if (To->getDefinition() || To->isBeingDefined()) { 2144 if (Kind == IDK_Everything) 2145 ImportDeclContext(From, /*ForceImport=*/true); 2146 return false; 2147 } 2148 2149 To->startDefinition(); 2150 2151 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From)); 2152 if (T.isNull()) 2153 return true; 2154 2155 QualType ToPromotionType = Importer.Import(From->getPromotionType()); 2156 if (ToPromotionType.isNull()) 2157 return true; 2158 2159 if (shouldForceImportDeclContext(Kind)) 2160 ImportDeclContext(From, /*ForceImport=*/true); 2161 2162 // FIXME: we might need to merge the number of positive or negative bits 2163 // if the enumerator lists don't match. 2164 To->completeDefinition(T, ToPromotionType, 2165 From->getNumPositiveBits(), 2166 From->getNumNegativeBits()); 2167 return false; 2168 } 2169 2170 TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList( 2171 TemplateParameterList *Params) { 2172 SmallVector<NamedDecl *, 4> ToParams; 2173 ToParams.reserve(Params->size()); 2174 for (TemplateParameterList::iterator P = Params->begin(), 2175 PEnd = Params->end(); 2176 P != PEnd; ++P) { 2177 Decl *To = Importer.Import(*P); 2178 if (!To) 2179 return nullptr; 2180 2181 ToParams.push_back(cast<NamedDecl>(To)); 2182 } 2183 2184 return TemplateParameterList::Create(Importer.getToContext(), 2185 Importer.Import(Params->getTemplateLoc()), 2186 Importer.Import(Params->getLAngleLoc()), 2187 ToParams, 2188 Importer.Import(Params->getRAngleLoc())); 2189 } 2190 2191 TemplateArgument 2192 ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { 2193 switch (From.getKind()) { 2194 case TemplateArgument::Null: 2195 return TemplateArgument(); 2196 2197 case TemplateArgument::Type: { 2198 QualType ToType = Importer.Import(From.getAsType()); 2199 if (ToType.isNull()) 2200 return TemplateArgument(); 2201 return TemplateArgument(ToType); 2202 } 2203 2204 case TemplateArgument::Integral: { 2205 QualType ToType = Importer.Import(From.getIntegralType()); 2206 if (ToType.isNull()) 2207 return TemplateArgument(); 2208 return TemplateArgument(From, ToType); 2209 } 2210 2211 case TemplateArgument::Declaration: { 2212 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl())); 2213 QualType ToType = Importer.Import(From.getParamTypeForDecl()); 2214 if (!To || ToType.isNull()) 2215 return TemplateArgument(); 2216 return TemplateArgument(To, ToType); 2217 } 2218 2219 case TemplateArgument::NullPtr: { 2220 QualType ToType = Importer.Import(From.getNullPtrType()); 2221 if (ToType.isNull()) 2222 return TemplateArgument(); 2223 return TemplateArgument(ToType, /*isNullPtr*/true); 2224 } 2225 2226 case TemplateArgument::Template: { 2227 TemplateName ToTemplate = Importer.Import(From.getAsTemplate()); 2228 if (ToTemplate.isNull()) 2229 return TemplateArgument(); 2230 2231 return TemplateArgument(ToTemplate); 2232 } 2233 2234 case TemplateArgument::TemplateExpansion: { 2235 TemplateName ToTemplate 2236 = Importer.Import(From.getAsTemplateOrTemplatePattern()); 2237 if (ToTemplate.isNull()) 2238 return TemplateArgument(); 2239 2240 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions()); 2241 } 2242 2243 case TemplateArgument::Expression: 2244 if (Expr *ToExpr = Importer.Import(From.getAsExpr())) 2245 return TemplateArgument(ToExpr); 2246 return TemplateArgument(); 2247 2248 case TemplateArgument::Pack: { 2249 SmallVector<TemplateArgument, 2> ToPack; 2250 ToPack.reserve(From.pack_size()); 2251 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack)) 2252 return TemplateArgument(); 2253 2254 return TemplateArgument( 2255 llvm::makeArrayRef(ToPack).copy(Importer.getToContext())); 2256 } 2257 } 2258 2259 llvm_unreachable("Invalid template argument kind"); 2260 } 2261 2262 bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs, 2263 unsigned NumFromArgs, 2264 SmallVectorImpl<TemplateArgument> &ToArgs) { 2265 for (unsigned I = 0; I != NumFromArgs; ++I) { 2266 TemplateArgument To = ImportTemplateArgument(FromArgs[I]); 2267 if (To.isNull() && !FromArgs[I].isNull()) 2268 return true; 2269 2270 ToArgs.push_back(To); 2271 } 2272 2273 return false; 2274 } 2275 2276 bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, 2277 RecordDecl *ToRecord, bool Complain) { 2278 // Eliminate a potential failure point where we attempt to re-import 2279 // something we're trying to import while completing ToRecord. 2280 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord); 2281 if (ToOrigin) { 2282 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin); 2283 if (ToOriginRecord) 2284 ToRecord = ToOriginRecord; 2285 } 2286 2287 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 2288 ToRecord->getASTContext(), 2289 Importer.getNonEquivalentDecls(), 2290 false, Complain); 2291 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord); 2292 } 2293 2294 bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, 2295 bool Complain) { 2296 StructuralEquivalenceContext Ctx( 2297 Importer.getFromContext(), Importer.getToContext(), 2298 Importer.getNonEquivalentDecls(), false, Complain); 2299 return Ctx.IsStructurallyEquivalent(FromVar, ToVar); 2300 } 2301 2302 bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { 2303 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 2304 Importer.getToContext(), 2305 Importer.getNonEquivalentDecls()); 2306 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum); 2307 } 2308 2309 bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC, 2310 EnumConstantDecl *ToEC) 2311 { 2312 const llvm::APSInt &FromVal = FromEC->getInitVal(); 2313 const llvm::APSInt &ToVal = ToEC->getInitVal(); 2314 2315 return FromVal.isSigned() == ToVal.isSigned() && 2316 FromVal.getBitWidth() == ToVal.getBitWidth() && 2317 FromVal == ToVal; 2318 } 2319 2320 bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From, 2321 ClassTemplateDecl *To) { 2322 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 2323 Importer.getToContext(), 2324 Importer.getNonEquivalentDecls()); 2325 return Ctx.IsStructurallyEquivalent(From, To); 2326 } 2327 2328 bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From, 2329 VarTemplateDecl *To) { 2330 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 2331 Importer.getToContext(), 2332 Importer.getNonEquivalentDecls()); 2333 return Ctx.IsStructurallyEquivalent(From, To); 2334 } 2335 2336 Decl *ASTNodeImporter::VisitDecl(Decl *D) { 2337 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) 2338 << D->getDeclKindName(); 2339 return nullptr; 2340 } 2341 2342 Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 2343 TranslationUnitDecl *ToD = 2344 Importer.getToContext().getTranslationUnitDecl(); 2345 2346 Importer.Imported(D, ToD); 2347 2348 return ToD; 2349 } 2350 2351 Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { 2352 2353 SourceLocation Loc = Importer.Import(D->getLocation()); 2354 SourceLocation ColonLoc = Importer.Import(D->getColonLoc()); 2355 2356 // Import the context of this declaration. 2357 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 2358 if (!DC) 2359 return nullptr; 2360 2361 AccessSpecDecl *accessSpecDecl 2362 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(), 2363 DC, Loc, ColonLoc); 2364 2365 if (!accessSpecDecl) 2366 return nullptr; 2367 2368 // Lexical DeclContext and Semantic DeclContext 2369 // is always the same for the accessSpec. 2370 accessSpecDecl->setLexicalDeclContext(DC); 2371 DC->addDeclInternal(accessSpecDecl); 2372 2373 return accessSpecDecl; 2374 } 2375 2376 Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { 2377 // Import the major distinguishing characteristics of this namespace. 2378 DeclContext *DC, *LexicalDC; 2379 DeclarationName Name; 2380 SourceLocation Loc; 2381 NamedDecl *ToD; 2382 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2383 return nullptr; 2384 if (ToD) 2385 return ToD; 2386 2387 NamespaceDecl *MergeWithNamespace = nullptr; 2388 if (!Name) { 2389 // This is an anonymous namespace. Adopt an existing anonymous 2390 // namespace if we can. 2391 // FIXME: Not testable. 2392 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) 2393 MergeWithNamespace = TU->getAnonymousNamespace(); 2394 else 2395 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace(); 2396 } else { 2397 SmallVector<NamedDecl *, 4> ConflictingDecls; 2398 SmallVector<NamedDecl *, 2> FoundDecls; 2399 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2400 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2401 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace)) 2402 continue; 2403 2404 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) { 2405 MergeWithNamespace = FoundNS; 2406 ConflictingDecls.clear(); 2407 break; 2408 } 2409 2410 ConflictingDecls.push_back(FoundDecls[I]); 2411 } 2412 2413 if (!ConflictingDecls.empty()) { 2414 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace, 2415 ConflictingDecls.data(), 2416 ConflictingDecls.size()); 2417 } 2418 } 2419 2420 // Create the "to" namespace, if needed. 2421 NamespaceDecl *ToNamespace = MergeWithNamespace; 2422 if (!ToNamespace) { 2423 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, 2424 D->isInline(), 2425 Importer.Import(D->getLocStart()), 2426 Loc, Name.getAsIdentifierInfo(), 2427 /*PrevDecl=*/nullptr); 2428 ToNamespace->setLexicalDeclContext(LexicalDC); 2429 LexicalDC->addDeclInternal(ToNamespace); 2430 2431 // If this is an anonymous namespace, register it as the anonymous 2432 // namespace within its context. 2433 if (!Name) { 2434 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) 2435 TU->setAnonymousNamespace(ToNamespace); 2436 else 2437 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace); 2438 } 2439 } 2440 Importer.Imported(D, ToNamespace); 2441 2442 ImportDeclContext(D); 2443 2444 return ToNamespace; 2445 } 2446 2447 Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { 2448 // Import the major distinguishing characteristics of this typedef. 2449 DeclContext *DC, *LexicalDC; 2450 DeclarationName Name; 2451 SourceLocation Loc; 2452 NamedDecl *ToD; 2453 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2454 return nullptr; 2455 if (ToD) 2456 return ToD; 2457 2458 // If this typedef is not in block scope, determine whether we've 2459 // seen a typedef with the same name (that we can merge with) or any 2460 // other entity by that name (which name lookup could conflict with). 2461 if (!DC->isFunctionOrMethod()) { 2462 SmallVector<NamedDecl *, 4> ConflictingDecls; 2463 unsigned IDNS = Decl::IDNS_Ordinary; 2464 SmallVector<NamedDecl *, 2> FoundDecls; 2465 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2466 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2467 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2468 continue; 2469 if (TypedefNameDecl *FoundTypedef = 2470 dyn_cast<TypedefNameDecl>(FoundDecls[I])) { 2471 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(), 2472 FoundTypedef->getUnderlyingType())) 2473 return Importer.Imported(D, FoundTypedef); 2474 } 2475 2476 ConflictingDecls.push_back(FoundDecls[I]); 2477 } 2478 2479 if (!ConflictingDecls.empty()) { 2480 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2481 ConflictingDecls.data(), 2482 ConflictingDecls.size()); 2483 if (!Name) 2484 return nullptr; 2485 } 2486 } 2487 2488 // Import the underlying type of this typedef; 2489 QualType T = Importer.Import(D->getUnderlyingType()); 2490 if (T.isNull()) 2491 return nullptr; 2492 2493 // Create the new typedef node. 2494 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 2495 SourceLocation StartL = Importer.Import(D->getLocStart()); 2496 TypedefNameDecl *ToTypedef; 2497 if (IsAlias) 2498 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, 2499 StartL, Loc, 2500 Name.getAsIdentifierInfo(), 2501 TInfo); 2502 else 2503 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC, 2504 StartL, Loc, 2505 Name.getAsIdentifierInfo(), 2506 TInfo); 2507 2508 ToTypedef->setAccess(D->getAccess()); 2509 ToTypedef->setLexicalDeclContext(LexicalDC); 2510 Importer.Imported(D, ToTypedef); 2511 LexicalDC->addDeclInternal(ToTypedef); 2512 2513 return ToTypedef; 2514 } 2515 2516 Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { 2517 return VisitTypedefNameDecl(D, /*IsAlias=*/false); 2518 } 2519 2520 Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { 2521 return VisitTypedefNameDecl(D, /*IsAlias=*/true); 2522 } 2523 2524 Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { 2525 // Import the major distinguishing characteristics of this enum. 2526 DeclContext *DC, *LexicalDC; 2527 DeclarationName Name; 2528 SourceLocation Loc; 2529 NamedDecl *ToD; 2530 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2531 return nullptr; 2532 if (ToD) 2533 return ToD; 2534 2535 // Figure out what enum name we're looking for. 2536 unsigned IDNS = Decl::IDNS_Tag; 2537 DeclarationName SearchName = Name; 2538 if (!SearchName && D->getTypedefNameForAnonDecl()) { 2539 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); 2540 IDNS = Decl::IDNS_Ordinary; 2541 } else if (Importer.getToContext().getLangOpts().CPlusPlus) 2542 IDNS |= Decl::IDNS_Ordinary; 2543 2544 // We may already have an enum of the same name; try to find and match it. 2545 if (!DC->isFunctionOrMethod() && SearchName) { 2546 SmallVector<NamedDecl *, 4> ConflictingDecls; 2547 SmallVector<NamedDecl *, 2> FoundDecls; 2548 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2549 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2550 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2551 continue; 2552 2553 Decl *Found = FoundDecls[I]; 2554 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { 2555 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) 2556 Found = Tag->getDecl(); 2557 } 2558 2559 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) { 2560 if (IsStructuralMatch(D, FoundEnum)) 2561 return Importer.Imported(D, FoundEnum); 2562 } 2563 2564 ConflictingDecls.push_back(FoundDecls[I]); 2565 } 2566 2567 if (!ConflictingDecls.empty()) { 2568 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2569 ConflictingDecls.data(), 2570 ConflictingDecls.size()); 2571 } 2572 } 2573 2574 // Create the enum declaration. 2575 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, 2576 Importer.Import(D->getLocStart()), 2577 Loc, Name.getAsIdentifierInfo(), nullptr, 2578 D->isScoped(), D->isScopedUsingClassTag(), 2579 D->isFixed()); 2580 // Import the qualifier, if any. 2581 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 2582 D2->setAccess(D->getAccess()); 2583 D2->setLexicalDeclContext(LexicalDC); 2584 Importer.Imported(D, D2); 2585 LexicalDC->addDeclInternal(D2); 2586 2587 // Import the integer type. 2588 QualType ToIntegerType = Importer.Import(D->getIntegerType()); 2589 if (ToIntegerType.isNull()) 2590 return nullptr; 2591 D2->setIntegerType(ToIntegerType); 2592 2593 // Import the definition 2594 if (D->isCompleteDefinition() && ImportDefinition(D, D2)) 2595 return nullptr; 2596 2597 return D2; 2598 } 2599 2600 Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { 2601 // If this record has a definition in the translation unit we're coming from, 2602 // but this particular declaration is not that definition, import the 2603 // definition and map to that. 2604 TagDecl *Definition = D->getDefinition(); 2605 if (Definition && Definition != D) { 2606 Decl *ImportedDef = Importer.Import(Definition); 2607 if (!ImportedDef) 2608 return nullptr; 2609 2610 return Importer.Imported(D, ImportedDef); 2611 } 2612 2613 // Import the major distinguishing characteristics of this record. 2614 DeclContext *DC, *LexicalDC; 2615 DeclarationName Name; 2616 SourceLocation Loc; 2617 NamedDecl *ToD; 2618 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2619 return nullptr; 2620 if (ToD) 2621 return ToD; 2622 2623 // Figure out what structure name we're looking for. 2624 unsigned IDNS = Decl::IDNS_Tag; 2625 DeclarationName SearchName = Name; 2626 if (!SearchName && D->getTypedefNameForAnonDecl()) { 2627 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); 2628 IDNS = Decl::IDNS_Ordinary; 2629 } else if (Importer.getToContext().getLangOpts().CPlusPlus) 2630 IDNS |= Decl::IDNS_Ordinary; 2631 2632 // We may already have a record of the same name; try to find and match it. 2633 RecordDecl *AdoptDecl = nullptr; 2634 if (!DC->isFunctionOrMethod()) { 2635 SmallVector<NamedDecl *, 4> ConflictingDecls; 2636 SmallVector<NamedDecl *, 2> FoundDecls; 2637 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2638 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2639 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2640 continue; 2641 2642 Decl *Found = FoundDecls[I]; 2643 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { 2644 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) 2645 Found = Tag->getDecl(); 2646 } 2647 2648 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) { 2649 if (D->isAnonymousStructOrUnion() && 2650 FoundRecord->isAnonymousStructOrUnion()) { 2651 // If both anonymous structs/unions are in a record context, make sure 2652 // they occur in the same location in the context records. 2653 if (Optional<unsigned> Index1 2654 = findAnonymousStructOrUnionIndex(D)) { 2655 if (Optional<unsigned> Index2 = 2656 findAnonymousStructOrUnionIndex(FoundRecord)) { 2657 if (*Index1 != *Index2) 2658 continue; 2659 } 2660 } 2661 } 2662 2663 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { 2664 if ((SearchName && !D->isCompleteDefinition()) 2665 || (D->isCompleteDefinition() && 2666 D->isAnonymousStructOrUnion() 2667 == FoundDef->isAnonymousStructOrUnion() && 2668 IsStructuralMatch(D, FoundDef))) { 2669 // The record types structurally match, or the "from" translation 2670 // unit only had a forward declaration anyway; call it the same 2671 // function. 2672 // FIXME: For C++, we should also merge methods here. 2673 return Importer.Imported(D, FoundDef); 2674 } 2675 } else if (!D->isCompleteDefinition()) { 2676 // We have a forward declaration of this type, so adopt that forward 2677 // declaration rather than building a new one. 2678 2679 // If one or both can be completed from external storage then try one 2680 // last time to complete and compare them before doing this. 2681 2682 if (FoundRecord->hasExternalLexicalStorage() && 2683 !FoundRecord->isCompleteDefinition()) 2684 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord); 2685 if (D->hasExternalLexicalStorage()) 2686 D->getASTContext().getExternalSource()->CompleteType(D); 2687 2688 if (FoundRecord->isCompleteDefinition() && 2689 D->isCompleteDefinition() && 2690 !IsStructuralMatch(D, FoundRecord)) 2691 continue; 2692 2693 AdoptDecl = FoundRecord; 2694 continue; 2695 } else if (!SearchName) { 2696 continue; 2697 } 2698 } 2699 2700 ConflictingDecls.push_back(FoundDecls[I]); 2701 } 2702 2703 if (!ConflictingDecls.empty() && SearchName) { 2704 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2705 ConflictingDecls.data(), 2706 ConflictingDecls.size()); 2707 } 2708 } 2709 2710 // Create the record declaration. 2711 RecordDecl *D2 = AdoptDecl; 2712 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 2713 if (!D2) { 2714 CXXRecordDecl *D2CXX = nullptr; 2715 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) { 2716 if (DCXX->isLambda()) { 2717 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo()); 2718 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(), 2719 DC, TInfo, Loc, 2720 DCXX->isDependentLambda(), 2721 DCXX->isGenericLambda(), 2722 DCXX->getLambdaCaptureDefault()); 2723 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl()); 2724 if (DCXX->getLambdaContextDecl() && !CDecl) 2725 return nullptr; 2726 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), 2727 CDecl); 2728 } else { 2729 D2CXX = CXXRecordDecl::Create(Importer.getToContext(), 2730 D->getTagKind(), 2731 DC, StartLoc, Loc, 2732 Name.getAsIdentifierInfo()); 2733 } 2734 D2 = D2CXX; 2735 D2->setAccess(D->getAccess()); 2736 } else { 2737 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(), 2738 DC, StartLoc, Loc, Name.getAsIdentifierInfo()); 2739 } 2740 2741 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 2742 D2->setLexicalDeclContext(LexicalDC); 2743 LexicalDC->addDeclInternal(D2); 2744 if (D->isAnonymousStructOrUnion()) 2745 D2->setAnonymousStructOrUnion(true); 2746 } 2747 2748 Importer.Imported(D, D2); 2749 2750 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default)) 2751 return nullptr; 2752 2753 return D2; 2754 } 2755 2756 Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { 2757 // Import the major distinguishing characteristics of this enumerator. 2758 DeclContext *DC, *LexicalDC; 2759 DeclarationName Name; 2760 SourceLocation Loc; 2761 NamedDecl *ToD; 2762 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2763 return nullptr; 2764 if (ToD) 2765 return ToD; 2766 2767 QualType T = Importer.Import(D->getType()); 2768 if (T.isNull()) 2769 return nullptr; 2770 2771 // Determine whether there are any other declarations with the same name and 2772 // in the same context. 2773 if (!LexicalDC->isFunctionOrMethod()) { 2774 SmallVector<NamedDecl *, 4> ConflictingDecls; 2775 unsigned IDNS = Decl::IDNS_Ordinary; 2776 SmallVector<NamedDecl *, 2> FoundDecls; 2777 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2778 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2779 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2780 continue; 2781 2782 if (EnumConstantDecl *FoundEnumConstant 2783 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) { 2784 if (IsStructuralMatch(D, FoundEnumConstant)) 2785 return Importer.Imported(D, FoundEnumConstant); 2786 } 2787 2788 ConflictingDecls.push_back(FoundDecls[I]); 2789 } 2790 2791 if (!ConflictingDecls.empty()) { 2792 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2793 ConflictingDecls.data(), 2794 ConflictingDecls.size()); 2795 if (!Name) 2796 return nullptr; 2797 } 2798 } 2799 2800 Expr *Init = Importer.Import(D->getInitExpr()); 2801 if (D->getInitExpr() && !Init) 2802 return nullptr; 2803 2804 EnumConstantDecl *ToEnumerator 2805 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc, 2806 Name.getAsIdentifierInfo(), T, 2807 Init, D->getInitVal()); 2808 ToEnumerator->setAccess(D->getAccess()); 2809 ToEnumerator->setLexicalDeclContext(LexicalDC); 2810 Importer.Imported(D, ToEnumerator); 2811 LexicalDC->addDeclInternal(ToEnumerator); 2812 return ToEnumerator; 2813 } 2814 2815 Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { 2816 // Import the major distinguishing characteristics of this function. 2817 DeclContext *DC, *LexicalDC; 2818 DeclarationName Name; 2819 SourceLocation Loc; 2820 NamedDecl *ToD; 2821 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2822 return nullptr; 2823 if (ToD) 2824 return ToD; 2825 2826 // Try to find a function in our own ("to") context with the same name, same 2827 // type, and in the same context as the function we're importing. 2828 if (!LexicalDC->isFunctionOrMethod()) { 2829 SmallVector<NamedDecl *, 4> ConflictingDecls; 2830 unsigned IDNS = Decl::IDNS_Ordinary; 2831 SmallVector<NamedDecl *, 2> FoundDecls; 2832 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2833 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2834 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2835 continue; 2836 2837 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) { 2838 if (FoundFunction->hasExternalFormalLinkage() && 2839 D->hasExternalFormalLinkage()) { 2840 if (Importer.IsStructurallyEquivalent(D->getType(), 2841 FoundFunction->getType())) { 2842 // FIXME: Actually try to merge the body and other attributes. 2843 return Importer.Imported(D, FoundFunction); 2844 } 2845 2846 // FIXME: Check for overloading more carefully, e.g., by boosting 2847 // Sema::IsOverload out to the AST library. 2848 2849 // Function overloading is okay in C++. 2850 if (Importer.getToContext().getLangOpts().CPlusPlus) 2851 continue; 2852 2853 // Complain about inconsistent function types. 2854 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) 2855 << Name << D->getType() << FoundFunction->getType(); 2856 Importer.ToDiag(FoundFunction->getLocation(), 2857 diag::note_odr_value_here) 2858 << FoundFunction->getType(); 2859 } 2860 } 2861 2862 ConflictingDecls.push_back(FoundDecls[I]); 2863 } 2864 2865 if (!ConflictingDecls.empty()) { 2866 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2867 ConflictingDecls.data(), 2868 ConflictingDecls.size()); 2869 if (!Name) 2870 return nullptr; 2871 } 2872 } 2873 2874 DeclarationNameInfo NameInfo(Name, Loc); 2875 // Import additional name location/type info. 2876 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); 2877 2878 QualType FromTy = D->getType(); 2879 bool usedDifferentExceptionSpec = false; 2880 2881 if (const FunctionProtoType * 2882 FromFPT = D->getType()->getAs<FunctionProtoType>()) { 2883 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); 2884 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the 2885 // FunctionDecl that we are importing the FunctionProtoType for. 2886 // To avoid an infinite recursion when importing, create the FunctionDecl 2887 // with a simplified function type and update it afterwards. 2888 if (FromEPI.ExceptionSpec.SourceDecl || 2889 FromEPI.ExceptionSpec.SourceTemplate || 2890 FromEPI.ExceptionSpec.NoexceptExpr) { 2891 FunctionProtoType::ExtProtoInfo DefaultEPI; 2892 FromTy = Importer.getFromContext().getFunctionType( 2893 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI); 2894 usedDifferentExceptionSpec = true; 2895 } 2896 } 2897 2898 // Import the type. 2899 QualType T = Importer.Import(FromTy); 2900 if (T.isNull()) 2901 return nullptr; 2902 2903 // Import the function parameters. 2904 SmallVector<ParmVarDecl *, 8> Parameters; 2905 for (auto P : D->params()) { 2906 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P)); 2907 if (!ToP) 2908 return nullptr; 2909 2910 Parameters.push_back(ToP); 2911 } 2912 2913 // Create the imported function. 2914 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 2915 FunctionDecl *ToFunction = nullptr; 2916 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart()); 2917 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { 2918 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(), 2919 cast<CXXRecordDecl>(DC), 2920 InnerLocStart, 2921 NameInfo, T, TInfo, 2922 FromConstructor->isExplicit(), 2923 D->isInlineSpecified(), 2924 D->isImplicit(), 2925 D->isConstexpr()); 2926 } else if (isa<CXXDestructorDecl>(D)) { 2927 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(), 2928 cast<CXXRecordDecl>(DC), 2929 InnerLocStart, 2930 NameInfo, T, TInfo, 2931 D->isInlineSpecified(), 2932 D->isImplicit()); 2933 } else if (CXXConversionDecl *FromConversion 2934 = dyn_cast<CXXConversionDecl>(D)) { 2935 ToFunction = CXXConversionDecl::Create(Importer.getToContext(), 2936 cast<CXXRecordDecl>(DC), 2937 InnerLocStart, 2938 NameInfo, T, TInfo, 2939 D->isInlineSpecified(), 2940 FromConversion->isExplicit(), 2941 D->isConstexpr(), 2942 Importer.Import(D->getLocEnd())); 2943 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 2944 ToFunction = CXXMethodDecl::Create(Importer.getToContext(), 2945 cast<CXXRecordDecl>(DC), 2946 InnerLocStart, 2947 NameInfo, T, TInfo, 2948 Method->getStorageClass(), 2949 Method->isInlineSpecified(), 2950 D->isConstexpr(), 2951 Importer.Import(D->getLocEnd())); 2952 } else { 2953 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, 2954 InnerLocStart, 2955 NameInfo, T, TInfo, D->getStorageClass(), 2956 D->isInlineSpecified(), 2957 D->hasWrittenPrototype(), 2958 D->isConstexpr()); 2959 } 2960 2961 // Import the qualifier, if any. 2962 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 2963 ToFunction->setAccess(D->getAccess()); 2964 ToFunction->setLexicalDeclContext(LexicalDC); 2965 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); 2966 ToFunction->setTrivial(D->isTrivial()); 2967 ToFunction->setPure(D->isPure()); 2968 Importer.Imported(D, ToFunction); 2969 2970 // Set the parameters. 2971 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) { 2972 Parameters[I]->setOwningFunction(ToFunction); 2973 ToFunction->addDeclInternal(Parameters[I]); 2974 } 2975 ToFunction->setParams(Parameters); 2976 2977 if (usedDifferentExceptionSpec) { 2978 // Update FunctionProtoType::ExtProtoInfo. 2979 QualType T = Importer.Import(D->getType()); 2980 if (T.isNull()) 2981 return nullptr; 2982 ToFunction->setType(T); 2983 } 2984 2985 // Import the body, if any. 2986 if (Stmt *FromBody = D->getBody()) { 2987 if (Stmt *ToBody = Importer.Import(FromBody)) { 2988 ToFunction->setBody(ToBody); 2989 } 2990 } 2991 2992 // FIXME: Other bits to merge? 2993 2994 // Add this function to the lexical context. 2995 LexicalDC->addDeclInternal(ToFunction); 2996 2997 return ToFunction; 2998 } 2999 3000 Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { 3001 return VisitFunctionDecl(D); 3002 } 3003 3004 Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { 3005 return VisitCXXMethodDecl(D); 3006 } 3007 3008 Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { 3009 return VisitCXXMethodDecl(D); 3010 } 3011 3012 Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { 3013 return VisitCXXMethodDecl(D); 3014 } 3015 3016 static unsigned getFieldIndex(Decl *F) { 3017 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext()); 3018 if (!Owner) 3019 return 0; 3020 3021 unsigned Index = 1; 3022 for (const auto *D : Owner->noload_decls()) { 3023 if (D == F) 3024 return Index; 3025 3026 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) 3027 ++Index; 3028 } 3029 3030 return Index; 3031 } 3032 3033 Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { 3034 // Import the major distinguishing characteristics of a variable. 3035 DeclContext *DC, *LexicalDC; 3036 DeclarationName Name; 3037 SourceLocation Loc; 3038 NamedDecl *ToD; 3039 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3040 return nullptr; 3041 if (ToD) 3042 return ToD; 3043 3044 // Determine whether we've already imported this field. 3045 SmallVector<NamedDecl *, 2> FoundDecls; 3046 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3047 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3048 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) { 3049 // For anonymous fields, match up by index. 3050 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) 3051 continue; 3052 3053 if (Importer.IsStructurallyEquivalent(D->getType(), 3054 FoundField->getType())) { 3055 Importer.Imported(D, FoundField); 3056 return FoundField; 3057 } 3058 3059 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) 3060 << Name << D->getType() << FoundField->getType(); 3061 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) 3062 << FoundField->getType(); 3063 return nullptr; 3064 } 3065 } 3066 3067 // Import the type. 3068 QualType T = Importer.Import(D->getType()); 3069 if (T.isNull()) 3070 return nullptr; 3071 3072 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3073 Expr *BitWidth = Importer.Import(D->getBitWidth()); 3074 if (!BitWidth && D->getBitWidth()) 3075 return nullptr; 3076 3077 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC, 3078 Importer.Import(D->getInnerLocStart()), 3079 Loc, Name.getAsIdentifierInfo(), 3080 T, TInfo, BitWidth, D->isMutable(), 3081 D->getInClassInitStyle()); 3082 ToField->setAccess(D->getAccess()); 3083 ToField->setLexicalDeclContext(LexicalDC); 3084 if (Expr *FromInitializer = D->getInClassInitializer()) { 3085 Expr *ToInitializer = Importer.Import(FromInitializer); 3086 if (ToInitializer) 3087 ToField->setInClassInitializer(ToInitializer); 3088 else 3089 return nullptr; 3090 } 3091 ToField->setImplicit(D->isImplicit()); 3092 Importer.Imported(D, ToField); 3093 LexicalDC->addDeclInternal(ToField); 3094 return ToField; 3095 } 3096 3097 Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { 3098 // Import the major distinguishing characteristics of a variable. 3099 DeclContext *DC, *LexicalDC; 3100 DeclarationName Name; 3101 SourceLocation Loc; 3102 NamedDecl *ToD; 3103 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3104 return nullptr; 3105 if (ToD) 3106 return ToD; 3107 3108 // Determine whether we've already imported this field. 3109 SmallVector<NamedDecl *, 2> FoundDecls; 3110 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3111 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3112 if (IndirectFieldDecl *FoundField 3113 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) { 3114 // For anonymous indirect fields, match up by index. 3115 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) 3116 continue; 3117 3118 if (Importer.IsStructurallyEquivalent(D->getType(), 3119 FoundField->getType(), 3120 !Name.isEmpty())) { 3121 Importer.Imported(D, FoundField); 3122 return FoundField; 3123 } 3124 3125 // If there are more anonymous fields to check, continue. 3126 if (!Name && I < N-1) 3127 continue; 3128 3129 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) 3130 << Name << D->getType() << FoundField->getType(); 3131 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) 3132 << FoundField->getType(); 3133 return nullptr; 3134 } 3135 } 3136 3137 // Import the type. 3138 QualType T = Importer.Import(D->getType()); 3139 if (T.isNull()) 3140 return nullptr; 3141 3142 NamedDecl **NamedChain = 3143 new (Importer.getToContext())NamedDecl*[D->getChainingSize()]; 3144 3145 unsigned i = 0; 3146 for (auto *PI : D->chain()) { 3147 Decl *D = Importer.Import(PI); 3148 if (!D) 3149 return nullptr; 3150 NamedChain[i++] = cast<NamedDecl>(D); 3151 } 3152 3153 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create( 3154 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T, 3155 NamedChain, D->getChainingSize()); 3156 3157 for (const auto *Attr : D->attrs()) 3158 ToIndirectField->addAttr(Attr->clone(Importer.getToContext())); 3159 3160 ToIndirectField->setAccess(D->getAccess()); 3161 ToIndirectField->setLexicalDeclContext(LexicalDC); 3162 Importer.Imported(D, ToIndirectField); 3163 LexicalDC->addDeclInternal(ToIndirectField); 3164 return ToIndirectField; 3165 } 3166 3167 Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { 3168 // Import the major distinguishing characteristics of an ivar. 3169 DeclContext *DC, *LexicalDC; 3170 DeclarationName Name; 3171 SourceLocation Loc; 3172 NamedDecl *ToD; 3173 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3174 return nullptr; 3175 if (ToD) 3176 return ToD; 3177 3178 // Determine whether we've already imported this ivar 3179 SmallVector<NamedDecl *, 2> FoundDecls; 3180 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3181 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3182 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) { 3183 if (Importer.IsStructurallyEquivalent(D->getType(), 3184 FoundIvar->getType())) { 3185 Importer.Imported(D, FoundIvar); 3186 return FoundIvar; 3187 } 3188 3189 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent) 3190 << Name << D->getType() << FoundIvar->getType(); 3191 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) 3192 << FoundIvar->getType(); 3193 return nullptr; 3194 } 3195 } 3196 3197 // Import the type. 3198 QualType T = Importer.Import(D->getType()); 3199 if (T.isNull()) 3200 return nullptr; 3201 3202 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3203 Expr *BitWidth = Importer.Import(D->getBitWidth()); 3204 if (!BitWidth && D->getBitWidth()) 3205 return nullptr; 3206 3207 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), 3208 cast<ObjCContainerDecl>(DC), 3209 Importer.Import(D->getInnerLocStart()), 3210 Loc, Name.getAsIdentifierInfo(), 3211 T, TInfo, D->getAccessControl(), 3212 BitWidth, D->getSynthesize()); 3213 ToIvar->setLexicalDeclContext(LexicalDC); 3214 Importer.Imported(D, ToIvar); 3215 LexicalDC->addDeclInternal(ToIvar); 3216 return ToIvar; 3217 3218 } 3219 3220 Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { 3221 // Import the major distinguishing characteristics of a variable. 3222 DeclContext *DC, *LexicalDC; 3223 DeclarationName Name; 3224 SourceLocation Loc; 3225 NamedDecl *ToD; 3226 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3227 return nullptr; 3228 if (ToD) 3229 return ToD; 3230 3231 // Try to find a variable in our own ("to") context with the same name and 3232 // in the same context as the variable we're importing. 3233 if (D->isFileVarDecl()) { 3234 VarDecl *MergeWithVar = nullptr; 3235 SmallVector<NamedDecl *, 4> ConflictingDecls; 3236 unsigned IDNS = Decl::IDNS_Ordinary; 3237 SmallVector<NamedDecl *, 2> FoundDecls; 3238 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3239 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3240 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 3241 continue; 3242 3243 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) { 3244 // We have found a variable that we may need to merge with. Check it. 3245 if (FoundVar->hasExternalFormalLinkage() && 3246 D->hasExternalFormalLinkage()) { 3247 if (Importer.IsStructurallyEquivalent(D->getType(), 3248 FoundVar->getType())) { 3249 MergeWithVar = FoundVar; 3250 break; 3251 } 3252 3253 const ArrayType *FoundArray 3254 = Importer.getToContext().getAsArrayType(FoundVar->getType()); 3255 const ArrayType *TArray 3256 = Importer.getToContext().getAsArrayType(D->getType()); 3257 if (FoundArray && TArray) { 3258 if (isa<IncompleteArrayType>(FoundArray) && 3259 isa<ConstantArrayType>(TArray)) { 3260 // Import the type. 3261 QualType T = Importer.Import(D->getType()); 3262 if (T.isNull()) 3263 return nullptr; 3264 3265 FoundVar->setType(T); 3266 MergeWithVar = FoundVar; 3267 break; 3268 } else if (isa<IncompleteArrayType>(TArray) && 3269 isa<ConstantArrayType>(FoundArray)) { 3270 MergeWithVar = FoundVar; 3271 break; 3272 } 3273 } 3274 3275 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) 3276 << Name << D->getType() << FoundVar->getType(); 3277 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) 3278 << FoundVar->getType(); 3279 } 3280 } 3281 3282 ConflictingDecls.push_back(FoundDecls[I]); 3283 } 3284 3285 if (MergeWithVar) { 3286 // An equivalent variable with external linkage has been found. Link 3287 // the two declarations, then merge them. 3288 Importer.Imported(D, MergeWithVar); 3289 3290 if (VarDecl *DDef = D->getDefinition()) { 3291 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { 3292 Importer.ToDiag(ExistingDef->getLocation(), 3293 diag::err_odr_variable_multiple_def) 3294 << Name; 3295 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); 3296 } else { 3297 Expr *Init = Importer.Import(DDef->getInit()); 3298 MergeWithVar->setInit(Init); 3299 if (DDef->isInitKnownICE()) { 3300 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt(); 3301 Eval->CheckedICE = true; 3302 Eval->IsICE = DDef->isInitICE(); 3303 } 3304 } 3305 } 3306 3307 return MergeWithVar; 3308 } 3309 3310 if (!ConflictingDecls.empty()) { 3311 Name = Importer.HandleNameConflict(Name, DC, IDNS, 3312 ConflictingDecls.data(), 3313 ConflictingDecls.size()); 3314 if (!Name) 3315 return nullptr; 3316 } 3317 } 3318 3319 // Import the type. 3320 QualType T = Importer.Import(D->getType()); 3321 if (T.isNull()) 3322 return nullptr; 3323 3324 // Create the imported variable. 3325 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3326 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, 3327 Importer.Import(D->getInnerLocStart()), 3328 Loc, Name.getAsIdentifierInfo(), 3329 T, TInfo, 3330 D->getStorageClass()); 3331 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 3332 ToVar->setAccess(D->getAccess()); 3333 ToVar->setLexicalDeclContext(LexicalDC); 3334 Importer.Imported(D, ToVar); 3335 LexicalDC->addDeclInternal(ToVar); 3336 3337 if (!D->isFileVarDecl() && 3338 D->isUsed()) 3339 ToVar->setIsUsed(); 3340 3341 // Merge the initializer. 3342 if (ImportDefinition(D, ToVar)) 3343 return nullptr; 3344 3345 return ToVar; 3346 } 3347 3348 Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { 3349 // Parameters are created in the translation unit's context, then moved 3350 // into the function declaration's context afterward. 3351 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); 3352 3353 // Import the name of this declaration. 3354 DeclarationName Name = Importer.Import(D->getDeclName()); 3355 if (D->getDeclName() && !Name) 3356 return nullptr; 3357 3358 // Import the location of this declaration. 3359 SourceLocation Loc = Importer.Import(D->getLocation()); 3360 3361 // Import the parameter's type. 3362 QualType T = Importer.Import(D->getType()); 3363 if (T.isNull()) 3364 return nullptr; 3365 3366 // Create the imported parameter. 3367 ImplicitParamDecl *ToParm 3368 = ImplicitParamDecl::Create(Importer.getToContext(), DC, 3369 Loc, Name.getAsIdentifierInfo(), 3370 T); 3371 return Importer.Imported(D, ToParm); 3372 } 3373 3374 Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { 3375 // Parameters are created in the translation unit's context, then moved 3376 // into the function declaration's context afterward. 3377 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); 3378 3379 // Import the name of this declaration. 3380 DeclarationName Name = Importer.Import(D->getDeclName()); 3381 if (D->getDeclName() && !Name) 3382 return nullptr; 3383 3384 // Import the location of this declaration. 3385 SourceLocation Loc = Importer.Import(D->getLocation()); 3386 3387 // Import the parameter's type. 3388 QualType T = Importer.Import(D->getType()); 3389 if (T.isNull()) 3390 return nullptr; 3391 3392 // Create the imported parameter. 3393 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3394 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC, 3395 Importer.Import(D->getInnerLocStart()), 3396 Loc, Name.getAsIdentifierInfo(), 3397 T, TInfo, D->getStorageClass(), 3398 /*FIXME: Default argument*/nullptr); 3399 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg()); 3400 3401 if (D->isUsed()) 3402 ToParm->setIsUsed(); 3403 3404 return Importer.Imported(D, ToParm); 3405 } 3406 3407 Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { 3408 // Import the major distinguishing characteristics of a method. 3409 DeclContext *DC, *LexicalDC; 3410 DeclarationName Name; 3411 SourceLocation Loc; 3412 NamedDecl *ToD; 3413 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3414 return nullptr; 3415 if (ToD) 3416 return ToD; 3417 3418 SmallVector<NamedDecl *, 2> FoundDecls; 3419 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3420 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3421 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) { 3422 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) 3423 continue; 3424 3425 // Check return types. 3426 if (!Importer.IsStructurallyEquivalent(D->getReturnType(), 3427 FoundMethod->getReturnType())) { 3428 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent) 3429 << D->isInstanceMethod() << Name << D->getReturnType() 3430 << FoundMethod->getReturnType(); 3431 Importer.ToDiag(FoundMethod->getLocation(), 3432 diag::note_odr_objc_method_here) 3433 << D->isInstanceMethod() << Name; 3434 return nullptr; 3435 } 3436 3437 // Check the number of parameters. 3438 if (D->param_size() != FoundMethod->param_size()) { 3439 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent) 3440 << D->isInstanceMethod() << Name 3441 << D->param_size() << FoundMethod->param_size(); 3442 Importer.ToDiag(FoundMethod->getLocation(), 3443 diag::note_odr_objc_method_here) 3444 << D->isInstanceMethod() << Name; 3445 return nullptr; 3446 } 3447 3448 // Check parameter types. 3449 for (ObjCMethodDecl::param_iterator P = D->param_begin(), 3450 PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); 3451 P != PEnd; ++P, ++FoundP) { 3452 if (!Importer.IsStructurallyEquivalent((*P)->getType(), 3453 (*FoundP)->getType())) { 3454 Importer.FromDiag((*P)->getLocation(), 3455 diag::err_odr_objc_method_param_type_inconsistent) 3456 << D->isInstanceMethod() << Name 3457 << (*P)->getType() << (*FoundP)->getType(); 3458 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) 3459 << (*FoundP)->getType(); 3460 return nullptr; 3461 } 3462 } 3463 3464 // Check variadic/non-variadic. 3465 // Check the number of parameters. 3466 if (D->isVariadic() != FoundMethod->isVariadic()) { 3467 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent) 3468 << D->isInstanceMethod() << Name; 3469 Importer.ToDiag(FoundMethod->getLocation(), 3470 diag::note_odr_objc_method_here) 3471 << D->isInstanceMethod() << Name; 3472 return nullptr; 3473 } 3474 3475 // FIXME: Any other bits we need to merge? 3476 return Importer.Imported(D, FoundMethod); 3477 } 3478 } 3479 3480 // Import the result type. 3481 QualType ResultTy = Importer.Import(D->getReturnType()); 3482 if (ResultTy.isNull()) 3483 return nullptr; 3484 3485 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo()); 3486 3487 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create( 3488 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()), 3489 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(), 3490 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(), 3491 D->getImplementationControl(), D->hasRelatedResultType()); 3492 3493 // FIXME: When we decide to merge method definitions, we'll need to 3494 // deal with implicit parameters. 3495 3496 // Import the parameters 3497 SmallVector<ParmVarDecl *, 5> ToParams; 3498 for (auto *FromP : D->params()) { 3499 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP)); 3500 if (!ToP) 3501 return nullptr; 3502 3503 ToParams.push_back(ToP); 3504 } 3505 3506 // Set the parameters. 3507 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) { 3508 ToParams[I]->setOwningFunction(ToMethod); 3509 ToMethod->addDeclInternal(ToParams[I]); 3510 } 3511 SmallVector<SourceLocation, 12> SelLocs; 3512 D->getSelectorLocs(SelLocs); 3513 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs); 3514 3515 ToMethod->setLexicalDeclContext(LexicalDC); 3516 Importer.Imported(D, ToMethod); 3517 LexicalDC->addDeclInternal(ToMethod); 3518 return ToMethod; 3519 } 3520 3521 Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { 3522 // Import the major distinguishing characteristics of a category. 3523 DeclContext *DC, *LexicalDC; 3524 DeclarationName Name; 3525 SourceLocation Loc; 3526 NamedDecl *ToD; 3527 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3528 return nullptr; 3529 if (ToD) 3530 return ToD; 3531 3532 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo()); 3533 if (!BoundInfo) 3534 return nullptr; 3535 3536 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create( 3537 Importer.getToContext(), DC, 3538 D->getVariance(), 3539 Importer.Import(D->getVarianceLoc()), 3540 D->getIndex(), 3541 Importer.Import(D->getLocation()), 3542 Name.getAsIdentifierInfo(), 3543 Importer.Import(D->getColonLoc()), 3544 BoundInfo); 3545 Importer.Imported(D, Result); 3546 Result->setLexicalDeclContext(LexicalDC); 3547 return Result; 3548 } 3549 3550 Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { 3551 // Import the major distinguishing characteristics of a category. 3552 DeclContext *DC, *LexicalDC; 3553 DeclarationName Name; 3554 SourceLocation Loc; 3555 NamedDecl *ToD; 3556 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3557 return nullptr; 3558 if (ToD) 3559 return ToD; 3560 3561 ObjCInterfaceDecl *ToInterface 3562 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface())); 3563 if (!ToInterface) 3564 return nullptr; 3565 3566 // Determine if we've already encountered this category. 3567 ObjCCategoryDecl *MergeWithCategory 3568 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); 3569 ObjCCategoryDecl *ToCategory = MergeWithCategory; 3570 if (!ToCategory) { 3571 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC, 3572 Importer.Import(D->getAtStartLoc()), 3573 Loc, 3574 Importer.Import(D->getCategoryNameLoc()), 3575 Name.getAsIdentifierInfo(), 3576 ToInterface, 3577 /*TypeParamList=*/nullptr, 3578 Importer.Import(D->getIvarLBraceLoc()), 3579 Importer.Import(D->getIvarRBraceLoc())); 3580 ToCategory->setLexicalDeclContext(LexicalDC); 3581 LexicalDC->addDeclInternal(ToCategory); 3582 Importer.Imported(D, ToCategory); 3583 // Import the type parameter list after calling Imported, to avoid 3584 // loops when bringing in their DeclContext. 3585 ToCategory->setTypeParamList(ImportObjCTypeParamList( 3586 D->getTypeParamList())); 3587 3588 // Import protocols 3589 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3590 SmallVector<SourceLocation, 4> ProtocolLocs; 3591 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc 3592 = D->protocol_loc_begin(); 3593 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), 3594 FromProtoEnd = D->protocol_end(); 3595 FromProto != FromProtoEnd; 3596 ++FromProto, ++FromProtoLoc) { 3597 ObjCProtocolDecl *ToProto 3598 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3599 if (!ToProto) 3600 return nullptr; 3601 Protocols.push_back(ToProto); 3602 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3603 } 3604 3605 // FIXME: If we're merging, make sure that the protocol list is the same. 3606 ToCategory->setProtocolList(Protocols.data(), Protocols.size(), 3607 ProtocolLocs.data(), Importer.getToContext()); 3608 3609 } else { 3610 Importer.Imported(D, ToCategory); 3611 } 3612 3613 // Import all of the members of this category. 3614 ImportDeclContext(D); 3615 3616 // If we have an implementation, import it as well. 3617 if (D->getImplementation()) { 3618 ObjCCategoryImplDecl *Impl 3619 = cast_or_null<ObjCCategoryImplDecl>( 3620 Importer.Import(D->getImplementation())); 3621 if (!Impl) 3622 return nullptr; 3623 3624 ToCategory->setImplementation(Impl); 3625 } 3626 3627 return ToCategory; 3628 } 3629 3630 bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From, 3631 ObjCProtocolDecl *To, 3632 ImportDefinitionKind Kind) { 3633 if (To->getDefinition()) { 3634 if (shouldForceImportDeclContext(Kind)) 3635 ImportDeclContext(From); 3636 return false; 3637 } 3638 3639 // Start the protocol definition 3640 To->startDefinition(); 3641 3642 // Import protocols 3643 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3644 SmallVector<SourceLocation, 4> ProtocolLocs; 3645 ObjCProtocolDecl::protocol_loc_iterator 3646 FromProtoLoc = From->protocol_loc_begin(); 3647 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), 3648 FromProtoEnd = From->protocol_end(); 3649 FromProto != FromProtoEnd; 3650 ++FromProto, ++FromProtoLoc) { 3651 ObjCProtocolDecl *ToProto 3652 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3653 if (!ToProto) 3654 return true; 3655 Protocols.push_back(ToProto); 3656 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3657 } 3658 3659 // FIXME: If we're merging, make sure that the protocol list is the same. 3660 To->setProtocolList(Protocols.data(), Protocols.size(), 3661 ProtocolLocs.data(), Importer.getToContext()); 3662 3663 if (shouldForceImportDeclContext(Kind)) { 3664 // Import all of the members of this protocol. 3665 ImportDeclContext(From, /*ForceImport=*/true); 3666 } 3667 return false; 3668 } 3669 3670 Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { 3671 // If this protocol has a definition in the translation unit we're coming 3672 // from, but this particular declaration is not that definition, import the 3673 // definition and map to that. 3674 ObjCProtocolDecl *Definition = D->getDefinition(); 3675 if (Definition && Definition != D) { 3676 Decl *ImportedDef = Importer.Import(Definition); 3677 if (!ImportedDef) 3678 return nullptr; 3679 3680 return Importer.Imported(D, ImportedDef); 3681 } 3682 3683 // Import the major distinguishing characteristics of a protocol. 3684 DeclContext *DC, *LexicalDC; 3685 DeclarationName Name; 3686 SourceLocation Loc; 3687 NamedDecl *ToD; 3688 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3689 return nullptr; 3690 if (ToD) 3691 return ToD; 3692 3693 ObjCProtocolDecl *MergeWithProtocol = nullptr; 3694 SmallVector<NamedDecl *, 2> FoundDecls; 3695 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3696 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3697 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) 3698 continue; 3699 3700 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I]))) 3701 break; 3702 } 3703 3704 ObjCProtocolDecl *ToProto = MergeWithProtocol; 3705 if (!ToProto) { 3706 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, 3707 Name.getAsIdentifierInfo(), Loc, 3708 Importer.Import(D->getAtStartLoc()), 3709 /*PrevDecl=*/nullptr); 3710 ToProto->setLexicalDeclContext(LexicalDC); 3711 LexicalDC->addDeclInternal(ToProto); 3712 } 3713 3714 Importer.Imported(D, ToProto); 3715 3716 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto)) 3717 return nullptr; 3718 3719 return ToProto; 3720 } 3721 3722 Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 3723 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 3724 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 3725 3726 SourceLocation ExternLoc = Importer.Import(D->getExternLoc()); 3727 SourceLocation LangLoc = Importer.Import(D->getLocation()); 3728 3729 bool HasBraces = D->hasBraces(); 3730 3731 LinkageSpecDecl *ToLinkageSpec = 3732 LinkageSpecDecl::Create(Importer.getToContext(), 3733 DC, 3734 ExternLoc, 3735 LangLoc, 3736 D->getLanguage(), 3737 HasBraces); 3738 3739 if (HasBraces) { 3740 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc()); 3741 ToLinkageSpec->setRBraceLoc(RBraceLoc); 3742 } 3743 3744 ToLinkageSpec->setLexicalDeclContext(LexicalDC); 3745 LexicalDC->addDeclInternal(ToLinkageSpec); 3746 3747 Importer.Imported(D, ToLinkageSpec); 3748 3749 return ToLinkageSpec; 3750 } 3751 3752 bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From, 3753 ObjCInterfaceDecl *To, 3754 ImportDefinitionKind Kind) { 3755 if (To->getDefinition()) { 3756 // Check consistency of superclass. 3757 ObjCInterfaceDecl *FromSuper = From->getSuperClass(); 3758 if (FromSuper) { 3759 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper)); 3760 if (!FromSuper) 3761 return true; 3762 } 3763 3764 ObjCInterfaceDecl *ToSuper = To->getSuperClass(); 3765 if ((bool)FromSuper != (bool)ToSuper || 3766 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { 3767 Importer.ToDiag(To->getLocation(), 3768 diag::err_odr_objc_superclass_inconsistent) 3769 << To->getDeclName(); 3770 if (ToSuper) 3771 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) 3772 << To->getSuperClass()->getDeclName(); 3773 else 3774 Importer.ToDiag(To->getLocation(), 3775 diag::note_odr_objc_missing_superclass); 3776 if (From->getSuperClass()) 3777 Importer.FromDiag(From->getSuperClassLoc(), 3778 diag::note_odr_objc_superclass) 3779 << From->getSuperClass()->getDeclName(); 3780 else 3781 Importer.FromDiag(From->getLocation(), 3782 diag::note_odr_objc_missing_superclass); 3783 } 3784 3785 if (shouldForceImportDeclContext(Kind)) 3786 ImportDeclContext(From); 3787 return false; 3788 } 3789 3790 // Start the definition. 3791 To->startDefinition(); 3792 3793 // If this class has a superclass, import it. 3794 if (From->getSuperClass()) { 3795 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo()); 3796 if (!SuperTInfo) 3797 return true; 3798 3799 To->setSuperClass(SuperTInfo); 3800 } 3801 3802 // Import protocols 3803 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3804 SmallVector<SourceLocation, 4> ProtocolLocs; 3805 ObjCInterfaceDecl::protocol_loc_iterator 3806 FromProtoLoc = From->protocol_loc_begin(); 3807 3808 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), 3809 FromProtoEnd = From->protocol_end(); 3810 FromProto != FromProtoEnd; 3811 ++FromProto, ++FromProtoLoc) { 3812 ObjCProtocolDecl *ToProto 3813 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3814 if (!ToProto) 3815 return true; 3816 Protocols.push_back(ToProto); 3817 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3818 } 3819 3820 // FIXME: If we're merging, make sure that the protocol list is the same. 3821 To->setProtocolList(Protocols.data(), Protocols.size(), 3822 ProtocolLocs.data(), Importer.getToContext()); 3823 3824 // Import categories. When the categories themselves are imported, they'll 3825 // hook themselves into this interface. 3826 for (auto *Cat : From->known_categories()) 3827 Importer.Import(Cat); 3828 3829 // If we have an @implementation, import it as well. 3830 if (From->getImplementation()) { 3831 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>( 3832 Importer.Import(From->getImplementation())); 3833 if (!Impl) 3834 return true; 3835 3836 To->setImplementation(Impl); 3837 } 3838 3839 if (shouldForceImportDeclContext(Kind)) { 3840 // Import all of the members of this class. 3841 ImportDeclContext(From, /*ForceImport=*/true); 3842 } 3843 return false; 3844 } 3845 3846 ObjCTypeParamList * 3847 ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { 3848 if (!list) 3849 return nullptr; 3850 3851 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams; 3852 for (auto fromTypeParam : *list) { 3853 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>( 3854 Importer.Import(fromTypeParam)); 3855 if (!toTypeParam) 3856 return nullptr; 3857 3858 toTypeParams.push_back(toTypeParam); 3859 } 3860 3861 return ObjCTypeParamList::create(Importer.getToContext(), 3862 Importer.Import(list->getLAngleLoc()), 3863 toTypeParams, 3864 Importer.Import(list->getRAngleLoc())); 3865 } 3866 3867 Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { 3868 // If this class has a definition in the translation unit we're coming from, 3869 // but this particular declaration is not that definition, import the 3870 // definition and map to that. 3871 ObjCInterfaceDecl *Definition = D->getDefinition(); 3872 if (Definition && Definition != D) { 3873 Decl *ImportedDef = Importer.Import(Definition); 3874 if (!ImportedDef) 3875 return nullptr; 3876 3877 return Importer.Imported(D, ImportedDef); 3878 } 3879 3880 // Import the major distinguishing characteristics of an @interface. 3881 DeclContext *DC, *LexicalDC; 3882 DeclarationName Name; 3883 SourceLocation Loc; 3884 NamedDecl *ToD; 3885 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3886 return nullptr; 3887 if (ToD) 3888 return ToD; 3889 3890 // Look for an existing interface with the same name. 3891 ObjCInterfaceDecl *MergeWithIface = nullptr; 3892 SmallVector<NamedDecl *, 2> FoundDecls; 3893 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3894 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3895 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 3896 continue; 3897 3898 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I]))) 3899 break; 3900 } 3901 3902 // Create an interface declaration, if one does not already exist. 3903 ObjCInterfaceDecl *ToIface = MergeWithIface; 3904 if (!ToIface) { 3905 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC, 3906 Importer.Import(D->getAtStartLoc()), 3907 Name.getAsIdentifierInfo(), 3908 /*TypeParamList=*/nullptr, 3909 /*PrevDecl=*/nullptr, Loc, 3910 D->isImplicitInterfaceDecl()); 3911 ToIface->setLexicalDeclContext(LexicalDC); 3912 LexicalDC->addDeclInternal(ToIface); 3913 } 3914 Importer.Imported(D, ToIface); 3915 // Import the type parameter list after calling Imported, to avoid 3916 // loops when bringing in their DeclContext. 3917 ToIface->setTypeParamList(ImportObjCTypeParamList( 3918 D->getTypeParamListAsWritten())); 3919 3920 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface)) 3921 return nullptr; 3922 3923 return ToIface; 3924 } 3925 3926 Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { 3927 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>( 3928 Importer.Import(D->getCategoryDecl())); 3929 if (!Category) 3930 return nullptr; 3931 3932 ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); 3933 if (!ToImpl) { 3934 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 3935 if (!DC) 3936 return nullptr; 3937 3938 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc()); 3939 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC, 3940 Importer.Import(D->getIdentifier()), 3941 Category->getClassInterface(), 3942 Importer.Import(D->getLocation()), 3943 Importer.Import(D->getAtStartLoc()), 3944 CategoryNameLoc); 3945 3946 DeclContext *LexicalDC = DC; 3947 if (D->getDeclContext() != D->getLexicalDeclContext()) { 3948 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 3949 if (!LexicalDC) 3950 return nullptr; 3951 3952 ToImpl->setLexicalDeclContext(LexicalDC); 3953 } 3954 3955 LexicalDC->addDeclInternal(ToImpl); 3956 Category->setImplementation(ToImpl); 3957 } 3958 3959 Importer.Imported(D, ToImpl); 3960 ImportDeclContext(D); 3961 return ToImpl; 3962 } 3963 3964 Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { 3965 // Find the corresponding interface. 3966 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>( 3967 Importer.Import(D->getClassInterface())); 3968 if (!Iface) 3969 return nullptr; 3970 3971 // Import the superclass, if any. 3972 ObjCInterfaceDecl *Super = nullptr; 3973 if (D->getSuperClass()) { 3974 Super = cast_or_null<ObjCInterfaceDecl>( 3975 Importer.Import(D->getSuperClass())); 3976 if (!Super) 3977 return nullptr; 3978 } 3979 3980 ObjCImplementationDecl *Impl = Iface->getImplementation(); 3981 if (!Impl) { 3982 // We haven't imported an implementation yet. Create a new @implementation 3983 // now. 3984 Impl = ObjCImplementationDecl::Create(Importer.getToContext(), 3985 Importer.ImportContext(D->getDeclContext()), 3986 Iface, Super, 3987 Importer.Import(D->getLocation()), 3988 Importer.Import(D->getAtStartLoc()), 3989 Importer.Import(D->getSuperClassLoc()), 3990 Importer.Import(D->getIvarLBraceLoc()), 3991 Importer.Import(D->getIvarRBraceLoc())); 3992 3993 if (D->getDeclContext() != D->getLexicalDeclContext()) { 3994 DeclContext *LexicalDC 3995 = Importer.ImportContext(D->getLexicalDeclContext()); 3996 if (!LexicalDC) 3997 return nullptr; 3998 Impl->setLexicalDeclContext(LexicalDC); 3999 } 4000 4001 // Associate the implementation with the class it implements. 4002 Iface->setImplementation(Impl); 4003 Importer.Imported(D, Iface->getImplementation()); 4004 } else { 4005 Importer.Imported(D, Iface->getImplementation()); 4006 4007 // Verify that the existing @implementation has the same superclass. 4008 if ((Super && !Impl->getSuperClass()) || 4009 (!Super && Impl->getSuperClass()) || 4010 (Super && Impl->getSuperClass() && 4011 !declaresSameEntity(Super->getCanonicalDecl(), 4012 Impl->getSuperClass()))) { 4013 Importer.ToDiag(Impl->getLocation(), 4014 diag::err_odr_objc_superclass_inconsistent) 4015 << Iface->getDeclName(); 4016 // FIXME: It would be nice to have the location of the superclass 4017 // below. 4018 if (Impl->getSuperClass()) 4019 Importer.ToDiag(Impl->getLocation(), 4020 diag::note_odr_objc_superclass) 4021 << Impl->getSuperClass()->getDeclName(); 4022 else 4023 Importer.ToDiag(Impl->getLocation(), 4024 diag::note_odr_objc_missing_superclass); 4025 if (D->getSuperClass()) 4026 Importer.FromDiag(D->getLocation(), 4027 diag::note_odr_objc_superclass) 4028 << D->getSuperClass()->getDeclName(); 4029 else 4030 Importer.FromDiag(D->getLocation(), 4031 diag::note_odr_objc_missing_superclass); 4032 return nullptr; 4033 } 4034 } 4035 4036 // Import all of the members of this @implementation. 4037 ImportDeclContext(D); 4038 4039 return Impl; 4040 } 4041 4042 Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { 4043 // Import the major distinguishing characteristics of an @property. 4044 DeclContext *DC, *LexicalDC; 4045 DeclarationName Name; 4046 SourceLocation Loc; 4047 NamedDecl *ToD; 4048 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4049 return nullptr; 4050 if (ToD) 4051 return ToD; 4052 4053 // Check whether we have already imported this property. 4054 SmallVector<NamedDecl *, 2> FoundDecls; 4055 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4056 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 4057 if (ObjCPropertyDecl *FoundProp 4058 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) { 4059 // Check property types. 4060 if (!Importer.IsStructurallyEquivalent(D->getType(), 4061 FoundProp->getType())) { 4062 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent) 4063 << Name << D->getType() << FoundProp->getType(); 4064 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) 4065 << FoundProp->getType(); 4066 return nullptr; 4067 } 4068 4069 // FIXME: Check property attributes, getters, setters, etc.? 4070 4071 // Consider these properties to be equivalent. 4072 Importer.Imported(D, FoundProp); 4073 return FoundProp; 4074 } 4075 } 4076 4077 // Import the type. 4078 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo()); 4079 if (!TSI) 4080 return nullptr; 4081 4082 // Create the new property. 4083 ObjCPropertyDecl *ToProperty 4084 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc, 4085 Name.getAsIdentifierInfo(), 4086 Importer.Import(D->getAtLoc()), 4087 Importer.Import(D->getLParenLoc()), 4088 Importer.Import(D->getType()), 4089 TSI, 4090 D->getPropertyImplementation()); 4091 Importer.Imported(D, ToProperty); 4092 ToProperty->setLexicalDeclContext(LexicalDC); 4093 LexicalDC->addDeclInternal(ToProperty); 4094 4095 ToProperty->setPropertyAttributes(D->getPropertyAttributes()); 4096 ToProperty->setPropertyAttributesAsWritten( 4097 D->getPropertyAttributesAsWritten()); 4098 ToProperty->setGetterName(Importer.Import(D->getGetterName())); 4099 ToProperty->setSetterName(Importer.Import(D->getSetterName())); 4100 ToProperty->setGetterMethodDecl( 4101 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl()))); 4102 ToProperty->setSetterMethodDecl( 4103 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl()))); 4104 ToProperty->setPropertyIvarDecl( 4105 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl()))); 4106 return ToProperty; 4107 } 4108 4109 Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { 4110 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>( 4111 Importer.Import(D->getPropertyDecl())); 4112 if (!Property) 4113 return nullptr; 4114 4115 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 4116 if (!DC) 4117 return nullptr; 4118 4119 // Import the lexical declaration context. 4120 DeclContext *LexicalDC = DC; 4121 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4122 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4123 if (!LexicalDC) 4124 return nullptr; 4125 } 4126 4127 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC); 4128 if (!InImpl) 4129 return nullptr; 4130 4131 // Import the ivar (for an @synthesize). 4132 ObjCIvarDecl *Ivar = nullptr; 4133 if (D->getPropertyIvarDecl()) { 4134 Ivar = cast_or_null<ObjCIvarDecl>( 4135 Importer.Import(D->getPropertyIvarDecl())); 4136 if (!Ivar) 4137 return nullptr; 4138 } 4139 4140 ObjCPropertyImplDecl *ToImpl 4141 = InImpl->FindPropertyImplDecl(Property->getIdentifier(), 4142 Property->getQueryKind()); 4143 if (!ToImpl) { 4144 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC, 4145 Importer.Import(D->getLocStart()), 4146 Importer.Import(D->getLocation()), 4147 Property, 4148 D->getPropertyImplementation(), 4149 Ivar, 4150 Importer.Import(D->getPropertyIvarDeclLoc())); 4151 ToImpl->setLexicalDeclContext(LexicalDC); 4152 Importer.Imported(D, ToImpl); 4153 LexicalDC->addDeclInternal(ToImpl); 4154 } else { 4155 // Check that we have the same kind of property implementation (@synthesize 4156 // vs. @dynamic). 4157 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { 4158 Importer.ToDiag(ToImpl->getLocation(), 4159 diag::err_odr_objc_property_impl_kind_inconsistent) 4160 << Property->getDeclName() 4161 << (ToImpl->getPropertyImplementation() 4162 == ObjCPropertyImplDecl::Dynamic); 4163 Importer.FromDiag(D->getLocation(), 4164 diag::note_odr_objc_property_impl_kind) 4165 << D->getPropertyDecl()->getDeclName() 4166 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); 4167 return nullptr; 4168 } 4169 4170 // For @synthesize, check that we have the same 4171 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && 4172 Ivar != ToImpl->getPropertyIvarDecl()) { 4173 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), 4174 diag::err_odr_objc_synthesize_ivar_inconsistent) 4175 << Property->getDeclName() 4176 << ToImpl->getPropertyIvarDecl()->getDeclName() 4177 << Ivar->getDeclName(); 4178 Importer.FromDiag(D->getPropertyIvarDeclLoc(), 4179 diag::note_odr_objc_synthesize_ivar_here) 4180 << D->getPropertyIvarDecl()->getDeclName(); 4181 return nullptr; 4182 } 4183 4184 // Merge the existing implementation with the new implementation. 4185 Importer.Imported(D, ToImpl); 4186 } 4187 4188 return ToImpl; 4189 } 4190 4191 Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { 4192 // For template arguments, we adopt the translation unit as our declaration 4193 // context. This context will be fixed when the actual template declaration 4194 // is created. 4195 4196 // FIXME: Import default argument. 4197 return TemplateTypeParmDecl::Create(Importer.getToContext(), 4198 Importer.getToContext().getTranslationUnitDecl(), 4199 Importer.Import(D->getLocStart()), 4200 Importer.Import(D->getLocation()), 4201 D->getDepth(), 4202 D->getIndex(), 4203 Importer.Import(D->getIdentifier()), 4204 D->wasDeclaredWithTypename(), 4205 D->isParameterPack()); 4206 } 4207 4208 Decl * 4209 ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { 4210 // Import the name of this declaration. 4211 DeclarationName Name = Importer.Import(D->getDeclName()); 4212 if (D->getDeclName() && !Name) 4213 return nullptr; 4214 4215 // Import the location of this declaration. 4216 SourceLocation Loc = Importer.Import(D->getLocation()); 4217 4218 // Import the type of this declaration. 4219 QualType T = Importer.Import(D->getType()); 4220 if (T.isNull()) 4221 return nullptr; 4222 4223 // Import type-source information. 4224 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 4225 if (D->getTypeSourceInfo() && !TInfo) 4226 return nullptr; 4227 4228 // FIXME: Import default argument. 4229 4230 return NonTypeTemplateParmDecl::Create(Importer.getToContext(), 4231 Importer.getToContext().getTranslationUnitDecl(), 4232 Importer.Import(D->getInnerLocStart()), 4233 Loc, D->getDepth(), D->getPosition(), 4234 Name.getAsIdentifierInfo(), 4235 T, D->isParameterPack(), TInfo); 4236 } 4237 4238 Decl * 4239 ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { 4240 // Import the name of this declaration. 4241 DeclarationName Name = Importer.Import(D->getDeclName()); 4242 if (D->getDeclName() && !Name) 4243 return nullptr; 4244 4245 // Import the location of this declaration. 4246 SourceLocation Loc = Importer.Import(D->getLocation()); 4247 4248 // Import template parameters. 4249 TemplateParameterList *TemplateParams 4250 = ImportTemplateParameterList(D->getTemplateParameters()); 4251 if (!TemplateParams) 4252 return nullptr; 4253 4254 // FIXME: Import default argument. 4255 4256 return TemplateTemplateParmDecl::Create(Importer.getToContext(), 4257 Importer.getToContext().getTranslationUnitDecl(), 4258 Loc, D->getDepth(), D->getPosition(), 4259 D->isParameterPack(), 4260 Name.getAsIdentifierInfo(), 4261 TemplateParams); 4262 } 4263 4264 Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 4265 // If this record has a definition in the translation unit we're coming from, 4266 // but this particular declaration is not that definition, import the 4267 // definition and map to that. 4268 CXXRecordDecl *Definition 4269 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition()); 4270 if (Definition && Definition != D->getTemplatedDecl()) { 4271 Decl *ImportedDef 4272 = Importer.Import(Definition->getDescribedClassTemplate()); 4273 if (!ImportedDef) 4274 return nullptr; 4275 4276 return Importer.Imported(D, ImportedDef); 4277 } 4278 4279 // Import the major distinguishing characteristics of this class template. 4280 DeclContext *DC, *LexicalDC; 4281 DeclarationName Name; 4282 SourceLocation Loc; 4283 NamedDecl *ToD; 4284 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4285 return nullptr; 4286 if (ToD) 4287 return ToD; 4288 4289 // We may already have a template of the same name; try to find and match it. 4290 if (!DC->isFunctionOrMethod()) { 4291 SmallVector<NamedDecl *, 4> ConflictingDecls; 4292 SmallVector<NamedDecl *, 2> FoundDecls; 4293 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4294 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 4295 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 4296 continue; 4297 4298 Decl *Found = FoundDecls[I]; 4299 if (ClassTemplateDecl *FoundTemplate 4300 = dyn_cast<ClassTemplateDecl>(Found)) { 4301 if (IsStructuralMatch(D, FoundTemplate)) { 4302 // The class templates structurally match; call it the same template. 4303 // FIXME: We may be filling in a forward declaration here. Handle 4304 // this case! 4305 Importer.Imported(D->getTemplatedDecl(), 4306 FoundTemplate->getTemplatedDecl()); 4307 return Importer.Imported(D, FoundTemplate); 4308 } 4309 } 4310 4311 ConflictingDecls.push_back(FoundDecls[I]); 4312 } 4313 4314 if (!ConflictingDecls.empty()) { 4315 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, 4316 ConflictingDecls.data(), 4317 ConflictingDecls.size()); 4318 } 4319 4320 if (!Name) 4321 return nullptr; 4322 } 4323 4324 CXXRecordDecl *DTemplated = D->getTemplatedDecl(); 4325 4326 // Create the declaration that is being templated. 4327 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart()); 4328 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation()); 4329 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(), 4330 DTemplated->getTagKind(), 4331 DC, StartLoc, IdLoc, 4332 Name.getAsIdentifierInfo()); 4333 D2Templated->setAccess(DTemplated->getAccess()); 4334 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc())); 4335 D2Templated->setLexicalDeclContext(LexicalDC); 4336 4337 // Create the class template declaration itself. 4338 TemplateParameterList *TemplateParams 4339 = ImportTemplateParameterList(D->getTemplateParameters()); 4340 if (!TemplateParams) 4341 return nullptr; 4342 4343 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC, 4344 Loc, Name, TemplateParams, 4345 D2Templated, 4346 /*PrevDecl=*/nullptr); 4347 D2Templated->setDescribedClassTemplate(D2); 4348 4349 D2->setAccess(D->getAccess()); 4350 D2->setLexicalDeclContext(LexicalDC); 4351 LexicalDC->addDeclInternal(D2); 4352 4353 // Note the relationship between the class templates. 4354 Importer.Imported(D, D2); 4355 Importer.Imported(DTemplated, D2Templated); 4356 4357 if (DTemplated->isCompleteDefinition() && 4358 !D2Templated->isCompleteDefinition()) { 4359 // FIXME: Import definition! 4360 } 4361 4362 return D2; 4363 } 4364 4365 Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl( 4366 ClassTemplateSpecializationDecl *D) { 4367 // If this record has a definition in the translation unit we're coming from, 4368 // but this particular declaration is not that definition, import the 4369 // definition and map to that. 4370 TagDecl *Definition = D->getDefinition(); 4371 if (Definition && Definition != D) { 4372 Decl *ImportedDef = Importer.Import(Definition); 4373 if (!ImportedDef) 4374 return nullptr; 4375 4376 return Importer.Imported(D, ImportedDef); 4377 } 4378 4379 ClassTemplateDecl *ClassTemplate 4380 = cast_or_null<ClassTemplateDecl>(Importer.Import( 4381 D->getSpecializedTemplate())); 4382 if (!ClassTemplate) 4383 return nullptr; 4384 4385 // Import the context of this declaration. 4386 DeclContext *DC = ClassTemplate->getDeclContext(); 4387 if (!DC) 4388 return nullptr; 4389 4390 DeclContext *LexicalDC = DC; 4391 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4392 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4393 if (!LexicalDC) 4394 return nullptr; 4395 } 4396 4397 // Import the location of this declaration. 4398 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 4399 SourceLocation IdLoc = Importer.Import(D->getLocation()); 4400 4401 // Import template arguments. 4402 SmallVector<TemplateArgument, 2> TemplateArgs; 4403 if (ImportTemplateArguments(D->getTemplateArgs().data(), 4404 D->getTemplateArgs().size(), 4405 TemplateArgs)) 4406 return nullptr; 4407 4408 // Try to find an existing specialization with these template arguments. 4409 void *InsertPos = nullptr; 4410 ClassTemplateSpecializationDecl *D2 4411 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos); 4412 if (D2) { 4413 // We already have a class template specialization with these template 4414 // arguments. 4415 4416 // FIXME: Check for specialization vs. instantiation errors. 4417 4418 if (RecordDecl *FoundDef = D2->getDefinition()) { 4419 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) { 4420 // The record types structurally match, or the "from" translation 4421 // unit only had a forward declaration anyway; call it the same 4422 // function. 4423 return Importer.Imported(D, FoundDef); 4424 } 4425 } 4426 } else { 4427 // Create a new specialization. 4428 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(), 4429 D->getTagKind(), DC, 4430 StartLoc, IdLoc, 4431 ClassTemplate, 4432 TemplateArgs.data(), 4433 TemplateArgs.size(), 4434 /*PrevDecl=*/nullptr); 4435 D2->setSpecializationKind(D->getSpecializationKind()); 4436 4437 // Add this specialization to the class template. 4438 ClassTemplate->AddSpecialization(D2, InsertPos); 4439 4440 // Import the qualifier, if any. 4441 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 4442 4443 // Add the specialization to this context. 4444 D2->setLexicalDeclContext(LexicalDC); 4445 LexicalDC->addDeclInternal(D2); 4446 } 4447 Importer.Imported(D, D2); 4448 4449 if (D->isCompleteDefinition() && ImportDefinition(D, D2)) 4450 return nullptr; 4451 4452 return D2; 4453 } 4454 4455 Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { 4456 // If this variable has a definition in the translation unit we're coming 4457 // from, 4458 // but this particular declaration is not that definition, import the 4459 // definition and map to that. 4460 VarDecl *Definition = 4461 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition()); 4462 if (Definition && Definition != D->getTemplatedDecl()) { 4463 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate()); 4464 if (!ImportedDef) 4465 return nullptr; 4466 4467 return Importer.Imported(D, ImportedDef); 4468 } 4469 4470 // Import the major distinguishing characteristics of this variable template. 4471 DeclContext *DC, *LexicalDC; 4472 DeclarationName Name; 4473 SourceLocation Loc; 4474 NamedDecl *ToD; 4475 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4476 return nullptr; 4477 if (ToD) 4478 return ToD; 4479 4480 // We may already have a template of the same name; try to find and match it. 4481 assert(!DC->isFunctionOrMethod() && 4482 "Variable templates cannot be declared at function scope"); 4483 SmallVector<NamedDecl *, 4> ConflictingDecls; 4484 SmallVector<NamedDecl *, 2> FoundDecls; 4485 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4486 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 4487 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 4488 continue; 4489 4490 Decl *Found = FoundDecls[I]; 4491 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) { 4492 if (IsStructuralMatch(D, FoundTemplate)) { 4493 // The variable templates structurally match; call it the same template. 4494 Importer.Imported(D->getTemplatedDecl(), 4495 FoundTemplate->getTemplatedDecl()); 4496 return Importer.Imported(D, FoundTemplate); 4497 } 4498 } 4499 4500 ConflictingDecls.push_back(FoundDecls[I]); 4501 } 4502 4503 if (!ConflictingDecls.empty()) { 4504 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, 4505 ConflictingDecls.data(), 4506 ConflictingDecls.size()); 4507 } 4508 4509 if (!Name) 4510 return nullptr; 4511 4512 VarDecl *DTemplated = D->getTemplatedDecl(); 4513 4514 // Import the type. 4515 QualType T = Importer.Import(DTemplated->getType()); 4516 if (T.isNull()) 4517 return nullptr; 4518 4519 // Create the declaration that is being templated. 4520 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart()); 4521 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation()); 4522 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo()); 4523 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc, 4524 IdLoc, Name.getAsIdentifierInfo(), T, 4525 TInfo, DTemplated->getStorageClass()); 4526 D2Templated->setAccess(DTemplated->getAccess()); 4527 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc())); 4528 D2Templated->setLexicalDeclContext(LexicalDC); 4529 4530 // Importer.Imported(DTemplated, D2Templated); 4531 // LexicalDC->addDeclInternal(D2Templated); 4532 4533 // Merge the initializer. 4534 if (ImportDefinition(DTemplated, D2Templated)) 4535 return nullptr; 4536 4537 // Create the variable template declaration itself. 4538 TemplateParameterList *TemplateParams = 4539 ImportTemplateParameterList(D->getTemplateParameters()); 4540 if (!TemplateParams) 4541 return nullptr; 4542 4543 VarTemplateDecl *D2 = VarTemplateDecl::Create( 4544 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated); 4545 D2Templated->setDescribedVarTemplate(D2); 4546 4547 D2->setAccess(D->getAccess()); 4548 D2->setLexicalDeclContext(LexicalDC); 4549 LexicalDC->addDeclInternal(D2); 4550 4551 // Note the relationship between the variable templates. 4552 Importer.Imported(D, D2); 4553 Importer.Imported(DTemplated, D2Templated); 4554 4555 if (DTemplated->isThisDeclarationADefinition() && 4556 !D2Templated->isThisDeclarationADefinition()) { 4557 // FIXME: Import definition! 4558 } 4559 4560 return D2; 4561 } 4562 4563 Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl( 4564 VarTemplateSpecializationDecl *D) { 4565 // If this record has a definition in the translation unit we're coming from, 4566 // but this particular declaration is not that definition, import the 4567 // definition and map to that. 4568 VarDecl *Definition = D->getDefinition(); 4569 if (Definition && Definition != D) { 4570 Decl *ImportedDef = Importer.Import(Definition); 4571 if (!ImportedDef) 4572 return nullptr; 4573 4574 return Importer.Imported(D, ImportedDef); 4575 } 4576 4577 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>( 4578 Importer.Import(D->getSpecializedTemplate())); 4579 if (!VarTemplate) 4580 return nullptr; 4581 4582 // Import the context of this declaration. 4583 DeclContext *DC = VarTemplate->getDeclContext(); 4584 if (!DC) 4585 return nullptr; 4586 4587 DeclContext *LexicalDC = DC; 4588 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4589 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4590 if (!LexicalDC) 4591 return nullptr; 4592 } 4593 4594 // Import the location of this declaration. 4595 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 4596 SourceLocation IdLoc = Importer.Import(D->getLocation()); 4597 4598 // Import template arguments. 4599 SmallVector<TemplateArgument, 2> TemplateArgs; 4600 if (ImportTemplateArguments(D->getTemplateArgs().data(), 4601 D->getTemplateArgs().size(), TemplateArgs)) 4602 return nullptr; 4603 4604 // Try to find an existing specialization with these template arguments. 4605 void *InsertPos = nullptr; 4606 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization( 4607 TemplateArgs, InsertPos); 4608 if (D2) { 4609 // We already have a variable template specialization with these template 4610 // arguments. 4611 4612 // FIXME: Check for specialization vs. instantiation errors. 4613 4614 if (VarDecl *FoundDef = D2->getDefinition()) { 4615 if (!D->isThisDeclarationADefinition() || 4616 IsStructuralMatch(D, FoundDef)) { 4617 // The record types structurally match, or the "from" translation 4618 // unit only had a forward declaration anyway; call it the same 4619 // variable. 4620 return Importer.Imported(D, FoundDef); 4621 } 4622 } 4623 } else { 4624 4625 // Import the type. 4626 QualType T = Importer.Import(D->getType()); 4627 if (T.isNull()) 4628 return nullptr; 4629 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 4630 4631 // Create a new specialization. 4632 D2 = VarTemplateSpecializationDecl::Create( 4633 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo, 4634 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size()); 4635 D2->setSpecializationKind(D->getSpecializationKind()); 4636 D2->setTemplateArgsInfo(D->getTemplateArgsInfo()); 4637 4638 // Add this specialization to the class template. 4639 VarTemplate->AddSpecialization(D2, InsertPos); 4640 4641 // Import the qualifier, if any. 4642 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 4643 4644 // Add the specialization to this context. 4645 D2->setLexicalDeclContext(LexicalDC); 4646 LexicalDC->addDeclInternal(D2); 4647 } 4648 Importer.Imported(D, D2); 4649 4650 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2)) 4651 return nullptr; 4652 4653 return D2; 4654 } 4655 4656 //---------------------------------------------------------------------------- 4657 // Import Statements 4658 //---------------------------------------------------------------------------- 4659 4660 DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) { 4661 if (DG.isNull()) 4662 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0); 4663 size_t NumDecls = DG.end() - DG.begin(); 4664 SmallVector<Decl *, 1> ToDecls(NumDecls); 4665 auto &_Importer = this->Importer; 4666 std::transform(DG.begin(), DG.end(), ToDecls.begin(), 4667 [&_Importer](Decl *D) -> Decl * { 4668 return _Importer.Import(D); 4669 }); 4670 return DeclGroupRef::Create(Importer.getToContext(), 4671 ToDecls.begin(), 4672 NumDecls); 4673 } 4674 4675 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) { 4676 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node) 4677 << S->getStmtClassName(); 4678 return nullptr; 4679 } 4680 4681 Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) { 4682 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup()); 4683 for (Decl *ToD : ToDG) { 4684 if (!ToD) 4685 return nullptr; 4686 } 4687 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc()); 4688 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc()); 4689 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc); 4690 } 4691 4692 Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) { 4693 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc()); 4694 return new (Importer.getToContext()) NullStmt(ToSemiLoc, 4695 S->hasLeadingEmptyMacro()); 4696 } 4697 4698 Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) { 4699 llvm::ArrayRef<Stmt *> ToStmts; 4700 4701 if (!ImportArray(S->body_begin(), S->body_end(), ToStmts)) 4702 return nullptr; 4703 4704 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc()); 4705 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc()); 4706 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(), 4707 ToStmts, 4708 ToLBraceLoc, ToRBraceLoc); 4709 } 4710 4711 Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) { 4712 Expr *ToLHS = Importer.Import(S->getLHS()); 4713 if (!ToLHS) 4714 return nullptr; 4715 Expr *ToRHS = Importer.Import(S->getRHS()); 4716 if (!ToRHS && S->getRHS()) 4717 return nullptr; 4718 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc()); 4719 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc()); 4720 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 4721 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS, 4722 ToCaseLoc, ToEllipsisLoc, 4723 ToColonLoc); 4724 } 4725 4726 Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) { 4727 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc()); 4728 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 4729 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 4730 if (!ToSubStmt && S->getSubStmt()) 4731 return nullptr; 4732 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc, 4733 ToSubStmt); 4734 } 4735 4736 Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) { 4737 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc()); 4738 LabelDecl *ToLabelDecl = 4739 cast_or_null<LabelDecl>(Importer.Import(S->getDecl())); 4740 if (!ToLabelDecl && S->getDecl()) 4741 return nullptr; 4742 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 4743 if (!ToSubStmt && S->getSubStmt()) 4744 return nullptr; 4745 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl, 4746 ToSubStmt); 4747 } 4748 4749 Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) { 4750 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc()); 4751 ArrayRef<const Attr*> FromAttrs(S->getAttrs()); 4752 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size()); 4753 ASTContext &_ToContext = Importer.getToContext(); 4754 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(), 4755 [&_ToContext](const Attr *A) -> const Attr * { 4756 return A->clone(_ToContext); 4757 }); 4758 for (const Attr *ToA : ToAttrs) { 4759 if (!ToA) 4760 return nullptr; 4761 } 4762 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 4763 if (!ToSubStmt && S->getSubStmt()) 4764 return nullptr; 4765 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc, 4766 ToAttrs, ToSubStmt); 4767 } 4768 4769 Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) { 4770 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc()); 4771 VarDecl *ToConditionVariable = nullptr; 4772 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 4773 ToConditionVariable = 4774 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 4775 if (!ToConditionVariable) 4776 return nullptr; 4777 } 4778 Expr *ToCondition = Importer.Import(S->getCond()); 4779 if (!ToCondition && S->getCond()) 4780 return nullptr; 4781 Stmt *ToThenStmt = Importer.Import(S->getThen()); 4782 if (!ToThenStmt && S->getThen()) 4783 return nullptr; 4784 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc()); 4785 Stmt *ToElseStmt = Importer.Import(S->getElse()); 4786 if (!ToElseStmt && S->getElse()) 4787 return nullptr; 4788 return new (Importer.getToContext()) IfStmt(Importer.getToContext(), 4789 ToIfLoc, ToConditionVariable, 4790 ToCondition, ToThenStmt, 4791 ToElseLoc, ToElseStmt); 4792 } 4793 4794 Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) { 4795 VarDecl *ToConditionVariable = nullptr; 4796 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 4797 ToConditionVariable = 4798 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 4799 if (!ToConditionVariable) 4800 return nullptr; 4801 } 4802 Expr *ToCondition = Importer.Import(S->getCond()); 4803 if (!ToCondition && S->getCond()) 4804 return nullptr; 4805 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt( 4806 Importer.getToContext(), ToConditionVariable, 4807 ToCondition); 4808 Stmt *ToBody = Importer.Import(S->getBody()); 4809 if (!ToBody && S->getBody()) 4810 return nullptr; 4811 ToStmt->setBody(ToBody); 4812 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc())); 4813 // Now we have to re-chain the cases. 4814 SwitchCase *LastChainedSwitchCase = nullptr; 4815 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr; 4816 SC = SC->getNextSwitchCase()) { 4817 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC)); 4818 if (!ToSC) 4819 return nullptr; 4820 if (LastChainedSwitchCase) 4821 LastChainedSwitchCase->setNextSwitchCase(ToSC); 4822 else 4823 ToStmt->setSwitchCaseList(ToSC); 4824 LastChainedSwitchCase = ToSC; 4825 } 4826 return ToStmt; 4827 } 4828 4829 Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) { 4830 VarDecl *ToConditionVariable = nullptr; 4831 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 4832 ToConditionVariable = 4833 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 4834 if (!ToConditionVariable) 4835 return nullptr; 4836 } 4837 Expr *ToCondition = Importer.Import(S->getCond()); 4838 if (!ToCondition && S->getCond()) 4839 return nullptr; 4840 Stmt *ToBody = Importer.Import(S->getBody()); 4841 if (!ToBody && S->getBody()) 4842 return nullptr; 4843 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); 4844 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(), 4845 ToConditionVariable, 4846 ToCondition, ToBody, 4847 ToWhileLoc); 4848 } 4849 4850 Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) { 4851 Stmt *ToBody = Importer.Import(S->getBody()); 4852 if (!ToBody && S->getBody()) 4853 return nullptr; 4854 Expr *ToCondition = Importer.Import(S->getCond()); 4855 if (!ToCondition && S->getCond()) 4856 return nullptr; 4857 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc()); 4858 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); 4859 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 4860 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition, 4861 ToDoLoc, ToWhileLoc, 4862 ToRParenLoc); 4863 } 4864 4865 Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) { 4866 Stmt *ToInit = Importer.Import(S->getInit()); 4867 if (!ToInit && S->getInit()) 4868 return nullptr; 4869 Expr *ToCondition = Importer.Import(S->getCond()); 4870 if (!ToCondition && S->getCond()) 4871 return nullptr; 4872 VarDecl *ToConditionVariable = nullptr; 4873 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 4874 ToConditionVariable = 4875 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 4876 if (!ToConditionVariable) 4877 return nullptr; 4878 } 4879 Expr *ToInc = Importer.Import(S->getInc()); 4880 if (!ToInc && S->getInc()) 4881 return nullptr; 4882 Stmt *ToBody = Importer.Import(S->getBody()); 4883 if (!ToBody && S->getBody()) 4884 return nullptr; 4885 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 4886 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc()); 4887 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 4888 return new (Importer.getToContext()) ForStmt(Importer.getToContext(), 4889 ToInit, ToCondition, 4890 ToConditionVariable, 4891 ToInc, ToBody, 4892 ToForLoc, ToLParenLoc, 4893 ToRParenLoc); 4894 } 4895 4896 Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) { 4897 LabelDecl *ToLabel = nullptr; 4898 if (LabelDecl *FromLabel = S->getLabel()) { 4899 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel)); 4900 if (!ToLabel) 4901 return nullptr; 4902 } 4903 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); 4904 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc()); 4905 return new (Importer.getToContext()) GotoStmt(ToLabel, 4906 ToGotoLoc, ToLabelLoc); 4907 } 4908 4909 Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { 4910 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); 4911 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc()); 4912 Expr *ToTarget = Importer.Import(S->getTarget()); 4913 if (!ToTarget && S->getTarget()) 4914 return nullptr; 4915 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc, 4916 ToTarget); 4917 } 4918 4919 Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) { 4920 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc()); 4921 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc); 4922 } 4923 4924 Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) { 4925 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc()); 4926 return new (Importer.getToContext()) BreakStmt(ToBreakLoc); 4927 } 4928 4929 Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) { 4930 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc()); 4931 Expr *ToRetExpr = Importer.Import(S->getRetValue()); 4932 if (!ToRetExpr && S->getRetValue()) 4933 return nullptr; 4934 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate()); 4935 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate)); 4936 if (!ToNRVOCandidate && NRVOCandidate) 4937 return nullptr; 4938 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr, 4939 ToNRVOCandidate); 4940 } 4941 4942 Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) { 4943 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc()); 4944 VarDecl *ToExceptionDecl = nullptr; 4945 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) { 4946 ToExceptionDecl = 4947 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); 4948 if (!ToExceptionDecl) 4949 return nullptr; 4950 } 4951 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock()); 4952 if (!ToHandlerBlock && S->getHandlerBlock()) 4953 return nullptr; 4954 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc, 4955 ToExceptionDecl, 4956 ToHandlerBlock); 4957 } 4958 4959 Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) { 4960 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc()); 4961 Stmt *ToTryBlock = Importer.Import(S->getTryBlock()); 4962 if (!ToTryBlock && S->getTryBlock()) 4963 return nullptr; 4964 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers()); 4965 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) { 4966 CXXCatchStmt *FromHandler = S->getHandler(HI); 4967 if (Stmt *ToHandler = Importer.Import(FromHandler)) 4968 ToHandlers[HI] = ToHandler; 4969 else 4970 return nullptr; 4971 } 4972 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock, 4973 ToHandlers); 4974 } 4975 4976 Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { 4977 DeclStmt *ToRange = 4978 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt())); 4979 if (!ToRange && S->getRangeStmt()) 4980 return nullptr; 4981 DeclStmt *ToBegin = 4982 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt())); 4983 if (!ToBegin && S->getBeginStmt()) 4984 return nullptr; 4985 DeclStmt *ToEnd = 4986 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt())); 4987 if (!ToEnd && S->getEndStmt()) 4988 return nullptr; 4989 Expr *ToCond = Importer.Import(S->getCond()); 4990 if (!ToCond && S->getCond()) 4991 return nullptr; 4992 Expr *ToInc = Importer.Import(S->getInc()); 4993 if (!ToInc && S->getInc()) 4994 return nullptr; 4995 DeclStmt *ToLoopVar = 4996 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt())); 4997 if (!ToLoopVar && S->getLoopVarStmt()) 4998 return nullptr; 4999 Stmt *ToBody = Importer.Import(S->getBody()); 5000 if (!ToBody && S->getBody()) 5001 return nullptr; 5002 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 5003 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc()); 5004 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 5005 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5006 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd, 5007 ToCond, ToInc, 5008 ToLoopVar, ToBody, 5009 ToForLoc, ToCoawaitLoc, 5010 ToColonLoc, ToRParenLoc); 5011 } 5012 5013 Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { 5014 Stmt *ToElem = Importer.Import(S->getElement()); 5015 if (!ToElem && S->getElement()) 5016 return nullptr; 5017 Expr *ToCollect = Importer.Import(S->getCollection()); 5018 if (!ToCollect && S->getCollection()) 5019 return nullptr; 5020 Stmt *ToBody = Importer.Import(S->getBody()); 5021 if (!ToBody && S->getBody()) 5022 return nullptr; 5023 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 5024 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5025 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem, 5026 ToCollect, 5027 ToBody, ToForLoc, 5028 ToRParenLoc); 5029 } 5030 5031 Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { 5032 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc()); 5033 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5034 VarDecl *ToExceptionDecl = nullptr; 5035 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) { 5036 ToExceptionDecl = 5037 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); 5038 if (!ToExceptionDecl) 5039 return nullptr; 5040 } 5041 Stmt *ToBody = Importer.Import(S->getCatchBody()); 5042 if (!ToBody && S->getCatchBody()) 5043 return nullptr; 5044 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc, 5045 ToRParenLoc, 5046 ToExceptionDecl, 5047 ToBody); 5048 } 5049 5050 Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { 5051 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc()); 5052 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody()); 5053 if (!ToAtFinallyStmt && S->getFinallyBody()) 5054 return nullptr; 5055 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc, 5056 ToAtFinallyStmt); 5057 } 5058 5059 Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { 5060 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc()); 5061 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody()); 5062 if (!ToAtTryStmt && S->getTryBody()) 5063 return nullptr; 5064 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts()); 5065 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) { 5066 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI); 5067 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt)) 5068 ToCatchStmts[CI] = ToCatchStmt; 5069 else 5070 return nullptr; 5071 } 5072 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt()); 5073 if (!ToAtFinallyStmt && S->getFinallyStmt()) 5074 return nullptr; 5075 return ObjCAtTryStmt::Create(Importer.getToContext(), 5076 ToAtTryLoc, ToAtTryStmt, 5077 ToCatchStmts.begin(), ToCatchStmts.size(), 5078 ToAtFinallyStmt); 5079 } 5080 5081 Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt 5082 (ObjCAtSynchronizedStmt *S) { 5083 SourceLocation ToAtSynchronizedLoc = 5084 Importer.Import(S->getAtSynchronizedLoc()); 5085 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr()); 5086 if (!ToSynchExpr && S->getSynchExpr()) 5087 return nullptr; 5088 Stmt *ToSynchBody = Importer.Import(S->getSynchBody()); 5089 if (!ToSynchBody && S->getSynchBody()) 5090 return nullptr; 5091 return new (Importer.getToContext()) ObjCAtSynchronizedStmt( 5092 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody); 5093 } 5094 5095 Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { 5096 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc()); 5097 Expr *ToThrow = Importer.Import(S->getThrowExpr()); 5098 if (!ToThrow && S->getThrowExpr()) 5099 return nullptr; 5100 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow); 5101 } 5102 5103 Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt 5104 (ObjCAutoreleasePoolStmt *S) { 5105 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc()); 5106 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 5107 if (!ToSubStmt && S->getSubStmt()) 5108 return nullptr; 5109 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc, 5110 ToSubStmt); 5111 } 5112 5113 //---------------------------------------------------------------------------- 5114 // Import Expressions 5115 //---------------------------------------------------------------------------- 5116 Expr *ASTNodeImporter::VisitExpr(Expr *E) { 5117 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node) 5118 << E->getStmtClassName(); 5119 return nullptr; 5120 } 5121 5122 Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { 5123 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl())); 5124 if (!ToD) 5125 return nullptr; 5126 5127 NamedDecl *FoundD = nullptr; 5128 if (E->getDecl() != E->getFoundDecl()) { 5129 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl())); 5130 if (!FoundD) 5131 return nullptr; 5132 } 5133 5134 QualType T = Importer.Import(E->getType()); 5135 if (T.isNull()) 5136 return nullptr; 5137 5138 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(), 5139 Importer.Import(E->getQualifierLoc()), 5140 Importer.Import(E->getTemplateKeywordLoc()), 5141 ToD, 5142 E->refersToEnclosingVariableOrCapture(), 5143 Importer.Import(E->getLocation()), 5144 T, E->getValueKind(), 5145 FoundD, 5146 /*FIXME:TemplateArgs=*/nullptr); 5147 if (E->hadMultipleCandidates()) 5148 DRE->setHadMultipleCandidates(true); 5149 return DRE; 5150 } 5151 5152 Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { 5153 QualType T = Importer.Import(E->getType()); 5154 if (T.isNull()) 5155 return nullptr; 5156 5157 return IntegerLiteral::Create(Importer.getToContext(), 5158 E->getValue(), T, 5159 Importer.Import(E->getLocation())); 5160 } 5161 5162 Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { 5163 QualType T = Importer.Import(E->getType()); 5164 if (T.isNull()) 5165 return nullptr; 5166 5167 return new (Importer.getToContext()) CharacterLiteral(E->getValue(), 5168 E->getKind(), T, 5169 Importer.Import(E->getLocation())); 5170 } 5171 5172 Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) { 5173 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5174 if (!SubExpr) 5175 return nullptr; 5176 5177 return new (Importer.getToContext()) 5178 ParenExpr(Importer.Import(E->getLParen()), 5179 Importer.Import(E->getRParen()), 5180 SubExpr); 5181 } 5182 5183 Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { 5184 QualType T = Importer.Import(E->getType()); 5185 if (T.isNull()) 5186 return nullptr; 5187 5188 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5189 if (!SubExpr) 5190 return nullptr; 5191 5192 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(), 5193 T, E->getValueKind(), 5194 E->getObjectKind(), 5195 Importer.Import(E->getOperatorLoc())); 5196 } 5197 5198 Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr( 5199 UnaryExprOrTypeTraitExpr *E) { 5200 QualType ResultType = Importer.Import(E->getType()); 5201 5202 if (E->isArgumentType()) { 5203 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo()); 5204 if (!TInfo) 5205 return nullptr; 5206 5207 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), 5208 TInfo, ResultType, 5209 Importer.Import(E->getOperatorLoc()), 5210 Importer.Import(E->getRParenLoc())); 5211 } 5212 5213 Expr *SubExpr = Importer.Import(E->getArgumentExpr()); 5214 if (!SubExpr) 5215 return nullptr; 5216 5217 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), 5218 SubExpr, ResultType, 5219 Importer.Import(E->getOperatorLoc()), 5220 Importer.Import(E->getRParenLoc())); 5221 } 5222 5223 Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { 5224 QualType T = Importer.Import(E->getType()); 5225 if (T.isNull()) 5226 return nullptr; 5227 5228 Expr *LHS = Importer.Import(E->getLHS()); 5229 if (!LHS) 5230 return nullptr; 5231 5232 Expr *RHS = Importer.Import(E->getRHS()); 5233 if (!RHS) 5234 return nullptr; 5235 5236 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(), 5237 T, E->getValueKind(), 5238 E->getObjectKind(), 5239 Importer.Import(E->getOperatorLoc()), 5240 E->isFPContractable()); 5241 } 5242 5243 Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { 5244 QualType T = Importer.Import(E->getType()); 5245 if (T.isNull()) 5246 return nullptr; 5247 5248 QualType CompLHSType = Importer.Import(E->getComputationLHSType()); 5249 if (CompLHSType.isNull()) 5250 return nullptr; 5251 5252 QualType CompResultType = Importer.Import(E->getComputationResultType()); 5253 if (CompResultType.isNull()) 5254 return nullptr; 5255 5256 Expr *LHS = Importer.Import(E->getLHS()); 5257 if (!LHS) 5258 return nullptr; 5259 5260 Expr *RHS = Importer.Import(E->getRHS()); 5261 if (!RHS) 5262 return nullptr; 5263 5264 return new (Importer.getToContext()) 5265 CompoundAssignOperator(LHS, RHS, E->getOpcode(), 5266 T, E->getValueKind(), 5267 E->getObjectKind(), 5268 CompLHSType, CompResultType, 5269 Importer.Import(E->getOperatorLoc()), 5270 E->isFPContractable()); 5271 } 5272 5273 static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) { 5274 if (E->path_empty()) return false; 5275 5276 // TODO: import cast paths 5277 return true; 5278 } 5279 5280 Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { 5281 QualType T = Importer.Import(E->getType()); 5282 if (T.isNull()) 5283 return nullptr; 5284 5285 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5286 if (!SubExpr) 5287 return nullptr; 5288 5289 CXXCastPath BasePath; 5290 if (ImportCastPath(E, BasePath)) 5291 return nullptr; 5292 5293 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(), 5294 SubExpr, &BasePath, E->getValueKind()); 5295 } 5296 5297 Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) { 5298 QualType T = Importer.Import(E->getType()); 5299 if (T.isNull()) 5300 return nullptr; 5301 5302 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5303 if (!SubExpr) 5304 return nullptr; 5305 5306 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten()); 5307 if (!TInfo && E->getTypeInfoAsWritten()) 5308 return nullptr; 5309 5310 CXXCastPath BasePath; 5311 if (ImportCastPath(E, BasePath)) 5312 return nullptr; 5313 5314 return CStyleCastExpr::Create(Importer.getToContext(), T, 5315 E->getValueKind(), E->getCastKind(), 5316 SubExpr, &BasePath, TInfo, 5317 Importer.Import(E->getLParenLoc()), 5318 Importer.Import(E->getRParenLoc())); 5319 } 5320 5321 Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) { 5322 QualType T = Importer.Import(E->getType()); 5323 if (T.isNull()) 5324 return nullptr; 5325 5326 CXXConstructorDecl *ToCCD = 5327 dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor())); 5328 if (!ToCCD && E->getConstructor()) 5329 return nullptr; 5330 5331 ArrayRef<Expr *> ToArgs; 5332 5333 if (!ImportArray(E->arg_begin(), E->arg_end(), ToArgs)) 5334 return nullptr; 5335 5336 return CXXConstructExpr::Create(Importer.getToContext(), T, 5337 Importer.Import(E->getLocation()), 5338 ToCCD, E->isElidable(), 5339 ToArgs, E->hadMultipleCandidates(), 5340 E->isListInitialization(), 5341 E->isStdInitListInitialization(), 5342 E->requiresZeroInitialization(), 5343 E->getConstructionKind(), 5344 Importer.Import(E->getParenOrBraceRange())); 5345 } 5346 5347 Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { 5348 QualType T = Importer.Import(E->getType()); 5349 if (T.isNull()) 5350 return nullptr; 5351 5352 Expr *ToFn = Importer.Import(E->getCallee()); 5353 if (!ToFn) 5354 return nullptr; 5355 5356 ArrayRef<Expr *> ToArgs; 5357 5358 if (!ImportArray(E->arg_begin(), E->arg_end(), ToArgs)) 5359 return nullptr; 5360 5361 return new (Importer.getToContext()) CXXMemberCallExpr(Importer.getToContext(), ToFn, 5362 ToArgs, T, E->getValueKind(), 5363 Importer.Import(E->getRParenLoc())); 5364 } 5365 5366 Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) { 5367 QualType T = Importer.Import(E->getType()); 5368 if (T.isNull()) 5369 return nullptr; 5370 5371 return new (Importer.getToContext()) 5372 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit()); 5373 } 5374 5375 Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { 5376 QualType T = Importer.Import(E->getType()); 5377 if (T.isNull()) 5378 return nullptr; 5379 5380 return new (Importer.getToContext()) 5381 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation())); 5382 } 5383 5384 5385 Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) { 5386 QualType T = Importer.Import(E->getType()); 5387 if (T.isNull()) 5388 return nullptr; 5389 5390 Expr *ToBase = Importer.Import(E->getBase()); 5391 if (!ToBase && E->getBase()) 5392 return nullptr; 5393 5394 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl())); 5395 if (!ToMember && E->getMemberDecl()) 5396 return nullptr; 5397 5398 DeclAccessPair ToFoundDecl = DeclAccessPair::make( 5399 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())), 5400 E->getFoundDecl().getAccess()); 5401 5402 DeclarationNameInfo ToMemberNameInfo( 5403 Importer.Import(E->getMemberNameInfo().getName()), 5404 Importer.Import(E->getMemberNameInfo().getLoc())); 5405 5406 if (E->hasExplicitTemplateArgs()) { 5407 return nullptr; // FIXME: handle template arguments 5408 } 5409 5410 return MemberExpr::Create(Importer.getToContext(), ToBase, 5411 E->isArrow(), 5412 Importer.Import(E->getOperatorLoc()), 5413 Importer.Import(E->getQualifierLoc()), 5414 Importer.Import(E->getTemplateKeywordLoc()), 5415 ToMember, ToFoundDecl, ToMemberNameInfo, 5416 nullptr, T, E->getValueKind(), 5417 E->getObjectKind()); 5418 } 5419 5420 Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) { 5421 QualType T = Importer.Import(E->getType()); 5422 if (T.isNull()) 5423 return nullptr; 5424 5425 Expr *ToCallee = Importer.Import(E->getCallee()); 5426 if (!ToCallee && E->getCallee()) 5427 return nullptr; 5428 5429 unsigned NumArgs = E->getNumArgs(); 5430 5431 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs); 5432 5433 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) { 5434 Expr *FromArg = E->getArg(ai); 5435 Expr *ToArg = Importer.Import(FromArg); 5436 if (!ToArg) 5437 return nullptr; 5438 ToArgs[ai] = ToArg; 5439 } 5440 5441 Expr **ToArgs_Copied = new (Importer.getToContext()) 5442 Expr*[NumArgs]; 5443 5444 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) 5445 ToArgs_Copied[ai] = ToArgs[ai]; 5446 5447 return new (Importer.getToContext()) 5448 CallExpr(Importer.getToContext(), ToCallee, 5449 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(), 5450 Importer.Import(E->getRParenLoc())); 5451 } 5452 5453 Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *E) { 5454 QualType T = Importer.Import(E->getType()); 5455 if (T.isNull()) 5456 return nullptr; 5457 5458 ArrayRef<Expr *> ToInits; 5459 5460 if (!ImportArray(E->inits().begin(), E->inits().end(), ToInits)) 5461 return nullptr; 5462 5463 InitListExpr *ToE = new (Importer.getToContext()) 5464 InitListExpr(Importer.getToContext(), 5465 Importer.Import(E->getLBraceLoc()), 5466 ToInits, 5467 Importer.Import(E->getRBraceLoc())); 5468 5469 if (ToE) 5470 ToE->setType(T); 5471 5472 return ToE; 5473 } 5474 5475 ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, 5476 ASTContext &FromContext, FileManager &FromFileManager, 5477 bool MinimalImport) 5478 : ToContext(ToContext), FromContext(FromContext), 5479 ToFileManager(ToFileManager), FromFileManager(FromFileManager), 5480 Minimal(MinimalImport), LastDiagFromFrom(false) 5481 { 5482 ImportedDecls[FromContext.getTranslationUnitDecl()] 5483 = ToContext.getTranslationUnitDecl(); 5484 } 5485 5486 ASTImporter::~ASTImporter() { } 5487 5488 QualType ASTImporter::Import(QualType FromT) { 5489 if (FromT.isNull()) 5490 return QualType(); 5491 5492 const Type *fromTy = FromT.getTypePtr(); 5493 5494 // Check whether we've already imported this type. 5495 llvm::DenseMap<const Type *, const Type *>::iterator Pos 5496 = ImportedTypes.find(fromTy); 5497 if (Pos != ImportedTypes.end()) 5498 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers()); 5499 5500 // Import the type 5501 ASTNodeImporter Importer(*this); 5502 QualType ToT = Importer.Visit(fromTy); 5503 if (ToT.isNull()) 5504 return ToT; 5505 5506 // Record the imported type. 5507 ImportedTypes[fromTy] = ToT.getTypePtr(); 5508 5509 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers()); 5510 } 5511 5512 TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { 5513 if (!FromTSI) 5514 return FromTSI; 5515 5516 // FIXME: For now we just create a "trivial" type source info based 5517 // on the type and a single location. Implement a real version of this. 5518 QualType T = Import(FromTSI->getType()); 5519 if (T.isNull()) 5520 return nullptr; 5521 5522 return ToContext.getTrivialTypeSourceInfo(T, 5523 Import(FromTSI->getTypeLoc().getLocStart())); 5524 } 5525 5526 Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) { 5527 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); 5528 if (Pos != ImportedDecls.end()) { 5529 Decl *ToD = Pos->second; 5530 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD); 5531 return ToD; 5532 } else { 5533 return nullptr; 5534 } 5535 } 5536 5537 Decl *ASTImporter::Import(Decl *FromD) { 5538 if (!FromD) 5539 return nullptr; 5540 5541 ASTNodeImporter Importer(*this); 5542 5543 // Check whether we've already imported this declaration. 5544 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); 5545 if (Pos != ImportedDecls.end()) { 5546 Decl *ToD = Pos->second; 5547 Importer.ImportDefinitionIfNeeded(FromD, ToD); 5548 return ToD; 5549 } 5550 5551 // Import the type 5552 Decl *ToD = Importer.Visit(FromD); 5553 if (!ToD) 5554 return nullptr; 5555 5556 // Record the imported declaration. 5557 ImportedDecls[FromD] = ToD; 5558 5559 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) { 5560 // Keep track of anonymous tags that have an associated typedef. 5561 if (FromTag->getTypedefNameForAnonDecl()) 5562 AnonTagsWithPendingTypedefs.push_back(FromTag); 5563 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) { 5564 // When we've finished transforming a typedef, see whether it was the 5565 // typedef for an anonymous tag. 5566 for (SmallVectorImpl<TagDecl *>::iterator 5567 FromTag = AnonTagsWithPendingTypedefs.begin(), 5568 FromTagEnd = AnonTagsWithPendingTypedefs.end(); 5569 FromTag != FromTagEnd; ++FromTag) { 5570 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) { 5571 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) { 5572 // We found the typedef for an anonymous tag; link them. 5573 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD)); 5574 AnonTagsWithPendingTypedefs.erase(FromTag); 5575 break; 5576 } 5577 } 5578 } 5579 } 5580 5581 return ToD; 5582 } 5583 5584 DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) { 5585 if (!FromDC) 5586 return FromDC; 5587 5588 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC))); 5589 if (!ToDC) 5590 return nullptr; 5591 5592 // When we're using a record/enum/Objective-C class/protocol as a context, we 5593 // need it to have a definition. 5594 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) { 5595 RecordDecl *FromRecord = cast<RecordDecl>(FromDC); 5596 if (ToRecord->isCompleteDefinition()) { 5597 // Do nothing. 5598 } else if (FromRecord->isCompleteDefinition()) { 5599 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord, 5600 ASTNodeImporter::IDK_Basic); 5601 } else { 5602 CompleteDecl(ToRecord); 5603 } 5604 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) { 5605 EnumDecl *FromEnum = cast<EnumDecl>(FromDC); 5606 if (ToEnum->isCompleteDefinition()) { 5607 // Do nothing. 5608 } else if (FromEnum->isCompleteDefinition()) { 5609 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum, 5610 ASTNodeImporter::IDK_Basic); 5611 } else { 5612 CompleteDecl(ToEnum); 5613 } 5614 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) { 5615 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC); 5616 if (ToClass->getDefinition()) { 5617 // Do nothing. 5618 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { 5619 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass, 5620 ASTNodeImporter::IDK_Basic); 5621 } else { 5622 CompleteDecl(ToClass); 5623 } 5624 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) { 5625 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC); 5626 if (ToProto->getDefinition()) { 5627 // Do nothing. 5628 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { 5629 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto, 5630 ASTNodeImporter::IDK_Basic); 5631 } else { 5632 CompleteDecl(ToProto); 5633 } 5634 } 5635 5636 return ToDC; 5637 } 5638 5639 Expr *ASTImporter::Import(Expr *FromE) { 5640 if (!FromE) 5641 return nullptr; 5642 5643 return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); 5644 } 5645 5646 Stmt *ASTImporter::Import(Stmt *FromS) { 5647 if (!FromS) 5648 return nullptr; 5649 5650 // Check whether we've already imported this declaration. 5651 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); 5652 if (Pos != ImportedStmts.end()) 5653 return Pos->second; 5654 5655 // Import the type 5656 ASTNodeImporter Importer(*this); 5657 Stmt *ToS = Importer.Visit(FromS); 5658 if (!ToS) 5659 return nullptr; 5660 5661 // Record the imported declaration. 5662 ImportedStmts[FromS] = ToS; 5663 return ToS; 5664 } 5665 5666 NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { 5667 if (!FromNNS) 5668 return nullptr; 5669 5670 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix()); 5671 5672 switch (FromNNS->getKind()) { 5673 case NestedNameSpecifier::Identifier: 5674 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) { 5675 return NestedNameSpecifier::Create(ToContext, prefix, II); 5676 } 5677 return nullptr; 5678 5679 case NestedNameSpecifier::Namespace: 5680 if (NamespaceDecl *NS = 5681 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) { 5682 return NestedNameSpecifier::Create(ToContext, prefix, NS); 5683 } 5684 return nullptr; 5685 5686 case NestedNameSpecifier::NamespaceAlias: 5687 if (NamespaceAliasDecl *NSAD = 5688 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) { 5689 return NestedNameSpecifier::Create(ToContext, prefix, NSAD); 5690 } 5691 return nullptr; 5692 5693 case NestedNameSpecifier::Global: 5694 return NestedNameSpecifier::GlobalSpecifier(ToContext); 5695 5696 case NestedNameSpecifier::Super: 5697 if (CXXRecordDecl *RD = 5698 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) { 5699 return NestedNameSpecifier::SuperSpecifier(ToContext, RD); 5700 } 5701 return nullptr; 5702 5703 case NestedNameSpecifier::TypeSpec: 5704 case NestedNameSpecifier::TypeSpecWithTemplate: { 5705 QualType T = Import(QualType(FromNNS->getAsType(), 0u)); 5706 if (!T.isNull()) { 5707 bool bTemplate = FromNNS->getKind() == 5708 NestedNameSpecifier::TypeSpecWithTemplate; 5709 return NestedNameSpecifier::Create(ToContext, prefix, 5710 bTemplate, T.getTypePtr()); 5711 } 5712 } 5713 return nullptr; 5714 } 5715 5716 llvm_unreachable("Invalid nested name specifier kind"); 5717 } 5718 5719 NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { 5720 // FIXME: Implement! 5721 return NestedNameSpecifierLoc(); 5722 } 5723 5724 TemplateName ASTImporter::Import(TemplateName From) { 5725 switch (From.getKind()) { 5726 case TemplateName::Template: 5727 if (TemplateDecl *ToTemplate 5728 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) 5729 return TemplateName(ToTemplate); 5730 5731 return TemplateName(); 5732 5733 case TemplateName::OverloadedTemplate: { 5734 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); 5735 UnresolvedSet<2> ToTemplates; 5736 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(), 5737 E = FromStorage->end(); 5738 I != E; ++I) { 5739 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I))) 5740 ToTemplates.addDecl(To); 5741 else 5742 return TemplateName(); 5743 } 5744 return ToContext.getOverloadedTemplateName(ToTemplates.begin(), 5745 ToTemplates.end()); 5746 } 5747 5748 case TemplateName::QualifiedTemplate: { 5749 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); 5750 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier()); 5751 if (!Qualifier) 5752 return TemplateName(); 5753 5754 if (TemplateDecl *ToTemplate 5755 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) 5756 return ToContext.getQualifiedTemplateName(Qualifier, 5757 QTN->hasTemplateKeyword(), 5758 ToTemplate); 5759 5760 return TemplateName(); 5761 } 5762 5763 case TemplateName::DependentTemplate: { 5764 DependentTemplateName *DTN = From.getAsDependentTemplateName(); 5765 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier()); 5766 if (!Qualifier) 5767 return TemplateName(); 5768 5769 if (DTN->isIdentifier()) { 5770 return ToContext.getDependentTemplateName(Qualifier, 5771 Import(DTN->getIdentifier())); 5772 } 5773 5774 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator()); 5775 } 5776 5777 case TemplateName::SubstTemplateTemplateParm: { 5778 SubstTemplateTemplateParmStorage *subst 5779 = From.getAsSubstTemplateTemplateParm(); 5780 TemplateTemplateParmDecl *param 5781 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter())); 5782 if (!param) 5783 return TemplateName(); 5784 5785 TemplateName replacement = Import(subst->getReplacement()); 5786 if (replacement.isNull()) return TemplateName(); 5787 5788 return ToContext.getSubstTemplateTemplateParm(param, replacement); 5789 } 5790 5791 case TemplateName::SubstTemplateTemplateParmPack: { 5792 SubstTemplateTemplateParmPackStorage *SubstPack 5793 = From.getAsSubstTemplateTemplateParmPack(); 5794 TemplateTemplateParmDecl *Param 5795 = cast_or_null<TemplateTemplateParmDecl>( 5796 Import(SubstPack->getParameterPack())); 5797 if (!Param) 5798 return TemplateName(); 5799 5800 ASTNodeImporter Importer(*this); 5801 TemplateArgument ArgPack 5802 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); 5803 if (ArgPack.isNull()) 5804 return TemplateName(); 5805 5806 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack); 5807 } 5808 } 5809 5810 llvm_unreachable("Invalid template name kind"); 5811 } 5812 5813 SourceLocation ASTImporter::Import(SourceLocation FromLoc) { 5814 if (FromLoc.isInvalid()) 5815 return SourceLocation(); 5816 5817 SourceManager &FromSM = FromContext.getSourceManager(); 5818 5819 // For now, map everything down to its spelling location, so that we 5820 // don't have to import macro expansions. 5821 // FIXME: Import macro expansions! 5822 FromLoc = FromSM.getSpellingLoc(FromLoc); 5823 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); 5824 SourceManager &ToSM = ToContext.getSourceManager(); 5825 FileID ToFileID = Import(Decomposed.first); 5826 if (ToFileID.isInvalid()) 5827 return SourceLocation(); 5828 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID) 5829 .getLocWithOffset(Decomposed.second); 5830 return ret; 5831 } 5832 5833 SourceRange ASTImporter::Import(SourceRange FromRange) { 5834 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); 5835 } 5836 5837 FileID ASTImporter::Import(FileID FromID) { 5838 llvm::DenseMap<FileID, FileID>::iterator Pos 5839 = ImportedFileIDs.find(FromID); 5840 if (Pos != ImportedFileIDs.end()) 5841 return Pos->second; 5842 5843 SourceManager &FromSM = FromContext.getSourceManager(); 5844 SourceManager &ToSM = ToContext.getSourceManager(); 5845 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); 5846 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet"); 5847 5848 // Include location of this file. 5849 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); 5850 5851 // Map the FileID for to the "to" source manager. 5852 FileID ToID; 5853 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); 5854 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { 5855 // FIXME: We probably want to use getVirtualFile(), so we don't hit the 5856 // disk again 5857 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather 5858 // than mmap the files several times. 5859 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName()); 5860 if (!Entry) 5861 return FileID(); 5862 ToID = ToSM.createFileID(Entry, ToIncludeLoc, 5863 FromSLoc.getFile().getFileCharacteristic()); 5864 } else { 5865 // FIXME: We want to re-use the existing MemoryBuffer! 5866 const llvm::MemoryBuffer * 5867 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM); 5868 std::unique_ptr<llvm::MemoryBuffer> ToBuf 5869 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(), 5870 FromBuf->getBufferIdentifier()); 5871 ToID = ToSM.createFileID(std::move(ToBuf), 5872 FromSLoc.getFile().getFileCharacteristic()); 5873 } 5874 5875 5876 ImportedFileIDs[FromID] = ToID; 5877 return ToID; 5878 } 5879 5880 void ASTImporter::ImportDefinition(Decl *From) { 5881 Decl *To = Import(From); 5882 if (!To) 5883 return; 5884 5885 if (DeclContext *FromDC = cast<DeclContext>(From)) { 5886 ASTNodeImporter Importer(*this); 5887 5888 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) { 5889 if (!ToRecord->getDefinition()) { 5890 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord, 5891 ASTNodeImporter::IDK_Everything); 5892 return; 5893 } 5894 } 5895 5896 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) { 5897 if (!ToEnum->getDefinition()) { 5898 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum, 5899 ASTNodeImporter::IDK_Everything); 5900 return; 5901 } 5902 } 5903 5904 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) { 5905 if (!ToIFace->getDefinition()) { 5906 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace, 5907 ASTNodeImporter::IDK_Everything); 5908 return; 5909 } 5910 } 5911 5912 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) { 5913 if (!ToProto->getDefinition()) { 5914 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto, 5915 ASTNodeImporter::IDK_Everything); 5916 return; 5917 } 5918 } 5919 5920 Importer.ImportDeclContext(FromDC, true); 5921 } 5922 } 5923 5924 DeclarationName ASTImporter::Import(DeclarationName FromName) { 5925 if (!FromName) 5926 return DeclarationName(); 5927 5928 switch (FromName.getNameKind()) { 5929 case DeclarationName::Identifier: 5930 return Import(FromName.getAsIdentifierInfo()); 5931 5932 case DeclarationName::ObjCZeroArgSelector: 5933 case DeclarationName::ObjCOneArgSelector: 5934 case DeclarationName::ObjCMultiArgSelector: 5935 return Import(FromName.getObjCSelector()); 5936 5937 case DeclarationName::CXXConstructorName: { 5938 QualType T = Import(FromName.getCXXNameType()); 5939 if (T.isNull()) 5940 return DeclarationName(); 5941 5942 return ToContext.DeclarationNames.getCXXConstructorName( 5943 ToContext.getCanonicalType(T)); 5944 } 5945 5946 case DeclarationName::CXXDestructorName: { 5947 QualType T = Import(FromName.getCXXNameType()); 5948 if (T.isNull()) 5949 return DeclarationName(); 5950 5951 return ToContext.DeclarationNames.getCXXDestructorName( 5952 ToContext.getCanonicalType(T)); 5953 } 5954 5955 case DeclarationName::CXXConversionFunctionName: { 5956 QualType T = Import(FromName.getCXXNameType()); 5957 if (T.isNull()) 5958 return DeclarationName(); 5959 5960 return ToContext.DeclarationNames.getCXXConversionFunctionName( 5961 ToContext.getCanonicalType(T)); 5962 } 5963 5964 case DeclarationName::CXXOperatorName: 5965 return ToContext.DeclarationNames.getCXXOperatorName( 5966 FromName.getCXXOverloadedOperator()); 5967 5968 case DeclarationName::CXXLiteralOperatorName: 5969 return ToContext.DeclarationNames.getCXXLiteralOperatorName( 5970 Import(FromName.getCXXLiteralIdentifier())); 5971 5972 case DeclarationName::CXXUsingDirective: 5973 // FIXME: STATICS! 5974 return DeclarationName::getUsingDirectiveName(); 5975 } 5976 5977 llvm_unreachable("Invalid DeclarationName Kind!"); 5978 } 5979 5980 IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { 5981 if (!FromId) 5982 return nullptr; 5983 5984 return &ToContext.Idents.get(FromId->getName()); 5985 } 5986 5987 Selector ASTImporter::Import(Selector FromSel) { 5988 if (FromSel.isNull()) 5989 return Selector(); 5990 5991 SmallVector<IdentifierInfo *, 4> Idents; 5992 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); 5993 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) 5994 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); 5995 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); 5996 } 5997 5998 DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name, 5999 DeclContext *DC, 6000 unsigned IDNS, 6001 NamedDecl **Decls, 6002 unsigned NumDecls) { 6003 return Name; 6004 } 6005 6006 DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { 6007 if (LastDiagFromFrom) 6008 ToContext.getDiagnostics().notePriorDiagnosticFrom( 6009 FromContext.getDiagnostics()); 6010 LastDiagFromFrom = false; 6011 return ToContext.getDiagnostics().Report(Loc, DiagID); 6012 } 6013 6014 DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { 6015 if (!LastDiagFromFrom) 6016 FromContext.getDiagnostics().notePriorDiagnosticFrom( 6017 ToContext.getDiagnostics()); 6018 LastDiagFromFrom = true; 6019 return FromContext.getDiagnostics().Report(Loc, DiagID); 6020 } 6021 6022 void ASTImporter::CompleteDecl (Decl *D) { 6023 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { 6024 if (!ID->getDefinition()) 6025 ID->startDefinition(); 6026 } 6027 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { 6028 if (!PD->getDefinition()) 6029 PD->startDefinition(); 6030 } 6031 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 6032 if (!TD->getDefinition() && !TD->isBeingDefined()) { 6033 TD->startDefinition(); 6034 TD->setCompleteDefinition(true); 6035 } 6036 } 6037 else { 6038 assert (0 && "CompleteDecl called on a Decl that can't be completed"); 6039 } 6040 } 6041 6042 Decl *ASTImporter::Imported(Decl *From, Decl *To) { 6043 if (From->hasAttrs()) { 6044 for (Attr *FromAttr : From->getAttrs()) 6045 To->addAttr(FromAttr->clone(To->getASTContext())); 6046 } 6047 if (From->isUsed()) { 6048 To->setIsUsed(); 6049 } 6050 ImportedDecls[From] = To; 6051 return To; 6052 } 6053 6054 bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, 6055 bool Complain) { 6056 llvm::DenseMap<const Type *, const Type *>::iterator Pos 6057 = ImportedTypes.find(From.getTypePtr()); 6058 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To)) 6059 return true; 6060 6061 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls, 6062 false, Complain); 6063 return Ctx.IsStructurallyEquivalent(From, To); 6064 } 6065