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 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/ASTDiagnostic.h" 17 #include "clang/AST/DeclCXX.h" 18 #include "clang/AST/DeclObjC.h" 19 #include "clang/AST/DeclVisitor.h" 20 #include "clang/AST/StmtVisitor.h" 21 #include "clang/AST/TypeVisitor.h" 22 #include "clang/Basic/FileManager.h" 23 #include "clang/Basic/SourceManager.h" 24 #include "llvm/Support/MemoryBuffer.h" 25 #include <deque> 26 27 namespace clang { 28 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>, 29 public DeclVisitor<ASTNodeImporter, Decl *>, 30 public StmtVisitor<ASTNodeImporter, Stmt *> { 31 ASTImporter &Importer; 32 33 public: 34 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { } 35 36 using TypeVisitor<ASTNodeImporter, QualType>::Visit; 37 using DeclVisitor<ASTNodeImporter, Decl *>::Visit; 38 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit; 39 40 // Importing types 41 QualType VisitType(const Type *T); 42 QualType VisitBuiltinType(const BuiltinType *T); 43 QualType VisitComplexType(const ComplexType *T); 44 QualType VisitPointerType(const PointerType *T); 45 QualType VisitBlockPointerType(const BlockPointerType *T); 46 QualType VisitLValueReferenceType(const LValueReferenceType *T); 47 QualType VisitRValueReferenceType(const RValueReferenceType *T); 48 QualType VisitMemberPointerType(const MemberPointerType *T); 49 QualType VisitConstantArrayType(const ConstantArrayType *T); 50 QualType VisitIncompleteArrayType(const IncompleteArrayType *T); 51 QualType VisitVariableArrayType(const VariableArrayType *T); 52 // FIXME: DependentSizedArrayType 53 // FIXME: DependentSizedExtVectorType 54 QualType VisitVectorType(const VectorType *T); 55 QualType VisitExtVectorType(const ExtVectorType *T); 56 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T); 57 QualType VisitFunctionProtoType(const FunctionProtoType *T); 58 // FIXME: UnresolvedUsingType 59 QualType VisitParenType(const ParenType *T); 60 QualType VisitTypedefType(const TypedefType *T); 61 QualType VisitTypeOfExprType(const TypeOfExprType *T); 62 // FIXME: DependentTypeOfExprType 63 QualType VisitTypeOfType(const TypeOfType *T); 64 QualType VisitDecltypeType(const DecltypeType *T); 65 QualType VisitUnaryTransformType(const UnaryTransformType *T); 66 QualType VisitAutoType(const AutoType *T); 67 QualType VisitInjectedClassNameType(const InjectedClassNameType *T); 68 // FIXME: DependentDecltypeType 69 QualType VisitRecordType(const RecordType *T); 70 QualType VisitEnumType(const EnumType *T); 71 QualType VisitAttributedType(const AttributedType *T); 72 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T); 73 // FIXME: SubstTemplateTypeParmType 74 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T); 75 QualType VisitElaboratedType(const ElaboratedType *T); 76 // FIXME: DependentNameType 77 // FIXME: DependentTemplateSpecializationType 78 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T); 79 QualType VisitObjCObjectType(const ObjCObjectType *T); 80 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T); 81 82 // Importing declarations 83 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC, 84 DeclContext *&LexicalDC, DeclarationName &Name, 85 NamedDecl *&ToD, SourceLocation &Loc); 86 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); 87 void ImportDeclarationNameLoc(const DeclarationNameInfo &From, 88 DeclarationNameInfo& To); 89 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); 90 91 typedef DesignatedInitExpr::Designator Designator; 92 Designator ImportDesignator(const Designator &D); 93 94 95 /// \brief What we should import from the definition. 96 enum ImportDefinitionKind { 97 /// \brief Import the default subset of the definition, which might be 98 /// nothing (if minimal import is set) or might be everything (if minimal 99 /// import is not set). 100 IDK_Default, 101 /// \brief Import everything. 102 IDK_Everything, 103 /// \brief Import only the bare bones needed to establish a valid 104 /// DeclContext. 105 IDK_Basic 106 }; 107 108 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { 109 return IDK == IDK_Everything || 110 (IDK == IDK_Default && !Importer.isMinimalImport()); 111 } 112 113 bool ImportDefinition(RecordDecl *From, RecordDecl *To, 114 ImportDefinitionKind Kind = IDK_Default); 115 bool ImportDefinition(VarDecl *From, VarDecl *To, 116 ImportDefinitionKind Kind = IDK_Default); 117 bool ImportDefinition(EnumDecl *From, EnumDecl *To, 118 ImportDefinitionKind Kind = IDK_Default); 119 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, 120 ImportDefinitionKind Kind = IDK_Default); 121 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To, 122 ImportDefinitionKind Kind = IDK_Default); 123 TemplateParameterList *ImportTemplateParameterList( 124 TemplateParameterList *Params); 125 TemplateArgument ImportTemplateArgument(const TemplateArgument &From); 126 bool ImportTemplateArguments(const TemplateArgument *FromArgs, 127 unsigned NumFromArgs, 128 SmallVectorImpl<TemplateArgument> &ToArgs); 129 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord, 130 bool Complain = true); 131 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, 132 bool Complain = true); 133 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord); 134 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC); 135 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To); 136 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To); 137 Decl *VisitDecl(Decl *D); 138 Decl *VisitAccessSpecDecl(AccessSpecDecl *D); 139 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); 140 Decl *VisitNamespaceDecl(NamespaceDecl *D); 141 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); 142 Decl *VisitTypedefDecl(TypedefDecl *D); 143 Decl *VisitTypeAliasDecl(TypeAliasDecl *D); 144 Decl *VisitLabelDecl(LabelDecl *D); 145 Decl *VisitEnumDecl(EnumDecl *D); 146 Decl *VisitRecordDecl(RecordDecl *D); 147 Decl *VisitEnumConstantDecl(EnumConstantDecl *D); 148 Decl *VisitFunctionDecl(FunctionDecl *D); 149 Decl *VisitCXXMethodDecl(CXXMethodDecl *D); 150 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); 151 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); 152 Decl *VisitCXXConversionDecl(CXXConversionDecl *D); 153 Decl *VisitFieldDecl(FieldDecl *D); 154 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D); 155 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D); 156 Decl *VisitVarDecl(VarDecl *D); 157 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D); 158 Decl *VisitParmVarDecl(ParmVarDecl *D); 159 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D); 160 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); 161 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D); 162 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D); 163 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D); 164 165 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list); 166 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); 167 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); 168 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D); 169 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D); 170 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); 171 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); 172 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); 173 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); 174 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); 175 Decl *VisitClassTemplateSpecializationDecl( 176 ClassTemplateSpecializationDecl *D); 177 Decl *VisitVarTemplateDecl(VarTemplateDecl *D); 178 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); 179 180 // Importing statements 181 DeclGroupRef ImportDeclGroup(DeclGroupRef DG); 182 183 Stmt *VisitStmt(Stmt *S); 184 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S); 185 Stmt *VisitDeclStmt(DeclStmt *S); 186 Stmt *VisitNullStmt(NullStmt *S); 187 Stmt *VisitCompoundStmt(CompoundStmt *S); 188 Stmt *VisitCaseStmt(CaseStmt *S); 189 Stmt *VisitDefaultStmt(DefaultStmt *S); 190 Stmt *VisitLabelStmt(LabelStmt *S); 191 Stmt *VisitAttributedStmt(AttributedStmt *S); 192 Stmt *VisitIfStmt(IfStmt *S); 193 Stmt *VisitSwitchStmt(SwitchStmt *S); 194 Stmt *VisitWhileStmt(WhileStmt *S); 195 Stmt *VisitDoStmt(DoStmt *S); 196 Stmt *VisitForStmt(ForStmt *S); 197 Stmt *VisitGotoStmt(GotoStmt *S); 198 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S); 199 Stmt *VisitContinueStmt(ContinueStmt *S); 200 Stmt *VisitBreakStmt(BreakStmt *S); 201 Stmt *VisitReturnStmt(ReturnStmt *S); 202 // FIXME: MSAsmStmt 203 // FIXME: SEHExceptStmt 204 // FIXME: SEHFinallyStmt 205 // FIXME: SEHTryStmt 206 // FIXME: SEHLeaveStmt 207 // FIXME: CapturedStmt 208 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S); 209 Stmt *VisitCXXTryStmt(CXXTryStmt *S); 210 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S); 211 // FIXME: MSDependentExistsStmt 212 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); 213 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); 214 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S); 215 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S); 216 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); 217 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); 218 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); 219 220 // Importing expressions 221 Expr *VisitExpr(Expr *E); 222 Expr *VisitVAArgExpr(VAArgExpr *E); 223 Expr *VisitGNUNullExpr(GNUNullExpr *E); 224 Expr *VisitPredefinedExpr(PredefinedExpr *E); 225 Expr *VisitDeclRefExpr(DeclRefExpr *E); 226 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE); 227 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E); 228 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); 229 Expr *VisitIntegerLiteral(IntegerLiteral *E); 230 Expr *VisitFloatingLiteral(FloatingLiteral *E); 231 Expr *VisitCharacterLiteral(CharacterLiteral *E); 232 Expr *VisitStringLiteral(StringLiteral *E); 233 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E); 234 Expr *VisitAtomicExpr(AtomicExpr *E); 235 Expr *VisitAddrLabelExpr(AddrLabelExpr *E); 236 Expr *VisitParenExpr(ParenExpr *E); 237 Expr *VisitParenListExpr(ParenListExpr *E); 238 Expr *VisitStmtExpr(StmtExpr *E); 239 Expr *VisitUnaryOperator(UnaryOperator *E); 240 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); 241 Expr *VisitBinaryOperator(BinaryOperator *E); 242 Expr *VisitConditionalOperator(ConditionalOperator *E); 243 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E); 244 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E); 245 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E); 246 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E); 247 Expr *VisitCStyleCastExpr(CStyleCastExpr *E); 248 Expr *VisitCXXConstructExpr(CXXConstructExpr *E); 249 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E); 250 Expr *VisitCXXThisExpr(CXXThisExpr *E); 251 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); 252 Expr *VisitMemberExpr(MemberExpr *E); 253 Expr *VisitCallExpr(CallExpr *E); 254 Expr *VisitInitListExpr(InitListExpr *E); 255 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E); 256 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E); 257 258 template<typename IIter, typename OIter> 259 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) { 260 typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT; 261 ASTImporter &ImporterRef = Importer; 262 std::transform(Ibegin, Iend, Obegin, 263 [&ImporterRef](ItemT From) -> ItemT { 264 return ImporterRef.Import(From); 265 }); 266 } 267 268 template<typename IIter, typename OIter> 269 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) { 270 typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT; 271 ASTImporter &ImporterRef = Importer; 272 bool Failed = false; 273 std::transform(Ibegin, Iend, Obegin, 274 [&ImporterRef, &Failed](ItemT *From) -> ItemT * { 275 ItemT *To = ImporterRef.Import(From); 276 if (!To && From) 277 Failed = true; 278 return To; 279 }); 280 return Failed; 281 } 282 }; 283 } 284 285 using namespace clang; 286 287 //---------------------------------------------------------------------------- 288 // Structural Equivalence 289 //---------------------------------------------------------------------------- 290 291 namespace { 292 struct StructuralEquivalenceContext { 293 /// \brief AST contexts for which we are checking structural equivalence. 294 ASTContext &C1, &C2; 295 296 /// \brief The set of "tentative" equivalences between two canonical 297 /// declarations, mapping from a declaration in the first context to the 298 /// declaration in the second context that we believe to be equivalent. 299 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences; 300 301 /// \brief Queue of declarations in the first context whose equivalence 302 /// with a declaration in the second context still needs to be verified. 303 std::deque<Decl *> DeclsToCheck; 304 305 /// \brief Declaration (from, to) pairs that are known not to be equivalent 306 /// (which we have already complained about). 307 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls; 308 309 /// \brief Whether we're being strict about the spelling of types when 310 /// unifying two types. 311 bool StrictTypeSpelling; 312 313 /// \brief Whether to complain about failures. 314 bool Complain; 315 316 /// \brief \c true if the last diagnostic came from C2. 317 bool LastDiagFromC2; 318 319 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2, 320 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls, 321 bool StrictTypeSpelling = false, 322 bool Complain = true) 323 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls), 324 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain), 325 LastDiagFromC2(false) {} 326 327 /// \brief Determine whether the two declarations are structurally 328 /// equivalent. 329 bool IsStructurallyEquivalent(Decl *D1, Decl *D2); 330 331 /// \brief Determine whether the two types are structurally equivalent. 332 bool IsStructurallyEquivalent(QualType T1, QualType T2); 333 334 private: 335 /// \brief Finish checking all of the structural equivalences. 336 /// 337 /// \returns true if an error occurred, false otherwise. 338 bool Finish(); 339 340 public: 341 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) { 342 assert(Complain && "Not allowed to complain"); 343 if (LastDiagFromC2) 344 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics()); 345 LastDiagFromC2 = false; 346 return C1.getDiagnostics().Report(Loc, DiagID); 347 } 348 349 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) { 350 assert(Complain && "Not allowed to complain"); 351 if (!LastDiagFromC2) 352 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics()); 353 LastDiagFromC2 = true; 354 return C2.getDiagnostics().Report(Loc, DiagID); 355 } 356 }; 357 } 358 359 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 360 QualType T1, QualType T2); 361 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 362 Decl *D1, Decl *D2); 363 364 /// \brief Determine structural equivalence of two expressions. 365 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 366 Expr *E1, Expr *E2) { 367 if (!E1 || !E2) 368 return E1 == E2; 369 370 // FIXME: Actually perform a structural comparison! 371 return true; 372 } 373 374 /// \brief Determine whether two identifiers are equivalent. 375 static bool IsStructurallyEquivalent(const IdentifierInfo *Name1, 376 const IdentifierInfo *Name2) { 377 if (!Name1 || !Name2) 378 return Name1 == Name2; 379 380 return Name1->getName() == Name2->getName(); 381 } 382 383 /// \brief Determine whether two nested-name-specifiers are equivalent. 384 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 385 NestedNameSpecifier *NNS1, 386 NestedNameSpecifier *NNS2) { 387 // FIXME: Implement! 388 return true; 389 } 390 391 /// \brief Determine whether two template arguments are equivalent. 392 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 393 const TemplateArgument &Arg1, 394 const TemplateArgument &Arg2) { 395 if (Arg1.getKind() != Arg2.getKind()) 396 return false; 397 398 switch (Arg1.getKind()) { 399 case TemplateArgument::Null: 400 return true; 401 402 case TemplateArgument::Type: 403 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType()); 404 405 case TemplateArgument::Integral: 406 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(), 407 Arg2.getIntegralType())) 408 return false; 409 410 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral()); 411 412 case TemplateArgument::Declaration: 413 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl()); 414 415 case TemplateArgument::NullPtr: 416 return true; // FIXME: Is this correct? 417 418 case TemplateArgument::Template: 419 return IsStructurallyEquivalent(Context, 420 Arg1.getAsTemplate(), 421 Arg2.getAsTemplate()); 422 423 case TemplateArgument::TemplateExpansion: 424 return IsStructurallyEquivalent(Context, 425 Arg1.getAsTemplateOrTemplatePattern(), 426 Arg2.getAsTemplateOrTemplatePattern()); 427 428 case TemplateArgument::Expression: 429 return IsStructurallyEquivalent(Context, 430 Arg1.getAsExpr(), Arg2.getAsExpr()); 431 432 case TemplateArgument::Pack: 433 if (Arg1.pack_size() != Arg2.pack_size()) 434 return false; 435 436 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I) 437 if (!IsStructurallyEquivalent(Context, 438 Arg1.pack_begin()[I], 439 Arg2.pack_begin()[I])) 440 return false; 441 442 return true; 443 } 444 445 llvm_unreachable("Invalid template argument kind"); 446 } 447 448 /// \brief Determine structural equivalence for the common part of array 449 /// types. 450 static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context, 451 const ArrayType *Array1, 452 const ArrayType *Array2) { 453 if (!IsStructurallyEquivalent(Context, 454 Array1->getElementType(), 455 Array2->getElementType())) 456 return false; 457 if (Array1->getSizeModifier() != Array2->getSizeModifier()) 458 return false; 459 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers()) 460 return false; 461 462 return true; 463 } 464 465 /// \brief Determine structural equivalence of two types. 466 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 467 QualType T1, QualType T2) { 468 if (T1.isNull() || T2.isNull()) 469 return T1.isNull() && T2.isNull(); 470 471 if (!Context.StrictTypeSpelling) { 472 // We aren't being strict about token-to-token equivalence of types, 473 // so map down to the canonical type. 474 T1 = Context.C1.getCanonicalType(T1); 475 T2 = Context.C2.getCanonicalType(T2); 476 } 477 478 if (T1.getQualifiers() != T2.getQualifiers()) 479 return false; 480 481 Type::TypeClass TC = T1->getTypeClass(); 482 483 if (T1->getTypeClass() != T2->getTypeClass()) { 484 // Compare function types with prototypes vs. without prototypes as if 485 // both did not have prototypes. 486 if (T1->getTypeClass() == Type::FunctionProto && 487 T2->getTypeClass() == Type::FunctionNoProto) 488 TC = Type::FunctionNoProto; 489 else if (T1->getTypeClass() == Type::FunctionNoProto && 490 T2->getTypeClass() == Type::FunctionProto) 491 TC = Type::FunctionNoProto; 492 else 493 return false; 494 } 495 496 switch (TC) { 497 case Type::Builtin: 498 // FIXME: Deal with Char_S/Char_U. 499 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind()) 500 return false; 501 break; 502 503 case Type::Complex: 504 if (!IsStructurallyEquivalent(Context, 505 cast<ComplexType>(T1)->getElementType(), 506 cast<ComplexType>(T2)->getElementType())) 507 return false; 508 break; 509 510 case Type::Adjusted: 511 case Type::Decayed: 512 if (!IsStructurallyEquivalent(Context, 513 cast<AdjustedType>(T1)->getOriginalType(), 514 cast<AdjustedType>(T2)->getOriginalType())) 515 return false; 516 break; 517 518 case Type::Pointer: 519 if (!IsStructurallyEquivalent(Context, 520 cast<PointerType>(T1)->getPointeeType(), 521 cast<PointerType>(T2)->getPointeeType())) 522 return false; 523 break; 524 525 case Type::BlockPointer: 526 if (!IsStructurallyEquivalent(Context, 527 cast<BlockPointerType>(T1)->getPointeeType(), 528 cast<BlockPointerType>(T2)->getPointeeType())) 529 return false; 530 break; 531 532 case Type::LValueReference: 533 case Type::RValueReference: { 534 const ReferenceType *Ref1 = cast<ReferenceType>(T1); 535 const ReferenceType *Ref2 = cast<ReferenceType>(T2); 536 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue()) 537 return false; 538 if (Ref1->isInnerRef() != Ref2->isInnerRef()) 539 return false; 540 if (!IsStructurallyEquivalent(Context, 541 Ref1->getPointeeTypeAsWritten(), 542 Ref2->getPointeeTypeAsWritten())) 543 return false; 544 break; 545 } 546 547 case Type::MemberPointer: { 548 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1); 549 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2); 550 if (!IsStructurallyEquivalent(Context, 551 MemPtr1->getPointeeType(), 552 MemPtr2->getPointeeType())) 553 return false; 554 if (!IsStructurallyEquivalent(Context, 555 QualType(MemPtr1->getClass(), 0), 556 QualType(MemPtr2->getClass(), 0))) 557 return false; 558 break; 559 } 560 561 case Type::ConstantArray: { 562 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1); 563 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2); 564 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize())) 565 return false; 566 567 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) 568 return false; 569 break; 570 } 571 572 case Type::IncompleteArray: 573 if (!IsArrayStructurallyEquivalent(Context, 574 cast<ArrayType>(T1), 575 cast<ArrayType>(T2))) 576 return false; 577 break; 578 579 case Type::VariableArray: { 580 const VariableArrayType *Array1 = cast<VariableArrayType>(T1); 581 const VariableArrayType *Array2 = cast<VariableArrayType>(T2); 582 if (!IsStructurallyEquivalent(Context, 583 Array1->getSizeExpr(), Array2->getSizeExpr())) 584 return false; 585 586 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) 587 return false; 588 589 break; 590 } 591 592 case Type::DependentSizedArray: { 593 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1); 594 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2); 595 if (!IsStructurallyEquivalent(Context, 596 Array1->getSizeExpr(), Array2->getSizeExpr())) 597 return false; 598 599 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) 600 return false; 601 602 break; 603 } 604 605 case Type::DependentSizedExtVector: { 606 const DependentSizedExtVectorType *Vec1 607 = cast<DependentSizedExtVectorType>(T1); 608 const DependentSizedExtVectorType *Vec2 609 = cast<DependentSizedExtVectorType>(T2); 610 if (!IsStructurallyEquivalent(Context, 611 Vec1->getSizeExpr(), Vec2->getSizeExpr())) 612 return false; 613 if (!IsStructurallyEquivalent(Context, 614 Vec1->getElementType(), 615 Vec2->getElementType())) 616 return false; 617 break; 618 } 619 620 case Type::Vector: 621 case Type::ExtVector: { 622 const VectorType *Vec1 = cast<VectorType>(T1); 623 const VectorType *Vec2 = cast<VectorType>(T2); 624 if (!IsStructurallyEquivalent(Context, 625 Vec1->getElementType(), 626 Vec2->getElementType())) 627 return false; 628 if (Vec1->getNumElements() != Vec2->getNumElements()) 629 return false; 630 if (Vec1->getVectorKind() != Vec2->getVectorKind()) 631 return false; 632 break; 633 } 634 635 case Type::FunctionProto: { 636 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1); 637 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2); 638 if (Proto1->getNumParams() != Proto2->getNumParams()) 639 return false; 640 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) { 641 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I), 642 Proto2->getParamType(I))) 643 return false; 644 } 645 if (Proto1->isVariadic() != Proto2->isVariadic()) 646 return false; 647 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType()) 648 return false; 649 if (Proto1->getExceptionSpecType() == EST_Dynamic) { 650 if (Proto1->getNumExceptions() != Proto2->getNumExceptions()) 651 return false; 652 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) { 653 if (!IsStructurallyEquivalent(Context, 654 Proto1->getExceptionType(I), 655 Proto2->getExceptionType(I))) 656 return false; 657 } 658 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) { 659 if (!IsStructurallyEquivalent(Context, 660 Proto1->getNoexceptExpr(), 661 Proto2->getNoexceptExpr())) 662 return false; 663 } 664 if (Proto1->getTypeQuals() != Proto2->getTypeQuals()) 665 return false; 666 667 // Fall through to check the bits common with FunctionNoProtoType. 668 } 669 670 case Type::FunctionNoProto: { 671 const FunctionType *Function1 = cast<FunctionType>(T1); 672 const FunctionType *Function2 = cast<FunctionType>(T2); 673 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(), 674 Function2->getReturnType())) 675 return false; 676 if (Function1->getExtInfo() != Function2->getExtInfo()) 677 return false; 678 break; 679 } 680 681 case Type::UnresolvedUsing: 682 if (!IsStructurallyEquivalent(Context, 683 cast<UnresolvedUsingType>(T1)->getDecl(), 684 cast<UnresolvedUsingType>(T2)->getDecl())) 685 return false; 686 687 break; 688 689 case Type::Attributed: 690 if (!IsStructurallyEquivalent(Context, 691 cast<AttributedType>(T1)->getModifiedType(), 692 cast<AttributedType>(T2)->getModifiedType())) 693 return false; 694 if (!IsStructurallyEquivalent(Context, 695 cast<AttributedType>(T1)->getEquivalentType(), 696 cast<AttributedType>(T2)->getEquivalentType())) 697 return false; 698 break; 699 700 case Type::Paren: 701 if (!IsStructurallyEquivalent(Context, 702 cast<ParenType>(T1)->getInnerType(), 703 cast<ParenType>(T2)->getInnerType())) 704 return false; 705 break; 706 707 case Type::Typedef: 708 if (!IsStructurallyEquivalent(Context, 709 cast<TypedefType>(T1)->getDecl(), 710 cast<TypedefType>(T2)->getDecl())) 711 return false; 712 break; 713 714 case Type::TypeOfExpr: 715 if (!IsStructurallyEquivalent(Context, 716 cast<TypeOfExprType>(T1)->getUnderlyingExpr(), 717 cast<TypeOfExprType>(T2)->getUnderlyingExpr())) 718 return false; 719 break; 720 721 case Type::TypeOf: 722 if (!IsStructurallyEquivalent(Context, 723 cast<TypeOfType>(T1)->getUnderlyingType(), 724 cast<TypeOfType>(T2)->getUnderlyingType())) 725 return false; 726 break; 727 728 case Type::UnaryTransform: 729 if (!IsStructurallyEquivalent(Context, 730 cast<UnaryTransformType>(T1)->getUnderlyingType(), 731 cast<UnaryTransformType>(T1)->getUnderlyingType())) 732 return false; 733 break; 734 735 case Type::Decltype: 736 if (!IsStructurallyEquivalent(Context, 737 cast<DecltypeType>(T1)->getUnderlyingExpr(), 738 cast<DecltypeType>(T2)->getUnderlyingExpr())) 739 return false; 740 break; 741 742 case Type::Auto: 743 if (!IsStructurallyEquivalent(Context, 744 cast<AutoType>(T1)->getDeducedType(), 745 cast<AutoType>(T2)->getDeducedType())) 746 return false; 747 break; 748 749 case Type::Record: 750 case Type::Enum: 751 if (!IsStructurallyEquivalent(Context, 752 cast<TagType>(T1)->getDecl(), 753 cast<TagType>(T2)->getDecl())) 754 return false; 755 break; 756 757 case Type::TemplateTypeParm: { 758 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1); 759 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2); 760 if (Parm1->getDepth() != Parm2->getDepth()) 761 return false; 762 if (Parm1->getIndex() != Parm2->getIndex()) 763 return false; 764 if (Parm1->isParameterPack() != Parm2->isParameterPack()) 765 return false; 766 767 // Names of template type parameters are never significant. 768 break; 769 } 770 771 case Type::SubstTemplateTypeParm: { 772 const SubstTemplateTypeParmType *Subst1 773 = cast<SubstTemplateTypeParmType>(T1); 774 const SubstTemplateTypeParmType *Subst2 775 = cast<SubstTemplateTypeParmType>(T2); 776 if (!IsStructurallyEquivalent(Context, 777 QualType(Subst1->getReplacedParameter(), 0), 778 QualType(Subst2->getReplacedParameter(), 0))) 779 return false; 780 if (!IsStructurallyEquivalent(Context, 781 Subst1->getReplacementType(), 782 Subst2->getReplacementType())) 783 return false; 784 break; 785 } 786 787 case Type::SubstTemplateTypeParmPack: { 788 const SubstTemplateTypeParmPackType *Subst1 789 = cast<SubstTemplateTypeParmPackType>(T1); 790 const SubstTemplateTypeParmPackType *Subst2 791 = cast<SubstTemplateTypeParmPackType>(T2); 792 if (!IsStructurallyEquivalent(Context, 793 QualType(Subst1->getReplacedParameter(), 0), 794 QualType(Subst2->getReplacedParameter(), 0))) 795 return false; 796 if (!IsStructurallyEquivalent(Context, 797 Subst1->getArgumentPack(), 798 Subst2->getArgumentPack())) 799 return false; 800 break; 801 } 802 case Type::TemplateSpecialization: { 803 const TemplateSpecializationType *Spec1 804 = cast<TemplateSpecializationType>(T1); 805 const TemplateSpecializationType *Spec2 806 = cast<TemplateSpecializationType>(T2); 807 if (!IsStructurallyEquivalent(Context, 808 Spec1->getTemplateName(), 809 Spec2->getTemplateName())) 810 return false; 811 if (Spec1->getNumArgs() != Spec2->getNumArgs()) 812 return false; 813 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { 814 if (!IsStructurallyEquivalent(Context, 815 Spec1->getArg(I), Spec2->getArg(I))) 816 return false; 817 } 818 break; 819 } 820 821 case Type::Elaborated: { 822 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1); 823 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2); 824 // CHECKME: what if a keyword is ETK_None or ETK_typename ? 825 if (Elab1->getKeyword() != Elab2->getKeyword()) 826 return false; 827 if (!IsStructurallyEquivalent(Context, 828 Elab1->getQualifier(), 829 Elab2->getQualifier())) 830 return false; 831 if (!IsStructurallyEquivalent(Context, 832 Elab1->getNamedType(), 833 Elab2->getNamedType())) 834 return false; 835 break; 836 } 837 838 case Type::InjectedClassName: { 839 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1); 840 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2); 841 if (!IsStructurallyEquivalent(Context, 842 Inj1->getInjectedSpecializationType(), 843 Inj2->getInjectedSpecializationType())) 844 return false; 845 break; 846 } 847 848 case Type::DependentName: { 849 const DependentNameType *Typename1 = cast<DependentNameType>(T1); 850 const DependentNameType *Typename2 = cast<DependentNameType>(T2); 851 if (!IsStructurallyEquivalent(Context, 852 Typename1->getQualifier(), 853 Typename2->getQualifier())) 854 return false; 855 if (!IsStructurallyEquivalent(Typename1->getIdentifier(), 856 Typename2->getIdentifier())) 857 return false; 858 859 break; 860 } 861 862 case Type::DependentTemplateSpecialization: { 863 const DependentTemplateSpecializationType *Spec1 = 864 cast<DependentTemplateSpecializationType>(T1); 865 const DependentTemplateSpecializationType *Spec2 = 866 cast<DependentTemplateSpecializationType>(T2); 867 if (!IsStructurallyEquivalent(Context, 868 Spec1->getQualifier(), 869 Spec2->getQualifier())) 870 return false; 871 if (!IsStructurallyEquivalent(Spec1->getIdentifier(), 872 Spec2->getIdentifier())) 873 return false; 874 if (Spec1->getNumArgs() != Spec2->getNumArgs()) 875 return false; 876 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { 877 if (!IsStructurallyEquivalent(Context, 878 Spec1->getArg(I), Spec2->getArg(I))) 879 return false; 880 } 881 break; 882 } 883 884 case Type::PackExpansion: 885 if (!IsStructurallyEquivalent(Context, 886 cast<PackExpansionType>(T1)->getPattern(), 887 cast<PackExpansionType>(T2)->getPattern())) 888 return false; 889 break; 890 891 case Type::ObjCInterface: { 892 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1); 893 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2); 894 if (!IsStructurallyEquivalent(Context, 895 Iface1->getDecl(), Iface2->getDecl())) 896 return false; 897 break; 898 } 899 900 case Type::ObjCObject: { 901 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1); 902 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2); 903 if (!IsStructurallyEquivalent(Context, 904 Obj1->getBaseType(), 905 Obj2->getBaseType())) 906 return false; 907 if (Obj1->getNumProtocols() != Obj2->getNumProtocols()) 908 return false; 909 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) { 910 if (!IsStructurallyEquivalent(Context, 911 Obj1->getProtocol(I), 912 Obj2->getProtocol(I))) 913 return false; 914 } 915 break; 916 } 917 918 case Type::ObjCObjectPointer: { 919 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1); 920 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2); 921 if (!IsStructurallyEquivalent(Context, 922 Ptr1->getPointeeType(), 923 Ptr2->getPointeeType())) 924 return false; 925 break; 926 } 927 928 case Type::Atomic: { 929 if (!IsStructurallyEquivalent(Context, 930 cast<AtomicType>(T1)->getValueType(), 931 cast<AtomicType>(T2)->getValueType())) 932 return false; 933 break; 934 } 935 936 case Type::Pipe: { 937 if (!IsStructurallyEquivalent(Context, 938 cast<PipeType>(T1)->getElementType(), 939 cast<PipeType>(T2)->getElementType())) 940 return false; 941 break; 942 } 943 944 } // end switch 945 946 return true; 947 } 948 949 /// \brief Determine structural equivalence of two fields. 950 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 951 FieldDecl *Field1, FieldDecl *Field2) { 952 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext()); 953 954 // For anonymous structs/unions, match up the anonymous struct/union type 955 // declarations directly, so that we don't go off searching for anonymous 956 // types 957 if (Field1->isAnonymousStructOrUnion() && 958 Field2->isAnonymousStructOrUnion()) { 959 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl(); 960 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl(); 961 return IsStructurallyEquivalent(Context, D1, D2); 962 } 963 964 // Check for equivalent field names. 965 IdentifierInfo *Name1 = Field1->getIdentifier(); 966 IdentifierInfo *Name2 = Field2->getIdentifier(); 967 if (!::IsStructurallyEquivalent(Name1, Name2)) 968 return false; 969 970 if (!IsStructurallyEquivalent(Context, 971 Field1->getType(), Field2->getType())) { 972 if (Context.Complain) { 973 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) 974 << Context.C2.getTypeDeclType(Owner2); 975 Context.Diag2(Field2->getLocation(), diag::note_odr_field) 976 << Field2->getDeclName() << Field2->getType(); 977 Context.Diag1(Field1->getLocation(), diag::note_odr_field) 978 << Field1->getDeclName() << Field1->getType(); 979 } 980 return false; 981 } 982 983 if (Field1->isBitField() != Field2->isBitField()) { 984 if (Context.Complain) { 985 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) 986 << Context.C2.getTypeDeclType(Owner2); 987 if (Field1->isBitField()) { 988 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) 989 << Field1->getDeclName() << Field1->getType() 990 << Field1->getBitWidthValue(Context.C1); 991 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field) 992 << Field2->getDeclName(); 993 } else { 994 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) 995 << Field2->getDeclName() << Field2->getType() 996 << Field2->getBitWidthValue(Context.C2); 997 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field) 998 << Field1->getDeclName(); 999 } 1000 } 1001 return false; 1002 } 1003 1004 if (Field1->isBitField()) { 1005 // Make sure that the bit-fields are the same length. 1006 unsigned Bits1 = Field1->getBitWidthValue(Context.C1); 1007 unsigned Bits2 = Field2->getBitWidthValue(Context.C2); 1008 1009 if (Bits1 != Bits2) { 1010 if (Context.Complain) { 1011 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1012 << Context.C2.getTypeDeclType(Owner2); 1013 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) 1014 << Field2->getDeclName() << Field2->getType() << Bits2; 1015 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) 1016 << Field1->getDeclName() << Field1->getType() << Bits1; 1017 } 1018 return false; 1019 } 1020 } 1021 1022 return true; 1023 } 1024 1025 /// \brief Find the index of the given anonymous struct/union within its 1026 /// context. 1027 /// 1028 /// \returns Returns the index of this anonymous struct/union in its context, 1029 /// including the next assigned index (if none of them match). Returns an 1030 /// empty option if the context is not a record, i.e.. if the anonymous 1031 /// struct/union is at namespace or block scope. 1032 static Optional<unsigned> findUntaggedStructOrUnionIndex(RecordDecl *Anon) { 1033 ASTContext &Context = Anon->getASTContext(); 1034 QualType AnonTy = Context.getRecordType(Anon); 1035 1036 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext()); 1037 if (!Owner) 1038 return None; 1039 1040 unsigned Index = 0; 1041 for (const auto *D : Owner->noload_decls()) { 1042 const auto *F = dyn_cast<FieldDecl>(D); 1043 if (!F) 1044 continue; 1045 1046 if (F->isAnonymousStructOrUnion()) { 1047 if (Context.hasSameType(F->getType(), AnonTy)) 1048 break; 1049 ++Index; 1050 continue; 1051 } 1052 1053 // If the field looks like this: 1054 // struct { ... } A; 1055 QualType FieldType = F->getType(); 1056 if (const auto *RecType = dyn_cast<RecordType>(FieldType)) { 1057 const RecordDecl *RecDecl = RecType->getDecl(); 1058 if (RecDecl->getDeclContext() == Owner && 1059 !RecDecl->getIdentifier()) { 1060 if (Context.hasSameType(FieldType, AnonTy)) 1061 break; 1062 ++Index; 1063 continue; 1064 } 1065 } 1066 } 1067 1068 return Index; 1069 } 1070 1071 /// \brief Determine structural equivalence of two records. 1072 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1073 RecordDecl *D1, RecordDecl *D2) { 1074 if (D1->isUnion() != D2->isUnion()) { 1075 if (Context.Complain) { 1076 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1077 << Context.C2.getTypeDeclType(D2); 1078 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here) 1079 << D1->getDeclName() << (unsigned)D1->getTagKind(); 1080 } 1081 return false; 1082 } 1083 1084 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) { 1085 // If both anonymous structs/unions are in a record context, make sure 1086 // they occur in the same location in the context records. 1087 if (Optional<unsigned> Index1 = findUntaggedStructOrUnionIndex(D1)) { 1088 if (Optional<unsigned> Index2 = findUntaggedStructOrUnionIndex(D2)) { 1089 if (*Index1 != *Index2) 1090 return false; 1091 } 1092 } 1093 } 1094 1095 // If both declarations are class template specializations, we know 1096 // the ODR applies, so check the template and template arguments. 1097 ClassTemplateSpecializationDecl *Spec1 1098 = dyn_cast<ClassTemplateSpecializationDecl>(D1); 1099 ClassTemplateSpecializationDecl *Spec2 1100 = dyn_cast<ClassTemplateSpecializationDecl>(D2); 1101 if (Spec1 && Spec2) { 1102 // Check that the specialized templates are the same. 1103 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(), 1104 Spec2->getSpecializedTemplate())) 1105 return false; 1106 1107 // Check that the template arguments are the same. 1108 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size()) 1109 return false; 1110 1111 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I) 1112 if (!IsStructurallyEquivalent(Context, 1113 Spec1->getTemplateArgs().get(I), 1114 Spec2->getTemplateArgs().get(I))) 1115 return false; 1116 } 1117 // If one is a class template specialization and the other is not, these 1118 // structures are different. 1119 else if (Spec1 || Spec2) 1120 return false; 1121 1122 // Compare the definitions of these two records. If either or both are 1123 // incomplete, we assume that they are equivalent. 1124 D1 = D1->getDefinition(); 1125 D2 = D2->getDefinition(); 1126 if (!D1 || !D2) 1127 return true; 1128 1129 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) { 1130 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) { 1131 if (D1CXX->getNumBases() != D2CXX->getNumBases()) { 1132 if (Context.Complain) { 1133 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1134 << Context.C2.getTypeDeclType(D2); 1135 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases) 1136 << D2CXX->getNumBases(); 1137 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases) 1138 << D1CXX->getNumBases(); 1139 } 1140 return false; 1141 } 1142 1143 // Check the base classes. 1144 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(), 1145 BaseEnd1 = D1CXX->bases_end(), 1146 Base2 = D2CXX->bases_begin(); 1147 Base1 != BaseEnd1; 1148 ++Base1, ++Base2) { 1149 if (!IsStructurallyEquivalent(Context, 1150 Base1->getType(), Base2->getType())) { 1151 if (Context.Complain) { 1152 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1153 << Context.C2.getTypeDeclType(D2); 1154 Context.Diag2(Base2->getLocStart(), diag::note_odr_base) 1155 << Base2->getType() 1156 << Base2->getSourceRange(); 1157 Context.Diag1(Base1->getLocStart(), diag::note_odr_base) 1158 << Base1->getType() 1159 << Base1->getSourceRange(); 1160 } 1161 return false; 1162 } 1163 1164 // Check virtual vs. non-virtual inheritance mismatch. 1165 if (Base1->isVirtual() != Base2->isVirtual()) { 1166 if (Context.Complain) { 1167 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1168 << Context.C2.getTypeDeclType(D2); 1169 Context.Diag2(Base2->getLocStart(), 1170 diag::note_odr_virtual_base) 1171 << Base2->isVirtual() << Base2->getSourceRange(); 1172 Context.Diag1(Base1->getLocStart(), diag::note_odr_base) 1173 << Base1->isVirtual() 1174 << Base1->getSourceRange(); 1175 } 1176 return false; 1177 } 1178 } 1179 } else if (D1CXX->getNumBases() > 0) { 1180 if (Context.Complain) { 1181 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1182 << Context.C2.getTypeDeclType(D2); 1183 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin(); 1184 Context.Diag1(Base1->getLocStart(), diag::note_odr_base) 1185 << Base1->getType() 1186 << Base1->getSourceRange(); 1187 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base); 1188 } 1189 return false; 1190 } 1191 } 1192 1193 // Check the fields for consistency. 1194 RecordDecl::field_iterator Field2 = D2->field_begin(), 1195 Field2End = D2->field_end(); 1196 for (RecordDecl::field_iterator Field1 = D1->field_begin(), 1197 Field1End = D1->field_end(); 1198 Field1 != Field1End; 1199 ++Field1, ++Field2) { 1200 if (Field2 == Field2End) { 1201 if (Context.Complain) { 1202 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1203 << Context.C2.getTypeDeclType(D2); 1204 Context.Diag1(Field1->getLocation(), diag::note_odr_field) 1205 << Field1->getDeclName() << Field1->getType(); 1206 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field); 1207 } 1208 return false; 1209 } 1210 1211 if (!IsStructurallyEquivalent(Context, *Field1, *Field2)) 1212 return false; 1213 } 1214 1215 if (Field2 != Field2End) { 1216 if (Context.Complain) { 1217 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1218 << Context.C2.getTypeDeclType(D2); 1219 Context.Diag2(Field2->getLocation(), diag::note_odr_field) 1220 << Field2->getDeclName() << Field2->getType(); 1221 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field); 1222 } 1223 return false; 1224 } 1225 1226 return true; 1227 } 1228 1229 /// \brief Determine structural equivalence of two enums. 1230 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1231 EnumDecl *D1, EnumDecl *D2) { 1232 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(), 1233 EC2End = D2->enumerator_end(); 1234 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(), 1235 EC1End = D1->enumerator_end(); 1236 EC1 != EC1End; ++EC1, ++EC2) { 1237 if (EC2 == EC2End) { 1238 if (Context.Complain) { 1239 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1240 << Context.C2.getTypeDeclType(D2); 1241 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) 1242 << EC1->getDeclName() 1243 << EC1->getInitVal().toString(10); 1244 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator); 1245 } 1246 return false; 1247 } 1248 1249 llvm::APSInt Val1 = EC1->getInitVal(); 1250 llvm::APSInt Val2 = EC2->getInitVal(); 1251 if (!llvm::APSInt::isSameValue(Val1, Val2) || 1252 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) { 1253 if (Context.Complain) { 1254 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1255 << Context.C2.getTypeDeclType(D2); 1256 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) 1257 << EC2->getDeclName() 1258 << EC2->getInitVal().toString(10); 1259 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) 1260 << EC1->getDeclName() 1261 << EC1->getInitVal().toString(10); 1262 } 1263 return false; 1264 } 1265 } 1266 1267 if (EC2 != EC2End) { 1268 if (Context.Complain) { 1269 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) 1270 << Context.C2.getTypeDeclType(D2); 1271 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) 1272 << EC2->getDeclName() 1273 << EC2->getInitVal().toString(10); 1274 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator); 1275 } 1276 return false; 1277 } 1278 1279 return true; 1280 } 1281 1282 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1283 TemplateParameterList *Params1, 1284 TemplateParameterList *Params2) { 1285 if (Params1->size() != Params2->size()) { 1286 if (Context.Complain) { 1287 Context.Diag2(Params2->getTemplateLoc(), 1288 diag::err_odr_different_num_template_parameters) 1289 << Params1->size() << Params2->size(); 1290 Context.Diag1(Params1->getTemplateLoc(), 1291 diag::note_odr_template_parameter_list); 1292 } 1293 return false; 1294 } 1295 1296 for (unsigned I = 0, N = Params1->size(); I != N; ++I) { 1297 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) { 1298 if (Context.Complain) { 1299 Context.Diag2(Params2->getParam(I)->getLocation(), 1300 diag::err_odr_different_template_parameter_kind); 1301 Context.Diag1(Params1->getParam(I)->getLocation(), 1302 diag::note_odr_template_parameter_here); 1303 } 1304 return false; 1305 } 1306 1307 if (!Context.IsStructurallyEquivalent(Params1->getParam(I), 1308 Params2->getParam(I))) { 1309 1310 return false; 1311 } 1312 } 1313 1314 return true; 1315 } 1316 1317 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1318 TemplateTypeParmDecl *D1, 1319 TemplateTypeParmDecl *D2) { 1320 if (D1->isParameterPack() != D2->isParameterPack()) { 1321 if (Context.Complain) { 1322 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) 1323 << D2->isParameterPack(); 1324 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) 1325 << D1->isParameterPack(); 1326 } 1327 return false; 1328 } 1329 1330 return true; 1331 } 1332 1333 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1334 NonTypeTemplateParmDecl *D1, 1335 NonTypeTemplateParmDecl *D2) { 1336 if (D1->isParameterPack() != D2->isParameterPack()) { 1337 if (Context.Complain) { 1338 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) 1339 << D2->isParameterPack(); 1340 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) 1341 << D1->isParameterPack(); 1342 } 1343 return false; 1344 } 1345 1346 // Check types. 1347 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) { 1348 if (Context.Complain) { 1349 Context.Diag2(D2->getLocation(), 1350 diag::err_odr_non_type_parameter_type_inconsistent) 1351 << D2->getType() << D1->getType(); 1352 Context.Diag1(D1->getLocation(), diag::note_odr_value_here) 1353 << D1->getType(); 1354 } 1355 return false; 1356 } 1357 1358 return true; 1359 } 1360 1361 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1362 TemplateTemplateParmDecl *D1, 1363 TemplateTemplateParmDecl *D2) { 1364 if (D1->isParameterPack() != D2->isParameterPack()) { 1365 if (Context.Complain) { 1366 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) 1367 << D2->isParameterPack(); 1368 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) 1369 << D1->isParameterPack(); 1370 } 1371 return false; 1372 } 1373 1374 // Check template parameter lists. 1375 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(), 1376 D2->getTemplateParameters()); 1377 } 1378 1379 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1380 ClassTemplateDecl *D1, 1381 ClassTemplateDecl *D2) { 1382 // Check template parameters. 1383 if (!IsStructurallyEquivalent(Context, 1384 D1->getTemplateParameters(), 1385 D2->getTemplateParameters())) 1386 return false; 1387 1388 // Check the templated declaration. 1389 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(), 1390 D2->getTemplatedDecl()); 1391 } 1392 1393 /// \brief Determine structural equivalence of two declarations. 1394 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, 1395 Decl *D1, Decl *D2) { 1396 // FIXME: Check for known structural equivalences via a callback of some sort. 1397 1398 // Check whether we already know that these two declarations are not 1399 // structurally equivalent. 1400 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(), 1401 D2->getCanonicalDecl()))) 1402 return false; 1403 1404 // Determine whether we've already produced a tentative equivalence for D1. 1405 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()]; 1406 if (EquivToD1) 1407 return EquivToD1 == D2->getCanonicalDecl(); 1408 1409 // Produce a tentative equivalence D1 <-> D2, which will be checked later. 1410 EquivToD1 = D2->getCanonicalDecl(); 1411 Context.DeclsToCheck.push_back(D1->getCanonicalDecl()); 1412 return true; 1413 } 1414 1415 bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1, 1416 Decl *D2) { 1417 if (!::IsStructurallyEquivalent(*this, D1, D2)) 1418 return false; 1419 1420 return !Finish(); 1421 } 1422 1423 bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1, 1424 QualType T2) { 1425 if (!::IsStructurallyEquivalent(*this, T1, T2)) 1426 return false; 1427 1428 return !Finish(); 1429 } 1430 1431 bool StructuralEquivalenceContext::Finish() { 1432 while (!DeclsToCheck.empty()) { 1433 // Check the next declaration. 1434 Decl *D1 = DeclsToCheck.front(); 1435 DeclsToCheck.pop_front(); 1436 1437 Decl *D2 = TentativeEquivalences[D1]; 1438 assert(D2 && "Unrecorded tentative equivalence?"); 1439 1440 bool Equivalent = true; 1441 1442 // FIXME: Switch on all declaration kinds. For now, we're just going to 1443 // check the obvious ones. 1444 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) { 1445 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) { 1446 // Check for equivalent structure names. 1447 IdentifierInfo *Name1 = Record1->getIdentifier(); 1448 if (!Name1 && Record1->getTypedefNameForAnonDecl()) 1449 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier(); 1450 IdentifierInfo *Name2 = Record2->getIdentifier(); 1451 if (!Name2 && Record2->getTypedefNameForAnonDecl()) 1452 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier(); 1453 if (!::IsStructurallyEquivalent(Name1, Name2) || 1454 !::IsStructurallyEquivalent(*this, Record1, Record2)) 1455 Equivalent = false; 1456 } else { 1457 // Record/non-record mismatch. 1458 Equivalent = false; 1459 } 1460 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) { 1461 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) { 1462 // Check for equivalent enum names. 1463 IdentifierInfo *Name1 = Enum1->getIdentifier(); 1464 if (!Name1 && Enum1->getTypedefNameForAnonDecl()) 1465 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier(); 1466 IdentifierInfo *Name2 = Enum2->getIdentifier(); 1467 if (!Name2 && Enum2->getTypedefNameForAnonDecl()) 1468 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier(); 1469 if (!::IsStructurallyEquivalent(Name1, Name2) || 1470 !::IsStructurallyEquivalent(*this, Enum1, Enum2)) 1471 Equivalent = false; 1472 } else { 1473 // Enum/non-enum mismatch 1474 Equivalent = false; 1475 } 1476 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) { 1477 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) { 1478 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(), 1479 Typedef2->getIdentifier()) || 1480 !::IsStructurallyEquivalent(*this, 1481 Typedef1->getUnderlyingType(), 1482 Typedef2->getUnderlyingType())) 1483 Equivalent = false; 1484 } else { 1485 // Typedef/non-typedef mismatch. 1486 Equivalent = false; 1487 } 1488 } else if (ClassTemplateDecl *ClassTemplate1 1489 = dyn_cast<ClassTemplateDecl>(D1)) { 1490 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) { 1491 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(), 1492 ClassTemplate2->getIdentifier()) || 1493 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2)) 1494 Equivalent = false; 1495 } else { 1496 // Class template/non-class-template mismatch. 1497 Equivalent = false; 1498 } 1499 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) { 1500 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) { 1501 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) 1502 Equivalent = false; 1503 } else { 1504 // Kind mismatch. 1505 Equivalent = false; 1506 } 1507 } else if (NonTypeTemplateParmDecl *NTTP1 1508 = dyn_cast<NonTypeTemplateParmDecl>(D1)) { 1509 if (NonTypeTemplateParmDecl *NTTP2 1510 = dyn_cast<NonTypeTemplateParmDecl>(D2)) { 1511 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2)) 1512 Equivalent = false; 1513 } else { 1514 // Kind mismatch. 1515 Equivalent = false; 1516 } 1517 } else if (TemplateTemplateParmDecl *TTP1 1518 = dyn_cast<TemplateTemplateParmDecl>(D1)) { 1519 if (TemplateTemplateParmDecl *TTP2 1520 = dyn_cast<TemplateTemplateParmDecl>(D2)) { 1521 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) 1522 Equivalent = false; 1523 } else { 1524 // Kind mismatch. 1525 Equivalent = false; 1526 } 1527 } 1528 1529 if (!Equivalent) { 1530 // Note that these two declarations are not equivalent (and we already 1531 // know about it). 1532 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(), 1533 D2->getCanonicalDecl())); 1534 return true; 1535 } 1536 // FIXME: Check other declaration kinds! 1537 } 1538 1539 return false; 1540 } 1541 1542 //---------------------------------------------------------------------------- 1543 // Import Types 1544 //---------------------------------------------------------------------------- 1545 1546 QualType ASTNodeImporter::VisitType(const Type *T) { 1547 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) 1548 << T->getTypeClassName(); 1549 return QualType(); 1550 } 1551 1552 QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { 1553 switch (T->getKind()) { 1554 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 1555 case BuiltinType::Id: \ 1556 return Importer.getToContext().SingletonId; 1557 #include "clang/Basic/OpenCLImageTypes.def" 1558 #define SHARED_SINGLETON_TYPE(Expansion) 1559 #define BUILTIN_TYPE(Id, SingletonId) \ 1560 case BuiltinType::Id: return Importer.getToContext().SingletonId; 1561 #include "clang/AST/BuiltinTypes.def" 1562 1563 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" 1564 // context supports C++. 1565 1566 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" 1567 // context supports ObjC. 1568 1569 case BuiltinType::Char_U: 1570 // The context we're importing from has an unsigned 'char'. If we're 1571 // importing into a context with a signed 'char', translate to 1572 // 'unsigned char' instead. 1573 if (Importer.getToContext().getLangOpts().CharIsSigned) 1574 return Importer.getToContext().UnsignedCharTy; 1575 1576 return Importer.getToContext().CharTy; 1577 1578 case BuiltinType::Char_S: 1579 // The context we're importing from has an unsigned 'char'. If we're 1580 // importing into a context with a signed 'char', translate to 1581 // 'unsigned char' instead. 1582 if (!Importer.getToContext().getLangOpts().CharIsSigned) 1583 return Importer.getToContext().SignedCharTy; 1584 1585 return Importer.getToContext().CharTy; 1586 1587 case BuiltinType::WChar_S: 1588 case BuiltinType::WChar_U: 1589 // FIXME: If not in C++, shall we translate to the C equivalent of 1590 // wchar_t? 1591 return Importer.getToContext().WCharTy; 1592 } 1593 1594 llvm_unreachable("Invalid BuiltinType Kind!"); 1595 } 1596 1597 QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) { 1598 QualType ToElementType = Importer.Import(T->getElementType()); 1599 if (ToElementType.isNull()) 1600 return QualType(); 1601 1602 return Importer.getToContext().getComplexType(ToElementType); 1603 } 1604 1605 QualType ASTNodeImporter::VisitPointerType(const PointerType *T) { 1606 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1607 if (ToPointeeType.isNull()) 1608 return QualType(); 1609 1610 return Importer.getToContext().getPointerType(ToPointeeType); 1611 } 1612 1613 QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { 1614 // FIXME: Check for blocks support in "to" context. 1615 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1616 if (ToPointeeType.isNull()) 1617 return QualType(); 1618 1619 return Importer.getToContext().getBlockPointerType(ToPointeeType); 1620 } 1621 1622 QualType 1623 ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { 1624 // FIXME: Check for C++ support in "to" context. 1625 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); 1626 if (ToPointeeType.isNull()) 1627 return QualType(); 1628 1629 return Importer.getToContext().getLValueReferenceType(ToPointeeType); 1630 } 1631 1632 QualType 1633 ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { 1634 // FIXME: Check for C++0x support in "to" context. 1635 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); 1636 if (ToPointeeType.isNull()) 1637 return QualType(); 1638 1639 return Importer.getToContext().getRValueReferenceType(ToPointeeType); 1640 } 1641 1642 QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { 1643 // FIXME: Check for C++ support in "to" context. 1644 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1645 if (ToPointeeType.isNull()) 1646 return QualType(); 1647 1648 QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); 1649 return Importer.getToContext().getMemberPointerType(ToPointeeType, 1650 ClassType.getTypePtr()); 1651 } 1652 1653 QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { 1654 QualType ToElementType = Importer.Import(T->getElementType()); 1655 if (ToElementType.isNull()) 1656 return QualType(); 1657 1658 return Importer.getToContext().getConstantArrayType(ToElementType, 1659 T->getSize(), 1660 T->getSizeModifier(), 1661 T->getIndexTypeCVRQualifiers()); 1662 } 1663 1664 QualType 1665 ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { 1666 QualType ToElementType = Importer.Import(T->getElementType()); 1667 if (ToElementType.isNull()) 1668 return QualType(); 1669 1670 return Importer.getToContext().getIncompleteArrayType(ToElementType, 1671 T->getSizeModifier(), 1672 T->getIndexTypeCVRQualifiers()); 1673 } 1674 1675 QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { 1676 QualType ToElementType = Importer.Import(T->getElementType()); 1677 if (ToElementType.isNull()) 1678 return QualType(); 1679 1680 Expr *Size = Importer.Import(T->getSizeExpr()); 1681 if (!Size) 1682 return QualType(); 1683 1684 SourceRange Brackets = Importer.Import(T->getBracketsRange()); 1685 return Importer.getToContext().getVariableArrayType(ToElementType, Size, 1686 T->getSizeModifier(), 1687 T->getIndexTypeCVRQualifiers(), 1688 Brackets); 1689 } 1690 1691 QualType ASTNodeImporter::VisitVectorType(const VectorType *T) { 1692 QualType ToElementType = Importer.Import(T->getElementType()); 1693 if (ToElementType.isNull()) 1694 return QualType(); 1695 1696 return Importer.getToContext().getVectorType(ToElementType, 1697 T->getNumElements(), 1698 T->getVectorKind()); 1699 } 1700 1701 QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { 1702 QualType ToElementType = Importer.Import(T->getElementType()); 1703 if (ToElementType.isNull()) 1704 return QualType(); 1705 1706 return Importer.getToContext().getExtVectorType(ToElementType, 1707 T->getNumElements()); 1708 } 1709 1710 QualType 1711 ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { 1712 // FIXME: What happens if we're importing a function without a prototype 1713 // into C++? Should we make it variadic? 1714 QualType ToResultType = Importer.Import(T->getReturnType()); 1715 if (ToResultType.isNull()) 1716 return QualType(); 1717 1718 return Importer.getToContext().getFunctionNoProtoType(ToResultType, 1719 T->getExtInfo()); 1720 } 1721 1722 QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { 1723 QualType ToResultType = Importer.Import(T->getReturnType()); 1724 if (ToResultType.isNull()) 1725 return QualType(); 1726 1727 // Import argument types 1728 SmallVector<QualType, 4> ArgTypes; 1729 for (const auto &A : T->param_types()) { 1730 QualType ArgType = Importer.Import(A); 1731 if (ArgType.isNull()) 1732 return QualType(); 1733 ArgTypes.push_back(ArgType); 1734 } 1735 1736 // Import exception types 1737 SmallVector<QualType, 4> ExceptionTypes; 1738 for (const auto &E : T->exceptions()) { 1739 QualType ExceptionType = Importer.Import(E); 1740 if (ExceptionType.isNull()) 1741 return QualType(); 1742 ExceptionTypes.push_back(ExceptionType); 1743 } 1744 1745 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); 1746 FunctionProtoType::ExtProtoInfo ToEPI; 1747 1748 ToEPI.ExtInfo = FromEPI.ExtInfo; 1749 ToEPI.Variadic = FromEPI.Variadic; 1750 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; 1751 ToEPI.TypeQuals = FromEPI.TypeQuals; 1752 ToEPI.RefQualifier = FromEPI.RefQualifier; 1753 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; 1754 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; 1755 ToEPI.ExceptionSpec.NoexceptExpr = 1756 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr); 1757 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>( 1758 Importer.Import(FromEPI.ExceptionSpec.SourceDecl)); 1759 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>( 1760 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate)); 1761 1762 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI); 1763 } 1764 1765 QualType ASTNodeImporter::VisitParenType(const ParenType *T) { 1766 QualType ToInnerType = Importer.Import(T->getInnerType()); 1767 if (ToInnerType.isNull()) 1768 return QualType(); 1769 1770 return Importer.getToContext().getParenType(ToInnerType); 1771 } 1772 1773 QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { 1774 TypedefNameDecl *ToDecl 1775 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl())); 1776 if (!ToDecl) 1777 return QualType(); 1778 1779 return Importer.getToContext().getTypeDeclType(ToDecl); 1780 } 1781 1782 QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { 1783 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); 1784 if (!ToExpr) 1785 return QualType(); 1786 1787 return Importer.getToContext().getTypeOfExprType(ToExpr); 1788 } 1789 1790 QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { 1791 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); 1792 if (ToUnderlyingType.isNull()) 1793 return QualType(); 1794 1795 return Importer.getToContext().getTypeOfType(ToUnderlyingType); 1796 } 1797 1798 QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { 1799 // FIXME: Make sure that the "to" context supports C++0x! 1800 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); 1801 if (!ToExpr) 1802 return QualType(); 1803 1804 QualType UnderlyingType = Importer.Import(T->getUnderlyingType()); 1805 if (UnderlyingType.isNull()) 1806 return QualType(); 1807 1808 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType); 1809 } 1810 1811 QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { 1812 QualType ToBaseType = Importer.Import(T->getBaseType()); 1813 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); 1814 if (ToBaseType.isNull() || ToUnderlyingType.isNull()) 1815 return QualType(); 1816 1817 return Importer.getToContext().getUnaryTransformType(ToBaseType, 1818 ToUnderlyingType, 1819 T->getUTTKind()); 1820 } 1821 1822 QualType ASTNodeImporter::VisitAutoType(const AutoType *T) { 1823 // FIXME: Make sure that the "to" context supports C++11! 1824 QualType FromDeduced = T->getDeducedType(); 1825 QualType ToDeduced; 1826 if (!FromDeduced.isNull()) { 1827 ToDeduced = Importer.Import(FromDeduced); 1828 if (ToDeduced.isNull()) 1829 return QualType(); 1830 } 1831 1832 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(), 1833 /*IsDependent*/false); 1834 } 1835 1836 QualType ASTNodeImporter::VisitInjectedClassNameType( 1837 const InjectedClassNameType *T) { 1838 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl())); 1839 if (!D) 1840 return QualType(); 1841 1842 QualType InjType = Importer.Import(T->getInjectedSpecializationType()); 1843 if (InjType.isNull()) 1844 return QualType(); 1845 1846 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading 1847 // See comments in InjectedClassNameType definition for details 1848 // return Importer.getToContext().getInjectedClassNameType(D, InjType); 1849 enum { 1850 TypeAlignmentInBits = 4, 1851 TypeAlignment = 1 << TypeAlignmentInBits 1852 }; 1853 1854 return QualType(new (Importer.getToContext(), TypeAlignment) 1855 InjectedClassNameType(D, InjType), 0); 1856 } 1857 1858 QualType ASTNodeImporter::VisitRecordType(const RecordType *T) { 1859 RecordDecl *ToDecl 1860 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); 1861 if (!ToDecl) 1862 return QualType(); 1863 1864 return Importer.getToContext().getTagDeclType(ToDecl); 1865 } 1866 1867 QualType ASTNodeImporter::VisitEnumType(const EnumType *T) { 1868 EnumDecl *ToDecl 1869 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl())); 1870 if (!ToDecl) 1871 return QualType(); 1872 1873 return Importer.getToContext().getTagDeclType(ToDecl); 1874 } 1875 1876 QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { 1877 QualType FromModifiedType = T->getModifiedType(); 1878 QualType FromEquivalentType = T->getEquivalentType(); 1879 QualType ToModifiedType; 1880 QualType ToEquivalentType; 1881 1882 if (!FromModifiedType.isNull()) { 1883 ToModifiedType = Importer.Import(FromModifiedType); 1884 if (ToModifiedType.isNull()) 1885 return QualType(); 1886 } 1887 if (!FromEquivalentType.isNull()) { 1888 ToEquivalentType = Importer.Import(FromEquivalentType); 1889 if (ToEquivalentType.isNull()) 1890 return QualType(); 1891 } 1892 1893 return Importer.getToContext().getAttributedType(T->getAttrKind(), 1894 ToModifiedType, ToEquivalentType); 1895 } 1896 1897 1898 QualType ASTNodeImporter::VisitTemplateTypeParmType( 1899 const TemplateTypeParmType *T) { 1900 TemplateTypeParmDecl *ParmDecl = 1901 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl())); 1902 if (!ParmDecl && T->getDecl()) 1903 return QualType(); 1904 1905 return Importer.getToContext().getTemplateTypeParmType( 1906 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl); 1907 } 1908 1909 QualType ASTNodeImporter::VisitTemplateSpecializationType( 1910 const TemplateSpecializationType *T) { 1911 TemplateName ToTemplate = Importer.Import(T->getTemplateName()); 1912 if (ToTemplate.isNull()) 1913 return QualType(); 1914 1915 SmallVector<TemplateArgument, 2> ToTemplateArgs; 1916 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs)) 1917 return QualType(); 1918 1919 QualType ToCanonType; 1920 if (!QualType(T, 0).isCanonical()) { 1921 QualType FromCanonType 1922 = Importer.getFromContext().getCanonicalType(QualType(T, 0)); 1923 ToCanonType =Importer.Import(FromCanonType); 1924 if (ToCanonType.isNull()) 1925 return QualType(); 1926 } 1927 return Importer.getToContext().getTemplateSpecializationType(ToTemplate, 1928 ToTemplateArgs, 1929 ToCanonType); 1930 } 1931 1932 QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { 1933 NestedNameSpecifier *ToQualifier = nullptr; 1934 // Note: the qualifier in an ElaboratedType is optional. 1935 if (T->getQualifier()) { 1936 ToQualifier = Importer.Import(T->getQualifier()); 1937 if (!ToQualifier) 1938 return QualType(); 1939 } 1940 1941 QualType ToNamedType = Importer.Import(T->getNamedType()); 1942 if (ToNamedType.isNull()) 1943 return QualType(); 1944 1945 return Importer.getToContext().getElaboratedType(T->getKeyword(), 1946 ToQualifier, ToNamedType); 1947 } 1948 1949 QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { 1950 ObjCInterfaceDecl *Class 1951 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl())); 1952 if (!Class) 1953 return QualType(); 1954 1955 return Importer.getToContext().getObjCInterfaceType(Class); 1956 } 1957 1958 QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { 1959 QualType ToBaseType = Importer.Import(T->getBaseType()); 1960 if (ToBaseType.isNull()) 1961 return QualType(); 1962 1963 SmallVector<QualType, 4> TypeArgs; 1964 for (auto TypeArg : T->getTypeArgsAsWritten()) { 1965 QualType ImportedTypeArg = Importer.Import(TypeArg); 1966 if (ImportedTypeArg.isNull()) 1967 return QualType(); 1968 1969 TypeArgs.push_back(ImportedTypeArg); 1970 } 1971 1972 SmallVector<ObjCProtocolDecl *, 4> Protocols; 1973 for (auto *P : T->quals()) { 1974 ObjCProtocolDecl *Protocol 1975 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P)); 1976 if (!Protocol) 1977 return QualType(); 1978 Protocols.push_back(Protocol); 1979 } 1980 1981 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs, 1982 Protocols, 1983 T->isKindOfTypeAsWritten()); 1984 } 1985 1986 QualType 1987 ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { 1988 QualType ToPointeeType = Importer.Import(T->getPointeeType()); 1989 if (ToPointeeType.isNull()) 1990 return QualType(); 1991 1992 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType); 1993 } 1994 1995 //---------------------------------------------------------------------------- 1996 // Import Declarations 1997 //---------------------------------------------------------------------------- 1998 bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC, 1999 DeclContext *&LexicalDC, 2000 DeclarationName &Name, 2001 NamedDecl *&ToD, 2002 SourceLocation &Loc) { 2003 // Import the context of this declaration. 2004 DC = Importer.ImportContext(D->getDeclContext()); 2005 if (!DC) 2006 return true; 2007 2008 LexicalDC = DC; 2009 if (D->getDeclContext() != D->getLexicalDeclContext()) { 2010 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 2011 if (!LexicalDC) 2012 return true; 2013 } 2014 2015 // Import the name of this declaration. 2016 Name = Importer.Import(D->getDeclName()); 2017 if (D->getDeclName() && !Name) 2018 return true; 2019 2020 // Import the location of this declaration. 2021 Loc = Importer.Import(D->getLocation()); 2022 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D)); 2023 return false; 2024 } 2025 2026 void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { 2027 if (!FromD) 2028 return; 2029 2030 if (!ToD) { 2031 ToD = Importer.Import(FromD); 2032 if (!ToD) 2033 return; 2034 } 2035 2036 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) { 2037 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) { 2038 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) { 2039 ImportDefinition(FromRecord, ToRecord); 2040 } 2041 } 2042 return; 2043 } 2044 2045 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) { 2046 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) { 2047 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { 2048 ImportDefinition(FromEnum, ToEnum); 2049 } 2050 } 2051 return; 2052 } 2053 } 2054 2055 void 2056 ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From, 2057 DeclarationNameInfo& To) { 2058 // NOTE: To.Name and To.Loc are already imported. 2059 // We only have to import To.LocInfo. 2060 switch (To.getName().getNameKind()) { 2061 case DeclarationName::Identifier: 2062 case DeclarationName::ObjCZeroArgSelector: 2063 case DeclarationName::ObjCOneArgSelector: 2064 case DeclarationName::ObjCMultiArgSelector: 2065 case DeclarationName::CXXUsingDirective: 2066 return; 2067 2068 case DeclarationName::CXXOperatorName: { 2069 SourceRange Range = From.getCXXOperatorNameRange(); 2070 To.setCXXOperatorNameRange(Importer.Import(Range)); 2071 return; 2072 } 2073 case DeclarationName::CXXLiteralOperatorName: { 2074 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc(); 2075 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc)); 2076 return; 2077 } 2078 case DeclarationName::CXXConstructorName: 2079 case DeclarationName::CXXDestructorName: 2080 case DeclarationName::CXXConversionFunctionName: { 2081 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo(); 2082 To.setNamedTypeInfo(Importer.Import(FromTInfo)); 2083 return; 2084 } 2085 } 2086 llvm_unreachable("Unknown name kind."); 2087 } 2088 2089 void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { 2090 if (Importer.isMinimalImport() && !ForceImport) { 2091 Importer.ImportContext(FromDC); 2092 return; 2093 } 2094 2095 for (auto *From : FromDC->decls()) 2096 Importer.Import(From); 2097 } 2098 2099 bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To, 2100 ImportDefinitionKind Kind) { 2101 if (To->getDefinition() || To->isBeingDefined()) { 2102 if (Kind == IDK_Everything) 2103 ImportDeclContext(From, /*ForceImport=*/true); 2104 2105 return false; 2106 } 2107 2108 To->startDefinition(); 2109 2110 // Add base classes. 2111 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) { 2112 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From); 2113 2114 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); 2115 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); 2116 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor; 2117 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers; 2118 ToData.Aggregate = FromData.Aggregate; 2119 ToData.PlainOldData = FromData.PlainOldData; 2120 ToData.Empty = FromData.Empty; 2121 ToData.Polymorphic = FromData.Polymorphic; 2122 ToData.Abstract = FromData.Abstract; 2123 ToData.IsStandardLayout = FromData.IsStandardLayout; 2124 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases; 2125 ToData.HasPrivateFields = FromData.HasPrivateFields; 2126 ToData.HasProtectedFields = FromData.HasProtectedFields; 2127 ToData.HasPublicFields = FromData.HasPublicFields; 2128 ToData.HasMutableFields = FromData.HasMutableFields; 2129 ToData.HasVariantMembers = FromData.HasVariantMembers; 2130 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers; 2131 ToData.HasInClassInitializer = FromData.HasInClassInitializer; 2132 ToData.HasUninitializedReferenceMember 2133 = FromData.HasUninitializedReferenceMember; 2134 ToData.HasUninitializedFields = FromData.HasUninitializedFields; 2135 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor; 2136 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment; 2137 ToData.NeedOverloadResolutionForMoveConstructor 2138 = FromData.NeedOverloadResolutionForMoveConstructor; 2139 ToData.NeedOverloadResolutionForMoveAssignment 2140 = FromData.NeedOverloadResolutionForMoveAssignment; 2141 ToData.NeedOverloadResolutionForDestructor 2142 = FromData.NeedOverloadResolutionForDestructor; 2143 ToData.DefaultedMoveConstructorIsDeleted 2144 = FromData.DefaultedMoveConstructorIsDeleted; 2145 ToData.DefaultedMoveAssignmentIsDeleted 2146 = FromData.DefaultedMoveAssignmentIsDeleted; 2147 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted; 2148 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers; 2149 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor; 2150 ToData.HasConstexprNonCopyMoveConstructor 2151 = FromData.HasConstexprNonCopyMoveConstructor; 2152 ToData.HasDefaultedDefaultConstructor 2153 = FromData.HasDefaultedDefaultConstructor; 2154 ToData.DefaultedDefaultConstructorIsConstexpr 2155 = FromData.DefaultedDefaultConstructorIsConstexpr; 2156 ToData.HasConstexprDefaultConstructor 2157 = FromData.HasConstexprDefaultConstructor; 2158 ToData.HasNonLiteralTypeFieldsOrBases 2159 = FromData.HasNonLiteralTypeFieldsOrBases; 2160 // ComputedVisibleConversions not imported. 2161 ToData.UserProvidedDefaultConstructor 2162 = FromData.UserProvidedDefaultConstructor; 2163 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers; 2164 ToData.ImplicitCopyConstructorHasConstParam 2165 = FromData.ImplicitCopyConstructorHasConstParam; 2166 ToData.ImplicitCopyAssignmentHasConstParam 2167 = FromData.ImplicitCopyAssignmentHasConstParam; 2168 ToData.HasDeclaredCopyConstructorWithConstParam 2169 = FromData.HasDeclaredCopyConstructorWithConstParam; 2170 ToData.HasDeclaredCopyAssignmentWithConstParam 2171 = FromData.HasDeclaredCopyAssignmentWithConstParam; 2172 ToData.IsLambda = FromData.IsLambda; 2173 2174 SmallVector<CXXBaseSpecifier *, 4> Bases; 2175 for (const auto &Base1 : FromCXX->bases()) { 2176 QualType T = Importer.Import(Base1.getType()); 2177 if (T.isNull()) 2178 return true; 2179 2180 SourceLocation EllipsisLoc; 2181 if (Base1.isPackExpansion()) 2182 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc()); 2183 2184 // Ensure that we have a definition for the base. 2185 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()); 2186 2187 Bases.push_back( 2188 new (Importer.getToContext()) 2189 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()), 2190 Base1.isVirtual(), 2191 Base1.isBaseOfClass(), 2192 Base1.getAccessSpecifierAsWritten(), 2193 Importer.Import(Base1.getTypeSourceInfo()), 2194 EllipsisLoc)); 2195 } 2196 if (!Bases.empty()) 2197 ToCXX->setBases(Bases.data(), Bases.size()); 2198 } 2199 2200 if (shouldForceImportDeclContext(Kind)) 2201 ImportDeclContext(From, /*ForceImport=*/true); 2202 2203 To->completeDefinition(); 2204 return false; 2205 } 2206 2207 bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To, 2208 ImportDefinitionKind Kind) { 2209 if (To->getAnyInitializer()) 2210 return false; 2211 2212 // FIXME: Can we really import any initializer? Alternatively, we could force 2213 // ourselves to import every declaration of a variable and then only use 2214 // getInit() here. 2215 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer()))); 2216 2217 // FIXME: Other bits to merge? 2218 2219 return false; 2220 } 2221 2222 bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To, 2223 ImportDefinitionKind Kind) { 2224 if (To->getDefinition() || To->isBeingDefined()) { 2225 if (Kind == IDK_Everything) 2226 ImportDeclContext(From, /*ForceImport=*/true); 2227 return false; 2228 } 2229 2230 To->startDefinition(); 2231 2232 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From)); 2233 if (T.isNull()) 2234 return true; 2235 2236 QualType ToPromotionType = Importer.Import(From->getPromotionType()); 2237 if (ToPromotionType.isNull()) 2238 return true; 2239 2240 if (shouldForceImportDeclContext(Kind)) 2241 ImportDeclContext(From, /*ForceImport=*/true); 2242 2243 // FIXME: we might need to merge the number of positive or negative bits 2244 // if the enumerator lists don't match. 2245 To->completeDefinition(T, ToPromotionType, 2246 From->getNumPositiveBits(), 2247 From->getNumNegativeBits()); 2248 return false; 2249 } 2250 2251 TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList( 2252 TemplateParameterList *Params) { 2253 SmallVector<NamedDecl *, 4> ToParams; 2254 ToParams.reserve(Params->size()); 2255 for (TemplateParameterList::iterator P = Params->begin(), 2256 PEnd = Params->end(); 2257 P != PEnd; ++P) { 2258 Decl *To = Importer.Import(*P); 2259 if (!To) 2260 return nullptr; 2261 2262 ToParams.push_back(cast<NamedDecl>(To)); 2263 } 2264 2265 return TemplateParameterList::Create(Importer.getToContext(), 2266 Importer.Import(Params->getTemplateLoc()), 2267 Importer.Import(Params->getLAngleLoc()), 2268 ToParams, 2269 Importer.Import(Params->getRAngleLoc())); 2270 } 2271 2272 TemplateArgument 2273 ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { 2274 switch (From.getKind()) { 2275 case TemplateArgument::Null: 2276 return TemplateArgument(); 2277 2278 case TemplateArgument::Type: { 2279 QualType ToType = Importer.Import(From.getAsType()); 2280 if (ToType.isNull()) 2281 return TemplateArgument(); 2282 return TemplateArgument(ToType); 2283 } 2284 2285 case TemplateArgument::Integral: { 2286 QualType ToType = Importer.Import(From.getIntegralType()); 2287 if (ToType.isNull()) 2288 return TemplateArgument(); 2289 return TemplateArgument(From, ToType); 2290 } 2291 2292 case TemplateArgument::Declaration: { 2293 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl())); 2294 QualType ToType = Importer.Import(From.getParamTypeForDecl()); 2295 if (!To || ToType.isNull()) 2296 return TemplateArgument(); 2297 return TemplateArgument(To, ToType); 2298 } 2299 2300 case TemplateArgument::NullPtr: { 2301 QualType ToType = Importer.Import(From.getNullPtrType()); 2302 if (ToType.isNull()) 2303 return TemplateArgument(); 2304 return TemplateArgument(ToType, /*isNullPtr*/true); 2305 } 2306 2307 case TemplateArgument::Template: { 2308 TemplateName ToTemplate = Importer.Import(From.getAsTemplate()); 2309 if (ToTemplate.isNull()) 2310 return TemplateArgument(); 2311 2312 return TemplateArgument(ToTemplate); 2313 } 2314 2315 case TemplateArgument::TemplateExpansion: { 2316 TemplateName ToTemplate 2317 = Importer.Import(From.getAsTemplateOrTemplatePattern()); 2318 if (ToTemplate.isNull()) 2319 return TemplateArgument(); 2320 2321 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions()); 2322 } 2323 2324 case TemplateArgument::Expression: 2325 if (Expr *ToExpr = Importer.Import(From.getAsExpr())) 2326 return TemplateArgument(ToExpr); 2327 return TemplateArgument(); 2328 2329 case TemplateArgument::Pack: { 2330 SmallVector<TemplateArgument, 2> ToPack; 2331 ToPack.reserve(From.pack_size()); 2332 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack)) 2333 return TemplateArgument(); 2334 2335 return TemplateArgument( 2336 llvm::makeArrayRef(ToPack).copy(Importer.getToContext())); 2337 } 2338 } 2339 2340 llvm_unreachable("Invalid template argument kind"); 2341 } 2342 2343 bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs, 2344 unsigned NumFromArgs, 2345 SmallVectorImpl<TemplateArgument> &ToArgs) { 2346 for (unsigned I = 0; I != NumFromArgs; ++I) { 2347 TemplateArgument To = ImportTemplateArgument(FromArgs[I]); 2348 if (To.isNull() && !FromArgs[I].isNull()) 2349 return true; 2350 2351 ToArgs.push_back(To); 2352 } 2353 2354 return false; 2355 } 2356 2357 bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, 2358 RecordDecl *ToRecord, bool Complain) { 2359 // Eliminate a potential failure point where we attempt to re-import 2360 // something we're trying to import while completing ToRecord. 2361 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord); 2362 if (ToOrigin) { 2363 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin); 2364 if (ToOriginRecord) 2365 ToRecord = ToOriginRecord; 2366 } 2367 2368 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 2369 ToRecord->getASTContext(), 2370 Importer.getNonEquivalentDecls(), 2371 false, Complain); 2372 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord); 2373 } 2374 2375 bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, 2376 bool Complain) { 2377 StructuralEquivalenceContext Ctx( 2378 Importer.getFromContext(), Importer.getToContext(), 2379 Importer.getNonEquivalentDecls(), false, Complain); 2380 return Ctx.IsStructurallyEquivalent(FromVar, ToVar); 2381 } 2382 2383 bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { 2384 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 2385 Importer.getToContext(), 2386 Importer.getNonEquivalentDecls()); 2387 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum); 2388 } 2389 2390 bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC, 2391 EnumConstantDecl *ToEC) 2392 { 2393 const llvm::APSInt &FromVal = FromEC->getInitVal(); 2394 const llvm::APSInt &ToVal = ToEC->getInitVal(); 2395 2396 return FromVal.isSigned() == ToVal.isSigned() && 2397 FromVal.getBitWidth() == ToVal.getBitWidth() && 2398 FromVal == ToVal; 2399 } 2400 2401 bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From, 2402 ClassTemplateDecl *To) { 2403 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 2404 Importer.getToContext(), 2405 Importer.getNonEquivalentDecls()); 2406 return Ctx.IsStructurallyEquivalent(From, To); 2407 } 2408 2409 bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From, 2410 VarTemplateDecl *To) { 2411 StructuralEquivalenceContext Ctx(Importer.getFromContext(), 2412 Importer.getToContext(), 2413 Importer.getNonEquivalentDecls()); 2414 return Ctx.IsStructurallyEquivalent(From, To); 2415 } 2416 2417 Decl *ASTNodeImporter::VisitDecl(Decl *D) { 2418 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) 2419 << D->getDeclKindName(); 2420 return nullptr; 2421 } 2422 2423 Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 2424 TranslationUnitDecl *ToD = 2425 Importer.getToContext().getTranslationUnitDecl(); 2426 2427 Importer.Imported(D, ToD); 2428 2429 return ToD; 2430 } 2431 2432 Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { 2433 2434 SourceLocation Loc = Importer.Import(D->getLocation()); 2435 SourceLocation ColonLoc = Importer.Import(D->getColonLoc()); 2436 2437 // Import the context of this declaration. 2438 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 2439 if (!DC) 2440 return nullptr; 2441 2442 AccessSpecDecl *accessSpecDecl 2443 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(), 2444 DC, Loc, ColonLoc); 2445 2446 if (!accessSpecDecl) 2447 return nullptr; 2448 2449 // Lexical DeclContext and Semantic DeclContext 2450 // is always the same for the accessSpec. 2451 accessSpecDecl->setLexicalDeclContext(DC); 2452 DC->addDeclInternal(accessSpecDecl); 2453 2454 return accessSpecDecl; 2455 } 2456 2457 Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { 2458 // Import the major distinguishing characteristics of this namespace. 2459 DeclContext *DC, *LexicalDC; 2460 DeclarationName Name; 2461 SourceLocation Loc; 2462 NamedDecl *ToD; 2463 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2464 return nullptr; 2465 if (ToD) 2466 return ToD; 2467 2468 NamespaceDecl *MergeWithNamespace = nullptr; 2469 if (!Name) { 2470 // This is an anonymous namespace. Adopt an existing anonymous 2471 // namespace if we can. 2472 // FIXME: Not testable. 2473 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) 2474 MergeWithNamespace = TU->getAnonymousNamespace(); 2475 else 2476 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace(); 2477 } else { 2478 SmallVector<NamedDecl *, 4> ConflictingDecls; 2479 SmallVector<NamedDecl *, 2> FoundDecls; 2480 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2481 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2482 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace)) 2483 continue; 2484 2485 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) { 2486 MergeWithNamespace = FoundNS; 2487 ConflictingDecls.clear(); 2488 break; 2489 } 2490 2491 ConflictingDecls.push_back(FoundDecls[I]); 2492 } 2493 2494 if (!ConflictingDecls.empty()) { 2495 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace, 2496 ConflictingDecls.data(), 2497 ConflictingDecls.size()); 2498 } 2499 } 2500 2501 // Create the "to" namespace, if needed. 2502 NamespaceDecl *ToNamespace = MergeWithNamespace; 2503 if (!ToNamespace) { 2504 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, 2505 D->isInline(), 2506 Importer.Import(D->getLocStart()), 2507 Loc, Name.getAsIdentifierInfo(), 2508 /*PrevDecl=*/nullptr); 2509 ToNamespace->setLexicalDeclContext(LexicalDC); 2510 LexicalDC->addDeclInternal(ToNamespace); 2511 2512 // If this is an anonymous namespace, register it as the anonymous 2513 // namespace within its context. 2514 if (!Name) { 2515 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) 2516 TU->setAnonymousNamespace(ToNamespace); 2517 else 2518 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace); 2519 } 2520 } 2521 Importer.Imported(D, ToNamespace); 2522 2523 ImportDeclContext(D); 2524 2525 return ToNamespace; 2526 } 2527 2528 Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { 2529 // Import the major distinguishing characteristics of this typedef. 2530 DeclContext *DC, *LexicalDC; 2531 DeclarationName Name; 2532 SourceLocation Loc; 2533 NamedDecl *ToD; 2534 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2535 return nullptr; 2536 if (ToD) 2537 return ToD; 2538 2539 // If this typedef is not in block scope, determine whether we've 2540 // seen a typedef with the same name (that we can merge with) or any 2541 // other entity by that name (which name lookup could conflict with). 2542 if (!DC->isFunctionOrMethod()) { 2543 SmallVector<NamedDecl *, 4> ConflictingDecls; 2544 unsigned IDNS = Decl::IDNS_Ordinary; 2545 SmallVector<NamedDecl *, 2> FoundDecls; 2546 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2547 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2548 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2549 continue; 2550 if (TypedefNameDecl *FoundTypedef = 2551 dyn_cast<TypedefNameDecl>(FoundDecls[I])) { 2552 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(), 2553 FoundTypedef->getUnderlyingType())) 2554 return Importer.Imported(D, FoundTypedef); 2555 } 2556 2557 ConflictingDecls.push_back(FoundDecls[I]); 2558 } 2559 2560 if (!ConflictingDecls.empty()) { 2561 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2562 ConflictingDecls.data(), 2563 ConflictingDecls.size()); 2564 if (!Name) 2565 return nullptr; 2566 } 2567 } 2568 2569 // Import the underlying type of this typedef; 2570 QualType T = Importer.Import(D->getUnderlyingType()); 2571 if (T.isNull()) 2572 return nullptr; 2573 2574 // Create the new typedef node. 2575 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 2576 SourceLocation StartL = Importer.Import(D->getLocStart()); 2577 TypedefNameDecl *ToTypedef; 2578 if (IsAlias) 2579 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, 2580 StartL, Loc, 2581 Name.getAsIdentifierInfo(), 2582 TInfo); 2583 else 2584 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC, 2585 StartL, Loc, 2586 Name.getAsIdentifierInfo(), 2587 TInfo); 2588 2589 ToTypedef->setAccess(D->getAccess()); 2590 ToTypedef->setLexicalDeclContext(LexicalDC); 2591 Importer.Imported(D, ToTypedef); 2592 LexicalDC->addDeclInternal(ToTypedef); 2593 2594 return ToTypedef; 2595 } 2596 2597 Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { 2598 return VisitTypedefNameDecl(D, /*IsAlias=*/false); 2599 } 2600 2601 Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { 2602 return VisitTypedefNameDecl(D, /*IsAlias=*/true); 2603 } 2604 2605 Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) { 2606 // Import the major distinguishing characteristics of this label. 2607 DeclContext *DC, *LexicalDC; 2608 DeclarationName Name; 2609 SourceLocation Loc; 2610 NamedDecl *ToD; 2611 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2612 return nullptr; 2613 if (ToD) 2614 return ToD; 2615 2616 assert(LexicalDC->isFunctionOrMethod()); 2617 2618 LabelDecl *ToLabel = D->isGnuLocal() 2619 ? LabelDecl::Create(Importer.getToContext(), 2620 DC, Importer.Import(D->getLocation()), 2621 Name.getAsIdentifierInfo(), 2622 Importer.Import(D->getLocStart())) 2623 : LabelDecl::Create(Importer.getToContext(), 2624 DC, Importer.Import(D->getLocation()), 2625 Name.getAsIdentifierInfo()); 2626 Importer.Imported(D, ToLabel); 2627 2628 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt())); 2629 if (!Label) 2630 return nullptr; 2631 2632 ToLabel->setStmt(Label); 2633 ToLabel->setLexicalDeclContext(LexicalDC); 2634 LexicalDC->addDeclInternal(ToLabel); 2635 return ToLabel; 2636 } 2637 2638 Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { 2639 // Import the major distinguishing characteristics of this enum. 2640 DeclContext *DC, *LexicalDC; 2641 DeclarationName Name; 2642 SourceLocation Loc; 2643 NamedDecl *ToD; 2644 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2645 return nullptr; 2646 if (ToD) 2647 return ToD; 2648 2649 // Figure out what enum name we're looking for. 2650 unsigned IDNS = Decl::IDNS_Tag; 2651 DeclarationName SearchName = Name; 2652 if (!SearchName && D->getTypedefNameForAnonDecl()) { 2653 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); 2654 IDNS = Decl::IDNS_Ordinary; 2655 } else if (Importer.getToContext().getLangOpts().CPlusPlus) 2656 IDNS |= Decl::IDNS_Ordinary; 2657 2658 // We may already have an enum of the same name; try to find and match it. 2659 if (!DC->isFunctionOrMethod() && SearchName) { 2660 SmallVector<NamedDecl *, 4> ConflictingDecls; 2661 SmallVector<NamedDecl *, 2> FoundDecls; 2662 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2663 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2664 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2665 continue; 2666 2667 Decl *Found = FoundDecls[I]; 2668 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { 2669 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) 2670 Found = Tag->getDecl(); 2671 } 2672 2673 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) { 2674 if (IsStructuralMatch(D, FoundEnum)) 2675 return Importer.Imported(D, FoundEnum); 2676 } 2677 2678 ConflictingDecls.push_back(FoundDecls[I]); 2679 } 2680 2681 if (!ConflictingDecls.empty()) { 2682 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2683 ConflictingDecls.data(), 2684 ConflictingDecls.size()); 2685 } 2686 } 2687 2688 // Create the enum declaration. 2689 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, 2690 Importer.Import(D->getLocStart()), 2691 Loc, Name.getAsIdentifierInfo(), nullptr, 2692 D->isScoped(), D->isScopedUsingClassTag(), 2693 D->isFixed()); 2694 // Import the qualifier, if any. 2695 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 2696 D2->setAccess(D->getAccess()); 2697 D2->setLexicalDeclContext(LexicalDC); 2698 Importer.Imported(D, D2); 2699 LexicalDC->addDeclInternal(D2); 2700 2701 // Import the integer type. 2702 QualType ToIntegerType = Importer.Import(D->getIntegerType()); 2703 if (ToIntegerType.isNull()) 2704 return nullptr; 2705 D2->setIntegerType(ToIntegerType); 2706 2707 // Import the definition 2708 if (D->isCompleteDefinition() && ImportDefinition(D, D2)) 2709 return nullptr; 2710 2711 return D2; 2712 } 2713 2714 Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { 2715 // If this record has a definition in the translation unit we're coming from, 2716 // but this particular declaration is not that definition, import the 2717 // definition and map to that. 2718 TagDecl *Definition = D->getDefinition(); 2719 if (Definition && Definition != D) { 2720 Decl *ImportedDef = Importer.Import(Definition); 2721 if (!ImportedDef) 2722 return nullptr; 2723 2724 return Importer.Imported(D, ImportedDef); 2725 } 2726 2727 // Import the major distinguishing characteristics of this record. 2728 DeclContext *DC, *LexicalDC; 2729 DeclarationName Name; 2730 SourceLocation Loc; 2731 NamedDecl *ToD; 2732 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2733 return nullptr; 2734 if (ToD) 2735 return ToD; 2736 2737 // Figure out what structure name we're looking for. 2738 unsigned IDNS = Decl::IDNS_Tag; 2739 DeclarationName SearchName = Name; 2740 if (!SearchName && D->getTypedefNameForAnonDecl()) { 2741 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); 2742 IDNS = Decl::IDNS_Ordinary; 2743 } else if (Importer.getToContext().getLangOpts().CPlusPlus) 2744 IDNS |= Decl::IDNS_Ordinary; 2745 2746 // We may already have a record of the same name; try to find and match it. 2747 RecordDecl *AdoptDecl = nullptr; 2748 if (!DC->isFunctionOrMethod()) { 2749 SmallVector<NamedDecl *, 4> ConflictingDecls; 2750 SmallVector<NamedDecl *, 2> FoundDecls; 2751 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2752 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2753 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2754 continue; 2755 2756 Decl *Found = FoundDecls[I]; 2757 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { 2758 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) 2759 Found = Tag->getDecl(); 2760 } 2761 2762 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) { 2763 if (D->isAnonymousStructOrUnion() && 2764 FoundRecord->isAnonymousStructOrUnion()) { 2765 // If both anonymous structs/unions are in a record context, make sure 2766 // they occur in the same location in the context records. 2767 if (Optional<unsigned> Index1 2768 = findUntaggedStructOrUnionIndex(D)) { 2769 if (Optional<unsigned> Index2 = 2770 findUntaggedStructOrUnionIndex(FoundRecord)) { 2771 if (*Index1 != *Index2) 2772 continue; 2773 } 2774 } 2775 } 2776 2777 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { 2778 if ((SearchName && !D->isCompleteDefinition()) 2779 || (D->isCompleteDefinition() && 2780 D->isAnonymousStructOrUnion() 2781 == FoundDef->isAnonymousStructOrUnion() && 2782 IsStructuralMatch(D, FoundDef))) { 2783 // The record types structurally match, or the "from" translation 2784 // unit only had a forward declaration anyway; call it the same 2785 // function. 2786 // FIXME: For C++, we should also merge methods here. 2787 return Importer.Imported(D, FoundDef); 2788 } 2789 } else if (!D->isCompleteDefinition()) { 2790 // We have a forward declaration of this type, so adopt that forward 2791 // declaration rather than building a new one. 2792 2793 // If one or both can be completed from external storage then try one 2794 // last time to complete and compare them before doing this. 2795 2796 if (FoundRecord->hasExternalLexicalStorage() && 2797 !FoundRecord->isCompleteDefinition()) 2798 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord); 2799 if (D->hasExternalLexicalStorage()) 2800 D->getASTContext().getExternalSource()->CompleteType(D); 2801 2802 if (FoundRecord->isCompleteDefinition() && 2803 D->isCompleteDefinition() && 2804 !IsStructuralMatch(D, FoundRecord)) 2805 continue; 2806 2807 AdoptDecl = FoundRecord; 2808 continue; 2809 } else if (!SearchName) { 2810 continue; 2811 } 2812 } 2813 2814 ConflictingDecls.push_back(FoundDecls[I]); 2815 } 2816 2817 if (!ConflictingDecls.empty() && SearchName) { 2818 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2819 ConflictingDecls.data(), 2820 ConflictingDecls.size()); 2821 } 2822 } 2823 2824 // Create the record declaration. 2825 RecordDecl *D2 = AdoptDecl; 2826 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 2827 if (!D2) { 2828 CXXRecordDecl *D2CXX = nullptr; 2829 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) { 2830 if (DCXX->isLambda()) { 2831 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo()); 2832 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(), 2833 DC, TInfo, Loc, 2834 DCXX->isDependentLambda(), 2835 DCXX->isGenericLambda(), 2836 DCXX->getLambdaCaptureDefault()); 2837 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl()); 2838 if (DCXX->getLambdaContextDecl() && !CDecl) 2839 return nullptr; 2840 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl); 2841 } else if (DCXX->isInjectedClassName()) { 2842 // We have to be careful to do a similar dance to the one in 2843 // Sema::ActOnStartCXXMemberDeclarations 2844 CXXRecordDecl *const PrevDecl = nullptr; 2845 const bool DelayTypeCreation = true; 2846 D2CXX = CXXRecordDecl::Create( 2847 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc, 2848 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation); 2849 Importer.getToContext().getTypeDeclType( 2850 D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC)); 2851 } else { 2852 D2CXX = CXXRecordDecl::Create(Importer.getToContext(), 2853 D->getTagKind(), 2854 DC, StartLoc, Loc, 2855 Name.getAsIdentifierInfo()); 2856 } 2857 D2 = D2CXX; 2858 D2->setAccess(D->getAccess()); 2859 } else { 2860 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(), 2861 DC, StartLoc, Loc, Name.getAsIdentifierInfo()); 2862 } 2863 2864 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 2865 D2->setLexicalDeclContext(LexicalDC); 2866 LexicalDC->addDeclInternal(D2); 2867 if (D->isAnonymousStructOrUnion()) 2868 D2->setAnonymousStructOrUnion(true); 2869 } 2870 2871 Importer.Imported(D, D2); 2872 2873 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default)) 2874 return nullptr; 2875 2876 return D2; 2877 } 2878 2879 Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { 2880 // Import the major distinguishing characteristics of this enumerator. 2881 DeclContext *DC, *LexicalDC; 2882 DeclarationName Name; 2883 SourceLocation Loc; 2884 NamedDecl *ToD; 2885 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2886 return nullptr; 2887 if (ToD) 2888 return ToD; 2889 2890 QualType T = Importer.Import(D->getType()); 2891 if (T.isNull()) 2892 return nullptr; 2893 2894 // Determine whether there are any other declarations with the same name and 2895 // in the same context. 2896 if (!LexicalDC->isFunctionOrMethod()) { 2897 SmallVector<NamedDecl *, 4> ConflictingDecls; 2898 unsigned IDNS = Decl::IDNS_Ordinary; 2899 SmallVector<NamedDecl *, 2> FoundDecls; 2900 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2901 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2902 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2903 continue; 2904 2905 if (EnumConstantDecl *FoundEnumConstant 2906 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) { 2907 if (IsStructuralMatch(D, FoundEnumConstant)) 2908 return Importer.Imported(D, FoundEnumConstant); 2909 } 2910 2911 ConflictingDecls.push_back(FoundDecls[I]); 2912 } 2913 2914 if (!ConflictingDecls.empty()) { 2915 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2916 ConflictingDecls.data(), 2917 ConflictingDecls.size()); 2918 if (!Name) 2919 return nullptr; 2920 } 2921 } 2922 2923 Expr *Init = Importer.Import(D->getInitExpr()); 2924 if (D->getInitExpr() && !Init) 2925 return nullptr; 2926 2927 EnumConstantDecl *ToEnumerator 2928 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc, 2929 Name.getAsIdentifierInfo(), T, 2930 Init, D->getInitVal()); 2931 ToEnumerator->setAccess(D->getAccess()); 2932 ToEnumerator->setLexicalDeclContext(LexicalDC); 2933 Importer.Imported(D, ToEnumerator); 2934 LexicalDC->addDeclInternal(ToEnumerator); 2935 return ToEnumerator; 2936 } 2937 2938 Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { 2939 // Import the major distinguishing characteristics of this function. 2940 DeclContext *DC, *LexicalDC; 2941 DeclarationName Name; 2942 SourceLocation Loc; 2943 NamedDecl *ToD; 2944 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2945 return nullptr; 2946 if (ToD) 2947 return ToD; 2948 2949 // Try to find a function in our own ("to") context with the same name, same 2950 // type, and in the same context as the function we're importing. 2951 if (!LexicalDC->isFunctionOrMethod()) { 2952 SmallVector<NamedDecl *, 4> ConflictingDecls; 2953 unsigned IDNS = Decl::IDNS_Ordinary; 2954 SmallVector<NamedDecl *, 2> FoundDecls; 2955 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2956 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2957 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 2958 continue; 2959 2960 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) { 2961 if (FoundFunction->hasExternalFormalLinkage() && 2962 D->hasExternalFormalLinkage()) { 2963 if (Importer.IsStructurallyEquivalent(D->getType(), 2964 FoundFunction->getType())) { 2965 // FIXME: Actually try to merge the body and other attributes. 2966 return Importer.Imported(D, FoundFunction); 2967 } 2968 2969 // FIXME: Check for overloading more carefully, e.g., by boosting 2970 // Sema::IsOverload out to the AST library. 2971 2972 // Function overloading is okay in C++. 2973 if (Importer.getToContext().getLangOpts().CPlusPlus) 2974 continue; 2975 2976 // Complain about inconsistent function types. 2977 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) 2978 << Name << D->getType() << FoundFunction->getType(); 2979 Importer.ToDiag(FoundFunction->getLocation(), 2980 diag::note_odr_value_here) 2981 << FoundFunction->getType(); 2982 } 2983 } 2984 2985 ConflictingDecls.push_back(FoundDecls[I]); 2986 } 2987 2988 if (!ConflictingDecls.empty()) { 2989 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2990 ConflictingDecls.data(), 2991 ConflictingDecls.size()); 2992 if (!Name) 2993 return nullptr; 2994 } 2995 } 2996 2997 DeclarationNameInfo NameInfo(Name, Loc); 2998 // Import additional name location/type info. 2999 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); 3000 3001 QualType FromTy = D->getType(); 3002 bool usedDifferentExceptionSpec = false; 3003 3004 if (const FunctionProtoType * 3005 FromFPT = D->getType()->getAs<FunctionProtoType>()) { 3006 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); 3007 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the 3008 // FunctionDecl that we are importing the FunctionProtoType for. 3009 // To avoid an infinite recursion when importing, create the FunctionDecl 3010 // with a simplified function type and update it afterwards. 3011 if (FromEPI.ExceptionSpec.SourceDecl || 3012 FromEPI.ExceptionSpec.SourceTemplate || 3013 FromEPI.ExceptionSpec.NoexceptExpr) { 3014 FunctionProtoType::ExtProtoInfo DefaultEPI; 3015 FromTy = Importer.getFromContext().getFunctionType( 3016 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI); 3017 usedDifferentExceptionSpec = true; 3018 } 3019 } 3020 3021 // Import the type. 3022 QualType T = Importer.Import(FromTy); 3023 if (T.isNull()) 3024 return nullptr; 3025 3026 // Import the function parameters. 3027 SmallVector<ParmVarDecl *, 8> Parameters; 3028 for (auto P : D->parameters()) { 3029 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P)); 3030 if (!ToP) 3031 return nullptr; 3032 3033 Parameters.push_back(ToP); 3034 } 3035 3036 // Create the imported function. 3037 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3038 FunctionDecl *ToFunction = nullptr; 3039 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart()); 3040 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { 3041 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(), 3042 cast<CXXRecordDecl>(DC), 3043 InnerLocStart, 3044 NameInfo, T, TInfo, 3045 FromConstructor->isExplicit(), 3046 D->isInlineSpecified(), 3047 D->isImplicit(), 3048 D->isConstexpr()); 3049 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) { 3050 SmallVector<CXXCtorInitializer *, 4> CtorInitializers; 3051 for (CXXCtorInitializer *I : FromConstructor->inits()) { 3052 CXXCtorInitializer *ToI = 3053 cast_or_null<CXXCtorInitializer>(Importer.Import(I)); 3054 if (!ToI && I) 3055 return nullptr; 3056 CtorInitializers.push_back(ToI); 3057 } 3058 CXXCtorInitializer **Memory = 3059 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers]; 3060 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory); 3061 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction); 3062 ToCtor->setCtorInitializers(Memory); 3063 ToCtor->setNumCtorInitializers(NumInitializers); 3064 } 3065 } else if (isa<CXXDestructorDecl>(D)) { 3066 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(), 3067 cast<CXXRecordDecl>(DC), 3068 InnerLocStart, 3069 NameInfo, T, TInfo, 3070 D->isInlineSpecified(), 3071 D->isImplicit()); 3072 } else if (CXXConversionDecl *FromConversion 3073 = dyn_cast<CXXConversionDecl>(D)) { 3074 ToFunction = CXXConversionDecl::Create(Importer.getToContext(), 3075 cast<CXXRecordDecl>(DC), 3076 InnerLocStart, 3077 NameInfo, T, TInfo, 3078 D->isInlineSpecified(), 3079 FromConversion->isExplicit(), 3080 D->isConstexpr(), 3081 Importer.Import(D->getLocEnd())); 3082 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 3083 ToFunction = CXXMethodDecl::Create(Importer.getToContext(), 3084 cast<CXXRecordDecl>(DC), 3085 InnerLocStart, 3086 NameInfo, T, TInfo, 3087 Method->getStorageClass(), 3088 Method->isInlineSpecified(), 3089 D->isConstexpr(), 3090 Importer.Import(D->getLocEnd())); 3091 } else { 3092 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, 3093 InnerLocStart, 3094 NameInfo, T, TInfo, D->getStorageClass(), 3095 D->isInlineSpecified(), 3096 D->hasWrittenPrototype(), 3097 D->isConstexpr()); 3098 } 3099 3100 // Import the qualifier, if any. 3101 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 3102 ToFunction->setAccess(D->getAccess()); 3103 ToFunction->setLexicalDeclContext(LexicalDC); 3104 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); 3105 ToFunction->setTrivial(D->isTrivial()); 3106 ToFunction->setPure(D->isPure()); 3107 Importer.Imported(D, ToFunction); 3108 3109 // Set the parameters. 3110 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) { 3111 Parameters[I]->setOwningFunction(ToFunction); 3112 ToFunction->addDeclInternal(Parameters[I]); 3113 } 3114 ToFunction->setParams(Parameters); 3115 3116 if (usedDifferentExceptionSpec) { 3117 // Update FunctionProtoType::ExtProtoInfo. 3118 QualType T = Importer.Import(D->getType()); 3119 if (T.isNull()) 3120 return nullptr; 3121 ToFunction->setType(T); 3122 } 3123 3124 // Import the body, if any. 3125 if (Stmt *FromBody = D->getBody()) { 3126 if (Stmt *ToBody = Importer.Import(FromBody)) { 3127 ToFunction->setBody(ToBody); 3128 } 3129 } 3130 3131 // FIXME: Other bits to merge? 3132 3133 // Add this function to the lexical context. 3134 LexicalDC->addDeclInternal(ToFunction); 3135 3136 return ToFunction; 3137 } 3138 3139 Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { 3140 return VisitFunctionDecl(D); 3141 } 3142 3143 Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { 3144 return VisitCXXMethodDecl(D); 3145 } 3146 3147 Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { 3148 return VisitCXXMethodDecl(D); 3149 } 3150 3151 Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { 3152 return VisitCXXMethodDecl(D); 3153 } 3154 3155 static unsigned getFieldIndex(Decl *F) { 3156 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext()); 3157 if (!Owner) 3158 return 0; 3159 3160 unsigned Index = 1; 3161 for (const auto *D : Owner->noload_decls()) { 3162 if (D == F) 3163 return Index; 3164 3165 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) 3166 ++Index; 3167 } 3168 3169 return Index; 3170 } 3171 3172 Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { 3173 // Import the major distinguishing characteristics of a variable. 3174 DeclContext *DC, *LexicalDC; 3175 DeclarationName Name; 3176 SourceLocation Loc; 3177 NamedDecl *ToD; 3178 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3179 return nullptr; 3180 if (ToD) 3181 return ToD; 3182 3183 // Determine whether we've already imported this field. 3184 SmallVector<NamedDecl *, 2> FoundDecls; 3185 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3186 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3187 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) { 3188 // For anonymous fields, match up by index. 3189 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) 3190 continue; 3191 3192 if (Importer.IsStructurallyEquivalent(D->getType(), 3193 FoundField->getType())) { 3194 Importer.Imported(D, FoundField); 3195 return FoundField; 3196 } 3197 3198 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) 3199 << Name << D->getType() << FoundField->getType(); 3200 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) 3201 << FoundField->getType(); 3202 return nullptr; 3203 } 3204 } 3205 3206 // Import the type. 3207 QualType T = Importer.Import(D->getType()); 3208 if (T.isNull()) 3209 return nullptr; 3210 3211 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3212 Expr *BitWidth = Importer.Import(D->getBitWidth()); 3213 if (!BitWidth && D->getBitWidth()) 3214 return nullptr; 3215 3216 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC, 3217 Importer.Import(D->getInnerLocStart()), 3218 Loc, Name.getAsIdentifierInfo(), 3219 T, TInfo, BitWidth, D->isMutable(), 3220 D->getInClassInitStyle()); 3221 ToField->setAccess(D->getAccess()); 3222 ToField->setLexicalDeclContext(LexicalDC); 3223 if (Expr *FromInitializer = D->getInClassInitializer()) { 3224 Expr *ToInitializer = Importer.Import(FromInitializer); 3225 if (ToInitializer) 3226 ToField->setInClassInitializer(ToInitializer); 3227 else 3228 return nullptr; 3229 } 3230 ToField->setImplicit(D->isImplicit()); 3231 Importer.Imported(D, ToField); 3232 LexicalDC->addDeclInternal(ToField); 3233 return ToField; 3234 } 3235 3236 Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { 3237 // Import the major distinguishing characteristics of a variable. 3238 DeclContext *DC, *LexicalDC; 3239 DeclarationName Name; 3240 SourceLocation Loc; 3241 NamedDecl *ToD; 3242 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3243 return nullptr; 3244 if (ToD) 3245 return ToD; 3246 3247 // Determine whether we've already imported this field. 3248 SmallVector<NamedDecl *, 2> FoundDecls; 3249 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3250 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3251 if (IndirectFieldDecl *FoundField 3252 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) { 3253 // For anonymous indirect fields, match up by index. 3254 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) 3255 continue; 3256 3257 if (Importer.IsStructurallyEquivalent(D->getType(), 3258 FoundField->getType(), 3259 !Name.isEmpty())) { 3260 Importer.Imported(D, FoundField); 3261 return FoundField; 3262 } 3263 3264 // If there are more anonymous fields to check, continue. 3265 if (!Name && I < N-1) 3266 continue; 3267 3268 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) 3269 << Name << D->getType() << FoundField->getType(); 3270 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) 3271 << FoundField->getType(); 3272 return nullptr; 3273 } 3274 } 3275 3276 // Import the type. 3277 QualType T = Importer.Import(D->getType()); 3278 if (T.isNull()) 3279 return nullptr; 3280 3281 NamedDecl **NamedChain = 3282 new (Importer.getToContext())NamedDecl*[D->getChainingSize()]; 3283 3284 unsigned i = 0; 3285 for (auto *PI : D->chain()) { 3286 Decl *D = Importer.Import(PI); 3287 if (!D) 3288 return nullptr; 3289 NamedChain[i++] = cast<NamedDecl>(D); 3290 } 3291 3292 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create( 3293 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T, 3294 {NamedChain, D->getChainingSize()}); 3295 3296 for (const auto *Attr : D->attrs()) 3297 ToIndirectField->addAttr(Attr->clone(Importer.getToContext())); 3298 3299 ToIndirectField->setAccess(D->getAccess()); 3300 ToIndirectField->setLexicalDeclContext(LexicalDC); 3301 Importer.Imported(D, ToIndirectField); 3302 LexicalDC->addDeclInternal(ToIndirectField); 3303 return ToIndirectField; 3304 } 3305 3306 Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { 3307 // Import the major distinguishing characteristics of an ivar. 3308 DeclContext *DC, *LexicalDC; 3309 DeclarationName Name; 3310 SourceLocation Loc; 3311 NamedDecl *ToD; 3312 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3313 return nullptr; 3314 if (ToD) 3315 return ToD; 3316 3317 // Determine whether we've already imported this ivar 3318 SmallVector<NamedDecl *, 2> FoundDecls; 3319 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3320 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3321 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) { 3322 if (Importer.IsStructurallyEquivalent(D->getType(), 3323 FoundIvar->getType())) { 3324 Importer.Imported(D, FoundIvar); 3325 return FoundIvar; 3326 } 3327 3328 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent) 3329 << Name << D->getType() << FoundIvar->getType(); 3330 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) 3331 << FoundIvar->getType(); 3332 return nullptr; 3333 } 3334 } 3335 3336 // Import the type. 3337 QualType T = Importer.Import(D->getType()); 3338 if (T.isNull()) 3339 return nullptr; 3340 3341 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3342 Expr *BitWidth = Importer.Import(D->getBitWidth()); 3343 if (!BitWidth && D->getBitWidth()) 3344 return nullptr; 3345 3346 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), 3347 cast<ObjCContainerDecl>(DC), 3348 Importer.Import(D->getInnerLocStart()), 3349 Loc, Name.getAsIdentifierInfo(), 3350 T, TInfo, D->getAccessControl(), 3351 BitWidth, D->getSynthesize()); 3352 ToIvar->setLexicalDeclContext(LexicalDC); 3353 Importer.Imported(D, ToIvar); 3354 LexicalDC->addDeclInternal(ToIvar); 3355 return ToIvar; 3356 3357 } 3358 3359 Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { 3360 // Import the major distinguishing characteristics of a variable. 3361 DeclContext *DC, *LexicalDC; 3362 DeclarationName Name; 3363 SourceLocation Loc; 3364 NamedDecl *ToD; 3365 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3366 return nullptr; 3367 if (ToD) 3368 return ToD; 3369 3370 // Try to find a variable in our own ("to") context with the same name and 3371 // in the same context as the variable we're importing. 3372 if (D->isFileVarDecl()) { 3373 VarDecl *MergeWithVar = nullptr; 3374 SmallVector<NamedDecl *, 4> ConflictingDecls; 3375 unsigned IDNS = Decl::IDNS_Ordinary; 3376 SmallVector<NamedDecl *, 2> FoundDecls; 3377 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3378 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3379 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) 3380 continue; 3381 3382 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) { 3383 // We have found a variable that we may need to merge with. Check it. 3384 if (FoundVar->hasExternalFormalLinkage() && 3385 D->hasExternalFormalLinkage()) { 3386 if (Importer.IsStructurallyEquivalent(D->getType(), 3387 FoundVar->getType())) { 3388 MergeWithVar = FoundVar; 3389 break; 3390 } 3391 3392 const ArrayType *FoundArray 3393 = Importer.getToContext().getAsArrayType(FoundVar->getType()); 3394 const ArrayType *TArray 3395 = Importer.getToContext().getAsArrayType(D->getType()); 3396 if (FoundArray && TArray) { 3397 if (isa<IncompleteArrayType>(FoundArray) && 3398 isa<ConstantArrayType>(TArray)) { 3399 // Import the type. 3400 QualType T = Importer.Import(D->getType()); 3401 if (T.isNull()) 3402 return nullptr; 3403 3404 FoundVar->setType(T); 3405 MergeWithVar = FoundVar; 3406 break; 3407 } else if (isa<IncompleteArrayType>(TArray) && 3408 isa<ConstantArrayType>(FoundArray)) { 3409 MergeWithVar = FoundVar; 3410 break; 3411 } 3412 } 3413 3414 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) 3415 << Name << D->getType() << FoundVar->getType(); 3416 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) 3417 << FoundVar->getType(); 3418 } 3419 } 3420 3421 ConflictingDecls.push_back(FoundDecls[I]); 3422 } 3423 3424 if (MergeWithVar) { 3425 // An equivalent variable with external linkage has been found. Link 3426 // the two declarations, then merge them. 3427 Importer.Imported(D, MergeWithVar); 3428 3429 if (VarDecl *DDef = D->getDefinition()) { 3430 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { 3431 Importer.ToDiag(ExistingDef->getLocation(), 3432 diag::err_odr_variable_multiple_def) 3433 << Name; 3434 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); 3435 } else { 3436 Expr *Init = Importer.Import(DDef->getInit()); 3437 MergeWithVar->setInit(Init); 3438 if (DDef->isInitKnownICE()) { 3439 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt(); 3440 Eval->CheckedICE = true; 3441 Eval->IsICE = DDef->isInitICE(); 3442 } 3443 } 3444 } 3445 3446 return MergeWithVar; 3447 } 3448 3449 if (!ConflictingDecls.empty()) { 3450 Name = Importer.HandleNameConflict(Name, DC, IDNS, 3451 ConflictingDecls.data(), 3452 ConflictingDecls.size()); 3453 if (!Name) 3454 return nullptr; 3455 } 3456 } 3457 3458 // Import the type. 3459 QualType T = Importer.Import(D->getType()); 3460 if (T.isNull()) 3461 return nullptr; 3462 3463 // Create the imported variable. 3464 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3465 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, 3466 Importer.Import(D->getInnerLocStart()), 3467 Loc, Name.getAsIdentifierInfo(), 3468 T, TInfo, 3469 D->getStorageClass()); 3470 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 3471 ToVar->setAccess(D->getAccess()); 3472 ToVar->setLexicalDeclContext(LexicalDC); 3473 Importer.Imported(D, ToVar); 3474 LexicalDC->addDeclInternal(ToVar); 3475 3476 if (!D->isFileVarDecl() && 3477 D->isUsed()) 3478 ToVar->setIsUsed(); 3479 3480 // Merge the initializer. 3481 if (ImportDefinition(D, ToVar)) 3482 return nullptr; 3483 3484 return ToVar; 3485 } 3486 3487 Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { 3488 // Parameters are created in the translation unit's context, then moved 3489 // into the function declaration's context afterward. 3490 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); 3491 3492 // Import the name of this declaration. 3493 DeclarationName Name = Importer.Import(D->getDeclName()); 3494 if (D->getDeclName() && !Name) 3495 return nullptr; 3496 3497 // Import the location of this declaration. 3498 SourceLocation Loc = Importer.Import(D->getLocation()); 3499 3500 // Import the parameter's type. 3501 QualType T = Importer.Import(D->getType()); 3502 if (T.isNull()) 3503 return nullptr; 3504 3505 // Create the imported parameter. 3506 ImplicitParamDecl *ToParm 3507 = ImplicitParamDecl::Create(Importer.getToContext(), DC, 3508 Loc, Name.getAsIdentifierInfo(), 3509 T); 3510 return Importer.Imported(D, ToParm); 3511 } 3512 3513 Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { 3514 // Parameters are created in the translation unit's context, then moved 3515 // into the function declaration's context afterward. 3516 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); 3517 3518 // Import the name of this declaration. 3519 DeclarationName Name = Importer.Import(D->getDeclName()); 3520 if (D->getDeclName() && !Name) 3521 return nullptr; 3522 3523 // Import the location of this declaration. 3524 SourceLocation Loc = Importer.Import(D->getLocation()); 3525 3526 // Import the parameter's type. 3527 QualType T = Importer.Import(D->getType()); 3528 if (T.isNull()) 3529 return nullptr; 3530 3531 // Create the imported parameter. 3532 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3533 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC, 3534 Importer.Import(D->getInnerLocStart()), 3535 Loc, Name.getAsIdentifierInfo(), 3536 T, TInfo, D->getStorageClass(), 3537 /*FIXME: Default argument*/nullptr); 3538 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg()); 3539 3540 if (D->isUsed()) 3541 ToParm->setIsUsed(); 3542 3543 return Importer.Imported(D, ToParm); 3544 } 3545 3546 Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { 3547 // Import the major distinguishing characteristics of a method. 3548 DeclContext *DC, *LexicalDC; 3549 DeclarationName Name; 3550 SourceLocation Loc; 3551 NamedDecl *ToD; 3552 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3553 return nullptr; 3554 if (ToD) 3555 return ToD; 3556 3557 SmallVector<NamedDecl *, 2> FoundDecls; 3558 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3559 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3560 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) { 3561 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) 3562 continue; 3563 3564 // Check return types. 3565 if (!Importer.IsStructurallyEquivalent(D->getReturnType(), 3566 FoundMethod->getReturnType())) { 3567 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent) 3568 << D->isInstanceMethod() << Name << D->getReturnType() 3569 << FoundMethod->getReturnType(); 3570 Importer.ToDiag(FoundMethod->getLocation(), 3571 diag::note_odr_objc_method_here) 3572 << D->isInstanceMethod() << Name; 3573 return nullptr; 3574 } 3575 3576 // Check the number of parameters. 3577 if (D->param_size() != FoundMethod->param_size()) { 3578 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent) 3579 << D->isInstanceMethod() << Name 3580 << D->param_size() << FoundMethod->param_size(); 3581 Importer.ToDiag(FoundMethod->getLocation(), 3582 diag::note_odr_objc_method_here) 3583 << D->isInstanceMethod() << Name; 3584 return nullptr; 3585 } 3586 3587 // Check parameter types. 3588 for (ObjCMethodDecl::param_iterator P = D->param_begin(), 3589 PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); 3590 P != PEnd; ++P, ++FoundP) { 3591 if (!Importer.IsStructurallyEquivalent((*P)->getType(), 3592 (*FoundP)->getType())) { 3593 Importer.FromDiag((*P)->getLocation(), 3594 diag::err_odr_objc_method_param_type_inconsistent) 3595 << D->isInstanceMethod() << Name 3596 << (*P)->getType() << (*FoundP)->getType(); 3597 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) 3598 << (*FoundP)->getType(); 3599 return nullptr; 3600 } 3601 } 3602 3603 // Check variadic/non-variadic. 3604 // Check the number of parameters. 3605 if (D->isVariadic() != FoundMethod->isVariadic()) { 3606 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent) 3607 << D->isInstanceMethod() << Name; 3608 Importer.ToDiag(FoundMethod->getLocation(), 3609 diag::note_odr_objc_method_here) 3610 << D->isInstanceMethod() << Name; 3611 return nullptr; 3612 } 3613 3614 // FIXME: Any other bits we need to merge? 3615 return Importer.Imported(D, FoundMethod); 3616 } 3617 } 3618 3619 // Import the result type. 3620 QualType ResultTy = Importer.Import(D->getReturnType()); 3621 if (ResultTy.isNull()) 3622 return nullptr; 3623 3624 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo()); 3625 3626 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create( 3627 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()), 3628 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(), 3629 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(), 3630 D->getImplementationControl(), D->hasRelatedResultType()); 3631 3632 // FIXME: When we decide to merge method definitions, we'll need to 3633 // deal with implicit parameters. 3634 3635 // Import the parameters 3636 SmallVector<ParmVarDecl *, 5> ToParams; 3637 for (auto *FromP : D->parameters()) { 3638 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP)); 3639 if (!ToP) 3640 return nullptr; 3641 3642 ToParams.push_back(ToP); 3643 } 3644 3645 // Set the parameters. 3646 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) { 3647 ToParams[I]->setOwningFunction(ToMethod); 3648 ToMethod->addDeclInternal(ToParams[I]); 3649 } 3650 SmallVector<SourceLocation, 12> SelLocs; 3651 D->getSelectorLocs(SelLocs); 3652 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs); 3653 3654 ToMethod->setLexicalDeclContext(LexicalDC); 3655 Importer.Imported(D, ToMethod); 3656 LexicalDC->addDeclInternal(ToMethod); 3657 return ToMethod; 3658 } 3659 3660 Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { 3661 // Import the major distinguishing characteristics of a category. 3662 DeclContext *DC, *LexicalDC; 3663 DeclarationName Name; 3664 SourceLocation Loc; 3665 NamedDecl *ToD; 3666 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3667 return nullptr; 3668 if (ToD) 3669 return ToD; 3670 3671 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo()); 3672 if (!BoundInfo) 3673 return nullptr; 3674 3675 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create( 3676 Importer.getToContext(), DC, 3677 D->getVariance(), 3678 Importer.Import(D->getVarianceLoc()), 3679 D->getIndex(), 3680 Importer.Import(D->getLocation()), 3681 Name.getAsIdentifierInfo(), 3682 Importer.Import(D->getColonLoc()), 3683 BoundInfo); 3684 Importer.Imported(D, Result); 3685 Result->setLexicalDeclContext(LexicalDC); 3686 return Result; 3687 } 3688 3689 Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { 3690 // Import the major distinguishing characteristics of a category. 3691 DeclContext *DC, *LexicalDC; 3692 DeclarationName Name; 3693 SourceLocation Loc; 3694 NamedDecl *ToD; 3695 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3696 return nullptr; 3697 if (ToD) 3698 return ToD; 3699 3700 ObjCInterfaceDecl *ToInterface 3701 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface())); 3702 if (!ToInterface) 3703 return nullptr; 3704 3705 // Determine if we've already encountered this category. 3706 ObjCCategoryDecl *MergeWithCategory 3707 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); 3708 ObjCCategoryDecl *ToCategory = MergeWithCategory; 3709 if (!ToCategory) { 3710 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC, 3711 Importer.Import(D->getAtStartLoc()), 3712 Loc, 3713 Importer.Import(D->getCategoryNameLoc()), 3714 Name.getAsIdentifierInfo(), 3715 ToInterface, 3716 /*TypeParamList=*/nullptr, 3717 Importer.Import(D->getIvarLBraceLoc()), 3718 Importer.Import(D->getIvarRBraceLoc())); 3719 ToCategory->setLexicalDeclContext(LexicalDC); 3720 LexicalDC->addDeclInternal(ToCategory); 3721 Importer.Imported(D, ToCategory); 3722 // Import the type parameter list after calling Imported, to avoid 3723 // loops when bringing in their DeclContext. 3724 ToCategory->setTypeParamList(ImportObjCTypeParamList( 3725 D->getTypeParamList())); 3726 3727 // Import protocols 3728 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3729 SmallVector<SourceLocation, 4> ProtocolLocs; 3730 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc 3731 = D->protocol_loc_begin(); 3732 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), 3733 FromProtoEnd = D->protocol_end(); 3734 FromProto != FromProtoEnd; 3735 ++FromProto, ++FromProtoLoc) { 3736 ObjCProtocolDecl *ToProto 3737 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3738 if (!ToProto) 3739 return nullptr; 3740 Protocols.push_back(ToProto); 3741 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3742 } 3743 3744 // FIXME: If we're merging, make sure that the protocol list is the same. 3745 ToCategory->setProtocolList(Protocols.data(), Protocols.size(), 3746 ProtocolLocs.data(), Importer.getToContext()); 3747 3748 } else { 3749 Importer.Imported(D, ToCategory); 3750 } 3751 3752 // Import all of the members of this category. 3753 ImportDeclContext(D); 3754 3755 // If we have an implementation, import it as well. 3756 if (D->getImplementation()) { 3757 ObjCCategoryImplDecl *Impl 3758 = cast_or_null<ObjCCategoryImplDecl>( 3759 Importer.Import(D->getImplementation())); 3760 if (!Impl) 3761 return nullptr; 3762 3763 ToCategory->setImplementation(Impl); 3764 } 3765 3766 return ToCategory; 3767 } 3768 3769 bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From, 3770 ObjCProtocolDecl *To, 3771 ImportDefinitionKind Kind) { 3772 if (To->getDefinition()) { 3773 if (shouldForceImportDeclContext(Kind)) 3774 ImportDeclContext(From); 3775 return false; 3776 } 3777 3778 // Start the protocol definition 3779 To->startDefinition(); 3780 3781 // Import protocols 3782 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3783 SmallVector<SourceLocation, 4> ProtocolLocs; 3784 ObjCProtocolDecl::protocol_loc_iterator 3785 FromProtoLoc = From->protocol_loc_begin(); 3786 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), 3787 FromProtoEnd = From->protocol_end(); 3788 FromProto != FromProtoEnd; 3789 ++FromProto, ++FromProtoLoc) { 3790 ObjCProtocolDecl *ToProto 3791 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3792 if (!ToProto) 3793 return true; 3794 Protocols.push_back(ToProto); 3795 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3796 } 3797 3798 // FIXME: If we're merging, make sure that the protocol list is the same. 3799 To->setProtocolList(Protocols.data(), Protocols.size(), 3800 ProtocolLocs.data(), Importer.getToContext()); 3801 3802 if (shouldForceImportDeclContext(Kind)) { 3803 // Import all of the members of this protocol. 3804 ImportDeclContext(From, /*ForceImport=*/true); 3805 } 3806 return false; 3807 } 3808 3809 Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { 3810 // If this protocol has a definition in the translation unit we're coming 3811 // from, but this particular declaration is not that definition, import the 3812 // definition and map to that. 3813 ObjCProtocolDecl *Definition = D->getDefinition(); 3814 if (Definition && Definition != D) { 3815 Decl *ImportedDef = Importer.Import(Definition); 3816 if (!ImportedDef) 3817 return nullptr; 3818 3819 return Importer.Imported(D, ImportedDef); 3820 } 3821 3822 // Import the major distinguishing characteristics of a protocol. 3823 DeclContext *DC, *LexicalDC; 3824 DeclarationName Name; 3825 SourceLocation Loc; 3826 NamedDecl *ToD; 3827 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3828 return nullptr; 3829 if (ToD) 3830 return ToD; 3831 3832 ObjCProtocolDecl *MergeWithProtocol = nullptr; 3833 SmallVector<NamedDecl *, 2> FoundDecls; 3834 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3835 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 3836 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) 3837 continue; 3838 3839 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I]))) 3840 break; 3841 } 3842 3843 ObjCProtocolDecl *ToProto = MergeWithProtocol; 3844 if (!ToProto) { 3845 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, 3846 Name.getAsIdentifierInfo(), Loc, 3847 Importer.Import(D->getAtStartLoc()), 3848 /*PrevDecl=*/nullptr); 3849 ToProto->setLexicalDeclContext(LexicalDC); 3850 LexicalDC->addDeclInternal(ToProto); 3851 } 3852 3853 Importer.Imported(D, ToProto); 3854 3855 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto)) 3856 return nullptr; 3857 3858 return ToProto; 3859 } 3860 3861 Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 3862 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 3863 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 3864 3865 SourceLocation ExternLoc = Importer.Import(D->getExternLoc()); 3866 SourceLocation LangLoc = Importer.Import(D->getLocation()); 3867 3868 bool HasBraces = D->hasBraces(); 3869 3870 LinkageSpecDecl *ToLinkageSpec = 3871 LinkageSpecDecl::Create(Importer.getToContext(), 3872 DC, 3873 ExternLoc, 3874 LangLoc, 3875 D->getLanguage(), 3876 HasBraces); 3877 3878 if (HasBraces) { 3879 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc()); 3880 ToLinkageSpec->setRBraceLoc(RBraceLoc); 3881 } 3882 3883 ToLinkageSpec->setLexicalDeclContext(LexicalDC); 3884 LexicalDC->addDeclInternal(ToLinkageSpec); 3885 3886 Importer.Imported(D, ToLinkageSpec); 3887 3888 return ToLinkageSpec; 3889 } 3890 3891 bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From, 3892 ObjCInterfaceDecl *To, 3893 ImportDefinitionKind Kind) { 3894 if (To->getDefinition()) { 3895 // Check consistency of superclass. 3896 ObjCInterfaceDecl *FromSuper = From->getSuperClass(); 3897 if (FromSuper) { 3898 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper)); 3899 if (!FromSuper) 3900 return true; 3901 } 3902 3903 ObjCInterfaceDecl *ToSuper = To->getSuperClass(); 3904 if ((bool)FromSuper != (bool)ToSuper || 3905 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { 3906 Importer.ToDiag(To->getLocation(), 3907 diag::err_odr_objc_superclass_inconsistent) 3908 << To->getDeclName(); 3909 if (ToSuper) 3910 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) 3911 << To->getSuperClass()->getDeclName(); 3912 else 3913 Importer.ToDiag(To->getLocation(), 3914 diag::note_odr_objc_missing_superclass); 3915 if (From->getSuperClass()) 3916 Importer.FromDiag(From->getSuperClassLoc(), 3917 diag::note_odr_objc_superclass) 3918 << From->getSuperClass()->getDeclName(); 3919 else 3920 Importer.FromDiag(From->getLocation(), 3921 diag::note_odr_objc_missing_superclass); 3922 } 3923 3924 if (shouldForceImportDeclContext(Kind)) 3925 ImportDeclContext(From); 3926 return false; 3927 } 3928 3929 // Start the definition. 3930 To->startDefinition(); 3931 3932 // If this class has a superclass, import it. 3933 if (From->getSuperClass()) { 3934 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo()); 3935 if (!SuperTInfo) 3936 return true; 3937 3938 To->setSuperClass(SuperTInfo); 3939 } 3940 3941 // Import protocols 3942 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3943 SmallVector<SourceLocation, 4> ProtocolLocs; 3944 ObjCInterfaceDecl::protocol_loc_iterator 3945 FromProtoLoc = From->protocol_loc_begin(); 3946 3947 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), 3948 FromProtoEnd = From->protocol_end(); 3949 FromProto != FromProtoEnd; 3950 ++FromProto, ++FromProtoLoc) { 3951 ObjCProtocolDecl *ToProto 3952 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3953 if (!ToProto) 3954 return true; 3955 Protocols.push_back(ToProto); 3956 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3957 } 3958 3959 // FIXME: If we're merging, make sure that the protocol list is the same. 3960 To->setProtocolList(Protocols.data(), Protocols.size(), 3961 ProtocolLocs.data(), Importer.getToContext()); 3962 3963 // Import categories. When the categories themselves are imported, they'll 3964 // hook themselves into this interface. 3965 for (auto *Cat : From->known_categories()) 3966 Importer.Import(Cat); 3967 3968 // If we have an @implementation, import it as well. 3969 if (From->getImplementation()) { 3970 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>( 3971 Importer.Import(From->getImplementation())); 3972 if (!Impl) 3973 return true; 3974 3975 To->setImplementation(Impl); 3976 } 3977 3978 if (shouldForceImportDeclContext(Kind)) { 3979 // Import all of the members of this class. 3980 ImportDeclContext(From, /*ForceImport=*/true); 3981 } 3982 return false; 3983 } 3984 3985 ObjCTypeParamList * 3986 ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { 3987 if (!list) 3988 return nullptr; 3989 3990 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams; 3991 for (auto fromTypeParam : *list) { 3992 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>( 3993 Importer.Import(fromTypeParam)); 3994 if (!toTypeParam) 3995 return nullptr; 3996 3997 toTypeParams.push_back(toTypeParam); 3998 } 3999 4000 return ObjCTypeParamList::create(Importer.getToContext(), 4001 Importer.Import(list->getLAngleLoc()), 4002 toTypeParams, 4003 Importer.Import(list->getRAngleLoc())); 4004 } 4005 4006 Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { 4007 // If this class has a definition in the translation unit we're coming from, 4008 // but this particular declaration is not that definition, import the 4009 // definition and map to that. 4010 ObjCInterfaceDecl *Definition = D->getDefinition(); 4011 if (Definition && Definition != D) { 4012 Decl *ImportedDef = Importer.Import(Definition); 4013 if (!ImportedDef) 4014 return nullptr; 4015 4016 return Importer.Imported(D, ImportedDef); 4017 } 4018 4019 // Import the major distinguishing characteristics of an @interface. 4020 DeclContext *DC, *LexicalDC; 4021 DeclarationName Name; 4022 SourceLocation Loc; 4023 NamedDecl *ToD; 4024 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4025 return nullptr; 4026 if (ToD) 4027 return ToD; 4028 4029 // Look for an existing interface with the same name. 4030 ObjCInterfaceDecl *MergeWithIface = nullptr; 4031 SmallVector<NamedDecl *, 2> FoundDecls; 4032 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4033 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 4034 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 4035 continue; 4036 4037 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I]))) 4038 break; 4039 } 4040 4041 // Create an interface declaration, if one does not already exist. 4042 ObjCInterfaceDecl *ToIface = MergeWithIface; 4043 if (!ToIface) { 4044 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC, 4045 Importer.Import(D->getAtStartLoc()), 4046 Name.getAsIdentifierInfo(), 4047 /*TypeParamList=*/nullptr, 4048 /*PrevDecl=*/nullptr, Loc, 4049 D->isImplicitInterfaceDecl()); 4050 ToIface->setLexicalDeclContext(LexicalDC); 4051 LexicalDC->addDeclInternal(ToIface); 4052 } 4053 Importer.Imported(D, ToIface); 4054 // Import the type parameter list after calling Imported, to avoid 4055 // loops when bringing in their DeclContext. 4056 ToIface->setTypeParamList(ImportObjCTypeParamList( 4057 D->getTypeParamListAsWritten())); 4058 4059 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface)) 4060 return nullptr; 4061 4062 return ToIface; 4063 } 4064 4065 Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { 4066 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>( 4067 Importer.Import(D->getCategoryDecl())); 4068 if (!Category) 4069 return nullptr; 4070 4071 ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); 4072 if (!ToImpl) { 4073 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 4074 if (!DC) 4075 return nullptr; 4076 4077 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc()); 4078 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC, 4079 Importer.Import(D->getIdentifier()), 4080 Category->getClassInterface(), 4081 Importer.Import(D->getLocation()), 4082 Importer.Import(D->getAtStartLoc()), 4083 CategoryNameLoc); 4084 4085 DeclContext *LexicalDC = DC; 4086 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4087 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4088 if (!LexicalDC) 4089 return nullptr; 4090 4091 ToImpl->setLexicalDeclContext(LexicalDC); 4092 } 4093 4094 LexicalDC->addDeclInternal(ToImpl); 4095 Category->setImplementation(ToImpl); 4096 } 4097 4098 Importer.Imported(D, ToImpl); 4099 ImportDeclContext(D); 4100 return ToImpl; 4101 } 4102 4103 Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { 4104 // Find the corresponding interface. 4105 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>( 4106 Importer.Import(D->getClassInterface())); 4107 if (!Iface) 4108 return nullptr; 4109 4110 // Import the superclass, if any. 4111 ObjCInterfaceDecl *Super = nullptr; 4112 if (D->getSuperClass()) { 4113 Super = cast_or_null<ObjCInterfaceDecl>( 4114 Importer.Import(D->getSuperClass())); 4115 if (!Super) 4116 return nullptr; 4117 } 4118 4119 ObjCImplementationDecl *Impl = Iface->getImplementation(); 4120 if (!Impl) { 4121 // We haven't imported an implementation yet. Create a new @implementation 4122 // now. 4123 Impl = ObjCImplementationDecl::Create(Importer.getToContext(), 4124 Importer.ImportContext(D->getDeclContext()), 4125 Iface, Super, 4126 Importer.Import(D->getLocation()), 4127 Importer.Import(D->getAtStartLoc()), 4128 Importer.Import(D->getSuperClassLoc()), 4129 Importer.Import(D->getIvarLBraceLoc()), 4130 Importer.Import(D->getIvarRBraceLoc())); 4131 4132 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4133 DeclContext *LexicalDC 4134 = Importer.ImportContext(D->getLexicalDeclContext()); 4135 if (!LexicalDC) 4136 return nullptr; 4137 Impl->setLexicalDeclContext(LexicalDC); 4138 } 4139 4140 // Associate the implementation with the class it implements. 4141 Iface->setImplementation(Impl); 4142 Importer.Imported(D, Iface->getImplementation()); 4143 } else { 4144 Importer.Imported(D, Iface->getImplementation()); 4145 4146 // Verify that the existing @implementation has the same superclass. 4147 if ((Super && !Impl->getSuperClass()) || 4148 (!Super && Impl->getSuperClass()) || 4149 (Super && Impl->getSuperClass() && 4150 !declaresSameEntity(Super->getCanonicalDecl(), 4151 Impl->getSuperClass()))) { 4152 Importer.ToDiag(Impl->getLocation(), 4153 diag::err_odr_objc_superclass_inconsistent) 4154 << Iface->getDeclName(); 4155 // FIXME: It would be nice to have the location of the superclass 4156 // below. 4157 if (Impl->getSuperClass()) 4158 Importer.ToDiag(Impl->getLocation(), 4159 diag::note_odr_objc_superclass) 4160 << Impl->getSuperClass()->getDeclName(); 4161 else 4162 Importer.ToDiag(Impl->getLocation(), 4163 diag::note_odr_objc_missing_superclass); 4164 if (D->getSuperClass()) 4165 Importer.FromDiag(D->getLocation(), 4166 diag::note_odr_objc_superclass) 4167 << D->getSuperClass()->getDeclName(); 4168 else 4169 Importer.FromDiag(D->getLocation(), 4170 diag::note_odr_objc_missing_superclass); 4171 return nullptr; 4172 } 4173 } 4174 4175 // Import all of the members of this @implementation. 4176 ImportDeclContext(D); 4177 4178 return Impl; 4179 } 4180 4181 Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { 4182 // Import the major distinguishing characteristics of an @property. 4183 DeclContext *DC, *LexicalDC; 4184 DeclarationName Name; 4185 SourceLocation Loc; 4186 NamedDecl *ToD; 4187 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4188 return nullptr; 4189 if (ToD) 4190 return ToD; 4191 4192 // Check whether we have already imported this property. 4193 SmallVector<NamedDecl *, 2> FoundDecls; 4194 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4195 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 4196 if (ObjCPropertyDecl *FoundProp 4197 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) { 4198 // Check property types. 4199 if (!Importer.IsStructurallyEquivalent(D->getType(), 4200 FoundProp->getType())) { 4201 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent) 4202 << Name << D->getType() << FoundProp->getType(); 4203 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) 4204 << FoundProp->getType(); 4205 return nullptr; 4206 } 4207 4208 // FIXME: Check property attributes, getters, setters, etc.? 4209 4210 // Consider these properties to be equivalent. 4211 Importer.Imported(D, FoundProp); 4212 return FoundProp; 4213 } 4214 } 4215 4216 // Import the type. 4217 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo()); 4218 if (!TSI) 4219 return nullptr; 4220 4221 // Create the new property. 4222 ObjCPropertyDecl *ToProperty 4223 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc, 4224 Name.getAsIdentifierInfo(), 4225 Importer.Import(D->getAtLoc()), 4226 Importer.Import(D->getLParenLoc()), 4227 Importer.Import(D->getType()), 4228 TSI, 4229 D->getPropertyImplementation()); 4230 Importer.Imported(D, ToProperty); 4231 ToProperty->setLexicalDeclContext(LexicalDC); 4232 LexicalDC->addDeclInternal(ToProperty); 4233 4234 ToProperty->setPropertyAttributes(D->getPropertyAttributes()); 4235 ToProperty->setPropertyAttributesAsWritten( 4236 D->getPropertyAttributesAsWritten()); 4237 ToProperty->setGetterName(Importer.Import(D->getGetterName())); 4238 ToProperty->setSetterName(Importer.Import(D->getSetterName())); 4239 ToProperty->setGetterMethodDecl( 4240 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl()))); 4241 ToProperty->setSetterMethodDecl( 4242 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl()))); 4243 ToProperty->setPropertyIvarDecl( 4244 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl()))); 4245 return ToProperty; 4246 } 4247 4248 Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { 4249 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>( 4250 Importer.Import(D->getPropertyDecl())); 4251 if (!Property) 4252 return nullptr; 4253 4254 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 4255 if (!DC) 4256 return nullptr; 4257 4258 // Import the lexical declaration context. 4259 DeclContext *LexicalDC = DC; 4260 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4261 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4262 if (!LexicalDC) 4263 return nullptr; 4264 } 4265 4266 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC); 4267 if (!InImpl) 4268 return nullptr; 4269 4270 // Import the ivar (for an @synthesize). 4271 ObjCIvarDecl *Ivar = nullptr; 4272 if (D->getPropertyIvarDecl()) { 4273 Ivar = cast_or_null<ObjCIvarDecl>( 4274 Importer.Import(D->getPropertyIvarDecl())); 4275 if (!Ivar) 4276 return nullptr; 4277 } 4278 4279 ObjCPropertyImplDecl *ToImpl 4280 = InImpl->FindPropertyImplDecl(Property->getIdentifier(), 4281 Property->getQueryKind()); 4282 if (!ToImpl) { 4283 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC, 4284 Importer.Import(D->getLocStart()), 4285 Importer.Import(D->getLocation()), 4286 Property, 4287 D->getPropertyImplementation(), 4288 Ivar, 4289 Importer.Import(D->getPropertyIvarDeclLoc())); 4290 ToImpl->setLexicalDeclContext(LexicalDC); 4291 Importer.Imported(D, ToImpl); 4292 LexicalDC->addDeclInternal(ToImpl); 4293 } else { 4294 // Check that we have the same kind of property implementation (@synthesize 4295 // vs. @dynamic). 4296 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { 4297 Importer.ToDiag(ToImpl->getLocation(), 4298 diag::err_odr_objc_property_impl_kind_inconsistent) 4299 << Property->getDeclName() 4300 << (ToImpl->getPropertyImplementation() 4301 == ObjCPropertyImplDecl::Dynamic); 4302 Importer.FromDiag(D->getLocation(), 4303 diag::note_odr_objc_property_impl_kind) 4304 << D->getPropertyDecl()->getDeclName() 4305 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); 4306 return nullptr; 4307 } 4308 4309 // For @synthesize, check that we have the same 4310 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && 4311 Ivar != ToImpl->getPropertyIvarDecl()) { 4312 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), 4313 diag::err_odr_objc_synthesize_ivar_inconsistent) 4314 << Property->getDeclName() 4315 << ToImpl->getPropertyIvarDecl()->getDeclName() 4316 << Ivar->getDeclName(); 4317 Importer.FromDiag(D->getPropertyIvarDeclLoc(), 4318 diag::note_odr_objc_synthesize_ivar_here) 4319 << D->getPropertyIvarDecl()->getDeclName(); 4320 return nullptr; 4321 } 4322 4323 // Merge the existing implementation with the new implementation. 4324 Importer.Imported(D, ToImpl); 4325 } 4326 4327 return ToImpl; 4328 } 4329 4330 Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { 4331 // For template arguments, we adopt the translation unit as our declaration 4332 // context. This context will be fixed when the actual template declaration 4333 // is created. 4334 4335 // FIXME: Import default argument. 4336 return TemplateTypeParmDecl::Create(Importer.getToContext(), 4337 Importer.getToContext().getTranslationUnitDecl(), 4338 Importer.Import(D->getLocStart()), 4339 Importer.Import(D->getLocation()), 4340 D->getDepth(), 4341 D->getIndex(), 4342 Importer.Import(D->getIdentifier()), 4343 D->wasDeclaredWithTypename(), 4344 D->isParameterPack()); 4345 } 4346 4347 Decl * 4348 ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { 4349 // Import the name of this declaration. 4350 DeclarationName Name = Importer.Import(D->getDeclName()); 4351 if (D->getDeclName() && !Name) 4352 return nullptr; 4353 4354 // Import the location of this declaration. 4355 SourceLocation Loc = Importer.Import(D->getLocation()); 4356 4357 // Import the type of this declaration. 4358 QualType T = Importer.Import(D->getType()); 4359 if (T.isNull()) 4360 return nullptr; 4361 4362 // Import type-source information. 4363 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 4364 if (D->getTypeSourceInfo() && !TInfo) 4365 return nullptr; 4366 4367 // FIXME: Import default argument. 4368 4369 return NonTypeTemplateParmDecl::Create(Importer.getToContext(), 4370 Importer.getToContext().getTranslationUnitDecl(), 4371 Importer.Import(D->getInnerLocStart()), 4372 Loc, D->getDepth(), D->getPosition(), 4373 Name.getAsIdentifierInfo(), 4374 T, D->isParameterPack(), TInfo); 4375 } 4376 4377 Decl * 4378 ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { 4379 // Import the name of this declaration. 4380 DeclarationName Name = Importer.Import(D->getDeclName()); 4381 if (D->getDeclName() && !Name) 4382 return nullptr; 4383 4384 // Import the location of this declaration. 4385 SourceLocation Loc = Importer.Import(D->getLocation()); 4386 4387 // Import template parameters. 4388 TemplateParameterList *TemplateParams 4389 = ImportTemplateParameterList(D->getTemplateParameters()); 4390 if (!TemplateParams) 4391 return nullptr; 4392 4393 // FIXME: Import default argument. 4394 4395 return TemplateTemplateParmDecl::Create(Importer.getToContext(), 4396 Importer.getToContext().getTranslationUnitDecl(), 4397 Loc, D->getDepth(), D->getPosition(), 4398 D->isParameterPack(), 4399 Name.getAsIdentifierInfo(), 4400 TemplateParams); 4401 } 4402 4403 Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 4404 // If this record has a definition in the translation unit we're coming from, 4405 // but this particular declaration is not that definition, import the 4406 // definition and map to that. 4407 CXXRecordDecl *Definition 4408 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition()); 4409 if (Definition && Definition != D->getTemplatedDecl()) { 4410 Decl *ImportedDef 4411 = Importer.Import(Definition->getDescribedClassTemplate()); 4412 if (!ImportedDef) 4413 return nullptr; 4414 4415 return Importer.Imported(D, ImportedDef); 4416 } 4417 4418 // Import the major distinguishing characteristics of this class template. 4419 DeclContext *DC, *LexicalDC; 4420 DeclarationName Name; 4421 SourceLocation Loc; 4422 NamedDecl *ToD; 4423 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4424 return nullptr; 4425 if (ToD) 4426 return ToD; 4427 4428 // We may already have a template of the same name; try to find and match it. 4429 if (!DC->isFunctionOrMethod()) { 4430 SmallVector<NamedDecl *, 4> ConflictingDecls; 4431 SmallVector<NamedDecl *, 2> FoundDecls; 4432 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4433 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 4434 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 4435 continue; 4436 4437 Decl *Found = FoundDecls[I]; 4438 if (ClassTemplateDecl *FoundTemplate 4439 = dyn_cast<ClassTemplateDecl>(Found)) { 4440 if (IsStructuralMatch(D, FoundTemplate)) { 4441 // The class templates structurally match; call it the same template. 4442 // FIXME: We may be filling in a forward declaration here. Handle 4443 // this case! 4444 Importer.Imported(D->getTemplatedDecl(), 4445 FoundTemplate->getTemplatedDecl()); 4446 return Importer.Imported(D, FoundTemplate); 4447 } 4448 } 4449 4450 ConflictingDecls.push_back(FoundDecls[I]); 4451 } 4452 4453 if (!ConflictingDecls.empty()) { 4454 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, 4455 ConflictingDecls.data(), 4456 ConflictingDecls.size()); 4457 } 4458 4459 if (!Name) 4460 return nullptr; 4461 } 4462 4463 CXXRecordDecl *DTemplated = D->getTemplatedDecl(); 4464 4465 // Create the declaration that is being templated. 4466 // Create the declaration that is being templated. 4467 CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>( 4468 Importer.Import(DTemplated)); 4469 if (!D2Templated) 4470 return nullptr; 4471 4472 // Resolve possible cyclic import. 4473 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D)) 4474 return AlreadyImported; 4475 4476 // Create the class template declaration itself. 4477 TemplateParameterList *TemplateParams 4478 = ImportTemplateParameterList(D->getTemplateParameters()); 4479 if (!TemplateParams) 4480 return nullptr; 4481 4482 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC, 4483 Loc, Name, TemplateParams, 4484 D2Templated, 4485 /*PrevDecl=*/nullptr); 4486 D2Templated->setDescribedClassTemplate(D2); 4487 4488 D2->setAccess(D->getAccess()); 4489 D2->setLexicalDeclContext(LexicalDC); 4490 LexicalDC->addDeclInternal(D2); 4491 4492 // Note the relationship between the class templates. 4493 Importer.Imported(D, D2); 4494 Importer.Imported(DTemplated, D2Templated); 4495 4496 if (DTemplated->isCompleteDefinition() && 4497 !D2Templated->isCompleteDefinition()) { 4498 // FIXME: Import definition! 4499 } 4500 4501 return D2; 4502 } 4503 4504 Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl( 4505 ClassTemplateSpecializationDecl *D) { 4506 // If this record has a definition in the translation unit we're coming from, 4507 // but this particular declaration is not that definition, import the 4508 // definition and map to that. 4509 TagDecl *Definition = D->getDefinition(); 4510 if (Definition && Definition != D) { 4511 Decl *ImportedDef = Importer.Import(Definition); 4512 if (!ImportedDef) 4513 return nullptr; 4514 4515 return Importer.Imported(D, ImportedDef); 4516 } 4517 4518 ClassTemplateDecl *ClassTemplate 4519 = cast_or_null<ClassTemplateDecl>(Importer.Import( 4520 D->getSpecializedTemplate())); 4521 if (!ClassTemplate) 4522 return nullptr; 4523 4524 // Import the context of this declaration. 4525 DeclContext *DC = ClassTemplate->getDeclContext(); 4526 if (!DC) 4527 return nullptr; 4528 4529 DeclContext *LexicalDC = DC; 4530 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4531 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4532 if (!LexicalDC) 4533 return nullptr; 4534 } 4535 4536 // Import the location of this declaration. 4537 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 4538 SourceLocation IdLoc = Importer.Import(D->getLocation()); 4539 4540 // Import template arguments. 4541 SmallVector<TemplateArgument, 2> TemplateArgs; 4542 if (ImportTemplateArguments(D->getTemplateArgs().data(), 4543 D->getTemplateArgs().size(), 4544 TemplateArgs)) 4545 return nullptr; 4546 4547 // Try to find an existing specialization with these template arguments. 4548 void *InsertPos = nullptr; 4549 ClassTemplateSpecializationDecl *D2 4550 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos); 4551 if (D2) { 4552 // We already have a class template specialization with these template 4553 // arguments. 4554 4555 // FIXME: Check for specialization vs. instantiation errors. 4556 4557 if (RecordDecl *FoundDef = D2->getDefinition()) { 4558 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) { 4559 // The record types structurally match, or the "from" translation 4560 // unit only had a forward declaration anyway; call it the same 4561 // function. 4562 return Importer.Imported(D, FoundDef); 4563 } 4564 } 4565 } else { 4566 // Create a new specialization. 4567 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(), 4568 D->getTagKind(), DC, 4569 StartLoc, IdLoc, 4570 ClassTemplate, 4571 TemplateArgs, 4572 /*PrevDecl=*/nullptr); 4573 D2->setSpecializationKind(D->getSpecializationKind()); 4574 4575 // Add this specialization to the class template. 4576 ClassTemplate->AddSpecialization(D2, InsertPos); 4577 4578 // Import the qualifier, if any. 4579 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 4580 4581 // Add the specialization to this context. 4582 D2->setLexicalDeclContext(LexicalDC); 4583 LexicalDC->addDeclInternal(D2); 4584 } 4585 Importer.Imported(D, D2); 4586 4587 if (D->isCompleteDefinition() && ImportDefinition(D, D2)) 4588 return nullptr; 4589 4590 return D2; 4591 } 4592 4593 Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { 4594 // If this variable has a definition in the translation unit we're coming 4595 // from, 4596 // but this particular declaration is not that definition, import the 4597 // definition and map to that. 4598 VarDecl *Definition = 4599 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition()); 4600 if (Definition && Definition != D->getTemplatedDecl()) { 4601 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate()); 4602 if (!ImportedDef) 4603 return nullptr; 4604 4605 return Importer.Imported(D, ImportedDef); 4606 } 4607 4608 // Import the major distinguishing characteristics of this variable template. 4609 DeclContext *DC, *LexicalDC; 4610 DeclarationName Name; 4611 SourceLocation Loc; 4612 NamedDecl *ToD; 4613 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4614 return nullptr; 4615 if (ToD) 4616 return ToD; 4617 4618 // We may already have a template of the same name; try to find and match it. 4619 assert(!DC->isFunctionOrMethod() && 4620 "Variable templates cannot be declared at function scope"); 4621 SmallVector<NamedDecl *, 4> ConflictingDecls; 4622 SmallVector<NamedDecl *, 2> FoundDecls; 4623 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4624 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 4625 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 4626 continue; 4627 4628 Decl *Found = FoundDecls[I]; 4629 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) { 4630 if (IsStructuralMatch(D, FoundTemplate)) { 4631 // The variable templates structurally match; call it the same template. 4632 Importer.Imported(D->getTemplatedDecl(), 4633 FoundTemplate->getTemplatedDecl()); 4634 return Importer.Imported(D, FoundTemplate); 4635 } 4636 } 4637 4638 ConflictingDecls.push_back(FoundDecls[I]); 4639 } 4640 4641 if (!ConflictingDecls.empty()) { 4642 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, 4643 ConflictingDecls.data(), 4644 ConflictingDecls.size()); 4645 } 4646 4647 if (!Name) 4648 return nullptr; 4649 4650 VarDecl *DTemplated = D->getTemplatedDecl(); 4651 4652 // Import the type. 4653 QualType T = Importer.Import(DTemplated->getType()); 4654 if (T.isNull()) 4655 return nullptr; 4656 4657 // Create the declaration that is being templated. 4658 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart()); 4659 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation()); 4660 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo()); 4661 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc, 4662 IdLoc, Name.getAsIdentifierInfo(), T, 4663 TInfo, DTemplated->getStorageClass()); 4664 D2Templated->setAccess(DTemplated->getAccess()); 4665 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc())); 4666 D2Templated->setLexicalDeclContext(LexicalDC); 4667 4668 // Importer.Imported(DTemplated, D2Templated); 4669 // LexicalDC->addDeclInternal(D2Templated); 4670 4671 // Merge the initializer. 4672 if (ImportDefinition(DTemplated, D2Templated)) 4673 return nullptr; 4674 4675 // Create the variable template declaration itself. 4676 TemplateParameterList *TemplateParams = 4677 ImportTemplateParameterList(D->getTemplateParameters()); 4678 if (!TemplateParams) 4679 return nullptr; 4680 4681 VarTemplateDecl *D2 = VarTemplateDecl::Create( 4682 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated); 4683 D2Templated->setDescribedVarTemplate(D2); 4684 4685 D2->setAccess(D->getAccess()); 4686 D2->setLexicalDeclContext(LexicalDC); 4687 LexicalDC->addDeclInternal(D2); 4688 4689 // Note the relationship between the variable templates. 4690 Importer.Imported(D, D2); 4691 Importer.Imported(DTemplated, D2Templated); 4692 4693 if (DTemplated->isThisDeclarationADefinition() && 4694 !D2Templated->isThisDeclarationADefinition()) { 4695 // FIXME: Import definition! 4696 } 4697 4698 return D2; 4699 } 4700 4701 Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl( 4702 VarTemplateSpecializationDecl *D) { 4703 // If this record has a definition in the translation unit we're coming from, 4704 // but this particular declaration is not that definition, import the 4705 // definition and map to that. 4706 VarDecl *Definition = D->getDefinition(); 4707 if (Definition && Definition != D) { 4708 Decl *ImportedDef = Importer.Import(Definition); 4709 if (!ImportedDef) 4710 return nullptr; 4711 4712 return Importer.Imported(D, ImportedDef); 4713 } 4714 4715 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>( 4716 Importer.Import(D->getSpecializedTemplate())); 4717 if (!VarTemplate) 4718 return nullptr; 4719 4720 // Import the context of this declaration. 4721 DeclContext *DC = VarTemplate->getDeclContext(); 4722 if (!DC) 4723 return nullptr; 4724 4725 DeclContext *LexicalDC = DC; 4726 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4727 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4728 if (!LexicalDC) 4729 return nullptr; 4730 } 4731 4732 // Import the location of this declaration. 4733 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 4734 SourceLocation IdLoc = Importer.Import(D->getLocation()); 4735 4736 // Import template arguments. 4737 SmallVector<TemplateArgument, 2> TemplateArgs; 4738 if (ImportTemplateArguments(D->getTemplateArgs().data(), 4739 D->getTemplateArgs().size(), TemplateArgs)) 4740 return nullptr; 4741 4742 // Try to find an existing specialization with these template arguments. 4743 void *InsertPos = nullptr; 4744 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization( 4745 TemplateArgs, InsertPos); 4746 if (D2) { 4747 // We already have a variable template specialization with these template 4748 // arguments. 4749 4750 // FIXME: Check for specialization vs. instantiation errors. 4751 4752 if (VarDecl *FoundDef = D2->getDefinition()) { 4753 if (!D->isThisDeclarationADefinition() || 4754 IsStructuralMatch(D, FoundDef)) { 4755 // The record types structurally match, or the "from" translation 4756 // unit only had a forward declaration anyway; call it the same 4757 // variable. 4758 return Importer.Imported(D, FoundDef); 4759 } 4760 } 4761 } else { 4762 4763 // Import the type. 4764 QualType T = Importer.Import(D->getType()); 4765 if (T.isNull()) 4766 return nullptr; 4767 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 4768 4769 // Create a new specialization. 4770 D2 = VarTemplateSpecializationDecl::Create( 4771 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo, 4772 D->getStorageClass(), TemplateArgs); 4773 D2->setSpecializationKind(D->getSpecializationKind()); 4774 D2->setTemplateArgsInfo(D->getTemplateArgsInfo()); 4775 4776 // Add this specialization to the class template. 4777 VarTemplate->AddSpecialization(D2, InsertPos); 4778 4779 // Import the qualifier, if any. 4780 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 4781 4782 // Add the specialization to this context. 4783 D2->setLexicalDeclContext(LexicalDC); 4784 LexicalDC->addDeclInternal(D2); 4785 } 4786 Importer.Imported(D, D2); 4787 4788 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2)) 4789 return nullptr; 4790 4791 return D2; 4792 } 4793 4794 //---------------------------------------------------------------------------- 4795 // Import Statements 4796 //---------------------------------------------------------------------------- 4797 4798 DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) { 4799 if (DG.isNull()) 4800 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0); 4801 size_t NumDecls = DG.end() - DG.begin(); 4802 SmallVector<Decl *, 1> ToDecls(NumDecls); 4803 auto &_Importer = this->Importer; 4804 std::transform(DG.begin(), DG.end(), ToDecls.begin(), 4805 [&_Importer](Decl *D) -> Decl * { 4806 return _Importer.Import(D); 4807 }); 4808 return DeclGroupRef::Create(Importer.getToContext(), 4809 ToDecls.begin(), 4810 NumDecls); 4811 } 4812 4813 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) { 4814 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node) 4815 << S->getStmtClassName(); 4816 return nullptr; 4817 } 4818 4819 4820 Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) { 4821 SmallVector<IdentifierInfo *, 4> Names; 4822 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { 4823 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I)); 4824 if (!ToII) 4825 return nullptr; 4826 Names.push_back(ToII); 4827 } 4828 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { 4829 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I)); 4830 if (!ToII) 4831 return nullptr; 4832 Names.push_back(ToII); 4833 } 4834 4835 SmallVector<StringLiteral *, 4> Clobbers; 4836 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) { 4837 StringLiteral *Clobber = cast_or_null<StringLiteral>( 4838 Importer.Import(S->getClobberStringLiteral(I))); 4839 if (!Clobber) 4840 return nullptr; 4841 Clobbers.push_back(Clobber); 4842 } 4843 4844 SmallVector<StringLiteral *, 4> Constraints; 4845 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { 4846 StringLiteral *Output = cast_or_null<StringLiteral>( 4847 Importer.Import(S->getOutputConstraintLiteral(I))); 4848 if (!Output) 4849 return nullptr; 4850 Constraints.push_back(Output); 4851 } 4852 4853 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { 4854 StringLiteral *Input = cast_or_null<StringLiteral>( 4855 Importer.Import(S->getInputConstraintLiteral(I))); 4856 if (!Input) 4857 return nullptr; 4858 Constraints.push_back(Input); 4859 } 4860 4861 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs()); 4862 if (ImportArrayChecked(S->begin_outputs(), S->end_outputs(), Exprs.begin())) 4863 return nullptr; 4864 4865 if (ImportArrayChecked(S->begin_inputs(), S->end_inputs(), 4866 Exprs.begin() + S->getNumOutputs())) 4867 return nullptr; 4868 4869 StringLiteral *AsmStr = cast_or_null<StringLiteral>( 4870 Importer.Import(S->getAsmString())); 4871 if (!AsmStr) 4872 return nullptr; 4873 4874 return new (Importer.getToContext()) GCCAsmStmt( 4875 Importer.getToContext(), 4876 Importer.Import(S->getAsmLoc()), 4877 S->isSimple(), 4878 S->isVolatile(), 4879 S->getNumOutputs(), 4880 S->getNumInputs(), 4881 Names.data(), 4882 Constraints.data(), 4883 Exprs.data(), 4884 AsmStr, 4885 S->getNumClobbers(), 4886 Clobbers.data(), 4887 Importer.Import(S->getRParenLoc())); 4888 } 4889 4890 Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) { 4891 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup()); 4892 for (Decl *ToD : ToDG) { 4893 if (!ToD) 4894 return nullptr; 4895 } 4896 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc()); 4897 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc()); 4898 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc); 4899 } 4900 4901 Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) { 4902 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc()); 4903 return new (Importer.getToContext()) NullStmt(ToSemiLoc, 4904 S->hasLeadingEmptyMacro()); 4905 } 4906 4907 Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) { 4908 llvm::SmallVector<Stmt *, 8> ToStmts(S->size()); 4909 4910 if (ImportArrayChecked(S->body_begin(), S->body_end(), ToStmts.begin())) 4911 return nullptr; 4912 4913 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc()); 4914 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc()); 4915 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(), 4916 ToStmts, 4917 ToLBraceLoc, ToRBraceLoc); 4918 } 4919 4920 Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) { 4921 Expr *ToLHS = Importer.Import(S->getLHS()); 4922 if (!ToLHS) 4923 return nullptr; 4924 Expr *ToRHS = Importer.Import(S->getRHS()); 4925 if (!ToRHS && S->getRHS()) 4926 return nullptr; 4927 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc()); 4928 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc()); 4929 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 4930 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS, 4931 ToCaseLoc, ToEllipsisLoc, 4932 ToColonLoc); 4933 } 4934 4935 Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) { 4936 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc()); 4937 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 4938 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 4939 if (!ToSubStmt && S->getSubStmt()) 4940 return nullptr; 4941 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc, 4942 ToSubStmt); 4943 } 4944 4945 Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) { 4946 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc()); 4947 LabelDecl *ToLabelDecl = 4948 cast_or_null<LabelDecl>(Importer.Import(S->getDecl())); 4949 if (!ToLabelDecl && S->getDecl()) 4950 return nullptr; 4951 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 4952 if (!ToSubStmt && S->getSubStmt()) 4953 return nullptr; 4954 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl, 4955 ToSubStmt); 4956 } 4957 4958 Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) { 4959 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc()); 4960 ArrayRef<const Attr*> FromAttrs(S->getAttrs()); 4961 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size()); 4962 ASTContext &_ToContext = Importer.getToContext(); 4963 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(), 4964 [&_ToContext](const Attr *A) -> const Attr * { 4965 return A->clone(_ToContext); 4966 }); 4967 for (const Attr *ToA : ToAttrs) { 4968 if (!ToA) 4969 return nullptr; 4970 } 4971 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 4972 if (!ToSubStmt && S->getSubStmt()) 4973 return nullptr; 4974 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc, 4975 ToAttrs, ToSubStmt); 4976 } 4977 4978 Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) { 4979 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc()); 4980 Stmt *ToInit = Importer.Import(S->getInit()); 4981 if (!ToInit && S->getInit()) 4982 return nullptr; 4983 VarDecl *ToConditionVariable = nullptr; 4984 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 4985 ToConditionVariable = 4986 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 4987 if (!ToConditionVariable) 4988 return nullptr; 4989 } 4990 Expr *ToCondition = Importer.Import(S->getCond()); 4991 if (!ToCondition && S->getCond()) 4992 return nullptr; 4993 Stmt *ToThenStmt = Importer.Import(S->getThen()); 4994 if (!ToThenStmt && S->getThen()) 4995 return nullptr; 4996 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc()); 4997 Stmt *ToElseStmt = Importer.Import(S->getElse()); 4998 if (!ToElseStmt && S->getElse()) 4999 return nullptr; 5000 return new (Importer.getToContext()) IfStmt(Importer.getToContext(), 5001 ToIfLoc, S->isConstexpr(), 5002 ToInit, 5003 ToConditionVariable, 5004 ToCondition, ToThenStmt, 5005 ToElseLoc, ToElseStmt); 5006 } 5007 5008 Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) { 5009 Stmt *ToInit = Importer.Import(S->getInit()); 5010 if (!ToInit && S->getInit()) 5011 return nullptr; 5012 VarDecl *ToConditionVariable = nullptr; 5013 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 5014 ToConditionVariable = 5015 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 5016 if (!ToConditionVariable) 5017 return nullptr; 5018 } 5019 Expr *ToCondition = Importer.Import(S->getCond()); 5020 if (!ToCondition && S->getCond()) 5021 return nullptr; 5022 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt( 5023 Importer.getToContext(), ToInit, 5024 ToConditionVariable, ToCondition); 5025 Stmt *ToBody = Importer.Import(S->getBody()); 5026 if (!ToBody && S->getBody()) 5027 return nullptr; 5028 ToStmt->setBody(ToBody); 5029 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc())); 5030 // Now we have to re-chain the cases. 5031 SwitchCase *LastChainedSwitchCase = nullptr; 5032 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr; 5033 SC = SC->getNextSwitchCase()) { 5034 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC)); 5035 if (!ToSC) 5036 return nullptr; 5037 if (LastChainedSwitchCase) 5038 LastChainedSwitchCase->setNextSwitchCase(ToSC); 5039 else 5040 ToStmt->setSwitchCaseList(ToSC); 5041 LastChainedSwitchCase = ToSC; 5042 } 5043 return ToStmt; 5044 } 5045 5046 Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) { 5047 VarDecl *ToConditionVariable = nullptr; 5048 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 5049 ToConditionVariable = 5050 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 5051 if (!ToConditionVariable) 5052 return nullptr; 5053 } 5054 Expr *ToCondition = Importer.Import(S->getCond()); 5055 if (!ToCondition && S->getCond()) 5056 return nullptr; 5057 Stmt *ToBody = Importer.Import(S->getBody()); 5058 if (!ToBody && S->getBody()) 5059 return nullptr; 5060 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); 5061 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(), 5062 ToConditionVariable, 5063 ToCondition, ToBody, 5064 ToWhileLoc); 5065 } 5066 5067 Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) { 5068 Stmt *ToBody = Importer.Import(S->getBody()); 5069 if (!ToBody && S->getBody()) 5070 return nullptr; 5071 Expr *ToCondition = Importer.Import(S->getCond()); 5072 if (!ToCondition && S->getCond()) 5073 return nullptr; 5074 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc()); 5075 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); 5076 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5077 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition, 5078 ToDoLoc, ToWhileLoc, 5079 ToRParenLoc); 5080 } 5081 5082 Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) { 5083 Stmt *ToInit = Importer.Import(S->getInit()); 5084 if (!ToInit && S->getInit()) 5085 return nullptr; 5086 Expr *ToCondition = Importer.Import(S->getCond()); 5087 if (!ToCondition && S->getCond()) 5088 return nullptr; 5089 VarDecl *ToConditionVariable = nullptr; 5090 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 5091 ToConditionVariable = 5092 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 5093 if (!ToConditionVariable) 5094 return nullptr; 5095 } 5096 Expr *ToInc = Importer.Import(S->getInc()); 5097 if (!ToInc && S->getInc()) 5098 return nullptr; 5099 Stmt *ToBody = Importer.Import(S->getBody()); 5100 if (!ToBody && S->getBody()) 5101 return nullptr; 5102 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 5103 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc()); 5104 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5105 return new (Importer.getToContext()) ForStmt(Importer.getToContext(), 5106 ToInit, ToCondition, 5107 ToConditionVariable, 5108 ToInc, ToBody, 5109 ToForLoc, ToLParenLoc, 5110 ToRParenLoc); 5111 } 5112 5113 Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) { 5114 LabelDecl *ToLabel = nullptr; 5115 if (LabelDecl *FromLabel = S->getLabel()) { 5116 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel)); 5117 if (!ToLabel) 5118 return nullptr; 5119 } 5120 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); 5121 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc()); 5122 return new (Importer.getToContext()) GotoStmt(ToLabel, 5123 ToGotoLoc, ToLabelLoc); 5124 } 5125 5126 Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { 5127 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); 5128 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc()); 5129 Expr *ToTarget = Importer.Import(S->getTarget()); 5130 if (!ToTarget && S->getTarget()) 5131 return nullptr; 5132 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc, 5133 ToTarget); 5134 } 5135 5136 Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) { 5137 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc()); 5138 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc); 5139 } 5140 5141 Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) { 5142 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc()); 5143 return new (Importer.getToContext()) BreakStmt(ToBreakLoc); 5144 } 5145 5146 Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) { 5147 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc()); 5148 Expr *ToRetExpr = Importer.Import(S->getRetValue()); 5149 if (!ToRetExpr && S->getRetValue()) 5150 return nullptr; 5151 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate()); 5152 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate)); 5153 if (!ToNRVOCandidate && NRVOCandidate) 5154 return nullptr; 5155 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr, 5156 ToNRVOCandidate); 5157 } 5158 5159 Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) { 5160 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc()); 5161 VarDecl *ToExceptionDecl = nullptr; 5162 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) { 5163 ToExceptionDecl = 5164 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); 5165 if (!ToExceptionDecl) 5166 return nullptr; 5167 } 5168 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock()); 5169 if (!ToHandlerBlock && S->getHandlerBlock()) 5170 return nullptr; 5171 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc, 5172 ToExceptionDecl, 5173 ToHandlerBlock); 5174 } 5175 5176 Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) { 5177 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc()); 5178 Stmt *ToTryBlock = Importer.Import(S->getTryBlock()); 5179 if (!ToTryBlock && S->getTryBlock()) 5180 return nullptr; 5181 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers()); 5182 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) { 5183 CXXCatchStmt *FromHandler = S->getHandler(HI); 5184 if (Stmt *ToHandler = Importer.Import(FromHandler)) 5185 ToHandlers[HI] = ToHandler; 5186 else 5187 return nullptr; 5188 } 5189 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock, 5190 ToHandlers); 5191 } 5192 5193 Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { 5194 DeclStmt *ToRange = 5195 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt())); 5196 if (!ToRange && S->getRangeStmt()) 5197 return nullptr; 5198 DeclStmt *ToBegin = 5199 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt())); 5200 if (!ToBegin && S->getBeginStmt()) 5201 return nullptr; 5202 DeclStmt *ToEnd = 5203 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt())); 5204 if (!ToEnd && S->getEndStmt()) 5205 return nullptr; 5206 Expr *ToCond = Importer.Import(S->getCond()); 5207 if (!ToCond && S->getCond()) 5208 return nullptr; 5209 Expr *ToInc = Importer.Import(S->getInc()); 5210 if (!ToInc && S->getInc()) 5211 return nullptr; 5212 DeclStmt *ToLoopVar = 5213 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt())); 5214 if (!ToLoopVar && S->getLoopVarStmt()) 5215 return nullptr; 5216 Stmt *ToBody = Importer.Import(S->getBody()); 5217 if (!ToBody && S->getBody()) 5218 return nullptr; 5219 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 5220 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc()); 5221 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 5222 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5223 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd, 5224 ToCond, ToInc, 5225 ToLoopVar, ToBody, 5226 ToForLoc, ToCoawaitLoc, 5227 ToColonLoc, ToRParenLoc); 5228 } 5229 5230 Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { 5231 Stmt *ToElem = Importer.Import(S->getElement()); 5232 if (!ToElem && S->getElement()) 5233 return nullptr; 5234 Expr *ToCollect = Importer.Import(S->getCollection()); 5235 if (!ToCollect && S->getCollection()) 5236 return nullptr; 5237 Stmt *ToBody = Importer.Import(S->getBody()); 5238 if (!ToBody && S->getBody()) 5239 return nullptr; 5240 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 5241 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5242 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem, 5243 ToCollect, 5244 ToBody, ToForLoc, 5245 ToRParenLoc); 5246 } 5247 5248 Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { 5249 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc()); 5250 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5251 VarDecl *ToExceptionDecl = nullptr; 5252 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) { 5253 ToExceptionDecl = 5254 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); 5255 if (!ToExceptionDecl) 5256 return nullptr; 5257 } 5258 Stmt *ToBody = Importer.Import(S->getCatchBody()); 5259 if (!ToBody && S->getCatchBody()) 5260 return nullptr; 5261 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc, 5262 ToRParenLoc, 5263 ToExceptionDecl, 5264 ToBody); 5265 } 5266 5267 Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { 5268 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc()); 5269 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody()); 5270 if (!ToAtFinallyStmt && S->getFinallyBody()) 5271 return nullptr; 5272 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc, 5273 ToAtFinallyStmt); 5274 } 5275 5276 Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { 5277 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc()); 5278 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody()); 5279 if (!ToAtTryStmt && S->getTryBody()) 5280 return nullptr; 5281 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts()); 5282 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) { 5283 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI); 5284 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt)) 5285 ToCatchStmts[CI] = ToCatchStmt; 5286 else 5287 return nullptr; 5288 } 5289 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt()); 5290 if (!ToAtFinallyStmt && S->getFinallyStmt()) 5291 return nullptr; 5292 return ObjCAtTryStmt::Create(Importer.getToContext(), 5293 ToAtTryLoc, ToAtTryStmt, 5294 ToCatchStmts.begin(), ToCatchStmts.size(), 5295 ToAtFinallyStmt); 5296 } 5297 5298 Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt 5299 (ObjCAtSynchronizedStmt *S) { 5300 SourceLocation ToAtSynchronizedLoc = 5301 Importer.Import(S->getAtSynchronizedLoc()); 5302 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr()); 5303 if (!ToSynchExpr && S->getSynchExpr()) 5304 return nullptr; 5305 Stmt *ToSynchBody = Importer.Import(S->getSynchBody()); 5306 if (!ToSynchBody && S->getSynchBody()) 5307 return nullptr; 5308 return new (Importer.getToContext()) ObjCAtSynchronizedStmt( 5309 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody); 5310 } 5311 5312 Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { 5313 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc()); 5314 Expr *ToThrow = Importer.Import(S->getThrowExpr()); 5315 if (!ToThrow && S->getThrowExpr()) 5316 return nullptr; 5317 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow); 5318 } 5319 5320 Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt 5321 (ObjCAutoreleasePoolStmt *S) { 5322 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc()); 5323 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 5324 if (!ToSubStmt && S->getSubStmt()) 5325 return nullptr; 5326 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc, 5327 ToSubStmt); 5328 } 5329 5330 //---------------------------------------------------------------------------- 5331 // Import Expressions 5332 //---------------------------------------------------------------------------- 5333 Expr *ASTNodeImporter::VisitExpr(Expr *E) { 5334 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node) 5335 << E->getStmtClassName(); 5336 return nullptr; 5337 } 5338 5339 Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) { 5340 QualType T = Importer.Import(E->getType()); 5341 if (T.isNull()) 5342 return nullptr; 5343 5344 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5345 if (!SubExpr && E->getSubExpr()) 5346 return nullptr; 5347 5348 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo()); 5349 if (!TInfo) 5350 return nullptr; 5351 5352 return new (Importer.getToContext()) VAArgExpr( 5353 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo, 5354 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI()); 5355 } 5356 5357 5358 Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) { 5359 QualType T = Importer.Import(E->getType()); 5360 if (T.isNull()) 5361 return nullptr; 5362 5363 return new (Importer.getToContext()) GNUNullExpr( 5364 T, Importer.Import(E->getExprLoc())); 5365 } 5366 5367 Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) { 5368 QualType T = Importer.Import(E->getType()); 5369 if (T.isNull()) 5370 return nullptr; 5371 5372 StringLiteral *SL = cast_or_null<StringLiteral>( 5373 Importer.Import(E->getFunctionName())); 5374 if (!SL && E->getFunctionName()) 5375 return nullptr; 5376 5377 return new (Importer.getToContext()) PredefinedExpr( 5378 Importer.Import(E->getExprLoc()), T, E->getIdentType(), SL); 5379 } 5380 5381 Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { 5382 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl())); 5383 if (!ToD) 5384 return nullptr; 5385 5386 NamedDecl *FoundD = nullptr; 5387 if (E->getDecl() != E->getFoundDecl()) { 5388 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl())); 5389 if (!FoundD) 5390 return nullptr; 5391 } 5392 5393 QualType T = Importer.Import(E->getType()); 5394 if (T.isNull()) 5395 return nullptr; 5396 5397 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(), 5398 Importer.Import(E->getQualifierLoc()), 5399 Importer.Import(E->getTemplateKeywordLoc()), 5400 ToD, 5401 E->refersToEnclosingVariableOrCapture(), 5402 Importer.Import(E->getLocation()), 5403 T, E->getValueKind(), 5404 FoundD, 5405 /*FIXME:TemplateArgs=*/nullptr); 5406 if (E->hadMultipleCandidates()) 5407 DRE->setHadMultipleCandidates(true); 5408 return DRE; 5409 } 5410 5411 Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { 5412 QualType T = Importer.Import(E->getType()); 5413 if (T.isNull()) 5414 return NULL; 5415 5416 return new (Importer.getToContext()) ImplicitValueInitExpr(T); 5417 } 5418 5419 ASTNodeImporter::Designator 5420 ASTNodeImporter::ImportDesignator(const Designator &D) { 5421 if (D.isFieldDesignator()) { 5422 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName()); 5423 // Caller checks for import error 5424 return Designator(ToFieldName, Importer.Import(D.getDotLoc()), 5425 Importer.Import(D.getFieldLoc())); 5426 } 5427 if (D.isArrayDesignator()) 5428 return Designator(D.getFirstExprIndex(), 5429 Importer.Import(D.getLBracketLoc()), 5430 Importer.Import(D.getRBracketLoc())); 5431 5432 assert(D.isArrayRangeDesignator()); 5433 return Designator(D.getFirstExprIndex(), 5434 Importer.Import(D.getLBracketLoc()), 5435 Importer.Import(D.getEllipsisLoc()), 5436 Importer.Import(D.getRBracketLoc())); 5437 } 5438 5439 5440 Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) { 5441 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit())); 5442 if (!Init) 5443 return nullptr; 5444 5445 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1); 5446 // List elements from the second, the first is Init itself 5447 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) { 5448 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I)))) 5449 IndexExprs[I - 1] = Arg; 5450 else 5451 return nullptr; 5452 } 5453 5454 SmallVector<Designator, 4> Designators(DIE->size()); 5455 llvm::transform(DIE->designators(), Designators.begin(), 5456 [this](const Designator &D) -> Designator { 5457 return ImportDesignator(D); 5458 }); 5459 5460 for (const Designator &D : DIE->designators()) 5461 if (D.isFieldDesignator() && !D.getFieldName()) 5462 return nullptr; 5463 5464 return DesignatedInitExpr::Create( 5465 Importer.getToContext(), Designators, 5466 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()), 5467 DIE->usesGNUSyntax(), Init); 5468 } 5469 5470 Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) { 5471 QualType T = Importer.Import(E->getType()); 5472 if (T.isNull()) 5473 return nullptr; 5474 5475 return new (Importer.getToContext()) 5476 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation())); 5477 } 5478 5479 Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { 5480 QualType T = Importer.Import(E->getType()); 5481 if (T.isNull()) 5482 return nullptr; 5483 5484 return IntegerLiteral::Create(Importer.getToContext(), 5485 E->getValue(), T, 5486 Importer.Import(E->getLocation())); 5487 } 5488 5489 Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) { 5490 QualType T = Importer.Import(E->getType()); 5491 if (T.isNull()) 5492 return nullptr; 5493 5494 return FloatingLiteral::Create(Importer.getToContext(), 5495 E->getValue(), E->isExact(), T, 5496 Importer.Import(E->getLocation())); 5497 } 5498 5499 Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { 5500 QualType T = Importer.Import(E->getType()); 5501 if (T.isNull()) 5502 return nullptr; 5503 5504 return new (Importer.getToContext()) CharacterLiteral(E->getValue(), 5505 E->getKind(), T, 5506 Importer.Import(E->getLocation())); 5507 } 5508 5509 Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) { 5510 QualType T = Importer.Import(E->getType()); 5511 if (T.isNull()) 5512 return nullptr; 5513 5514 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated()); 5515 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin()); 5516 5517 return StringLiteral::Create(Importer.getToContext(), E->getBytes(), 5518 E->getKind(), E->isPascal(), T, 5519 Locations.data(), Locations.size()); 5520 } 5521 5522 Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 5523 QualType T = Importer.Import(E->getType()); 5524 if (T.isNull()) 5525 return nullptr; 5526 5527 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo()); 5528 if (!TInfo) 5529 return nullptr; 5530 5531 Expr *Init = Importer.Import(E->getInitializer()); 5532 if (!Init) 5533 return nullptr; 5534 5535 return new (Importer.getToContext()) CompoundLiteralExpr( 5536 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(), 5537 Init, E->isFileScope()); 5538 } 5539 5540 Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) { 5541 QualType T = Importer.Import(E->getType()); 5542 if (T.isNull()) 5543 return nullptr; 5544 5545 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs()); 5546 if (ImportArrayChecked( 5547 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(), 5548 Exprs.begin())) 5549 return nullptr; 5550 5551 return new (Importer.getToContext()) AtomicExpr( 5552 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(), 5553 Importer.Import(E->getRParenLoc())); 5554 } 5555 5556 Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) { 5557 QualType T = Importer.Import(E->getType()); 5558 if (T.isNull()) 5559 return nullptr; 5560 5561 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel())); 5562 if (!ToLabel) 5563 return nullptr; 5564 5565 return new (Importer.getToContext()) AddrLabelExpr( 5566 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()), 5567 ToLabel, T); 5568 } 5569 5570 Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) { 5571 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5572 if (!SubExpr) 5573 return nullptr; 5574 5575 return new (Importer.getToContext()) 5576 ParenExpr(Importer.Import(E->getLParen()), 5577 Importer.Import(E->getRParen()), 5578 SubExpr); 5579 } 5580 5581 Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) { 5582 SmallVector<Expr *, 4> Exprs(E->getNumExprs()); 5583 if (ImportArrayChecked( 5584 E->getExprs(), E->getExprs() + E->getNumExprs(), Exprs.begin())) 5585 return nullptr; 5586 5587 return new (Importer.getToContext()) ParenListExpr( 5588 Importer.getToContext(), Importer.Import(E->getLParenLoc()), 5589 Exprs, Importer.Import(E->getLParenLoc())); 5590 } 5591 5592 Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) { 5593 QualType T = Importer.Import(E->getType()); 5594 if (T.isNull()) 5595 return nullptr; 5596 5597 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>( 5598 Importer.Import(E->getSubStmt())); 5599 if (!ToSubStmt && E->getSubStmt()) 5600 return nullptr; 5601 5602 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T, 5603 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc())); 5604 } 5605 5606 Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { 5607 QualType T = Importer.Import(E->getType()); 5608 if (T.isNull()) 5609 return nullptr; 5610 5611 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5612 if (!SubExpr) 5613 return nullptr; 5614 5615 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(), 5616 T, E->getValueKind(), 5617 E->getObjectKind(), 5618 Importer.Import(E->getOperatorLoc())); 5619 } 5620 5621 Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr( 5622 UnaryExprOrTypeTraitExpr *E) { 5623 QualType ResultType = Importer.Import(E->getType()); 5624 5625 if (E->isArgumentType()) { 5626 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo()); 5627 if (!TInfo) 5628 return nullptr; 5629 5630 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), 5631 TInfo, ResultType, 5632 Importer.Import(E->getOperatorLoc()), 5633 Importer.Import(E->getRParenLoc())); 5634 } 5635 5636 Expr *SubExpr = Importer.Import(E->getArgumentExpr()); 5637 if (!SubExpr) 5638 return nullptr; 5639 5640 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), 5641 SubExpr, ResultType, 5642 Importer.Import(E->getOperatorLoc()), 5643 Importer.Import(E->getRParenLoc())); 5644 } 5645 5646 Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { 5647 QualType T = Importer.Import(E->getType()); 5648 if (T.isNull()) 5649 return nullptr; 5650 5651 Expr *LHS = Importer.Import(E->getLHS()); 5652 if (!LHS) 5653 return nullptr; 5654 5655 Expr *RHS = Importer.Import(E->getRHS()); 5656 if (!RHS) 5657 return nullptr; 5658 5659 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(), 5660 T, E->getValueKind(), 5661 E->getObjectKind(), 5662 Importer.Import(E->getOperatorLoc()), 5663 E->isFPContractable()); 5664 } 5665 5666 Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) { 5667 QualType T = Importer.Import(E->getType()); 5668 if (T.isNull()) 5669 return nullptr; 5670 5671 Expr *ToLHS = Importer.Import(E->getLHS()); 5672 if (!ToLHS) 5673 return nullptr; 5674 5675 Expr *ToRHS = Importer.Import(E->getRHS()); 5676 if (!ToRHS) 5677 return nullptr; 5678 5679 Expr *ToCond = Importer.Import(E->getCond()); 5680 if (!ToCond) 5681 return nullptr; 5682 5683 return new (Importer.getToContext()) ConditionalOperator( 5684 ToCond, Importer.Import(E->getQuestionLoc()), 5685 ToLHS, Importer.Import(E->getColonLoc()), 5686 ToRHS, T, E->getValueKind(), E->getObjectKind()); 5687 } 5688 5689 Expr *ASTNodeImporter::VisitBinaryConditionalOperator( 5690 BinaryConditionalOperator *E) { 5691 QualType T = Importer.Import(E->getType()); 5692 if (T.isNull()) 5693 return nullptr; 5694 5695 Expr *Common = Importer.Import(E->getCommon()); 5696 if (!Common) 5697 return nullptr; 5698 5699 Expr *Cond = Importer.Import(E->getCond()); 5700 if (!Cond) 5701 return nullptr; 5702 5703 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>( 5704 Importer.Import(E->getOpaqueValue())); 5705 if (!OpaqueValue) 5706 return nullptr; 5707 5708 Expr *TrueExpr = Importer.Import(E->getTrueExpr()); 5709 if (!TrueExpr) 5710 return nullptr; 5711 5712 Expr *FalseExpr = Importer.Import(E->getFalseExpr()); 5713 if (!FalseExpr) 5714 return nullptr; 5715 5716 return new (Importer.getToContext()) BinaryConditionalOperator( 5717 Common, OpaqueValue, Cond, TrueExpr, FalseExpr, 5718 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()), 5719 T, E->getValueKind(), E->getObjectKind()); 5720 } 5721 5722 Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) { 5723 QualType T = Importer.Import(E->getType()); 5724 if (T.isNull()) 5725 return nullptr; 5726 5727 Expr *SourceExpr = Importer.Import(E->getSourceExpr()); 5728 if (!SourceExpr && E->getSourceExpr()) 5729 return nullptr; 5730 5731 return new (Importer.getToContext()) OpaqueValueExpr( 5732 Importer.Import(E->getExprLoc()), T, E->getValueKind(), 5733 E->getObjectKind(), SourceExpr); 5734 } 5735 5736 Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { 5737 QualType T = Importer.Import(E->getType()); 5738 if (T.isNull()) 5739 return nullptr; 5740 5741 QualType CompLHSType = Importer.Import(E->getComputationLHSType()); 5742 if (CompLHSType.isNull()) 5743 return nullptr; 5744 5745 QualType CompResultType = Importer.Import(E->getComputationResultType()); 5746 if (CompResultType.isNull()) 5747 return nullptr; 5748 5749 Expr *LHS = Importer.Import(E->getLHS()); 5750 if (!LHS) 5751 return nullptr; 5752 5753 Expr *RHS = Importer.Import(E->getRHS()); 5754 if (!RHS) 5755 return nullptr; 5756 5757 return new (Importer.getToContext()) 5758 CompoundAssignOperator(LHS, RHS, E->getOpcode(), 5759 T, E->getValueKind(), 5760 E->getObjectKind(), 5761 CompLHSType, CompResultType, 5762 Importer.Import(E->getOperatorLoc()), 5763 E->isFPContractable()); 5764 } 5765 5766 static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) { 5767 if (E->path_empty()) return false; 5768 5769 // TODO: import cast paths 5770 return true; 5771 } 5772 5773 Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { 5774 QualType T = Importer.Import(E->getType()); 5775 if (T.isNull()) 5776 return nullptr; 5777 5778 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5779 if (!SubExpr) 5780 return nullptr; 5781 5782 CXXCastPath BasePath; 5783 if (ImportCastPath(E, BasePath)) 5784 return nullptr; 5785 5786 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(), 5787 SubExpr, &BasePath, E->getValueKind()); 5788 } 5789 5790 Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) { 5791 QualType T = Importer.Import(E->getType()); 5792 if (T.isNull()) 5793 return nullptr; 5794 5795 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5796 if (!SubExpr) 5797 return nullptr; 5798 5799 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten()); 5800 if (!TInfo && E->getTypeInfoAsWritten()) 5801 return nullptr; 5802 5803 CXXCastPath BasePath; 5804 if (ImportCastPath(E, BasePath)) 5805 return nullptr; 5806 5807 return CStyleCastExpr::Create(Importer.getToContext(), T, 5808 E->getValueKind(), E->getCastKind(), 5809 SubExpr, &BasePath, TInfo, 5810 Importer.Import(E->getLParenLoc()), 5811 Importer.Import(E->getRParenLoc())); 5812 } 5813 5814 Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) { 5815 QualType T = Importer.Import(E->getType()); 5816 if (T.isNull()) 5817 return nullptr; 5818 5819 CXXConstructorDecl *ToCCD = 5820 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor())); 5821 if (!ToCCD) 5822 return nullptr; 5823 5824 SmallVector<Expr *, 6> ToArgs(E->getNumArgs()); 5825 if (ImportArrayChecked(E->getArgs(), E->getArgs() + E->getNumArgs(), 5826 ToArgs.begin())) 5827 return nullptr; 5828 5829 return CXXConstructExpr::Create(Importer.getToContext(), T, 5830 Importer.Import(E->getLocation()), 5831 ToCCD, E->isElidable(), 5832 ToArgs, E->hadMultipleCandidates(), 5833 E->isListInitialization(), 5834 E->isStdInitListInitialization(), 5835 E->requiresZeroInitialization(), 5836 E->getConstructionKind(), 5837 Importer.Import(E->getParenOrBraceRange())); 5838 } 5839 5840 Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { 5841 QualType T = Importer.Import(E->getType()); 5842 if (T.isNull()) 5843 return nullptr; 5844 5845 Expr *ToFn = Importer.Import(E->getCallee()); 5846 if (!ToFn) 5847 return nullptr; 5848 5849 SmallVector<Expr *, 4> ToArgs(E->getNumArgs()); 5850 5851 if (ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin())) 5852 return nullptr; 5853 5854 return new (Importer.getToContext()) CXXMemberCallExpr( 5855 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(), 5856 Importer.Import(E->getRParenLoc())); 5857 } 5858 5859 Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) { 5860 QualType T = Importer.Import(E->getType()); 5861 if (T.isNull()) 5862 return nullptr; 5863 5864 return new (Importer.getToContext()) 5865 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit()); 5866 } 5867 5868 Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { 5869 QualType T = Importer.Import(E->getType()); 5870 if (T.isNull()) 5871 return nullptr; 5872 5873 return new (Importer.getToContext()) 5874 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation())); 5875 } 5876 5877 5878 Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) { 5879 QualType T = Importer.Import(E->getType()); 5880 if (T.isNull()) 5881 return nullptr; 5882 5883 Expr *ToBase = Importer.Import(E->getBase()); 5884 if (!ToBase && E->getBase()) 5885 return nullptr; 5886 5887 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl())); 5888 if (!ToMember && E->getMemberDecl()) 5889 return nullptr; 5890 5891 DeclAccessPair ToFoundDecl = DeclAccessPair::make( 5892 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())), 5893 E->getFoundDecl().getAccess()); 5894 5895 DeclarationNameInfo ToMemberNameInfo( 5896 Importer.Import(E->getMemberNameInfo().getName()), 5897 Importer.Import(E->getMemberNameInfo().getLoc())); 5898 5899 if (E->hasExplicitTemplateArgs()) { 5900 return nullptr; // FIXME: handle template arguments 5901 } 5902 5903 return MemberExpr::Create(Importer.getToContext(), ToBase, 5904 E->isArrow(), 5905 Importer.Import(E->getOperatorLoc()), 5906 Importer.Import(E->getQualifierLoc()), 5907 Importer.Import(E->getTemplateKeywordLoc()), 5908 ToMember, ToFoundDecl, ToMemberNameInfo, 5909 nullptr, T, E->getValueKind(), 5910 E->getObjectKind()); 5911 } 5912 5913 Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) { 5914 QualType T = Importer.Import(E->getType()); 5915 if (T.isNull()) 5916 return nullptr; 5917 5918 Expr *ToCallee = Importer.Import(E->getCallee()); 5919 if (!ToCallee && E->getCallee()) 5920 return nullptr; 5921 5922 unsigned NumArgs = E->getNumArgs(); 5923 5924 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs); 5925 5926 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) { 5927 Expr *FromArg = E->getArg(ai); 5928 Expr *ToArg = Importer.Import(FromArg); 5929 if (!ToArg) 5930 return nullptr; 5931 ToArgs[ai] = ToArg; 5932 } 5933 5934 Expr **ToArgs_Copied = new (Importer.getToContext()) 5935 Expr*[NumArgs]; 5936 5937 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) 5938 ToArgs_Copied[ai] = ToArgs[ai]; 5939 5940 return new (Importer.getToContext()) 5941 CallExpr(Importer.getToContext(), ToCallee, 5942 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(), 5943 Importer.Import(E->getRParenLoc())); 5944 } 5945 5946 Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) { 5947 QualType T = Importer.Import(ILE->getType()); 5948 if (T.isNull()) 5949 return nullptr; 5950 5951 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits()); 5952 if (ImportArrayChecked( 5953 ILE->getInits(), ILE->getInits() + ILE->getNumInits(), Exprs.begin())) 5954 return nullptr; 5955 5956 ASTContext &ToCtx = Importer.getToContext(); 5957 InitListExpr *To = new (ToCtx) InitListExpr( 5958 ToCtx, Importer.Import(ILE->getLBraceLoc()), 5959 Exprs, Importer.Import(ILE->getLBraceLoc())); 5960 To->setType(T); 5961 5962 if (ILE->hasArrayFiller()) { 5963 Expr *Filler = Importer.Import(ILE->getArrayFiller()); 5964 if (!Filler) 5965 return nullptr; 5966 To->setArrayFiller(Filler); 5967 } 5968 5969 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) { 5970 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD)); 5971 if (!ToFD) 5972 return nullptr; 5973 To->setInitializedFieldInUnion(ToFD); 5974 } 5975 5976 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) { 5977 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>( 5978 Importer.Import(SyntForm)); 5979 if (!ToSyntForm) 5980 return nullptr; 5981 To->setSyntacticForm(ToSyntForm); 5982 } 5983 5984 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator()); 5985 To->setValueDependent(ILE->isValueDependent()); 5986 To->setInstantiationDependent(ILE->isInstantiationDependent()); 5987 5988 return To; 5989 } 5990 5991 Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) { 5992 FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>( 5993 Importer.Import(DIE->getField())); 5994 if (!ToField && DIE->getField()) 5995 return nullptr; 5996 5997 return CXXDefaultInitExpr::Create( 5998 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField); 5999 } 6000 6001 Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { 6002 QualType ToType = Importer.Import(E->getType()); 6003 if (ToType.isNull() && !E->getType().isNull()) 6004 return nullptr; 6005 ExprValueKind VK = E->getValueKind(); 6006 CastKind CK = E->getCastKind(); 6007 Expr *ToOp = Importer.Import(E->getSubExpr()); 6008 if (!ToOp && E->getSubExpr()) 6009 return nullptr; 6010 CXXCastPath BasePath; 6011 if (ImportCastPath(E, BasePath)) 6012 return nullptr; 6013 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten()); 6014 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc()); 6015 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc()); 6016 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets()); 6017 6018 if (isa<CXXStaticCastExpr>(E)) { 6019 return CXXStaticCastExpr::Create( 6020 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, 6021 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); 6022 } else if (isa<CXXDynamicCastExpr>(E)) { 6023 return CXXDynamicCastExpr::Create( 6024 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, 6025 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); 6026 } else if (isa<CXXReinterpretCastExpr>(E)) { 6027 return CXXReinterpretCastExpr::Create( 6028 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, 6029 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); 6030 } else { 6031 return nullptr; 6032 } 6033 } 6034 6035 ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, 6036 ASTContext &FromContext, FileManager &FromFileManager, 6037 bool MinimalImport) 6038 : ToContext(ToContext), FromContext(FromContext), 6039 ToFileManager(ToFileManager), FromFileManager(FromFileManager), 6040 Minimal(MinimalImport), LastDiagFromFrom(false) 6041 { 6042 ImportedDecls[FromContext.getTranslationUnitDecl()] 6043 = ToContext.getTranslationUnitDecl(); 6044 } 6045 6046 ASTImporter::~ASTImporter() { } 6047 6048 QualType ASTImporter::Import(QualType FromT) { 6049 if (FromT.isNull()) 6050 return QualType(); 6051 6052 const Type *fromTy = FromT.getTypePtr(); 6053 6054 // Check whether we've already imported this type. 6055 llvm::DenseMap<const Type *, const Type *>::iterator Pos 6056 = ImportedTypes.find(fromTy); 6057 if (Pos != ImportedTypes.end()) 6058 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers()); 6059 6060 // Import the type 6061 ASTNodeImporter Importer(*this); 6062 QualType ToT = Importer.Visit(fromTy); 6063 if (ToT.isNull()) 6064 return ToT; 6065 6066 // Record the imported type. 6067 ImportedTypes[fromTy] = ToT.getTypePtr(); 6068 6069 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers()); 6070 } 6071 6072 TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { 6073 if (!FromTSI) 6074 return FromTSI; 6075 6076 // FIXME: For now we just create a "trivial" type source info based 6077 // on the type and a single location. Implement a real version of this. 6078 QualType T = Import(FromTSI->getType()); 6079 if (T.isNull()) 6080 return nullptr; 6081 6082 return ToContext.getTrivialTypeSourceInfo(T, 6083 Import(FromTSI->getTypeLoc().getLocStart())); 6084 } 6085 6086 Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) { 6087 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); 6088 if (Pos != ImportedDecls.end()) { 6089 Decl *ToD = Pos->second; 6090 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD); 6091 return ToD; 6092 } else { 6093 return nullptr; 6094 } 6095 } 6096 6097 Decl *ASTImporter::Import(Decl *FromD) { 6098 if (!FromD) 6099 return nullptr; 6100 6101 ASTNodeImporter Importer(*this); 6102 6103 // Check whether we've already imported this declaration. 6104 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); 6105 if (Pos != ImportedDecls.end()) { 6106 Decl *ToD = Pos->second; 6107 Importer.ImportDefinitionIfNeeded(FromD, ToD); 6108 return ToD; 6109 } 6110 6111 // Import the type 6112 Decl *ToD = Importer.Visit(FromD); 6113 if (!ToD) 6114 return nullptr; 6115 6116 // Record the imported declaration. 6117 ImportedDecls[FromD] = ToD; 6118 6119 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) { 6120 // Keep track of anonymous tags that have an associated typedef. 6121 if (FromTag->getTypedefNameForAnonDecl()) 6122 AnonTagsWithPendingTypedefs.push_back(FromTag); 6123 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) { 6124 // When we've finished transforming a typedef, see whether it was the 6125 // typedef for an anonymous tag. 6126 for (SmallVectorImpl<TagDecl *>::iterator 6127 FromTag = AnonTagsWithPendingTypedefs.begin(), 6128 FromTagEnd = AnonTagsWithPendingTypedefs.end(); 6129 FromTag != FromTagEnd; ++FromTag) { 6130 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) { 6131 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) { 6132 // We found the typedef for an anonymous tag; link them. 6133 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD)); 6134 AnonTagsWithPendingTypedefs.erase(FromTag); 6135 break; 6136 } 6137 } 6138 } 6139 } 6140 6141 return ToD; 6142 } 6143 6144 DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) { 6145 if (!FromDC) 6146 return FromDC; 6147 6148 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC))); 6149 if (!ToDC) 6150 return nullptr; 6151 6152 // When we're using a record/enum/Objective-C class/protocol as a context, we 6153 // need it to have a definition. 6154 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) { 6155 RecordDecl *FromRecord = cast<RecordDecl>(FromDC); 6156 if (ToRecord->isCompleteDefinition()) { 6157 // Do nothing. 6158 } else if (FromRecord->isCompleteDefinition()) { 6159 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord, 6160 ASTNodeImporter::IDK_Basic); 6161 } else { 6162 CompleteDecl(ToRecord); 6163 } 6164 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) { 6165 EnumDecl *FromEnum = cast<EnumDecl>(FromDC); 6166 if (ToEnum->isCompleteDefinition()) { 6167 // Do nothing. 6168 } else if (FromEnum->isCompleteDefinition()) { 6169 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum, 6170 ASTNodeImporter::IDK_Basic); 6171 } else { 6172 CompleteDecl(ToEnum); 6173 } 6174 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) { 6175 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC); 6176 if (ToClass->getDefinition()) { 6177 // Do nothing. 6178 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { 6179 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass, 6180 ASTNodeImporter::IDK_Basic); 6181 } else { 6182 CompleteDecl(ToClass); 6183 } 6184 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) { 6185 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC); 6186 if (ToProto->getDefinition()) { 6187 // Do nothing. 6188 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { 6189 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto, 6190 ASTNodeImporter::IDK_Basic); 6191 } else { 6192 CompleteDecl(ToProto); 6193 } 6194 } 6195 6196 return ToDC; 6197 } 6198 6199 Expr *ASTImporter::Import(Expr *FromE) { 6200 if (!FromE) 6201 return nullptr; 6202 6203 return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); 6204 } 6205 6206 Stmt *ASTImporter::Import(Stmt *FromS) { 6207 if (!FromS) 6208 return nullptr; 6209 6210 // Check whether we've already imported this declaration. 6211 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); 6212 if (Pos != ImportedStmts.end()) 6213 return Pos->second; 6214 6215 // Import the type 6216 ASTNodeImporter Importer(*this); 6217 Stmt *ToS = Importer.Visit(FromS); 6218 if (!ToS) 6219 return nullptr; 6220 6221 // Record the imported declaration. 6222 ImportedStmts[FromS] = ToS; 6223 return ToS; 6224 } 6225 6226 NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { 6227 if (!FromNNS) 6228 return nullptr; 6229 6230 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix()); 6231 6232 switch (FromNNS->getKind()) { 6233 case NestedNameSpecifier::Identifier: 6234 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) { 6235 return NestedNameSpecifier::Create(ToContext, prefix, II); 6236 } 6237 return nullptr; 6238 6239 case NestedNameSpecifier::Namespace: 6240 if (NamespaceDecl *NS = 6241 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) { 6242 return NestedNameSpecifier::Create(ToContext, prefix, NS); 6243 } 6244 return nullptr; 6245 6246 case NestedNameSpecifier::NamespaceAlias: 6247 if (NamespaceAliasDecl *NSAD = 6248 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) { 6249 return NestedNameSpecifier::Create(ToContext, prefix, NSAD); 6250 } 6251 return nullptr; 6252 6253 case NestedNameSpecifier::Global: 6254 return NestedNameSpecifier::GlobalSpecifier(ToContext); 6255 6256 case NestedNameSpecifier::Super: 6257 if (CXXRecordDecl *RD = 6258 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) { 6259 return NestedNameSpecifier::SuperSpecifier(ToContext, RD); 6260 } 6261 return nullptr; 6262 6263 case NestedNameSpecifier::TypeSpec: 6264 case NestedNameSpecifier::TypeSpecWithTemplate: { 6265 QualType T = Import(QualType(FromNNS->getAsType(), 0u)); 6266 if (!T.isNull()) { 6267 bool bTemplate = FromNNS->getKind() == 6268 NestedNameSpecifier::TypeSpecWithTemplate; 6269 return NestedNameSpecifier::Create(ToContext, prefix, 6270 bTemplate, T.getTypePtr()); 6271 } 6272 } 6273 return nullptr; 6274 } 6275 6276 llvm_unreachable("Invalid nested name specifier kind"); 6277 } 6278 6279 NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { 6280 // FIXME: Implement! 6281 return NestedNameSpecifierLoc(); 6282 } 6283 6284 TemplateName ASTImporter::Import(TemplateName From) { 6285 switch (From.getKind()) { 6286 case TemplateName::Template: 6287 if (TemplateDecl *ToTemplate 6288 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) 6289 return TemplateName(ToTemplate); 6290 6291 return TemplateName(); 6292 6293 case TemplateName::OverloadedTemplate: { 6294 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); 6295 UnresolvedSet<2> ToTemplates; 6296 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(), 6297 E = FromStorage->end(); 6298 I != E; ++I) { 6299 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I))) 6300 ToTemplates.addDecl(To); 6301 else 6302 return TemplateName(); 6303 } 6304 return ToContext.getOverloadedTemplateName(ToTemplates.begin(), 6305 ToTemplates.end()); 6306 } 6307 6308 case TemplateName::QualifiedTemplate: { 6309 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); 6310 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier()); 6311 if (!Qualifier) 6312 return TemplateName(); 6313 6314 if (TemplateDecl *ToTemplate 6315 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) 6316 return ToContext.getQualifiedTemplateName(Qualifier, 6317 QTN->hasTemplateKeyword(), 6318 ToTemplate); 6319 6320 return TemplateName(); 6321 } 6322 6323 case TemplateName::DependentTemplate: { 6324 DependentTemplateName *DTN = From.getAsDependentTemplateName(); 6325 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier()); 6326 if (!Qualifier) 6327 return TemplateName(); 6328 6329 if (DTN->isIdentifier()) { 6330 return ToContext.getDependentTemplateName(Qualifier, 6331 Import(DTN->getIdentifier())); 6332 } 6333 6334 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator()); 6335 } 6336 6337 case TemplateName::SubstTemplateTemplateParm: { 6338 SubstTemplateTemplateParmStorage *subst 6339 = From.getAsSubstTemplateTemplateParm(); 6340 TemplateTemplateParmDecl *param 6341 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter())); 6342 if (!param) 6343 return TemplateName(); 6344 6345 TemplateName replacement = Import(subst->getReplacement()); 6346 if (replacement.isNull()) return TemplateName(); 6347 6348 return ToContext.getSubstTemplateTemplateParm(param, replacement); 6349 } 6350 6351 case TemplateName::SubstTemplateTemplateParmPack: { 6352 SubstTemplateTemplateParmPackStorage *SubstPack 6353 = From.getAsSubstTemplateTemplateParmPack(); 6354 TemplateTemplateParmDecl *Param 6355 = cast_or_null<TemplateTemplateParmDecl>( 6356 Import(SubstPack->getParameterPack())); 6357 if (!Param) 6358 return TemplateName(); 6359 6360 ASTNodeImporter Importer(*this); 6361 TemplateArgument ArgPack 6362 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); 6363 if (ArgPack.isNull()) 6364 return TemplateName(); 6365 6366 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack); 6367 } 6368 } 6369 6370 llvm_unreachable("Invalid template name kind"); 6371 } 6372 6373 SourceLocation ASTImporter::Import(SourceLocation FromLoc) { 6374 if (FromLoc.isInvalid()) 6375 return SourceLocation(); 6376 6377 SourceManager &FromSM = FromContext.getSourceManager(); 6378 6379 // For now, map everything down to its spelling location, so that we 6380 // don't have to import macro expansions. 6381 // FIXME: Import macro expansions! 6382 FromLoc = FromSM.getSpellingLoc(FromLoc); 6383 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); 6384 SourceManager &ToSM = ToContext.getSourceManager(); 6385 FileID ToFileID = Import(Decomposed.first); 6386 if (ToFileID.isInvalid()) 6387 return SourceLocation(); 6388 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID) 6389 .getLocWithOffset(Decomposed.second); 6390 return ret; 6391 } 6392 6393 SourceRange ASTImporter::Import(SourceRange FromRange) { 6394 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); 6395 } 6396 6397 FileID ASTImporter::Import(FileID FromID) { 6398 llvm::DenseMap<FileID, FileID>::iterator Pos 6399 = ImportedFileIDs.find(FromID); 6400 if (Pos != ImportedFileIDs.end()) 6401 return Pos->second; 6402 6403 SourceManager &FromSM = FromContext.getSourceManager(); 6404 SourceManager &ToSM = ToContext.getSourceManager(); 6405 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); 6406 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet"); 6407 6408 // Include location of this file. 6409 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); 6410 6411 // Map the FileID for to the "to" source manager. 6412 FileID ToID; 6413 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); 6414 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { 6415 // FIXME: We probably want to use getVirtualFile(), so we don't hit the 6416 // disk again 6417 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather 6418 // than mmap the files several times. 6419 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName()); 6420 if (!Entry) 6421 return FileID(); 6422 ToID = ToSM.createFileID(Entry, ToIncludeLoc, 6423 FromSLoc.getFile().getFileCharacteristic()); 6424 } else { 6425 // FIXME: We want to re-use the existing MemoryBuffer! 6426 const llvm::MemoryBuffer * 6427 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM); 6428 std::unique_ptr<llvm::MemoryBuffer> ToBuf 6429 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(), 6430 FromBuf->getBufferIdentifier()); 6431 ToID = ToSM.createFileID(std::move(ToBuf), 6432 FromSLoc.getFile().getFileCharacteristic()); 6433 } 6434 6435 6436 ImportedFileIDs[FromID] = ToID; 6437 return ToID; 6438 } 6439 6440 CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) { 6441 Expr *ToExpr = Import(From->getInit()); 6442 if (!ToExpr && From->getInit()) 6443 return nullptr; 6444 6445 if (From->isBaseInitializer()) { 6446 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo()); 6447 if (!ToTInfo && From->getTypeSourceInfo()) 6448 return nullptr; 6449 6450 return new (ToContext) CXXCtorInitializer( 6451 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()), 6452 ToExpr, Import(From->getRParenLoc()), 6453 From->isPackExpansion() ? Import(From->getEllipsisLoc()) 6454 : SourceLocation()); 6455 } else if (From->isMemberInitializer()) { 6456 FieldDecl *ToField = 6457 llvm::cast_or_null<FieldDecl>(Import(From->getMember())); 6458 if (!ToField && From->getMember()) 6459 return nullptr; 6460 6461 return new (ToContext) CXXCtorInitializer( 6462 ToContext, ToField, Import(From->getMemberLocation()), 6463 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc())); 6464 } else if (From->isIndirectMemberInitializer()) { 6465 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>( 6466 Import(From->getIndirectMember())); 6467 if (!ToIField && From->getIndirectMember()) 6468 return nullptr; 6469 6470 return new (ToContext) CXXCtorInitializer( 6471 ToContext, ToIField, Import(From->getMemberLocation()), 6472 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc())); 6473 } else if (From->isDelegatingInitializer()) { 6474 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo()); 6475 if (!ToTInfo && From->getTypeSourceInfo()) 6476 return nullptr; 6477 6478 return new (ToContext) 6479 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()), 6480 ToExpr, Import(From->getRParenLoc())); 6481 } else if (unsigned NumArrayIndices = From->getNumArrayIndices()) { 6482 FieldDecl *ToField = 6483 llvm::cast_or_null<FieldDecl>(Import(From->getMember())); 6484 if (!ToField && From->getMember()) 6485 return nullptr; 6486 6487 SmallVector<VarDecl *, 4> ToAIs(NumArrayIndices); 6488 6489 for (unsigned AII = 0; AII < NumArrayIndices; ++AII) { 6490 VarDecl *ToArrayIndex = 6491 dyn_cast_or_null<VarDecl>(Import(From->getArrayIndex(AII))); 6492 if (!ToArrayIndex && From->getArrayIndex(AII)) 6493 return nullptr; 6494 } 6495 6496 return CXXCtorInitializer::Create( 6497 ToContext, ToField, Import(From->getMemberLocation()), 6498 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()), 6499 ToAIs.data(), NumArrayIndices); 6500 } else { 6501 return nullptr; 6502 } 6503 } 6504 6505 6506 void ASTImporter::ImportDefinition(Decl *From) { 6507 Decl *To = Import(From); 6508 if (!To) 6509 return; 6510 6511 if (DeclContext *FromDC = cast<DeclContext>(From)) { 6512 ASTNodeImporter Importer(*this); 6513 6514 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) { 6515 if (!ToRecord->getDefinition()) { 6516 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord, 6517 ASTNodeImporter::IDK_Everything); 6518 return; 6519 } 6520 } 6521 6522 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) { 6523 if (!ToEnum->getDefinition()) { 6524 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum, 6525 ASTNodeImporter::IDK_Everything); 6526 return; 6527 } 6528 } 6529 6530 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) { 6531 if (!ToIFace->getDefinition()) { 6532 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace, 6533 ASTNodeImporter::IDK_Everything); 6534 return; 6535 } 6536 } 6537 6538 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) { 6539 if (!ToProto->getDefinition()) { 6540 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto, 6541 ASTNodeImporter::IDK_Everything); 6542 return; 6543 } 6544 } 6545 6546 Importer.ImportDeclContext(FromDC, true); 6547 } 6548 } 6549 6550 DeclarationName ASTImporter::Import(DeclarationName FromName) { 6551 if (!FromName) 6552 return DeclarationName(); 6553 6554 switch (FromName.getNameKind()) { 6555 case DeclarationName::Identifier: 6556 return Import(FromName.getAsIdentifierInfo()); 6557 6558 case DeclarationName::ObjCZeroArgSelector: 6559 case DeclarationName::ObjCOneArgSelector: 6560 case DeclarationName::ObjCMultiArgSelector: 6561 return Import(FromName.getObjCSelector()); 6562 6563 case DeclarationName::CXXConstructorName: { 6564 QualType T = Import(FromName.getCXXNameType()); 6565 if (T.isNull()) 6566 return DeclarationName(); 6567 6568 return ToContext.DeclarationNames.getCXXConstructorName( 6569 ToContext.getCanonicalType(T)); 6570 } 6571 6572 case DeclarationName::CXXDestructorName: { 6573 QualType T = Import(FromName.getCXXNameType()); 6574 if (T.isNull()) 6575 return DeclarationName(); 6576 6577 return ToContext.DeclarationNames.getCXXDestructorName( 6578 ToContext.getCanonicalType(T)); 6579 } 6580 6581 case DeclarationName::CXXConversionFunctionName: { 6582 QualType T = Import(FromName.getCXXNameType()); 6583 if (T.isNull()) 6584 return DeclarationName(); 6585 6586 return ToContext.DeclarationNames.getCXXConversionFunctionName( 6587 ToContext.getCanonicalType(T)); 6588 } 6589 6590 case DeclarationName::CXXOperatorName: 6591 return ToContext.DeclarationNames.getCXXOperatorName( 6592 FromName.getCXXOverloadedOperator()); 6593 6594 case DeclarationName::CXXLiteralOperatorName: 6595 return ToContext.DeclarationNames.getCXXLiteralOperatorName( 6596 Import(FromName.getCXXLiteralIdentifier())); 6597 6598 case DeclarationName::CXXUsingDirective: 6599 // FIXME: STATICS! 6600 return DeclarationName::getUsingDirectiveName(); 6601 } 6602 6603 llvm_unreachable("Invalid DeclarationName Kind!"); 6604 } 6605 6606 IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { 6607 if (!FromId) 6608 return nullptr; 6609 6610 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName()); 6611 6612 if (!ToId->getBuiltinID() && FromId->getBuiltinID()) 6613 ToId->setBuiltinID(FromId->getBuiltinID()); 6614 6615 return ToId; 6616 } 6617 6618 Selector ASTImporter::Import(Selector FromSel) { 6619 if (FromSel.isNull()) 6620 return Selector(); 6621 6622 SmallVector<IdentifierInfo *, 4> Idents; 6623 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); 6624 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) 6625 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); 6626 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); 6627 } 6628 6629 DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name, 6630 DeclContext *DC, 6631 unsigned IDNS, 6632 NamedDecl **Decls, 6633 unsigned NumDecls) { 6634 return Name; 6635 } 6636 6637 DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { 6638 if (LastDiagFromFrom) 6639 ToContext.getDiagnostics().notePriorDiagnosticFrom( 6640 FromContext.getDiagnostics()); 6641 LastDiagFromFrom = false; 6642 return ToContext.getDiagnostics().Report(Loc, DiagID); 6643 } 6644 6645 DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { 6646 if (!LastDiagFromFrom) 6647 FromContext.getDiagnostics().notePriorDiagnosticFrom( 6648 ToContext.getDiagnostics()); 6649 LastDiagFromFrom = true; 6650 return FromContext.getDiagnostics().Report(Loc, DiagID); 6651 } 6652 6653 void ASTImporter::CompleteDecl (Decl *D) { 6654 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { 6655 if (!ID->getDefinition()) 6656 ID->startDefinition(); 6657 } 6658 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { 6659 if (!PD->getDefinition()) 6660 PD->startDefinition(); 6661 } 6662 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 6663 if (!TD->getDefinition() && !TD->isBeingDefined()) { 6664 TD->startDefinition(); 6665 TD->setCompleteDefinition(true); 6666 } 6667 } 6668 else { 6669 assert (0 && "CompleteDecl called on a Decl that can't be completed"); 6670 } 6671 } 6672 6673 Decl *ASTImporter::Imported(Decl *From, Decl *To) { 6674 if (From->hasAttrs()) { 6675 for (Attr *FromAttr : From->getAttrs()) 6676 To->addAttr(FromAttr->clone(To->getASTContext())); 6677 } 6678 if (From->isUsed()) { 6679 To->setIsUsed(); 6680 } 6681 if (From->isImplicit()) { 6682 To->setImplicit(); 6683 } 6684 ImportedDecls[From] = To; 6685 return To; 6686 } 6687 6688 bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, 6689 bool Complain) { 6690 llvm::DenseMap<const Type *, const Type *>::iterator Pos 6691 = ImportedTypes.find(From.getTypePtr()); 6692 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To)) 6693 return true; 6694 6695 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls, 6696 false, Complain); 6697 return Ctx.IsStructurallyEquivalent(From, To); 6698 } 6699