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