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