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