Skip to content

Commit c0fd3fc

Browse files
committed
feat: TArg stores types
1 parent 2cc093d commit c0fd3fc

39 files changed

+714
-369
lines changed

include/mrdox/Metadata/Specialization.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct SpecializationInfo
5050
: IsInfo<InfoKind::Specialization>
5151
{
5252
/** The template arguments the parent template is specialized for */
53-
std::vector<TArg> Args;
53+
std::vector<std::unique_ptr<TArg>> Args;
5454

5555
/** ID of the template to which the arguments pertain */
5656
SymbolID Primary = SymbolID::zero;

include/mrdox/Metadata/Template.hpp

+125-30
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,123 @@
1616
#include <mrdox/Platform.hpp>
1717
#include <mrdox/ADT/Optional.hpp>
1818
#include <mrdox/Metadata/Type.hpp>
19+
#include <mrdox/Support/TypeTraits.hpp>
1920
#include <optional>
2021
#include <string>
2122
#include <vector>
2223

2324
namespace clang {
2425
namespace mrdox {
2526

27+
enum class TArgKind : int
28+
{
29+
// type arguments
30+
Type = 1, // for bitstream
31+
// non-type arguments, i.e. expressions
32+
NonType,
33+
// template template arguments, i.e. template names
34+
Template
35+
};
36+
37+
MRDOX_DECL dom::String toString(TArgKind kind) noexcept;
38+
39+
struct TArg
40+
{
41+
/** The kind of template argument this is. */
42+
TArgKind Kind;
43+
44+
/** Whether this template argument is a parameter expansion. */
45+
bool IsPackExpansion = false;
46+
47+
constexpr virtual ~TArg() = default;
48+
49+
constexpr bool isType() const noexcept { return Kind == TArgKind::Type; }
50+
constexpr bool isNonType() const noexcept { return Kind == TArgKind::NonType; }
51+
constexpr bool isTemplate() const noexcept { return Kind == TArgKind::Template; }
52+
53+
protected:
54+
constexpr
55+
TArg(
56+
TArgKind kind) noexcept
57+
: Kind(kind)
58+
{
59+
}
60+
};
61+
62+
template<TArgKind K>
63+
struct IsTArg : TArg
64+
{
65+
static constexpr TArgKind kind_id = K;
66+
67+
static constexpr bool isType() noexcept { return K == TArgKind::Type; }
68+
static constexpr bool isNonType() noexcept { return K == TArgKind::NonType; }
69+
static constexpr bool isTemplate() noexcept { return K == TArgKind::Template; }
70+
71+
protected:
72+
constexpr
73+
IsTArg() noexcept
74+
: TArg(K)
75+
{
76+
}
77+
};
78+
79+
struct TypeTArg
80+
: IsTArg<TArgKind::Type>
81+
{
82+
/** Template argument type. */
83+
std::unique_ptr<TypeInfo> Type;
84+
};
85+
86+
struct NonTypeTArg
87+
: IsTArg<TArgKind::NonType>
88+
{
89+
/** Template argument expression. */
90+
ExprInfo Value;
91+
};
92+
93+
struct TemplateTArg
94+
: IsTArg<TArgKind::Template>
95+
{
96+
/** SymbolID of the referenced template. */
97+
SymbolID Template;
98+
99+
/** Name of the referenced template. */
100+
std::string Name;
101+
};
102+
103+
template<
104+
typename TArgTy,
105+
typename F,
106+
typename... Args>
107+
requires std::derived_from<TArgTy, TArg>
108+
constexpr
109+
decltype(auto)
110+
visit(
111+
TArgTy& A,
112+
F&& f,
113+
Args&&... args)
114+
{
115+
switch(A.Kind)
116+
{
117+
case TArgKind::Type:
118+
return f(static_cast<add_cv_from_t<
119+
TArgTy, TypeTArg>&>(A),
120+
std::forward<Args>(args)...);
121+
case TArgKind::NonType:
122+
return f(static_cast<add_cv_from_t<
123+
TArgTy, NonTypeTArg>&>(A),
124+
std::forward<Args>(args)...);
125+
case TArgKind::Template:
126+
return f(static_cast<add_cv_from_t<
127+
TArgTy, TemplateTArg>&>(A),
128+
std::forward<Args>(args)...);
129+
default:
130+
MRDOX_UNREACHABLE();
131+
}
132+
}
133+
134+
// ----------------------------------------------------------------
135+
26136
enum class TParamKind : int
27137
{
28138
// template type parameter, e.g. "typename T" or "class T"
@@ -103,48 +213,31 @@ struct TemplateTParam
103213
Optional<std::string> Default;
104214
};
105215

106-
template<typename F, typename... Args>
216+
template<
217+
typename TParamTy,
218+
typename F,
219+
typename... Args>
220+
requires std::derived_from<TParamTy, TParam>
107221
constexpr
108222
decltype(auto)
109223
visit(
110-
TParam& P,
224+
TParamTy& P,
111225
F&& f,
112226
Args&&... args)
113227
{
114228
switch(P.Kind)
115229
{
116230
case TParamKind::Type:
117-
return f(static_cast<TypeTParam&>(P),
231+
return f(static_cast<add_cv_from_t<
232+
TParamTy, TypeTParam>&>(P),
118233
std::forward<Args>(args)...);
119234
case TParamKind::NonType:
120-
return f(static_cast<NonTypeTParam&>(P),
235+
return f(static_cast<add_cv_from_t<
236+
TParamTy, NonTypeTParam>&>(P),
121237
std::forward<Args>(args)...);
122238
case TParamKind::Template:
123-
return f(static_cast<TemplateTParam&>(P),
124-
std::forward<Args>(args)...);
125-
default:
126-
MRDOX_UNREACHABLE();
127-
}
128-
}
129-
130-
template<typename F, typename... Args>
131-
constexpr
132-
decltype(auto)
133-
visit(
134-
const TParam& P,
135-
F&& f,
136-
Args&&... args)
137-
{
138-
switch(P.Kind)
139-
{
140-
case TParamKind::Type:
141-
return f(static_cast<const TypeTParam&>(P),
142-
std::forward<Args>(args)...);
143-
case TParamKind::NonType:
144-
return f(static_cast<const NonTypeTParam&>(P),
145-
std::forward<Args>(args)...);
146-
case TParamKind::Template:
147-
return f(static_cast<const TemplateTParam&>(P),
239+
return f(static_cast<add_cv_from_t<
240+
TParamTy, TemplateTParam>&>(P),
148241
std::forward<Args>(args)...);
149242
default:
150243
MRDOX_UNREACHABLE();
@@ -153,6 +246,7 @@ visit(
153246

154247
// ----------------------------------------------------------------
155248

249+
#if 0
156250
struct TArg
157251
{
158252
std::string Value;
@@ -162,6 +256,7 @@ struct TArg
162256
MRDOX_DECL
163257
TArg(std::string&& value);
164258
};
259+
#endif
165260

166261
// ----------------------------------------------------------------
167262

@@ -191,7 +286,7 @@ struct TemplateInfo
191286
once in Args outside of a non-deduced context
192287
*/
193288
std::vector<std::unique_ptr<TParam>> Params;
194-
std::vector<TArg> Args;
289+
std::vector<std::unique_ptr<TArg>> Args;
195290

196291
/** Primary template ID for partial and explicit specializations.
197292
*/

include/mrdox/Metadata/Type.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ struct SpecializationTypeInfo
123123
std::unique_ptr<TypeInfo> ParentType;
124124
std::string Name;
125125
SymbolID id = SymbolID::zero;
126-
std::vector<TArg> TemplateArgs;
126+
std::vector<std::unique_ptr<TArg>> TemplateArgs;
127127
};
128128

129129
struct LValueReferenceTypeInfo

include/mrdox/MetadataFwd.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ struct ConstantExprInfo;
6060

6161
struct TemplateInfo;
6262
struct TArg;
63+
struct TypeTArg;
64+
struct NonTypeTArg;
65+
struct TemplateTArg;
6366
struct TParam;
6467
struct TypeTParam;
6568
struct NonTypeTParam;

src/lib/-XML/CXXTags.hpp

+17-4
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ writeType(
262262
if constexpr(T::isSpecialization())
263263
{
264264
for(const auto& targ : t.TemplateArgs)
265-
writeTemplateArg(targ, tags);
265+
writeTemplateArg(*targ, tags);
266266
}
267267

268268
if constexpr(requires { t.PointeeType; })
@@ -365,9 +365,22 @@ inline void writeTemplateParam(const TParam& I, XMLTags& tags)
365365

366366
inline void writeTemplateArg(const TArg& I, XMLTags& tags)
367367
{
368-
tags.write(targTagName, {}, {
369-
{ "value", I.Value }
370-
});
368+
visit(I, [&]<typename T>(const T& A)
369+
{
370+
Attributes attrs = {
371+
{"class", toString(T::kind_id)}
372+
};
373+
374+
if constexpr(T::isType())
375+
attrs.push({"type", toString(*A.Type)});
376+
else if constexpr(T::isNonType())
377+
attrs.push({"value", A.Value.Written});
378+
else if constexpr(T::isTemplate())
379+
attrs.push({A.Template});
380+
381+
tags.write(targTagName, {},
382+
std::move(attrs));
383+
});
371384
}
372385

373386
/** Return the xml tag name for the Info.

src/lib/-XML/XMLWriter.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ openTemplate(
493493
for(const auto& tparam : I->Params)
494494
writeTemplateParam(*tparam, tags_);
495495
for(const auto& targ : I->Args)
496-
writeTemplateArg(targ, tags_);
496+
writeTemplateArg(*targ, tags_);
497497
}
498498

499499
void
@@ -516,8 +516,8 @@ writeSpecialization(
516516
{"primary", toString(I.Primary) }
517517
});
518518

519-
for(const TArg& targ : I.Args)
520-
writeTemplateArg(targ, tags_);
519+
for(const auto& targ : I.Args)
520+
writeTemplateArg(*targ, tags_);
521521

522522
corpus_.traverse(I, *this);
523523

0 commit comments

Comments
 (0)