Commit | Line | Data |
---|---|---|
05628561 MD |
1 | /* |
2 | * ctf-visitor-generate-io-struct.c | |
3 | * | |
4 | * Common Trace Format Metadata Visitor (generate I/O structures). | |
5 | * | |
6 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | */ | |
18 | ||
19 | #include <stdio.h> | |
20 | #include <unistd.h> | |
21 | #include <string.h> | |
22 | #include <stdlib.h> | |
23 | #include <assert.h> | |
24 | #include <glib.h> | |
25 | #include <inttypes.h> | |
add40b62 | 26 | #include <endian.h> |
05628561 MD |
27 | #include <errno.h> |
28 | #include <babeltrace/list.h> | |
a0720417 MD |
29 | #include <babeltrace/types.h> |
30 | #include <babeltrace/ctf/metadata.h> | |
41253107 | 31 | #include <uuid/uuid.h> |
05628561 MD |
32 | #include "ctf-scanner.h" |
33 | #include "ctf-parser.h" | |
34 | #include "ctf-ast.h" | |
35 | ||
36 | #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args) | |
37 | ||
de47353a MD |
38 | #define _cds_list_first_entry(ptr, type, member) \ |
39 | cds_list_entry((ptr)->next, type, member) | |
05628561 | 40 | |
a3cca9e9 | 41 | static |
78af2bcd MD |
42 | struct declaration *ctf_type_specifier_list_visit(FILE *fd, |
43 | int depth, struct ctf_node *type_specifier_list, | |
ab4cf058 MD |
44 | struct declaration_scope *declaration_scope, |
45 | struct ctf_trace *trace); | |
a3cca9e9 | 46 | |
33105c61 MD |
47 | static |
48 | int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node, | |
49 | struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace); | |
50 | ||
05628561 | 51 | /* |
41253107 | 52 | * String returned must be freed by the caller using g_free. |
05628561 MD |
53 | */ |
54 | static | |
add40b62 | 55 | char *concatenate_unary_strings(struct cds_list_head *head) |
05628561 | 56 | { |
41253107 MD |
57 | struct ctf_node *node; |
58 | GString *str; | |
59 | int i = 0; | |
60 | ||
a0720417 | 61 | str = g_string_new(""); |
41253107 MD |
62 | cds_list_for_each_entry(node, head, siblings) { |
63 | char *src_string; | |
64 | ||
65 | assert(node->type == NODE_UNARY_EXPRESSION); | |
66 | assert(node->u.unary_expression.type == UNARY_STRING); | |
67 | assert((node->u.unary_expression.link == UNARY_LINK_UNKNOWN) | |
a0720417 | 68 | ^ (i != 0)); |
41253107 MD |
69 | switch (node->u.unary_expression.link) { |
70 | case UNARY_DOTLINK: | |
a0720417 | 71 | g_string_append(str, "."); |
41253107 MD |
72 | break; |
73 | case UNARY_ARROWLINK: | |
a0720417 | 74 | g_string_append(str, "->"); |
41253107 MD |
75 | break; |
76 | case UNARY_DOTDOTDOT: | |
a0720417 MD |
77 | g_string_append(str, "..."); |
78 | break; | |
79 | default: | |
41253107 MD |
80 | break; |
81 | } | |
a0720417 | 82 | src_string = node->u.unary_expression.u.string; |
41253107 MD |
83 | g_string_append(str, src_string); |
84 | i++; | |
85 | } | |
86 | return g_string_free(str, FALSE); | |
05628561 MD |
87 | } |
88 | ||
89 | static | |
add40b62 | 90 | int get_unary_unsigned(struct cds_list_head *head, uint64_t *value) |
05628561 | 91 | { |
41253107 MD |
92 | struct ctf_node *node; |
93 | int i = 0; | |
94 | ||
95 | cds_list_for_each_entry(node, head, siblings) { | |
96 | assert(node->type == NODE_UNARY_EXPRESSION); | |
97 | assert(node->u.unary_expression.type == UNARY_UNSIGNED_CONSTANT); | |
98 | assert(node->u.unary_expression.link == UNARY_LINK_UNKNOWN); | |
99 | assert(i == 0); | |
a0720417 | 100 | *value = node->u.unary_expression.u.unsigned_constant; |
41253107 MD |
101 | i++; |
102 | } | |
103 | return 0; | |
05628561 MD |
104 | } |
105 | ||
106 | static | |
add40b62 | 107 | int get_unary_uuid(struct cds_list_head *head, uuid_t *uuid) |
05628561 | 108 | { |
41253107 MD |
109 | struct ctf_node *node; |
110 | int i = 0; | |
111 | int ret = -1; | |
112 | ||
113 | cds_list_for_each_entry(node, head, siblings) { | |
114 | const char *src_string; | |
115 | ||
116 | assert(node->type == NODE_UNARY_EXPRESSION); | |
117 | assert(node->u.unary_expression.type == UNARY_STRING); | |
118 | assert(node->u.unary_expression.link == UNARY_LINK_UNKNOWN); | |
119 | assert(i == 0); | |
a0720417 MD |
120 | src_string = node->u.unary_expression.u.string; |
121 | ret = uuid_parse(node->u.unary_expression.u.string, *uuid); | |
41253107 MD |
122 | } |
123 | return ret; | |
05628561 MD |
124 | } |
125 | ||
126 | static | |
127 | struct ctf_stream *trace_stream_lookup(struct ctf_trace *trace, uint64_t stream_id) | |
128 | { | |
129 | if (trace->streams->len <= stream_id) | |
130 | return NULL; | |
131 | return g_ptr_array_index(trace->streams, stream_id); | |
132 | } | |
133 | ||
add40b62 | 134 | static |
78af2bcd MD |
135 | int visit_type_specifier(FILE *fd, struct ctf_node *type_specifier, GString *str) |
136 | { | |
137 | assert(type_specifier->type == NODE_TYPE_SPECIFIER); | |
138 | ||
139 | switch (type_specifier->u.type_specifier.type) { | |
140 | case TYPESPEC_VOID: | |
141 | g_string_append(str, "void"); | |
142 | break; | |
143 | case TYPESPEC_CHAR: | |
144 | g_string_append(str, "char"); | |
145 | break; | |
146 | case TYPESPEC_SHORT: | |
147 | g_string_append(str, "short"); | |
148 | break; | |
149 | case TYPESPEC_INT: | |
150 | g_string_append(str, "int"); | |
151 | break; | |
152 | case TYPESPEC_LONG: | |
153 | g_string_append(str, "long"); | |
154 | break; | |
155 | case TYPESPEC_FLOAT: | |
156 | g_string_append(str, "float"); | |
157 | break; | |
158 | case TYPESPEC_DOUBLE: | |
159 | g_string_append(str, "double"); | |
160 | break; | |
161 | case TYPESPEC_SIGNED: | |
162 | g_string_append(str, "signed"); | |
163 | break; | |
164 | case TYPESPEC_UNSIGNED: | |
165 | g_string_append(str, "unsigned"); | |
166 | break; | |
167 | case TYPESPEC_BOOL: | |
168 | g_string_append(str, "bool"); | |
169 | break; | |
170 | case TYPESPEC_COMPLEX: | |
171 | g_string_append(str, "_Complex"); | |
172 | break; | |
173 | case TYPESPEC_IMAGINARY: | |
174 | g_string_append(str, "_Imaginary"); | |
175 | break; | |
176 | case TYPESPEC_CONST: | |
177 | g_string_append(str, "const"); | |
178 | break; | |
179 | case TYPESPEC_ID_TYPE: | |
180 | if (type_specifier->u.type_specifier.id_type) | |
181 | g_string_append(str, type_specifier->u.type_specifier.id_type); | |
182 | break; | |
183 | case TYPESPEC_STRUCT: | |
184 | { | |
185 | struct ctf_node *node = type_specifier->u.type_specifier.node; | |
186 | ||
187 | if (!node->u._struct.name) { | |
188 | fprintf(fd, "[error] %s: unexpected empty variant name\n", __func__); | |
189 | return -EINVAL; | |
190 | } | |
191 | g_string_append(str, "struct "); | |
192 | g_string_append(str, node->u._struct.name); | |
193 | break; | |
194 | } | |
195 | case TYPESPEC_VARIANT: | |
196 | { | |
197 | struct ctf_node *node = type_specifier->u.type_specifier.node; | |
198 | ||
199 | if (!node->u.variant.name) { | |
200 | fprintf(fd, "[error] %s: unexpected empty variant name\n", __func__); | |
201 | return -EINVAL; | |
202 | } | |
203 | g_string_append(str, "variant "); | |
204 | g_string_append(str, node->u.variant.name); | |
205 | break; | |
206 | } | |
207 | case TYPESPEC_ENUM: | |
208 | { | |
209 | struct ctf_node *node = type_specifier->u.type_specifier.node; | |
210 | ||
211 | if (!node->u._enum.enum_id) { | |
212 | fprintf(fd, "[error] %s: unexpected empty enum ID\n", __func__); | |
213 | return -EINVAL; | |
214 | } | |
215 | g_string_append(str, "enum "); | |
216 | g_string_append(str, node->u._enum.enum_id); | |
217 | break; | |
218 | } | |
219 | case TYPESPEC_FLOATING_POINT: | |
220 | case TYPESPEC_INTEGER: | |
221 | case TYPESPEC_STRING: | |
222 | default: | |
223 | fprintf(fd, "[error] %s: unknown specifier\n", __func__); | |
224 | return -EINVAL; | |
225 | } | |
226 | return 0; | |
227 | } | |
228 | ||
229 | static | |
230 | int visit_type_specifier_list(FILE *fd, struct ctf_node *type_specifier_list, GString *str) | |
add40b62 MD |
231 | { |
232 | struct ctf_node *iter; | |
233 | int alias_item_nr = 0; | |
78af2bcd | 234 | int ret; |
add40b62 | 235 | |
78af2bcd | 236 | cds_list_for_each_entry(iter, &type_specifier_list->u.type_specifier_list.head, siblings) { |
add40b62 MD |
237 | if (alias_item_nr != 0) |
238 | g_string_append(str, " "); | |
239 | alias_item_nr++; | |
78af2bcd MD |
240 | ret = visit_type_specifier(fd, iter, str); |
241 | if (ret) | |
242 | return ret; | |
add40b62 MD |
243 | } |
244 | return 0; | |
add40b62 MD |
245 | } |
246 | ||
247 | static | |
8fdba45b | 248 | GQuark create_typealias_identifier(FILE *fd, int depth, |
78af2bcd | 249 | struct ctf_node *type_specifier_list, |
add40b62 MD |
250 | struct ctf_node *node_type_declarator) |
251 | { | |
a0720417 | 252 | struct ctf_node *iter; |
add40b62 | 253 | GString *str; |
a0720417 | 254 | char *str_c; |
add40b62 MD |
255 | GQuark alias_q; |
256 | int ret; | |
257 | ||
a0720417 | 258 | str = g_string_new(""); |
78af2bcd | 259 | ret = visit_type_specifier_list(fd, type_specifier_list, str); |
add40b62 MD |
260 | if (ret) { |
261 | g_string_free(str, TRUE); | |
262 | return 0; | |
263 | } | |
264 | cds_list_for_each_entry(iter, &node_type_declarator->u.type_declarator.pointers, siblings) { | |
265 | g_string_append(str, " *"); | |
266 | if (iter->u.pointer.const_qualifier) | |
267 | g_string_append(str, " const"); | |
268 | } | |
269 | str_c = g_string_free(str, FALSE); | |
270 | alias_q = g_quark_from_string(str_c); | |
271 | g_free(str_c); | |
272 | return alias_q; | |
273 | } | |
274 | ||
a3cca9e9 | 275 | static |
8fdba45b | 276 | struct declaration *ctf_type_declarator_visit(FILE *fd, int depth, |
78af2bcd | 277 | struct ctf_node *type_specifier_list, |
a3cca9e9 MD |
278 | GQuark *field_name, |
279 | struct ctf_node *node_type_declarator, | |
add40b62 | 280 | struct declaration_scope *declaration_scope, |
e397791f MD |
281 | struct declaration *nested_declaration, |
282 | struct ctf_trace *trace) | |
a3cca9e9 MD |
283 | { |
284 | /* | |
285 | * Visit type declarator by first taking care of sequence/array | |
286 | * (recursively). Then, when we get to the identifier, take care | |
287 | * of pointers. | |
288 | */ | |
289 | ||
e397791f MD |
290 | if (node_type_declarator) { |
291 | assert(node_type_declarator->u.type_declarator.type != TYPEDEC_UNKNOWN); | |
add40b62 | 292 | |
e397791f MD |
293 | /* TODO: gcc bitfields not supported yet. */ |
294 | if (node_type_declarator->u.type_declarator.bitfield_len != NULL) { | |
78af2bcd | 295 | fprintf(fd, "[error] %s: gcc bitfields are not supported yet.\n", __func__); |
e397791f MD |
296 | return NULL; |
297 | } | |
add40b62 | 298 | } |
a3cca9e9 MD |
299 | |
300 | if (!nested_declaration) { | |
e397791f | 301 | if (node_type_declarator && !cds_list_empty(&node_type_declarator->u.type_declarator.pointers)) { |
add40b62 MD |
302 | GQuark alias_q; |
303 | ||
a3cca9e9 MD |
304 | /* |
305 | * If we have a pointer declarator, it _has_ to be present in | |
306 | * the typealiases (else fail). | |
307 | */ | |
add40b62 | 308 | alias_q = create_typealias_identifier(fd, depth, |
78af2bcd | 309 | type_specifier_list, node_type_declarator); |
add40b62 MD |
310 | nested_declaration = lookup_declaration(alias_q, declaration_scope); |
311 | if (!nested_declaration) { | |
78af2bcd | 312 | fprintf(fd, "[error] %s: cannot find typealias \"%s\".\n", __func__, g_quark_to_string(alias_q)); |
add40b62 MD |
313 | return NULL; |
314 | } | |
a3cca9e9 | 315 | } else { |
78af2bcd MD |
316 | nested_declaration = ctf_type_specifier_list_visit(fd, depth, |
317 | type_specifier_list, declaration_scope, trace); | |
a3cca9e9 | 318 | } |
a3cca9e9 MD |
319 | } |
320 | ||
e397791f MD |
321 | if (!node_type_declarator) |
322 | return nested_declaration; | |
323 | ||
a3cca9e9 MD |
324 | if (node_type_declarator->u.type_declarator.type == TYPEDEC_ID) { |
325 | if (node_type_declarator->u.type_declarator.u.id) | |
326 | *field_name = g_quark_from_string(node_type_declarator->u.type_declarator.u.id); | |
327 | else | |
328 | *field_name = 0; | |
329 | return nested_declaration; | |
330 | } else { | |
331 | struct declaration *declaration; | |
78af2bcd | 332 | struct ctf_node *length; |
a3cca9e9 MD |
333 | |
334 | /* TYPEDEC_NESTED */ | |
335 | ||
336 | /* create array/sequence, pass nested_declaration as child. */ | |
78af2bcd MD |
337 | length = node_type_declarator->u.type_declarator.u.nested.length; |
338 | if (!length) { | |
339 | fprintf(fd, "[error] %s: expecting length type or value.\n", __func__); | |
a0720417 MD |
340 | return NULL; |
341 | } | |
78af2bcd | 342 | switch (length->type) { |
a0720417 MD |
343 | case NODE_UNARY_EXPRESSION: |
344 | { | |
345 | struct declaration_array *array_declaration; | |
346 | size_t len; | |
347 | ||
78af2bcd MD |
348 | if (length->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) { |
349 | fprintf(fd, "[error] %s: array: unexpected unary expression.\n", __func__); | |
a0720417 | 350 | return NULL; |
a3cca9e9 | 351 | } |
78af2bcd | 352 | len = length->u.unary_expression.u.unsigned_constant; |
a0720417 MD |
353 | array_declaration = array_declaration_new(len, nested_declaration, |
354 | declaration_scope); | |
355 | declaration = &array_declaration->p; | |
356 | break; | |
357 | } | |
2dd46001 | 358 | case NODE_TYPE_SPECIFIER_LIST: |
a0720417 MD |
359 | { |
360 | struct declaration_sequence *sequence_declaration; | |
361 | struct declaration_integer *integer_declaration; | |
a0720417 | 362 | |
2dd46001 MD |
363 | declaration = ctf_type_specifier_list_visit(fd, depth, |
364 | length, declaration_scope, trace); | |
365 | if (!declaration) { | |
366 | fprintf(fd, "[error] %s: unable to find declaration type for sequence length\n", __func__); | |
367 | return NULL; | |
368 | } | |
369 | if (declaration->id != CTF_TYPE_INTEGER) { | |
370 | fprintf(fd, "[error] %s: length type for sequence is expected to be an integer (unsigned).\n", __func__); | |
371 | declaration_unref(declaration); | |
372 | return NULL; | |
373 | } | |
a0720417 | 374 | integer_declaration = container_of(declaration, struct declaration_integer, p); |
2dd46001 MD |
375 | if (integer_declaration->signedness != false) { |
376 | fprintf(fd, "[error] %s: length type for sequence should always be an unsigned integer.\n", __func__); | |
377 | declaration_unref(declaration); | |
378 | return NULL; | |
379 | } | |
380 | ||
a0720417 MD |
381 | sequence_declaration = sequence_declaration_new(integer_declaration, |
382 | nested_declaration, declaration_scope); | |
383 | declaration = &sequence_declaration->p; | |
384 | break; | |
385 | } | |
386 | default: | |
387 | assert(0); | |
a3cca9e9 MD |
388 | } |
389 | ||
390 | /* Pass it as content of outer container */ | |
391 | declaration = ctf_type_declarator_visit(fd, depth, | |
78af2bcd | 392 | type_specifier_list, field_name, |
a3cca9e9 | 393 | node_type_declarator->u.type_declarator.u.nested.type_declarator, |
e397791f | 394 | declaration_scope, declaration, trace); |
a3cca9e9 MD |
395 | return declaration; |
396 | } | |
397 | } | |
398 | ||
399 | static | |
8fdba45b | 400 | int ctf_struct_type_declarators_visit(FILE *fd, int depth, |
a3cca9e9 | 401 | struct declaration_struct *struct_declaration, |
78af2bcd | 402 | struct ctf_node *type_specifier_list, |
a3cca9e9 | 403 | struct cds_list_head *type_declarators, |
e397791f MD |
404 | struct declaration_scope *declaration_scope, |
405 | struct ctf_trace *trace) | |
a3cca9e9 MD |
406 | { |
407 | struct ctf_node *iter; | |
408 | GQuark field_name; | |
409 | ||
410 | cds_list_for_each_entry(iter, type_declarators, siblings) { | |
411 | struct declaration *field_declaration; | |
412 | ||
413 | field_declaration = ctf_type_declarator_visit(fd, depth, | |
78af2bcd | 414 | type_specifier_list, |
a3cca9e9 MD |
415 | &field_name, iter, |
416 | struct_declaration->scope, | |
e397791f | 417 | NULL, trace); |
2dd46001 MD |
418 | if (!field_declaration) { |
419 | fprintf(fd, "[error] %s: unable to find struct field declaration type\n", __func__); | |
420 | return -EINVAL; | |
421 | } | |
a3cca9e9 MD |
422 | struct_declaration_add_field(struct_declaration, |
423 | g_quark_to_string(field_name), | |
424 | field_declaration); | |
425 | } | |
add40b62 MD |
426 | return 0; |
427 | } | |
a3cca9e9 | 428 | |
add40b62 | 429 | static |
8fdba45b | 430 | int ctf_variant_type_declarators_visit(FILE *fd, int depth, |
a0720417 | 431 | struct declaration_untagged_variant *untagged_variant_declaration, |
78af2bcd | 432 | struct ctf_node *type_specifier_list, |
add40b62 | 433 | struct cds_list_head *type_declarators, |
e397791f MD |
434 | struct declaration_scope *declaration_scope, |
435 | struct ctf_trace *trace) | |
add40b62 MD |
436 | { |
437 | struct ctf_node *iter; | |
438 | GQuark field_name; | |
439 | ||
440 | cds_list_for_each_entry(iter, type_declarators, siblings) { | |
441 | struct declaration *field_declaration; | |
442 | ||
443 | field_declaration = ctf_type_declarator_visit(fd, depth, | |
78af2bcd | 444 | type_specifier_list, |
add40b62 | 445 | &field_name, iter, |
a0720417 | 446 | untagged_variant_declaration->scope, |
e397791f | 447 | NULL, trace); |
2dd46001 MD |
448 | if (!field_declaration) { |
449 | fprintf(fd, "[error] %s: unable to find variant field declaration type\n", __func__); | |
450 | return -EINVAL; | |
451 | } | |
a0720417 | 452 | untagged_variant_declaration_add_field(untagged_variant_declaration, |
add40b62 MD |
453 | g_quark_to_string(field_name), |
454 | field_declaration); | |
455 | } | |
a3cca9e9 MD |
456 | return 0; |
457 | } | |
458 | ||
459 | static | |
8fdba45b | 460 | int ctf_typedef_visit(FILE *fd, int depth, struct declaration_scope *scope, |
78af2bcd | 461 | struct ctf_node *type_specifier_list, |
e397791f MD |
462 | struct cds_list_head *type_declarators, |
463 | struct ctf_trace *trace) | |
a3cca9e9 MD |
464 | { |
465 | struct ctf_node *iter; | |
466 | GQuark identifier; | |
467 | ||
468 | cds_list_for_each_entry(iter, type_declarators, siblings) { | |
469 | struct declaration *type_declaration; | |
470 | int ret; | |
471 | ||
472 | type_declaration = ctf_type_declarator_visit(fd, depth, | |
78af2bcd | 473 | type_specifier_list, |
a3cca9e9 | 474 | &identifier, iter, |
e397791f | 475 | scope, NULL, trace); |
2dd46001 MD |
476 | if (!type_declaration) { |
477 | fprintf(fd, "[error] %s: problem creating type declaration\n", __func__); | |
478 | return -EINVAL; | |
479 | } | |
480 | /* | |
481 | * Don't allow typedef and typealias of untagged | |
482 | * variants. | |
483 | */ | |
484 | if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) { | |
485 | fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__); | |
486 | declaration_unref(type_declaration); | |
487 | return -EPERM; | |
488 | } | |
a3cca9e9 MD |
489 | ret = register_declaration(identifier, type_declaration, scope); |
490 | if (ret) { | |
491 | type_declaration->declaration_free(type_declaration); | |
492 | return ret; | |
493 | } | |
494 | } | |
495 | return 0; | |
496 | } | |
497 | ||
498 | static | |
8fdba45b | 499 | int ctf_typealias_visit(FILE *fd, int depth, struct declaration_scope *scope, |
e397791f MD |
500 | struct ctf_node *target, struct ctf_node *alias, |
501 | struct ctf_trace *trace) | |
a3cca9e9 MD |
502 | { |
503 | struct declaration *type_declaration; | |
a0720417 | 504 | struct ctf_node *node; |
add40b62 | 505 | GQuark dummy_id; |
a3cca9e9 | 506 | GQuark alias_q; |
a0720417 | 507 | int err; |
a3cca9e9 MD |
508 | |
509 | /* See ctf_visitor_type_declarator() in the semantic validator. */ | |
510 | ||
511 | /* | |
512 | * Create target type declaration. | |
513 | */ | |
514 | ||
427c09b7 | 515 | if (cds_list_empty(&target->u.typealias_target.type_declarators)) |
a0720417 MD |
516 | node = NULL; |
517 | else | |
427c09b7 | 518 | node = _cds_list_first_entry(&target->u.typealias_target.type_declarators, |
a0720417 | 519 | struct ctf_node, siblings); |
a3cca9e9 | 520 | type_declaration = ctf_type_declarator_visit(fd, depth, |
78af2bcd | 521 | target->u.typealias_target.type_specifier_list, |
a0720417 | 522 | &dummy_id, node, |
e397791f | 523 | scope, NULL, trace); |
a3cca9e9 | 524 | if (!type_declaration) { |
78af2bcd | 525 | fprintf(fd, "[error] %s: problem creating type declaration\n", __func__); |
a3cca9e9 MD |
526 | err = -EINVAL; |
527 | goto error; | |
528 | } | |
2dd46001 MD |
529 | /* |
530 | * Don't allow typedef and typealias of untagged | |
531 | * variants. | |
532 | */ | |
533 | if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) { | |
534 | fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__); | |
535 | declaration_unref(type_declaration); | |
536 | return -EPERM; | |
537 | } | |
a3cca9e9 MD |
538 | /* |
539 | * The semantic validator does not check whether the target is | |
540 | * abstract or not (if it has an identifier). Check it here. | |
541 | */ | |
542 | if (dummy_id != 0) { | |
78af2bcd | 543 | fprintf(fd, "[error] %s: expecting empty identifier\n", __func__); |
a3cca9e9 MD |
544 | err = -EINVAL; |
545 | goto error; | |
546 | } | |
547 | /* | |
548 | * Create alias identifier. | |
549 | */ | |
a3cca9e9 MD |
550 | |
551 | node = _cds_list_first_entry(&alias->u.typealias_alias.type_declarators, | |
a0720417 | 552 | struct ctf_node, siblings); |
add40b62 | 553 | alias_q = create_typealias_identifier(fd, depth, |
78af2bcd | 554 | alias->u.typealias_alias.type_specifier_list, node); |
a0720417 MD |
555 | err = register_declaration(alias_q, type_declaration, scope); |
556 | if (err) | |
a3cca9e9 MD |
557 | goto error; |
558 | return 0; | |
559 | ||
560 | error: | |
561 | type_declaration->declaration_free(type_declaration); | |
a0720417 | 562 | return err; |
a3cca9e9 MD |
563 | } |
564 | ||
565 | static | |
8fdba45b | 566 | int ctf_struct_declaration_list_visit(FILE *fd, int depth, |
e397791f MD |
567 | struct ctf_node *iter, struct declaration_struct *struct_declaration, |
568 | struct ctf_trace *trace) | |
a3cca9e9 | 569 | { |
a3cca9e9 MD |
570 | int ret; |
571 | ||
572 | switch (iter->type) { | |
573 | case NODE_TYPEDEF: | |
574 | /* For each declarator, declare type and add type to struct declaration scope */ | |
575 | ret = ctf_typedef_visit(fd, depth, | |
576 | struct_declaration->scope, | |
78af2bcd | 577 | iter->u._typedef.type_specifier_list, |
e397791f | 578 | &iter->u._typedef.type_declarators, trace); |
a3cca9e9 MD |
579 | if (ret) |
580 | return ret; | |
581 | break; | |
582 | case NODE_TYPEALIAS: | |
583 | /* Declare type with declarator and add type to struct declaration scope */ | |
584 | ret = ctf_typealias_visit(fd, depth, | |
585 | struct_declaration->scope, | |
586 | iter->u.typealias.target, | |
e397791f | 587 | iter->u.typealias.alias, trace); |
a3cca9e9 MD |
588 | if (ret) |
589 | return ret; | |
590 | break; | |
591 | case NODE_STRUCT_OR_VARIANT_DECLARATION: | |
592 | /* Add field to structure declaration */ | |
593 | ret = ctf_struct_type_declarators_visit(fd, depth, | |
594 | struct_declaration, | |
78af2bcd | 595 | iter->u.struct_or_variant_declaration.type_specifier_list, |
a0720417 MD |
596 | &iter->u.struct_or_variant_declaration.type_declarators, |
597 | struct_declaration->scope, trace); | |
a3cca9e9 MD |
598 | if (ret) |
599 | return ret; | |
600 | break; | |
601 | default: | |
78af2bcd | 602 | fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) iter->type); |
a3cca9e9 MD |
603 | assert(0); |
604 | } | |
605 | return 0; | |
606 | } | |
607 | ||
add40b62 | 608 | static |
8fdba45b | 609 | int ctf_variant_declaration_list_visit(FILE *fd, int depth, |
a0720417 MD |
610 | struct ctf_node *iter, |
611 | struct declaration_untagged_variant *untagged_variant_declaration, | |
e397791f | 612 | struct ctf_trace *trace) |
add40b62 | 613 | { |
add40b62 MD |
614 | int ret; |
615 | ||
616 | switch (iter->type) { | |
617 | case NODE_TYPEDEF: | |
618 | /* For each declarator, declare type and add type to variant declaration scope */ | |
619 | ret = ctf_typedef_visit(fd, depth, | |
a0720417 | 620 | untagged_variant_declaration->scope, |
78af2bcd | 621 | iter->u._typedef.type_specifier_list, |
e397791f | 622 | &iter->u._typedef.type_declarators, trace); |
add40b62 MD |
623 | if (ret) |
624 | return ret; | |
625 | break; | |
626 | case NODE_TYPEALIAS: | |
627 | /* Declare type with declarator and add type to variant declaration scope */ | |
628 | ret = ctf_typealias_visit(fd, depth, | |
a0720417 | 629 | untagged_variant_declaration->scope, |
add40b62 | 630 | iter->u.typealias.target, |
e397791f | 631 | iter->u.typealias.alias, trace); |
add40b62 MD |
632 | if (ret) |
633 | return ret; | |
634 | break; | |
635 | case NODE_STRUCT_OR_VARIANT_DECLARATION: | |
636 | /* Add field to structure declaration */ | |
637 | ret = ctf_variant_type_declarators_visit(fd, depth, | |
a0720417 | 638 | untagged_variant_declaration, |
78af2bcd | 639 | iter->u.struct_or_variant_declaration.type_specifier_list, |
a0720417 MD |
640 | &iter->u.struct_or_variant_declaration.type_declarators, |
641 | untagged_variant_declaration->scope, trace); | |
add40b62 MD |
642 | if (ret) |
643 | return ret; | |
644 | break; | |
645 | default: | |
78af2bcd | 646 | fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) iter->type); |
add40b62 MD |
647 | assert(0); |
648 | } | |
649 | return 0; | |
650 | } | |
651 | ||
a3cca9e9 | 652 | static |
a0720417 | 653 | struct declaration *ctf_declaration_struct_visit(FILE *fd, |
a3cca9e9 | 654 | int depth, const char *name, struct cds_list_head *declaration_list, |
e397791f MD |
655 | int has_body, struct declaration_scope *declaration_scope, |
656 | struct ctf_trace *trace) | |
a3cca9e9 | 657 | { |
a3cca9e9 MD |
658 | struct declaration_struct *struct_declaration; |
659 | struct ctf_node *iter; | |
a0720417 | 660 | int ret; |
a3cca9e9 MD |
661 | |
662 | /* | |
663 | * For named struct (without body), lookup in | |
664 | * declaration scope. Don't take reference on struct | |
665 | * declaration: ref is only taken upon definition. | |
666 | */ | |
667 | if (!has_body) { | |
668 | assert(name); | |
669 | struct_declaration = | |
670 | lookup_struct_declaration(g_quark_from_string(name), | |
671 | declaration_scope); | |
a0720417 | 672 | return &struct_declaration->p; |
a3cca9e9 MD |
673 | } else { |
674 | /* For unnamed struct, create type */ | |
675 | /* For named struct (with body), create type and add to declaration scope */ | |
676 | if (name) { | |
677 | if (lookup_struct_declaration(g_quark_from_string(name), | |
678 | declaration_scope)) { | |
679 | ||
78af2bcd | 680 | fprintf(fd, "[error] %s: struct %s already declared in scope\n", __func__, name); |
a3cca9e9 MD |
681 | return NULL; |
682 | } | |
683 | } | |
a0720417 | 684 | struct_declaration = struct_declaration_new(declaration_scope); |
a3cca9e9 | 685 | cds_list_for_each_entry(iter, declaration_list, siblings) { |
e397791f MD |
686 | ret = ctf_struct_declaration_list_visit(fd, depth + 1, iter, |
687 | struct_declaration, trace); | |
a3cca9e9 MD |
688 | if (ret) |
689 | goto error; | |
690 | } | |
691 | if (name) { | |
692 | ret = register_struct_declaration(g_quark_from_string(name), | |
693 | struct_declaration, | |
694 | declaration_scope); | |
695 | assert(!ret); | |
696 | } | |
a0720417 | 697 | return &struct_declaration->p; |
a3cca9e9 MD |
698 | } |
699 | error: | |
700 | struct_declaration->p.declaration_free(&struct_declaration->p); | |
701 | return NULL; | |
702 | } | |
703 | ||
05628561 | 704 | static |
a0720417 MD |
705 | struct declaration *ctf_declaration_variant_visit(FILE *fd, |
706 | int depth, const char *name, const char *choice, | |
707 | struct cds_list_head *declaration_list, | |
e397791f MD |
708 | int has_body, struct declaration_scope *declaration_scope, |
709 | struct ctf_trace *trace) | |
05628561 | 710 | { |
a0720417 | 711 | struct declaration_untagged_variant *untagged_variant_declaration; |
add40b62 MD |
712 | struct declaration_variant *variant_declaration; |
713 | struct ctf_node *iter; | |
a0720417 | 714 | int ret; |
de47353a | 715 | |
add40b62 MD |
716 | /* |
717 | * For named variant (without body), lookup in | |
718 | * declaration scope. Don't take reference on variant | |
719 | * declaration: ref is only taken upon definition. | |
720 | */ | |
721 | if (!has_body) { | |
722 | assert(name); | |
a0720417 | 723 | untagged_variant_declaration = |
add40b62 MD |
724 | lookup_variant_declaration(g_quark_from_string(name), |
725 | declaration_scope); | |
add40b62 | 726 | } else { |
de47353a | 727 | /* For unnamed variant, create type */ |
add40b62 MD |
728 | /* For named variant (with body), create type and add to declaration scope */ |
729 | if (name) { | |
730 | if (lookup_variant_declaration(g_quark_from_string(name), | |
731 | declaration_scope)) { | |
732 | ||
78af2bcd | 733 | fprintf(fd, "[error] %s: variant %s already declared in scope\n", __func__, name); |
add40b62 MD |
734 | return NULL; |
735 | } | |
736 | } | |
a0720417 | 737 | untagged_variant_declaration = untagged_variant_declaration_new(declaration_scope); |
add40b62 | 738 | cds_list_for_each_entry(iter, declaration_list, siblings) { |
e397791f | 739 | ret = ctf_variant_declaration_list_visit(fd, depth + 1, iter, |
a0720417 | 740 | untagged_variant_declaration, trace); |
add40b62 MD |
741 | if (ret) |
742 | goto error; | |
743 | } | |
744 | if (name) { | |
745 | ret = register_variant_declaration(g_quark_from_string(name), | |
a0720417 | 746 | untagged_variant_declaration, |
add40b62 MD |
747 | declaration_scope); |
748 | assert(!ret); | |
749 | } | |
a0720417 MD |
750 | } |
751 | /* | |
752 | * if tagged, create tagged variant and return. else return | |
753 | * untagged variant. | |
754 | */ | |
755 | if (!choice) { | |
756 | return &untagged_variant_declaration->p; | |
757 | } else { | |
758 | variant_declaration = variant_declaration_new(untagged_variant_declaration, choice); | |
759 | if (!variant_declaration) | |
760 | goto error; | |
761 | declaration_unref(&untagged_variant_declaration->p); | |
762 | return &variant_declaration->p; | |
de47353a | 763 | } |
add40b62 | 764 | error: |
2dd46001 | 765 | untagged_variant_declaration->p.declaration_free(&untagged_variant_declaration->p); |
add40b62 | 766 | return NULL; |
05628561 MD |
767 | } |
768 | ||
05628561 | 769 | static |
8fdba45b | 770 | int ctf_enumerator_list_visit(FILE *fd, int depth, |
add40b62 MD |
771 | struct ctf_node *enumerator, |
772 | struct declaration_enum *enum_declaration) | |
773 | { | |
1cfda062 MD |
774 | GQuark q; |
775 | struct ctf_node *iter; | |
776 | ||
777 | q = g_quark_from_string(enumerator->u.enumerator.id); | |
a0720417 | 778 | if (enum_declaration->integer_declaration->signedness) { |
1cfda062 MD |
779 | int64_t start, end; |
780 | int nr_vals = 0; | |
781 | ||
a0720417 | 782 | cds_list_for_each_entry(iter, &enumerator->u.enumerator.values, siblings) { |
1cfda062 MD |
783 | int64_t *target; |
784 | ||
785 | assert(iter->type == NODE_UNARY_EXPRESSION); | |
786 | if (nr_vals == 0) | |
787 | target = &start; | |
788 | else | |
789 | target = &end; | |
790 | ||
791 | switch (iter->u.unary_expression.type) { | |
792 | case UNARY_SIGNED_CONSTANT: | |
793 | *target = iter->u.unary_expression.u.signed_constant; | |
794 | break; | |
795 | case UNARY_UNSIGNED_CONSTANT: | |
796 | *target = iter->u.unary_expression.u.unsigned_constant; | |
797 | break; | |
798 | default: | |
78af2bcd | 799 | fprintf(fd, "[error] %s: invalid enumerator\n", __func__); |
1cfda062 MD |
800 | return -EINVAL; |
801 | } | |
802 | if (nr_vals > 1) { | |
78af2bcd | 803 | fprintf(fd, "[error] %s: invalid enumerator\n", __func__); |
1cfda062 MD |
804 | return -EINVAL; |
805 | } | |
806 | nr_vals++; | |
807 | } | |
808 | if (nr_vals == 1) | |
809 | end = start; | |
810 | enum_signed_insert(enum_declaration, start, end, q); | |
a0720417 | 811 | } else { |
1cfda062 MD |
812 | uint64_t start, end; |
813 | int nr_vals = 0; | |
814 | ||
a0720417 MD |
815 | cds_list_for_each_entry(iter, &enumerator->u.enumerator.values, siblings) { |
816 | uint64_t *target; | |
1cfda062 MD |
817 | |
818 | assert(iter->type == NODE_UNARY_EXPRESSION); | |
819 | if (nr_vals == 0) | |
820 | target = &start; | |
821 | else | |
822 | target = &end; | |
823 | ||
824 | switch (iter->u.unary_expression.type) { | |
825 | case UNARY_UNSIGNED_CONSTANT: | |
826 | *target = iter->u.unary_expression.u.unsigned_constant; | |
827 | break; | |
828 | case UNARY_SIGNED_CONSTANT: | |
829 | /* | |
830 | * We don't accept signed constants for enums with unsigned | |
831 | * container type. | |
832 | */ | |
78af2bcd | 833 | fprintf(fd, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__); |
1cfda062 MD |
834 | return -EINVAL; |
835 | default: | |
78af2bcd | 836 | fprintf(fd, "[error] %s: invalid enumerator\n", __func__); |
1cfda062 MD |
837 | return -EINVAL; |
838 | } | |
839 | if (nr_vals > 1) { | |
78af2bcd | 840 | fprintf(fd, "[error] %s: invalid enumerator\n", __func__); |
1cfda062 MD |
841 | return -EINVAL; |
842 | } | |
843 | nr_vals++; | |
844 | } | |
845 | if (nr_vals == 1) | |
846 | end = start; | |
847 | enum_unsigned_insert(enum_declaration, start, end, q); | |
848 | } | |
add40b62 MD |
849 | return 0; |
850 | } | |
851 | ||
852 | static | |
8fdba45b | 853 | struct declaration *ctf_declaration_enum_visit(FILE *fd, int depth, |
add40b62 | 854 | const char *name, |
78af2bcd | 855 | struct ctf_node *container_type, |
add40b62 | 856 | struct cds_list_head *enumerator_list, |
1cfda062 MD |
857 | int has_body, |
858 | struct declaration_scope *declaration_scope, | |
859 | struct ctf_trace *trace) | |
05628561 | 860 | { |
add40b62 MD |
861 | struct declaration *declaration; |
862 | struct declaration_enum *enum_declaration; | |
863 | struct declaration_integer *integer_declaration; | |
78af2bcd | 864 | struct ctf_node *iter; |
1cfda062 | 865 | GQuark dummy_id; |
a0720417 | 866 | int ret; |
add40b62 | 867 | |
05628561 | 868 | /* |
add40b62 MD |
869 | * For named enum (without body), lookup in |
870 | * declaration scope. Don't take reference on enum | |
871 | * declaration: ref is only taken upon definition. | |
05628561 | 872 | */ |
add40b62 MD |
873 | if (!has_body) { |
874 | assert(name); | |
875 | enum_declaration = | |
876 | lookup_enum_declaration(g_quark_from_string(name), | |
877 | declaration_scope); | |
a0720417 | 878 | return &enum_declaration->p; |
add40b62 MD |
879 | } else { |
880 | /* For unnamed enum, create type */ | |
881 | /* For named enum (with body), create type and add to declaration scope */ | |
882 | if (name) { | |
883 | if (lookup_enum_declaration(g_quark_from_string(name), | |
884 | declaration_scope)) { | |
885 | ||
78af2bcd | 886 | fprintf(fd, "[error] %s: enum %s already declared in scope\n", __func__, name); |
add40b62 MD |
887 | return NULL; |
888 | } | |
889 | } | |
78af2bcd | 890 | if (!container_type) { |
6743829a MD |
891 | declaration = lookup_declaration(g_quark_from_static_string("int"), |
892 | declaration_scope); | |
893 | if (!declaration) { | |
894 | fprintf(fd, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__); | |
895 | return NULL; | |
896 | } | |
897 | } else { | |
898 | declaration = ctf_type_declarator_visit(fd, depth, | |
899 | container_type, | |
900 | &dummy_id, NULL, | |
901 | declaration_scope, | |
902 | NULL, trace); | |
1cfda062 | 903 | } |
30ea18a1 MD |
904 | if (!declaration) { |
905 | fprintf(fd, "[error] %s: unable to create container type for enumeration\n", __func__); | |
906 | return NULL; | |
907 | } | |
908 | if (declaration->id != CTF_TYPE_INTEGER) { | |
909 | fprintf(fd, "[error] %s: container type for enumeration is not integer\n", __func__); | |
910 | return NULL; | |
1cfda062 | 911 | } |
30ea18a1 | 912 | integer_declaration = container_of(declaration, struct declaration_integer, p); |
a0720417 | 913 | enum_declaration = enum_declaration_new(integer_declaration); |
add40b62 MD |
914 | declaration_unref(&integer_declaration->p); /* leave ref to enum */ |
915 | cds_list_for_each_entry(iter, enumerator_list, siblings) { | |
916 | ret = ctf_enumerator_list_visit(fd, depth + 1, iter, enum_declaration); | |
917 | if (ret) | |
918 | goto error; | |
919 | } | |
920 | if (name) { | |
921 | ret = register_enum_declaration(g_quark_from_string(name), | |
922 | enum_declaration, | |
923 | declaration_scope); | |
924 | assert(!ret); | |
925 | } | |
a0720417 | 926 | return &enum_declaration->p; |
05628561 | 927 | } |
add40b62 MD |
928 | error: |
929 | enum_declaration->p.declaration_free(&enum_declaration->p); | |
930 | return NULL; | |
05628561 MD |
931 | } |
932 | ||
933 | static | |
8fdba45b | 934 | struct declaration *ctf_declaration_type_specifier_visit(FILE *fd, int depth, |
78af2bcd | 935 | struct ctf_node *type_specifier_list, |
d20f5e59 | 936 | struct declaration_scope *declaration_scope) |
05628561 | 937 | { |
add40b62 MD |
938 | GString *str; |
939 | struct declaration *declaration; | |
a0720417 MD |
940 | char *str_c; |
941 | int ret; | |
942 | GQuark id_q; | |
05628561 | 943 | |
a0720417 | 944 | str = g_string_new(""); |
78af2bcd | 945 | ret = visit_type_specifier_list(fd, type_specifier_list, str); |
add40b62 MD |
946 | if (ret) |
947 | return NULL; | |
948 | str_c = g_string_free(str, FALSE); | |
949 | id_q = g_quark_from_string(str_c); | |
950 | g_free(str_c); | |
951 | declaration = lookup_declaration(id_q, declaration_scope); | |
952 | return declaration; | |
953 | } | |
954 | ||
ab4cf058 MD |
955 | /* |
956 | * Returns 0/1 boolean, or < 0 on error. | |
957 | */ | |
958 | static | |
a0720417 | 959 | int get_boolean(FILE *fd, int depth, struct ctf_node *unary_expression) |
ab4cf058 MD |
960 | { |
961 | if (unary_expression->type != NODE_UNARY_EXPRESSION) { | |
78af2bcd | 962 | fprintf(fd, "[error] %s: expecting unary expression\n", |
ab4cf058 MD |
963 | __func__); |
964 | return -EINVAL; | |
965 | } | |
966 | switch (unary_expression->u.unary_expression.type) { | |
967 | case UNARY_UNSIGNED_CONSTANT: | |
968 | if (unary_expression->u.unary_expression.u.unsigned_constant == 0) | |
969 | return 0; | |
970 | else | |
971 | return 1; | |
972 | case UNARY_SIGNED_CONSTANT: | |
973 | if (unary_expression->u.unary_expression.u.signed_constant == 0) | |
974 | return 0; | |
975 | else | |
976 | return 1; | |
977 | case UNARY_STRING: | |
978 | if (!strcmp(unary_expression->u.unary_expression.u.string, "true")) | |
979 | return 1; | |
980 | else if (!strcmp(unary_expression->u.unary_expression.u.string, "TRUE")) | |
981 | return 1; | |
982 | else if (!strcmp(unary_expression->u.unary_expression.u.string, "false")) | |
983 | return 0; | |
984 | else if (!strcmp(unary_expression->u.unary_expression.u.string, "FALSE")) | |
985 | return 0; | |
986 | else { | |
78af2bcd | 987 | fprintf(fd, "[error] %s: unexpected string \"%s\"\n", |
ab4cf058 MD |
988 | __func__, unary_expression->u.unary_expression.u.string); |
989 | return -EINVAL; | |
990 | } | |
991 | break; | |
992 | default: | |
78af2bcd | 993 | fprintf(fd, "[error] %s: unexpected unary expression type\n", |
ab4cf058 MD |
994 | __func__); |
995 | return -EINVAL; | |
996 | } | |
997 | ||
998 | } | |
999 | ||
0f980a35 MD |
1000 | static |
1001 | int get_trace_byte_order(FILE *fd, int depth, struct ctf_node *unary_expression) | |
1002 | { | |
1003 | int byte_order; | |
1004 | ||
1005 | if (unary_expression->u.unary_expression.type != UNARY_STRING) { | |
1006 | fprintf(fd, "[error] %s: byte_order: expecting string\n", | |
1007 | __func__); | |
1008 | return -EINVAL; | |
1009 | } | |
1010 | if (!strcmp(unary_expression->u.unary_expression.u.string, "be")) | |
1011 | byte_order = BIG_ENDIAN; | |
1012 | else if (!strcmp(unary_expression->u.unary_expression.u.string, "le")) | |
1013 | byte_order = LITTLE_ENDIAN; | |
1014 | else { | |
1015 | fprintf(fd, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n", | |
1016 | __func__, unary_expression->u.unary_expression.u.string); | |
1017 | return -EINVAL; | |
1018 | } | |
1019 | return byte_order; | |
1020 | } | |
1021 | ||
ab4cf058 | 1022 | static |
a0720417 MD |
1023 | int get_byte_order(FILE *fd, int depth, struct ctf_node *unary_expression, |
1024 | struct ctf_trace *trace) | |
ab4cf058 MD |
1025 | { |
1026 | int byte_order; | |
1027 | ||
1028 | if (unary_expression->u.unary_expression.type != UNARY_STRING) { | |
78af2bcd | 1029 | fprintf(fd, "[error] %s: byte_order: expecting string\n", |
ab4cf058 MD |
1030 | __func__); |
1031 | return -EINVAL; | |
1032 | } | |
1033 | if (!strcmp(unary_expression->u.unary_expression.u.string, "native")) | |
1034 | byte_order = trace->byte_order; | |
1035 | else if (!strcmp(unary_expression->u.unary_expression.u.string, "network")) | |
1036 | byte_order = BIG_ENDIAN; | |
1037 | else if (!strcmp(unary_expression->u.unary_expression.u.string, "be")) | |
1038 | byte_order = BIG_ENDIAN; | |
1039 | else if (!strcmp(unary_expression->u.unary_expression.u.string, "le")) | |
1040 | byte_order = LITTLE_ENDIAN; | |
1041 | else { | |
78af2bcd | 1042 | fprintf(fd, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n", |
a0720417 | 1043 | __func__, unary_expression->u.unary_expression.u.string); |
ab4cf058 MD |
1044 | return -EINVAL; |
1045 | } | |
1046 | return byte_order; | |
1047 | } | |
1048 | ||
add40b62 | 1049 | static |
8fdba45b | 1050 | struct declaration *ctf_declaration_integer_visit(FILE *fd, int depth, |
ab4cf058 MD |
1051 | struct cds_list_head *expressions, |
1052 | struct ctf_trace *trace) | |
add40b62 | 1053 | { |
a0720417 | 1054 | struct ctf_node *expression; |
ab4cf058 MD |
1055 | uint64_t alignment, size; |
1056 | int byte_order = trace->byte_order; | |
1057 | int signedness = 0; | |
add40b62 MD |
1058 | int has_alignment = 0, has_size = 0; |
1059 | struct declaration_integer *integer_declaration; | |
1060 | ||
1061 | cds_list_for_each_entry(expression, expressions, siblings) { | |
a0720417 | 1062 | struct ctf_node *left, *right; |
add40b62 | 1063 | |
a0720417 MD |
1064 | left = _cds_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings); |
1065 | right = _cds_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings); | |
add40b62 MD |
1066 | assert(left->u.unary_expression.type == UNARY_STRING); |
1067 | if (!strcmp(left->u.unary_expression.u.string, "signed")) { | |
ab4cf058 MD |
1068 | signedness = get_boolean(fd, depth, right); |
1069 | if (signedness < 0) | |
1070 | return NULL; | |
add40b62 | 1071 | } else if (!strcmp(left->u.unary_expression.u.string, "byte_order")) { |
a0720417 | 1072 | byte_order = get_byte_order(fd, depth, right, trace); |
ab4cf058 MD |
1073 | if (byte_order < 0) |
1074 | return NULL; | |
add40b62 | 1075 | } else if (!strcmp(left->u.unary_expression.u.string, "size")) { |
ab4cf058 | 1076 | if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) { |
78af2bcd | 1077 | fprintf(fd, "[error] %s: size: expecting unsigned constant\n", |
ab4cf058 MD |
1078 | __func__); |
1079 | return NULL; | |
1080 | } | |
1081 | size = right->u.unary_expression.u.unsigned_constant; | |
add40b62 MD |
1082 | has_size = 1; |
1083 | } else if (!strcmp(left->u.unary_expression.u.string, "align")) { | |
ab4cf058 | 1084 | if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) { |
78af2bcd | 1085 | fprintf(fd, "[error] %s: align: expecting unsigned constant\n", |
ab4cf058 MD |
1086 | __func__); |
1087 | return NULL; | |
1088 | } | |
1089 | alignment = right->u.unary_expression.u.unsigned_constant; | |
add40b62 MD |
1090 | has_alignment = 1; |
1091 | } else { | |
78af2bcd | 1092 | fprintf(fd, "[error] %s: unknown attribute name %s\n", |
add40b62 MD |
1093 | __func__, left->u.unary_expression.u.string); |
1094 | return NULL; | |
1095 | } | |
05628561 | 1096 | } |
add40b62 | 1097 | if (!has_size) { |
78af2bcd | 1098 | fprintf(fd, "[error] %s: missing size attribute\n", __func__); |
add40b62 MD |
1099 | return NULL; |
1100 | } | |
ab4cf058 MD |
1101 | if (!has_alignment) { |
1102 | if (size % CHAR_BIT) { | |
1103 | /* bit-packed alignment */ | |
1104 | alignment = 1; | |
1105 | } else { | |
1106 | /* byte-packed alignment */ | |
1107 | alignment = CHAR_BIT; | |
1108 | } | |
1109 | } | |
add40b62 MD |
1110 | integer_declaration = integer_declaration_new(size, |
1111 | byte_order, signedness, alignment); | |
1112 | return &integer_declaration->p; | |
05628561 MD |
1113 | } |
1114 | ||
ab4cf058 | 1115 | static |
8fdba45b | 1116 | struct declaration *ctf_declaration_floating_point_visit(FILE *fd, int depth, |
ab4cf058 MD |
1117 | struct cds_list_head *expressions, |
1118 | struct ctf_trace *trace) | |
1119 | { | |
a0720417 | 1120 | struct ctf_node *expression; |
ab4cf058 MD |
1121 | uint64_t alignment, exp_dig, mant_dig, byte_order = trace->byte_order; |
1122 | int has_alignment = 0, has_exp_dig = 0, has_mant_dig = 0; | |
1123 | struct declaration_float *float_declaration; | |
1124 | ||
1125 | cds_list_for_each_entry(expression, expressions, siblings) { | |
a0720417 | 1126 | struct ctf_node *left, *right; |
ab4cf058 | 1127 | |
a0720417 MD |
1128 | left = _cds_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings); |
1129 | right = _cds_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings); | |
ab4cf058 MD |
1130 | assert(left->u.unary_expression.type == UNARY_STRING); |
1131 | if (!strcmp(left->u.unary_expression.u.string, "byte_order")) { | |
a0720417 | 1132 | byte_order = get_byte_order(fd, depth, right, trace); |
ab4cf058 MD |
1133 | if (byte_order < 0) |
1134 | return NULL; | |
1135 | } else if (!strcmp(left->u.unary_expression.u.string, "exp_dig")) { | |
1136 | if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) { | |
78af2bcd | 1137 | fprintf(fd, "[error] %s: exp_dig: expecting unsigned constant\n", |
ab4cf058 MD |
1138 | __func__); |
1139 | return NULL; | |
1140 | } | |
1141 | exp_dig = right->u.unary_expression.u.unsigned_constant; | |
1142 | has_exp_dig = 1; | |
1143 | } else if (!strcmp(left->u.unary_expression.u.string, "mant_dig")) { | |
1144 | if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) { | |
78af2bcd | 1145 | fprintf(fd, "[error] %s: mant_dig: expecting unsigned constant\n", |
ab4cf058 MD |
1146 | __func__); |
1147 | return NULL; | |
1148 | } | |
1149 | mant_dig = right->u.unary_expression.u.unsigned_constant; | |
1150 | has_mant_dig = 1; | |
1151 | } else if (!strcmp(left->u.unary_expression.u.string, "align")) { | |
1152 | if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) { | |
78af2bcd | 1153 | fprintf(fd, "[error] %s: align: expecting unsigned constant\n", |
ab4cf058 MD |
1154 | __func__); |
1155 | return NULL; | |
1156 | } | |
1157 | alignment = right->u.unary_expression.u.unsigned_constant; | |
1158 | has_alignment = 1; | |
1159 | } else { | |
78af2bcd | 1160 | fprintf(fd, "[error] %s: unknown attribute name %s\n", |
ab4cf058 MD |
1161 | __func__, left->u.unary_expression.u.string); |
1162 | return NULL; | |
1163 | } | |
1164 | } | |
1165 | if (!has_mant_dig) { | |
78af2bcd | 1166 | fprintf(fd, "[error] %s: missing mant_dig attribute\n", __func__); |
ab4cf058 MD |
1167 | return NULL; |
1168 | } | |
1169 | if (!has_exp_dig) { | |
78af2bcd | 1170 | fprintf(fd, "[error] %s: missing exp_dig attribute\n", __func__); |
ab4cf058 MD |
1171 | return NULL; |
1172 | } | |
1173 | if (!has_alignment) { | |
1174 | if ((mant_dig + exp_dig) % CHAR_BIT) { | |
1175 | /* bit-packed alignment */ | |
1176 | alignment = 1; | |
1177 | } else { | |
1178 | /* byte-packed alignment */ | |
1179 | alignment = CHAR_BIT; | |
1180 | } | |
1181 | } | |
1182 | float_declaration = float_declaration_new(mant_dig, exp_dig, | |
1183 | byte_order, alignment); | |
1184 | return &float_declaration->p; | |
1185 | } | |
1186 | ||
1187 | static | |
8fdba45b | 1188 | struct declaration *ctf_declaration_string_visit(FILE *fd, int depth, |
ab4cf058 MD |
1189 | struct cds_list_head *expressions, |
1190 | struct ctf_trace *trace) | |
1191 | { | |
a0720417 | 1192 | struct ctf_node *expression; |
ab4cf058 MD |
1193 | const char *encoding_c = NULL; |
1194 | enum ctf_string_encoding encoding = CTF_STRING_UTF8; | |
1195 | struct declaration_string *string_declaration; | |
1196 | ||
1197 | cds_list_for_each_entry(expression, expressions, siblings) { | |
a0720417 | 1198 | struct ctf_node *left, *right; |
ab4cf058 | 1199 | |
a0720417 MD |
1200 | left = _cds_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings); |
1201 | right = _cds_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings); | |
ab4cf058 MD |
1202 | assert(left->u.unary_expression.type == UNARY_STRING); |
1203 | if (!strcmp(left->u.unary_expression.u.string, "encoding")) { | |
a0720417 | 1204 | if (right->u.unary_expression.type != UNARY_STRING) { |
78af2bcd | 1205 | fprintf(fd, "[error] %s: encoding: expecting string\n", |
ab4cf058 MD |
1206 | __func__); |
1207 | return NULL; | |
1208 | } | |
1209 | encoding_c = right->u.unary_expression.u.string; | |
1210 | } else { | |
78af2bcd | 1211 | fprintf(fd, "[error] %s: unknown attribute name %s\n", |
ab4cf058 MD |
1212 | __func__, left->u.unary_expression.u.string); |
1213 | return NULL; | |
1214 | } | |
1215 | } | |
1216 | if (encoding_c && !strcmp(encoding_c, "ASCII")) | |
1217 | encoding = CTF_STRING_ASCII; | |
1218 | string_declaration = string_declaration_new(encoding); | |
1219 | return &string_declaration->p; | |
1220 | } | |
1221 | ||
1222 | ||
05628561 | 1223 | static |
78af2bcd MD |
1224 | struct declaration *ctf_type_specifier_list_visit(FILE *fd, |
1225 | int depth, struct ctf_node *type_specifier_list, | |
ab4cf058 MD |
1226 | struct declaration_scope *declaration_scope, |
1227 | struct ctf_trace *trace) | |
05628561 | 1228 | { |
a0720417 | 1229 | struct ctf_node *first; |
78af2bcd | 1230 | struct ctf_node *node; |
d20f5e59 | 1231 | |
427c09b7 MD |
1232 | assert(type_specifier_list->type == NODE_TYPE_SPECIFIER_LIST); |
1233 | ||
78af2bcd | 1234 | first = _cds_list_first_entry(&type_specifier_list->u.type_specifier_list.head, struct ctf_node, siblings); |
05628561 | 1235 | |
78af2bcd MD |
1236 | assert(first->type == NODE_TYPE_SPECIFIER); |
1237 | ||
1238 | node = first->u.type_specifier.node; | |
1239 | ||
1240 | switch (first->u.type_specifier.type) { | |
1241 | case TYPESPEC_FLOATING_POINT: | |
1242 | return ctf_declaration_floating_point_visit(fd, depth, | |
1243 | &node->u.floating_point.expressions, trace); | |
1244 | case TYPESPEC_INTEGER: | |
1245 | return ctf_declaration_integer_visit(fd, depth, | |
1246 | &node->u.integer.expressions, trace); | |
1247 | case TYPESPEC_STRING: | |
1248 | return ctf_declaration_string_visit(fd, depth, | |
5039b4cc | 1249 | &node->u.string.expressions, trace); |
78af2bcd | 1250 | case TYPESPEC_STRUCT: |
add40b62 | 1251 | return ctf_declaration_struct_visit(fd, depth, |
78af2bcd MD |
1252 | node->u._struct.name, |
1253 | &node->u._struct.declaration_list, | |
1254 | node->u._struct.has_body, | |
ab4cf058 MD |
1255 | declaration_scope, |
1256 | trace); | |
78af2bcd | 1257 | case TYPESPEC_VARIANT: |
add40b62 | 1258 | return ctf_declaration_variant_visit(fd, depth, |
78af2bcd MD |
1259 | node->u.variant.name, |
1260 | node->u.variant.choice, | |
1261 | &node->u.variant.declaration_list, | |
1262 | node->u.variant.has_body, | |
ab4cf058 MD |
1263 | declaration_scope, |
1264 | trace); | |
78af2bcd | 1265 | case TYPESPEC_ENUM: |
add40b62 | 1266 | return ctf_declaration_enum_visit(fd, depth, |
78af2bcd MD |
1267 | node->u._enum.enum_id, |
1268 | node->u._enum.container_type, | |
1269 | &node->u._enum.enumerator_list, | |
1270 | node->u._enum.has_body, | |
1cfda062 | 1271 | declaration_scope, |
ab4cf058 | 1272 | trace); |
78af2bcd MD |
1273 | |
1274 | case TYPESPEC_VOID: | |
1275 | case TYPESPEC_CHAR: | |
1276 | case TYPESPEC_SHORT: | |
1277 | case TYPESPEC_INT: | |
1278 | case TYPESPEC_LONG: | |
1279 | case TYPESPEC_FLOAT: | |
1280 | case TYPESPEC_DOUBLE: | |
1281 | case TYPESPEC_SIGNED: | |
1282 | case TYPESPEC_UNSIGNED: | |
1283 | case TYPESPEC_BOOL: | |
1284 | case TYPESPEC_COMPLEX: | |
1285 | case TYPESPEC_IMAGINARY: | |
1286 | case TYPESPEC_CONST: | |
1287 | case TYPESPEC_ID_TYPE: | |
add40b62 | 1288 | return ctf_declaration_type_specifier_visit(fd, depth, |
78af2bcd | 1289 | type_specifier_list, declaration_scope); |
a0720417 | 1290 | default: |
78af2bcd | 1291 | fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) first->u.type_specifier.type); |
a0720417 | 1292 | return NULL; |
add40b62 | 1293 | } |
05628561 MD |
1294 | } |
1295 | ||
1296 | static | |
1297 | int ctf_event_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_event *event, struct ctf_trace *trace) | |
1298 | { | |
1299 | int ret = 0; | |
1300 | ||
1301 | switch (node->type) { | |
1302 | case NODE_TYPEDEF: | |
1303 | ret = ctf_typedef_visit(fd, depth + 1, | |
a0720417 | 1304 | event->declaration_scope, |
78af2bcd | 1305 | node->u._typedef.type_specifier_list, |
05628561 | 1306 | &node->u._typedef.type_declarators, |
a0720417 | 1307 | trace); |
05628561 MD |
1308 | if (ret) |
1309 | return ret; | |
1310 | break; | |
1311 | case NODE_TYPEALIAS: | |
1312 | ret = ctf_typealias_visit(fd, depth + 1, | |
a0720417 MD |
1313 | event->declaration_scope, |
1314 | node->u.typealias.target, node->u.typealias.alias, | |
1315 | trace); | |
05628561 MD |
1316 | if (ret) |
1317 | return ret; | |
1318 | break; | |
1319 | case NODE_CTF_EXPRESSION: | |
1320 | { | |
1321 | char *left; | |
1322 | ||
1323 | left = concatenate_unary_strings(&node->u.ctf_expression.left); | |
1324 | if (!strcmp(left, "name")) { | |
1325 | char *right; | |
1326 | ||
427c09b7 MD |
1327 | if (CTF_EVENT_FIELD_IS_SET(event, name)) { |
1328 | fprintf(fd, "[error] %s: name already declared in event declaration\n", __func__); | |
0f980a35 MD |
1329 | ret = -EPERM; |
1330 | goto error; | |
427c09b7 | 1331 | } |
05628561 MD |
1332 | right = concatenate_unary_strings(&node->u.ctf_expression.right); |
1333 | if (!right) { | |
78af2bcd | 1334 | fprintf(fd, "[error] %s: unexpected unary expression for event name\n", __func__); |
0f980a35 MD |
1335 | ret = -EINVAL; |
1336 | goto error; | |
05628561 MD |
1337 | } |
1338 | event->name = g_quark_from_string(right); | |
41253107 | 1339 | g_free(right); |
05628561 MD |
1340 | CTF_EVENT_SET_FIELD(event, name); |
1341 | } else if (!strcmp(left, "id")) { | |
427c09b7 MD |
1342 | if (CTF_EVENT_FIELD_IS_SET(event, id)) { |
1343 | fprintf(fd, "[error] %s: id already declared in event declaration\n", __func__); | |
0f980a35 MD |
1344 | ret = -EPERM; |
1345 | goto error; | |
427c09b7 | 1346 | } |
05628561 MD |
1347 | ret = get_unary_unsigned(&node->u.ctf_expression.right, &event->id); |
1348 | if (ret) { | |
78af2bcd | 1349 | fprintf(fd, "[error] %s: unexpected unary expression for event id\n", __func__); |
0f980a35 MD |
1350 | ret = -EINVAL; |
1351 | goto error; | |
05628561 MD |
1352 | } |
1353 | CTF_EVENT_SET_FIELD(event, id); | |
1354 | } else if (!strcmp(left, "stream_id")) { | |
427c09b7 MD |
1355 | if (CTF_EVENT_FIELD_IS_SET(event, stream_id)) { |
1356 | fprintf(fd, "[error] %s: stream_id already declared in event declaration\n", __func__); | |
0f980a35 MD |
1357 | ret = -EPERM; |
1358 | goto error; | |
427c09b7 | 1359 | } |
05628561 MD |
1360 | ret = get_unary_unsigned(&node->u.ctf_expression.right, &event->stream_id); |
1361 | if (ret) { | |
78af2bcd | 1362 | fprintf(fd, "[error] %s: unexpected unary expression for event stream_id\n", __func__); |
0f980a35 MD |
1363 | ret = -EINVAL; |
1364 | goto error; | |
05628561 MD |
1365 | } |
1366 | event->stream = trace_stream_lookup(trace, event->stream_id); | |
1367 | if (!event->stream) { | |
78af2bcd | 1368 | fprintf(fd, "[error] %s: stream id %" PRIu64 " cannot be found\n", __func__, event->stream_id); |
0f980a35 MD |
1369 | ret = -EINVAL; |
1370 | goto error; | |
05628561 | 1371 | } |
05628561 MD |
1372 | CTF_EVENT_SET_FIELD(event, stream_id); |
1373 | } else if (!strcmp(left, "context")) { | |
1374 | struct declaration *declaration; | |
1375 | ||
427c09b7 MD |
1376 | if (event->context_decl) { |
1377 | fprintf(fd, "[error] %s: context already declared in event declaration\n", __func__); | |
0f980a35 MD |
1378 | ret = -EINVAL; |
1379 | goto error; | |
427c09b7 | 1380 | } |
78af2bcd MD |
1381 | declaration = ctf_type_specifier_list_visit(fd, depth, |
1382 | _cds_list_first_entry(&node->u.ctf_expression.right, | |
1383 | struct ctf_node, siblings), | |
ab4cf058 | 1384 | event->declaration_scope, trace); |
0f980a35 MD |
1385 | if (!declaration) { |
1386 | ret = -EPERM; | |
1387 | goto error; | |
1388 | } | |
1389 | if (declaration->id != CTF_TYPE_STRUCT) { | |
1390 | ret = -EPERM; | |
1391 | goto error; | |
1392 | } | |
9e29e16e | 1393 | event->context_decl = container_of(declaration, struct declaration_struct, p); |
05628561 MD |
1394 | } else if (!strcmp(left, "fields")) { |
1395 | struct declaration *declaration; | |
1396 | ||
427c09b7 MD |
1397 | if (event->fields_decl) { |
1398 | fprintf(fd, "[error] %s: fields already declared in event declaration\n", __func__); | |
0f980a35 MD |
1399 | ret = -EINVAL; |
1400 | goto error; | |
427c09b7 | 1401 | } |
78af2bcd MD |
1402 | declaration = ctf_type_specifier_list_visit(fd, depth, |
1403 | _cds_list_first_entry(&node->u.ctf_expression.right, | |
1404 | struct ctf_node, siblings), | |
ab4cf058 | 1405 | event->declaration_scope, trace); |
0f980a35 MD |
1406 | if (!declaration) { |
1407 | ret = -EPERM; | |
1408 | goto error; | |
1409 | } | |
1410 | if (declaration->id != CTF_TYPE_STRUCT) { | |
1411 | ret = -EPERM; | |
1412 | goto error; | |
1413 | } | |
9e29e16e | 1414 | event->fields_decl = container_of(declaration, struct declaration_struct, p); |
05628561 | 1415 | } |
0f980a35 | 1416 | error: |
41253107 | 1417 | g_free(left); |
05628561 MD |
1418 | break; |
1419 | } | |
1420 | default: | |
1421 | return -EPERM; | |
1422 | /* TODO: declaration specifier should be added. */ | |
1423 | } | |
1424 | ||
0f980a35 | 1425 | return ret; |
05628561 MD |
1426 | } |
1427 | ||
1428 | static | |
1429 | int ctf_event_visit(FILE *fd, int depth, struct ctf_node *node, | |
d20f5e59 | 1430 | struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace) |
05628561 MD |
1431 | { |
1432 | int ret = 0; | |
1433 | struct ctf_node *iter; | |
1434 | struct ctf_event *event; | |
9e29e16e | 1435 | struct definition_scope *parent_def_scope; |
05628561 MD |
1436 | |
1437 | event = g_new0(struct ctf_event, 1); | |
d20f5e59 | 1438 | event->declaration_scope = new_declaration_scope(parent_declaration_scope); |
05628561 MD |
1439 | cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) { |
1440 | ret = ctf_event_declaration_visit(fd, depth + 1, iter, event, trace); | |
1441 | if (ret) | |
1442 | goto error; | |
1443 | } | |
1444 | if (!CTF_EVENT_FIELD_IS_SET(event, name)) { | |
1445 | ret = -EPERM; | |
427c09b7 | 1446 | fprintf(fd, "[error] %s: missing name field in event declaration\n", __func__); |
05628561 MD |
1447 | goto error; |
1448 | } | |
05628561 | 1449 | if (!CTF_EVENT_FIELD_IS_SET(event, stream_id)) { |
0f980a35 | 1450 | /* Allow missing stream_id if there is only a single stream */ |
33105c61 MD |
1451 | switch (trace->streams->len) { |
1452 | case 0: /* Create stream if there was none. */ | |
1453 | ret = ctf_stream_visit(fd, depth, NULL, trace->root_declaration_scope, trace); | |
1454 | if (ret) | |
1455 | goto error; | |
1456 | /* Fall-through */ | |
1457 | case 1: | |
0f980a35 MD |
1458 | event->stream_id = 0; |
1459 | event->stream = trace_stream_lookup(trace, event->stream_id); | |
33105c61 MD |
1460 | break; |
1461 | default: | |
0f980a35 MD |
1462 | ret = -EPERM; |
1463 | fprintf(fd, "[error] %s: missing stream_id field in event declaration\n", __func__); | |
1464 | goto error; | |
1465 | } | |
05628561 | 1466 | } |
8c572eba MD |
1467 | /* Allow only one event without id per stream */ |
1468 | if (!CTF_EVENT_FIELD_IS_SET(event, id) | |
1469 | && event->stream->events_by_id->len != 0) { | |
1470 | ret = -EPERM; | |
1471 | fprintf(fd, "[error] %s: missing id field in event declaration\n", __func__); | |
1472 | goto error; | |
1473 | } | |
05628561 MD |
1474 | if (event->stream->events_by_id->len <= event->id) |
1475 | g_ptr_array_set_size(event->stream->events_by_id, event->id + 1); | |
1476 | g_ptr_array_index(event->stream->events_by_id, event->id) = event; | |
1477 | g_hash_table_insert(event->stream->event_quark_to_id, | |
1478 | (gpointer)(unsigned long) event->name, | |
1479 | &event->id); | |
a0720417 | 1480 | parent_def_scope = event->stream->definition_scope; |
9e29e16e MD |
1481 | if (event->context_decl) { |
1482 | event->context = | |
a0720417 MD |
1483 | container_of( |
1484 | event->context_decl->p.definition_new(&event->context_decl->p, | |
1485 | parent_def_scope, 0, 0), | |
1486 | struct definition_struct, p); | |
6a36ddca MD |
1487 | set_dynamic_definition_scope(&event->context->p, |
1488 | event->context->scope, | |
d00d17d1 | 1489 | "event.context"); |
9e29e16e | 1490 | parent_def_scope = event->context->scope; |
a0720417 | 1491 | declaration_unref(&event->context_decl->p); |
9e29e16e MD |
1492 | } |
1493 | if (event->fields_decl) { | |
1494 | event->fields = | |
a0720417 MD |
1495 | container_of( |
1496 | event->fields_decl->p.definition_new(&event->fields_decl->p, | |
1497 | parent_def_scope, 0, 0), | |
1498 | struct definition_struct, p); | |
6a36ddca MD |
1499 | set_dynamic_definition_scope(&event->fields->p, |
1500 | event->fields->scope, | |
d00d17d1 | 1501 | "event.fields"); |
9e29e16e | 1502 | parent_def_scope = event->fields->scope; |
a0720417 | 1503 | declaration_unref(&event->fields_decl->p); |
9e29e16e | 1504 | } |
05628561 MD |
1505 | return 0; |
1506 | ||
1507 | error: | |
a0720417 MD |
1508 | declaration_unref(&event->fields_decl->p); |
1509 | declaration_unref(&event->context_decl->p); | |
d20f5e59 | 1510 | free_declaration_scope(event->declaration_scope); |
05628561 MD |
1511 | g_free(event); |
1512 | return ret; | |
1513 | } | |
1514 | ||
1515 | ||
1516 | static | |
1517 | int ctf_stream_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_stream *stream, struct ctf_trace *trace) | |
1518 | { | |
1519 | int ret = 0; | |
1520 | ||
1521 | switch (node->type) { | |
1522 | case NODE_TYPEDEF: | |
1523 | ret = ctf_typedef_visit(fd, depth + 1, | |
a0720417 | 1524 | stream->declaration_scope, |
78af2bcd | 1525 | node->u._typedef.type_specifier_list, |
05628561 | 1526 | &node->u._typedef.type_declarators, |
a0720417 | 1527 | trace); |
05628561 MD |
1528 | if (ret) |
1529 | return ret; | |
1530 | break; | |
1531 | case NODE_TYPEALIAS: | |
1532 | ret = ctf_typealias_visit(fd, depth + 1, | |
a0720417 MD |
1533 | stream->declaration_scope, |
1534 | node->u.typealias.target, node->u.typealias.alias, | |
1535 | trace); | |
05628561 MD |
1536 | if (ret) |
1537 | return ret; | |
1538 | break; | |
1539 | case NODE_CTF_EXPRESSION: | |
1540 | { | |
1541 | char *left; | |
1542 | ||
1543 | left = concatenate_unary_strings(&node->u.ctf_expression.left); | |
427c09b7 MD |
1544 | if (!strcmp(left, "id")) { |
1545 | if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) { | |
1546 | fprintf(fd, "[error] %s: id already declared in stream declaration\n", __func__); | |
0f980a35 MD |
1547 | ret = -EPERM; |
1548 | goto error; | |
427c09b7 | 1549 | } |
a0720417 | 1550 | ret = get_unary_unsigned(&node->u.ctf_expression.right, &stream->stream_id); |
05628561 | 1551 | if (ret) { |
427c09b7 | 1552 | fprintf(fd, "[error] %s: unexpected unary expression for stream id\n", __func__); |
0f980a35 MD |
1553 | ret = -EINVAL; |
1554 | goto error; | |
05628561 | 1555 | } |
a0720417 | 1556 | CTF_STREAM_SET_FIELD(stream, stream_id); |
9e29e16e | 1557 | } else if (!strcmp(left, "event.header")) { |
05628561 MD |
1558 | struct declaration *declaration; |
1559 | ||
427c09b7 MD |
1560 | if (stream->event_header_decl) { |
1561 | fprintf(fd, "[error] %s: event.header already declared in stream declaration\n", __func__); | |
0f980a35 MD |
1562 | ret = -EINVAL; |
1563 | goto error; | |
427c09b7 | 1564 | } |
78af2bcd MD |
1565 | declaration = ctf_type_specifier_list_visit(fd, depth, |
1566 | _cds_list_first_entry(&node->u.ctf_expression.right, | |
1567 | struct ctf_node, siblings), | |
a0720417 | 1568 | stream->declaration_scope, trace); |
0f980a35 MD |
1569 | if (!declaration) { |
1570 | ret = -EPERM; | |
1571 | goto error; | |
1572 | } | |
1573 | if (declaration->id != CTF_TYPE_STRUCT) { | |
1574 | ret = -EPERM; | |
1575 | goto error; | |
1576 | } | |
9e29e16e MD |
1577 | stream->event_header_decl = container_of(declaration, struct declaration_struct, p); |
1578 | } else if (!strcmp(left, "event.context")) { | |
05628561 MD |
1579 | struct declaration *declaration; |
1580 | ||
427c09b7 MD |
1581 | if (stream->event_context_decl) { |
1582 | fprintf(fd, "[error] %s: event.context already declared in stream declaration\n", __func__); | |
0f980a35 MD |
1583 | ret = -EINVAL; |
1584 | goto error; | |
427c09b7 | 1585 | } |
78af2bcd MD |
1586 | declaration = ctf_type_specifier_list_visit(fd, depth, |
1587 | _cds_list_first_entry(&node->u.ctf_expression.right, | |
1588 | struct ctf_node, siblings), | |
ab4cf058 | 1589 | stream->declaration_scope, trace); |
0f980a35 MD |
1590 | if (!declaration) { |
1591 | ret = -EPERM; | |
1592 | goto error; | |
1593 | } | |
1594 | if (declaration->id != CTF_TYPE_STRUCT) { | |
1595 | ret = -EPERM; | |
1596 | goto error; | |
1597 | } | |
9e29e16e MD |
1598 | stream->event_context_decl = container_of(declaration, struct declaration_struct, p); |
1599 | } else if (!strcmp(left, "packet.context")) { | |
05628561 MD |
1600 | struct declaration *declaration; |
1601 | ||
427c09b7 MD |
1602 | if (stream->packet_context_decl) { |
1603 | fprintf(fd, "[error] %s: packet.context already declared in stream declaration\n", __func__); | |
0f980a35 MD |
1604 | ret = -EINVAL; |
1605 | goto error; | |
427c09b7 | 1606 | } |
78af2bcd MD |
1607 | declaration = ctf_type_specifier_list_visit(fd, depth, |
1608 | _cds_list_first_entry(&node->u.ctf_expression.right, | |
1609 | struct ctf_node, siblings), | |
ab4cf058 | 1610 | stream->declaration_scope, trace); |
0f980a35 MD |
1611 | if (!declaration) { |
1612 | ret = -EPERM; | |
1613 | goto error; | |
1614 | } | |
1615 | if (declaration->id != CTF_TYPE_STRUCT) { | |
1616 | ret = -EPERM; | |
1617 | goto error; | |
1618 | } | |
9e29e16e | 1619 | stream->packet_context_decl = container_of(declaration, struct declaration_struct, p); |
05628561 | 1620 | } |
0f980a35 | 1621 | error: |
41253107 | 1622 | g_free(left); |
05628561 MD |
1623 | break; |
1624 | } | |
1625 | default: | |
1626 | return -EPERM; | |
1627 | /* TODO: declaration specifier should be added. */ | |
1628 | } | |
1629 | ||
0f980a35 | 1630 | return ret; |
05628561 MD |
1631 | } |
1632 | ||
1633 | static | |
1634 | int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node, | |
d20f5e59 | 1635 | struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace) |
05628561 MD |
1636 | { |
1637 | int ret = 0; | |
1638 | struct ctf_node *iter; | |
1639 | struct ctf_stream *stream; | |
9e29e16e | 1640 | struct definition_scope *parent_def_scope; |
05628561 MD |
1641 | |
1642 | stream = g_new0(struct ctf_stream, 1); | |
d20f5e59 | 1643 | stream->declaration_scope = new_declaration_scope(parent_declaration_scope); |
05628561 | 1644 | stream->events_by_id = g_ptr_array_new(); |
068665f5 | 1645 | stream->event_quark_to_id = g_hash_table_new(g_direct_hash, g_direct_equal); |
0f980a35 | 1646 | stream->files = g_ptr_array_new(); |
33105c61 MD |
1647 | if (node) { |
1648 | cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) { | |
1649 | ret = ctf_stream_declaration_visit(fd, depth + 1, iter, stream, trace); | |
1650 | if (ret) | |
1651 | goto error; | |
1652 | } | |
05628561 | 1653 | } |
0f980a35 MD |
1654 | if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) { |
1655 | /* check that packet header has stream_id field. */ | |
1656 | if (!trace->packet_header_decl | |
1657 | || struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("stream_id")) < 0) { | |
1658 | ret = -EPERM; | |
1659 | fprintf(fd, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__); | |
1660 | goto error; | |
1661 | } | |
8c572eba MD |
1662 | } else { |
1663 | /* Allow only one id-less stream */ | |
1664 | if (trace->streams->len != 0) { | |
1665 | ret = -EPERM; | |
1666 | fprintf(fd, "[error] %s: missing id field in stream declaration\n", __func__); | |
1667 | goto error; | |
1668 | } | |
1669 | stream->stream_id = 0; | |
05628561 MD |
1670 | } |
1671 | if (trace->streams->len <= stream->stream_id) | |
1672 | g_ptr_array_set_size(trace->streams, stream->stream_id + 1); | |
1673 | g_ptr_array_index(trace->streams, stream->stream_id) = stream; | |
9e29e16e | 1674 | |
0f980a35 | 1675 | parent_def_scope = trace->definition_scope; |
9e29e16e MD |
1676 | if (stream->packet_context_decl) { |
1677 | stream->packet_context = | |
a0720417 MD |
1678 | container_of( |
1679 | stream->packet_context_decl->p.definition_new(&stream->packet_context_decl->p, | |
1680 | parent_def_scope, 0, 0), | |
1681 | struct definition_struct, p); | |
6a36ddca MD |
1682 | set_dynamic_definition_scope(&stream->packet_context->p, |
1683 | stream->packet_context->scope, | |
d00d17d1 | 1684 | "stream.packet.context"); |
9e29e16e | 1685 | parent_def_scope = stream->packet_context->scope; |
a0720417 | 1686 | declaration_unref(&stream->packet_context_decl->p); |
9e29e16e MD |
1687 | } |
1688 | if (stream->event_header_decl) { | |
1689 | stream->event_header = | |
a0720417 MD |
1690 | container_of( |
1691 | stream->event_header_decl->p.definition_new(&stream->event_header_decl->p, | |
1692 | parent_def_scope, 0, 0), | |
1693 | struct definition_struct, p); | |
6a36ddca MD |
1694 | set_dynamic_definition_scope(&stream->event_header->p, |
1695 | stream->event_header->scope, | |
d00d17d1 | 1696 | "stream.event.header"); |
9e29e16e | 1697 | parent_def_scope = stream->event_header->scope; |
a0720417 | 1698 | declaration_unref(&stream->event_header_decl->p); |
9e29e16e MD |
1699 | } |
1700 | if (stream->event_context_decl) { | |
1701 | stream->event_context = | |
a0720417 MD |
1702 | container_of( |
1703 | stream->event_context_decl->p.definition_new(&stream->event_context_decl->p, | |
1704 | parent_def_scope, 0, 0), | |
1705 | struct definition_struct, p); | |
6a36ddca MD |
1706 | set_dynamic_definition_scope(&stream->event_context->p, |
1707 | stream->event_context->scope, | |
d00d17d1 | 1708 | "stream.event.context"); |
9e29e16e | 1709 | parent_def_scope = stream->event_context->scope; |
a0720417 | 1710 | declaration_unref(&stream->event_context_decl->p); |
9e29e16e | 1711 | } |
41253107 | 1712 | stream->definition_scope = parent_def_scope; |
9e29e16e | 1713 | |
05628561 MD |
1714 | return 0; |
1715 | ||
1716 | error: | |
a0720417 MD |
1717 | declaration_unref(&stream->event_header_decl->p); |
1718 | declaration_unref(&stream->event_context_decl->p); | |
1719 | declaration_unref(&stream->packet_context_decl->p); | |
0f980a35 | 1720 | g_ptr_array_free(stream->files, TRUE); |
05628561 | 1721 | g_ptr_array_free(stream->events_by_id, TRUE); |
a0720417 | 1722 | g_hash_table_destroy(stream->event_quark_to_id); |
d20f5e59 | 1723 | free_declaration_scope(stream->declaration_scope); |
05628561 MD |
1724 | g_free(stream); |
1725 | return ret; | |
1726 | } | |
1727 | ||
1728 | static | |
1729 | int ctf_trace_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace) | |
1730 | { | |
1731 | int ret = 0; | |
1732 | ||
1733 | switch (node->type) { | |
1734 | case NODE_TYPEDEF: | |
1735 | ret = ctf_typedef_visit(fd, depth + 1, | |
a0720417 | 1736 | trace->declaration_scope, |
78af2bcd | 1737 | node->u._typedef.type_specifier_list, |
05628561 | 1738 | &node->u._typedef.type_declarators, |
a0720417 | 1739 | trace); |
05628561 MD |
1740 | if (ret) |
1741 | return ret; | |
1742 | break; | |
1743 | case NODE_TYPEALIAS: | |
1744 | ret = ctf_typealias_visit(fd, depth + 1, | |
a0720417 MD |
1745 | trace->declaration_scope, |
1746 | node->u.typealias.target, node->u.typealias.alias, | |
1747 | trace); | |
05628561 MD |
1748 | if (ret) |
1749 | return ret; | |
1750 | break; | |
1751 | case NODE_CTF_EXPRESSION: | |
1752 | { | |
1753 | char *left; | |
1754 | ||
1755 | left = concatenate_unary_strings(&node->u.ctf_expression.left); | |
1756 | if (!strcmp(left, "major")) { | |
427c09b7 MD |
1757 | if (CTF_TRACE_FIELD_IS_SET(trace, major)) { |
1758 | fprintf(fd, "[error] %s: major already declared in trace declaration\n", __func__); | |
0f980a35 MD |
1759 | ret = -EPERM; |
1760 | goto error; | |
427c09b7 | 1761 | } |
05628561 MD |
1762 | ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->major); |
1763 | if (ret) { | |
78af2bcd | 1764 | fprintf(fd, "[error] %s: unexpected unary expression for trace major number\n", __func__); |
0f980a35 MD |
1765 | ret = -EINVAL; |
1766 | goto error; | |
05628561 | 1767 | } |
a0720417 | 1768 | CTF_TRACE_SET_FIELD(trace, major); |
05628561 | 1769 | } else if (!strcmp(left, "minor")) { |
427c09b7 MD |
1770 | if (CTF_TRACE_FIELD_IS_SET(trace, minor)) { |
1771 | fprintf(fd, "[error] %s: minor already declared in trace declaration\n", __func__); | |
0f980a35 MD |
1772 | ret = -EPERM; |
1773 | goto error; | |
427c09b7 | 1774 | } |
05628561 MD |
1775 | ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->minor); |
1776 | if (ret) { | |
78af2bcd | 1777 | fprintf(fd, "[error] %s: unexpected unary expression for trace minor number\n", __func__); |
0f980a35 MD |
1778 | ret = -EINVAL; |
1779 | goto error; | |
05628561 | 1780 | } |
a0720417 | 1781 | CTF_TRACE_SET_FIELD(trace, minor); |
05628561 | 1782 | } else if (!strcmp(left, "uuid")) { |
427c09b7 MD |
1783 | if (CTF_TRACE_FIELD_IS_SET(trace, uuid)) { |
1784 | fprintf(fd, "[error] %s: uuid already declared in trace declaration\n", __func__); | |
0f980a35 MD |
1785 | ret = -EPERM; |
1786 | goto error; | |
427c09b7 | 1787 | } |
05628561 MD |
1788 | ret = get_unary_uuid(&node->u.ctf_expression.right, &trace->uuid); |
1789 | if (ret) { | |
78af2bcd | 1790 | fprintf(fd, "[error] %s: unexpected unary expression for trace uuid\n", __func__); |
0f980a35 MD |
1791 | ret = -EINVAL; |
1792 | goto error; | |
05628561 | 1793 | } |
a0720417 | 1794 | CTF_TRACE_SET_FIELD(trace, uuid); |
0f980a35 MD |
1795 | } else if (!strcmp(left, "byte_order")) { |
1796 | struct ctf_node *right; | |
1797 | int byte_order; | |
1798 | ||
1799 | if (CTF_TRACE_FIELD_IS_SET(trace, byte_order)) { | |
1800 | fprintf(fd, "[error] %s: endianness already declared in trace declaration\n", __func__); | |
1801 | ret = -EPERM; | |
1802 | goto error; | |
1803 | } | |
1804 | right = _cds_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings); | |
1805 | byte_order = get_trace_byte_order(fd, depth, right); | |
1806 | if (byte_order < 0) | |
1807 | return -EINVAL; | |
1808 | trace->byte_order = byte_order; | |
1809 | CTF_TRACE_SET_FIELD(trace, byte_order); | |
1810 | } else if (!strcmp(left, "packet.header")) { | |
1811 | struct declaration *declaration; | |
1812 | ||
1813 | if (trace->packet_header_decl) { | |
1814 | fprintf(fd, "[error] %s: packet.header already declared in trace declaration\n", __func__); | |
1815 | ret = -EINVAL; | |
1816 | goto error; | |
1817 | } | |
1818 | declaration = ctf_type_specifier_list_visit(fd, depth, | |
1819 | _cds_list_first_entry(&node->u.ctf_expression.right, | |
1820 | struct ctf_node, siblings), | |
1821 | trace->declaration_scope, trace); | |
1822 | if (!declaration) { | |
1823 | ret = -EPERM; | |
1824 | goto error; | |
1825 | } | |
1826 | if (declaration->id != CTF_TYPE_STRUCT) { | |
1827 | ret = -EPERM; | |
1828 | goto error; | |
1829 | } | |
1830 | trace->packet_header_decl = container_of(declaration, struct declaration_struct, p); | |
05628561 | 1831 | } |
0f980a35 | 1832 | error: |
41253107 | 1833 | g_free(left); |
05628561 MD |
1834 | break; |
1835 | } | |
1836 | default: | |
1837 | return -EPERM; | |
1838 | /* TODO: declaration specifier should be added. */ | |
1839 | } | |
1840 | ||
0f980a35 | 1841 | return ret; |
05628561 MD |
1842 | } |
1843 | ||
05628561 MD |
1844 | static |
1845 | int ctf_trace_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace) | |
1846 | { | |
0f980a35 | 1847 | struct definition_scope *parent_def_scope; |
05628561 MD |
1848 | int ret = 0; |
1849 | struct ctf_node *iter; | |
1850 | ||
d20f5e59 | 1851 | if (trace->declaration_scope) |
05628561 | 1852 | return -EEXIST; |
d20f5e59 | 1853 | trace->declaration_scope = new_declaration_scope(trace->root_declaration_scope); |
05628561 MD |
1854 | trace->streams = g_ptr_array_new(); |
1855 | cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) { | |
1856 | ret = ctf_trace_declaration_visit(fd, depth + 1, iter, trace); | |
1857 | if (ret) | |
1858 | goto error; | |
1859 | } | |
a0720417 | 1860 | if (!CTF_TRACE_FIELD_IS_SET(trace, major)) { |
05628561 | 1861 | ret = -EPERM; |
427c09b7 | 1862 | fprintf(fd, "[error] %s: missing major field in trace declaration\n", __func__); |
05628561 MD |
1863 | goto error; |
1864 | } | |
a0720417 | 1865 | if (!CTF_TRACE_FIELD_IS_SET(trace, minor)) { |
05628561 | 1866 | ret = -EPERM; |
427c09b7 | 1867 | fprintf(fd, "[error] %s: missing minor field in trace declaration\n", __func__); |
05628561 MD |
1868 | goto error; |
1869 | } | |
a0720417 | 1870 | if (!CTF_TRACE_FIELD_IS_SET(trace, uuid)) { |
05628561 | 1871 | ret = -EPERM; |
427c09b7 | 1872 | fprintf(fd, "[error] %s: missing uuid field in trace declaration\n", __func__); |
05628561 MD |
1873 | goto error; |
1874 | } | |
dc48ecad MD |
1875 | if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) { |
1876 | ret = -EPERM; | |
1877 | fprintf(fd, "[error] %s: missing byte_order field in trace declaration\n", __func__); | |
1878 | goto error; | |
1879 | } | |
0f980a35 MD |
1880 | |
1881 | parent_def_scope = NULL; | |
1882 | if (trace->packet_header_decl) { | |
1883 | trace->packet_header = | |
1884 | container_of( | |
1885 | trace->packet_header_decl->p.definition_new(&trace->packet_header_decl->p, | |
1886 | parent_def_scope, 0, 0), | |
1887 | struct definition_struct, p); | |
1888 | set_dynamic_definition_scope(&trace->packet_header->p, | |
1889 | trace->packet_header->scope, | |
1890 | "trace.packet.header"); | |
1891 | parent_def_scope = trace->packet_header->scope; | |
1892 | declaration_unref(&trace->packet_header_decl->p); | |
1893 | } | |
1894 | trace->definition_scope = parent_def_scope; | |
1895 | ||
1896 | if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) { | |
1897 | /* check that the packet header contains a "magic" field */ | |
1898 | if (!trace->packet_header | |
1899 | || struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("magic")) < 0) { | |
1900 | ret = -EPERM; | |
1901 | fprintf(fd, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__); | |
1902 | goto error_free_def; | |
1903 | } | |
1904 | } | |
05628561 MD |
1905 | return 0; |
1906 | ||
0f980a35 MD |
1907 | error_free_def: |
1908 | definition_unref(&trace->packet_header->p); | |
05628561 MD |
1909 | error: |
1910 | g_ptr_array_free(trace->streams, TRUE); | |
a0720417 | 1911 | free_declaration_scope(trace->declaration_scope); |
05628561 MD |
1912 | return ret; |
1913 | } | |
1914 | ||
78af2bcd MD |
1915 | static |
1916 | int ctf_root_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace) | |
1917 | { | |
1918 | int ret = 0; | |
1919 | ||
1920 | switch (node->type) { | |
1921 | case NODE_TYPEDEF: | |
1922 | ret = ctf_typedef_visit(fd, depth + 1, | |
1923 | trace->root_declaration_scope, | |
1924 | node->u._typedef.type_specifier_list, | |
1925 | &node->u._typedef.type_declarators, | |
1926 | trace); | |
1927 | if (ret) | |
1928 | return ret; | |
1929 | break; | |
1930 | case NODE_TYPEALIAS: | |
1931 | ret = ctf_typealias_visit(fd, depth + 1, | |
1932 | trace->root_declaration_scope, | |
1933 | node->u.typealias.target, node->u.typealias.alias, | |
1934 | trace); | |
1935 | if (ret) | |
1936 | return ret; | |
1937 | break; | |
1938 | case NODE_TYPE_SPECIFIER_LIST: | |
1939 | { | |
1940 | struct declaration *declaration; | |
1941 | ||
1942 | /* | |
1943 | * Just add the type specifier to the root scope | |
1944 | * declaration scope. Release local reference. | |
1945 | */ | |
1946 | declaration = ctf_type_specifier_list_visit(fd, depth + 1, | |
1947 | node, trace->root_declaration_scope, trace); | |
1948 | if (!declaration) | |
1949 | return -ENOMEM; | |
1950 | declaration_unref(declaration); | |
1951 | break; | |
1952 | } | |
1953 | default: | |
1954 | return -EPERM; | |
1955 | } | |
1956 | ||
1957 | return 0; | |
1958 | } | |
1959 | ||
ab4cf058 MD |
1960 | int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node, |
1961 | struct ctf_trace *trace, int byte_order) | |
05628561 MD |
1962 | { |
1963 | int ret = 0; | |
1964 | struct ctf_node *iter; | |
1965 | ||
78af2bcd MD |
1966 | fprintf(fd, "CTF visitor: metadata construction... "); |
1967 | trace->root_declaration_scope = new_declaration_scope(NULL); | |
ab4cf058 MD |
1968 | trace->byte_order = byte_order; |
1969 | ||
05628561 MD |
1970 | switch (node->type) { |
1971 | case NODE_ROOT: | |
3e11b713 | 1972 | cds_list_for_each_entry(iter, &node->u.root.declaration_list, |
05628561 | 1973 | siblings) { |
78af2bcd | 1974 | ret = ctf_root_declaration_visit(fd, depth + 1, iter, trace); |
427c09b7 MD |
1975 | if (ret) { |
1976 | fprintf(fd, "[error] %s: root declaration error\n", __func__); | |
78af2bcd | 1977 | return ret; |
427c09b7 | 1978 | } |
05628561 MD |
1979 | } |
1980 | cds_list_for_each_entry(iter, &node->u.root.trace, siblings) { | |
1981 | ret = ctf_trace_visit(fd, depth + 1, iter, trace); | |
427c09b7 MD |
1982 | if (ret) { |
1983 | fprintf(fd, "[error] %s: trace declaration error\n", __func__); | |
05628561 | 1984 | return ret; |
427c09b7 | 1985 | } |
05628561 | 1986 | } |
5039b4cc MD |
1987 | if (!trace->streams) { |
1988 | fprintf(fd, "[error] %s: missing trace declaration\n", __func__); | |
1989 | return -EINVAL; | |
1990 | } | |
05628561 MD |
1991 | cds_list_for_each_entry(iter, &node->u.root.stream, siblings) { |
1992 | ret = ctf_stream_visit(fd, depth + 1, iter, | |
41253107 | 1993 | trace->root_declaration_scope, trace); |
427c09b7 MD |
1994 | if (ret) { |
1995 | fprintf(fd, "[error] %s: stream declaration error\n", __func__); | |
05628561 | 1996 | return ret; |
427c09b7 | 1997 | } |
05628561 MD |
1998 | } |
1999 | cds_list_for_each_entry(iter, &node->u.root.event, siblings) { | |
2000 | ret = ctf_event_visit(fd, depth + 1, iter, | |
41253107 | 2001 | trace->root_declaration_scope, trace); |
427c09b7 MD |
2002 | if (ret) { |
2003 | fprintf(fd, "[error] %s: event declaration error\n", __func__); | |
05628561 | 2004 | return ret; |
427c09b7 | 2005 | } |
05628561 MD |
2006 | } |
2007 | break; | |
05628561 MD |
2008 | case NODE_UNKNOWN: |
2009 | default: | |
78af2bcd | 2010 | fprintf(fd, "[error] %s: unknown node type %d\n", __func__, |
05628561 MD |
2011 | (int) node->type); |
2012 | return -EINVAL; | |
2013 | } | |
78af2bcd | 2014 | fprintf(fd, "done.\n"); |
05628561 MD |
2015 | return ret; |
2016 | } |