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