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