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