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