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