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 both unnamed structs/unions are in a record context, make sure 2076 // they occur in the same location in the context records. 2077 if (Optional<unsigned> Index1 = 2078 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex( 2079 D)) { 2080 if (Optional<unsigned> Index2 = StructuralEquivalenceContext:: 2081 findUntaggedStructOrUnionIndex(FoundRecord)) { 2082 if (*Index1 != *Index2) 2083 continue; 2084 } 2085 } 2086 } 2087 2088 PrevDecl = FoundRecord; 2089 2090 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { 2091 if ((SearchName && !D->isCompleteDefinition()) 2092 || (D->isCompleteDefinition() && 2093 D->isAnonymousStructOrUnion() 2094 == FoundDef->isAnonymousStructOrUnion() && 2095 IsStructuralMatch(D, FoundDef))) { 2096 // The record types structurally match, or the "from" translation 2097 // unit only had a forward declaration anyway; call it the same 2098 // function. 2099 // FIXME: For C++, we should also merge methods here. 2100 return Importer.Imported(D, FoundDef); 2101 } 2102 } else if (!D->isCompleteDefinition()) { 2103 // We have a forward declaration of this type, so adopt that forward 2104 // declaration rather than building a new one. 2105 2106 // If one or both can be completed from external storage then try one 2107 // last time to complete and compare them before doing this. 2108 2109 if (FoundRecord->hasExternalLexicalStorage() && 2110 !FoundRecord->isCompleteDefinition()) 2111 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord); 2112 if (D->hasExternalLexicalStorage()) 2113 D->getASTContext().getExternalSource()->CompleteType(D); 2114 2115 if (FoundRecord->isCompleteDefinition() && 2116 D->isCompleteDefinition() && 2117 !IsStructuralMatch(D, FoundRecord)) 2118 continue; 2119 2120 AdoptDecl = FoundRecord; 2121 continue; 2122 } else if (!SearchName) { 2123 continue; 2124 } 2125 } 2126 2127 ConflictingDecls.push_back(FoundDecl); 2128 } 2129 2130 if (!ConflictingDecls.empty() && SearchName) { 2131 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2132 ConflictingDecls.data(), 2133 ConflictingDecls.size()); 2134 } 2135 } 2136 2137 // Create the record declaration. 2138 RecordDecl *D2 = AdoptDecl; 2139 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 2140 if (!D2) { 2141 CXXRecordDecl *D2CXX = nullptr; 2142 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) { 2143 if (DCXX->isLambda()) { 2144 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo()); 2145 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(), 2146 DC, TInfo, Loc, 2147 DCXX->isDependentLambda(), 2148 DCXX->isGenericLambda(), 2149 DCXX->getLambdaCaptureDefault()); 2150 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl()); 2151 if (DCXX->getLambdaContextDecl() && !CDecl) 2152 return nullptr; 2153 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl); 2154 } else if (DCXX->isInjectedClassName()) { 2155 // We have to be careful to do a similar dance to the one in 2156 // Sema::ActOnStartCXXMemberDeclarations 2157 CXXRecordDecl *const PrevDecl = nullptr; 2158 const bool DelayTypeCreation = true; 2159 D2CXX = CXXRecordDecl::Create( 2160 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc, 2161 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation); 2162 Importer.getToContext().getTypeDeclType( 2163 D2CXX, dyn_cast<CXXRecordDecl>(DC)); 2164 } else { 2165 D2CXX = CXXRecordDecl::Create(Importer.getToContext(), 2166 D->getTagKind(), 2167 DC, StartLoc, Loc, 2168 Name.getAsIdentifierInfo()); 2169 } 2170 D2 = D2CXX; 2171 D2->setAccess(D->getAccess()); 2172 D2->setLexicalDeclContext(LexicalDC); 2173 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit()) 2174 LexicalDC->addDeclInternal(D2); 2175 2176 Importer.Imported(D, D2); 2177 2178 if (ClassTemplateDecl *FromDescribed = 2179 DCXX->getDescribedClassTemplate()) { 2180 auto *ToDescribed = cast_or_null<ClassTemplateDecl>( 2181 Importer.Import(FromDescribed)); 2182 if (!ToDescribed) 2183 return nullptr; 2184 D2CXX->setDescribedClassTemplate(ToDescribed); 2185 if (!DCXX->isInjectedClassName()) { 2186 // In a record describing a template the type should be an 2187 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the 2188 // previously set type to the correct value here (ToDescribed is not 2189 // available at record create). 2190 // FIXME: The previous type is cleared but not removed from 2191 // ASTContext's internal storage. 2192 CXXRecordDecl *Injected = nullptr; 2193 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) { 2194 auto *Record = dyn_cast<CXXRecordDecl>(Found); 2195 if (Record && Record->isInjectedClassName()) { 2196 Injected = Record; 2197 break; 2198 } 2199 } 2200 D2CXX->setTypeForDecl(nullptr); 2201 Importer.getToContext().getInjectedClassNameType(D2CXX, 2202 ToDescribed->getInjectedClassNameSpecialization()); 2203 if (Injected) { 2204 Injected->setTypeForDecl(nullptr); 2205 Importer.getToContext().getTypeDeclType(Injected, D2CXX); 2206 } 2207 } 2208 } else if (MemberSpecializationInfo *MemberInfo = 2209 DCXX->getMemberSpecializationInfo()) { 2210 TemplateSpecializationKind SK = 2211 MemberInfo->getTemplateSpecializationKind(); 2212 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass(); 2213 auto *ToInst = 2214 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst)); 2215 if (FromInst && !ToInst) 2216 return nullptr; 2217 D2CXX->setInstantiationOfMemberClass(ToInst, SK); 2218 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation( 2219 Importer.Import(MemberInfo->getPointOfInstantiation())); 2220 } 2221 } else { 2222 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(), 2223 DC, StartLoc, Loc, Name.getAsIdentifierInfo()); 2224 D2->setLexicalDeclContext(LexicalDC); 2225 LexicalDC->addDeclInternal(D2); 2226 } 2227 2228 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 2229 if (D->isAnonymousStructOrUnion()) 2230 D2->setAnonymousStructOrUnion(true); 2231 if (PrevDecl) { 2232 // FIXME: do this for all Redeclarables, not just RecordDecls. 2233 D2->setPreviousDecl(PrevDecl); 2234 } 2235 } 2236 2237 Importer.Imported(D, D2); 2238 2239 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default)) 2240 return nullptr; 2241 2242 return D2; 2243 } 2244 2245 Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { 2246 // Import the major distinguishing characteristics of this enumerator. 2247 DeclContext *DC, *LexicalDC; 2248 DeclarationName Name; 2249 SourceLocation Loc; 2250 NamedDecl *ToD; 2251 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2252 return nullptr; 2253 if (ToD) 2254 return ToD; 2255 2256 QualType T = Importer.Import(D->getType()); 2257 if (T.isNull()) 2258 return nullptr; 2259 2260 // Determine whether there are any other declarations with the same name and 2261 // in the same context. 2262 if (!LexicalDC->isFunctionOrMethod()) { 2263 SmallVector<NamedDecl *, 4> ConflictingDecls; 2264 unsigned IDNS = Decl::IDNS_Ordinary; 2265 SmallVector<NamedDecl *, 2> FoundDecls; 2266 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2267 for (auto *FoundDecl : FoundDecls) { 2268 if (!FoundDecl->isInIdentifierNamespace(IDNS)) 2269 continue; 2270 2271 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) { 2272 if (IsStructuralMatch(D, FoundEnumConstant)) 2273 return Importer.Imported(D, FoundEnumConstant); 2274 } 2275 2276 ConflictingDecls.push_back(FoundDecl); 2277 } 2278 2279 if (!ConflictingDecls.empty()) { 2280 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2281 ConflictingDecls.data(), 2282 ConflictingDecls.size()); 2283 if (!Name) 2284 return nullptr; 2285 } 2286 } 2287 2288 Expr *Init = Importer.Import(D->getInitExpr()); 2289 if (D->getInitExpr() && !Init) 2290 return nullptr; 2291 2292 EnumConstantDecl *ToEnumerator 2293 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc, 2294 Name.getAsIdentifierInfo(), T, 2295 Init, D->getInitVal()); 2296 ToEnumerator->setAccess(D->getAccess()); 2297 ToEnumerator->setLexicalDeclContext(LexicalDC); 2298 Importer.Imported(D, ToEnumerator); 2299 LexicalDC->addDeclInternal(ToEnumerator); 2300 return ToEnumerator; 2301 } 2302 2303 bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD, 2304 FunctionDecl *ToFD) { 2305 switch (FromFD->getTemplatedKind()) { 2306 case FunctionDecl::TK_NonTemplate: 2307 case FunctionDecl::TK_FunctionTemplate: 2308 return false; 2309 2310 case FunctionDecl::TK_MemberSpecialization: { 2311 auto *InstFD = cast_or_null<FunctionDecl>( 2312 Importer.Import(FromFD->getInstantiatedFromMemberFunction())); 2313 if (!InstFD) 2314 return true; 2315 2316 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind(); 2317 SourceLocation POI = Importer.Import( 2318 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()); 2319 ToFD->setInstantiationOfMemberFunction(InstFD, TSK); 2320 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI); 2321 return false; 2322 } 2323 2324 case FunctionDecl::TK_FunctionTemplateSpecialization: { 2325 FunctionTemplateDecl* Template; 2326 OptionalTemplateArgsTy ToTemplArgs; 2327 std::tie(Template, ToTemplArgs) = 2328 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); 2329 if (!Template || !ToTemplArgs) 2330 return true; 2331 2332 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy( 2333 Importer.getToContext(), *ToTemplArgs); 2334 2335 auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); 2336 TemplateArgumentListInfo ToTAInfo; 2337 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten; 2338 if (FromTAArgsAsWritten) 2339 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo)) 2340 return true; 2341 2342 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation()); 2343 2344 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind(); 2345 ToFD->setFunctionTemplateSpecialization( 2346 Template, ToTAList, /* InsertPos= */ nullptr, 2347 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI); 2348 return false; 2349 } 2350 2351 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { 2352 auto *FromInfo = FromFD->getDependentSpecializationInfo(); 2353 UnresolvedSet<8> TemplDecls; 2354 unsigned NumTemplates = FromInfo->getNumTemplates(); 2355 for (unsigned I = 0; I < NumTemplates; I++) { 2356 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>( 2357 Importer.Import(FromInfo->getTemplate(I)))) 2358 TemplDecls.addDecl(ToFTD); 2359 else 2360 return true; 2361 } 2362 2363 // Import TemplateArgumentListInfo. 2364 TemplateArgumentListInfo ToTAInfo; 2365 if (ImportTemplateArgumentListInfo( 2366 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(), 2367 llvm::makeArrayRef(FromInfo->getTemplateArgs(), 2368 FromInfo->getNumTemplateArgs()), 2369 ToTAInfo)) 2370 return true; 2371 2372 ToFD->setDependentTemplateSpecialization(Importer.getToContext(), 2373 TemplDecls, ToTAInfo); 2374 return false; 2375 } 2376 } 2377 llvm_unreachable("All cases should be covered!"); 2378 } 2379 2380 FunctionDecl * 2381 ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) { 2382 FunctionTemplateDecl* Template; 2383 OptionalTemplateArgsTy ToTemplArgs; 2384 std::tie(Template, ToTemplArgs) = 2385 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); 2386 if (!Template || !ToTemplArgs) 2387 return nullptr; 2388 2389 void *InsertPos = nullptr; 2390 auto *FoundSpec = Template->findSpecialization(*ToTemplArgs, InsertPos); 2391 return FoundSpec; 2392 } 2393 2394 Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { 2395 2396 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D); 2397 auto RedeclIt = Redecls.begin(); 2398 // Import the first part of the decl chain. I.e. import all previous 2399 // declarations starting from the canonical decl. 2400 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) 2401 if (!Importer.Import(*RedeclIt)) 2402 return nullptr; 2403 assert(*RedeclIt == D); 2404 2405 // Import the major distinguishing characteristics of this function. 2406 DeclContext *DC, *LexicalDC; 2407 DeclarationName Name; 2408 SourceLocation Loc; 2409 NamedDecl *ToD; 2410 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2411 return nullptr; 2412 if (ToD) 2413 return ToD; 2414 2415 const FunctionDecl *FoundByLookup = nullptr; 2416 2417 // If this is a function template specialization, then try to find the same 2418 // existing specialization in the "to" context. The localUncachedLookup 2419 // below will not find any specialization, but would find the primary 2420 // template; thus, we have to skip normal lookup in case of specializations. 2421 // FIXME handle member function templates (TK_MemberSpecialization) similarly? 2422 if (D->getTemplatedKind() == 2423 FunctionDecl::TK_FunctionTemplateSpecialization) { 2424 if (FunctionDecl *FoundFunction = FindFunctionTemplateSpecialization(D)) { 2425 if (D->doesThisDeclarationHaveABody() && 2426 FoundFunction->hasBody()) 2427 return Importer.Imported(D, FoundFunction); 2428 FoundByLookup = FoundFunction; 2429 } 2430 } 2431 // Try to find a function in our own ("to") context with the same name, same 2432 // type, and in the same context as the function we're importing. 2433 else if (!LexicalDC->isFunctionOrMethod()) { 2434 SmallVector<NamedDecl *, 4> ConflictingDecls; 2435 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; 2436 SmallVector<NamedDecl *, 2> FoundDecls; 2437 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2438 for (auto *FoundDecl : FoundDecls) { 2439 if (!FoundDecl->isInIdentifierNamespace(IDNS)) 2440 continue; 2441 2442 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) { 2443 if (FoundFunction->hasExternalFormalLinkage() && 2444 D->hasExternalFormalLinkage()) { 2445 if (Importer.IsStructurallyEquivalent(D->getType(), 2446 FoundFunction->getType())) { 2447 if (D->doesThisDeclarationHaveABody() && 2448 FoundFunction->hasBody()) 2449 return Importer.Imported(D, FoundFunction); 2450 FoundByLookup = FoundFunction; 2451 break; 2452 } 2453 2454 // FIXME: Check for overloading more carefully, e.g., by boosting 2455 // Sema::IsOverload out to the AST library. 2456 2457 // Function overloading is okay in C++. 2458 if (Importer.getToContext().getLangOpts().CPlusPlus) 2459 continue; 2460 2461 // Complain about inconsistent function types. 2462 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) 2463 << Name << D->getType() << FoundFunction->getType(); 2464 Importer.ToDiag(FoundFunction->getLocation(), 2465 diag::note_odr_value_here) 2466 << FoundFunction->getType(); 2467 } 2468 } 2469 2470 ConflictingDecls.push_back(FoundDecl); 2471 } 2472 2473 if (!ConflictingDecls.empty()) { 2474 Name = Importer.HandleNameConflict(Name, DC, IDNS, 2475 ConflictingDecls.data(), 2476 ConflictingDecls.size()); 2477 if (!Name) 2478 return nullptr; 2479 } 2480 } 2481 2482 DeclarationNameInfo NameInfo(Name, Loc); 2483 // Import additional name location/type info. 2484 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); 2485 2486 QualType FromTy = D->getType(); 2487 bool usedDifferentExceptionSpec = false; 2488 2489 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) { 2490 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); 2491 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the 2492 // FunctionDecl that we are importing the FunctionProtoType for. 2493 // To avoid an infinite recursion when importing, create the FunctionDecl 2494 // with a simplified function type and update it afterwards. 2495 if (FromEPI.ExceptionSpec.SourceDecl || 2496 FromEPI.ExceptionSpec.SourceTemplate || 2497 FromEPI.ExceptionSpec.NoexceptExpr) { 2498 FunctionProtoType::ExtProtoInfo DefaultEPI; 2499 FromTy = Importer.getFromContext().getFunctionType( 2500 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI); 2501 usedDifferentExceptionSpec = true; 2502 } 2503 } 2504 2505 // Import the type. 2506 QualType T = Importer.Import(FromTy); 2507 if (T.isNull()) 2508 return nullptr; 2509 2510 // Import the function parameters. 2511 SmallVector<ParmVarDecl *, 8> Parameters; 2512 for (auto P : D->parameters()) { 2513 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P)); 2514 if (!ToP) 2515 return nullptr; 2516 2517 Parameters.push_back(ToP); 2518 } 2519 2520 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 2521 if (D->getTypeSourceInfo() && !TInfo) 2522 return nullptr; 2523 2524 // Create the imported function. 2525 FunctionDecl *ToFunction = nullptr; 2526 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart()); 2527 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { 2528 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(), 2529 cast<CXXRecordDecl>(DC), 2530 InnerLocStart, 2531 NameInfo, T, TInfo, 2532 FromConstructor->isExplicit(), 2533 D->isInlineSpecified(), 2534 D->isImplicit(), 2535 D->isConstexpr()); 2536 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) { 2537 SmallVector<CXXCtorInitializer *, 4> CtorInitializers; 2538 for (auto *I : FromConstructor->inits()) { 2539 auto *ToI = cast_or_null<CXXCtorInitializer>(Importer.Import(I)); 2540 if (!ToI && I) 2541 return nullptr; 2542 CtorInitializers.push_back(ToI); 2543 } 2544 auto **Memory = 2545 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers]; 2546 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory); 2547 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction); 2548 ToCtor->setCtorInitializers(Memory); 2549 ToCtor->setNumCtorInitializers(NumInitializers); 2550 } 2551 } else if (isa<CXXDestructorDecl>(D)) { 2552 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(), 2553 cast<CXXRecordDecl>(DC), 2554 InnerLocStart, 2555 NameInfo, T, TInfo, 2556 D->isInlineSpecified(), 2557 D->isImplicit()); 2558 } else if (auto *FromConversion = dyn_cast<CXXConversionDecl>(D)) { 2559 ToFunction = CXXConversionDecl::Create(Importer.getToContext(), 2560 cast<CXXRecordDecl>(DC), 2561 InnerLocStart, 2562 NameInfo, T, TInfo, 2563 D->isInlineSpecified(), 2564 FromConversion->isExplicit(), 2565 D->isConstexpr(), 2566 Importer.Import(D->getLocEnd())); 2567 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) { 2568 ToFunction = CXXMethodDecl::Create(Importer.getToContext(), 2569 cast<CXXRecordDecl>(DC), 2570 InnerLocStart, 2571 NameInfo, T, TInfo, 2572 Method->getStorageClass(), 2573 Method->isInlineSpecified(), 2574 D->isConstexpr(), 2575 Importer.Import(D->getLocEnd())); 2576 } else { 2577 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, 2578 InnerLocStart, 2579 NameInfo, T, TInfo, D->getStorageClass(), 2580 D->isInlineSpecified(), 2581 D->hasWrittenPrototype(), 2582 D->isConstexpr()); 2583 } 2584 2585 // Import the qualifier, if any. 2586 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 2587 ToFunction->setAccess(D->getAccess()); 2588 ToFunction->setLexicalDeclContext(LexicalDC); 2589 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); 2590 ToFunction->setTrivial(D->isTrivial()); 2591 ToFunction->setPure(D->isPure()); 2592 Importer.Imported(D, ToFunction); 2593 2594 // Set the parameters. 2595 for (auto *Param : Parameters) { 2596 Param->setOwningFunction(ToFunction); 2597 ToFunction->addDeclInternal(Param); 2598 } 2599 ToFunction->setParams(Parameters); 2600 2601 if (FoundByLookup) { 2602 auto *Recent = const_cast<FunctionDecl *>( 2603 FoundByLookup->getMostRecentDecl()); 2604 ToFunction->setPreviousDecl(Recent); 2605 } 2606 2607 // We need to complete creation of FunctionProtoTypeLoc manually with setting 2608 // params it refers to. 2609 if (TInfo) { 2610 if (auto ProtoLoc = 2611 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) { 2612 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) 2613 ProtoLoc.setParam(I, Parameters[I]); 2614 } 2615 } 2616 2617 if (usedDifferentExceptionSpec) { 2618 // Update FunctionProtoType::ExtProtoInfo. 2619 QualType T = Importer.Import(D->getType()); 2620 if (T.isNull()) 2621 return nullptr; 2622 ToFunction->setType(T); 2623 } 2624 2625 if (D->doesThisDeclarationHaveABody()) { 2626 if (Stmt *FromBody = D->getBody()) { 2627 if (Stmt *ToBody = Importer.Import(FromBody)) { 2628 ToFunction->setBody(ToBody); 2629 } 2630 } 2631 } 2632 2633 // FIXME: Other bits to merge? 2634 2635 // If it is a template, import all related things. 2636 if (ImportTemplateInformation(D, ToFunction)) 2637 return nullptr; 2638 2639 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend); 2640 2641 // TODO Can we generalize this approach to other AST nodes as well? 2642 if (D->getDeclContext()->containsDeclAndLoad(D)) 2643 DC->addDeclInternal(ToFunction); 2644 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D)) 2645 LexicalDC->addDeclInternal(ToFunction); 2646 2647 // Friend declaration's lexical context is the befriending class, but the 2648 // semantic context is the enclosing scope of the befriending class. 2649 // We want the friend functions to be found in the semantic context by lookup. 2650 // FIXME should we handle this generically in VisitFriendDecl? 2651 // In Other cases when LexicalDC != DC we don't want it to be added, 2652 // e.g out-of-class definitions like void B::f() {} . 2653 if (LexicalDC != DC && IsFriend) { 2654 DC->makeDeclVisibleInContext(ToFunction); 2655 } 2656 2657 // Import the rest of the chain. I.e. import all subsequent declarations. 2658 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) 2659 if (!Importer.Import(*RedeclIt)) 2660 return nullptr; 2661 2662 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D)) 2663 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod); 2664 2665 return ToFunction; 2666 } 2667 2668 Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { 2669 return VisitFunctionDecl(D); 2670 } 2671 2672 Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { 2673 return VisitCXXMethodDecl(D); 2674 } 2675 2676 Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { 2677 return VisitCXXMethodDecl(D); 2678 } 2679 2680 Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { 2681 return VisitCXXMethodDecl(D); 2682 } 2683 2684 static unsigned getFieldIndex(Decl *F) { 2685 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext()); 2686 if (!Owner) 2687 return 0; 2688 2689 unsigned Index = 1; 2690 for (const auto *D : Owner->noload_decls()) { 2691 if (D == F) 2692 return Index; 2693 2694 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) 2695 ++Index; 2696 } 2697 2698 return Index; 2699 } 2700 2701 Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { 2702 // Import the major distinguishing characteristics of a variable. 2703 DeclContext *DC, *LexicalDC; 2704 DeclarationName Name; 2705 SourceLocation Loc; 2706 NamedDecl *ToD; 2707 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2708 return nullptr; 2709 if (ToD) 2710 return ToD; 2711 2712 // Determine whether we've already imported this field. 2713 SmallVector<NamedDecl *, 2> FoundDecls; 2714 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2715 for (auto *FoundDecl : FoundDecls) { 2716 if (auto *FoundField = dyn_cast<FieldDecl>(FoundDecl)) { 2717 // For anonymous fields, match up by index. 2718 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) 2719 continue; 2720 2721 if (Importer.IsStructurallyEquivalent(D->getType(), 2722 FoundField->getType())) { 2723 Importer.Imported(D, FoundField); 2724 return FoundField; 2725 } 2726 2727 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) 2728 << Name << D->getType() << FoundField->getType(); 2729 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) 2730 << FoundField->getType(); 2731 return nullptr; 2732 } 2733 } 2734 2735 // Import the type. 2736 QualType T = Importer.Import(D->getType()); 2737 if (T.isNull()) 2738 return nullptr; 2739 2740 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 2741 Expr *BitWidth = Importer.Import(D->getBitWidth()); 2742 if (!BitWidth && D->getBitWidth()) 2743 return nullptr; 2744 2745 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC, 2746 Importer.Import(D->getInnerLocStart()), 2747 Loc, Name.getAsIdentifierInfo(), 2748 T, TInfo, BitWidth, D->isMutable(), 2749 D->getInClassInitStyle()); 2750 ToField->setAccess(D->getAccess()); 2751 ToField->setLexicalDeclContext(LexicalDC); 2752 if (Expr *FromInitializer = D->getInClassInitializer()) { 2753 Expr *ToInitializer = Importer.Import(FromInitializer); 2754 if (ToInitializer) 2755 ToField->setInClassInitializer(ToInitializer); 2756 else 2757 return nullptr; 2758 } 2759 ToField->setImplicit(D->isImplicit()); 2760 Importer.Imported(D, ToField); 2761 LexicalDC->addDeclInternal(ToField); 2762 return ToField; 2763 } 2764 2765 Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { 2766 // Import the major distinguishing characteristics of a variable. 2767 DeclContext *DC, *LexicalDC; 2768 DeclarationName Name; 2769 SourceLocation Loc; 2770 NamedDecl *ToD; 2771 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2772 return nullptr; 2773 if (ToD) 2774 return ToD; 2775 2776 // Determine whether we've already imported this field. 2777 SmallVector<NamedDecl *, 2> FoundDecls; 2778 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2779 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { 2780 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) { 2781 // For anonymous indirect fields, match up by index. 2782 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) 2783 continue; 2784 2785 if (Importer.IsStructurallyEquivalent(D->getType(), 2786 FoundField->getType(), 2787 !Name.isEmpty())) { 2788 Importer.Imported(D, FoundField); 2789 return FoundField; 2790 } 2791 2792 // If there are more anonymous fields to check, continue. 2793 if (!Name && I < N-1) 2794 continue; 2795 2796 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) 2797 << Name << D->getType() << FoundField->getType(); 2798 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) 2799 << FoundField->getType(); 2800 return nullptr; 2801 } 2802 } 2803 2804 // Import the type. 2805 QualType T = Importer.Import(D->getType()); 2806 if (T.isNull()) 2807 return nullptr; 2808 2809 auto **NamedChain = 2810 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()]; 2811 2812 unsigned i = 0; 2813 for (auto *PI : D->chain()) { 2814 Decl *D = Importer.Import(PI); 2815 if (!D) 2816 return nullptr; 2817 NamedChain[i++] = cast<NamedDecl>(D); 2818 } 2819 2820 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create( 2821 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T, 2822 {NamedChain, D->getChainingSize()}); 2823 2824 for (const auto *A : D->attrs()) 2825 ToIndirectField->addAttr(Importer.Import(A)); 2826 2827 ToIndirectField->setAccess(D->getAccess()); 2828 ToIndirectField->setLexicalDeclContext(LexicalDC); 2829 Importer.Imported(D, ToIndirectField); 2830 LexicalDC->addDeclInternal(ToIndirectField); 2831 return ToIndirectField; 2832 } 2833 2834 Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) { 2835 // Import the major distinguishing characteristics of a declaration. 2836 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 2837 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext() 2838 ? DC : Importer.ImportContext(D->getLexicalDeclContext()); 2839 if (!DC || !LexicalDC) 2840 return nullptr; 2841 2842 // Determine whether we've already imported this decl. 2843 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup. 2844 auto *RD = cast<CXXRecordDecl>(DC); 2845 FriendDecl *ImportedFriend = RD->getFirstFriend(); 2846 StructuralEquivalenceContext Context( 2847 Importer.getFromContext(), Importer.getToContext(), 2848 Importer.getNonEquivalentDecls(), false, false); 2849 2850 while (ImportedFriend) { 2851 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) { 2852 if (Context.IsStructurallyEquivalent(D->getFriendDecl(), 2853 ImportedFriend->getFriendDecl())) 2854 return Importer.Imported(D, ImportedFriend); 2855 2856 } else if (D->getFriendType() && ImportedFriend->getFriendType()) { 2857 if (Importer.IsStructurallyEquivalent( 2858 D->getFriendType()->getType(), 2859 ImportedFriend->getFriendType()->getType(), true)) 2860 return Importer.Imported(D, ImportedFriend); 2861 } 2862 ImportedFriend = ImportedFriend->getNextFriend(); 2863 } 2864 2865 // Not found. Create it. 2866 FriendDecl::FriendUnion ToFU; 2867 if (NamedDecl *FriendD = D->getFriendDecl()) { 2868 auto *ToFriendD = cast_or_null<NamedDecl>(Importer.Import(FriendD)); 2869 if (ToFriendD && FriendD->getFriendObjectKind() != Decl::FOK_None && 2870 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator))) 2871 ToFriendD->setObjectOfFriendDecl(false); 2872 2873 ToFU = ToFriendD; 2874 } else // The friend is a type, not a decl. 2875 ToFU = Importer.Import(D->getFriendType()); 2876 if (!ToFU) 2877 return nullptr; 2878 2879 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists); 2880 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>(); 2881 for (unsigned I = 0; I < D->NumTPLists; I++) { 2882 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]); 2883 if (!List) 2884 return nullptr; 2885 ToTPLists[I] = List; 2886 } 2887 2888 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC, 2889 Importer.Import(D->getLocation()), 2890 ToFU, Importer.Import(D->getFriendLoc()), 2891 ToTPLists); 2892 2893 Importer.Imported(D, FrD); 2894 2895 FrD->setAccess(D->getAccess()); 2896 FrD->setLexicalDeclContext(LexicalDC); 2897 LexicalDC->addDeclInternal(FrD); 2898 return FrD; 2899 } 2900 2901 Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { 2902 // Import the major distinguishing characteristics of an ivar. 2903 DeclContext *DC, *LexicalDC; 2904 DeclarationName Name; 2905 SourceLocation Loc; 2906 NamedDecl *ToD; 2907 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2908 return nullptr; 2909 if (ToD) 2910 return ToD; 2911 2912 // Determine whether we've already imported this ivar 2913 SmallVector<NamedDecl *, 2> FoundDecls; 2914 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2915 for (auto *FoundDecl : FoundDecls) { 2916 if (auto *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) { 2917 if (Importer.IsStructurallyEquivalent(D->getType(), 2918 FoundIvar->getType())) { 2919 Importer.Imported(D, FoundIvar); 2920 return FoundIvar; 2921 } 2922 2923 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent) 2924 << Name << D->getType() << FoundIvar->getType(); 2925 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) 2926 << FoundIvar->getType(); 2927 return nullptr; 2928 } 2929 } 2930 2931 // Import the type. 2932 QualType T = Importer.Import(D->getType()); 2933 if (T.isNull()) 2934 return nullptr; 2935 2936 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 2937 Expr *BitWidth = Importer.Import(D->getBitWidth()); 2938 if (!BitWidth && D->getBitWidth()) 2939 return nullptr; 2940 2941 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), 2942 cast<ObjCContainerDecl>(DC), 2943 Importer.Import(D->getInnerLocStart()), 2944 Loc, Name.getAsIdentifierInfo(), 2945 T, TInfo, D->getAccessControl(), 2946 BitWidth, D->getSynthesize()); 2947 ToIvar->setLexicalDeclContext(LexicalDC); 2948 Importer.Imported(D, ToIvar); 2949 LexicalDC->addDeclInternal(ToIvar); 2950 return ToIvar; 2951 } 2952 2953 Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { 2954 // Import the major distinguishing characteristics of a variable. 2955 DeclContext *DC, *LexicalDC; 2956 DeclarationName Name; 2957 SourceLocation Loc; 2958 NamedDecl *ToD; 2959 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 2960 return nullptr; 2961 if (ToD) 2962 return ToD; 2963 2964 // Try to find a variable in our own ("to") context with the same name and 2965 // in the same context as the variable we're importing. 2966 if (D->isFileVarDecl()) { 2967 VarDecl *MergeWithVar = nullptr; 2968 SmallVector<NamedDecl *, 4> ConflictingDecls; 2969 unsigned IDNS = Decl::IDNS_Ordinary; 2970 SmallVector<NamedDecl *, 2> FoundDecls; 2971 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 2972 for (auto *FoundDecl : FoundDecls) { 2973 if (!FoundDecl->isInIdentifierNamespace(IDNS)) 2974 continue; 2975 2976 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) { 2977 // We have found a variable that we may need to merge with. Check it. 2978 if (FoundVar->hasExternalFormalLinkage() && 2979 D->hasExternalFormalLinkage()) { 2980 if (Importer.IsStructurallyEquivalent(D->getType(), 2981 FoundVar->getType())) { 2982 MergeWithVar = FoundVar; 2983 break; 2984 } 2985 2986 const ArrayType *FoundArray 2987 = Importer.getToContext().getAsArrayType(FoundVar->getType()); 2988 const ArrayType *TArray 2989 = Importer.getToContext().getAsArrayType(D->getType()); 2990 if (FoundArray && TArray) { 2991 if (isa<IncompleteArrayType>(FoundArray) && 2992 isa<ConstantArrayType>(TArray)) { 2993 // Import the type. 2994 QualType T = Importer.Import(D->getType()); 2995 if (T.isNull()) 2996 return nullptr; 2997 2998 FoundVar->setType(T); 2999 MergeWithVar = FoundVar; 3000 break; 3001 } else if (isa<IncompleteArrayType>(TArray) && 3002 isa<ConstantArrayType>(FoundArray)) { 3003 MergeWithVar = FoundVar; 3004 break; 3005 } 3006 } 3007 3008 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) 3009 << Name << D->getType() << FoundVar->getType(); 3010 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) 3011 << FoundVar->getType(); 3012 } 3013 } 3014 3015 ConflictingDecls.push_back(FoundDecl); 3016 } 3017 3018 if (MergeWithVar) { 3019 // An equivalent variable with external linkage has been found. Link 3020 // the two declarations, then merge them. 3021 Importer.Imported(D, MergeWithVar); 3022 3023 if (VarDecl *DDef = D->getDefinition()) { 3024 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { 3025 Importer.ToDiag(ExistingDef->getLocation(), 3026 diag::err_odr_variable_multiple_def) 3027 << Name; 3028 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); 3029 } else { 3030 Expr *Init = Importer.Import(DDef->getInit()); 3031 MergeWithVar->setInit(Init); 3032 if (DDef->isInitKnownICE()) { 3033 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt(); 3034 Eval->CheckedICE = true; 3035 Eval->IsICE = DDef->isInitICE(); 3036 } 3037 } 3038 } 3039 3040 return MergeWithVar; 3041 } 3042 3043 if (!ConflictingDecls.empty()) { 3044 Name = Importer.HandleNameConflict(Name, DC, IDNS, 3045 ConflictingDecls.data(), 3046 ConflictingDecls.size()); 3047 if (!Name) 3048 return nullptr; 3049 } 3050 } 3051 3052 // Import the type. 3053 QualType T = Importer.Import(D->getType()); 3054 if (T.isNull()) 3055 return nullptr; 3056 3057 // Create the imported variable. 3058 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3059 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, 3060 Importer.Import(D->getInnerLocStart()), 3061 Loc, Name.getAsIdentifierInfo(), 3062 T, TInfo, 3063 D->getStorageClass()); 3064 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 3065 ToVar->setAccess(D->getAccess()); 3066 ToVar->setLexicalDeclContext(LexicalDC); 3067 Importer.Imported(D, ToVar); 3068 3069 // Templated declarations should never appear in the enclosing DeclContext. 3070 if (!D->getDescribedVarTemplate()) 3071 LexicalDC->addDeclInternal(ToVar); 3072 3073 if (!D->isFileVarDecl() && 3074 D->isUsed()) 3075 ToVar->setIsUsed(); 3076 3077 // Merge the initializer. 3078 if (ImportDefinition(D, ToVar)) 3079 return nullptr; 3080 3081 if (D->isConstexpr()) 3082 ToVar->setConstexpr(true); 3083 3084 return ToVar; 3085 } 3086 3087 Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { 3088 // Parameters are created in the translation unit's context, then moved 3089 // into the function declaration's context afterward. 3090 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); 3091 3092 // Import the name of this declaration. 3093 DeclarationName Name = Importer.Import(D->getDeclName()); 3094 if (D->getDeclName() && !Name) 3095 return nullptr; 3096 3097 // Import the location of this declaration. 3098 SourceLocation Loc = Importer.Import(D->getLocation()); 3099 3100 // Import the parameter's type. 3101 QualType T = Importer.Import(D->getType()); 3102 if (T.isNull()) 3103 return nullptr; 3104 3105 // Create the imported parameter. 3106 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc, 3107 Name.getAsIdentifierInfo(), T, 3108 D->getParameterKind()); 3109 return Importer.Imported(D, ToParm); 3110 } 3111 3112 Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { 3113 // Parameters are created in the translation unit's context, then moved 3114 // into the function declaration's context afterward. 3115 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); 3116 3117 // Import the name of this declaration. 3118 DeclarationName Name = Importer.Import(D->getDeclName()); 3119 if (D->getDeclName() && !Name) 3120 return nullptr; 3121 3122 // Import the location of this declaration. 3123 SourceLocation Loc = Importer.Import(D->getLocation()); 3124 3125 // Import the parameter's type. 3126 QualType T = Importer.Import(D->getType()); 3127 if (T.isNull()) 3128 return nullptr; 3129 3130 // Create the imported parameter. 3131 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 3132 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC, 3133 Importer.Import(D->getInnerLocStart()), 3134 Loc, Name.getAsIdentifierInfo(), 3135 T, TInfo, D->getStorageClass(), 3136 /*DefaultArg*/ nullptr); 3137 3138 // Set the default argument. 3139 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg()); 3140 ToParm->setKNRPromoted(D->isKNRPromoted()); 3141 3142 Expr *ToDefArg = nullptr; 3143 Expr *FromDefArg = nullptr; 3144 if (D->hasUninstantiatedDefaultArg()) { 3145 FromDefArg = D->getUninstantiatedDefaultArg(); 3146 ToDefArg = Importer.Import(FromDefArg); 3147 ToParm->setUninstantiatedDefaultArg(ToDefArg); 3148 } else if (D->hasUnparsedDefaultArg()) { 3149 ToParm->setUnparsedDefaultArg(); 3150 } else if (D->hasDefaultArg()) { 3151 FromDefArg = D->getDefaultArg(); 3152 ToDefArg = Importer.Import(FromDefArg); 3153 ToParm->setDefaultArg(ToDefArg); 3154 } 3155 if (FromDefArg && !ToDefArg) 3156 return nullptr; 3157 3158 if (D->isObjCMethodParameter()) { 3159 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex()); 3160 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier()); 3161 } else { 3162 ToParm->setScopeInfo(D->getFunctionScopeDepth(), 3163 D->getFunctionScopeIndex()); 3164 } 3165 3166 if (D->isUsed()) 3167 ToParm->setIsUsed(); 3168 3169 return Importer.Imported(D, ToParm); 3170 } 3171 3172 Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { 3173 // Import the major distinguishing characteristics of a method. 3174 DeclContext *DC, *LexicalDC; 3175 DeclarationName Name; 3176 SourceLocation Loc; 3177 NamedDecl *ToD; 3178 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3179 return nullptr; 3180 if (ToD) 3181 return ToD; 3182 3183 SmallVector<NamedDecl *, 2> FoundDecls; 3184 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3185 for (auto *FoundDecl : FoundDecls) { 3186 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) { 3187 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) 3188 continue; 3189 3190 // Check return types. 3191 if (!Importer.IsStructurallyEquivalent(D->getReturnType(), 3192 FoundMethod->getReturnType())) { 3193 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent) 3194 << D->isInstanceMethod() << Name << D->getReturnType() 3195 << FoundMethod->getReturnType(); 3196 Importer.ToDiag(FoundMethod->getLocation(), 3197 diag::note_odr_objc_method_here) 3198 << D->isInstanceMethod() << Name; 3199 return nullptr; 3200 } 3201 3202 // Check the number of parameters. 3203 if (D->param_size() != FoundMethod->param_size()) { 3204 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent) 3205 << D->isInstanceMethod() << Name 3206 << D->param_size() << FoundMethod->param_size(); 3207 Importer.ToDiag(FoundMethod->getLocation(), 3208 diag::note_odr_objc_method_here) 3209 << D->isInstanceMethod() << Name; 3210 return nullptr; 3211 } 3212 3213 // Check parameter types. 3214 for (ObjCMethodDecl::param_iterator P = D->param_begin(), 3215 PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); 3216 P != PEnd; ++P, ++FoundP) { 3217 if (!Importer.IsStructurallyEquivalent((*P)->getType(), 3218 (*FoundP)->getType())) { 3219 Importer.FromDiag((*P)->getLocation(), 3220 diag::err_odr_objc_method_param_type_inconsistent) 3221 << D->isInstanceMethod() << Name 3222 << (*P)->getType() << (*FoundP)->getType(); 3223 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) 3224 << (*FoundP)->getType(); 3225 return nullptr; 3226 } 3227 } 3228 3229 // Check variadic/non-variadic. 3230 // Check the number of parameters. 3231 if (D->isVariadic() != FoundMethod->isVariadic()) { 3232 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent) 3233 << D->isInstanceMethod() << Name; 3234 Importer.ToDiag(FoundMethod->getLocation(), 3235 diag::note_odr_objc_method_here) 3236 << D->isInstanceMethod() << Name; 3237 return nullptr; 3238 } 3239 3240 // FIXME: Any other bits we need to merge? 3241 return Importer.Imported(D, FoundMethod); 3242 } 3243 } 3244 3245 // Import the result type. 3246 QualType ResultTy = Importer.Import(D->getReturnType()); 3247 if (ResultTy.isNull()) 3248 return nullptr; 3249 3250 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo()); 3251 3252 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create( 3253 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()), 3254 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(), 3255 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(), 3256 D->getImplementationControl(), D->hasRelatedResultType()); 3257 3258 // FIXME: When we decide to merge method definitions, we'll need to 3259 // deal with implicit parameters. 3260 3261 // Import the parameters 3262 SmallVector<ParmVarDecl *, 5> ToParams; 3263 for (auto *FromP : D->parameters()) { 3264 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP)); 3265 if (!ToP) 3266 return nullptr; 3267 3268 ToParams.push_back(ToP); 3269 } 3270 3271 // Set the parameters. 3272 for (auto *ToParam : ToParams) { 3273 ToParam->setOwningFunction(ToMethod); 3274 ToMethod->addDeclInternal(ToParam); 3275 } 3276 3277 SmallVector<SourceLocation, 12> SelLocs; 3278 D->getSelectorLocs(SelLocs); 3279 for (auto &Loc : SelLocs) 3280 Loc = Importer.Import(Loc); 3281 3282 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs); 3283 3284 ToMethod->setLexicalDeclContext(LexicalDC); 3285 Importer.Imported(D, ToMethod); 3286 LexicalDC->addDeclInternal(ToMethod); 3287 return ToMethod; 3288 } 3289 3290 Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { 3291 // Import the major distinguishing characteristics of a category. 3292 DeclContext *DC, *LexicalDC; 3293 DeclarationName Name; 3294 SourceLocation Loc; 3295 NamedDecl *ToD; 3296 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3297 return nullptr; 3298 if (ToD) 3299 return ToD; 3300 3301 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo()); 3302 if (!BoundInfo) 3303 return nullptr; 3304 3305 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create( 3306 Importer.getToContext(), DC, 3307 D->getVariance(), 3308 Importer.Import(D->getVarianceLoc()), 3309 D->getIndex(), 3310 Importer.Import(D->getLocation()), 3311 Name.getAsIdentifierInfo(), 3312 Importer.Import(D->getColonLoc()), 3313 BoundInfo); 3314 Importer.Imported(D, Result); 3315 Result->setLexicalDeclContext(LexicalDC); 3316 return Result; 3317 } 3318 3319 Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { 3320 // Import the major distinguishing characteristics of a category. 3321 DeclContext *DC, *LexicalDC; 3322 DeclarationName Name; 3323 SourceLocation Loc; 3324 NamedDecl *ToD; 3325 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3326 return nullptr; 3327 if (ToD) 3328 return ToD; 3329 3330 auto *ToInterface = 3331 cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface())); 3332 if (!ToInterface) 3333 return nullptr; 3334 3335 // Determine if we've already encountered this category. 3336 ObjCCategoryDecl *MergeWithCategory 3337 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); 3338 ObjCCategoryDecl *ToCategory = MergeWithCategory; 3339 if (!ToCategory) { 3340 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC, 3341 Importer.Import(D->getAtStartLoc()), 3342 Loc, 3343 Importer.Import(D->getCategoryNameLoc()), 3344 Name.getAsIdentifierInfo(), 3345 ToInterface, 3346 /*TypeParamList=*/nullptr, 3347 Importer.Import(D->getIvarLBraceLoc()), 3348 Importer.Import(D->getIvarRBraceLoc())); 3349 ToCategory->setLexicalDeclContext(LexicalDC); 3350 LexicalDC->addDeclInternal(ToCategory); 3351 Importer.Imported(D, ToCategory); 3352 // Import the type parameter list after calling Imported, to avoid 3353 // loops when bringing in their DeclContext. 3354 ToCategory->setTypeParamList(ImportObjCTypeParamList( 3355 D->getTypeParamList())); 3356 3357 // Import protocols 3358 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3359 SmallVector<SourceLocation, 4> ProtocolLocs; 3360 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc 3361 = D->protocol_loc_begin(); 3362 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), 3363 FromProtoEnd = D->protocol_end(); 3364 FromProto != FromProtoEnd; 3365 ++FromProto, ++FromProtoLoc) { 3366 auto *ToProto = 3367 cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3368 if (!ToProto) 3369 return nullptr; 3370 Protocols.push_back(ToProto); 3371 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3372 } 3373 3374 // FIXME: If we're merging, make sure that the protocol list is the same. 3375 ToCategory->setProtocolList(Protocols.data(), Protocols.size(), 3376 ProtocolLocs.data(), Importer.getToContext()); 3377 } else { 3378 Importer.Imported(D, ToCategory); 3379 } 3380 3381 // Import all of the members of this category. 3382 ImportDeclContext(D); 3383 3384 // If we have an implementation, import it as well. 3385 if (D->getImplementation()) { 3386 auto *Impl = 3387 cast_or_null<ObjCCategoryImplDecl>( 3388 Importer.Import(D->getImplementation())); 3389 if (!Impl) 3390 return nullptr; 3391 3392 ToCategory->setImplementation(Impl); 3393 } 3394 3395 return ToCategory; 3396 } 3397 3398 bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From, 3399 ObjCProtocolDecl *To, 3400 ImportDefinitionKind Kind) { 3401 if (To->getDefinition()) { 3402 if (shouldForceImportDeclContext(Kind)) 3403 ImportDeclContext(From); 3404 return false; 3405 } 3406 3407 // Start the protocol definition 3408 To->startDefinition(); 3409 3410 // Import protocols 3411 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3412 SmallVector<SourceLocation, 4> ProtocolLocs; 3413 ObjCProtocolDecl::protocol_loc_iterator 3414 FromProtoLoc = From->protocol_loc_begin(); 3415 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), 3416 FromProtoEnd = From->protocol_end(); 3417 FromProto != FromProtoEnd; 3418 ++FromProto, ++FromProtoLoc) { 3419 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3420 if (!ToProto) 3421 return true; 3422 Protocols.push_back(ToProto); 3423 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3424 } 3425 3426 // FIXME: If we're merging, make sure that the protocol list is the same. 3427 To->setProtocolList(Protocols.data(), Protocols.size(), 3428 ProtocolLocs.data(), Importer.getToContext()); 3429 3430 if (shouldForceImportDeclContext(Kind)) { 3431 // Import all of the members of this protocol. 3432 ImportDeclContext(From, /*ForceImport=*/true); 3433 } 3434 return false; 3435 } 3436 3437 Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { 3438 // If this protocol has a definition in the translation unit we're coming 3439 // from, but this particular declaration is not that definition, import the 3440 // definition and map to that. 3441 ObjCProtocolDecl *Definition = D->getDefinition(); 3442 if (Definition && Definition != D) { 3443 Decl *ImportedDef = Importer.Import(Definition); 3444 if (!ImportedDef) 3445 return nullptr; 3446 3447 return Importer.Imported(D, ImportedDef); 3448 } 3449 3450 // Import the major distinguishing characteristics of a protocol. 3451 DeclContext *DC, *LexicalDC; 3452 DeclarationName Name; 3453 SourceLocation Loc; 3454 NamedDecl *ToD; 3455 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3456 return nullptr; 3457 if (ToD) 3458 return ToD; 3459 3460 ObjCProtocolDecl *MergeWithProtocol = nullptr; 3461 SmallVector<NamedDecl *, 2> FoundDecls; 3462 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3463 for (auto *FoundDecl : FoundDecls) { 3464 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) 3465 continue; 3466 3467 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl))) 3468 break; 3469 } 3470 3471 ObjCProtocolDecl *ToProto = MergeWithProtocol; 3472 if (!ToProto) { 3473 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, 3474 Name.getAsIdentifierInfo(), Loc, 3475 Importer.Import(D->getAtStartLoc()), 3476 /*PrevDecl=*/nullptr); 3477 ToProto->setLexicalDeclContext(LexicalDC); 3478 LexicalDC->addDeclInternal(ToProto); 3479 } 3480 3481 Importer.Imported(D, ToProto); 3482 3483 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto)) 3484 return nullptr; 3485 3486 return ToProto; 3487 } 3488 3489 Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 3490 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 3491 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 3492 3493 SourceLocation ExternLoc = Importer.Import(D->getExternLoc()); 3494 SourceLocation LangLoc = Importer.Import(D->getLocation()); 3495 3496 bool HasBraces = D->hasBraces(); 3497 3498 LinkageSpecDecl *ToLinkageSpec = 3499 LinkageSpecDecl::Create(Importer.getToContext(), 3500 DC, 3501 ExternLoc, 3502 LangLoc, 3503 D->getLanguage(), 3504 HasBraces); 3505 3506 if (HasBraces) { 3507 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc()); 3508 ToLinkageSpec->setRBraceLoc(RBraceLoc); 3509 } 3510 3511 ToLinkageSpec->setLexicalDeclContext(LexicalDC); 3512 LexicalDC->addDeclInternal(ToLinkageSpec); 3513 3514 Importer.Imported(D, ToLinkageSpec); 3515 3516 return ToLinkageSpec; 3517 } 3518 3519 Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) { 3520 DeclContext *DC, *LexicalDC; 3521 DeclarationName Name; 3522 SourceLocation Loc; 3523 NamedDecl *ToD = nullptr; 3524 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3525 return nullptr; 3526 if (ToD) 3527 return ToD; 3528 3529 DeclarationNameInfo NameInfo(Name, 3530 Importer.Import(D->getNameInfo().getLoc())); 3531 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); 3532 3533 UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC, 3534 Importer.Import(D->getUsingLoc()), 3535 Importer.Import(D->getQualifierLoc()), 3536 NameInfo, D->hasTypename()); 3537 ToUsing->setLexicalDeclContext(LexicalDC); 3538 LexicalDC->addDeclInternal(ToUsing); 3539 Importer.Imported(D, ToUsing); 3540 3541 if (NamedDecl *FromPattern = 3542 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) { 3543 if (auto *ToPattern = 3544 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern))) 3545 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern); 3546 else 3547 return nullptr; 3548 } 3549 3550 for (auto *FromShadow : D->shadows()) { 3551 if (auto *ToShadow = 3552 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow))) 3553 ToUsing->addShadowDecl(ToShadow); 3554 else 3555 // FIXME: We return a nullptr here but the definition is already created 3556 // and available with lookups. How to fix this?.. 3557 return nullptr; 3558 } 3559 return ToUsing; 3560 } 3561 3562 Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) { 3563 DeclContext *DC, *LexicalDC; 3564 DeclarationName Name; 3565 SourceLocation Loc; 3566 NamedDecl *ToD = nullptr; 3567 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3568 return nullptr; 3569 if (ToD) 3570 return ToD; 3571 3572 auto *ToUsing = dyn_cast_or_null<UsingDecl>( 3573 Importer.Import(D->getUsingDecl())); 3574 if (!ToUsing) 3575 return nullptr; 3576 3577 auto *ToTarget = dyn_cast_or_null<NamedDecl>( 3578 Importer.Import(D->getTargetDecl())); 3579 if (!ToTarget) 3580 return nullptr; 3581 3582 UsingShadowDecl *ToShadow = UsingShadowDecl::Create( 3583 Importer.getToContext(), DC, Loc, ToUsing, ToTarget); 3584 3585 ToShadow->setLexicalDeclContext(LexicalDC); 3586 ToShadow->setAccess(D->getAccess()); 3587 Importer.Imported(D, ToShadow); 3588 3589 if (UsingShadowDecl *FromPattern = 3590 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) { 3591 if (auto *ToPattern = 3592 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern))) 3593 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow, 3594 ToPattern); 3595 else 3596 // FIXME: We return a nullptr here but the definition is already created 3597 // and available with lookups. How to fix this?.. 3598 return nullptr; 3599 } 3600 3601 LexicalDC->addDeclInternal(ToShadow); 3602 3603 return ToShadow; 3604 } 3605 3606 Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 3607 DeclContext *DC, *LexicalDC; 3608 DeclarationName Name; 3609 SourceLocation Loc; 3610 NamedDecl *ToD = nullptr; 3611 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3612 return nullptr; 3613 if (ToD) 3614 return ToD; 3615 3616 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor()); 3617 if (!ToComAncestor) 3618 return nullptr; 3619 3620 auto *ToNominated = cast_or_null<NamespaceDecl>( 3621 Importer.Import(D->getNominatedNamespace())); 3622 if (!ToNominated) 3623 return nullptr; 3624 3625 UsingDirectiveDecl *ToUsingDir = UsingDirectiveDecl::Create( 3626 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()), 3627 Importer.Import(D->getNamespaceKeyLocation()), 3628 Importer.Import(D->getQualifierLoc()), 3629 Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor); 3630 ToUsingDir->setLexicalDeclContext(LexicalDC); 3631 LexicalDC->addDeclInternal(ToUsingDir); 3632 Importer.Imported(D, ToUsingDir); 3633 3634 return ToUsingDir; 3635 } 3636 3637 Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl( 3638 UnresolvedUsingValueDecl *D) { 3639 DeclContext *DC, *LexicalDC; 3640 DeclarationName Name; 3641 SourceLocation Loc; 3642 NamedDecl *ToD = nullptr; 3643 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3644 return nullptr; 3645 if (ToD) 3646 return ToD; 3647 3648 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc())); 3649 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); 3650 3651 UnresolvedUsingValueDecl *ToUsingValue = UnresolvedUsingValueDecl::Create( 3652 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()), 3653 Importer.Import(D->getQualifierLoc()), NameInfo, 3654 Importer.Import(D->getEllipsisLoc())); 3655 3656 Importer.Imported(D, ToUsingValue); 3657 ToUsingValue->setAccess(D->getAccess()); 3658 ToUsingValue->setLexicalDeclContext(LexicalDC); 3659 LexicalDC->addDeclInternal(ToUsingValue); 3660 3661 return ToUsingValue; 3662 } 3663 3664 Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl( 3665 UnresolvedUsingTypenameDecl *D) { 3666 DeclContext *DC, *LexicalDC; 3667 DeclarationName Name; 3668 SourceLocation Loc; 3669 NamedDecl *ToD = nullptr; 3670 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3671 return nullptr; 3672 if (ToD) 3673 return ToD; 3674 3675 UnresolvedUsingTypenameDecl *ToUsing = UnresolvedUsingTypenameDecl::Create( 3676 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()), 3677 Importer.Import(D->getTypenameLoc()), 3678 Importer.Import(D->getQualifierLoc()), Loc, Name, 3679 Importer.Import(D->getEllipsisLoc())); 3680 3681 Importer.Imported(D, ToUsing); 3682 ToUsing->setAccess(D->getAccess()); 3683 ToUsing->setLexicalDeclContext(LexicalDC); 3684 LexicalDC->addDeclInternal(ToUsing); 3685 3686 return ToUsing; 3687 } 3688 3689 bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From, 3690 ObjCInterfaceDecl *To, 3691 ImportDefinitionKind Kind) { 3692 if (To->getDefinition()) { 3693 // Check consistency of superclass. 3694 ObjCInterfaceDecl *FromSuper = From->getSuperClass(); 3695 if (FromSuper) { 3696 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper)); 3697 if (!FromSuper) 3698 return true; 3699 } 3700 3701 ObjCInterfaceDecl *ToSuper = To->getSuperClass(); 3702 if ((bool)FromSuper != (bool)ToSuper || 3703 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { 3704 Importer.ToDiag(To->getLocation(), 3705 diag::err_odr_objc_superclass_inconsistent) 3706 << To->getDeclName(); 3707 if (ToSuper) 3708 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) 3709 << To->getSuperClass()->getDeclName(); 3710 else 3711 Importer.ToDiag(To->getLocation(), 3712 diag::note_odr_objc_missing_superclass); 3713 if (From->getSuperClass()) 3714 Importer.FromDiag(From->getSuperClassLoc(), 3715 diag::note_odr_objc_superclass) 3716 << From->getSuperClass()->getDeclName(); 3717 else 3718 Importer.FromDiag(From->getLocation(), 3719 diag::note_odr_objc_missing_superclass); 3720 } 3721 3722 if (shouldForceImportDeclContext(Kind)) 3723 ImportDeclContext(From); 3724 return false; 3725 } 3726 3727 // Start the definition. 3728 To->startDefinition(); 3729 3730 // If this class has a superclass, import it. 3731 if (From->getSuperClass()) { 3732 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo()); 3733 if (!SuperTInfo) 3734 return true; 3735 3736 To->setSuperClass(SuperTInfo); 3737 } 3738 3739 // Import protocols 3740 SmallVector<ObjCProtocolDecl *, 4> Protocols; 3741 SmallVector<SourceLocation, 4> ProtocolLocs; 3742 ObjCInterfaceDecl::protocol_loc_iterator 3743 FromProtoLoc = From->protocol_loc_begin(); 3744 3745 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), 3746 FromProtoEnd = From->protocol_end(); 3747 FromProto != FromProtoEnd; 3748 ++FromProto, ++FromProtoLoc) { 3749 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); 3750 if (!ToProto) 3751 return true; 3752 Protocols.push_back(ToProto); 3753 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); 3754 } 3755 3756 // FIXME: If we're merging, make sure that the protocol list is the same. 3757 To->setProtocolList(Protocols.data(), Protocols.size(), 3758 ProtocolLocs.data(), Importer.getToContext()); 3759 3760 // Import categories. When the categories themselves are imported, they'll 3761 // hook themselves into this interface. 3762 for (auto *Cat : From->known_categories()) 3763 Importer.Import(Cat); 3764 3765 // If we have an @implementation, import it as well. 3766 if (From->getImplementation()) { 3767 auto *Impl = cast_or_null<ObjCImplementationDecl>( 3768 Importer.Import(From->getImplementation())); 3769 if (!Impl) 3770 return true; 3771 3772 To->setImplementation(Impl); 3773 } 3774 3775 if (shouldForceImportDeclContext(Kind)) { 3776 // Import all of the members of this class. 3777 ImportDeclContext(From, /*ForceImport=*/true); 3778 } 3779 return false; 3780 } 3781 3782 ObjCTypeParamList * 3783 ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { 3784 if (!list) 3785 return nullptr; 3786 3787 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams; 3788 for (auto fromTypeParam : *list) { 3789 auto *toTypeParam = cast_or_null<ObjCTypeParamDecl>( 3790 Importer.Import(fromTypeParam)); 3791 if (!toTypeParam) 3792 return nullptr; 3793 3794 toTypeParams.push_back(toTypeParam); 3795 } 3796 3797 return ObjCTypeParamList::create(Importer.getToContext(), 3798 Importer.Import(list->getLAngleLoc()), 3799 toTypeParams, 3800 Importer.Import(list->getRAngleLoc())); 3801 } 3802 3803 Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { 3804 // If this class has a definition in the translation unit we're coming from, 3805 // but this particular declaration is not that definition, import the 3806 // definition and map to that. 3807 ObjCInterfaceDecl *Definition = D->getDefinition(); 3808 if (Definition && Definition != D) { 3809 Decl *ImportedDef = Importer.Import(Definition); 3810 if (!ImportedDef) 3811 return nullptr; 3812 3813 return Importer.Imported(D, ImportedDef); 3814 } 3815 3816 // Import the major distinguishing characteristics of an @interface. 3817 DeclContext *DC, *LexicalDC; 3818 DeclarationName Name; 3819 SourceLocation Loc; 3820 NamedDecl *ToD; 3821 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3822 return nullptr; 3823 if (ToD) 3824 return ToD; 3825 3826 // Look for an existing interface with the same name. 3827 ObjCInterfaceDecl *MergeWithIface = nullptr; 3828 SmallVector<NamedDecl *, 2> FoundDecls; 3829 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3830 for (auto *FoundDecl : FoundDecls) { 3831 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 3832 continue; 3833 3834 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl))) 3835 break; 3836 } 3837 3838 // Create an interface declaration, if one does not already exist. 3839 ObjCInterfaceDecl *ToIface = MergeWithIface; 3840 if (!ToIface) { 3841 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC, 3842 Importer.Import(D->getAtStartLoc()), 3843 Name.getAsIdentifierInfo(), 3844 /*TypeParamList=*/nullptr, 3845 /*PrevDecl=*/nullptr, Loc, 3846 D->isImplicitInterfaceDecl()); 3847 ToIface->setLexicalDeclContext(LexicalDC); 3848 LexicalDC->addDeclInternal(ToIface); 3849 } 3850 Importer.Imported(D, ToIface); 3851 // Import the type parameter list after calling Imported, to avoid 3852 // loops when bringing in their DeclContext. 3853 ToIface->setTypeParamList(ImportObjCTypeParamList( 3854 D->getTypeParamListAsWritten())); 3855 3856 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface)) 3857 return nullptr; 3858 3859 return ToIface; 3860 } 3861 3862 Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { 3863 auto *Category = cast_or_null<ObjCCategoryDecl>( 3864 Importer.Import(D->getCategoryDecl())); 3865 if (!Category) 3866 return nullptr; 3867 3868 ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); 3869 if (!ToImpl) { 3870 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 3871 if (!DC) 3872 return nullptr; 3873 3874 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc()); 3875 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC, 3876 Importer.Import(D->getIdentifier()), 3877 Category->getClassInterface(), 3878 Importer.Import(D->getLocation()), 3879 Importer.Import(D->getAtStartLoc()), 3880 CategoryNameLoc); 3881 3882 DeclContext *LexicalDC = DC; 3883 if (D->getDeclContext() != D->getLexicalDeclContext()) { 3884 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 3885 if (!LexicalDC) 3886 return nullptr; 3887 3888 ToImpl->setLexicalDeclContext(LexicalDC); 3889 } 3890 3891 LexicalDC->addDeclInternal(ToImpl); 3892 Category->setImplementation(ToImpl); 3893 } 3894 3895 Importer.Imported(D, ToImpl); 3896 ImportDeclContext(D); 3897 return ToImpl; 3898 } 3899 3900 Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { 3901 // Find the corresponding interface. 3902 auto *Iface = cast_or_null<ObjCInterfaceDecl>( 3903 Importer.Import(D->getClassInterface())); 3904 if (!Iface) 3905 return nullptr; 3906 3907 // Import the superclass, if any. 3908 ObjCInterfaceDecl *Super = nullptr; 3909 if (D->getSuperClass()) { 3910 Super = cast_or_null<ObjCInterfaceDecl>( 3911 Importer.Import(D->getSuperClass())); 3912 if (!Super) 3913 return nullptr; 3914 } 3915 3916 ObjCImplementationDecl *Impl = Iface->getImplementation(); 3917 if (!Impl) { 3918 // We haven't imported an implementation yet. Create a new @implementation 3919 // now. 3920 Impl = ObjCImplementationDecl::Create(Importer.getToContext(), 3921 Importer.ImportContext(D->getDeclContext()), 3922 Iface, Super, 3923 Importer.Import(D->getLocation()), 3924 Importer.Import(D->getAtStartLoc()), 3925 Importer.Import(D->getSuperClassLoc()), 3926 Importer.Import(D->getIvarLBraceLoc()), 3927 Importer.Import(D->getIvarRBraceLoc())); 3928 3929 if (D->getDeclContext() != D->getLexicalDeclContext()) { 3930 DeclContext *LexicalDC 3931 = Importer.ImportContext(D->getLexicalDeclContext()); 3932 if (!LexicalDC) 3933 return nullptr; 3934 Impl->setLexicalDeclContext(LexicalDC); 3935 } 3936 3937 // Associate the implementation with the class it implements. 3938 Iface->setImplementation(Impl); 3939 Importer.Imported(D, Iface->getImplementation()); 3940 } else { 3941 Importer.Imported(D, Iface->getImplementation()); 3942 3943 // Verify that the existing @implementation has the same superclass. 3944 if ((Super && !Impl->getSuperClass()) || 3945 (!Super && Impl->getSuperClass()) || 3946 (Super && Impl->getSuperClass() && 3947 !declaresSameEntity(Super->getCanonicalDecl(), 3948 Impl->getSuperClass()))) { 3949 Importer.ToDiag(Impl->getLocation(), 3950 diag::err_odr_objc_superclass_inconsistent) 3951 << Iface->getDeclName(); 3952 // FIXME: It would be nice to have the location of the superclass 3953 // below. 3954 if (Impl->getSuperClass()) 3955 Importer.ToDiag(Impl->getLocation(), 3956 diag::note_odr_objc_superclass) 3957 << Impl->getSuperClass()->getDeclName(); 3958 else 3959 Importer.ToDiag(Impl->getLocation(), 3960 diag::note_odr_objc_missing_superclass); 3961 if (D->getSuperClass()) 3962 Importer.FromDiag(D->getLocation(), 3963 diag::note_odr_objc_superclass) 3964 << D->getSuperClass()->getDeclName(); 3965 else 3966 Importer.FromDiag(D->getLocation(), 3967 diag::note_odr_objc_missing_superclass); 3968 return nullptr; 3969 } 3970 } 3971 3972 // Import all of the members of this @implementation. 3973 ImportDeclContext(D); 3974 3975 return Impl; 3976 } 3977 3978 Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { 3979 // Import the major distinguishing characteristics of an @property. 3980 DeclContext *DC, *LexicalDC; 3981 DeclarationName Name; 3982 SourceLocation Loc; 3983 NamedDecl *ToD; 3984 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 3985 return nullptr; 3986 if (ToD) 3987 return ToD; 3988 3989 // Check whether we have already imported this property. 3990 SmallVector<NamedDecl *, 2> FoundDecls; 3991 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 3992 for (auto *FoundDecl : FoundDecls) { 3993 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) { 3994 // Check property types. 3995 if (!Importer.IsStructurallyEquivalent(D->getType(), 3996 FoundProp->getType())) { 3997 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent) 3998 << Name << D->getType() << FoundProp->getType(); 3999 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) 4000 << FoundProp->getType(); 4001 return nullptr; 4002 } 4003 4004 // FIXME: Check property attributes, getters, setters, etc.? 4005 4006 // Consider these properties to be equivalent. 4007 Importer.Imported(D, FoundProp); 4008 return FoundProp; 4009 } 4010 } 4011 4012 // Import the type. 4013 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo()); 4014 if (!TSI) 4015 return nullptr; 4016 4017 // Create the new property. 4018 ObjCPropertyDecl *ToProperty 4019 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc, 4020 Name.getAsIdentifierInfo(), 4021 Importer.Import(D->getAtLoc()), 4022 Importer.Import(D->getLParenLoc()), 4023 Importer.Import(D->getType()), 4024 TSI, 4025 D->getPropertyImplementation()); 4026 Importer.Imported(D, ToProperty); 4027 ToProperty->setLexicalDeclContext(LexicalDC); 4028 LexicalDC->addDeclInternal(ToProperty); 4029 4030 ToProperty->setPropertyAttributes(D->getPropertyAttributes()); 4031 ToProperty->setPropertyAttributesAsWritten( 4032 D->getPropertyAttributesAsWritten()); 4033 ToProperty->setGetterName(Importer.Import(D->getGetterName()), 4034 Importer.Import(D->getGetterNameLoc())); 4035 ToProperty->setSetterName(Importer.Import(D->getSetterName()), 4036 Importer.Import(D->getSetterNameLoc())); 4037 ToProperty->setGetterMethodDecl( 4038 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl()))); 4039 ToProperty->setSetterMethodDecl( 4040 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl()))); 4041 ToProperty->setPropertyIvarDecl( 4042 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl()))); 4043 return ToProperty; 4044 } 4045 4046 Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { 4047 auto *Property = cast_or_null<ObjCPropertyDecl>( 4048 Importer.Import(D->getPropertyDecl())); 4049 if (!Property) 4050 return nullptr; 4051 4052 DeclContext *DC = Importer.ImportContext(D->getDeclContext()); 4053 if (!DC) 4054 return nullptr; 4055 4056 // Import the lexical declaration context. 4057 DeclContext *LexicalDC = DC; 4058 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4059 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4060 if (!LexicalDC) 4061 return nullptr; 4062 } 4063 4064 auto *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC); 4065 if (!InImpl) 4066 return nullptr; 4067 4068 // Import the ivar (for an @synthesize). 4069 ObjCIvarDecl *Ivar = nullptr; 4070 if (D->getPropertyIvarDecl()) { 4071 Ivar = cast_or_null<ObjCIvarDecl>( 4072 Importer.Import(D->getPropertyIvarDecl())); 4073 if (!Ivar) 4074 return nullptr; 4075 } 4076 4077 ObjCPropertyImplDecl *ToImpl 4078 = InImpl->FindPropertyImplDecl(Property->getIdentifier(), 4079 Property->getQueryKind()); 4080 if (!ToImpl) { 4081 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC, 4082 Importer.Import(D->getLocStart()), 4083 Importer.Import(D->getLocation()), 4084 Property, 4085 D->getPropertyImplementation(), 4086 Ivar, 4087 Importer.Import(D->getPropertyIvarDeclLoc())); 4088 ToImpl->setLexicalDeclContext(LexicalDC); 4089 Importer.Imported(D, ToImpl); 4090 LexicalDC->addDeclInternal(ToImpl); 4091 } else { 4092 // Check that we have the same kind of property implementation (@synthesize 4093 // vs. @dynamic). 4094 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { 4095 Importer.ToDiag(ToImpl->getLocation(), 4096 diag::err_odr_objc_property_impl_kind_inconsistent) 4097 << Property->getDeclName() 4098 << (ToImpl->getPropertyImplementation() 4099 == ObjCPropertyImplDecl::Dynamic); 4100 Importer.FromDiag(D->getLocation(), 4101 diag::note_odr_objc_property_impl_kind) 4102 << D->getPropertyDecl()->getDeclName() 4103 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); 4104 return nullptr; 4105 } 4106 4107 // For @synthesize, check that we have the same 4108 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && 4109 Ivar != ToImpl->getPropertyIvarDecl()) { 4110 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), 4111 diag::err_odr_objc_synthesize_ivar_inconsistent) 4112 << Property->getDeclName() 4113 << ToImpl->getPropertyIvarDecl()->getDeclName() 4114 << Ivar->getDeclName(); 4115 Importer.FromDiag(D->getPropertyIvarDeclLoc(), 4116 diag::note_odr_objc_synthesize_ivar_here) 4117 << D->getPropertyIvarDecl()->getDeclName(); 4118 return nullptr; 4119 } 4120 4121 // Merge the existing implementation with the new implementation. 4122 Importer.Imported(D, ToImpl); 4123 } 4124 4125 return ToImpl; 4126 } 4127 4128 Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { 4129 // For template arguments, we adopt the translation unit as our declaration 4130 // context. This context will be fixed when the actual template declaration 4131 // is created. 4132 4133 // FIXME: Import default argument. 4134 return TemplateTypeParmDecl::Create(Importer.getToContext(), 4135 Importer.getToContext().getTranslationUnitDecl(), 4136 Importer.Import(D->getLocStart()), 4137 Importer.Import(D->getLocation()), 4138 D->getDepth(), 4139 D->getIndex(), 4140 Importer.Import(D->getIdentifier()), 4141 D->wasDeclaredWithTypename(), 4142 D->isParameterPack()); 4143 } 4144 4145 Decl * 4146 ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { 4147 // Import the name of this declaration. 4148 DeclarationName Name = Importer.Import(D->getDeclName()); 4149 if (D->getDeclName() && !Name) 4150 return nullptr; 4151 4152 // Import the location of this declaration. 4153 SourceLocation Loc = Importer.Import(D->getLocation()); 4154 4155 // Import the type of this declaration. 4156 QualType T = Importer.Import(D->getType()); 4157 if (T.isNull()) 4158 return nullptr; 4159 4160 // Import type-source information. 4161 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 4162 if (D->getTypeSourceInfo() && !TInfo) 4163 return nullptr; 4164 4165 // FIXME: Import default argument. 4166 4167 return NonTypeTemplateParmDecl::Create(Importer.getToContext(), 4168 Importer.getToContext().getTranslationUnitDecl(), 4169 Importer.Import(D->getInnerLocStart()), 4170 Loc, D->getDepth(), D->getPosition(), 4171 Name.getAsIdentifierInfo(), 4172 T, D->isParameterPack(), TInfo); 4173 } 4174 4175 Decl * 4176 ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { 4177 // Import the name of this declaration. 4178 DeclarationName Name = Importer.Import(D->getDeclName()); 4179 if (D->getDeclName() && !Name) 4180 return nullptr; 4181 4182 // Import the location of this declaration. 4183 SourceLocation Loc = Importer.Import(D->getLocation()); 4184 4185 // Import template parameters. 4186 TemplateParameterList *TemplateParams 4187 = ImportTemplateParameterList(D->getTemplateParameters()); 4188 if (!TemplateParams) 4189 return nullptr; 4190 4191 // FIXME: Import default argument. 4192 4193 return TemplateTemplateParmDecl::Create(Importer.getToContext(), 4194 Importer.getToContext().getTranslationUnitDecl(), 4195 Loc, D->getDepth(), D->getPosition(), 4196 D->isParameterPack(), 4197 Name.getAsIdentifierInfo(), 4198 TemplateParams); 4199 } 4200 4201 // Returns the definition for a (forward) declaration of a ClassTemplateDecl, if 4202 // it has any definition in the redecl chain. 4203 static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) { 4204 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition(); 4205 if (!ToTemplatedDef) 4206 return nullptr; 4207 ClassTemplateDecl *TemplateWithDef = 4208 ToTemplatedDef->getDescribedClassTemplate(); 4209 return TemplateWithDef; 4210 } 4211 4212 Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 4213 // If this record has a definition in the translation unit we're coming from, 4214 // but this particular declaration is not that definition, import the 4215 // definition and map to that. 4216 auto *Definition = 4217 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition()); 4218 if (Definition && Definition != D->getTemplatedDecl()) { 4219 Decl *ImportedDef 4220 = Importer.Import(Definition->getDescribedClassTemplate()); 4221 if (!ImportedDef) 4222 return nullptr; 4223 4224 return Importer.Imported(D, ImportedDef); 4225 } 4226 4227 // Import the major distinguishing characteristics of this class template. 4228 DeclContext *DC, *LexicalDC; 4229 DeclarationName Name; 4230 SourceLocation Loc; 4231 NamedDecl *ToD; 4232 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4233 return nullptr; 4234 if (ToD) 4235 return ToD; 4236 4237 // We may already have a template of the same name; try to find and match it. 4238 if (!DC->isFunctionOrMethod()) { 4239 SmallVector<NamedDecl *, 4> ConflictingDecls; 4240 SmallVector<NamedDecl *, 2> FoundDecls; 4241 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4242 for (auto *FoundDecl : FoundDecls) { 4243 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 4244 continue; 4245 4246 Decl *Found = FoundDecl; 4247 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) { 4248 4249 // The class to be imported is a definition. 4250 if (D->isThisDeclarationADefinition()) { 4251 // Lookup will find the fwd decl only if that is more recent than the 4252 // definition. So, try to get the definition if that is available in 4253 // the redecl chain. 4254 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate); 4255 if (!TemplateWithDef) 4256 continue; 4257 FoundTemplate = TemplateWithDef; // Continue with the definition. 4258 } 4259 4260 if (IsStructuralMatch(D, FoundTemplate)) { 4261 // The class templates structurally match; call it the same template. 4262 4263 Importer.Imported(D->getTemplatedDecl(), 4264 FoundTemplate->getTemplatedDecl()); 4265 return Importer.Imported(D, FoundTemplate); 4266 } 4267 } 4268 4269 ConflictingDecls.push_back(FoundDecl); 4270 } 4271 4272 if (!ConflictingDecls.empty()) { 4273 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, 4274 ConflictingDecls.data(), 4275 ConflictingDecls.size()); 4276 } 4277 4278 if (!Name) 4279 return nullptr; 4280 } 4281 4282 CXXRecordDecl *FromTemplated = D->getTemplatedDecl(); 4283 4284 // Create the declaration that is being templated. 4285 auto *ToTemplated = cast_or_null<CXXRecordDecl>( 4286 Importer.Import(FromTemplated)); 4287 if (!ToTemplated) 4288 return nullptr; 4289 4290 // Resolve possible cyclic import. 4291 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D)) 4292 return AlreadyImported; 4293 4294 // Create the class template declaration itself. 4295 TemplateParameterList *TemplateParams = 4296 ImportTemplateParameterList(D->getTemplateParameters()); 4297 if (!TemplateParams) 4298 return nullptr; 4299 4300 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC, 4301 Loc, Name, TemplateParams, 4302 ToTemplated); 4303 ToTemplated->setDescribedClassTemplate(D2); 4304 4305 D2->setAccess(D->getAccess()); 4306 D2->setLexicalDeclContext(LexicalDC); 4307 LexicalDC->addDeclInternal(D2); 4308 4309 // Note the relationship between the class templates. 4310 Importer.Imported(D, D2); 4311 Importer.Imported(FromTemplated, ToTemplated); 4312 4313 if (FromTemplated->isCompleteDefinition() && 4314 !ToTemplated->isCompleteDefinition()) { 4315 // FIXME: Import definition! 4316 } 4317 4318 return D2; 4319 } 4320 4321 Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl( 4322 ClassTemplateSpecializationDecl *D) { 4323 // If this record has a definition in the translation unit we're coming from, 4324 // but this particular declaration is not that definition, import the 4325 // definition and map to that. 4326 TagDecl *Definition = D->getDefinition(); 4327 if (Definition && Definition != D) { 4328 Decl *ImportedDef = Importer.Import(Definition); 4329 if (!ImportedDef) 4330 return nullptr; 4331 4332 return Importer.Imported(D, ImportedDef); 4333 } 4334 4335 auto *ClassTemplate = 4336 cast_or_null<ClassTemplateDecl>(Importer.Import( 4337 D->getSpecializedTemplate())); 4338 if (!ClassTemplate) 4339 return nullptr; 4340 4341 // Import the context of this declaration. 4342 DeclContext *DC = ClassTemplate->getDeclContext(); 4343 if (!DC) 4344 return nullptr; 4345 4346 DeclContext *LexicalDC = DC; 4347 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4348 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4349 if (!LexicalDC) 4350 return nullptr; 4351 } 4352 4353 // Import the location of this declaration. 4354 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 4355 SourceLocation IdLoc = Importer.Import(D->getLocation()); 4356 4357 // Import template arguments. 4358 SmallVector<TemplateArgument, 2> TemplateArgs; 4359 if (ImportTemplateArguments(D->getTemplateArgs().data(), 4360 D->getTemplateArgs().size(), 4361 TemplateArgs)) 4362 return nullptr; 4363 4364 // Try to find an existing specialization with these template arguments. 4365 void *InsertPos = nullptr; 4366 ClassTemplateSpecializationDecl *D2 4367 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos); 4368 if (D2) { 4369 // We already have a class template specialization with these template 4370 // arguments. 4371 4372 // FIXME: Check for specialization vs. instantiation errors. 4373 4374 if (RecordDecl *FoundDef = D2->getDefinition()) { 4375 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) { 4376 // The record types structurally match, or the "from" translation 4377 // unit only had a forward declaration anyway; call it the same 4378 // function. 4379 return Importer.Imported(D, FoundDef); 4380 } 4381 } 4382 } else { 4383 // Create a new specialization. 4384 if (auto *PartialSpec = 4385 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) { 4386 // Import TemplateArgumentListInfo 4387 TemplateArgumentListInfo ToTAInfo; 4388 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten(); 4389 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo)) 4390 return nullptr; 4391 4392 QualType CanonInjType = Importer.Import( 4393 PartialSpec->getInjectedSpecializationType()); 4394 if (CanonInjType.isNull()) 4395 return nullptr; 4396 CanonInjType = CanonInjType.getCanonicalType(); 4397 4398 TemplateParameterList *ToTPList = ImportTemplateParameterList( 4399 PartialSpec->getTemplateParameters()); 4400 if (!ToTPList && PartialSpec->getTemplateParameters()) 4401 return nullptr; 4402 4403 D2 = ClassTemplatePartialSpecializationDecl::Create( 4404 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc, 4405 ToTPList, ClassTemplate, 4406 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()), 4407 ToTAInfo, CanonInjType, nullptr); 4408 4409 } else { 4410 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(), 4411 D->getTagKind(), DC, 4412 StartLoc, IdLoc, 4413 ClassTemplate, 4414 TemplateArgs, 4415 /*PrevDecl=*/nullptr); 4416 } 4417 4418 D2->setSpecializationKind(D->getSpecializationKind()); 4419 4420 // Add this specialization to the class template. 4421 ClassTemplate->AddSpecialization(D2, InsertPos); 4422 4423 // Import the qualifier, if any. 4424 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 4425 4426 Importer.Imported(D, D2); 4427 4428 if (auto *TSI = D->getTypeAsWritten()) { 4429 TypeSourceInfo *TInfo = Importer.Import(TSI); 4430 if (!TInfo) 4431 return nullptr; 4432 D2->setTypeAsWritten(TInfo); 4433 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc())); 4434 D2->setExternLoc(Importer.Import(D->getExternLoc())); 4435 } 4436 4437 SourceLocation POI = Importer.Import(D->getPointOfInstantiation()); 4438 if (POI.isValid()) 4439 D2->setPointOfInstantiation(POI); 4440 else if (D->getPointOfInstantiation().isValid()) 4441 return nullptr; 4442 4443 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind()); 4444 4445 // Set the context of this specialization/instantiation. 4446 D2->setLexicalDeclContext(LexicalDC); 4447 4448 // Add to the DC only if it was an explicit specialization/instantiation. 4449 if (D2->isExplicitInstantiationOrSpecialization()) { 4450 LexicalDC->addDeclInternal(D2); 4451 } 4452 } 4453 Importer.Imported(D, D2); 4454 if (D->isCompleteDefinition() && ImportDefinition(D, D2)) 4455 return nullptr; 4456 4457 return D2; 4458 } 4459 4460 Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { 4461 // If this variable has a definition in the translation unit we're coming 4462 // from, 4463 // but this particular declaration is not that definition, import the 4464 // definition and map to that. 4465 auto *Definition = 4466 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition()); 4467 if (Definition && Definition != D->getTemplatedDecl()) { 4468 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate()); 4469 if (!ImportedDef) 4470 return nullptr; 4471 4472 return Importer.Imported(D, ImportedDef); 4473 } 4474 4475 // Import the major distinguishing characteristics of this variable template. 4476 DeclContext *DC, *LexicalDC; 4477 DeclarationName Name; 4478 SourceLocation Loc; 4479 NamedDecl *ToD; 4480 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4481 return nullptr; 4482 if (ToD) 4483 return ToD; 4484 4485 // We may already have a template of the same name; try to find and match it. 4486 assert(!DC->isFunctionOrMethod() && 4487 "Variable templates cannot be declared at function scope"); 4488 SmallVector<NamedDecl *, 4> ConflictingDecls; 4489 SmallVector<NamedDecl *, 2> FoundDecls; 4490 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4491 for (auto *FoundDecl : FoundDecls) { 4492 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) 4493 continue; 4494 4495 Decl *Found = FoundDecl; 4496 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) { 4497 if (IsStructuralMatch(D, FoundTemplate)) { 4498 // The variable templates structurally match; call it the same template. 4499 Importer.Imported(D->getTemplatedDecl(), 4500 FoundTemplate->getTemplatedDecl()); 4501 return Importer.Imported(D, FoundTemplate); 4502 } 4503 } 4504 4505 ConflictingDecls.push_back(FoundDecl); 4506 } 4507 4508 if (!ConflictingDecls.empty()) { 4509 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, 4510 ConflictingDecls.data(), 4511 ConflictingDecls.size()); 4512 } 4513 4514 if (!Name) 4515 return nullptr; 4516 4517 VarDecl *DTemplated = D->getTemplatedDecl(); 4518 4519 // Import the type. 4520 QualType T = Importer.Import(DTemplated->getType()); 4521 if (T.isNull()) 4522 return nullptr; 4523 4524 // Create the declaration that is being templated. 4525 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated)); 4526 if (!ToTemplated) 4527 return nullptr; 4528 4529 // Create the variable template declaration itself. 4530 TemplateParameterList *TemplateParams = 4531 ImportTemplateParameterList(D->getTemplateParameters()); 4532 if (!TemplateParams) 4533 return nullptr; 4534 4535 VarTemplateDecl *ToVarTD = VarTemplateDecl::Create( 4536 Importer.getToContext(), DC, Loc, Name, TemplateParams, ToTemplated); 4537 ToTemplated->setDescribedVarTemplate(ToVarTD); 4538 4539 ToVarTD->setAccess(D->getAccess()); 4540 ToVarTD->setLexicalDeclContext(LexicalDC); 4541 LexicalDC->addDeclInternal(ToVarTD); 4542 4543 // Note the relationship between the variable templates. 4544 Importer.Imported(D, ToVarTD); 4545 Importer.Imported(DTemplated, ToTemplated); 4546 4547 if (DTemplated->isThisDeclarationADefinition() && 4548 !ToTemplated->isThisDeclarationADefinition()) { 4549 // FIXME: Import definition! 4550 } 4551 4552 return ToVarTD; 4553 } 4554 4555 Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl( 4556 VarTemplateSpecializationDecl *D) { 4557 // If this record has a definition in the translation unit we're coming from, 4558 // but this particular declaration is not that definition, import the 4559 // definition and map to that. 4560 VarDecl *Definition = D->getDefinition(); 4561 if (Definition && Definition != D) { 4562 Decl *ImportedDef = Importer.Import(Definition); 4563 if (!ImportedDef) 4564 return nullptr; 4565 4566 return Importer.Imported(D, ImportedDef); 4567 } 4568 4569 auto *VarTemplate = cast_or_null<VarTemplateDecl>( 4570 Importer.Import(D->getSpecializedTemplate())); 4571 if (!VarTemplate) 4572 return nullptr; 4573 4574 // Import the context of this declaration. 4575 DeclContext *DC = VarTemplate->getDeclContext(); 4576 if (!DC) 4577 return nullptr; 4578 4579 DeclContext *LexicalDC = DC; 4580 if (D->getDeclContext() != D->getLexicalDeclContext()) { 4581 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); 4582 if (!LexicalDC) 4583 return nullptr; 4584 } 4585 4586 // Import the location of this declaration. 4587 SourceLocation StartLoc = Importer.Import(D->getLocStart()); 4588 SourceLocation IdLoc = Importer.Import(D->getLocation()); 4589 4590 // Import template arguments. 4591 SmallVector<TemplateArgument, 2> TemplateArgs; 4592 if (ImportTemplateArguments(D->getTemplateArgs().data(), 4593 D->getTemplateArgs().size(), TemplateArgs)) 4594 return nullptr; 4595 4596 // Try to find an existing specialization with these template arguments. 4597 void *InsertPos = nullptr; 4598 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization( 4599 TemplateArgs, InsertPos); 4600 if (D2) { 4601 // We already have a variable template specialization with these template 4602 // arguments. 4603 4604 // FIXME: Check for specialization vs. instantiation errors. 4605 4606 if (VarDecl *FoundDef = D2->getDefinition()) { 4607 if (!D->isThisDeclarationADefinition() || 4608 IsStructuralMatch(D, FoundDef)) { 4609 // The record types structurally match, or the "from" translation 4610 // unit only had a forward declaration anyway; call it the same 4611 // variable. 4612 return Importer.Imported(D, FoundDef); 4613 } 4614 } 4615 } else { 4616 // Import the type. 4617 QualType T = Importer.Import(D->getType()); 4618 if (T.isNull()) 4619 return nullptr; 4620 4621 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); 4622 if (D->getTypeSourceInfo() && !TInfo) 4623 return nullptr; 4624 4625 TemplateArgumentListInfo ToTAInfo; 4626 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo)) 4627 return nullptr; 4628 4629 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl; 4630 // Create a new specialization. 4631 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) { 4632 // Import TemplateArgumentListInfo 4633 TemplateArgumentListInfo ArgInfos; 4634 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten(); 4635 // NOTE: FromTAArgsAsWritten and template parameter list are non-null. 4636 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos)) 4637 return nullptr; 4638 4639 TemplateParameterList *ToTPList = ImportTemplateParameterList( 4640 FromPartial->getTemplateParameters()); 4641 if (!ToTPList) 4642 return nullptr; 4643 4644 auto *ToPartial = PartVarSpecDecl::Create( 4645 Importer.getToContext(), DC, StartLoc, IdLoc, ToTPList, VarTemplate, 4646 T, TInfo, D->getStorageClass(), TemplateArgs, ArgInfos); 4647 4648 auto *FromInst = FromPartial->getInstantiatedFromMember(); 4649 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst)); 4650 if (FromInst && !ToInst) 4651 return nullptr; 4652 4653 ToPartial->setInstantiatedFromMember(ToInst); 4654 if (FromPartial->isMemberSpecialization()) 4655 ToPartial->setMemberSpecialization(); 4656 4657 D2 = ToPartial; 4658 } else { // Full specialization 4659 D2 = VarTemplateSpecializationDecl::Create( 4660 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo, 4661 D->getStorageClass(), TemplateArgs); 4662 } 4663 4664 SourceLocation POI = D->getPointOfInstantiation(); 4665 if (POI.isValid()) 4666 D2->setPointOfInstantiation(Importer.Import(POI)); 4667 4668 D2->setSpecializationKind(D->getSpecializationKind()); 4669 D2->setTemplateArgsInfo(ToTAInfo); 4670 4671 // Add this specialization to the class template. 4672 VarTemplate->AddSpecialization(D2, InsertPos); 4673 4674 // Import the qualifier, if any. 4675 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); 4676 4677 if (D->isConstexpr()) 4678 D2->setConstexpr(true); 4679 4680 // Add the specialization to this context. 4681 D2->setLexicalDeclContext(LexicalDC); 4682 LexicalDC->addDeclInternal(D2); 4683 4684 D2->setAccess(D->getAccess()); 4685 } 4686 4687 Importer.Imported(D, D2); 4688 4689 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if 4690 // declaration has initializer. Should this be fixed in the AST?.. Anyway, 4691 // we have to check the declaration for initializer - otherwise, it won't be 4692 // imported. 4693 if ((D->isThisDeclarationADefinition() || D->hasInit()) && 4694 ImportDefinition(D, D2)) 4695 return nullptr; 4696 4697 return D2; 4698 } 4699 4700 Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 4701 DeclContext *DC, *LexicalDC; 4702 DeclarationName Name; 4703 SourceLocation Loc; 4704 NamedDecl *ToD; 4705 4706 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) 4707 return nullptr; 4708 4709 if (ToD) 4710 return ToD; 4711 4712 // Try to find a function in our own ("to") context with the same name, same 4713 // type, and in the same context as the function we're importing. 4714 if (!LexicalDC->isFunctionOrMethod()) { 4715 unsigned IDNS = Decl::IDNS_Ordinary; 4716 SmallVector<NamedDecl *, 2> FoundDecls; 4717 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); 4718 for (auto *FoundDecl : FoundDecls) { 4719 if (!FoundDecl->isInIdentifierNamespace(IDNS)) 4720 continue; 4721 4722 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) { 4723 if (FoundFunction->hasExternalFormalLinkage() && 4724 D->hasExternalFormalLinkage()) { 4725 if (IsStructuralMatch(D, FoundFunction)) { 4726 Importer.Imported(D, FoundFunction); 4727 // FIXME: Actually try to merge the body and other attributes. 4728 return FoundFunction; 4729 } 4730 } 4731 } 4732 } 4733 } 4734 4735 TemplateParameterList *Params = 4736 ImportTemplateParameterList(D->getTemplateParameters()); 4737 if (!Params) 4738 return nullptr; 4739 4740 auto *TemplatedFD = 4741 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl())); 4742 if (!TemplatedFD) 4743 return nullptr; 4744 4745 FunctionTemplateDecl *ToFunc = FunctionTemplateDecl::Create( 4746 Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD); 4747 4748 TemplatedFD->setDescribedFunctionTemplate(ToFunc); 4749 ToFunc->setAccess(D->getAccess()); 4750 ToFunc->setLexicalDeclContext(LexicalDC); 4751 Importer.Imported(D, ToFunc); 4752 4753 LexicalDC->addDeclInternal(ToFunc); 4754 return ToFunc; 4755 } 4756 4757 //---------------------------------------------------------------------------- 4758 // Import Statements 4759 //---------------------------------------------------------------------------- 4760 4761 DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) { 4762 if (DG.isNull()) 4763 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0); 4764 size_t NumDecls = DG.end() - DG.begin(); 4765 SmallVector<Decl *, 1> ToDecls(NumDecls); 4766 auto &_Importer = this->Importer; 4767 std::transform(DG.begin(), DG.end(), ToDecls.begin(), 4768 [&_Importer](Decl *D) -> Decl * { 4769 return _Importer.Import(D); 4770 }); 4771 return DeclGroupRef::Create(Importer.getToContext(), 4772 ToDecls.begin(), 4773 NumDecls); 4774 } 4775 4776 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) { 4777 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node) 4778 << S->getStmtClassName(); 4779 return nullptr; 4780 } 4781 4782 Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) { 4783 SmallVector<IdentifierInfo *, 4> Names; 4784 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { 4785 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I)); 4786 // ToII is nullptr when no symbolic name is given for output operand 4787 // see ParseStmtAsm::ParseAsmOperandsOpt 4788 if (!ToII && S->getOutputIdentifier(I)) 4789 return nullptr; 4790 Names.push_back(ToII); 4791 } 4792 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { 4793 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I)); 4794 // ToII is nullptr when no symbolic name is given for input operand 4795 // see ParseStmtAsm::ParseAsmOperandsOpt 4796 if (!ToII && S->getInputIdentifier(I)) 4797 return nullptr; 4798 Names.push_back(ToII); 4799 } 4800 4801 SmallVector<StringLiteral *, 4> Clobbers; 4802 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) { 4803 auto *Clobber = cast_or_null<StringLiteral>( 4804 Importer.Import(S->getClobberStringLiteral(I))); 4805 if (!Clobber) 4806 return nullptr; 4807 Clobbers.push_back(Clobber); 4808 } 4809 4810 SmallVector<StringLiteral *, 4> Constraints; 4811 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { 4812 auto *Output = cast_or_null<StringLiteral>( 4813 Importer.Import(S->getOutputConstraintLiteral(I))); 4814 if (!Output) 4815 return nullptr; 4816 Constraints.push_back(Output); 4817 } 4818 4819 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { 4820 auto *Input = cast_or_null<StringLiteral>( 4821 Importer.Import(S->getInputConstraintLiteral(I))); 4822 if (!Input) 4823 return nullptr; 4824 Constraints.push_back(Input); 4825 } 4826 4827 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs()); 4828 if (ImportContainerChecked(S->outputs(), Exprs)) 4829 return nullptr; 4830 4831 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs())) 4832 return nullptr; 4833 4834 auto *AsmStr = cast_or_null<StringLiteral>( 4835 Importer.Import(S->getAsmString())); 4836 if (!AsmStr) 4837 return nullptr; 4838 4839 return new (Importer.getToContext()) GCCAsmStmt( 4840 Importer.getToContext(), 4841 Importer.Import(S->getAsmLoc()), 4842 S->isSimple(), 4843 S->isVolatile(), 4844 S->getNumOutputs(), 4845 S->getNumInputs(), 4846 Names.data(), 4847 Constraints.data(), 4848 Exprs.data(), 4849 AsmStr, 4850 S->getNumClobbers(), 4851 Clobbers.data(), 4852 Importer.Import(S->getRParenLoc())); 4853 } 4854 4855 Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) { 4856 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup()); 4857 for (auto *ToD : ToDG) { 4858 if (!ToD) 4859 return nullptr; 4860 } 4861 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc()); 4862 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc()); 4863 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc); 4864 } 4865 4866 Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) { 4867 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc()); 4868 return new (Importer.getToContext()) NullStmt(ToSemiLoc, 4869 S->hasLeadingEmptyMacro()); 4870 } 4871 4872 Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) { 4873 SmallVector<Stmt *, 8> ToStmts(S->size()); 4874 4875 if (ImportContainerChecked(S->body(), ToStmts)) 4876 return nullptr; 4877 4878 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc()); 4879 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc()); 4880 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc, 4881 ToRBraceLoc); 4882 } 4883 4884 Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) { 4885 Expr *ToLHS = Importer.Import(S->getLHS()); 4886 if (!ToLHS) 4887 return nullptr; 4888 Expr *ToRHS = Importer.Import(S->getRHS()); 4889 if (!ToRHS && S->getRHS()) 4890 return nullptr; 4891 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 4892 if (!ToSubStmt && S->getSubStmt()) 4893 return nullptr; 4894 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc()); 4895 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc()); 4896 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 4897 auto *ToStmt = new (Importer.getToContext()) 4898 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc); 4899 ToStmt->setSubStmt(ToSubStmt); 4900 return ToStmt; 4901 } 4902 4903 Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) { 4904 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc()); 4905 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 4906 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 4907 if (!ToSubStmt && S->getSubStmt()) 4908 return nullptr; 4909 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc, 4910 ToSubStmt); 4911 } 4912 4913 Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) { 4914 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc()); 4915 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl())); 4916 if (!ToLabelDecl && S->getDecl()) 4917 return nullptr; 4918 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 4919 if (!ToSubStmt && S->getSubStmt()) 4920 return nullptr; 4921 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl, 4922 ToSubStmt); 4923 } 4924 4925 Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) { 4926 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc()); 4927 ArrayRef<const Attr*> FromAttrs(S->getAttrs()); 4928 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size()); 4929 if (ImportContainerChecked(FromAttrs, ToAttrs)) 4930 return nullptr; 4931 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 4932 if (!ToSubStmt && S->getSubStmt()) 4933 return nullptr; 4934 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc, 4935 ToAttrs, ToSubStmt); 4936 } 4937 4938 Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) { 4939 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc()); 4940 Stmt *ToInit = Importer.Import(S->getInit()); 4941 if (!ToInit && S->getInit()) 4942 return nullptr; 4943 VarDecl *ToConditionVariable = nullptr; 4944 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 4945 ToConditionVariable = 4946 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 4947 if (!ToConditionVariable) 4948 return nullptr; 4949 } 4950 Expr *ToCondition = Importer.Import(S->getCond()); 4951 if (!ToCondition && S->getCond()) 4952 return nullptr; 4953 Stmt *ToThenStmt = Importer.Import(S->getThen()); 4954 if (!ToThenStmt && S->getThen()) 4955 return nullptr; 4956 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc()); 4957 Stmt *ToElseStmt = Importer.Import(S->getElse()); 4958 if (!ToElseStmt && S->getElse()) 4959 return nullptr; 4960 return new (Importer.getToContext()) IfStmt(Importer.getToContext(), 4961 ToIfLoc, S->isConstexpr(), 4962 ToInit, 4963 ToConditionVariable, 4964 ToCondition, ToThenStmt, 4965 ToElseLoc, ToElseStmt); 4966 } 4967 4968 Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) { 4969 Stmt *ToInit = Importer.Import(S->getInit()); 4970 if (!ToInit && S->getInit()) 4971 return nullptr; 4972 VarDecl *ToConditionVariable = nullptr; 4973 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 4974 ToConditionVariable = 4975 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 4976 if (!ToConditionVariable) 4977 return nullptr; 4978 } 4979 Expr *ToCondition = Importer.Import(S->getCond()); 4980 if (!ToCondition && S->getCond()) 4981 return nullptr; 4982 auto *ToStmt = new (Importer.getToContext()) SwitchStmt( 4983 Importer.getToContext(), ToInit, 4984 ToConditionVariable, ToCondition); 4985 Stmt *ToBody = Importer.Import(S->getBody()); 4986 if (!ToBody && S->getBody()) 4987 return nullptr; 4988 ToStmt->setBody(ToBody); 4989 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc())); 4990 // Now we have to re-chain the cases. 4991 SwitchCase *LastChainedSwitchCase = nullptr; 4992 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr; 4993 SC = SC->getNextSwitchCase()) { 4994 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC)); 4995 if (!ToSC) 4996 return nullptr; 4997 if (LastChainedSwitchCase) 4998 LastChainedSwitchCase->setNextSwitchCase(ToSC); 4999 else 5000 ToStmt->setSwitchCaseList(ToSC); 5001 LastChainedSwitchCase = ToSC; 5002 } 5003 return ToStmt; 5004 } 5005 5006 Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) { 5007 VarDecl *ToConditionVariable = nullptr; 5008 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 5009 ToConditionVariable = 5010 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 5011 if (!ToConditionVariable) 5012 return nullptr; 5013 } 5014 Expr *ToCondition = Importer.Import(S->getCond()); 5015 if (!ToCondition && S->getCond()) 5016 return nullptr; 5017 Stmt *ToBody = Importer.Import(S->getBody()); 5018 if (!ToBody && S->getBody()) 5019 return nullptr; 5020 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); 5021 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(), 5022 ToConditionVariable, 5023 ToCondition, ToBody, 5024 ToWhileLoc); 5025 } 5026 5027 Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) { 5028 Stmt *ToBody = Importer.Import(S->getBody()); 5029 if (!ToBody && S->getBody()) 5030 return nullptr; 5031 Expr *ToCondition = Importer.Import(S->getCond()); 5032 if (!ToCondition && S->getCond()) 5033 return nullptr; 5034 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc()); 5035 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc()); 5036 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5037 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition, 5038 ToDoLoc, ToWhileLoc, 5039 ToRParenLoc); 5040 } 5041 5042 Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) { 5043 Stmt *ToInit = Importer.Import(S->getInit()); 5044 if (!ToInit && S->getInit()) 5045 return nullptr; 5046 Expr *ToCondition = Importer.Import(S->getCond()); 5047 if (!ToCondition && S->getCond()) 5048 return nullptr; 5049 VarDecl *ToConditionVariable = nullptr; 5050 if (VarDecl *FromConditionVariable = S->getConditionVariable()) { 5051 ToConditionVariable = 5052 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable)); 5053 if (!ToConditionVariable) 5054 return nullptr; 5055 } 5056 Expr *ToInc = Importer.Import(S->getInc()); 5057 if (!ToInc && S->getInc()) 5058 return nullptr; 5059 Stmt *ToBody = Importer.Import(S->getBody()); 5060 if (!ToBody && S->getBody()) 5061 return nullptr; 5062 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 5063 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc()); 5064 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5065 return new (Importer.getToContext()) ForStmt(Importer.getToContext(), 5066 ToInit, ToCondition, 5067 ToConditionVariable, 5068 ToInc, ToBody, 5069 ToForLoc, ToLParenLoc, 5070 ToRParenLoc); 5071 } 5072 5073 Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) { 5074 LabelDecl *ToLabel = nullptr; 5075 if (LabelDecl *FromLabel = S->getLabel()) { 5076 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel)); 5077 if (!ToLabel) 5078 return nullptr; 5079 } 5080 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); 5081 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc()); 5082 return new (Importer.getToContext()) GotoStmt(ToLabel, 5083 ToGotoLoc, ToLabelLoc); 5084 } 5085 5086 Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { 5087 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc()); 5088 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc()); 5089 Expr *ToTarget = Importer.Import(S->getTarget()); 5090 if (!ToTarget && S->getTarget()) 5091 return nullptr; 5092 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc, 5093 ToTarget); 5094 } 5095 5096 Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) { 5097 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc()); 5098 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc); 5099 } 5100 5101 Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) { 5102 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc()); 5103 return new (Importer.getToContext()) BreakStmt(ToBreakLoc); 5104 } 5105 5106 Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) { 5107 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc()); 5108 Expr *ToRetExpr = Importer.Import(S->getRetValue()); 5109 if (!ToRetExpr && S->getRetValue()) 5110 return nullptr; 5111 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate()); 5112 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate)); 5113 if (!ToNRVOCandidate && NRVOCandidate) 5114 return nullptr; 5115 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr, 5116 ToNRVOCandidate); 5117 } 5118 5119 Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) { 5120 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc()); 5121 VarDecl *ToExceptionDecl = nullptr; 5122 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) { 5123 ToExceptionDecl = 5124 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); 5125 if (!ToExceptionDecl) 5126 return nullptr; 5127 } 5128 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock()); 5129 if (!ToHandlerBlock && S->getHandlerBlock()) 5130 return nullptr; 5131 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc, 5132 ToExceptionDecl, 5133 ToHandlerBlock); 5134 } 5135 5136 Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) { 5137 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc()); 5138 Stmt *ToTryBlock = Importer.Import(S->getTryBlock()); 5139 if (!ToTryBlock && S->getTryBlock()) 5140 return nullptr; 5141 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers()); 5142 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) { 5143 CXXCatchStmt *FromHandler = S->getHandler(HI); 5144 if (Stmt *ToHandler = Importer.Import(FromHandler)) 5145 ToHandlers[HI] = ToHandler; 5146 else 5147 return nullptr; 5148 } 5149 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock, 5150 ToHandlers); 5151 } 5152 5153 Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { 5154 auto *ToRange = 5155 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt())); 5156 if (!ToRange && S->getRangeStmt()) 5157 return nullptr; 5158 auto *ToBegin = 5159 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt())); 5160 if (!ToBegin && S->getBeginStmt()) 5161 return nullptr; 5162 auto *ToEnd = 5163 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt())); 5164 if (!ToEnd && S->getEndStmt()) 5165 return nullptr; 5166 Expr *ToCond = Importer.Import(S->getCond()); 5167 if (!ToCond && S->getCond()) 5168 return nullptr; 5169 Expr *ToInc = Importer.Import(S->getInc()); 5170 if (!ToInc && S->getInc()) 5171 return nullptr; 5172 auto *ToLoopVar = 5173 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt())); 5174 if (!ToLoopVar && S->getLoopVarStmt()) 5175 return nullptr; 5176 Stmt *ToBody = Importer.Import(S->getBody()); 5177 if (!ToBody && S->getBody()) 5178 return nullptr; 5179 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 5180 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc()); 5181 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc()); 5182 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5183 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd, 5184 ToCond, ToInc, 5185 ToLoopVar, ToBody, 5186 ToForLoc, ToCoawaitLoc, 5187 ToColonLoc, ToRParenLoc); 5188 } 5189 5190 Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { 5191 Stmt *ToElem = Importer.Import(S->getElement()); 5192 if (!ToElem && S->getElement()) 5193 return nullptr; 5194 Expr *ToCollect = Importer.Import(S->getCollection()); 5195 if (!ToCollect && S->getCollection()) 5196 return nullptr; 5197 Stmt *ToBody = Importer.Import(S->getBody()); 5198 if (!ToBody && S->getBody()) 5199 return nullptr; 5200 SourceLocation ToForLoc = Importer.Import(S->getForLoc()); 5201 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5202 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem, 5203 ToCollect, 5204 ToBody, ToForLoc, 5205 ToRParenLoc); 5206 } 5207 5208 Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { 5209 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc()); 5210 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc()); 5211 VarDecl *ToExceptionDecl = nullptr; 5212 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) { 5213 ToExceptionDecl = 5214 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl)); 5215 if (!ToExceptionDecl) 5216 return nullptr; 5217 } 5218 Stmt *ToBody = Importer.Import(S->getCatchBody()); 5219 if (!ToBody && S->getCatchBody()) 5220 return nullptr; 5221 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc, 5222 ToRParenLoc, 5223 ToExceptionDecl, 5224 ToBody); 5225 } 5226 5227 Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { 5228 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc()); 5229 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody()); 5230 if (!ToAtFinallyStmt && S->getFinallyBody()) 5231 return nullptr; 5232 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc, 5233 ToAtFinallyStmt); 5234 } 5235 5236 Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { 5237 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc()); 5238 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody()); 5239 if (!ToAtTryStmt && S->getTryBody()) 5240 return nullptr; 5241 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts()); 5242 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) { 5243 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI); 5244 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt)) 5245 ToCatchStmts[CI] = ToCatchStmt; 5246 else 5247 return nullptr; 5248 } 5249 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt()); 5250 if (!ToAtFinallyStmt && S->getFinallyStmt()) 5251 return nullptr; 5252 return ObjCAtTryStmt::Create(Importer.getToContext(), 5253 ToAtTryLoc, ToAtTryStmt, 5254 ToCatchStmts.begin(), ToCatchStmts.size(), 5255 ToAtFinallyStmt); 5256 } 5257 5258 Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt 5259 (ObjCAtSynchronizedStmt *S) { 5260 SourceLocation ToAtSynchronizedLoc = 5261 Importer.Import(S->getAtSynchronizedLoc()); 5262 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr()); 5263 if (!ToSynchExpr && S->getSynchExpr()) 5264 return nullptr; 5265 Stmt *ToSynchBody = Importer.Import(S->getSynchBody()); 5266 if (!ToSynchBody && S->getSynchBody()) 5267 return nullptr; 5268 return new (Importer.getToContext()) ObjCAtSynchronizedStmt( 5269 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody); 5270 } 5271 5272 Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { 5273 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc()); 5274 Expr *ToThrow = Importer.Import(S->getThrowExpr()); 5275 if (!ToThrow && S->getThrowExpr()) 5276 return nullptr; 5277 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow); 5278 } 5279 5280 Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt 5281 (ObjCAutoreleasePoolStmt *S) { 5282 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc()); 5283 Stmt *ToSubStmt = Importer.Import(S->getSubStmt()); 5284 if (!ToSubStmt && S->getSubStmt()) 5285 return nullptr; 5286 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc, 5287 ToSubStmt); 5288 } 5289 5290 //---------------------------------------------------------------------------- 5291 // Import Expressions 5292 //---------------------------------------------------------------------------- 5293 Expr *ASTNodeImporter::VisitExpr(Expr *E) { 5294 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node) 5295 << E->getStmtClassName(); 5296 return nullptr; 5297 } 5298 5299 Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) { 5300 QualType T = Importer.Import(E->getType()); 5301 if (T.isNull()) 5302 return nullptr; 5303 5304 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5305 if (!SubExpr && E->getSubExpr()) 5306 return nullptr; 5307 5308 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo()); 5309 if (!TInfo) 5310 return nullptr; 5311 5312 return new (Importer.getToContext()) VAArgExpr( 5313 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo, 5314 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI()); 5315 } 5316 5317 Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) { 5318 QualType T = Importer.Import(E->getType()); 5319 if (T.isNull()) 5320 return nullptr; 5321 5322 return new (Importer.getToContext()) GNUNullExpr( 5323 T, Importer.Import(E->getLocStart())); 5324 } 5325 5326 Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) { 5327 QualType T = Importer.Import(E->getType()); 5328 if (T.isNull()) 5329 return nullptr; 5330 5331 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName())); 5332 if (!SL && E->getFunctionName()) 5333 return nullptr; 5334 5335 return new (Importer.getToContext()) PredefinedExpr( 5336 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL); 5337 } 5338 5339 Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { 5340 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl())); 5341 if (!ToD) 5342 return nullptr; 5343 5344 NamedDecl *FoundD = nullptr; 5345 if (E->getDecl() != E->getFoundDecl()) { 5346 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl())); 5347 if (!FoundD) 5348 return nullptr; 5349 } 5350 5351 QualType T = Importer.Import(E->getType()); 5352 if (T.isNull()) 5353 return nullptr; 5354 5355 TemplateArgumentListInfo ToTAInfo; 5356 TemplateArgumentListInfo *ResInfo = nullptr; 5357 if (E->hasExplicitTemplateArgs()) { 5358 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo)) 5359 return nullptr; 5360 ResInfo = &ToTAInfo; 5361 } 5362 5363 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(), 5364 Importer.Import(E->getQualifierLoc()), 5365 Importer.Import(E->getTemplateKeywordLoc()), 5366 ToD, 5367 E->refersToEnclosingVariableOrCapture(), 5368 Importer.Import(E->getLocation()), 5369 T, E->getValueKind(), 5370 FoundD, ResInfo); 5371 if (E->hadMultipleCandidates()) 5372 DRE->setHadMultipleCandidates(true); 5373 return DRE; 5374 } 5375 5376 Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { 5377 QualType T = Importer.Import(E->getType()); 5378 if (T.isNull()) 5379 return nullptr; 5380 5381 return new (Importer.getToContext()) ImplicitValueInitExpr(T); 5382 } 5383 5384 ASTNodeImporter::Designator 5385 ASTNodeImporter::ImportDesignator(const Designator &D) { 5386 if (D.isFieldDesignator()) { 5387 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName()); 5388 // Caller checks for import error 5389 return Designator(ToFieldName, Importer.Import(D.getDotLoc()), 5390 Importer.Import(D.getFieldLoc())); 5391 } 5392 if (D.isArrayDesignator()) 5393 return Designator(D.getFirstExprIndex(), 5394 Importer.Import(D.getLBracketLoc()), 5395 Importer.Import(D.getRBracketLoc())); 5396 5397 assert(D.isArrayRangeDesignator()); 5398 return Designator(D.getFirstExprIndex(), 5399 Importer.Import(D.getLBracketLoc()), 5400 Importer.Import(D.getEllipsisLoc()), 5401 Importer.Import(D.getRBracketLoc())); 5402 } 5403 5404 5405 Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) { 5406 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit())); 5407 if (!Init) 5408 return nullptr; 5409 5410 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1); 5411 // List elements from the second, the first is Init itself 5412 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) { 5413 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I)))) 5414 IndexExprs[I - 1] = Arg; 5415 else 5416 return nullptr; 5417 } 5418 5419 SmallVector<Designator, 4> Designators(DIE->size()); 5420 llvm::transform(DIE->designators(), Designators.begin(), 5421 [this](const Designator &D) -> Designator { 5422 return ImportDesignator(D); 5423 }); 5424 5425 for (const auto &D : DIE->designators()) 5426 if (D.isFieldDesignator() && !D.getFieldName()) 5427 return nullptr; 5428 5429 return DesignatedInitExpr::Create( 5430 Importer.getToContext(), Designators, 5431 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()), 5432 DIE->usesGNUSyntax(), Init); 5433 } 5434 5435 Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) { 5436 QualType T = Importer.Import(E->getType()); 5437 if (T.isNull()) 5438 return nullptr; 5439 5440 return new (Importer.getToContext()) 5441 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation())); 5442 } 5443 5444 Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { 5445 QualType T = Importer.Import(E->getType()); 5446 if (T.isNull()) 5447 return nullptr; 5448 5449 return IntegerLiteral::Create(Importer.getToContext(), 5450 E->getValue(), T, 5451 Importer.Import(E->getLocation())); 5452 } 5453 5454 Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) { 5455 QualType T = Importer.Import(E->getType()); 5456 if (T.isNull()) 5457 return nullptr; 5458 5459 return FloatingLiteral::Create(Importer.getToContext(), 5460 E->getValue(), E->isExact(), T, 5461 Importer.Import(E->getLocation())); 5462 } 5463 5464 Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { 5465 QualType T = Importer.Import(E->getType()); 5466 if (T.isNull()) 5467 return nullptr; 5468 5469 return new (Importer.getToContext()) CharacterLiteral(E->getValue(), 5470 E->getKind(), T, 5471 Importer.Import(E->getLocation())); 5472 } 5473 5474 Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) { 5475 QualType T = Importer.Import(E->getType()); 5476 if (T.isNull()) 5477 return nullptr; 5478 5479 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated()); 5480 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin()); 5481 5482 return StringLiteral::Create(Importer.getToContext(), E->getBytes(), 5483 E->getKind(), E->isPascal(), T, 5484 Locations.data(), Locations.size()); 5485 } 5486 5487 Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 5488 QualType T = Importer.Import(E->getType()); 5489 if (T.isNull()) 5490 return nullptr; 5491 5492 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo()); 5493 if (!TInfo) 5494 return nullptr; 5495 5496 Expr *Init = Importer.Import(E->getInitializer()); 5497 if (!Init) 5498 return nullptr; 5499 5500 return new (Importer.getToContext()) CompoundLiteralExpr( 5501 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(), 5502 Init, E->isFileScope()); 5503 } 5504 5505 Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) { 5506 QualType T = Importer.Import(E->getType()); 5507 if (T.isNull()) 5508 return nullptr; 5509 5510 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs()); 5511 if (ImportArrayChecked( 5512 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(), 5513 Exprs.begin())) 5514 return nullptr; 5515 5516 return new (Importer.getToContext()) AtomicExpr( 5517 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(), 5518 Importer.Import(E->getRParenLoc())); 5519 } 5520 5521 Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) { 5522 QualType T = Importer.Import(E->getType()); 5523 if (T.isNull()) 5524 return nullptr; 5525 5526 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel())); 5527 if (!ToLabel) 5528 return nullptr; 5529 5530 return new (Importer.getToContext()) AddrLabelExpr( 5531 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()), 5532 ToLabel, T); 5533 } 5534 5535 Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) { 5536 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5537 if (!SubExpr) 5538 return nullptr; 5539 5540 return new (Importer.getToContext()) 5541 ParenExpr(Importer.Import(E->getLParen()), 5542 Importer.Import(E->getRParen()), 5543 SubExpr); 5544 } 5545 5546 Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) { 5547 SmallVector<Expr *, 4> Exprs(E->getNumExprs()); 5548 if (ImportContainerChecked(E->exprs(), Exprs)) 5549 return nullptr; 5550 5551 return new (Importer.getToContext()) ParenListExpr( 5552 Importer.getToContext(), Importer.Import(E->getLParenLoc()), 5553 Exprs, Importer.Import(E->getLParenLoc())); 5554 } 5555 5556 Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) { 5557 QualType T = Importer.Import(E->getType()); 5558 if (T.isNull()) 5559 return nullptr; 5560 5561 auto *ToSubStmt = cast_or_null<CompoundStmt>( 5562 Importer.Import(E->getSubStmt())); 5563 if (!ToSubStmt && E->getSubStmt()) 5564 return nullptr; 5565 5566 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T, 5567 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc())); 5568 } 5569 5570 Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { 5571 QualType T = Importer.Import(E->getType()); 5572 if (T.isNull()) 5573 return nullptr; 5574 5575 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5576 if (!SubExpr) 5577 return nullptr; 5578 5579 return new (Importer.getToContext()) UnaryOperator( 5580 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(), 5581 Importer.Import(E->getOperatorLoc()), E->canOverflow()); 5582 } 5583 5584 Expr * 5585 ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) { 5586 QualType ResultType = Importer.Import(E->getType()); 5587 5588 if (E->isArgumentType()) { 5589 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo()); 5590 if (!TInfo) 5591 return nullptr; 5592 5593 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), 5594 TInfo, ResultType, 5595 Importer.Import(E->getOperatorLoc()), 5596 Importer.Import(E->getRParenLoc())); 5597 } 5598 5599 Expr *SubExpr = Importer.Import(E->getArgumentExpr()); 5600 if (!SubExpr) 5601 return nullptr; 5602 5603 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), 5604 SubExpr, ResultType, 5605 Importer.Import(E->getOperatorLoc()), 5606 Importer.Import(E->getRParenLoc())); 5607 } 5608 5609 Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { 5610 QualType T = Importer.Import(E->getType()); 5611 if (T.isNull()) 5612 return nullptr; 5613 5614 Expr *LHS = Importer.Import(E->getLHS()); 5615 if (!LHS) 5616 return nullptr; 5617 5618 Expr *RHS = Importer.Import(E->getRHS()); 5619 if (!RHS) 5620 return nullptr; 5621 5622 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(), 5623 T, E->getValueKind(), 5624 E->getObjectKind(), 5625 Importer.Import(E->getOperatorLoc()), 5626 E->getFPFeatures()); 5627 } 5628 5629 Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) { 5630 QualType T = Importer.Import(E->getType()); 5631 if (T.isNull()) 5632 return nullptr; 5633 5634 Expr *ToLHS = Importer.Import(E->getLHS()); 5635 if (!ToLHS) 5636 return nullptr; 5637 5638 Expr *ToRHS = Importer.Import(E->getRHS()); 5639 if (!ToRHS) 5640 return nullptr; 5641 5642 Expr *ToCond = Importer.Import(E->getCond()); 5643 if (!ToCond) 5644 return nullptr; 5645 5646 return new (Importer.getToContext()) ConditionalOperator( 5647 ToCond, Importer.Import(E->getQuestionLoc()), 5648 ToLHS, Importer.Import(E->getColonLoc()), 5649 ToRHS, T, E->getValueKind(), E->getObjectKind()); 5650 } 5651 5652 Expr *ASTNodeImporter::VisitBinaryConditionalOperator( 5653 BinaryConditionalOperator *E) { 5654 QualType T = Importer.Import(E->getType()); 5655 if (T.isNull()) 5656 return nullptr; 5657 5658 Expr *Common = Importer.Import(E->getCommon()); 5659 if (!Common) 5660 return nullptr; 5661 5662 Expr *Cond = Importer.Import(E->getCond()); 5663 if (!Cond) 5664 return nullptr; 5665 5666 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>( 5667 Importer.Import(E->getOpaqueValue())); 5668 if (!OpaqueValue) 5669 return nullptr; 5670 5671 Expr *TrueExpr = Importer.Import(E->getTrueExpr()); 5672 if (!TrueExpr) 5673 return nullptr; 5674 5675 Expr *FalseExpr = Importer.Import(E->getFalseExpr()); 5676 if (!FalseExpr) 5677 return nullptr; 5678 5679 return new (Importer.getToContext()) BinaryConditionalOperator( 5680 Common, OpaqueValue, Cond, TrueExpr, FalseExpr, 5681 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()), 5682 T, E->getValueKind(), E->getObjectKind()); 5683 } 5684 5685 Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 5686 QualType T = Importer.Import(E->getType()); 5687 if (T.isNull()) 5688 return nullptr; 5689 5690 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo()); 5691 if (!ToQueried) 5692 return nullptr; 5693 5694 Expr *Dim = Importer.Import(E->getDimensionExpression()); 5695 if (!Dim && E->getDimensionExpression()) 5696 return nullptr; 5697 5698 return new (Importer.getToContext()) ArrayTypeTraitExpr( 5699 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried, 5700 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T); 5701 } 5702 5703 Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 5704 QualType T = Importer.Import(E->getType()); 5705 if (T.isNull()) 5706 return nullptr; 5707 5708 Expr *ToQueried = Importer.Import(E->getQueriedExpression()); 5709 if (!ToQueried) 5710 return nullptr; 5711 5712 return new (Importer.getToContext()) ExpressionTraitExpr( 5713 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried, 5714 E->getValue(), Importer.Import(E->getLocEnd()), T); 5715 } 5716 5717 Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) { 5718 QualType T = Importer.Import(E->getType()); 5719 if (T.isNull()) 5720 return nullptr; 5721 5722 Expr *SourceExpr = Importer.Import(E->getSourceExpr()); 5723 if (!SourceExpr && E->getSourceExpr()) 5724 return nullptr; 5725 5726 return new (Importer.getToContext()) OpaqueValueExpr( 5727 Importer.Import(E->getLocation()), T, E->getValueKind(), 5728 E->getObjectKind(), SourceExpr); 5729 } 5730 5731 Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 5732 QualType T = Importer.Import(E->getType()); 5733 if (T.isNull()) 5734 return nullptr; 5735 5736 Expr *ToLHS = Importer.Import(E->getLHS()); 5737 if (!ToLHS) 5738 return nullptr; 5739 5740 Expr *ToRHS = Importer.Import(E->getRHS()); 5741 if (!ToRHS) 5742 return nullptr; 5743 5744 return new (Importer.getToContext()) ArraySubscriptExpr( 5745 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(), 5746 Importer.Import(E->getRBracketLoc())); 5747 } 5748 5749 Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { 5750 QualType T = Importer.Import(E->getType()); 5751 if (T.isNull()) 5752 return nullptr; 5753 5754 QualType CompLHSType = Importer.Import(E->getComputationLHSType()); 5755 if (CompLHSType.isNull()) 5756 return nullptr; 5757 5758 QualType CompResultType = Importer.Import(E->getComputationResultType()); 5759 if (CompResultType.isNull()) 5760 return nullptr; 5761 5762 Expr *LHS = Importer.Import(E->getLHS()); 5763 if (!LHS) 5764 return nullptr; 5765 5766 Expr *RHS = Importer.Import(E->getRHS()); 5767 if (!RHS) 5768 return nullptr; 5769 5770 return new (Importer.getToContext()) 5771 CompoundAssignOperator(LHS, RHS, E->getOpcode(), 5772 T, E->getValueKind(), 5773 E->getObjectKind(), 5774 CompLHSType, CompResultType, 5775 Importer.Import(E->getOperatorLoc()), 5776 E->getFPFeatures()); 5777 } 5778 5779 bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) { 5780 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) { 5781 if (CXXBaseSpecifier *Spec = Importer.Import(*I)) 5782 Path.push_back(Spec); 5783 else 5784 return true; 5785 } 5786 return false; 5787 } 5788 5789 Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { 5790 QualType T = Importer.Import(E->getType()); 5791 if (T.isNull()) 5792 return nullptr; 5793 5794 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5795 if (!SubExpr) 5796 return nullptr; 5797 5798 CXXCastPath BasePath; 5799 if (ImportCastPath(E, BasePath)) 5800 return nullptr; 5801 5802 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(), 5803 SubExpr, &BasePath, E->getValueKind()); 5804 } 5805 5806 Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) { 5807 QualType T = Importer.Import(E->getType()); 5808 if (T.isNull()) 5809 return nullptr; 5810 5811 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5812 if (!SubExpr) 5813 return nullptr; 5814 5815 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten()); 5816 if (!TInfo && E->getTypeInfoAsWritten()) 5817 return nullptr; 5818 5819 CXXCastPath BasePath; 5820 if (ImportCastPath(E, BasePath)) 5821 return nullptr; 5822 5823 switch (E->getStmtClass()) { 5824 case Stmt::CStyleCastExprClass: { 5825 auto *CCE = cast<CStyleCastExpr>(E); 5826 return CStyleCastExpr::Create(Importer.getToContext(), T, 5827 E->getValueKind(), E->getCastKind(), 5828 SubExpr, &BasePath, TInfo, 5829 Importer.Import(CCE->getLParenLoc()), 5830 Importer.Import(CCE->getRParenLoc())); 5831 } 5832 5833 case Stmt::CXXFunctionalCastExprClass: { 5834 auto *FCE = cast<CXXFunctionalCastExpr>(E); 5835 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T, 5836 E->getValueKind(), TInfo, 5837 E->getCastKind(), SubExpr, &BasePath, 5838 Importer.Import(FCE->getLParenLoc()), 5839 Importer.Import(FCE->getRParenLoc())); 5840 } 5841 5842 case Stmt::ObjCBridgedCastExprClass: { 5843 auto *OCE = cast<ObjCBridgedCastExpr>(E); 5844 return new (Importer.getToContext()) ObjCBridgedCastExpr( 5845 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(), 5846 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()), 5847 TInfo, SubExpr); 5848 } 5849 default: 5850 break; // just fall through 5851 } 5852 5853 auto *Named = cast<CXXNamedCastExpr>(E); 5854 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()), 5855 RParenLoc = Importer.Import(Named->getRParenLoc()); 5856 SourceRange Brackets = Importer.Import(Named->getAngleBrackets()); 5857 5858 switch (E->getStmtClass()) { 5859 case Stmt::CXXStaticCastExprClass: 5860 return CXXStaticCastExpr::Create(Importer.getToContext(), T, 5861 E->getValueKind(), E->getCastKind(), 5862 SubExpr, &BasePath, TInfo, 5863 ExprLoc, RParenLoc, Brackets); 5864 5865 case Stmt::CXXDynamicCastExprClass: 5866 return CXXDynamicCastExpr::Create(Importer.getToContext(), T, 5867 E->getValueKind(), E->getCastKind(), 5868 SubExpr, &BasePath, TInfo, 5869 ExprLoc, RParenLoc, Brackets); 5870 5871 case Stmt::CXXReinterpretCastExprClass: 5872 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T, 5873 E->getValueKind(), E->getCastKind(), 5874 SubExpr, &BasePath, TInfo, 5875 ExprLoc, RParenLoc, Brackets); 5876 5877 case Stmt::CXXConstCastExprClass: 5878 return CXXConstCastExpr::Create(Importer.getToContext(), T, 5879 E->getValueKind(), SubExpr, TInfo, ExprLoc, 5880 RParenLoc, Brackets); 5881 default: 5882 llvm_unreachable("Cast expression of unsupported type!"); 5883 return nullptr; 5884 } 5885 } 5886 5887 Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) { 5888 QualType T = Importer.Import(OE->getType()); 5889 if (T.isNull()) 5890 return nullptr; 5891 5892 SmallVector<OffsetOfNode, 4> Nodes; 5893 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) { 5894 const OffsetOfNode &Node = OE->getComponent(I); 5895 5896 switch (Node.getKind()) { 5897 case OffsetOfNode::Array: 5898 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), 5899 Node.getArrayExprIndex(), 5900 Importer.Import(Node.getLocEnd()))); 5901 break; 5902 5903 case OffsetOfNode::Base: { 5904 CXXBaseSpecifier *BS = Importer.Import(Node.getBase()); 5905 if (!BS && Node.getBase()) 5906 return nullptr; 5907 Nodes.push_back(OffsetOfNode(BS)); 5908 break; 5909 } 5910 case OffsetOfNode::Field: { 5911 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField())); 5912 if (!FD) 5913 return nullptr; 5914 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD, 5915 Importer.Import(Node.getLocEnd()))); 5916 break; 5917 } 5918 case OffsetOfNode::Identifier: { 5919 IdentifierInfo *ToII = Importer.Import(Node.getFieldName()); 5920 if (!ToII) 5921 return nullptr; 5922 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII, 5923 Importer.Import(Node.getLocEnd()))); 5924 break; 5925 } 5926 } 5927 } 5928 5929 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions()); 5930 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) { 5931 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I)); 5932 if (!ToIndexExpr) 5933 return nullptr; 5934 Exprs[I] = ToIndexExpr; 5935 } 5936 5937 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo()); 5938 if (!TInfo && OE->getTypeSourceInfo()) 5939 return nullptr; 5940 5941 return OffsetOfExpr::Create(Importer.getToContext(), T, 5942 Importer.Import(OE->getOperatorLoc()), 5943 TInfo, Nodes, Exprs, 5944 Importer.Import(OE->getRParenLoc())); 5945 } 5946 5947 Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 5948 QualType T = Importer.Import(E->getType()); 5949 if (T.isNull()) 5950 return nullptr; 5951 5952 Expr *Operand = Importer.Import(E->getOperand()); 5953 if (!Operand) 5954 return nullptr; 5955 5956 CanThrowResult CanThrow; 5957 if (E->isValueDependent()) 5958 CanThrow = CT_Dependent; 5959 else 5960 CanThrow = E->getValue() ? CT_Can : CT_Cannot; 5961 5962 return new (Importer.getToContext()) CXXNoexceptExpr( 5963 T, Operand, CanThrow, 5964 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd())); 5965 } 5966 5967 Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) { 5968 QualType T = Importer.Import(E->getType()); 5969 if (T.isNull()) 5970 return nullptr; 5971 5972 Expr *SubExpr = Importer.Import(E->getSubExpr()); 5973 if (!SubExpr && E->getSubExpr()) 5974 return nullptr; 5975 5976 return new (Importer.getToContext()) CXXThrowExpr( 5977 SubExpr, T, Importer.Import(E->getThrowLoc()), 5978 E->isThrownVariableInScope()); 5979 } 5980 5981 Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { 5982 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam())); 5983 if (!Param) 5984 return nullptr; 5985 5986 return CXXDefaultArgExpr::Create( 5987 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param); 5988 } 5989 5990 Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { 5991 QualType T = Importer.Import(E->getType()); 5992 if (T.isNull()) 5993 return nullptr; 5994 5995 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo()); 5996 if (!TypeInfo) 5997 return nullptr; 5998 5999 return new (Importer.getToContext()) CXXScalarValueInitExpr( 6000 T, TypeInfo, Importer.Import(E->getRParenLoc())); 6001 } 6002 6003 Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { 6004 Expr *SubExpr = Importer.Import(E->getSubExpr()); 6005 if (!SubExpr) 6006 return nullptr; 6007 6008 auto *Dtor = cast_or_null<CXXDestructorDecl>( 6009 Importer.Import(const_cast<CXXDestructorDecl *>( 6010 E->getTemporary()->getDestructor()))); 6011 if (!Dtor) 6012 return nullptr; 6013 6014 ASTContext &ToCtx = Importer.getToContext(); 6015 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor); 6016 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr); 6017 } 6018 6019 Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) { 6020 QualType T = Importer.Import(CE->getType()); 6021 if (T.isNull()) 6022 return nullptr; 6023 6024 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo()); 6025 if (!TInfo) 6026 return nullptr; 6027 6028 SmallVector<Expr *, 8> Args(CE->getNumArgs()); 6029 if (ImportContainerChecked(CE->arguments(), Args)) 6030 return nullptr; 6031 6032 auto *Ctor = cast_or_null<CXXConstructorDecl>( 6033 Importer.Import(CE->getConstructor())); 6034 if (!Ctor) 6035 return nullptr; 6036 6037 return new (Importer.getToContext()) CXXTemporaryObjectExpr( 6038 Importer.getToContext(), Ctor, T, TInfo, Args, 6039 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(), 6040 CE->isListInitialization(), CE->isStdInitListInitialization(), 6041 CE->requiresZeroInitialization()); 6042 } 6043 6044 Expr * 6045 ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) { 6046 QualType T = Importer.Import(E->getType()); 6047 if (T.isNull()) 6048 return nullptr; 6049 6050 Expr *TempE = Importer.Import(E->GetTemporaryExpr()); 6051 if (!TempE) 6052 return nullptr; 6053 6054 auto *ExtendedBy = cast_or_null<ValueDecl>( 6055 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl()))); 6056 if (!ExtendedBy && E->getExtendingDecl()) 6057 return nullptr; 6058 6059 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr( 6060 T, TempE, E->isBoundToLvalueReference()); 6061 6062 // FIXME: Should ManglingNumber get numbers associated with 'to' context? 6063 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber()); 6064 return ToMTE; 6065 } 6066 6067 Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) { 6068 QualType T = Importer.Import(E->getType()); 6069 if (T.isNull()) 6070 return nullptr; 6071 6072 Expr *Pattern = Importer.Import(E->getPattern()); 6073 if (!Pattern) 6074 return nullptr; 6075 6076 return new (Importer.getToContext()) PackExpansionExpr( 6077 T, Pattern, Importer.Import(E->getEllipsisLoc()), 6078 E->getNumExpansions()); 6079 } 6080 6081 Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 6082 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack())); 6083 if (!Pack) 6084 return nullptr; 6085 6086 Optional<unsigned> Length; 6087 6088 if (!E->isValueDependent()) 6089 Length = E->getPackLength(); 6090 6091 SmallVector<TemplateArgument, 8> PartialArguments; 6092 if (E->isPartiallySubstituted()) { 6093 if (ImportTemplateArguments(E->getPartialArguments().data(), 6094 E->getPartialArguments().size(), 6095 PartialArguments)) 6096 return nullptr; 6097 } 6098 6099 return SizeOfPackExpr::Create( 6100 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack, 6101 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()), 6102 Length, PartialArguments); 6103 } 6104 6105 Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) { 6106 QualType T = Importer.Import(CE->getType()); 6107 if (T.isNull()) 6108 return nullptr; 6109 6110 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs()); 6111 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs)) 6112 return nullptr; 6113 6114 auto *OperatorNewDecl = cast_or_null<FunctionDecl>( 6115 Importer.Import(CE->getOperatorNew())); 6116 if (!OperatorNewDecl && CE->getOperatorNew()) 6117 return nullptr; 6118 6119 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>( 6120 Importer.Import(CE->getOperatorDelete())); 6121 if (!OperatorDeleteDecl && CE->getOperatorDelete()) 6122 return nullptr; 6123 6124 Expr *ToInit = Importer.Import(CE->getInitializer()); 6125 if (!ToInit && CE->getInitializer()) 6126 return nullptr; 6127 6128 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo()); 6129 if (!TInfo) 6130 return nullptr; 6131 6132 Expr *ToArrSize = Importer.Import(CE->getArraySize()); 6133 if (!ToArrSize && CE->getArraySize()) 6134 return nullptr; 6135 6136 return new (Importer.getToContext()) CXXNewExpr( 6137 Importer.getToContext(), 6138 CE->isGlobalNew(), 6139 OperatorNewDecl, OperatorDeleteDecl, 6140 CE->passAlignment(), 6141 CE->doesUsualArrayDeleteWantSize(), 6142 PlacementArgs, 6143 Importer.Import(CE->getTypeIdParens()), 6144 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo, 6145 Importer.Import(CE->getSourceRange()), 6146 Importer.Import(CE->getDirectInitRange())); 6147 } 6148 6149 Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 6150 QualType T = Importer.Import(E->getType()); 6151 if (T.isNull()) 6152 return nullptr; 6153 6154 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>( 6155 Importer.Import(E->getOperatorDelete())); 6156 if (!OperatorDeleteDecl && E->getOperatorDelete()) 6157 return nullptr; 6158 6159 Expr *ToArg = Importer.Import(E->getArgument()); 6160 if (!ToArg && E->getArgument()) 6161 return nullptr; 6162 6163 return new (Importer.getToContext()) CXXDeleteExpr( 6164 T, E->isGlobalDelete(), 6165 E->isArrayForm(), 6166 E->isArrayFormAsWritten(), 6167 E->doesUsualArrayDeleteWantSize(), 6168 OperatorDeleteDecl, 6169 ToArg, 6170 Importer.Import(E->getLocStart())); 6171 } 6172 6173 Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) { 6174 QualType T = Importer.Import(E->getType()); 6175 if (T.isNull()) 6176 return nullptr; 6177 6178 auto *ToCCD = 6179 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor())); 6180 if (!ToCCD) 6181 return nullptr; 6182 6183 SmallVector<Expr *, 6> ToArgs(E->getNumArgs()); 6184 if (ImportContainerChecked(E->arguments(), ToArgs)) 6185 return nullptr; 6186 6187 return CXXConstructExpr::Create(Importer.getToContext(), T, 6188 Importer.Import(E->getLocation()), 6189 ToCCD, E->isElidable(), 6190 ToArgs, E->hadMultipleCandidates(), 6191 E->isListInitialization(), 6192 E->isStdInitListInitialization(), 6193 E->requiresZeroInitialization(), 6194 E->getConstructionKind(), 6195 Importer.Import(E->getParenOrBraceRange())); 6196 } 6197 6198 Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) { 6199 Expr *SubExpr = Importer.Import(EWC->getSubExpr()); 6200 if (!SubExpr && EWC->getSubExpr()) 6201 return nullptr; 6202 6203 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects()); 6204 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++) 6205 if (ExprWithCleanups::CleanupObject Obj = 6206 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I)))) 6207 Objs[I] = Obj; 6208 else 6209 return nullptr; 6210 6211 return ExprWithCleanups::Create(Importer.getToContext(), 6212 SubExpr, EWC->cleanupsHaveSideEffects(), 6213 Objs); 6214 } 6215 6216 Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { 6217 QualType T = Importer.Import(E->getType()); 6218 if (T.isNull()) 6219 return nullptr; 6220 6221 Expr *ToFn = Importer.Import(E->getCallee()); 6222 if (!ToFn) 6223 return nullptr; 6224 6225 SmallVector<Expr *, 4> ToArgs(E->getNumArgs()); 6226 if (ImportContainerChecked(E->arguments(), ToArgs)) 6227 return nullptr; 6228 6229 return new (Importer.getToContext()) CXXMemberCallExpr( 6230 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(), 6231 Importer.Import(E->getRParenLoc())); 6232 } 6233 6234 Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) { 6235 QualType T = Importer.Import(E->getType()); 6236 if (T.isNull()) 6237 return nullptr; 6238 6239 return new (Importer.getToContext()) 6240 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit()); 6241 } 6242 6243 Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { 6244 QualType T = Importer.Import(E->getType()); 6245 if (T.isNull()) 6246 return nullptr; 6247 6248 return new (Importer.getToContext()) 6249 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation())); 6250 } 6251 6252 6253 Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) { 6254 QualType T = Importer.Import(E->getType()); 6255 if (T.isNull()) 6256 return nullptr; 6257 6258 Expr *ToBase = Importer.Import(E->getBase()); 6259 if (!ToBase && E->getBase()) 6260 return nullptr; 6261 6262 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl())); 6263 if (!ToMember && E->getMemberDecl()) 6264 return nullptr; 6265 6266 auto *ToDecl = 6267 dyn_cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())); 6268 if (!ToDecl && E->getFoundDecl().getDecl()) 6269 return nullptr; 6270 6271 DeclAccessPair ToFoundDecl = 6272 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess()); 6273 6274 DeclarationNameInfo ToMemberNameInfo( 6275 Importer.Import(E->getMemberNameInfo().getName()), 6276 Importer.Import(E->getMemberNameInfo().getLoc())); 6277 6278 if (E->hasExplicitTemplateArgs()) { 6279 return nullptr; // FIXME: handle template arguments 6280 } 6281 6282 return MemberExpr::Create(Importer.getToContext(), ToBase, 6283 E->isArrow(), 6284 Importer.Import(E->getOperatorLoc()), 6285 Importer.Import(E->getQualifierLoc()), 6286 Importer.Import(E->getTemplateKeywordLoc()), 6287 ToMember, ToFoundDecl, ToMemberNameInfo, 6288 nullptr, T, E->getValueKind(), 6289 E->getObjectKind()); 6290 } 6291 6292 Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr( 6293 CXXPseudoDestructorExpr *E) { 6294 Expr *BaseE = Importer.Import(E->getBase()); 6295 if (!BaseE) 6296 return nullptr; 6297 6298 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo()); 6299 if (!ScopeInfo && E->getScopeTypeInfo()) 6300 return nullptr; 6301 6302 PseudoDestructorTypeStorage Storage; 6303 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) { 6304 IdentifierInfo *ToII = Importer.Import(FromII); 6305 if (!ToII) 6306 return nullptr; 6307 Storage = PseudoDestructorTypeStorage( 6308 ToII, Importer.Import(E->getDestroyedTypeLoc())); 6309 } else { 6310 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo()); 6311 if (!TI) 6312 return nullptr; 6313 Storage = PseudoDestructorTypeStorage(TI); 6314 } 6315 6316 return new (Importer.getToContext()) CXXPseudoDestructorExpr( 6317 Importer.getToContext(), BaseE, E->isArrow(), 6318 Importer.Import(E->getOperatorLoc()), 6319 Importer.Import(E->getQualifierLoc()), 6320 ScopeInfo, Importer.Import(E->getColonColonLoc()), 6321 Importer.Import(E->getTildeLoc()), Storage); 6322 } 6323 6324 Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr( 6325 CXXDependentScopeMemberExpr *E) { 6326 Expr *Base = nullptr; 6327 if (!E->isImplicitAccess()) { 6328 Base = Importer.Import(E->getBase()); 6329 if (!Base) 6330 return nullptr; 6331 } 6332 6333 QualType BaseType = Importer.Import(E->getBaseType()); 6334 if (BaseType.isNull()) 6335 return nullptr; 6336 6337 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; 6338 if (E->hasExplicitTemplateArgs()) { 6339 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(), 6340 E->template_arguments(), ToTAInfo)) 6341 return nullptr; 6342 ResInfo = &ToTAInfo; 6343 } 6344 6345 DeclarationName Name = Importer.Import(E->getMember()); 6346 if (!E->getMember().isEmpty() && Name.isEmpty()) 6347 return nullptr; 6348 6349 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc())); 6350 // Import additional name location/type info. 6351 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo); 6352 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope()); 6353 if (!ToFQ && E->getFirstQualifierFoundInScope()) 6354 return nullptr; 6355 6356 return CXXDependentScopeMemberExpr::Create( 6357 Importer.getToContext(), Base, BaseType, E->isArrow(), 6358 Importer.Import(E->getOperatorLoc()), 6359 Importer.Import(E->getQualifierLoc()), 6360 Importer.Import(E->getTemplateKeywordLoc()), 6361 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo); 6362 } 6363 6364 Expr * 6365 ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { 6366 DeclarationName Name = Importer.Import(E->getDeclName()); 6367 if (!E->getDeclName().isEmpty() && Name.isEmpty()) 6368 return nullptr; 6369 6370 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getExprLoc())); 6371 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo); 6372 6373 TemplateArgumentListInfo ToTAInfo(Importer.Import(E->getLAngleLoc()), 6374 Importer.Import(E->getRAngleLoc())); 6375 TemplateArgumentListInfo *ResInfo = nullptr; 6376 if (E->hasExplicitTemplateArgs()) { 6377 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo)) 6378 return nullptr; 6379 ResInfo = &ToTAInfo; 6380 } 6381 6382 return DependentScopeDeclRefExpr::Create( 6383 Importer.getToContext(), Importer.Import(E->getQualifierLoc()), 6384 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo); 6385 } 6386 6387 Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr( 6388 CXXUnresolvedConstructExpr *CE) { 6389 unsigned NumArgs = CE->arg_size(); 6390 6391 SmallVector<Expr *, 8> ToArgs(NumArgs); 6392 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin())) 6393 return nullptr; 6394 6395 return CXXUnresolvedConstructExpr::Create( 6396 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()), 6397 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs), 6398 Importer.Import(CE->getRParenLoc())); 6399 } 6400 6401 Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) { 6402 auto *NamingClass = 6403 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass())); 6404 if (E->getNamingClass() && !NamingClass) 6405 return nullptr; 6406 6407 DeclarationName Name = Importer.Import(E->getName()); 6408 if (E->getName() && !Name) 6409 return nullptr; 6410 6411 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc())); 6412 // Import additional name location/type info. 6413 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo); 6414 6415 UnresolvedSet<8> ToDecls; 6416 for (auto *D : E->decls()) { 6417 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D))) 6418 ToDecls.addDecl(To); 6419 else 6420 return nullptr; 6421 } 6422 6423 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; 6424 if (E->hasExplicitTemplateArgs()) { 6425 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(), 6426 E->template_arguments(), ToTAInfo)) 6427 return nullptr; 6428 ResInfo = &ToTAInfo; 6429 } 6430 6431 if (ResInfo || E->getTemplateKeywordLoc().isValid()) 6432 return UnresolvedLookupExpr::Create( 6433 Importer.getToContext(), NamingClass, 6434 Importer.Import(E->getQualifierLoc()), 6435 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(), 6436 ResInfo, ToDecls.begin(), ToDecls.end()); 6437 6438 return UnresolvedLookupExpr::Create( 6439 Importer.getToContext(), NamingClass, 6440 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(), 6441 E->isOverloaded(), ToDecls.begin(), ToDecls.end()); 6442 } 6443 6444 Expr *ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) { 6445 DeclarationName Name = Importer.Import(E->getName()); 6446 if (!E->getName().isEmpty() && Name.isEmpty()) 6447 return nullptr; 6448 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc())); 6449 // Import additional name location/type info. 6450 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo); 6451 6452 QualType BaseType = Importer.Import(E->getType()); 6453 if (!E->getType().isNull() && BaseType.isNull()) 6454 return nullptr; 6455 6456 UnresolvedSet<8> ToDecls; 6457 for (Decl *D : E->decls()) { 6458 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D))) 6459 ToDecls.addDecl(To); 6460 else 6461 return nullptr; 6462 } 6463 6464 TemplateArgumentListInfo ToTAInfo; 6465 TemplateArgumentListInfo *ResInfo = nullptr; 6466 if (E->hasExplicitTemplateArgs()) { 6467 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo)) 6468 return nullptr; 6469 ResInfo = &ToTAInfo; 6470 } 6471 6472 Expr *BaseE = E->isImplicitAccess() ? nullptr : Importer.Import(E->getBase()); 6473 if (!BaseE && !E->isImplicitAccess() && E->getBase()) { 6474 return nullptr; 6475 } 6476 6477 return UnresolvedMemberExpr::Create( 6478 Importer.getToContext(), E->hasUnresolvedUsing(), BaseE, BaseType, 6479 E->isArrow(), Importer.Import(E->getOperatorLoc()), 6480 Importer.Import(E->getQualifierLoc()), 6481 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo, 6482 ToDecls.begin(), ToDecls.end()); 6483 } 6484 6485 Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) { 6486 QualType T = Importer.Import(E->getType()); 6487 if (T.isNull()) 6488 return nullptr; 6489 6490 Expr *ToCallee = Importer.Import(E->getCallee()); 6491 if (!ToCallee && E->getCallee()) 6492 return nullptr; 6493 6494 unsigned NumArgs = E->getNumArgs(); 6495 SmallVector<Expr *, 2> ToArgs(NumArgs); 6496 if (ImportContainerChecked(E->arguments(), ToArgs)) 6497 return nullptr; 6498 6499 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs]; 6500 6501 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) 6502 ToArgs_Copied[ai] = ToArgs[ai]; 6503 6504 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) { 6505 return new (Importer.getToContext()) CXXOperatorCallExpr( 6506 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T, 6507 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()), 6508 OCE->getFPFeatures()); 6509 } 6510 6511 return new (Importer.getToContext()) 6512 CallExpr(Importer.getToContext(), ToCallee, 6513 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(), 6514 Importer.Import(E->getRParenLoc())); 6515 } 6516 6517 Optional<LambdaCapture> 6518 ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) { 6519 VarDecl *Var = nullptr; 6520 if (From.capturesVariable()) { 6521 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar())); 6522 if (!Var) 6523 return None; 6524 } 6525 6526 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(), 6527 From.getCaptureKind(), Var, 6528 From.isPackExpansion() 6529 ? Importer.Import(From.getEllipsisLoc()) 6530 : SourceLocation()); 6531 } 6532 6533 Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) { 6534 CXXRecordDecl *FromClass = LE->getLambdaClass(); 6535 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass)); 6536 if (!ToClass) 6537 return nullptr; 6538 6539 // NOTE: lambda classes are created with BeingDefined flag set up. 6540 // It means that ImportDefinition doesn't work for them and we should fill it 6541 // manually. 6542 if (ToClass->isBeingDefined()) { 6543 for (auto FromField : FromClass->fields()) { 6544 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField)); 6545 if (!ToField) 6546 return nullptr; 6547 } 6548 } 6549 6550 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>( 6551 Importer.Import(LE->getCallOperator())); 6552 if (!ToCallOp) 6553 return nullptr; 6554 6555 ToClass->completeDefinition(); 6556 6557 unsigned NumCaptures = LE->capture_size(); 6558 SmallVector<LambdaCapture, 8> Captures; 6559 Captures.reserve(NumCaptures); 6560 for (const auto &FromCapture : LE->captures()) { 6561 if (auto ToCapture = ImportLambdaCapture(FromCapture)) 6562 Captures.push_back(*ToCapture); 6563 else 6564 return nullptr; 6565 } 6566 6567 SmallVector<Expr *, 8> InitCaptures(NumCaptures); 6568 if (ImportContainerChecked(LE->capture_inits(), InitCaptures)) 6569 return nullptr; 6570 6571 return LambdaExpr::Create(Importer.getToContext(), ToClass, 6572 Importer.Import(LE->getIntroducerRange()), 6573 LE->getCaptureDefault(), 6574 Importer.Import(LE->getCaptureDefaultLoc()), 6575 Captures, 6576 LE->hasExplicitParameters(), 6577 LE->hasExplicitResultType(), 6578 InitCaptures, 6579 Importer.Import(LE->getLocEnd()), 6580 LE->containsUnexpandedParameterPack()); 6581 } 6582 6583 Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) { 6584 QualType T = Importer.Import(ILE->getType()); 6585 if (T.isNull()) 6586 return nullptr; 6587 6588 SmallVector<Expr *, 4> Exprs(ILE->getNumInits()); 6589 if (ImportContainerChecked(ILE->inits(), Exprs)) 6590 return nullptr; 6591 6592 ASTContext &ToCtx = Importer.getToContext(); 6593 InitListExpr *To = new (ToCtx) InitListExpr( 6594 ToCtx, Importer.Import(ILE->getLBraceLoc()), 6595 Exprs, Importer.Import(ILE->getLBraceLoc())); 6596 To->setType(T); 6597 6598 if (ILE->hasArrayFiller()) { 6599 Expr *Filler = Importer.Import(ILE->getArrayFiller()); 6600 if (!Filler) 6601 return nullptr; 6602 To->setArrayFiller(Filler); 6603 } 6604 6605 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) { 6606 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD)); 6607 if (!ToFD) 6608 return nullptr; 6609 To->setInitializedFieldInUnion(ToFD); 6610 } 6611 6612 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) { 6613 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm)); 6614 if (!ToSyntForm) 6615 return nullptr; 6616 To->setSyntacticForm(ToSyntForm); 6617 } 6618 6619 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator()); 6620 To->setValueDependent(ILE->isValueDependent()); 6621 To->setInstantiationDependent(ILE->isInstantiationDependent()); 6622 6623 return To; 6624 } 6625 6626 Expr *ASTNodeImporter::VisitCXXStdInitializerListExpr( 6627 CXXStdInitializerListExpr *E) { 6628 QualType T = Importer.Import(E->getType()); 6629 if (T.isNull()) 6630 return nullptr; 6631 6632 Expr *SE = Importer.Import(E->getSubExpr()); 6633 if (!SE) 6634 return nullptr; 6635 6636 return new (Importer.getToContext()) CXXStdInitializerListExpr(T, SE); 6637 } 6638 6639 Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) { 6640 QualType ToType = Importer.Import(E->getType()); 6641 if (ToType.isNull()) 6642 return nullptr; 6643 6644 Expr *ToCommon = Importer.Import(E->getCommonExpr()); 6645 if (!ToCommon && E->getCommonExpr()) 6646 return nullptr; 6647 6648 Expr *ToSubExpr = Importer.Import(E->getSubExpr()); 6649 if (!ToSubExpr && E->getSubExpr()) 6650 return nullptr; 6651 6652 return new (Importer.getToContext()) 6653 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr); 6654 } 6655 6656 Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) { 6657 QualType ToType = Importer.Import(E->getType()); 6658 if (ToType.isNull()) 6659 return nullptr; 6660 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType); 6661 } 6662 6663 Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) { 6664 auto *ToField = dyn_cast_or_null<FieldDecl>(Importer.Import(DIE->getField())); 6665 if (!ToField && DIE->getField()) 6666 return nullptr; 6667 6668 return CXXDefaultInitExpr::Create( 6669 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField); 6670 } 6671 6672 Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { 6673 QualType ToType = Importer.Import(E->getType()); 6674 if (ToType.isNull() && !E->getType().isNull()) 6675 return nullptr; 6676 ExprValueKind VK = E->getValueKind(); 6677 CastKind CK = E->getCastKind(); 6678 Expr *ToOp = Importer.Import(E->getSubExpr()); 6679 if (!ToOp && E->getSubExpr()) 6680 return nullptr; 6681 CXXCastPath BasePath; 6682 if (ImportCastPath(E, BasePath)) 6683 return nullptr; 6684 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten()); 6685 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc()); 6686 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc()); 6687 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets()); 6688 6689 if (isa<CXXStaticCastExpr>(E)) { 6690 return CXXStaticCastExpr::Create( 6691 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, 6692 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); 6693 } else if (isa<CXXDynamicCastExpr>(E)) { 6694 return CXXDynamicCastExpr::Create( 6695 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, 6696 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); 6697 } else if (isa<CXXReinterpretCastExpr>(E)) { 6698 return CXXReinterpretCastExpr::Create( 6699 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, 6700 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); 6701 } else { 6702 return nullptr; 6703 } 6704 } 6705 6706 Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr( 6707 SubstNonTypeTemplateParmExpr *E) { 6708 QualType T = Importer.Import(E->getType()); 6709 if (T.isNull()) 6710 return nullptr; 6711 6712 auto *Param = cast_or_null<NonTypeTemplateParmDecl>( 6713 Importer.Import(E->getParameter())); 6714 if (!Param) 6715 return nullptr; 6716 6717 Expr *Replacement = Importer.Import(E->getReplacement()); 6718 if (!Replacement) 6719 return nullptr; 6720 6721 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr( 6722 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param, 6723 Replacement); 6724 } 6725 6726 Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) { 6727 QualType ToType = Importer.Import(E->getType()); 6728 if (ToType.isNull()) 6729 return nullptr; 6730 6731 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs()); 6732 if (ImportContainerChecked(E->getArgs(), ToArgs)) 6733 return nullptr; 6734 6735 // According to Sema::BuildTypeTrait(), if E is value-dependent, 6736 // Value is always false. 6737 bool ToValue = false; 6738 if (!E->isValueDependent()) 6739 ToValue = E->getValue(); 6740 6741 return TypeTraitExpr::Create( 6742 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()), 6743 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue); 6744 } 6745 6746 Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) { 6747 QualType ToType = Importer.Import(E->getType()); 6748 if (ToType.isNull()) 6749 return nullptr; 6750 6751 if (E->isTypeOperand()) { 6752 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo()); 6753 if (!TSI) 6754 return nullptr; 6755 6756 return new (Importer.getToContext()) 6757 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange())); 6758 } 6759 6760 Expr *Op = Importer.Import(E->getExprOperand()); 6761 if (!Op) 6762 return nullptr; 6763 6764 return new (Importer.getToContext()) 6765 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange())); 6766 } 6767 6768 void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod, 6769 CXXMethodDecl *FromMethod) { 6770 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) 6771 ToMethod->addOverriddenMethod( 6772 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>( 6773 FromOverriddenMethod)))); 6774 } 6775 6776 ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, 6777 ASTContext &FromContext, FileManager &FromFileManager, 6778 bool MinimalImport) 6779 : ToContext(ToContext), FromContext(FromContext), 6780 ToFileManager(ToFileManager), FromFileManager(FromFileManager), 6781 Minimal(MinimalImport) { 6782 ImportedDecls[FromContext.getTranslationUnitDecl()] 6783 = ToContext.getTranslationUnitDecl(); 6784 } 6785 6786 ASTImporter::~ASTImporter() = default; 6787 6788 QualType ASTImporter::Import(QualType FromT) { 6789 if (FromT.isNull()) 6790 return {}; 6791 6792 const Type *fromTy = FromT.getTypePtr(); 6793 6794 // Check whether we've already imported this type. 6795 llvm::DenseMap<const Type *, const Type *>::iterator Pos 6796 = ImportedTypes.find(fromTy); 6797 if (Pos != ImportedTypes.end()) 6798 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers()); 6799 6800 // Import the type 6801 ASTNodeImporter Importer(*this); 6802 QualType ToT = Importer.Visit(fromTy); 6803 if (ToT.isNull()) 6804 return ToT; 6805 6806 // Record the imported type. 6807 ImportedTypes[fromTy] = ToT.getTypePtr(); 6808 6809 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers()); 6810 } 6811 6812 TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { 6813 if (!FromTSI) 6814 return FromTSI; 6815 6816 // FIXME: For now we just create a "trivial" type source info based 6817 // on the type and a single location. Implement a real version of this. 6818 QualType T = Import(FromTSI->getType()); 6819 if (T.isNull()) 6820 return nullptr; 6821 6822 return ToContext.getTrivialTypeSourceInfo(T, 6823 Import(FromTSI->getTypeLoc().getLocStart())); 6824 } 6825 6826 Attr *ASTImporter::Import(const Attr *FromAttr) { 6827 Attr *ToAttr = FromAttr->clone(ToContext); 6828 ToAttr->setRange(Import(FromAttr->getRange())); 6829 return ToAttr; 6830 } 6831 6832 Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) { 6833 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); 6834 if (Pos != ImportedDecls.end()) { 6835 Decl *ToD = Pos->second; 6836 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD); 6837 return ToD; 6838 } else { 6839 return nullptr; 6840 } 6841 } 6842 6843 Decl *ASTImporter::Import(Decl *FromD) { 6844 if (!FromD) 6845 return nullptr; 6846 6847 ASTNodeImporter Importer(*this); 6848 6849 // Check whether we've already imported this declaration. 6850 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); 6851 if (Pos != ImportedDecls.end()) { 6852 Decl *ToD = Pos->second; 6853 Importer.ImportDefinitionIfNeeded(FromD, ToD); 6854 return ToD; 6855 } 6856 6857 // Import the type 6858 Decl *ToD = Importer.Visit(FromD); 6859 if (!ToD) 6860 return nullptr; 6861 6862 // Record the imported declaration. 6863 ImportedDecls[FromD] = ToD; 6864 ToD->IdentifierNamespace = FromD->IdentifierNamespace; 6865 return ToD; 6866 } 6867 6868 DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) { 6869 if (!FromDC) 6870 return FromDC; 6871 6872 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC))); 6873 if (!ToDC) 6874 return nullptr; 6875 6876 // When we're using a record/enum/Objective-C class/protocol as a context, we 6877 // need it to have a definition. 6878 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) { 6879 auto *FromRecord = cast<RecordDecl>(FromDC); 6880 if (ToRecord->isCompleteDefinition()) { 6881 // Do nothing. 6882 } else if (FromRecord->isCompleteDefinition()) { 6883 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord, 6884 ASTNodeImporter::IDK_Basic); 6885 } else { 6886 CompleteDecl(ToRecord); 6887 } 6888 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) { 6889 auto *FromEnum = cast<EnumDecl>(FromDC); 6890 if (ToEnum->isCompleteDefinition()) { 6891 // Do nothing. 6892 } else if (FromEnum->isCompleteDefinition()) { 6893 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum, 6894 ASTNodeImporter::IDK_Basic); 6895 } else { 6896 CompleteDecl(ToEnum); 6897 } 6898 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) { 6899 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC); 6900 if (ToClass->getDefinition()) { 6901 // Do nothing. 6902 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { 6903 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass, 6904 ASTNodeImporter::IDK_Basic); 6905 } else { 6906 CompleteDecl(ToClass); 6907 } 6908 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) { 6909 auto *FromProto = cast<ObjCProtocolDecl>(FromDC); 6910 if (ToProto->getDefinition()) { 6911 // Do nothing. 6912 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { 6913 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto, 6914 ASTNodeImporter::IDK_Basic); 6915 } else { 6916 CompleteDecl(ToProto); 6917 } 6918 } 6919 6920 return ToDC; 6921 } 6922 6923 Expr *ASTImporter::Import(Expr *FromE) { 6924 if (!FromE) 6925 return nullptr; 6926 6927 return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); 6928 } 6929 6930 Stmt *ASTImporter::Import(Stmt *FromS) { 6931 if (!FromS) 6932 return nullptr; 6933 6934 // Check whether we've already imported this declaration. 6935 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); 6936 if (Pos != ImportedStmts.end()) 6937 return Pos->second; 6938 6939 // Import the type 6940 ASTNodeImporter Importer(*this); 6941 Stmt *ToS = Importer.Visit(FromS); 6942 if (!ToS) 6943 return nullptr; 6944 6945 // Record the imported declaration. 6946 ImportedStmts[FromS] = ToS; 6947 return ToS; 6948 } 6949 6950 NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { 6951 if (!FromNNS) 6952 return nullptr; 6953 6954 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix()); 6955 6956 switch (FromNNS->getKind()) { 6957 case NestedNameSpecifier::Identifier: 6958 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) { 6959 return NestedNameSpecifier::Create(ToContext, prefix, II); 6960 } 6961 return nullptr; 6962 6963 case NestedNameSpecifier::Namespace: 6964 if (auto *NS = 6965 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) { 6966 return NestedNameSpecifier::Create(ToContext, prefix, NS); 6967 } 6968 return nullptr; 6969 6970 case NestedNameSpecifier::NamespaceAlias: 6971 if (auto *NSAD = 6972 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) { 6973 return NestedNameSpecifier::Create(ToContext, prefix, NSAD); 6974 } 6975 return nullptr; 6976 6977 case NestedNameSpecifier::Global: 6978 return NestedNameSpecifier::GlobalSpecifier(ToContext); 6979 6980 case NestedNameSpecifier::Super: 6981 if (auto *RD = 6982 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) { 6983 return NestedNameSpecifier::SuperSpecifier(ToContext, RD); 6984 } 6985 return nullptr; 6986 6987 case NestedNameSpecifier::TypeSpec: 6988 case NestedNameSpecifier::TypeSpecWithTemplate: { 6989 QualType T = Import(QualType(FromNNS->getAsType(), 0u)); 6990 if (!T.isNull()) { 6991 bool bTemplate = FromNNS->getKind() == 6992 NestedNameSpecifier::TypeSpecWithTemplate; 6993 return NestedNameSpecifier::Create(ToContext, prefix, 6994 bTemplate, T.getTypePtr()); 6995 } 6996 } 6997 return nullptr; 6998 } 6999 7000 llvm_unreachable("Invalid nested name specifier kind"); 7001 } 7002 7003 NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { 7004 // Copied from NestedNameSpecifier mostly. 7005 SmallVector<NestedNameSpecifierLoc , 8> NestedNames; 7006 NestedNameSpecifierLoc NNS = FromNNS; 7007 7008 // Push each of the nested-name-specifiers's onto a stack for 7009 // serialization in reverse order. 7010 while (NNS) { 7011 NestedNames.push_back(NNS); 7012 NNS = NNS.getPrefix(); 7013 } 7014 7015 NestedNameSpecifierLocBuilder Builder; 7016 7017 while (!NestedNames.empty()) { 7018 NNS = NestedNames.pop_back_val(); 7019 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier()); 7020 if (!Spec) 7021 return NestedNameSpecifierLoc(); 7022 7023 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind(); 7024 switch (Kind) { 7025 case NestedNameSpecifier::Identifier: 7026 Builder.Extend(getToContext(), 7027 Spec->getAsIdentifier(), 7028 Import(NNS.getLocalBeginLoc()), 7029 Import(NNS.getLocalEndLoc())); 7030 break; 7031 7032 case NestedNameSpecifier::Namespace: 7033 Builder.Extend(getToContext(), 7034 Spec->getAsNamespace(), 7035 Import(NNS.getLocalBeginLoc()), 7036 Import(NNS.getLocalEndLoc())); 7037 break; 7038 7039 case NestedNameSpecifier::NamespaceAlias: 7040 Builder.Extend(getToContext(), 7041 Spec->getAsNamespaceAlias(), 7042 Import(NNS.getLocalBeginLoc()), 7043 Import(NNS.getLocalEndLoc())); 7044 break; 7045 7046 case NestedNameSpecifier::TypeSpec: 7047 case NestedNameSpecifier::TypeSpecWithTemplate: { 7048 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo( 7049 QualType(Spec->getAsType(), 0)); 7050 Builder.Extend(getToContext(), 7051 Import(NNS.getLocalBeginLoc()), 7052 TSI->getTypeLoc(), 7053 Import(NNS.getLocalEndLoc())); 7054 break; 7055 } 7056 7057 case NestedNameSpecifier::Global: 7058 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc())); 7059 break; 7060 7061 case NestedNameSpecifier::Super: { 7062 SourceRange ToRange = Import(NNS.getSourceRange()); 7063 Builder.MakeSuper(getToContext(), 7064 Spec->getAsRecordDecl(), 7065 ToRange.getBegin(), 7066 ToRange.getEnd()); 7067 } 7068 } 7069 } 7070 7071 return Builder.getWithLocInContext(getToContext()); 7072 } 7073 7074 TemplateName ASTImporter::Import(TemplateName From) { 7075 switch (From.getKind()) { 7076 case TemplateName::Template: 7077 if (auto *ToTemplate = 7078 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) 7079 return TemplateName(ToTemplate); 7080 7081 return {}; 7082 7083 case TemplateName::OverloadedTemplate: { 7084 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); 7085 UnresolvedSet<2> ToTemplates; 7086 for (auto *I : *FromStorage) { 7087 if (auto *To = cast_or_null<NamedDecl>(Import(I))) 7088 ToTemplates.addDecl(To); 7089 else 7090 return {}; 7091 } 7092 return ToContext.getOverloadedTemplateName(ToTemplates.begin(), 7093 ToTemplates.end()); 7094 } 7095 7096 case TemplateName::QualifiedTemplate: { 7097 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); 7098 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier()); 7099 if (!Qualifier) 7100 return {}; 7101 7102 if (auto *ToTemplate = 7103 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) 7104 return ToContext.getQualifiedTemplateName(Qualifier, 7105 QTN->hasTemplateKeyword(), 7106 ToTemplate); 7107 7108 return {}; 7109 } 7110 7111 case TemplateName::DependentTemplate: { 7112 DependentTemplateName *DTN = From.getAsDependentTemplateName(); 7113 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier()); 7114 if (!Qualifier) 7115 return {}; 7116 7117 if (DTN->isIdentifier()) { 7118 return ToContext.getDependentTemplateName(Qualifier, 7119 Import(DTN->getIdentifier())); 7120 } 7121 7122 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator()); 7123 } 7124 7125 case TemplateName::SubstTemplateTemplateParm: { 7126 SubstTemplateTemplateParmStorage *subst 7127 = From.getAsSubstTemplateTemplateParm(); 7128 auto *param = 7129 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter())); 7130 if (!param) 7131 return {}; 7132 7133 TemplateName replacement = Import(subst->getReplacement()); 7134 if (replacement.isNull()) 7135 return {}; 7136 7137 return ToContext.getSubstTemplateTemplateParm(param, replacement); 7138 } 7139 7140 case TemplateName::SubstTemplateTemplateParmPack: { 7141 SubstTemplateTemplateParmPackStorage *SubstPack 7142 = From.getAsSubstTemplateTemplateParmPack(); 7143 auto *Param = 7144 cast_or_null<TemplateTemplateParmDecl>( 7145 Import(SubstPack->getParameterPack())); 7146 if (!Param) 7147 return {}; 7148 7149 ASTNodeImporter Importer(*this); 7150 TemplateArgument ArgPack 7151 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); 7152 if (ArgPack.isNull()) 7153 return {}; 7154 7155 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack); 7156 } 7157 } 7158 7159 llvm_unreachable("Invalid template name kind"); 7160 } 7161 7162 SourceLocation ASTImporter::Import(SourceLocation FromLoc) { 7163 if (FromLoc.isInvalid()) 7164 return {}; 7165 7166 SourceManager &FromSM = FromContext.getSourceManager(); 7167 7168 // For now, map everything down to its file location, so that we 7169 // don't have to import macro expansions. 7170 // FIXME: Import macro expansions! 7171 FromLoc = FromSM.getFileLoc(FromLoc); 7172 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); 7173 SourceManager &ToSM = ToContext.getSourceManager(); 7174 FileID ToFileID = Import(Decomposed.first); 7175 if (ToFileID.isInvalid()) 7176 return {}; 7177 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID) 7178 .getLocWithOffset(Decomposed.second); 7179 return ret; 7180 } 7181 7182 SourceRange ASTImporter::Import(SourceRange FromRange) { 7183 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); 7184 } 7185 7186 FileID ASTImporter::Import(FileID FromID) { 7187 llvm::DenseMap<FileID, FileID>::iterator Pos 7188 = ImportedFileIDs.find(FromID); 7189 if (Pos != ImportedFileIDs.end()) 7190 return Pos->second; 7191 7192 SourceManager &FromSM = FromContext.getSourceManager(); 7193 SourceManager &ToSM = ToContext.getSourceManager(); 7194 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); 7195 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet"); 7196 7197 // Include location of this file. 7198 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); 7199 7200 // Map the FileID for to the "to" source manager. 7201 FileID ToID; 7202 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); 7203 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { 7204 // FIXME: We probably want to use getVirtualFile(), so we don't hit the 7205 // disk again 7206 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather 7207 // than mmap the files several times. 7208 const FileEntry *Entry = 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 * 7216 FromBuf = 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 ImportedFileIDs[FromID] = ToID; 7225 return ToID; 7226 } 7227 7228 CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) { 7229 Expr *ToExpr = Import(From->getInit()); 7230 if (!ToExpr && From->getInit()) 7231 return nullptr; 7232 7233 if (From->isBaseInitializer()) { 7234 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo()); 7235 if (!ToTInfo && From->getTypeSourceInfo()) 7236 return nullptr; 7237 7238 return new (ToContext) CXXCtorInitializer( 7239 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()), 7240 ToExpr, Import(From->getRParenLoc()), 7241 From->isPackExpansion() ? Import(From->getEllipsisLoc()) 7242 : SourceLocation()); 7243 } else if (From->isMemberInitializer()) { 7244 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember())); 7245 if (!ToField && From->getMember()) 7246 return nullptr; 7247 7248 return new (ToContext) CXXCtorInitializer( 7249 ToContext, ToField, Import(From->getMemberLocation()), 7250 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc())); 7251 } else if (From->isIndirectMemberInitializer()) { 7252 auto *ToIField = cast_or_null<IndirectFieldDecl>( 7253 Import(From->getIndirectMember())); 7254 if (!ToIField && From->getIndirectMember()) 7255 return nullptr; 7256 7257 return new (ToContext) CXXCtorInitializer( 7258 ToContext, ToIField, Import(From->getMemberLocation()), 7259 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc())); 7260 } else if (From->isDelegatingInitializer()) { 7261 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo()); 7262 if (!ToTInfo && From->getTypeSourceInfo()) 7263 return nullptr; 7264 7265 return new (ToContext) 7266 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()), 7267 ToExpr, Import(From->getRParenLoc())); 7268 } else { 7269 return nullptr; 7270 } 7271 } 7272 7273 CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) { 7274 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec); 7275 if (Pos != ImportedCXXBaseSpecifiers.end()) 7276 return Pos->second; 7277 7278 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier( 7279 Import(BaseSpec->getSourceRange()), 7280 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(), 7281 BaseSpec->getAccessSpecifierAsWritten(), 7282 Import(BaseSpec->getTypeSourceInfo()), 7283 Import(BaseSpec->getEllipsisLoc())); 7284 ImportedCXXBaseSpecifiers[BaseSpec] = Imported; 7285 return Imported; 7286 } 7287 7288 void ASTImporter::ImportDefinition(Decl *From) { 7289 Decl *To = Import(From); 7290 if (!To) 7291 return; 7292 7293 if (auto *FromDC = cast<DeclContext>(From)) { 7294 ASTNodeImporter Importer(*this); 7295 7296 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) { 7297 if (!ToRecord->getDefinition()) { 7298 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord, 7299 ASTNodeImporter::IDK_Everything); 7300 return; 7301 } 7302 } 7303 7304 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) { 7305 if (!ToEnum->getDefinition()) { 7306 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum, 7307 ASTNodeImporter::IDK_Everything); 7308 return; 7309 } 7310 } 7311 7312 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) { 7313 if (!ToIFace->getDefinition()) { 7314 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace, 7315 ASTNodeImporter::IDK_Everything); 7316 return; 7317 } 7318 } 7319 7320 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) { 7321 if (!ToProto->getDefinition()) { 7322 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto, 7323 ASTNodeImporter::IDK_Everything); 7324 return; 7325 } 7326 } 7327 7328 Importer.ImportDeclContext(FromDC, true); 7329 } 7330 } 7331 7332 DeclarationName ASTImporter::Import(DeclarationName FromName) { 7333 if (!FromName) 7334 return {}; 7335 7336 switch (FromName.getNameKind()) { 7337 case DeclarationName::Identifier: 7338 return Import(FromName.getAsIdentifierInfo()); 7339 7340 case DeclarationName::ObjCZeroArgSelector: 7341 case DeclarationName::ObjCOneArgSelector: 7342 case DeclarationName::ObjCMultiArgSelector: 7343 return Import(FromName.getObjCSelector()); 7344 7345 case DeclarationName::CXXConstructorName: { 7346 QualType T = Import(FromName.getCXXNameType()); 7347 if (T.isNull()) 7348 return {}; 7349 7350 return ToContext.DeclarationNames.getCXXConstructorName( 7351 ToContext.getCanonicalType(T)); 7352 } 7353 7354 case DeclarationName::CXXDestructorName: { 7355 QualType T = Import(FromName.getCXXNameType()); 7356 if (T.isNull()) 7357 return {}; 7358 7359 return ToContext.DeclarationNames.getCXXDestructorName( 7360 ToContext.getCanonicalType(T)); 7361 } 7362 7363 case DeclarationName::CXXDeductionGuideName: { 7364 auto *Template = cast_or_null<TemplateDecl>( 7365 Import(FromName.getCXXDeductionGuideTemplate())); 7366 if (!Template) 7367 return {}; 7368 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template); 7369 } 7370 7371 case DeclarationName::CXXConversionFunctionName: { 7372 QualType T = Import(FromName.getCXXNameType()); 7373 if (T.isNull()) 7374 return {}; 7375 7376 return ToContext.DeclarationNames.getCXXConversionFunctionName( 7377 ToContext.getCanonicalType(T)); 7378 } 7379 7380 case DeclarationName::CXXOperatorName: 7381 return ToContext.DeclarationNames.getCXXOperatorName( 7382 FromName.getCXXOverloadedOperator()); 7383 7384 case DeclarationName::CXXLiteralOperatorName: 7385 return ToContext.DeclarationNames.getCXXLiteralOperatorName( 7386 Import(FromName.getCXXLiteralIdentifier())); 7387 7388 case DeclarationName::CXXUsingDirective: 7389 // FIXME: STATICS! 7390 return DeclarationName::getUsingDirectiveName(); 7391 } 7392 7393 llvm_unreachable("Invalid DeclarationName Kind!"); 7394 } 7395 7396 IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { 7397 if (!FromId) 7398 return nullptr; 7399 7400 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName()); 7401 7402 if (!ToId->getBuiltinID() && FromId->getBuiltinID()) 7403 ToId->setBuiltinID(FromId->getBuiltinID()); 7404 7405 return ToId; 7406 } 7407 7408 Selector ASTImporter::Import(Selector FromSel) { 7409 if (FromSel.isNull()) 7410 return {}; 7411 7412 SmallVector<IdentifierInfo *, 4> Idents; 7413 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); 7414 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) 7415 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); 7416 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); 7417 } 7418 7419 DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name, 7420 DeclContext *DC, 7421 unsigned IDNS, 7422 NamedDecl **Decls, 7423 unsigned NumDecls) { 7424 return Name; 7425 } 7426 7427 DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { 7428 if (LastDiagFromFrom) 7429 ToContext.getDiagnostics().notePriorDiagnosticFrom( 7430 FromContext.getDiagnostics()); 7431 LastDiagFromFrom = false; 7432 return ToContext.getDiagnostics().Report(Loc, DiagID); 7433 } 7434 7435 DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { 7436 if (!LastDiagFromFrom) 7437 FromContext.getDiagnostics().notePriorDiagnosticFrom( 7438 ToContext.getDiagnostics()); 7439 LastDiagFromFrom = true; 7440 return FromContext.getDiagnostics().Report(Loc, DiagID); 7441 } 7442 7443 void ASTImporter::CompleteDecl (Decl *D) { 7444 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) { 7445 if (!ID->getDefinition()) 7446 ID->startDefinition(); 7447 } 7448 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) { 7449 if (!PD->getDefinition()) 7450 PD->startDefinition(); 7451 } 7452 else if (auto *TD = dyn_cast<TagDecl>(D)) { 7453 if (!TD->getDefinition() && !TD->isBeingDefined()) { 7454 TD->startDefinition(); 7455 TD->setCompleteDefinition(true); 7456 } 7457 } 7458 else { 7459 assert(0 && "CompleteDecl called on a Decl that can't be completed"); 7460 } 7461 } 7462 7463 Decl *ASTImporter::Imported(Decl *From, Decl *To) { 7464 if (From->hasAttrs()) { 7465 for (const auto *FromAttr : From->getAttrs()) 7466 To->addAttr(Import(FromAttr)); 7467 } 7468 if (From->isUsed()) { 7469 To->setIsUsed(); 7470 } 7471 if (From->isImplicit()) { 7472 To->setImplicit(); 7473 } 7474 ImportedDecls[From] = To; 7475 return To; 7476 } 7477 7478 bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, 7479 bool Complain) { 7480 llvm::DenseMap<const Type *, const Type *>::iterator Pos 7481 = ImportedTypes.find(From.getTypePtr()); 7482 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To)) 7483 return true; 7484 7485 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls, 7486 false, Complain); 7487 return Ctx.IsStructurallyEquivalent(From, To); 7488 } 7489