2 * SPDX-License-Identifier: MIT
4 * Copyright 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 #ifndef BABELTRACE_PLUGINS_CTF_COMMON_SRC_METADATA_TSDL_AST_HPP
8 #define BABELTRACE_PLUGINS_CTF_COMMON_SRC_METADATA_TSDL_AST_HPP
16 #include <babeltrace2/babeltrace.h>
18 #include "common/assert.h"
19 #include "common/list.h"
20 #include "cpp-common/bt2c/logging.hpp"
21 #include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */
23 #include "ctf-meta.hpp"
25 // the parameter name (of the reentrant 'yyparse' function)
26 // data is a pointer to a 'SParserParam' structure
27 //#define YYPARSE_PARAM scanner
31 struct ctf_visitor_generate_ir;
33 #define EINCOMPLETE 1000
35 #define FOREACH_CTF_NODES(F) \
45 F(NODE_CTF_EXPRESSION) \
46 F(NODE_UNARY_EXPRESSION) \
48 F(NODE_TYPEALIAS_TARGET) \
49 F(NODE_TYPEALIAS_ALIAS) \
51 F(NODE_TYPE_SPECIFIER) \
52 F(NODE_TYPE_SPECIFIER_LIST) \
54 F(NODE_TYPE_DECLARATOR) \
55 F(NODE_FLOATING_POINT) \
60 F(NODE_STRUCT_OR_VARIANT_DECLARATION) \
67 FOREACH_CTF_NODES(ENTRY)
71 inline const char *format_as(enum node_type type) noexcept
76 return G_STRINGIFY(S);
78 FOREACH_CTF_NODES(ENTRY)
89 UNARY_SIGNED_CONSTANT,
90 UNARY_UNSIGNED_CONSTANT,
94 inline const char *format_as(ctf_unary value) noexcept
98 return "UNARY_UNKNOWN";
101 return "UNARY_STRING";
103 case UNARY_SIGNED_CONSTANT:
104 return "UNARY_SIGNED_CONSTANT";
106 case UNARY_UNSIGNED_CONSTANT:
107 return "UNARY_UNSIGNED_CONSTANT";
110 return "UNARY_SBRAC";
118 UNARY_LINK_UNKNOWN = 0,
127 TYPEDEC_ID, /* identifier */
128 TYPEDEC_NESTED, /* (), array or sequence */
131 inline const char *format_as(ctf_typedec value) noexcept
134 case TYPEDEC_UNKNOWN:
135 return "TYPEDEC_UNKNOWN";
141 return "TYPEDEC_NESTED";
149 TYPESPEC_UNKNOWN = 0,
164 TYPESPEC_FLOATING_POINT,
172 inline const char *format_as(ctf_typespec value) noexcept
175 case TYPESPEC_UNKNOWN:
176 return "TYPESPEC_UNKNOWN";
179 return "TYPESPEC_VOID";
182 return "TYPESPEC_CHAR";
185 return "TYPESPEC_SHORT";
188 return "TYPESPEC_INT";
191 return "TYPESPEC_LONG";
194 return "TYPESPEC_FLOAT";
196 case TYPESPEC_DOUBLE:
197 return "TYPESPEC_DOUBLE";
199 case TYPESPEC_SIGNED:
200 return "TYPESPEC_SIGNED";
202 case TYPESPEC_UNSIGNED:
203 return "TYPESPEC_UNSIGNED";
206 return "TYPESPEC_BOOL";
208 case TYPESPEC_COMPLEX:
209 return "TYPESPEC_COMPLEX";
211 case TYPESPEC_IMAGINARY:
212 return "TYPESPEC_IMAGINARY";
215 return "TYPESPEC_CONST";
217 case TYPESPEC_ID_TYPE:
218 return "TYPESPEC_ID_TYPE";
220 case TYPESPEC_FLOATING_POINT:
221 return "TYPESPEC_FLOATING_POINT";
223 case TYPESPEC_INTEGER:
224 return "TYPESPEC_INTEGER";
226 case TYPESPEC_STRING:
227 return "TYPESPEC_STRING";
229 case TYPESPEC_STRUCT:
230 return "TYPESPEC_STRUCT";
232 case TYPESPEC_VARIANT:
233 return "TYPESPEC_VARIANT";
236 return "TYPESPEC_ENUM";
245 * Parent node is only set on demand by specific visitor.
247 struct ctf_node *parent;
248 struct bt_list_head siblings;
249 struct bt_list_head tmp_head;
252 * We mark nodes visited in the generate-ir phase (last
253 * phase). We only mark the 1-depth level nodes as visited
254 * (never the root node, and not their sub-nodes). This allows
255 * skipping already visited nodes when doing incremental
269 * Children nodes are ctf_expression, field_class_def,
270 * field_class_alias and field_class_specifier_list.
272 struct bt_list_head declaration_list;
273 struct bt_list_head trace;
274 struct bt_list_head env;
275 struct bt_list_head stream;
276 struct bt_list_head event;
277 struct bt_list_head clock;
278 struct bt_list_head callsite;
283 * Children nodes are ctf_expression, field_class_def,
284 * field_class_alias and field_class_specifier_list.
286 struct bt_list_head declaration_list;
291 * Children nodes are ctf_expression, field_class_def,
292 * field_class_alias and field_class_specifier_list.
294 struct bt_list_head declaration_list;
299 * Children nodes are ctf_expression, field_class_def,
300 * field_class_alias and field_class_specifier_list.
302 struct bt_list_head declaration_list;
307 * Children nodes are ctf_expression, field_class_def,
308 * field_class_alias and field_class_specifier_list.
310 struct bt_list_head declaration_list;
315 * Children nodes are ctf_expression, field_class_def,
316 * field_class_alias and field_class_specifier_list.
318 struct bt_list_head declaration_list;
323 * Children nodes are ctf_expression, field_class_def,
324 * field_class_alias and field_class_specifier_list.
326 struct bt_list_head declaration_list;
330 struct bt_list_head left; /* Should be string */
331 struct bt_list_head right; /* Unary exp. or type */
339 * string for identifier, id_type, keywords,
340 * string literals and character constants.
343 int64_t signed_constant;
344 uint64_t unsigned_constant;
345 struct ctf_node *sbrac_exp;
351 struct ctf_node *field_class_specifier_list;
352 struct bt_list_head field_class_declarators;
354 /* new type is "alias", existing type "target" */
357 struct ctf_node *field_class_specifier_list;
358 struct bt_list_head field_class_declarators;
359 } field_class_alias_target;
362 struct ctf_node *field_class_specifier_list;
363 struct bt_list_head field_class_declarators;
364 } field_class_alias_name;
367 struct ctf_node *target;
368 struct ctf_node *alias;
373 /* For struct, variant and enum */
374 struct ctf_node *node;
376 } field_class_specifier;
379 /* list of field_class_specifier */
380 struct bt_list_head head;
381 } field_class_specifier_list;
384 unsigned int const_qualifier;
388 struct bt_list_head pointers;
395 /* typedec has no pointer list */
396 struct ctf_node *field_class_declarator;
398 * unary expression (value) or
399 * field_class_specifier_list.
401 struct bt_list_head length;
402 /* for abstract type declarator */
403 unsigned int abstract_array;
406 struct ctf_node *bitfield_len;
407 } field_class_declarator;
410 /* Children nodes are ctf_expression. */
411 struct bt_list_head expressions;
415 /* Children nodes are ctf_expression. */
416 struct bt_list_head expressions;
420 /* Children nodes are ctf_expression. */
421 struct bt_list_head expressions;
427 * Range list or single value node. Contains unary
430 struct bt_list_head values;
436 * Either NULL, or points to unary expression or
437 * field_class_specifier_list.
439 struct ctf_node *container_field_class;
440 struct bt_list_head enumerator_list;
445 struct ctf_node *field_class_specifier_list;
446 struct bt_list_head field_class_declarators;
447 } struct_or_variant_declaration;
453 * list of field_class_def, field_class_alias and
456 struct bt_list_head declaration_list;
463 * list of field_class_def, field_class_alias and
466 struct bt_list_head declaration_list;
468 struct bt_list_head min_align; /* align() attribute */
475 struct ctf_node root;
478 const char *node_type(struct ctf_node *node);
480 struct ctf_visitor_generate_ir
482 using UP = std::unique_ptr<ctf_visitor_generate_ir>;
484 explicit ctf_visitor_generate_ir(const bt2c::Logger& parentLogger) :
485 logger {parentLogger, "PLUGIN/CTF/VISITOR-GENERATE-IR"}
489 ~ctf_visitor_generate_ir();
493 /* CTF meta trace being filled (owned by this) */
494 struct ctf_trace_class *ctf_tc = nullptr;
496 /* Current declaration scope (top of the stack) (owned by this) */
497 struct ctx_decl_scope *current_scope = nullptr;
499 /* True if trace declaration is visited */
500 bool is_trace_visited = false;
502 /* True if this is an LTTng trace */
503 bool is_lttng = false;
506 ctf_visitor_generate_ir::UP ctf_visitor_generate_ir_create(const bt2c::Logger& parentLogger);
508 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor,
509 struct ctf_node *node);
511 int ctf_visitor_semantic_check(int depth, struct ctf_node *node, const bt2c::Logger& logger);
513 int ctf_visitor_parent_links(int depth, struct ctf_node *node, const bt2c::Logger& logger);
515 static inline char *ctf_ast_concatenate_unary_strings(struct bt_list_head *head)
519 struct ctf_node *node;
521 str = g_string_new(NULL);
524 bt_list_for_each_entry (node, head, siblings) {
527 if (node->type != NODE_UNARY_EXPRESSION || node->u.unary_expression.type != UNARY_STRING ||
528 !((node->u.unary_expression.link != UNARY_LINK_UNKNOWN) ^ (i == 0))) {
532 switch (node->u.unary_expression.link) {
534 g_string_append(str, ".");
536 case UNARY_ARROWLINK:
537 g_string_append(str, "->");
539 case UNARY_DOTDOTDOT:
540 g_string_append(str, "...");
546 src_string = node->u.unary_expression.u.string;
547 g_string_append(str, src_string);
551 /* Destroys the container, returns the underlying string */
552 return g_string_free(str, FALSE);
555 /* This always returns NULL */
556 return g_string_free(str, TRUE);
559 static inline int ctf_ast_get_unary_uuid(struct bt_list_head *head, bt_uuid_t uuid,
560 const bt2c::Logger& logger)
564 struct ctf_node *node;
566 bt_list_for_each_entry (node, head, siblings) {
567 int uexpr_type = node->u.unary_expression.type;
568 int uexpr_link = node->u.unary_expression.link;
569 const char *src_string;
571 if (node->type != NODE_UNARY_EXPRESSION || uexpr_type != UNARY_STRING ||
572 uexpr_link != UNARY_LINK_UNKNOWN || i != 0) {
577 src_string = node->u.unary_expression.u.string;
578 ret = bt_uuid_from_c_str(src_string, uuid);
580 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot parse UUID: uuid=\"{}\"", src_string);
589 #endif /* BABELTRACE_PLUGINS_CTF_COMMON_SRC_METADATA_TSDL_AST_HPP */