-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathtype_parser.cpp
774 lines (688 loc) · 27.9 KB
/
type_parser.cpp
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
// Copyright (C) 2017-2023 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
#include "parse_functions.hpp"
#include <cctype>
#include <cppast/cpp_array_type.hpp>
#include <cppast/cpp_decltype_type.hpp>
#include <cppast/cpp_expression.hpp>
#include <cppast/cpp_function_type.hpp>
#include <cppast/cpp_template.hpp>
#include <cppast/cpp_template_parameter.hpp>
#include <cppast/cpp_type.hpp>
#include <cppast/cpp_type_alias.hpp>
#include "libclang_visitor.hpp"
using namespace cppast;
namespace
{
void remove_trailing_ws(std::string& str)
{
while (!str.empty() && std::isspace(str.back()))
str.pop_back();
}
void remove_leading_ws(std::string& str)
{
while (!str.empty() && std::isspace(str.front()))
str.erase(0, 1);
}
bool can_extend_suffix(const std::string& str, std::size_t suffix_length)
{
if (str.length() <= suffix_length + 1u)
{
auto c = str[str.length() - suffix_length - 1u];
return std::isalnum(c) || c == '_';
}
else
return false;
}
// if identifier only removes maximal suffix according to tokenization
bool remove_suffix(std::string& str, const char* suffix, bool is_identifier)
{
auto length = std::strlen(suffix);
if (str.length() >= length && str.compare(str.length() - length, length, suffix) == 0
&& (!is_identifier || !can_extend_suffix(str, length)))
{
str.erase(str.length() - length);
remove_trailing_ws(str);
return true;
}
else
return false;
}
bool can_extend_prefix(const std::string& str, std::size_t prefix_length)
{
if (prefix_length < str.size())
{
auto c = str[prefix_length];
return std::isalnum(c) || c == '_';
}
else
return false;
}
// if identifier only removes maximal suffix according to tokenization
bool remove_prefix(std::string& str, const char* prefix, bool is_identifier)
{
auto length = std::strlen(prefix);
if (str.length() >= length && str.compare(0u, length, prefix) == 0
&& (!is_identifier || !can_extend_prefix(str, length)))
{
str.erase(0u, length);
remove_leading_ws(str);
return true;
}
else
return false;
}
bool need_to_remove_scope(const CXCursor& cur, const CXType& type)
{
if (type.kind != CXType_Typedef)
return false;
// typedefs declared in the same class include all scopes in the name)
// (because of course they do)
auto parent = clang_getCursorSemanticParent(cur);
auto decl_parent = clang_getCursorSemanticParent(clang_getTypeDeclaration(type));
return clang_equalCursors(parent, decl_parent) != 0;
}
std::string get_type_spelling(const CXCursor& cur, const CXType& type)
{
// TODO: There are two interesting things we should keep track of here:
// * The canonical name of a type (easy, clang has a shortcut for that)
// * The type definition of this type, say for:
// std::vector<int, default_alloator...> this should be the type (we
// should ignore specialization here!) vector<T, S> whose parent is a
// namespace in this case but could again be a concrete type as well (to
// handle nested type specializations correctly.)
/*
auto ns_name = [](const CXCursor& cur) {
auto parent = clang_getCursorSemanticParent(cur);
if (clang_getCursorKind(parent) == CXCursor_TranslationUnit)
return "";
if (clang_getCursorKind(parent) == CXCursor_Namespace)
return spelling of cursor ...
};
*/
// auto canonical =
// detail::cxstring(clang_getTypeSpelling(clang_getCanonicalType(type))).std_str(); auto
// declaration =
// detail::cxstring(clang_getCursorSpelling(clang_getTypeDeclaration(type))).std_str();
auto spelling = detail::cxstring(clang_getTypeSpelling(type)).std_str();
if (need_to_remove_scope(cur, type))
{
auto colon = spelling.rfind(':');
if (colon != std::string::npos)
spelling.erase(0, colon + 1u);
}
return spelling;
}
// const/volatile at the end (because people do that apparently!)
// also used on member function pointers
cpp_cv suffix_cv(std::string& spelling)
{
auto cv = cpp_cv_none;
if (remove_suffix(spelling, "const", true))
{
if (remove_suffix(spelling, "volatile", true))
cv = cpp_cv_const_volatile;
else
cv = cpp_cv_const;
}
else if (remove_suffix(spelling, "volatile", true))
{
if (remove_suffix(spelling, "const", true))
cv = cpp_cv_const_volatile;
else
cv = cpp_cv_volatile;
}
return cv;
}
// const/volatile at the beginning
cpp_cv prefix_cv(std::string& spelling)
{
auto cv = cpp_cv_none;
if (remove_prefix(spelling, "const", true))
{
if (remove_prefix(spelling, "volatile", true))
cv = cpp_cv_const_volatile;
else
cv = cpp_cv_const;
}
else if (remove_prefix(spelling, "volatile", true))
{
if (remove_prefix(spelling, "const", true))
cv = cpp_cv_const_volatile;
else
cv = cpp_cv_volatile;
}
return cv;
}
cpp_cv merge_cv(cpp_cv a, cpp_cv b)
{
switch (a)
{
case cpp_cv_none:
return b;
case cpp_cv_const:
switch (b)
{
case cpp_cv_none:
return cpp_cv_const;
case cpp_cv_const:
return cpp_cv_const;
case cpp_cv_volatile:
return cpp_cv_const_volatile;
case cpp_cv_const_volatile:
return cpp_cv_const_volatile;
}
break;
case cpp_cv_volatile:
switch (b)
{
case cpp_cv_none:
return cpp_cv_volatile;
case cpp_cv_const:
return cpp_cv_const_volatile;
case cpp_cv_volatile:
return cpp_cv_volatile;
case cpp_cv_const_volatile:
return cpp_cv_const_volatile;
}
break;
case cpp_cv_const_volatile:
return cpp_cv_const_volatile;
}
DEBUG_UNREACHABLE(detail::assert_handler{});
return cpp_cv_none;
}
std::unique_ptr<cpp_type> make_cv_qualified(std::unique_ptr<cpp_type> entity, cpp_cv cv)
{
if (cv == cpp_cv_none)
return entity;
return cpp_cv_qualified_type::build(std::move(entity), cv);
}
template <typename Builder>
std::unique_ptr<cpp_type> make_leave_type(const CXCursor& cur, const CXType& type, Builder b)
{
auto spelling = get_type_spelling(cur, type);
// check for cv qualifiers on the leave type
auto prefix = prefix_cv(spelling);
auto suffix = suffix_cv(spelling);
auto cv = merge_cv(prefix, suffix);
// remove struct/class/union prefix on inline type definition
// i.e. C's typedef struct idiom
remove_prefix(spelling, "struct", true);
remove_prefix(spelling, "class", true);
remove_prefix(spelling, "union", true);
auto entity = b(std::move(spelling));
if (!entity)
return nullptr;
return make_cv_qualified(std::move(entity), cv);
}
cpp_reference get_reference_kind(const CXType& type)
{
if (type.kind == CXType_LValueReference)
return cpp_ref_lvalue;
else if (type.kind == CXType_RValueReference)
return cpp_ref_rvalue;
return cpp_ref_none;
}
std::unique_ptr<cpp_type> parse_type_impl(const detail::parse_context& context, const CXCursor& cur,
const CXType& type);
std::unique_ptr<cpp_expression> parse_array_size(const CXCursor& cur, const CXType& type)
{
auto size = clang_getArraySize(type);
if (size != -1)
return cpp_literal_expression::build(cpp_builtin_type::build(cpp_ulonglong),
std::to_string(size));
auto spelling = get_type_spelling(cur, type);
DEBUG_ASSERT(spelling.size() > 2u && spelling.back() == ']', detail::parse_error_handler{},
type, "unexpected token");
std::string size_expr;
auto ptr = spelling.rbegin();
while (true)
{
// Consume the final closing ].
++ptr;
auto bracket_count = 1;
while (bracket_count != 0)
{
if (*ptr == ']')
++bracket_count;
else if (*ptr == '[')
--bracket_count;
if (bracket_count != 0)
size_expr += *ptr;
++ptr;
}
if (ptr == spelling.rend() || *ptr != ']')
// We don't have another ].
break;
// We do have another ], as we want the innermost, we need to restart.
size_expr.clear();
}
return size_expr.empty()
? nullptr
: cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_ulonglong),
cpp_token_string::tokenize(
std::string(size_expr.rbegin(),
size_expr.rend())));
}
std::unique_ptr<cpp_type> try_parse_array_type(const detail::parse_context& context,
const CXCursor& cur, const CXType& type)
{
auto canonical = clang_getCanonicalType(type);
auto value_type = clang_getArrayElementType(type);
if (value_type.kind == CXType_Invalid)
{
// value_type is invalid, however type can still be an array
// as there seems to be a libclang bug
// only if the canonical type is not an array,
// is it truly not an array
value_type = clang_getArrayElementType(canonical);
if (value_type.kind == CXType_Invalid)
return nullptr;
// we have an array, even though type isn't one directly
// only downside of this workaround: we've stripped away typedefs
}
auto size = parse_array_size(cur, canonical); // type may not work, see above
return cpp_array_type::build(parse_type_impl(context, cur, value_type), std::move(size));
}
template <class Builder>
std::unique_ptr<cpp_type> add_parameters(Builder& builder, const detail::parse_context& context,
const CXCursor& cur, const CXType& type)
{
auto no_args = clang_getNumArgTypes(type);
DEBUG_ASSERT(no_args >= 0, detail::parse_error_handler{}, type, "invalid number of arguments");
for (auto i = 0u; i != unsigned(no_args); ++i)
builder.add_parameter(parse_type_impl(context, cur, clang_getArgType(type, i)));
if (clang_isFunctionTypeVariadic(type))
builder.is_variadic();
return builder.finish();
}
std::unique_ptr<cpp_type> try_parse_function_type(const detail::parse_context& context,
const CXCursor& cur, const CXType& type)
{
auto result = clang_getResultType(type);
if (result.kind == CXType_Invalid)
// not a function type
return nullptr;
cpp_function_type::builder builder(parse_type_impl(context, cur, result));
return add_parameters(builder, context, cur, type);
}
cpp_reference member_function_ref_qualifier(std::string& spelling)
{
if (remove_suffix(spelling, "&&", false))
return cpp_ref_rvalue;
else if (remove_suffix(spelling, "&", false))
return cpp_ref_lvalue;
return cpp_ref_none;
}
std::unique_ptr<cpp_type> make_ref_qualified(std::unique_ptr<cpp_type> type, cpp_reference ref)
{
if (ref == cpp_ref_none)
return type;
return cpp_reference_type::build(std::move(type), ref);
}
std::unique_ptr<cpp_type> parse_member_pointee_type(const detail::parse_context& context,
const CXCursor& cur, const CXType& type)
{
auto spelling = get_type_spelling(cur, type);
auto ref = member_function_ref_qualifier(spelling);
auto cv = suffix_cv(spelling);
auto class_t = clang_Type_getClassType(type);
auto class_entity
= make_ref_qualified(make_cv_qualified(parse_type_impl(context, cur, class_t), cv), ref);
auto pointee = clang_getPointeeType(type); // for everything except the class type
auto result = clang_getResultType(pointee);
if (result.kind == CXType_Invalid)
{
// member data pointer
return cpp_member_object_type::build(std::move(class_entity),
parse_type_impl(context, cur, pointee));
}
else
{
cpp_member_function_type::builder builder(std::move(class_entity),
parse_type_impl(context, cur, result));
return add_parameters(builder, context, cur, pointee);
}
}
bool is_direct_templated(const CXCursor& cur)
{
// TODO: variable template
auto kind = clang_getCursorKind(cur);
return kind == CXCursor_TypeAliasTemplateDecl || kind == CXCursor_ClassTemplate
|| kind == CXCursor_ClassTemplatePartialSpecialization
|| kind == CXCursor_FunctionTemplate;
}
bool check_parent(const CXCursor& parent)
{
if (clang_Cursor_isNull(parent))
return false;
auto kind = clang_getCursorKind(parent);
return kind != CXCursor_Namespace && kind != CXCursor_TranslationUnit;
}
CXCursor get_template(CXCursor cur)
{
do
{
if (is_direct_templated(cur))
return cur;
cur = clang_getCursorSemanticParent(cur);
} while (check_parent(cur));
return clang_getNullCursor();
}
std::unique_ptr<cpp_type> try_parse_template_parameter_type(const detail::parse_context& context,
const CXCursor& cur, const CXType& type)
{
// see if we have a parent template
auto templ = get_template(cur);
if (clang_Cursor_isNull(templ))
// not a template
return nullptr;
// doesn't respect cv qualifiers properly
auto result
= make_leave_type(cur, type, [&](std::string&& type_spelling) -> std::unique_ptr<cpp_type> {
// look at the template parameters,
// see if we find a matching one
auto param = clang_getNullCursor();
detail::visit_children(templ, [&](const CXCursor& child) {
if (clang_getCursorKind(child) == CXCursor_TemplateTypeParameter
&& get_type_spelling(child, clang_getCursorType(child)) == type_spelling)
{
// found one
DEBUG_ASSERT(clang_Cursor_isNull(param), detail::assert_handler{});
param = child;
}
});
if (clang_Cursor_isNull(param))
return nullptr;
else
// found matching parameter
return cpp_template_parameter_type::build(
cpp_template_type_parameter_ref(detail::get_entity_id(param),
std::move(type_spelling)));
});
if (result)
return result;
else
// try again in a possible parent template
return try_parse_template_parameter_type(context, clang_getCursorSemanticParent(templ),
type);
}
CXCursor get_instantiation_template(const CXCursor& cur, const CXType& type,
const std::string& templ_name)
{
// look if the type has a declaration that is a template
auto decl = clang_getTypeDeclaration(type);
auto count = clang_Type_getNumTemplateArguments(clang_getCursorType(decl));
if (count > 0 || is_direct_templated(decl))
{
auto specialized = clang_getSpecializedCursorTemplate(decl);
if (clang_Cursor_isNull(specialized))
// is already a template
return decl;
else
return specialized;
}
else
{
// look if the templ_name matches a template template parameter
auto param = clang_getNullCursor();
detail::visit_children(cur, [&](const CXCursor& child) {
if (clang_getCursorKind(child) == CXCursor_TemplateTemplateParameter
&& detail::get_cursor_name(child) == templ_name.c_str())
{
DEBUG_ASSERT(clang_Cursor_isNull(param), detail::parse_error_handler{}, cur,
"multiple template template parameters with the same name?!");
param = child;
}
});
return param;
}
}
std::unique_ptr<cpp_type> try_parse_instantiation_type(const detail::parse_context&,
const CXCursor& cur, const CXType& type)
{
return make_leave_type(cur, type, [&](std::string&& spelling) -> std::unique_ptr<cpp_type> {
auto ptr = spelling.c_str();
std::string templ_name;
for (; *ptr && *ptr != '<'; ++ptr)
templ_name += *ptr;
if (*ptr != '<')
return nullptr;
++ptr;
auto templ = get_instantiation_template(cur, type, templ_name);
if (clang_Cursor_isNull(templ))
return nullptr;
cpp_template_instantiation_type::builder builder(
cpp_template_ref(detail::get_entity_id(templ), std::move(templ_name)));
// parse arguments
// i.e. not parse really, just add the string
if (spelling.empty() || spelling.back() != '>')
return nullptr;
spelling.pop_back();
while (!spelling.empty() && spelling.back() == ' ')
spelling.pop_back();
builder.add_unexposed_arguments(ptr);
return builder.finish();
});
}
std::unique_ptr<cpp_type> try_parse_decltype_type(const detail::parse_context&, const CXCursor& cur,
const CXType& type)
{
if (clang_isExpression(clang_getCursorKind(cur)))
return nullptr; // don't use decltype here
return make_leave_type(cur, type, [&](std::string&& spelling) -> std::unique_ptr<cpp_type> {
if (!remove_prefix(spelling, "decltype(", false))
return nullptr;
remove_suffix(spelling, "...", false); // variadic decltype. fun
DEBUG_ASSERT(!spelling.empty() && spelling.back() == ')', detail::parse_error_handler{},
type, "unexpected spelling");
spelling.pop_back();
return cpp_decltype_type::build(
cpp_unexposed_expression::build(cpp_unexposed_type::build("<decltype>"),
cpp_token_string::tokenize(spelling)));
});
}
std::unique_ptr<cpp_type> parse_type_impl(const detail::parse_context& context, const CXCursor& cur,
const CXType& type)
{
switch (type.kind)
{
// stuff I can't parse
// or have no idea what it is and wait for bug report
default:
context.logger->log("libclang parser",
format_diagnostic(severity::warning, detail::make_location(type),
"unexpected type of kind '",
detail::get_type_kind_spelling(type).c_str(), "'"));
// fallthrough
case CXType_Dependent: // seems to have something to do with expressions, just ignore that (for
// now?)
case CXType_Unexposed:
if (auto ftype = try_parse_function_type(context, cur, type))
// guess what: after you've called clang_getPointeeType() on a function pointer
// you'll get an unexposed type
return ftype;
else if (auto atype = try_parse_array_type(context, cur, type))
// same deal here
return atype;
else if (auto dtype = try_parse_decltype_type(context, cur, type))
// decltype unexposed
return dtype;
else if (auto itype = try_parse_instantiation_type(context, cur, type))
// instantiation unexposed
return itype;
else if (auto ptype = try_parse_template_parameter_type(context, cur, type))
// template parameter type is unexposed
return ptype;
// fallthrough
case CXType_Complex:
return cpp_unexposed_type::build(get_type_spelling(cur, type));
case CXType_Void:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_void); });
case CXType_Bool:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_bool); });
case CXType_UChar:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_uchar); });
case CXType_UShort:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_ushort); });
case CXType_UInt:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_uint); });
case CXType_ULong:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_ulong); });
case CXType_ULongLong:
return make_leave_type(cur, type, [](std::string&&) {
return cpp_builtin_type::build(cpp_ulonglong);
});
case CXType_UInt128:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_uint128); });
case CXType_SChar:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_schar); });
case CXType_Short:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_short); });
case CXType_Int:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_int); });
case CXType_Long:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_long); });
case CXType_LongLong:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_longlong); });
case CXType_Int128:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_int128); });
case CXType_Float:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_float); });
case CXType_Double:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_double); });
case CXType_LongDouble:
return make_leave_type(cur, type, [](std::string&&) {
return cpp_builtin_type::build(cpp_longdouble);
});
case CXType_Float128:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_float128); });
case CXType_Char_U:
case CXType_Char_S:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_char); });
case CXType_Char16:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_char16); });
case CXType_Char32:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_char32); });
case CXType_WChar:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_wchar); });
case CXType_NullPtr:
return make_leave_type(cur, type,
[](std::string&&) { return cpp_builtin_type::build(cpp_nullptr); });
case CXType_Elaborated:
if (auto itype = try_parse_instantiation_type(context, cur, type))
// elaborated types can be instantiation types
return itype;
// fallthrough
case CXType_Record:
case CXType_Enum:
case CXType_Typedef:
return make_leave_type(cur, type, [&](std::string&& spelling) {
auto decl = clang_getTypeDeclaration(type);
if (clang_isInvalid(clang_getCursorKind(decl)))
{
// We can reach this point if we have an elaborated type referencing a using
// declaration. Give it an invalid id, since we have no way of retrieving it, but
// keep the spelling.
return cpp_user_defined_type::build(
cpp_type_ref(cpp_entity_id(""), std::move(spelling)));
}
else
{
if (detail::cxstring(clang_getCursorSpelling(decl)).empty())
spelling = ""; // anonymous type
return cpp_user_defined_type::build(
cpp_type_ref(detail::get_entity_id(decl), std::move(spelling)));
}
});
case CXType_Pointer: {
auto pointee = parse_type_impl(context, cur, clang_getPointeeType(type));
auto pointer = cpp_pointer_type::build(std::move(pointee));
auto spelling = get_type_spelling(cur, type);
auto cv = suffix_cv(spelling);
return make_cv_qualified(std::move(pointer), cv);
}
case CXType_LValueReference:
case CXType_RValueReference: {
auto referee = parse_type_impl(context, cur, clang_getPointeeType(type));
return cpp_reference_type::build(std::move(referee), get_reference_kind(type));
}
case CXType_IncompleteArray:
case CXType_VariableArray:
case CXType_DependentSizedArray:
case CXType_ConstantArray:
return try_parse_array_type(context, cur, type);
case CXType_FunctionNoProto:
case CXType_FunctionProto:
return try_parse_function_type(context, cur, type);
case CXType_MemberPointer:
return cpp_pointer_type::build(parse_member_pointee_type(context, cur, type));
case CXType_Auto:
return make_leave_type(cur, type, [&](std::string&&) { return cpp_auto_type::build(); });
}
}
} // namespace
std::unique_ptr<cpp_type> detail::parse_type(const detail::parse_context& context,
const CXCursor& cur, const CXType& type)
{
auto result = parse_type_impl(context, cur, type);
DEBUG_ASSERT(result != nullptr, detail::parse_error_handler{}, type, "invalid type");
return result;
}
std::unique_ptr<cpp_type> detail::parse_raw_type(const detail::parse_context&,
detail::cxtoken_stream& stream,
detail::cxtoken_iterator end)
{
auto result = detail::to_string(stream, end, false);
return cpp_unexposed_type::build(result.as_string());
}
std::unique_ptr<cpp_entity> detail::parse_cpp_type_alias(const detail::parse_context& context,
const CXCursor& cur,
const CXCursor& template_cur)
{
DEBUG_ASSERT(cur.kind == CXCursor_TypeAliasDecl || cur.kind == CXCursor_TypedefDecl,
detail::assert_handler{});
auto name = detail::get_cursor_name(cur);
auto type = parse_type(context, clang_Cursor_isNull(template_cur) ? cur : template_cur,
clang_getTypedefDeclUnderlyingType(cur));
std::unique_ptr<cpp_type_alias> result;
if (!clang_Cursor_isNull(template_cur))
result = cpp_type_alias::build(name.c_str(), std::move(type));
else
{
result = cpp_type_alias::build(*context.idx, get_entity_id(cur), name.c_str(),
std::move(type));
context.comments.match(*result, cur);
}
// look for attributes
detail::cxtokenizer tokenizer(context.tu, context.file, cur);
detail::cxtoken_stream stream(tokenizer, cur);
if (detail::skip_if(stream, "using"))
{
// syntax: using <identifier> attributes
detail::skip(stream, name.c_str());
result->add_attribute(detail::parse_attributes(stream));
}
return result;
}