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