-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathcpp_member_function.hpp
More file actions
340 lines (277 loc) · 9.68 KB
/
cpp_member_function.hpp
File metadata and controls
340 lines (277 loc) · 9.68 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (C) 2017-2023 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
#ifndef CPPAST_CPP_MEMBER_FUNCTION_HPP_INCLUDED
#define CPPAST_CPP_MEMBER_FUNCTION_HPP_INCLUDED
#include <type_safe/flag_set.hpp>
#include <cppast/cpp_function.hpp>
namespace cppast
{
/// The `virtual`-ness of a member function.
///
/// This is a [ts::flag_set]() `enum`.
/// \notes It does not specify whether a member function is `virtual` or not,
/// only the kind of `virtual`.
/// \notes As surprising as it may be, any of these can be used in combination,
/// i.e. you can have a `final` non-overriding function or an overriding pure `virtual` function.
enum class cpp_virtual_flags
{
pure, //< Set if the function is pure.
override, //< Set if the function overrides a base class function.
final, //< Set if the function is marked `final`.
_flag_set_size, //< \exclude
};
/// The `virtual` information of a member function.
///
/// This is an optional of the combination of the [cppast::cpp_virtual_flags]().
/// If the optional has a value, the member function is `virtual`,
/// and the [ts::flag_set]() describes additional information.
using cpp_virtual = type_safe::optional<type_safe::flag_set<cpp_virtual_flags>>;
/// \returns Whether or not a member function is `virtual`.
inline bool is_virtual(const cpp_virtual& virt) noexcept
{
return virt.has_value();
}
/// \returns Whether or not a member function is pure.
inline bool is_pure(const cpp_virtual& virt) noexcept
{
return static_cast<bool>(virt.value_or(cpp_virtual_flags::final) & cpp_virtual_flags::pure);
}
/// \returns Whether or not a member function overrides another one.
inline bool is_overriding(const cpp_virtual& virt) noexcept
{
return static_cast<bool>(virt.value_or(cpp_virtual_flags::pure) & cpp_virtual_flags::override);
}
/// \returns Whether or not a member function is `final`.
inline bool is_final(const cpp_virtual& virt) noexcept
{
return static_cast<bool>(virt.value_or(cpp_virtual_flags::pure) & cpp_virtual_flags::final);
}
/// Base classes for all regular member function.
///
/// The two derived classes are [cppast::cpp_member_function]() and [cppast::cpp_conversion_op]().
class cpp_member_function_base : public cpp_function_base
{
public:
/// \returns The return type of the member function.
const cpp_type& return_type() const noexcept
{
return *return_type_;
}
/// \returns Whether or not it is `virtual`.
bool is_virtual() const noexcept
{
return virtual_info().has_value();
}
/// \returns The `virtual`-ness of the member function.
const cpp_virtual& virtual_info() const noexcept
{
return virtual_;
}
/// \returns The cv-qualifier on the member function.
cpp_cv cv_qualifier() const noexcept
{
return cv_;
}
/// \returns The ref-qualifier on the member function.
cpp_reference ref_qualifier() const noexcept
{
return ref_;
}
/// \returns Whether or not the member function is `constexpr`.
bool is_constexpr() const noexcept
{
return constexpr_;
}
/// \returns Whether or not the member function is `consteval`.
bool is_consteval() const noexcept
{
return consteval_;
}
protected:
/// Builder class for member functions.
template <typename T>
class basic_member_builder : public basic_builder<T>
{
public:
/// \effects Sets the name and return type.
basic_member_builder(std::string name, std::unique_ptr<cpp_type> return_type)
{
this->function = std::unique_ptr<T>(new T(std::move(name), std::move(return_type)));
}
/// \effects Sets the cv- and ref-qualifier.
void cv_ref_qualifier(cpp_cv cv, cpp_reference ref) noexcept
{
auto& base = static_cast<cpp_member_function_base&>(*this->function);
base.cv_ = cv;
base.ref_ = ref;
}
/// \effects Sets the `virtual`-ness of the function.
void virtual_info(type_safe::flag_set<cpp_virtual_flags> virt) noexcept
{
static_cast<cpp_member_function_base&>(*this->function).virtual_ = virt;
}
/// \effects Marks the function as `constexpr`.
void is_constexpr() noexcept
{
static_cast<cpp_member_function_base&>(*this->function).constexpr_ = true;
}
/// \effects Marks the function as `consteval`.
void is_consteval() noexcept
{
static_cast<cpp_member_function_base&>(*this->function).consteval_ = true;
}
protected:
basic_member_builder() noexcept = default;
};
/// \effects Sets name and return type, as well as the rest to defaults.
cpp_member_function_base(std::string name, std::unique_ptr<cpp_type> return_type)
: cpp_function_base(std::move(name)), return_type_(std::move(return_type)), cv_(cpp_cv_none),
ref_(cpp_ref_none), constexpr_(false), consteval_(false)
{}
protected:
std::string do_get_signature() const override;
private:
std::unique_ptr<cpp_type> return_type_;
cpp_virtual virtual_;
cpp_cv cv_;
cpp_reference ref_;
bool constexpr_;
bool consteval_;
};
/// A [cppast::cpp_entity]() modelling a member function.
class cpp_member_function final : public cpp_member_function_base
{
public:
static cpp_entity_kind kind() noexcept;
/// Builder for [cppast::cpp_member_function]().
class builder : public cpp_member_function_base::basic_member_builder<cpp_member_function>
{
public:
using cpp_member_function_base::basic_member_builder<
cpp_member_function>::basic_member_builder;
};
private:
using cpp_member_function_base::cpp_member_function_base;
cpp_entity_kind do_get_entity_kind() const noexcept override;
friend basic_member_builder<cpp_member_function>;
};
/// A [cppast::cpp_entity]() modelling a C++ conversion operator.
class cpp_conversion_op final : public cpp_member_function_base
{
public:
static cpp_entity_kind kind() noexcept;
/// Builder for [cppast::cpp_conversion_op]().
class builder : public basic_member_builder<cpp_conversion_op>
{
public:
using basic_member_builder::basic_member_builder;
/// \effects Marks the conversion operator `explicit`.
void is_explicit() noexcept
{
function->explicit_ = true;
}
private:
using basic_member_builder::add_parameter;
using basic_member_builder::is_variadic;
};
/// \returns Whether or not the conversion is `explicit`.
bool is_explicit() const noexcept
{
return explicit_;
}
private:
cpp_conversion_op(std::string name, std::unique_ptr<cpp_type> return_t)
: cpp_member_function_base(std::move(name), std::move(return_t)), explicit_(false)
{}
cpp_entity_kind do_get_entity_kind() const noexcept override;
bool explicit_;
friend basic_member_builder<cpp_conversion_op>;
};
/// A [cppast::cpp_entity]() modelling a C++ constructor.
class cpp_constructor final : public cpp_function_base
{
public:
static cpp_entity_kind kind() noexcept;
/// Builder for [cppast::cpp_constructor]().
class builder : public basic_builder<cpp_constructor>
{
public:
using basic_builder::basic_builder;
/// \effects Marks the constructor `explicit`.
void is_explicit() noexcept
{
function->explicit_ = true;
}
/// \effects Marks the constructor `constexpr`.
void is_constexpr() noexcept
{
function->constexpr_ = true;
}
/// \effects Marks the constructor `consteval`.
void is_consteval() noexcept
{
function->consteval_ = true;
}
};
/// \returns Whether or not the constructor is `explicit`.
bool is_explicit() const noexcept
{
return explicit_;
}
/// \returns Whether or not the constructor is `constexpr`.
bool is_constexpr() const noexcept
{
return constexpr_;
}
/// \returns Whether or not the constructor is `consteval`.
bool is_consteval() const noexcept
{
return consteval_;
}
private:
cpp_constructor(std::string name)
: cpp_function_base(std::move(name)), explicit_(false), constexpr_(false), consteval_(false)
{}
cpp_entity_kind do_get_entity_kind() const noexcept override;
bool explicit_;
bool constexpr_;
bool consteval_;
friend basic_builder<cpp_constructor>;
};
/// A [cppast::cpp_entity]() modelling a C++ destructor.
class cpp_destructor final : public cpp_function_base
{
public:
static cpp_entity_kind kind() noexcept;
/// Builds a [cppast::cpp_destructor]().
class builder : public basic_builder<cpp_destructor>
{
public:
using basic_builder::basic_builder;
/// \effects Sets the `virtual`-ness of the destructor.
void virtual_info(cpp_virtual virt) noexcept
{
function->virtual_ = virt;
}
private:
using basic_builder::add_parameter;
using basic_builder::is_variadic;
};
/// \returns Whether or not it is `virtual`.
bool is_virtual() const noexcept
{
return virtual_info().has_value();
}
/// \returns The `virtual`-ness of the constructor.
cpp_virtual virtual_info() const noexcept
{
return virtual_;
}
private:
cpp_destructor(std::string name) : cpp_function_base(std::move(name)) {}
cpp_entity_kind do_get_entity_kind() const noexcept override;
cpp_virtual virtual_;
friend basic_builder<cpp_destructor>;
};
} // namespace cppast
#endif // CPPAST_CPP_MEMBER_FUNCTION_HPP_INCLUDED