From c61f5a14e5bb16dff162b9d96d3c00f663318773 Mon Sep 17 00:00:00 2001 From: alandefreitas Date: Mon, 27 Jan 2025 15:32:51 -0300 Subject: [PATCH 1/2] populate overloads use explicit types #perf --- include/mrdocs/Metadata/Info.hpp | 5 +- include/mrdocs/Metadata/Info/Concept.hpp | 1 - include/mrdocs/Metadata/Info/Enum.hpp | 1 - include/mrdocs/Metadata/Info/EnumConstant.hpp | 1 - include/mrdocs/Metadata/Info/Field.hpp | 1 - include/mrdocs/Metadata/Info/Friend.hpp | 1 - include/mrdocs/Metadata/Info/Function.hpp | 24 +- include/mrdocs/Metadata/Info/Guide.hpp | 1 - .../mrdocs/Metadata/Info/NamespaceAlias.hpp | 1 - include/mrdocs/Metadata/Info/Record.hpp | 1 - include/mrdocs/Metadata/Info/Typedef.hpp | 17 +- include/mrdocs/Metadata/Info/Variable.hpp | 1 - include/mrdocs/Metadata/Source.hpp | 4 +- include/mrdocs/Metadata/Using.hpp | 3 +- src/lib/AST/ASTVisitor.cpp | 274 ++++++++++-------- src/lib/AST/ASTVisitor.hpp | 32 +- src/lib/AST/ClangHelpers.hpp | 68 ++++- 17 files changed, 262 insertions(+), 174 deletions(-) diff --git a/include/mrdocs/Metadata/Info.hpp b/include/mrdocs/Metadata/Info.hpp index 30826f9c9..4a022321d 100644 --- a/include/mrdocs/Metadata/Info.hpp +++ b/include/mrdocs/Metadata/Info.hpp @@ -20,9 +20,11 @@ #include #include #include +#include #include #include + namespace clang::mrdocs { /* Forward declarations @@ -59,8 +61,7 @@ tag_invoke( /** Base class with common properties of all symbols */ -struct MRDOCS_VISIBLE - Info +struct MRDOCS_VISIBLE Info : SourceInfo { /** The unique identifier for this symbol. */ diff --git a/include/mrdocs/Metadata/Info/Concept.hpp b/include/mrdocs/Metadata/Info/Concept.hpp index abe14ab37..5651efa1b 100644 --- a/include/mrdocs/Metadata/Info/Concept.hpp +++ b/include/mrdocs/Metadata/Info/Concept.hpp @@ -22,7 +22,6 @@ namespace clang::mrdocs { */ struct ConceptInfo final : InfoCommonBase - , SourceInfo { /** The concepts template parameters */ diff --git a/include/mrdocs/Metadata/Info/Enum.hpp b/include/mrdocs/Metadata/Info/Enum.hpp index cfda1d96a..37ddc9e6a 100644 --- a/include/mrdocs/Metadata/Info/Enum.hpp +++ b/include/mrdocs/Metadata/Info/Enum.hpp @@ -22,7 +22,6 @@ namespace clang::mrdocs { struct EnumInfo final : InfoCommonBase - , SourceInfo , ScopeInfo { // Indicates whether this enum is scoped (e.g. enum class). diff --git a/include/mrdocs/Metadata/Info/EnumConstant.hpp b/include/mrdocs/Metadata/Info/EnumConstant.hpp index 8a24bd25b..9625de237 100644 --- a/include/mrdocs/Metadata/Info/EnumConstant.hpp +++ b/include/mrdocs/Metadata/Info/EnumConstant.hpp @@ -21,7 +21,6 @@ namespace clang::mrdocs { */ struct EnumConstantInfo final : InfoCommonBase - , SourceInfo { /** The initializer expression, if any */ diff --git a/include/mrdocs/Metadata/Info/Field.hpp b/include/mrdocs/Metadata/Info/Field.hpp index 946aa5789..875dad105 100644 --- a/include/mrdocs/Metadata/Info/Field.hpp +++ b/include/mrdocs/Metadata/Info/Field.hpp @@ -27,7 +27,6 @@ namespace clang::mrdocs { */ struct FieldInfo final : InfoCommonBase - , SourceInfo { /** Type of the field */ PolymorphicValue Type; diff --git a/include/mrdocs/Metadata/Info/Friend.hpp b/include/mrdocs/Metadata/Info/Friend.hpp index 926e78bcf..488988555 100644 --- a/include/mrdocs/Metadata/Info/Friend.hpp +++ b/include/mrdocs/Metadata/Info/Friend.hpp @@ -21,7 +21,6 @@ namespace clang::mrdocs { */ struct FriendInfo final : InfoCommonBase - , SourceInfo { /** Befriended symbol. */ diff --git a/include/mrdocs/Metadata/Info/Function.hpp b/include/mrdocs/Metadata/Info/Function.hpp index 9b21eae44..86d298a9d 100644 --- a/include/mrdocs/Metadata/Info/Function.hpp +++ b/include/mrdocs/Metadata/Info/Function.hpp @@ -126,7 +126,6 @@ tag_invoke( // Info for functions. struct FunctionInfo final : InfoCommonBase - , SourceInfo { /// Info about the return type of this function. PolymorphicValue ReturnType; @@ -141,15 +140,8 @@ struct FunctionInfo final FunctionClass Class = FunctionClass::Normal; NoexceptInfo Noexcept; - - ExplicitInfo Explicit; - ExprInfo Requires; - bool IsVariadic = false; - bool IsVirtual = false; - bool IsVirtualAsWritten = false; - bool IsPure = false; bool IsDefaulted = false; bool IsExplicitlyDefaulted = false; bool IsDeleted = false; @@ -157,19 +149,23 @@ struct FunctionInfo final bool IsNoReturn = false; bool HasOverrideAttr = false; bool HasTrailingReturn = false; - bool IsConst = false; - bool IsVolatile = false; - bool IsFinal = false; bool IsNodiscard = false; bool IsExplicitObjectMemberFunction = false; - ConstexprKind Constexpr = ConstexprKind::None; OperatorKind OverloadedOperator = OperatorKind::None; StorageClassKind StorageClass = StorageClassKind::None; - ReferenceKind RefQualifier = ReferenceKind::None; - std::vector Attributes; + // CXXMethodDecl + bool IsVirtual = false; + bool IsVirtualAsWritten = false; + bool IsPure = false; + bool IsConst = false; + bool IsVolatile = false; + bool IsFinal = false; + ReferenceKind RefQualifier = ReferenceKind::None; + ExplicitInfo Explicit; + //-------------------------------------------- explicit FunctionInfo(SymbolID const& ID) noexcept diff --git a/include/mrdocs/Metadata/Info/Guide.hpp b/include/mrdocs/Metadata/Info/Guide.hpp index ba2b77991..58481d042 100644 --- a/include/mrdocs/Metadata/Info/Guide.hpp +++ b/include/mrdocs/Metadata/Info/Guide.hpp @@ -23,7 +23,6 @@ namespace clang::mrdocs { */ struct GuideInfo final : InfoCommonBase - , SourceInfo { /** The pattern for the deduced specialization. diff --git a/include/mrdocs/Metadata/Info/NamespaceAlias.hpp b/include/mrdocs/Metadata/Info/NamespaceAlias.hpp index 8d0e20a59..632ec7c08 100644 --- a/include/mrdocs/Metadata/Info/NamespaceAlias.hpp +++ b/include/mrdocs/Metadata/Info/NamespaceAlias.hpp @@ -20,7 +20,6 @@ namespace clang::mrdocs { */ struct NamespaceAliasInfo final : InfoCommonBase - , SourceInfo { /** The aliased symbol. */ PolymorphicValue AliasedSymbol; diff --git a/include/mrdocs/Metadata/Info/Record.hpp b/include/mrdocs/Metadata/Info/Record.hpp index 0c6058471..1aded0677 100644 --- a/include/mrdocs/Metadata/Info/Record.hpp +++ b/include/mrdocs/Metadata/Info/Record.hpp @@ -75,7 +75,6 @@ tag_invoke( */ struct RecordInfo final : InfoCommonBase - , SourceInfo , ScopeInfo { /** Kind of record this is (class, struct, or union). diff --git a/include/mrdocs/Metadata/Info/Typedef.hpp b/include/mrdocs/Metadata/Info/Typedef.hpp index 84684036a..3623e5acd 100644 --- a/include/mrdocs/Metadata/Info/Typedef.hpp +++ b/include/mrdocs/Metadata/Info/Typedef.hpp @@ -22,14 +22,21 @@ namespace clang::mrdocs { // Info for typedef and using statements. struct TypedefInfo final : InfoCommonBase - , SourceInfo { PolymorphicValue Type; - // Indicates if this is a new C++ "using"-style typedef: - // using MyVector = std::vector - // False means it's a C-style typedef: - // typedef std::vector MyVector; + /** Indicates if this is a new C++ "using"-style typedef + + @code + using MyVector = std::vector + @endcode + + False means it's a C-style typedef: + + @code + typedef std::vector MyVector; + @endcode + */ bool IsUsing = false; std::optional Template; diff --git a/include/mrdocs/Metadata/Info/Variable.hpp b/include/mrdocs/Metadata/Info/Variable.hpp index b4b3486d5..73959930d 100644 --- a/include/mrdocs/Metadata/Info/Variable.hpp +++ b/include/mrdocs/Metadata/Info/Variable.hpp @@ -26,7 +26,6 @@ namespace clang::mrdocs { */ struct VariableInfo final : InfoCommonBase - , SourceInfo { /** The type of the variable */ PolymorphicValue Type; diff --git a/include/mrdocs/Metadata/Source.hpp b/include/mrdocs/Metadata/Source.hpp index 27b6f4fa4..95e825455 100644 --- a/include/mrdocs/Metadata/Source.hpp +++ b/include/mrdocs/Metadata/Source.hpp @@ -15,7 +15,7 @@ #include #include -#include +#include #include namespace clang::mrdocs { @@ -118,7 +118,7 @@ struct MRDOCS_DECL */ OptionalLocation DefLoc; - /** Locations where the entity was declared, + /** Locations where the entity was declared. This does not include the definition. */ diff --git a/include/mrdocs/Metadata/Using.hpp b/include/mrdocs/Metadata/Using.hpp index bb3aeaa45..90b201967 100644 --- a/include/mrdocs/Metadata/Using.hpp +++ b/include/mrdocs/Metadata/Using.hpp @@ -56,8 +56,7 @@ tag_invoke( /** Info for using declarations. */ struct UsingInfo final - : InfoCommonBase, - SourceInfo + : InfoCommonBase { /** The kind of using declaration. */ diff --git a/src/lib/AST/ASTVisitor.cpp b/src/lib/AST/ASTVisitor.cpp index d79731a5e..f23260021 100644 --- a/src/lib/AST/ASTVisitor.cpp +++ b/src/lib/AST/ASTVisitor.cpp @@ -112,7 +112,6 @@ traverse(DeclTy* D) using R = std::conditional_t< std::same_as, InfoTypeFor_t, - InfoTy>; auto exp = upsert(D); @@ -122,7 +121,7 @@ traverse(DeclTy* D) // Populate the base classes with the necessary information. // Even when the object is new, we want to update the source locations // and the documentation status. - populateInfoBases(I, isNew, D); + populate(dynamic_cast(I), isNew, D); // Populate the derived Info object with the necessary information // when the object is new. If the object already exists, this @@ -506,36 +505,42 @@ generateID(const Decl* D) const return id; } -template InfoTy, class DeclTy> +namespace +{ +template +bool +isDefinition(DeclTy* D) +{ + if constexpr (requires {D->isThisDeclarationADefinition();}) + { + return D->isThisDeclarationADefinition(); + } + else + { + return false; + } +} +} + +template DeclTy> void ASTVisitor:: -populateInfoBases(InfoTy& I, bool const isNew, DeclTy* D) +populate(Info& I, bool const isNew, DeclTy* D) { // Populate the documentation bool const isDocumented = generateJavadoc(I.javadoc, D); // Populate the source info - if constexpr (std::derived_from) + clang::SourceLocation Loc = D->getBeginLoc(); + if (Loc.isInvalid()) { - bool const isDefinition = [&]() { - if constexpr (requires {D->isThisDeclarationADefinition();}) - { - return D->isThisDeclarationADefinition(); - } - else - { - return false; - } - }(); - clang::SourceLocation Loc = D->getBeginLoc(); - if (Loc.isInvalid()) - { - Loc = D->getLocation(); - } - if (Loc.isValid()) - { - populate(I, Loc, isDefinition, isDocumented); - } + Loc = D->getLocation(); + } + if (Loc.isValid()) + { + populate( + dynamic_cast(I), + Loc, isDefinition(D), isDocumented); } // All other information is redundant if the symbol is not new @@ -545,30 +550,7 @@ populateInfoBases(InfoTy& I, bool const isNew, DeclTy* D) MRDOCS_ASSERT(I.id); MRDOCS_ASSERT(I.Kind != InfoKind::None); - if constexpr (std::same_as) - { - I.Name = extractName(D->getDeducedTemplate()); - } - else if constexpr (std::derived_from) - { - if (auto* FD = D->getFriendDecl()) - { - I.Name = extractName(D->getFriendDecl()); - } - else if (TypeSourceInfo const* FT = D->getFriendType()) - { - llvm::raw_string_ostream os(I.Name); - FT->getType().print(os, context_.getPrintingPolicy()); - } - } - else if constexpr (std::derived_from) - { - I.Name = extractName(D->getNominatedNamespace()); - } - else if constexpr (std::derived_from) - { - I.Name = extractName(D); - } + I.Name = extractName(D); } void @@ -700,12 +682,11 @@ populate(RecordInfo& I, ClassTemplateSpecializationDecl* D) populate(I, cast(D)); } -template DeclTy> void ASTVisitor:: populate( FunctionInfo& I, - DeclTy* D) + FunctionDecl* D) { MRDOCS_SYMBOL_TRACE(D, context_); @@ -719,8 +700,9 @@ populate( if (auto* FTSI = D->getTemplateSpecializationInfo()) { - generateID(getInstantiatedFrom( - FTSI->getTemplate()), I.Template->Primary); + generateID( + getInstantiatedFrom(FTSI->getTemplate()), + I.Template->Primary); // TemplateArguments is used instead of TemplateArgumentsAsWritten // because explicit specializations of function templates may have @@ -756,76 +738,30 @@ populate( I.HasTrailingReturn |= FPT->hasTrailingReturn(); } - // - // FunctionDecl - // - FunctionDecl const* FD = D; - I.OverloadedOperator = toOperatorKind(FD->getOverloadedOperator()); - I.IsVariadic |= FD->isVariadic(); - I.IsDefaulted |= FD->isDefaulted(); - I.IsExplicitlyDefaulted |= FD->isExplicitlyDefaulted(); - I.IsDeleted |= FD->isDeleted(); - I.IsDeletedAsWritten |= FD->isDeletedAsWritten(); - I.IsNoReturn |= FD->isNoReturn(); - I.HasOverrideAttr |= FD->hasAttr(); - - if (ConstexprSpecKind const CSK = FD->getConstexprKind(); + I.OverloadedOperator = toOperatorKind(D->getOverloadedOperator()); + I.IsVariadic |= D->isVariadic(); + I.IsDefaulted |= D->isDefaulted(); + I.IsExplicitlyDefaulted |= D->isExplicitlyDefaulted(); + I.IsDeleted |= D->isDeleted(); + I.IsDeletedAsWritten |= D->isDeletedAsWritten(); + I.IsNoReturn |= D->isNoReturn(); + I.HasOverrideAttr |= D->hasAttr(); + + if (ConstexprSpecKind const CSK = D->getConstexprKind(); CSK != ConstexprSpecKind::Unspecified) { I.Constexpr = toConstexprKind(CSK); } - if (StorageClass const SC = FD->getStorageClass()) + if (StorageClass const SC = D->getStorageClass()) { I.StorageClass = toStorageClassKind(SC); } - I.IsNodiscard |= FD->hasAttr(); - I.IsExplicitObjectMemberFunction |= FD->hasCXXExplicitFunctionObjectParameter(); - - // - // CXXMethodDecl - // - if constexpr(std::derived_from) - { - CXXMethodDecl const* MD = D; - I.IsVirtual |= MD->isVirtual(); - I.IsVirtualAsWritten |= MD->isVirtualAsWritten(); - I.IsPure |= MD->isPureVirtual(); - I.IsConst |= MD->isConst(); - I.IsVolatile |= MD->isVolatile(); - I.RefQualifier = toReferenceKind(MD->getRefQualifier()); - I.IsFinal |= MD->hasAttr(); - //MD->isCopyAssignmentOperator() - //MD->isMoveAssignmentOperator() - //MD->isOverloadedOperator(); - //MD->isStaticOverloadedOperator(); - - // - // CXXDestructorDecl - // - // if constexpr(std::derived_from) - // { - // } - - // - // CXXConstructorDecl - // - if constexpr(std::derived_from) - { - populate(I.Explicit, D->getExplicitSpecifier()); - } - - // - // CXXConversionDecl - // - if constexpr(std::derived_from) - { - populate(I.Explicit, D->getExplicitSpecifier()); - } - } + I.IsNodiscard |= D->hasAttr(); + I.IsExplicitObjectMemberFunction |= D->hasCXXExplicitFunctionObjectParameter(); - ArrayRef const params = FD->parameters(); + ArrayRef const params = D->parameters(); I.Params.resize(params.size()); for (std::size_t i = 0; i < params.size(); ++i) { @@ -858,16 +794,16 @@ populate( } } - I.Class = toFunctionClass(FD->getDeclKind()); + I.Class = toFunctionClass(D->getDeclKind()); // extract the return type in direct dependency mode // if it contains a placeholder type which is // deduceded as a local class type - QualType const RT = FD->getReturnType(); + QualType const RT = D->getReturnType(); MRDOCS_SYMBOL_TRACE(RT, context_); I.ReturnType = toTypeInfo(RT); - if (auto* TRC = FD->getTrailingRequiresClause()) + if (auto* TRC = D->getTrailingRequiresClause()) { populate(I.Requires, TRC); } @@ -922,6 +858,48 @@ populate(FunctionInfo& I, FunctionTemplateDecl* D) populate(I, TD); } +void +ASTVisitor:: +populate(FunctionInfo& I, CXXMethodDecl* D) +{ + FunctionDecl* FD = D; + populate(I, FD); + I.IsVirtual |= D->isVirtual(); + I.IsVirtualAsWritten |= D->isVirtualAsWritten(); + I.IsPure |= D->isPureVirtual(); + I.IsConst |= D->isConst(); + I.IsVolatile |= D->isVolatile(); + I.RefQualifier = toReferenceKind(D->getRefQualifier()); + I.IsFinal |= D->hasAttr(); +} + +void +ASTVisitor:: +populate(FunctionInfo& I, CXXConstructorDecl* D) +{ + CXXMethodDecl* FD = D; + populate(I, FD); + populate(I.Explicit, D->getExplicitSpecifier()); +} + +void +ASTVisitor:: +populate(FunctionInfo& I, CXXDestructorDecl* D) +{ + CXXMethodDecl* FD = D; + populate(I, FD); +} + +void +ASTVisitor:: +populate(FunctionInfo& I, CXXConversionDecl* D) +{ + CXXMethodDecl* FD = D; + populate(I, FD); + populate(I.Explicit, D->getExplicitSpecifier()); +} + + void ASTVisitor:: populate( @@ -948,27 +926,45 @@ populate( D->getInitVal()); } -template TypedefNameDeclTy> void ASTVisitor:: -populate( - TypedefInfo& I, - TypedefNameDeclTy* D) +populate(TypedefInfo& I, TypedefNameDecl* D) { - I.IsUsing = isa(D); QualType const QT = D->getUnderlyingType(); I.Type = toTypeInfo(QT); } +void +ASTVisitor:: +populate(TypedefInfo& I, TypedefDecl* D) +{ + populate(I, cast(D)); +} + +void +ASTVisitor:: +populate(TypedefInfo& I, TypeAliasDecl* D) +{ + I.IsUsing = isa(D); + populate(I, cast(D)); +} + void ASTVisitor:: populate(TypedefInfo& I, TypeAliasTemplateDecl* D) { populate(I.Template, D->getTemplatedDecl(), D); - populate(I, D->getTemplatedDecl()); + if (auto* TD = D->getTemplatedDecl(); + isa(TD)) + { + populate(I, cast(TD)); + } + else + { + populate(I, TD); + } } - void ASTVisitor:: populate( @@ -1569,6 +1565,40 @@ populateAttributes(InfoTy& I, const Decl* D) } } +template DeclTy> +std::string +ASTVisitor:: +extractName(DeclTy const* D) +{ + if constexpr (std::same_as) + { + return extractName(D->getDeducedTemplate()); + } + else if constexpr (std::derived_from) + { + if (auto* FD = D->getFriendDecl()) + { + return extractName(D->getFriendDecl()); + } + if (TypeSourceInfo const* FT = D->getFriendType()) + { + std::string Name; + llvm::raw_string_ostream os(Name); + FT->getType().print(os, context_.getPrintingPolicy()); + return Name; + } + } + else if constexpr (std::derived_from) + { + return extractName(D->getNominatedNamespace()); + } + else if constexpr (std::derived_from) + { + return extractName(cast(D)); + } + return {}; +} + std::string ASTVisitor:: extractName(NamedDecl const* D) diff --git a/src/lib/AST/ASTVisitor.hpp b/src/lib/AST/ASTVisitor.hpp index 8bd645cdf..f0943d225 100644 --- a/src/lib/AST/ASTVisitor.hpp +++ b/src/lib/AST/ASTVisitor.hpp @@ -444,9 +444,9 @@ class ASTVisitor // ================================================= // Populate functions // ================================================= - template InfoTy, class DeclTy> + template DeclTy> void - populateInfoBases(InfoTy& I, bool isNew, DeclTy* D); + populate(Info& I, bool isNew, DeclTy* D); void populate(SourceInfo& I, clang::SourceLocation loc, bool definition, bool documented); @@ -467,22 +467,38 @@ class ASTVisitor void populate(RecordInfo& I, ClassTemplateSpecializationDecl* D); - template DeclTy> void - populate(FunctionInfo& I, DeclTy* D); + populate(FunctionInfo& I, FunctionDecl* D); void populate(FunctionInfo& I, FunctionTemplateDecl* D); + void + populate(FunctionInfo& I, CXXMethodDecl* D); + + void + populate(FunctionInfo& I, CXXConstructorDecl* D); + + void + populate(FunctionInfo& I, CXXDestructorDecl* D); + + void + populate(FunctionInfo& I, CXXConversionDecl* D); + void populate(EnumInfo& I, EnumDecl* D); void populate(EnumConstantInfo& I, EnumConstantDecl* D); - template TypedefNameDeclTy> void - populate(TypedefInfo& I, TypedefNameDeclTy* D); + populate(TypedefInfo& I, TypedefNameDecl* D); + + void + populate(TypedefInfo& I, TypedefDecl* D); + + void + populate(TypedefInfo& I, TypeAliasDecl* D); void populate(TypedefInfo& I, TypeAliasTemplateDecl* D); @@ -638,6 +654,10 @@ class ASTVisitor // ================================================= // Populate function helpers // ================================================= + template DeclTy> + std::string + extractName(DeclTy const* D); + // Extract the name of a declaration std::string extractName(NamedDecl const* D); diff --git a/src/lib/AST/ClangHelpers.hpp b/src/lib/AST/ClangHelpers.hpp index 27b5fa812..c65027195 100644 --- a/src/lib/AST/ClangHelpers.hpp +++ b/src/lib/AST/ClangHelpers.hpp @@ -79,8 +79,16 @@ struct InfoTypeFor // Extract RecordInfo from anything derived from CXXRecordDecl // and ClassTemplateDecl. Decls derived from CXXRecordDecl // include class specializations. -template DeclType> -struct InfoTypeFor +template <> +struct InfoTypeFor + : std::type_identity {}; + +template <> +struct InfoTypeFor + : std::type_identity {}; + +template <> +struct InfoTypeFor : std::type_identity {}; template <> @@ -88,9 +96,24 @@ struct InfoTypeFor : std::type_identity {}; // Extract FunctionInfo from anything derived from FunctionDecl -template FunctionTy> -requires (!std::same_as) -struct InfoTypeFor +template <> +struct InfoTypeFor + : std::type_identity {}; + +template <> +struct InfoTypeFor + : std::type_identity {}; + +template <> +struct InfoTypeFor + : std::type_identity {}; + +template <> +struct InfoTypeFor + : std::type_identity {}; + +template <> +struct InfoTypeFor : std::type_identity {}; template <> @@ -108,8 +131,16 @@ struct InfoTypeFor : std::type_identity {}; // Extract TypedefInfo from anything derived from TypedefNameDecl -template TypedefNameTy> -struct InfoTypeFor +template <> +struct InfoTypeFor + : std::type_identity {}; + +template <> +struct InfoTypeFor + : std::type_identity {}; + +template <> +struct InfoTypeFor : std::type_identity {}; template <> @@ -118,8 +149,16 @@ struct InfoTypeFor // Extract VariableInfo from anything derived from VarDecl // and VarTemplateDecl. -template VarTy> -struct InfoTypeFor +template <> +struct InfoTypeFor + : std::type_identity {}; + +template <> +struct InfoTypeFor + : std::type_identity {}; + +template <> +struct InfoTypeFor : std::type_identity {}; template <> @@ -657,9 +696,14 @@ TypeLocToKind() /** Get the user-written `Decl` for a `Decl` Given a `Decl` `D`, `getInstantiatedFrom` will return the - user-written `Decl` corresponding to `D`. For specializations - which were implicitly instantiated, this will be whichever `Decl` - was used as the pattern for instantiation. + user-written `Decl` corresponding to `D`. + + For specializations which were implicitly instantiated, + this will be whichever `Decl` was used as the pattern + for instantiation. + + For instance, if `D` represents `std::vector`, the + user-written `Decl` will be the `std::vector` template. */ template DeclTy* From d29195dfe75fbfd9668a86ab0070f1e96eb80db7 Mon Sep 17 00:00:00 2001 From: alandefreitas Date: Mon, 27 Jan 2025 16:35:16 -0300 Subject: [PATCH 2/2] AST visits const declarations #refactor --- src/lib/AST/ASTVisitor.cpp | 165 +++++++++++------------- src/lib/AST/ASTVisitor.hpp | 140 ++++++++++---------- src/lib/AST/ClangHelpers.cpp | 4 +- src/lib/AST/ClangHelpers.hpp | 38 +++--- src/lib/AST/InstantiatedFromVisitor.hpp | 60 ++++----- 5 files changed, 192 insertions(+), 215 deletions(-) diff --git a/src/lib/AST/ASTVisitor.cpp b/src/lib/AST/ASTVisitor.cpp index f23260021..150f1d2ea 100644 --- a/src/lib/AST/ASTVisitor.cpp +++ b/src/lib/AST/ASTVisitor.cpp @@ -75,7 +75,7 @@ build() // traverse the translation unit, only extracting // declarations which satisfy all filter conditions. // dependencies will be tracked, but not extracted - auto* TU = context_.getTranslationUnitDecl(); + TranslationUnitDecl const* TU = context_.getTranslationUnitDecl(); traverse(TU); MRDOCS_ASSERT(find(SymbolID::global)); } @@ -85,7 +85,7 @@ template < std::derived_from DeclTy> Info* ASTVisitor:: -traverse(DeclTy* D) +traverse(DeclTy const* D) { MRDOCS_ASSERT(D); MRDOCS_CHECK_OR(!D->isInvalidDecl(), nullptr); @@ -142,7 +142,7 @@ traverse(DeclTy* D) Info* ASTVisitor:: -traverse(FunctionTemplateDecl* D) +traverse(FunctionTemplateDecl const* D) { // Route the traversal to GuideInfo or FunctionInfo if (FunctionDecl* FD = D->getTemplatedDecl(); @@ -155,19 +155,19 @@ traverse(FunctionTemplateDecl* D) Info* ASTVisitor:: -traverse(UsingDirectiveDecl* D) +traverse(UsingDirectiveDecl const* D) { // Find the parent namespace ScopeExitRestore s1(mode_, TraversalMode::Dependency); - Decl* P = getParent(D); + Decl const* P = getParent(D); MRDOCS_SYMBOL_TRACE(P, context_); Info* PI = findOrTraverse(P); MRDOCS_CHECK_OR(PI, nullptr); - auto PNI = dynamic_cast(PI); + auto* const PNI = dynamic_cast(PI); MRDOCS_CHECK_OR(PNI, nullptr); // Find the nominated namespace - Decl* ND = D->getNominatedNamespace(); + Decl const* ND = D->getNominatedNamespace(); MRDOCS_SYMBOL_TRACE(ND, context_); ScopeExitRestore s2(mode_, TraversalMode::Dependency); Info* NDI = findOrTraverse(ND); @@ -182,7 +182,7 @@ traverse(UsingDirectiveDecl* D) Info* ASTVisitor:: -traverse(IndirectFieldDecl* D) +traverse(IndirectFieldDecl const* D) { return traverse(D->getAnonField()); } @@ -193,7 +193,7 @@ template < requires (!std::derived_from) void ASTVisitor:: -traverseMembers(InfoTy& I, DeclTy* DC) +traverseMembers(InfoTy& I, DeclTy const* DC) { // When a declaration context is a function, we should // not traverse its members as function arguments are @@ -242,7 +242,7 @@ template < std::derived_from DeclTy> void ASTVisitor:: -traverseMembers(InfoTy& I, DeclTy* D) +traverseMembers(InfoTy& I, DeclTy const* D) { traverseMembers(I, D->getTemplatedDecl()); } @@ -253,10 +253,10 @@ template < requires (!std::derived_from) void ASTVisitor:: -traverseParents(InfoTy& I, DeclTy* DC) +traverseParents(InfoTy& I, DeclTy const* DC) { MRDOCS_SYMBOL_TRACE(DC, context_); - if (Decl* PD = getParent(DC)) + if (Decl const* PD = getParent(DC)) { MRDOCS_SYMBOL_TRACE(PD, context_); @@ -288,7 +288,7 @@ template < std::derived_from DeclTy> void ASTVisitor:: -traverseParents(InfoTy& I, DeclTy* D) +traverseParents(InfoTy& I, DeclTy const* D) { traverseParents(I, D->getTemplatedDecl()); } @@ -525,7 +525,7 @@ isDefinition(DeclTy* D) template DeclTy> void ASTVisitor:: -populate(Info& I, bool const isNew, DeclTy* D) +populate(Info& I, bool const isNew, DeclTy const* D) { // Populate the documentation bool const isDocumented = generateJavadoc(I.javadoc, D); @@ -595,7 +595,7 @@ void ASTVisitor:: populate( NamespaceInfo& I, - NamespaceDecl* D) + NamespaceDecl const* D) { I.IsAnonymous = D->isAnonymousNamespace(); if (!I.IsAnonymous) @@ -609,7 +609,7 @@ void ASTVisitor:: populate( NamespaceInfo& I, - TranslationUnitDecl*) + TranslationUnitDecl const*) { I.id = SymbolID::global; I.IsAnonymous = false; @@ -620,7 +620,7 @@ void ASTVisitor:: populate( RecordInfo& I, - CXXRecordDecl* D) + CXXRecordDecl const* D) { if (D->getTypedefNameForAnonDecl()) { @@ -668,7 +668,7 @@ populate( void ASTVisitor:: -populate(RecordInfo& I, ClassTemplateDecl* D) +populate(RecordInfo& I, ClassTemplateDecl const* D) { populate(I.Template, D->getTemplatedDecl(), D); populate(I, D->getTemplatedDecl()); @@ -676,7 +676,7 @@ populate(RecordInfo& I, ClassTemplateDecl* D) void ASTVisitor:: -populate(RecordInfo& I, ClassTemplateSpecializationDecl* D) +populate(RecordInfo& I, ClassTemplateSpecializationDecl const* D) { populate(I.Template, D, D->getSpecializedTemplate()); populate(I, cast(D)); @@ -686,7 +686,7 @@ void ASTVisitor:: populate( FunctionInfo& I, - FunctionDecl* D) + FunctionDecl const* D) { MRDOCS_SYMBOL_TRACE(D, context_); @@ -851,18 +851,18 @@ populate( void ASTVisitor:: -populate(FunctionInfo& I, FunctionTemplateDecl* D) +populate(FunctionInfo& I, FunctionTemplateDecl const* D) { - FunctionDecl* TD = D->getTemplatedDecl(); + FunctionDecl const* TD = D->getTemplatedDecl(); populate(I.Template, TD, D); populate(I, TD); } void ASTVisitor:: -populate(FunctionInfo& I, CXXMethodDecl* D) +populate(FunctionInfo& I, CXXMethodDecl const* D) { - FunctionDecl* FD = D; + FunctionDecl const* FD = D; populate(I, FD); I.IsVirtual |= D->isVirtual(); I.IsVirtualAsWritten |= D->isVirtualAsWritten(); @@ -875,26 +875,26 @@ populate(FunctionInfo& I, CXXMethodDecl* D) void ASTVisitor:: -populate(FunctionInfo& I, CXXConstructorDecl* D) +populate(FunctionInfo& I, CXXConstructorDecl const* D) { - CXXMethodDecl* FD = D; + CXXMethodDecl const* FD = D; populate(I, FD); populate(I.Explicit, D->getExplicitSpecifier()); } void ASTVisitor:: -populate(FunctionInfo& I, CXXDestructorDecl* D) +populate(FunctionInfo& I, CXXDestructorDecl const* D) { - CXXMethodDecl* FD = D; + CXXMethodDecl const* FD = D; populate(I, FD); } void ASTVisitor:: -populate(FunctionInfo& I, CXXConversionDecl* D) +populate(FunctionInfo& I, CXXConversionDecl const* D) { - CXXMethodDecl* FD = D; + CXXMethodDecl const* FD = D; populate(I, FD); populate(I.Explicit, D->getExplicitSpecifier()); } @@ -904,7 +904,7 @@ void ASTVisitor:: populate( EnumInfo& I, - EnumDecl* D) + EnumDecl const* D) { I.Scoped = D->isScoped(); if (D->isFixed()) @@ -917,7 +917,7 @@ void ASTVisitor:: populate( EnumConstantInfo& I, - EnumConstantDecl* D) + EnumConstantDecl const* D) { I.Name = extractName(D); populate( @@ -928,7 +928,7 @@ populate( void ASTVisitor:: -populate(TypedefInfo& I, TypedefNameDecl* D) +populate(TypedefInfo& I, TypedefNameDecl const* D) { QualType const QT = D->getUnderlyingType(); I.Type = toTypeInfo(QT); @@ -936,14 +936,14 @@ populate(TypedefInfo& I, TypedefNameDecl* D) void ASTVisitor:: -populate(TypedefInfo& I, TypedefDecl* D) +populate(TypedefInfo& I, TypedefDecl const* D) { populate(I, cast(D)); } void ASTVisitor:: -populate(TypedefInfo& I, TypeAliasDecl* D) +populate(TypedefInfo& I, TypeAliasDecl const* D) { I.IsUsing = isa(D); populate(I, cast(D)); @@ -951,7 +951,7 @@ populate(TypedefInfo& I, TypeAliasDecl* D) void ASTVisitor:: -populate(TypedefInfo& I, TypeAliasTemplateDecl* D) +populate(TypedefInfo& I, TypeAliasTemplateDecl const* D) { populate(I.Template, D->getTemplatedDecl(), D); if (auto* TD = D->getTemplatedDecl(); @@ -969,7 +969,7 @@ void ASTVisitor:: populate( VariableInfo& I, - VarDecl* D) + VarDecl const* D) { // KRYSTIAN FIXME: we need to properly merge storage class if (StorageClass const SC = D->getStorageClass()) @@ -1003,11 +1003,27 @@ populate( populateAttributes(I, D); } +void +ASTVisitor:: +populate(VariableInfo& I, VarTemplateDecl const* D) +{ + populate(I.Template, D->getTemplatedDecl(), D); + populate(I, D->getTemplatedDecl()); +} + +void +ASTVisitor:: +populate(VariableInfo& I, VarTemplateSpecializationDecl const* D) +{ + populate(I.Template, D, D->getSpecializedTemplate()); + populate(I, cast(D)); +} + void ASTVisitor:: populate( FieldInfo& I, - FieldDecl* D) + FieldDecl const* D) { I.Type = toTypeInfo(D->getType()); I.IsVariant = D->getParent()->isUnion(); @@ -1027,27 +1043,11 @@ populate( populateAttributes(I, D); } -void -ASTVisitor:: -populate(VariableInfo& I, VarTemplateDecl* D) -{ - populate(I.Template, D->getTemplatedDecl(), D); - populate(I, D->getTemplatedDecl()); -} - -void -ASTVisitor:: -populate(VariableInfo& I, VarTemplateSpecializationDecl* D) -{ - populate(I.Template, D, D->getSpecializedTemplate()); - populate(I, cast(D)); -} - void ASTVisitor:: populate( SpecializationInfo& I, - ClassTemplateSpecializationDecl* D) + ClassTemplateSpecializationDecl const* D) { CXXRecordDecl const* PD = getInstantiatedFrom(D); populate(I.Args, D->getTemplateArgs().asArray()); @@ -1058,7 +1058,7 @@ void ASTVisitor:: populate( FriendInfo& I, - FriendDecl* D) + FriendDecl const* D) { // A NamedDecl nominated by a FriendDecl // will be one of the following: @@ -1092,7 +1092,7 @@ void ASTVisitor:: populate( GuideInfo& I, - CXXDeductionGuideDecl* D) + CXXDeductionGuideDecl const* D) { I.Deduced = toTypeInfo(D->getReturnType()); for (const ParmVarDecl* P : D->parameters()) @@ -1110,7 +1110,7 @@ void ASTVisitor:: populate( GuideInfo& I, - FunctionTemplateDecl* D) + FunctionTemplateDecl const* D) { populate(I.Template, D->getTemplatedDecl(), D); populate(I, cast(D->getTemplatedDecl())); @@ -1120,7 +1120,7 @@ void ASTVisitor:: populate( NamespaceAliasInfo& I, - NamespaceAliasDecl* D) + NamespaceAliasDecl const* D) { NamedDecl const* Aliased = D->getAliasedNamespace(); NestedNameSpecifier const* NNS = D->getQualifier(); @@ -1132,7 +1132,7 @@ void ASTVisitor:: populate( UsingInfo& I, - UsingDecl* D) + UsingDecl const* D) { I.Class = UsingClass::Normal; I.Qualifier = toNameInfo(D->getQualifier()); @@ -1151,7 +1151,7 @@ void ASTVisitor:: populate( ConceptInfo& I, - ConceptDecl* D) + ConceptDecl const* D) { populate(I.Template, D, D); populate(I.Constraint, D->getConstraintExpr()); @@ -1165,31 +1165,19 @@ template < std::derived_from TemplateDeclTy> void ASTVisitor:: -populate(TemplateInfo& Template, DeclTy*, TemplateDeclTy* TD) +populate(TemplateInfo& Template, DeclTy const*, TemplateDeclTy const* TD) { MRDOCS_ASSERT(TD); TemplateParameterList const* TPL = TD->getTemplateParameters(); populate(Template, TPL); } -template CXXRecordDeclTy> -void -ASTVisitor:: -populate( - TemplateInfo& Template, - CXXRecordDeclTy*, - ClassTemplateDecl* CTD) -{ - MRDOCS_ASSERT(CTD); - populate(Template, CTD->getTemplateParameters()); -} - void ASTVisitor:: populate( TemplateInfo& Template, - ClassTemplateSpecializationDecl* CTSD, - ClassTemplateDecl* CTD) + ClassTemplateSpecializationDecl const* CTSD, + ClassTemplateDecl const* CTD) { MRDOCS_ASSERT(CTD); @@ -1197,9 +1185,12 @@ populate( generateID(getInstantiatedFrom(CTD), Template.Primary); // Extract the template arguments of the specialization - if (const auto* argsAsWritten = CTSD->getTemplateArgsAsWritten()) { + if (const auto* argsAsWritten = CTSD->getTemplateArgsAsWritten()) + { populate(Template.Args, argsAsWritten); - } else { + } + else + { // Implicit specializations do not have template arguments as written populate(Template.Args, CTSD->getTemplateArgs().asArray()); } @@ -1238,7 +1229,6 @@ populate( TemplateParameterList* params = CTPSD->getTemplateParameters(); populate(Template, params); } - } template VarDeclTy> @@ -1246,8 +1236,8 @@ void ASTVisitor:: populate( TemplateInfo& Template, - VarDeclTy* D, - VarTemplateDecl* VTD) + VarDeclTy const* D, + VarTemplateDecl const* VTD) { MRDOCS_ASSERT(VTD); @@ -1258,8 +1248,10 @@ populate( // extract the template arguments of the specialization populate(Template.Args, VTSD->getTemplateArgsAsWritten()); // extract the template parameters if this is a partial specialization - if(auto* VTPSD = dyn_cast(D)) + if (auto* VTPSD = dyn_cast(D)) + { populate(Template, VTPSD->getTemplateParameters()); + } } else { @@ -1268,12 +1260,11 @@ populate( } } - void ASTVisitor:: populate( NoexceptInfo& I, - const FunctionProtoType* FPT) + FunctionProtoType const* FPT) { MRDOCS_ASSERT(FPT); I.Implicit = ! FPT->hasNoexceptExceptionSpec(); @@ -1545,7 +1536,7 @@ populate( template InfoTy> void ASTVisitor:: -populateAttributes(InfoTy& I, const Decl* D) +populateAttributes(InfoTy& I, Decl const* D) { if constexpr (requires { I.Attributes; }) { @@ -2880,7 +2871,7 @@ find(SymbolID const& id) Info* ASTVisitor:: -find(Decl* D) +find(Decl const* D) { auto ID = generateID(D); MRDOCS_CHECK_OR(ID, nullptr); @@ -3058,7 +3049,7 @@ Expected< InfoTypeFor_t, InfoTy>>> ASTVisitor:: -upsert(DeclType* D) +upsert(DeclType const* D) { ExtractionMode const m = checkFilters(D); if (m == ExtractionMode::Dependency) diff --git a/src/lib/AST/ASTVisitor.hpp b/src/lib/AST/ASTVisitor.hpp index f0943d225..f348e9e71 100644 --- a/src/lib/AST/ASTVisitor.hpp +++ b/src/lib/AST/ASTVisitor.hpp @@ -313,7 +313,7 @@ class ASTVisitor class InfoTy = void, std::derived_from DeclTy> Info* - traverse(DeclTy* D); + traverse(DeclTy const* D); /* Traverse a function template @@ -323,7 +323,7 @@ class ASTVisitor */ Info* - traverse(FunctionTemplateDecl* D); + traverse(FunctionTemplateDecl const* D); /* Traverse a using directive @@ -331,10 +331,10 @@ class ASTVisitor such as `using namespace std;`. If the parent declaration is a Namespace, we - update its `UsingDirectives` field. + update its `UsingDirectives` const field. */ Info* - traverse(UsingDirectiveDecl* D); + traverse(UsingDirectiveDecl const* D); /* Traverse a member of an anonymous union. @@ -342,7 +342,7 @@ class ASTVisitor as a regular `FieldDecl`. */ Info* - traverse(IndirectFieldDecl*); + traverse(IndirectFieldDecl const* D); // ================================================= // AST Traversal Helpers @@ -361,13 +361,13 @@ class ASTVisitor std::derived_from DeclTy> requires (!std::derived_from) void - traverseMembers(InfoTy& I, DeclTy* DC); + traverseMembers(InfoTy& I, DeclTy const* DC); template < std::derived_from InfoTy, std::derived_from DeclTy> void - traverseMembers(InfoTy& I, DeclTy* DC); + traverseMembers(InfoTy& I, DeclTy const* DC); /* Traverse the parents of a declaration @@ -388,13 +388,13 @@ class ASTVisitor std::derived_from DeclTy> requires (!std::derived_from) void - traverseParents(InfoTy& I, DeclTy* DC); + traverseParents(InfoTy& I, DeclTy const* DC); template < std::derived_from InfoTy, std::derived_from DeclTy> void - traverseParents(InfoTy& I, DeclTy* DC); + traverseParents(InfoTy& I, DeclTy const* DC); /* Generates a Unified Symbol Resolution value for a declaration. @@ -406,7 +406,7 @@ class ASTVisitor @returns true if USR generation succeeded. */ Expected> - generateUSR(const Decl* D) const; + generateUSR(Decl const* D) const; /* Generate the symbol ID for a declaration. @@ -426,7 +426,7 @@ class ASTVisitor @return true if the symbol ID could be extracted. */ bool - generateID(const Decl* D, SymbolID& id) const; + generateID(Decl const* D, SymbolID& id) const; /* Extracts the symbol ID for a declaration. @@ -439,102 +439,102 @@ class ASTVisitor @return the symbol ID for the declaration. */ SymbolID - generateID(const Decl* D) const; + generateID(Decl const* D) const; // ================================================= // Populate functions // ================================================= template DeclTy> void - populate(Info& I, bool isNew, DeclTy* D); + populate(Info& I, bool isNew, DeclTy const* D); void populate(SourceInfo& I, clang::SourceLocation loc, bool definition, bool documented); void - populate(NamespaceInfo& I, NamespaceDecl* D); + populate(NamespaceInfo& I, NamespaceDecl const* D); static void - populate(NamespaceInfo& I, TranslationUnitDecl* D); + populate(NamespaceInfo& I, TranslationUnitDecl const* D); void - populate(RecordInfo& I, CXXRecordDecl* D); + populate(RecordInfo& I, CXXRecordDecl const* D); void - populate(RecordInfo& I, ClassTemplateDecl* D); + populate(RecordInfo& I, ClassTemplateDecl const* D); void - populate(RecordInfo& I, ClassTemplateSpecializationDecl* D); + populate(RecordInfo& I, ClassTemplateSpecializationDecl const* D); void - populate(FunctionInfo& I, FunctionDecl* D); + populate(FunctionInfo& I, FunctionDecl const* D); void - populate(FunctionInfo& I, FunctionTemplateDecl* D); + populate(FunctionInfo& I, FunctionTemplateDecl const* D); void - populate(FunctionInfo& I, CXXMethodDecl* D); + populate(FunctionInfo& I, CXXMethodDecl const* D); void - populate(FunctionInfo& I, CXXConstructorDecl* D); + populate(FunctionInfo& I, CXXConstructorDecl const* D); void - populate(FunctionInfo& I, CXXDestructorDecl* D); + populate(FunctionInfo& I, CXXDestructorDecl const* D); void - populate(FunctionInfo& I, CXXConversionDecl* D); + populate(FunctionInfo& I, CXXConversionDecl const* D); void - populate(EnumInfo& I, EnumDecl* D); + populate(EnumInfo& I, EnumDecl const* D); void - populate(EnumConstantInfo& I, EnumConstantDecl* D); + populate(EnumConstantInfo& I, EnumConstantDecl const* D); void - populate(TypedefInfo& I, TypedefNameDecl* D); + populate(TypedefInfo& I, TypedefNameDecl const* D); void - populate(TypedefInfo& I, TypedefDecl* D); + populate(TypedefInfo& I, TypedefDecl const* D); void - populate(TypedefInfo& I, TypeAliasDecl* D); + populate(TypedefInfo& I, TypeAliasDecl const* D); void - populate(TypedefInfo& I, TypeAliasTemplateDecl* D); + populate(TypedefInfo& I, TypeAliasTemplateDecl const* D); void - populate(VariableInfo& I, VarDecl* D); + populate(VariableInfo& I, VarDecl const* D); void - populate(VariableInfo& I, VarTemplateDecl* D); + populate(VariableInfo& I, VarTemplateDecl const* D); void - populate(VariableInfo& I, VarTemplateSpecializationDecl* D); + populate(VariableInfo& I, VarTemplateSpecializationDecl const* D); void - populate(FieldInfo& I, FieldDecl* D); + populate(FieldInfo& I, FieldDecl const* D); void - populate(SpecializationInfo& I, ClassTemplateSpecializationDecl* D); + populate(SpecializationInfo& I, ClassTemplateSpecializationDecl const* D); void - populate(FriendInfo& I, FriendDecl* D); + populate(FriendInfo& I, FriendDecl const* D); void - populate(GuideInfo& I, CXXDeductionGuideDecl* D); + populate(GuideInfo& I, CXXDeductionGuideDecl const* D); void - populate(GuideInfo& I, FunctionTemplateDecl* D); + populate(GuideInfo& I, FunctionTemplateDecl const* D); void - populate(NamespaceAliasInfo& I, NamespaceAliasDecl* D); + populate(NamespaceAliasInfo& I, NamespaceAliasDecl const* D); void - populate(UsingInfo& I, UsingDecl* D); + populate(UsingInfo& I, UsingDecl const* D); void - populate(ConceptInfo& I, ConceptDecl* D); + populate(ConceptInfo& I, ConceptDecl const* D); /* Default function to populate the template information @@ -546,19 +546,10 @@ class ASTVisitor std::derived_from DeclTy, std::derived_from TemplateDeclTy> void - populate(TemplateInfo& Template, DeclTy* D, TemplateDeclTy* TD); + populate(TemplateInfo& Template, DeclTy const* D, TemplateDeclTy const* TD); - /* Populate the template information for a class template - - The function will populate the template parameters - depending on whether the record is a specialization. - */ - template CXXRecordDeclTy> void - populate(TemplateInfo& Template, CXXRecordDeclTy*, ClassTemplateDecl* CTD); - - void - populate(TemplateInfo& Template, ClassTemplateSpecializationDecl* D, ClassTemplateDecl* CTD); + populate(TemplateInfo& Template, ClassTemplateSpecializationDecl const* D, ClassTemplateDecl const* CTD); /* Populate the template information for a variable template @@ -567,13 +558,13 @@ class ASTVisitor */ template VarDeclTy> void - populate(TemplateInfo& Template, VarDeclTy* D, VarTemplateDecl* VTD); + populate(TemplateInfo& Template, VarDeclTy const* D, VarTemplateDecl const* VTD); template< std::derived_from DeclTy, std::derived_from TemplateDeclTy> void - populate(std::optional& Template, DeclTy* D, TemplateDeclTy* VTD) + populate(std::optional& Template, DeclTy const* D, TemplateDeclTy const* VTD) { if (!Template) { @@ -584,27 +575,28 @@ class ASTVisitor } void - populate(NoexceptInfo& I, const FunctionProtoType* FPT); + populate(NoexceptInfo& I, FunctionProtoType const* FPT); void - populate(ExplicitInfo& I, const ExplicitSpecifier& ES); + populate(ExplicitInfo& I, ExplicitSpecifier const& ES); void - populate(ExprInfo& I, const Expr* E); + populate(ExprInfo& I, Expr const* E); template void - populate(ConstantExprInfo& I, const Expr* E); + populate(ConstantExprInfo& I, Expr const* E); template void - populate(ConstantExprInfo& I, const Expr* E, const llvm::APInt& V); + populate(ConstantExprInfo& I, Expr const* E, llvm::APInt const& V); void - populate(PolymorphicValue& I, const NamedDecl* N); + populate(PolymorphicValue& I, NamedDecl const* N); void - populate(std::optional& TI, const TemplateParameterList* TPL) { + populate(std::optional& TI, TemplateParameterList const* TPL) + { if (!TI) { TI.emplace(); @@ -613,7 +605,7 @@ class ASTVisitor } void - populate(TemplateInfo& TI, const TemplateParameterList* TPL); + populate(TemplateInfo& TI, TemplateParameterList const* TPL); template Range> void @@ -644,7 +636,7 @@ class ASTVisitor void populate( std::vector>& result, - const ASTTemplateArgumentListInfo* args); + ASTTemplateArgumentListInfo const* args); template InfoTy> static @@ -690,7 +682,7 @@ class ASTVisitor toTypeInfo(QualType qt, TraversalMode mode); PolymorphicValue - toTypeInfo(QualType qt) + toTypeInfo(QualType const qt) { return toTypeInfo(qt, TraversalMode::Dependency); } @@ -704,29 +696,29 @@ class ASTVisitor toNameInfo( DeclarationName Name, std::optional TArgs = std::nullopt, - const NestedNameSpecifier* NNS = nullptr); + NestedNameSpecifier const* NNS = nullptr); template > PolymorphicValue toNameInfo( - const Decl* D, + Decl const* D, std::optional TArgs = std::nullopt, - const NestedNameSpecifier* NNS = nullptr); + NestedNameSpecifier const* NNS = nullptr); PolymorphicValue - toTArg(const TemplateArgument& A); + toTArg(TemplateArgument const& A); // Pretty-print an expression std::string - toString(const Expr* E); + toString(Expr const* E); // Pretty-print a type std::string - toString(const Type* T); + toString(Type const* T); template Integer - toInteger(const llvm::APInt& V); + toInteger(llvm::APInt const& V); /* Get the original source code in a range @@ -996,7 +988,7 @@ class ASTVisitor to get the Info object for the declaration. */ Info* - find(Decl* D); + find(Decl const* D); /* Find or traverse a declaration @@ -1027,7 +1019,7 @@ class ASTVisitor @return a pointer to the Info object. */ Info* - findOrTraverse(Decl* D) + findOrTraverse(Decl const* D) { MRDOCS_CHECK_OR(D, nullptr); if (auto* I = find(D)) @@ -1145,7 +1137,7 @@ class ASTVisitor std::same_as, InfoTypeFor_t, InfoTy>>> - upsert(DeclType* D); + upsert(DeclType const* D); }; } // clang::mrdocs diff --git a/src/lib/AST/ClangHelpers.cpp b/src/lib/AST/ClangHelpers.cpp index 529e31dd5..7e66a10cc 100644 --- a/src/lib/AST/ClangHelpers.cpp +++ b/src/lib/AST/ClangHelpers.cpp @@ -213,8 +213,8 @@ getNTTPFromExpr(const Expr* E, unsigned const Depth) return NTTPD; } -Decl* -getParent(Decl* D) +Decl const* +getParent(Decl const* D) { while((D = cast_if_present< Decl>(D->getDeclContext()))) diff --git a/src/lib/AST/ClangHelpers.hpp b/src/lib/AST/ClangHelpers.hpp index c65027195..b99c71b65 100644 --- a/src/lib/AST/ClangHelpers.hpp +++ b/src/lib/AST/ClangHelpers.hpp @@ -706,15 +706,14 @@ TypeLocToKind() user-written `Decl` will be the `std::vector` template. */ template -DeclTy* -getInstantiatedFrom(DeclTy* D) +DeclTy const* +getInstantiatedFrom(DeclTy const* D) { if (!D) { return nullptr; } - auto* decayedD = const_cast(static_cast(D)); - Decl* resultDecl = InstantiatedFromVisitor().Visit(decayedD); + Decl const* resultDecl = InstantiatedFromVisitor().Visit(D); return cast(resultDecl); } @@ -722,8 +721,8 @@ template requires std::derived_from || std::same_as> -FunctionDecl* -getInstantiatedFrom(DeclTy* D) +FunctionDecl const* +getInstantiatedFrom(DeclTy const* D) { return dyn_cast_if_present( getInstantiatedFrom(D)); @@ -733,8 +732,8 @@ template requires std::derived_from || std::same_as> -CXXRecordDecl* -getInstantiatedFrom(DeclTy* D) +CXXRecordDecl const* +getInstantiatedFrom(DeclTy const* D) { return dyn_cast_if_present( getInstantiatedFrom(D)); @@ -744,8 +743,8 @@ template requires std::derived_from || std::same_as> -VarDecl* -getInstantiatedFrom(DeclTy* D) +VarDecl const* +getInstantiatedFrom(DeclTy const* D) { return dyn_cast_if_present( getInstantiatedFrom(D)); @@ -755,8 +754,8 @@ template requires std::derived_from || std::same_as> -TypedefNameDecl* -getInstantiatedFrom(DeclTy* D) +TypedefNameDecl const* +getInstantiatedFrom(DeclTy const* D) { return dyn_cast_if_present( getInstantiatedFrom(D)); @@ -769,11 +768,11 @@ getInstantiatedFrom(DeclTy* D) */ MRDOCS_DECL AccessSpecifier -getAccess(const Decl* D); +getAccess(Decl const* D); MRDOCS_DECL QualType -getDeclaratorType(const DeclaratorDecl* DD); +getDeclaratorType(DeclaratorDecl const* DD); /** Get the NonTypeTemplateParm of an expression @@ -797,19 +796,12 @@ getDeclaratorType(const DeclaratorDecl* DD); */ MRDOCS_DECL NonTypeTemplateParmDecl const* -getNTTPFromExpr(const Expr* E, unsigned Depth); +getNTTPFromExpr(Expr const* E, unsigned Depth); // Get the parent declaration of a declaration MRDOCS_DECL -Decl* -getParent(Decl* D); - -// Get the parent declaration of a declaration -inline Decl const* -getParent(Decl const* D) { - return getParent(const_cast(D)); -} +getParent(Decl const* D); MRDOCS_DECL void diff --git a/src/lib/AST/InstantiatedFromVisitor.hpp b/src/lib/AST/InstantiatedFromVisitor.hpp index 4872997d6..f284ba637 100644 --- a/src/lib/AST/InstantiatedFromVisitor.hpp +++ b/src/lib/AST/InstantiatedFromVisitor.hpp @@ -24,16 +24,16 @@ namespace clang::mrdocs { and retrieve the original declarations from which they were instantiated. */ class InstantiatedFromVisitor - : public DeclVisitor + : public ConstDeclVisitor { public: - Decl* - VisitDecl(Decl* D) + Decl const* + VisitDecl(Decl const* D) { return D; } - FunctionDecl* + FunctionDecl const* VisitFunctionTemplateDecl(FunctionTemplateDecl const* D) { while(auto* MT = D->getInstantiatedFromMemberTemplate()) @@ -47,7 +47,7 @@ class InstantiatedFromVisitor return D->getTemplatedDecl(); } - CXXRecordDecl* + CXXRecordDecl const* VisitClassTemplateDecl(ClassTemplateDecl const* D) { while (auto* MT = D->getInstantiatedFromMemberTemplate()) @@ -61,7 +61,7 @@ class InstantiatedFromVisitor return D->getTemplatedDecl(); } - VarDecl* + VarDecl const* VisitVarTemplateDecl(VarTemplateDecl const* D) { while(auto* MT = D->getInstantiatedFromMemberTemplate()) @@ -75,7 +75,7 @@ class InstantiatedFromVisitor return D->getTemplatedDecl(); } - TypedefNameDecl* + TypedefNameDecl const* VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl const* D) { if(auto* MT = D->getInstantiatedFromMemberTemplate()) @@ -89,8 +89,8 @@ class InstantiatedFromVisitor return VisitTypedefNameDecl(D->getTemplatedDecl()); } - FunctionDecl* - VisitFunctionDecl(FunctionDecl* D) + FunctionDecl const* + VisitFunctionDecl(FunctionDecl const* D) { if (FunctionDecl const* DD = nullptr; D->isDefined(DD, false)) @@ -117,8 +117,8 @@ class InstantiatedFromVisitor return D; } - CXXRecordDecl* - VisitClassTemplatePartialSpecializationDecl(ClassTemplatePartialSpecializationDecl* D) + CXXRecordDecl const* + VisitClassTemplatePartialSpecializationDecl(ClassTemplatePartialSpecializationDecl const* D) { while (auto* MT = D->getInstantiatedFromMember()) { @@ -131,8 +131,8 @@ class InstantiatedFromVisitor return VisitClassTemplateSpecializationDecl(D); } - CXXRecordDecl* - VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl* D) + CXXRecordDecl const* + VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl const* D) { if (!D->isExplicitSpecialization()) { @@ -152,8 +152,8 @@ class InstantiatedFromVisitor return VisitCXXRecordDecl(D); } - CXXRecordDecl* - VisitCXXRecordDecl(CXXRecordDecl* D) + CXXRecordDecl const* + VisitCXXRecordDecl(CXXRecordDecl const* D) { while (MemberSpecializationInfo const* MSI = D->getMemberSpecializationInfo()) @@ -169,8 +169,8 @@ class InstantiatedFromVisitor return D; } - VarDecl* - VisitVarTemplatePartialSpecializationDecl(VarTemplatePartialSpecializationDecl* D) + VarDecl const* + VisitVarTemplatePartialSpecializationDecl(VarTemplatePartialSpecializationDecl const* D) { while(auto* MT = D->getInstantiatedFromMember()) { @@ -183,8 +183,8 @@ class InstantiatedFromVisitor return VisitVarTemplateSpecializationDecl(D); } - VarDecl* - VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl* D) + VarDecl const* + VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl const* D) { if(! D->isExplicitSpecialization()) { @@ -205,8 +205,8 @@ class InstantiatedFromVisitor return VisitVarDecl(D); } - VarDecl* - VisitVarDecl(VarDecl* D) + VarDecl const* + VisitVarDecl(VarDecl const* D) { while(MemberSpecializationInfo* MSI = D->getMemberSpecializationInfo()) @@ -220,8 +220,8 @@ class InstantiatedFromVisitor return D; } - EnumDecl* - VisitEnumDecl(EnumDecl* D) + EnumDecl const* + VisitEnumDecl(EnumDecl const* D) { while(MemberSpecializationInfo* MSI = D->getMemberSpecializationInfo()) @@ -235,16 +235,18 @@ class InstantiatedFromVisitor return D; } - TypedefNameDecl* - VisitTypedefNameDecl(TypedefNameDecl* D) + TypedefNameDecl const* + VisitTypedefNameDecl(TypedefNameDecl const* D) { - DeclContext* Context = D->getNonTransparentDeclContext(); + DeclContext const* Context = D->getNonTransparentDeclContext(); if (Context->isFileContext()) { return D; } - auto const* ContextPattern = - cast(Visit(cast(Context))); + Decl const* ContextDecl = Decl::castFromDeclContext(Context); + Decl const* ContextInstatiationContextDecl = Visit(ContextDecl); + DeclContext const* ContextPattern = + Decl::castToDeclContext(ContextInstatiationContextDecl); if (Context == ContextPattern) { return D; @@ -252,7 +254,7 @@ class InstantiatedFromVisitor for (auto lookup = ContextPattern->lookup(D->getDeclName()); NamedDecl * ND : lookup) { - if (auto* TND = dyn_cast(ND)) + if (auto const* TND = dyn_cast(ND)) { return TND; }