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