cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / plugins / ctf / common / src / metadata / tsdl / ast.hpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
7 #ifndef _CTF_AST_H
8 #define _CTF_AST_H
9
10 #include <memory>
11
12 #include <glib.h>
13 #include <stdint.h>
14 #include <stdio.h>
15
16 #include <babeltrace2/babeltrace.h>
17
18 #include "common/assert.h"
19 #include "common/list.h"
20 #include "cpp-common/bt2/trace-ir.hpp"
21 #include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */
22
23 #include "ctf-meta.hpp"
24 #include "decoder.hpp"
25
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;
32 struct ctf_visitor_generate_ir;
33
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)
69 #undef ENTRY
70 };
71
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
86 enum ctf_unary
87 {
88 UNARY_UNKNOWN = 0,
89 UNARY_STRING,
90 UNARY_SIGNED_CONSTANT,
91 UNARY_UNSIGNED_CONSTANT,
92 UNARY_SBRAC,
93 };
94
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
117 enum ctf_unary_link
118 {
119 UNARY_LINK_UNKNOWN = 0,
120 UNARY_DOTLINK,
121 UNARY_ARROWLINK,
122 UNARY_DOTDOTDOT,
123 };
124
125 enum ctf_typedec
126 {
127 TYPEDEC_UNKNOWN = 0,
128 TYPEDEC_ID, /* identifier */
129 TYPEDEC_NESTED, /* (), array or sequence */
130 };
131
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
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,
171 };
172
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
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;
472 };
473
474 struct ctf_ast
475 {
476 struct ctf_node root;
477 };
478
479 const char *node_type(struct ctf_node *node);
480
481 struct ctf_visitor_generate_ir
482 {
483 using UP = std::unique_ptr<ctf_visitor_generate_ir>;
484
485 explicit ctf_visitor_generate_ir(ctf_metadata_decoder_config decoderConfig,
486 bt2c::Logger loggerParam) :
487 decoder_config {std::move(decoderConfig)},
488 logger {std::move(loggerParam)}
489 {
490 }
491
492 ~ctf_visitor_generate_ir();
493
494 /* Trace IR trace class being filled (owned by this) */
495 bt2::TraceClass::Shared trace_class;
496
497 /* CTF meta trace being filled (owned by this) */
498 struct ctf_trace_class *ctf_tc = nullptr;
499
500 /* Current declaration scope (top of the stack) (owned by this) */
501 struct ctx_decl_scope *current_scope = nullptr;
502
503 /* True if trace declaration is visited */
504 bool is_trace_visited = false;
505
506 /* True if this is an LTTng trace */
507 bool is_lttng = false;
508
509 /* Config passed by the user */
510 struct ctf_metadata_decoder_config decoder_config;
511
512 bt2c::Logger logger;
513 };
514
515 ctf_visitor_generate_ir::UP
516 ctf_visitor_generate_ir_create(const struct ctf_metadata_decoder_config *config);
517
518 bt2::TraceClass::Shared
519 ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_generate_ir *visitor);
520
521 struct ctf_trace_class *
522 ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir *visitor);
523
524 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor,
525 struct ctf_node *node);
526
527 int ctf_visitor_semantic_check(int depth, struct ctf_node *node, const bt2c::Logger& logger);
528
529 int ctf_visitor_parent_links(int depth, struct ctf_node *node, const bt2c::Logger& logger);
530
531 static inline char *ctf_ast_concatenate_unary_strings(struct bt_list_head *head)
532 {
533 int i = 0;
534 GString *str;
535 struct ctf_node *node;
536
537 str = g_string_new(NULL);
538 BT_ASSERT(str);
539
540 bt_list_for_each_entry (node, head, siblings) {
541 char *src_string;
542
543 if (node->type != NODE_UNARY_EXPRESSION || node->u.unary_expression.type != UNARY_STRING ||
544 !((node->u.unary_expression.link != UNARY_LINK_UNKNOWN) ^ (i == 0))) {
545 goto error;
546 }
547
548 switch (node->u.unary_expression.link) {
549 case UNARY_DOTLINK:
550 g_string_append(str, ".");
551 break;
552 case UNARY_ARROWLINK:
553 g_string_append(str, "->");
554 break;
555 case UNARY_DOTDOTDOT:
556 g_string_append(str, "...");
557 break;
558 default:
559 break;
560 }
561
562 src_string = node->u.unary_expression.u.string;
563 g_string_append(str, src_string);
564 i++;
565 }
566
567 /* Destroys the container, returns the underlying string */
568 return g_string_free(str, FALSE);
569
570 error:
571 /* This always returns NULL */
572 return g_string_free(str, TRUE);
573 }
574
575 #ifndef BT_COMP_LOG_CUR_LVL
576 # define BT_AST_LOG_LEVEL_UNUSED_ATTR __attribute__((unused))
577 #else
578 # define BT_AST_LOG_LEVEL_UNUSED_ATTR
579 #endif
580
581 static inline int ctf_ast_get_unary_uuid(struct bt_list_head *head, bt_uuid_t uuid,
582 const bt2c::Logger& logger)
583 {
584 int i = 0;
585 int ret = 0;
586 struct ctf_node *node;
587
588 bt_list_for_each_entry (node, head, siblings) {
589 int uexpr_type = node->u.unary_expression.type;
590 int uexpr_link = node->u.unary_expression.link;
591 const char *src_string;
592
593 if (node->type != NODE_UNARY_EXPRESSION || uexpr_type != UNARY_STRING ||
594 uexpr_link != UNARY_LINK_UNKNOWN || i != 0) {
595 ret = -EINVAL;
596 goto end;
597 }
598
599 src_string = node->u.unary_expression.u.string;
600 ret = bt_uuid_from_str(src_string, uuid);
601 if (ret) {
602 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot parse UUID: uuid=\"{}\"", src_string);
603 goto end;
604 }
605 }
606
607 end:
608 return ret;
609 }
610
611 #endif /* _CTF_AST_H */
This page took 0.043174 seconds and 4 git commands to generate.