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 IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 1513 case BuiltinType::Id: \ 1514 return Importer.getToContext().SingletonId; 1515 #include "clang/AST/OpenCLImageTypes.def" 1516 #define SHARED_SINGLETON_TYPE(Expansion) 1517 #define BUILTIN_TYPE(Id, SingletonId) \ 1518 case BuiltinType::Id: return Importer.getToContext().SingletonId; 1519 #include "clang/AST/BuiltinTypes.def" 1520 1521 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" 1522 // context supports C++. 1523 1524 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" 1525 // context supports ObjC. 1526 1527 case BuiltinType::Char_U: 1528 // The context we're importing from has an unsigned 'char'. If we're 1529 // importing into a context with a signed 'char', translate to 1530 // 'unsigned char' instead. 1531 if (Importer.getToContext().getLangOpts().CharIsSigned) 1532 return Importer.getToContext().UnsignedCharTy; 1533 1534 return Importer.getToContext().CharTy; 1535 1536 case BuiltinType::Char_S: 1537 // The context we're importing from has an unsigned 'char'. If we're 1538 // importing into a context with a signed 'char', translate to 1539 // 'unsigned char' instead. 1540 if (!Importer.getToContext().getLangOpts().CharIsSigned) 1541 return Importer.getToContext().SignedCharTy; 1542 1543 return Importer.getToContext().CharTy; 1544 1545 case BuiltinType::WChar_S: 1546 case BuiltinType::WChar_U: 1547 // FIXME: If not in C++, shall we translate to the C equivalent of 1548 // wchar_t? 1549 return Importer.getToContext().WCharTy; 1550 } 1551 1552 llvm_unreachable("Invalid BuiltinType Kind!"); 1553 } 1554 1555 QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) { 1556 QualType ToElementType = Importer.Import(T->getElementType()); 1557 if (ToElementType.isNull()) 1558 return QualType(); 1559 1560 return Importer.getToContext().getComplexType(ToElementType); 1561 } 1562 1563 QualType ASTNodeImporter::VisitPointerType(const PointerType *T) { 1564 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1565 if (ToPointeeType.isNull()) 1566 return QualType(); 1567 1568 return Importer.getToContext().getPointerType(ToPointeeType); 1569 } 1570 1571 QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { 1572 // FIXME: Check for blocks support in "to" context. 1573 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1574 if (ToPointeeType.isNull()) 1575 return QualType(); 1576 1577 return Importer.getToContext().getBlockPointerType(ToPointeeType); 1578 } 1579 1580 QualType 1581 ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { 1582 // FIXME: Check for C++ support in "to" context. 1583 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); 1584 if (ToPointeeType.isNull()) 1585 return QualType(); 1586 1587 return Importer.getToContext().getLValueReferenceType(ToPointeeType); 1588 } 1589 1590 QualType 1591 ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { 1592 // FIXME: Check for C++0x support in "to" context. 1593 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); 1594 if (ToPointeeType.isNull()) 1595 return QualType(); 1596 1597 return Importer.getToContext().getRValueReferenceType(ToPointeeType); 1598 } 1599 1600 QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { 1601 // FIXME: Check for C++ support in "to" context. 1602 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1603 if (ToPointeeType.isNull()) 1604 return QualType(); 1605 1606 QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); 1607 return Importer.getToContext().getMemberPointerType(ToPointeeType, 1608 ClassType.getTypePtr()); 1609 } 1610 1611 QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { 1612 QualType ToElementType = Importer.Import(T->getElementType()); 1613 if (ToElementType.isNull()) 1614 return QualType(); 1615 1616 return Importer.getToContext().getConstantArrayType(ToElementType, 1617 T->getSize(), 1618 T->getSizeModifier(), 1619 T->getIndexTypeCVRQualifiers()); 1620 } 1621 1622 QualType 1623 ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { 1624 QualType ToElementType = Importer.Import(T->getElementType()); 1625 if (ToElementType.isNull()) 1626 return QualType(); 1627 1628 return Importer.getToContext().getIncompleteArrayType(ToElementType, 1629 T->getSizeModifier(), 1630 T->getIndexTypeCVRQualifiers()); 1631 } 1632 1633 QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { 1634 QualType ToElementType = Importer.Import(T->getElementType()); 1635 if (ToElementType.isNull()) 1636 return QualType(); 1637 1638 Expr *Size = Importer.Import(T->getSizeExpr()); 1639 if (!Size) 1640 return QualType(); 1641 1642 SourceRange Brackets = Importer.Import(T->getBracketsRange()); 1643 return Importer.getToContext().getVariableArrayType(ToElementType, Size, 1644 T->getSizeModifier(), 1645 T->getIndexTypeCVRQualifiers(), 1646 Brackets); 1647 } 1648 1649 QualType ASTNodeImporter::VisitVectorType(const VectorType *T) { 1650 QualType ToElementType = Importer.Import(T->getElementType()); 1651 if (ToElementType.isNull()) 1652 return QualType(); 1653 1654 return Importer.getToContext().getVectorType(ToElementType, 1655 T->getNumElements(), 1656 T->getVectorKind()); 1657 } 1658 1659 QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { 1660 QualType ToElementType = Importer.Import(T->getElementType()); 1661 if (ToElementType.isNull()) 1662 return QualType(); 1663 1664 return Importer.getToContext().getExtVectorType(ToElementType, 1665 T->getNumElements()); 1666 } 1667 1668 QualType 1669 ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { 1670 // FIXME: What happens if we're importing a function without a prototype 1671 // into C++? Should we make it variadic? 1672 QualType ToResultType = Importer.Import(T->getReturnType()); 1673 if (ToResultType.isNull()) 1674 return QualType(); 1675 1676 return Importer.getToContext().getFunctionNoProtoType(ToResultType, 1677 T->getExtInfo()); 1678 } 1679 1680 QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { 1681 QualType ToResultType = Importer.Import(T->getReturnType()); 1682 if (ToResultType.isNull()) 1683 return QualType(); 1684 1685 // Import argument types 1686 SmallVector<QualType, 4> ArgTypes; 1687 for (const auto &A : T->param_types()) { 1688 QualType ArgType = Importer.Import(A); 1689 if (ArgType.isNull()) 1690 return QualType(); 1691 ArgTypes.push_back(ArgType); 1692 } 1693 1694 // Import exception types 1695 SmallVector<QualType, 4> ExceptionTypes; 1696 for (const auto &E : T->exceptions()) { 1697 QualType ExceptionType = Importer.Import(E); 1698 if (ExceptionType.isNull()) 1699 return QualType(); 1700 ExceptionTypes.push_back(ExceptionType); 1701 } 1702 1703 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); 1704 FunctionProtoType::ExtProtoInfo ToEPI; 1705 1706 ToEPI.ExtInfo = FromEPI.ExtInfo; 1707 ToEPI.Variadic = FromEPI.Variadic; 1708 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; 1709 ToEPI.TypeQuals = FromEPI.TypeQuals; 1710 ToEPI.RefQualifier = FromEPI.RefQualifier; 1711 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; 1712 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; 1713 ToEPI.ExceptionSpec.NoexceptExpr = 1714 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr); 1715 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>( 1716 Importer.Import(FromEPI.ExceptionSpec.SourceDecl)); 1717 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>( 1718 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate)); 1719 1720 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI); 1721 } 1722 1723 QualType ASTNodeImporter::VisitParenType(const ParenType *T) { 1724 QualType ToInnerType = Importer.Import(T->getInnerType()); 1725 if (ToInnerType.isNull()) 1726 return QualType(); 1727 1728 return Importer.getToContext().getParenType(ToInnerType); 1729 } 1730 1731 QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { 1732 TypedefNameDecl *ToDecl 1733 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl())); 1734 if (!ToDecl) 1735 return QualType(); 1736 1737 return Importer.getToContext().getTypeDeclType(ToDecl); 1738 } 1739 1740 QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { 1741 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); 1742 if (!ToExpr) 1743 return QualType(); 1744 1745 return Importer.getToContext().getTypeOfExprType(ToExpr); 1746 } 1747 1748 QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { 1749 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); 1750 if (ToUnderlyingType.isNull()) 1751 return QualType(); 1752 1753 return Importer.getToContext().getTypeOfType(ToUnderlyingType); 1754 } 1755 1756 QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { 1757 // FIXME: Make sure that the "to" context supports C++0x! 1758 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); 1759 if (!ToExpr) 1760 return QualType(); 1761 1762 QualType UnderlyingType = Importer.Import(T->getUnderlyingType()); 1763 if (UnderlyingType.isNull()) 1764 return QualType(); 1765 1766 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType); 1767 } 1768 1769 QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { 1770 QualType ToBaseType = Importer.Import(T->getBaseType()); 1771 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); 1772 if (ToBaseType.isNull() || ToUnderlyingType.isNull()) 1773 return QualType(); 1774 1775 return Importer.getToContext().getUnaryTransformType(ToBaseType, 1776 ToUnderlyingType, 1777 T->getUTTKind()); 1778 } 1779 1780 QualType ASTNodeImporter::VisitAutoType(const AutoType *T) { 1781 // FIXME: Make sure that the "to" context supports C++11! 1782 QualType FromDeduced = T->getDeducedType(); 1783 QualType ToDeduced; 1784 if (!FromDeduced.isNull()) { 1785 ToDeduced = Importer.Import(FromDeduced); 1786 if (ToDeduced.isNull()) 1787 return QualType(); 1788 } 1789 1790 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(), 1791 /*IsDependent*/false); 1792 } 1793 1794 QualType ASTNodeImporter::VisitRecordType(const RecordType *T) { 1795 RecordDecl *ToDecl 1796 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); 1797 if (!ToDecl) 1798 return QualType(); 1799 1800 return Importer.getToContext().getTagDeclType(ToDecl); 1801 } 1802 1803 QualType ASTNodeImporter::VisitEnumType(const EnumType *T) { 1804 EnumDecl *ToDecl 1805 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl())); 1806 if (!ToDecl) 1807 return QualType(); 1808 1809 return Importer.getToContext().getTagDeclType(ToDecl); 1810 } 1811 1812 QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { 1813 QualType FromModifiedType = T->getModifiedType(); 1814 QualType FromEquivalentType = T->getEquivalentType(); 1815 QualType ToModifiedType; 1816 QualType ToEquivalentType; 1817 1818 if (!FromModifiedType.isNull()) { 1819 ToModifiedType = Importer.Import(FromModifiedType); 1820 if (ToModifiedType.isNull()) 1821 return QualType(); 1822 } 1823 if (!FromEquivalentType.isNull()) { 1824 ToEquivalentType = Importer.Import(FromEquivalentType); 1825 if (ToEquivalentType.isNull()) 1826 return QualType(); 1827 } 1828 1829 return Importer.getToContext().getAttributedType(T->getAttrKind(), 1830 ToModifiedType, ToEquivalentType); 1831 } 1832 1833 QualType ASTNodeImporter::VisitTemplateSpecializationType( 1834 const TemplateSpecializationType *T) { 1835 TemplateName ToTemplate = Importer.Import(T->getTemplateName()); 1836 if (ToTemplate.isNull()) 1837 return QualType(); 1838 1839 SmallVector<TemplateArgument, 2> ToTemplateArgs; 1840 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs)) 1841 return QualType(); 1842 1843 QualType ToCanonType; 1844 if (!QualType(T, 0).isCanonical()) { 1845 QualType FromCanonType 1846 = Importer.getFromContext().getCanonicalType(QualType(T, 0)); 1847 ToCanonType =Importer.Import(FromCanonType); 1848 if (ToCanonType.isNull()) 1849 return QualType(); 1850 } 1851 return Importer.getToContext().getTemplateSpecializationType(ToTemplate, 1852 ToTemplateArgs.data(), 1853 ToTemplateArgs.size(), 1854 ToCanonType); 1855 } 1856 1857 QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { 1858 NestedNameSpecifier *ToQualifier = nullptr; 1859 // Note: the qualifier in an ElaboratedType is optional. 1860 if (T->getQualifier()) { 1861 ToQualifier = Importer.Import(T->getQualifier()); 1862 if (!ToQualifier) 1863 return QualType(); 1864 } 1865 1866 QualType ToNamedType = Importer.Import(T->getNamedType()); 1867 if (ToNamedType.isNull()) 1868 return QualType(); 1869 1870 return Importer.getToContext().getElaboratedType(T->getKeyword(), 1871 ToQualifier, ToNamedType); 1872 } 1873 1874 QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { 1875 ObjCInterfaceDecl *Class 1876 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl())); 1877 if (!Class) 1878 return QualType(); 1879 1880 return Importer.getToContext().getObjCInterfaceType(Class); 1881 } 1882 1883 QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { 1884 QualType ToBaseType = Importer.Import(T->getBaseType()); 1885 if (ToBaseType.isNull()) 1886 return QualType(); 1887 1888 SmallVector<QualType, 4> TypeArgs; 1889 for (auto TypeArg : T->getTypeArgsAsWritten()) { 1890 QualType ImportedTypeArg = Importer.Import(TypeArg); 1891 if (ImportedTypeArg.isNull()) 1892 return QualType(); 1893 1894 TypeArgs.push_back(ImportedTypeArg); 1895 } 1896 1897 SmallVector<ObjCProtocolDecl *, 4> Protocols; 1898 for (auto *P : T->quals()) { 1899 ObjCProtocolDecl *Protocol 1900 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P)); 1901 if (!Protocol) 1902 return QualType(); 1903 Protocols.push_back(Protocol); 1904 } 1905 1906 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs, 1907 Protocols, 1908 T->isKindOfTypeAsWritten()); 1909 } 1910 1911 QualType 1912 ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { 1913 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1914 if (ToPointeeType.isNull()) 1915 return QualType(); 1916 1917 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType); 1918 } 1919 1920 //---------------------------------------------------------------------------- 1921 // Import Declarations 1922 //---------------------------------------------------------------------------- 1923 bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC, 1924 DeclContext *&LexicalDC, 1925 DeclarationName &Name, 1926 NamedDecl *&ToD, 1927 SourceLocation &Loc) { 1928 // Import the context of this declaration. 1929 DC = Importer.ImportContext(D->getDeclContext()); 1930 if (!DC) 1931 return true; 1932 1933 LexicalDC = DC; 1934 if (D->getDeclContext() != D->getLexicalDeclContext()) { 1935 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 1936 if (!LexicalDC) 1937 return true; 1938 } 1939 1940 // Import the name of this declaration. 1941 Name = Importer.Import(D->getDeclName()); 1942 if (D->getDeclName() && !Name) 1943 return true; 1944 1945 // Import the location of this declaration. 1946 Loc = Importer.Import(D->getLocation()); 1947 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D)); 1948 return false; 1949 } 1950 1951 void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { 1952 if (!FromD) 1953 return; 1954 1955 if (!ToD) { 1956 ToD = Importer.Import(FromD); 1957 if (!ToD) 1958 return; 1959 } 1960 1961 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) { 1962 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) { 1963 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) { 1964 ImportDefinition(FromRecord, ToRecord); 1965 } 1966 } 1967 return; 1968 } 1969 1970 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) { 1971 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) { 1972 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { 1973 ImportDefinition(FromEnum, ToEnum); 1974 } 1975 } 1976 return; 1977 } 1978 } 1979 1980 void 1981 ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From, 1982 DeclarationNameInfo& To) { 1983 // NOTE: To.Name and To.Loc are already imported. 1984 // We only have to import To.LocInfo. 1985 switch (To.getName().getNameKind()) { 1986 case DeclarationName::Identifier: 1987 case DeclarationName::ObjCZeroArgSelector: 1988 case DeclarationName::ObjCOneArgSelector: 1989 case DeclarationName::ObjCMultiArgSelector: 1990 case DeclarationName::CXXUsingDirective: 1991 return; 1992 1993 case DeclarationName::CXXOperatorName: { 1994 SourceRange Range = From.getCXXOperatorNameRange(); 1995 To.setCXXOperatorNameRange(Importer.Import(Range)); 1996 return; 1997 } 1998 case DeclarationName::CXXLiteralOperatorName: { 1999 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc(); 2000 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc)); 2001 return; 2002 } 2003 case DeclarationName::CXXConstructorName: 2004 case DeclarationName::CXXDestructorName: 2005 case DeclarationName::CXXConversionFunctionName: { 2006 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo(); 2007 To.setNamedTypeInfo(Importer.Import(FromTInfo)); 2008 return; 2009 } 2010 } 2011 llvm_unreachable("Unknown name kind."); 2012 } 2013 2014 void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { 2015 if (Importer.isMinimalImport() && !ForceImport) { 2016 Importer.ImportContext(FromDC); 2017 return; 2018 } 2019 2020 for (auto *From : FromDC->decls()) 2021 Importer.Import(From); 2022 } 2023 2024 bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To, 2025 ImportDefinitionKind Kind) { 2026 if (To->getDefinition() || To->isBeingDefined()) { 2027 if (Kind == IDK_Everything) 2028 ImportDeclContext(From, /*ForceImport=*/true); 2029 2030 return false; 2031 } 2032 2033 To->startDefinition(); 2034 2035 // Add base classes. 2036 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) { 2037 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From); 2038 2039 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); 2040 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); 2041 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor; 2042 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers; 2043 ToData.Aggregate = FromData.Aggregate; 2044 ToData.PlainOldData = FromData.PlainOldData; 2045 ToData.Empty = FromData.Empty; 2046 ToData.Polymorphic = FromData.Polymorphic; 2047 ToData.Abstract = FromData.Abstract; 2048 ToData.IsStandardLayout = FromData.IsStandardLayout; 2049 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases; 2050 ToData.HasPrivateFields = FromData.HasPrivateFields; 2051 ToData.HasProtectedFields = FromData.HasProtectedFields; 2052 ToData.HasPublicFields = FromData.HasPublicFields; 2053 ToData.HasMutableFields = FromData.HasMutableFields; 2054 ToData.HasVariantMembers = FromData.HasVariantMembers; 2055 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers; 2056 ToData.HasInClassInitializer = FromData.HasInClassInitializer; 2057 ToData.HasUninitializedReferenceMember 2058 = FromData.HasUninitializedReferenceMember; 2059 ToData.HasUninitializedFields = FromData.HasUninitializedFields; 2060 ToData.NeedOverloadResolutionForMoveConstructor 2061 = FromData.NeedOverloadResolutionForMoveConstructor; 2062 ToData.NeedOverloadResolutionForMoveAssignment 2063 = FromData.NeedOverloadResolutionForMoveAssignment; 2064 ToData.NeedOverloadResolutionForDestructor 2065 = FromData.NeedOverloadResolutionForDestructor; 2066 ToData.DefaultedMoveConstructorIsDeleted 2067 = FromData.DefaultedMoveConstructorIsDeleted; 2068 ToData.DefaultedMoveAssignmentIsDeleted 2069 = FromData.DefaultedMoveAssignmentIsDeleted; 2070 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted; 2071 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers; 2072 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor; 2073 ToData.HasConstexprNonCopyMoveConstructor 2074 = FromData.HasConstexprNonCopyMoveConstructor; 2075 ToData.HasDefaultedDefaultConstructor 2076 = FromData.HasDefaultedDefaultConstructor; 2077 ToData.DefaultedDefaultConstructorIsConstexpr 2078 = FromData.DefaultedDefaultConstructorIsConstexpr; 2079 ToData.HasConstexprDefaultConstructor 2080 = FromData.HasConstexprDefaultConstructor; 2081 ToData.HasNonLiteralTypeFieldsOrBases 2082 = FromData.HasNonLiteralTypeFieldsOrBases; 2083 // ComputedVisibleConversions not imported. 2084 ToData.UserProvidedDefaultConstructor 2085 = FromData.UserProvidedDefaultConstructor; 2086 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers; 2087 ToData.ImplicitCopyConstructorHasConstParam 2088 = FromData.ImplicitCopyConstructorHasConstParam; 2089 ToData.ImplicitCopyAssignmentHasConstParam 2090 = FromData.ImplicitCopyAssignmentHasConstParam; 2091 ToData.HasDeclaredCopyConstructorWithConstParam 2092 = FromData.HasDeclaredCopyConstructorWithConstParam; 2093 ToData.HasDeclaredCopyAssignmentWithConstParam 2094 = FromData.HasDeclaredCopyAssignmentWithConstParam; 2095 ToData.IsLambda = FromData.IsLambda; 2096 2097 SmallVector<CXXBaseSpecifier *, 4> Bases; 2098 for (const auto &Base1 : FromCXX->bases()) { 2099 QualType T = Importer.Import(Base1.getType()); 2100 if (T.isNull()) 2101 return true; 2102 2103 SourceLocation EllipsisLoc; 2104 if (Base1.isPackExpansion()) 2105 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc()); 2106 2107 // Ensure that we have a definition for the base. 2108 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()); 2109 2110 Bases.push_back( 2111 new (Importer.getToContext()) 2112 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()), 2113 Base1.isVirtual(), 2114 Base1.isBaseOfClass(), 2115 Base1.getAccessSpecifierAsWritten(), 2116 Importer.Import(Base1.getTypeSourceInfo()), 2117 EllipsisLoc)); 2118 } 2119 if (!Bases.empty()) 2120 ToCXX->setBases(Bases.data(), Bases.size()); 2121 } 2122 2123 if (shouldForceImportDeclContext(Kind)) 2124 ImportDeclContext(From, /*ForceImport=*/true); 2125 2126 To->completeDefinition(); 2127 return false; 2128 } 2129 2130 bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To, 2131 ImportDefinitionKind Kind) { 2132 if (To->getAnyInitializer()) 2133 return false; 2134 2135 // FIXME: Can we really import any initializer? Alternatively, we could force 2136 // ourselves to import every declaration of a variable and then only use 2137 // getInit() here. 2138 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer()))); 2139 2140 // FIXME: Other bits to merge? 2141 2142 return false; 2143 } 2144 2145 bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To, 2146 ImportDefinitionKind Kind) { 2147 if (To->getDefinition() || To->isBeingDefined()) { 2148 if (Kind == IDK_Everything) 2149 ImportDeclContext(From, /*ForceImport=*/true); 2150 return false; 2151 } 2152 2153 To->startDefinition(); 2154 2155 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From)); 2156 if (T.isNull()) 2157 return true; 2158 2159 QualType ToPromotionType = Importer.Import(From->getPromotionType()); 2160 if (ToPromotionType.isNull()) 2161 return true; 2162 2163 if (shouldForceImportDeclContext(Kind)) 2164 ImportDeclContext(From, /*ForceImport=*/true); 2165 2166 // FIXME: we might need to merge the number of positive or negative bits 2167 // if the enumerator lists don't match. 2168 To->completeDefinition(T, ToPromotionType, 2169 From->getNumPositiveBits(), 2170 From->getNumNegativeBits()); 2171 return false; 2172 } 2173 2174 TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList( 2175 TemplateParameterList *Params) { 2176 SmallVector<NamedDecl *, 4> ToParams; 2177 ToParams.reserve(Params->size()); 2178 for (TemplateParameterList::iterator P = Params->begin(), 2179 PEnd = Params->end(); 2180 P != PEnd; ++P) { 2181 Decl *To = Importer.Import(*P); 2182 if (!To) 2183 return nullptr; 2184 2185 ToParams.push_back(cast<NamedDecl>(To)); 2186 } 2187 2188 return TemplateParameterList::Create(Importer.getToContext(), 2189 Importer.Import(Params->getTemplateLoc()), 2190 Importer.Import(Params->getLAngleLoc()), 2191 ToParams, 2192 Importer.Import(Params->getRAngleLoc())); 2193 } 2194 2195 TemplateArgument 2196 ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { 2197 switch (From.getKind()) { 2198 case TemplateArgument::Null: 2199 return TemplateArgument(); 2200 2201 case TemplateArgument::Type: { 2202 QualType ToType = Importer.Import(From.getAsType()); 2203 if (ToType.isNull()) 2204 return TemplateArgument(); 2205 return TemplateArgument(ToType); 2206 } 2207 2208 case TemplateArgument::Integral: { 2209 QualType ToType = Importer.Import(From.getIntegralType()); 2210 if (ToType.isNull()) 2211 return TemplateArgument(); 2212 return TemplateArgument(From, ToType); 2213 } 2214 2215 case TemplateArgument::Declaration: { 2216 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl())); 2217 QualType ToType = Importer.Import(From.getParamTypeForDecl()); 2218 if (!To || ToType.isNull()) 2219 return TemplateArgument(); 2220 return TemplateArgument(To, ToType); 2221 } 2222 2223 case TemplateArgument::NullPtr: { 2224 QualType ToType = Importer.Import(From.getNullPtrType()); 2225 if (ToType.isNull()) 2226 return TemplateArgument(); 2227 return TemplateArgument(ToType, /*isNullPtr*/true); 2228 } 2229 2230 case TemplateArgument::Template: { 2231 TemplateName ToTemplate = Importer.Import(From.getAsTemplate()); 2232 if (ToTemplate.isNull()) 2233 return TemplateArgument(); 2234 2235 return TemplateArgument(ToTemplate); 2236 } 2237 2238 case TemplateArgument::TemplateExpansion: { 2239 TemplateName ToTemplate 2240 = Importer.Import(From.getAsTemplateOrTemplatePattern()); 2241 if (ToTemplate.isNull()) 2242 return TemplateArgument(); 2243 2244 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions()); 2245 } 2246 2247 case TemplateArgument::Expression: 2248 if (Expr *ToExpr = Importer.Import(From.getAsExpr())) 2249 return TemplateArgument(ToExpr); 2250 return TemplateArgument(); 2251 2252 case TemplateArgument::Pack: { 2253 SmallVector<TemplateArgument, 2> ToPack; 2254 ToPack.reserve(From.pack_size()); 2255 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack)) 2256 return TemplateArgument(); 2257 2258 return TemplateArgument( 2259 llvm::makeArrayRef(ToPack).copy(Importer.getToContext())); 2260 } 2261 } 2262 2263 llvm_unreachable("Invalid template argument kind"); 2264 } 2265 2266 bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs, 2267 unsigned NumFromArgs, 2268 SmallVectorImpl<TemplateArgument> &ToArgs) { 2269 for (unsigned I = 0; I != NumFromArgs; ++I) { 2270 TemplateArgument To = ImportTemplateArgument(FromArgs[I]); 2271 if (To.isNull() && !FromArgs[I].isNull()) 2272 return true; 2273 2274 ToArgs.push_back(To); 2275 } 2276 2277 return false; 2278 } 2279 2280 bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, 2281 RecordDecl *ToRecord, bool Complain) { 2282 // Eliminate a potential failure point where we attempt to re-import 2283 // something we're trying to import while completing ToRecord. 2284 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord); 2285 if (ToOrigin) { 2286 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin); 2287 if (ToOriginRecord) 2288 ToRecord = ToOriginRecord; 2289 } 2290 2291 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 2292 ToRecord->getASTContext(), 2293 Importer.getNonEquivalentDecls(), 2294 false, Complain); 2295 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord); 2296 } 2297 2298 bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, 2299 bool Complain) { 2300 StructuralEquivalenceContext Ctx( 2301 Importer.getFromContext(), Importer.getToContext(), 2302 Importer.getNonEquivalentDecls(), false, Complain); 2303 return Ctx.IsStructurallyEquivalent(FromVar, ToVar); 2304 } 2305 2306 bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { 2307 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 2308 Importer.getToContext(), 2309 Importer.getNonEquivalentDecls()); 2310 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum); 2311 } 2312 2313 bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC, 2314 EnumConstantDecl *ToEC) 2315 { 2316 const llvm::APSInt &FromVal = FromEC->getInitVal(); 2317 const llvm::APSInt &ToVal = ToEC->getInitVal(); 2318 2319 return FromVal.isSigned() == ToVal.isSigned() && 2320 FromVal.getBitWidth() == ToVal.getBitWidth() && 2321 FromVal == ToVal; 2322 } 2323 2324 bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From, 2325 ClassTemplateDecl *To) { 2326 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 2327 Importer.getToContext(), 2328 Importer.getNonEquivalentDecls()); 2329 return Ctx.IsStructurallyEquivalent(From, To); 2330 } 2331 2332 bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From, 2333 VarTemplateDecl *To) { 2334 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 2335 Importer.getToContext(), 2336 Importer.getNonEquivalentDecls()); 2337 return Ctx.IsStructurallyEquivalent(From, To); 2338 } 2339 2340 Decl *ASTNodeImporter::VisitDecl(Decl *D) { 2341 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) 2342 << D->getDeclKindName(); 2343 return nullptr; 2344 } 2345 2346 Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 2347 TranslationUnitDecl *ToD = 2348 Importer.getToContext().getTranslationUnitDecl(); 2349 2350 Importer.Imported(D, ToD); 2351 2352 return ToD; 2353 } 2354 2355 Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { 2356 2357 SourceLocation Loc = Importer.Import(D->getLocation()); 2358 SourceLocation ColonLoc = Importer.Import(D->getColonLoc()); 2359 2360 // Import the context of this declaration. 2361 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 2362 if (!DC) 2363 return nullptr; 2364 2365 AccessSpecDecl *accessSpecDecl 2366 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(), 2367 DC, Loc, ColonLoc); 2368 2369 if (!accessSpecDecl) 2370 return nullptr; 2371 2372 // Lexical DeclContext and Semantic DeclContext 2373 // is always the same for the accessSpec. 2374 accessSpecDecl->setLexicalDeclContext(DC); 2375 DC->addDeclInternal(accessSpecDecl); 2376 2377 return accessSpecDecl; 2378 } 2379 2380 Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { 2381 // Import the major distinguishing characteristics of this namespace. 2382 DeclContext *DC, *LexicalDC; 2383 DeclarationName Name; 2384 SourceLocation Loc; 2385 NamedDecl *ToD; 2386 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2387 return nullptr; 2388 if (ToD) 2389 return ToD; 2390 2391 NamespaceDecl *MergeWithNamespace = nullptr; 2392 if (!Name) { 2393 // This is an anonymous namespace. Adopt an existing anonymous 2394 // namespace if we can. 2395 // FIXME: Not testable. 2396 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) 2397 MergeWithNamespace = TU->getAnonymousNamespace(); 2398 else 2399 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace(); 2400 } else { 2401 SmallVector<NamedDecl *, 4> ConflictingDecls; 2402 SmallVector<NamedDecl *, 2> FoundDecls; 2403 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2404 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2405 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace)) 2406 continue; 2407 2408 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) { 2409 MergeWithNamespace = FoundNS; 2410 ConflictingDecls.clear(); 2411 break; 2412 } 2413 2414 ConflictingDecls.push_back(FoundDecls[I]); 2415 } 2416 2417 if (!ConflictingDecls.empty()) { 2418 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace, 2419 ConflictingDecls.data(), 2420 ConflictingDecls.size()); 2421 } 2422 } 2423 2424 // Create the "to" namespace, if needed. 2425 NamespaceDecl *ToNamespace = MergeWithNamespace; 2426 if (!ToNamespace) { 2427 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, 2428 D->isInline(), 2429 Importer.Import(D->getLocStart()), 2430 Loc, Name.getAsIdentifierInfo(), 2431 /*PrevDecl=*/nullptr); 2432 ToNamespace->setLexicalDeclContext(LexicalDC); 2433 LexicalDC->addDeclInternal(ToNamespace); 2434 2435 // If this is an anonymous namespace, register it as the anonymous 2436 // namespace within its context. 2437 if (!Name) { 2438 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) 2439 TU->setAnonymousNamespace(ToNamespace); 2440 else 2441 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace); 2442 } 2443 } 2444 Importer.Imported(D, ToNamespace); 2445 2446 ImportDeclContext(D); 2447 2448 return ToNamespace; 2449 } 2450 2451 Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { 2452 // Import the major distinguishing characteristics of this typedef. 2453 DeclContext *DC, *LexicalDC; 2454 DeclarationName Name; 2455 SourceLocation Loc; 2456 NamedDecl *ToD; 2457 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2458 return nullptr; 2459 if (ToD) 2460 return ToD; 2461 2462 // If this typedef is not in block scope, determine whether we've 2463 // seen a typedef with the same name (that we can merge with) or any 2464 // other entity by that name (which name lookup could conflict with). 2465 if (!DC->isFunctionOrMethod()) { 2466 SmallVector<NamedDecl *, 4> ConflictingDecls; 2467 unsigned IDNS = Decl::IDNS_Ordinary; 2468 SmallVector<NamedDecl *, 2> FoundDecls; 2469 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2470 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2471 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2472 continue; 2473 if (TypedefNameDecl *FoundTypedef = 2474 dyn_cast<TypedefNameDecl>(FoundDecls[I])) { 2475 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(), 2476 FoundTypedef->getUnderlyingType())) 2477 return Importer.Imported(D, FoundTypedef); 2478 } 2479 2480 ConflictingDecls.push_back(FoundDecls[I]); 2481 } 2482 2483 if (!ConflictingDecls.empty()) { 2484 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2485 ConflictingDecls.data(), 2486 ConflictingDecls.size()); 2487 if (!Name) 2488 return nullptr; 2489 } 2490 } 2491 2492 // Import the underlying type of this typedef; 2493 QualType T = Importer.Import(D->getUnderlyingType()); 2494 if (T.isNull()) 2495 return nullptr; 2496 2497 // Create the new typedef node. 2498 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 2499 SourceLocation StartL = Importer.Import(D->getLocStart()); 2500 TypedefNameDecl *ToTypedef; 2501 if (IsAlias) 2502 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, 2503 StartL, Loc, 2504 Name.getAsIdentifierInfo(), 2505 TInfo); 2506 else 2507 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC, 2508 StartL, Loc, 2509 Name.getAsIdentifierInfo(), 2510 TInfo); 2511 2512 ToTypedef->setAccess(D->getAccess()); 2513 ToTypedef->setLexicalDeclContext(LexicalDC); 2514 Importer.Imported(D, ToTypedef); 2515 LexicalDC->addDeclInternal(ToTypedef); 2516 2517 return ToTypedef; 2518 } 2519 2520 Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { 2521 return VisitTypedefNameDecl(D, /*IsAlias=*/false); 2522 } 2523 2524 Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { 2525 return VisitTypedefNameDecl(D, /*IsAlias=*/true); 2526 } 2527 2528 Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { 2529 // Import the major distinguishing characteristics of this enum. 2530 DeclContext *DC, *LexicalDC; 2531 DeclarationName Name; 2532 SourceLocation Loc; 2533 NamedDecl *ToD; 2534 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2535 return nullptr; 2536 if (ToD) 2537 return ToD; 2538 2539 // Figure out what enum name we're looking for. 2540 unsigned IDNS = Decl::IDNS_Tag; 2541 DeclarationName SearchName = Name; 2542 if (!SearchName && D->getTypedefNameForAnonDecl()) { 2543 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); 2544 IDNS = Decl::IDNS_Ordinary; 2545 } else if (Importer.getToContext().getLangOpts().CPlusPlus) 2546 IDNS |= Decl::IDNS_Ordinary; 2547 2548 // We may already have an enum of the same name; try to find and match it. 2549 if (!DC->isFunctionOrMethod() && SearchName) { 2550 SmallVector<NamedDecl *, 4> ConflictingDecls; 2551 SmallVector<NamedDecl *, 2> FoundDecls; 2552 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2553 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2554 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2555 continue; 2556 2557 Decl *Found = FoundDecls[I]; 2558 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { 2559 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) 2560 Found = Tag->getDecl(); 2561 } 2562 2563 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) { 2564 if (IsStructuralMatch(D, FoundEnum)) 2565 return Importer.Imported(D, FoundEnum); 2566 } 2567 2568 ConflictingDecls.push_back(FoundDecls[I]); 2569 } 2570 2571 if (!ConflictingDecls.empty()) { 2572 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2573 ConflictingDecls.data(), 2574 ConflictingDecls.size()); 2575 } 2576 } 2577 2578 // Create the enum declaration. 2579 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, 2580 Importer.Import(D->getLocStart()), 2581 Loc, Name.getAsIdentifierInfo(), nullptr, 2582 D->isScoped(), D->isScopedUsingClassTag(), 2583 D->isFixed()); 2584 // Import the qualifier, if any. 2585 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 2586 D2->setAccess(D->getAccess()); 2587 D2->setLexicalDeclContext(LexicalDC); 2588 Importer.Imported(D, D2); 2589 LexicalDC->addDeclInternal(D2); 2590 2591 // Import the integer type. 2592 QualType ToIntegerType = Importer.Import(D->getIntegerType()); 2593 if (ToIntegerType.isNull()) 2594 return nullptr; 2595 D2->setIntegerType(ToIntegerType); 2596 2597 // Import the definition 2598 if (D->isCompleteDefinition() && ImportDefinition(D, D2)) 2599 return nullptr; 2600 2601 return D2; 2602 } 2603 2604 Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { 2605 // If this record has a definition in the translation unit we're coming from, 2606 // but this particular declaration is not that definition, import the 2607 // definition and map to that. 2608 TagDecl *Definition = D->getDefinition(); 2609 if (Definition && Definition != D) { 2610 Decl *ImportedDef = Importer.Import(Definition); 2611 if (!ImportedDef) 2612 return nullptr; 2613 2614 return Importer.Imported(D, ImportedDef); 2615 } 2616 2617 // Import the major distinguishing characteristics of this record. 2618 DeclContext *DC, *LexicalDC; 2619 DeclarationName Name; 2620 SourceLocation Loc; 2621 NamedDecl *ToD; 2622 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2623 return nullptr; 2624 if (ToD) 2625 return ToD; 2626 2627 // Figure out what structure name we're looking for. 2628 unsigned IDNS = Decl::IDNS_Tag; 2629 DeclarationName SearchName = Name; 2630 if (!SearchName && D->getTypedefNameForAnonDecl()) { 2631 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); 2632 IDNS = Decl::IDNS_Ordinary; 2633 } else if (Importer.getToContext().getLangOpts().CPlusPlus) 2634 IDNS |= Decl::IDNS_Ordinary; 2635 2636 // We may already have a record of the same name; try to find and match it. 2637 RecordDecl *AdoptDecl = nullptr; 2638 if (!DC->isFunctionOrMethod()) { 2639 SmallVector<NamedDecl *, 4> ConflictingDecls; 2640 SmallVector<NamedDecl *, 2> FoundDecls; 2641 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2642 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2643 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2644 continue; 2645 2646 Decl *Found = FoundDecls[I]; 2647 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { 2648 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) 2649 Found = Tag->getDecl(); 2650 } 2651 2652 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) { 2653 if (D->isAnonymousStructOrUnion() && 2654 FoundRecord->isAnonymousStructOrUnion()) { 2655 // If both anonymous structs/unions are in a record context, make sure 2656 // they occur in the same location in the context records. 2657 if (Optional<unsigned> Index1 2658 = findAnonymousStructOrUnionIndex(D)) { 2659 if (Optional<unsigned> Index2 = 2660 findAnonymousStructOrUnionIndex(FoundRecord)) { 2661 if (*Index1 != *Index2) 2662 continue; 2663 } 2664 } 2665 } 2666 2667 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { 2668 if ((SearchName && !D->isCompleteDefinition()) 2669 || (D->isCompleteDefinition() && 2670 D->isAnonymousStructOrUnion() 2671 == FoundDef->isAnonymousStructOrUnion() && 2672 IsStructuralMatch(D, FoundDef))) { 2673 // The record types structurally match, or the "from" translation 2674 // unit only had a forward declaration anyway; call it the same 2675 // function. 2676 // FIXME: For C++, we should also merge methods here. 2677 return Importer.Imported(D, FoundDef); 2678 } 2679 } else if (!D->isCompleteDefinition()) { 2680 // We have a forward declaration of this type, so adopt that forward 2681 // declaration rather than building a new one. 2682 2683 // If one or both can be completed from external storage then try one 2684 // last time to complete and compare them before doing this. 2685 2686 if (FoundRecord->hasExternalLexicalStorage() && 2687 !FoundRecord->isCompleteDefinition()) 2688 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord); 2689 if (D->hasExternalLexicalStorage()) 2690 D->getASTContext().getExternalSource()->CompleteType(D); 2691 2692 if (FoundRecord->isCompleteDefinition() && 2693 D->isCompleteDefinition() && 2694 !IsStructuralMatch(D, FoundRecord)) 2695 continue; 2696 2697 AdoptDecl = FoundRecord; 2698 continue; 2699 } else if (!SearchName) { 2700 continue; 2701 } 2702 } 2703 2704 ConflictingDecls.push_back(FoundDecls[I]); 2705 } 2706 2707 if (!ConflictingDecls.empty() && SearchName) { 2708 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2709 ConflictingDecls.data(), 2710 ConflictingDecls.size()); 2711 } 2712 } 2713 2714 // Create the record declaration. 2715 RecordDecl *D2 = AdoptDecl; 2716 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 2717 if (!D2) { 2718 CXXRecordDecl *D2CXX = nullptr; 2719 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) { 2720 if (DCXX->isLambda()) { 2721 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo()); 2722 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(), 2723 DC, TInfo, Loc, 2724 DCXX->isDependentLambda(), 2725 DCXX->isGenericLambda(), 2726 DCXX->getLambdaCaptureDefault()); 2727 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl()); 2728 if (DCXX->getLambdaContextDecl() && !CDecl) 2729 return nullptr; 2730 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), 2731 CDecl); 2732 } else { 2733 D2CXX = CXXRecordDecl::Create(Importer.getToContext(), 2734 D->getTagKind(), 2735 DC, StartLoc, Loc, 2736 Name.getAsIdentifierInfo()); 2737 } 2738 D2 = D2CXX; 2739 D2->setAccess(D->getAccess()); 2740 } else { 2741 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(), 2742 DC, StartLoc, Loc, Name.getAsIdentifierInfo()); 2743 } 2744 2745 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 2746 D2->setLexicalDeclContext(LexicalDC); 2747 LexicalDC->addDeclInternal(D2); 2748 if (D->isAnonymousStructOrUnion()) 2749 D2->setAnonymousStructOrUnion(true); 2750 } 2751 2752 Importer.Imported(D, D2); 2753 2754 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default)) 2755 return nullptr; 2756 2757 return D2; 2758 } 2759 2760 Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { 2761 // Import the major distinguishing characteristics of this enumerator. 2762 DeclContext *DC, *LexicalDC; 2763 DeclarationName Name; 2764 SourceLocation Loc; 2765 NamedDecl *ToD; 2766 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2767 return nullptr; 2768 if (ToD) 2769 return ToD; 2770 2771 QualType T = Importer.Import(D->getType()); 2772 if (T.isNull()) 2773 return nullptr; 2774 2775 // Determine whether there are any other declarations with the same name and 2776 // in the same context. 2777 if (!LexicalDC->isFunctionOrMethod()) { 2778 SmallVector<NamedDecl *, 4> ConflictingDecls; 2779 unsigned IDNS = Decl::IDNS_Ordinary; 2780 SmallVector<NamedDecl *, 2> FoundDecls; 2781 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2782 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2783 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2784 continue; 2785 2786 if (EnumConstantDecl *FoundEnumConstant 2787 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) { 2788 if (IsStructuralMatch(D, FoundEnumConstant)) 2789 return Importer.Imported(D, FoundEnumConstant); 2790 } 2791 2792 ConflictingDecls.push_back(FoundDecls[I]); 2793 } 2794 2795 if (!ConflictingDecls.empty()) { 2796 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2797 ConflictingDecls.data(), 2798 ConflictingDecls.size()); 2799 if (!Name) 2800 return nullptr; 2801 } 2802 } 2803 2804 Expr *Init = Importer.Import(D->getInitExpr()); 2805 if (D->getInitExpr() && !Init) 2806 return nullptr; 2807 2808 EnumConstantDecl *ToEnumerator 2809 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc, 2810 Name.getAsIdentifierInfo(), T, 2811 Init, D->getInitVal()); 2812 ToEnumerator->setAccess(D->getAccess()); 2813 ToEnumerator->setLexicalDeclContext(LexicalDC); 2814 Importer.Imported(D, ToEnumerator); 2815 LexicalDC->addDeclInternal(ToEnumerator); 2816 return ToEnumerator; 2817 } 2818 2819 Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { 2820 // Import the major distinguishing characteristics of this function. 2821 DeclContext *DC, *LexicalDC; 2822 DeclarationName Name; 2823 SourceLocation Loc; 2824 NamedDecl *ToD; 2825 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2826 return nullptr; 2827 if (ToD) 2828 return ToD; 2829 2830 // Try to find a function in our own ("to") context with the same name, same 2831 // type, and in the same context as the function we're importing. 2832 if (!LexicalDC->isFunctionOrMethod()) { 2833 SmallVector<NamedDecl *, 4> ConflictingDecls; 2834 unsigned IDNS = Decl::IDNS_Ordinary; 2835 SmallVector<NamedDecl *, 2> FoundDecls; 2836 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2837 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2838 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2839 continue; 2840 2841 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) { 2842 if (FoundFunction->hasExternalFormalLinkage() && 2843 D->hasExternalFormalLinkage()) { 2844 if (Importer.IsStructurallyEquivalent(D->getType(), 2845 FoundFunction->getType())) { 2846 // FIXME: Actually try to merge the body and other attributes. 2847 return Importer.Imported(D, FoundFunction); 2848 } 2849 2850 // FIXME: Check for overloading more carefully, e.g., by boosting 2851 // Sema::IsOverload out to the AST library. 2852 2853 // Function overloading is okay in C++. 2854 if (Importer.getToContext().getLangOpts().CPlusPlus) 2855 continue; 2856 2857 // Complain about inconsistent function types. 2858 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) 2859 << Name << D->getType() << FoundFunction->getType(); 2860 Importer.ToDiag(FoundFunction->getLocation(), 2861 diag::note_odr_value_here) 2862 << FoundFunction->getType(); 2863 } 2864 } 2865 2866 ConflictingDecls.push_back(FoundDecls[I]); 2867 } 2868 2869 if (!ConflictingDecls.empty()) { 2870 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2871 ConflictingDecls.data(), 2872 ConflictingDecls.size()); 2873 if (!Name) 2874 return nullptr; 2875 } 2876 } 2877 2878 DeclarationNameInfo NameInfo(Name, Loc); 2879 // Import additional name location/type info. 2880 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); 2881 2882 QualType FromTy = D->getType(); 2883 bool usedDifferentExceptionSpec = false; 2884 2885 if (const FunctionProtoType * 2886 FromFPT = D->getType()->getAs<FunctionProtoType>()) { 2887 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); 2888 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the 2889 // FunctionDecl that we are importing the FunctionProtoType for. 2890 // To avoid an infinite recursion when importing, create the FunctionDecl 2891 // with a simplified function type and update it afterwards. 2892 if (FromEPI.ExceptionSpec.SourceDecl || 2893 FromEPI.ExceptionSpec.SourceTemplate || 2894 FromEPI.ExceptionSpec.NoexceptExpr) { 2895 FunctionProtoType::ExtProtoInfo DefaultEPI; 2896 FromTy = Importer.getFromContext().getFunctionType( 2897 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI); 2898 usedDifferentExceptionSpec = true; 2899 } 2900 } 2901 2902 // Import the type. 2903 QualType T = Importer.Import(FromTy); 2904 if (T.isNull()) 2905 return nullptr; 2906 2907 // Import the function parameters. 2908 SmallVector<ParmVarDecl *, 8> Parameters; 2909 for (auto P : D->params()) { 2910 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P)); 2911 if (!ToP) 2912 return nullptr; 2913 2914 Parameters.push_back(ToP); 2915 } 2916 2917 // Create the imported function. 2918 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 2919 FunctionDecl *ToFunction = nullptr; 2920 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart()); 2921 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { 2922 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(), 2923 cast<CXXRecordDecl>(DC), 2924 InnerLocStart, 2925 NameInfo, T, TInfo, 2926 FromConstructor->isExplicit(), 2927 D->isInlineSpecified(), 2928 D->isImplicit(), 2929 D->isConstexpr()); 2930 } else if (isa<CXXDestructorDecl>(D)) { 2931 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(), 2932 cast<CXXRecordDecl>(DC), 2933 InnerLocStart, 2934 NameInfo, T, TInfo, 2935 D->isInlineSpecified(), 2936 D->isImplicit()); 2937 } else if (CXXConversionDecl *FromConversion 2938 = dyn_cast<CXXConversionDecl>(D)) { 2939 ToFunction = CXXConversionDecl::Create(Importer.getToContext(), 2940 cast<CXXRecordDecl>(DC), 2941 InnerLocStart, 2942 NameInfo, T, TInfo, 2943 D->isInlineSpecified(), 2944 FromConversion->isExplicit(), 2945 D->isConstexpr(), 2946 Importer.Import(D->getLocEnd())); 2947 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 2948 ToFunction = CXXMethodDecl::Create(Importer.getToContext(), 2949 cast<CXXRecordDecl>(DC), 2950 InnerLocStart, 2951 NameInfo, T, TInfo, 2952 Method->getStorageClass(), 2953 Method->isInlineSpecified(), 2954 D->isConstexpr(), 2955 Importer.Import(D->getLocEnd())); 2956 } else { 2957 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, 2958 InnerLocStart, 2959 NameInfo, T, TInfo, D->getStorageClass(), 2960 D->isInlineSpecified(), 2961 D->hasWrittenPrototype(), 2962 D->isConstexpr()); 2963 } 2964 2965 // Import the qualifier, if any. 2966 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 2967 ToFunction->setAccess(D->getAccess()); 2968 ToFunction->setLexicalDeclContext(LexicalDC); 2969 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); 2970 ToFunction->setTrivial(D->isTrivial()); 2971 ToFunction->setPure(D->isPure()); 2972 Importer.Imported(D, ToFunction); 2973 2974 // Set the parameters. 2975 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) { 2976 Parameters[I]->setOwningFunction(ToFunction); 2977 ToFunction->addDeclInternal(Parameters[I]); 2978 } 2979 ToFunction->setParams(Parameters); 2980 2981 if (usedDifferentExceptionSpec) { 2982 // Update FunctionProtoType::ExtProtoInfo. 2983 QualType T = Importer.Import(D->getType()); 2984 if (T.isNull()) 2985 return nullptr; 2986 ToFunction->setType(T); 2987 } 2988 2989 // Import the body, if any. 2990 if (Stmt *FromBody = D->getBody()) { 2991 if (Stmt *ToBody = Importer.Import(FromBody)) { 2992 ToFunction->setBody(ToBody); 2993 } 2994 } 2995 2996 // FIXME: Other bits to merge? 2997 2998 // Add this function to the lexical context. 2999 LexicalDC->addDeclInternal(ToFunction); 3000 3001 return ToFunction; 3002 } 3003 3004 Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { 3005 return VisitFunctionDecl(D); 3006 } 3007 3008 Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { 3009 return VisitCXXMethodDecl(D); 3010 } 3011 3012 Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { 3013 return VisitCXXMethodDecl(D); 3014 } 3015 3016 Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { 3017 return VisitCXXMethodDecl(D); 3018 } 3019 3020 static unsigned getFieldIndex(Decl *F) { 3021 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext()); 3022 if (!Owner) 3023 return 0; 3024 3025 unsigned Index = 1; 3026 for (const auto *D : Owner->noload_decls()) { 3027 if (D == F) 3028 return Index; 3029 3030 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) 3031 ++Index; 3032 } 3033 3034 return Index; 3035 } 3036 3037 Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { 3038 // Import the major distinguishing characteristics of a variable. 3039 DeclContext *DC, *LexicalDC; 3040 DeclarationName Name; 3041 SourceLocation Loc; 3042 NamedDecl *ToD; 3043 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3044 return nullptr; 3045 if (ToD) 3046 return ToD; 3047 3048 // Determine whether we've already imported this field. 3049 SmallVector<NamedDecl *, 2> FoundDecls; 3050 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3051 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3052 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) { 3053 // For anonymous fields, match up by index. 3054 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) 3055 continue; 3056 3057 if (Importer.IsStructurallyEquivalent(D->getType(), 3058 FoundField->getType())) { 3059 Importer.Imported(D, FoundField); 3060 return FoundField; 3061 } 3062 3063 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) 3064 << Name << D->getType() << FoundField->getType(); 3065 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) 3066 << FoundField->getType(); 3067 return nullptr; 3068 } 3069 } 3070 3071 // Import the type. 3072 QualType T = Importer.Import(D->getType()); 3073 if (T.isNull()) 3074 return nullptr; 3075 3076 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3077 Expr *BitWidth = Importer.Import(D->getBitWidth()); 3078 if (!BitWidth && D->getBitWidth()) 3079 return nullptr; 3080 3081 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC, 3082 Importer.Import(D->getInnerLocStart()), 3083 Loc, Name.getAsIdentifierInfo(), 3084 T, TInfo, BitWidth, D->isMutable(), 3085 D->getInClassInitStyle()); 3086 ToField->setAccess(D->getAccess()); 3087 ToField->setLexicalDeclContext(LexicalDC); 3088 if (Expr *FromInitializer = D->getInClassInitializer()) { 3089 Expr *ToInitializer = Importer.Import(FromInitializer); 3090 if (ToInitializer) 3091 ToField->setInClassInitializer(ToInitializer); 3092 else 3093 return nullptr; 3094 } 3095 ToField->setImplicit(D->isImplicit()); 3096 Importer.Imported(D, ToField); 3097 LexicalDC->addDeclInternal(ToField); 3098 return ToField; 3099 } 3100 3101 Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { 3102 // Import the major distinguishing characteristics of a variable. 3103 DeclContext *DC, *LexicalDC; 3104 DeclarationName Name; 3105 SourceLocation Loc; 3106 NamedDecl *ToD; 3107 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3108 return nullptr; 3109 if (ToD) 3110 return ToD; 3111 3112 // Determine whether we've already imported this field. 3113 SmallVector<NamedDecl *, 2> FoundDecls; 3114 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3115 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3116 if (IndirectFieldDecl *FoundField 3117 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) { 3118 // For anonymous indirect fields, match up by index. 3119 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) 3120 continue; 3121 3122 if (Importer.IsStructurallyEquivalent(D->getType(), 3123 FoundField->getType(), 3124 !Name.isEmpty())) { 3125 Importer.Imported(D, FoundField); 3126 return FoundField; 3127 } 3128 3129 // If there are more anonymous fields to check, continue. 3130 if (!Name && I < N-1) 3131 continue; 3132 3133 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) 3134 << Name << D->getType() << FoundField->getType(); 3135 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) 3136 << FoundField->getType(); 3137 return nullptr; 3138 } 3139 } 3140 3141 // Import the type. 3142 QualType T = Importer.Import(D->getType()); 3143 if (T.isNull()) 3144 return nullptr; 3145 3146 NamedDecl **NamedChain = 3147 new (Importer.getToContext())NamedDecl*[D->getChainingSize()]; 3148 3149 unsigned i = 0; 3150 for (auto *PI : D->chain()) { 3151 Decl *D = Importer.Import(PI); 3152 if (!D) 3153 return nullptr; 3154 NamedChain[i++] = cast<NamedDecl>(D); 3155 } 3156 3157 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create( 3158 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T, 3159 NamedChain, D->getChainingSize()); 3160 3161 for (const auto *Attr : D->attrs()) 3162 ToIndirectField->addAttr(Attr->clone(Importer.getToContext())); 3163 3164 ToIndirectField->setAccess(D->getAccess()); 3165 ToIndirectField->setLexicalDeclContext(LexicalDC); 3166 Importer.Imported(D, ToIndirectField); 3167 LexicalDC->addDeclInternal(ToIndirectField); 3168 return ToIndirectField; 3169 } 3170 3171 Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { 3172 // Import the major distinguishing characteristics of an ivar. 3173 DeclContext *DC, *LexicalDC; 3174 DeclarationName Name; 3175 SourceLocation Loc; 3176 NamedDecl *ToD; 3177 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3178 return nullptr; 3179 if (ToD) 3180 return ToD; 3181 3182 // Determine whether we've already imported this ivar 3183 SmallVector<NamedDecl *, 2> FoundDecls; 3184 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3185 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3186 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) { 3187 if (Importer.IsStructurallyEquivalent(D->getType(), 3188 FoundIvar->getType())) { 3189 Importer.Imported(D, FoundIvar); 3190 return FoundIvar; 3191 } 3192 3193 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent) 3194 << Name << D->getType() << FoundIvar->getType(); 3195 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) 3196 << FoundIvar->getType(); 3197 return nullptr; 3198 } 3199 } 3200 3201 // Import the type. 3202 QualType T = Importer.Import(D->getType()); 3203 if (T.isNull()) 3204 return nullptr; 3205 3206 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3207 Expr *BitWidth = Importer.Import(D->getBitWidth()); 3208 if (!BitWidth && D->getBitWidth()) 3209 return nullptr; 3210 3211 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), 3212 cast<ObjCContainerDecl>(DC), 3213 Importer.Import(D->getInnerLocStart()), 3214 Loc, Name.getAsIdentifierInfo(), 3215 T, TInfo, D->getAccessControl(), 3216 BitWidth, D->getSynthesize()); 3217 ToIvar->setLexicalDeclContext(LexicalDC); 3218 Importer.Imported(D, ToIvar); 3219 LexicalDC->addDeclInternal(ToIvar); 3220 return ToIvar; 3221 3222 } 3223 3224 Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { 3225 // Import the major distinguishing characteristics of a variable. 3226 DeclContext *DC, *LexicalDC; 3227 DeclarationName Name; 3228 SourceLocation Loc; 3229 NamedDecl *ToD; 3230 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3231 return nullptr; 3232 if (ToD) 3233 return ToD; 3234 3235 // Try to find a variable in our own ("to") context with the same name and 3236 // in the same context as the variable we're importing. 3237 if (D->isFileVarDecl()) { 3238 VarDecl *MergeWithVar = nullptr; 3239 SmallVector<NamedDecl *, 4> ConflictingDecls; 3240 unsigned IDNS = Decl::IDNS_Ordinary; 3241 SmallVector<NamedDecl *, 2> FoundDecls; 3242 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3243 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3244 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 3245 continue; 3246 3247 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) { 3248 // We have found a variable that we may need to merge with. Check it. 3249 if (FoundVar->hasExternalFormalLinkage() && 3250 D->hasExternalFormalLinkage()) { 3251 if (Importer.IsStructurallyEquivalent(D->getType(), 3252 FoundVar->getType())) { 3253 MergeWithVar = FoundVar; 3254 break; 3255 } 3256 3257 const ArrayType *FoundArray 3258 = Importer.getToContext().getAsArrayType(FoundVar->getType()); 3259 const ArrayType *TArray 3260 = Importer.getToContext().getAsArrayType(D->getType()); 3261 if (FoundArray && TArray) { 3262 if (isa<IncompleteArrayType>(FoundArray) && 3263 isa<ConstantArrayType>(TArray)) { 3264 // Import the type. 3265 QualType T = Importer.Import(D->getType()); 3266 if (T.isNull()) 3267 return nullptr; 3268 3269 FoundVar->setType(T); 3270 MergeWithVar = FoundVar; 3271 break; 3272 } else if (isa<IncompleteArrayType>(TArray) && 3273 isa<ConstantArrayType>(FoundArray)) { 3274 MergeWithVar = FoundVar; 3275 break; 3276 } 3277 } 3278 3279 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) 3280 << Name << D->getType() << FoundVar->getType(); 3281 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) 3282 << FoundVar->getType(); 3283 } 3284 } 3285 3286 ConflictingDecls.push_back(FoundDecls[I]); 3287 } 3288 3289 if (MergeWithVar) { 3290 // An equivalent variable with external linkage has been found. Link 3291 // the two declarations, then merge them. 3292 Importer.Imported(D, MergeWithVar); 3293 3294 if (VarDecl *DDef = D->getDefinition()) { 3295 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { 3296 Importer.ToDiag(ExistingDef->getLocation(), 3297 diag::err_odr_variable_multiple_def) 3298 << Name; 3299 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); 3300 } else { 3301 Expr *Init = Importer.Import(DDef->getInit()); 3302 MergeWithVar->setInit(Init); 3303 if (DDef->isInitKnownICE()) { 3304 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt(); 3305 Eval->CheckedICE = true; 3306 Eval->IsICE = DDef->isInitICE(); 3307 } 3308 } 3309 } 3310 3311 return MergeWithVar; 3312 } 3313 3314 if (!ConflictingDecls.empty()) { 3315 Name = Importer.HandleNameConflict(Name, DC, IDNS, 3316 ConflictingDecls.data(), 3317 ConflictingDecls.size()); 3318 if (!Name) 3319 return nullptr; 3320 } 3321 } 3322 3323 // Import the type. 3324 QualType T = Importer.Import(D->getType()); 3325 if (T.isNull()) 3326 return nullptr; 3327 3328 // Create the imported variable. 3329 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3330 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, 3331 Importer.Import(D->getInnerLocStart()), 3332 Loc, Name.getAsIdentifierInfo(), 3333 T, TInfo, 3334 D->getStorageClass()); 3335 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 3336 ToVar->setAccess(D->getAccess()); 3337 ToVar->setLexicalDeclContext(LexicalDC); 3338 Importer.Imported(D, ToVar); 3339 LexicalDC->addDeclInternal(ToVar); 3340 3341 if (!D->isFileVarDecl() && 3342 D->isUsed()) 3343 ToVar->setIsUsed(); 3344 3345 // Merge the initializer. 3346 if (ImportDefinition(D, ToVar)) 3347 return nullptr; 3348 3349 return ToVar; 3350 } 3351 3352 Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { 3353 // Parameters are created in the translation unit's context, then moved 3354 // into the function declaration's context afterward. 3355 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); 3356 3357 // Import the name of this declaration. 3358 DeclarationName Name = Importer.Import(D->getDeclName()); 3359 if (D->getDeclName() && !Name) 3360 return nullptr; 3361 3362 // Import the location of this declaration. 3363 SourceLocation Loc = Importer.Import(D->getLocation()); 3364 3365 // Import the parameter's type. 3366 QualType T = Importer.Import(D->getType()); 3367 if (T.isNull()) 3368 return nullptr; 3369 3370 // Create the imported parameter. 3371 ImplicitParamDecl *ToParm 3372 = ImplicitParamDecl::Create(Importer.getToContext(), DC, 3373 Loc, Name.getAsIdentifierInfo(), 3374 T); 3375 return Importer.Imported(D, ToParm); 3376 } 3377 3378 Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { 3379 // Parameters are created in the translation unit's context, then moved 3380 // into the function declaration's context afterward. 3381 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); 3382 3383 // Import the name of this declaration. 3384 DeclarationName Name = Importer.Import(D->getDeclName()); 3385 if (D->getDeclName() && !Name) 3386 return nullptr; 3387 3388 // Import the location of this declaration. 3389 SourceLocation Loc = Importer.Import(D->getLocation()); 3390 3391 // Import the parameter's type. 3392 QualType T = Importer.Import(D->getType()); 3393 if (T.isNull()) 3394 return nullptr; 3395 3396 // Create the imported parameter. 3397 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3398 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC, 3399 Importer.Import(D->getInnerLocStart()), 3400 Loc, Name.getAsIdentifierInfo(), 3401 T, TInfo, D->getStorageClass(), 3402 /*FIXME: Default argument*/nullptr); 3403 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg()); 3404 3405 if (D->isUsed()) 3406 ToParm->setIsUsed(); 3407 3408 return Importer.Imported(D, ToParm); 3409 } 3410 3411 Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { 3412 // Import the major distinguishing characteristics of a method. 3413 DeclContext *DC, *LexicalDC; 3414 DeclarationName Name; 3415 SourceLocation Loc; 3416 NamedDecl *ToD; 3417 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3418 return nullptr; 3419 if (ToD) 3420 return ToD; 3421 3422 SmallVector<NamedDecl *, 2> FoundDecls; 3423 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3424 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3425 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) { 3426 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) 3427 continue; 3428 3429 // Check return types. 3430 if (!Importer.IsStructurallyEquivalent(D->getReturnType(), 3431 FoundMethod->getReturnType())) { 3432 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent) 3433 << D->isInstanceMethod() << Name << D->getReturnType() 3434 << FoundMethod->getReturnType(); 3435 Importer.ToDiag(FoundMethod->getLocation(), 3436 diag::note_odr_objc_method_here) 3437 << D->isInstanceMethod() << Name; 3438 return nullptr; 3439 } 3440 3441 // Check the number of parameters. 3442 if (D->param_size() != FoundMethod->param_size()) { 3443 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent) 3444 << D->isInstanceMethod() << Name 3445 << D->param_size() << FoundMethod->param_size(); 3446 Importer.ToDiag(FoundMethod->getLocation(), 3447 diag::note_odr_objc_method_here) 3448 << D->isInstanceMethod() << Name; 3449 return nullptr; 3450 } 3451 3452 // Check parameter types. 3453 for (ObjCMethodDecl::param_iterator P = D->param_begin(), 3454 PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); 3455 P != PEnd; ++P, ++FoundP) { 3456 if (!Importer.IsStructurallyEquivalent((*P)->getType(), 3457 (*FoundP)->getType())) { 3458 Importer.FromDiag((*P)->getLocation(), 3459 diag::err_odr_objc_method_param_type_inconsistent) 3460 << D->isInstanceMethod() << Name 3461 << (*P)->getType() << (*FoundP)->getType(); 3462 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) 3463 << (*FoundP)->getType(); 3464 return nullptr; 3465 } 3466 } 3467 3468 // Check variadic/non-variadic. 3469 // Check the number of parameters. 3470 if (D->isVariadic() != FoundMethod->isVariadic()) { 3471 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent) 3472 << D->isInstanceMethod() << Name; 3473 Importer.ToDiag(FoundMethod->getLocation(), 3474 diag::note_odr_objc_method_here) 3475 << D->isInstanceMethod() << Name; 3476 return nullptr; 3477 } 3478 3479 // FIXME: Any other bits we need to merge? 3480 return Importer.Imported(D, FoundMethod); 3481 } 3482 } 3483 3484 // Import the result type. 3485 QualType ResultTy = Importer.Import(D->getReturnType()); 3486 if (ResultTy.isNull()) 3487 return nullptr; 3488 3489 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo()); 3490 3491 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create( 3492 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()), 3493 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(), 3494 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(), 3495 D->getImplementationControl(), D->hasRelatedResultType()); 3496 3497 // FIXME: When we decide to merge method definitions, we'll need to 3498 // deal with implicit parameters. 3499 3500 // Import the parameters 3501 SmallVector<ParmVarDecl *, 5> ToParams; 3502 for (auto *FromP : D->params()) { 3503 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP)); 3504 if (!ToP) 3505 return nullptr; 3506 3507 ToParams.push_back(ToP); 3508 } 3509 3510 // Set the parameters. 3511 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) { 3512 ToParams[I]->setOwningFunction(ToMethod); 3513 ToMethod->addDeclInternal(ToParams[I]); 3514 } 3515 SmallVector<SourceLocation, 12> SelLocs; 3516 D->getSelectorLocs(SelLocs); 3517 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs); 3518 3519 ToMethod->setLexicalDeclContext(LexicalDC); 3520 Importer.Imported(D, ToMethod); 3521 LexicalDC->addDeclInternal(ToMethod); 3522 return ToMethod; 3523 } 3524 3525 Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { 3526 // Import the major distinguishing characteristics of a category. 3527 DeclContext *DC, *LexicalDC; 3528 DeclarationName Name; 3529 SourceLocation Loc; 3530 NamedDecl *ToD; 3531 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3532 return nullptr; 3533 if (ToD) 3534 return ToD; 3535 3536 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo()); 3537 if (!BoundInfo) 3538 return nullptr; 3539 3540 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create( 3541 Importer.getToContext(), DC, 3542 D->getVariance(), 3543 Importer.Import(D->getVarianceLoc()), 3544 D->getIndex(), 3545 Importer.Import(D->getLocation()), 3546 Name.getAsIdentifierInfo(), 3547 Importer.Import(D->getColonLoc()), 3548 BoundInfo); 3549 Importer.Imported(D, Result); 3550 Result->setLexicalDeclContext(LexicalDC); 3551 return Result; 3552 } 3553 3554 Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { 3555 // Import the major distinguishing characteristics of a category. 3556 DeclContext *DC, *LexicalDC; 3557 DeclarationName Name; 3558 SourceLocation Loc; 3559 NamedDecl *ToD; 3560 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3561 return nullptr; 3562 if (ToD) 3563 return ToD; 3564 3565 ObjCInterfaceDecl *ToInterface 3566 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface())); 3567 if (!ToInterface) 3568 return nullptr; 3569 3570 // Determine if we've already encountered this category. 3571 ObjCCategoryDecl *MergeWithCategory 3572 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); 3573 ObjCCategoryDecl *ToCategory = MergeWithCategory; 3574 if (!ToCategory) { 3575 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC, 3576 Importer.Import(D->getAtStartLoc()), 3577 Loc, 3578 Importer.Import(D->getCategoryNameLoc()), 3579 Name.getAsIdentifierInfo(), 3580 ToInterface, 3581 /*TypeParamList=*/nullptr, 3582 Importer.Import(D->getIvarLBraceLoc()), 3583 Importer.Import(D->getIvarRBraceLoc())); 3584 ToCategory->setLexicalDeclContext(LexicalDC); 3585 LexicalDC->addDeclInternal(ToCategory); 3586 Importer.Imported(D, ToCategory); 3587 // Import the type parameter list after calling Imported, to avoid 3588 // loops when bringing in their DeclContext. 3589 ToCategory->setTypeParamList(ImportObjCTypeParamList( 3590 D->getTypeParamList())); 3591 3592 // Import protocols 3593 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3594 SmallVector<SourceLocation, 4> ProtocolLocs; 3595 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc 3596 = D->protocol_loc_begin(); 3597 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), 3598 FromProtoEnd = D->protocol_end(); 3599 FromProto != FromProtoEnd; 3600 ++FromProto, ++FromProtoLoc) { 3601 ObjCProtocolDecl *ToProto 3602 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3603 if (!ToProto) 3604 return nullptr; 3605 Protocols.push_back(ToProto); 3606 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3607 } 3608 3609 // FIXME: If we're merging, make sure that the protocol list is the same. 3610 ToCategory->setProtocolList(Protocols.data(), Protocols.size(), 3611 ProtocolLocs.data(), Importer.getToContext()); 3612 3613 } else { 3614 Importer.Imported(D, ToCategory); 3615 } 3616 3617 // Import all of the members of this category. 3618 ImportDeclContext(D); 3619 3620 // If we have an implementation, import it as well. 3621 if (D->getImplementation()) { 3622 ObjCCategoryImplDecl *Impl 3623 = cast_or_null<ObjCCategoryImplDecl>( 3624 Importer.Import(D->getImplementation())); 3625 if (!Impl) 3626 return nullptr; 3627 3628 ToCategory->setImplementation(Impl); 3629 } 3630 3631 return ToCategory; 3632 } 3633 3634 bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From, 3635 ObjCProtocolDecl *To, 3636 ImportDefinitionKind Kind) { 3637 if (To->getDefinition()) { 3638 if (shouldForceImportDeclContext(Kind)) 3639 ImportDeclContext(From); 3640 return false; 3641 } 3642 3643 // Start the protocol definition 3644 To->startDefinition(); 3645 3646 // Import protocols 3647 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3648 SmallVector<SourceLocation, 4> ProtocolLocs; 3649 ObjCProtocolDecl::protocol_loc_iterator 3650 FromProtoLoc = From->protocol_loc_begin(); 3651 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), 3652 FromProtoEnd = From->protocol_end(); 3653 FromProto != FromProtoEnd; 3654 ++FromProto, ++FromProtoLoc) { 3655 ObjCProtocolDecl *ToProto 3656 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3657 if (!ToProto) 3658 return true; 3659 Protocols.push_back(ToProto); 3660 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3661 } 3662 3663 // FIXME: If we're merging, make sure that the protocol list is the same. 3664 To->setProtocolList(Protocols.data(), Protocols.size(), 3665 ProtocolLocs.data(), Importer.getToContext()); 3666 3667 if (shouldForceImportDeclContext(Kind)) { 3668 // Import all of the members of this protocol. 3669 ImportDeclContext(From, /*ForceImport=*/true); 3670 } 3671 return false; 3672 } 3673 3674 Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { 3675 // If this protocol has a definition in the translation unit we're coming 3676 // from, but this particular declaration is not that definition, import the 3677 // definition and map to that. 3678 ObjCProtocolDecl *Definition = D->getDefinition(); 3679 if (Definition && Definition != D) { 3680 Decl *ImportedDef = Importer.Import(Definition); 3681 if (!ImportedDef) 3682 return nullptr; 3683 3684 return Importer.Imported(D, ImportedDef); 3685 } 3686 3687 // Import the major distinguishing characteristics of a protocol. 3688 DeclContext *DC, *LexicalDC; 3689 DeclarationName Name; 3690 SourceLocation Loc; 3691 NamedDecl *ToD; 3692 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3693 return nullptr; 3694 if (ToD) 3695 return ToD; 3696 3697 ObjCProtocolDecl *MergeWithProtocol = nullptr; 3698 SmallVector<NamedDecl *, 2> FoundDecls; 3699 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3700 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3701 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) 3702 continue; 3703 3704 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I]))) 3705 break; 3706 } 3707 3708 ObjCProtocolDecl *ToProto = MergeWithProtocol; 3709 if (!ToProto) { 3710 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, 3711 Name.getAsIdentifierInfo(), Loc, 3712 Importer.Import(D->getAtStartLoc()), 3713 /*PrevDecl=*/nullptr); 3714 ToProto->setLexicalDeclContext(LexicalDC); 3715 LexicalDC->addDeclInternal(ToProto); 3716 } 3717 3718 Importer.Imported(D, ToProto); 3719 3720 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto)) 3721 return nullptr; 3722 3723 return ToProto; 3724 } 3725 3726 Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 3727 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 3728 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 3729 3730 SourceLocation ExternLoc = Importer.Import(D->getExternLoc()); 3731 SourceLocation LangLoc = Importer.Import(D->getLocation()); 3732 3733 bool HasBraces = D->hasBraces(); 3734 3735 LinkageSpecDecl *ToLinkageSpec = 3736 LinkageSpecDecl::Create(Importer.getToContext(), 3737 DC, 3738 ExternLoc, 3739 LangLoc, 3740 D->getLanguage(), 3741 HasBraces); 3742 3743 if (HasBraces) { 3744 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc()); 3745 ToLinkageSpec->setRBraceLoc(RBraceLoc); 3746 } 3747 3748 ToLinkageSpec->setLexicalDeclContext(LexicalDC); 3749 LexicalDC->addDeclInternal(ToLinkageSpec); 3750 3751 Importer.Imported(D, ToLinkageSpec); 3752 3753 return ToLinkageSpec; 3754 } 3755 3756 bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From, 3757 ObjCInterfaceDecl *To, 3758 ImportDefinitionKind Kind) { 3759 if (To->getDefinition()) { 3760 // Check consistency of superclass. 3761 ObjCInterfaceDecl *FromSuper = From->getSuperClass(); 3762 if (FromSuper) { 3763 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper)); 3764 if (!FromSuper) 3765 return true; 3766 } 3767 3768 ObjCInterfaceDecl *ToSuper = To->getSuperClass(); 3769 if ((bool)FromSuper != (bool)ToSuper || 3770 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { 3771 Importer.ToDiag(To->getLocation(), 3772 diag::err_odr_objc_superclass_inconsistent) 3773 << To->getDeclName(); 3774 if (ToSuper) 3775 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) 3776 << To->getSuperClass()->getDeclName(); 3777 else 3778 Importer.ToDiag(To->getLocation(), 3779 diag::note_odr_objc_missing_superclass); 3780 if (From->getSuperClass()) 3781 Importer.FromDiag(From->getSuperClassLoc(), 3782 diag::note_odr_objc_superclass) 3783 << From->getSuperClass()->getDeclName(); 3784 else 3785 Importer.FromDiag(From->getLocation(), 3786 diag::note_odr_objc_missing_superclass); 3787 } 3788 3789 if (shouldForceImportDeclContext(Kind)) 3790 ImportDeclContext(From); 3791 return false; 3792 } 3793 3794 // Start the definition. 3795 To->startDefinition(); 3796 3797 // If this class has a superclass, import it. 3798 if (From->getSuperClass()) { 3799 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo()); 3800 if (!SuperTInfo) 3801 return true; 3802 3803 To->setSuperClass(SuperTInfo); 3804 } 3805 3806 // Import protocols 3807 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3808 SmallVector<SourceLocation, 4> ProtocolLocs; 3809 ObjCInterfaceDecl::protocol_loc_iterator 3810 FromProtoLoc = From->protocol_loc_begin(); 3811 3812 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), 3813 FromProtoEnd = From->protocol_end(); 3814 FromProto != FromProtoEnd; 3815 ++FromProto, ++FromProtoLoc) { 3816 ObjCProtocolDecl *ToProto 3817 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3818 if (!ToProto) 3819 return true; 3820 Protocols.push_back(ToProto); 3821 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3822 } 3823 3824 // FIXME: If we're merging, make sure that the protocol list is the same. 3825 To->setProtocolList(Protocols.data(), Protocols.size(), 3826 ProtocolLocs.data(), Importer.getToContext()); 3827 3828 // Import categories. When the categories themselves are imported, they'll 3829 // hook themselves into this interface. 3830 for (auto *Cat : From->known_categories()) 3831 Importer.Import(Cat); 3832 3833 // If we have an @implementation, import it as well. 3834 if (From->getImplementation()) { 3835 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>( 3836 Importer.Import(From->getImplementation())); 3837 if (!Impl) 3838 return true; 3839 3840 To->setImplementation(Impl); 3841 } 3842 3843 if (shouldForceImportDeclContext(Kind)) { 3844 // Import all of the members of this class. 3845 ImportDeclContext(From, /*ForceImport=*/true); 3846 } 3847 return false; 3848 } 3849 3850 ObjCTypeParamList * 3851 ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { 3852 if (!list) 3853 return nullptr; 3854 3855 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams; 3856 for (auto fromTypeParam : *list) { 3857 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>( 3858 Importer.Import(fromTypeParam)); 3859 if (!toTypeParam) 3860 return nullptr; 3861 3862 toTypeParams.push_back(toTypeParam); 3863 } 3864 3865 return ObjCTypeParamList::create(Importer.getToContext(), 3866 Importer.Import(list->getLAngleLoc()), 3867 toTypeParams, 3868 Importer.Import(list->getRAngleLoc())); 3869 } 3870 3871 Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { 3872 // If this class has a definition in the translation unit we're coming from, 3873 // but this particular declaration is not that definition, import the 3874 // definition and map to that. 3875 ObjCInterfaceDecl *Definition = D->getDefinition(); 3876 if (Definition && Definition != D) { 3877 Decl *ImportedDef = Importer.Import(Definition); 3878 if (!ImportedDef) 3879 return nullptr; 3880 3881 return Importer.Imported(D, ImportedDef); 3882 } 3883 3884 // Import the major distinguishing characteristics of an @interface. 3885 DeclContext *DC, *LexicalDC; 3886 DeclarationName Name; 3887 SourceLocation Loc; 3888 NamedDecl *ToD; 3889 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3890 return nullptr; 3891 if (ToD) 3892 return ToD; 3893 3894 // Look for an existing interface with the same name. 3895 ObjCInterfaceDecl *MergeWithIface = nullptr; 3896 SmallVector<NamedDecl *, 2> FoundDecls; 3897 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3898 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3899 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 3900 continue; 3901 3902 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I]))) 3903 break; 3904 } 3905 3906 // Create an interface declaration, if one does not already exist. 3907 ObjCInterfaceDecl *ToIface = MergeWithIface; 3908 if (!ToIface) { 3909 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC, 3910 Importer.Import(D->getAtStartLoc()), 3911 Name.getAsIdentifierInfo(), 3912 /*TypeParamList=*/nullptr, 3913 /*PrevDecl=*/nullptr, Loc, 3914 D->isImplicitInterfaceDecl()); 3915 ToIface->setLexicalDeclContext(LexicalDC); 3916 LexicalDC->addDeclInternal(ToIface); 3917 } 3918 Importer.Imported(D, ToIface); 3919 // Import the type parameter list after calling Imported, to avoid 3920 // loops when bringing in their DeclContext. 3921 ToIface->setTypeParamList(ImportObjCTypeParamList( 3922 D->getTypeParamListAsWritten())); 3923 3924 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface)) 3925 return nullptr; 3926 3927 return ToIface; 3928 } 3929 3930 Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { 3931 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>( 3932 Importer.Import(D->getCategoryDecl())); 3933 if (!Category) 3934 return nullptr; 3935 3936 ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); 3937 if (!ToImpl) { 3938 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 3939 if (!DC) 3940 return nullptr; 3941 3942 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc()); 3943 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC, 3944 Importer.Import(D->getIdentifier()), 3945 Category->getClassInterface(), 3946 Importer.Import(D->getLocation()), 3947 Importer.Import(D->getAtStartLoc()), 3948 CategoryNameLoc); 3949 3950 DeclContext *LexicalDC = DC; 3951 if (D->getDeclContext() != D->getLexicalDeclContext()) { 3952 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 3953 if (!LexicalDC) 3954 return nullptr; 3955 3956 ToImpl->setLexicalDeclContext(LexicalDC); 3957 } 3958 3959 LexicalDC->addDeclInternal(ToImpl); 3960 Category->setImplementation(ToImpl); 3961 } 3962 3963 Importer.Imported(D, ToImpl); 3964 ImportDeclContext(D); 3965 return ToImpl; 3966 } 3967 3968 Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { 3969 // Find the corresponding interface. 3970 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>( 3971 Importer.Import(D->getClassInterface())); 3972 if (!Iface) 3973 return nullptr; 3974 3975 // Import the superclass, if any. 3976 ObjCInterfaceDecl *Super = nullptr; 3977 if (D->getSuperClass()) { 3978 Super = cast_or_null<ObjCInterfaceDecl>( 3979 Importer.Import(D->getSuperClass())); 3980 if (!Super) 3981 return nullptr; 3982 } 3983 3984 ObjCImplementationDecl *Impl = Iface->getImplementation(); 3985 if (!Impl) { 3986 // We haven't imported an implementation yet. Create a new @implementation 3987 // now. 3988 Impl = ObjCImplementationDecl::Create(Importer.getToContext(), 3989 Importer.ImportContext(D->getDeclContext()), 3990 Iface, Super, 3991 Importer.Import(D->getLocation()), 3992 Importer.Import(D->getAtStartLoc()), 3993 Importer.Import(D->getSuperClassLoc()), 3994 Importer.Import(D->getIvarLBraceLoc()), 3995 Importer.Import(D->getIvarRBraceLoc())); 3996 3997 if (D->getDeclContext() != D->getLexicalDeclContext()) { 3998 DeclContext *LexicalDC 3999 = Importer.ImportContext(D->getLexicalDeclContext()); 4000 if (!LexicalDC) 4001 return nullptr; 4002 Impl->setLexicalDeclContext(LexicalDC); 4003 } 4004 4005 // Associate the implementation with the class it implements. 4006 Iface->setImplementation(Impl); 4007 Importer.Imported(D, Iface->getImplementation()); 4008 } else { 4009 Importer.Imported(D, Iface->getImplementation()); 4010 4011 // Verify that the existing @implementation has the same superclass. 4012 if ((Super && !Impl->getSuperClass()) || 4013 (!Super && Impl->getSuperClass()) || 4014 (Super && Impl->getSuperClass() && 4015 !declaresSameEntity(Super->getCanonicalDecl(), 4016 Impl->getSuperClass()))) { 4017 Importer.ToDiag(Impl->getLocation(), 4018 diag::err_odr_objc_superclass_inconsistent) 4019 << Iface->getDeclName(); 4020 // FIXME: It would be nice to have the location of the superclass 4021 // below. 4022 if (Impl->getSuperClass()) 4023 Importer.ToDiag(Impl->getLocation(), 4024 diag::note_odr_objc_superclass) 4025 << Impl->getSuperClass()->getDeclName(); 4026 else 4027 Importer.ToDiag(Impl->getLocation(), 4028 diag::note_odr_objc_missing_superclass); 4029 if (D->getSuperClass()) 4030 Importer.FromDiag(D->getLocation(), 4031 diag::note_odr_objc_superclass) 4032 << D->getSuperClass()->getDeclName(); 4033 else 4034 Importer.FromDiag(D->getLocation(), 4035 diag::note_odr_objc_missing_superclass); 4036 return nullptr; 4037 } 4038 } 4039 4040 // Import all of the members of this @implementation. 4041 ImportDeclContext(D); 4042 4043 return Impl; 4044 } 4045 4046 Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { 4047 // Import the major distinguishing characteristics of an @property. 4048 DeclContext *DC, *LexicalDC; 4049 DeclarationName Name; 4050 SourceLocation Loc; 4051 NamedDecl *ToD; 4052 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4053 return nullptr; 4054 if (ToD) 4055 return ToD; 4056 4057 // Check whether we have already imported this property. 4058 SmallVector<NamedDecl *, 2> FoundDecls; 4059 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4060 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 4061 if (ObjCPropertyDecl *FoundProp 4062 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) { 4063 // Check property types. 4064 if (!Importer.IsStructurallyEquivalent(D->getType(), 4065 FoundProp->getType())) { 4066 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent) 4067 << Name << D->getType() << FoundProp->getType(); 4068 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) 4069 << FoundProp->getType(); 4070 return nullptr; 4071 } 4072 4073 // FIXME: Check property attributes, getters, setters, etc.? 4074 4075 // Consider these properties to be equivalent. 4076 Importer.Imported(D, FoundProp); 4077 return FoundProp; 4078 } 4079 } 4080 4081 // Import the type. 4082 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo()); 4083 if (!TSI) 4084 return nullptr; 4085 4086 // Create the new property. 4087 ObjCPropertyDecl *ToProperty 4088 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc, 4089 Name.getAsIdentifierInfo(), 4090 Importer.Import(D->getAtLoc()), 4091 Importer.Import(D->getLParenLoc()), 4092 Importer.Import(D->getType()), 4093 TSI, 4094 D->getPropertyImplementation()); 4095 Importer.Imported(D, ToProperty); 4096 ToProperty->setLexicalDeclContext(LexicalDC); 4097 LexicalDC->addDeclInternal(ToProperty); 4098 4099 ToProperty->setPropertyAttributes(D->getPropertyAttributes()); 4100 ToProperty->setPropertyAttributesAsWritten( 4101 D->getPropertyAttributesAsWritten()); 4102 ToProperty->setGetterName(Importer.Import(D->getGetterName())); 4103 ToProperty->setSetterName(Importer.Import(D->getSetterName())); 4104 ToProperty->setGetterMethodDecl( 4105 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl()))); 4106 ToProperty->setSetterMethodDecl( 4107 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl()))); 4108 ToProperty->setPropertyIvarDecl( 4109 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl()))); 4110 return ToProperty; 4111 } 4112 4113 Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { 4114 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>( 4115 Importer.Import(D->getPropertyDecl())); 4116 if (!Property) 4117 return nullptr; 4118 4119 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 4120 if (!DC) 4121 return nullptr; 4122 4123 // Import the lexical declaration context. 4124 DeclContext *LexicalDC = DC; 4125 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4126 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4127 if (!LexicalDC) 4128 return nullptr; 4129 } 4130 4131 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC); 4132 if (!InImpl) 4133 return nullptr; 4134 4135 // Import the ivar (for an @synthesize). 4136 ObjCIvarDecl *Ivar = nullptr; 4137 if (D->getPropertyIvarDecl()) { 4138 Ivar = cast_or_null<ObjCIvarDecl>( 4139 Importer.Import(D->getPropertyIvarDecl())); 4140 if (!Ivar) 4141 return nullptr; 4142 } 4143 4144 ObjCPropertyImplDecl *ToImpl 4145 = InImpl->FindPropertyImplDecl(Property->getIdentifier(), 4146 Property->getQueryKind()); 4147 if (!ToImpl) { 4148 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC, 4149 Importer.Import(D->getLocStart()), 4150 Importer.Import(D->getLocation()), 4151 Property, 4152 D->getPropertyImplementation(), 4153 Ivar, 4154 Importer.Import(D->getPropertyIvarDeclLoc())); 4155 ToImpl->setLexicalDeclContext(LexicalDC); 4156 Importer.Imported(D, ToImpl); 4157 LexicalDC->addDeclInternal(ToImpl); 4158 } else { 4159 // Check that we have the same kind of property implementation (@synthesize 4160 // vs. @dynamic). 4161 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { 4162 Importer.ToDiag(ToImpl->getLocation(), 4163 diag::err_odr_objc_property_impl_kind_inconsistent) 4164 << Property->getDeclName() 4165 << (ToImpl->getPropertyImplementation() 4166 == ObjCPropertyImplDecl::Dynamic); 4167 Importer.FromDiag(D->getLocation(), 4168 diag::note_odr_objc_property_impl_kind) 4169 << D->getPropertyDecl()->getDeclName() 4170 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); 4171 return nullptr; 4172 } 4173 4174 // For @synthesize, check that we have the same 4175 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && 4176 Ivar != ToImpl->getPropertyIvarDecl()) { 4177 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), 4178 diag::err_odr_objc_synthesize_ivar_inconsistent) 4179 << Property->getDeclName() 4180 << ToImpl->getPropertyIvarDecl()->getDeclName() 4181 << Ivar->getDeclName(); 4182 Importer.FromDiag(D->getPropertyIvarDeclLoc(), 4183 diag::note_odr_objc_synthesize_ivar_here) 4184 << D->getPropertyIvarDecl()->getDeclName(); 4185 return nullptr; 4186 } 4187 4188 // Merge the existing implementation with the new implementation. 4189 Importer.Imported(D, ToImpl); 4190 } 4191 4192 return ToImpl; 4193 } 4194 4195 Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { 4196 // For template arguments, we adopt the translation unit as our declaration 4197 // context. This context will be fixed when the actual template declaration 4198 // is created. 4199 4200 // FIXME: Import default argument. 4201 return TemplateTypeParmDecl::Create(Importer.getToContext(), 4202 Importer.getToContext().getTranslationUnitDecl(), 4203 Importer.Import(D->getLocStart()), 4204 Importer.Import(D->getLocation()), 4205 D->getDepth(), 4206 D->getIndex(), 4207 Importer.Import(D->getIdentifier()), 4208 D->wasDeclaredWithTypename(), 4209 D->isParameterPack()); 4210 } 4211 4212 Decl * 4213 ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { 4214 // Import the name of this declaration. 4215 DeclarationName Name = Importer.Import(D->getDeclName()); 4216 if (D->getDeclName() && !Name) 4217 return nullptr; 4218 4219 // Import the location of this declaration. 4220 SourceLocation Loc = Importer.Import(D->getLocation()); 4221 4222 // Import the type of this declaration. 4223 QualType T = Importer.Import(D->getType()); 4224 if (T.isNull()) 4225 return nullptr; 4226 4227 // Import type-source information. 4228 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 4229 if (D->getTypeSourceInfo() && !TInfo) 4230 return nullptr; 4231 4232 // FIXME: Import default argument. 4233 4234 return NonTypeTemplateParmDecl::Create(Importer.getToContext(), 4235 Importer.getToContext().getTranslationUnitDecl(), 4236 Importer.Import(D->getInnerLocStart()), 4237 Loc, D->getDepth(), D->getPosition(), 4238 Name.getAsIdentifierInfo(), 4239 T, D->isParameterPack(), TInfo); 4240 } 4241 4242 Decl * 4243 ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { 4244 // Import the name of this declaration. 4245 DeclarationName Name = Importer.Import(D->getDeclName()); 4246 if (D->getDeclName() && !Name) 4247 return nullptr; 4248 4249 // Import the location of this declaration. 4250 SourceLocation Loc = Importer.Import(D->getLocation()); 4251 4252 // Import template parameters. 4253 TemplateParameterList *TemplateParams 4254 = ImportTemplateParameterList(D->getTemplateParameters()); 4255 if (!TemplateParams) 4256 return nullptr; 4257 4258 // FIXME: Import default argument. 4259 4260 return TemplateTemplateParmDecl::Create(Importer.getToContext(), 4261 Importer.getToContext().getTranslationUnitDecl(), 4262 Loc, D->getDepth(), D->getPosition(), 4263 D->isParameterPack(), 4264 Name.getAsIdentifierInfo(), 4265 TemplateParams); 4266 } 4267 4268 Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 4269 // If this record has a definition in the translation unit we're coming from, 4270 // but this particular declaration is not that definition, import the 4271 // definition and map to that. 4272 CXXRecordDecl *Definition 4273 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition()); 4274 if (Definition && Definition != D->getTemplatedDecl()) { 4275 Decl *ImportedDef 4276 = Importer.Import(Definition->getDescribedClassTemplate()); 4277 if (!ImportedDef) 4278 return nullptr; 4279 4280 return Importer.Imported(D, ImportedDef); 4281 } 4282 4283 // Import the major distinguishing characteristics of this class template. 4284 DeclContext *DC, *LexicalDC; 4285 DeclarationName Name; 4286 SourceLocation Loc; 4287 NamedDecl *ToD; 4288 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4289 return nullptr; 4290 if (ToD) 4291 return ToD; 4292 4293 // We may already have a template of the same name; try to find and match it. 4294 if (!DC->isFunctionOrMethod()) { 4295 SmallVector<NamedDecl *, 4> ConflictingDecls; 4296 SmallVector<NamedDecl *, 2> FoundDecls; 4297 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4298 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 4299 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 4300 continue; 4301 4302 Decl *Found = FoundDecls[I]; 4303 if (ClassTemplateDecl *FoundTemplate 4304 = dyn_cast<ClassTemplateDecl>(Found)) { 4305 if (IsStructuralMatch(D, FoundTemplate)) { 4306 // The class templates structurally match; call it the same template. 4307 // FIXME: We may be filling in a forward declaration here. Handle 4308 // this case! 4309 Importer.Imported(D->getTemplatedDecl(), 4310 FoundTemplate->getTemplatedDecl()); 4311 return Importer.Imported(D, FoundTemplate); 4312 } 4313 } 4314 4315 ConflictingDecls.push_back(FoundDecls[I]); 4316 } 4317 4318 if (!ConflictingDecls.empty()) { 4319 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, 4320 ConflictingDecls.data(), 4321 ConflictingDecls.size()); 4322 } 4323 4324 if (!Name) 4325 return nullptr; 4326 } 4327 4328 CXXRecordDecl *DTemplated = D->getTemplatedDecl(); 4329 4330 // Create the declaration that is being templated. 4331 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart()); 4332 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation()); 4333 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(), 4334 DTemplated->getTagKind(), 4335 DC, StartLoc, IdLoc, 4336 Name.getAsIdentifierInfo()); 4337 D2Templated->setAccess(DTemplated->getAccess()); 4338 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc())); 4339 D2Templated->setLexicalDeclContext(LexicalDC); 4340 4341 // Create the class template declaration itself. 4342 TemplateParameterList *TemplateParams 4343 = ImportTemplateParameterList(D->getTemplateParameters()); 4344 if (!TemplateParams) 4345 return nullptr; 4346 4347 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC, 4348 Loc, Name, TemplateParams, 4349 D2Templated, 4350 /*PrevDecl=*/nullptr); 4351 D2Templated->setDescribedClassTemplate(D2); 4352 4353 D2->setAccess(D->getAccess()); 4354 D2->setLexicalDeclContext(LexicalDC); 4355 LexicalDC->addDeclInternal(D2); 4356 4357 // Note the relationship between the class templates. 4358 Importer.Imported(D, D2); 4359 Importer.Imported(DTemplated, D2Templated); 4360 4361 if (DTemplated->isCompleteDefinition() && 4362 !D2Templated->isCompleteDefinition()) { 4363 // FIXME: Import definition! 4364 } 4365 4366 return D2; 4367 } 4368 4369 Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl( 4370 ClassTemplateSpecializationDecl *D) { 4371 // If this record has a definition in the translation unit we're coming from, 4372 // but this particular declaration is not that definition, import the 4373 // definition and map to that. 4374 TagDecl *Definition = D->getDefinition(); 4375 if (Definition && Definition != D) { 4376 Decl *ImportedDef = Importer.Import(Definition); 4377 if (!ImportedDef) 4378 return nullptr; 4379 4380 return Importer.Imported(D, ImportedDef); 4381 } 4382 4383 ClassTemplateDecl *ClassTemplate 4384 = cast_or_null<ClassTemplateDecl>(Importer.Import( 4385 D->getSpecializedTemplate())); 4386 if (!ClassTemplate) 4387 return nullptr; 4388 4389 // Import the context of this declaration. 4390 DeclContext *DC = ClassTemplate->getDeclContext(); 4391 if (!DC) 4392 return nullptr; 4393 4394 DeclContext *LexicalDC = DC; 4395 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4396 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4397 if (!LexicalDC) 4398 return nullptr; 4399 } 4400 4401 // Import the location of this declaration. 4402 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 4403 SourceLocation IdLoc = Importer.Import(D->getLocation()); 4404 4405 // Import template arguments. 4406 SmallVector<TemplateArgument, 2> TemplateArgs; 4407 if (ImportTemplateArguments(D->getTemplateArgs().data(), 4408 D->getTemplateArgs().size(), 4409 TemplateArgs)) 4410 return nullptr; 4411 4412 // Try to find an existing specialization with these template arguments. 4413 void *InsertPos = nullptr; 4414 ClassTemplateSpecializationDecl *D2 4415 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos); 4416 if (D2) { 4417 // We already have a class template specialization with these template 4418 // arguments. 4419 4420 // FIXME: Check for specialization vs. instantiation errors. 4421 4422 if (RecordDecl *FoundDef = D2->getDefinition()) { 4423 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) { 4424 // The record types structurally match, or the "from" translation 4425 // unit only had a forward declaration anyway; call it the same 4426 // function. 4427 return Importer.Imported(D, FoundDef); 4428 } 4429 } 4430 } else { 4431 // Create a new specialization. 4432 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(), 4433 D->getTagKind(), DC, 4434 StartLoc, IdLoc, 4435 ClassTemplate, 4436 TemplateArgs.data(), 4437 TemplateArgs.size(), 4438 /*PrevDecl=*/nullptr); 4439 D2->setSpecializationKind(D->getSpecializationKind()); 4440 4441 // Add this specialization to the class template. 4442 ClassTemplate->AddSpecialization(D2, InsertPos); 4443 4444 // Import the qualifier, if any. 4445 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 4446 4447 // Add the specialization to this context. 4448 D2->setLexicalDeclContext(LexicalDC); 4449 LexicalDC->addDeclInternal(D2); 4450 } 4451 Importer.Imported(D, D2); 4452 4453 if (D->isCompleteDefinition() && ImportDefinition(D, D2)) 4454 return nullptr; 4455 4456 return D2; 4457 } 4458 4459 Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { 4460 // If this variable has a definition in the translation unit we're coming 4461 // from, 4462 // but this particular declaration is not that definition, import the 4463 // definition and map to that. 4464 VarDecl *Definition = 4465 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition()); 4466 if (Definition && Definition != D->getTemplatedDecl()) { 4467 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate()); 4468 if (!ImportedDef) 4469 return nullptr; 4470 4471 return Importer.Imported(D, ImportedDef); 4472 } 4473 4474 // Import the major distinguishing characteristics of this variable template. 4475 DeclContext *DC, *LexicalDC; 4476 DeclarationName Name; 4477 SourceLocation Loc; 4478 NamedDecl *ToD; 4479 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4480 return nullptr; 4481 if (ToD) 4482 return ToD; 4483 4484 // We may already have a template of the same name; try to find and match it. 4485 assert(!DC->isFunctionOrMethod() && 4486 "Variable templates cannot be declared at function scope"); 4487 SmallVector<NamedDecl *, 4> ConflictingDecls; 4488 SmallVector<NamedDecl *, 2> FoundDecls; 4489 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4490 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 4491 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 4492 continue; 4493 4494 Decl *Found = FoundDecls[I]; 4495 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) { 4496 if (IsStructuralMatch(D, FoundTemplate)) { 4497 // The variable templates structurally match; call it the same template. 4498 Importer.Imported(D->getTemplatedDecl(), 4499 FoundTemplate->getTemplatedDecl()); 4500 return Importer.Imported(D, FoundTemplate); 4501 } 4502 } 4503 4504 ConflictingDecls.push_back(FoundDecls[I]); 4505 } 4506 4507 if (!ConflictingDecls.empty()) { 4508 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, 4509 ConflictingDecls.data(), 4510 ConflictingDecls.size()); 4511 } 4512 4513 if (!Name) 4514 return nullptr; 4515 4516 VarDecl *DTemplated = D->getTemplatedDecl(); 4517 4518 // Import the type. 4519 QualType T = Importer.Import(DTemplated->getType()); 4520 if (T.isNull()) 4521 return nullptr; 4522 4523 // Create the declaration that is being templated. 4524 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart()); 4525 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation()); 4526 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo()); 4527 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc, 4528 IdLoc, Name.getAsIdentifierInfo(), T, 4529 TInfo, DTemplated->getStorageClass()); 4530 D2Templated->setAccess(DTemplated->getAccess()); 4531 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc())); 4532 D2Templated->setLexicalDeclContext(LexicalDC); 4533 4534 // Importer.Imported(DTemplated, D2Templated); 4535 // LexicalDC->addDeclInternal(D2Templated); 4536 4537 // Merge the initializer. 4538 if (ImportDefinition(DTemplated, D2Templated)) 4539 return nullptr; 4540 4541 // Create the variable template declaration itself. 4542 TemplateParameterList *TemplateParams = 4543 ImportTemplateParameterList(D->getTemplateParameters()); 4544 if (!TemplateParams) 4545 return nullptr; 4546 4547 VarTemplateDecl *D2 = VarTemplateDecl::Create( 4548 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated); 4549 D2Templated->setDescribedVarTemplate(D2); 4550 4551 D2->setAccess(D->getAccess()); 4552 D2->setLexicalDeclContext(LexicalDC); 4553 LexicalDC->addDeclInternal(D2); 4554 4555 // Note the relationship between the variable templates. 4556 Importer.Imported(D, D2); 4557 Importer.Imported(DTemplated, D2Templated); 4558 4559 if (DTemplated->isThisDeclarationADefinition() && 4560 !D2Templated->isThisDeclarationADefinition()) { 4561 // FIXME: Import definition! 4562 } 4563 4564 return D2; 4565 } 4566 4567 Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl( 4568 VarTemplateSpecializationDecl *D) { 4569 // If this record has a definition in the translation unit we're coming from, 4570 // but this particular declaration is not that definition, import the 4571 // definition and map to that. 4572 VarDecl *Definition = D->getDefinition(); 4573 if (Definition && Definition != D) { 4574 Decl *ImportedDef = Importer.Import(Definition); 4575 if (!ImportedDef) 4576 return nullptr; 4577 4578 return Importer.Imported(D, ImportedDef); 4579 } 4580 4581 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>( 4582 Importer.Import(D->getSpecializedTemplate())); 4583 if (!VarTemplate) 4584 return nullptr; 4585 4586 // Import the context of this declaration. 4587 DeclContext *DC = VarTemplate->getDeclContext(); 4588 if (!DC) 4589 return nullptr; 4590 4591 DeclContext *LexicalDC = DC; 4592 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4593 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4594 if (!LexicalDC) 4595 return nullptr; 4596 } 4597 4598 // Import the location of this declaration. 4599 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 4600 SourceLocation IdLoc = Importer.Import(D->getLocation()); 4601 4602 // Import template arguments. 4603 SmallVector<TemplateArgument, 2> TemplateArgs; 4604 if (ImportTemplateArguments(D->getTemplateArgs().data(), 4605 D->getTemplateArgs().size(), TemplateArgs)) 4606 return nullptr; 4607 4608 // Try to find an existing specialization with these template arguments. 4609 void *InsertPos = nullptr; 4610 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization( 4611 TemplateArgs, InsertPos); 4612 if (D2) { 4613 // We already have a variable template specialization with these template 4614 // arguments. 4615 4616 // FIXME: Check for specialization vs. instantiation errors. 4617 4618 if (VarDecl *FoundDef = D2->getDefinition()) { 4619 if (!D->isThisDeclarationADefinition() || 4620 IsStructuralMatch(D, FoundDef)) { 4621 // The record types structurally match, or the "from" translation 4622 // unit only had a forward declaration anyway; call it the same 4623 // variable. 4624 return Importer.Imported(D, FoundDef); 4625 } 4626 } 4627 } else { 4628 4629 // Import the type. 4630 QualType T = Importer.Import(D->getType()); 4631 if (T.isNull()) 4632 return nullptr; 4633 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 4634 4635 // Create a new specialization. 4636 D2 = VarTemplateSpecializationDecl::Create( 4637 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo, 4638 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size()); 4639 D2->setSpecializationKind(D->getSpecializationKind()); 4640 D2->setTemplateArgsInfo(D->getTemplateArgsInfo()); 4641 4642 // Add this specialization to the class template. 4643 VarTemplate->AddSpecialization(D2, InsertPos); 4644 4645 // Import the qualifier, if any. 4646 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 4647 4648 // Add the specialization to this context. 4649 D2->setLexicalDeclContext(LexicalDC); 4650 LexicalDC->addDeclInternal(D2); 4651 } 4652 Importer.Imported(D, D2); 4653 4654 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2)) 4655 return nullptr; 4656 4657 return D2; 4658 } 4659 4660 //---------------------------------------------------------------------------- 4661 // Import Statements 4662 //---------------------------------------------------------------------------- 4663 4664 DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) { 4665 if (DG.isNull()) 4666 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0); 4667 size_t NumDecls = DG.end() - DG.begin(); 4668 SmallVector<Decl *, 1> ToDecls(NumDecls); 4669 auto &_Importer = this->Importer; 4670 std::transform(DG.begin(), DG.end(), ToDecls.begin(), 4671 [&_Importer](Decl *D) -> Decl * { 4672 return _Importer.Import(D); 4673 }); 4674 return DeclGroupRef::Create(Importer.getToContext(), 4675 ToDecls.begin(), 4676 NumDecls); 4677 } 4678 4679 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) { 4680 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node) 4681 << S->getStmtClassName(); 4682 return nullptr; 4683 } 4684 4685 Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) { 4686 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup()); 4687 for (Decl *ToD : ToDG) { 4688 if (!ToD) 4689 return nullptr; 4690 } 4691 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc()); 4692 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc()); 4693 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc); 4694 } 4695 4696 Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) { 4697 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc()); 4698 return new (Importer.getToContext()) NullStmt(ToSemiLoc, 4699 S->hasLeadingEmptyMacro()); 4700 } 4701 4702 Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) { 4703 llvm::ArrayRef<Stmt *> ToStmts; 4704 4705 if (!ImportArray(S->body_begin(), S->body_end(), ToStmts)) 4706 return nullptr; 4707 4708 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc()); 4709 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc()); 4710 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(), 4711 ToStmts, 4712 ToLBraceLoc, ToRBraceLoc); 4713 } 4714 4715 Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) { 4716 Expr *ToLHS = Importer.Import(S->getLHS()); 4717 if (!ToLHS) 4718 return nullptr; 4719 Expr *ToRHS = Importer.Import(S->getRHS()); 4720 if (!ToRHS && S->getRHS()) 4721 return nullptr; 4722 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc()); 4723 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc()); 4724 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 4725 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS, 4726 ToCaseLoc, ToEllipsisLoc, 4727 ToColonLoc); 4728 } 4729 4730 Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) { 4731 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc()); 4732 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 4733 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 4734 if (!ToSubStmt && S->getSubStmt()) 4735 return nullptr; 4736 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc, 4737 ToSubStmt); 4738 } 4739 4740 Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) { 4741 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc()); 4742 LabelDecl *ToLabelDecl = 4743 cast_or_null<LabelDecl>(Importer.Import(S->getDecl())); 4744 if (!ToLabelDecl && S->getDecl()) 4745 return nullptr; 4746 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 4747 if (!ToSubStmt && S->getSubStmt()) 4748 return nullptr; 4749 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl, 4750 ToSubStmt); 4751 } 4752 4753 Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) { 4754 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc()); 4755 ArrayRef<const Attr*> FromAttrs(S->getAttrs()); 4756 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size()); 4757 ASTContext &_ToContext = Importer.getToContext(); 4758 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(), 4759 [&_ToContext](const Attr *A) -> const Attr * { 4760 return A->clone(_ToContext); 4761 }); 4762 for (const Attr *ToA : ToAttrs) { 4763 if (!ToA) 4764 return nullptr; 4765 } 4766 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 4767 if (!ToSubStmt && S->getSubStmt()) 4768 return nullptr; 4769 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc, 4770 ToAttrs, ToSubStmt); 4771 } 4772 4773 Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) { 4774 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc()); 4775 VarDecl *ToConditionVariable = nullptr; 4776 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 4777 ToConditionVariable = 4778 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 4779 if (!ToConditionVariable) 4780 return nullptr; 4781 } 4782 Expr *ToCondition = Importer.Import(S->getCond()); 4783 if (!ToCondition && S->getCond()) 4784 return nullptr; 4785 Stmt *ToThenStmt = Importer.Import(S->getThen()); 4786 if (!ToThenStmt && S->getThen()) 4787 return nullptr; 4788 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc()); 4789 Stmt *ToElseStmt = Importer.Import(S->getElse()); 4790 if (!ToElseStmt && S->getElse()) 4791 return nullptr; 4792 return new (Importer.getToContext()) IfStmt(Importer.getToContext(), 4793 ToIfLoc, ToConditionVariable, 4794 ToCondition, ToThenStmt, 4795 ToElseLoc, ToElseStmt); 4796 } 4797 4798 Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) { 4799 VarDecl *ToConditionVariable = nullptr; 4800 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 4801 ToConditionVariable = 4802 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 4803 if (!ToConditionVariable) 4804 return nullptr; 4805 } 4806 Expr *ToCondition = Importer.Import(S->getCond()); 4807 if (!ToCondition && S->getCond()) 4808 return nullptr; 4809 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt( 4810 Importer.getToContext(), ToConditionVariable, 4811 ToCondition); 4812 Stmt *ToBody = Importer.Import(S->getBody()); 4813 if (!ToBody && S->getBody()) 4814 return nullptr; 4815 ToStmt->setBody(ToBody); 4816 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc())); 4817 // Now we have to re-chain the cases. 4818 SwitchCase *LastChainedSwitchCase = nullptr; 4819 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr; 4820 SC = SC->getNextSwitchCase()) { 4821 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC)); 4822 if (!ToSC) 4823 return nullptr; 4824 if (LastChainedSwitchCase) 4825 LastChainedSwitchCase->setNextSwitchCase(ToSC); 4826 else 4827 ToStmt->setSwitchCaseList(ToSC); 4828 LastChainedSwitchCase = ToSC; 4829 } 4830 return ToStmt; 4831 } 4832 4833 Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) { 4834 VarDecl *ToConditionVariable = nullptr; 4835 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 4836 ToConditionVariable = 4837 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 4838 if (!ToConditionVariable) 4839 return nullptr; 4840 } 4841 Expr *ToCondition = Importer.Import(S->getCond()); 4842 if (!ToCondition && S->getCond()) 4843 return nullptr; 4844 Stmt *ToBody = Importer.Import(S->getBody()); 4845 if (!ToBody && S->getBody()) 4846 return nullptr; 4847 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); 4848 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(), 4849 ToConditionVariable, 4850 ToCondition, ToBody, 4851 ToWhileLoc); 4852 } 4853 4854 Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) { 4855 Stmt *ToBody = Importer.Import(S->getBody()); 4856 if (!ToBody && S->getBody()) 4857 return nullptr; 4858 Expr *ToCondition = Importer.Import(S->getCond()); 4859 if (!ToCondition && S->getCond()) 4860 return nullptr; 4861 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc()); 4862 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); 4863 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 4864 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition, 4865 ToDoLoc, ToWhileLoc, 4866 ToRParenLoc); 4867 } 4868 4869 Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) { 4870 Stmt *ToInit = Importer.Import(S->getInit()); 4871 if (!ToInit && S->getInit()) 4872 return nullptr; 4873 Expr *ToCondition = Importer.Import(S->getCond()); 4874 if (!ToCondition && S->getCond()) 4875 return nullptr; 4876 VarDecl *ToConditionVariable = nullptr; 4877 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 4878 ToConditionVariable = 4879 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 4880 if (!ToConditionVariable) 4881 return nullptr; 4882 } 4883 Expr *ToInc = Importer.Import(S->getInc()); 4884 if (!ToInc && S->getInc()) 4885 return nullptr; 4886 Stmt *ToBody = Importer.Import(S->getBody()); 4887 if (!ToBody && S->getBody()) 4888 return nullptr; 4889 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 4890 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc()); 4891 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 4892 return new (Importer.getToContext()) ForStmt(Importer.getToContext(), 4893 ToInit, ToCondition, 4894 ToConditionVariable, 4895 ToInc, ToBody, 4896 ToForLoc, ToLParenLoc, 4897 ToRParenLoc); 4898 } 4899 4900 Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) { 4901 LabelDecl *ToLabel = nullptr; 4902 if (LabelDecl *FromLabel = S->getLabel()) { 4903 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel)); 4904 if (!ToLabel) 4905 return nullptr; 4906 } 4907 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); 4908 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc()); 4909 return new (Importer.getToContext()) GotoStmt(ToLabel, 4910 ToGotoLoc, ToLabelLoc); 4911 } 4912 4913 Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { 4914 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); 4915 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc()); 4916 Expr *ToTarget = Importer.Import(S->getTarget()); 4917 if (!ToTarget && S->getTarget()) 4918 return nullptr; 4919 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc, 4920 ToTarget); 4921 } 4922 4923 Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) { 4924 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc()); 4925 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc); 4926 } 4927 4928 Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) { 4929 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc()); 4930 return new (Importer.getToContext()) BreakStmt(ToBreakLoc); 4931 } 4932 4933 Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) { 4934 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc()); 4935 Expr *ToRetExpr = Importer.Import(S->getRetValue()); 4936 if (!ToRetExpr && S->getRetValue()) 4937 return nullptr; 4938 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate()); 4939 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate)); 4940 if (!ToNRVOCandidate && NRVOCandidate) 4941 return nullptr; 4942 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr, 4943 ToNRVOCandidate); 4944 } 4945 4946 Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) { 4947 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc()); 4948 VarDecl *ToExceptionDecl = nullptr; 4949 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) { 4950 ToExceptionDecl = 4951 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); 4952 if (!ToExceptionDecl) 4953 return nullptr; 4954 } 4955 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock()); 4956 if (!ToHandlerBlock && S->getHandlerBlock()) 4957 return nullptr; 4958 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc, 4959 ToExceptionDecl, 4960 ToHandlerBlock); 4961 } 4962 4963 Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) { 4964 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc()); 4965 Stmt *ToTryBlock = Importer.Import(S->getTryBlock()); 4966 if (!ToTryBlock && S->getTryBlock()) 4967 return nullptr; 4968 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers()); 4969 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) { 4970 CXXCatchStmt *FromHandler = S->getHandler(HI); 4971 if (Stmt *ToHandler = Importer.Import(FromHandler)) 4972 ToHandlers[HI] = ToHandler; 4973 else 4974 return nullptr; 4975 } 4976 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock, 4977 ToHandlers); 4978 } 4979 4980 Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { 4981 DeclStmt *ToRange = 4982 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt())); 4983 if (!ToRange && S->getRangeStmt()) 4984 return nullptr; 4985 DeclStmt *ToBegin = 4986 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt())); 4987 if (!ToBegin && S->getBeginStmt()) 4988 return nullptr; 4989 DeclStmt *ToEnd = 4990 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt())); 4991 if (!ToEnd && S->getEndStmt()) 4992 return nullptr; 4993 Expr *ToCond = Importer.Import(S->getCond()); 4994 if (!ToCond && S->getCond()) 4995 return nullptr; 4996 Expr *ToInc = Importer.Import(S->getInc()); 4997 if (!ToInc && S->getInc()) 4998 return nullptr; 4999 DeclStmt *ToLoopVar = 5000 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt())); 5001 if (!ToLoopVar && S->getLoopVarStmt()) 5002 return nullptr; 5003 Stmt *ToBody = Importer.Import(S->getBody()); 5004 if (!ToBody && S->getBody()) 5005 return nullptr; 5006 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 5007 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc()); 5008 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 5009 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5010 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd, 5011 ToCond, ToInc, 5012 ToLoopVar, ToBody, 5013 ToForLoc, ToCoawaitLoc, 5014 ToColonLoc, ToRParenLoc); 5015 } 5016 5017 Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { 5018 Stmt *ToElem = Importer.Import(S->getElement()); 5019 if (!ToElem && S->getElement()) 5020 return nullptr; 5021 Expr *ToCollect = Importer.Import(S->getCollection()); 5022 if (!ToCollect && S->getCollection()) 5023 return nullptr; 5024 Stmt *ToBody = Importer.Import(S->getBody()); 5025 if (!ToBody && S->getBody()) 5026 return nullptr; 5027 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 5028 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5029 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem, 5030 ToCollect, 5031 ToBody, ToForLoc, 5032 ToRParenLoc); 5033 } 5034 5035 Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { 5036 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc()); 5037 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5038 VarDecl *ToExceptionDecl = nullptr; 5039 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) { 5040 ToExceptionDecl = 5041 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); 5042 if (!ToExceptionDecl) 5043 return nullptr; 5044 } 5045 Stmt *ToBody = Importer.Import(S->getCatchBody()); 5046 if (!ToBody && S->getCatchBody()) 5047 return nullptr; 5048 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc, 5049 ToRParenLoc, 5050 ToExceptionDecl, 5051 ToBody); 5052 } 5053 5054 Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { 5055 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc()); 5056 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody()); 5057 if (!ToAtFinallyStmt && S->getFinallyBody()) 5058 return nullptr; 5059 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc, 5060 ToAtFinallyStmt); 5061 } 5062 5063 Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { 5064 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc()); 5065 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody()); 5066 if (!ToAtTryStmt && S->getTryBody()) 5067 return nullptr; 5068 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts()); 5069 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) { 5070 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI); 5071 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt)) 5072 ToCatchStmts[CI] = ToCatchStmt; 5073 else 5074 return nullptr; 5075 } 5076 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt()); 5077 if (!ToAtFinallyStmt && S->getFinallyStmt()) 5078 return nullptr; 5079 return ObjCAtTryStmt::Create(Importer.getToContext(), 5080 ToAtTryLoc, ToAtTryStmt, 5081 ToCatchStmts.begin(), ToCatchStmts.size(), 5082 ToAtFinallyStmt); 5083 } 5084 5085 Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt 5086 (ObjCAtSynchronizedStmt *S) { 5087 SourceLocation ToAtSynchronizedLoc = 5088 Importer.Import(S->getAtSynchronizedLoc()); 5089 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr()); 5090 if (!ToSynchExpr && S->getSynchExpr()) 5091 return nullptr; 5092 Stmt *ToSynchBody = Importer.Import(S->getSynchBody()); 5093 if (!ToSynchBody && S->getSynchBody()) 5094 return nullptr; 5095 return new (Importer.getToContext()) ObjCAtSynchronizedStmt( 5096 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody); 5097 } 5098 5099 Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { 5100 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc()); 5101 Expr *ToThrow = Importer.Import(S->getThrowExpr()); 5102 if (!ToThrow && S->getThrowExpr()) 5103 return nullptr; 5104 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow); 5105 } 5106 5107 Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt 5108 (ObjCAutoreleasePoolStmt *S) { 5109 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc()); 5110 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 5111 if (!ToSubStmt && S->getSubStmt()) 5112 return nullptr; 5113 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc, 5114 ToSubStmt); 5115 } 5116 5117 //---------------------------------------------------------------------------- 5118 // Import Expressions 5119 //---------------------------------------------------------------------------- 5120 Expr *ASTNodeImporter::VisitExpr(Expr *E) { 5121 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node) 5122 << E->getStmtClassName(); 5123 return nullptr; 5124 } 5125 5126 Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { 5127 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl())); 5128 if (!ToD) 5129 return nullptr; 5130 5131 NamedDecl *FoundD = nullptr; 5132 if (E->getDecl() != E->getFoundDecl()) { 5133 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl())); 5134 if (!FoundD) 5135 return nullptr; 5136 } 5137 5138 QualType T = Importer.Import(E->getType()); 5139 if (T.isNull()) 5140 return nullptr; 5141 5142 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(), 5143 Importer.Import(E->getQualifierLoc()), 5144 Importer.Import(E->getTemplateKeywordLoc()), 5145 ToD, 5146 E->refersToEnclosingVariableOrCapture(), 5147 Importer.Import(E->getLocation()), 5148 T, E->getValueKind(), 5149 FoundD, 5150 /*FIXME:TemplateArgs=*/nullptr); 5151 if (E->hadMultipleCandidates()) 5152 DRE->setHadMultipleCandidates(true); 5153 return DRE; 5154 } 5155 5156 Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { 5157 QualType T = Importer.Import(E->getType()); 5158 if (T.isNull()) 5159 return nullptr; 5160 5161 return IntegerLiteral::Create(Importer.getToContext(), 5162 E->getValue(), T, 5163 Importer.Import(E->getLocation())); 5164 } 5165 5166 Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { 5167 QualType T = Importer.Import(E->getType()); 5168 if (T.isNull()) 5169 return nullptr; 5170 5171 return new (Importer.getToContext()) CharacterLiteral(E->getValue(), 5172 E->getKind(), T, 5173 Importer.Import(E->getLocation())); 5174 } 5175 5176 Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) { 5177 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5178 if (!SubExpr) 5179 return nullptr; 5180 5181 return new (Importer.getToContext()) 5182 ParenExpr(Importer.Import(E->getLParen()), 5183 Importer.Import(E->getRParen()), 5184 SubExpr); 5185 } 5186 5187 Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { 5188 QualType T = Importer.Import(E->getType()); 5189 if (T.isNull()) 5190 return nullptr; 5191 5192 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5193 if (!SubExpr) 5194 return nullptr; 5195 5196 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(), 5197 T, E->getValueKind(), 5198 E->getObjectKind(), 5199 Importer.Import(E->getOperatorLoc())); 5200 } 5201 5202 Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr( 5203 UnaryExprOrTypeTraitExpr *E) { 5204 QualType ResultType = Importer.Import(E->getType()); 5205 5206 if (E->isArgumentType()) { 5207 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo()); 5208 if (!TInfo) 5209 return nullptr; 5210 5211 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), 5212 TInfo, ResultType, 5213 Importer.Import(E->getOperatorLoc()), 5214 Importer.Import(E->getRParenLoc())); 5215 } 5216 5217 Expr *SubExpr = Importer.Import(E->getArgumentExpr()); 5218 if (!SubExpr) 5219 return nullptr; 5220 5221 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), 5222 SubExpr, ResultType, 5223 Importer.Import(E->getOperatorLoc()), 5224 Importer.Import(E->getRParenLoc())); 5225 } 5226 5227 Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { 5228 QualType T = Importer.Import(E->getType()); 5229 if (T.isNull()) 5230 return nullptr; 5231 5232 Expr *LHS = Importer.Import(E->getLHS()); 5233 if (!LHS) 5234 return nullptr; 5235 5236 Expr *RHS = Importer.Import(E->getRHS()); 5237 if (!RHS) 5238 return nullptr; 5239 5240 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(), 5241 T, E->getValueKind(), 5242 E->getObjectKind(), 5243 Importer.Import(E->getOperatorLoc()), 5244 E->isFPContractable()); 5245 } 5246 5247 Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { 5248 QualType T = Importer.Import(E->getType()); 5249 if (T.isNull()) 5250 return nullptr; 5251 5252 QualType CompLHSType = Importer.Import(E->getComputationLHSType()); 5253 if (CompLHSType.isNull()) 5254 return nullptr; 5255 5256 QualType CompResultType = Importer.Import(E->getComputationResultType()); 5257 if (CompResultType.isNull()) 5258 return nullptr; 5259 5260 Expr *LHS = Importer.Import(E->getLHS()); 5261 if (!LHS) 5262 return nullptr; 5263 5264 Expr *RHS = Importer.Import(E->getRHS()); 5265 if (!RHS) 5266 return nullptr; 5267 5268 return new (Importer.getToContext()) 5269 CompoundAssignOperator(LHS, RHS, E->getOpcode(), 5270 T, E->getValueKind(), 5271 E->getObjectKind(), 5272 CompLHSType, CompResultType, 5273 Importer.Import(E->getOperatorLoc()), 5274 E->isFPContractable()); 5275 } 5276 5277 static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) { 5278 if (E->path_empty()) return false; 5279 5280 // TODO: import cast paths 5281 return true; 5282 } 5283 5284 Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { 5285 QualType T = Importer.Import(E->getType()); 5286 if (T.isNull()) 5287 return nullptr; 5288 5289 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5290 if (!SubExpr) 5291 return nullptr; 5292 5293 CXXCastPath BasePath; 5294 if (ImportCastPath(E, BasePath)) 5295 return nullptr; 5296 5297 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(), 5298 SubExpr, &BasePath, E->getValueKind()); 5299 } 5300 5301 Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) { 5302 QualType T = Importer.Import(E->getType()); 5303 if (T.isNull()) 5304 return nullptr; 5305 5306 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5307 if (!SubExpr) 5308 return nullptr; 5309 5310 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten()); 5311 if (!TInfo && E->getTypeInfoAsWritten()) 5312 return nullptr; 5313 5314 CXXCastPath BasePath; 5315 if (ImportCastPath(E, BasePath)) 5316 return nullptr; 5317 5318 return CStyleCastExpr::Create(Importer.getToContext(), T, 5319 E->getValueKind(), E->getCastKind(), 5320 SubExpr, &BasePath, TInfo, 5321 Importer.Import(E->getLParenLoc()), 5322 Importer.Import(E->getRParenLoc())); 5323 } 5324 5325 Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) { 5326 QualType T = Importer.Import(E->getType()); 5327 if (T.isNull()) 5328 return nullptr; 5329 5330 CXXConstructorDecl *ToCCD = 5331 dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor())); 5332 if (!ToCCD && E->getConstructor()) 5333 return nullptr; 5334 5335 ArrayRef<Expr *> ToArgs; 5336 5337 if (!ImportArray(E->arg_begin(), E->arg_end(), ToArgs)) 5338 return nullptr; 5339 5340 return CXXConstructExpr::Create(Importer.getToContext(), T, 5341 Importer.Import(E->getLocation()), 5342 ToCCD, E->isElidable(), 5343 ToArgs, E->hadMultipleCandidates(), 5344 E->isListInitialization(), 5345 E->isStdInitListInitialization(), 5346 E->requiresZeroInitialization(), 5347 E->getConstructionKind(), 5348 Importer.Import(E->getParenOrBraceRange())); 5349 } 5350 5351 Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { 5352 QualType T = Importer.Import(E->getType()); 5353 if (T.isNull()) 5354 return nullptr; 5355 5356 Expr *ToFn = Importer.Import(E->getCallee()); 5357 if (!ToFn) 5358 return nullptr; 5359 5360 ArrayRef<Expr *> ToArgs; 5361 5362 if (!ImportArray(E->arg_begin(), E->arg_end(), ToArgs)) 5363 return nullptr; 5364 5365 return new (Importer.getToContext()) CXXMemberCallExpr(Importer.getToContext(), ToFn, 5366 ToArgs, T, E->getValueKind(), 5367 Importer.Import(E->getRParenLoc())); 5368 } 5369 5370 Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) { 5371 QualType T = Importer.Import(E->getType()); 5372 if (T.isNull()) 5373 return nullptr; 5374 5375 return new (Importer.getToContext()) 5376 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit()); 5377 } 5378 5379 Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { 5380 QualType T = Importer.Import(E->getType()); 5381 if (T.isNull()) 5382 return nullptr; 5383 5384 return new (Importer.getToContext()) 5385 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation())); 5386 } 5387 5388 5389 Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) { 5390 QualType T = Importer.Import(E->getType()); 5391 if (T.isNull()) 5392 return nullptr; 5393 5394 Expr *ToBase = Importer.Import(E->getBase()); 5395 if (!ToBase && E->getBase()) 5396 return nullptr; 5397 5398 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl())); 5399 if (!ToMember && E->getMemberDecl()) 5400 return nullptr; 5401 5402 DeclAccessPair ToFoundDecl = DeclAccessPair::make( 5403 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())), 5404 E->getFoundDecl().getAccess()); 5405 5406 DeclarationNameInfo ToMemberNameInfo( 5407 Importer.Import(E->getMemberNameInfo().getName()), 5408 Importer.Import(E->getMemberNameInfo().getLoc())); 5409 5410 if (E->hasExplicitTemplateArgs()) { 5411 return nullptr; // FIXME: handle template arguments 5412 } 5413 5414 return MemberExpr::Create(Importer.getToContext(), ToBase, 5415 E->isArrow(), 5416 Importer.Import(E->getOperatorLoc()), 5417 Importer.Import(E->getQualifierLoc()), 5418 Importer.Import(E->getTemplateKeywordLoc()), 5419 ToMember, ToFoundDecl, ToMemberNameInfo, 5420 nullptr, T, E->getValueKind(), 5421 E->getObjectKind()); 5422 } 5423 5424 Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) { 5425 QualType T = Importer.Import(E->getType()); 5426 if (T.isNull()) 5427 return nullptr; 5428 5429 Expr *ToCallee = Importer.Import(E->getCallee()); 5430 if (!ToCallee && E->getCallee()) 5431 return nullptr; 5432 5433 unsigned NumArgs = E->getNumArgs(); 5434 5435 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs); 5436 5437 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) { 5438 Expr *FromArg = E->getArg(ai); 5439 Expr *ToArg = Importer.Import(FromArg); 5440 if (!ToArg) 5441 return nullptr; 5442 ToArgs[ai] = ToArg; 5443 } 5444 5445 Expr **ToArgs_Copied = new (Importer.getToContext()) 5446 Expr*[NumArgs]; 5447 5448 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) 5449 ToArgs_Copied[ai] = ToArgs[ai]; 5450 5451 return new (Importer.getToContext()) 5452 CallExpr(Importer.getToContext(), ToCallee, 5453 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(), 5454 Importer.Import(E->getRParenLoc())); 5455 } 5456 5457 Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *E) { 5458 QualType T = Importer.Import(E->getType()); 5459 if (T.isNull()) 5460 return nullptr; 5461 5462 ArrayRef<Expr *> ToInits; 5463 5464 if (!ImportArray(E->inits().begin(), E->inits().end(), ToInits)) 5465 return nullptr; 5466 5467 InitListExpr *ToE = new (Importer.getToContext()) 5468 InitListExpr(Importer.getToContext(), 5469 Importer.Import(E->getLBraceLoc()), 5470 ToInits, 5471 Importer.Import(E->getRBraceLoc())); 5472 5473 if (ToE) 5474 ToE->setType(T); 5475 5476 return ToE; 5477 } 5478 5479 ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, 5480 ASTContext &FromContext, FileManager &FromFileManager, 5481 bool MinimalImport) 5482 : ToContext(ToContext), FromContext(FromContext), 5483 ToFileManager(ToFileManager), FromFileManager(FromFileManager), 5484 Minimal(MinimalImport), LastDiagFromFrom(false) 5485 { 5486 ImportedDecls[FromContext.getTranslationUnitDecl()] 5487 = ToContext.getTranslationUnitDecl(); 5488 } 5489 5490 ASTImporter::~ASTImporter() { } 5491 5492 QualType ASTImporter::Import(QualType FromT) { 5493 if (FromT.isNull()) 5494 return QualType(); 5495 5496 const Type *fromTy = FromT.getTypePtr(); 5497 5498 // Check whether we've already imported this type. 5499 llvm::DenseMap<const Type *, const Type *>::iterator Pos 5500 = ImportedTypes.find(fromTy); 5501 if (Pos != ImportedTypes.end()) 5502 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers()); 5503 5504 // Import the type 5505 ASTNodeImporter Importer(*this); 5506 QualType ToT = Importer.Visit(fromTy); 5507 if (ToT.isNull()) 5508 return ToT; 5509 5510 // Record the imported type. 5511 ImportedTypes[fromTy] = ToT.getTypePtr(); 5512 5513 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers()); 5514 } 5515 5516 TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { 5517 if (!FromTSI) 5518 return FromTSI; 5519 5520 // FIXME: For now we just create a "trivial" type source info based 5521 // on the type and a single location. Implement a real version of this. 5522 QualType T = Import(FromTSI->getType()); 5523 if (T.isNull()) 5524 return nullptr; 5525 5526 return ToContext.getTrivialTypeSourceInfo(T, 5527 Import(FromTSI->getTypeLoc().getLocStart())); 5528 } 5529 5530 Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) { 5531 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); 5532 if (Pos != ImportedDecls.end()) { 5533 Decl *ToD = Pos->second; 5534 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD); 5535 return ToD; 5536 } else { 5537 return nullptr; 5538 } 5539 } 5540 5541 Decl *ASTImporter::Import(Decl *FromD) { 5542 if (!FromD) 5543 return nullptr; 5544 5545 ASTNodeImporter Importer(*this); 5546 5547 // Check whether we've already imported this declaration. 5548 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); 5549 if (Pos != ImportedDecls.end()) { 5550 Decl *ToD = Pos->second; 5551 Importer.ImportDefinitionIfNeeded(FromD, ToD); 5552 return ToD; 5553 } 5554 5555 // Import the type 5556 Decl *ToD = Importer.Visit(FromD); 5557 if (!ToD) 5558 return nullptr; 5559 5560 // Record the imported declaration. 5561 ImportedDecls[FromD] = ToD; 5562 5563 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) { 5564 // Keep track of anonymous tags that have an associated typedef. 5565 if (FromTag->getTypedefNameForAnonDecl()) 5566 AnonTagsWithPendingTypedefs.push_back(FromTag); 5567 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) { 5568 // When we've finished transforming a typedef, see whether it was the 5569 // typedef for an anonymous tag. 5570 for (SmallVectorImpl<TagDecl *>::iterator 5571 FromTag = AnonTagsWithPendingTypedefs.begin(), 5572 FromTagEnd = AnonTagsWithPendingTypedefs.end(); 5573 FromTag != FromTagEnd; ++FromTag) { 5574 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) { 5575 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) { 5576 // We found the typedef for an anonymous tag; link them. 5577 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD)); 5578 AnonTagsWithPendingTypedefs.erase(FromTag); 5579 break; 5580 } 5581 } 5582 } 5583 } 5584 5585 return ToD; 5586 } 5587 5588 DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) { 5589 if (!FromDC) 5590 return FromDC; 5591 5592 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC))); 5593 if (!ToDC) 5594 return nullptr; 5595 5596 // When we're using a record/enum/Objective-C class/protocol as a context, we 5597 // need it to have a definition. 5598 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) { 5599 RecordDecl *FromRecord = cast<RecordDecl>(FromDC); 5600 if (ToRecord->isCompleteDefinition()) { 5601 // Do nothing. 5602 } else if (FromRecord->isCompleteDefinition()) { 5603 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord, 5604 ASTNodeImporter::IDK_Basic); 5605 } else { 5606 CompleteDecl(ToRecord); 5607 } 5608 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) { 5609 EnumDecl *FromEnum = cast<EnumDecl>(FromDC); 5610 if (ToEnum->isCompleteDefinition()) { 5611 // Do nothing. 5612 } else if (FromEnum->isCompleteDefinition()) { 5613 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum, 5614 ASTNodeImporter::IDK_Basic); 5615 } else { 5616 CompleteDecl(ToEnum); 5617 } 5618 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) { 5619 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC); 5620 if (ToClass->getDefinition()) { 5621 // Do nothing. 5622 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { 5623 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass, 5624 ASTNodeImporter::IDK_Basic); 5625 } else { 5626 CompleteDecl(ToClass); 5627 } 5628 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) { 5629 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC); 5630 if (ToProto->getDefinition()) { 5631 // Do nothing. 5632 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { 5633 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto, 5634 ASTNodeImporter::IDK_Basic); 5635 } else { 5636 CompleteDecl(ToProto); 5637 } 5638 } 5639 5640 return ToDC; 5641 } 5642 5643 Expr *ASTImporter::Import(Expr *FromE) { 5644 if (!FromE) 5645 return nullptr; 5646 5647 return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); 5648 } 5649 5650 Stmt *ASTImporter::Import(Stmt *FromS) { 5651 if (!FromS) 5652 return nullptr; 5653 5654 // Check whether we've already imported this declaration. 5655 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); 5656 if (Pos != ImportedStmts.end()) 5657 return Pos->second; 5658 5659 // Import the type 5660 ASTNodeImporter Importer(*this); 5661 Stmt *ToS = Importer.Visit(FromS); 5662 if (!ToS) 5663 return nullptr; 5664 5665 // Record the imported declaration. 5666 ImportedStmts[FromS] = ToS; 5667 return ToS; 5668 } 5669 5670 NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { 5671 if (!FromNNS) 5672 return nullptr; 5673 5674 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix()); 5675 5676 switch (FromNNS->getKind()) { 5677 case NestedNameSpecifier::Identifier: 5678 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) { 5679 return NestedNameSpecifier::Create(ToContext, prefix, II); 5680 } 5681 return nullptr; 5682 5683 case NestedNameSpecifier::Namespace: 5684 if (NamespaceDecl *NS = 5685 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) { 5686 return NestedNameSpecifier::Create(ToContext, prefix, NS); 5687 } 5688 return nullptr; 5689 5690 case NestedNameSpecifier::NamespaceAlias: 5691 if (NamespaceAliasDecl *NSAD = 5692 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) { 5693 return NestedNameSpecifier::Create(ToContext, prefix, NSAD); 5694 } 5695 return nullptr; 5696 5697 case NestedNameSpecifier::Global: 5698 return NestedNameSpecifier::GlobalSpecifier(ToContext); 5699 5700 case NestedNameSpecifier::Super: 5701 if (CXXRecordDecl *RD = 5702 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) { 5703 return NestedNameSpecifier::SuperSpecifier(ToContext, RD); 5704 } 5705 return nullptr; 5706 5707 case NestedNameSpecifier::TypeSpec: 5708 case NestedNameSpecifier::TypeSpecWithTemplate: { 5709 QualType T = Import(QualType(FromNNS->getAsType(), 0u)); 5710 if (!T.isNull()) { 5711 bool bTemplate = FromNNS->getKind() == 5712 NestedNameSpecifier::TypeSpecWithTemplate; 5713 return NestedNameSpecifier::Create(ToContext, prefix, 5714 bTemplate, T.getTypePtr()); 5715 } 5716 } 5717 return nullptr; 5718 } 5719 5720 llvm_unreachable("Invalid nested name specifier kind"); 5721 } 5722 5723 NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { 5724 // FIXME: Implement! 5725 return NestedNameSpecifierLoc(); 5726 } 5727 5728 TemplateName ASTImporter::Import(TemplateName From) { 5729 switch (From.getKind()) { 5730 case TemplateName::Template: 5731 if (TemplateDecl *ToTemplate 5732 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) 5733 return TemplateName(ToTemplate); 5734 5735 return TemplateName(); 5736 5737 case TemplateName::OverloadedTemplate: { 5738 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); 5739 UnresolvedSet<2> ToTemplates; 5740 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(), 5741 E = FromStorage->end(); 5742 I != E; ++I) { 5743 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I))) 5744 ToTemplates.addDecl(To); 5745 else 5746 return TemplateName(); 5747 } 5748 return ToContext.getOverloadedTemplateName(ToTemplates.begin(), 5749 ToTemplates.end()); 5750 } 5751 5752 case TemplateName::QualifiedTemplate: { 5753 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); 5754 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier()); 5755 if (!Qualifier) 5756 return TemplateName(); 5757 5758 if (TemplateDecl *ToTemplate 5759 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) 5760 return ToContext.getQualifiedTemplateName(Qualifier, 5761 QTN->hasTemplateKeyword(), 5762 ToTemplate); 5763 5764 return TemplateName(); 5765 } 5766 5767 case TemplateName::DependentTemplate: { 5768 DependentTemplateName *DTN = From.getAsDependentTemplateName(); 5769 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier()); 5770 if (!Qualifier) 5771 return TemplateName(); 5772 5773 if (DTN->isIdentifier()) { 5774 return ToContext.getDependentTemplateName(Qualifier, 5775 Import(DTN->getIdentifier())); 5776 } 5777 5778 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator()); 5779 } 5780 5781 case TemplateName::SubstTemplateTemplateParm: { 5782 SubstTemplateTemplateParmStorage *subst 5783 = From.getAsSubstTemplateTemplateParm(); 5784 TemplateTemplateParmDecl *param 5785 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter())); 5786 if (!param) 5787 return TemplateName(); 5788 5789 TemplateName replacement = Import(subst->getReplacement()); 5790 if (replacement.isNull()) return TemplateName(); 5791 5792 return ToContext.getSubstTemplateTemplateParm(param, replacement); 5793 } 5794 5795 case TemplateName::SubstTemplateTemplateParmPack: { 5796 SubstTemplateTemplateParmPackStorage *SubstPack 5797 = From.getAsSubstTemplateTemplateParmPack(); 5798 TemplateTemplateParmDecl *Param 5799 = cast_or_null<TemplateTemplateParmDecl>( 5800 Import(SubstPack->getParameterPack())); 5801 if (!Param) 5802 return TemplateName(); 5803 5804 ASTNodeImporter Importer(*this); 5805 TemplateArgument ArgPack 5806 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); 5807 if (ArgPack.isNull()) 5808 return TemplateName(); 5809 5810 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack); 5811 } 5812 } 5813 5814 llvm_unreachable("Invalid template name kind"); 5815 } 5816 5817 SourceLocation ASTImporter::Import(SourceLocation FromLoc) { 5818 if (FromLoc.isInvalid()) 5819 return SourceLocation(); 5820 5821 SourceManager &FromSM = FromContext.getSourceManager(); 5822 5823 // For now, map everything down to its spelling location, so that we 5824 // don't have to import macro expansions. 5825 // FIXME: Import macro expansions! 5826 FromLoc = FromSM.getSpellingLoc(FromLoc); 5827 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); 5828 SourceManager &ToSM = ToContext.getSourceManager(); 5829 FileID ToFileID = Import(Decomposed.first); 5830 if (ToFileID.isInvalid()) 5831 return SourceLocation(); 5832 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID) 5833 .getLocWithOffset(Decomposed.second); 5834 return ret; 5835 } 5836 5837 SourceRange ASTImporter::Import(SourceRange FromRange) { 5838 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); 5839 } 5840 5841 FileID ASTImporter::Import(FileID FromID) { 5842 llvm::DenseMap<FileID, FileID>::iterator Pos 5843 = ImportedFileIDs.find(FromID); 5844 if (Pos != ImportedFileIDs.end()) 5845 return Pos->second; 5846 5847 SourceManager &FromSM = FromContext.getSourceManager(); 5848 SourceManager &ToSM = ToContext.getSourceManager(); 5849 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); 5850 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet"); 5851 5852 // Include location of this file. 5853 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); 5854 5855 // Map the FileID for to the "to" source manager. 5856 FileID ToID; 5857 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); 5858 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { 5859 // FIXME: We probably want to use getVirtualFile(), so we don't hit the 5860 // disk again 5861 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather 5862 // than mmap the files several times. 5863 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName()); 5864 if (!Entry) 5865 return FileID(); 5866 ToID = ToSM.createFileID(Entry, ToIncludeLoc, 5867 FromSLoc.getFile().getFileCharacteristic()); 5868 } else { 5869 // FIXME: We want to re-use the existing MemoryBuffer! 5870 const llvm::MemoryBuffer * 5871 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM); 5872 std::unique_ptr<llvm::MemoryBuffer> ToBuf 5873 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(), 5874 FromBuf->getBufferIdentifier()); 5875 ToID = ToSM.createFileID(std::move(ToBuf), 5876 FromSLoc.getFile().getFileCharacteristic()); 5877 } 5878 5879 5880 ImportedFileIDs[FromID] = ToID; 5881 return ToID; 5882 } 5883 5884 void ASTImporter::ImportDefinition(Decl *From) { 5885 Decl *To = Import(From); 5886 if (!To) 5887 return; 5888 5889 if (DeclContext *FromDC = cast<DeclContext>(From)) { 5890 ASTNodeImporter Importer(*this); 5891 5892 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) { 5893 if (!ToRecord->getDefinition()) { 5894 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord, 5895 ASTNodeImporter::IDK_Everything); 5896 return; 5897 } 5898 } 5899 5900 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) { 5901 if (!ToEnum->getDefinition()) { 5902 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum, 5903 ASTNodeImporter::IDK_Everything); 5904 return; 5905 } 5906 } 5907 5908 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) { 5909 if (!ToIFace->getDefinition()) { 5910 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace, 5911 ASTNodeImporter::IDK_Everything); 5912 return; 5913 } 5914 } 5915 5916 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) { 5917 if (!ToProto->getDefinition()) { 5918 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto, 5919 ASTNodeImporter::IDK_Everything); 5920 return; 5921 } 5922 } 5923 5924 Importer.ImportDeclContext(FromDC, true); 5925 } 5926 } 5927 5928 DeclarationName ASTImporter::Import(DeclarationName FromName) { 5929 if (!FromName) 5930 return DeclarationName(); 5931 5932 switch (FromName.getNameKind()) { 5933 case DeclarationName::Identifier: 5934 return Import(FromName.getAsIdentifierInfo()); 5935 5936 case DeclarationName::ObjCZeroArgSelector: 5937 case DeclarationName::ObjCOneArgSelector: 5938 case DeclarationName::ObjCMultiArgSelector: 5939 return Import(FromName.getObjCSelector()); 5940 5941 case DeclarationName::CXXConstructorName: { 5942 QualType T = Import(FromName.getCXXNameType()); 5943 if (T.isNull()) 5944 return DeclarationName(); 5945 5946 return ToContext.DeclarationNames.getCXXConstructorName( 5947 ToContext.getCanonicalType(T)); 5948 } 5949 5950 case DeclarationName::CXXDestructorName: { 5951 QualType T = Import(FromName.getCXXNameType()); 5952 if (T.isNull()) 5953 return DeclarationName(); 5954 5955 return ToContext.DeclarationNames.getCXXDestructorName( 5956 ToContext.getCanonicalType(T)); 5957 } 5958 5959 case DeclarationName::CXXConversionFunctionName: { 5960 QualType T = Import(FromName.getCXXNameType()); 5961 if (T.isNull()) 5962 return DeclarationName(); 5963 5964 return ToContext.DeclarationNames.getCXXConversionFunctionName( 5965 ToContext.getCanonicalType(T)); 5966 } 5967 5968 case DeclarationName::CXXOperatorName: 5969 return ToContext.DeclarationNames.getCXXOperatorName( 5970 FromName.getCXXOverloadedOperator()); 5971 5972 case DeclarationName::CXXLiteralOperatorName: 5973 return ToContext.DeclarationNames.getCXXLiteralOperatorName( 5974 Import(FromName.getCXXLiteralIdentifier())); 5975 5976 case DeclarationName::CXXUsingDirective: 5977 // FIXME: STATICS! 5978 return DeclarationName::getUsingDirectiveName(); 5979 } 5980 5981 llvm_unreachable("Invalid DeclarationName Kind!"); 5982 } 5983 5984 IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { 5985 if (!FromId) 5986 return nullptr; 5987 5988 return &ToContext.Idents.get(FromId->getName()); 5989 } 5990 5991 Selector ASTImporter::Import(Selector FromSel) { 5992 if (FromSel.isNull()) 5993 return Selector(); 5994 5995 SmallVector<IdentifierInfo *, 4> Idents; 5996 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); 5997 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) 5998 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); 5999 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); 6000 } 6001 6002 DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name, 6003 DeclContext *DC, 6004 unsigned IDNS, 6005 NamedDecl **Decls, 6006 unsigned NumDecls) { 6007 return Name; 6008 } 6009 6010 DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { 6011 if (LastDiagFromFrom) 6012 ToContext.getDiagnostics().notePriorDiagnosticFrom( 6013 FromContext.getDiagnostics()); 6014 LastDiagFromFrom = false; 6015 return ToContext.getDiagnostics().Report(Loc, DiagID); 6016 } 6017 6018 DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { 6019 if (!LastDiagFromFrom) 6020 FromContext.getDiagnostics().notePriorDiagnosticFrom( 6021 ToContext.getDiagnostics()); 6022 LastDiagFromFrom = true; 6023 return FromContext.getDiagnostics().Report(Loc, DiagID); 6024 } 6025 6026 void ASTImporter::CompleteDecl (Decl *D) { 6027 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { 6028 if (!ID->getDefinition()) 6029 ID->startDefinition(); 6030 } 6031 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { 6032 if (!PD->getDefinition()) 6033 PD->startDefinition(); 6034 } 6035 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 6036 if (!TD->getDefinition() && !TD->isBeingDefined()) { 6037 TD->startDefinition(); 6038 TD->setCompleteDefinition(true); 6039 } 6040 } 6041 else { 6042 assert (0 && "CompleteDecl called on a Decl that can't be completed"); 6043 } 6044 } 6045 6046 Decl *ASTImporter::Imported(Decl *From, Decl *To) { 6047 if (From->hasAttrs()) { 6048 for (Attr *FromAttr : From->getAttrs()) 6049 To->addAttr(FromAttr->clone(To->getASTContext())); 6050 } 6051 if (From->isUsed()) { 6052 To->setIsUsed(); 6053 } 6054 ImportedDecls[From] = To; 6055 return To; 6056 } 6057 6058 bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, 6059 bool Complain) { 6060 llvm::DenseMap<const Type *, const Type *>::iterator Pos 6061 = ImportedTypes.find(From.getTypePtr()); 6062 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To)) 6063 return true; 6064 6065 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls, 6066 false, Complain); 6067 return Ctx.IsStructurallyEquivalent(From, To); 6068 } 6069