| //===- ASTImporter.cpp - Importing ASTs from other Contexts ---------------===// |
| // |
| // The LLVM Compiler Infrastructure |
| // |
| // This file is distributed under the University of Illinois Open Source |
| // License. See LICENSE.TXT for details. |
| // |
| //===----------------------------------------------------------------------===// |
| // |
| // This file defines the ASTImporter class which imports AST nodes from one |
| // context into another context. |
| // |
| //===----------------------------------------------------------------------===// |
| |
| #include "clang/AST/ASTImporter.h" |
| #include "clang/AST/ASTContext.h" |
| #include "clang/AST/ASTDiagnostic.h" |
| #include "clang/AST/ASTStructuralEquivalence.h" |
| #include "clang/AST/Attr.h" |
| #include "clang/AST/Decl.h" |
| #include "clang/AST/DeclAccessPair.h" |
| #include "clang/AST/DeclBase.h" |
| #include "clang/AST/DeclCXX.h" |
| #include "clang/AST/DeclFriend.h" |
| #include "clang/AST/DeclGroup.h" |
| #include "clang/AST/DeclObjC.h" |
| #include "clang/AST/DeclTemplate.h" |
| #include "clang/AST/DeclVisitor.h" |
| #include "clang/AST/DeclarationName.h" |
| #include "clang/AST/Expr.h" |
| #include "clang/AST/ExprCXX.h" |
| #include "clang/AST/ExprObjC.h" |
| #include "clang/AST/ExternalASTSource.h" |
| #include "clang/AST/LambdaCapture.h" |
| #include "clang/AST/NestedNameSpecifier.h" |
| #include "clang/AST/OperationKinds.h" |
| #include "clang/AST/Stmt.h" |
| #include "clang/AST/StmtCXX.h" |
| #include "clang/AST/StmtObjC.h" |
| #include "clang/AST/StmtVisitor.h" |
| #include "clang/AST/TemplateBase.h" |
| #include "clang/AST/TemplateName.h" |
| #include "clang/AST/Type.h" |
| #include "clang/AST/TypeLoc.h" |
| #include "clang/AST/TypeVisitor.h" |
| #include "clang/AST/UnresolvedSet.h" |
| #include "clang/Basic/ExceptionSpecificationType.h" |
| #include "clang/Basic/FileManager.h" |
| #include "clang/Basic/IdentifierTable.h" |
| #include "clang/Basic/LLVM.h" |
| #include "clang/Basic/LangOptions.h" |
| #include "clang/Basic/SourceLocation.h" |
| #include "clang/Basic/SourceManager.h" |
| #include "clang/Basic/Specifiers.h" |
| #include "llvm/ADT/APSInt.h" |
| #include "llvm/ADT/ArrayRef.h" |
| #include "llvm/ADT/DenseMap.h" |
| #include "llvm/ADT/None.h" |
| #include "llvm/ADT/Optional.h" |
| #include "llvm/ADT/STLExtras.h" |
| #include "llvm/ADT/SmallVector.h" |
| #include "llvm/Support/Casting.h" |
| #include "llvm/Support/ErrorHandling.h" |
| #include "llvm/Support/MemoryBuffer.h" |
| #include <algorithm> |
| #include <cassert> |
| #include <cstddef> |
| #include <memory> |
| #include <type_traits> |
| #include <utility> |
| |
| namespace clang { |
| |
| template <class T> |
| SmallVector<Decl*, 2> |
| getCanonicalForwardRedeclChain(Redeclarable<T>* D) { |
| SmallVector<Decl*, 2> Redecls; |
| for (auto *R : D->getFirstDecl()->redecls()) { |
| if (R != D->getFirstDecl()) |
| Redecls.push_back(R); |
| } |
| Redecls.push_back(D->getFirstDecl()); |
| std::reverse(Redecls.begin(), Redecls.end()); |
| return Redecls; |
| } |
| |
| SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) { |
| // Currently only FunctionDecl is supported |
| auto FD = cast<FunctionDecl>(D); |
| return getCanonicalForwardRedeclChain<FunctionDecl>(FD); |
| } |
| |
| void updateFlags(const Decl *From, Decl *To) { |
| // Check if some flags or attrs are new in 'From' and copy into 'To'. |
| // FIXME: Other flags or attrs? |
| if (From->isUsed(false) && !To->isUsed(false)) |
| To->setIsUsed(); |
| } |
| |
| class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>, |
| public DeclVisitor<ASTNodeImporter, Decl *>, |
| public StmtVisitor<ASTNodeImporter, Stmt *> { |
| ASTImporter &Importer; |
| |
| // Wrapper for an overload set. |
| template <typename ToDeclT> struct CallOverloadedCreateFun { |
| template <typename... Args> |
| auto operator()(Args &&... args) |
| -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) { |
| return ToDeclT::Create(std::forward<Args>(args)...); |
| } |
| }; |
| |
| // Always use these functions to create a Decl during import. There are |
| // certain tasks which must be done after the Decl was created, e.g. we |
| // must immediately register that as an imported Decl. The parameter `ToD` |
| // will be set to the newly created Decl or if had been imported before |
| // then to the already imported Decl. Returns a bool value set to true if |
| // the `FromD` had been imported before. |
| template <typename ToDeclT, typename FromDeclT, typename... Args> |
| LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD, |
| Args &&... args) { |
| // There may be several overloads of ToDeclT::Create. We must make sure |
| // to call the one which would be chosen by the arguments, thus we use a |
| // wrapper for the overload set. |
| CallOverloadedCreateFun<ToDeclT> OC; |
| return GetImportedOrCreateSpecialDecl(ToD, OC, FromD, |
| std::forward<Args>(args)...); |
| } |
| // Use this overload if a special Type is needed to be created. E.g if we |
| // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl` |
| // then: |
| // TypedefNameDecl *ToTypedef; |
| // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...); |
| template <typename NewDeclT, typename ToDeclT, typename FromDeclT, |
| typename... Args> |
| LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD, |
| Args &&... args) { |
| CallOverloadedCreateFun<NewDeclT> OC; |
| return GetImportedOrCreateSpecialDecl(ToD, OC, FromD, |
| std::forward<Args>(args)...); |
| } |
| // Use this version if a special create function must be |
| // used, e.g. CXXRecordDecl::CreateLambda . |
| template <typename ToDeclT, typename CreateFunT, typename FromDeclT, |
| typename... Args> |
| LLVM_NODISCARD bool |
| GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun, |
| FromDeclT *FromD, Args &&... args) { |
| ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD)); |
| if (ToD) |
| return true; // Already imported. |
| ToD = CreateFun(std::forward<Args>(args)...); |
| InitializeImportedDecl(FromD, ToD); |
| return false; // A new Decl is created. |
| } |
| |
| void InitializeImportedDecl(Decl *FromD, Decl *ToD) { |
| Importer.MapImported(FromD, ToD); |
| ToD->IdentifierNamespace = FromD->IdentifierNamespace; |
| if (FromD->hasAttrs()) |
| for (const Attr *FromAttr : FromD->getAttrs()) |
| ToD->addAttr(Importer.Import(FromAttr)); |
| if (FromD->isUsed()) |
| ToD->setIsUsed(); |
| if (FromD->isImplicit()) |
| ToD->setImplicit(); |
| } |
| |
| public: |
| explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {} |
| |
| using TypeVisitor<ASTNodeImporter, QualType>::Visit; |
| using DeclVisitor<ASTNodeImporter, Decl *>::Visit; |
| using StmtVisitor<ASTNodeImporter, Stmt *>::Visit; |
| |
| // Importing types |
| QualType VisitType(const Type *T); |
| QualType VisitAtomicType(const AtomicType *T); |
| QualType VisitBuiltinType(const BuiltinType *T); |
| QualType VisitDecayedType(const DecayedType *T); |
| QualType VisitComplexType(const ComplexType *T); |
| QualType VisitPointerType(const PointerType *T); |
| QualType VisitBlockPointerType(const BlockPointerType *T); |
| QualType VisitLValueReferenceType(const LValueReferenceType *T); |
| QualType VisitRValueReferenceType(const RValueReferenceType *T); |
| QualType VisitMemberPointerType(const MemberPointerType *T); |
| QualType VisitConstantArrayType(const ConstantArrayType *T); |
| QualType VisitIncompleteArrayType(const IncompleteArrayType *T); |
| QualType VisitVariableArrayType(const VariableArrayType *T); |
| QualType VisitDependentSizedArrayType(const DependentSizedArrayType *T); |
| // FIXME: DependentSizedExtVectorType |
| QualType VisitVectorType(const VectorType *T); |
| QualType VisitExtVectorType(const ExtVectorType *T); |
| QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T); |
| QualType VisitFunctionProtoType(const FunctionProtoType *T); |
| QualType VisitUnresolvedUsingType(const UnresolvedUsingType *T); |
| QualType VisitParenType(const ParenType *T); |
| QualType VisitTypedefType(const TypedefType *T); |
| QualType VisitTypeOfExprType(const TypeOfExprType *T); |
| // FIXME: DependentTypeOfExprType |
| QualType VisitTypeOfType(const TypeOfType *T); |
| QualType VisitDecltypeType(const DecltypeType *T); |
| QualType VisitUnaryTransformType(const UnaryTransformType *T); |
| QualType VisitAutoType(const AutoType *T); |
| QualType VisitInjectedClassNameType(const InjectedClassNameType *T); |
| // FIXME: DependentDecltypeType |
| QualType VisitRecordType(const RecordType *T); |
| QualType VisitEnumType(const EnumType *T); |
| QualType VisitAttributedType(const AttributedType *T); |
| QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T); |
| QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T); |
| QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T); |
| QualType VisitElaboratedType(const ElaboratedType *T); |
| QualType VisitDependentNameType(const DependentNameType *T); |
| QualType VisitPackExpansionType(const PackExpansionType *T); |
| QualType VisitDependentTemplateSpecializationType( |
| const DependentTemplateSpecializationType *T); |
| QualType VisitObjCInterfaceType(const ObjCInterfaceType *T); |
| QualType VisitObjCObjectType(const ObjCObjectType *T); |
| QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T); |
| |
| // Importing declarations |
| bool ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| DeclContext *&LexicalDC, DeclarationName &Name, |
| NamedDecl *&ToD, SourceLocation &Loc); |
| void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); |
| void ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| DeclarationNameInfo& To); |
| void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); |
| void ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To); |
| |
| bool ImportCastPath(CastExpr *E, CXXCastPath &Path); |
| |
| using Designator = DesignatedInitExpr::Designator; |
| |
| Designator ImportDesignator(const Designator &D); |
| |
| Optional<LambdaCapture> ImportLambdaCapture(const LambdaCapture &From); |
| |
| /// What we should import from the definition. |
| enum ImportDefinitionKind { |
| /// Import the default subset of the definition, which might be |
| /// nothing (if minimal import is set) or might be everything (if minimal |
| /// import is not set). |
| IDK_Default, |
| |
| /// Import everything. |
| IDK_Everything, |
| |
| /// Import only the bare bones needed to establish a valid |
| /// DeclContext. |
| IDK_Basic |
| }; |
| |
| bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { |
| return IDK == IDK_Everything || |
| (IDK == IDK_Default && !Importer.isMinimalImport()); |
| } |
| |
| bool ImportDefinition(RecordDecl *From, RecordDecl *To, |
| ImportDefinitionKind Kind = IDK_Default); |
| bool ImportDefinition(VarDecl *From, VarDecl *To, |
| ImportDefinitionKind Kind = IDK_Default); |
| bool ImportDefinition(EnumDecl *From, EnumDecl *To, |
| ImportDefinitionKind Kind = IDK_Default); |
| bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, |
| ImportDefinitionKind Kind = IDK_Default); |
| bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To, |
| ImportDefinitionKind Kind = IDK_Default); |
| TemplateParameterList *ImportTemplateParameterList( |
| TemplateParameterList *Params); |
| TemplateArgument ImportTemplateArgument(const TemplateArgument &From); |
| Optional<TemplateArgumentLoc> ImportTemplateArgumentLoc( |
| const TemplateArgumentLoc &TALoc); |
| bool ImportTemplateArguments(const TemplateArgument *FromArgs, |
| unsigned NumFromArgs, |
| SmallVectorImpl<TemplateArgument> &ToArgs); |
| |
| template <typename InContainerTy> |
| bool ImportTemplateArgumentListInfo(const InContainerTy &Container, |
| TemplateArgumentListInfo &ToTAInfo); |
| |
| template<typename InContainerTy> |
| bool ImportTemplateArgumentListInfo(SourceLocation FromLAngleLoc, |
| SourceLocation FromRAngleLoc, |
| const InContainerTy &Container, |
| TemplateArgumentListInfo &Result); |
| |
| using TemplateArgsTy = SmallVector<TemplateArgument, 8>; |
| using OptionalTemplateArgsTy = Optional<TemplateArgsTy>; |
| std::tuple<FunctionTemplateDecl *, OptionalTemplateArgsTy> |
| ImportFunctionTemplateWithTemplateArgsFromSpecialization( |
| FunctionDecl *FromFD); |
| |
| bool ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD); |
| |
| bool IsStructuralMatch(Decl *From, Decl *To, bool Complain); |
| bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord, |
| bool Complain = true); |
| bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, |
| bool Complain = true); |
| bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord); |
| bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC); |
| bool IsStructuralMatch(FunctionTemplateDecl *From, |
| FunctionTemplateDecl *To); |
| bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To); |
| bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To); |
| bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To); |
| Decl *VisitDecl(Decl *D); |
| Decl *VisitEmptyDecl(EmptyDecl *D); |
| Decl *VisitAccessSpecDecl(AccessSpecDecl *D); |
| Decl *VisitStaticAssertDecl(StaticAssertDecl *D); |
| Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| Decl *VisitNamespaceDecl(NamespaceDecl *D); |
| Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
| Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); |
| Decl *VisitTypedefDecl(TypedefDecl *D); |
| Decl *VisitTypeAliasDecl(TypeAliasDecl *D); |
| Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D); |
| Decl *VisitLabelDecl(LabelDecl *D); |
| Decl *VisitEnumDecl(EnumDecl *D); |
| Decl *VisitRecordDecl(RecordDecl *D); |
| Decl *VisitEnumConstantDecl(EnumConstantDecl *D); |
| Decl *VisitFunctionDecl(FunctionDecl *D); |
| Decl *VisitCXXMethodDecl(CXXMethodDecl *D); |
| Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); |
| Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); |
| Decl *VisitCXXConversionDecl(CXXConversionDecl *D); |
| Decl *VisitFieldDecl(FieldDecl *D); |
| Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D); |
| Decl *VisitFriendDecl(FriendDecl *D); |
| Decl *VisitObjCIvarDecl(ObjCIvarDecl *D); |
| Decl *VisitVarDecl(VarDecl *D); |
| Decl *VisitImplicitParamDecl(ImplicitParamDecl *D); |
| Decl *VisitParmVarDecl(ParmVarDecl *D); |
| Decl *VisitObjCMethodDecl(ObjCMethodDecl *D); |
| Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); |
| Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
| Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
| Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D); |
| Decl *VisitUsingDecl(UsingDecl *D); |
| Decl *VisitUsingShadowDecl(UsingShadowDecl *D); |
| Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
| Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); |
| Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); |
| |
| ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list); |
| Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
| Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
| Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
| Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
| Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); |
| Decl *VisitClassTemplateSpecializationDecl( |
| ClassTemplateSpecializationDecl *D); |
| Decl *VisitVarTemplateDecl(VarTemplateDecl *D); |
| Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); |
| Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
| |
| // Importing statements |
| DeclGroupRef ImportDeclGroup(DeclGroupRef DG); |
| |
| Stmt *VisitStmt(Stmt *S); |
| Stmt *VisitGCCAsmStmt(GCCAsmStmt *S); |
| Stmt *VisitDeclStmt(DeclStmt *S); |
| Stmt *VisitNullStmt(NullStmt *S); |
| Stmt *VisitCompoundStmt(CompoundStmt *S); |
| Stmt *VisitCaseStmt(CaseStmt *S); |
| Stmt *VisitDefaultStmt(DefaultStmt *S); |
| Stmt *VisitLabelStmt(LabelStmt *S); |
| Stmt *VisitAttributedStmt(AttributedStmt *S); |
| Stmt *VisitIfStmt(IfStmt *S); |
| Stmt *VisitSwitchStmt(SwitchStmt *S); |
| Stmt *VisitWhileStmt(WhileStmt *S); |
| Stmt *VisitDoStmt(DoStmt *S); |
| Stmt *VisitForStmt(ForStmt *S); |
| Stmt *VisitGotoStmt(GotoStmt *S); |
| Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S); |
| Stmt *VisitContinueStmt(ContinueStmt *S); |
| Stmt *VisitBreakStmt(BreakStmt *S); |
| Stmt *VisitReturnStmt(ReturnStmt *S); |
| // FIXME: MSAsmStmt |
| // FIXME: SEHExceptStmt |
| // FIXME: SEHFinallyStmt |
| // FIXME: SEHTryStmt |
| // FIXME: SEHLeaveStmt |
| // FIXME: CapturedStmt |
| Stmt *VisitCXXCatchStmt(CXXCatchStmt *S); |
| Stmt *VisitCXXTryStmt(CXXTryStmt *S); |
| Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S); |
| // FIXME: MSDependentExistsStmt |
| Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); |
| Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); |
| Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S); |
| Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S); |
| Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); |
| Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); |
| |
| // Importing expressions |
| Expr *VisitExpr(Expr *E); |
| Expr *VisitVAArgExpr(VAArgExpr *E); |
| Expr *VisitGNUNullExpr(GNUNullExpr *E); |
| Expr *VisitPredefinedExpr(PredefinedExpr *E); |
| Expr *VisitDeclRefExpr(DeclRefExpr *E); |
| Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE); |
| Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E); |
| Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); |
| Expr *VisitIntegerLiteral(IntegerLiteral *E); |
| Expr *VisitFloatingLiteral(FloatingLiteral *E); |
| Expr *VisitCharacterLiteral(CharacterLiteral *E); |
| Expr *VisitStringLiteral(StringLiteral *E); |
| Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| Expr *VisitAtomicExpr(AtomicExpr *E); |
| Expr *VisitAddrLabelExpr(AddrLabelExpr *E); |
| Expr *VisitParenExpr(ParenExpr *E); |
| Expr *VisitParenListExpr(ParenListExpr *E); |
| Expr *VisitStmtExpr(StmtExpr *E); |
| Expr *VisitUnaryOperator(UnaryOperator *E); |
| Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
| Expr *VisitBinaryOperator(BinaryOperator *E); |
| Expr *VisitConditionalOperator(ConditionalOperator *E); |
| Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E); |
| Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E); |
| Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); |
| Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E); |
| Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
| Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E); |
| Expr *VisitImplicitCastExpr(ImplicitCastExpr *E); |
| Expr *VisitExplicitCastExpr(ExplicitCastExpr *E); |
| Expr *VisitOffsetOfExpr(OffsetOfExpr *OE); |
| Expr *VisitCXXThrowExpr(CXXThrowExpr *E); |
| Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E); |
| Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E); |
| Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
| Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); |
| Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE); |
| Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E); |
| Expr *VisitPackExpansionExpr(PackExpansionExpr *E); |
| Expr *VisitSizeOfPackExpr(SizeOfPackExpr *E); |
| Expr *VisitCXXNewExpr(CXXNewExpr *CE); |
| Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E); |
| Expr *VisitCXXConstructExpr(CXXConstructExpr *E); |
| Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E); |
| Expr *VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); |
| Expr *VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); |
| Expr *VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *CE); |
| Expr *VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E); |
| Expr *VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E); |
| Expr *VisitExprWithCleanups(ExprWithCleanups *EWC); |
| Expr *VisitCXXThisExpr(CXXThisExpr *E); |
| Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); |
| Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); |
| Expr *VisitMemberExpr(MemberExpr *E); |
| Expr *VisitCallExpr(CallExpr *E); |
| Expr *VisitLambdaExpr(LambdaExpr *LE); |
| Expr *VisitInitListExpr(InitListExpr *E); |
| Expr *VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E); |
| Expr *VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E); |
| Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E); |
| Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E); |
| Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E); |
| Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E); |
| Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E); |
| Expr *VisitTypeTraitExpr(TypeTraitExpr *E); |
| Expr *VisitCXXTypeidExpr(CXXTypeidExpr *E); |
| |
| template<typename IIter, typename OIter> |
| void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) { |
| using ItemT = typename std::remove_reference<decltype(*Obegin)>::type; |
| |
| ASTImporter &ImporterRef = Importer; |
| std::transform(Ibegin, Iend, Obegin, |
| [&ImporterRef](ItemT From) -> ItemT { |
| return ImporterRef.Import(From); |
| }); |
| } |
| |
| template<typename IIter, typename OIter> |
| bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) { |
| using ItemT = typename std::remove_reference<decltype(**Obegin)>::type; |
| |
| ASTImporter &ImporterRef = Importer; |
| bool Failed = false; |
| std::transform(Ibegin, Iend, Obegin, |
| [&ImporterRef, &Failed](ItemT *From) -> ItemT * { |
| auto *To = cast_or_null<ItemT>(ImporterRef.Import(From)); |
| if (!To && From) |
| Failed = true; |
| return To; |
| }); |
| return Failed; |
| } |
| |
| template<typename InContainerTy, typename OutContainerTy> |
| bool ImportContainerChecked(const InContainerTy &InContainer, |
| OutContainerTy &OutContainer) { |
| return ImportArrayChecked(InContainer.begin(), InContainer.end(), |
| OutContainer.begin()); |
| } |
| |
| template<typename InContainerTy, typename OIter> |
| bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) { |
| return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin); |
| } |
| |
| // Importing overrides. |
| void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod); |
| |
| FunctionDecl *FindFunctionTemplateSpecialization(FunctionDecl *FromFD); |
| }; |
| |
| template <typename InContainerTy> |
| bool ASTNodeImporter::ImportTemplateArgumentListInfo( |
| SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc, |
| const InContainerTy &Container, TemplateArgumentListInfo &Result) { |
| TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc), |
| Importer.Import(FromRAngleLoc)); |
| if (ImportTemplateArgumentListInfo(Container, ToTAInfo)) |
| return true; |
| Result = ToTAInfo; |
| return false; |
| } |
| |
| template <> |
| bool ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>( |
| const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) { |
| return ImportTemplateArgumentListInfo( |
| From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result); |
| } |
| |
| template <> |
| bool ASTNodeImporter::ImportTemplateArgumentListInfo< |
| ASTTemplateArgumentListInfo>(const ASTTemplateArgumentListInfo &From, |
| TemplateArgumentListInfo &Result) { |
| return ImportTemplateArgumentListInfo(From.LAngleLoc, From.RAngleLoc, |
| From.arguments(), Result); |
| } |
| |
| std::tuple<FunctionTemplateDecl *, ASTNodeImporter::OptionalTemplateArgsTy> |
| ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization( |
| FunctionDecl *FromFD) { |
| assert(FromFD->getTemplatedKind() == |
| FunctionDecl::TK_FunctionTemplateSpecialization); |
| auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); |
| auto *Template = cast_or_null<FunctionTemplateDecl>( |
| Importer.Import(FTSInfo->getTemplate())); |
| |
| // Import template arguments. |
| auto TemplArgs = FTSInfo->TemplateArguments->asArray(); |
| TemplateArgsTy ToTemplArgs; |
| if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(), |
| ToTemplArgs)) // Error during import. |
| return std::make_tuple(Template, OptionalTemplateArgsTy()); |
| |
| return std::make_tuple(Template, ToTemplArgs); |
| } |
| |
| } // namespace clang |
| |
| //---------------------------------------------------------------------------- |
| // Import Types |
| //---------------------------------------------------------------------------- |
| |
| using namespace clang; |
| |
| QualType ASTNodeImporter::VisitType(const Type *T) { |
| Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) |
| << T->getTypeClassName(); |
| return {}; |
| } |
| |
| QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){ |
| QualType UnderlyingType = Importer.Import(T->getValueType()); |
| if(UnderlyingType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getAtomicType(UnderlyingType); |
| } |
| |
| QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { |
| switch (T->getKind()) { |
| #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| case BuiltinType::Id: \ |
| return Importer.getToContext().SingletonId; |
| #include "clang/Basic/OpenCLImageTypes.def" |
| #define SHARED_SINGLETON_TYPE(Expansion) |
| #define BUILTIN_TYPE(Id, SingletonId) \ |
| case BuiltinType::Id: return Importer.getToContext().SingletonId; |
| #include "clang/AST/BuiltinTypes.def" |
| |
| // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" |
| // context supports C++. |
| |
| // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" |
| // context supports ObjC. |
| |
| case BuiltinType::Char_U: |
| // The context we're importing from has an unsigned 'char'. If we're |
| // importing into a context with a signed 'char', translate to |
| // 'unsigned char' instead. |
| if (Importer.getToContext().getLangOpts().CharIsSigned) |
| return Importer.getToContext().UnsignedCharTy; |
| |
| return Importer.getToContext().CharTy; |
| |
| case BuiltinType::Char_S: |
| // The context we're importing from has an unsigned 'char'. If we're |
| // importing into a context with a signed 'char', translate to |
| // 'unsigned char' instead. |
| if (!Importer.getToContext().getLangOpts().CharIsSigned) |
| return Importer.getToContext().SignedCharTy; |
| |
| return Importer.getToContext().CharTy; |
| |
| case BuiltinType::WChar_S: |
| case BuiltinType::WChar_U: |
| // FIXME: If not in C++, shall we translate to the C equivalent of |
| // wchar_t? |
| return Importer.getToContext().WCharTy; |
| } |
| |
| llvm_unreachable("Invalid BuiltinType Kind!"); |
| } |
| |
| QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) { |
| QualType OrigT = Importer.Import(T->getOriginalType()); |
| if (OrigT.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getDecayedType(OrigT); |
| } |
| |
| QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) { |
| QualType ToElementType = Importer.Import(T->getElementType()); |
| if (ToElementType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getComplexType(ToElementType); |
| } |
| |
| QualType ASTNodeImporter::VisitPointerType(const PointerType *T) { |
| QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| if (ToPointeeType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getPointerType(ToPointeeType); |
| } |
| |
| QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { |
| // FIXME: Check for blocks support in "to" context. |
| QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| if (ToPointeeType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getBlockPointerType(ToPointeeType); |
| } |
| |
| QualType |
| ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { |
| // FIXME: Check for C++ support in "to" context. |
| QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| if (ToPointeeType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getLValueReferenceType(ToPointeeType); |
| } |
| |
| QualType |
| ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { |
| // FIXME: Check for C++0x support in "to" context. |
| QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| if (ToPointeeType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getRValueReferenceType(ToPointeeType); |
| } |
| |
| QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { |
| // FIXME: Check for C++ support in "to" context. |
| QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| if (ToPointeeType.isNull()) |
| return {}; |
| |
| QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); |
| return Importer.getToContext().getMemberPointerType(ToPointeeType, |
| ClassType.getTypePtr()); |
| } |
| |
| QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { |
| QualType ToElementType = Importer.Import(T->getElementType()); |
| if (ToElementType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getConstantArrayType(ToElementType, |
| T->getSize(), |
| T->getSizeModifier(), |
| T->getIndexTypeCVRQualifiers()); |
| } |
| |
| QualType |
| ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
| QualType ToElementType = Importer.Import(T->getElementType()); |
| if (ToElementType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getIncompleteArrayType(ToElementType, |
| T->getSizeModifier(), |
| T->getIndexTypeCVRQualifiers()); |
| } |
| |
| QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { |
| QualType ToElementType = Importer.Import(T->getElementType()); |
| if (ToElementType.isNull()) |
| return {}; |
| |
| Expr *Size = Importer.Import(T->getSizeExpr()); |
| if (!Size) |
| return {}; |
| |
| SourceRange Brackets = Importer.Import(T->getBracketsRange()); |
| return Importer.getToContext().getVariableArrayType(ToElementType, Size, |
| T->getSizeModifier(), |
| T->getIndexTypeCVRQualifiers(), |
| Brackets); |
| } |
| |
| QualType ASTNodeImporter::VisitDependentSizedArrayType( |
| const DependentSizedArrayType *T) { |
| QualType ToElementType = Importer.Import(T->getElementType()); |
| if (ToElementType.isNull()) |
| return {}; |
| |
| // SizeExpr may be null if size is not specified directly. |
| // For example, 'int a[]'. |
| Expr *Size = Importer.Import(T->getSizeExpr()); |
| if (!Size && T->getSizeExpr()) |
| return {}; |
| |
| SourceRange Brackets = Importer.Import(T->getBracketsRange()); |
| return Importer.getToContext().getDependentSizedArrayType( |
| ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(), |
| Brackets); |
| } |
| |
| QualType ASTNodeImporter::VisitVectorType(const VectorType *T) { |
| QualType ToElementType = Importer.Import(T->getElementType()); |
| if (ToElementType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getVectorType(ToElementType, |
| T->getNumElements(), |
| T->getVectorKind()); |
| } |
| |
| QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { |
| QualType ToElementType = Importer.Import(T->getElementType()); |
| if (ToElementType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getExtVectorType(ToElementType, |
| T->getNumElements()); |
| } |
| |
| QualType |
| ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
| // FIXME: What happens if we're importing a function without a prototype |
| // into C++? Should we make it variadic? |
| QualType ToResultType = Importer.Import(T->getReturnType()); |
| if (ToResultType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getFunctionNoProtoType(ToResultType, |
| T->getExtInfo()); |
| } |
| |
| QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { |
| QualType ToResultType = Importer.Import(T->getReturnType()); |
| if (ToResultType.isNull()) |
| return {}; |
| |
| // Import argument types |
| SmallVector<QualType, 4> ArgTypes; |
| for (const auto &A : T->param_types()) { |
| QualType ArgType = Importer.Import(A); |
| if (ArgType.isNull()) |
| return {}; |
| ArgTypes.push_back(ArgType); |
| } |
| |
| // Import exception types |
| SmallVector<QualType, 4> ExceptionTypes; |
| for (const auto &E : T->exceptions()) { |
| QualType ExceptionType = Importer.Import(E); |
| if (ExceptionType.isNull()) |
| return {}; |
| ExceptionTypes.push_back(ExceptionType); |
| } |
| |
| FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); |
| FunctionProtoType::ExtProtoInfo ToEPI; |
| |
| ToEPI.ExtInfo = FromEPI.ExtInfo; |
| ToEPI.Variadic = FromEPI.Variadic; |
| ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; |
| ToEPI.TypeQuals = FromEPI.TypeQuals; |
| ToEPI.RefQualifier = FromEPI.RefQualifier; |
| ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; |
| ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; |
| ToEPI.ExceptionSpec.NoexceptExpr = |
| Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr); |
| ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>( |
| Importer.Import(FromEPI.ExceptionSpec.SourceDecl)); |
| ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>( |
| Importer.Import(FromEPI.ExceptionSpec.SourceTemplate)); |
| |
| return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI); |
| } |
| |
| QualType ASTNodeImporter::VisitUnresolvedUsingType( |
| const UnresolvedUsingType *T) { |
| const auto *ToD = |
| cast_or_null<UnresolvedUsingTypenameDecl>(Importer.Import(T->getDecl())); |
| if (!ToD) |
| return {}; |
| |
| auto *ToPrevD = |
| cast_or_null<UnresolvedUsingTypenameDecl>( |
| Importer.Import(T->getDecl()->getPreviousDecl())); |
| if (!ToPrevD && T->getDecl()->getPreviousDecl()) |
| return {}; |
| |
| return Importer.getToContext().getTypeDeclType(ToD, ToPrevD); |
| } |
| |
| QualType ASTNodeImporter::VisitParenType(const ParenType *T) { |
| QualType ToInnerType = Importer.Import(T->getInnerType()); |
| if (ToInnerType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getParenType(ToInnerType); |
| } |
| |
| QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { |
| auto *ToDecl = |
| dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl())); |
| if (!ToDecl) |
| return {}; |
| |
| return Importer.getToContext().getTypeDeclType(ToDecl); |
| } |
| |
| QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { |
| Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| if (!ToExpr) |
| return {}; |
| |
| return Importer.getToContext().getTypeOfExprType(ToExpr); |
| } |
| |
| QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { |
| QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| if (ToUnderlyingType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getTypeOfType(ToUnderlyingType); |
| } |
| |
| QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { |
| // FIXME: Make sure that the "to" context supports C++0x! |
| Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| if (!ToExpr) |
| return {}; |
| |
| QualType UnderlyingType = Importer.Import(T->getUnderlyingType()); |
| if (UnderlyingType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType); |
| } |
| |
| QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { |
| QualType ToBaseType = Importer.Import(T->getBaseType()); |
| QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| if (ToBaseType.isNull() || ToUnderlyingType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getUnaryTransformType(ToBaseType, |
| ToUnderlyingType, |
| T->getUTTKind()); |
| } |
| |
| QualType ASTNodeImporter::VisitAutoType(const AutoType *T) { |
| // FIXME: Make sure that the "to" context supports C++11! |
| QualType FromDeduced = T->getDeducedType(); |
| QualType ToDeduced; |
| if (!FromDeduced.isNull()) { |
| ToDeduced = Importer.Import(FromDeduced); |
| if (ToDeduced.isNull()) |
| return {}; |
| } |
| |
| return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(), |
| /*IsDependent*/false); |
| } |
| |
| QualType ASTNodeImporter::VisitInjectedClassNameType( |
| const InjectedClassNameType *T) { |
| auto *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl())); |
| if (!D) |
| return {}; |
| |
| QualType InjType = Importer.Import(T->getInjectedSpecializationType()); |
| if (InjType.isNull()) |
| return {}; |
| |
| // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading |
| // See comments in InjectedClassNameType definition for details |
| // return Importer.getToContext().getInjectedClassNameType(D, InjType); |
| enum { |
| TypeAlignmentInBits = 4, |
| TypeAlignment = 1 << TypeAlignmentInBits |
| }; |
| |
| return QualType(new (Importer.getToContext(), TypeAlignment) |
| InjectedClassNameType(D, InjType), 0); |
| } |
| |
| QualType ASTNodeImporter::VisitRecordType(const RecordType *T) { |
| auto *ToDecl = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); |
| if (!ToDecl) |
| return {}; |
| |
| return Importer.getToContext().getTagDeclType(ToDecl); |
| } |
| |
| QualType ASTNodeImporter::VisitEnumType(const EnumType *T) { |
| auto *ToDecl = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl())); |
| if (!ToDecl) |
| return {}; |
| |
| return Importer.getToContext().getTagDeclType(ToDecl); |
| } |
| |
| QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { |
| QualType FromModifiedType = T->getModifiedType(); |
| QualType FromEquivalentType = T->getEquivalentType(); |
| QualType ToModifiedType; |
| QualType ToEquivalentType; |
| |
| if (!FromModifiedType.isNull()) { |
| ToModifiedType = Importer.Import(FromModifiedType); |
| if (ToModifiedType.isNull()) |
| return {}; |
| } |
| if (!FromEquivalentType.isNull()) { |
| ToEquivalentType = Importer.Import(FromEquivalentType); |
| if (ToEquivalentType.isNull()) |
| return {}; |
| } |
| |
| return Importer.getToContext().getAttributedType(T->getAttrKind(), |
| ToModifiedType, ToEquivalentType); |
| } |
| |
| QualType ASTNodeImporter::VisitTemplateTypeParmType( |
| const TemplateTypeParmType *T) { |
| auto *ParmDecl = |
| cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl())); |
| if (!ParmDecl && T->getDecl()) |
| return {}; |
| |
| return Importer.getToContext().getTemplateTypeParmType( |
| T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl); |
| } |
| |
| QualType ASTNodeImporter::VisitSubstTemplateTypeParmType( |
| const SubstTemplateTypeParmType *T) { |
| const auto *Replaced = |
| cast_or_null<TemplateTypeParmType>(Importer.Import( |
| QualType(T->getReplacedParameter(), 0)).getTypePtr()); |
| if (!Replaced) |
| return {}; |
| |
| QualType Replacement = Importer.Import(T->getReplacementType()); |
| if (Replacement.isNull()) |
| return {}; |
| Replacement = Replacement.getCanonicalType(); |
| |
| return Importer.getToContext().getSubstTemplateTypeParmType( |
| Replaced, Replacement); |
| } |
| |
| QualType ASTNodeImporter::VisitTemplateSpecializationType( |
| const TemplateSpecializationType *T) { |
| TemplateName ToTemplate = Importer.Import(T->getTemplateName()); |
| if (ToTemplate.isNull()) |
| return {}; |
| |
| SmallVector<TemplateArgument, 2> ToTemplateArgs; |
| if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs)) |
| return {}; |
| |
| QualType ToCanonType; |
| if (!QualType(T, 0).isCanonical()) { |
| QualType FromCanonType |
| = Importer.getFromContext().getCanonicalType(QualType(T, 0)); |
| ToCanonType =Importer.Import(FromCanonType); |
| if (ToCanonType.isNull()) |
| return {}; |
| } |
| return Importer.getToContext().getTemplateSpecializationType(ToTemplate, |
| ToTemplateArgs, |
| ToCanonType); |
| } |
| |
| QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { |
| NestedNameSpecifier *ToQualifier = nullptr; |
| // Note: the qualifier in an ElaboratedType is optional. |
| if (T->getQualifier()) { |
| ToQualifier = Importer.Import(T->getQualifier()); |
| if (!ToQualifier) |
| return {}; |
| } |
| |
| QualType ToNamedType = Importer.Import(T->getNamedType()); |
| if (ToNamedType.isNull()) |
| return {}; |
| |
| TagDecl *OwnedTagDecl = |
| cast_or_null<TagDecl>(Importer.Import(T->getOwnedTagDecl())); |
| if (!OwnedTagDecl && T->getOwnedTagDecl()) |
| return {}; |
| |
| return Importer.getToContext().getElaboratedType(T->getKeyword(), |
| ToQualifier, ToNamedType, |
| OwnedTagDecl); |
| } |
| |
| QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) { |
| QualType Pattern = Importer.Import(T->getPattern()); |
| if (Pattern.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getPackExpansionType(Pattern, |
| T->getNumExpansions()); |
| } |
| |
| QualType ASTNodeImporter::VisitDependentTemplateSpecializationType( |
| const DependentTemplateSpecializationType *T) { |
| NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier()); |
| if (!Qualifier && T->getQualifier()) |
| return {}; |
| |
| IdentifierInfo *Name = Importer.Import(T->getIdentifier()); |
| if (!Name && T->getIdentifier()) |
| return {}; |
| |
| SmallVector<TemplateArgument, 2> ToPack; |
| ToPack.reserve(T->getNumArgs()); |
| if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack)) |
| return {}; |
| |
| return Importer.getToContext().getDependentTemplateSpecializationType( |
| T->getKeyword(), Qualifier, Name, ToPack); |
| } |
| |
| QualType ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) { |
| NestedNameSpecifier *NNS = Importer.Import(T->getQualifier()); |
| if (!NNS && T->getQualifier()) |
| return QualType(); |
| |
| IdentifierInfo *Name = Importer.Import(T->getIdentifier()); |
| if (!Name && T->getIdentifier()) |
| return QualType(); |
| |
| QualType Canon = (T == T->getCanonicalTypeInternal().getTypePtr()) |
| ? QualType() |
| : Importer.Import(T->getCanonicalTypeInternal()); |
| if (!Canon.isNull()) |
| Canon = Canon.getCanonicalType(); |
| |
| return Importer.getToContext().getDependentNameType(T->getKeyword(), NNS, |
| Name, Canon); |
| } |
| |
| QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
| auto *Class = |
| dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl())); |
| if (!Class) |
| return {}; |
| |
| return Importer.getToContext().getObjCInterfaceType(Class); |
| } |
| |
| QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { |
| QualType ToBaseType = Importer.Import(T->getBaseType()); |
| if (ToBaseType.isNull()) |
| return {}; |
| |
| SmallVector<QualType, 4> TypeArgs; |
| for (auto TypeArg : T->getTypeArgsAsWritten()) { |
| QualType ImportedTypeArg = Importer.Import(TypeArg); |
| if (ImportedTypeArg.isNull()) |
| return {}; |
| |
| TypeArgs.push_back(ImportedTypeArg); |
| } |
| |
| SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| for (auto *P : T->quals()) { |
| auto *Protocol = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P)); |
| if (!Protocol) |
| return {}; |
| Protocols.push_back(Protocol); |
| } |
| |
| return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs, |
| Protocols, |
| T->isKindOfTypeAsWritten()); |
| } |
| |
| QualType |
| ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
| QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| if (ToPointeeType.isNull()) |
| return {}; |
| |
| return Importer.getToContext().getObjCObjectPointerType(ToPointeeType); |
| } |
| |
| //---------------------------------------------------------------------------- |
| // Import Declarations |
| //---------------------------------------------------------------------------- |
| bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| DeclContext *&LexicalDC, |
| DeclarationName &Name, |
| NamedDecl *&ToD, |
| SourceLocation &Loc) { |
| // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop. |
| // example: int struct_in_proto(struct data_t{int a;int b;} *d); |
| DeclContext *OrigDC = D->getDeclContext(); |
| FunctionDecl *FunDecl; |
| if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) && |
| FunDecl->hasBody()) { |
| SourceRange RecR = D->getSourceRange(); |
| SourceRange BodyR = FunDecl->getBody()->getSourceRange(); |
| // If RecordDecl is not in Body (it is a param), we bail out. |
| if (RecR.isValid() && BodyR.isValid() && |
| (RecR.getBegin() < BodyR.getBegin() || |
| BodyR.getEnd() < RecR.getEnd())) { |
| Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
| << D->getDeclKindName(); |
| return true; |
| } |
| } |
| |
| // Import the context of this declaration. |
| DC = Importer.ImportContext(OrigDC); |
| if (!DC) |
| return true; |
| |
| LexicalDC = DC; |
| if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| if (!LexicalDC) |
| return true; |
| } |
| |
| // Import the name of this declaration. |
| Name = Importer.Import(D->getDeclName()); |
| if (D->getDeclName() && !Name) |
| return true; |
| |
| // Import the location of this declaration. |
| Loc = Importer.Import(D->getLocation()); |
| ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D)); |
| return false; |
| } |
| |
| void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { |
| if (!FromD) |
| return; |
| |
| if (!ToD) { |
| ToD = Importer.Import(FromD); |
| if (!ToD) |
| return; |
| } |
| |
| if (auto *FromRecord = dyn_cast<RecordDecl>(FromD)) { |
| if (auto *ToRecord = cast_or_null<RecordDecl>(ToD)) { |
| if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) { |
| ImportDefinition(FromRecord, ToRecord); |
| } |
| } |
| return; |
| } |
| |
| if (auto *FromEnum = dyn_cast<EnumDecl>(FromD)) { |
| if (auto *ToEnum = cast_or_null<EnumDecl>(ToD)) { |
| if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { |
| ImportDefinition(FromEnum, ToEnum); |
| } |
| } |
| return; |
| } |
| } |
| |
| void |
| ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| DeclarationNameInfo& To) { |
| // NOTE: To.Name and To.Loc are already imported. |
| // We only have to import To.LocInfo. |
| switch (To.getName().getNameKind()) { |
| case DeclarationName::Identifier: |
| case DeclarationName::ObjCZeroArgSelector: |
| case DeclarationName::ObjCOneArgSelector: |
| case DeclarationName::ObjCMultiArgSelector: |
| case DeclarationName::CXXUsingDirective: |
| case DeclarationName::CXXDeductionGuideName: |
| return; |
| |
| case DeclarationName::CXXOperatorName: { |
| SourceRange Range = From.getCXXOperatorNameRange(); |
| To.setCXXOperatorNameRange(Importer.Import(Range)); |
| return; |
| } |
| case DeclarationName::CXXLiteralOperatorName: { |
| SourceLocation Loc = From.getCXXLiteralOperatorNameLoc(); |
| To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc)); |
| return; |
| } |
| case DeclarationName::CXXConstructorName: |
| case DeclarationName::CXXDestructorName: |
| case DeclarationName::CXXConversionFunctionName: { |
| TypeSourceInfo *FromTInfo = From.getNamedTypeInfo(); |
| To.setNamedTypeInfo(Importer.Import(FromTInfo)); |
| return; |
| } |
| } |
| llvm_unreachable("Unknown name kind."); |
| } |
| |
| void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { |
| if (Importer.isMinimalImport() && !ForceImport) { |
| Importer.ImportContext(FromDC); |
| return; |
| } |
| |
| for (auto *From : FromDC->decls()) |
| Importer.Import(From); |
| } |
| |
| void ASTNodeImporter::ImportImplicitMethods( |
| const CXXRecordDecl *From, CXXRecordDecl *To) { |
| assert(From->isCompleteDefinition() && To->getDefinition() == To && |
| "Import implicit methods to or from non-definition"); |
| |
| for (CXXMethodDecl *FromM : From->methods()) |
| if (FromM->isImplicit()) |
| Importer.Import(FromM); |
| } |
| |
| static void setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To, |
| ASTImporter &Importer) { |
| if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) { |
| auto *ToTypedef = |
| cast_or_null<TypedefNameDecl>(Importer.Import(FromTypedef)); |
| assert (ToTypedef && "Failed to import typedef of an anonymous structure"); |
| |
| To->setTypedefNameForAnonDecl(ToTypedef); |
| } |
| } |
| |
| bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To, |
| ImportDefinitionKind Kind) { |
| if (To->getDefinition() || To->isBeingDefined()) { |
| if (Kind == IDK_Everything) |
| ImportDeclContext(From, /*ForceImport=*/true); |
| |
| return false; |
| } |
| |
| To->startDefinition(); |
| |
| setTypedefNameForAnonDecl(From, To, Importer); |
| |
| // Add base classes. |
| if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) { |
| auto *FromCXX = cast<CXXRecordDecl>(From); |
| |
| struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); |
| struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); |
| ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor; |
| ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers; |
| ToData.Aggregate = FromData.Aggregate; |
| ToData.PlainOldData = FromData.PlainOldData; |
| ToData.Empty = FromData.Empty; |
| ToData.Polymorphic = FromData.Polymorphic; |
| ToData.Abstract = FromData.Abstract; |
| ToData.IsStandardLayout = FromData.IsStandardLayout; |
| ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout; |
| ToData.HasBasesWithFields = FromData.HasBasesWithFields; |
| ToData.HasBasesWithNonStaticDataMembers = |
| FromData.HasBasesWithNonStaticDataMembers; |
| ToData.HasPrivateFields = FromData.HasPrivateFields; |
| ToData.HasProtectedFields = FromData.HasProtectedFields; |
| ToData.HasPublicFields = FromData.HasPublicFields; |
| ToData.HasMutableFields = FromData.HasMutableFields; |
| ToData.HasVariantMembers = FromData.HasVariantMembers; |
| ToData.HasOnlyCMembers = FromData.HasOnlyCMembers; |
| ToData.HasInClassInitializer = FromData.HasInClassInitializer; |
| ToData.HasUninitializedReferenceMember |
| = FromData.HasUninitializedReferenceMember; |
| ToData.HasUninitializedFields = FromData.HasUninitializedFields; |
| ToData.HasInheritedConstructor = FromData.HasInheritedConstructor; |
| ToData.HasInheritedAssignment = FromData.HasInheritedAssignment; |
| ToData.NeedOverloadResolutionForCopyConstructor |
| = FromData.NeedOverloadResolutionForCopyConstructor; |
| ToData.NeedOverloadResolutionForMoveConstructor |
| = FromData.NeedOverloadResolutionForMoveConstructor; |
| ToData.NeedOverloadResolutionForMoveAssignment |
| = FromData.NeedOverloadResolutionForMoveAssignment; |
| ToData.NeedOverloadResolutionForDestructor |
| = FromData.NeedOverloadResolutionForDestructor; |
| ToData.DefaultedCopyConstructorIsDeleted |
| = FromData.DefaultedCopyConstructorIsDeleted; |
| ToData.DefaultedMoveConstructorIsDeleted |
| = FromData.DefaultedMoveConstructorIsDeleted; |
| ToData.DefaultedMoveAssignmentIsDeleted |
| = FromData.DefaultedMoveAssignmentIsDeleted; |
| ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted; |
| ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers; |
| ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor; |
| ToData.HasConstexprNonCopyMoveConstructor |
| = FromData.HasConstexprNonCopyMoveConstructor; |
| ToData.HasDefaultedDefaultConstructor |
| = FromData.HasDefaultedDefaultConstructor; |
| ToData.DefaultedDefaultConstructorIsConstexpr |
| = FromData.DefaultedDefaultConstructorIsConstexpr; |
| ToData.HasConstexprDefaultConstructor |
| = FromData.HasConstexprDefaultConstructor; |
| ToData.HasNonLiteralTypeFieldsOrBases |
| = FromData.HasNonLiteralTypeFieldsOrBases; |
| // ComputedVisibleConversions not imported. |
| ToData.UserProvidedDefaultConstructor |
| = FromData.UserProvidedDefaultConstructor; |
| ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers; |
| ToData.ImplicitCopyConstructorCanHaveConstParamForVBase |
| = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase; |
| ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase |
| = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase; |
| ToData.ImplicitCopyAssignmentHasConstParam |
| = FromData.ImplicitCopyAssignmentHasConstParam; |
| ToData.HasDeclaredCopyConstructorWithConstParam |
| = FromData.HasDeclaredCopyConstructorWithConstParam; |
| ToData.HasDeclaredCopyAssignmentWithConstParam |
| = FromData.HasDeclaredCopyAssignmentWithConstParam; |
| |
| SmallVector<CXXBaseSpecifier *, 4> Bases; |
| for (const auto &Base1 : FromCXX->bases()) { |
| QualType T = Importer.Import(Base1.getType()); |
| if (T.isNull()) |
| return true; |
| |
| SourceLocation EllipsisLoc; |
| if (Base1.isPackExpansion()) |
| EllipsisLoc = Importer.Import(Base1.getEllipsisLoc()); |
| |
| // Ensure that we have a definition for the base. |
| ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()); |
| |
| Bases.push_back( |
| new (Importer.getToContext()) |
| CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()), |
| Base1.isVirtual(), |
| Base1.isBaseOfClass(), |
| Base1.getAccessSpecifierAsWritten(), |
| Importer.Import(Base1.getTypeSourceInfo()), |
| EllipsisLoc)); |
| } |
| if (!Bases.empty()) |
| ToCXX->setBases(Bases.data(), Bases.size()); |
| } |
| |
| if (shouldForceImportDeclContext(Kind)) |
| ImportDeclContext(From, /*ForceImport=*/true); |
| |
| To->completeDefinition(); |
| return false; |
| } |
| |
| bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To, |
| ImportDefinitionKind Kind) { |
| if (To->getAnyInitializer()) |
| return false; |
| |
| // FIXME: Can we really import any initializer? Alternatively, we could force |
| // ourselves to import every declaration of a variable and then only use |
| // getInit() here. |
| To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer()))); |
| |
| // FIXME: Other bits to merge? |
| |
| return false; |
| } |
| |
| bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To, |
| ImportDefinitionKind Kind) { |
| if (To->getDefinition() || To->isBeingDefined()) { |
| if (Kind == IDK_Everything) |
| ImportDeclContext(From, /*ForceImport=*/true); |
| return false; |
| } |
| |
| To->startDefinition(); |
| |
| setTypedefNameForAnonDecl(From, To, Importer); |
| |
| QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From)); |
| if (T.isNull()) |
| return true; |
| |
| QualType ToPromotionType = Importer.Import(From->getPromotionType()); |
| if (ToPromotionType.isNull()) |
| return true; |
| |
| if (shouldForceImportDeclContext(Kind)) |
| ImportDeclContext(From, /*ForceImport=*/true); |
| |
| // FIXME: we might need to merge the number of positive or negative bits |
| // if the enumerator lists don't match. |
| To->completeDefinition(T, ToPromotionType, |
| From->getNumPositiveBits(), |
| From->getNumNegativeBits()); |
| return false; |
| } |
| |
| TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList( |
| TemplateParameterList *Params) { |
| SmallVector<NamedDecl *, 4> ToParams(Params->size()); |
| if (ImportContainerChecked(*Params, ToParams)) |
| return nullptr; |
| |
| Expr *ToRequiresClause; |
| if (Expr *const R = Params->getRequiresClause()) { |
| ToRequiresClause = Importer.Import(R); |
| if (!ToRequiresClause) |
| return nullptr; |
| } else { |
| ToRequiresClause = nullptr; |
| } |
| |
| return TemplateParameterList::Create(Importer.getToContext(), |
| Importer.Import(Params->getTemplateLoc()), |
| Importer.Import(Params->getLAngleLoc()), |
| ToParams, |
| Importer.Import(Params->getRAngleLoc()), |
| ToRequiresClause); |
| } |
| |
| TemplateArgument |
| ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { |
| switch (From.getKind()) { |
| case TemplateArgument::Null: |
| return TemplateArgument(); |
| |
| case TemplateArgument::Type: { |
| QualType ToType = Importer.Import(From.getAsType()); |
| if (ToType.isNull()) |
| return {}; |
| return TemplateArgument(ToType); |
| } |
| |
| case TemplateArgument::Integral: { |
| QualType ToType = Importer.Import(From.getIntegralType()); |
| if (ToType.isNull()) |
| return {}; |
| return TemplateArgument(From, ToType); |
| } |
| |
| case TemplateArgument::Declaration: { |
| auto *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl())); |
| QualType ToType = Importer.Import(From.getParamTypeForDecl()); |
| if (!To || ToType.isNull()) |
| return {}; |
| return TemplateArgument(To, ToType); |
| } |
| |
| case TemplateArgument::NullPtr: { |
| QualType ToType = Importer.Import(From.getNullPtrType()); |
| if (ToType.isNull()) |
| return {}; |
| return TemplateArgument(ToType, /*isNullPtr*/true); |
| } |
| |
| case TemplateArgument::Template: { |
| TemplateName ToTemplate = Importer.Import(From.getAsTemplate()); |
| if (ToTemplate.isNull()) |
| return {}; |
| |
| return TemplateArgument(ToTemplate); |
| } |
| |
| case TemplateArgument::TemplateExpansion: { |
| TemplateName ToTemplate |
| = Importer.Import(From.getAsTemplateOrTemplatePattern()); |
| if (ToTemplate.isNull()) |
| return {}; |
| |
| return TemplateArgument(ToTemplate, From.getNumTemplateExpansions()); |
| } |
| |
| case TemplateArgument::Expression: |
| if (Expr *ToExpr = Importer.Import(From.getAsExpr())) |
| return TemplateArgument(ToExpr); |
| return TemplateArgument(); |
| |
| case TemplateArgument::Pack: { |
| SmallVector<TemplateArgument, 2> ToPack; |
| ToPack.reserve(From.pack_size()); |
| if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack)) |
| return {}; |
| |
| return TemplateArgument( |
| llvm::makeArrayRef(ToPack).copy(Importer.getToContext())); |
| } |
| } |
| |
| llvm_unreachable("Invalid template argument kind"); |
| } |
| |
| Optional<TemplateArgumentLoc> |
| ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) { |
| TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument()); |
| TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo(); |
| TemplateArgumentLocInfo ToInfo; |
| if (Arg.getKind() == TemplateArgument::Expression) { |
| Expr *E = Importer.Import(FromInfo.getAsExpr()); |
| ToInfo = TemplateArgumentLocInfo(E); |
| if (!E) |
| return None; |
| } else if (Arg.getKind() == TemplateArgument::Type) { |
| if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo())) |
| ToInfo = TemplateArgumentLocInfo(TSI); |
| else |
| return None; |
| } else { |
| ToInfo = TemplateArgumentLocInfo( |
| Importer.Import(FromInfo.getTemplateQualifierLoc()), |
| Importer.Import(FromInfo.getTemplateNameLoc()), |
| Importer.Import(FromInfo.getTemplateEllipsisLoc())); |
| } |
| return TemplateArgumentLoc(Arg, ToInfo); |
| } |
| |
| bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs, |
| unsigned NumFromArgs, |
| SmallVectorImpl<TemplateArgument> &ToArgs) { |
| for (unsigned I = 0; I != NumFromArgs; ++I) { |
| TemplateArgument To = ImportTemplateArgument(FromArgs[I]); |
| if (To.isNull() && !FromArgs[I].isNull()) |
| return true; |
| |
| ToArgs.push_back(To); |
| } |
| |
| return false; |
| } |
| |
| // We cannot use Optional<> pattern here and below because |
| // TemplateArgumentListInfo's operator new is declared as deleted so it cannot |
| // be stored in Optional. |
| template <typename InContainerTy> |
| bool ASTNodeImporter::ImportTemplateArgumentListInfo( |
| const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) { |
| for (const auto &FromLoc : Container) { |
| if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc)) |
| ToTAInfo.addArgument(*ToLoc); |
| else |
| return true; |
| } |
| return false; |
| } |
| |
| static StructuralEquivalenceKind |
| getStructuralEquivalenceKind(const ASTImporter &Importer) { |
| return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal |
| : StructuralEquivalenceKind::Default; |
| } |
| |
| bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) { |
| StructuralEquivalenceContext Ctx( |
| Importer.getFromContext(), Importer.getToContext(), |
| Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer), |
| false, Complain); |
| return Ctx.IsEquivalent(From, To); |
| } |
| |
| bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, |
| RecordDecl *ToRecord, bool Complain) { |
| // Eliminate a potential failure point where we attempt to re-import |
| // something we're trying to import while completing ToRecord. |
| Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord); |
| if (ToOrigin) { |
| auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin); |
| if (ToOriginRecord) |
| ToRecord = ToOriginRecord; |
| } |
| |
| StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
| ToRecord->getASTContext(), |
| Importer.getNonEquivalentDecls(), |
| getStructuralEquivalenceKind(Importer), |
| false, Complain); |
| return Ctx.IsEquivalent(FromRecord, ToRecord); |
| } |
| |
| bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, |
| bool Complain) { |
| StructuralEquivalenceContext Ctx( |
| Importer.getFromContext(), Importer.getToContext(), |
| Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer), |
| false, Complain); |
| return Ctx.IsEquivalent(FromVar, ToVar); |
| } |
| |
| bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { |
| StructuralEquivalenceContext Ctx( |
| Importer.getFromContext(), Importer.getToContext(), |
| Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer)); |
| return Ctx.IsEquivalent(FromEnum, ToEnum); |
| } |
| |
| bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From, |
| FunctionTemplateDecl *To) { |
| StructuralEquivalenceContext Ctx( |
| Importer.getFromContext(), Importer.getToContext(), |
| Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer), |
| false, false); |
| return Ctx.IsEquivalent(From, To); |
| } |
| |
| bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) { |
| StructuralEquivalenceContext Ctx( |
| Importer.getFromContext(), Importer.getToContext(), |
| Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer), |
| false, false); |
| return Ctx.IsEquivalent(From, To); |
| } |
| |
| bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC, |
| EnumConstantDecl *ToEC) { |
| const llvm::APSInt &FromVal = FromEC->getInitVal(); |
| const llvm::APSInt &ToVal = ToEC->getInitVal(); |
| |
| return FromVal.isSigned() == ToVal.isSigned() && |
| FromVal.getBitWidth() == ToVal.getBitWidth() && |
| FromVal == ToVal; |
| } |
| |
| bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From, |
| ClassTemplateDecl *To) { |
| StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
| Importer.getToContext(), |
| Importer.getNonEquivalentDecls(), |
| getStructuralEquivalenceKind(Importer)); |
| return Ctx.IsEquivalent(From, To); |
| } |
| |
| bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From, |
| VarTemplateDecl *To) { |
| StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
| Importer.getToContext(), |
| Importer.getNonEquivalentDecls(), |
| getStructuralEquivalenceKind(Importer)); |
| return Ctx.IsEquivalent(From, To); |
| } |
| |
| Decl *ASTNodeImporter::VisitDecl(Decl *D) { |
| Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
| << D->getDeclKindName(); |
| return nullptr; |
| } |
| |
| Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) { |
| // Import the context of this declaration. |
| DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| if (!DC) |
| return nullptr; |
| |
| DeclContext *LexicalDC = DC; |
| if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| if (!LexicalDC) |
| return nullptr; |
| } |
| |
| // Import the location of this declaration. |
| SourceLocation Loc = Importer.Import(D->getLocation()); |
| |
| EmptyDecl *ToD; |
| if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, Loc)) |
| return ToD; |
| |
| ToD->setLexicalDeclContext(LexicalDC); |
| LexicalDC->addDeclInternal(ToD); |
| return ToD; |
| } |
| |
| Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| TranslationUnitDecl *ToD = |
| Importer.getToContext().getTranslationUnitDecl(); |
| |
| Importer.MapImported(D, ToD); |
| |
| return ToD; |
| } |
| |
| Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| SourceLocation Loc = Importer.Import(D->getLocation()); |
| SourceLocation ColonLoc = Importer.Import(D->getColonLoc()); |
| |
| // Import the context of this declaration. |
| DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| if (!DC) |
| return nullptr; |
| |
| AccessSpecDecl *ToD; |
| if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(), |
| DC, Loc, ColonLoc)) |
| return ToD; |
| |
| // Lexical DeclContext and Semantic DeclContext |
| // is always the same for the accessSpec. |
| ToD->setLexicalDeclContext(DC); |
| DC->addDeclInternal(ToD); |
| |
| return ToD; |
| } |
| |
| Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| if (!DC) |
| return nullptr; |
| |
| DeclContext *LexicalDC = DC; |
| |
| // Import the location of this declaration. |
| SourceLocation Loc = Importer.Import(D->getLocation()); |
| |
| Expr *AssertExpr = Importer.Import(D->getAssertExpr()); |
| if (!AssertExpr) |
| return nullptr; |
| |
| StringLiteral *FromMsg = D->getMessage(); |
| auto *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg)); |
| if (!ToMsg && FromMsg) |
| return nullptr; |
| |
| StaticAssertDecl *ToD; |
| if (GetImportedOrCreateDecl( |
| ToD, D, Importer.getToContext(), DC, Loc, AssertExpr, ToMsg, |
| Importer.Import(D->getRParenLoc()), D->isFailed())) |
| return ToD; |
| |
| ToD->setLexicalDeclContext(LexicalDC); |
| LexicalDC->addDeclInternal(ToD); |
| return ToD; |
| } |
| |
| Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { |
| // Import the major distinguishing characteristics of this namespace. |
| DeclContext *DC, *LexicalDC; |
| DeclarationName Name; |
| SourceLocation Loc; |
| NamedDecl *ToD; |
| if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| return nullptr; |
| if (ToD) |
| return ToD; |
| |
| NamespaceDecl *MergeWithNamespace = nullptr; |
| if (!Name) { |
| // This is an anonymous namespace. Adopt an existing anonymous |
| // namespace if we can. |
| // FIXME: Not testable. |
| if (auto *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| MergeWithNamespace = TU->getAnonymousNamespace(); |
| else |
| MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace(); |
| } else { |
| SmallVector<NamedDecl *, 4> ConflictingDecls; |
| SmallVector<NamedDecl *, 2> FoundDecls; |
| DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
| for (auto *FoundDecl : FoundDecls) { |
| if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace)) |
| continue; |
| |
| if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) { |
| MergeWithNamespace = FoundNS; |
| ConflictingDecls.clear(); |
| break; |
| } |
| |
| ConflictingDecls.push_back(FoundDecl); |
| } |
| |
| if (!ConflictingDecls.empty()) { |
| Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace, |
| ConflictingDecls.data(), |
| ConflictingDecls.size()); |
| } |
| } |
| |
| // Create the "to" namespace, if needed. |
| NamespaceDecl *ToNamespace = MergeWithNamespace; |
| if (!ToNamespace) { |
| if (GetImportedOrCreateDecl( |
| ToNamespace, D, Importer.getToContext(), DC, D->isInline(), |
| Importer.Import(D->getLocStart()), Loc, Name.getAsIdentifierInfo(), |
| /*PrevDecl=*/nullptr)) |
| return ToNamespace; |
| ToNamespace->setLexicalDeclContext(LexicalDC); |
| LexicalDC->addDeclInternal(ToNamespace); |
| |
| // If this is an anonymous namespace, register it as the anonymous |
| // namespace within its context. |
| if (!Name) { |
| if (auto *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| TU->setAnonymousNamespace(ToNamespace); |
| else |
| cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace); |
| } |
| } |
| Importer.MapImported(D, ToNamespace); |
| |
| ImportDeclContext(D); |
| |
| return ToNamespace; |
| } |
| |
| Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| // Import the major distinguishing characteristics of this namespace. |
| DeclContext *DC, *LexicalDC; |
| DeclarationName Name; |
| SourceLocation Loc; |
| NamedDecl *LookupD; |
| if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc)) |
| return nullptr; |
| if (LookupD) |
| return LookupD; |
| |
| // NOTE: No conflict resolution is done for namespace aliases now. |
| |
| auto *TargetDecl = cast_or_null<NamespaceDecl>( |
| Importer.Import(D->getNamespace())); |
| if (!TargetDecl) |
| return nullptr; |
| |
| IdentifierInfo *ToII = Importer.Import(D->getIdentifier()); |
| if (!ToII) |
| return nullptr; |
| |
| NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc()); |
| if (D->getQualifierLoc() && !ToQLoc) |
| return nullptr; |
| |
| NamespaceAliasDecl *ToD; |
| if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, |
| Importer.Import(D->getNamespaceLoc()), |
| Importer.Import(D->getAliasLoc()), ToII, ToQLoc, |
| Importer.Import(D->getTargetNameLoc()), |
| TargetDecl)) |
| return ToD; |
| |
| ToD->setLexicalDeclContext(LexicalDC); |
| LexicalDC->addDeclInternal(ToD); |
| |
| return ToD; |
| } |
| |
| Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { |
| // Import the major distinguishing characteristics of this typedef. |
| DeclContext *DC, *LexicalDC; |
| DeclarationName Name; |
| SourceLocation Loc; |
| NamedDecl *ToD; |
| if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| return nullptr; |
| if (ToD) |
| return ToD; |
| |
| // If this typedef is not in block scope, determine whether we've |
| // seen a typedef with the same name (that we can merge with) or any |
| // other entity by that name (which name lookup could conflict with). |
| if (!DC->isFunctionOrMethod()) { |
| SmallVector<NamedDecl *, 4> ConflictingDecls; |
| unsigned IDNS = Decl::IDNS_Ordinary; |
| SmallVector<NamedDecl *, 2> FoundDecls; |
| DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
| for (auto *FoundDecl : FoundDecls) { |
| if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
| continue; |
| if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) { |
| if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(), |
| FoundTypedef->getUnderlyingType())) |
| return Importer.MapImported(D, FoundTypedef); |
| } |
| |
| ConflictingDecls.push_back(FoundDecl); |
| } |
| |
| if (!ConflictingDecls.empty()) { |
| Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| ConflictingDecls.data(), |
| ConflictingDecls.size()); |
| if (!Name) |
| return nullptr; |
| } |
| } |
| |
| // Import the underlying type of this typedef; |
| QualType T = Importer.Import(D->getUnderlyingType()); |
| if (T.isNull()) |
| return nullptr; |
| |
| // Create the new typedef node. |
| TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| SourceLocation StartL = Importer.Import(D->getLocStart()); |
| |
| TypedefNameDecl *ToTypedef; |
| if (IsAlias) { |
| if (GetImportedOrCreateDecl<TypeAliasDecl>( |
| ToTypedef, D, Importer.getToContext(), DC, StartL, Loc, |
| Name.getAsIdentifierInfo(), TInfo)) |
| return ToTypedef; |
| } else if (GetImportedOrCreateDecl<TypedefDecl>( |
| ToTypedef, D, Importer.getToContext(), DC, StartL, Loc, |
| Name.getAsIdentifierInfo(), TInfo)) |
| return ToTypedef; |
| |
| ToTypedef->setAccess(D->getAccess()); |
| ToTypedef->setLexicalDeclContext(LexicalDC); |
| |
| // Templated declarations should not appear in DeclContext. |
| TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr; |
| if (!FromAlias || !FromAlias->getDescribedAliasTemplate()) |
| LexicalDC->addDeclInternal(ToTypedef); |
| |
| return ToTypedef; |
| } |
| |
| Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { |
| return VisitTypedefNameDecl(D, /*IsAlias=*/false); |
| } |
| |
| Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| return VisitTypedefNameDecl(D, /*IsAlias=*/true); |
| } |
| |
| Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| // Import the major distinguishing characteristics of this typedef. |
| DeclContext *DC, *LexicalDC; |
| DeclarationName Name; |
| SourceLocation Loc; |
| NamedDecl *FoundD; |
| if (ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc)) |
| return nullptr; |
| if (FoundD) |
| return FoundD; |
| |
| // If this typedef is not in block scope, determine whether we've |
| // seen a typedef with the same name (that we can merge with) or any |
| // other entity by that name (which name lookup could conflict with). |
| if (!DC->isFunctionOrMethod()) { |
| SmallVector<NamedDecl *, 4> ConflictingDecls; |
| unsigned IDNS = Decl::IDNS_Ordinary; |
| SmallVector<NamedDecl *, 2> FoundDecls; |
| DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
| for (auto *FoundDecl : FoundDecls) { |
| if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
| continue; |
| if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl)) |
| return Importer.MapImported(D, FoundAlias); |
| ConflictingDecls.push_back(FoundDecl); |
| } |
| |
| if (!ConflictingDecls.empty()) { |
| Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| ConflictingDecls.data(), |
| ConflictingDecls.size()); |
| if (!Name) |
| return nullptr; |
| } |
| } |
| |
| TemplateParameterList *Params = ImportTemplateParameterList( |
| D->getTemplateParameters()); |
| if (!Params) |
| return nullptr; |
| |
| auto *TemplDecl = cast_or_null<TypeAliasDecl>( |
| Importer.Import(D->getTemplatedDecl())); |
| if (!TemplDecl) |
| return nullptr; |
| |
| TypeAliasTemplateDecl *ToAlias; |
| if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc, |
| Name, Params, TemplDecl)) |
| return ToAlias; |
| |
| TemplDecl->setDescribedAliasTemplate(ToAlias); |
| |
| ToAlias->setAccess(D->getAccess()); |
| ToAlias->setLexicalDeclContext(LexicalDC); |
| LexicalDC->addDeclInternal(ToAlias); |
| return ToAlias; |
| } |
| |
| Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) { |
| // Import the major distinguishing characteristics of this label. |
| DeclContext *DC, *LexicalDC; |
| DeclarationName Name; |
| SourceLocation Loc; |
| NamedDecl *ToD; |
| if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| return nullptr; |
| if (ToD) |
| return ToD; |
| |
| assert(LexicalDC->isFunctionOrMethod()); |
| |
| LabelDecl *ToLabel; |
| if (D->isGnuLocal() |
| ? GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, |
| Importer.Import(D->getLocation()), |
| Name.getAsIdentifierInfo(), |
| Importer.Import(D->getLocStart())) |
| : GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, |
| Importer.Import(D->getLocation()), |
| Name.getAsIdentifierInfo())) |
| return ToLabel; |
| |
| auto *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt())); |
| if (!Label) |
| return nullptr; |
| |
| ToLabel->setStmt(Label); |
| ToLabel->setLexicalDeclContext(LexicalDC); |
| LexicalDC->addDeclInternal(ToLabel); |
| return ToLabel; |
| } |
| |
| Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { |
| // Import the major distinguishing characteristics of this enum. |
| DeclContext *DC, *LexicalDC; |
| DeclarationName Name; |
| SourceLocation Loc; |
| NamedDecl *ToD; |
| if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| return nullptr; |
| if (ToD) |
| return ToD; |
| |
| // Figure out what enum name we're looking for. |
| unsigned IDNS = Decl::IDNS_Tag; |
| DeclarationName SearchName = Name; |
| if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); |
| IDNS = Decl::IDNS_Ordinary; |
| } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
| IDNS |= Decl::IDNS_Ordinary; |
| |
| // We may already have an enum of the same name; try to find and match it. |
| if (!DC->isFunctionOrMethod() && SearchName) { |
| SmallVector<NamedDecl *, 4> ConflictingDecls; |
| SmallVector<NamedDecl *, 2> FoundDecls; |
| DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls); |
| for (auto *FoundDecl : FoundDecls) { |
| if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
| continue; |
| |
| Decl *Found = FoundDecl; |
| if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
| if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| Found = Tag->getDecl(); |
| } |
| |
| if (auto *FoundEnum = dyn_cast<EnumDecl>(Found)) { |
| if (IsStructuralMatch(D, FoundEnum)) |
| return Importer.MapImported(D, FoundEnum); |
| } |
| |
| ConflictingDecls.push_back(FoundDecl); |
| } |
| |
| if (!ConflictingDecls.empty()) { |
| Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| ConflictingDecls.data(), |
| ConflictingDecls.size()); |
| } |
| } |
| |
| // Create the enum declaration. |
| EnumDecl *D2; |
| if (GetImportedOrCreateDecl( |
| D2, D, Importer.getToContext(), DC, Importer.Import(D->getLocStart()), |
| Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(), |
| D->isScopedUsingClassTag(), D->isFixed())) |
| return D2; |
| |
| // Import the qualifier, if any. |
| D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
| D2->setAccess(D->getAccess()); |
| D2->setLexicalDeclContext(LexicalDC); |
| LexicalDC->addDeclInternal(D2); |
| |
| // Import the integer type. |
| QualType ToIntegerType = Importer.Import(D->getIntegerType()); |
| if (ToIntegerType.isNull()) |
| return nullptr; |
| D2->setIntegerType(ToIntegerType); |
| |
| // Import the definition |
| if (D->isCompleteDefinition() && ImportDefinition(D, D2)) |
| return nullptr; |
| |
| return D2; |
| } |
| |
| Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { |
| // If this record has a definition in the translation unit we're coming from, |
| // but this particular declaration is not that definition, import the |
| // definition and map to that. |
| TagDecl *Definition = D->getDefinition(); |
| if (Definition && Definition != D && |
| // In contrast to a normal CXXRecordDecl, the implicit |
| // CXXRecordDecl of ClassTemplateSpecializationDecl is its redeclaration. |
| // The definition of the implicit CXXRecordDecl in this case is the |
| // ClassTemplateSpecializationDecl itself. Thus, we start with an extra |
| // condition in order to be able to import the implict Decl. |
| !D->isImplicit()) { |
| Decl *ImportedDef = Importer.Import(Definition); |
| if (!ImportedDef) |
| return nullptr; |
| |
| return Importer.MapImported(D, ImportedDef); |
| } |
| |
| // Import the major distinguishing characteristics of this record. |
| DeclContext *DC, *LexicalDC; |
| DeclarationName Name; |
| SourceLocation Loc; |
| NamedDecl *ToD; |
| if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| return nullptr; |
| if (ToD) |
| return ToD; |
| |
| // Figure out what structure name we're looking for. |
| unsigned IDNS = Decl::IDNS_Tag; |
| DeclarationName SearchName = Name; |
| if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); |
| IDNS = Decl::IDNS_Ordinary; |
| } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
| IDNS |= Decl::IDNS_Ordinary; |
| |
| // We may already have a record of the same name; try to find and match it. |
| RecordDecl *AdoptDecl = nullptr; |
| RecordDecl *PrevDecl = nullptr; |
| if (!DC->isFunctionOrMethod()) { |
| SmallVector<NamedDecl *, 4> ConflictingDecls; |
| SmallVector<NamedDecl *, 2> FoundDecls; |
| DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls); |
| |
| if (!FoundDecls.empty()) { |
| // We're going to have to compare D against potentially conflicting Decls, so complete it. |
| if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition()) |
| D->getASTContext().getExternalSource()->CompleteType(D); |
| } |
| |
| for (auto *FoundDecl : FoundDecls) { |
| if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
| continue; |
| |
| Decl *Found = FoundDecl; |
| if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
| if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| Found = Tag->getDecl(); |
| } |
| |
| if (D->getDescribedTemplate()) { |
| if (auto *Template = dyn_cast<ClassTemplateDecl>(Found)) |
| Found = Template->getTemplatedDecl(); |
| else |
| continue; |
| } |
| |
| if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) { |
| if (!SearchName) { |
| if (!IsStructuralMatch(D, FoundRecord, false)) |
| continue; |
| } |
| |
| PrevDecl = FoundRecord; |
| |
| if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { |
| if ((SearchName && !D->isCompleteDefinition()) |
| || (D->isCompleteDefinition() && |
| D->isAnonymousStructOrUnion() |
| == FoundDef->isAnonymousStructOrUnion() && |
| IsStructuralMatch(D, FoundDef))) { |
| // The record types structurally match, or the "from" translation |
| // unit only had a forward declaration anyway; call it the same |
| // function. |
| // FIXME: Structural equivalence check should check for same |
| // user-defined methods. |
| Importer.MapImported(D, FoundDef); |
| if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) { |
| auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef); |
| assert(FoundCXX && "Record type mismatch"); |
| |
| if (D->isCompleteDefinition() && !Importer.isMinimalImport()) |
| // FoundDef may not have every implicit method that D has |
| // because implicit methods are created only if they are used. |
| ImportImplicitMethods(DCXX, FoundCXX); |
| } |
| return FoundDef; |
| } |
| } else if (!D->isCompleteDefinition()) { |
| // We have a forward declaration of this type, so adopt that forward |
| // declaration rather than building a new one. |
| |
| // If one or both can be completed from external storage then try one |
| // last time to complete and compare them before doing this. |
| |
| if (FoundRecord->hasExternalLexicalStorage() && |
| !FoundRecord->isCompleteDefinition()) |
| FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord); |
| if (D->hasExternalLexicalStorage()) |
| D->getASTContext().getExternalSource()->CompleteType(D); |
| |
| if (FoundRecord->isCompleteDefinition() && |
| D->isCompleteDefinition() && |
| !IsStructuralMatch(D, FoundRecord)) |
| continue; |
| |
| AdoptDecl = FoundRecord; |
| continue; |
| } else if (!SearchName) { |
| continue; |
| } |
| } |
| |
| ConflictingDecls.push_back(FoundDecl); |
| } |
| |
| if (!ConflictingDecls.empty() && SearchName) { |
| Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| ConflictingDecls.data(), |
| ConflictingDecls.size()); |
| } |
| } |
| |
| // Create the record declaration. |
| RecordDecl *D2 = AdoptDecl; |
| SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
| if (!D2) { |
| CXXRecordDecl *D2CXX = nullptr; |
| if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) { |
| if (DCXX->isLambda()) { |
| TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo()); |
| if (GetImportedOrCreateSpecialDecl( |
| D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(), |
| DC, TInfo, Loc, DCXX->isDependentLambda(), |
| DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault())) |
| return D2CXX; |
| Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl()); |
| if (DCXX->getLambdaContextDecl() && !CDecl) |
| return nullptr; |
| D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl); |
| } else if (DCXX->isInjectedClassName()) { |
| // We have to be careful to do a similar dance to the one in |
| // Sema::ActOnStartCXXMemberDeclarations |
| CXXRecordDecl *const PrevDecl = nullptr; |
| const bool DelayTypeCreation = true; |
| if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(), |
| D->getTagKind(), DC, StartLoc, Loc, |
| Name.getAsIdentifierInfo(), PrevDecl, |
| DelayTypeCreation)) |
| return D2CXX; |
| Importer.getToContext().getTypeDeclType( |
| D2CXX, dyn_cast<CXXRecordDecl>(DC)); |
| } else { |
| if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(), |
| D->getTagKind(), DC, StartLoc, Loc, |
| Name.getAsIdentifierInfo(), |
| cast_or_null<CXXRecordDecl>(PrevDecl))) |
| return D2CXX; |
| } |
| |
| D2 = D2CXX; |
| D2->setAccess(D->getAccess()); |
| D2->setLexicalDeclContext(LexicalDC); |
| if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit()) |
| LexicalDC->addDeclInternal(D2); |
| |
| if (ClassTemplateDecl *FromDescribed = |
| DCXX->getDescribedClassTemplate()) { |
| auto *ToDescribed = cast_or_null<ClassTemplateDecl>( |
| Importer.Import(FromDescribed)); |
| if (!ToDescribed) |
| return nullptr; |
| D2CXX->setDescribedClassTemplate(ToDescribed); |
| if (!DCXX->isInjectedClassName()) { |
| // In a record describing a template the type should be an |
| // InjectedClassNameType (see Sema::CheckClassTemplate). Update the |
| // previously set type to the correct value here (ToDescribed is not |
| // available at record create). |
| // FIXME: The previous type is cleared but not removed from |
| // ASTContext's internal storage. |
| CXXRecordDecl *Injected = nullptr; |
| for (NamedDecl *Found : D2CXX->noload_lookup(Name)) { |
| auto *Record = dyn_cast<CXXRecordDecl>(Found); |
| if (Record && Record->isInjectedClassName()) { |
| Injected = Record; |
| break; |
| } |
| } |
| D2CXX->setTypeForDecl(nullptr); |
| Importer.getToContext().getInjectedClassNameType(D2CXX, |
| ToDescribed->getInjectedClassNameSpecialization()); |
| if (Injected) { |
| Injected->setTypeForDecl(nullptr); |
| Importer.getToContext().getTypeDeclType(Injected, D2CXX); |
| } |
| } |
| } else if (MemberSpecializationInfo *MemberInfo = |
| DCXX->getMemberSpecializationInfo()) { |
| TemplateSpecializationKind SK = |
| MemberInfo->getTemplateSpecializationKind(); |
| CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass(); |
| auto *ToInst = |
| cast_or_null<CXXRecordDecl>(Importer.Import(FromInst)); |
| if (FromInst && !ToInst) |
| return nullptr; |
| D2CXX->setInstantiationOfMemberClass(ToInst, SK); |
| D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation( |
| Importer.Import(MemberInfo->getPointOfInstantiation())); |
| } |
| } else { |
| if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), |
| D->getTagKind(), DC, StartLoc, Loc, |
| Name.getAsIdentifierInfo(), PrevDecl)) |
| return D2; |
| D2->setLexicalDeclContext(LexicalDC); |
| LexicalDC->addDeclInternal(D2); |
| } |
| |
| D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
| if (D->isAnonymousStructOrUnion()) |
| D2->setAnonymousStructOrUnion(true); |
| } |
| |
| Importer.MapImported(D, D2); |
| |
| if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default)) |
| return nullptr; |
| |
| return D2; |
| } |
| |
| Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| // Import the major distinguishing characteristics of this enumerator. |
| DeclContext *DC, *LexicalDC; |
| DeclarationName Name; |
| SourceLocation Loc; |
| NamedDecl *ToD; |
| if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| return nullptr; |
| if (ToD) |
| return ToD; |
| |
| QualType T = Importer.Import(D->getType()); |
| if (T.isNull()) |
| return nullptr; |
| |
| // Determine whether there are any other declarations with the same name and |
| // in the same context. |
| if (!LexicalDC->isFunctionOrMethod()) { |
| SmallVector<NamedDecl *, 4> ConflictingDecls; |
| unsigned IDNS = Decl::IDNS_Ordinary; |
| SmallVector<NamedDecl *, 2> FoundDecls; |
| DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
| for (auto *FoundDecl : FoundDecls) { |
| if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
| continue; |
| |
| if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) { |
| if (IsStructuralMatch(D, FoundEnumConstant)) |
| return Importer.MapImported(D, FoundEnumConstant); |
| } |
| |
| ConflictingDecls.push_back(FoundDecl); |
| } |
| |
| if (!ConflictingDecls.empty()) { |
| Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| ConflictingDecls.data(), |
| ConflictingDecls.size()); |
| if (!Name) |
| return nullptr; |
| } |
| } |
| |
| Expr *Init = Importer.Import(D->getInitExpr()); |
| if (D->getInitExpr() && !Init) |
| return nullptr; |
| |
| EnumConstantDecl *ToEnumerator; |
| if (GetImportedOrCreateDecl( |
| ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc, |
| Name.getAsIdentifierInfo(), T, Init, D->getInitVal())) |
| return ToEnumerator; |
| |
| ToEnumerator->setAccess(D->getAccess()); |
| ToEnumerator->setLexicalDeclContext(LexicalDC); |
| LexicalDC->addDeclInternal(ToEnumerator); |
| return ToEnumerator; |
| } |
| |
| bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD, |
| FunctionDecl *ToFD) { |
| switch (FromFD->getTemplatedKind()) { |
| case FunctionDecl::TK_NonTemplate: |
| case FunctionDecl::TK_FunctionTemplate: |
| return false; |
| |
| case FunctionDecl::TK_MemberSpecialization: { |
| auto *InstFD = cast_or_null<FunctionDecl>( |
| Importer.Import(FromFD->getInstantiatedFromMemberFunction())); |
| if (!InstFD) |
| return true; |
| |
| TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind(); |
| SourceLocation POI = Importer.Import( |
| FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()); |
| ToFD->setInstantiationOfMemberFunction(InstFD, TSK); |
| ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI); |
| return false; |
| } |
| |
| case FunctionDecl::TK_FunctionTemplateSpecialization: { |
| FunctionTemplateDecl* Template; |
| OptionalTemplateArgsTy ToTemplArgs; |
| std::tie(Template, ToTemplArgs) = |
| ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); |
| if (!Template || !ToTemplArgs) |
| return true; |
| |
| TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy( |
| Importer.getToContext(), *ToTemplArgs); |
| |
| auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); |
| TemplateArgumentListInfo ToTAInfo; |
| const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten; |
| if (FromTAArgsAsWritten) |
| if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo)) |
| return true; |
| |
| SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation()); |
| |
| TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind(); |
| ToFD->setFunctionTemplateSpecialization( |
| Template, ToTAList, /* InsertPos= */ nullptr, |
| TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI); |
| return false; |
| } |
| |
| case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { |
| auto *FromInfo = FromFD->getDependentSpecializationInfo(); |
| UnresolvedSet<8> TemplDecls; |
| unsigned NumTemplates = FromInfo->getNumTemplates(); |
| for (unsigned I = 0; I < NumTemplates; I++) { |
| if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>( |
| Importer.Import(FromInfo->getTemplate(I)))) |
| TemplDecls.addDecl(ToFTD); |
| else |
| return true; |
| } |
| |
| // Import TemplateArgumentListInfo. |
| TemplateArgumentListInfo ToTAInfo; |
| if (ImportTemplateArgumentListInfo( |
| FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(), |
| llvm::makeArrayRef(FromInfo->getTemplateArgs(), |
| FromInfo->getNumTemplateArgs()), |
| ToTAInfo)) |
| return true; |
| |
| ToFD->setDependentTemplateSpecialization(Importer.getToContext(), |
| TemplDecls, ToTAInfo); |
| return false; |
| } |
| } |
| llvm_unreachable("All cases should be covered!"); |
| } |
| |
| FunctionDecl * |
| ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) { |
| FunctionTemplateDecl* Template; |
| OptionalTemplateArgsTy ToTemplArgs; |
| std::tie(Template, ToTemplArgs) = |
| ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); |
| if (!Template || !ToTemplArgs) |
| return nullptr; |
| |
| void *InsertPos = nullptr; |
| auto *FoundSpec = Template->findSpecialization(*ToTemplArgs, InsertPos); |
| return FoundSpec; |
| } |
| |
| Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { |
| |
| SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D); |
| auto RedeclIt = Redecls.begin(); |
| // Import the first part of the decl chain. I.e. import all previous |
| // declarations starting from the canonical decl. |
| for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) |
| if (!Importer.Import(*RedeclIt)) |
| return nullptr; |
| assert(*RedeclIt == D); |
| |
| // Import the major distinguishing characteristics of this function. |
| DeclContext *DC, *LexicalDC; |
| DeclarationName Name; |
| SourceLocation Loc; |
| NamedDecl *ToD; |
| if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) |
| return nullptr; |
| if (ToD) |
| return ToD; |
| |
| const FunctionDecl *FoundByLookup = nullptr; |
| FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate(); |
| |
| // If this is a function template specialization, then try to find the same |
| // existing specialization in the "to" context. The localUncachedLookup |
| // below will not find any specialization, but would find the primary |
| // template; thus, we have to skip normal lookup in case of specializations. |
| // FIXME handle member function templates (TK_MemberSpecialization) similarly? |
| if (D->getTemplatedKind() == |
| FunctionDecl::TK_FunctionTemplateSpecialization) { |
| if (FunctionDecl *FoundFunction = FindFunctionTemplateSpecialization(D)) { |
| if (D->doesThisDeclarationHaveABody() && |
| FoundFunction->hasBody()) |
| return Importer.Imported(D, FoundFunction); |
| FoundByLookup = FoundFunction; |
| } |
| } |
| // Try to find a function in our own ("to") context with the same name, same |
| // type, and in the same context as the function we're importing. |
| else if (!LexicalDC->isFunctionOrMethod()) { |
| SmallVector<NamedDecl *, 4> ConflictingDecls; |
| unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; |
| SmallVector<NamedDecl *, 2> FoundDecls; |
| DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); |
| for (auto *FoundDecl : FoundDecls) { |
| if (!FoundDecl->isInIdentifierNamespace(IDNS)) |
| continue; |
| |
| // If template was found, look at the templated function. |
| if (FromFT) { |
| if (auto *Template = dyn_cast<FunctionTemplateDecl>(FoundDecl)) |
| FoundDecl = Template->getTemplatedDecl(); |
| else |
| continue; |
| } |
| |
| if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) { |
| if (FoundFunction->hasExternalFormalLinkage() && |
| D->hasExternalFormalLinkage()) { |
| if (IsStructuralMatch(D, FoundFunction)) { |
| const FunctionDecl *Definition = nullptr; |
| if (D->doesThisDeclarationHaveABody() && |
| FoundFunction->hasBody(Definition)) { |
| return Importer.MapImported( |
| D, const_cast<FunctionDecl *>(Definition)); |
| } |
| FoundByLookup = FoundFunction; |
| break; |
| } |
| |
| // FIXME: Check for overloading more carefully, e.g., by boosting |
| // Sema::IsOverload out to the AST library. |
| |
| // Function overloading is okay in C++. |
| if (Importer.getToContext().getLangOpts().CPlusPlus) |
| continue; |
| |
| // Complain about inconsistent function types. |
| Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) |
| << Name << D->getType() << FoundFunction->getType(); |
| Importer.ToDiag(FoundFunction->getLocation(), |
| diag::note_odr_value_here) |
| |