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