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