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