Commit | Line | Data |
---|---|---|
e98a2d6e | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
e98a2d6e | 3 | * |
0235b0db | 4 | * Copyright 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
e98a2d6e PP |
5 | */ |
6 | ||
0235b0db MJ |
7 | #ifndef _CTF_AST_H |
8 | #define _CTF_AST_H | |
9 | ||
708dab6b SM |
10 | #include <memory> |
11 | ||
c802cacb | 12 | #include <glib.h> |
e98a2d6e PP |
13 | #include <stdint.h> |
14 | #include <stdio.h> | |
c802cacb | 15 | |
3fadfbc0 | 16 | #include <babeltrace2/babeltrace.h> |
c802cacb | 17 | |
1a6da3f9 | 18 | #include "common/assert.h" |
c802cacb | 19 | #include "common/list.h" |
70923576 | 20 | #include "cpp-common/bt2/trace-ir.hpp" |
0f5c5d5c | 21 | #include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */ |
e98a2d6e | 22 | |
087cd0f5 | 23 | #include "ctf-meta.hpp" |
c802cacb | 24 | #include "decoder.hpp" |
a2a54545 | 25 | |
e98a2d6e PP |
26 | // the parameter name (of the reentrant 'yyparse' function) |
27 | // data is a pointer to a 'SParserParam' structure | |
28 | //#define YYPARSE_PARAM scanner | |
29 | ||
30 | struct ctf_node; | |
31 | struct ctf_parser; | |
1e649dff PP |
32 | struct ctf_visitor_generate_ir; |
33 | ||
4164020e SM |
34 | #define EINCOMPLETE 1000 |
35 | ||
36 | #define FOREACH_CTF_NODES(F) \ | |
37 | F(NODE_UNKNOWN) \ | |
38 | F(NODE_ROOT) \ | |
39 | F(NODE_ERROR) \ | |
40 | F(NODE_EVENT) \ | |
41 | F(NODE_STREAM) \ | |
42 | F(NODE_ENV) \ | |
43 | F(NODE_TRACE) \ | |
44 | F(NODE_CLOCK) \ | |
45 | F(NODE_CALLSITE) \ | |
46 | F(NODE_CTF_EXPRESSION) \ | |
47 | F(NODE_UNARY_EXPRESSION) \ | |
48 | F(NODE_TYPEDEF) \ | |
49 | F(NODE_TYPEALIAS_TARGET) \ | |
50 | F(NODE_TYPEALIAS_ALIAS) \ | |
51 | F(NODE_TYPEALIAS) \ | |
52 | F(NODE_TYPE_SPECIFIER) \ | |
53 | F(NODE_TYPE_SPECIFIER_LIST) \ | |
54 | F(NODE_POINTER) \ | |
55 | F(NODE_TYPE_DECLARATOR) \ | |
56 | F(NODE_FLOATING_POINT) \ | |
57 | F(NODE_INTEGER) \ | |
58 | F(NODE_STRING) \ | |
59 | F(NODE_ENUMERATOR) \ | |
60 | F(NODE_ENUM) \ | |
61 | F(NODE_STRUCT_OR_VARIANT_DECLARATION) \ | |
62 | F(NODE_VARIANT) \ | |
63 | F(NODE_STRUCT) | |
64 | ||
65 | enum node_type | |
66 | { | |
67 | #define ENTRY(S) S, | |
68 | FOREACH_CTF_NODES(ENTRY) | |
e98a2d6e | 69 | #undef ENTRY |
087cd0f5 SM |
70 | }; |
71 | ||
0f5c5d5c SM |
72 | inline const char *format_as(enum node_type type) noexcept |
73 | { | |
74 | switch (type) { | |
75 | #define ENTRY(S) \ | |
76 | case S: \ | |
77 | return G_STRINGIFY(S); | |
78 | ||
79 | FOREACH_CTF_NODES(ENTRY) | |
80 | #undef ENTRY | |
81 | } | |
82 | ||
83 | bt_common_abort(); | |
84 | } | |
85 | ||
4164020e SM |
86 | enum ctf_unary |
87 | { | |
88 | UNARY_UNKNOWN = 0, | |
89 | UNARY_STRING, | |
90 | UNARY_SIGNED_CONSTANT, | |
91 | UNARY_UNSIGNED_CONSTANT, | |
92 | UNARY_SBRAC, | |
087cd0f5 SM |
93 | }; |
94 | ||
0f5c5d5c SM |
95 | inline const char *format_as(ctf_unary value) noexcept |
96 | { | |
97 | switch (value) { | |
98 | case UNARY_UNKNOWN: | |
99 | return "UNARY_UNKNOWN"; | |
100 | ||
101 | case UNARY_STRING: | |
102 | return "UNARY_STRING"; | |
103 | ||
104 | case UNARY_SIGNED_CONSTANT: | |
105 | return "UNARY_SIGNED_CONSTANT"; | |
106 | ||
107 | case UNARY_UNSIGNED_CONSTANT: | |
108 | return "UNARY_UNSIGNED_CONSTANT"; | |
109 | ||
110 | case UNARY_SBRAC: | |
111 | return "UNARY_SBRAC"; | |
112 | } | |
113 | ||
114 | bt_common_abort(); | |
115 | } | |
116 | ||
4164020e SM |
117 | enum ctf_unary_link |
118 | { | |
119 | UNARY_LINK_UNKNOWN = 0, | |
120 | UNARY_DOTLINK, | |
121 | UNARY_ARROWLINK, | |
122 | UNARY_DOTDOTDOT, | |
087cd0f5 SM |
123 | }; |
124 | ||
4164020e SM |
125 | enum ctf_typedec |
126 | { | |
127 | TYPEDEC_UNKNOWN = 0, | |
128 | TYPEDEC_ID, /* identifier */ | |
129 | TYPEDEC_NESTED, /* (), array or sequence */ | |
087cd0f5 SM |
130 | }; |
131 | ||
0f5c5d5c SM |
132 | inline const char *format_as(ctf_typedec value) noexcept |
133 | { | |
134 | switch (value) { | |
135 | case TYPEDEC_UNKNOWN: | |
136 | return "TYPEDEC_UNKNOWN"; | |
137 | ||
138 | case TYPEDEC_ID: | |
139 | return "TYPEDEC_ID"; | |
140 | ||
141 | case TYPEDEC_NESTED: | |
142 | return "TYPEDEC_NESTED"; | |
143 | } | |
144 | ||
145 | bt_common_abort(); | |
146 | } | |
147 | ||
4164020e SM |
148 | enum ctf_typespec |
149 | { | |
150 | TYPESPEC_UNKNOWN = 0, | |
151 | TYPESPEC_VOID, | |
152 | TYPESPEC_CHAR, | |
153 | TYPESPEC_SHORT, | |
154 | TYPESPEC_INT, | |
155 | TYPESPEC_LONG, | |
156 | TYPESPEC_FLOAT, | |
157 | TYPESPEC_DOUBLE, | |
158 | TYPESPEC_SIGNED, | |
159 | TYPESPEC_UNSIGNED, | |
160 | TYPESPEC_BOOL, | |
161 | TYPESPEC_COMPLEX, | |
162 | TYPESPEC_IMAGINARY, | |
163 | TYPESPEC_CONST, | |
164 | TYPESPEC_ID_TYPE, | |
165 | TYPESPEC_FLOATING_POINT, | |
166 | TYPESPEC_INTEGER, | |
167 | TYPESPEC_STRING, | |
168 | TYPESPEC_STRUCT, | |
169 | TYPESPEC_VARIANT, | |
170 | TYPESPEC_ENUM, | |
e98a2d6e PP |
171 | }; |
172 | ||
0f5c5d5c SM |
173 | inline const char *format_as(ctf_typespec value) noexcept |
174 | { | |
175 | switch (value) { | |
176 | case TYPESPEC_UNKNOWN: | |
177 | return "TYPESPEC_UNKNOWN"; | |
178 | ||
179 | case TYPESPEC_VOID: | |
180 | return "TYPESPEC_VOID"; | |
181 | ||
182 | case TYPESPEC_CHAR: | |
183 | return "TYPESPEC_CHAR"; | |
184 | ||
185 | case TYPESPEC_SHORT: | |
186 | return "TYPESPEC_SHORT"; | |
187 | ||
188 | case TYPESPEC_INT: | |
189 | return "TYPESPEC_INT"; | |
190 | ||
191 | case TYPESPEC_LONG: | |
192 | return "TYPESPEC_LONG"; | |
193 | ||
194 | case TYPESPEC_FLOAT: | |
195 | return "TYPESPEC_FLOAT"; | |
196 | ||
197 | case TYPESPEC_DOUBLE: | |
198 | return "TYPESPEC_DOUBLE"; | |
199 | ||
200 | case TYPESPEC_SIGNED: | |
201 | return "TYPESPEC_SIGNED"; | |
202 | ||
203 | case TYPESPEC_UNSIGNED: | |
204 | return "TYPESPEC_UNSIGNED"; | |
205 | ||
206 | case TYPESPEC_BOOL: | |
207 | return "TYPESPEC_BOOL"; | |
208 | ||
209 | case TYPESPEC_COMPLEX: | |
210 | return "TYPESPEC_COMPLEX"; | |
211 | ||
212 | case TYPESPEC_IMAGINARY: | |
213 | return "TYPESPEC_IMAGINARY"; | |
214 | ||
215 | case TYPESPEC_CONST: | |
216 | return "TYPESPEC_CONST"; | |
217 | ||
218 | case TYPESPEC_ID_TYPE: | |
219 | return "TYPESPEC_ID_TYPE"; | |
220 | ||
221 | case TYPESPEC_FLOATING_POINT: | |
222 | return "TYPESPEC_FLOATING_POINT"; | |
223 | ||
224 | case TYPESPEC_INTEGER: | |
225 | return "TYPESPEC_INTEGER"; | |
226 | ||
227 | case TYPESPEC_STRING: | |
228 | return "TYPESPEC_STRING"; | |
229 | ||
230 | case TYPESPEC_STRUCT: | |
231 | return "TYPESPEC_STRUCT"; | |
232 | ||
233 | case TYPESPEC_VARIANT: | |
234 | return "TYPESPEC_VARIANT"; | |
235 | ||
236 | case TYPESPEC_ENUM: | |
237 | return "TYPESPEC_ENUM"; | |
238 | } | |
239 | ||
240 | bt_common_abort(); | |
241 | } | |
242 | ||
4164020e SM |
243 | struct ctf_node |
244 | { | |
245 | /* | |
246 | * Parent node is only set on demand by specific visitor. | |
247 | */ | |
248 | struct ctf_node *parent; | |
249 | struct bt_list_head siblings; | |
250 | struct bt_list_head tmp_head; | |
251 | unsigned int lineno; | |
252 | /* | |
253 | * We mark nodes visited in the generate-ir phase (last | |
254 | * phase). We only mark the 1-depth level nodes as visited | |
255 | * (never the root node, and not their sub-nodes). This allows | |
256 | * skipping already visited nodes when doing incremental | |
257 | * metadata append. | |
258 | */ | |
259 | int visited; | |
260 | ||
261 | enum node_type type; | |
262 | union | |
263 | { | |
264 | struct | |
265 | { | |
266 | } unknown; | |
267 | struct | |
268 | { | |
269 | /* | |
270 | * Children nodes are ctf_expression, field_class_def, | |
271 | * field_class_alias and field_class_specifier_list. | |
272 | */ | |
273 | struct bt_list_head declaration_list; | |
274 | struct bt_list_head trace; | |
275 | struct bt_list_head env; | |
276 | struct bt_list_head stream; | |
277 | struct bt_list_head event; | |
278 | struct bt_list_head clock; | |
279 | struct bt_list_head callsite; | |
280 | } root; | |
281 | struct | |
282 | { | |
283 | /* | |
284 | * Children nodes are ctf_expression, field_class_def, | |
285 | * field_class_alias and field_class_specifier_list. | |
286 | */ | |
287 | struct bt_list_head declaration_list; | |
288 | } event; | |
289 | struct | |
290 | { | |
291 | /* | |
292 | * Children nodes are ctf_expression, field_class_def, | |
293 | * field_class_alias and field_class_specifier_list. | |
294 | */ | |
295 | struct bt_list_head declaration_list; | |
296 | } stream; | |
297 | struct | |
298 | { | |
299 | /* | |
300 | * Children nodes are ctf_expression, field_class_def, | |
301 | * field_class_alias and field_class_specifier_list. | |
302 | */ | |
303 | struct bt_list_head declaration_list; | |
304 | } env; | |
305 | struct | |
306 | { | |
307 | /* | |
308 | * Children nodes are ctf_expression, field_class_def, | |
309 | * field_class_alias and field_class_specifier_list. | |
310 | */ | |
311 | struct bt_list_head declaration_list; | |
312 | } trace; | |
313 | struct | |
314 | { | |
315 | /* | |
316 | * Children nodes are ctf_expression, field_class_def, | |
317 | * field_class_alias and field_class_specifier_list. | |
318 | */ | |
319 | struct bt_list_head declaration_list; | |
320 | } clock; | |
321 | struct | |
322 | { | |
323 | /* | |
324 | * Children nodes are ctf_expression, field_class_def, | |
325 | * field_class_alias and field_class_specifier_list. | |
326 | */ | |
327 | struct bt_list_head declaration_list; | |
328 | } callsite; | |
329 | struct | |
330 | { | |
331 | struct bt_list_head left; /* Should be string */ | |
332 | struct bt_list_head right; /* Unary exp. or type */ | |
333 | } ctf_expression; | |
334 | struct | |
335 | { | |
336 | ctf_unary type; | |
337 | union | |
338 | { | |
339 | /* | |
340 | * string for identifier, id_type, keywords, | |
341 | * string literals and character constants. | |
342 | */ | |
343 | char *string; | |
344 | int64_t signed_constant; | |
345 | uint64_t unsigned_constant; | |
346 | struct ctf_node *sbrac_exp; | |
347 | } u; | |
348 | ctf_unary_link link; | |
349 | } unary_expression; | |
350 | struct | |
351 | { | |
352 | struct ctf_node *field_class_specifier_list; | |
353 | struct bt_list_head field_class_declarators; | |
354 | } field_class_def; | |
355 | /* new type is "alias", existing type "target" */ | |
356 | struct | |
357 | { | |
358 | struct ctf_node *field_class_specifier_list; | |
359 | struct bt_list_head field_class_declarators; | |
360 | } field_class_alias_target; | |
361 | struct | |
362 | { | |
363 | struct ctf_node *field_class_specifier_list; | |
364 | struct bt_list_head field_class_declarators; | |
365 | } field_class_alias_name; | |
366 | struct | |
367 | { | |
368 | struct ctf_node *target; | |
369 | struct ctf_node *alias; | |
370 | } field_class_alias; | |
371 | struct | |
372 | { | |
373 | ctf_typespec type; | |
374 | /* For struct, variant and enum */ | |
375 | struct ctf_node *node; | |
376 | const char *id_type; | |
377 | } field_class_specifier; | |
378 | struct | |
379 | { | |
380 | /* list of field_class_specifier */ | |
381 | struct bt_list_head head; | |
382 | } field_class_specifier_list; | |
383 | struct | |
384 | { | |
385 | unsigned int const_qualifier; | |
386 | } pointer; | |
387 | struct | |
388 | { | |
389 | struct bt_list_head pointers; | |
390 | ctf_typedec type; | |
391 | union | |
392 | { | |
393 | char *id; | |
394 | struct | |
395 | { | |
396 | /* typedec has no pointer list */ | |
397 | struct ctf_node *field_class_declarator; | |
398 | /* | |
399 | * unary expression (value) or | |
400 | * field_class_specifier_list. | |
401 | */ | |
402 | struct bt_list_head length; | |
403 | /* for abstract type declarator */ | |
404 | unsigned int abstract_array; | |
405 | } nested; | |
406 | } u; | |
407 | struct ctf_node *bitfield_len; | |
408 | } field_class_declarator; | |
409 | struct | |
410 | { | |
411 | /* Children nodes are ctf_expression. */ | |
412 | struct bt_list_head expressions; | |
413 | } floating_point; | |
414 | struct | |
415 | { | |
416 | /* Children nodes are ctf_expression. */ | |
417 | struct bt_list_head expressions; | |
418 | } integer; | |
419 | struct | |
420 | { | |
421 | /* Children nodes are ctf_expression. */ | |
422 | struct bt_list_head expressions; | |
423 | } string; | |
424 | struct | |
425 | { | |
426 | char *id; | |
427 | /* | |
428 | * Range list or single value node. Contains unary | |
429 | * expressions. | |
430 | */ | |
431 | struct bt_list_head values; | |
432 | } enumerator; | |
433 | struct | |
434 | { | |
435 | char *enum_id; | |
436 | /* | |
437 | * Either NULL, or points to unary expression or | |
438 | * field_class_specifier_list. | |
439 | */ | |
440 | struct ctf_node *container_field_class; | |
441 | struct bt_list_head enumerator_list; | |
442 | int has_body; | |
443 | } _enum; | |
444 | struct | |
445 | { | |
446 | struct ctf_node *field_class_specifier_list; | |
447 | struct bt_list_head field_class_declarators; | |
448 | } struct_or_variant_declaration; | |
449 | struct | |
450 | { | |
451 | char *name; | |
452 | char *choice; | |
453 | /* | |
454 | * list of field_class_def, field_class_alias and | |
455 | * declarations | |
456 | */ | |
457 | struct bt_list_head declaration_list; | |
458 | int has_body; | |
459 | } variant; | |
460 | struct | |
461 | { | |
462 | char *name; | |
463 | /* | |
464 | * list of field_class_def, field_class_alias and | |
465 | * declarations | |
466 | */ | |
467 | struct bt_list_head declaration_list; | |
468 | int has_body; | |
469 | struct bt_list_head min_align; /* align() attribute */ | |
470 | } _struct; | |
471 | } u; | |
e98a2d6e PP |
472 | }; |
473 | ||
4164020e SM |
474 | struct ctf_ast |
475 | { | |
476 | struct ctf_node root; | |
e98a2d6e PP |
477 | }; |
478 | ||
479 | const char *node_type(struct ctf_node *node); | |
480 | ||
708dab6b SM |
481 | struct ctf_visitor_generate_ir_deleter |
482 | { | |
483 | void operator()(struct ctf_visitor_generate_ir *visitor); | |
484 | }; | |
485 | ||
0f80c86b SM |
486 | struct ctf_visitor_generate_ir |
487 | { | |
708dab6b SM |
488 | using UP = std::unique_ptr<ctf_visitor_generate_ir, ctf_visitor_generate_ir_deleter>; |
489 | ||
0f80c86b SM |
490 | explicit ctf_visitor_generate_ir(ctf_metadata_decoder_config decoderConfig, |
491 | bt2c::Logger loggerParam) : | |
492 | decoder_config {std::move(decoderConfig)}, | |
493 | logger {std::move(loggerParam)} | |
494 | { | |
495 | } | |
496 | ||
497 | /* Trace IR trace class being filled (owned by this) */ | |
70923576 | 498 | bt2::TraceClass::Shared trace_class; |
0f80c86b SM |
499 | |
500 | /* CTF meta trace being filled (owned by this) */ | |
501 | struct ctf_trace_class *ctf_tc = nullptr; | |
502 | ||
503 | /* Current declaration scope (top of the stack) (owned by this) */ | |
504 | struct ctx_decl_scope *current_scope = nullptr; | |
505 | ||
506 | /* True if trace declaration is visited */ | |
507 | bool is_trace_visited = false; | |
508 | ||
509 | /* True if this is an LTTng trace */ | |
510 | bool is_lttng = false; | |
511 | ||
512 | /* Config passed by the user */ | |
513 | struct ctf_metadata_decoder_config decoder_config; | |
514 | ||
515 | bt2c::Logger logger; | |
516 | }; | |
517 | ||
708dab6b | 518 | ctf_visitor_generate_ir::UP |
4164020e | 519 | ctf_visitor_generate_ir_create(const struct ctf_metadata_decoder_config *config); |
1e649dff | 520 | |
70923576 SM |
521 | bt2::TraceClass::Shared |
522 | ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_generate_ir *visitor); | |
44c440bc | 523 | |
4164020e SM |
524 | struct ctf_trace_class * |
525 | ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir *visitor); | |
e98a2d6e | 526 | |
1e649dff | 527 | int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor, |
4164020e | 528 | struct ctf_node *node); |
e98a2d6e | 529 | |
0f5c5d5c | 530 | int ctf_visitor_semantic_check(int depth, struct ctf_node *node, const bt2c::Logger& logger); |
e98a2d6e | 531 | |
0f5c5d5c | 532 | int ctf_visitor_parent_links(int depth, struct ctf_node *node, const bt2c::Logger& logger); |
e98a2d6e | 533 | |
4164020e | 534 | static inline char *ctf_ast_concatenate_unary_strings(struct bt_list_head *head) |
1a6da3f9 | 535 | { |
4164020e SM |
536 | int i = 0; |
537 | GString *str; | |
538 | struct ctf_node *node; | |
539 | ||
540 | str = g_string_new(NULL); | |
541 | BT_ASSERT(str); | |
542 | ||
543 | bt_list_for_each_entry (node, head, siblings) { | |
544 | char *src_string; | |
545 | ||
546 | if (node->type != NODE_UNARY_EXPRESSION || node->u.unary_expression.type != UNARY_STRING || | |
547 | !((node->u.unary_expression.link != UNARY_LINK_UNKNOWN) ^ (i == 0))) { | |
548 | goto error; | |
549 | } | |
550 | ||
551 | switch (node->u.unary_expression.link) { | |
552 | case UNARY_DOTLINK: | |
553 | g_string_append(str, "."); | |
554 | break; | |
555 | case UNARY_ARROWLINK: | |
556 | g_string_append(str, "->"); | |
557 | break; | |
558 | case UNARY_DOTDOTDOT: | |
559 | g_string_append(str, "..."); | |
560 | break; | |
561 | default: | |
562 | break; | |
563 | } | |
564 | ||
565 | src_string = node->u.unary_expression.u.string; | |
566 | g_string_append(str, src_string); | |
567 | i++; | |
568 | } | |
569 | ||
570 | /* Destroys the container, returns the underlying string */ | |
571 | return g_string_free(str, FALSE); | |
1a6da3f9 PP |
572 | |
573 | error: | |
4164020e SM |
574 | /* This always returns NULL */ |
575 | return g_string_free(str, TRUE); | |
1a6da3f9 PP |
576 | } |
577 | ||
ecd7492f | 578 | #ifndef BT_COMP_LOG_CUR_LVL |
5bcd9f04 | 579 | # define BT_AST_LOG_LEVEL_UNUSED_ATTR __attribute__((unused)) |
ecd7492f | 580 | #else |
5bcd9f04 | 581 | # define BT_AST_LOG_LEVEL_UNUSED_ATTR |
ecd7492f MJ |
582 | #endif |
583 | ||
584 | static inline int ctf_ast_get_unary_uuid(struct bt_list_head *head, bt_uuid_t uuid, | |
0f5c5d5c | 585 | const bt2c::Logger& logger) |
1a6da3f9 | 586 | { |
4164020e SM |
587 | int i = 0; |
588 | int ret = 0; | |
589 | struct ctf_node *node; | |
590 | ||
591 | bt_list_for_each_entry (node, head, siblings) { | |
592 | int uexpr_type = node->u.unary_expression.type; | |
593 | int uexpr_link = node->u.unary_expression.link; | |
594 | const char *src_string; | |
595 | ||
596 | if (node->type != NODE_UNARY_EXPRESSION || uexpr_type != UNARY_STRING || | |
597 | uexpr_link != UNARY_LINK_UNKNOWN || i != 0) { | |
598 | ret = -EINVAL; | |
599 | goto end; | |
600 | } | |
601 | ||
602 | src_string = node->u.unary_expression.u.string; | |
603 | ret = bt_uuid_from_str(src_string, uuid); | |
604 | if (ret) { | |
0f5c5d5c | 605 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot parse UUID: uuid=\"{}\"", src_string); |
4164020e SM |
606 | goto end; |
607 | } | |
608 | } | |
1a6da3f9 PP |
609 | |
610 | end: | |
4164020e | 611 | return ret; |
1a6da3f9 PP |
612 | } |
613 | ||
e98a2d6e | 614 | #endif /* _CTF_AST_H */ |