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