Commit | Line | Data |
---|---|---|
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 | |
087cd0f5 SM |
18 | #include "decoder.hpp" |
19 | #include "ctf-meta.hpp" | |
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 | ||
25 | struct ctf_node; | |
26 | struct ctf_parser; | |
1e649dff PP |
27 | struct ctf_visitor_generate_ir; |
28 | ||
4164020e SM |
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 | { | |
62 | #define ENTRY(S) S, | |
63 | FOREACH_CTF_NODES(ENTRY) | |
e98a2d6e | 64 | #undef ENTRY |
087cd0f5 SM |
65 | }; |
66 | ||
4164020e SM |
67 | enum ctf_unary |
68 | { | |
69 | UNARY_UNKNOWN = 0, | |
70 | UNARY_STRING, | |
71 | UNARY_SIGNED_CONSTANT, | |
72 | UNARY_UNSIGNED_CONSTANT, | |
73 | UNARY_SBRAC, | |
087cd0f5 SM |
74 | }; |
75 | ||
4164020e SM |
76 | enum ctf_unary_link |
77 | { | |
78 | UNARY_LINK_UNKNOWN = 0, | |
79 | UNARY_DOTLINK, | |
80 | UNARY_ARROWLINK, | |
81 | UNARY_DOTDOTDOT, | |
087cd0f5 SM |
82 | }; |
83 | ||
4164020e SM |
84 | enum ctf_typedec |
85 | { | |
86 | TYPEDEC_UNKNOWN = 0, | |
87 | TYPEDEC_ID, /* identifier */ | |
88 | TYPEDEC_NESTED, /* (), array or sequence */ | |
087cd0f5 SM |
89 | }; |
90 | ||
4164020e SM |
91 | enum ctf_typespec |
92 | { | |
93 | TYPESPEC_UNKNOWN = 0, | |
94 | TYPESPEC_VOID, | |
95 | TYPESPEC_CHAR, | |
96 | TYPESPEC_SHORT, | |
97 | TYPESPEC_INT, | |
98 | TYPESPEC_LONG, | |
99 | TYPESPEC_FLOAT, | |
100 | TYPESPEC_DOUBLE, | |
101 | TYPESPEC_SIGNED, | |
102 | TYPESPEC_UNSIGNED, | |
103 | TYPESPEC_BOOL, | |
104 | TYPESPEC_COMPLEX, | |
105 | TYPESPEC_IMAGINARY, | |
106 | TYPESPEC_CONST, | |
107 | TYPESPEC_ID_TYPE, | |
108 | TYPESPEC_FLOATING_POINT, | |
109 | TYPESPEC_INTEGER, | |
110 | TYPESPEC_STRING, | |
111 | TYPESPEC_STRUCT, | |
112 | TYPESPEC_VARIANT, | |
113 | TYPESPEC_ENUM, | |
e98a2d6e PP |
114 | }; |
115 | ||
4164020e SM |
116 | struct ctf_node |
117 | { | |
118 | /* | |
119 | * Parent node is only set on demand by specific visitor. | |
120 | */ | |
121 | struct ctf_node *parent; | |
122 | struct bt_list_head siblings; | |
123 | struct bt_list_head tmp_head; | |
124 | unsigned int lineno; | |
125 | /* | |
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 | |
130 | * metadata append. | |
131 | */ | |
132 | int visited; | |
133 | ||
134 | enum node_type type; | |
135 | union | |
136 | { | |
137 | struct | |
138 | { | |
139 | } unknown; | |
140 | struct | |
141 | { | |
142 | /* | |
143 | * Children nodes are ctf_expression, field_class_def, | |
144 | * field_class_alias and field_class_specifier_list. | |
145 | */ | |
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; | |
153 | } root; | |
154 | struct | |
155 | { | |
156 | /* | |
157 | * Children nodes are ctf_expression, field_class_def, | |
158 | * field_class_alias and field_class_specifier_list. | |
159 | */ | |
160 | struct bt_list_head declaration_list; | |
161 | } event; | |
162 | struct | |
163 | { | |
164 | /* | |
165 | * Children nodes are ctf_expression, field_class_def, | |
166 | * field_class_alias and field_class_specifier_list. | |
167 | */ | |
168 | struct bt_list_head declaration_list; | |
169 | } stream; | |
170 | struct | |
171 | { | |
172 | /* | |
173 | * Children nodes are ctf_expression, field_class_def, | |
174 | * field_class_alias and field_class_specifier_list. | |
175 | */ | |
176 | struct bt_list_head declaration_list; | |
177 | } env; | |
178 | struct | |
179 | { | |
180 | /* | |
181 | * Children nodes are ctf_expression, field_class_def, | |
182 | * field_class_alias and field_class_specifier_list. | |
183 | */ | |
184 | struct bt_list_head declaration_list; | |
185 | } trace; | |
186 | struct | |
187 | { | |
188 | /* | |
189 | * Children nodes are ctf_expression, field_class_def, | |
190 | * field_class_alias and field_class_specifier_list. | |
191 | */ | |
192 | struct bt_list_head declaration_list; | |
193 | } clock; | |
194 | struct | |
195 | { | |
196 | /* | |
197 | * Children nodes are ctf_expression, field_class_def, | |
198 | * field_class_alias and field_class_specifier_list. | |
199 | */ | |
200 | struct bt_list_head declaration_list; | |
201 | } callsite; | |
202 | struct | |
203 | { | |
204 | struct bt_list_head left; /* Should be string */ | |
205 | struct bt_list_head right; /* Unary exp. or type */ | |
206 | } ctf_expression; | |
207 | struct | |
208 | { | |
209 | ctf_unary type; | |
210 | union | |
211 | { | |
212 | /* | |
213 | * string for identifier, id_type, keywords, | |
214 | * string literals and character constants. | |
215 | */ | |
216 | char *string; | |
217 | int64_t signed_constant; | |
218 | uint64_t unsigned_constant; | |
219 | struct ctf_node *sbrac_exp; | |
220 | } u; | |
221 | ctf_unary_link link; | |
222 | } unary_expression; | |
223 | struct | |
224 | { | |
225 | struct ctf_node *field_class_specifier_list; | |
226 | struct bt_list_head field_class_declarators; | |
227 | } field_class_def; | |
228 | /* new type is "alias", existing type "target" */ | |
229 | struct | |
230 | { | |
231 | struct ctf_node *field_class_specifier_list; | |
232 | struct bt_list_head field_class_declarators; | |
233 | } field_class_alias_target; | |
234 | struct | |
235 | { | |
236 | struct ctf_node *field_class_specifier_list; | |
237 | struct bt_list_head field_class_declarators; | |
238 | } field_class_alias_name; | |
239 | struct | |
240 | { | |
241 | struct ctf_node *target; | |
242 | struct ctf_node *alias; | |
243 | } field_class_alias; | |
244 | struct | |
245 | { | |
246 | ctf_typespec type; | |
247 | /* For struct, variant and enum */ | |
248 | struct ctf_node *node; | |
249 | const char *id_type; | |
250 | } field_class_specifier; | |
251 | struct | |
252 | { | |
253 | /* list of field_class_specifier */ | |
254 | struct bt_list_head head; | |
255 | } field_class_specifier_list; | |
256 | struct | |
257 | { | |
258 | unsigned int const_qualifier; | |
259 | } pointer; | |
260 | struct | |
261 | { | |
262 | struct bt_list_head pointers; | |
263 | ctf_typedec type; | |
264 | union | |
265 | { | |
266 | char *id; | |
267 | struct | |
268 | { | |
269 | /* typedec has no pointer list */ | |
270 | struct ctf_node *field_class_declarator; | |
271 | /* | |
272 | * unary expression (value) or | |
273 | * field_class_specifier_list. | |
274 | */ | |
275 | struct bt_list_head length; | |
276 | /* for abstract type declarator */ | |
277 | unsigned int abstract_array; | |
278 | } nested; | |
279 | } u; | |
280 | struct ctf_node *bitfield_len; | |
281 | } field_class_declarator; | |
282 | struct | |
283 | { | |
284 | /* Children nodes are ctf_expression. */ | |
285 | struct bt_list_head expressions; | |
286 | } floating_point; | |
287 | struct | |
288 | { | |
289 | /* Children nodes are ctf_expression. */ | |
290 | struct bt_list_head expressions; | |
291 | } integer; | |
292 | struct | |
293 | { | |
294 | /* Children nodes are ctf_expression. */ | |
295 | struct bt_list_head expressions; | |
296 | } string; | |
297 | struct | |
298 | { | |
299 | char *id; | |
300 | /* | |
301 | * Range list or single value node. Contains unary | |
302 | * expressions. | |
303 | */ | |
304 | struct bt_list_head values; | |
305 | } enumerator; | |
306 | struct | |
307 | { | |
308 | char *enum_id; | |
309 | /* | |
310 | * Either NULL, or points to unary expression or | |
311 | * field_class_specifier_list. | |
312 | */ | |
313 | struct ctf_node *container_field_class; | |
314 | struct bt_list_head enumerator_list; | |
315 | int has_body; | |
316 | } _enum; | |
317 | struct | |
318 | { | |
319 | struct ctf_node *field_class_specifier_list; | |
320 | struct bt_list_head field_class_declarators; | |
321 | } struct_or_variant_declaration; | |
322 | struct | |
323 | { | |
324 | char *name; | |
325 | char *choice; | |
326 | /* | |
327 | * list of field_class_def, field_class_alias and | |
328 | * declarations | |
329 | */ | |
330 | struct bt_list_head declaration_list; | |
331 | int has_body; | |
332 | } variant; | |
333 | struct | |
334 | { | |
335 | char *name; | |
336 | /* | |
337 | * list of field_class_def, field_class_alias and | |
338 | * declarations | |
339 | */ | |
340 | struct bt_list_head declaration_list; | |
341 | int has_body; | |
342 | struct bt_list_head min_align; /* align() attribute */ | |
343 | } _struct; | |
344 | } u; | |
e98a2d6e PP |
345 | }; |
346 | ||
4164020e SM |
347 | struct ctf_ast |
348 | { | |
349 | struct ctf_node root; | |
e98a2d6e PP |
350 | }; |
351 | ||
352 | const char *node_type(struct ctf_node *node); | |
353 | ||
f7b785ac PP |
354 | struct meta_log_config; |
355 | ||
4164020e SM |
356 | struct ctf_visitor_generate_ir * |
357 | ctf_visitor_generate_ir_create(const struct ctf_metadata_decoder_config *config); | |
1e649dff PP |
358 | |
359 | void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir *visitor); | |
e98a2d6e | 360 | |
4164020e | 361 | bt_trace_class *ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_generate_ir *visitor); |
44c440bc | 362 | |
4164020e SM |
363 | struct ctf_trace_class * |
364 | ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir *visitor); | |
e98a2d6e | 365 | |
1e649dff | 366 | int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor, |
4164020e | 367 | struct ctf_node *node); |
e98a2d6e | 368 | |
4164020e | 369 | int ctf_visitor_semantic_check(int depth, struct ctf_node *node, struct meta_log_config *log_cfg); |
e98a2d6e | 370 | |
4164020e | 371 | int ctf_visitor_parent_links(int depth, struct ctf_node *node, struct meta_log_config *log_cfg); |
e98a2d6e | 372 | |
4164020e | 373 | static inline char *ctf_ast_concatenate_unary_strings(struct bt_list_head *head) |
1a6da3f9 | 374 | { |
4164020e SM |
375 | int i = 0; |
376 | GString *str; | |
377 | struct ctf_node *node; | |
378 | ||
379 | str = g_string_new(NULL); | |
380 | BT_ASSERT(str); | |
381 | ||
382 | bt_list_for_each_entry (node, head, siblings) { | |
383 | char *src_string; | |
384 | ||
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))) { | |
387 | goto error; | |
388 | } | |
389 | ||
390 | switch (node->u.unary_expression.link) { | |
391 | case UNARY_DOTLINK: | |
392 | g_string_append(str, "."); | |
393 | break; | |
394 | case UNARY_ARROWLINK: | |
395 | g_string_append(str, "->"); | |
396 | break; | |
397 | case UNARY_DOTDOTDOT: | |
398 | g_string_append(str, "..."); | |
399 | break; | |
400 | default: | |
401 | break; | |
402 | } | |
403 | ||
404 | src_string = node->u.unary_expression.u.string; | |
405 | g_string_append(str, src_string); | |
406 | i++; | |
407 | } | |
408 | ||
409 | /* Destroys the container, returns the underlying string */ | |
410 | return g_string_free(str, FALSE); | |
1a6da3f9 PP |
411 | |
412 | error: | |
4164020e SM |
413 | /* This always returns NULL */ |
414 | return g_string_free(str, TRUE); | |
1a6da3f9 PP |
415 | } |
416 | ||
4164020e SM |
417 | static inline int ctf_ast_get_unary_uuid(struct bt_list_head *head, bt_uuid_t uuid, int log_level, |
418 | bt_self_component *self_comp) | |
1a6da3f9 | 419 | { |
4164020e SM |
420 | int i = 0; |
421 | int ret = 0; | |
422 | struct ctf_node *node; | |
423 | ||
424 | bt_list_for_each_entry (node, head, siblings) { | |
425 | int uexpr_type = node->u.unary_expression.type; | |
426 | int uexpr_link = node->u.unary_expression.link; | |
427 | const char *src_string; | |
428 | ||
429 | if (node->type != NODE_UNARY_EXPRESSION || uexpr_type != UNARY_STRING || | |
430 | uexpr_link != UNARY_LINK_UNKNOWN || i != 0) { | |
431 | ret = -EINVAL; | |
432 | goto end; | |
433 | } | |
434 | ||
435 | src_string = node->u.unary_expression.u.string; | |
436 | ret = bt_uuid_from_str(src_string, uuid); | |
437 | if (ret) { | |
1a6da3f9 | 438 | #ifdef BT_COMP_LOG_CUR_LVL |
4164020e SM |
439 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp, |
440 | "Cannot parse UUID: uuid=\"%s\"", src_string); | |
1a6da3f9 | 441 | #endif |
4164020e SM |
442 | goto end; |
443 | } | |
444 | } | |
1a6da3f9 PP |
445 | |
446 | end: | |
4164020e | 447 | return ret; |
1a6da3f9 PP |
448 | } |
449 | ||
e98a2d6e | 450 | #endif /* _CTF_AST_H */ |