2 * SPDX-License-Identifier: MIT
4 * Copyright 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
14 #include <babeltrace2/babeltrace.h>
16 #include "common/assert.h"
17 #include "common/list.h"
18 #include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */
20 #include "ctf-meta.hpp"
21 #include "decoder.hpp"
23 // the parameter name (of the reentrant 'yyparse' function)
24 // data is a pointer to a 'SParserParam' structure
25 //#define YYPARSE_PARAM scanner
29 struct ctf_visitor_generate_ir;
31 #define EINCOMPLETE 1000
33 #define FOREACH_CTF_NODES(F) \
43 F(NODE_CTF_EXPRESSION) \
44 F(NODE_UNARY_EXPRESSION) \
46 F(NODE_TYPEALIAS_TARGET) \
47 F(NODE_TYPEALIAS_ALIAS) \
49 F(NODE_TYPE_SPECIFIER) \
50 F(NODE_TYPE_SPECIFIER_LIST) \
52 F(NODE_TYPE_DECLARATOR) \
53 F(NODE_FLOATING_POINT) \
58 F(NODE_STRUCT_OR_VARIANT_DECLARATION) \
65 FOREACH_CTF_NODES(ENTRY)
69 inline const char *format_as(enum node_type type) noexcept
74 return G_STRINGIFY(S);
76 FOREACH_CTF_NODES(ENTRY)
87 UNARY_SIGNED_CONSTANT,
88 UNARY_UNSIGNED_CONSTANT,
92 inline const char *format_as(ctf_unary value) noexcept
96 return "UNARY_UNKNOWN";
99 return "UNARY_STRING";
101 case UNARY_SIGNED_CONSTANT:
102 return "UNARY_SIGNED_CONSTANT";
104 case UNARY_UNSIGNED_CONSTANT:
105 return "UNARY_UNSIGNED_CONSTANT";
108 return "UNARY_SBRAC";
116 UNARY_LINK_UNKNOWN = 0,
125 TYPEDEC_ID, /* identifier */
126 TYPEDEC_NESTED, /* (), array or sequence */
129 inline const char *format_as(ctf_typedec value) noexcept
132 case TYPEDEC_UNKNOWN:
133 return "TYPEDEC_UNKNOWN";
139 return "TYPEDEC_NESTED";
147 TYPESPEC_UNKNOWN = 0,
162 TYPESPEC_FLOATING_POINT,
170 inline const char *format_as(ctf_typespec value) noexcept
173 case TYPESPEC_UNKNOWN:
174 return "TYPESPEC_UNKNOWN";
177 return "TYPESPEC_VOID";
180 return "TYPESPEC_CHAR";
183 return "TYPESPEC_SHORT";
186 return "TYPESPEC_INT";
189 return "TYPESPEC_LONG";
192 return "TYPESPEC_FLOAT";
194 case TYPESPEC_DOUBLE:
195 return "TYPESPEC_DOUBLE";
197 case TYPESPEC_SIGNED:
198 return "TYPESPEC_SIGNED";
200 case TYPESPEC_UNSIGNED:
201 return "TYPESPEC_UNSIGNED";
204 return "TYPESPEC_BOOL";
206 case TYPESPEC_COMPLEX:
207 return "TYPESPEC_COMPLEX";
209 case TYPESPEC_IMAGINARY:
210 return "TYPESPEC_IMAGINARY";
213 return "TYPESPEC_CONST";
215 case TYPESPEC_ID_TYPE:
216 return "TYPESPEC_ID_TYPE";
218 case TYPESPEC_FLOATING_POINT:
219 return "TYPESPEC_FLOATING_POINT";
221 case TYPESPEC_INTEGER:
222 return "TYPESPEC_INTEGER";
224 case TYPESPEC_STRING:
225 return "TYPESPEC_STRING";
227 case TYPESPEC_STRUCT:
228 return "TYPESPEC_STRUCT";
230 case TYPESPEC_VARIANT:
231 return "TYPESPEC_VARIANT";
234 return "TYPESPEC_ENUM";
243 * Parent node is only set on demand by specific visitor.
245 struct ctf_node *parent;
246 struct bt_list_head siblings;
247 struct bt_list_head tmp_head;
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
267 * Children nodes are ctf_expression, field_class_def,
268 * field_class_alias and field_class_specifier_list.
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;
281 * Children nodes are ctf_expression, field_class_def,
282 * field_class_alias and field_class_specifier_list.
284 struct bt_list_head declaration_list;
289 * Children nodes are ctf_expression, field_class_def,
290 * field_class_alias and field_class_specifier_list.
292 struct bt_list_head declaration_list;
297 * Children nodes are ctf_expression, field_class_def,
298 * field_class_alias and field_class_specifier_list.
300 struct bt_list_head declaration_list;
305 * Children nodes are ctf_expression, field_class_def,
306 * field_class_alias and field_class_specifier_list.
308 struct bt_list_head declaration_list;
313 * Children nodes are ctf_expression, field_class_def,
314 * field_class_alias and field_class_specifier_list.
316 struct bt_list_head declaration_list;
321 * Children nodes are ctf_expression, field_class_def,
322 * field_class_alias and field_class_specifier_list.
324 struct bt_list_head declaration_list;
328 struct bt_list_head left; /* Should be string */
329 struct bt_list_head right; /* Unary exp. or type */
337 * string for identifier, id_type, keywords,
338 * string literals and character constants.
341 int64_t signed_constant;
342 uint64_t unsigned_constant;
343 struct ctf_node *sbrac_exp;
349 struct ctf_node *field_class_specifier_list;
350 struct bt_list_head field_class_declarators;
352 /* new type is "alias", existing type "target" */
355 struct ctf_node *field_class_specifier_list;
356 struct bt_list_head field_class_declarators;
357 } field_class_alias_target;
360 struct ctf_node *field_class_specifier_list;
361 struct bt_list_head field_class_declarators;
362 } field_class_alias_name;
365 struct ctf_node *target;
366 struct ctf_node *alias;
371 /* For struct, variant and enum */
372 struct ctf_node *node;
374 } field_class_specifier;
377 /* list of field_class_specifier */
378 struct bt_list_head head;
379 } field_class_specifier_list;
382 unsigned int const_qualifier;
386 struct bt_list_head pointers;
393 /* typedec has no pointer list */
394 struct ctf_node *field_class_declarator;
396 * unary expression (value) or
397 * field_class_specifier_list.
399 struct bt_list_head length;
400 /* for abstract type declarator */
401 unsigned int abstract_array;
404 struct ctf_node *bitfield_len;
405 } field_class_declarator;
408 /* Children nodes are ctf_expression. */
409 struct bt_list_head expressions;
413 /* Children nodes are ctf_expression. */
414 struct bt_list_head expressions;
418 /* Children nodes are ctf_expression. */
419 struct bt_list_head expressions;
425 * Range list or single value node. Contains unary
428 struct bt_list_head values;
434 * Either NULL, or points to unary expression or
435 * field_class_specifier_list.
437 struct ctf_node *container_field_class;
438 struct bt_list_head enumerator_list;
443 struct ctf_node *field_class_specifier_list;
444 struct bt_list_head field_class_declarators;
445 } struct_or_variant_declaration;
451 * list of field_class_def, field_class_alias and
454 struct bt_list_head declaration_list;
461 * list of field_class_def, field_class_alias and
464 struct bt_list_head declaration_list;
466 struct bt_list_head min_align; /* align() attribute */
473 struct ctf_node root;
476 const char *node_type(struct ctf_node *node);
478 struct ctf_visitor_generate_ir
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)}
487 /* Trace IR trace class being filled (owned by this) */
488 bt_trace_class *trace_class = nullptr;
490 /* CTF meta trace being filled (owned by this) */
491 struct ctf_trace_class *ctf_tc = nullptr;
493 /* Current declaration scope (top of the stack) (owned by this) */
494 struct ctx_decl_scope *current_scope = nullptr;
496 /* True if trace declaration is visited */
497 bool is_trace_visited = false;
499 /* True if this is an LTTng trace */
500 bool is_lttng = false;
502 /* Config passed by the user */
503 struct ctf_metadata_decoder_config decoder_config;
508 struct ctf_visitor_generate_ir *
509 ctf_visitor_generate_ir_create(const struct ctf_metadata_decoder_config *config);
511 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir *visitor);
513 bt_trace_class *ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_generate_ir *visitor);
515 struct ctf_trace_class *
516 ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir *visitor);
518 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor,
519 struct ctf_node *node);
521 int ctf_visitor_semantic_check(int depth, struct ctf_node *node, const bt2c::Logger& logger);
523 int ctf_visitor_parent_links(int depth, struct ctf_node *node, const bt2c::Logger& logger);
525 static inline char *ctf_ast_concatenate_unary_strings(struct bt_list_head *head)
529 struct ctf_node *node;
531 str = g_string_new(NULL);
534 bt_list_for_each_entry (node, head, siblings) {
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))) {
542 switch (node->u.unary_expression.link) {
544 g_string_append(str, ".");
546 case UNARY_ARROWLINK:
547 g_string_append(str, "->");
549 case UNARY_DOTDOTDOT:
550 g_string_append(str, "...");
556 src_string = node->u.unary_expression.u.string;
557 g_string_append(str, src_string);
561 /* Destroys the container, returns the underlying string */
562 return g_string_free(str, FALSE);
565 /* This always returns NULL */
566 return g_string_free(str, TRUE);
569 #ifndef BT_COMP_LOG_CUR_LVL
570 # define BT_AST_LOG_LEVEL_UNUSED_ATTR __attribute__((unused))
572 # define BT_AST_LOG_LEVEL_UNUSED_ATTR
575 static inline int ctf_ast_get_unary_uuid(struct bt_list_head *head, bt_uuid_t uuid,
576 const bt2c::Logger& logger)
580 struct ctf_node *node;
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;
587 if (node->type != NODE_UNARY_EXPRESSION || uexpr_type != UNARY_STRING ||
588 uexpr_link != UNARY_LINK_UNKNOWN || i != 0) {
593 src_string = node->u.unary_expression.u.string;
594 ret = bt_uuid_from_str(src_string, uuid);
596 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot parse UUID: uuid=\"{}\"", src_string);
605 #endif /* _CTF_AST_H */