ASTImporter.cpp revision 008847a70ab122a99911149199855060fb3753b4
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/TypeLoc.h" 23#include "clang/AST/TypeVisitor.h" 24#include "clang/Basic/FileManager.h" 25#include "clang/Basic/SourceManager.h" 26#include "llvm/Support/MemoryBuffer.h" 27#include <deque> 28 29using namespace clang; 30 31namespace { 32 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>, 33 public DeclVisitor<ASTNodeImporter, Decl *>, 34 public StmtVisitor<ASTNodeImporter, Stmt *> { 35 ASTImporter &Importer; 36 37 public: 38 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { } 39 40 using TypeVisitor<ASTNodeImporter, QualType>::Visit; 41 using DeclVisitor<ASTNodeImporter, Decl *>::Visit; 42 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit; 43 44 // Importing types 45 QualType VisitType(Type *T); 46 QualType VisitBuiltinType(BuiltinType *T); 47 QualType VisitComplexType(ComplexType *T); 48 QualType VisitPointerType(PointerType *T); 49 QualType VisitBlockPointerType(BlockPointerType *T); 50 QualType VisitLValueReferenceType(LValueReferenceType *T); 51 QualType VisitRValueReferenceType(RValueReferenceType *T); 52 QualType VisitMemberPointerType(MemberPointerType *T); 53 QualType VisitConstantArrayType(ConstantArrayType *T); 54 QualType VisitIncompleteArrayType(IncompleteArrayType *T); 55 QualType VisitVariableArrayType(VariableArrayType *T); 56 // FIXME: DependentSizedArrayType 57 // FIXME: DependentSizedExtVectorType 58 QualType VisitVectorType(VectorType *T); 59 QualType VisitExtVectorType(ExtVectorType *T); 60 QualType VisitFunctionNoProtoType(FunctionNoProtoType *T); 61 QualType VisitFunctionProtoType(FunctionProtoType *T); 62 // FIXME: UnresolvedUsingType 63 QualType VisitTypedefType(TypedefType *T); 64 QualType VisitTypeOfExprType(TypeOfExprType *T); 65 // FIXME: DependentTypeOfExprType 66 QualType VisitTypeOfType(TypeOfType *T); 67 QualType VisitDecltypeType(DecltypeType *T); 68 // FIXME: DependentDecltypeType 69 QualType VisitRecordType(RecordType *T); 70 QualType VisitEnumType(EnumType *T); 71 QualType VisitElaboratedType(ElaboratedType *T); 72 // FIXME: TemplateTypeParmType 73 // FIXME: SubstTemplateTypeParmType 74 // FIXME: TemplateSpecializationType 75 QualType VisitQualifiedNameType(QualifiedNameType *T); 76 // FIXME: TypenameType 77 QualType VisitObjCInterfaceType(ObjCInterfaceType *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 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord); 85 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord); 86 Decl *VisitDecl(Decl *D); 87 Decl *VisitTypedefDecl(TypedefDecl *D); 88 Decl *VisitEnumDecl(EnumDecl *D); 89 Decl *VisitRecordDecl(RecordDecl *D); 90 Decl *VisitEnumConstantDecl(EnumConstantDecl *D); 91 Decl *VisitFunctionDecl(FunctionDecl *D); 92 Decl *VisitFieldDecl(FieldDecl *D); 93 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D); 94 Decl *VisitVarDecl(VarDecl *D); 95 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D); 96 Decl *VisitParmVarDecl(ParmVarDecl *D); 97 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D); 98 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D); 99 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D); 100 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); 101 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D); 102 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); 103 Decl *VisitObjCClassDecl(ObjCClassDecl *D); 104 105 // Importing statements 106 Stmt *VisitStmt(Stmt *S); 107 108 // Importing expressions 109 Expr *VisitExpr(Expr *E); 110 Expr *VisitDeclRefExpr(DeclRefExpr *E); 111 Expr *VisitIntegerLiteral(IntegerLiteral *E); 112 Expr *VisitCharacterLiteral(CharacterLiteral *E); 113 Expr *VisitParenExpr(ParenExpr *E); 114 Expr *VisitUnaryOperator(UnaryOperator *E); 115 Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E); 116 Expr *VisitBinaryOperator(BinaryOperator *E); 117 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E); 118 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E); 119 Expr *VisitCStyleCastExpr(CStyleCastExpr *E); 120 }; 121} 122 123//---------------------------------------------------------------------------- 124// Structural Equivalence 125//---------------------------------------------------------------------------- 126 127namespace { 128 struct StructuralEquivalenceContext { 129 /// \brief AST contexts for which we are checking structural equivalence. 130 ASTContext &C1, &C2; 131 132 /// \brief Diagnostic object used to emit diagnostics. 133 Diagnostic &Diags; 134 135 /// \brief The set of "tentative" equivalences between two canonical 136 /// declarations, mapping from a declaration in the first context to the 137 /// declaration in the second context that we believe to be equivalent. 138 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences; 139 140 /// \brief Queue of declarations in the first context whose equivalence 141 /// with a declaration in the second context still needs to be verified. 142 std::deque<Decl *> DeclsToCheck; 143 144 /// \brief Declaration (from, to) pairs that are known not to be equivalent 145 /// (which we have already complained about). 146 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls; 147 148 /// \brief Whether we're being strict about the spelling of types when 149 /// unifying two types. 150 bool StrictTypeSpelling; 151 152 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2, 153 Diagnostic &Diags, 154 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls, 155 bool StrictTypeSpelling = false) 156 : C1(C1), C2(C2), Diags(Diags), NonEquivalentDecls(NonEquivalentDecls), 157 StrictTypeSpelling(StrictTypeSpelling) { } 158 159 /// \brief Determine whether the two declarations are structurally 160 /// equivalent. 161 bool IsStructurallyEquivalent(Decl *D1, Decl *D2); 162 163 /// \brief Determine whether the two types are structurally equivalent. 164 bool IsStructurallyEquivalent(QualType T1, QualType T2); 165 166 private: 167 /// \brief Finish checking all of the structural equivalences. 168 /// 169 /// \returns true if an error occurred, false otherwise. 170 bool Finish(); 171 172 public: 173 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) { 174 return Diags.Report(FullSourceLoc(Loc, C1.getSourceManager()), DiagID); 175 } 176 177 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) { 178 return Diags.Report(FullSourceLoc(Loc, C2.getSourceManager()), DiagID); 179 } 180 }; 181} 182 183static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 184 QualType T1, QualType T2); 185static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 186 Decl *D1, Decl *D2); 187 188/// \brief Determine if two APInts have the same value, after zero-extending 189/// one of them (if needed!) to ensure that the bit-widths match. 190static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) { 191 if (I1.getBitWidth() == I2.getBitWidth()) 192 return I1 == I2; 193 194 if (I1.getBitWidth() > I2.getBitWidth()) 195 return I1 == llvm::APInt(I2).zext(I1.getBitWidth()); 196 197 return llvm::APInt(I1).zext(I2.getBitWidth()) == I2; 198} 199 200/// \brief Determine if two APSInts have the same value, zero- or sign-extending 201/// as needed. 202static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) { 203 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned()) 204 return I1 == I2; 205 206 // Check for a bit-width mismatch. 207 if (I1.getBitWidth() > I2.getBitWidth()) 208 return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth())); 209 else if (I2.getBitWidth() > I1.getBitWidth()) 210 return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2); 211 212 // We have a signedness mismatch. Turn the signed value into an unsigned 213 // value. 214 if (I1.isSigned()) { 215 if (I1.isNegative()) 216 return false; 217 218 return llvm::APSInt(I1, true) == I2; 219 } 220 221 if (I2.isNegative()) 222 return false; 223 224 return I1 == llvm::APSInt(I2, true); 225} 226 227/// \brief Determine structural equivalence of two expressions. 228static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 229 Expr *E1, Expr *E2) { 230 if (!E1 || !E2) 231 return E1 == E2; 232 233 // FIXME: Actually perform a structural comparison! 234 return true; 235} 236 237/// \brief Determine whether two identifiers are equivalent. 238static bool IsStructurallyEquivalent(const IdentifierInfo *Name1, 239 const IdentifierInfo *Name2) { 240 if (!Name1 || !Name2) 241 return Name1 == Name2; 242 243 return Name1->getName() == Name2->getName(); 244} 245 246/// \brief Determine whether two nested-name-specifiers are equivalent. 247static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 248 NestedNameSpecifier *NNS1, 249 NestedNameSpecifier *NNS2) { 250 // FIXME: Implement! 251 return true; 252} 253 254/// \brief Determine whether two template arguments are equivalent. 255static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 256 const TemplateArgument &Arg1, 257 const TemplateArgument &Arg2) { 258 // FIXME: Implement! 259 return true; 260} 261 262/// \brief Determine structural equivalence for the common part of array 263/// types. 264static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context, 265 const ArrayType *Array1, 266 const ArrayType *Array2) { 267 if (!IsStructurallyEquivalent(Context, 268 Array1->getElementType(), 269 Array2->getElementType())) 270 return false; 271 if (Array1->getSizeModifier() != Array2->getSizeModifier()) 272 return false; 273 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers()) 274 return false; 275 276 return true; 277} 278 279/// \brief Determine structural equivalence of two types. 280static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 281 QualType T1, QualType T2) { 282 if (T1.isNull() || T2.isNull()) 283 return T1.isNull() && T2.isNull(); 284 285 if (!Context.StrictTypeSpelling) { 286 // We aren't being strict about token-to-token equivalence of types, 287 // so map down to the canonical type. 288 T1 = Context.C1.getCanonicalType(T1); 289 T2 = Context.C2.getCanonicalType(T2); 290 } 291 292 if (T1.getQualifiers() != T2.getQualifiers()) 293 return false; 294 295 Type::TypeClass TC = T1->getTypeClass(); 296 297 if (T1->getTypeClass() != T2->getTypeClass()) { 298 // Compare function types with prototypes vs. without prototypes as if 299 // both did not have prototypes. 300 if (T1->getTypeClass() == Type::FunctionProto && 301 T2->getTypeClass() == Type::FunctionNoProto) 302 TC = Type::FunctionNoProto; 303 else if (T1->getTypeClass() == Type::FunctionNoProto && 304 T2->getTypeClass() == Type::FunctionProto) 305 TC = Type::FunctionNoProto; 306 else 307 return false; 308 } 309 310 switch (TC) { 311 case Type::Builtin: 312 // FIXME: Deal with Char_S/Char_U. 313 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind()) 314 return false; 315 break; 316 317 case Type::Complex: 318 if (!IsStructurallyEquivalent(Context, 319 cast<ComplexType>(T1)->getElementType(), 320 cast<ComplexType>(T2)->getElementType())) 321 return false; 322 break; 323 324 case Type::Pointer: 325 if (!IsStructurallyEquivalent(Context, 326 cast<PointerType>(T1)->getPointeeType(), 327 cast<PointerType>(T2)->getPointeeType())) 328 return false; 329 break; 330 331 case Type::BlockPointer: 332 if (!IsStructurallyEquivalent(Context, 333 cast<BlockPointerType>(T1)->getPointeeType(), 334 cast<BlockPointerType>(T2)->getPointeeType())) 335 return false; 336 break; 337 338 case Type::LValueReference: 339 case Type::RValueReference: { 340 const ReferenceType *Ref1 = cast<ReferenceType>(T1); 341 const ReferenceType *Ref2 = cast<ReferenceType>(T2); 342 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue()) 343 return false; 344 if (Ref1->isInnerRef() != Ref2->isInnerRef()) 345 return false; 346 if (!IsStructurallyEquivalent(Context, 347 Ref1->getPointeeTypeAsWritten(), 348 Ref2->getPointeeTypeAsWritten())) 349 return false; 350 break; 351 } 352 353 case Type::MemberPointer: { 354 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1); 355 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2); 356 if (!IsStructurallyEquivalent(Context, 357 MemPtr1->getPointeeType(), 358 MemPtr2->getPointeeType())) 359 return false; 360 if (!IsStructurallyEquivalent(Context, 361 QualType(MemPtr1->getClass(), 0), 362 QualType(MemPtr2->getClass(), 0))) 363 return false; 364 break; 365 } 366 367 case Type::ConstantArray: { 368 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1); 369 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2); 370 if (!IsSameValue(Array1->getSize(), Array2->getSize())) 371 return false; 372 373 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) 374 return false; 375 break; 376 } 377 378 case Type::IncompleteArray: 379 if (!IsArrayStructurallyEquivalent(Context, 380 cast<ArrayType>(T1), 381 cast<ArrayType>(T2))) 382 return false; 383 break; 384 385 case Type::VariableArray: { 386 const VariableArrayType *Array1 = cast<VariableArrayType>(T1); 387 const VariableArrayType *Array2 = cast<VariableArrayType>(T2); 388 if (!IsStructurallyEquivalent(Context, 389 Array1->getSizeExpr(), Array2->getSizeExpr())) 390 return false; 391 392 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) 393 return false; 394 395 break; 396 } 397 398 case Type::DependentSizedArray: { 399 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1); 400 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2); 401 if (!IsStructurallyEquivalent(Context, 402 Array1->getSizeExpr(), Array2->getSizeExpr())) 403 return false; 404 405 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) 406 return false; 407 408 break; 409 } 410 411 case Type::DependentSizedExtVector: { 412 const DependentSizedExtVectorType *Vec1 413 = cast<DependentSizedExtVectorType>(T1); 414 const DependentSizedExtVectorType *Vec2 415 = cast<DependentSizedExtVectorType>(T2); 416 if (!IsStructurallyEquivalent(Context, 417 Vec1->getSizeExpr(), Vec2->getSizeExpr())) 418 return false; 419 if (!IsStructurallyEquivalent(Context, 420 Vec1->getElementType(), 421 Vec2->getElementType())) 422 return false; 423 break; 424 } 425 426 case Type::Vector: 427 case Type::ExtVector: { 428 const VectorType *Vec1 = cast<VectorType>(T1); 429 const VectorType *Vec2 = cast<VectorType>(T2); 430 if (!IsStructurallyEquivalent(Context, 431 Vec1->getElementType(), 432 Vec2->getElementType())) 433 return false; 434 if (Vec1->getNumElements() != Vec2->getNumElements()) 435 return false; 436 if (Vec1->isAltiVec() != Vec2->isAltiVec()) 437 return false; 438 if (Vec1->isPixel() != Vec2->isPixel()) 439 return false; 440 } 441 442 case Type::FunctionProto: { 443 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1); 444 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2); 445 if (Proto1->getNumArgs() != Proto2->getNumArgs()) 446 return false; 447 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) { 448 if (!IsStructurallyEquivalent(Context, 449 Proto1->getArgType(I), 450 Proto2->getArgType(I))) 451 return false; 452 } 453 if (Proto1->isVariadic() != Proto2->isVariadic()) 454 return false; 455 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec()) 456 return false; 457 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec()) 458 return false; 459 if (Proto1->getNumExceptions() != Proto2->getNumExceptions()) 460 return false; 461 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) { 462 if (!IsStructurallyEquivalent(Context, 463 Proto1->getExceptionType(I), 464 Proto2->getExceptionType(I))) 465 return false; 466 } 467 if (Proto1->getTypeQuals() != Proto2->getTypeQuals()) 468 return false; 469 470 // Fall through to check the bits common with FunctionNoProtoType. 471 } 472 473 case Type::FunctionNoProto: { 474 const FunctionType *Function1 = cast<FunctionType>(T1); 475 const FunctionType *Function2 = cast<FunctionType>(T2); 476 if (!IsStructurallyEquivalent(Context, 477 Function1->getResultType(), 478 Function2->getResultType())) 479 return false; 480 if (Function1->getNoReturnAttr() != Function2->getNoReturnAttr()) 481 return false; 482 if (Function1->getCallConv() != Function2->getCallConv()) 483 return false; 484 break; 485 } 486 487 case Type::UnresolvedUsing: 488 if (!IsStructurallyEquivalent(Context, 489 cast<UnresolvedUsingType>(T1)->getDecl(), 490 cast<UnresolvedUsingType>(T2)->getDecl())) 491 return false; 492 493 break; 494 495 case Type::Typedef: 496 if (!IsStructurallyEquivalent(Context, 497 cast<TypedefType>(T1)->getDecl(), 498 cast<TypedefType>(T2)->getDecl())) 499 return false; 500 break; 501 502 case Type::TypeOfExpr: 503 if (!IsStructurallyEquivalent(Context, 504 cast<TypeOfExprType>(T1)->getUnderlyingExpr(), 505 cast<TypeOfExprType>(T2)->getUnderlyingExpr())) 506 return false; 507 break; 508 509 case Type::TypeOf: 510 if (!IsStructurallyEquivalent(Context, 511 cast<TypeOfType>(T1)->getUnderlyingType(), 512 cast<TypeOfType>(T2)->getUnderlyingType())) 513 return false; 514 break; 515 516 case Type::Decltype: 517 if (!IsStructurallyEquivalent(Context, 518 cast<DecltypeType>(T1)->getUnderlyingExpr(), 519 cast<DecltypeType>(T2)->getUnderlyingExpr())) 520 return false; 521 break; 522 523 case Type::Record: 524 case Type::Enum: 525 if (!IsStructurallyEquivalent(Context, 526 cast<TagType>(T1)->getDecl(), 527 cast<TagType>(T2)->getDecl())) 528 return false; 529 break; 530 531 case Type::Elaborated: { 532 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1); 533 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2); 534 if (Elab1->getTagKind() != Elab2->getTagKind()) 535 return false; 536 if (!IsStructurallyEquivalent(Context, 537 Elab1->getUnderlyingType(), 538 Elab2->getUnderlyingType())) 539 return false; 540 break; 541 } 542 543 case Type::TemplateTypeParm: { 544 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1); 545 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2); 546 if (Parm1->getDepth() != Parm2->getDepth()) 547 return false; 548 if (Parm1->getIndex() != Parm2->getIndex()) 549 return false; 550 if (Parm1->isParameterPack() != Parm2->isParameterPack()) 551 return false; 552 553 // Names of template type parameters are never significant. 554 break; 555 } 556 557 case Type::SubstTemplateTypeParm: { 558 const SubstTemplateTypeParmType *Subst1 559 = cast<SubstTemplateTypeParmType>(T1); 560 const SubstTemplateTypeParmType *Subst2 561 = cast<SubstTemplateTypeParmType>(T2); 562 if (!IsStructurallyEquivalent(Context, 563 QualType(Subst1->getReplacedParameter(), 0), 564 QualType(Subst2->getReplacedParameter(), 0))) 565 return false; 566 if (!IsStructurallyEquivalent(Context, 567 Subst1->getReplacementType(), 568 Subst2->getReplacementType())) 569 return false; 570 break; 571 } 572 573 case Type::TemplateSpecialization: { 574 const TemplateSpecializationType *Spec1 575 = cast<TemplateSpecializationType>(T1); 576 const TemplateSpecializationType *Spec2 577 = cast<TemplateSpecializationType>(T2); 578 if (!IsStructurallyEquivalent(Context, 579 Spec1->getTemplateName(), 580 Spec2->getTemplateName())) 581 return false; 582 if (Spec1->getNumArgs() != Spec2->getNumArgs()) 583 return false; 584 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { 585 if (!IsStructurallyEquivalent(Context, 586 Spec1->getArg(I), Spec2->getArg(I))) 587 return false; 588 } 589 break; 590 } 591 592 case Type::QualifiedName: { 593 const QualifiedNameType *Qual1 = cast<QualifiedNameType>(T1); 594 const QualifiedNameType *Qual2 = cast<QualifiedNameType>(T2); 595 if (!IsStructurallyEquivalent(Context, 596 Qual1->getQualifier(), 597 Qual2->getQualifier())) 598 return false; 599 if (!IsStructurallyEquivalent(Context, 600 Qual1->getNamedType(), 601 Qual2->getNamedType())) 602 return false; 603 break; 604 } 605 606 case Type::Typename: { 607 const TypenameType *Typename1 = cast<TypenameType>(T1); 608 const TypenameType *Typename2 = cast<TypenameType>(T2); 609 if (!IsStructurallyEquivalent(Context, 610 Typename1->getQualifier(), 611 Typename2->getQualifier())) 612 return false; 613 if (!IsStructurallyEquivalent(Typename1->getIdentifier(), 614 Typename2->getIdentifier())) 615 return false; 616 if (!IsStructurallyEquivalent(Context, 617 QualType(Typename1->getTemplateId(), 0), 618 QualType(Typename2->getTemplateId(), 0))) 619 return false; 620 621 break; 622 } 623 624 case Type::ObjCInterface: { 625 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1); 626 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2); 627 if (!IsStructurallyEquivalent(Context, 628 Iface1->getDecl(), Iface2->getDecl())) 629 return false; 630 if (Iface1->getNumProtocols() != Iface2->getNumProtocols()) 631 return false; 632 for (unsigned I = 0, N = Iface1->getNumProtocols(); I != N; ++I) { 633 if (!IsStructurallyEquivalent(Context, 634 Iface1->getProtocol(I), 635 Iface2->getProtocol(I))) 636 return false; 637 } 638 break; 639 } 640 641 case Type::ObjCObjectPointer: { 642 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1); 643 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2); 644 if (!IsStructurallyEquivalent(Context, 645 Ptr1->getPointeeType(), 646 Ptr2->getPointeeType())) 647 return false; 648 if (Ptr1->getNumProtocols() != Ptr2->getNumProtocols()) 649 return false; 650 for (unsigned I = 0, N = Ptr1->getNumProtocols(); I != N; ++I) { 651 if (!IsStructurallyEquivalent(Context, 652 Ptr1->getProtocol(I), 653 Ptr2->getProtocol(I))) 654 return false; 655 } 656 break; 657 } 658 659 } // end switch 660 661 return true; 662} 663 664/// \brief Determine structural equivalence of two records. 665static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 666 RecordDecl *D1, RecordDecl *D2) { 667 if (D1->isUnion() != D2->isUnion()) { 668 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 669 << Context.C2.getTypeDeclType(D2); 670 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here) 671 << D1->getDeclName() << (unsigned)D1->getTagKind(); 672 return false; 673 } 674 675 // Compare the definitions of these two records. If either or both are 676 // incomplete, we assume that they are equivalent. 677 D1 = D1->getDefinition(); 678 D2 = D2->getDefinition(); 679 if (!D1 || !D2) 680 return true; 681 682 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) { 683 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) { 684 if (D1CXX->getNumBases() != D2CXX->getNumBases()) { 685 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 686 << Context.C2.getTypeDeclType(D2); 687 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases) 688 << D2CXX->getNumBases(); 689 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases) 690 << D1CXX->getNumBases(); 691 return false; 692 } 693 694 // Check the base classes. 695 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(), 696 BaseEnd1 = D1CXX->bases_end(), 697 Base2 = D2CXX->bases_begin(); 698 Base1 != BaseEnd1; 699 ++Base1, ++Base2) { 700 if (!IsStructurallyEquivalent(Context, 701 Base1->getType(), Base2->getType())) { 702 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 703 << Context.C2.getTypeDeclType(D2); 704 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base) 705 << Base2->getType() 706 << Base2->getSourceRange(); 707 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base) 708 << Base1->getType() 709 << Base1->getSourceRange(); 710 return false; 711 } 712 713 // Check virtual vs. non-virtual inheritance mismatch. 714 if (Base1->isVirtual() != Base2->isVirtual()) { 715 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 716 << Context.C2.getTypeDeclType(D2); 717 Context.Diag2(Base2->getSourceRange().getBegin(), 718 diag::note_odr_virtual_base) 719 << Base2->isVirtual() << Base2->getSourceRange(); 720 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base) 721 << Base1->isVirtual() 722 << Base1->getSourceRange(); 723 return false; 724 } 725 } 726 } else if (D1CXX->getNumBases() > 0) { 727 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 728 << Context.C2.getTypeDeclType(D2); 729 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin(); 730 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base) 731 << Base1->getType() 732 << Base1->getSourceRange(); 733 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base); 734 return false; 735 } 736 } 737 738 // Check the fields for consistency. 739 CXXRecordDecl::field_iterator Field2 = D2->field_begin(), 740 Field2End = D2->field_end(); 741 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(), 742 Field1End = D1->field_end(); 743 Field1 != Field1End; 744 ++Field1, ++Field2) { 745 if (Field2 == Field2End) { 746 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 747 << Context.C2.getTypeDeclType(D2); 748 Context.Diag1(Field1->getLocation(), diag::note_odr_field) 749 << Field1->getDeclName() << Field1->getType(); 750 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field); 751 return false; 752 } 753 754 if (!IsStructurallyEquivalent(Context, 755 Field1->getType(), Field2->getType())) { 756 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 757 << Context.C2.getTypeDeclType(D2); 758 Context.Diag2(Field2->getLocation(), diag::note_odr_field) 759 << Field2->getDeclName() << Field2->getType(); 760 Context.Diag1(Field1->getLocation(), diag::note_odr_field) 761 << Field1->getDeclName() << Field1->getType(); 762 return false; 763 } 764 765 if (Field1->isBitField() != Field2->isBitField()) { 766 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 767 << Context.C2.getTypeDeclType(D2); 768 if (Field1->isBitField()) { 769 llvm::APSInt Bits; 770 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1); 771 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) 772 << Field1->getDeclName() << Field1->getType() 773 << Bits.toString(10, false); 774 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field) 775 << Field2->getDeclName(); 776 } else { 777 llvm::APSInt Bits; 778 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2); 779 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) 780 << Field2->getDeclName() << Field2->getType() 781 << Bits.toString(10, false); 782 Context.Diag1(Field1->getLocation(), 783 diag::note_odr_not_bit_field) 784 << Field1->getDeclName(); 785 } 786 return false; 787 } 788 789 if (Field1->isBitField()) { 790 // Make sure that the bit-fields are the same length. 791 llvm::APSInt Bits1, Bits2; 792 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1)) 793 return false; 794 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2)) 795 return false; 796 797 if (!IsSameValue(Bits1, Bits2)) { 798 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 799 << Context.C2.getTypeDeclType(D2); 800 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) 801 << Field2->getDeclName() << Field2->getType() 802 << Bits2.toString(10, false); 803 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) 804 << Field1->getDeclName() << Field1->getType() 805 << Bits1.toString(10, false); 806 return false; 807 } 808 } 809 } 810 811 if (Field2 != Field2End) { 812 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 813 << Context.C2.getTypeDeclType(D2); 814 Context.Diag2(Field2->getLocation(), diag::note_odr_field) 815 << Field2->getDeclName() << Field2->getType(); 816 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field); 817 return false; 818 } 819 820 return true; 821} 822 823/// \brief Determine structural equivalence of two enums. 824static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 825 EnumDecl *D1, EnumDecl *D2) { 826 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(), 827 EC2End = D2->enumerator_end(); 828 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(), 829 EC1End = D1->enumerator_end(); 830 EC1 != EC1End; ++EC1, ++EC2) { 831 if (EC2 == EC2End) { 832 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 833 << Context.C2.getTypeDeclType(D2); 834 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) 835 << EC1->getDeclName() 836 << EC1->getInitVal().toString(10); 837 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator); 838 return false; 839 } 840 841 llvm::APSInt Val1 = EC1->getInitVal(); 842 llvm::APSInt Val2 = EC2->getInitVal(); 843 if (!IsSameValue(Val1, Val2) || 844 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) { 845 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 846 << Context.C2.getTypeDeclType(D2); 847 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) 848 << EC2->getDeclName() 849 << EC2->getInitVal().toString(10); 850 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) 851 << EC1->getDeclName() 852 << EC1->getInitVal().toString(10); 853 return false; 854 } 855 } 856 857 if (EC2 != EC2End) { 858 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 859 << Context.C2.getTypeDeclType(D2); 860 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) 861 << EC2->getDeclName() 862 << EC2->getInitVal().toString(10); 863 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator); 864 return false; 865 } 866 867 return true; 868} 869 870/// \brief Determine structural equivalence of two declarations. 871static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 872 Decl *D1, Decl *D2) { 873 // FIXME: Check for known structural equivalences via a callback of some sort. 874 875 // Check whether we already know that these two declarations are not 876 // structurally equivalent. 877 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(), 878 D2->getCanonicalDecl()))) 879 return false; 880 881 // Determine whether we've already produced a tentative equivalence for D1. 882 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()]; 883 if (EquivToD1) 884 return EquivToD1 == D2->getCanonicalDecl(); 885 886 // Produce a tentative equivalence D1 <-> D2, which will be checked later. 887 EquivToD1 = D2->getCanonicalDecl(); 888 Context.DeclsToCheck.push_back(D1->getCanonicalDecl()); 889 return true; 890} 891 892bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1, 893 Decl *D2) { 894 if (!::IsStructurallyEquivalent(*this, D1, D2)) 895 return false; 896 897 return !Finish(); 898} 899 900bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1, 901 QualType T2) { 902 if (!::IsStructurallyEquivalent(*this, T1, T2)) 903 return false; 904 905 return !Finish(); 906} 907 908bool StructuralEquivalenceContext::Finish() { 909 while (!DeclsToCheck.empty()) { 910 // Check the next declaration. 911 Decl *D1 = DeclsToCheck.front(); 912 DeclsToCheck.pop_front(); 913 914 Decl *D2 = TentativeEquivalences[D1]; 915 assert(D2 && "Unrecorded tentative equivalence?"); 916 917 bool Equivalent = true; 918 919 // FIXME: Switch on all declaration kinds. For now, we're just going to 920 // check the obvious ones. 921 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) { 922 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) { 923 // Check for equivalent structure names. 924 IdentifierInfo *Name1 = Record1->getIdentifier(); 925 if (!Name1 && Record1->getTypedefForAnonDecl()) 926 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier(); 927 IdentifierInfo *Name2 = Record2->getIdentifier(); 928 if (!Name2 && Record2->getTypedefForAnonDecl()) 929 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier(); 930 if (!::IsStructurallyEquivalent(Name1, Name2) || 931 !::IsStructurallyEquivalent(*this, Record1, Record2)) 932 Equivalent = false; 933 } else { 934 // Record/non-record mismatch. 935 Equivalent = false; 936 } 937 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) { 938 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) { 939 // Check for equivalent enum names. 940 IdentifierInfo *Name1 = Enum1->getIdentifier(); 941 if (!Name1 && Enum1->getTypedefForAnonDecl()) 942 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier(); 943 IdentifierInfo *Name2 = Enum2->getIdentifier(); 944 if (!Name2 && Enum2->getTypedefForAnonDecl()) 945 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier(); 946 if (!::IsStructurallyEquivalent(Name1, Name2) || 947 !::IsStructurallyEquivalent(*this, Enum1, Enum2)) 948 Equivalent = false; 949 } else { 950 // Enum/non-enum mismatch 951 Equivalent = false; 952 } 953 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) { 954 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) { 955 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(), 956 Typedef2->getIdentifier()) || 957 !::IsStructurallyEquivalent(*this, 958 Typedef1->getUnderlyingType(), 959 Typedef2->getUnderlyingType())) 960 Equivalent = false; 961 } else { 962 // Typedef/non-typedef mismatch. 963 Equivalent = false; 964 } 965 } 966 967 if (!Equivalent) { 968 // Note that these two declarations are not equivalent (and we already 969 // know about it). 970 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(), 971 D2->getCanonicalDecl())); 972 return true; 973 } 974 // FIXME: Check other declaration kinds! 975 } 976 977 return false; 978} 979 980//---------------------------------------------------------------------------- 981// Import Types 982//---------------------------------------------------------------------------- 983 984QualType ASTNodeImporter::VisitType(Type *T) { 985 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) 986 << T->getTypeClassName(); 987 return QualType(); 988} 989 990QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) { 991 switch (T->getKind()) { 992 case BuiltinType::Void: return Importer.getToContext().VoidTy; 993 case BuiltinType::Bool: return Importer.getToContext().BoolTy; 994 995 case BuiltinType::Char_U: 996 // The context we're importing from has an unsigned 'char'. If we're 997 // importing into a context with a signed 'char', translate to 998 // 'unsigned char' instead. 999 if (Importer.getToContext().getLangOptions().CharIsSigned) 1000 return Importer.getToContext().UnsignedCharTy; 1001 1002 return Importer.getToContext().CharTy; 1003 1004 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy; 1005 1006 case BuiltinType::Char16: 1007 // FIXME: Make sure that the "to" context supports C++! 1008 return Importer.getToContext().Char16Ty; 1009 1010 case BuiltinType::Char32: 1011 // FIXME: Make sure that the "to" context supports C++! 1012 return Importer.getToContext().Char32Ty; 1013 1014 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy; 1015 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy; 1016 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy; 1017 case BuiltinType::ULongLong: 1018 return Importer.getToContext().UnsignedLongLongTy; 1019 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty; 1020 1021 case BuiltinType::Char_S: 1022 // The context we're importing from has an unsigned 'char'. If we're 1023 // importing into a context with a signed 'char', translate to 1024 // 'unsigned char' instead. 1025 if (!Importer.getToContext().getLangOptions().CharIsSigned) 1026 return Importer.getToContext().SignedCharTy; 1027 1028 return Importer.getToContext().CharTy; 1029 1030 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy; 1031 case BuiltinType::WChar: 1032 // FIXME: If not in C++, shall we translate to the C equivalent of 1033 // wchar_t? 1034 return Importer.getToContext().WCharTy; 1035 1036 case BuiltinType::Short : return Importer.getToContext().ShortTy; 1037 case BuiltinType::Int : return Importer.getToContext().IntTy; 1038 case BuiltinType::Long : return Importer.getToContext().LongTy; 1039 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy; 1040 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty; 1041 case BuiltinType::Float: return Importer.getToContext().FloatTy; 1042 case BuiltinType::Double: return Importer.getToContext().DoubleTy; 1043 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy; 1044 1045 case BuiltinType::NullPtr: 1046 // FIXME: Make sure that the "to" context supports C++0x! 1047 return Importer.getToContext().NullPtrTy; 1048 1049 case BuiltinType::Overload: return Importer.getToContext().OverloadTy; 1050 case BuiltinType::Dependent: return Importer.getToContext().DependentTy; 1051 case BuiltinType::UndeducedAuto: 1052 // FIXME: Make sure that the "to" context supports C++0x! 1053 return Importer.getToContext().UndeducedAutoTy; 1054 1055 case BuiltinType::ObjCId: 1056 // FIXME: Make sure that the "to" context supports Objective-C! 1057 return Importer.getToContext().ObjCBuiltinIdTy; 1058 1059 case BuiltinType::ObjCClass: 1060 return Importer.getToContext().ObjCBuiltinClassTy; 1061 1062 case BuiltinType::ObjCSel: 1063 return Importer.getToContext().ObjCBuiltinSelTy; 1064 } 1065 1066 return QualType(); 1067} 1068 1069QualType ASTNodeImporter::VisitComplexType(ComplexType *T) { 1070 QualType ToElementType = Importer.Import(T->getElementType()); 1071 if (ToElementType.isNull()) 1072 return QualType(); 1073 1074 return Importer.getToContext().getComplexType(ToElementType); 1075} 1076 1077QualType ASTNodeImporter::VisitPointerType(PointerType *T) { 1078 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1079 if (ToPointeeType.isNull()) 1080 return QualType(); 1081 1082 return Importer.getToContext().getPointerType(ToPointeeType); 1083} 1084 1085QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) { 1086 // FIXME: Check for blocks support in "to" context. 1087 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1088 if (ToPointeeType.isNull()) 1089 return QualType(); 1090 1091 return Importer.getToContext().getBlockPointerType(ToPointeeType); 1092} 1093 1094QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) { 1095 // FIXME: Check for C++ support in "to" context. 1096 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); 1097 if (ToPointeeType.isNull()) 1098 return QualType(); 1099 1100 return Importer.getToContext().getLValueReferenceType(ToPointeeType); 1101} 1102 1103QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) { 1104 // FIXME: Check for C++0x support in "to" context. 1105 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); 1106 if (ToPointeeType.isNull()) 1107 return QualType(); 1108 1109 return Importer.getToContext().getRValueReferenceType(ToPointeeType); 1110} 1111 1112QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) { 1113 // FIXME: Check for C++ support in "to" context. 1114 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1115 if (ToPointeeType.isNull()) 1116 return QualType(); 1117 1118 QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); 1119 return Importer.getToContext().getMemberPointerType(ToPointeeType, 1120 ClassType.getTypePtr()); 1121} 1122 1123QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) { 1124 QualType ToElementType = Importer.Import(T->getElementType()); 1125 if (ToElementType.isNull()) 1126 return QualType(); 1127 1128 return Importer.getToContext().getConstantArrayType(ToElementType, 1129 T->getSize(), 1130 T->getSizeModifier(), 1131 T->getIndexTypeCVRQualifiers()); 1132} 1133 1134QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) { 1135 QualType ToElementType = Importer.Import(T->getElementType()); 1136 if (ToElementType.isNull()) 1137 return QualType(); 1138 1139 return Importer.getToContext().getIncompleteArrayType(ToElementType, 1140 T->getSizeModifier(), 1141 T->getIndexTypeCVRQualifiers()); 1142} 1143 1144QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) { 1145 QualType ToElementType = Importer.Import(T->getElementType()); 1146 if (ToElementType.isNull()) 1147 return QualType(); 1148 1149 Expr *Size = Importer.Import(T->getSizeExpr()); 1150 if (!Size) 1151 return QualType(); 1152 1153 SourceRange Brackets = Importer.Import(T->getBracketsRange()); 1154 return Importer.getToContext().getVariableArrayType(ToElementType, Size, 1155 T->getSizeModifier(), 1156 T->getIndexTypeCVRQualifiers(), 1157 Brackets); 1158} 1159 1160QualType ASTNodeImporter::VisitVectorType(VectorType *T) { 1161 QualType ToElementType = Importer.Import(T->getElementType()); 1162 if (ToElementType.isNull()) 1163 return QualType(); 1164 1165 return Importer.getToContext().getVectorType(ToElementType, 1166 T->getNumElements(), 1167 T->isAltiVec(), 1168 T->isPixel()); 1169} 1170 1171QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) { 1172 QualType ToElementType = Importer.Import(T->getElementType()); 1173 if (ToElementType.isNull()) 1174 return QualType(); 1175 1176 return Importer.getToContext().getExtVectorType(ToElementType, 1177 T->getNumElements()); 1178} 1179 1180QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) { 1181 // FIXME: What happens if we're importing a function without a prototype 1182 // into C++? Should we make it variadic? 1183 QualType ToResultType = Importer.Import(T->getResultType()); 1184 if (ToResultType.isNull()) 1185 return QualType(); 1186 1187 return Importer.getToContext().getFunctionNoProtoType(ToResultType, 1188 T->getNoReturnAttr(), 1189 T->getCallConv()); 1190} 1191 1192QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) { 1193 QualType ToResultType = Importer.Import(T->getResultType()); 1194 if (ToResultType.isNull()) 1195 return QualType(); 1196 1197 // Import argument types 1198 llvm::SmallVector<QualType, 4> ArgTypes; 1199 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), 1200 AEnd = T->arg_type_end(); 1201 A != AEnd; ++A) { 1202 QualType ArgType = Importer.Import(*A); 1203 if (ArgType.isNull()) 1204 return QualType(); 1205 ArgTypes.push_back(ArgType); 1206 } 1207 1208 // Import exception types 1209 llvm::SmallVector<QualType, 4> ExceptionTypes; 1210 for (FunctionProtoType::exception_iterator E = T->exception_begin(), 1211 EEnd = T->exception_end(); 1212 E != EEnd; ++E) { 1213 QualType ExceptionType = Importer.Import(*E); 1214 if (ExceptionType.isNull()) 1215 return QualType(); 1216 ExceptionTypes.push_back(ExceptionType); 1217 } 1218 1219 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(), 1220 ArgTypes.size(), 1221 T->isVariadic(), 1222 T->getTypeQuals(), 1223 T->hasExceptionSpec(), 1224 T->hasAnyExceptionSpec(), 1225 ExceptionTypes.size(), 1226 ExceptionTypes.data(), 1227 T->getNoReturnAttr(), 1228 T->getCallConv()); 1229} 1230 1231QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) { 1232 TypedefDecl *ToDecl 1233 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl())); 1234 if (!ToDecl) 1235 return QualType(); 1236 1237 return Importer.getToContext().getTypeDeclType(ToDecl); 1238} 1239 1240QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) { 1241 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); 1242 if (!ToExpr) 1243 return QualType(); 1244 1245 return Importer.getToContext().getTypeOfExprType(ToExpr); 1246} 1247 1248QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) { 1249 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); 1250 if (ToUnderlyingType.isNull()) 1251 return QualType(); 1252 1253 return Importer.getToContext().getTypeOfType(ToUnderlyingType); 1254} 1255 1256QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) { 1257 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); 1258 if (!ToExpr) 1259 return QualType(); 1260 1261 return Importer.getToContext().getDecltypeType(ToExpr); 1262} 1263 1264QualType ASTNodeImporter::VisitRecordType(RecordType *T) { 1265 RecordDecl *ToDecl 1266 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); 1267 if (!ToDecl) 1268 return QualType(); 1269 1270 return Importer.getToContext().getTagDeclType(ToDecl); 1271} 1272 1273QualType ASTNodeImporter::VisitEnumType(EnumType *T) { 1274 EnumDecl *ToDecl 1275 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl())); 1276 if (!ToDecl) 1277 return QualType(); 1278 1279 return Importer.getToContext().getTagDeclType(ToDecl); 1280} 1281 1282QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) { 1283 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); 1284 if (ToUnderlyingType.isNull()) 1285 return QualType(); 1286 1287 return Importer.getToContext().getElaboratedType(ToUnderlyingType, 1288 T->getTagKind()); 1289} 1290 1291QualType ASTNodeImporter::VisitQualifiedNameType(QualifiedNameType *T) { 1292 NestedNameSpecifier *ToQualifier = Importer.Import(T->getQualifier()); 1293 if (!ToQualifier) 1294 return QualType(); 1295 1296 QualType ToNamedType = Importer.Import(T->getNamedType()); 1297 if (ToNamedType.isNull()) 1298 return QualType(); 1299 1300 return Importer.getToContext().getQualifiedNameType(ToQualifier, ToNamedType); 1301} 1302 1303QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) { 1304 ObjCInterfaceDecl *Class 1305 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl())); 1306 if (!Class) 1307 return QualType(); 1308 1309 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; 1310 for (ObjCInterfaceType::qual_iterator P = T->qual_begin(), 1311 PEnd = T->qual_end(); 1312 P != PEnd; ++P) { 1313 ObjCProtocolDecl *Protocol 1314 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P)); 1315 if (!Protocol) 1316 return QualType(); 1317 Protocols.push_back(Protocol); 1318 } 1319 1320 return Importer.getToContext().getObjCInterfaceType(Class, 1321 Protocols.data(), 1322 Protocols.size()); 1323} 1324 1325QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) { 1326 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1327 if (ToPointeeType.isNull()) 1328 return QualType(); 1329 1330 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; 1331 for (ObjCObjectPointerType::qual_iterator P = T->qual_begin(), 1332 PEnd = T->qual_end(); 1333 P != PEnd; ++P) { 1334 ObjCProtocolDecl *Protocol 1335 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P)); 1336 if (!Protocol) 1337 return QualType(); 1338 Protocols.push_back(Protocol); 1339 } 1340 1341 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType, 1342 Protocols.data(), 1343 Protocols.size()); 1344} 1345 1346//---------------------------------------------------------------------------- 1347// Import Declarations 1348//---------------------------------------------------------------------------- 1349bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC, 1350 DeclContext *&LexicalDC, 1351 DeclarationName &Name, 1352 SourceLocation &Loc) { 1353 // Import the context of this declaration. 1354 DC = Importer.ImportContext(D->getDeclContext()); 1355 if (!DC) 1356 return true; 1357 1358 LexicalDC = DC; 1359 if (D->getDeclContext() != D->getLexicalDeclContext()) { 1360 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 1361 if (!LexicalDC) 1362 return true; 1363 } 1364 1365 // Import the name of this declaration. 1366 Name = Importer.Import(D->getDeclName()); 1367 if (D->getDeclName() && !Name) 1368 return true; 1369 1370 // Import the location of this declaration. 1371 Loc = Importer.Import(D->getLocation()); 1372 return false; 1373} 1374 1375bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, 1376 RecordDecl *ToRecord) { 1377 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 1378 Importer.getToContext(), 1379 Importer.getDiags(), 1380 Importer.getNonEquivalentDecls()); 1381 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord); 1382} 1383 1384bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { 1385 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 1386 Importer.getToContext(), 1387 Importer.getDiags(), 1388 Importer.getNonEquivalentDecls()); 1389 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum); 1390} 1391 1392Decl *ASTNodeImporter::VisitDecl(Decl *D) { 1393 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) 1394 << D->getDeclKindName(); 1395 return 0; 1396} 1397 1398Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { 1399 // Import the major distinguishing characteristics of this typedef. 1400 DeclContext *DC, *LexicalDC; 1401 DeclarationName Name; 1402 SourceLocation Loc; 1403 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) 1404 return 0; 1405 1406 // If this typedef is not in block scope, determine whether we've 1407 // seen a typedef with the same name (that we can merge with) or any 1408 // other entity by that name (which name lookup could conflict with). 1409 if (!DC->isFunctionOrMethod()) { 1410 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; 1411 unsigned IDNS = Decl::IDNS_Ordinary; 1412 for (DeclContext::lookup_result Lookup = DC->lookup(Name); 1413 Lookup.first != Lookup.second; 1414 ++Lookup.first) { 1415 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) 1416 continue; 1417 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) { 1418 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(), 1419 FoundTypedef->getUnderlyingType())) 1420 return Importer.Imported(D, FoundTypedef); 1421 } 1422 1423 ConflictingDecls.push_back(*Lookup.first); 1424 } 1425 1426 if (!ConflictingDecls.empty()) { 1427 Name = Importer.HandleNameConflict(Name, DC, IDNS, 1428 ConflictingDecls.data(), 1429 ConflictingDecls.size()); 1430 if (!Name) 1431 return 0; 1432 } 1433 } 1434 1435 // Import the underlying type of this typedef; 1436 QualType T = Importer.Import(D->getUnderlyingType()); 1437 if (T.isNull()) 1438 return 0; 1439 1440 // Create the new typedef node. 1441 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 1442 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC, 1443 Loc, Name.getAsIdentifierInfo(), 1444 TInfo); 1445 ToTypedef->setLexicalDeclContext(LexicalDC); 1446 Importer.Imported(D, ToTypedef); 1447 LexicalDC->addDecl(ToTypedef); 1448 1449 return ToTypedef; 1450} 1451 1452Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { 1453 // Import the major distinguishing characteristics of this enum. 1454 DeclContext *DC, *LexicalDC; 1455 DeclarationName Name; 1456 SourceLocation Loc; 1457 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) 1458 return 0; 1459 1460 // Figure out what enum name we're looking for. 1461 unsigned IDNS = Decl::IDNS_Tag; 1462 DeclarationName SearchName = Name; 1463 if (!SearchName && D->getTypedefForAnonDecl()) { 1464 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName()); 1465 IDNS = Decl::IDNS_Ordinary; 1466 } else if (Importer.getToContext().getLangOptions().CPlusPlus) 1467 IDNS |= Decl::IDNS_Ordinary; 1468 1469 // We may already have an enum of the same name; try to find and match it. 1470 if (!DC->isFunctionOrMethod() && SearchName) { 1471 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; 1472 for (DeclContext::lookup_result Lookup = DC->lookup(Name); 1473 Lookup.first != Lookup.second; 1474 ++Lookup.first) { 1475 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) 1476 continue; 1477 1478 Decl *Found = *Lookup.first; 1479 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) { 1480 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) 1481 Found = Tag->getDecl(); 1482 } 1483 1484 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) { 1485 if (IsStructuralMatch(D, FoundEnum)) 1486 return Importer.Imported(D, FoundEnum); 1487 } 1488 1489 ConflictingDecls.push_back(*Lookup.first); 1490 } 1491 1492 if (!ConflictingDecls.empty()) { 1493 Name = Importer.HandleNameConflict(Name, DC, IDNS, 1494 ConflictingDecls.data(), 1495 ConflictingDecls.size()); 1496 } 1497 } 1498 1499 // Create the enum declaration. 1500 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc, 1501 Name.getAsIdentifierInfo(), 1502 Importer.Import(D->getTagKeywordLoc()), 1503 0); 1504 D2->setLexicalDeclContext(LexicalDC); 1505 Importer.Imported(D, D2); 1506 LexicalDC->addDecl(D2); 1507 1508 // Import the integer type. 1509 QualType ToIntegerType = Importer.Import(D->getIntegerType()); 1510 if (ToIntegerType.isNull()) 1511 return 0; 1512 D2->setIntegerType(ToIntegerType); 1513 1514 // Import the definition 1515 if (D->isDefinition()) { 1516 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D)); 1517 if (T.isNull()) 1518 return 0; 1519 1520 QualType ToPromotionType = Importer.Import(D->getPromotionType()); 1521 if (ToPromotionType.isNull()) 1522 return 0; 1523 1524 D2->startDefinition(); 1525 for (DeclContext::decl_iterator FromMem = D->decls_begin(), 1526 FromMemEnd = D->decls_end(); 1527 FromMem != FromMemEnd; 1528 ++FromMem) 1529 Importer.Import(*FromMem); 1530 1531 D2->completeDefinition(T, ToPromotionType); 1532 } 1533 1534 return D2; 1535} 1536 1537Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { 1538 // If this record has a definition in the translation unit we're coming from, 1539 // but this particular declaration is not that definition, import the 1540 // definition and map to that. 1541 TagDecl *Definition = D->getDefinition(); 1542 if (Definition && Definition != D) { 1543 Decl *ImportedDef = Importer.Import(Definition); 1544 if (!ImportedDef) 1545 return 0; 1546 1547 return Importer.Imported(D, ImportedDef); 1548 } 1549 1550 // Import the major distinguishing characteristics of this record. 1551 DeclContext *DC, *LexicalDC; 1552 DeclarationName Name; 1553 SourceLocation Loc; 1554 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) 1555 return 0; 1556 1557 // Figure out what structure name we're looking for. 1558 unsigned IDNS = Decl::IDNS_Tag; 1559 DeclarationName SearchName = Name; 1560 if (!SearchName && D->getTypedefForAnonDecl()) { 1561 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName()); 1562 IDNS = Decl::IDNS_Ordinary; 1563 } else if (Importer.getToContext().getLangOptions().CPlusPlus) 1564 IDNS |= Decl::IDNS_Ordinary; 1565 1566 // We may already have a record of the same name; try to find and match it. 1567 RecordDecl *AdoptDecl = 0; 1568 if (!DC->isFunctionOrMethod() && SearchName) { 1569 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; 1570 for (DeclContext::lookup_result Lookup = DC->lookup(Name); 1571 Lookup.first != Lookup.second; 1572 ++Lookup.first) { 1573 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) 1574 continue; 1575 1576 Decl *Found = *Lookup.first; 1577 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) { 1578 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) 1579 Found = Tag->getDecl(); 1580 } 1581 1582 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) { 1583 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { 1584 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) { 1585 // The record types structurally match, or the "from" translation 1586 // unit only had a forward declaration anyway; call it the same 1587 // function. 1588 // FIXME: For C++, we should also merge methods here. 1589 return Importer.Imported(D, FoundDef); 1590 } 1591 } else { 1592 // We have a forward declaration of this type, so adopt that forward 1593 // declaration rather than building a new one. 1594 AdoptDecl = FoundRecord; 1595 continue; 1596 } 1597 } 1598 1599 ConflictingDecls.push_back(*Lookup.first); 1600 } 1601 1602 if (!ConflictingDecls.empty()) { 1603 Name = Importer.HandleNameConflict(Name, DC, IDNS, 1604 ConflictingDecls.data(), 1605 ConflictingDecls.size()); 1606 } 1607 } 1608 1609 // Create the record declaration. 1610 RecordDecl *D2 = AdoptDecl; 1611 if (!D2) { 1612 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D)) { 1613 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(), 1614 D->getTagKind(), 1615 DC, Loc, 1616 Name.getAsIdentifierInfo(), 1617 Importer.Import(D->getTagKeywordLoc())); 1618 D2 = D2CXX; 1619 1620 if (D->isDefinition()) { 1621 // Add base classes. 1622 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases; 1623 for (CXXRecordDecl::base_class_iterator 1624 Base1 = D1CXX->bases_begin(), 1625 FromBaseEnd = D1CXX->bases_end(); 1626 Base1 != FromBaseEnd; 1627 ++Base1) { 1628 QualType T = Importer.Import(Base1->getType()); 1629 if (T.isNull()) 1630 return 0; 1631 1632 Bases.push_back( 1633 new (Importer.getToContext()) 1634 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()), 1635 Base1->isVirtual(), 1636 Base1->isBaseOfClass(), 1637 Base1->getAccessSpecifierAsWritten(), 1638 T)); 1639 } 1640 if (!Bases.empty()) 1641 D2CXX->setBases(Bases.data(), Bases.size()); 1642 } 1643 } else { 1644 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(), 1645 DC, Loc, 1646 Name.getAsIdentifierInfo(), 1647 Importer.Import(D->getTagKeywordLoc())); 1648 } 1649 D2->setLexicalDeclContext(LexicalDC); 1650 LexicalDC->addDecl(D2); 1651 } 1652 1653 Importer.Imported(D, D2); 1654 1655 if (D->isDefinition()) { 1656 D2->startDefinition(); 1657 for (DeclContext::decl_iterator FromMem = D->decls_begin(), 1658 FromMemEnd = D->decls_end(); 1659 FromMem != FromMemEnd; 1660 ++FromMem) 1661 Importer.Import(*FromMem); 1662 1663 D2->completeDefinition(); 1664 } 1665 1666 return D2; 1667} 1668 1669Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { 1670 // Import the major distinguishing characteristics of this enumerator. 1671 DeclContext *DC, *LexicalDC; 1672 DeclarationName Name; 1673 SourceLocation Loc; 1674 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) 1675 return 0; 1676 1677 QualType T = Importer.Import(D->getType()); 1678 if (T.isNull()) 1679 return 0; 1680 1681 // Determine whether there are any other declarations with the same name and 1682 // in the same context. 1683 if (!LexicalDC->isFunctionOrMethod()) { 1684 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; 1685 unsigned IDNS = Decl::IDNS_Ordinary; 1686 for (DeclContext::lookup_result Lookup = DC->lookup(Name); 1687 Lookup.first != Lookup.second; 1688 ++Lookup.first) { 1689 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) 1690 continue; 1691 1692 ConflictingDecls.push_back(*Lookup.first); 1693 } 1694 1695 if (!ConflictingDecls.empty()) { 1696 Name = Importer.HandleNameConflict(Name, DC, IDNS, 1697 ConflictingDecls.data(), 1698 ConflictingDecls.size()); 1699 if (!Name) 1700 return 0; 1701 } 1702 } 1703 1704 Expr *Init = Importer.Import(D->getInitExpr()); 1705 if (D->getInitExpr() && !Init) 1706 return 0; 1707 1708 EnumConstantDecl *ToEnumerator 1709 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc, 1710 Name.getAsIdentifierInfo(), T, 1711 Init, D->getInitVal()); 1712 ToEnumerator->setLexicalDeclContext(LexicalDC); 1713 Importer.Imported(D, ToEnumerator); 1714 LexicalDC->addDecl(ToEnumerator); 1715 return ToEnumerator; 1716} 1717 1718Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { 1719 // Import the major distinguishing characteristics of this function. 1720 DeclContext *DC, *LexicalDC; 1721 DeclarationName Name; 1722 SourceLocation Loc; 1723 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) 1724 return 0; 1725 1726 // Try to find a function in our own ("to") context with the same name, same 1727 // type, and in the same context as the function we're importing. 1728 if (!LexicalDC->isFunctionOrMethod()) { 1729 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; 1730 unsigned IDNS = Decl::IDNS_Ordinary; 1731 for (DeclContext::lookup_result Lookup = DC->lookup(Name); 1732 Lookup.first != Lookup.second; 1733 ++Lookup.first) { 1734 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) 1735 continue; 1736 1737 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) { 1738 if (isExternalLinkage(FoundFunction->getLinkage()) && 1739 isExternalLinkage(D->getLinkage())) { 1740 if (Importer.IsStructurallyEquivalent(D->getType(), 1741 FoundFunction->getType())) { 1742 // FIXME: Actually try to merge the body and other attributes. 1743 return Importer.Imported(D, FoundFunction); 1744 } 1745 1746 // FIXME: Check for overloading more carefully, e.g., by boosting 1747 // Sema::IsOverload out to the AST library. 1748 1749 // Function overloading is okay in C++. 1750 if (Importer.getToContext().getLangOptions().CPlusPlus) 1751 continue; 1752 1753 // Complain about inconsistent function types. 1754 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) 1755 << Name << D->getType() << FoundFunction->getType(); 1756 Importer.ToDiag(FoundFunction->getLocation(), 1757 diag::note_odr_value_here) 1758 << FoundFunction->getType(); 1759 } 1760 } 1761 1762 ConflictingDecls.push_back(*Lookup.first); 1763 } 1764 1765 if (!ConflictingDecls.empty()) { 1766 Name = Importer.HandleNameConflict(Name, DC, IDNS, 1767 ConflictingDecls.data(), 1768 ConflictingDecls.size()); 1769 if (!Name) 1770 return 0; 1771 } 1772 } 1773 1774 // Import the type. 1775 QualType T = Importer.Import(D->getType()); 1776 if (T.isNull()) 1777 return 0; 1778 1779 // Import the function parameters. 1780 llvm::SmallVector<ParmVarDecl *, 8> Parameters; 1781 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); 1782 P != PEnd; ++P) { 1783 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P)); 1784 if (!ToP) 1785 return 0; 1786 1787 Parameters.push_back(ToP); 1788 } 1789 1790 // Create the imported function. 1791 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 1792 FunctionDecl *ToFunction 1793 = FunctionDecl::Create(Importer.getToContext(), DC, Loc, 1794 Name, T, TInfo, D->getStorageClass(), 1795 D->isInlineSpecified(), 1796 D->hasWrittenPrototype()); 1797 ToFunction->setLexicalDeclContext(LexicalDC); 1798 Importer.Imported(D, ToFunction); 1799 LexicalDC->addDecl(ToFunction); 1800 1801 // Set the parameters. 1802 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) { 1803 Parameters[I]->setOwningFunction(ToFunction); 1804 ToFunction->addDecl(Parameters[I]); 1805 } 1806 ToFunction->setParams(Parameters.data(), Parameters.size()); 1807 1808 // FIXME: Other bits to merge? 1809 1810 return ToFunction; 1811} 1812 1813Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { 1814 // Import the major distinguishing characteristics of a variable. 1815 DeclContext *DC, *LexicalDC; 1816 DeclarationName Name; 1817 SourceLocation Loc; 1818 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) 1819 return 0; 1820 1821 // Import the type. 1822 QualType T = Importer.Import(D->getType()); 1823 if (T.isNull()) 1824 return 0; 1825 1826 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 1827 Expr *BitWidth = Importer.Import(D->getBitWidth()); 1828 if (!BitWidth && D->getBitWidth()) 1829 return 0; 1830 1831 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC, 1832 Loc, Name.getAsIdentifierInfo(), 1833 T, TInfo, BitWidth, D->isMutable()); 1834 ToField->setLexicalDeclContext(LexicalDC); 1835 Importer.Imported(D, ToField); 1836 LexicalDC->addDecl(ToField); 1837 return ToField; 1838} 1839 1840Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { 1841 // Import the major distinguishing characteristics of an ivar. 1842 DeclContext *DC, *LexicalDC; 1843 DeclarationName Name; 1844 SourceLocation Loc; 1845 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) 1846 return 0; 1847 1848 // Determine whether we've already imported this ivar 1849 for (DeclContext::lookup_result Lookup = DC->lookup(Name); 1850 Lookup.first != Lookup.second; 1851 ++Lookup.first) { 1852 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) { 1853 if (Importer.IsStructurallyEquivalent(D->getType(), 1854 FoundIvar->getType())) { 1855 Importer.Imported(D, FoundIvar); 1856 return FoundIvar; 1857 } 1858 1859 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent) 1860 << Name << D->getType() << FoundIvar->getType(); 1861 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) 1862 << FoundIvar->getType(); 1863 return 0; 1864 } 1865 } 1866 1867 // Import the type. 1868 QualType T = Importer.Import(D->getType()); 1869 if (T.isNull()) 1870 return 0; 1871 1872 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 1873 Expr *BitWidth = Importer.Import(D->getBitWidth()); 1874 if (!BitWidth && D->getBitWidth()) 1875 return 0; 1876 1877 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), DC, 1878 Loc, Name.getAsIdentifierInfo(), 1879 T, TInfo, D->getAccessControl(), 1880 BitWidth); 1881 ToIvar->setLexicalDeclContext(LexicalDC); 1882 Importer.Imported(D, ToIvar); 1883 LexicalDC->addDecl(ToIvar); 1884 return ToIvar; 1885 1886} 1887 1888Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { 1889 // Import the major distinguishing characteristics of a variable. 1890 DeclContext *DC, *LexicalDC; 1891 DeclarationName Name; 1892 SourceLocation Loc; 1893 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) 1894 return 0; 1895 1896 // Try to find a variable in our own ("to") context with the same name and 1897 // in the same context as the variable we're importing. 1898 if (D->isFileVarDecl()) { 1899 VarDecl *MergeWithVar = 0; 1900 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls; 1901 unsigned IDNS = Decl::IDNS_Ordinary; 1902 for (DeclContext::lookup_result Lookup = DC->lookup(Name); 1903 Lookup.first != Lookup.second; 1904 ++Lookup.first) { 1905 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS)) 1906 continue; 1907 1908 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) { 1909 // We have found a variable that we may need to merge with. Check it. 1910 if (isExternalLinkage(FoundVar->getLinkage()) && 1911 isExternalLinkage(D->getLinkage())) { 1912 if (Importer.IsStructurallyEquivalent(D->getType(), 1913 FoundVar->getType())) { 1914 MergeWithVar = FoundVar; 1915 break; 1916 } 1917 1918 const ArrayType *FoundArray 1919 = Importer.getToContext().getAsArrayType(FoundVar->getType()); 1920 const ArrayType *TArray 1921 = Importer.getToContext().getAsArrayType(D->getType()); 1922 if (FoundArray && TArray) { 1923 if (isa<IncompleteArrayType>(FoundArray) && 1924 isa<ConstantArrayType>(TArray)) { 1925 // Import the type. 1926 QualType T = Importer.Import(D->getType()); 1927 if (T.isNull()) 1928 return 0; 1929 1930 FoundVar->setType(T); 1931 MergeWithVar = FoundVar; 1932 break; 1933 } else if (isa<IncompleteArrayType>(TArray) && 1934 isa<ConstantArrayType>(FoundArray)) { 1935 MergeWithVar = FoundVar; 1936 break; 1937 } 1938 } 1939 1940 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) 1941 << Name << D->getType() << FoundVar->getType(); 1942 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) 1943 << FoundVar->getType(); 1944 } 1945 } 1946 1947 ConflictingDecls.push_back(*Lookup.first); 1948 } 1949 1950 if (MergeWithVar) { 1951 // An equivalent variable with external linkage has been found. Link 1952 // the two declarations, then merge them. 1953 Importer.Imported(D, MergeWithVar); 1954 1955 if (VarDecl *DDef = D->getDefinition()) { 1956 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { 1957 Importer.ToDiag(ExistingDef->getLocation(), 1958 diag::err_odr_variable_multiple_def) 1959 << Name; 1960 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); 1961 } else { 1962 Expr *Init = Importer.Import(DDef->getInit()); 1963 MergeWithVar->setInit(Init); 1964 } 1965 } 1966 1967 return MergeWithVar; 1968 } 1969 1970 if (!ConflictingDecls.empty()) { 1971 Name = Importer.HandleNameConflict(Name, DC, IDNS, 1972 ConflictingDecls.data(), 1973 ConflictingDecls.size()); 1974 if (!Name) 1975 return 0; 1976 } 1977 } 1978 1979 // Import the type. 1980 QualType T = Importer.Import(D->getType()); 1981 if (T.isNull()) 1982 return 0; 1983 1984 // Create the imported variable. 1985 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 1986 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc, 1987 Name.getAsIdentifierInfo(), T, TInfo, 1988 D->getStorageClass()); 1989 ToVar->setLexicalDeclContext(LexicalDC); 1990 Importer.Imported(D, ToVar); 1991 LexicalDC->addDecl(ToVar); 1992 1993 // Merge the initializer. 1994 // FIXME: Can we really import any initializer? Alternatively, we could force 1995 // ourselves to import every declaration of a variable and then only use 1996 // getInit() here. 1997 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer()))); 1998 1999 // FIXME: Other bits to merge? 2000 2001 return ToVar; 2002} 2003 2004Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { 2005 // Parameters are created in the translation unit's context, then moved 2006 // into the function declaration's context afterward. 2007 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); 2008 2009 // Import the name of this declaration. 2010 DeclarationName Name = Importer.Import(D->getDeclName()); 2011 if (D->getDeclName() && !Name) 2012 return 0; 2013 2014 // Import the location of this declaration. 2015 SourceLocation Loc = Importer.Import(D->getLocation()); 2016 2017 // Import the parameter's type. 2018 QualType T = Importer.Import(D->getType()); 2019 if (T.isNull()) 2020 return 0; 2021 2022 // Create the imported parameter. 2023 ImplicitParamDecl *ToParm 2024 = ImplicitParamDecl::Create(Importer.getToContext(), DC, 2025 Loc, Name.getAsIdentifierInfo(), 2026 T); 2027 return Importer.Imported(D, ToParm); 2028} 2029 2030Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { 2031 // Parameters are created in the translation unit's context, then moved 2032 // into the function declaration's context afterward. 2033 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); 2034 2035 // Import the name of this declaration. 2036 DeclarationName Name = Importer.Import(D->getDeclName()); 2037 if (D->getDeclName() && !Name) 2038 return 0; 2039 2040 // Import the location of this declaration. 2041 SourceLocation Loc = Importer.Import(D->getLocation()); 2042 2043 // Import the parameter's type. 2044 QualType T = Importer.Import(D->getType()); 2045 if (T.isNull()) 2046 return 0; 2047 2048 // Create the imported parameter. 2049 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 2050 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC, 2051 Loc, Name.getAsIdentifierInfo(), 2052 T, TInfo, D->getStorageClass(), 2053 /*FIXME: Default argument*/ 0); 2054 return Importer.Imported(D, ToParm); 2055} 2056 2057Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { 2058 // Import the major distinguishing characteristics of a method. 2059 DeclContext *DC, *LexicalDC; 2060 DeclarationName Name; 2061 SourceLocation Loc; 2062 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) 2063 return 0; 2064 2065 for (DeclContext::lookup_result Lookup = DC->lookup(Name); 2066 Lookup.first != Lookup.second; 2067 ++Lookup.first) { 2068 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) { 2069 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) 2070 continue; 2071 2072 // Check return types. 2073 if (!Importer.IsStructurallyEquivalent(D->getResultType(), 2074 FoundMethod->getResultType())) { 2075 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent) 2076 << D->isInstanceMethod() << Name 2077 << D->getResultType() << FoundMethod->getResultType(); 2078 Importer.ToDiag(FoundMethod->getLocation(), 2079 diag::note_odr_objc_method_here) 2080 << D->isInstanceMethod() << Name; 2081 return 0; 2082 } 2083 2084 // Check the number of parameters. 2085 if (D->param_size() != FoundMethod->param_size()) { 2086 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent) 2087 << D->isInstanceMethod() << Name 2088 << D->param_size() << FoundMethod->param_size(); 2089 Importer.ToDiag(FoundMethod->getLocation(), 2090 diag::note_odr_objc_method_here) 2091 << D->isInstanceMethod() << Name; 2092 return 0; 2093 } 2094 2095 // Check parameter types. 2096 for (ObjCMethodDecl::param_iterator P = D->param_begin(), 2097 PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); 2098 P != PEnd; ++P, ++FoundP) { 2099 if (!Importer.IsStructurallyEquivalent((*P)->getType(), 2100 (*FoundP)->getType())) { 2101 Importer.FromDiag((*P)->getLocation(), 2102 diag::err_odr_objc_method_param_type_inconsistent) 2103 << D->isInstanceMethod() << Name 2104 << (*P)->getType() << (*FoundP)->getType(); 2105 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) 2106 << (*FoundP)->getType(); 2107 return 0; 2108 } 2109 } 2110 2111 // Check variadic/non-variadic. 2112 // Check the number of parameters. 2113 if (D->isVariadic() != FoundMethod->isVariadic()) { 2114 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent) 2115 << D->isInstanceMethod() << Name; 2116 Importer.ToDiag(FoundMethod->getLocation(), 2117 diag::note_odr_objc_method_here) 2118 << D->isInstanceMethod() << Name; 2119 return 0; 2120 } 2121 2122 // FIXME: Any other bits we need to merge? 2123 return Importer.Imported(D, FoundMethod); 2124 } 2125 } 2126 2127 // Import the result type. 2128 QualType ResultTy = Importer.Import(D->getResultType()); 2129 if (ResultTy.isNull()) 2130 return 0; 2131 2132 ObjCMethodDecl *ToMethod 2133 = ObjCMethodDecl::Create(Importer.getToContext(), 2134 Loc, 2135 Importer.Import(D->getLocEnd()), 2136 Name.getObjCSelector(), 2137 ResultTy, DC, 2138 D->isInstanceMethod(), 2139 D->isVariadic(), 2140 D->isSynthesized(), 2141 D->getImplementationControl()); 2142 2143 // FIXME: When we decide to merge method definitions, we'll need to 2144 // deal with implicit parameters. 2145 2146 // Import the parameters 2147 llvm::SmallVector<ParmVarDecl *, 5> ToParams; 2148 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(), 2149 FromPEnd = D->param_end(); 2150 FromP != FromPEnd; 2151 ++FromP) { 2152 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP)); 2153 if (!ToP) 2154 return 0; 2155 2156 ToParams.push_back(ToP); 2157 } 2158 2159 // Set the parameters. 2160 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) { 2161 ToParams[I]->setOwningFunction(ToMethod); 2162 ToMethod->addDecl(ToParams[I]); 2163 } 2164 ToMethod->setMethodParams(Importer.getToContext(), 2165 ToParams.data(), ToParams.size()); 2166 2167 ToMethod->setLexicalDeclContext(LexicalDC); 2168 Importer.Imported(D, ToMethod); 2169 LexicalDC->addDecl(ToMethod); 2170 return ToMethod; 2171} 2172 2173Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { 2174 // Import the major distinguishing characteristics of a category. 2175 DeclContext *DC, *LexicalDC; 2176 DeclarationName Name; 2177 SourceLocation Loc; 2178 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) 2179 return 0; 2180 2181 ObjCInterfaceDecl *ToInterface 2182 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface())); 2183 if (!ToInterface) 2184 return 0; 2185 2186 // Determine if we've already encountered this category. 2187 ObjCCategoryDecl *MergeWithCategory 2188 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); 2189 ObjCCategoryDecl *ToCategory = MergeWithCategory; 2190 if (!ToCategory) { 2191 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC, 2192 Importer.Import(D->getAtLoc()), 2193 Loc, 2194 Importer.Import(D->getCategoryNameLoc()), 2195 Name.getAsIdentifierInfo()); 2196 ToCategory->setLexicalDeclContext(LexicalDC); 2197 LexicalDC->addDecl(ToCategory); 2198 Importer.Imported(D, ToCategory); 2199 2200 // Link this category into its class's category list. 2201 ToCategory->setClassInterface(ToInterface); 2202 ToCategory->insertNextClassCategory(); 2203 2204 // Import protocols 2205 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; 2206 llvm::SmallVector<SourceLocation, 4> ProtocolLocs; 2207 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc 2208 = D->protocol_loc_begin(); 2209 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), 2210 FromProtoEnd = D->protocol_end(); 2211 FromProto != FromProtoEnd; 2212 ++FromProto, ++FromProtoLoc) { 2213 ObjCProtocolDecl *ToProto 2214 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 2215 if (!ToProto) 2216 return 0; 2217 Protocols.push_back(ToProto); 2218 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 2219 } 2220 2221 // FIXME: If we're merging, make sure that the protocol list is the same. 2222 ToCategory->setProtocolList(Protocols.data(), Protocols.size(), 2223 ProtocolLocs.data(), Importer.getToContext()); 2224 2225 } else { 2226 Importer.Imported(D, ToCategory); 2227 } 2228 2229 // Import all of the members of this category. 2230 for (DeclContext::decl_iterator FromMem = D->decls_begin(), 2231 FromMemEnd = D->decls_end(); 2232 FromMem != FromMemEnd; 2233 ++FromMem) 2234 Importer.Import(*FromMem); 2235 2236 // If we have an implementation, import it as well. 2237 if (D->getImplementation()) { 2238 ObjCCategoryImplDecl *Impl 2239 = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation())); 2240 if (!Impl) 2241 return 0; 2242 2243 ToCategory->setImplementation(Impl); 2244 } 2245 2246 return ToCategory; 2247} 2248 2249Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { 2250 // Import the major distinguishing characteristics of a protocol. 2251 DeclContext *DC, *LexicalDC; 2252 DeclarationName Name; 2253 SourceLocation Loc; 2254 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) 2255 return 0; 2256 2257 ObjCProtocolDecl *MergeWithProtocol = 0; 2258 for (DeclContext::lookup_result Lookup = DC->lookup(Name); 2259 Lookup.first != Lookup.second; 2260 ++Lookup.first) { 2261 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) 2262 continue; 2263 2264 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first))) 2265 break; 2266 } 2267 2268 ObjCProtocolDecl *ToProto = MergeWithProtocol; 2269 if (!ToProto || ToProto->isForwardDecl()) { 2270 if (!ToProto) { 2271 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc, 2272 Name.getAsIdentifierInfo()); 2273 ToProto->setForwardDecl(D->isForwardDecl()); 2274 ToProto->setLexicalDeclContext(LexicalDC); 2275 LexicalDC->addDecl(ToProto); 2276 } 2277 Importer.Imported(D, ToProto); 2278 2279 // Import protocols 2280 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; 2281 llvm::SmallVector<SourceLocation, 4> ProtocolLocs; 2282 ObjCProtocolDecl::protocol_loc_iterator 2283 FromProtoLoc = D->protocol_loc_begin(); 2284 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(), 2285 FromProtoEnd = D->protocol_end(); 2286 FromProto != FromProtoEnd; 2287 ++FromProto, ++FromProtoLoc) { 2288 ObjCProtocolDecl *ToProto 2289 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 2290 if (!ToProto) 2291 return 0; 2292 Protocols.push_back(ToProto); 2293 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 2294 } 2295 2296 // FIXME: If we're merging, make sure that the protocol list is the same. 2297 ToProto->setProtocolList(Protocols.data(), Protocols.size(), 2298 ProtocolLocs.data(), Importer.getToContext()); 2299 } else { 2300 Importer.Imported(D, ToProto); 2301 } 2302 2303 // Import all of the members of this protocol. 2304 for (DeclContext::decl_iterator FromMem = D->decls_begin(), 2305 FromMemEnd = D->decls_end(); 2306 FromMem != FromMemEnd; 2307 ++FromMem) 2308 Importer.Import(*FromMem); 2309 2310 return ToProto; 2311} 2312 2313Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { 2314 // Import the major distinguishing characteristics of an @interface. 2315 DeclContext *DC, *LexicalDC; 2316 DeclarationName Name; 2317 SourceLocation Loc; 2318 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) 2319 return 0; 2320 2321 ObjCInterfaceDecl *MergeWithIface = 0; 2322 for (DeclContext::lookup_result Lookup = DC->lookup(Name); 2323 Lookup.first != Lookup.second; 2324 ++Lookup.first) { 2325 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 2326 continue; 2327 2328 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first))) 2329 break; 2330 } 2331 2332 ObjCInterfaceDecl *ToIface = MergeWithIface; 2333 if (!ToIface || ToIface->isForwardDecl()) { 2334 if (!ToIface) { 2335 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), 2336 DC, Loc, 2337 Name.getAsIdentifierInfo(), 2338 Importer.Import(D->getClassLoc()), 2339 D->isForwardDecl(), 2340 D->isImplicitInterfaceDecl()); 2341 ToIface->setForwardDecl(D->isForwardDecl()); 2342 ToIface->setLexicalDeclContext(LexicalDC); 2343 LexicalDC->addDecl(ToIface); 2344 } 2345 Importer.Imported(D, ToIface); 2346 2347 if (D->getSuperClass()) { 2348 ObjCInterfaceDecl *Super 2349 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass())); 2350 if (!Super) 2351 return 0; 2352 2353 ToIface->setSuperClass(Super); 2354 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc())); 2355 } 2356 2357 // Import protocols 2358 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; 2359 llvm::SmallVector<SourceLocation, 4> ProtocolLocs; 2360 ObjCInterfaceDecl::protocol_loc_iterator 2361 FromProtoLoc = D->protocol_loc_begin(); 2362 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(), 2363 FromProtoEnd = D->protocol_end(); 2364 FromProto != FromProtoEnd; 2365 ++FromProto, ++FromProtoLoc) { 2366 ObjCProtocolDecl *ToProto 2367 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 2368 if (!ToProto) 2369 return 0; 2370 Protocols.push_back(ToProto); 2371 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 2372 } 2373 2374 // FIXME: If we're merging, make sure that the protocol list is the same. 2375 ToIface->setProtocolList(Protocols.data(), Protocols.size(), 2376 ProtocolLocs.data(), Importer.getToContext()); 2377 2378 // Import @end range 2379 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange())); 2380 } else { 2381 Importer.Imported(D, ToIface); 2382 2383 // Check for consistency of superclasses. 2384 DeclarationName FromSuperName, ToSuperName; 2385 if (D->getSuperClass()) 2386 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName()); 2387 if (ToIface->getSuperClass()) 2388 ToSuperName = ToIface->getSuperClass()->getDeclName(); 2389 if (FromSuperName != ToSuperName) { 2390 Importer.ToDiag(ToIface->getLocation(), 2391 diag::err_odr_objc_superclass_inconsistent) 2392 << ToIface->getDeclName(); 2393 if (ToIface->getSuperClass()) 2394 Importer.ToDiag(ToIface->getSuperClassLoc(), 2395 diag::note_odr_objc_superclass) 2396 << ToIface->getSuperClass()->getDeclName(); 2397 else 2398 Importer.ToDiag(ToIface->getLocation(), 2399 diag::note_odr_objc_missing_superclass); 2400 if (D->getSuperClass()) 2401 Importer.FromDiag(D->getSuperClassLoc(), 2402 diag::note_odr_objc_superclass) 2403 << D->getSuperClass()->getDeclName(); 2404 else 2405 Importer.FromDiag(D->getLocation(), 2406 diag::note_odr_objc_missing_superclass); 2407 return 0; 2408 } 2409 } 2410 2411 // Import categories. When the categories themselves are imported, they'll 2412 // hook themselves into this interface. 2413 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat; 2414 FromCat = FromCat->getNextClassCategory()) 2415 Importer.Import(FromCat); 2416 2417 // Import all of the members of this class. 2418 for (DeclContext::decl_iterator FromMem = D->decls_begin(), 2419 FromMemEnd = D->decls_end(); 2420 FromMem != FromMemEnd; 2421 ++FromMem) 2422 Importer.Import(*FromMem); 2423 2424 // If we have an @implementation, import it as well. 2425 if (D->getImplementation()) { 2426 ObjCImplementationDecl *Impl 2427 = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation())); 2428 if (!Impl) 2429 return 0; 2430 2431 ToIface->setImplementation(Impl); 2432 } 2433 2434 return ToIface; 2435} 2436 2437Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { 2438 // Import the major distinguishing characteristics of an @property. 2439 DeclContext *DC, *LexicalDC; 2440 DeclarationName Name; 2441 SourceLocation Loc; 2442 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) 2443 return 0; 2444 2445 // Check whether we have already imported this property. 2446 for (DeclContext::lookup_result Lookup = DC->lookup(Name); 2447 Lookup.first != Lookup.second; 2448 ++Lookup.first) { 2449 if (ObjCPropertyDecl *FoundProp 2450 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) { 2451 // Check property types. 2452 if (!Importer.IsStructurallyEquivalent(D->getType(), 2453 FoundProp->getType())) { 2454 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent) 2455 << Name << D->getType() << FoundProp->getType(); 2456 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) 2457 << FoundProp->getType(); 2458 return 0; 2459 } 2460 2461 // FIXME: Check property attributes, getters, setters, etc.? 2462 2463 // Consider these properties to be equivalent. 2464 Importer.Imported(D, FoundProp); 2465 return FoundProp; 2466 } 2467 } 2468 2469 // Import the type. 2470 QualType T = Importer.Import(D->getType()); 2471 if (T.isNull()) 2472 return 0; 2473 2474 // Create the new property. 2475 ObjCPropertyDecl *ToProperty 2476 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc, 2477 Name.getAsIdentifierInfo(), 2478 Importer.Import(D->getAtLoc()), 2479 T, 2480 D->getPropertyImplementation()); 2481 Importer.Imported(D, ToProperty); 2482 ToProperty->setLexicalDeclContext(LexicalDC); 2483 LexicalDC->addDecl(ToProperty); 2484 2485 ToProperty->setPropertyAttributes(D->getPropertyAttributes()); 2486 ToProperty->setGetterName(Importer.Import(D->getGetterName())); 2487 ToProperty->setSetterName(Importer.Import(D->getSetterName())); 2488 ToProperty->setGetterMethodDecl( 2489 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl()))); 2490 ToProperty->setSetterMethodDecl( 2491 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl()))); 2492 ToProperty->setPropertyIvarDecl( 2493 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl()))); 2494 return ToProperty; 2495} 2496 2497Decl * 2498ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { 2499 // Import the context of this declaration. 2500 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 2501 if (!DC) 2502 return 0; 2503 2504 DeclContext *LexicalDC = DC; 2505 if (D->getDeclContext() != D->getLexicalDeclContext()) { 2506 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 2507 if (!LexicalDC) 2508 return 0; 2509 } 2510 2511 // Import the location of this declaration. 2512 SourceLocation Loc = Importer.Import(D->getLocation()); 2513 2514 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols; 2515 llvm::SmallVector<SourceLocation, 4> Locations; 2516 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc 2517 = D->protocol_loc_begin(); 2518 for (ObjCForwardProtocolDecl::protocol_iterator FromProto 2519 = D->protocol_begin(), FromProtoEnd = D->protocol_end(); 2520 FromProto != FromProtoEnd; 2521 ++FromProto, ++FromProtoLoc) { 2522 ObjCProtocolDecl *ToProto 2523 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 2524 if (!ToProto) 2525 continue; 2526 2527 Protocols.push_back(ToProto); 2528 Locations.push_back(Importer.Import(*FromProtoLoc)); 2529 } 2530 2531 ObjCForwardProtocolDecl *ToForward 2532 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc, 2533 Protocols.data(), Protocols.size(), 2534 Locations.data()); 2535 ToForward->setLexicalDeclContext(LexicalDC); 2536 LexicalDC->addDecl(ToForward); 2537 Importer.Imported(D, ToForward); 2538 return ToForward; 2539} 2540 2541Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) { 2542 // Import the context of this declaration. 2543 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 2544 if (!DC) 2545 return 0; 2546 2547 DeclContext *LexicalDC = DC; 2548 if (D->getDeclContext() != D->getLexicalDeclContext()) { 2549 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 2550 if (!LexicalDC) 2551 return 0; 2552 } 2553 2554 // Import the location of this declaration. 2555 SourceLocation Loc = Importer.Import(D->getLocation()); 2556 2557 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces; 2558 llvm::SmallVector<SourceLocation, 4> Locations; 2559 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end(); 2560 From != FromEnd; ++From) { 2561 ObjCInterfaceDecl *ToIface 2562 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface())); 2563 if (!ToIface) 2564 continue; 2565 2566 Interfaces.push_back(ToIface); 2567 Locations.push_back(Importer.Import(From->getLocation())); 2568 } 2569 2570 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC, 2571 Loc, 2572 Interfaces.data(), 2573 Locations.data(), 2574 Interfaces.size()); 2575 ToClass->setLexicalDeclContext(LexicalDC); 2576 LexicalDC->addDecl(ToClass); 2577 Importer.Imported(D, ToClass); 2578 return ToClass; 2579} 2580 2581//---------------------------------------------------------------------------- 2582// Import Statements 2583//---------------------------------------------------------------------------- 2584 2585Stmt *ASTNodeImporter::VisitStmt(Stmt *S) { 2586 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node) 2587 << S->getStmtClassName(); 2588 return 0; 2589} 2590 2591//---------------------------------------------------------------------------- 2592// Import Expressions 2593//---------------------------------------------------------------------------- 2594Expr *ASTNodeImporter::VisitExpr(Expr *E) { 2595 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node) 2596 << E->getStmtClassName(); 2597 return 0; 2598} 2599 2600Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { 2601 NestedNameSpecifier *Qualifier = 0; 2602 if (E->getQualifier()) { 2603 Qualifier = Importer.Import(E->getQualifier()); 2604 if (!E->getQualifier()) 2605 return 0; 2606 } 2607 2608 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl())); 2609 if (!ToD) 2610 return 0; 2611 2612 QualType T = Importer.Import(E->getType()); 2613 if (T.isNull()) 2614 return 0; 2615 2616 return DeclRefExpr::Create(Importer.getToContext(), Qualifier, 2617 Importer.Import(E->getQualifierRange()), 2618 ToD, 2619 Importer.Import(E->getLocation()), 2620 T, 2621 /*FIXME:TemplateArgs=*/0); 2622} 2623 2624Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { 2625 QualType T = Importer.Import(E->getType()); 2626 if (T.isNull()) 2627 return 0; 2628 2629 return new (Importer.getToContext()) 2630 IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation())); 2631} 2632 2633Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { 2634 QualType T = Importer.Import(E->getType()); 2635 if (T.isNull()) 2636 return 0; 2637 2638 return new (Importer.getToContext()) CharacterLiteral(E->getValue(), 2639 E->isWide(), T, 2640 Importer.Import(E->getLocation())); 2641} 2642 2643Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) { 2644 Expr *SubExpr = Importer.Import(E->getSubExpr()); 2645 if (!SubExpr) 2646 return 0; 2647 2648 return new (Importer.getToContext()) 2649 ParenExpr(Importer.Import(E->getLParen()), 2650 Importer.Import(E->getRParen()), 2651 SubExpr); 2652} 2653 2654Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { 2655 QualType T = Importer.Import(E->getType()); 2656 if (T.isNull()) 2657 return 0; 2658 2659 Expr *SubExpr = Importer.Import(E->getSubExpr()); 2660 if (!SubExpr) 2661 return 0; 2662 2663 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(), 2664 T, 2665 Importer.Import(E->getOperatorLoc())); 2666} 2667 2668Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { 2669 QualType ResultType = Importer.Import(E->getType()); 2670 2671 if (E->isArgumentType()) { 2672 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo()); 2673 if (!TInfo) 2674 return 0; 2675 2676 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(), 2677 TInfo, ResultType, 2678 Importer.Import(E->getOperatorLoc()), 2679 Importer.Import(E->getRParenLoc())); 2680 } 2681 2682 Expr *SubExpr = Importer.Import(E->getArgumentExpr()); 2683 if (!SubExpr) 2684 return 0; 2685 2686 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(), 2687 SubExpr, ResultType, 2688 Importer.Import(E->getOperatorLoc()), 2689 Importer.Import(E->getRParenLoc())); 2690} 2691 2692Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { 2693 QualType T = Importer.Import(E->getType()); 2694 if (T.isNull()) 2695 return 0; 2696 2697 Expr *LHS = Importer.Import(E->getLHS()); 2698 if (!LHS) 2699 return 0; 2700 2701 Expr *RHS = Importer.Import(E->getRHS()); 2702 if (!RHS) 2703 return 0; 2704 2705 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(), 2706 T, 2707 Importer.Import(E->getOperatorLoc())); 2708} 2709 2710Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { 2711 QualType T = Importer.Import(E->getType()); 2712 if (T.isNull()) 2713 return 0; 2714 2715 QualType CompLHSType = Importer.Import(E->getComputationLHSType()); 2716 if (CompLHSType.isNull()) 2717 return 0; 2718 2719 QualType CompResultType = Importer.Import(E->getComputationResultType()); 2720 if (CompResultType.isNull()) 2721 return 0; 2722 2723 Expr *LHS = Importer.Import(E->getLHS()); 2724 if (!LHS) 2725 return 0; 2726 2727 Expr *RHS = Importer.Import(E->getRHS()); 2728 if (!RHS) 2729 return 0; 2730 2731 return new (Importer.getToContext()) 2732 CompoundAssignOperator(LHS, RHS, E->getOpcode(), 2733 T, CompLHSType, CompResultType, 2734 Importer.Import(E->getOperatorLoc())); 2735} 2736 2737Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { 2738 QualType T = Importer.Import(E->getType()); 2739 if (T.isNull()) 2740 return 0; 2741 2742 Expr *SubExpr = Importer.Import(E->getSubExpr()); 2743 if (!SubExpr) 2744 return 0; 2745 2746 return new (Importer.getToContext()) ImplicitCastExpr(T, E->getCastKind(), 2747 SubExpr, 2748 E->isLvalueCast()); 2749} 2750 2751Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) { 2752 QualType T = Importer.Import(E->getType()); 2753 if (T.isNull()) 2754 return 0; 2755 2756 Expr *SubExpr = Importer.Import(E->getSubExpr()); 2757 if (!SubExpr) 2758 return 0; 2759 2760 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten()); 2761 if (!TInfo && E->getTypeInfoAsWritten()) 2762 return 0; 2763 2764 return new (Importer.getToContext()) CStyleCastExpr(T, E->getCastKind(), 2765 SubExpr, TInfo, 2766 Importer.Import(E->getLParenLoc()), 2767 Importer.Import(E->getRParenLoc())); 2768} 2769 2770ASTImporter::ASTImporter(Diagnostic &Diags, 2771 ASTContext &ToContext, FileManager &ToFileManager, 2772 ASTContext &FromContext, FileManager &FromFileManager) 2773 : ToContext(ToContext), FromContext(FromContext), 2774 ToFileManager(ToFileManager), FromFileManager(FromFileManager), 2775 Diags(Diags) { 2776 ImportedDecls[FromContext.getTranslationUnitDecl()] 2777 = ToContext.getTranslationUnitDecl(); 2778} 2779 2780ASTImporter::~ASTImporter() { } 2781 2782QualType ASTImporter::Import(QualType FromT) { 2783 if (FromT.isNull()) 2784 return QualType(); 2785 2786 // Check whether we've already imported this type. 2787 llvm::DenseMap<Type *, Type *>::iterator Pos 2788 = ImportedTypes.find(FromT.getTypePtr()); 2789 if (Pos != ImportedTypes.end()) 2790 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers()); 2791 2792 // Import the type 2793 ASTNodeImporter Importer(*this); 2794 QualType ToT = Importer.Visit(FromT.getTypePtr()); 2795 if (ToT.isNull()) 2796 return ToT; 2797 2798 // Record the imported type. 2799 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr(); 2800 2801 return ToContext.getQualifiedType(ToT, FromT.getQualifiers()); 2802} 2803 2804TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { 2805 if (!FromTSI) 2806 return FromTSI; 2807 2808 // FIXME: For now we just create a "trivial" type source info based 2809 // on the type and a seingle location. Implement a real version of 2810 // this. 2811 QualType T = Import(FromTSI->getType()); 2812 if (T.isNull()) 2813 return 0; 2814 2815 return ToContext.getTrivialTypeSourceInfo(T, 2816 FromTSI->getTypeLoc().getFullSourceRange().getBegin()); 2817} 2818 2819Decl *ASTImporter::Import(Decl *FromD) { 2820 if (!FromD) 2821 return 0; 2822 2823 // Check whether we've already imported this declaration. 2824 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); 2825 if (Pos != ImportedDecls.end()) 2826 return Pos->second; 2827 2828 // Import the type 2829 ASTNodeImporter Importer(*this); 2830 Decl *ToD = Importer.Visit(FromD); 2831 if (!ToD) 2832 return 0; 2833 2834 // Record the imported declaration. 2835 ImportedDecls[FromD] = ToD; 2836 2837 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) { 2838 // Keep track of anonymous tags that have an associated typedef. 2839 if (FromTag->getTypedefForAnonDecl()) 2840 AnonTagsWithPendingTypedefs.push_back(FromTag); 2841 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) { 2842 // When we've finished transforming a typedef, see whether it was the 2843 // typedef for an anonymous tag. 2844 for (llvm::SmallVector<TagDecl *, 4>::iterator 2845 FromTag = AnonTagsWithPendingTypedefs.begin(), 2846 FromTagEnd = AnonTagsWithPendingTypedefs.end(); 2847 FromTag != FromTagEnd; ++FromTag) { 2848 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) { 2849 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) { 2850 // We found the typedef for an anonymous tag; link them. 2851 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD)); 2852 AnonTagsWithPendingTypedefs.erase(FromTag); 2853 break; 2854 } 2855 } 2856 } 2857 } 2858 2859 return ToD; 2860} 2861 2862DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) { 2863 if (!FromDC) 2864 return FromDC; 2865 2866 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC))); 2867} 2868 2869Expr *ASTImporter::Import(Expr *FromE) { 2870 if (!FromE) 2871 return 0; 2872 2873 return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); 2874} 2875 2876Stmt *ASTImporter::Import(Stmt *FromS) { 2877 if (!FromS) 2878 return 0; 2879 2880 // Check whether we've already imported this declaration. 2881 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); 2882 if (Pos != ImportedStmts.end()) 2883 return Pos->second; 2884 2885 // Import the type 2886 ASTNodeImporter Importer(*this); 2887 Stmt *ToS = Importer.Visit(FromS); 2888 if (!ToS) 2889 return 0; 2890 2891 // Record the imported declaration. 2892 ImportedStmts[FromS] = ToS; 2893 return ToS; 2894} 2895 2896NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { 2897 if (!FromNNS) 2898 return 0; 2899 2900 // FIXME: Implement! 2901 return 0; 2902} 2903 2904SourceLocation ASTImporter::Import(SourceLocation FromLoc) { 2905 if (FromLoc.isInvalid()) 2906 return SourceLocation(); 2907 2908 SourceManager &FromSM = FromContext.getSourceManager(); 2909 2910 // For now, map everything down to its spelling location, so that we 2911 // don't have to import macro instantiations. 2912 // FIXME: Import macro instantiations! 2913 FromLoc = FromSM.getSpellingLoc(FromLoc); 2914 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); 2915 SourceManager &ToSM = ToContext.getSourceManager(); 2916 return ToSM.getLocForStartOfFile(Import(Decomposed.first)) 2917 .getFileLocWithOffset(Decomposed.second); 2918} 2919 2920SourceRange ASTImporter::Import(SourceRange FromRange) { 2921 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); 2922} 2923 2924FileID ASTImporter::Import(FileID FromID) { 2925 llvm::DenseMap<unsigned, FileID>::iterator Pos 2926 = ImportedFileIDs.find(FromID.getHashValue()); 2927 if (Pos != ImportedFileIDs.end()) 2928 return Pos->second; 2929 2930 SourceManager &FromSM = FromContext.getSourceManager(); 2931 SourceManager &ToSM = ToContext.getSourceManager(); 2932 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); 2933 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet"); 2934 2935 // Include location of this file. 2936 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); 2937 2938 // Map the FileID for to the "to" source manager. 2939 FileID ToID; 2940 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); 2941 if (Cache->Entry) { 2942 // FIXME: We probably want to use getVirtualFile(), so we don't hit the 2943 // disk again 2944 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather 2945 // than mmap the files several times. 2946 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName()); 2947 ToID = ToSM.createFileID(Entry, ToIncludeLoc, 2948 FromSLoc.getFile().getFileCharacteristic()); 2949 } else { 2950 // FIXME: We want to re-use the existing MemoryBuffer! 2951 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(); 2952 llvm::MemoryBuffer *ToBuf 2953 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBufferStart(), 2954 FromBuf->getBufferEnd(), 2955 FromBuf->getBufferIdentifier()); 2956 ToID = ToSM.createFileIDForMemBuffer(ToBuf); 2957 } 2958 2959 2960 ImportedFileIDs[FromID.getHashValue()] = ToID; 2961 return ToID; 2962} 2963 2964DeclarationName ASTImporter::Import(DeclarationName FromName) { 2965 if (!FromName) 2966 return DeclarationName(); 2967 2968 switch (FromName.getNameKind()) { 2969 case DeclarationName::Identifier: 2970 return Import(FromName.getAsIdentifierInfo()); 2971 2972 case DeclarationName::ObjCZeroArgSelector: 2973 case DeclarationName::ObjCOneArgSelector: 2974 case DeclarationName::ObjCMultiArgSelector: 2975 return Import(FromName.getObjCSelector()); 2976 2977 case DeclarationName::CXXConstructorName: { 2978 QualType T = Import(FromName.getCXXNameType()); 2979 if (T.isNull()) 2980 return DeclarationName(); 2981 2982 return ToContext.DeclarationNames.getCXXConstructorName( 2983 ToContext.getCanonicalType(T)); 2984 } 2985 2986 case DeclarationName::CXXDestructorName: { 2987 QualType T = Import(FromName.getCXXNameType()); 2988 if (T.isNull()) 2989 return DeclarationName(); 2990 2991 return ToContext.DeclarationNames.getCXXDestructorName( 2992 ToContext.getCanonicalType(T)); 2993 } 2994 2995 case DeclarationName::CXXConversionFunctionName: { 2996 QualType T = Import(FromName.getCXXNameType()); 2997 if (T.isNull()) 2998 return DeclarationName(); 2999 3000 return ToContext.DeclarationNames.getCXXConversionFunctionName( 3001 ToContext.getCanonicalType(T)); 3002 } 3003 3004 case DeclarationName::CXXOperatorName: 3005 return ToContext.DeclarationNames.getCXXOperatorName( 3006 FromName.getCXXOverloadedOperator()); 3007 3008 case DeclarationName::CXXLiteralOperatorName: 3009 return ToContext.DeclarationNames.getCXXLiteralOperatorName( 3010 Import(FromName.getCXXLiteralIdentifier())); 3011 3012 case DeclarationName::CXXUsingDirective: 3013 // FIXME: STATICS! 3014 return DeclarationName::getUsingDirectiveName(); 3015 } 3016 3017 // Silence bogus GCC warning 3018 return DeclarationName(); 3019} 3020 3021IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) { 3022 if (!FromId) 3023 return 0; 3024 3025 return &ToContext.Idents.get(FromId->getName()); 3026} 3027 3028Selector ASTImporter::Import(Selector FromSel) { 3029 if (FromSel.isNull()) 3030 return Selector(); 3031 3032 llvm::SmallVector<IdentifierInfo *, 4> Idents; 3033 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); 3034 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) 3035 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); 3036 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); 3037} 3038 3039DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name, 3040 DeclContext *DC, 3041 unsigned IDNS, 3042 NamedDecl **Decls, 3043 unsigned NumDecls) { 3044 return Name; 3045} 3046 3047DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { 3048 return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()), 3049 DiagID); 3050} 3051 3052DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { 3053 return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()), 3054 DiagID); 3055} 3056 3057Decl *ASTImporter::Imported(Decl *From, Decl *To) { 3058 ImportedDecls[From] = To; 3059 return To; 3060} 3061 3062bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) { 3063 llvm::DenseMap<Type *, Type *>::iterator Pos 3064 = ImportedTypes.find(From.getTypePtr()); 3065 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To)) 3066 return true; 3067 3068 StructuralEquivalenceContext Ctx(FromContext, ToContext, Diags, 3069 NonEquivalentDecls); 3070 return Ctx.IsStructurallyEquivalent(From, To); 3071} 3072