Skip to content

Commit f225a4f

Browse files
committed
InfoTypeFor trait
#refactor
1 parent 938b673 commit f225a4f

File tree

3 files changed

+66
-36
lines changed

3 files changed

+66
-36
lines changed

src/lib/AST/ASTVisitor.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -348,22 +348,20 @@ defaultTraverseImpl(DeclTy* D, TemplateDeclTy* TD)
348348

349349
// If D is also a template declaration, we extract
350350
// the template information from the declaration itself
351-
if constexpr (!PopulateFromTD && std::derived_from<DeclTy, TemplateDecl>)
352-
{
353-
defaultTraverseImpl<true>(D, D);
354-
return;
355-
}
356-
357-
using InfoT = MrDocsType_t<DeclTy>;
358-
static constexpr bool HasMrDocsInfoType = !std::same_as<InfoT, Info>;
359-
if constexpr (!HasMrDocsInfoType)
351+
if constexpr (!HasInfoTypeFor<DeclTy>)
360352
{
361353
// Traverse the members of the declaration even if
362354
// the declaration does not have a corresponding Info type
363355
traverseMembers(D);
364356
}
365357
else
366358
{
359+
if constexpr (!PopulateFromTD && std::derived_from<DeclTy, TemplateDecl>)
360+
{
361+
defaultTraverseImpl<true>(D, D);
362+
return;
363+
}
364+
367365
// If the declaration has a corresponding Info type,
368366
// we build the Info object and populate it with the
369367
// necessary information.
@@ -2805,7 +2803,7 @@ upsert(SymbolID const& id)
28052803
}
28062804

28072805
template <std::derived_from<Decl> DeclType>
2808-
Expected<ASTVisitor::upsertResult<MrDocsType_t<DeclType>>>
2806+
Expected<ASTVisitor::upsertResult<InfoTypeFor_t<DeclType>>>
28092807
ASTVisitor::
28102808
upsert(DeclType* D)
28112809
{
@@ -2817,10 +2815,10 @@ upsert(DeclType* D)
28172815
SymbolID const id = generateID(D);
28182816
MRDOCS_CHECK_MSG(id, "Failed to extract symbol ID");
28192817

2820-
auto [I, isNew] = upsert<MrDocsType_t<DeclType>>(id);
2818+
auto [I, isNew] = upsert<InfoTypeFor_t<DeclType>>(id);
28212819
I.Access = convertToAccessKind(access);
28222820

2823-
using R = upsertResult<MrDocsType_t<DeclType>>;
2821+
using R = upsertResult<InfoTypeFor_t<DeclType>>;
28242822
return R{std::ref(I), isNew};
28252823
}
28262824

src/lib/AST/ASTVisitor.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ class ASTVisitor
879879
@param D The declaration to extract
880880
*/
881881
template <std::derived_from<Decl> DeclType>
882-
Expected<upsertResult<MrDocsType_t<DeclType>>>
882+
Expected<upsertResult<InfoTypeFor_t<DeclType>>>
883883
upsert(DeclType* D);
884884

885885
/* Get or construct an empty Info for a dependency declaration.

src/lib/AST/ClangHelpers.hpp

+55-23
Original file line numberDiff line numberDiff line change
@@ -63,53 +63,85 @@ SubstituteConstraintExpressionWithoutSatisfaction(
6363
6464
When there's no direct correspondence, this trait returns
6565
the base Info type.
66-
6766
*/
68-
template <class DeclType>
69-
struct MrDocsType : std::type_identity<Info> {};
67+
template <class>
68+
struct InfoTypeFor {};
7069

71-
template <std::derived_from<CXXRecordDecl> DeclType>
72-
struct MrDocsType<DeclType> : std::type_identity<RecordInfo> {};
70+
// Extract NamespaceInfo from NamespaceDecl
71+
template <>
72+
struct InfoTypeFor<NamespaceDecl>
73+
: std::type_identity<NamespaceInfo> {};
7374

74-
template <std::derived_from<VarDecl> VarTy>
75-
struct MrDocsType<VarTy> : std::type_identity<VariableInfo> {};
75+
// Extract RecordInfo from anything derived from CXXRecordDecl
76+
template <std::derived_from<CXXRecordDecl> DeclType>
77+
struct InfoTypeFor<DeclType>
78+
: std::type_identity<RecordInfo> {};
7679

