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