Show loglevel information only with value
[babeltrace.git] / formats / ctf / metadata / ctf-visitor-generate-io-struct.c
CommitLineData
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 27#include <errno.h>
70bd0a12 28#include <babeltrace/babeltrace-internal.h>
05628561 29#include <babeltrace/list.h>
a0720417
MD
30#include <babeltrace/types.h>
31#include <babeltrace/ctf/metadata.h>
41253107 32#include <uuid/uuid.h>
05628561
MD
33#include "ctf-scanner.h"
34#include "ctf-parser.h"
35#include "ctf-ast.h"
36
37#define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
38
de47353a
MD
39#define _cds_list_first_entry(ptr, type, member) \
40 cds_list_entry((ptr)->next, type, member)
05628561 41
a3cca9e9 42static
78af2bcd
MD
43struct declaration *ctf_type_specifier_list_visit(FILE *fd,
44 int depth, struct ctf_node *type_specifier_list,
ab4cf058
MD
45 struct declaration_scope *declaration_scope,
46 struct ctf_trace *trace);
a3cca9e9 47
33105c61
MD
48static
49int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node,
50 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace);
51
05628561 52/*
41253107 53 * String returned must be freed by the caller using g_free.
05628561
MD
54 */
55static
add40b62 56char *concatenate_unary_strings(struct cds_list_head *head)
05628561 57{
41253107
MD
58 struct ctf_node *node;
59 GString *str;
60 int i = 0;
61
a0720417 62 str = g_string_new("");
41253107
MD
63 cds_list_for_each_entry(node, head, siblings) {
64 char *src_string;
65
66 assert(node->type == NODE_UNARY_EXPRESSION);
67 assert(node->u.unary_expression.type == UNARY_STRING);
68 assert((node->u.unary_expression.link == UNARY_LINK_UNKNOWN)
a0720417 69 ^ (i != 0));
41253107
MD
70 switch (node->u.unary_expression.link) {
71 case UNARY_DOTLINK:
a0720417 72 g_string_append(str, ".");
41253107
MD
73 break;
74 case UNARY_ARROWLINK:
a0720417 75 g_string_append(str, "->");
41253107
MD
76 break;
77 case UNARY_DOTDOTDOT:
a0720417
MD
78 g_string_append(str, "...");
79 break;
80 default:
41253107
MD
81 break;
82 }
a0720417 83 src_string = node->u.unary_expression.u.string;
41253107
MD
84 g_string_append(str, src_string);
85 i++;
86 }
87 return g_string_free(str, FALSE);
05628561
MD
88}
89
56e60373
MD
90static
91GQuark get_map_clock_name_value(struct cds_list_head *head)
92{
93 struct ctf_node *node;
94 const char *name = NULL;
95 int i = 0;
96
97 cds_list_for_each_entry(node, head, siblings) {
98 char *src_string;
99
100 assert(node->type == NODE_UNARY_EXPRESSION);
101 assert(node->u.unary_expression.type == UNARY_STRING);
102 assert((node->u.unary_expression.link == UNARY_LINK_UNKNOWN)
103 ^ (i != 0));
104 /* needs to be chained with . */
105 switch (node->u.unary_expression.link) {
106 case UNARY_DOTLINK:
107 break;
108 case UNARY_ARROWLINK:
109 case UNARY_DOTDOTDOT:
110 return 0;
111 default:
112 break;
113 }
114 src_string = node->u.unary_expression.u.string;
115 switch (i) {
116 case 0: if (strcmp("clock", src_string) != 0) {
117 return 0;
118 }
119 break;
120 case 1: name = src_string;
121 break;
122 case 2: if (strcmp("value", src_string) != 0) {
123 return 0;
124 }
125 break;
126 default:
127 return 0; /* extra identifier, unknown */
128 }
129 i++;
130 }
131 return g_quark_from_string(name);
132}
133
05628561 134static
add40b62 135int get_unary_unsigned(struct cds_list_head *head, uint64_t *value)
05628561 136{
41253107
MD
137 struct ctf_node *node;
138 int i = 0;
139
140 cds_list_for_each_entry(node, head, siblings) {
141 assert(node->type == NODE_UNARY_EXPRESSION);
142 assert(node->u.unary_expression.type == UNARY_UNSIGNED_CONSTANT);
143 assert(node->u.unary_expression.link == UNARY_LINK_UNKNOWN);
144 assert(i == 0);
a0720417 145 *value = node->u.unary_expression.u.unsigned_constant;
41253107
MD
146 i++;
147 }
148 return 0;
05628561
MD
149}
150
d86d62f8
MD
151static
152int get_unary_signed(struct cds_list_head *head, int64_t *value)
153{
154 struct ctf_node *node;
155 int i = 0;
156
157 cds_list_for_each_entry(node, head, siblings) {
158 assert(node->type == NODE_UNARY_EXPRESSION);
159 assert(node->u.unary_expression.type == UNARY_UNSIGNED_CONSTANT);
160 assert(node->u.unary_expression.link == UNARY_LINK_UNKNOWN);
161 assert(i == 0);
162 *value = node->u.unary_expression.u.signed_constant;
163 i++;
164 }
165 return 0;
166}
167
05628561 168static
add40b62 169int get_unary_uuid(struct cds_list_head *head, uuid_t *uuid)
05628561 170{
41253107
MD
171 struct ctf_node *node;
172 int i = 0;
173 int ret = -1;
174
175 cds_list_for_each_entry(node, head, siblings) {
176 const char *src_string;
177
178 assert(node->type == NODE_UNARY_EXPRESSION);
179 assert(node->u.unary_expression.type == UNARY_STRING);
180 assert(node->u.unary_expression.link == UNARY_LINK_UNKNOWN);
181 assert(i == 0);
a0720417 182 src_string = node->u.unary_expression.u.string;
5c551b40 183 ret = uuid_parse(src_string, *uuid);
41253107
MD
184 }
185 return ret;
05628561
MD
186}
187
188static
aa6bffae 189struct ctf_stream_class *trace_stream_lookup(struct ctf_trace *trace, uint64_t stream_id)
05628561
MD
190{
191 if (trace->streams->len <= stream_id)
192 return NULL;
193 return g_ptr_array_index(trace->streams, stream_id);
194}
195
56e60373
MD
196static
197struct ctf_clock *trace_clock_lookup(struct ctf_trace *trace, GQuark clock_name)
198{
199 return g_hash_table_lookup(trace->clocks, (gpointer) (unsigned long) clock_name);
200}
201
add40b62 202static
78af2bcd
MD
203int visit_type_specifier(FILE *fd, struct ctf_node *type_specifier, GString *str)
204{
205 assert(type_specifier->type == NODE_TYPE_SPECIFIER);
206
207 switch (type_specifier->u.type_specifier.type) {
208 case TYPESPEC_VOID:
209 g_string_append(str, "void");
210 break;
211 case TYPESPEC_CHAR:
212 g_string_append(str, "char");
213 break;
214 case TYPESPEC_SHORT:
215 g_string_append(str, "short");
216 break;
217 case TYPESPEC_INT:
218 g_string_append(str, "int");
219 break;
220 case TYPESPEC_LONG:
221 g_string_append(str, "long");
222 break;
223 case TYPESPEC_FLOAT:
224 g_string_append(str, "float");
225 break;
226 case TYPESPEC_DOUBLE:
227 g_string_append(str, "double");
228 break;
229 case TYPESPEC_SIGNED:
230 g_string_append(str, "signed");
231 break;
232 case TYPESPEC_UNSIGNED:
233 g_string_append(str, "unsigned");
234 break;
235 case TYPESPEC_BOOL:
236 g_string_append(str, "bool");
237 break;
238 case TYPESPEC_COMPLEX:
239 g_string_append(str, "_Complex");
240 break;
241 case TYPESPEC_IMAGINARY:
242 g_string_append(str, "_Imaginary");
243 break;
244 case TYPESPEC_CONST:
245 g_string_append(str, "const");
246 break;
247 case TYPESPEC_ID_TYPE:
248 if (type_specifier->u.type_specifier.id_type)
249 g_string_append(str, type_specifier->u.type_specifier.id_type);
250 break;
251 case TYPESPEC_STRUCT:
252 {
253 struct ctf_node *node = type_specifier->u.type_specifier.node;
254
255 if (!node->u._struct.name) {
256 fprintf(fd, "[error] %s: unexpected empty variant name\n", __func__);
257 return -EINVAL;
258 }
259 g_string_append(str, "struct ");
260 g_string_append(str, node->u._struct.name);
261 break;
262 }
263 case TYPESPEC_VARIANT:
264 {
265 struct ctf_node *node = type_specifier->u.type_specifier.node;
266
267 if (!node->u.variant.name) {
268 fprintf(fd, "[error] %s: unexpected empty variant name\n", __func__);
269 return -EINVAL;
270 }
271 g_string_append(str, "variant ");
272 g_string_append(str, node->u.variant.name);
273 break;
274 }
275 case TYPESPEC_ENUM:
276 {
277 struct ctf_node *node = type_specifier->u.type_specifier.node;
278
279 if (!node->u._enum.enum_id) {
280 fprintf(fd, "[error] %s: unexpected empty enum ID\n", __func__);
281 return -EINVAL;
282 }
283 g_string_append(str, "enum ");
284 g_string_append(str, node->u._enum.enum_id);
285 break;
286 }
287 case TYPESPEC_FLOATING_POINT:
288 case TYPESPEC_INTEGER:
289 case TYPESPEC_STRING:
290 default:
291 fprintf(fd, "[error] %s: unknown specifier\n", __func__);
292 return -EINVAL;
293 }
294 return 0;
295}
296
297static
298int visit_type_specifier_list(FILE *fd, struct ctf_node *type_specifier_list, GString *str)
add40b62
MD
299{
300 struct ctf_node *iter;
301 int alias_item_nr = 0;
78af2bcd 302 int ret;
add40b62 303
78af2bcd 304 cds_list_for_each_entry(iter, &type_specifier_list->u.type_specifier_list.head, siblings) {
add40b62
MD
305 if (alias_item_nr != 0)
306 g_string_append(str, " ");
307 alias_item_nr++;
78af2bcd
MD
308 ret = visit_type_specifier(fd, iter, str);
309 if (ret)
310 return ret;
add40b62
MD
311 }
312 return 0;
add40b62
MD
313}
314
315static
8fdba45b 316GQuark create_typealias_identifier(FILE *fd, int depth,
78af2bcd 317 struct ctf_node *type_specifier_list,
add40b62
MD
318 struct ctf_node *node_type_declarator)
319{
a0720417 320 struct ctf_node *iter;
add40b62 321 GString *str;
a0720417 322 char *str_c;
add40b62
MD
323 GQuark alias_q;
324 int ret;
325
a0720417 326 str = g_string_new("");
78af2bcd 327 ret = visit_type_specifier_list(fd, type_specifier_list, str);
add40b62
MD
328 if (ret) {
329 g_string_free(str, TRUE);
330 return 0;
331 }
332 cds_list_for_each_entry(iter, &node_type_declarator->u.type_declarator.pointers, siblings) {
333 g_string_append(str, " *");
334 if (iter->u.pointer.const_qualifier)
335 g_string_append(str, " const");
336 }
337 str_c = g_string_free(str, FALSE);
338 alias_q = g_quark_from_string(str_c);
339 g_free(str_c);
340 return alias_q;
341}
342
a3cca9e9 343static
8fdba45b 344struct declaration *ctf_type_declarator_visit(FILE *fd, int depth,
78af2bcd 345 struct ctf_node *type_specifier_list,
a3cca9e9
MD
346 GQuark *field_name,
347 struct ctf_node *node_type_declarator,
add40b62 348 struct declaration_scope *declaration_scope,
e397791f
MD
349 struct declaration *nested_declaration,
350 struct ctf_trace *trace)
a3cca9e9
MD
351{
352 /*
353 * Visit type declarator by first taking care of sequence/array
354 * (recursively). Then, when we get to the identifier, take care
355 * of pointers.
356 */
357
e397791f
MD
358 if (node_type_declarator) {
359 assert(node_type_declarator->u.type_declarator.type != TYPEDEC_UNKNOWN);
add40b62 360
e397791f
MD
361 /* TODO: gcc bitfields not supported yet. */
362 if (node_type_declarator->u.type_declarator.bitfield_len != NULL) {
78af2bcd 363 fprintf(fd, "[error] %s: gcc bitfields are not supported yet.\n", __func__);
e397791f
MD
364 return NULL;
365 }
add40b62 366 }
a3cca9e9
MD
367
368 if (!nested_declaration) {
e397791f 369 if (node_type_declarator && !cds_list_empty(&node_type_declarator->u.type_declarator.pointers)) {
add40b62
MD
370 GQuark alias_q;
371
a3cca9e9
MD
372 /*
373 * If we have a pointer declarator, it _has_ to be present in
374 * the typealiases (else fail).
375 */
add40b62 376 alias_q = create_typealias_identifier(fd, depth,
78af2bcd 377 type_specifier_list, node_type_declarator);
add40b62
MD
378 nested_declaration = lookup_declaration(alias_q, declaration_scope);
379 if (!nested_declaration) {
78af2bcd 380 fprintf(fd, "[error] %s: cannot find typealias \"%s\".\n", __func__, g_quark_to_string(alias_q));
add40b62
MD
381 return NULL;
382 }
4d5fc303
MD
383 if (nested_declaration->id == CTF_TYPE_INTEGER) {
384 struct declaration_integer *integer_declaration =
385 container_of(nested_declaration, struct declaration_integer, p);
386 /* For base to 16 for pointers (expected pretty-print) */
2527e149
MD
387 if (!integer_declaration->base) {
388 /*
389 * We need to do a copy of the
390 * integer declaration to modify it. There could be other references to
391 * it.
392 */
393 integer_declaration = integer_declaration_new(integer_declaration->len,
394 integer_declaration->byte_order, integer_declaration->signedness,
56e60373
MD
395 integer_declaration->p.alignment, 16, integer_declaration->encoding,
396 integer_declaration->clock);
2527e149
MD
397 nested_declaration = &integer_declaration->p;
398 }
4d5fc303 399 }
a3cca9e9 400 } else {
78af2bcd
MD
401 nested_declaration = ctf_type_specifier_list_visit(fd, depth,
402 type_specifier_list, declaration_scope, trace);
a3cca9e9 403 }
a3cca9e9
MD
404 }
405
e397791f
MD
406 if (!node_type_declarator)
407 return nested_declaration;
408
a3cca9e9
MD
409 if (node_type_declarator->u.type_declarator.type == TYPEDEC_ID) {
410 if (node_type_declarator->u.type_declarator.u.id)
411 *field_name = g_quark_from_string(node_type_declarator->u.type_declarator.u.id);
412 else
413 *field_name = 0;
414 return nested_declaration;
415 } else {
416 struct declaration *declaration;
98df1c9f 417 struct ctf_node *first;
a3cca9e9
MD
418
419 /* TYPEDEC_NESTED */
420
b5bf4179
MD
421 if (!nested_declaration) {
422 fprintf(fd, "[error] %s: nested type is unknown.\n", __func__);
423 return NULL;
424 }
425
a3cca9e9 426 /* create array/sequence, pass nested_declaration as child. */
98df1c9f
MD
427 if (cds_list_empty(&node_type_declarator->u.type_declarator.u.nested.length)) {
428 fprintf(fd, "[error] %s: expecting length field reference or value.\n", __func__);
a0720417
MD
429 return NULL;
430 }
98df1c9f
MD
431 first = _cds_list_first_entry(&node_type_declarator->u.type_declarator.u.nested.length,
432 struct ctf_node, siblings);
433 assert(first->type == NODE_UNARY_EXPRESSION);
434
435 switch (first->u.unary_expression.type) {
436 case UNARY_UNSIGNED_CONSTANT:
a0720417
MD
437 {
438 struct declaration_array *array_declaration;
439 size_t len;
440
98df1c9f 441 len = first->u.unary_expression.u.unsigned_constant;
a0720417
MD
442 array_declaration = array_declaration_new(len, nested_declaration,
443 declaration_scope);
98df1c9f
MD
444
445 if (!array_declaration) {
446 fprintf(fd, "[error] %s: cannot create array declaration.\n", __func__);
447 return NULL;
448 }
a0720417
MD
449 declaration = &array_declaration->p;
450 break;
451 }
98df1c9f 452 case UNARY_STRING:
a0720417 453 {
98df1c9f
MD
454 /* Lookup unsigned integer definition, create sequence */
455 char *length_name = concatenate_unary_strings(&node_type_declarator->u.type_declarator.u.nested.length);
a0720417 456 struct declaration_sequence *sequence_declaration;
a0720417 457
98df1c9f
MD
458 sequence_declaration = sequence_declaration_new(length_name, nested_declaration, declaration_scope);
459 if (!sequence_declaration) {
460 fprintf(fd, "[error] %s: cannot create sequence declaration.\n", __func__);
2dd46001
MD
461 return NULL;
462 }
a0720417
MD
463 declaration = &sequence_declaration->p;
464 break;
465 }
466 default:
467 assert(0);
a3cca9e9
MD
468 }
469
470 /* Pass it as content of outer container */
471 declaration = ctf_type_declarator_visit(fd, depth,
78af2bcd 472 type_specifier_list, field_name,
a3cca9e9 473 node_type_declarator->u.type_declarator.u.nested.type_declarator,
e397791f 474 declaration_scope, declaration, trace);
a3cca9e9
MD
475 return declaration;
476 }
477}
478
479static
8fdba45b 480int ctf_struct_type_declarators_visit(FILE *fd, int depth,
a3cca9e9 481 struct declaration_struct *struct_declaration,
78af2bcd 482 struct ctf_node *type_specifier_list,
a3cca9e9 483 struct cds_list_head *type_declarators,
e397791f
MD
484 struct declaration_scope *declaration_scope,
485 struct ctf_trace *trace)
a3cca9e9
MD
486{
487 struct ctf_node *iter;
488 GQuark field_name;
489
490 cds_list_for_each_entry(iter, type_declarators, siblings) {
491 struct declaration *field_declaration;
492
493 field_declaration = ctf_type_declarator_visit(fd, depth,
78af2bcd 494 type_specifier_list,
a3cca9e9
MD
495 &field_name, iter,
496 struct_declaration->scope,
e397791f 497 NULL, trace);
2dd46001
MD
498 if (!field_declaration) {
499 fprintf(fd, "[error] %s: unable to find struct field declaration type\n", __func__);
500 return -EINVAL;
501 }
d3006d91
SM
502
503 /* Check if field with same name already exists */
504 if (struct_declaration_lookup_field_index(struct_declaration, field_name) >= 0) {
505 fprintf(fd, "[error] %s: duplicate field %s in struct\n", __func__, g_quark_to_string(field_name));
506 return -EINVAL;
507 }
508
a3cca9e9
MD
509 struct_declaration_add_field(struct_declaration,
510 g_quark_to_string(field_name),
511 field_declaration);
512 }
add40b62
MD
513 return 0;
514}
a3cca9e9 515
add40b62 516static
8fdba45b 517int ctf_variant_type_declarators_visit(FILE *fd, int depth,
a0720417 518 struct declaration_untagged_variant *untagged_variant_declaration,
78af2bcd 519 struct ctf_node *type_specifier_list,
add40b62 520 struct cds_list_head *type_declarators,
e397791f
MD
521 struct declaration_scope *declaration_scope,
522 struct ctf_trace *trace)
add40b62
MD
523{
524 struct ctf_node *iter;
525 GQuark field_name;
526
527 cds_list_for_each_entry(iter, type_declarators, siblings) {
528 struct declaration *field_declaration;
529
530 field_declaration = ctf_type_declarator_visit(fd, depth,
78af2bcd 531 type_specifier_list,
add40b62 532 &field_name, iter,
a0720417 533 untagged_variant_declaration->scope,
e397791f 534 NULL, trace);
2dd46001
MD
535 if (!field_declaration) {
536 fprintf(fd, "[error] %s: unable to find variant field declaration type\n", __func__);
537 return -EINVAL;
538 }
43f9090c
SM
539
540 if (untagged_variant_declaration_get_field_from_tag(untagged_variant_declaration, field_name) != NULL) {
541 fprintf(fd, "[error] %s: duplicate field %s in variant\n", __func__, g_quark_to_string(field_name));
542 return -EINVAL;
543 }
544
545
a0720417 546 untagged_variant_declaration_add_field(untagged_variant_declaration,
add40b62
MD
547 g_quark_to_string(field_name),
548 field_declaration);
549 }
a3cca9e9
MD
550 return 0;
551}
552
553static
8fdba45b 554int ctf_typedef_visit(FILE *fd, int depth, struct declaration_scope *scope,
78af2bcd 555 struct ctf_node *type_specifier_list,
e397791f
MD
556 struct cds_list_head *type_declarators,
557 struct ctf_trace *trace)
a3cca9e9
MD
558{
559 struct ctf_node *iter;
560 GQuark identifier;
561
562 cds_list_for_each_entry(iter, type_declarators, siblings) {
563 struct declaration *type_declaration;
564 int ret;
565
566 type_declaration = ctf_type_declarator_visit(fd, depth,
78af2bcd 567 type_specifier_list,
a3cca9e9 568 &identifier, iter,
e397791f 569 scope, NULL, trace);
2dd46001
MD
570 if (!type_declaration) {
571 fprintf(fd, "[error] %s: problem creating type declaration\n", __func__);
572 return -EINVAL;
573 }
574 /*
575 * Don't allow typedef and typealias of untagged
576 * variants.
577 */
578 if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) {
579 fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__);
580 declaration_unref(type_declaration);
581 return -EPERM;
582 }
a3cca9e9
MD
583 ret = register_declaration(identifier, type_declaration, scope);
584 if (ret) {
585 type_declaration->declaration_free(type_declaration);
586 return ret;
587 }
588 }
589 return 0;
590}
591
592static
8fdba45b 593int ctf_typealias_visit(FILE *fd, int depth, struct declaration_scope *scope,
e397791f
MD
594 struct ctf_node *target, struct ctf_node *alias,
595 struct ctf_trace *trace)
a3cca9e9
MD
596{
597 struct declaration *type_declaration;
a0720417 598 struct ctf_node *node;
add40b62 599 GQuark dummy_id;
a3cca9e9 600 GQuark alias_q;
a0720417 601 int err;
a3cca9e9
MD
602
603 /* See ctf_visitor_type_declarator() in the semantic validator. */
604
605 /*
606 * Create target type declaration.
607 */
608
427c09b7 609 if (cds_list_empty(&target->u.typealias_target.type_declarators))
a0720417
MD
610 node = NULL;
611 else
427c09b7 612 node = _cds_list_first_entry(&target->u.typealias_target.type_declarators,
a0720417 613 struct ctf_node, siblings);
a3cca9e9 614 type_declaration = ctf_type_declarator_visit(fd, depth,
78af2bcd 615 target->u.typealias_target.type_specifier_list,
a0720417 616 &dummy_id, node,
e397791f 617 scope, NULL, trace);
a3cca9e9 618 if (!type_declaration) {
78af2bcd 619 fprintf(fd, "[error] %s: problem creating type declaration\n", __func__);
a3cca9e9
MD
620 err = -EINVAL;
621 goto error;
622 }
2dd46001
MD
623 /*
624 * Don't allow typedef and typealias of untagged
625 * variants.
626 */
627 if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) {
628 fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__);
629 declaration_unref(type_declaration);
630 return -EPERM;
631 }
a3cca9e9
MD
632 /*
633 * The semantic validator does not check whether the target is
634 * abstract or not (if it has an identifier). Check it here.
635 */
636 if (dummy_id != 0) {
78af2bcd 637 fprintf(fd, "[error] %s: expecting empty identifier\n", __func__);
a3cca9e9
MD
638 err = -EINVAL;
639 goto error;
640 }
641 /*
642 * Create alias identifier.
643 */
a3cca9e9
MD
644
645 node = _cds_list_first_entry(&alias->u.typealias_alias.type_declarators,
a0720417 646 struct ctf_node, siblings);
add40b62 647 alias_q = create_typealias_identifier(fd, depth,
78af2bcd 648 alias->u.typealias_alias.type_specifier_list, node);
a0720417
MD
649 err = register_declaration(alias_q, type_declaration, scope);
650 if (err)
a3cca9e9
MD
651 goto error;
652 return 0;
653
654error:
230da743
SM
655 if (type_declaration) {
656 type_declaration->declaration_free(type_declaration);
657 }
a0720417 658 return err;
a3cca9e9
MD
659}
660
661static
8fdba45b 662int ctf_struct_declaration_list_visit(FILE *fd, int depth,
e397791f
MD
663 struct ctf_node *iter, struct declaration_struct *struct_declaration,
664 struct ctf_trace *trace)
a3cca9e9 665{
a3cca9e9
MD
666 int ret;
667
668 switch (iter->type) {
669 case NODE_TYPEDEF:
670 /* For each declarator, declare type and add type to struct declaration scope */
671 ret = ctf_typedef_visit(fd, depth,
672 struct_declaration->scope,
78af2bcd 673 iter->u._typedef.type_specifier_list,
e397791f 674 &iter->u._typedef.type_declarators, trace);
a3cca9e9
MD
675 if (ret)
676 return ret;
677 break;
678 case NODE_TYPEALIAS:
679 /* Declare type with declarator and add type to struct declaration scope */
680 ret = ctf_typealias_visit(fd, depth,
681 struct_declaration->scope,
682 iter->u.typealias.target,
e397791f 683 iter->u.typealias.alias, trace);
a3cca9e9
MD
684 if (ret)
685 return ret;
686 break;
687 case NODE_STRUCT_OR_VARIANT_DECLARATION:
688 /* Add field to structure declaration */
689 ret = ctf_struct_type_declarators_visit(fd, depth,
690 struct_declaration,
78af2bcd 691 iter->u.struct_or_variant_declaration.type_specifier_list,
a0720417
MD
692 &iter->u.struct_or_variant_declaration.type_declarators,
693 struct_declaration->scope, trace);
a3cca9e9
MD
694 if (ret)
695 return ret;
696 break;
697 default:
78af2bcd 698 fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) iter->type);
a3cca9e9
MD
699 assert(0);
700 }
701 return 0;
702}
703
add40b62 704static
8fdba45b 705int ctf_variant_declaration_list_visit(FILE *fd, int depth,
a0720417
MD
706 struct ctf_node *iter,
707 struct declaration_untagged_variant *untagged_variant_declaration,
e397791f 708 struct ctf_trace *trace)
add40b62 709{
add40b62
MD
710 int ret;
711
712 switch (iter->type) {
713 case NODE_TYPEDEF:
714 /* For each declarator, declare type and add type to variant declaration scope */
715 ret = ctf_typedef_visit(fd, depth,
a0720417 716 untagged_variant_declaration->scope,
78af2bcd 717 iter->u._typedef.type_specifier_list,
e397791f 718 &iter->u._typedef.type_declarators, trace);
add40b62
MD
719 if (ret)
720 return ret;
721 break;
722 case NODE_TYPEALIAS:
723 /* Declare type with declarator and add type to variant declaration scope */
724 ret = ctf_typealias_visit(fd, depth,
a0720417 725 untagged_variant_declaration->scope,
add40b62 726 iter->u.typealias.target,
e397791f 727 iter->u.typealias.alias, trace);
add40b62
MD
728 if (ret)
729 return ret;
730 break;
731 case NODE_STRUCT_OR_VARIANT_DECLARATION:
732 /* Add field to structure declaration */
733 ret = ctf_variant_type_declarators_visit(fd, depth,
a0720417 734 untagged_variant_declaration,
78af2bcd 735 iter->u.struct_or_variant_declaration.type_specifier_list,
a0720417
MD
736 &iter->u.struct_or_variant_declaration.type_declarators,
737 untagged_variant_declaration->scope, trace);
add40b62
MD
738 if (ret)
739 return ret;
740 break;
741 default:
78af2bcd 742 fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) iter->type);
add40b62
MD
743 assert(0);
744 }
745 return 0;
746}
747
a3cca9e9 748static
a0720417 749struct declaration *ctf_declaration_struct_visit(FILE *fd,
a3cca9e9 750 int depth, const char *name, struct cds_list_head *declaration_list,
b7e35bad
MD
751 int has_body, struct cds_list_head *min_align,
752 struct declaration_scope *declaration_scope,
e397791f 753 struct ctf_trace *trace)
a3cca9e9 754{
a3cca9e9
MD
755 struct declaration_struct *struct_declaration;
756 struct ctf_node *iter;
a0720417 757 int ret;
a3cca9e9
MD
758
759 /*
760 * For named struct (without body), lookup in
761 * declaration scope. Don't take reference on struct
762 * declaration: ref is only taken upon definition.
763 */
764 if (!has_body) {
765 assert(name);
766 struct_declaration =
767 lookup_struct_declaration(g_quark_from_string(name),
768 declaration_scope);
a0720417 769 return &struct_declaration->p;
a3cca9e9 770 } else {
b7e35bad
MD
771 uint64_t min_align_value = 0;
772
a3cca9e9
MD
773 /* For unnamed struct, create type */
774 /* For named struct (with body), create type and add to declaration scope */
775 if (name) {
776 if (lookup_struct_declaration(g_quark_from_string(name),
777 declaration_scope)) {
778
78af2bcd 779 fprintf(fd, "[error] %s: struct %s already declared in scope\n", __func__, name);
a3cca9e9
MD
780 return NULL;
781 }
782 }
b7e35bad
MD
783 if (!cds_list_empty(min_align)) {
784 ret = get_unary_unsigned(min_align, &min_align_value);
785 if (ret) {
786 fprintf(fd, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__);
787 ret = -EINVAL;
788 goto error;
789 }
790 }
791 struct_declaration = struct_declaration_new(declaration_scope,
792 min_align_value);
a3cca9e9 793 cds_list_for_each_entry(iter, declaration_list, siblings) {
e397791f
MD
794 ret = ctf_struct_declaration_list_visit(fd, depth + 1, iter,
795 struct_declaration, trace);
a3cca9e9 796 if (ret)
5c551b40 797 goto error_free_declaration;
a3cca9e9
MD
798 }
799 if (name) {
800 ret = register_struct_declaration(g_quark_from_string(name),
801 struct_declaration,
802 declaration_scope);
803 assert(!ret);
804 }
a0720417 805 return &struct_declaration->p;
a3cca9e9 806 }
5c551b40 807error_free_declaration:
a3cca9e9 808 struct_declaration->p.declaration_free(&struct_declaration->p);
5c551b40 809error:
a3cca9e9
MD
810 return NULL;
811}
812
05628561 813static
a0720417
MD
814struct declaration *ctf_declaration_variant_visit(FILE *fd,
815 int depth, const char *name, const char *choice,
816 struct cds_list_head *declaration_list,
e397791f
MD
817 int has_body, struct declaration_scope *declaration_scope,
818 struct ctf_trace *trace)
05628561 819{
a0720417 820 struct declaration_untagged_variant *untagged_variant_declaration;
add40b62
MD
821 struct declaration_variant *variant_declaration;
822 struct ctf_node *iter;
a0720417 823 int ret;
de47353a 824
add40b62
MD
825 /*
826 * For named variant (without body), lookup in
827 * declaration scope. Don't take reference on variant
828 * declaration: ref is only taken upon definition.
829 */
830 if (!has_body) {
831 assert(name);
a0720417 832 untagged_variant_declaration =
add40b62
MD
833 lookup_variant_declaration(g_quark_from_string(name),
834 declaration_scope);
add40b62 835 } else {
de47353a 836 /* For unnamed variant, create type */
add40b62
MD
837 /* For named variant (with body), create type and add to declaration scope */
838 if (name) {
839 if (lookup_variant_declaration(g_quark_from_string(name),
840 declaration_scope)) {
841
78af2bcd 842 fprintf(fd, "[error] %s: variant %s already declared in scope\n", __func__, name);
add40b62
MD
843 return NULL;
844 }
845 }
a0720417 846 untagged_variant_declaration = untagged_variant_declaration_new(declaration_scope);
add40b62 847 cds_list_for_each_entry(iter, declaration_list, siblings) {
e397791f 848 ret = ctf_variant_declaration_list_visit(fd, depth + 1, iter,
a0720417 849 untagged_variant_declaration, trace);
add40b62
MD
850 if (ret)
851 goto error;
852 }
853 if (name) {
854 ret = register_variant_declaration(g_quark_from_string(name),
a0720417 855 untagged_variant_declaration,
add40b62
MD
856 declaration_scope);
857 assert(!ret);
858 }
a0720417
MD
859 }
860 /*
861 * if tagged, create tagged variant and return. else return
862 * untagged variant.
863 */
864 if (!choice) {
865 return &untagged_variant_declaration->p;
866 } else {
867 variant_declaration = variant_declaration_new(untagged_variant_declaration, choice);
868 if (!variant_declaration)
869 goto error;
870 declaration_unref(&untagged_variant_declaration->p);
871 return &variant_declaration->p;
de47353a 872 }
add40b62 873error:
2dd46001 874 untagged_variant_declaration->p.declaration_free(&untagged_variant_declaration->p);
add40b62 875 return NULL;
05628561
MD
876}
877
05628561 878static
8fdba45b 879int ctf_enumerator_list_visit(FILE *fd, int depth,
add40b62
MD
880 struct ctf_node *enumerator,
881 struct declaration_enum *enum_declaration)
882{
1cfda062
MD
883 GQuark q;
884 struct ctf_node *iter;
885
886 q = g_quark_from_string(enumerator->u.enumerator.id);
a0720417 887 if (enum_declaration->integer_declaration->signedness) {
1cfda062
MD
888 int64_t start, end;
889 int nr_vals = 0;
890
a0720417 891 cds_list_for_each_entry(iter, &enumerator->u.enumerator.values, siblings) {
1cfda062
MD
892 int64_t *target;
893
894 assert(iter->type == NODE_UNARY_EXPRESSION);
895 if (nr_vals == 0)
896 target = &start;
897 else
898 target = &end;
899
900 switch (iter->u.unary_expression.type) {
901 case UNARY_SIGNED_CONSTANT:
902 *target = iter->u.unary_expression.u.signed_constant;
903 break;
904 case UNARY_UNSIGNED_CONSTANT:
905 *target = iter->u.unary_expression.u.unsigned_constant;
906 break;
907 default:
78af2bcd 908 fprintf(fd, "[error] %s: invalid enumerator\n", __func__);
1cfda062
MD
909 return -EINVAL;
910 }
911 if (nr_vals > 1) {
78af2bcd 912 fprintf(fd, "[error] %s: invalid enumerator\n", __func__);
1cfda062
MD
913 return -EINVAL;
914 }
915 nr_vals++;
916 }
917 if (nr_vals == 1)
918 end = start;
919 enum_signed_insert(enum_declaration, start, end, q);
a0720417 920 } else {
1cfda062
MD
921 uint64_t start, end;
922 int nr_vals = 0;
923
a0720417
MD
924 cds_list_for_each_entry(iter, &enumerator->u.enumerator.values, siblings) {
925 uint64_t *target;
1cfda062
MD
926
927 assert(iter->type == NODE_UNARY_EXPRESSION);
928 if (nr_vals == 0)
929 target = &start;
930 else
931 target = &end;
932
933 switch (iter->u.unary_expression.type) {
934 case UNARY_UNSIGNED_CONSTANT:
935 *target = iter->u.unary_expression.u.unsigned_constant;
936 break;
937 case UNARY_SIGNED_CONSTANT:
938 /*
939 * We don't accept signed constants for enums with unsigned
940 * container type.
941 */
78af2bcd 942 fprintf(fd, "[error] %s: invalid enumerator (signed constant encountered, but enum container type is unsigned)\n", __func__);
1cfda062
MD
943 return -EINVAL;
944 default:
78af2bcd 945 fprintf(fd, "[error] %s: invalid enumerator\n", __func__);
1cfda062
MD
946 return -EINVAL;
947 }
948 if (nr_vals > 1) {
78af2bcd 949 fprintf(fd, "[error] %s: invalid enumerator\n", __func__);
1cfda062
MD
950 return -EINVAL;
951 }
952 nr_vals++;
953 }
954 if (nr_vals == 1)
955 end = start;
956 enum_unsigned_insert(enum_declaration, start, end, q);
957 }
add40b62
MD
958 return 0;
959}
960
961static
8fdba45b 962struct declaration *ctf_declaration_enum_visit(FILE *fd, int depth,
add40b62 963 const char *name,
78af2bcd 964 struct ctf_node *container_type,
add40b62 965 struct cds_list_head *enumerator_list,
1cfda062
MD
966 int has_body,
967 struct declaration_scope *declaration_scope,
968 struct ctf_trace *trace)
05628561 969{
add40b62
MD
970 struct declaration *declaration;
971 struct declaration_enum *enum_declaration;
972 struct declaration_integer *integer_declaration;
78af2bcd 973 struct ctf_node *iter;
1cfda062 974 GQuark dummy_id;
a0720417 975 int ret;
add40b62 976
05628561 977 /*
add40b62
MD
978 * For named enum (without body), lookup in
979 * declaration scope. Don't take reference on enum
980 * declaration: ref is only taken upon definition.
05628561 981 */
add40b62
MD
982 if (!has_body) {
983 assert(name);
984 enum_declaration =
985 lookup_enum_declaration(g_quark_from_string(name),
986 declaration_scope);
a0720417 987 return &enum_declaration->p;
add40b62
MD
988 } else {
989 /* For unnamed enum, create type */
990 /* For named enum (with body), create type and add to declaration scope */
991 if (name) {
992 if (lookup_enum_declaration(g_quark_from_string(name),
993 declaration_scope)) {
994
78af2bcd 995 fprintf(fd, "[error] %s: enum %s already declared in scope\n", __func__, name);
add40b62
MD
996 return NULL;
997 }
998 }
78af2bcd 999 if (!container_type) {
6743829a
MD
1000 declaration = lookup_declaration(g_quark_from_static_string("int"),
1001 declaration_scope);
1002 if (!declaration) {
1003 fprintf(fd, "[error] %s: \"int\" type declaration missing for enumeration\n", __func__);
1004 return NULL;
1005 }
1006 } else {
1007 declaration = ctf_type_declarator_visit(fd, depth,
1008 container_type,
1009 &dummy_id, NULL,
1010 declaration_scope,
1011 NULL, trace);
1cfda062 1012 }
30ea18a1
MD
1013 if (!declaration) {
1014 fprintf(fd, "[error] %s: unable to create container type for enumeration\n", __func__);
1015 return NULL;
1016 }
1017 if (declaration->id != CTF_TYPE_INTEGER) {
1018 fprintf(fd, "[error] %s: container type for enumeration is not integer\n", __func__);
1019 return NULL;
1cfda062 1020 }
30ea18a1 1021 integer_declaration = container_of(declaration, struct declaration_integer, p);
a0720417 1022 enum_declaration = enum_declaration_new(integer_declaration);
add40b62
MD
1023 declaration_unref(&integer_declaration->p); /* leave ref to enum */
1024 cds_list_for_each_entry(iter, enumerator_list, siblings) {
1025 ret = ctf_enumerator_list_visit(fd, depth + 1, iter, enum_declaration);
1026 if (ret)
1027 goto error;
1028 }
1029 if (name) {
1030 ret = register_enum_declaration(g_quark_from_string(name),
1031 enum_declaration,
1032 declaration_scope);
1033 assert(!ret);
1034 }
a0720417 1035 return &enum_declaration->p;
05628561 1036 }
add40b62
MD
1037error:
1038 enum_declaration->p.declaration_free(&enum_declaration->p);
1039 return NULL;
05628561
MD
1040}
1041
1042static
8fdba45b 1043struct declaration *ctf_declaration_type_specifier_visit(FILE *fd, int depth,
78af2bcd 1044 struct ctf_node *type_specifier_list,
d20f5e59 1045 struct declaration_scope *declaration_scope)
05628561 1046{
add40b62
MD
1047 GString *str;
1048 struct declaration *declaration;
a0720417
MD
1049 char *str_c;
1050 int ret;
1051 GQuark id_q;
05628561 1052
a0720417 1053 str = g_string_new("");
78af2bcd 1054 ret = visit_type_specifier_list(fd, type_specifier_list, str);
add40b62
MD
1055 if (ret)
1056 return NULL;
1057 str_c = g_string_free(str, FALSE);
1058 id_q = g_quark_from_string(str_c);
1059 g_free(str_c);
1060 declaration = lookup_declaration(id_q, declaration_scope);
1061 return declaration;
1062}
1063
ab4cf058
MD
1064/*
1065 * Returns 0/1 boolean, or < 0 on error.
1066 */
1067static
a0720417 1068int get_boolean(FILE *fd, int depth, struct ctf_node *unary_expression)
ab4cf058
MD
1069{
1070 if (unary_expression->type != NODE_UNARY_EXPRESSION) {
78af2bcd 1071 fprintf(fd, "[error] %s: expecting unary expression\n",
ab4cf058
MD
1072 __func__);
1073 return -EINVAL;
1074 }
1075 switch (unary_expression->u.unary_expression.type) {
1076 case UNARY_UNSIGNED_CONSTANT:
1077 if (unary_expression->u.unary_expression.u.unsigned_constant == 0)
1078 return 0;
1079 else
1080 return 1;
1081 case UNARY_SIGNED_CONSTANT:
1082 if (unary_expression->u.unary_expression.u.signed_constant == 0)
1083 return 0;
1084 else
1085 return 1;
1086 case UNARY_STRING:
1087 if (!strcmp(unary_expression->u.unary_expression.u.string, "true"))
1088 return 1;
1089 else if (!strcmp(unary_expression->u.unary_expression.u.string, "TRUE"))
1090 return 1;
1091 else if (!strcmp(unary_expression->u.unary_expression.u.string, "false"))
1092 return 0;
1093 else if (!strcmp(unary_expression->u.unary_expression.u.string, "FALSE"))
1094 return 0;
1095 else {
78af2bcd 1096 fprintf(fd, "[error] %s: unexpected string \"%s\"\n",
ab4cf058
MD
1097 __func__, unary_expression->u.unary_expression.u.string);
1098 return -EINVAL;
1099 }
1100 break;
1101 default:
78af2bcd 1102 fprintf(fd, "[error] %s: unexpected unary expression type\n",
ab4cf058
MD
1103 __func__);
1104 return -EINVAL;
1105 }
1106
1107}
1108
0f980a35
MD
1109static
1110int get_trace_byte_order(FILE *fd, int depth, struct ctf_node *unary_expression)
1111{
1112 int byte_order;
1113
1114 if (unary_expression->u.unary_expression.type != UNARY_STRING) {
1115 fprintf(fd, "[error] %s: byte_order: expecting string\n",
1116 __func__);
1117 return -EINVAL;
1118 }
1119 if (!strcmp(unary_expression->u.unary_expression.u.string, "be"))
1120 byte_order = BIG_ENDIAN;
1121 else if (!strcmp(unary_expression->u.unary_expression.u.string, "le"))
1122 byte_order = LITTLE_ENDIAN;
1123 else {
1124 fprintf(fd, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1125 __func__, unary_expression->u.unary_expression.u.string);
1126 return -EINVAL;
1127 }
1128 return byte_order;
1129}
1130
ab4cf058 1131static
a0720417
MD
1132int get_byte_order(FILE *fd, int depth, struct ctf_node *unary_expression,
1133 struct ctf_trace *trace)
ab4cf058
MD
1134{
1135 int byte_order;
1136
1137 if (unary_expression->u.unary_expression.type != UNARY_STRING) {
78af2bcd 1138 fprintf(fd, "[error] %s: byte_order: expecting string\n",
ab4cf058
MD
1139 __func__);
1140 return -EINVAL;
1141 }
1142 if (!strcmp(unary_expression->u.unary_expression.u.string, "native"))
1143 byte_order = trace->byte_order;
1144 else if (!strcmp(unary_expression->u.unary_expression.u.string, "network"))
1145 byte_order = BIG_ENDIAN;
1146 else if (!strcmp(unary_expression->u.unary_expression.u.string, "be"))
1147 byte_order = BIG_ENDIAN;
1148 else if (!strcmp(unary_expression->u.unary_expression.u.string, "le"))
1149 byte_order = LITTLE_ENDIAN;
1150 else {
78af2bcd 1151 fprintf(fd, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
a0720417 1152 __func__, unary_expression->u.unary_expression.u.string);
ab4cf058
MD
1153 return -EINVAL;
1154 }
1155 return byte_order;
1156}
1157
add40b62 1158static
8fdba45b 1159struct declaration *ctf_declaration_integer_visit(FILE *fd, int depth,
ab4cf058
MD
1160 struct cds_list_head *expressions,
1161 struct ctf_trace *trace)
add40b62 1162{
a0720417 1163 struct ctf_node *expression;
68262b11 1164 uint64_t alignment = 1, size = 0;
ab4cf058
MD
1165 int byte_order = trace->byte_order;
1166 int signedness = 0;
add40b62 1167 int has_alignment = 0, has_size = 0;
4d5fc303 1168 int base = 0;
81dee1bb 1169 enum ctf_string_encoding encoding = CTF_STRING_NONE;
56e60373 1170 struct ctf_clock *clock = NULL;
add40b62
MD
1171 struct declaration_integer *integer_declaration;
1172
1173 cds_list_for_each_entry(expression, expressions, siblings) {
a0720417 1174 struct ctf_node *left, *right;
add40b62 1175
a0720417
MD
1176 left = _cds_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings);
1177 right = _cds_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings);
add40b62
MD
1178 assert(left->u.unary_expression.type == UNARY_STRING);
1179 if (!strcmp(left->u.unary_expression.u.string, "signed")) {
ab4cf058
MD
1180 signedness = get_boolean(fd, depth, right);
1181 if (signedness < 0)
1182 return NULL;
add40b62 1183 } else if (!strcmp(left->u.unary_expression.u.string, "byte_order")) {
a0720417 1184 byte_order = get_byte_order(fd, depth, right, trace);
ab4cf058
MD
1185 if (byte_order < 0)
1186 return NULL;
add40b62 1187 } else if (!strcmp(left->u.unary_expression.u.string, "size")) {
ab4cf058 1188 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
78af2bcd 1189 fprintf(fd, "[error] %s: size: expecting unsigned constant\n",
ab4cf058
MD
1190 __func__);
1191 return NULL;
1192 }
1193 size = right->u.unary_expression.u.unsigned_constant;
add40b62
MD
1194 has_size = 1;
1195 } else if (!strcmp(left->u.unary_expression.u.string, "align")) {
ab4cf058 1196 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
78af2bcd 1197 fprintf(fd, "[error] %s: align: expecting unsigned constant\n",
ab4cf058
MD
1198 __func__);
1199 return NULL;
1200 }
1201 alignment = right->u.unary_expression.u.unsigned_constant;
53532829
MD
1202 /* Make sure alignment is a power of two */
1203 if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
1204 fprintf(fd, "[error] %s: align: expecting power of two\n",
1205 __func__);
1206 return NULL;
1207 }
add40b62 1208 has_alignment = 1;
164078da
MD
1209 } else if (!strcmp(left->u.unary_expression.u.string, "base")) {
1210 switch (right->u.unary_expression.type) {
1211 case UNARY_UNSIGNED_CONSTANT:
1212 switch (right->u.unary_expression.u.unsigned_constant) {
1213 case 2:
1214 case 8:
1215 case 10:
1216 case 16:
1217 base = right->u.unary_expression.u.unsigned_constant;
1218 break;
1219 default:
1220 fprintf(fd, "[error] %s: base not supported (%" PRIu64 ")\n",
1221 __func__, right->u.unary_expression.u.unsigned_constant);
1222 return NULL;
1223 }
1224 break;
1225 case UNARY_STRING:
1226 {
1227 char *s_right = concatenate_unary_strings(&expression->u.ctf_expression.right);
1228 if (!s_right) {
1229 fprintf(fd, "[error] %s: unexpected unary expression for integer base\n", __func__);
1230 g_free(s_right);
1231 return NULL;
1232 }
1233 if (!strcmp(s_right, "decimal") || !strcmp(s_right, "dec") || !strcmp(s_right, "d")
1234 || !strcmp(s_right, "i") || !strcmp(s_right, "u")) {
1235 base = 10;
1236 } else if (!strcmp(s_right, "hexadecimal") || !strcmp(s_right, "hex")
1237 || !strcmp(s_right, "x") || !strcmp(s_right, "X")
1238 || !strcmp(s_right, "p")) {
1239 base = 16;
1240 } else if (!strcmp(s_right, "octal") || !strcmp(s_right, "oct")
1241 || !strcmp(s_right, "o")) {
1242 base = 8;
1243 } else if (!strcmp(s_right, "binary") || !strcmp(s_right, "b")) {
1244 base = 2;
1245 } else {
1246 fprintf(fd, "[error] %s: unexpected expression for integer base (%s)\n", __func__, s_right);
1247 g_free(s_right);
1248 return NULL;
1249 }
1250
1251 g_free(s_right);
1252 break;
1253 }
1254 default:
1255 fprintf(fd, "[error] %s: base: expecting unsigned constant or unary string\n",
1256 __func__);
1257 return NULL;
1258 }
81dee1bb
MD
1259 } else if (!strcmp(left->u.unary_expression.u.string, "encoding")) {
1260 char *s_right;
1261
1262 if (right->u.unary_expression.type != UNARY_STRING) {
1263 fprintf(fd, "[error] %s: encoding: expecting unary string\n",
1264 __func__);
1265 return NULL;
1266 }
1267 s_right = concatenate_unary_strings(&expression->u.ctf_expression.right);
1268 if (!s_right) {
1269 fprintf(fd, "[error] %s: unexpected unary expression for integer base\n", __func__);
1270 g_free(s_right);
1271 return NULL;
1272 }
1273 if (!strcmp(s_right, "UTF8")
1274 || !strcmp(s_right, "utf8")
1275 || !strcmp(s_right, "utf-8")
1276 || !strcmp(s_right, "UTF-8"))
1277 encoding = CTF_STRING_UTF8;
1278 else if (!strcmp(s_right, "ASCII")
1279 || !strcmp(s_right, "ascii"))
1280 encoding = CTF_STRING_ASCII;
b5bf4179
MD
1281 else if (!strcmp(s_right, "none"))
1282 encoding = CTF_STRING_NONE;
81dee1bb
MD
1283 else {
1284 fprintf(fd, "[error] %s: unknown string encoding \"%s\"\n", __func__, s_right);
1285 g_free(s_right);
1286 return NULL;
1287 }
1288 g_free(s_right);
73d15916 1289 } else if (!strcmp(left->u.unary_expression.u.string, "map")) {
56e60373 1290 GQuark clock_name;
73d15916
MD
1291
1292 if (right->u.unary_expression.type != UNARY_STRING) {
1293 fprintf(fd, "[error] %s: map: expecting identifier\n",
1294 __func__);
1295 return NULL;
1296 }
56e60373
MD
1297 /* currently only support clock.name.value */
1298 clock_name = get_map_clock_name_value(&expression->u.ctf_expression.right);
1299 if (!clock_name) {
1300 char *s_right;
1301
1302 s_right = concatenate_unary_strings(&expression->u.ctf_expression.right);
1303 if (!s_right) {
1304 fprintf(fd, "[error] %s: unexpected unary expression for integer map\n", __func__);
1305 g_free(s_right);
1306 return NULL;
1307 }
1308 fprintf(fd, "[warning] %s: unknown map %s in integer declaration\n", __func__,
1309 s_right);
73d15916 1310 g_free(s_right);
56e60373
MD
1311 continue;
1312 }
1313 clock = trace_clock_lookup(trace, clock_name);
1314 if (!clock) {
1315 fprintf(fd, "[error] %s: map: unable to find clock %s declaration\n",
1316 __func__, g_quark_to_string(clock_name));
73d15916
MD
1317 return NULL;
1318 }
add40b62 1319 } else {
b3ab6665 1320 fprintf(fd, "[warning] %s: unknown attribute name %s\n",
add40b62 1321 __func__, left->u.unary_expression.u.string);
b3ab6665 1322 /* Fall-through after warning */
add40b62 1323 }
05628561 1324 }
add40b62 1325 if (!has_size) {
78af2bcd 1326 fprintf(fd, "[error] %s: missing size attribute\n", __func__);
add40b62
MD
1327 return NULL;
1328 }
ab4cf058
MD
1329 if (!has_alignment) {
1330 if (size % CHAR_BIT) {
1331 /* bit-packed alignment */
1332 alignment = 1;
1333 } else {
1334 /* byte-packed alignment */
1335 alignment = CHAR_BIT;
1336 }
1337 }
add40b62 1338 integer_declaration = integer_declaration_new(size,
81dee1bb 1339 byte_order, signedness, alignment,
56e60373 1340 base, encoding, clock);
add40b62 1341 return &integer_declaration->p;
05628561
MD
1342}
1343
ab4cf058 1344static
8fdba45b 1345struct declaration *ctf_declaration_floating_point_visit(FILE *fd, int depth,
ab4cf058
MD
1346 struct cds_list_head *expressions,
1347 struct ctf_trace *trace)
1348{
a0720417 1349 struct ctf_node *expression;
5c551b40
MD
1350 uint64_t alignment = 1, exp_dig = 0, mant_dig = 0,
1351 byte_order = trace->byte_order;
ab4cf058
MD
1352 int has_alignment = 0, has_exp_dig = 0, has_mant_dig = 0;
1353 struct declaration_float *float_declaration;
1354
1355 cds_list_for_each_entry(expression, expressions, siblings) {
a0720417 1356 struct ctf_node *left, *right;
ab4cf058 1357
a0720417
MD
1358 left = _cds_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings);
1359 right = _cds_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings);
ab4cf058
MD
1360 assert(left->u.unary_expression.type == UNARY_STRING);
1361 if (!strcmp(left->u.unary_expression.u.string, "byte_order")) {
a0720417 1362 byte_order = get_byte_order(fd, depth, right, trace);
ab4cf058
MD
1363 if (byte_order < 0)
1364 return NULL;
1365 } else if (!strcmp(left->u.unary_expression.u.string, "exp_dig")) {
1366 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
78af2bcd 1367 fprintf(fd, "[error] %s: exp_dig: expecting unsigned constant\n",
ab4cf058
MD
1368 __func__);
1369 return NULL;
1370 }
1371 exp_dig = right->u.unary_expression.u.unsigned_constant;
1372 has_exp_dig = 1;
1373 } else if (!strcmp(left->u.unary_expression.u.string, "mant_dig")) {
1374 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
78af2bcd 1375 fprintf(fd, "[error] %s: mant_dig: expecting unsigned constant\n",
ab4cf058
MD
1376 __func__);
1377 return NULL;
1378 }
1379 mant_dig = right->u.unary_expression.u.unsigned_constant;
1380 has_mant_dig = 1;
1381 } else if (!strcmp(left->u.unary_expression.u.string, "align")) {
1382 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
78af2bcd 1383 fprintf(fd, "[error] %s: align: expecting unsigned constant\n",
ab4cf058
MD
1384 __func__);
1385 return NULL;
1386 }
1387 alignment = right->u.unary_expression.u.unsigned_constant;
53532829
MD
1388 /* Make sure alignment is a power of two */
1389 if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
1390 fprintf(fd, "[error] %s: align: expecting power of two\n",
1391 __func__);
1392 return NULL;
1393 }
ab4cf058
MD
1394 has_alignment = 1;
1395 } else {
b3ab6665 1396 fprintf(fd, "[warning] %s: unknown attribute name %s\n",
ab4cf058 1397 __func__, left->u.unary_expression.u.string);
b3ab6665 1398 /* Fall-through after warning */
ab4cf058
MD
1399 }
1400 }
1401 if (!has_mant_dig) {
78af2bcd 1402 fprintf(fd, "[error] %s: missing mant_dig attribute\n", __func__);
ab4cf058
MD
1403 return NULL;
1404 }
1405 if (!has_exp_dig) {
78af2bcd 1406 fprintf(fd, "[error] %s: missing exp_dig attribute\n", __func__);
ab4cf058
MD
1407 return NULL;
1408 }
1409 if (!has_alignment) {
1410 if ((mant_dig + exp_dig) % CHAR_BIT) {
1411 /* bit-packed alignment */
1412 alignment = 1;
1413 } else {
1414 /* byte-packed alignment */
1415 alignment = CHAR_BIT;
1416 }
1417 }
1418 float_declaration = float_declaration_new(mant_dig, exp_dig,
1419 byte_order, alignment);
1420 return &float_declaration->p;
1421}
1422
1423static
8fdba45b 1424struct declaration *ctf_declaration_string_visit(FILE *fd, int depth,
ab4cf058
MD
1425 struct cds_list_head *expressions,
1426 struct ctf_trace *trace)
1427{
a0720417 1428 struct ctf_node *expression;
ab4cf058
MD
1429 const char *encoding_c = NULL;
1430 enum ctf_string_encoding encoding = CTF_STRING_UTF8;
1431 struct declaration_string *string_declaration;
1432
1433 cds_list_for_each_entry(expression, expressions, siblings) {
a0720417 1434 struct ctf_node *left, *right;
ab4cf058 1435
a0720417
MD
1436 left = _cds_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings);
1437 right = _cds_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings);
ab4cf058
MD
1438 assert(left->u.unary_expression.type == UNARY_STRING);
1439 if (!strcmp(left->u.unary_expression.u.string, "encoding")) {
a0720417 1440 if (right->u.unary_expression.type != UNARY_STRING) {
78af2bcd 1441 fprintf(fd, "[error] %s: encoding: expecting string\n",
ab4cf058
MD
1442 __func__);
1443 return NULL;
1444 }
1445 encoding_c = right->u.unary_expression.u.string;
1446 } else {
b3ab6665 1447 fprintf(fd, "[warning] %s: unknown attribute name %s\n",
ab4cf058 1448 __func__, left->u.unary_expression.u.string);
b3ab6665 1449 /* Fall-through after warning */
ab4cf058
MD
1450 }
1451 }
1452 if (encoding_c && !strcmp(encoding_c, "ASCII"))
1453 encoding = CTF_STRING_ASCII;
1454 string_declaration = string_declaration_new(encoding);
1455 return &string_declaration->p;
1456}
1457
1458
05628561 1459static
78af2bcd
MD
1460struct declaration *ctf_type_specifier_list_visit(FILE *fd,
1461 int depth, struct ctf_node *type_specifier_list,
ab4cf058
MD
1462 struct declaration_scope *declaration_scope,
1463 struct ctf_trace *trace)
05628561 1464{
a0720417 1465 struct ctf_node *first;
78af2bcd 1466 struct ctf_node *node;
d20f5e59 1467
427c09b7
MD
1468 assert(type_specifier_list->type == NODE_TYPE_SPECIFIER_LIST);
1469
78af2bcd 1470 first = _cds_list_first_entry(&type_specifier_list->u.type_specifier_list.head, struct ctf_node, siblings);
05628561 1471
78af2bcd
MD
1472 assert(first->type == NODE_TYPE_SPECIFIER);
1473
1474 node = first->u.type_specifier.node;
1475
1476 switch (first->u.type_specifier.type) {
1477 case TYPESPEC_FLOATING_POINT:
1478 return ctf_declaration_floating_point_visit(fd, depth,
1479 &node->u.floating_point.expressions, trace);
1480 case TYPESPEC_INTEGER:
1481 return ctf_declaration_integer_visit(fd, depth,
1482 &node->u.integer.expressions, trace);
1483 case TYPESPEC_STRING:
1484 return ctf_declaration_string_visit(fd, depth,
5039b4cc 1485 &node->u.string.expressions, trace);
78af2bcd 1486 case TYPESPEC_STRUCT:
add40b62 1487 return ctf_declaration_struct_visit(fd, depth,
78af2bcd
MD
1488 node->u._struct.name,
1489 &node->u._struct.declaration_list,
1490 node->u._struct.has_body,
b7e35bad 1491 &node->u._struct.min_align,
ab4cf058
MD
1492 declaration_scope,
1493 trace);
78af2bcd 1494 case TYPESPEC_VARIANT:
add40b62 1495 return ctf_declaration_variant_visit(fd, depth,
78af2bcd
MD
1496 node->u.variant.name,
1497 node->u.variant.choice,
1498 &node->u.variant.declaration_list,
1499 node->u.variant.has_body,
ab4cf058
MD
1500 declaration_scope,
1501 trace);
78af2bcd 1502 case TYPESPEC_ENUM:
add40b62 1503 return ctf_declaration_enum_visit(fd, depth,
78af2bcd
MD
1504 node->u._enum.enum_id,
1505 node->u._enum.container_type,
1506 &node->u._enum.enumerator_list,
1507 node->u._enum.has_body,
1cfda062 1508 declaration_scope,
ab4cf058 1509 trace);
78af2bcd
MD
1510
1511 case TYPESPEC_VOID:
1512 case TYPESPEC_CHAR:
1513 case TYPESPEC_SHORT:
1514 case TYPESPEC_INT:
1515 case TYPESPEC_LONG:
1516 case TYPESPEC_FLOAT:
1517 case TYPESPEC_DOUBLE:
1518 case TYPESPEC_SIGNED:
1519 case TYPESPEC_UNSIGNED:
1520 case TYPESPEC_BOOL:
1521 case TYPESPEC_COMPLEX:
1522 case TYPESPEC_IMAGINARY:
1523 case TYPESPEC_CONST:
1524 case TYPESPEC_ID_TYPE:
add40b62 1525 return ctf_declaration_type_specifier_visit(fd, depth,
78af2bcd 1526 type_specifier_list, declaration_scope);
a0720417 1527 default:
78af2bcd 1528 fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) first->u.type_specifier.type);
a0720417 1529 return NULL;
add40b62 1530 }
05628561
MD
1531}
1532
1533static
1534int ctf_event_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_event *event, struct ctf_trace *trace)
1535{
1536 int ret = 0;
1537
1538 switch (node->type) {
1539 case NODE_TYPEDEF:
1540 ret = ctf_typedef_visit(fd, depth + 1,
a0720417 1541 event->declaration_scope,
78af2bcd 1542 node->u._typedef.type_specifier_list,
05628561 1543 &node->u._typedef.type_declarators,
a0720417 1544 trace);
05628561
MD
1545 if (ret)
1546 return ret;
1547 break;
1548 case NODE_TYPEALIAS:
1549 ret = ctf_typealias_visit(fd, depth + 1,
a0720417
MD
1550 event->declaration_scope,
1551 node->u.typealias.target, node->u.typealias.alias,
1552 trace);
05628561
MD
1553 if (ret)
1554 return ret;
1555 break;
1556 case NODE_CTF_EXPRESSION:
1557 {
1558 char *left;
1559
1560 left = concatenate_unary_strings(&node->u.ctf_expression.left);
1561 if (!strcmp(left, "name")) {
1562 char *right;
1563
427c09b7
MD
1564 if (CTF_EVENT_FIELD_IS_SET(event, name)) {
1565 fprintf(fd, "[error] %s: name already declared in event declaration\n", __func__);
0f980a35
MD
1566 ret = -EPERM;
1567 goto error;
427c09b7 1568 }
05628561
MD
1569 right = concatenate_unary_strings(&node->u.ctf_expression.right);
1570 if (!right) {
78af2bcd 1571 fprintf(fd, "[error] %s: unexpected unary expression for event name\n", __func__);
0f980a35
MD
1572 ret = -EINVAL;
1573 goto error;
05628561
MD
1574 }
1575 event->name = g_quark_from_string(right);
41253107 1576 g_free(right);
05628561
MD
1577 CTF_EVENT_SET_FIELD(event, name);
1578 } else if (!strcmp(left, "id")) {
427c09b7
MD
1579 if (CTF_EVENT_FIELD_IS_SET(event, id)) {
1580 fprintf(fd, "[error] %s: id already declared in event declaration\n", __func__);
0f980a35
MD
1581 ret = -EPERM;
1582 goto error;
427c09b7 1583 }
05628561
MD
1584 ret = get_unary_unsigned(&node->u.ctf_expression.right, &event->id);
1585 if (ret) {
78af2bcd 1586 fprintf(fd, "[error] %s: unexpected unary expression for event id\n", __func__);
0f980a35
MD
1587 ret = -EINVAL;
1588 goto error;
05628561
MD
1589 }
1590 CTF_EVENT_SET_FIELD(event, id);
1591 } else if (!strcmp(left, "stream_id")) {
427c09b7
MD
1592 if (CTF_EVENT_FIELD_IS_SET(event, stream_id)) {
1593 fprintf(fd, "[error] %s: stream_id already declared in event declaration\n", __func__);
0f980a35
MD
1594 ret = -EPERM;
1595 goto error;
427c09b7 1596 }
05628561
MD
1597 ret = get_unary_unsigned(&node->u.ctf_expression.right, &event->stream_id);
1598 if (ret) {
78af2bcd 1599 fprintf(fd, "[error] %s: unexpected unary expression for event stream_id\n", __func__);
0f980a35
MD
1600 ret = -EINVAL;
1601 goto error;
05628561
MD
1602 }
1603 event->stream = trace_stream_lookup(trace, event->stream_id);
1604 if (!event->stream) {
78af2bcd 1605 fprintf(fd, "[error] %s: stream id %" PRIu64 " cannot be found\n", __func__, event->stream_id);
0f980a35
MD
1606 ret = -EINVAL;
1607 goto error;
05628561 1608 }
05628561
MD
1609 CTF_EVENT_SET_FIELD(event, stream_id);
1610 } else if (!strcmp(left, "context")) {
1611 struct declaration *declaration;
1612
427c09b7
MD
1613 if (event->context_decl) {
1614 fprintf(fd, "[error] %s: context already declared in event declaration\n", __func__);
0f980a35
MD
1615 ret = -EINVAL;
1616 goto error;
427c09b7 1617 }
78af2bcd
MD
1618 declaration = ctf_type_specifier_list_visit(fd, depth,
1619 _cds_list_first_entry(&node->u.ctf_expression.right,
1620 struct ctf_node, siblings),
ab4cf058 1621 event->declaration_scope, trace);
0f980a35
MD
1622 if (!declaration) {
1623 ret = -EPERM;
1624 goto error;
1625 }
1626 if (declaration->id != CTF_TYPE_STRUCT) {
1627 ret = -EPERM;
1628 goto error;
1629 }
9e29e16e 1630 event->context_decl = container_of(declaration, struct declaration_struct, p);
05628561
MD
1631 } else if (!strcmp(left, "fields")) {
1632 struct declaration *declaration;
1633
427c09b7
MD
1634 if (event->fields_decl) {
1635 fprintf(fd, "[error] %s: fields already declared in event declaration\n", __func__);
0f980a35
MD
1636 ret = -EINVAL;
1637 goto error;
427c09b7 1638 }
78af2bcd
MD
1639 declaration = ctf_type_specifier_list_visit(fd, depth,
1640 _cds_list_first_entry(&node->u.ctf_expression.right,
1641 struct ctf_node, siblings),
ab4cf058 1642 event->declaration_scope, trace);
0f980a35
MD
1643 if (!declaration) {
1644 ret = -EPERM;
1645 goto error;
1646 }
1647 if (declaration->id != CTF_TYPE_STRUCT) {
1648 ret = -EPERM;
1649 goto error;
1650 }
9e29e16e 1651 event->fields_decl = container_of(declaration, struct declaration_struct, p);
306eeaa6
MD
1652 } else if (!strcmp(left, "loglevel")) {
1653 int64_t loglevel = -1;
d86d62f8 1654
306eeaa6
MD
1655 if (CTF_EVENT_FIELD_IS_SET(event, loglevel)) {
1656 fprintf(fd, "[error] %s: loglevel already declared in event declaration\n", __func__);
d86d62f8
MD
1657 ret = -EPERM;
1658 goto error;
1659 }
306eeaa6
MD
1660 ret = get_unary_signed(&node->u.ctf_expression.right, &loglevel);
1661 event->loglevel = (int) loglevel;
d86d62f8 1662 if (ret) {
306eeaa6 1663 fprintf(fd, "[error] %s: unexpected unary expression for event loglevel\n", __func__);
d86d62f8
MD
1664 ret = -EINVAL;
1665 goto error;
1666 }
306eeaa6 1667 CTF_EVENT_SET_FIELD(event, loglevel);
98df1c9f 1668 } else {
b3ab6665
MD
1669 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in event declaration.\n", __func__, left);
1670 /* Fall-through after warning */
05628561 1671 }
0f980a35 1672error:
41253107 1673 g_free(left);
05628561
MD
1674 break;
1675 }
1676 default:
1677 return -EPERM;
1678 /* TODO: declaration specifier should be added. */
1679 }
1680
0f980a35 1681 return ret;
05628561
MD
1682}
1683
1684static
1685int ctf_event_visit(FILE *fd, int depth, struct ctf_node *node,
d20f5e59 1686 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace)
05628561
MD
1687{
1688 int ret = 0;
1689 struct ctf_node *iter;
1690 struct ctf_event *event;
1691
1692 event = g_new0(struct ctf_event, 1);
d20f5e59 1693 event->declaration_scope = new_declaration_scope(parent_declaration_scope);
306eeaa6 1694 event->loglevel = -1;
05628561
MD
1695 cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
1696 ret = ctf_event_declaration_visit(fd, depth + 1, iter, event, trace);
1697 if (ret)
1698 goto error;
1699 }
1700 if (!CTF_EVENT_FIELD_IS_SET(event, name)) {
1701 ret = -EPERM;
427c09b7 1702 fprintf(fd, "[error] %s: missing name field in event declaration\n", __func__);
05628561
MD
1703 goto error;
1704 }
05628561 1705 if (!CTF_EVENT_FIELD_IS_SET(event, stream_id)) {
0f980a35 1706 /* Allow missing stream_id if there is only a single stream */
33105c61
MD
1707 switch (trace->streams->len) {
1708 case 0: /* Create stream if there was none. */
1709 ret = ctf_stream_visit(fd, depth, NULL, trace->root_declaration_scope, trace);
1710 if (ret)
1711 goto error;
1712 /* Fall-through */
1713 case 1:
0f980a35
MD
1714 event->stream_id = 0;
1715 event->stream = trace_stream_lookup(trace, event->stream_id);
33105c61
MD
1716 break;
1717 default:
0f980a35
MD
1718 ret = -EPERM;
1719 fprintf(fd, "[error] %s: missing stream_id field in event declaration\n", __func__);
1720 goto error;
1721 }
05628561 1722 }
8c572eba
MD
1723 /* Allow only one event without id per stream */
1724 if (!CTF_EVENT_FIELD_IS_SET(event, id)
1725 && event->stream->events_by_id->len != 0) {
1726 ret = -EPERM;
1727 fprintf(fd, "[error] %s: missing id field in event declaration\n", __func__);
1728 goto error;
1729 }
05628561
MD
1730 if (event->stream->events_by_id->len <= event->id)
1731 g_ptr_array_set_size(event->stream->events_by_id, event->id + 1);
1732 g_ptr_array_index(event->stream->events_by_id, event->id) = event;
1733 g_hash_table_insert(event->stream->event_quark_to_id,
56e60373 1734 (gpointer) (unsigned long) event->name,
05628561
MD
1735 &event->id);
1736 return 0;
1737
1738error:
98df1c9f
MD
1739 if (event->fields_decl)
1740 declaration_unref(&event->fields_decl->p);
1741 if (event->context_decl)
1742 declaration_unref(&event->context_decl->p);
d20f5e59 1743 free_declaration_scope(event->declaration_scope);
05628561
MD
1744 g_free(event);
1745 return ret;
1746}
1747
1748
1749static
aa6bffae 1750int ctf_stream_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_stream_class *stream, struct ctf_trace *trace)
05628561
MD
1751{
1752 int ret = 0;
1753
1754 switch (node->type) {
1755 case NODE_TYPEDEF:
1756 ret = ctf_typedef_visit(fd, depth + 1,
a0720417 1757 stream->declaration_scope,
78af2bcd 1758 node->u._typedef.type_specifier_list,
05628561 1759 &node->u._typedef.type_declarators,
a0720417 1760 trace);
05628561
MD
1761 if (ret)
1762 return ret;
1763 break;
1764 case NODE_TYPEALIAS:
1765 ret = ctf_typealias_visit(fd, depth + 1,
a0720417
MD
1766 stream->declaration_scope,
1767 node->u.typealias.target, node->u.typealias.alias,
1768 trace);
05628561
MD
1769 if (ret)
1770 return ret;
1771 break;
1772 case NODE_CTF_EXPRESSION:
1773 {
1774 char *left;
1775
1776 left = concatenate_unary_strings(&node->u.ctf_expression.left);
427c09b7
MD
1777 if (!strcmp(left, "id")) {
1778 if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) {
1779 fprintf(fd, "[error] %s: id already declared in stream declaration\n", __func__);
0f980a35
MD
1780 ret = -EPERM;
1781 goto error;
427c09b7 1782 }
a0720417 1783 ret = get_unary_unsigned(&node->u.ctf_expression.right, &stream->stream_id);
05628561 1784 if (ret) {
427c09b7 1785 fprintf(fd, "[error] %s: unexpected unary expression for stream id\n", __func__);
0f980a35
MD
1786 ret = -EINVAL;
1787 goto error;
05628561 1788 }
a0720417 1789 CTF_STREAM_SET_FIELD(stream, stream_id);
9e29e16e 1790 } else if (!strcmp(left, "event.header")) {
05628561
MD
1791 struct declaration *declaration;
1792
427c09b7
MD
1793 if (stream->event_header_decl) {
1794 fprintf(fd, "[error] %s: event.header already declared in stream declaration\n", __func__);
0f980a35
MD
1795 ret = -EINVAL;
1796 goto error;
427c09b7 1797 }
78af2bcd
MD
1798 declaration = ctf_type_specifier_list_visit(fd, depth,
1799 _cds_list_first_entry(&node->u.ctf_expression.right,
1800 struct ctf_node, siblings),
a0720417 1801 stream->declaration_scope, trace);
0f980a35
MD
1802 if (!declaration) {
1803 ret = -EPERM;
1804 goto error;
1805 }
1806 if (declaration->id != CTF_TYPE_STRUCT) {
1807 ret = -EPERM;
1808 goto error;
1809 }
9e29e16e
MD
1810 stream->event_header_decl = container_of(declaration, struct declaration_struct, p);
1811 } else if (!strcmp(left, "event.context")) {
05628561
MD
1812 struct declaration *declaration;
1813
427c09b7
MD
1814 if (stream->event_context_decl) {
1815 fprintf(fd, "[error] %s: event.context already declared in stream declaration\n", __func__);
0f980a35
MD
1816 ret = -EINVAL;
1817 goto error;
427c09b7 1818 }
78af2bcd
MD
1819 declaration = ctf_type_specifier_list_visit(fd, depth,
1820 _cds_list_first_entry(&node->u.ctf_expression.right,
1821 struct ctf_node, siblings),
ab4cf058 1822 stream->declaration_scope, trace);
0f980a35
MD
1823 if (!declaration) {
1824 ret = -EPERM;
1825 goto error;
1826 }
1827 if (declaration->id != CTF_TYPE_STRUCT) {
1828 ret = -EPERM;
1829 goto error;
1830 }
9e29e16e
MD
1831 stream->event_context_decl = container_of(declaration, struct declaration_struct, p);
1832 } else if (!strcmp(left, "packet.context")) {
05628561
MD
1833 struct declaration *declaration;
1834
427c09b7
MD
1835 if (stream->packet_context_decl) {
1836 fprintf(fd, "[error] %s: packet.context already declared in stream declaration\n", __func__);
0f980a35
MD
1837 ret = -EINVAL;
1838 goto error;
427c09b7 1839 }
78af2bcd
MD
1840 declaration = ctf_type_specifier_list_visit(fd, depth,
1841 _cds_list_first_entry(&node->u.ctf_expression.right,
1842 struct ctf_node, siblings),
ab4cf058 1843 stream->declaration_scope, trace);
0f980a35
MD
1844 if (!declaration) {
1845 ret = -EPERM;
1846 goto error;
1847 }
1848 if (declaration->id != CTF_TYPE_STRUCT) {
1849 ret = -EPERM;
1850 goto error;
1851 }
9e29e16e 1852 stream->packet_context_decl = container_of(declaration, struct declaration_struct, p);
98df1c9f 1853 } else {
b3ab6665
MD
1854 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__, left);
1855 /* Fall-through after warning */
05628561 1856 }
98df1c9f 1857
0f980a35 1858error:
41253107 1859 g_free(left);
05628561
MD
1860 break;
1861 }
1862 default:
1863 return -EPERM;
1864 /* TODO: declaration specifier should be added. */
1865 }
1866
0f980a35 1867 return ret;
05628561
MD
1868}
1869
1870static
1871int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node,
d20f5e59 1872 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace)
05628561
MD
1873{
1874 int ret = 0;
1875 struct ctf_node *iter;
aa6bffae 1876 struct ctf_stream_class *stream;
05628561 1877
aa6bffae 1878 stream = g_new0(struct ctf_stream_class, 1);
d20f5e59 1879 stream->declaration_scope = new_declaration_scope(parent_declaration_scope);
05628561 1880 stream->events_by_id = g_ptr_array_new();
068665f5 1881 stream->event_quark_to_id = g_hash_table_new(g_direct_hash, g_direct_equal);
2d0bea29 1882 stream->streams = g_ptr_array_new();
33105c61
MD
1883 if (node) {
1884 cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
1885 ret = ctf_stream_declaration_visit(fd, depth + 1, iter, stream, trace);
1886 if (ret)
1887 goto error;
1888 }
05628561 1889 }
0f980a35
MD
1890 if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) {
1891 /* check that packet header has stream_id field. */
1892 if (!trace->packet_header_decl
1893 || struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("stream_id")) < 0) {
1894 ret = -EPERM;
1895 fprintf(fd, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__);
1896 goto error;
1897 }
8c572eba
MD
1898 } else {
1899 /* Allow only one id-less stream */
1900 if (trace->streams->len != 0) {
1901 ret = -EPERM;
1902 fprintf(fd, "[error] %s: missing id field in stream declaration\n", __func__);
1903 goto error;
1904 }
1905 stream->stream_id = 0;
05628561
MD
1906 }
1907 if (trace->streams->len <= stream->stream_id)
1908 g_ptr_array_set_size(trace->streams, stream->stream_id + 1);
1909 g_ptr_array_index(trace->streams, stream->stream_id) = stream;
82662ad4 1910 stream->trace = trace;
9e29e16e 1911
05628561
MD
1912 return 0;
1913
1914error:
98df1c9f
MD
1915 if (stream->event_header_decl)
1916 declaration_unref(&stream->event_header_decl->p);
1917 if (stream->event_context_decl)
1918 declaration_unref(&stream->event_context_decl->p);
1919 if (stream->packet_context_decl)
1920 declaration_unref(&stream->packet_context_decl->p);
2d0bea29 1921 g_ptr_array_free(stream->streams, TRUE);
05628561 1922 g_ptr_array_free(stream->events_by_id, TRUE);
a0720417 1923 g_hash_table_destroy(stream->event_quark_to_id);
d20f5e59 1924 free_declaration_scope(stream->declaration_scope);
05628561
MD
1925 g_free(stream);
1926 return ret;
1927}
1928
1929static
1930int ctf_trace_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
1931{
1932 int ret = 0;
1933
1934 switch (node->type) {
1935 case NODE_TYPEDEF:
1936 ret = ctf_typedef_visit(fd, depth + 1,
a0720417 1937 trace->declaration_scope,
78af2bcd 1938 node->u._typedef.type_specifier_list,
05628561 1939 &node->u._typedef.type_declarators,
a0720417 1940 trace);
05628561
MD
1941 if (ret)
1942 return ret;
1943 break;
1944 case NODE_TYPEALIAS:
1945 ret = ctf_typealias_visit(fd, depth + 1,
a0720417
MD
1946 trace->declaration_scope,
1947 node->u.typealias.target, node->u.typealias.alias,
1948 trace);
05628561
MD
1949 if (ret)
1950 return ret;
1951 break;
1952 case NODE_CTF_EXPRESSION:
1953 {
1954 char *left;
1955
1956 left = concatenate_unary_strings(&node->u.ctf_expression.left);
1957 if (!strcmp(left, "major")) {
427c09b7
MD
1958 if (CTF_TRACE_FIELD_IS_SET(trace, major)) {
1959 fprintf(fd, "[error] %s: major already declared in trace declaration\n", __func__);
0f980a35
MD
1960 ret = -EPERM;
1961 goto error;
427c09b7 1962 }
05628561
MD
1963 ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->major);
1964 if (ret) {
78af2bcd 1965 fprintf(fd, "[error] %s: unexpected unary expression for trace major number\n", __func__);
0f980a35
MD
1966 ret = -EINVAL;
1967 goto error;
05628561 1968 }
a0720417 1969 CTF_TRACE_SET_FIELD(trace, major);
05628561 1970 } else if (!strcmp(left, "minor")) {
427c09b7
MD
1971 if (CTF_TRACE_FIELD_IS_SET(trace, minor)) {
1972 fprintf(fd, "[error] %s: minor already declared in trace declaration\n", __func__);
0f980a35
MD
1973 ret = -EPERM;
1974 goto error;
427c09b7 1975 }
05628561
MD
1976 ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->minor);
1977 if (ret) {
78af2bcd 1978 fprintf(fd, "[error] %s: unexpected unary expression for trace minor number\n", __func__);
0f980a35
MD
1979 ret = -EINVAL;
1980 goto error;
05628561 1981 }
a0720417 1982 CTF_TRACE_SET_FIELD(trace, minor);
05628561 1983 } else if (!strcmp(left, "uuid")) {
b4c19c1e 1984 uuid_t uuid;
a0fe7d97
MD
1985
1986 ret = get_unary_uuid(&node->u.ctf_expression.right, &uuid);
05628561 1987 if (ret) {
78af2bcd 1988 fprintf(fd, "[error] %s: unexpected unary expression for trace uuid\n", __func__);
0f980a35
MD
1989 ret = -EINVAL;
1990 goto error;
05628561 1991 }
b4c19c1e
MD
1992 if (CTF_TRACE_FIELD_IS_SET(trace, uuid)
1993 && uuid_compare(uuid, trace->uuid)) {
1994 fprintf(fd, "[error] %s: uuid mismatch\n", __func__);
1995 ret = -EPERM;
1996 goto error;
a0fe7d97
MD
1997 } else {
1998 memcpy(trace->uuid, uuid, sizeof(uuid));
b4c19c1e 1999 }
a0720417 2000 CTF_TRACE_SET_FIELD(trace, uuid);
0f980a35
MD
2001 } else if (!strcmp(left, "byte_order")) {
2002 struct ctf_node *right;
2003 int byte_order;
2004
0f980a35
MD
2005 right = _cds_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings);
2006 byte_order = get_trace_byte_order(fd, depth, right);
2007 if (byte_order < 0)
2008 return -EINVAL;
a0fe7d97
MD
2009
2010 if (CTF_TRACE_FIELD_IS_SET(trace, byte_order)
2011 && byte_order != trace->byte_order) {
2012 fprintf(fd, "[error] %s: endianness mismatch\n", __func__);
2013 ret = -EPERM;
2014 goto error;
2015 } else {
fdce39de
MD
2016 if (byte_order != trace->byte_order) {
2017 trace->byte_order = byte_order;
2018 /*
2019 * We need to restart
2020 * construction of the
2021 * intermediate representation.
2022 */
2e0c6b58
MD
2023 trace->field_mask = 0;
2024 CTF_TRACE_SET_FIELD(trace, byte_order);
2025 ret = -EINTR;
2026 goto error;
fdce39de 2027 }
a0fe7d97 2028 }
7c8a1386 2029 CTF_TRACE_SET_FIELD(trace, byte_order);
0f980a35
MD
2030 } else if (!strcmp(left, "packet.header")) {
2031 struct declaration *declaration;
2032
2033 if (trace->packet_header_decl) {
2034 fprintf(fd, "[error] %s: packet.header already declared in trace declaration\n", __func__);
2035 ret = -EINVAL;
2036 goto error;
2037 }
2038 declaration = ctf_type_specifier_list_visit(fd, depth,
2039 _cds_list_first_entry(&node->u.ctf_expression.right,
2040 struct ctf_node, siblings),
2041 trace->declaration_scope, trace);
2042 if (!declaration) {
2043 ret = -EPERM;
2044 goto error;
2045 }
2046 if (declaration->id != CTF_TYPE_STRUCT) {
2047 ret = -EPERM;
2048 goto error;
2049 }
2050 trace->packet_header_decl = container_of(declaration, struct declaration_struct, p);
98df1c9f 2051 } else {
b3ab6665 2052 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__, left);
05628561 2053 }
98df1c9f 2054
0f980a35 2055error:
41253107 2056 g_free(left);
05628561
MD
2057 break;
2058 }
2059 default:
2060 return -EPERM;
2061 /* TODO: declaration specifier should be added. */
2062 }
2063
0f980a35 2064 return ret;
05628561
MD
2065}
2066
05628561
MD
2067static
2068int ctf_trace_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2069{
2070 int ret = 0;
2071 struct ctf_node *iter;
2072
d20f5e59 2073 if (trace->declaration_scope)
05628561 2074 return -EEXIST;
d20f5e59 2075 trace->declaration_scope = new_declaration_scope(trace->root_declaration_scope);
05628561
MD
2076 trace->streams = g_ptr_array_new();
2077 cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
2078 ret = ctf_trace_declaration_visit(fd, depth + 1, iter, trace);
2079 if (ret)
2080 goto error;
2081 }
a0720417 2082 if (!CTF_TRACE_FIELD_IS_SET(trace, major)) {
05628561 2083 ret = -EPERM;
427c09b7 2084 fprintf(fd, "[error] %s: missing major field in trace declaration\n", __func__);
05628561
MD
2085 goto error;
2086 }
a0720417 2087 if (!CTF_TRACE_FIELD_IS_SET(trace, minor)) {
05628561 2088 ret = -EPERM;
427c09b7 2089 fprintf(fd, "[error] %s: missing minor field in trace declaration\n", __func__);
05628561
MD
2090 goto error;
2091 }
a0720417 2092 if (!CTF_TRACE_FIELD_IS_SET(trace, uuid)) {
05628561 2093 ret = -EPERM;
427c09b7 2094 fprintf(fd, "[error] %s: missing uuid field in trace declaration\n", __func__);
05628561
MD
2095 goto error;
2096 }
dc48ecad
MD
2097 if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) {
2098 ret = -EPERM;
2099 fprintf(fd, "[error] %s: missing byte_order field in trace declaration\n", __func__);
2100 goto error;
2101 }
0f980a35 2102
0f980a35
MD
2103 if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) {
2104 /* check that the packet header contains a "magic" field */
e28d4618 2105 if (!trace->packet_header_decl
0f980a35
MD
2106 || struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("magic")) < 0) {
2107 ret = -EPERM;
2108 fprintf(fd, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__);
98df1c9f 2109 goto error;
0f980a35
MD
2110 }
2111 }
05628561
MD
2112 return 0;
2113
2114error:
0d72513f 2115 if (trace->packet_header_decl) {
98df1c9f 2116 declaration_unref(&trace->packet_header_decl->p);
0d72513f
MD
2117 trace->packet_header_decl = NULL;
2118 }
05628561 2119 g_ptr_array_free(trace->streams, TRUE);
a0720417 2120 free_declaration_scope(trace->declaration_scope);
fdce39de 2121 trace->declaration_scope = NULL;
05628561
MD
2122 return ret;
2123}
2124
50cb9c56
MD
2125static
2126int ctf_clock_declaration_visit(FILE *fd, int depth, struct ctf_node *node,
2127 struct ctf_clock *clock, struct ctf_trace *trace)
2128{
2129 int ret = 0;
2130
2131 switch (node->type) {
2132 case NODE_CTF_EXPRESSION:
2133 {
2134 char *left;
2135
2136 left = concatenate_unary_strings(&node->u.ctf_expression.left);
2137 if (!strcmp(left, "name")) {
2138 char *right;
2139
2140 if (CTF_CLOCK_FIELD_IS_SET(clock, name)) {
2141 fprintf(fd, "[error] %s: name already declared in clock declaration\n", __func__);
2142 ret = -EPERM;
2143 goto error;
2144 }
2145 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2146 if (!right) {
2147 fprintf(fd, "[error] %s: unexpected unary expression for clock name\n", __func__);
2148 ret = -EINVAL;
2149 goto error;
2150 }
2151 clock->name = g_quark_from_string(right);
2152 g_free(right);
2153 CTF_EVENT_SET_FIELD(clock, name);
2154 } else if (!strcmp(left, "uuid")) {
2155 char *right;
2156
2157 if (clock->uuid) {
2158 fprintf(fd, "[error] %s: uuid already declared in clock declaration\n", __func__);
2159 ret = -EPERM;
2160 goto error;
2161 }
2162 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2163 if (!right) {
2164 fprintf(fd, "[error] %s: unexpected unary expression for clock uuid\n", __func__);
2165 ret = -EINVAL;
2166 goto error;
2167 }
2168 clock->uuid = g_quark_from_string(right);
2169 g_free(right);
2170 } else if (!strcmp(left, "description")) {
2171 char *right;
2172
2173 if (clock->description) {
2174 fprintf(fd, "[warning] %s: duplicated clock description\n", __func__);
2175 goto error; /* ret is 0, so not an actual error, just warn. */
2176 }
2177 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2178 if (!right) {
2179 fprintf(fd, "[warning] %s: unexpected unary expression for clock description\n", __func__);
2180 goto error; /* ret is 0, so not an actual error, just warn. */
2181 }
2182 clock->description = right;
2183 } else if (!strcmp(left, "freq")) {
2184 if (clock->freq) {
2185 fprintf(fd, "[error] %s: freq already declared in clock declaration\n", __func__);
2186 ret = -EPERM;
2187 goto error;
2188 }
2189 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->freq);
2190 if (ret) {
2191 fprintf(fd, "[error] %s: unexpected unary expression for clock freq\n", __func__);
2192 ret = -EINVAL;
2193 goto error;
2194 }
2195 } else if (!strcmp(left, "precision")) {
2196 if (clock->precision) {
2197 fprintf(fd, "[error] %s: precision already declared in clock declaration\n", __func__);
2198 ret = -EPERM;
2199 goto error;
2200 }
2201 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->precision);
2202 if (ret) {
2203 fprintf(fd, "[error] %s: unexpected unary expression for clock precision\n", __func__);
2204 ret = -EINVAL;
2205 goto error;
2206 }
2207 } else if (!strcmp(left, "offset_s")) {
2208 if (clock->offset_s) {
2209 fprintf(fd, "[error] %s: offset_s already declared in clock declaration\n", __func__);
2210 ret = -EPERM;
2211 goto error;
2212 }
2213 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->offset_s);
2214 if (ret) {
2215 fprintf(fd, "[error] %s: unexpected unary expression for clock offset_s\n", __func__);
2216 ret = -EINVAL;
2217 goto error;
2218 }
2219 } else if (!strcmp(left, "offset")) {
2220 if (clock->offset) {
2221 fprintf(fd, "[error] %s: offset already declared in clock declaration\n", __func__);
2222 ret = -EPERM;
2223 goto error;
2224 }
2225 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->offset);
2226 if (ret) {
2227 fprintf(fd, "[error] %s: unexpected unary expression for clock offset\n", __func__);
2228 ret = -EINVAL;
2229 goto error;
2230 }
11ac6674
MD
2231 } else if (!strcmp(left, "absolute")) {
2232 struct ctf_node *right;
2233
2234 right = _cds_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings);
2235 ret = get_boolean(fd, depth, right);
2236 if (ret < 0) {
2237 fprintf(fd, "[error] %s: unexpected \"absolute\" right member\n", __func__);
2238 ret = -EINVAL;
2239 goto error;
2240 }
2241 clock->absolute = ret;
50cb9c56
MD
2242 } else {
2243 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in clock declaration.\n", __func__, left);
2244 }
2245
2246error:
2247 g_free(left);
2248 break;
2249 }
2250 default:
2251 return -EPERM;
2252 /* TODO: declaration specifier should be added. */
2253 }
2254
2255 return ret;
2256}
2257
2258static
2259int ctf_clock_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2260{
2261 int ret = 0;
2262 struct ctf_node *iter;
2263 struct ctf_clock *clock;
2264
2265 clock = g_new0(struct ctf_clock, 1);
2266 cds_list_for_each_entry(iter, &node->u.clock.declaration_list, siblings) {
2267 ret = ctf_clock_declaration_visit(fd, depth + 1, iter, clock, trace);
2268 if (ret)
2269 goto error;
2270 }
2271 if (!CTF_CLOCK_FIELD_IS_SET(clock, name)) {
2272 ret = -EPERM;
2273 fprintf(fd, "[error] %s: missing namefield in clock declaration\n", __func__);
2274 goto error;
2275 }
2276 g_hash_table_insert(trace->clocks, (gpointer) (unsigned long) clock->name, clock);
2277 return 0;
2278
2279error:
2280 g_free(clock->description);
2281 g_free(clock);
2282 return ret;
2283}
2284
2285static
2286void clock_free(gpointer data)
2287{
2288 struct ctf_clock *clock = data;
2289
2290 g_free(clock->description);
2291 g_free(clock);
2292}
2293
e2c76a4d
MD
2294static
2295int ctf_env_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2296{
2297 fprintf(fd, "[warning] %s: environment declaration support not implement yet.\n", __func__);
2298 return 0; /* continue */
2299}
2300
78af2bcd
MD
2301static
2302int ctf_root_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2303{
2304 int ret = 0;
2305
2306 switch (node->type) {
2307 case NODE_TYPEDEF:
2308 ret = ctf_typedef_visit(fd, depth + 1,
2309 trace->root_declaration_scope,
2310 node->u._typedef.type_specifier_list,
2311 &node->u._typedef.type_declarators,
2312 trace);
2313 if (ret)
2314 return ret;
2315 break;
2316 case NODE_TYPEALIAS:
2317 ret = ctf_typealias_visit(fd, depth + 1,
2318 trace->root_declaration_scope,
2319 node->u.typealias.target, node->u.typealias.alias,
2320 trace);
2321 if (ret)
2322 return ret;
2323 break;
2324 case NODE_TYPE_SPECIFIER_LIST:
2325 {
2326 struct declaration *declaration;
2327
2328 /*
2329 * Just add the type specifier to the root scope
2330 * declaration scope. Release local reference.
2331 */
2332 declaration = ctf_type_specifier_list_visit(fd, depth + 1,
2333 node, trace->root_declaration_scope, trace);
2334 if (!declaration)
2335 return -ENOMEM;
2336 declaration_unref(declaration);
2337 break;
2338 }
2339 default:
2340 return -EPERM;
2341 }
2342
2343 return 0;
2344}
2345
50cb9c56 2346/* TODO: missing metadata "destroy" (memory leak) */
ab4cf058
MD
2347int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node,
2348 struct ctf_trace *trace, int byte_order)
05628561
MD
2349{
2350 int ret = 0;
2351 struct ctf_node *iter;
e2c76a4d 2352 int env_clock_done = 0;
05628561 2353
c8b219a3 2354 printf_verbose("CTF visitor: metadata construction... ");
ab4cf058 2355 trace->byte_order = byte_order;
50cb9c56
MD
2356 trace->clocks = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2357 NULL, clock_free);
ab4cf058 2358
00f7c1a5
MD
2359retry:
2360 trace->root_declaration_scope = new_declaration_scope(NULL);
2361
05628561
MD
2362 switch (node->type) {
2363 case NODE_ROOT:
e2c76a4d
MD
2364 if (!env_clock_done) {
2365 cds_list_for_each_entry(iter, &node->u.root.env, siblings) {
2366 ret = ctf_env_visit(fd, depth + 1, iter,
2367 trace);
2368 if (ret) {
2369 fprintf(fd, "[error] %s: env declaration error\n", __func__);
2370 goto error;
2371 }
2372 }
2373
56e60373
MD
2374 /*
2375 * declarations need to query clock hash table,
2376 * so clock need to be treated first.
2377 */
2378 cds_list_for_each_entry(iter, &node->u.root.clock, siblings) {
2379 ret = ctf_clock_visit(fd, depth + 1, iter,
2380 trace);
2381 if (ret) {
2382 fprintf(fd, "[error] %s: clock declaration error\n", __func__);
2383 goto error;
2384 }
2385 }
e2c76a4d 2386 env_clock_done = 1;
56e60373 2387 }
3e11b713 2388 cds_list_for_each_entry(iter, &node->u.root.declaration_list,
05628561 2389 siblings) {
78af2bcd 2390 ret = ctf_root_declaration_visit(fd, depth + 1, iter, trace);
427c09b7
MD
2391 if (ret) {
2392 fprintf(fd, "[error] %s: root declaration error\n", __func__);
a0fe7d97 2393 goto error;
427c09b7 2394 }
05628561
MD
2395 }
2396 cds_list_for_each_entry(iter, &node->u.root.trace, siblings) {
2397 ret = ctf_trace_visit(fd, depth + 1, iter, trace);
00f7c1a5
MD
2398 if (ret == -EINTR) {
2399 free_declaration_scope(trace->root_declaration_scope);
2400 /*
2401 * Need to restart creation of type
2402 * definitions, aliases and
2403 * trace header declarations.
2404 */
2405 goto retry;
2406 }
427c09b7
MD
2407 if (ret) {
2408 fprintf(fd, "[error] %s: trace declaration error\n", __func__);
a0fe7d97 2409 goto error;
427c09b7 2410 }
05628561 2411 }
5039b4cc
MD
2412 if (!trace->streams) {
2413 fprintf(fd, "[error] %s: missing trace declaration\n", __func__);
a0fe7d97
MD
2414 ret = -EINVAL;
2415 goto error;
5039b4cc 2416 }
05628561
MD
2417 cds_list_for_each_entry(iter, &node->u.root.stream, siblings) {
2418 ret = ctf_stream_visit(fd, depth + 1, iter,
41253107 2419 trace->root_declaration_scope, trace);
427c09b7
MD
2420 if (ret) {
2421 fprintf(fd, "[error] %s: stream declaration error\n", __func__);
a0fe7d97 2422 goto error;
427c09b7 2423 }
05628561
MD
2424 }
2425 cds_list_for_each_entry(iter, &node->u.root.event, siblings) {
2426 ret = ctf_event_visit(fd, depth + 1, iter,
41253107 2427 trace->root_declaration_scope, trace);
427c09b7
MD
2428 if (ret) {
2429 fprintf(fd, "[error] %s: event declaration error\n", __func__);
a0fe7d97 2430 goto error;
427c09b7 2431 }
05628561
MD
2432 }
2433 break;
05628561
MD
2434 case NODE_UNKNOWN:
2435 default:
78af2bcd 2436 fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
05628561 2437 (int) node->type);
a0fe7d97
MD
2438 ret = -EINVAL;
2439 goto error;
05628561 2440 }
c8b219a3 2441 printf_verbose("done.\n");
05628561 2442 return ret;
a0fe7d97
MD
2443
2444error:
2445 free_declaration_scope(trace->root_declaration_scope);
50cb9c56 2446 g_hash_table_destroy(trace->clocks);
a0fe7d97 2447 return ret;
05628561 2448}
This page took 0.154519 seconds and 4 git commands to generate.