forked from standardese/cppast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_type_alias.cpp
More file actions
521 lines (464 loc) · 19.1 KB
/
Copy pathcpp_type_alias.cpp
File metadata and controls
521 lines (464 loc) · 19.1 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// 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.
#include <cppast/cpp_type_alias.hpp>
#include <cppast/cpp_array_type.hpp>
#include <cppast/cpp_decltype_type.hpp>
#include <cppast/cpp_function_type.hpp>
#include <cppast/cpp_template.hpp>
#include <cppast/cpp_template_parameter.hpp>
#include "test_parser.hpp"
using namespace cppast;
bool equal_types(const cpp_entity_index& idx, const cpp_type& parsed, const cpp_type& synthesized)
{
if (parsed.kind() != synthesized.kind())
return false;
switch (parsed.kind())
{
case cpp_type_kind::builtin_t:
return static_cast<const cpp_builtin_type&>(parsed).builtin_type_kind()
== static_cast<const cpp_builtin_type&>(synthesized).builtin_type_kind();
case cpp_type_kind::user_defined_t:
{
auto& user_parsed = static_cast<const cpp_user_defined_type&>(parsed);
auto& user_synthesized = static_cast<const cpp_user_defined_type&>(synthesized);
return equal_ref(idx, user_parsed.entity(), user_synthesized.entity());
}
case cpp_type_kind::auto_t:
return true;
case cpp_type_kind::decltype_t:
return equal_expressions(static_cast<const cpp_decltype_type&>(parsed).expression(),
static_cast<const cpp_decltype_type&>(synthesized).expression());
case cpp_type_kind::decltype_auto_t:
return true;
case cpp_type_kind::cv_qualified_t:
{
auto& cv_a = static_cast<const cpp_cv_qualified_type&>(parsed);
auto& cv_b = static_cast<const cpp_cv_qualified_type&>(synthesized);
return cv_a.cv_qualifier() == cv_b.cv_qualifier()
&& equal_types(idx, cv_a.type(), cv_b.type());
}
case cpp_type_kind::pointer_t:
return equal_types(idx, static_cast<const cpp_pointer_type&>(parsed).pointee(),
static_cast<const cpp_pointer_type&>(synthesized).pointee());
case cpp_type_kind::reference_t:
{
auto& ref_a = static_cast<const cpp_reference_type&>(parsed);
auto& ref_b = static_cast<const cpp_reference_type&>(synthesized);
return ref_a.reference_kind() == ref_b.reference_kind()
&& equal_types(idx, ref_a.referee(), ref_b.referee());
}
case cpp_type_kind::array_t:
{
auto& array_a = static_cast<const cpp_array_type&>(parsed);
auto& array_b = static_cast<const cpp_array_type&>(synthesized);
// check value type
if (!equal_types(idx, array_a.value_type(), array_b.value_type()))
return false;
// check size
if (!array_a.size().has_value() && !array_b.size().has_value())
return true;
auto& size_a = array_a.size().value();
auto& size_b = array_b.size().value();
return equal_expressions(size_a, size_b);
}
case cpp_type_kind::function_t:
{
auto& func_a = static_cast<const cpp_function_type&>(parsed);
auto& func_b = static_cast<const cpp_function_type&>(synthesized);
if (!equal_types(idx, func_a.return_type(), func_b.return_type()))
return false;
else if (func_a.is_variadic() != func_b.is_variadic())
return false;
auto iter_a = func_a.parameter_types().begin();
auto iter_b = func_b.parameter_types().begin();
while (iter_a != func_a.parameter_types().end() && iter_b != func_b.parameter_types().end())
{
if (!equal_types(idx, *iter_a, *iter_b))
return false;
++iter_a;
++iter_b;
}
return iter_a == func_a.parameter_types().end() && iter_b == func_b.parameter_types().end();
}
case cpp_type_kind::member_function_t:
{
auto& func_a = static_cast<const cpp_member_function_type&>(parsed);
auto& func_b = static_cast<const cpp_member_function_type&>(synthesized);
if (!equal_types(idx, func_a.return_type(), func_b.return_type()))
return false;
else if (!equal_types(idx, func_a.class_type(), func_b.class_type()))
return false;
else if (func_a.is_variadic() != func_b.is_variadic())
return false;
auto iter_a = func_a.parameter_types().begin();
auto iter_b = func_b.parameter_types().begin();
while (iter_a != func_a.parameter_types().end() && iter_b != func_b.parameter_types().end())
{
if (!equal_types(idx, *iter_a, *iter_b))
return false;
++iter_a;
++iter_b;
}
return iter_a == func_a.parameter_types().end() && iter_b == func_b.parameter_types().end();
}
case cpp_type_kind::member_object_t:
{
auto& obj_a = static_cast<const cpp_member_object_type&>(parsed);
auto& obj_b = static_cast<const cpp_member_object_type&>(synthesized);
if (!equal_types(idx, obj_a.class_type(), obj_b.class_type()))
return false;
return equal_types(idx, obj_a.object_type(), obj_b.object_type());
}
case cpp_type_kind::template_parameter_t:
{
auto& entity_parsed = static_cast<const cpp_template_parameter_type&>(parsed).entity();
auto& entity_synthesized
= static_cast<const cpp_template_parameter_type&>(synthesized).entity();
return equal_ref(idx, entity_parsed, entity_synthesized);
}
case cpp_type_kind::template_instantiation_t:
{
auto& inst_parsed = static_cast<const cpp_template_instantiation_type&>(parsed);
auto& inst_synthesized = static_cast<const cpp_template_instantiation_type&>(synthesized);
if (!equal_ref(idx, inst_parsed.primary_template(), inst_synthesized.primary_template()))
return false;
else if (inst_parsed.arguments_exposed() != inst_synthesized.arguments_exposed())
return false;
else if (!inst_parsed.arguments_exposed())
return inst_parsed.unexposed_arguments() == inst_synthesized.unexposed_arguments();
else if (inst_parsed.arguments().has_value() != inst_synthesized.arguments().has_value())
return false;
else if (!inst_parsed.arguments().has_value())
return true;
auto iter_a = inst_parsed.arguments().value().begin();
auto iter_b = inst_synthesized.arguments().value().begin();
while (iter_a != inst_parsed.arguments().value().end()
&& iter_b != inst_synthesized.arguments().value().end())
{
if (iter_a->type().has_value() && iter_b->type().has_value())
{
if (!equal_types(idx, iter_a->type().value(), iter_b->type().value()))
return false;
}
else if (iter_a->expression().has_value() && iter_b->expression().has_value())
{
if (!equal_expressions(iter_a->expression().value(), iter_b->expression().value()))
return false;
}
else if (iter_a->template_ref().has_value() && iter_b->template_ref().has_value())
{
if (!equal_ref(idx, iter_a->template_ref().value(), iter_b->template_ref().value()))
return false;
}
else
return false;
++iter_a;
++iter_b;
}
return iter_a == inst_parsed.arguments().value().end()
&& iter_b == inst_synthesized.arguments().value().end();
}
case cpp_type_kind::dependent_t:
{
auto& dep_a = static_cast<const cpp_dependent_type&>(parsed);
auto& dep_b = static_cast<const cpp_dependent_type&>(synthesized);
return dep_a.name() == dep_b.name() && equal_types(idx, dep_a.dependee(), dep_b.dependee());
}
case cpp_type_kind::unexposed_t:
return static_cast<const cpp_unexposed_type&>(parsed).name()
== static_cast<const cpp_unexposed_type&>(synthesized).name();
}
return false;
}
// also tests the type parsing code
// other test cases don't need that anymore
TEST_CASE("cpp_type_alias")
{
const char* code = nullptr;
auto check_code = false;
SECTION("using")
{
check_code = true;
code = R"(
// basic
/// using a=int;
using a = int;
/// using b=long double const volatile;
using b = const long double volatile;
// pointers
/// using c=int*;
using c = int*;
/// using d=unsigned int const*;
using d = const unsigned int*;
/// using e=unsigned int const* volatile;
using e = unsigned const * volatile;
// references
/// using f=int&;
using f = int&;
/// using g=int const&&;
using g = const int&&;
// user-defined types
/// using h=c;
using h = c;
/// using i=d const;
using i = const d;
/// using j=e*;
using j = e*;
// arrays
/// using k=int[42];
using k = int[42];
/// using l=float*[];
using l = float*[];
/// using m=char[42];
using m = char[3 * 2 + 4 ? 42 : 43];
// function pointers
/// using n=void(*)(int);
using n = void(*)(int);
/// using o=char*(&)(int const&,...);
using o = char*(&)(const int&,...);
/// using p=n(*)(int,o);
using p = n(*)(int, o);
struct foo {};
// member function pointers
/// using q=void(foo::*)(int);
using q = void(foo::*)(int);
/// using r=void(foo::*)(int,...)const&;
using r = void(foo::*)(int,...) const &;
// member data pointers
/// using s=int(foo::*);
using s = int(foo::*);
// user-defined types inline definition
using t = struct t_ {};
using u = const struct u_ {}*;
using v = struct {};
// decltype
/// using w=decltype(0);
using w = decltype(0);
)";
}
SECTION("typedef")
{
check_code = false; // will always generate using
code = R"(
// basic
typedef int a;
typedef const long double volatile b;
// pointers
typedef int* c;
typedef const unsigned int* d;
typedef unsigned const * volatile e;
// references
typedef int& f;
typedef const int&& g;
// user-defined types
typedef c h;
typedef const d i;
typedef e* j;
// arrays
typedef int k[42];
typedef float* l[];
typedef char m[3 * 2 + 4 ? 42 : 43];
// function pointers
typedef void(*n)(int);
typedef char*(&o)(const int&,...);
typedef n(*p)(int, o);
struct foo {};
// member function pointers
typedef void(foo::*q)(int);
typedef void(foo::*r)(int,...) const &;
// member data pointers
typedef int(foo::*s);
// user-defined types inline definition
typedef struct t_ {} t;
typedef const struct u_ {}* u;
typedef struct {} v;
// decltype
typedef decltype(0) w;
)";
}
auto add_cv = [](std::unique_ptr<cpp_type> type, cpp_cv cv) {
return cpp_cv_qualified_type::build(std::move(type), cv);
};
auto make_size = [](std::string size, bool literal) -> std::unique_ptr<cpp_expression> {
auto type = cpp_builtin_type::build(cpp_ulonglong);
if (literal)
return cpp_literal_expression::build(std::move(type), std::move(size));
else
return cpp_unexposed_expression::build(std::move(type),
cpp_token_string::tokenize(std::move(size)));
};
cpp_entity_index idx;
auto file = parse(idx, "cpp_type_alias.cpp", code);
auto count = test_visit<cpp_type_alias>(*file, [&](const cpp_type_alias& alias) {
if (alias.name() == "a")
{
auto type = cpp_builtin_type::build(cpp_int);
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "b")
{
auto type = add_cv(cpp_builtin_type::build(cpp_longdouble), cpp_cv_const_volatile);
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "c")
{
auto type = cpp_pointer_type::build(cpp_builtin_type::build(cpp_int));
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "d")
{
auto type
= cpp_pointer_type::build(add_cv(cpp_builtin_type::build(cpp_uint), cpp_cv_const));
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "e")
{
auto type = add_cv(cpp_pointer_type::build(
add_cv(cpp_builtin_type::build(cpp_uint), cpp_cv_const)),
cpp_cv_volatile);
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "f")
{
auto type = cpp_reference_type::build(cpp_builtin_type::build(cpp_int), cpp_ref_lvalue);
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "g")
{
auto type
= cpp_reference_type::build(add_cv(cpp_builtin_type::build(cpp_int), cpp_cv_const),
cpp_ref_rvalue);
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "h")
{
auto type = cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "c"));
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "i")
{
auto type = add_cv(cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "d")),
cpp_cv_const);
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "j")
{
auto type = cpp_pointer_type::build(
cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "e")));
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "k")
{
auto type
= cpp_array_type::build(cpp_builtin_type::build(cpp_int), make_size("42", true));
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "l")
{
auto type
= cpp_array_type::build(cpp_pointer_type::build(cpp_builtin_type::build(cpp_float)),
nullptr);
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "m")
{
auto type
= cpp_array_type::build(cpp_builtin_type::build(cpp_char), make_size("42", true));
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "n")
{
cpp_function_type::builder builder(cpp_builtin_type::build(cpp_void));
builder.add_parameter(cpp_builtin_type::build(cpp_int));
auto type = cpp_pointer_type::build(builder.finish());
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "o")
{
cpp_function_type::builder builder(
cpp_pointer_type::build(cpp_builtin_type::build(cpp_char)));
builder.add_parameter(
cpp_reference_type::build(add_cv(cpp_builtin_type::build(cpp_int), cpp_cv_const),
cpp_ref_lvalue));
builder.is_variadic();
auto type = cpp_reference_type::build(builder.finish(), cpp_ref_lvalue);
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "p")
{
cpp_function_type::builder builder(
cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "n")));
builder.add_parameter(cpp_builtin_type::build(cpp_int));
builder.add_parameter(
cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "o")));
auto type = cpp_pointer_type::build(builder.finish());
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "q")
{
cpp_member_function_type::builder builder(cpp_user_defined_type::build(
cpp_type_ref(cpp_entity_id(""), "foo")),
cpp_builtin_type::build(cpp_void));
builder.add_parameter(cpp_builtin_type::build(cpp_int));
auto type = cpp_pointer_type::build(builder.finish());
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "r")
{
auto obj = cpp_reference_type::build(add_cv(cpp_user_defined_type::build(
cpp_type_ref(cpp_entity_id(""), "foo")),
cpp_cv_const),
cpp_ref_lvalue);
cpp_member_function_type::builder builder(std::move(obj),
cpp_builtin_type::build(cpp_void));
builder.add_parameter(cpp_builtin_type::build(cpp_int));
builder.is_variadic();
auto type = cpp_pointer_type::build(builder.finish());
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "s")
{
auto pointee
= cpp_member_object_type::build(cpp_user_defined_type::build(
cpp_type_ref(cpp_entity_id(""), "foo")),
cpp_unexposed_type::build(
"int")); // type not exposed directly for some
// reason
auto type = cpp_pointer_type::build(std::move(pointee));
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "t")
{
auto type = cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "t_"));
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
return false; // inhibit comment check for next three entities
// as they can't be documented (will always apply to the inline type)
}
else if (alias.name() == "u")
{
auto type = cpp_pointer_type::build(
add_cv(cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "u_")),
cpp_cv_const));
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
return false;
}
else if (alias.name() == "v")
{
auto type = cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "v"));
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
return false;
}
else if (alias.name() == "w")
{
auto type = cpp_decltype_type::build(
cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_int),
cpp_token_string::tokenize("0")));
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else
REQUIRE(false);
return check_code;
});
REQUIRE(count == 23u);
}