Repository navigation
Expand file tree
/
Copy pathtemplate_parser.cpp
More file actions
392 lines (335 loc) · 15.3 KB
/
Copy pathtemplate_parser.cpp
File metadata and controls
392 lines (335 loc) · 15.3 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
// Copyright (C) 2017-2019 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#include <cppast/cpp_alias_template.hpp>
#include <cppast/cpp_class_template.hpp>
#include <cppast/cpp_function_template.hpp>
#include <cppast/cpp_entity_kind.hpp>
#include "libclang_visitor.hpp"
#include "parse_functions.hpp"
using namespace cppast;
namespace
{
template <typename TemplateT, typename EntityT, typename Predicate>
type_safe::optional<typename TemplateT::builder> get_builder(const detail::parse_context& context,
const CXCursor& cur, Predicate p)
{
// we need the actual entity first, then the parameters
// so two visit calls are required
auto result = clang_getNullCursor();
detail::visit_children(cur, [&](const CXCursor& child) {
auto kind = clang_getCursorKind(child);
if (kind == CXCursor_TemplateTypeParameter || kind == CXCursor_NonTypeTemplateParameter
|| kind == CXCursor_TemplateTemplateParameter)
return;
DEBUG_ASSERT(clang_Cursor_isNull(result), detail::parse_error_handler{}, cur,
"unexpected child of template");
result = child;
});
DEBUG_ASSERT(!clang_Cursor_isNull(result), detail::parse_error_handler{}, cur,
"missing child of template");
auto entity = detail::parse_entity(context, nullptr, result, cur);
if (!entity)
return type_safe::nullopt;
DEBUG_ASSERT(p(entity->kind()), detail::parse_error_handler{}, cur, "wrong child of template");
return typename TemplateT::builder(
std::unique_ptr<EntityT>(static_cast<EntityT*>(entity.release())));
}
std::unique_ptr<cpp_template_parameter> parse_type_parameter(const detail::parse_context& context,
const CXCursor& cur)
{
DEBUG_ASSERT(clang_getCursorKind(cur) == CXCursor_TemplateTypeParameter,
detail::assert_handler{});
detail::cxtokenizer tokenizer(context.tu, context.file, cur);
detail::cxtoken_stream stream(tokenizer, cur);
auto name = detail::get_cursor_name(cur);
// syntax: typename/class [...] name [= ...]
auto keyword = cpp_template_keyword::keyword_class;
if (detail::skip_if(stream, "typename"))
keyword = cpp_template_keyword::keyword_typename;
else
detail::skip(stream, "class");
auto variadic = false;
if (detail::skip_if(stream, "..."))
variadic = true;
if (stream.peek() != "=")
detail::skip(stream, name.c_str());
std::unique_ptr<cpp_type> def;
if (detail::skip_if(stream, "="))
// default type
def = detail::parse_raw_type(context, stream, stream.end());
return cpp_template_type_parameter::build(*context.idx, detail::get_entity_id(cur),
name.c_str(), keyword, variadic, std::move(def));
}
std::unique_ptr<cpp_template_parameter> parse_non_type_parameter(
const detail::parse_context& context, const CXCursor& cur)
{
DEBUG_ASSERT(clang_getCursorKind(cur) == CXCursor_NonTypeTemplateParameter,
detail::assert_handler{});
auto name = detail::get_cursor_name(cur);
auto type = clang_getCursorType(cur);
cpp_attribute_list attributes;
auto def = detail::parse_default_value(attributes, context, cur, name.c_str());
detail::cxtokenizer tokenizer(context.tu, context.file, cur);
detail::cxtoken_stream stream(tokenizer, cur);
// see if it is variadic
// syntax a): some-tokens ... name some-tokens
// syntax b): some-tokens (some-tokens ... name) some-tokens-or-...
// name might be empty, so can't loop until it occurs
// some-tokens will not contain ... or parenthesis, however
auto is_variadic = false;
for (; !stream.done(); stream.bump())
{
if (stream.peek() == "...")
{
is_variadic = true;
break;
}
else if (stream.peek() == ")")
break;
}
auto result = cpp_non_type_template_parameter::build(*context.idx, detail::get_entity_id(cur),
name.c_str(),
detail::parse_type(context, cur, type),
is_variadic, std::move(def));
result->add_attribute(attributes);
return result;
}
std::unique_ptr<cpp_template_template_parameter> parse_template_parameter(
const detail::parse_context& context, const CXCursor& cur)
{
DEBUG_ASSERT(clang_getCursorKind(cur) == CXCursor_TemplateTemplateParameter,
detail::assert_handler{});
detail::cxtokenizer tokenizer(context.tu, context.file, cur);
detail::cxtoken_stream stream(tokenizer, cur);
auto name = detail::get_cursor_name(cur);
// syntax: template <…> class/typename [...] name [= …]
detail::skip(stream, "template");
detail::skip_brackets(stream);
auto keyword = cpp_template_keyword::keyword_class;
if (detail::skip_if(stream, "typename"))
keyword = cpp_template_keyword::keyword_typename;
else
detail::skip(stream, "class");
auto is_variadic = detail::skip_if(stream, "...");
detail::skip(stream, name.c_str());
// now we can create the builder
cpp_template_template_parameter::builder builder(name.c_str(), is_variadic);
builder.keyword(keyword);
// look for parameters and default
detail::visit_children(cur, [&](const CXCursor& child) {
auto kind = clang_getCursorKind(child);
if (kind == CXCursor_TemplateTypeParameter)
builder.add_parameter(parse_type_parameter(context, child));
else if (kind == CXCursor_NonTypeTemplateParameter)
builder.add_parameter(parse_non_type_parameter(context, child));
else if (kind == CXCursor_TemplateTemplateParameter)
builder.add_parameter(parse_template_parameter(context, child));
else if (kind == CXCursor_TemplateRef)
{
auto target = clang_getCursorReferenced(child);
// stream is after the keyword
// syntax: = default
detail::skip(stream, "=");
std::string spelling;
while (!stream.done())
spelling += stream.get().c_str();
if (stream.unmunch())
{
DEBUG_ASSERT(!spelling.empty() && spelling.back() == '>', detail::assert_handler{});
spelling.pop_back();
DEBUG_ASSERT(!spelling.empty() && spelling.back() == '>', detail::assert_handler{});
}
builder.default_template(
cpp_template_ref(detail::get_entity_id(target), std::move(spelling)));
}
else
DEBUG_ASSERT(clang_isReference(kind), detail::parse_error_handler{}, cur,
"unexpected child of template template parameter");
});
return builder.finish(*context.idx, detail::get_entity_id(cur));
}
template <class Builder>
void parse_parameters(Builder& builder, const detail::parse_context& context, const CXCursor& cur)
{
// now visit to get the parameters
detail::visit_children(cur, [&](const CXCursor& child) {
auto kind = clang_getCursorKind(child);
if (kind == CXCursor_TemplateTypeParameter)
builder.add_parameter(parse_type_parameter(context, child));
else if (kind == CXCursor_NonTypeTemplateParameter)
builder.add_parameter(parse_non_type_parameter(context, child));
else if (kind == CXCursor_TemplateTemplateParameter)
builder.add_parameter(parse_template_parameter(context, child));
});
}
void handle_comment_attributes(cpp_entity& templ_entity, cpp_entity& non_template)
{
// steal comment
auto comment = type_safe::copy(non_template.comment());
non_template.set_comment(type_safe::nullopt);
templ_entity.set_comment(std::move(comment));
// copy attributes over
templ_entity.add_attribute(non_template.attributes());
}
} // namespace
std::unique_ptr<cpp_entity> detail::parse_cpp_alias_template(const detail::parse_context& context,
const CXCursor& cur)
{
DEBUG_ASSERT(clang_getCursorKind(cur) == CXCursor_TypeAliasTemplateDecl,
detail::assert_handler{});
auto builder
= get_builder<cpp_alias_template, cpp_type_alias>(context, cur, [](cpp_entity_kind k) {
return k == cpp_entity_kind::type_alias_t;
});
if (!builder)
return nullptr;
context.comments.match(builder.value().get(), cur);
builder.value().get().add_attribute(builder.value().get().begin()->attributes());
parse_parameters(builder.value(), context, cur);
return builder.value().finish(*context.idx, detail::get_entity_id(cur),
false); // not a definition
}
std::unique_ptr<cpp_entity> detail::parse_cpp_function_template(
const detail::parse_context& context, const CXCursor& cur, bool is_friend)
{
DEBUG_ASSERT(clang_getCursorKind(cur) == CXCursor_FunctionTemplate, detail::assert_handler{});
std::unique_ptr<cpp_entity> func;
switch (clang_getTemplateCursorKind(cur))
{
case CXCursor_FunctionDecl:
func = detail::parse_cpp_function(context, cur, is_friend);
break;
case CXCursor_CXXMethod:
if (auto sfunc = detail::try_parse_static_cpp_function(context, cur))
func = std::move(sfunc);
else
func = detail::parse_cpp_member_function(context, cur, is_friend);
break;
case CXCursor_ConversionFunction:
func = detail::parse_cpp_conversion_op(context, cur, is_friend);
break;
case CXCursor_Constructor:
func = detail::parse_cpp_constructor(context, cur, is_friend);
break;
default:
DEBUG_UNREACHABLE(detail::parse_error_handler{}, cur, "unexpected template cursor kind");
}
if (!func)
return nullptr;
auto func_ptr = func.get();
cpp_function_template::builder builder(
std::unique_ptr<cpp_function_base>(static_cast<cpp_function_base*>(func.release())));
parse_parameters(builder, context, cur);
handle_comment_attributes(builder.get(), *func_ptr);
return builder.finish(*context.idx, detail::get_entity_id(cur),
builder.get().function().is_definition());
}
namespace
{
template <class Builder>
void parse_arguments(Builder& b, const detail::parse_context& context, const CXCursor& cur)
{
detail::cxtokenizer tokenizer(context.tu, context.file, cur);
detail::cxtoken_stream stream(tokenizer, cur);
while (!stream.done() && !detail::skip_if(stream, detail::get_cursor_name(cur).c_str(), true))
stream.bump();
if (stream.peek() == "<")
{
auto iter = detail::find_closing_bracket(stream);
stream.bump();
auto args = detail::to_string(stream, iter);
b.add_unexposed_arguments(std::move(args));
}
else
b.add_unexposed_arguments(cpp_token_string::builder().finish());
}
} // namespace
std::unique_ptr<cpp_entity> detail::try_parse_cpp_function_template_specialization(
const detail::parse_context& context, const CXCursor& cur, bool is_friend)
{
auto templ = clang_getSpecializedCursorTemplate(cur);
if (clang_Cursor_isNull(templ))
return nullptr;
std::unique_ptr<cpp_entity> func;
switch (clang_getCursorKind(cur))
{
case CXCursor_FunctionDecl:
func = detail::parse_cpp_function(context, cur, is_friend);
break;
case CXCursor_CXXMethod:
if (auto sfunc = detail::try_parse_static_cpp_function(context, cur))
func = std::move(sfunc);
else
func = detail::parse_cpp_member_function(context, cur, is_friend);
break;
case CXCursor_ConversionFunction:
func = detail::parse_cpp_conversion_op(context, cur, is_friend);
break;
case CXCursor_Constructor:
func = detail::parse_cpp_constructor(context, cur, is_friend);
break;
default:
DEBUG_UNREACHABLE(detail::parse_error_handler{}, cur,
"unexpected function kind that is being specialized");
}
if (!func)
return nullptr;
auto func_ptr = func.get();
cpp_function_template_specialization::builder
builder(std::unique_ptr<cpp_function_base>(static_cast<cpp_function_base*>(func.release())),
cpp_template_ref(detail::get_entity_id(templ), ""));
parse_arguments(builder, context, cur);
handle_comment_attributes(builder.get(), *func_ptr);
return builder.finish(*context.idx, detail::get_entity_id(cur),
builder.get().function().is_definition());
}
std::unique_ptr<cpp_entity> detail::parse_cpp_class_template(const detail::parse_context& context,
const CXCursor& cur)
{
DEBUG_ASSERT(clang_getCursorKind(cur) == CXCursor_ClassTemplate, detail::assert_handler{});
auto c = detail::parse_cpp_class(context, cur, clang_getNullCursor());
if (!c)
return nullptr;
auto c_ptr = c.get();
cpp_class_template::builder builder(
std::unique_ptr<cpp_class>(static_cast<cpp_class*>(c.release())));
handle_comment_attributes(builder.get(), *c_ptr);
parse_parameters(builder, context, cur);
return builder.finish(*context.idx, detail::get_entity_id(cur),
builder.get().class_().is_definition());
}
std::unique_ptr<cpp_entity> detail::try_parse_full_cpp_class_template_specialization(
const detail::parse_context& context, const CXCursor& cur)
{
DEBUG_ASSERT(clang_getCursorKind(cur) == CXCursor_ClassDecl
|| clang_getCursorKind(cur) == CXCursor_StructDecl
|| clang_getCursorKind(cur) == CXCursor_UnionDecl,
detail::assert_handler{});
auto templ = clang_getSpecializedCursorTemplate(cur);
if (clang_Cursor_isNull(templ))
return nullptr;
return detail::parse_cpp_class_template_specialization(context, cur);
}
std::unique_ptr<cpp_entity> detail::parse_cpp_class_template_specialization(
const detail::parse_context& context, const CXCursor& cur)
{
DEBUG_ASSERT(clang_getCursorKind(cur) == CXCursor_ClassTemplatePartialSpecialization
|| clang_getCursorKind(cur) == CXCursor_ClassDecl
|| clang_getCursorKind(cur) == CXCursor_StructDecl
|| clang_getCursorKind(cur) == CXCursor_UnionDecl,
detail::assert_handler{});
auto primary = clang_getSpecializedCursorTemplate(cur);
auto c = detail::parse_cpp_class(context, cur, clang_getNullCursor());
if (!c)
return nullptr;
auto c_ptr = c.get();
cpp_class_template_specialization::builder
builder(std::unique_ptr<cpp_class>(static_cast<cpp_class*>(c.release())),
cpp_template_ref(detail::get_entity_id(primary), ""));
handle_comment_attributes(builder.get(), *c_ptr);
parse_parameters(builder, context, cur);
parse_arguments(builder, context, cur);
return builder.finish(*context.idx, detail::get_entity_id(cur),
builder.get().class_().is_definition());
}