Skip to content

Commit f9aa994

Browse files
committed
rename TemplateParamKind to TParamKind, move TParam and TArg to Template.hpp
1 parent cc0617c commit f9aa994

File tree

8 files changed

+193
-238
lines changed

8 files changed

+193
-238
lines changed

include/mrdox/Metadata.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <mrdox/Metadata/Symbol.hpp>
3333
#include <mrdox/Metadata/Symbols.hpp>
3434
#include <mrdox/Metadata/Template.hpp>
35-
#include <mrdox/Metadata/TemplateParam.hpp>
3635
#include <mrdox/Metadata/Type.hpp>
3736
#include <mrdox/Metadata/Typedef.hpp>
3837
#include <mrdox/Metadata/Var.hpp>

include/mrdox/Metadata/Template.hpp

Lines changed: 173 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,183 @@
1414
#define MRDOX_METADATA_TEMPLATE_HPP
1515

1616
#include <mrdox/Platform.hpp>
17-
#include <mrdox/Metadata/TemplateArg.hpp>
18-
#include <mrdox/Metadata/TemplateParam.hpp>
17+
#include <mrdox/Metadata/Type.hpp>
18+
#include <string>
1919

2020
namespace clang {
2121
namespace mrdox {
2222

23+
enum class TParamKind : int
24+
{
25+
None = 0, // for bitstream
26+
// template type parameter, e.g. "typename T" or "class T"
27+
Type,
28+
// template non-type parameter, e.g. "int N" or "auto N"
29+
NonType,
30+
// template template parameter, e.g. "template<typename> typename T"
31+
Template,
32+
};
33+
34+
struct TParam;
35+
36+
struct TypeTParam
37+
{
38+
// default type for the type template parameter
39+
std::optional<TypeInfo> Default;
40+
};
41+
42+
struct NonTypeTParam
43+
{
44+
// type of the non-type template parameter
45+
TypeInfo Type;
46+
// non-type template parameter default value (if any)
47+
std::optional<std::string> Default;
48+
};
49+
50+
struct TemplateTParam
51+
{
52+
// template parameters for the template template parameter
53+
std::vector<TParam> Params;
54+
// non-type template parameter default value (if any)
55+
std::optional<std::string> Default;
56+
};
57+
58+
// ----------------------------------------------------------------
59+
60+
struct TParam
61+
{
62+
private:
63+
union Variant
64+
{
65+
Variant() { }
66+
~Variant() { }
67+
68+
TypeTParam Type;
69+
NonTypeTParam NonType;
70+
TemplateTParam Template;
71+
} Variant_;
72+
73+
void destroy() const noexcept;
74+
75+
template<typename T>
76+
void
77+
construct(T&& other)
78+
{
79+
switch(other.Kind)
80+
{
81+
case TParamKind::Type:
82+
emplace<TypeTParam>(
83+
std::forward<T>(other).Variant_.Type);
84+
break;
85+
case TParamKind::NonType:
86+
emplace<NonTypeTParam>(
87+
std::forward<T>(other).Variant_.NonType);
88+
break;
89+
case TParamKind::Template:
90+
emplace<TemplateTParam>(
91+
std::forward<T>(other).Variant_.Template);
92+
break;
93+
default:
94+
break;
95+
}
96+
}
97+
98+
public:
99+
// The kind of template parameter this is.
100+
TParamKind Kind = TParamKind::None;
101+
// The template parameters name, if any
102+
std::string Name;
103+
// Whether this template parameter is a parameter pack.
104+
bool IsParameterPack = false;
105+
106+
107+
TParam() = default;
108+
109+
MRDOX_DECL
110+
TParam(
111+
TParam&& other) noexcept;
112+
113+
MRDOX_DECL
114+
TParam(
115+
const TParam& other);
116+
117+
MRDOX_DECL
118+
TParam(
119+
std::string&& name,
120+
bool is_pack);
121+
122+
MRDOX_DECL ~TParam();
123+
124+
MRDOX_DECL
125+
TParam&
126+
operator=(
127+
const TParam& other);
128+
129+
MRDOX_DECL
130+
TParam&
131+
operator=(
132+
TParam&& other) noexcept;
133+
134+
template<typename T, typename... Args>
135+
auto&
136+
emplace(Args&&... args)
137+
{
138+
using U = std::remove_cvref_t<T>;
139+
if constexpr(std::is_same_v<U, TypeTParam>)
140+
Kind = TParamKind::Type;
141+
else if constexpr(std::is_same_v<U, NonTypeTParam>)
142+
Kind = TParamKind::NonType;
143+
else if constexpr(std::is_same_v<U, TemplateTParam>)
144+
Kind = TParamKind::Template;
145+
else
146+
assert(! "invalid template parameter kind");
147+
return *::new (static_cast<void*>(&Variant_)) T(
148+
std::forward<Args>(args)...);
149+
}
150+
151+
template<typename T>
152+
auto& get() noexcept
153+
{
154+
using U = std::remove_cvref_t<T>;
155+
if constexpr(std::is_same_v<U, TypeTParam>)
156+
return Variant_.Type;
157+
else if constexpr(std::is_same_v<U, NonTypeTParam>)
158+
return Variant_.NonType;
159+
else if constexpr(std::is_same_v<U, TemplateTParam>)
160+
return Variant_.Template;
161+
else
162+
assert(! "invalid template parameter kind");
163+
}
164+
165+
template<typename T>
166+
const auto& get() const noexcept
167+
{
168+
using U = std::remove_cvref_t<T>;
169+
if constexpr(std::is_same_v<U, TypeTParam>)
170+
return Variant_.Type;
171+
else if constexpr(std::is_same_v<U, NonTypeTParam>)
172+
return Variant_.NonType;
173+
else if constexpr(std::is_same_v<U, TemplateTParam>)
174+
return Variant_.Template;
175+
else
176+
assert(! "invalid template parameter kind");
177+
}
178+
};
179+
180+
// ----------------------------------------------------------------
181+
182+
struct TArg
183+
{
184+
std::string Value;
185+
186+
TArg() = default;
187+
188+
MRDOX_DECL
189+
TArg(std::string&& value);
190+
};
191+
192+
// ----------------------------------------------------------------
193+
23194
enum class TemplateSpecKind
24195
{
25196
Primary = 0, // for bitstream

include/mrdox/Metadata/TemplateArg.hpp

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)