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