forked from standardese/cppast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_template_parameter.hpp
More file actions
302 lines (252 loc) · 10.5 KB
/
Copy pathcpp_template_parameter.hpp
File metadata and controls
302 lines (252 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright (C) 2017-2018 Jonathan Müller <[email protected]>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#ifndef CPPAST_CPP_TEMPLATE_PARAMETER_HPP_INCLUDED
#define CPPAST_CPP_TEMPLATE_PARAMETER_HPP_INCLUDED
#include <type_safe/optional.hpp>
#include <type_safe/variant.hpp>
#include <cppast/cpp_entity.hpp>
#include <cppast/cpp_variable_base.hpp>
#include <cppast/detail/intrusive_list.hpp>
namespace cppast
{
/// Base class for all entities modelling a template parameter of some kind.
class cpp_template_parameter : public cpp_entity
{
public:
/// \returns Whether or not the parameter is variadic.
bool is_variadic() const noexcept
{
return variadic_;
}
protected:
cpp_template_parameter(std::string name, bool variadic)
: cpp_entity(std::move(name)), variadic_(variadic)
{}
private:
bool variadic_;
};
/// The kind of keyword used in a template parameter.
enum class cpp_template_keyword
{
keyword_class,
keyword_typename
};
/// \returns The string associated of the keyword.
const char* to_string(cpp_template_keyword kw) noexcept;
/// A [cppast::cpp_entity]() modelling a C++ template type parameter.
class cpp_template_type_parameter final : public cpp_template_parameter
{
public:
static cpp_entity_kind kind() noexcept;
/// \returns A newly created and registered template type parameter.
/// \notes The `default_type` may be `nullptr` in which case the parameter has no default.
static std::unique_ptr<cpp_template_type_parameter> build(
const cpp_entity_index& idx, cpp_entity_id id, std::string name, cpp_template_keyword kw,
bool variadic, std::unique_ptr<cpp_type> default_type = nullptr);
/// \returns A [ts::optional_ref]() to the default type.
type_safe::optional_ref<const cpp_type> default_type() const noexcept
{
return type_safe::opt_cref(default_type_.get());
}
/// \returns The keyword used in the template parameter.
cpp_template_keyword keyword() const noexcept
{
return keyword_;
}
private:
cpp_template_type_parameter(std::string name, cpp_template_keyword kw, bool variadic,
std::unique_ptr<cpp_type> default_type)
: cpp_template_parameter(std::move(name), variadic), default_type_(std::move(default_type)),
keyword_(kw)
{}
cpp_entity_kind do_get_entity_kind() const noexcept override;
std::unique_ptr<cpp_type> default_type_;
cpp_template_keyword keyword_;
};
/// \exclude
namespace detail
{
struct cpp_template_parameter_ref_predicate
{
bool operator()(const cpp_entity& e);
};
} // namespace detail
/// Reference to a [cppast::cpp_template_type_parameter]().
using cpp_template_type_parameter_ref
= basic_cpp_entity_ref<cpp_template_type_parameter,
detail::cpp_template_parameter_ref_predicate>;
/// A [cppast::cpp_type]() defined by a [cppast::cpp_template_type_parameter]().
class cpp_template_parameter_type final : public cpp_type
{
public:
/// \returns A newly created parameter type.
static std::unique_ptr<cpp_template_parameter_type> build(
cpp_template_type_parameter_ref parameter)
{
return std::unique_ptr<cpp_template_parameter_type>(
new cpp_template_parameter_type(std::move(parameter)));
}
/// \returns A reference to the [cppast::cpp_template_type_parameter]() this type refers to.
const cpp_template_type_parameter_ref& entity() const noexcept
{
return parameter_;
}
private:
cpp_template_parameter_type(cpp_template_type_parameter_ref parameter)
: parameter_(std::move(parameter))
{}
cpp_type_kind do_get_kind() const noexcept override
{
return cpp_type_kind::template_parameter_t;
}
cpp_template_type_parameter_ref parameter_;
};
/// A [cppast::cpp_entity]() modelling a C++ non-type template parameter.
class cpp_non_type_template_parameter final : public cpp_template_parameter,
public cpp_variable_base
{
public:
static cpp_entity_kind kind() noexcept;
/// \returns A newly created and registered non type template parameter.
/// \notes The `default_value` may be `nullptr` in which case the parameter has no default.
static std::unique_ptr<cpp_non_type_template_parameter> build(
const cpp_entity_index& idx, cpp_entity_id id, std::string name,
std::unique_ptr<cpp_type> type, bool is_variadic,
std::unique_ptr<cpp_expression> default_value = nullptr);
private:
cpp_non_type_template_parameter(std::string name, std::unique_ptr<cpp_type> type, bool variadic,
std::unique_ptr<cpp_expression> def)
: cpp_template_parameter(std::move(name), variadic),
cpp_variable_base(std::move(type), std::move(def))
{}
cpp_entity_kind do_get_entity_kind() const noexcept override;
};
/// \exclude
namespace detail
{
struct cpp_template_ref_predicate
{
bool operator()(const cpp_entity& e);
};
} // namespace detail
class cpp_template;
/// A reference to a [cppast::cpp_template]() or a [cppast::cpp_template_template_parameter]().
using cpp_template_ref = basic_cpp_entity_ref<cpp_entity, detail::cpp_template_ref_predicate>;
/// A [cppast::cpp_entity]() modelling a C++ template template parameter.
class cpp_template_template_parameter final : public cpp_template_parameter
{
public:
static cpp_entity_kind kind() noexcept;
/// Builds a [cppast::cpp_template_template_parameter]().
class builder
{
public:
/// \effects Sets the name and whether it is variadic.
builder(std::string name, bool variadic)
: parameter_(new cpp_template_template_parameter(std::move(name), variadic))
{}
/// \effects Sets the keyword,
/// default is [cpp_template_keyword::keyword_class]().
void keyword(cpp_template_keyword kw)
{
parameter_->keyword_ = kw;
}
/// \effects Adds a parameter to the template.
void add_parameter(std::unique_ptr<cpp_template_parameter> param)
{
parameter_->parameters_.push_back(*parameter_, std::move(param));
}
/// \effects Sets the default template.
void default_template(cpp_template_ref templ)
{
parameter_->default_ = std::move(templ);
}
/// \effects Registers the parameter in the [cppast::cpp_entity_index](),
/// using the given [cppast::cpp_entity_id]().
/// \returns The finished parameter.
std::unique_ptr<cpp_template_template_parameter> finish(const cpp_entity_index& idx,
cpp_entity_id id)
{
idx.register_definition(std::move(id), type_safe::ref(*parameter_));
return std::move(parameter_);
}
private:
std::unique_ptr<cpp_template_template_parameter> parameter_;
};
/// \returns An iteratable object containing the template parameters of the template template
/// parameter.
detail::iteratable_intrusive_list<cpp_template_parameter> parameters() const noexcept
{
return type_safe::ref(parameters_);
}
/// \returns The keyword used in the template parameter.
cpp_template_keyword keyword() const noexcept
{
return keyword_;
}
/// \returns A [ts::optional]() that is the default template.
type_safe::optional<cpp_template_ref> default_template() const noexcept
{
return default_;
}
private:
cpp_template_template_parameter(std::string name, bool variadic)
: cpp_template_parameter(std::move(name), variadic),
keyword_(cpp_template_keyword::keyword_class)
{}
cpp_entity_kind do_get_entity_kind() const noexcept override;
detail::intrusive_list<cpp_template_parameter> parameters_;
type_safe::optional<cpp_template_ref> default_;
cpp_template_keyword keyword_;
};
/// An argument for a [cppast::cpp_template_parameter]().
///
/// It is based on a [ts::variant]() of [cppast::cpp_type]() (for
/// [cppast::cpp_template_type_parameter]()), [cppast::cpp_expression]() (for
/// [cppast::cpp_non_type_template_parameter]()) and [cppast::cpp_template_ref]() (for
/// [cppast::cpp_template_template_parameter]().
class cpp_template_argument
{
public:
/// \effects Initializes it passing a type as argument.
/// This corresponds to a [cppast::cpp_template_type_parameter]().
/// \notes This constructor only participates in overload resolution if `T` is dervied from
/// [cppast::cpp_type](). \param 1 \exclude
template <typename T,
typename std::enable_if<std::is_base_of<cpp_type, T>::value, int>::type = 0>
cpp_template_argument(std::unique_ptr<T> type)
: arg_(std::unique_ptr<cpp_type>(std::move(type)))
{}
/// \effects Initializes it passing an expression as argument.
/// This corresponds to a [cppast::cpp_non_type_template_parameter]().
/// \notes This constructor only participates in overload resolution if `T` is dervied from
/// [cppast::cpp_expression](). \param 1 \exclude
template <typename T,
typename = typename std::enable_if<std::is_base_of<cpp_expression, T>::value>::type>
cpp_template_argument(std::unique_ptr<T> expr)
: arg_(std::unique_ptr<cpp_expression>(std::move(expr)))
{}
/// \effects Initializes it passing a template as argument.
/// This corresponds to a [cppast::cpp_template_template_parameter]().
cpp_template_argument(cpp_template_ref templ) : arg_(std::move(templ)) {}
type_safe::optional_ref<const cpp_type> type() const noexcept
{
return arg_.optional_value(type_safe::variant_type<std::unique_ptr<cpp_type>>{})
.map([](const std::unique_ptr<cpp_type>& type) { return type_safe::ref(*type); });
}
type_safe::optional_ref<const cpp_expression> expression() const noexcept
{
return arg_.optional_value(type_safe::variant_type<std::unique_ptr<cpp_expression>>{})
.map([](const std::unique_ptr<cpp_expression>& expr) { return type_safe::ref(*expr); });
}
type_safe::optional_ref<const cpp_template_ref> template_ref() const noexcept
{
return arg_.optional_value(type_safe::variant_type<cpp_template_ref>{});
}
private:
type_safe::variant<std::unique_ptr<cpp_type>, std::unique_ptr<cpp_expression>, cpp_template_ref>
arg_;
};
} // namespace cppast
#endif // CPPAST_CPP_TEMPLATE_PARAMETER_HPP_INCLUDED