2 * SPDX-License-Identifier: MIT
4 * Copyright 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 #include "common/list.h"
14 #include <babeltrace2/babeltrace.h>
15 #include "common/macros.h"
16 #include "common/assert.h"
18 #include "decoder.hpp"
19 #include "ctf-meta.hpp"
21 // the parameter name (of the reentrant 'yyparse' function)
22 // data is a pointer to a 'SParserParam' structure
23 //#define YYPARSE_PARAM scanner
27 struct ctf_visitor_generate_ir;
29 #define EINCOMPLETE 1000
31 #define FOREACH_CTF_NODES(F) \
41 F(NODE_CTF_EXPRESSION) \
42 F(NODE_UNARY_EXPRESSION) \
44 F(NODE_TYPEALIAS_TARGET) \
45 F(NODE_TYPEALIAS_ALIAS) \
47 F(NODE_TYPE_SPECIFIER) \
48 F(NODE_TYPE_SPECIFIER_LIST) \
50 F(NODE_TYPE_DECLARATOR) \
51 F(NODE_FLOATING_POINT) \
56 F(NODE_STRUCT_OR_VARIANT_DECLARATION) \
63 FOREACH_CTF_NODES(ENTRY)
71 UNARY_SIGNED_CONSTANT,
72 UNARY_UNSIGNED_CONSTANT,
78 UNARY_LINK_UNKNOWN = 0,
87 TYPEDEC_ID, /* identifier */
88 TYPEDEC_NESTED, /* (), array or sequence */
108 TYPESPEC_FLOATING_POINT,
119 * Parent node is only set on demand by specific visitor.
121 struct ctf_node *parent;
122 struct bt_list_head siblings;
123 struct bt_list_head tmp_head;
126 * We mark nodes visited in the generate-ir phase (last
127 * phase). We only mark the 1-depth level nodes as visited
128 * (never the root node, and not their sub-nodes). This allows
129 * skipping already visited nodes when doing incremental
143 * Children nodes are ctf_expression, field_class_def,
144 * field_class_alias and field_class_specifier_list.
146 struct bt_list_head declaration_list;
147 struct bt_list_head trace;
148 struct bt_list_head env;
149 struct bt_list_head stream;
150 struct bt_list_head event;
151 struct bt_list_head clock;
152 struct bt_list_head callsite;
157 * Children nodes are ctf_expression, field_class_def,
158 * field_class_alias and field_class_specifier_list.
160 struct bt_list_head declaration_list;
165 * Children nodes are ctf_expression, field_class_def,
166 * field_class_alias and field_class_specifier_list.
168 struct bt_list_head declaration_list;
173 * Children nodes are ctf_expression, field_class_def,
174 * field_class_alias and field_class_specifier_list.
176 struct bt_list_head declaration_list;
181 * Children nodes are ctf_expression, field_class_def,
182 * field_class_alias and field_class_specifier_list.
184 struct bt_list_head declaration_list;
189 * Children nodes are ctf_expression, field_class_def,
190 * field_class_alias and field_class_specifier_list.
192 struct bt_list_head declaration_list;
197 * Children nodes are ctf_expression, field_class_def,
198 * field_class_alias and field_class_specifier_list.
200 struct bt_list_head declaration_list;
204 struct bt_list_head left; /* Should be string */
205 struct bt_list_head right; /* Unary exp. or type */
213 * string for identifier, id_type, keywords,
214 * string literals and character constants.
217 int64_t signed_constant;
218 uint64_t unsigned_constant;
219 struct ctf_node *sbrac_exp;
225 struct ctf_node *field_class_specifier_list;
226 struct bt_list_head field_class_declarators;
228 /* new type is "alias", existing type "target" */
231 struct ctf_node *field_class_specifier_list;
232 struct bt_list_head field_class_declarators;
233 } field_class_alias_target;
236 struct ctf_node *field_class_specifier_list;
237 struct bt_list_head field_class_declarators;
238 } field_class_alias_name;
241 struct ctf_node *target;
242 struct ctf_node *alias;
247 /* For struct, variant and enum */
248 struct ctf_node *node;
250 } field_class_specifier;
253 /* list of field_class_specifier */
254 struct bt_list_head head;
255 } field_class_specifier_list;
258 unsigned int const_qualifier;
262 struct bt_list_head pointers;
269 /* typedec has no pointer list */
270 struct ctf_node *field_class_declarator;
272 * unary expression (value) or
273 * field_class_specifier_list.
275 struct bt_list_head length;
276 /* for abstract type declarator */
277 unsigned int abstract_array;
280 struct ctf_node *bitfield_len;
281 } field_class_declarator;
284 /* Children nodes are ctf_expression. */
285 struct bt_list_head expressions;
289 /* Children nodes are ctf_expression. */
290 struct bt_list_head expressions;
294 /* Children nodes are ctf_expression. */
295 struct bt_list_head expressions;
301 * Range list or single value node. Contains unary
304 struct bt_list_head values;
310 * Either NULL, or points to unary expression or
311 * field_class_specifier_list.
313 struct ctf_node *container_field_class;
314 struct bt_list_head enumerator_list;
319 struct ctf_node *field_class_specifier_list;
320 struct bt_list_head field_class_declarators;
321 } struct_or_variant_declaration;
327 * list of field_class_def, field_class_alias and
330 struct bt_list_head declaration_list;
337 * list of field_class_def, field_class_alias and
340 struct bt_list_head declaration_list;
342 struct bt_list_head min_align; /* align() attribute */
349 struct ctf_node root;
352 const char *node_type(struct ctf_node *node);
354 struct meta_log_config;
356 struct ctf_visitor_generate_ir *
357 ctf_visitor_generate_ir_create(const struct ctf_metadata_decoder_config *config);
359 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir *visitor);
361 bt_trace_class *ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_generate_ir *visitor);
363 struct ctf_trace_class *
364 ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir *visitor);
366 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor,
367 struct ctf_node *node);
369 int ctf_visitor_semantic_check(int depth, struct ctf_node *node, struct meta_log_config *log_cfg);
371 int ctf_visitor_parent_links(int depth, struct ctf_node *node, struct meta_log_config *log_cfg);
373 static inline char *ctf_ast_concatenate_unary_strings(struct bt_list_head *head)
377 struct ctf_node *node;
379 str = g_string_new(NULL);
382 bt_list_for_each_entry (node, head, siblings) {
385 if (node->type != NODE_UNARY_EXPRESSION || node->u.unary_expression.type != UNARY_STRING ||
386 !((node->u.unary_expression.link != UNARY_LINK_UNKNOWN) ^ (i == 0))) {
390 switch (node->u.unary_expression.link) {
392 g_string_append(str, ".");
394 case UNARY_ARROWLINK:
395 g_string_append(str, "->");
397 case UNARY_DOTDOTDOT:
398 g_string_append(str, "...");
404 src_string = node->u.unary_expression.u.string;
405 g_string_append(str, src_string);
409 /* Destroys the container, returns the underlying string */
410 return g_string_free(str, FALSE);
413 /* This always returns NULL */
414 return g_string_free(str, TRUE);
417 #ifndef BT_COMP_LOG_CUR_LVL
418 #define BT_AST_LOG_LEVEL_UNUSED_ATTR __attribute__((unused))
420 #define BT_AST_LOG_LEVEL_UNUSED_ATTR
423 static inline int ctf_ast_get_unary_uuid(struct bt_list_head *head, bt_uuid_t uuid,
424 int log_level BT_AST_LOG_LEVEL_UNUSED_ATTR,
425 bt_self_component *self_comp BT_AST_LOG_LEVEL_UNUSED_ATTR)
429 struct ctf_node *node;
431 bt_list_for_each_entry (node, head, siblings) {
432 int uexpr_type = node->u.unary_expression.type;
433 int uexpr_link = node->u.unary_expression.link;
434 const char *src_string;
436 if (node->type != NODE_UNARY_EXPRESSION || uexpr_type != UNARY_STRING ||
437 uexpr_link != UNARY_LINK_UNKNOWN || i != 0) {
442 src_string = node->u.unary_expression.u.string;
443 ret = bt_uuid_from_str(src_string, uuid);
445 #ifdef BT_COMP_LOG_CUR_LVL
446 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
447 "Cannot parse UUID: uuid=\"%s\"", src_string);
457 #endif /* _CTF_AST_H */