2 * SPDX-License-Identifier: MIT
4 * Copyright 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
16 #include <babeltrace2/babeltrace.h>
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 */
23 #include "ctf-meta.hpp"
24 #include "decoder.hpp"
26 // the parameter name (of the reentrant 'yyparse' function)
27 // data is a pointer to a 'SParserParam' structure
28 //#define YYPARSE_PARAM scanner
32 struct ctf_visitor_generate_ir;
34 #define EINCOMPLETE 1000
36 #define FOREACH_CTF_NODES(F) \
46 F(NODE_CTF_EXPRESSION) \
47 F(NODE_UNARY_EXPRESSION) \
49 F(NODE_TYPEALIAS_TARGET) \
50 F(NODE_TYPEALIAS_ALIAS) \
52 F(NODE_TYPE_SPECIFIER) \
53 F(NODE_TYPE_SPECIFIER_LIST) \
55 F(NODE_TYPE_DECLARATOR) \
56 F(NODE_FLOATING_POINT) \
61 F(NODE_STRUCT_OR_VARIANT_DECLARATION) \
68 FOREACH_CTF_NODES(ENTRY)
72 inline const char *format_as(enum node_type type) noexcept
77 return G_STRINGIFY(S);
79 FOREACH_CTF_NODES(ENTRY)
90 UNARY_SIGNED_CONSTANT,
91 UNARY_UNSIGNED_CONSTANT,
95 inline const char *format_as(ctf_unary value) noexcept
99 return "UNARY_UNKNOWN";
102 return "UNARY_STRING";
104 case UNARY_SIGNED_CONSTANT:
105 return "UNARY_SIGNED_CONSTANT";
107 case UNARY_UNSIGNED_CONSTANT:
108 return "UNARY_UNSIGNED_CONSTANT";
111 return "UNARY_SBRAC";
119 UNARY_LINK_UNKNOWN = 0,
128 TYPEDEC_ID, /* identifier */
129 TYPEDEC_NESTED, /* (), array or sequence */
132 inline const char *format_as(ctf_typedec value) noexcept
135 case TYPEDEC_UNKNOWN:
136 return "TYPEDEC_UNKNOWN";
142 return "TYPEDEC_NESTED";
150 TYPESPEC_UNKNOWN = 0,
165 TYPESPEC_FLOATING_POINT,
173 inline const char *format_as(ctf_typespec value) noexcept
176 case TYPESPEC_UNKNOWN:
177 return "TYPESPEC_UNKNOWN";
180 return "TYPESPEC_VOID";
183 return "TYPESPEC_CHAR";
186 return "TYPESPEC_SHORT";
189 return "TYPESPEC_INT";
192 return "TYPESPEC_LONG";
195 return "TYPESPEC_FLOAT";
197 case TYPESPEC_DOUBLE:
198 return "TYPESPEC_DOUBLE";
200 case TYPESPEC_SIGNED:
201 return "TYPESPEC_SIGNED";
203 case TYPESPEC_UNSIGNED:
204 return "TYPESPEC_UNSIGNED";
207 return "TYPESPEC_BOOL";
209 case TYPESPEC_COMPLEX:
210 return "TYPESPEC_COMPLEX";
212 case TYPESPEC_IMAGINARY:
213 return "TYPESPEC_IMAGINARY";
216 return "TYPESPEC_CONST";
218 case TYPESPEC_ID_TYPE:
219 return "TYPESPEC_ID_TYPE";
221 case TYPESPEC_FLOATING_POINT:
222 return "TYPESPEC_FLOATING_POINT";
224 case TYPESPEC_INTEGER:
225 return "TYPESPEC_INTEGER";
227 case TYPESPEC_STRING:
228 return "TYPESPEC_STRING";
230 case TYPESPEC_STRUCT:
231 return "TYPESPEC_STRUCT";
233 case TYPESPEC_VARIANT:
234 return "TYPESPEC_VARIANT";
237 return "TYPESPEC_ENUM";
246 * Parent node is only set on demand by specific visitor.
248 struct ctf_node *parent;
249 struct bt_list_head siblings;
250 struct bt_list_head tmp_head;
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
270 * Children nodes are ctf_expression, field_class_def,
271 * field_class_alias and field_class_specifier_list.
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;
284 * Children nodes are ctf_expression, field_class_def,
285 * field_class_alias and field_class_specifier_list.
287 struct bt_list_head declaration_list;
292 * Children nodes are ctf_expression, field_class_def,
293 * field_class_alias and field_class_specifier_list.
295 struct bt_list_head declaration_list;
300 * Children nodes are ctf_expression, field_class_def,
301 * field_class_alias and field_class_specifier_list.
303 struct bt_list_head declaration_list;
308 * Children nodes are ctf_expression, field_class_def,
309 * field_class_alias and field_class_specifier_list.
311 struct bt_list_head declaration_list;
316 * Children nodes are ctf_expression, field_class_def,
317 * field_class_alias and field_class_specifier_list.
319 struct bt_list_head declaration_list;
324 * Children nodes are ctf_expression, field_class_def,
325 * field_class_alias and field_class_specifier_list.
327 struct bt_list_head declaration_list;
331 struct bt_list_head left; /* Should be string */
332 struct bt_list_head right; /* Unary exp. or type */
340 * string for identifier, id_type, keywords,
341 * string literals and character constants.
344 int64_t signed_constant;
345 uint64_t unsigned_constant;
346 struct ctf_node *sbrac_exp;
352 struct ctf_node *field_class_specifier_list;
353 struct bt_list_head field_class_declarators;
355 /* new type is "alias", existing type "target" */
358 struct ctf_node *field_class_specifier_list;
359 struct bt_list_head field_class_declarators;
360 } field_class_alias_target;
363 struct ctf_node *field_class_specifier_list;
364 struct bt_list_head field_class_declarators;
365 } field_class_alias_name;
368 struct ctf_node *target;
369 struct ctf_node *alias;
374 /* For struct, variant and enum */
375 struct ctf_node *node;
377 } field_class_specifier;
380 /* list of field_class_specifier */
381 struct bt_list_head head;
382 } field_class_specifier_list;
385 unsigned int const_qualifier;
389 struct bt_list_head pointers;
396 /* typedec has no pointer list */
397 struct ctf_node *field_class_declarator;
399 * unary expression (value) or
400 * field_class_specifier_list.
402 struct bt_list_head length;
403 /* for abstract type declarator */
404 unsigned int abstract_array;
407 struct ctf_node *bitfield_len;
408 } field_class_declarator;
411 /* Children nodes are ctf_expression. */
412 struct bt_list_head expressions;
416 /* Children nodes are ctf_expression. */
417 struct bt_list_head expressions;
421 /* Children nodes are ctf_expression. */
422 struct bt_list_head expressions;
428 * Range list or single value node. Contains unary
431 struct bt_list_head values;
437 * Either NULL, or points to unary expression or
438 * field_class_specifier_list.
440 struct ctf_node *container_field_class;
441 struct bt_list_head enumerator_list;
446 struct ctf_node *field_class_specifier_list;
447 struct bt_list_head field_class_declarators;
448 } struct_or_variant_declaration;
454 * list of field_class_def, field_class_alias and
457 struct bt_list_head declaration_list;
464 * list of field_class_def, field_class_alias and
467 struct bt_list_head declaration_list;
469 struct bt_list_head min_align; /* align() attribute */
476 struct ctf_node root;
479 const char *node_type(struct ctf_node *node);
481 struct ctf_visitor_generate_ir_deleter
483 void operator()(struct ctf_visitor_generate_ir *visitor);
486 struct ctf_visitor_generate_ir
488 using UP = std::unique_ptr<ctf_visitor_generate_ir, ctf_visitor_generate_ir_deleter>;
490 explicit ctf_visitor_generate_ir(ctf_metadata_decoder_config decoderConfig,
491 bt2c::Logger loggerParam) :
492 decoder_config {std::move(decoderConfig)},
493 logger {std::move(loggerParam)}
497 /* Trace IR trace class being filled (owned by this) */
498 bt2::TraceClass::Shared trace_class;
500 /* CTF meta trace being filled (owned by this) */
501 struct ctf_trace_class *ctf_tc = nullptr;
503 /* Current declaration scope (top of the stack) (owned by this) */
504 struct ctx_decl_scope *current_scope = nullptr;
506 /* True if trace declaration is visited */
507 bool is_trace_visited = false;
509 /* True if this is an LTTng trace */
510 bool is_lttng = false;
512 /* Config passed by the user */
513 struct ctf_metadata_decoder_config decoder_config;
518 ctf_visitor_generate_ir::UP
519 ctf_visitor_generate_ir_create(const struct ctf_metadata_decoder_config *config);
521 bt2::TraceClass::Shared
522 ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_generate_ir *visitor);
524 struct ctf_trace_class *
525 ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir *visitor);
527 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor,
528 struct ctf_node *node);
530 int ctf_visitor_semantic_check(int depth, struct ctf_node *node, const bt2c::Logger& logger);
532 int ctf_visitor_parent_links(int depth, struct ctf_node *node, const bt2c::Logger& logger);
534 static inline char *ctf_ast_concatenate_unary_strings(struct bt_list_head *head)
538 struct ctf_node *node;
540 str = g_string_new(NULL);
543 bt_list_for_each_entry (node, head, siblings) {
546 if (node->type != NODE_UNARY_EXPRESSION || node->u.unary_expression.type != UNARY_STRING ||
547 !((node->u.unary_expression.link != UNARY_LINK_UNKNOWN) ^ (i == 0))) {
551 switch (node->u.unary_expression.link) {
553 g_string_append(str, ".");
555 case UNARY_ARROWLINK:
556 g_string_append(str, "->");
558 case UNARY_DOTDOTDOT:
559 g_string_append(str, "...");
565 src_string = node->u.unary_expression.u.string;
566 g_string_append(str, src_string);
570 /* Destroys the container, returns the underlying string */
571 return g_string_free(str, FALSE);
574 /* This always returns NULL */
575 return g_string_free(str, TRUE);
578 #ifndef BT_COMP_LOG_CUR_LVL
579 # define BT_AST_LOG_LEVEL_UNUSED_ATTR __attribute__((unused))
581 # define BT_AST_LOG_LEVEL_UNUSED_ATTR
584 static inline int ctf_ast_get_unary_uuid(struct bt_list_head *head, bt_uuid_t uuid,
585 const bt2c::Logger& logger)
589 struct ctf_node *node;
591 bt_list_for_each_entry (node, head, siblings) {
592 int uexpr_type = node->u.unary_expression.type;
593 int uexpr_link = node->u.unary_expression.link;
594 const char *src_string;
596 if (node->type != NODE_UNARY_EXPRESSION || uexpr_type != UNARY_STRING ||
597 uexpr_link != UNARY_LINK_UNKNOWN || i != 0) {
602 src_string = node->u.unary_expression.u.string;
603 ret = bt_uuid_from_str(src_string, uuid);
605 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot parse UUID: uuid=\"{}\"", src_string);
614 #endif /* _CTF_AST_H */