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