80+
// Extract FunctionInfo from anything derived from FunctionDecl
7781
template <std::derived_from<FunctionDecl> FunctionTy>
78-
struct MrDocsType<FunctionTy> : std::type_identity<FunctionInfo> {};
79-
80-
template <std::derived_from<TypedefNameDecl> TypedefNameTy>
81-
struct MrDocsType<TypedefNameTy> : std::type_identity<TypedefInfo> {};
82+
struct InfoTypeFor<FunctionTy>
83+
: std::type_identity<FunctionInfo> {};
8284

85+
// Extract EnumInfo from EnumDecl
8386
template <>
84-
struct MrDocsType<EnumDecl> : std::type_identity<EnumInfo> {};
87+
struct InfoTypeFor<EnumDecl>
88+
: std::type_identity<EnumInfo> {};
8589

90+
// Extract EnumConstantInfo from EnumConstantDecl
8691
template <>
87-
struct MrDocsType<FieldDecl> : std::type_identity<FieldInfo> {};
92+
struct InfoTypeFor<EnumConstantDecl>
93+
: std::type_identity<EnumConstantInfo> {};
8894

89-
template <>
90-
struct MrDocsType<EnumConstantDecl> : std::type_identity<EnumConstantInfo> {};
95+
// Extract TypedefInfo from anything derived from TypedefNameDecl
96+
template <std::derived_from<TypedefNameDecl> TypedefNameTy>
97+
struct InfoTypeFor<TypedefNameTy>
98+
: std::type_identity<TypedefInfo> {};
9199

100+
// Extract VariableInfo from anything derived from VarDecl
101+
template <std::derived_from<VarDecl> VarTy>
102+
struct InfoTypeFor<VarTy>
103+
: std::type_identity<VariableInfo> {};
104+
105+
// Extract FieldInfo from FieldDecl
92106
template <>
93-
struct MrDocsType<FriendDecl> : std::type_identity<FriendInfo> {};
107+
struct InfoTypeFor<FieldDecl>
108+
: std::type_identity<FieldInfo> {};
94109

110+
// Extract FriendInfo from FriendDecl
95111
template <>
96-
struct MrDocsType<CXXDeductionGuideDecl> : std::type_identity<GuideInfo> {};
112+
struct InfoTypeFor<FriendDecl>
113+
: std::type_identity<FriendInfo> {};
97114

115+
// Extract GuideInfo from CXXDeductionGuideDecl
98116
template <>
99-
struct MrDocsType<NamespaceAliasDecl> : std::type_identity<NamespaceAliasInfo> {};
117+
struct InfoTypeFor<CXXDeductionGuideDecl>
118+
: std::type_identity<GuideInfo> {};
100119

120+
// Extract NamespaceAliasInfo from NamespaceAliasDecl
101121
template <>
102-
struct MrDocsType<UsingDecl> : std::type_identity<UsingInfo> {};
122+
struct InfoTypeFor<NamespaceAliasDecl>
123+
: std::type_identity<NamespaceAliasInfo> {};
103124

125+
// Extract UsingInfo from UsingDecl
104126
template <>
105-
struct MrDocsType<NamespaceDecl> : std::type_identity<NamespaceInfo>{};
127+
struct InfoTypeFor<UsingDecl>
128+
: std::type_identity<UsingInfo> {};
106129

130+
// Extract ConceptInfo from ConceptDecl
107131
template <>
108-
struct MrDocsType<ConceptDecl> : std::type_identity<ConceptInfo>{};
132+
struct InfoTypeFor<ConceptDecl>
133+
: std::type_identity<ConceptInfo> {};
134+
135+
/// Determine if there's a MrDocs Info type for a Clang DeclType
136+
template <class T>
137+
concept HasInfoTypeFor = requires
138+
{
139+
typename InfoTypeFor<T>::type;
140+
};
109141

110-
/// @copydoc MrDocsType
142+
/// @copydoc InfoTypeFor
111143
template <class DeclType>
112-
using MrDocsType_t = typename MrDocsType<DeclType>::type;
144+
using InfoTypeFor_t = typename InfoTypeFor<DeclType>::type;
113145

114146
/** Convert a Clang AccessSpecifier into a MrDocs AccessKind
115147
*/

0 commit comments

Comments
 (0)