490e4d29b61ef616fa39b123c933eb77e6e980c7
[babeltrace.git] / src / plugins / ctf / common / metadata / ast.hpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
7 #ifndef _CTF_AST_H
8 #define _CTF_AST_H
9
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <glib.h>
13 #include "common/list.h"
14 #include <babeltrace2/babeltrace.h>
15 #include "common/macros.h"
16 #include "common/assert.h"
17
18 #include "decoder.hpp"
19 #include "ctf-meta.hpp"
20
21 // the parameter name (of the reentrant 'yyparse' function)
22 // data is a pointer to a 'SParserParam' structure
23 //#define YYPARSE_PARAM scanner
24
25 struct ctf_node;
26 struct ctf_parser;
27 struct ctf_visitor_generate_ir;
28
29 #define EINCOMPLETE 1000
30
31 #define FOREACH_CTF_NODES(F) \
32 F(NODE_UNKNOWN) \
33 F(NODE_ROOT) \
34 F(NODE_ERROR) \
35 F(NODE_EVENT) \
36 F(NODE_STREAM) \
37 F(NODE_ENV) \
38 F(NODE_TRACE) \
39 F(NODE_CLOCK) \
40 F(NODE_CALLSITE) \
41 F(NODE_CTF_EXPRESSION) \
42 F(NODE_UNARY_EXPRESSION) \
43 F(NODE_TYPEDEF) \
44 F(NODE_TYPEALIAS_TARGET) \
45 F(NODE_TYPEALIAS_ALIAS) \
46 F(NODE_TYPEALIAS) \
47 F(NODE_TYPE_SPECIFIER) \
48 F(NODE_TYPE_SPECIFIER_LIST) \
49 F(NODE_POINTER) \
50 F(NODE_TYPE_DECLARATOR) \
51 F(NODE_FLOATING_POINT) \
52 F(NODE_INTEGER) \
53 F(NODE_STRING) \
54 F(NODE_ENUMERATOR) \
55 F(NODE_ENUM) \
56 F(NODE_STRUCT_OR_VARIANT_DECLARATION) \
57 F(NODE_VARIANT) \
58 F(NODE_STRUCT)
59
60 enum node_type {
61 #define ENTRY(S) S,
62 FOREACH_CTF_NODES(ENTRY)
63 #undef ENTRY
64 };
65
66 enum ctf_unary {
67 UNARY_UNKNOWN = 0,
68 UNARY_STRING,
69 UNARY_SIGNED_CONSTANT,
70 UNARY_UNSIGNED_CONSTANT,
71 UNARY_SBRAC,
72 };
73
74 enum ctf_unary_link {
75 UNARY_LINK_UNKNOWN = 0,
76 UNARY_DOTLINK,
77 UNARY_ARROWLINK,
78 UNARY_DOTDOTDOT,
79 };
80
81 enum ctf_typedec {
82 TYPEDEC_UNKNOWN = 0,
83 TYPEDEC_ID, /* identifier */
84 TYPEDEC_NESTED, /* (), array or sequence */
85 };
86
87 enum ctf_typespec {
88 TYPESPEC_UNKNOWN = 0,
89 TYPESPEC_VOID,
90 TYPESPEC_CHAR,
91 TYPESPEC_SHORT,
92 TYPESPEC_INT,
93 TYPESPEC_LONG,
94 TYPESPEC_FLOAT,
95 TYPESPEC_DOUBLE,
96 TYPESPEC_SIGNED,
97 TYPESPEC_UNSIGNED,
98 TYPESPEC_BOOL,
99 TYPESPEC_COMPLEX,
100 TYPESPEC_IMAGINARY,
101 TYPESPEC_CONST,
102 TYPESPEC_ID_TYPE,
103 TYPESPEC_FLOATING_POINT,
104 TYPESPEC_INTEGER,
105 TYPESPEC_STRING,
106 TYPESPEC_STRUCT,
107 TYPESPEC_VARIANT,
108 TYPESPEC_ENUM,
109 };
110
111 struct ctf_node {
112 /*
113 * Parent node is only set on demand by specific visitor.
114 */
115 struct ctf_node *parent;
116 struct bt_list_head siblings;
117 struct bt_list_head tmp_head;
118 unsigned int lineno;
119 /*
120 * We mark nodes visited in the generate-ir phase (last
121 * phase). We only mark the 1-depth level nodes as visited
122 * (never the root node, and not their sub-nodes). This allows
123 * skipping already visited nodes when doing incremental
124 * metadata append.
125 */
126 int visited;
127
128 enum node_type type;
129 union {
130 struct {
131 } unknown;
132 struct {
133 /*
134 * Children nodes are ctf_expression, field_class_def,
135 * field_class_alias and field_class_specifier_list.
136 */
137 struct bt_list_head declaration_list;
138 struct bt_list_head trace;
139 struct bt_list_head env;
140 struct bt_list_head stream;
141 struct bt_list_head event;
142 struct bt_list_head clock;
143 struct bt_list_head callsite;
144 } root;
145 struct {
146 /*
147 * Children nodes are ctf_expression, field_class_def,
148 * field_class_alias and field_class_specifier_list.
149 */
150 struct bt_list_head declaration_list;
151 } event;
152 struct {
153 /*
154 * Children nodes are ctf_expression, field_class_def,
155 * field_class_alias and field_class_specifier_list.
156 */
157 struct bt_list_head declaration_list;
158 } stream;
159 struct {
160 /*
161 * Children nodes are ctf_expression, field_class_def,
162 * field_class_alias and field_class_specifier_list.
163 */
164 struct bt_list_head declaration_list;
165 } env;
166 struct {
167 /*
168 * Children nodes are ctf_expression, field_class_def,
169 * field_class_alias and field_class_specifier_list.
170 */
171 struct bt_list_head declaration_list;
172 } trace;
173 struct {
174 /*
175 * Children nodes are ctf_expression, field_class_def,
176 * field_class_alias and field_class_specifier_list.
177 */
178 struct bt_list_head declaration_list;
179 } clock;
180 struct {
181 /*
182 * Children nodes are ctf_expression, field_class_def,
183 * field_class_alias and field_class_specifier_list.
184 */
185 struct bt_list_head declaration_list;
186 } callsite;
187 struct {
188 struct bt_list_head left; /* Should be string */
189 struct bt_list_head right; /* Unary exp. or type */
190 } ctf_expression;
191 struct {
192 ctf_unary type;
193 union {
194 /*
195 * string for identifier, id_type, keywords,
196 * string literals and character constants.
197 */
198 char *string;
199 int64_t signed_constant;
200 uint64_t unsigned_constant;
201 struct ctf_node *sbrac_exp;
202 } u;
203 ctf_unary_link link;
204 } unary_expression;
205 struct {
206 struct ctf_node *field_class_specifier_list;
207 struct bt_list_head field_class_declarators;
208 } field_class_def;
209 /* new type is "alias", existing type "target" */
210 struct {
211 struct ctf_node *field_class_specifier_list;
212 struct bt_list_head field_class_declarators;
213 } field_class_alias_target;
214 struct {
215 struct ctf_node *field_class_specifier_list;
216 struct bt_list_head field_class_declarators;
217 } field_class_alias_name;
218 struct {
219 struct ctf_node *target;
220 struct ctf_node *alias;
221 } field_class_alias;
222 struct {
223 ctf_typespec type;
224 /* For struct, variant and enum */
225 struct ctf_node *node;
226 const char *id_type;
227 } field_class_specifier;
228 struct {
229 /* list of field_class_specifier */
230 struct bt_list_head head;
231 } field_class_specifier_list;
232 struct {
233 unsigned int const_qualifier;
234 } pointer;
235 struct {
236 struct bt_list_head pointers;
237 ctf_typedec type;
238 union {
239 char *id;
240 struct {
241 /* typedec has no pointer list */
242 struct ctf_node *field_class_declarator;
243 /*
244 * unary expression (value) or
245 * field_class_specifier_list.
246 */
247 struct bt_list_head length;
248 /* for abstract type declarator */
249 unsigned int abstract_array;
250 } nested;
251 } u;
252 struct ctf_node *bitfield_len;
253 } field_class_declarator;
254 struct {
255 /* Children nodes are ctf_expression. */
256 struct bt_list_head expressions;
257 } floating_point;
258 struct {
259 /* Children nodes are ctf_expression. */
260 struct bt_list_head expressions;
261 } integer;
262 struct {
263 /* Children nodes are ctf_expression. */
264 struct bt_list_head expressions;
265 } string;
266 struct {
267 char *id;
268 /*
269 * Range list or single value node. Contains unary
270 * expressions.
271 */
272 struct bt_list_head values;
273 } enumerator;
274 struct {
275 char *enum_id;
276 /*
277 * Either NULL, or points to unary expression or
278 * field_class_specifier_list.
279 */
280 struct ctf_node *container_field_class;
281 struct bt_list_head enumerator_list;
282 int has_body;
283 } _enum;
284 struct {
285 struct ctf_node *field_class_specifier_list;
286 struct bt_list_head field_class_declarators;
287 } struct_or_variant_declaration;
288 struct {
289 char *name;
290 char *choice;
291 /*
292 * list of field_class_def, field_class_alias and
293 * declarations
294 */
295 struct bt_list_head declaration_list;
296 int has_body;
297 } variant;
298 struct {
299 char *name;
300 /*
301 * list of field_class_def, field_class_alias and
302 * declarations
303 */
304 struct bt_list_head declaration_list;
305 int has_body;
306 struct bt_list_head min_align; /* align() attribute */
307 } _struct;
308 } u;
309 };
310
311 struct ctf_ast {
312 struct ctf_node root;
313 };
314
315 const char *node_type(struct ctf_node *node);
316
317 struct meta_log_config;
318
319 BT_HIDDEN
320 struct ctf_visitor_generate_ir *ctf_visitor_generate_ir_create(
321 const struct ctf_metadata_decoder_config *config);
322
323 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir *visitor);
324
325 BT_HIDDEN
326 bt_trace_class *ctf_visitor_generate_ir_get_ir_trace_class(
327 struct ctf_visitor_generate_ir *visitor);
328
329 BT_HIDDEN
330 struct ctf_trace_class *ctf_visitor_generate_ir_borrow_ctf_trace_class(
331 struct ctf_visitor_generate_ir *visitor);
332
333 BT_HIDDEN
334 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor,
335 struct ctf_node *node);
336
337 BT_HIDDEN
338 int ctf_visitor_semantic_check(int depth, struct ctf_node *node,
339 struct meta_log_config *log_cfg);
340
341 BT_HIDDEN
342 int ctf_visitor_parent_links(int depth, struct ctf_node *node,
343 struct meta_log_config *log_cfg);
344
345 static inline
346 char *ctf_ast_concatenate_unary_strings(struct bt_list_head *head)
347 {
348 int i = 0;
349 GString *str;
350 struct ctf_node *node;
351
352 str = g_string_new(NULL);
353 BT_ASSERT(str);
354
355 bt_list_for_each_entry(node, head, siblings) {
356 char *src_string;
357
358 if (
359 node->type != NODE_UNARY_EXPRESSION ||
360 node->u.unary_expression.type != UNARY_STRING ||
361 !(
362 (
363 node->u.unary_expression.link !=
364 UNARY_LINK_UNKNOWN
365 ) ^ (i == 0)
366 )
367 ) {
368 goto error;
369 }
370
371 switch (node->u.unary_expression.link) {
372 case UNARY_DOTLINK:
373 g_string_append(str, ".");
374 break;
375 case UNARY_ARROWLINK:
376 g_string_append(str, "->");
377 break;
378 case UNARY_DOTDOTDOT:
379 g_string_append(str, "...");
380 break;
381 default:
382 break;
383 }
384
385 src_string = node->u.unary_expression.u.string;
386 g_string_append(str, src_string);
387 i++;
388 }
389
390 /* Destroys the container, returns the underlying string */
391 return g_string_free(str, FALSE);
392
393 error:
394 /* This always returns NULL */
395 return g_string_free(str, TRUE);
396 }
397
398 static inline
399 int ctf_ast_get_unary_uuid(struct bt_list_head *head,
400 bt_uuid_t uuid, int log_level, bt_self_component *self_comp)
401 {
402 int i = 0;
403 int ret = 0;
404 struct ctf_node *node;
405
406 bt_list_for_each_entry(node, head, siblings) {
407 int uexpr_type = node->u.unary_expression.type;
408 int uexpr_link = node->u.unary_expression.link;
409 const char *src_string;
410
411 if (node->type != NODE_UNARY_EXPRESSION ||
412 uexpr_type != UNARY_STRING ||
413 uexpr_link != UNARY_LINK_UNKNOWN ||
414 i != 0) {
415 ret = -EINVAL;
416 goto end;
417 }
418
419 src_string = node->u.unary_expression.u.string;
420 ret = bt_uuid_from_str(src_string, uuid);
421 if (ret) {
422 #ifdef BT_COMP_LOG_CUR_LVL
423 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level,
424 self_comp,
425 "Cannot parse UUID: uuid=\"%s\"", src_string);
426 #endif
427 goto end;
428 }
429 }
430
431 end:
432 return ret;
433 }
434
435 #endif /* _CTF_AST_H */
This page took 0.036771 seconds and 3 git commands to generate.