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