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