Implement clock mapping within integer type
[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);
d86d62f8
MD
1652 } else if (!strcmp(left, "loglevel.identifier")) {
1653 char *right;
1654
1655 if (CTF_EVENT_FIELD_IS_SET(event, loglevel_identifier)) {
1656 fprintf(fd, "[error] %s: identifier already declared in event declaration\n", __func__);
1657 ret = -EPERM;
1658 goto error;
1659 }
1660 right = concatenate_unary_strings(&node->u.ctf_expression.right);
1661 if (!right) {
1662 fprintf(fd, "[error] %s: unexpected unary expression for event identifier\n", __func__);
1663 ret = -EINVAL;
1664 goto error;
1665 }
1666 event->loglevel_identifier = g_quark_from_string(right);
1667 g_free(right);
1668 CTF_EVENT_SET_FIELD(event, loglevel_identifier);
1669 } else if (!strcmp(left, "loglevel.value")) {
1670 if (CTF_EVENT_FIELD_IS_SET(event, loglevel_value)) {
1671 fprintf(fd, "[error] %s: loglevel value already declared in event declaration\n", __func__);
1672 ret = -EPERM;
1673 goto error;
1674 }
1675 ret = get_unary_signed(&node->u.ctf_expression.right, &event->loglevel_value);
1676 if (ret) {
1677 fprintf(fd, "[error] %s: unexpected unary expression for event loglevel value\n", __func__);
1678 ret = -EINVAL;
1679 goto error;
1680 }
1681 CTF_EVENT_SET_FIELD(event, loglevel_value);
98df1c9f 1682 } else {
b3ab6665
MD
1683 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in event declaration.\n", __func__, left);
1684 /* Fall-through after warning */
05628561 1685 }
0f980a35 1686error:
41253107 1687 g_free(left);
05628561
MD
1688 break;
1689 }
1690 default:
1691 return -EPERM;
1692 /* TODO: declaration specifier should be added. */
1693 }
1694
0f980a35 1695 return ret;
05628561
MD
1696}
1697
1698static
1699int ctf_event_visit(FILE *fd, int depth, struct ctf_node *node,
d20f5e59 1700 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace)
05628561
MD
1701{
1702 int ret = 0;
1703 struct ctf_node *iter;
1704 struct ctf_event *event;
1705
1706 event = g_new0(struct ctf_event, 1);
d20f5e59 1707 event->declaration_scope = new_declaration_scope(parent_declaration_scope);
05628561
MD
1708 cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
1709 ret = ctf_event_declaration_visit(fd, depth + 1, iter, event, trace);
1710 if (ret)
1711 goto error;
1712 }
1713 if (!CTF_EVENT_FIELD_IS_SET(event, name)) {
1714 ret = -EPERM;
427c09b7 1715 fprintf(fd, "[error] %s: missing name field in event declaration\n", __func__);
05628561
MD
1716 goto error;
1717 }
05628561 1718 if (!CTF_EVENT_FIELD_IS_SET(event, stream_id)) {
0f980a35 1719 /* Allow missing stream_id if there is only a single stream */
33105c61
MD
1720 switch (trace->streams->len) {
1721 case 0: /* Create stream if there was none. */
1722 ret = ctf_stream_visit(fd, depth, NULL, trace->root_declaration_scope, trace);
1723 if (ret)
1724 goto error;
1725 /* Fall-through */
1726 case 1:
0f980a35
MD
1727 event->stream_id = 0;
1728 event->stream = trace_stream_lookup(trace, event->stream_id);
33105c61
MD
1729 break;
1730 default:
0f980a35
MD
1731 ret = -EPERM;
1732 fprintf(fd, "[error] %s: missing stream_id field in event declaration\n", __func__);
1733 goto error;
1734 }
05628561 1735 }
8c572eba
MD
1736 /* Allow only one event without id per stream */
1737 if (!CTF_EVENT_FIELD_IS_SET(event, id)
1738 && event->stream->events_by_id->len != 0) {
1739 ret = -EPERM;
1740 fprintf(fd, "[error] %s: missing id field in event declaration\n", __func__);
1741 goto error;
1742 }
05628561
MD
1743 if (event->stream->events_by_id->len <= event->id)
1744 g_ptr_array_set_size(event->stream->events_by_id, event->id + 1);
1745 g_ptr_array_index(event->stream->events_by_id, event->id) = event;
1746 g_hash_table_insert(event->stream->event_quark_to_id,
56e60373 1747 (gpointer) (unsigned long) event->name,
05628561
MD
1748 &event->id);
1749 return 0;
1750
1751error:
98df1c9f
MD
1752 if (event->fields_decl)
1753 declaration_unref(&event->fields_decl->p);
1754 if (event->context_decl)
1755 declaration_unref(&event->context_decl->p);
d20f5e59 1756 free_declaration_scope(event->declaration_scope);
05628561
MD
1757 g_free(event);
1758 return ret;
1759}
1760
1761
1762static
aa6bffae 1763int ctf_stream_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_stream_class *stream, struct ctf_trace *trace)
05628561
MD
1764{
1765 int ret = 0;
1766
1767 switch (node->type) {
1768 case NODE_TYPEDEF:
1769 ret = ctf_typedef_visit(fd, depth + 1,
a0720417 1770 stream->declaration_scope,
78af2bcd 1771 node->u._typedef.type_specifier_list,
05628561 1772 &node->u._typedef.type_declarators,
a0720417 1773 trace);
05628561
MD
1774 if (ret)
1775 return ret;
1776 break;
1777 case NODE_TYPEALIAS:
1778 ret = ctf_typealias_visit(fd, depth + 1,
a0720417
MD
1779 stream->declaration_scope,
1780 node->u.typealias.target, node->u.typealias.alias,
1781 trace);
05628561
MD
1782 if (ret)
1783 return ret;
1784 break;
1785 case NODE_CTF_EXPRESSION:
1786 {
1787 char *left;
1788
1789 left = concatenate_unary_strings(&node->u.ctf_expression.left);
427c09b7
MD
1790 if (!strcmp(left, "id")) {
1791 if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) {
1792 fprintf(fd, "[error] %s: id already declared in stream declaration\n", __func__);
0f980a35
MD
1793 ret = -EPERM;
1794 goto error;
427c09b7 1795 }
a0720417 1796 ret = get_unary_unsigned(&node->u.ctf_expression.right, &stream->stream_id);
05628561 1797 if (ret) {
427c09b7 1798 fprintf(fd, "[error] %s: unexpected unary expression for stream id\n", __func__);
0f980a35
MD
1799 ret = -EINVAL;
1800 goto error;
05628561 1801 }
a0720417 1802 CTF_STREAM_SET_FIELD(stream, stream_id);
9e29e16e 1803 } else if (!strcmp(left, "event.header")) {
05628561
MD
1804 struct declaration *declaration;
1805
427c09b7
MD
1806 if (stream->event_header_decl) {
1807 fprintf(fd, "[error] %s: event.header already declared in stream declaration\n", __func__);
0f980a35
MD
1808 ret = -EINVAL;
1809 goto error;
427c09b7 1810 }
78af2bcd
MD
1811 declaration = ctf_type_specifier_list_visit(fd, depth,
1812 _cds_list_first_entry(&node->u.ctf_expression.right,
1813 struct ctf_node, siblings),
a0720417 1814 stream->declaration_scope, trace);
0f980a35
MD
1815 if (!declaration) {
1816 ret = -EPERM;
1817 goto error;
1818 }
1819 if (declaration->id != CTF_TYPE_STRUCT) {
1820 ret = -EPERM;
1821 goto error;
1822 }
9e29e16e
MD
1823 stream->event_header_decl = container_of(declaration, struct declaration_struct, p);
1824 } else if (!strcmp(left, "event.context")) {
05628561
MD
1825 struct declaration *declaration;
1826
427c09b7
MD
1827 if (stream->event_context_decl) {
1828 fprintf(fd, "[error] %s: event.context already declared in stream declaration\n", __func__);
0f980a35
MD
1829 ret = -EINVAL;
1830 goto error;
427c09b7 1831 }
78af2bcd
MD
1832 declaration = ctf_type_specifier_list_visit(fd, depth,
1833 _cds_list_first_entry(&node->u.ctf_expression.right,
1834 struct ctf_node, siblings),
ab4cf058 1835 stream->declaration_scope, trace);
0f980a35
MD
1836 if (!declaration) {
1837 ret = -EPERM;
1838 goto error;
1839 }
1840 if (declaration->id != CTF_TYPE_STRUCT) {
1841 ret = -EPERM;
1842 goto error;
1843 }
9e29e16e
MD
1844 stream->event_context_decl = container_of(declaration, struct declaration_struct, p);
1845 } else if (!strcmp(left, "packet.context")) {
05628561
MD
1846 struct declaration *declaration;
1847
427c09b7
MD
1848 if (stream->packet_context_decl) {
1849 fprintf(fd, "[error] %s: packet.context already declared in stream declaration\n", __func__);
0f980a35
MD
1850 ret = -EINVAL;
1851 goto error;
427c09b7 1852 }
78af2bcd
MD
1853 declaration = ctf_type_specifier_list_visit(fd, depth,
1854 _cds_list_first_entry(&node->u.ctf_expression.right,
1855 struct ctf_node, siblings),
ab4cf058 1856 stream->declaration_scope, trace);
0f980a35
MD
1857 if (!declaration) {
1858 ret = -EPERM;
1859 goto error;
1860 }
1861 if (declaration->id != CTF_TYPE_STRUCT) {
1862 ret = -EPERM;
1863 goto error;
1864 }
9e29e16e 1865 stream->packet_context_decl = container_of(declaration, struct declaration_struct, p);
98df1c9f 1866 } else {
b3ab6665
MD
1867 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__, left);
1868 /* Fall-through after warning */
05628561 1869 }
98df1c9f 1870
0f980a35 1871error:
41253107 1872 g_free(left);
05628561
MD
1873 break;
1874 }
1875 default:
1876 return -EPERM;
1877 /* TODO: declaration specifier should be added. */
1878 }
1879
0f980a35 1880 return ret;
05628561
MD
1881}
1882
1883static
1884int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node,
d20f5e59 1885 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace)
05628561
MD
1886{
1887 int ret = 0;
1888 struct ctf_node *iter;
aa6bffae 1889 struct ctf_stream_class *stream;
05628561 1890
aa6bffae 1891 stream = g_new0(struct ctf_stream_class, 1);
d20f5e59 1892 stream->declaration_scope = new_declaration_scope(parent_declaration_scope);
05628561 1893 stream->events_by_id = g_ptr_array_new();
068665f5 1894 stream->event_quark_to_id = g_hash_table_new(g_direct_hash, g_direct_equal);
2d0bea29 1895 stream->streams = g_ptr_array_new();
33105c61
MD
1896 if (node) {
1897 cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
1898 ret = ctf_stream_declaration_visit(fd, depth + 1, iter, stream, trace);
1899 if (ret)
1900 goto error;
1901 }
05628561 1902 }
0f980a35
MD
1903 if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) {
1904 /* check that packet header has stream_id field. */
1905 if (!trace->packet_header_decl
1906 || struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("stream_id")) < 0) {
1907 ret = -EPERM;
1908 fprintf(fd, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__);
1909 goto error;
1910 }
8c572eba
MD
1911 } else {
1912 /* Allow only one id-less stream */
1913 if (trace->streams->len != 0) {
1914 ret = -EPERM;
1915 fprintf(fd, "[error] %s: missing id field in stream declaration\n", __func__);
1916 goto error;
1917 }
1918 stream->stream_id = 0;
05628561
MD
1919 }
1920 if (trace->streams->len <= stream->stream_id)
1921 g_ptr_array_set_size(trace->streams, stream->stream_id + 1);
1922 g_ptr_array_index(trace->streams, stream->stream_id) = stream;
82662ad4 1923 stream->trace = trace;
9e29e16e 1924
05628561
MD
1925 return 0;
1926
1927error:
98df1c9f
MD
1928 if (stream->event_header_decl)
1929 declaration_unref(&stream->event_header_decl->p);
1930 if (stream->event_context_decl)
1931 declaration_unref(&stream->event_context_decl->p);
1932 if (stream->packet_context_decl)
1933 declaration_unref(&stream->packet_context_decl->p);
2d0bea29 1934 g_ptr_array_free(stream->streams, TRUE);
05628561 1935 g_ptr_array_free(stream->events_by_id, TRUE);
a0720417 1936 g_hash_table_destroy(stream->event_quark_to_id);
d20f5e59 1937 free_declaration_scope(stream->declaration_scope);
05628561
MD
1938 g_free(stream);
1939 return ret;
1940}
1941
1942static
1943int ctf_trace_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
1944{
1945 int ret = 0;
1946
1947 switch (node->type) {
1948 case NODE_TYPEDEF:
1949 ret = ctf_typedef_visit(fd, depth + 1,
a0720417 1950 trace->declaration_scope,
78af2bcd 1951 node->u._typedef.type_specifier_list,
05628561 1952 &node->u._typedef.type_declarators,
a0720417 1953 trace);
05628561
MD
1954 if (ret)
1955 return ret;
1956 break;
1957 case NODE_TYPEALIAS:
1958 ret = ctf_typealias_visit(fd, depth + 1,
a0720417
MD
1959 trace->declaration_scope,
1960 node->u.typealias.target, node->u.typealias.alias,
1961 trace);
05628561
MD
1962 if (ret)
1963 return ret;
1964 break;
1965 case NODE_CTF_EXPRESSION:
1966 {
1967 char *left;
1968
1969 left = concatenate_unary_strings(&node->u.ctf_expression.left);
1970 if (!strcmp(left, "major")) {
427c09b7
MD
1971 if (CTF_TRACE_FIELD_IS_SET(trace, major)) {
1972 fprintf(fd, "[error] %s: major 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->major);
1977 if (ret) {
78af2bcd 1978 fprintf(fd, "[error] %s: unexpected unary expression for trace major number\n", __func__);
0f980a35
MD
1979 ret = -EINVAL;
1980 goto error;
05628561 1981 }
a0720417 1982 CTF_TRACE_SET_FIELD(trace, major);
05628561 1983 } else if (!strcmp(left, "minor")) {
427c09b7
MD
1984 if (CTF_TRACE_FIELD_IS_SET(trace, minor)) {
1985 fprintf(fd, "[error] %s: minor already declared in trace declaration\n", __func__);
0f980a35
MD
1986 ret = -EPERM;
1987 goto error;
427c09b7 1988 }
05628561
MD
1989 ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->minor);
1990 if (ret) {
78af2bcd 1991 fprintf(fd, "[error] %s: unexpected unary expression for trace minor number\n", __func__);
0f980a35
MD
1992 ret = -EINVAL;
1993 goto error;
05628561 1994 }
a0720417 1995 CTF_TRACE_SET_FIELD(trace, minor);
05628561 1996 } else if (!strcmp(left, "uuid")) {
b4c19c1e 1997 uuid_t uuid;
a0fe7d97
MD
1998
1999 ret = get_unary_uuid(&node->u.ctf_expression.right, &uuid);
05628561 2000 if (ret) {
78af2bcd 2001 fprintf(fd, "[error] %s: unexpected unary expression for trace uuid\n", __func__);
0f980a35
MD
2002 ret = -EINVAL;
2003 goto error;
05628561 2004 }
b4c19c1e
MD
2005 if (CTF_TRACE_FIELD_IS_SET(trace, uuid)
2006 && uuid_compare(uuid, trace->uuid)) {
2007 fprintf(fd, "[error] %s: uuid mismatch\n", __func__);
2008 ret = -EPERM;
2009 goto error;
a0fe7d97
MD
2010 } else {
2011 memcpy(trace->uuid, uuid, sizeof(uuid));
b4c19c1e 2012 }
a0720417 2013 CTF_TRACE_SET_FIELD(trace, uuid);
0f980a35
MD
2014 } else if (!strcmp(left, "byte_order")) {
2015 struct ctf_node *right;
2016 int byte_order;
2017
0f980a35
MD
2018 right = _cds_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings);
2019 byte_order = get_trace_byte_order(fd, depth, right);
2020 if (byte_order < 0)
2021 return -EINVAL;
a0fe7d97
MD
2022
2023 if (CTF_TRACE_FIELD_IS_SET(trace, byte_order)
2024 && byte_order != trace->byte_order) {
2025 fprintf(fd, "[error] %s: endianness mismatch\n", __func__);
2026 ret = -EPERM;
2027 goto error;
2028 } else {
fdce39de
MD
2029 if (byte_order != trace->byte_order) {
2030 trace->byte_order = byte_order;
2031 /*
2032 * We need to restart
2033 * construction of the
2034 * intermediate representation.
2035 */
2e0c6b58
MD
2036 trace->field_mask = 0;
2037 CTF_TRACE_SET_FIELD(trace, byte_order);
2038 ret = -EINTR;
2039 goto error;
fdce39de 2040 }
a0fe7d97 2041 }
7c8a1386 2042 CTF_TRACE_SET_FIELD(trace, byte_order);
0f980a35
MD
2043 } else if (!strcmp(left, "packet.header")) {
2044 struct declaration *declaration;
2045
2046 if (trace->packet_header_decl) {
2047 fprintf(fd, "[error] %s: packet.header already declared in trace declaration\n", __func__);
2048 ret = -EINVAL;
2049 goto error;
2050 }
2051 declaration = ctf_type_specifier_list_visit(fd, depth,
2052 _cds_list_first_entry(&node->u.ctf_expression.right,
2053 struct ctf_node, siblings),
2054 trace->declaration_scope, trace);
2055 if (!declaration) {
2056 ret = -EPERM;
2057 goto error;
2058 }
2059 if (declaration->id != CTF_TYPE_STRUCT) {
2060 ret = -EPERM;
2061 goto error;
2062 }
2063 trace->packet_header_decl = container_of(declaration, struct declaration_struct, p);
98df1c9f 2064 } else {
b3ab6665 2065 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__, left);
05628561 2066 }
98df1c9f 2067
0f980a35 2068error:
41253107 2069 g_free(left);
05628561
MD
2070 break;
2071 }
2072 default:
2073 return -EPERM;
2074 /* TODO: declaration specifier should be added. */
2075 }
2076
0f980a35 2077 return ret;
05628561
MD
2078}
2079
05628561
MD
2080static
2081int ctf_trace_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2082{
2083 int ret = 0;
2084 struct ctf_node *iter;
2085
d20f5e59 2086 if (trace->declaration_scope)
05628561 2087 return -EEXIST;
d20f5e59 2088 trace->declaration_scope = new_declaration_scope(trace->root_declaration_scope);
05628561
MD
2089 trace->streams = g_ptr_array_new();
2090 cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
2091 ret = ctf_trace_declaration_visit(fd, depth + 1, iter, trace);
2092 if (ret)
2093 goto error;
2094 }
a0720417 2095 if (!CTF_TRACE_FIELD_IS_SET(trace, major)) {
05628561 2096 ret = -EPERM;
427c09b7 2097 fprintf(fd, "[error] %s: missing major field in trace declaration\n", __func__);
05628561
MD
2098 goto error;
2099 }
a0720417 2100 if (!CTF_TRACE_FIELD_IS_SET(trace, minor)) {
05628561 2101 ret = -EPERM;
427c09b7 2102 fprintf(fd, "[error] %s: missing minor field in trace declaration\n", __func__);
05628561
MD
2103 goto error;
2104 }
a0720417 2105 if (!CTF_TRACE_FIELD_IS_SET(trace, uuid)) {
05628561 2106 ret = -EPERM;
427c09b7 2107 fprintf(fd, "[error] %s: missing uuid field in trace declaration\n", __func__);
05628561
MD
2108 goto error;
2109 }
dc48ecad
MD
2110 if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) {
2111 ret = -EPERM;
2112 fprintf(fd, "[error] %s: missing byte_order field in trace declaration\n", __func__);
2113 goto error;
2114 }
0f980a35 2115
0f980a35
MD
2116 if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) {
2117 /* check that the packet header contains a "magic" field */
e28d4618 2118 if (!trace->packet_header_decl
0f980a35
MD
2119 || struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("magic")) < 0) {
2120 ret = -EPERM;
2121 fprintf(fd, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__);
98df1c9f 2122 goto error;
0f980a35
MD
2123 }
2124 }
05628561
MD
2125 return 0;
2126
2127error:
0d72513f 2128 if (trace->packet_header_decl) {
98df1c9f 2129 declaration_unref(&trace->packet_header_decl->p);
0d72513f
MD
2130 trace->packet_header_decl = NULL;
2131 }
05628561 2132 g_ptr_array_free(trace->streams, TRUE);
a0720417 2133 free_declaration_scope(trace->declaration_scope);
fdce39de 2134 trace->declaration_scope = NULL;
05628561
MD
2135 return ret;
2136}
2137
50cb9c56
MD
2138static
2139int ctf_clock_declaration_visit(FILE *fd, int depth, struct ctf_node *node,
2140 struct ctf_clock *clock, struct ctf_trace *trace)
2141{
2142 int ret = 0;
2143
2144 switch (node->type) {
2145 case NODE_CTF_EXPRESSION:
2146 {
2147 char *left;
2148
2149 left = concatenate_unary_strings(&node->u.ctf_expression.left);
2150 if (!strcmp(left, "name")) {
2151 char *right;
2152
2153 if (CTF_CLOCK_FIELD_IS_SET(clock, name)) {
2154 fprintf(fd, "[error] %s: name already declared in clock declaration\n", __func__);
2155 ret = -EPERM;
2156 goto error;
2157 }
2158 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2159 if (!right) {
2160 fprintf(fd, "[error] %s: unexpected unary expression for clock name\n", __func__);
2161 ret = -EINVAL;
2162 goto error;
2163 }
2164 clock->name = g_quark_from_string(right);
2165 g_free(right);
2166 CTF_EVENT_SET_FIELD(clock, name);
2167 } else if (!strcmp(left, "uuid")) {
2168 char *right;
2169
2170 if (clock->uuid) {
2171 fprintf(fd, "[error] %s: uuid already declared in clock declaration\n", __func__);
2172 ret = -EPERM;
2173 goto error;
2174 }
2175 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2176 if (!right) {
2177 fprintf(fd, "[error] %s: unexpected unary expression for clock uuid\n", __func__);
2178 ret = -EINVAL;
2179 goto error;
2180 }
2181 clock->uuid = g_quark_from_string(right);
2182 g_free(right);
2183 } else if (!strcmp(left, "description")) {
2184 char *right;
2185
2186 if (clock->description) {
2187 fprintf(fd, "[warning] %s: duplicated clock description\n", __func__);
2188 goto error; /* ret is 0, so not an actual error, just warn. */
2189 }
2190 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2191 if (!right) {
2192 fprintf(fd, "[warning] %s: unexpected unary expression for clock description\n", __func__);
2193 goto error; /* ret is 0, so not an actual error, just warn. */
2194 }
2195 clock->description = right;
2196 } else if (!strcmp(left, "freq")) {
2197 if (clock->freq) {
2198 fprintf(fd, "[error] %s: freq already declared in clock declaration\n", __func__);
2199 ret = -EPERM;
2200 goto error;
2201 }
2202 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->freq);
2203 if (ret) {
2204 fprintf(fd, "[error] %s: unexpected unary expression for clock freq\n", __func__);
2205 ret = -EINVAL;
2206 goto error;
2207 }
2208 } else if (!strcmp(left, "precision")) {
2209 if (clock->precision) {
2210 fprintf(fd, "[error] %s: precision already declared in clock declaration\n", __func__);
2211 ret = -EPERM;
2212 goto error;
2213 }
2214 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->precision);
2215 if (ret) {
2216 fprintf(fd, "[error] %s: unexpected unary expression for clock precision\n", __func__);
2217 ret = -EINVAL;
2218 goto error;
2219 }
2220 } else if (!strcmp(left, "offset_s")) {
2221 if (clock->offset_s) {
2222 fprintf(fd, "[error] %s: offset_s already declared in clock declaration\n", __func__);
2223 ret = -EPERM;
2224 goto error;
2225 }
2226 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->offset_s);
2227 if (ret) {
2228 fprintf(fd, "[error] %s: unexpected unary expression for clock offset_s\n", __func__);
2229 ret = -EINVAL;
2230 goto error;
2231 }
2232 } else if (!strcmp(left, "offset")) {
2233 if (clock->offset) {
2234 fprintf(fd, "[error] %s: offset already declared in clock declaration\n", __func__);
2235 ret = -EPERM;
2236 goto error;
2237 }
2238 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->offset);
2239 if (ret) {
2240 fprintf(fd, "[error] %s: unexpected unary expression for clock offset\n", __func__);
2241 ret = -EINVAL;
2242 goto error;
2243 }
2244 } else {
2245 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in clock declaration.\n", __func__, left);
2246 }
2247
2248error:
2249 g_free(left);
2250 break;
2251 }
2252 default:
2253 return -EPERM;
2254 /* TODO: declaration specifier should be added. */
2255 }
2256
2257 return ret;
2258}
2259
2260static
2261int ctf_clock_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2262{
2263 int ret = 0;
2264 struct ctf_node *iter;
2265 struct ctf_clock *clock;
2266
2267 clock = g_new0(struct ctf_clock, 1);
2268 cds_list_for_each_entry(iter, &node->u.clock.declaration_list, siblings) {
2269 ret = ctf_clock_declaration_visit(fd, depth + 1, iter, clock, trace);
2270 if (ret)
2271 goto error;
2272 }
2273 if (!CTF_CLOCK_FIELD_IS_SET(clock, name)) {
2274 ret = -EPERM;
2275 fprintf(fd, "[error] %s: missing namefield in clock declaration\n", __func__);
2276 goto error;
2277 }
2278 g_hash_table_insert(trace->clocks, (gpointer) (unsigned long) clock->name, clock);
2279 return 0;
2280
2281error:
2282 g_free(clock->description);
2283 g_free(clock);
2284 return ret;
2285}
2286
2287static
2288void clock_free(gpointer data)
2289{
2290 struct ctf_clock *clock = data;
2291
2292 g_free(clock->description);
2293 g_free(clock);
2294}
2295
78af2bcd
MD
2296static
2297int ctf_root_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2298{
2299 int ret = 0;
2300
2301 switch (node->type) {
2302 case NODE_TYPEDEF:
2303 ret = ctf_typedef_visit(fd, depth + 1,
2304 trace->root_declaration_scope,
2305 node->u._typedef.type_specifier_list,
2306 &node->u._typedef.type_declarators,
2307 trace);
2308 if (ret)
2309 return ret;
2310 break;
2311 case NODE_TYPEALIAS:
2312 ret = ctf_typealias_visit(fd, depth + 1,
2313 trace->root_declaration_scope,
2314 node->u.typealias.target, node->u.typealias.alias,
2315 trace);
2316 if (ret)
2317 return ret;
2318 break;
2319 case NODE_TYPE_SPECIFIER_LIST:
2320 {
2321 struct declaration *declaration;
2322
2323 /*
2324 * Just add the type specifier to the root scope
2325 * declaration scope. Release local reference.
2326 */
2327 declaration = ctf_type_specifier_list_visit(fd, depth + 1,
2328 node, trace->root_declaration_scope, trace);
2329 if (!declaration)
2330 return -ENOMEM;
2331 declaration_unref(declaration);
2332 break;
2333 }
2334 default:
2335 return -EPERM;
2336 }
2337
2338 return 0;
2339}
2340
50cb9c56 2341/* TODO: missing metadata "destroy" (memory leak) */
ab4cf058
MD
2342int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node,
2343 struct ctf_trace *trace, int byte_order)
05628561
MD
2344{
2345 int ret = 0;
2346 struct ctf_node *iter;
56e60373 2347 int clock_done = 0;
05628561 2348
c8b219a3 2349 printf_verbose("CTF visitor: metadata construction... ");
ab4cf058 2350 trace->byte_order = byte_order;
50cb9c56
MD
2351 trace->clocks = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2352 NULL, clock_free);
ab4cf058 2353
00f7c1a5
MD
2354retry:
2355 trace->root_declaration_scope = new_declaration_scope(NULL);
2356
05628561
MD
2357 switch (node->type) {
2358 case NODE_ROOT:
56e60373
MD
2359 if (!clock_done) {
2360 /*
2361 * declarations need to query clock hash table,
2362 * so clock need to be treated first.
2363 */
2364 cds_list_for_each_entry(iter, &node->u.root.clock, siblings) {
2365 ret = ctf_clock_visit(fd, depth + 1, iter,
2366 trace);
2367 if (ret) {
2368 fprintf(fd, "[error] %s: clock declaration error\n", __func__);
2369 goto error;
2370 }
2371 }
2372 clock_done = 1;
2373 }
3e11b713 2374 cds_list_for_each_entry(iter, &node->u.root.declaration_list,
05628561 2375 siblings) {
78af2bcd 2376 ret = ctf_root_declaration_visit(fd, depth + 1, iter, trace);
427c09b7
MD
2377 if (ret) {
2378 fprintf(fd, "[error] %s: root declaration error\n", __func__);
a0fe7d97 2379 goto error;
427c09b7 2380 }
05628561
MD
2381 }
2382 cds_list_for_each_entry(iter, &node->u.root.trace, siblings) {
2383 ret = ctf_trace_visit(fd, depth + 1, iter, trace);
00f7c1a5
MD
2384 if (ret == -EINTR) {
2385 free_declaration_scope(trace->root_declaration_scope);
2386 /*
2387 * Need to restart creation of type
2388 * definitions, aliases and
2389 * trace header declarations.
2390 */
2391 goto retry;
2392 }
427c09b7
MD
2393 if (ret) {
2394 fprintf(fd, "[error] %s: trace declaration error\n", __func__);
a0fe7d97 2395 goto error;
427c09b7 2396 }
05628561 2397 }
5039b4cc
MD
2398 if (!trace->streams) {
2399 fprintf(fd, "[error] %s: missing trace declaration\n", __func__);
a0fe7d97
MD
2400 ret = -EINVAL;
2401 goto error;
5039b4cc 2402 }
05628561
MD
2403 cds_list_for_each_entry(iter, &node->u.root.stream, siblings) {
2404 ret = ctf_stream_visit(fd, depth + 1, iter,
41253107 2405 trace->root_declaration_scope, trace);
427c09b7
MD
2406 if (ret) {
2407 fprintf(fd, "[error] %s: stream declaration error\n", __func__);
a0fe7d97 2408 goto error;
427c09b7 2409 }
05628561
MD
2410 }
2411 cds_list_for_each_entry(iter, &node->u.root.event, siblings) {
2412 ret = ctf_event_visit(fd, depth + 1, iter,
41253107 2413 trace->root_declaration_scope, trace);
427c09b7
MD
2414 if (ret) {
2415 fprintf(fd, "[error] %s: event declaration error\n", __func__);
a0fe7d97 2416 goto error;
427c09b7 2417 }
05628561
MD
2418 }
2419 break;
05628561
MD
2420 case NODE_UNKNOWN:
2421 default:
78af2bcd 2422 fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
05628561 2423 (int) node->type);
a0fe7d97
MD
2424 ret = -EINVAL;
2425 goto error;
05628561 2426 }
c8b219a3 2427 printf_verbose("done.\n");
05628561 2428 return ret;
a0fe7d97
MD
2429
2430error:
2431 free_declaration_scope(trace->root_declaration_scope);
50cb9c56 2432 g_hash_table_destroy(trace->clocks);
a0fe7d97 2433 return ret;
05628561 2434}
This page took 0.253254 seconds and 4 git commands to generate.