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