ctf: move ctf_visitor_generate_ir to header file
[babeltrace.git] / src / plugins / ctf / common / src / metadata / tsdl / ast.hpp
CommitLineData
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
c802cacb 10#include <glib.h>
e98a2d6e
PP
11#include <stdint.h>
12#include <stdio.h>
c802cacb 13
3fadfbc0 14#include <babeltrace2/babeltrace.h>
c802cacb 15
1a6da3f9 16#include "common/assert.h"
c802cacb 17#include "common/list.h"
0f5c5d5c 18#include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */
e98a2d6e 19
087cd0f5 20#include "ctf-meta.hpp"
c802cacb 21#include "decoder.hpp"
a2a54545 22
e98a2d6e
PP
23// the parameter name (of the reentrant 'yyparse' function)
24// data is a pointer to a 'SParserParam' structure
25//#define YYPARSE_PARAM scanner
26
27struct ctf_node;
28struct ctf_parser;
1e649dff
PP
29struct ctf_visitor_generate_ir;
30
4164020e
SM
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
62enum node_type
63{
64#define ENTRY(S) S,
65 FOREACH_CTF_NODES(ENTRY)
e98a2d6e 66#undef ENTRY
087cd0f5
SM
67};
68
0f5c5d5c
SM
69inline const char *format_as(enum node_type type) noexcept
70{
71 switch (type) {
72#define ENTRY(S) \
73case S: \
74 return G_STRINGIFY(S);
75
76 FOREACH_CTF_NODES(ENTRY)
77#undef ENTRY
78 }
79
80 bt_common_abort();
81}
82
4164020e
SM
83enum ctf_unary
84{
85 UNARY_UNKNOWN = 0,
86 UNARY_STRING,
87 UNARY_SIGNED_CONSTANT,
88 UNARY_UNSIGNED_CONSTANT,
89 UNARY_SBRAC,
087cd0f5
SM
90};
91
0f5c5d5c
SM
92inline 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
4164020e
SM
114enum ctf_unary_link
115{
116 UNARY_LINK_UNKNOWN = 0,
117 UNARY_DOTLINK,
118 UNARY_ARROWLINK,
119 UNARY_DOTDOTDOT,
087cd0f5
SM
120};
121
4164020e
SM
122enum ctf_typedec
123{
124 TYPEDEC_UNKNOWN = 0,
125 TYPEDEC_ID, /* identifier */
126 TYPEDEC_NESTED, /* (), array or sequence */
087cd0f5
SM
127};
128
0f5c5d5c
SM
129inline 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
4164020e
SM
145enum 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,
e98a2d6e
PP
168};
169
0f5c5d5c
SM
170inline 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
4164020e
SM
240struct 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;
e98a2d6e
PP
469};
470
4164020e
SM
471struct ctf_ast
472{
473 struct ctf_node root;
e98a2d6e
PP
474};
475
476const char *node_type(struct ctf_node *node);
477
0f80c86b
SM
478struct ctf_visitor_generate_ir
479{
480 explicit ctf_visitor_generate_ir(ctf_metadata_decoder_config decoderConfig,
481 bt2c::Logger loggerParam) :
482 decoder_config {std::move(decoderConfig)},
483 logger {std::move(loggerParam)}
484 {
485 }
486
487 /* Trace IR trace class being filled (owned by this) */
488 bt_trace_class *trace_class = nullptr;
489
490 /* CTF meta trace being filled (owned by this) */
491 struct ctf_trace_class *ctf_tc = nullptr;
492
493 /* Current declaration scope (top of the stack) (owned by this) */
494 struct ctx_decl_scope *current_scope = nullptr;
495
496 /* True if trace declaration is visited */
497 bool is_trace_visited = false;
498
499 /* True if this is an LTTng trace */
500 bool is_lttng = false;
501
502 /* Config passed by the user */
503 struct ctf_metadata_decoder_config decoder_config;
504
505 bt2c::Logger logger;
506};
507
4164020e
SM
508struct ctf_visitor_generate_ir *
509ctf_visitor_generate_ir_create(const struct ctf_metadata_decoder_config *config);
1e649dff
PP
510
511void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir *visitor);
e98a2d6e 512
4164020e 513bt_trace_class *ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_generate_ir *visitor);
44c440bc 514
4164020e
SM
515struct ctf_trace_class *
516ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir *visitor);
e98a2d6e 517
1e649dff 518int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor,
4164020e 519 struct ctf_node *node);
e98a2d6e 520
0f5c5d5c 521int ctf_visitor_semantic_check(int depth, struct ctf_node *node, const bt2c::Logger& logger);
e98a2d6e 522
0f5c5d5c 523int ctf_visitor_parent_links(int depth, struct ctf_node *node, const bt2c::Logger& logger);
e98a2d6e 524
4164020e 525static inline char *ctf_ast_concatenate_unary_strings(struct bt_list_head *head)
1a6da3f9 526{
4164020e
SM
527 int i = 0;
528 GString *str;
529 struct ctf_node *node;
530
531 str = g_string_new(NULL);
532 BT_ASSERT(str);
533
534 bt_list_for_each_entry (node, head, siblings) {
535 char *src_string;
536
537 if (node->type != NODE_UNARY_EXPRESSION || node->u.unary_expression.type != UNARY_STRING ||
538 !((node->u.unary_expression.link != UNARY_LINK_UNKNOWN) ^ (i == 0))) {
539 goto error;
540 }
541
542 switch (node->u.unary_expression.link) {
543 case UNARY_DOTLINK:
544 g_string_append(str, ".");
545 break;
546 case UNARY_ARROWLINK:
547 g_string_append(str, "->");
548 break;
549 case UNARY_DOTDOTDOT:
550 g_string_append(str, "...");
551 break;
552 default:
553 break;
554 }
555
556 src_string = node->u.unary_expression.u.string;
557 g_string_append(str, src_string);
558 i++;
559 }
560
561 /* Destroys the container, returns the underlying string */
562 return g_string_free(str, FALSE);
1a6da3f9
PP
563
564error:
4164020e
SM
565 /* This always returns NULL */
566 return g_string_free(str, TRUE);
1a6da3f9
PP
567}
568
ecd7492f 569#ifndef BT_COMP_LOG_CUR_LVL
5bcd9f04 570# define BT_AST_LOG_LEVEL_UNUSED_ATTR __attribute__((unused))
ecd7492f 571#else
5bcd9f04 572# define BT_AST_LOG_LEVEL_UNUSED_ATTR
ecd7492f
MJ
573#endif
574
575static inline int ctf_ast_get_unary_uuid(struct bt_list_head *head, bt_uuid_t uuid,
0f5c5d5c 576 const bt2c::Logger& logger)
1a6da3f9 577{
4164020e
SM
578 int i = 0;
579 int ret = 0;
580 struct ctf_node *node;
581
582 bt_list_for_each_entry (node, head, siblings) {
583 int uexpr_type = node->u.unary_expression.type;
584 int uexpr_link = node->u.unary_expression.link;
585 const char *src_string;
586
587 if (node->type != NODE_UNARY_EXPRESSION || uexpr_type != UNARY_STRING ||
588 uexpr_link != UNARY_LINK_UNKNOWN || i != 0) {
589 ret = -EINVAL;
590 goto end;
591 }
592
593 src_string = node->u.unary_expression.u.string;
594 ret = bt_uuid_from_str(src_string, uuid);
595 if (ret) {
0f5c5d5c 596 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot parse UUID: uuid=\"{}\"", src_string);
4164020e
SM
597 goto end;
598 }
599 }
1a6da3f9
PP
600
601end:
4164020e 602 return ret;
1a6da3f9
PP
603}
604
e98a2d6e 605#endif /* _CTF_AST_H */
This page took 0.124451 seconds and 4 git commands to generate.