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