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"
19 #include "ctf-meta.hpp"
20 #include "decoder.hpp"
22 // the parameter name (of the reentrant 'yyparse' function)
23 // data is a pointer to a 'SParserParam' structure
24 //#define YYPARSE_PARAM scanner
28 struct ctf_visitor_generate_ir;
30 #define EINCOMPLETE 1000
32 #define FOREACH_CTF_NODES(F) \
42 F(NODE_CTF_EXPRESSION) \
43 F(NODE_UNARY_EXPRESSION) \
45 F(NODE_TYPEALIAS_TARGET) \
46 F(NODE_TYPEALIAS_ALIAS) \
48 F(NODE_TYPE_SPECIFIER) \
49 F(NODE_TYPE_SPECIFIER_LIST) \
51 F(NODE_TYPE_DECLARATOR) \
52 F(NODE_FLOATING_POINT) \
57 F(NODE_STRUCT_OR_VARIANT_DECLARATION) \
64 FOREACH_CTF_NODES(ENTRY)
72 UNARY_SIGNED_CONSTANT,
73 UNARY_UNSIGNED_CONSTANT,
79 UNARY_LINK_UNKNOWN = 0,
88 TYPEDEC_ID, /* identifier */
89 TYPEDEC_NESTED, /* (), array or sequence */
109 TYPESPEC_FLOATING_POINT,
120 * Parent node is only set on demand by specific visitor.
122 struct ctf_node *parent;
123 struct bt_list_head siblings;
124 struct bt_list_head tmp_head;
127 * We mark nodes visited in the generate-ir phase (last
128 * phase). We only mark the 1-depth level nodes as visited
129 * (never the root node, and not their sub-nodes). This allows
130 * skipping already visited nodes when doing incremental
144 * Children nodes are ctf_expression, field_class_def,
145 * field_class_alias and field_class_specifier_list.
147 struct bt_list_head declaration_list;
148 struct bt_list_head trace;
149 struct bt_list_head env;
150 struct bt_list_head stream;
151 struct bt_list_head event;
152 struct bt_list_head clock;
153 struct bt_list_head callsite;
158 * Children nodes are ctf_expression, field_class_def,
159 * field_class_alias and field_class_specifier_list.
161 struct bt_list_head declaration_list;
166 * Children nodes are ctf_expression, field_class_def,
167 * field_class_alias and field_class_specifier_list.
169 struct bt_list_head declaration_list;
174 * Children nodes are ctf_expression, field_class_def,
175 * field_class_alias and field_class_specifier_list.
177 struct bt_list_head declaration_list;
182 * Children nodes are ctf_expression, field_class_def,
183 * field_class_alias and field_class_specifier_list.
185 struct bt_list_head declaration_list;
190 * Children nodes are ctf_expression, field_class_def,
191 * field_class_alias and field_class_specifier_list.
193 struct bt_list_head declaration_list;
198 * Children nodes are ctf_expression, field_class_def,
199 * field_class_alias and field_class_specifier_list.
201 struct bt_list_head declaration_list;
205 struct bt_list_head left; /* Should be string */
206 struct bt_list_head right; /* Unary exp. or type */
214 * string for identifier, id_type, keywords,
215 * string literals and character constants.
218 int64_t signed_constant;
219 uint64_t unsigned_constant;
220 struct ctf_node *sbrac_exp;
226 struct ctf_node *field_class_specifier_list;
227 struct bt_list_head field_class_declarators;
229 /* new type is "alias", existing type "target" */
232 struct ctf_node *field_class_specifier_list;
233 struct bt_list_head field_class_declarators;
234 } field_class_alias_target;
237 struct ctf_node *field_class_specifier_list;
238 struct bt_list_head field_class_declarators;
239 } field_class_alias_name;
242 struct ctf_node *target;
243 struct ctf_node *alias;
248 /* For struct, variant and enum */
249 struct ctf_node *node;
251 } field_class_specifier;
254 /* list of field_class_specifier */
255 struct bt_list_head head;
256 } field_class_specifier_list;
259 unsigned int const_qualifier;
263 struct bt_list_head pointers;
270 /* typedec has no pointer list */
271 struct ctf_node *field_class_declarator;
273 * unary expression (value) or
274 * field_class_specifier_list.
276 struct bt_list_head length;
277 /* for abstract type declarator */
278 unsigned int abstract_array;
281 struct ctf_node *bitfield_len;
282 } field_class_declarator;
285 /* Children nodes are ctf_expression. */
286 struct bt_list_head expressions;
290 /* Children nodes are ctf_expression. */
291 struct bt_list_head expressions;
295 /* Children nodes are ctf_expression. */
296 struct bt_list_head expressions;
302 * Range list or single value node. Contains unary
305 struct bt_list_head values;
311 * Either NULL, or points to unary expression or
312 * field_class_specifier_list.
314 struct ctf_node *container_field_class;
315 struct bt_list_head enumerator_list;
320 struct ctf_node *field_class_specifier_list;
321 struct bt_list_head field_class_declarators;
322 } struct_or_variant_declaration;
328 * list of field_class_def, field_class_alias and
331 struct bt_list_head declaration_list;
338 * list of field_class_def, field_class_alias and
341 struct bt_list_head declaration_list;
343 struct bt_list_head min_align; /* align() attribute */
350 struct ctf_node root;
353 const char *node_type(struct ctf_node *node);
355 struct meta_log_config;
357 struct ctf_visitor_generate_ir *
358 ctf_visitor_generate_ir_create(const struct ctf_metadata_decoder_config *config);
360 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir *visitor);
362 bt_trace_class *ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_generate_ir *visitor);
364 struct ctf_trace_class *
365 ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir *visitor);
367 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor,
368 struct ctf_node *node);
370 int ctf_visitor_semantic_check(int depth, struct ctf_node *node, struct meta_log_config *log_cfg);
372 int ctf_visitor_parent_links(int depth, struct ctf_node *node, struct meta_log_config *log_cfg);
374 static inline char *ctf_ast_concatenate_unary_strings(struct bt_list_head *head)
378 struct ctf_node *node;
380 str = g_string_new(NULL);
383 bt_list_for_each_entry (node, head, siblings) {
386 if (node->type != NODE_UNARY_EXPRESSION || node->u.unary_expression.type != UNARY_STRING ||
387 !((node->u.unary_expression.link != UNARY_LINK_UNKNOWN) ^ (i == 0))) {
391 switch (node->u.unary_expression.link) {
393 g_string_append(str, ".");
395 case UNARY_ARROWLINK:
396 g_string_append(str, "->");
398 case UNARY_DOTDOTDOT:
399 g_string_append(str, "...");
405 src_string = node->u.unary_expression.u.string;
406 g_string_append(str, src_string);
410 /* Destroys the container, returns the underlying string */
411 return g_string_free(str, FALSE);
414 /* This always returns NULL */
415 return g_string_free(str, TRUE);
418 #ifndef BT_COMP_LOG_CUR_LVL
419 # define BT_AST_LOG_LEVEL_UNUSED_ATTR __attribute__((unused))
421 # define BT_AST_LOG_LEVEL_UNUSED_ATTR
424 static inline int ctf_ast_get_unary_uuid(struct bt_list_head *head, bt_uuid_t uuid,
425 int log_level BT_AST_LOG_LEVEL_UNUSED_ATTR,
426 bt_self_component *self_comp BT_AST_LOG_LEVEL_UNUSED_ATTR)
430 struct ctf_node *node;
432 bt_list_for_each_entry (node, head, siblings) {
433 int uexpr_type = node->u.unary_expression.type;
434 int uexpr_link = node->u.unary_expression.link;
435 const char *src_string;
437 if (node->type != NODE_UNARY_EXPRESSION || uexpr_type != UNARY_STRING ||
438 uexpr_link != UNARY_LINK_UNKNOWN || i != 0) {
443 src_string = node->u.unary_expression.u.string;
444 ret = bt_uuid_from_str(src_string, uuid);
446 #ifdef BT_COMP_LOG_CUR_LVL
447 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
448 "Cannot parse UUID: uuid=\"%s\"", src_string);
458 #endif /* _CTF_AST_H */