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 "common/macros.h"
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)
73 UNARY_SIGNED_CONSTANT,
74 UNARY_UNSIGNED_CONSTANT,
80 UNARY_LINK_UNKNOWN = 0,
89 TYPEDEC_ID, /* identifier */
90 TYPEDEC_NESTED, /* (), array or sequence */
110 TYPESPEC_FLOATING_POINT,
121 * Parent node is only set on demand by specific visitor.
123 struct ctf_node *parent;
124 struct bt_list_head siblings;
125 struct bt_list_head tmp_head;
128 * We mark nodes visited in the generate-ir phase (last
129 * phase). We only mark the 1-depth level nodes as visited
130 * (never the root node, and not their sub-nodes). This allows
131 * skipping already visited nodes when doing incremental
145 * Children nodes are ctf_expression, field_class_def,
146 * field_class_alias and field_class_specifier_list.
148 struct bt_list_head declaration_list;
149 struct bt_list_head trace;
150 struct bt_list_head env;
151 struct bt_list_head stream;
152 struct bt_list_head event;
153 struct bt_list_head clock;
154 struct bt_list_head callsite;
159 * Children nodes are ctf_expression, field_class_def,
160 * field_class_alias and field_class_specifier_list.
162 struct bt_list_head declaration_list;
167 * Children nodes are ctf_expression, field_class_def,
168 * field_class_alias and field_class_specifier_list.
170 struct bt_list_head declaration_list;
175 * Children nodes are ctf_expression, field_class_def,
176 * field_class_alias and field_class_specifier_list.
178 struct bt_list_head declaration_list;
183 * Children nodes are ctf_expression, field_class_def,
184 * field_class_alias and field_class_specifier_list.
186 struct bt_list_head declaration_list;
191 * Children nodes are ctf_expression, field_class_def,
192 * field_class_alias and field_class_specifier_list.
194 struct bt_list_head declaration_list;
199 * Children nodes are ctf_expression, field_class_def,
200 * field_class_alias and field_class_specifier_list.
202 struct bt_list_head declaration_list;
206 struct bt_list_head left; /* Should be string */
207 struct bt_list_head right; /* Unary exp. or type */
215 * string for identifier, id_type, keywords,
216 * string literals and character constants.
219 int64_t signed_constant;
220 uint64_t unsigned_constant;
221 struct ctf_node *sbrac_exp;
227 struct ctf_node *field_class_specifier_list;
228 struct bt_list_head field_class_declarators;
230 /* new type is "alias", existing type "target" */
233 struct ctf_node *field_class_specifier_list;
234 struct bt_list_head field_class_declarators;
235 } field_class_alias_target;
238 struct ctf_node *field_class_specifier_list;
239 struct bt_list_head field_class_declarators;
240 } field_class_alias_name;
243 struct ctf_node *target;
244 struct ctf_node *alias;
249 /* For struct, variant and enum */
250 struct ctf_node *node;
252 } field_class_specifier;
255 /* list of field_class_specifier */
256 struct bt_list_head head;
257 } field_class_specifier_list;
260 unsigned int const_qualifier;
264 struct bt_list_head pointers;
271 /* typedec has no pointer list */
272 struct ctf_node *field_class_declarator;
274 * unary expression (value) or
275 * field_class_specifier_list.
277 struct bt_list_head length;
278 /* for abstract type declarator */
279 unsigned int abstract_array;
282 struct ctf_node *bitfield_len;
283 } field_class_declarator;
286 /* Children nodes are ctf_expression. */
287 struct bt_list_head expressions;
291 /* Children nodes are ctf_expression. */
292 struct bt_list_head expressions;
296 /* Children nodes are ctf_expression. */
297 struct bt_list_head expressions;
303 * Range list or single value node. Contains unary
306 struct bt_list_head values;
312 * Either NULL, or points to unary expression or
313 * field_class_specifier_list.
315 struct ctf_node *container_field_class;
316 struct bt_list_head enumerator_list;
321 struct ctf_node *field_class_specifier_list;
322 struct bt_list_head field_class_declarators;
323 } struct_or_variant_declaration;
329 * list of field_class_def, field_class_alias and
332 struct bt_list_head declaration_list;
339 * list of field_class_def, field_class_alias and
342 struct bt_list_head declaration_list;
344 struct bt_list_head min_align; /* align() attribute */
351 struct ctf_node root;
354 const char *node_type(struct ctf_node *node);
356 struct meta_log_config;
358 struct ctf_visitor_generate_ir *
359 ctf_visitor_generate_ir_create(const struct ctf_metadata_decoder_config *config);
361 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir *visitor);
363 bt_trace_class *ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_generate_ir *visitor);
365 struct ctf_trace_class *
366 ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir *visitor);
368 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor,
369 struct ctf_node *node);
371 int ctf_visitor_semantic_check(int depth, struct ctf_node *node, struct meta_log_config *log_cfg);
373 int ctf_visitor_parent_links(int depth, struct ctf_node *node, struct meta_log_config *log_cfg);
375 static inline char *ctf_ast_concatenate_unary_strings(struct bt_list_head *head)
379 struct ctf_node *node;
381 str = g_string_new(NULL);
384 bt_list_for_each_entry (node, head, siblings) {
387 if (node->type != NODE_UNARY_EXPRESSION || node->u.unary_expression.type != UNARY_STRING ||
388 !((node->u.unary_expression.link != UNARY_LINK_UNKNOWN) ^ (i == 0))) {
392 switch (node->u.unary_expression.link) {
394 g_string_append(str, ".");
396 case UNARY_ARROWLINK:
397 g_string_append(str, "->");
399 case UNARY_DOTDOTDOT:
400 g_string_append(str, "...");
406 src_string = node->u.unary_expression.u.string;
407 g_string_append(str, src_string);
411 /* Destroys the container, returns the underlying string */
412 return g_string_free(str, FALSE);
415 /* This always returns NULL */
416 return g_string_free(str, TRUE);
419 #ifndef BT_COMP_LOG_CUR_LVL
420 # define BT_AST_LOG_LEVEL_UNUSED_ATTR __attribute__((unused))
422 # define BT_AST_LOG_LEVEL_UNUSED_ATTR
425 static inline int ctf_ast_get_unary_uuid(struct bt_list_head *head, bt_uuid_t uuid,
426 int log_level BT_AST_LOG_LEVEL_UNUSED_ATTR,
427 bt_self_component *self_comp BT_AST_LOG_LEVEL_UNUSED_ATTR)
431 struct ctf_node *node;
433 bt_list_for_each_entry (node, head, siblings) {
434 int uexpr_type = node->u.unary_expression.type;
435 int uexpr_link = node->u.unary_expression.link;
436 const char *src_string;
438 if (node->type != NODE_UNARY_EXPRESSION || uexpr_type != UNARY_STRING ||
439 uexpr_link != UNARY_LINK_UNKNOWN || i != 0) {
444 src_string = node->u.unary_expression.u.string;
445 ret = bt_uuid_from_str(src_string, uuid);
447 #ifdef BT_COMP_LOG_CUR_LVL
448 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
449 "Cannot parse UUID: uuid=\"%s\"", src_string);
459 #endif /* _CTF_AST_H */