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