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