d216cd0e6479acd1cb65a626336bf59b85539d2b
[babeltrace.git] / formats / ctf / metadata / ctf-parser.y
1 %{
2 /*
3 * ctf-parser.y
4 *
5 * Common Trace Format Metadata Grammar.
6 *
7 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 */
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <glib.h>
26 #include <errno.h>
27 #include <inttypes.h>
28 #include <babeltrace/list.h>
29 #include <babeltrace/babeltrace-internal.h>
30 #include "ctf-scanner.h"
31 #include "ctf-parser.h"
32 #include "ctf-ast.h"
33
34 int yydebug;
35
36 /* Join two lists, put "add" at the end of "head". */
37 static inline void
38 _cds_list_splice_tail (struct cds_list_head *add, struct cds_list_head *head)
39 {
40 /* Do nothing if the list which gets added is empty. */
41 if (add != add->next) {
42 add->next->prev = head->prev;
43 add->prev->next = head;
44 head->prev->next = add->next;
45 head->prev = add->prev;
46 }
47 }
48
49 int yyparse(struct ctf_scanner *scanner);
50 int yylex(union YYSTYPE *yyval, struct ctf_scanner *scanner);
51 int yylex_init_extra(struct ctf_scanner *scanner, yyscan_t * ptr_yy_globals);
52 int yylex_destroy(yyscan_t yyscanner);
53 void yyrestart(FILE * in_str, yyscan_t scanner);
54
55 int yydebug;
56
57 struct gc_string {
58 struct cds_list_head gc;
59 size_t alloclen;
60 char s[];
61 };
62
63 static const char *node_type_to_str[] = {
64 [ NODE_UNKNOWN ] = "NODE_UNKNOWN",
65 [ NODE_ROOT ] = "NODE_ROOT",
66 [ NODE_EVENT ] = "NODE_EVENT",
67 [ NODE_STREAM ] = "NODE_STREAM",
68 [ NODE_TRACE ] = "NODE_TRACE",
69 [ NODE_CTF_EXPRESSION ] = "NODE_CTF_EXPRESSION",
70 [ NODE_UNARY_EXPRESSION ] = "NODE_UNARY_EXPRESSION",
71 [ NODE_TYPEDEF ] = "NODE_TYPEDEF",
72 [ NODE_TYPEALIAS_TARGET ] = "NODE_TYPEALIAS_TARGET",
73 [ NODE_TYPEALIAS_ALIAS ] = "NODE_TYPEALIAS_ALIAS",
74 [ NODE_TYPEALIAS ] = "NODE_TYPEALIAS",
75 [ NODE_TYPE_SPECIFIER ] = "NODE_TYPE_SPECIFIER",
76 [ NODE_TYPE_SPECIFIER_LIST ] = "NODE_TYPE_SPECIFIER_LIST",
77 [ NODE_POINTER ] = "NODE_POINTER",
78 [ NODE_TYPE_DECLARATOR ] = "NODE_TYPE_DECLARATOR",
79 [ NODE_FLOATING_POINT ] = "NODE_FLOATING_POINT",
80 [ NODE_INTEGER ] = "NODE_INTEGER",
81 [ NODE_STRING ] = "NODE_STRING",
82 [ NODE_ENUMERATOR ] = "NODE_ENUMERATOR",
83 [ NODE_ENUM ] = "NODE_ENUM",
84 [ NODE_STRUCT_OR_VARIANT_DECLARATION ] = "NODE_STRUCT_OR_VARIANT_DECLARATION",
85 [ NODE_VARIANT ] = "NODE_VARIANT",
86 [ NODE_STRUCT ] = "NODE_STRUCT",
87 };
88
89 const char *node_type(struct ctf_node *node)
90 {
91 if (node->type < NR_NODE_TYPES)
92 return node_type_to_str[node->type];
93 else
94 return NULL;
95 }
96
97 static struct gc_string *gc_string_alloc(struct ctf_scanner *scanner,
98 size_t len)
99 {
100 struct gc_string *gstr;
101 size_t alloclen;
102
103 /* TODO: could be faster with find first bit or glib Gstring */
104 /* sizeof long to account for malloc header (int or long ?) */
105 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + len;
106 alloclen *= 2);
107
108 gstr = malloc(alloclen);
109 cds_list_add(&gstr->gc, &scanner->allocated_strings);
110 gstr->alloclen = alloclen;
111 return gstr;
112 }
113
114 /*
115 * note: never use gc_string_append on a string that has external references.
116 * gsrc will be garbage collected immediately, and gstr might be.
117 * Should only be used to append characters to a string literal or constant.
118 */
119 struct gc_string *gc_string_append(struct ctf_scanner *scanner,
120 struct gc_string *gstr,
121 struct gc_string *gsrc)
122 {
123 size_t newlen = strlen(gsrc->s) + strlen(gstr->s) + 1;
124 size_t alloclen;
125
126 /* TODO: could be faster with find first bit or glib Gstring */
127 /* sizeof long to account for malloc header (int or long ?) */
128 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + newlen;
129 alloclen *= 2);
130
131 if (alloclen > gstr->alloclen) {
132 struct gc_string *newgstr;
133
134 newgstr = gc_string_alloc(scanner, newlen);
135 strcpy(newgstr->s, gstr->s);
136 strcat(newgstr->s, gsrc->s);
137 cds_list_del(&gstr->gc);
138 free(gstr);
139 gstr = newgstr;
140 } else {
141 strcat(gstr->s, gsrc->s);
142 }
143 cds_list_del(&gsrc->gc);
144 free(gsrc);
145 return gstr;
146 }
147
148 void setstring(struct ctf_scanner *scanner, YYSTYPE *lvalp, const char *src)
149 {
150 lvalp->gs = gc_string_alloc(scanner, strlen(src) + 1);
151 strcpy(lvalp->gs->s, src);
152 }
153
154 static void init_scope(struct ctf_scanner_scope *scope,
155 struct ctf_scanner_scope *parent)
156 {
157 scope->parent = parent;
158 scope->types = g_hash_table_new_full(g_str_hash, g_str_equal,
159 NULL, NULL);
160 }
161
162 static void finalize_scope(struct ctf_scanner_scope *scope)
163 {
164 g_hash_table_destroy(scope->types);
165 }
166
167 static void push_scope(struct ctf_scanner *scanner)
168 {
169 struct ctf_scanner_scope *ns;
170
171 printf_debug("push scope\n");
172 ns = malloc(sizeof(struct ctf_scanner_scope));
173 init_scope(ns, scanner->cs);
174 scanner->cs = ns;
175 }
176
177 static void pop_scope(struct ctf_scanner *scanner)
178 {
179 struct ctf_scanner_scope *os;
180
181 printf_debug("pop scope\n");
182 os = scanner->cs;
183 scanner->cs = os->parent;
184 finalize_scope(os);
185 free(os);
186 }
187
188 static int lookup_type(struct ctf_scanner_scope *s, const char *id)
189 {
190 int ret;
191
192 ret = (int) (long) g_hash_table_lookup(s->types, id);
193 printf_debug("lookup %p %s %d\n", s, id, ret);
194 return ret;
195 }
196
197 int is_type(struct ctf_scanner *scanner, const char *id)
198 {
199 struct ctf_scanner_scope *it;
200 int ret = 0;
201
202 for (it = scanner->cs; it != NULL; it = it->parent) {
203 if (lookup_type(it, id)) {
204 ret = 1;
205 break;
206 }
207 }
208 printf_debug("is type %s %d\n", id, ret);
209 return ret;
210 }
211
212 static void add_type(struct ctf_scanner *scanner, struct gc_string *id)
213 {
214 printf_debug("add type %s\n", id->s);
215 if (lookup_type(scanner->cs, id->s))
216 return;
217 g_hash_table_insert(scanner->cs->types, id->s, id->s);
218 }
219
220 static struct ctf_node *make_node(struct ctf_scanner *scanner,
221 enum node_type type)
222 {
223 struct ctf_ast *ast = ctf_scanner_get_ast(scanner);
224 struct ctf_node *node;
225
226 node = malloc(sizeof(*node));
227 if (!node)
228 return NULL;
229 memset(node, 0, sizeof(*node));
230 node->type = type;
231 CDS_INIT_LIST_HEAD(&node->tmp_head);
232 cds_list_add(&node->gc, &ast->allocated_nodes);
233 cds_list_add(&node->siblings, &node->tmp_head);
234
235 switch (type) {
236 case NODE_ROOT:
237 fprintf(stderr, "[error] %s: trying to create root node\n", __func__);
238 break;
239
240 case NODE_EVENT:
241 CDS_INIT_LIST_HEAD(&node->u.event.declaration_list);
242 break;
243 case NODE_STREAM:
244 CDS_INIT_LIST_HEAD(&node->u.stream.declaration_list);
245 break;
246 case NODE_TRACE:
247 CDS_INIT_LIST_HEAD(&node->u.trace.declaration_list);
248 break;
249
250 case NODE_CTF_EXPRESSION:
251 CDS_INIT_LIST_HEAD(&node->u.ctf_expression.left);
252 CDS_INIT_LIST_HEAD(&node->u.ctf_expression.right);
253 break;
254 case NODE_UNARY_EXPRESSION:
255 break;
256
257 case NODE_TYPEDEF:
258 CDS_INIT_LIST_HEAD(&node->u._typedef.type_declarators);
259 break;
260 case NODE_TYPEALIAS_TARGET:
261 CDS_INIT_LIST_HEAD(&node->u.typealias_target.type_declarators);
262 break;
263 case NODE_TYPEALIAS_ALIAS:
264 CDS_INIT_LIST_HEAD(&node->u.typealias_alias.type_declarators);
265 break;
266 case NODE_TYPEALIAS:
267 break;
268
269 case NODE_TYPE_SPECIFIER:
270 break;
271 case NODE_TYPE_SPECIFIER_LIST:
272 CDS_INIT_LIST_HEAD(&node->u.type_specifier_list.head);
273 break;
274 case NODE_POINTER:
275 break;
276 case NODE_TYPE_DECLARATOR:
277 CDS_INIT_LIST_HEAD(&node->u.type_declarator.pointers);
278 break;
279
280 case NODE_FLOATING_POINT:
281 CDS_INIT_LIST_HEAD(&node->u.floating_point.expressions);
282 break;
283 case NODE_INTEGER:
284 CDS_INIT_LIST_HEAD(&node->u.integer.expressions);
285 break;
286 case NODE_STRING:
287 CDS_INIT_LIST_HEAD(&node->u.string.expressions);
288 break;
289 case NODE_ENUMERATOR:
290 CDS_INIT_LIST_HEAD(&node->u.enumerator.values);
291 break;
292 case NODE_ENUM:
293 CDS_INIT_LIST_HEAD(&node->u._enum.enumerator_list);
294 break;
295 case NODE_STRUCT_OR_VARIANT_DECLARATION:
296 CDS_INIT_LIST_HEAD(&node->u.struct_or_variant_declaration.type_declarators);
297 break;
298 case NODE_VARIANT:
299 CDS_INIT_LIST_HEAD(&node->u.variant.declaration_list);
300 break;
301 case NODE_STRUCT:
302 CDS_INIT_LIST_HEAD(&node->u._struct.declaration_list);
303 CDS_INIT_LIST_HEAD(&node->u._struct.min_align);
304 break;
305
306 case NODE_UNKNOWN:
307 default:
308 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
309 (int) type);
310 break;
311 }
312
313 return node;
314 }
315
316 static int reparent_ctf_expression(struct ctf_node *node,
317 struct ctf_node *parent)
318 {
319 switch (parent->type) {
320 case NODE_EVENT:
321 _cds_list_splice_tail(&node->tmp_head, &parent->u.event.declaration_list);
322 break;
323 case NODE_STREAM:
324 _cds_list_splice_tail(&node->tmp_head, &parent->u.stream.declaration_list);
325 break;
326 case NODE_TRACE:
327 _cds_list_splice_tail(&node->tmp_head, &parent->u.trace.declaration_list);
328 break;
329 case NODE_FLOATING_POINT:
330 _cds_list_splice_tail(&node->tmp_head, &parent->u.floating_point.expressions);
331 break;
332 case NODE_INTEGER:
333 _cds_list_splice_tail(&node->tmp_head, &parent->u.integer.expressions);
334 break;
335 case NODE_STRING:
336 _cds_list_splice_tail(&node->tmp_head, &parent->u.string.expressions);
337 break;
338
339 case NODE_ROOT:
340 case NODE_CTF_EXPRESSION:
341 case NODE_TYPEDEF:
342 case NODE_TYPEALIAS_TARGET:
343 case NODE_TYPEALIAS_ALIAS:
344 case NODE_TYPEALIAS:
345 case NODE_TYPE_SPECIFIER:
346 case NODE_TYPE_SPECIFIER_LIST:
347 case NODE_POINTER:
348 case NODE_TYPE_DECLARATOR:
349 case NODE_ENUMERATOR:
350 case NODE_ENUM:
351 case NODE_STRUCT_OR_VARIANT_DECLARATION:
352 case NODE_VARIANT:
353 case NODE_STRUCT:
354 case NODE_UNARY_EXPRESSION:
355 return -EPERM;
356
357 case NODE_UNKNOWN:
358 default:
359 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
360 (int) parent->type);
361 return -EINVAL;
362 }
363 return 0;
364 }
365
366 static int reparent_typedef(struct ctf_node *node, struct ctf_node *parent)
367 {
368 switch (parent->type) {
369 case NODE_ROOT:
370 _cds_list_splice_tail(&node->tmp_head, &parent->u.root.declaration_list);
371 break;
372 case NODE_EVENT:
373 _cds_list_splice_tail(&node->tmp_head, &parent->u.event.declaration_list);
374 break;
375 case NODE_STREAM:
376 _cds_list_splice_tail(&node->tmp_head, &parent->u.stream.declaration_list);
377 break;
378 case NODE_TRACE:
379 _cds_list_splice_tail(&node->tmp_head, &parent->u.trace.declaration_list);
380 break;
381 case NODE_VARIANT:
382 _cds_list_splice_tail(&node->tmp_head, &parent->u.variant.declaration_list);
383 break;
384 case NODE_STRUCT:
385 _cds_list_splice_tail(&node->tmp_head, &parent->u._struct.declaration_list);
386 break;
387
388 case NODE_FLOATING_POINT:
389 case NODE_INTEGER:
390 case NODE_STRING:
391 case NODE_CTF_EXPRESSION:
392 case NODE_TYPEDEF:
393 case NODE_TYPEALIAS_TARGET:
394 case NODE_TYPEALIAS_ALIAS:
395 case NODE_TYPEALIAS:
396 case NODE_TYPE_SPECIFIER:
397 case NODE_TYPE_SPECIFIER_LIST:
398 case NODE_POINTER:
399 case NODE_TYPE_DECLARATOR:
400 case NODE_ENUMERATOR:
401 case NODE_ENUM:
402 case NODE_STRUCT_OR_VARIANT_DECLARATION:
403 case NODE_UNARY_EXPRESSION:
404 return -EPERM;
405
406 case NODE_UNKNOWN:
407 default:
408 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
409 (int) parent->type);
410 return -EINVAL;
411 }
412 return 0;
413 }
414
415 static int reparent_typealias(struct ctf_node *node, struct ctf_node *parent)
416 {
417 switch (parent->type) {
418 case NODE_ROOT:
419 _cds_list_splice_tail(&node->tmp_head, &parent->u.root.declaration_list);
420 break;
421 case NODE_EVENT:
422 _cds_list_splice_tail(&node->tmp_head, &parent->u.event.declaration_list);
423 break;
424 case NODE_STREAM:
425 _cds_list_splice_tail(&node->tmp_head, &parent->u.stream.declaration_list);
426 break;
427 case NODE_TRACE:
428 _cds_list_splice_tail(&node->tmp_head, &parent->u.trace.declaration_list);
429 break;
430 case NODE_VARIANT:
431 _cds_list_splice_tail(&node->tmp_head, &parent->u.variant.declaration_list);
432 break;
433 case NODE_STRUCT:
434 _cds_list_splice_tail(&node->tmp_head, &parent->u._struct.declaration_list);
435 break;
436
437 case NODE_FLOATING_POINT:
438 case NODE_INTEGER:
439 case NODE_STRING:
440 case NODE_CTF_EXPRESSION:
441 case NODE_TYPEDEF:
442 case NODE_TYPEALIAS_TARGET:
443 case NODE_TYPEALIAS_ALIAS:
444 case NODE_TYPEALIAS:
445 case NODE_TYPE_SPECIFIER:
446 case NODE_TYPE_SPECIFIER_LIST:
447 case NODE_POINTER:
448 case NODE_TYPE_DECLARATOR:
449 case NODE_ENUMERATOR:
450 case NODE_ENUM:
451 case NODE_STRUCT_OR_VARIANT_DECLARATION:
452 case NODE_UNARY_EXPRESSION:
453 return -EPERM;
454
455 case NODE_UNKNOWN:
456 default:
457 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
458 (int) parent->type);
459 return -EINVAL;
460 }
461 return 0;
462 }
463
464 static int reparent_type_specifier(struct ctf_node *node,
465 struct ctf_node *parent)
466 {
467 switch (parent->type) {
468 case NODE_TYPE_SPECIFIER_LIST:
469 _cds_list_splice_tail(&node->tmp_head, &parent->u.type_specifier_list.head);
470 break;
471
472 case NODE_TYPE_SPECIFIER:
473 case NODE_EVENT:
474 case NODE_STREAM:
475 case NODE_TRACE:
476 case NODE_VARIANT:
477 case NODE_STRUCT:
478 case NODE_TYPEDEF:
479 case NODE_TYPEALIAS_TARGET:
480 case NODE_TYPEALIAS_ALIAS:
481 case NODE_TYPE_DECLARATOR:
482 case NODE_ENUM:
483 case NODE_STRUCT_OR_VARIANT_DECLARATION:
484 case NODE_TYPEALIAS:
485 case NODE_FLOATING_POINT:
486 case NODE_INTEGER:
487 case NODE_STRING:
488 case NODE_CTF_EXPRESSION:
489 case NODE_POINTER:
490 case NODE_ENUMERATOR:
491 case NODE_UNARY_EXPRESSION:
492 return -EPERM;
493
494 case NODE_UNKNOWN:
495 default:
496 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
497 (int) parent->type);
498 return -EINVAL;
499 }
500 return 0;
501 }
502
503 static int reparent_type_specifier_list(struct ctf_node *node,
504 struct ctf_node *parent)
505 {
506 switch (parent->type) {
507 case NODE_ROOT:
508 cds_list_add_tail(&node->siblings, &parent->u.root.declaration_list);
509 break;
510 case NODE_EVENT:
511 cds_list_add_tail(&node->siblings, &parent->u.event.declaration_list);
512 break;
513 case NODE_STREAM:
514 cds_list_add_tail(&node->siblings, &parent->u.stream.declaration_list);
515 break;
516 case NODE_TRACE:
517 cds_list_add_tail(&node->siblings, &parent->u.trace.declaration_list);
518 break;
519 case NODE_VARIANT:
520 cds_list_add_tail(&node->siblings, &parent->u.variant.declaration_list);
521 break;
522 case NODE_STRUCT:
523 cds_list_add_tail(&node->siblings, &parent->u._struct.declaration_list);
524 break;
525 case NODE_TYPEDEF:
526 parent->u._typedef.type_specifier_list = node;
527 break;
528 case NODE_TYPEALIAS_TARGET:
529 parent->u.typealias_target.type_specifier_list = node;
530 break;
531 case NODE_TYPEALIAS_ALIAS:
532 parent->u.typealias_alias.type_specifier_list = node;
533 break;
534 case NODE_ENUM:
535 parent->u._enum.container_type = node;
536 break;
537 case NODE_STRUCT_OR_VARIANT_DECLARATION:
538 parent->u.struct_or_variant_declaration.type_specifier_list = node;
539 break;
540 case NODE_TYPE_DECLARATOR:
541 case NODE_TYPE_SPECIFIER:
542 case NODE_TYPEALIAS:
543 case NODE_FLOATING_POINT:
544 case NODE_INTEGER:
545 case NODE_STRING:
546 case NODE_CTF_EXPRESSION:
547 case NODE_POINTER:
548 case NODE_ENUMERATOR:
549 case NODE_UNARY_EXPRESSION:
550 return -EPERM;
551
552 case NODE_UNKNOWN:
553 default:
554 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
555 (int) parent->type);
556 return -EINVAL;
557 }
558 return 0;
559 }
560
561 static int reparent_type_declarator(struct ctf_node *node,
562 struct ctf_node *parent)
563 {
564 switch (parent->type) {
565 case NODE_TYPE_DECLARATOR:
566 parent->u.type_declarator.type = TYPEDEC_NESTED;
567 parent->u.type_declarator.u.nested.type_declarator = node;
568 break;
569 case NODE_STRUCT_OR_VARIANT_DECLARATION:
570 _cds_list_splice_tail(&node->tmp_head, &parent->u.struct_or_variant_declaration.type_declarators);
571 break;
572 case NODE_TYPEDEF:
573 _cds_list_splice_tail(&node->tmp_head, &parent->u._typedef.type_declarators);
574 break;
575 case NODE_TYPEALIAS_TARGET:
576 _cds_list_splice_tail(&node->tmp_head, &parent->u.typealias_target.type_declarators);
577 break;
578 case NODE_TYPEALIAS_ALIAS:
579 _cds_list_splice_tail(&node->tmp_head, &parent->u.typealias_alias.type_declarators);
580 break;
581
582 case NODE_ROOT:
583 case NODE_EVENT:
584 case NODE_STREAM:
585 case NODE_TRACE:
586 case NODE_VARIANT:
587 case NODE_STRUCT:
588 case NODE_TYPEALIAS:
589 case NODE_ENUM:
590 case NODE_FLOATING_POINT:
591 case NODE_INTEGER:
592 case NODE_STRING:
593 case NODE_CTF_EXPRESSION:
594 case NODE_TYPE_SPECIFIER:
595 case NODE_TYPE_SPECIFIER_LIST:
596 case NODE_POINTER:
597 case NODE_ENUMERATOR:
598 case NODE_UNARY_EXPRESSION:
599 return -EPERM;
600
601 case NODE_UNKNOWN:
602 default:
603 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
604 (int) parent->type);
605 return -EINVAL;
606 }
607 return 0;
608 }
609
610 /*
611 * set_parent_node
612 *
613 * Link node to parent. Returns 0 on success, -EPERM if it is not permitted to
614 * create the link declared by the input, -ENOENT if node or parent is NULL,
615 * -EINVAL if there is an internal structure problem.
616 */
617 static int set_parent_node(struct ctf_node *node,
618 struct ctf_node *parent)
619 {
620 if (!node || !parent)
621 return -ENOENT;
622
623 /* Note: Linking to parent will be done only by an external visitor */
624
625 switch (node->type) {
626 case NODE_ROOT:
627 fprintf(stderr, "[error] %s: trying to reparent root node\n", __func__);
628 return -EINVAL;
629
630 case NODE_EVENT:
631 if (parent->type == NODE_ROOT) {
632 _cds_list_splice_tail(&node->tmp_head, &parent->u.root.event);
633 } else {
634 return -EPERM;
635 }
636 break;
637 case NODE_STREAM:
638 if (parent->type == NODE_ROOT) {
639 _cds_list_splice_tail(&node->tmp_head, &parent->u.root.stream);
640 } else {
641 return -EPERM;
642 }
643 break;
644 case NODE_TRACE:
645 if (parent->type == NODE_ROOT) {
646 _cds_list_splice_tail(&node->tmp_head, &parent->u.root.trace);
647 } else {
648 return -EPERM;
649 }
650 break;
651
652 case NODE_CTF_EXPRESSION:
653 return reparent_ctf_expression(node, parent);
654 case NODE_UNARY_EXPRESSION:
655 if (parent->type == NODE_TYPE_DECLARATOR)
656 parent->u.type_declarator.bitfield_len = node;
657 else
658 return -EPERM;
659 break;
660
661 case NODE_TYPEDEF:
662 return reparent_typedef(node, parent);
663 case NODE_TYPEALIAS_TARGET:
664 if (parent->type == NODE_TYPEALIAS)
665 parent->u.typealias.target = node;
666 else
667 return -EINVAL;
668 case NODE_TYPEALIAS_ALIAS:
669 if (parent->type == NODE_TYPEALIAS)
670 parent->u.typealias.alias = node;
671 else
672 return -EINVAL;
673 case NODE_TYPEALIAS:
674 return reparent_typealias(node, parent);
675
676 case NODE_POINTER:
677 if (parent->type == NODE_TYPE_DECLARATOR) {
678 _cds_list_splice_tail(&node->tmp_head, &parent->u.type_declarator.pointers);
679 } else
680 return -EPERM;
681 break;
682 case NODE_TYPE_DECLARATOR:
683 return reparent_type_declarator(node, parent);
684
685 case NODE_TYPE_SPECIFIER_LIST:
686 return reparent_type_specifier_list(node, parent);
687
688 case NODE_TYPE_SPECIFIER:
689 return reparent_type_specifier(node, parent);
690
691 case NODE_FLOATING_POINT:
692 case NODE_INTEGER:
693 case NODE_STRING:
694 case NODE_ENUM:
695 case NODE_VARIANT:
696 case NODE_STRUCT:
697 return -EINVAL; /* Dealt with internally within grammar */
698
699 case NODE_ENUMERATOR:
700 if (parent->type == NODE_ENUM) {
701 _cds_list_splice_tail(&node->tmp_head, &parent->u._enum.enumerator_list);
702 } else {
703 return -EPERM;
704 }
705 break;
706 case NODE_STRUCT_OR_VARIANT_DECLARATION:
707 switch (parent->type) {
708 case NODE_STRUCT:
709 _cds_list_splice_tail(&node->tmp_head, &parent->u._struct.declaration_list);
710 break;
711 case NODE_VARIANT:
712 _cds_list_splice_tail(&node->tmp_head, &parent->u.variant.declaration_list);
713 break;
714 default:
715 return -EINVAL;
716 }
717 break;
718
719 case NODE_UNKNOWN:
720 default:
721 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
722 (int) parent->type);
723 return -EINVAL;
724 }
725 return 0;
726 }
727
728 void yyerror(struct ctf_scanner *scanner, const char *str)
729 {
730 fprintf(stderr, "error %s\n", str);
731 }
732
733 int yywrap(void)
734 {
735 return 1;
736 }
737
738 #define reparent_error(scanner, str) \
739 do { \
740 yyerror(scanner, YY_("reparent_error: " str "\n")); \
741 YYERROR; \
742 } while (0)
743
744 static void free_strings(struct cds_list_head *list)
745 {
746 struct gc_string *gstr, *tmp;
747
748 cds_list_for_each_entry_safe(gstr, tmp, list, gc)
749 free(gstr);
750 }
751
752 static struct ctf_ast *ctf_ast_alloc(void)
753 {
754 struct ctf_ast *ast;
755
756 ast = malloc(sizeof(*ast));
757 if (!ast)
758 return NULL;
759 memset(ast, 0, sizeof(*ast));
760 CDS_INIT_LIST_HEAD(&ast->allocated_nodes);
761 ast->root.type = NODE_ROOT;
762 CDS_INIT_LIST_HEAD(&ast->root.tmp_head);
763 CDS_INIT_LIST_HEAD(&ast->root.u.root.declaration_list);
764 CDS_INIT_LIST_HEAD(&ast->root.u.root.trace);
765 CDS_INIT_LIST_HEAD(&ast->root.u.root.stream);
766 CDS_INIT_LIST_HEAD(&ast->root.u.root.event);
767 return ast;
768 }
769
770 static void ctf_ast_free(struct ctf_ast *ast)
771 {
772 struct ctf_node *node, *tmp;
773
774 cds_list_for_each_entry_safe(node, tmp, &ast->allocated_nodes, gc)
775 free(node);
776 }
777
778 int ctf_scanner_append_ast(struct ctf_scanner *scanner)
779 {
780 return yyparse(scanner);
781 }
782
783 struct ctf_scanner *ctf_scanner_alloc(FILE *input)
784 {
785 struct ctf_scanner *scanner;
786 int ret;
787
788 yydebug = babeltrace_debug;
789
790 scanner = malloc(sizeof(*scanner));
791 if (!scanner)
792 return NULL;
793 memset(scanner, 0, sizeof(*scanner));
794
795 ret = yylex_init_extra(scanner, &scanner->scanner);
796 if (ret) {
797 fprintf(stderr, "yylex_init error\n");
798 goto cleanup_scanner;
799 }
800 /* Start processing new stream */
801 yyrestart(input, scanner->scanner);
802
803 scanner->ast = ctf_ast_alloc();
804 if (!scanner->ast)
805 goto cleanup_lexer;
806 init_scope(&scanner->root_scope, NULL);
807 scanner->cs = &scanner->root_scope;
808 CDS_INIT_LIST_HEAD(&scanner->allocated_strings);
809
810 if (yydebug)
811 fprintf(stdout, "Scanner input is a%s.\n",
812 isatty(fileno(input)) ? "n interactive tty" :
813 " noninteractive file");
814
815 return scanner;
816
817 cleanup_lexer:
818 ret = yylex_destroy(scanner->scanner);
819 if (!ret)
820 fprintf(stderr, "yylex_destroy error\n");
821 cleanup_scanner:
822 free(scanner);
823 return NULL;
824 }
825
826 void ctf_scanner_free(struct ctf_scanner *scanner)
827 {
828 int ret;
829
830 finalize_scope(&scanner->root_scope);
831 free_strings(&scanner->allocated_strings);
832 ctf_ast_free(scanner->ast);
833 ret = yylex_destroy(scanner->scanner);
834 if (ret)
835 fprintf(stderr, "yylex_destroy error\n");
836 free(scanner);
837 }
838
839 %}
840
841 %define api.pure
842 /* %locations */
843 %parse-param {struct ctf_scanner *scanner}
844 %lex-param {struct ctf_scanner *scanner}
845 /*
846 * Expect two shift-reduce conflicts. Caused by enum name-opt : type {}
847 * vs struct { int :value; } (unnamed bit-field). The default is to
848 * shift, so whenever we encounter an enumeration, we are doing the
849 * proper thing (shift). It is illegal to declare an enumeration
850 * "bit-field", so it is OK if this situation ends up in a parsing
851 * error.
852 */
853 %expect 2
854 %start file
855 %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE ESCSEQ CHAR_STRING_TOKEN LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW STAR PLUS MINUS LT GT TYPEASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA CONST CHAR DOUBLE ENUM EVENT FLOATING_POINT FLOAT INTEGER INT LONG SHORT SIGNED STREAM STRING STRUCT TRACE TYPEALIAS TYPEDEF UNSIGNED VARIANT VOID _BOOL _COMPLEX _IMAGINARY DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT TOK_ALIGN
856 %token <gs> IDENTIFIER ID_TYPE
857 %token ERROR
858 %union
859 {
860 long long ll;
861 char c;
862 struct gc_string *gs;
863 struct ctf_node *n;
864 }
865
866 %type <gs> keywords
867 %type <gs> s_char s_char_sequence c_char c_char_sequence
868
869 %type <n> postfix_expression unary_expression unary_expression_or_range
870
871 %type <n> declaration
872 %type <n> event_declaration
873 %type <n> stream_declaration
874 %type <n> trace_declaration
875 %type <n> integer_declaration_specifiers
876 %type <n> declaration_specifiers
877 %type <n> alias_declaration_specifiers
878
879 %type <n> type_declarator_list
880 %type <n> integer_type_specifier
881 %type <n> type_specifier
882 %type <n> struct_type_specifier
883 %type <n> variant_type_specifier
884 %type <n> enum_type_specifier
885 %type <n> struct_or_variant_declaration_list
886 %type <n> struct_or_variant_declaration
887 %type <n> struct_or_variant_declarator_list
888 %type <n> struct_or_variant_declarator
889 %type <n> enumerator_list
890 %type <n> enumerator
891 %type <n> abstract_declarator_list
892 %type <n> abstract_declarator
893 %type <n> direct_abstract_declarator
894 %type <n> alias_abstract_declarator_list
895 %type <n> alias_abstract_declarator
896 %type <n> direct_alias_abstract_declarator
897 %type <n> declarator
898 %type <n> direct_declarator
899 %type <n> type_declarator
900 %type <n> direct_type_declarator
901 %type <n> pointer
902 %type <n> ctf_assignment_expression_list
903 %type <n> ctf_assignment_expression
904
905 %%
906
907 file:
908 declaration
909 {
910 if (set_parent_node($1, &ctf_scanner_get_ast(scanner)->root))
911 reparent_error(scanner, "error reparenting to root");
912 }
913 | file declaration
914 {
915 if (set_parent_node($2, &ctf_scanner_get_ast(scanner)->root))
916 reparent_error(scanner, "error reparenting to root");
917 }
918 ;
919
920 keywords:
921 VOID
922 { $$ = yylval.gs; }
923 | CHAR
924 { $$ = yylval.gs; }
925 | SHORT
926 { $$ = yylval.gs; }
927 | INT
928 { $$ = yylval.gs; }
929 | LONG
930 { $$ = yylval.gs; }
931 | FLOAT
932 { $$ = yylval.gs; }
933 | DOUBLE
934 { $$ = yylval.gs; }
935 | SIGNED
936 { $$ = yylval.gs; }
937 | UNSIGNED
938 { $$ = yylval.gs; }
939 | _BOOL
940 { $$ = yylval.gs; }
941 | _COMPLEX
942 { $$ = yylval.gs; }
943 | _IMAGINARY
944 { $$ = yylval.gs; }
945 | FLOATING_POINT
946 { $$ = yylval.gs; }
947 | INTEGER
948 { $$ = yylval.gs; }
949 | STRING
950 { $$ = yylval.gs; }
951 | ENUM
952 { $$ = yylval.gs; }
953 | VARIANT
954 { $$ = yylval.gs; }
955 | STRUCT
956 { $$ = yylval.gs; }
957 | CONST
958 { $$ = yylval.gs; }
959 | TYPEDEF
960 { $$ = yylval.gs; }
961 | EVENT
962 { $$ = yylval.gs; }
963 | STREAM
964 { $$ = yylval.gs; }
965 | TRACE
966 { $$ = yylval.gs; }
967 | TOK_ALIGN
968 { $$ = yylval.gs; }
969 ;
970
971 /* 1.5 Constants */
972
973 c_char_sequence:
974 c_char
975 { $$ = $1; }
976 | c_char_sequence c_char
977 { $$ = gc_string_append(scanner, $1, $2); }
978 ;
979
980 c_char:
981 CHAR_STRING_TOKEN
982 { $$ = yylval.gs; }
983 | ESCSEQ
984 {
985 reparent_error(scanner, "escape sequences not supported yet");
986 }
987 ;
988
989 /* 1.6 String literals */
990
991 s_char_sequence:
992 s_char
993 { $$ = $1; }
994 | s_char_sequence s_char
995 { $$ = gc_string_append(scanner, $1, $2); }
996 ;
997
998 s_char:
999 CHAR_STRING_TOKEN
1000 { $$ = yylval.gs; }
1001 | ESCSEQ
1002 {
1003 reparent_error(scanner, "escape sequences not supported yet");
1004 }
1005 ;
1006
1007 /* 2: Phrase structure grammar */
1008
1009 postfix_expression:
1010 IDENTIFIER
1011 {
1012 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1013 $$->u.unary_expression.type = UNARY_STRING;
1014 $$->u.unary_expression.u.string = yylval.gs->s;
1015 }
1016 | ID_TYPE
1017 {
1018 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1019 $$->u.unary_expression.type = UNARY_STRING;
1020 $$->u.unary_expression.u.string = yylval.gs->s;
1021 }
1022 | keywords
1023 {
1024 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1025 $$->u.unary_expression.type = UNARY_STRING;
1026 $$->u.unary_expression.u.string = yylval.gs->s;
1027 }
1028 | DECIMAL_CONSTANT
1029 {
1030 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1031 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
1032 sscanf(yylval.gs->s, "%" PRIu64,
1033 &$$->u.unary_expression.u.unsigned_constant);
1034 }
1035 | OCTAL_CONSTANT
1036 {
1037 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1038 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
1039 sscanf(yylval.gs->s, "0%" PRIo64,
1040 &$$->u.unary_expression.u.unsigned_constant);
1041 }
1042 | HEXADECIMAL_CONSTANT
1043 {
1044 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1045 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
1046 sscanf(yylval.gs->s, "0x%" PRIx64,
1047 &$$->u.unary_expression.u.unsigned_constant);
1048 }
1049 | STRING_LITERAL_START DQUOTE
1050 {
1051 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1052 $$->u.unary_expression.type = UNARY_STRING;
1053 $$->u.unary_expression.u.string = "";
1054 }
1055 | STRING_LITERAL_START s_char_sequence DQUOTE
1056 {
1057 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1058 $$->u.unary_expression.type = UNARY_STRING;
1059 $$->u.unary_expression.u.string = $2->s;
1060 }
1061 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
1062 {
1063 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1064 $$->u.unary_expression.type = UNARY_STRING;
1065 $$->u.unary_expression.u.string = $2->s;
1066 }
1067 | LPAREN unary_expression RPAREN
1068 {
1069 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1070 $$->u.unary_expression.type = UNARY_NESTED;
1071 $$->u.unary_expression.u.nested_exp = $2;
1072 }
1073 | postfix_expression LSBRAC unary_expression RSBRAC
1074 {
1075 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1076 $$->u.unary_expression.type = UNARY_SBRAC;
1077 $$->u.unary_expression.u.sbrac_exp = $3;
1078 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1079 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1080 }
1081 | postfix_expression DOT IDENTIFIER
1082 {
1083 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1084 $$->u.unary_expression.type = UNARY_STRING;
1085 $$->u.unary_expression.u.string = yylval.gs->s;
1086 $$->u.unary_expression.link = UNARY_DOTLINK;
1087 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1088 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1089 }
1090 | postfix_expression DOT ID_TYPE
1091 {
1092 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1093 $$->u.unary_expression.type = UNARY_STRING;
1094 $$->u.unary_expression.u.string = yylval.gs->s;
1095 $$->u.unary_expression.link = UNARY_DOTLINK;
1096 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1097 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1098 }
1099 | postfix_expression RARROW IDENTIFIER
1100 {
1101 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1102 $$->u.unary_expression.type = UNARY_STRING;
1103 $$->u.unary_expression.u.string = yylval.gs->s;
1104 $$->u.unary_expression.link = UNARY_ARROWLINK;
1105 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1106 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1107 }
1108 | postfix_expression RARROW ID_TYPE
1109 {
1110 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1111 $$->u.unary_expression.type = UNARY_STRING;
1112 $$->u.unary_expression.u.string = yylval.gs->s;
1113 $$->u.unary_expression.link = UNARY_ARROWLINK;
1114 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1115 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1116 }
1117 ;
1118
1119 unary_expression:
1120 postfix_expression
1121 { $$ = $1; }
1122 | PLUS postfix_expression
1123 { $$ = $2; }
1124 | MINUS postfix_expression
1125 {
1126 $$ = $2;
1127 if ($$->u.unary_expression.type != UNARY_SIGNED_CONSTANT
1128 && $$->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT)
1129 reparent_error(scanner, "expecting numeric constant");
1130
1131 if ($$->u.unary_expression.type == UNARY_UNSIGNED_CONSTANT) {
1132 $$->u.unary_expression.type = UNARY_SIGNED_CONSTANT;
1133 $$->u.unary_expression.u.signed_constant =
1134 -($$->u.unary_expression.u.unsigned_constant);
1135 } else {
1136 $$->u.unary_expression.u.signed_constant =
1137 -($$->u.unary_expression.u.signed_constant);
1138 }
1139 }
1140 ;
1141
1142 unary_expression_or_range:
1143 unary_expression DOTDOTDOT unary_expression
1144 {
1145 $$ = $1;
1146 _cds_list_splice_tail(&($3)->tmp_head, &($$)->tmp_head);
1147 $3->u.unary_expression.link = UNARY_DOTDOTDOT;
1148 }
1149 | unary_expression
1150 { $$ = $1; }
1151 ;
1152
1153 /* 2.2: Declarations */
1154
1155 declaration:
1156 declaration_specifiers SEMICOLON
1157 { $$ = $1; }
1158 | event_declaration
1159 { $$ = $1; }
1160 | stream_declaration
1161 { $$ = $1; }
1162 | trace_declaration
1163 { $$ = $1; }
1164 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
1165 {
1166 struct ctf_node *list;
1167
1168 $$ = make_node(scanner, NODE_TYPEDEF);
1169 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1170 $$->u._typedef.type_specifier_list = list;
1171 _cds_list_splice_tail(&($1)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1172 _cds_list_splice_tail(&($3)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1173 _cds_list_splice_tail(&($4)->tmp_head, &($$)->u._typedef.type_declarators);
1174 }
1175 | TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
1176 {
1177 struct ctf_node *list;
1178
1179 $$ = make_node(scanner, NODE_TYPEDEF);
1180 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1181 $$->u._typedef.type_specifier_list = list;
1182 _cds_list_splice_tail(&($2)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1183 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
1184 }
1185 | declaration_specifiers TYPEDEF type_declarator_list SEMICOLON
1186 {
1187 struct ctf_node *list;
1188
1189 $$ = make_node(scanner, NODE_TYPEDEF);
1190 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1191 $$->u._typedef.type_specifier_list = list;
1192 _cds_list_splice_tail(&($1)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1193 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
1194 }
1195 | TYPEALIAS declaration_specifiers abstract_declarator_list TYPEASSIGN alias_declaration_specifiers alias_abstract_declarator_list SEMICOLON
1196 {
1197 struct ctf_node *list;
1198
1199 $$ = make_node(scanner, NODE_TYPEALIAS);
1200 $$->u.typealias.target = make_node(scanner, NODE_TYPEALIAS_TARGET);
1201 $$->u.typealias.alias = make_node(scanner, NODE_TYPEALIAS_ALIAS);
1202
1203 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1204 $$->u.typealias.target->u.typealias_target.type_specifier_list = list;
1205 _cds_list_splice_tail(&($2)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1206 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.typealias.target->u.typealias_target.type_declarators);
1207
1208 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1209 $$->u.typealias.alias->u.typealias_alias.type_specifier_list = list;
1210 _cds_list_splice_tail(&($5)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1211 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.type_declarators);
1212 }
1213 ;
1214
1215 event_declaration:
1216 event_declaration_begin event_declaration_end
1217 {
1218 $$ = make_node(scanner, NODE_EVENT);
1219 }
1220 | event_declaration_begin ctf_assignment_expression_list event_declaration_end
1221 {
1222 $$ = make_node(scanner, NODE_EVENT);
1223 if (set_parent_node($2, $$))
1224 reparent_error(scanner, "event_declaration");
1225 }
1226 ;
1227
1228 event_declaration_begin:
1229 EVENT LBRAC
1230 { push_scope(scanner); }
1231 ;
1232
1233 event_declaration_end:
1234 RBRAC SEMICOLON
1235 { pop_scope(scanner); }
1236 ;
1237
1238
1239 stream_declaration:
1240 stream_declaration_begin stream_declaration_end
1241 {
1242 $$ = make_node(scanner, NODE_STREAM);
1243 }
1244 | stream_declaration_begin ctf_assignment_expression_list stream_declaration_end
1245 {
1246 $$ = make_node(scanner, NODE_STREAM);
1247 if (set_parent_node($2, $$))
1248 reparent_error(scanner, "stream_declaration");
1249 }
1250 ;
1251
1252 stream_declaration_begin:
1253 STREAM LBRAC
1254 { push_scope(scanner); }
1255 ;
1256
1257 stream_declaration_end:
1258 RBRAC SEMICOLON
1259 { pop_scope(scanner); }
1260 ;
1261
1262
1263 trace_declaration:
1264 trace_declaration_begin trace_declaration_end
1265 {
1266 $$ = make_node(scanner, NODE_TRACE);
1267 }
1268 | trace_declaration_begin ctf_assignment_expression_list trace_declaration_end
1269 {
1270 $$ = make_node(scanner, NODE_TRACE);
1271 if (set_parent_node($2, $$))
1272 reparent_error(scanner, "trace_declaration");
1273 }
1274 ;
1275
1276 trace_declaration_begin:
1277 TRACE LBRAC
1278 { push_scope(scanner); }
1279 ;
1280
1281 trace_declaration_end:
1282 RBRAC SEMICOLON
1283 { pop_scope(scanner); }
1284 ;
1285
1286 integer_declaration_specifiers:
1287 CONST
1288 {
1289 struct ctf_node *node;
1290
1291 $$ = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1292 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1293 node->u.type_specifier.type = TYPESPEC_CONST;
1294 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1295 }
1296 | integer_type_specifier
1297 {
1298 struct ctf_node *node;
1299
1300 $$ = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1301 node = $1;
1302 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1303 }
1304 | integer_declaration_specifiers CONST
1305 {
1306 struct ctf_node *node;
1307
1308 $$ = $1;
1309 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1310 node->u.type_specifier.type = TYPESPEC_CONST;
1311 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1312 }
1313 | integer_declaration_specifiers integer_type_specifier
1314 {
1315 $$ = $1;
1316 cds_list_add_tail(&($2)->siblings, &($$)->u.type_specifier_list.head);
1317 }
1318 ;
1319
1320 declaration_specifiers:
1321 CONST
1322 {
1323 struct ctf_node *node;
1324
1325 $$ = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1326 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1327 node->u.type_specifier.type = TYPESPEC_CONST;
1328 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1329 }
1330 | type_specifier
1331 {
1332 struct ctf_node *node;
1333
1334 $$ = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1335 node = $1;
1336 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1337 }
1338 | declaration_specifiers CONST
1339 {
1340 struct ctf_node *node;
1341
1342 $$ = $1;
1343 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1344 node->u.type_specifier.type = TYPESPEC_CONST;
1345 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1346 }
1347 | declaration_specifiers type_specifier
1348 {
1349 $$ = $1;
1350 cds_list_add_tail(&($2)->siblings, &($$)->u.type_specifier_list.head);
1351 }
1352 ;
1353
1354 type_declarator_list:
1355 type_declarator
1356 { $$ = $1; }
1357 | type_declarator_list COMMA type_declarator
1358 {
1359 $$ = $1;
1360 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
1361 }
1362 ;
1363
1364 integer_type_specifier:
1365 CHAR
1366 {
1367 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1368 $$->u.type_specifier.type = TYPESPEC_CHAR;
1369 }
1370 | SHORT
1371 {
1372 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1373 $$->u.type_specifier.type = TYPESPEC_SHORT;
1374 }
1375 | INT
1376 {
1377 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1378 $$->u.type_specifier.type = TYPESPEC_INT;
1379 }
1380 | LONG
1381 {
1382 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1383 $$->u.type_specifier.type = TYPESPEC_LONG;
1384 }
1385 | SIGNED
1386 {
1387 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1388 $$->u.type_specifier.type = TYPESPEC_SIGNED;
1389 }
1390 | UNSIGNED
1391 {
1392 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1393 $$->u.type_specifier.type = TYPESPEC_UNSIGNED;
1394 }
1395 | _BOOL
1396 {
1397 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1398 $$->u.type_specifier.type = TYPESPEC_BOOL;
1399 }
1400 | ID_TYPE
1401 {
1402 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1403 $$->u.type_specifier.type = TYPESPEC_ID_TYPE;
1404 $$->u.type_specifier.id_type = yylval.gs->s;
1405 }
1406 | INTEGER LBRAC RBRAC
1407 {
1408 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1409 $$->u.type_specifier.type = TYPESPEC_INTEGER;
1410 $$->u.type_specifier.node = make_node(scanner, NODE_INTEGER);
1411 }
1412 | INTEGER LBRAC ctf_assignment_expression_list RBRAC
1413 {
1414 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1415 $$->u.type_specifier.type = TYPESPEC_INTEGER;
1416 $$->u.type_specifier.node = make_node(scanner, NODE_INTEGER);
1417 if (set_parent_node($3, $$->u.type_specifier.node))
1418 reparent_error(scanner, "integer reparent error");
1419 }
1420 ;
1421
1422 type_specifier:
1423 VOID
1424 {
1425 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1426 $$->u.type_specifier.type = TYPESPEC_VOID;
1427 }
1428 | CHAR
1429 {
1430 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1431 $$->u.type_specifier.type = TYPESPEC_CHAR;
1432 }
1433 | SHORT
1434 {
1435 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1436 $$->u.type_specifier.type = TYPESPEC_SHORT;
1437 }
1438 | INT
1439 {
1440 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1441 $$->u.type_specifier.type = TYPESPEC_INT;
1442 }
1443 | LONG
1444 {
1445 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1446 $$->u.type_specifier.type = TYPESPEC_LONG;
1447 }
1448 | FLOAT
1449 {
1450 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1451 $$->u.type_specifier.type = TYPESPEC_FLOAT;
1452 }
1453 | DOUBLE
1454 {
1455 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1456 $$->u.type_specifier.type = TYPESPEC_DOUBLE;
1457 }
1458 | SIGNED
1459 {
1460 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1461 $$->u.type_specifier.type = TYPESPEC_SIGNED;
1462 }
1463 | UNSIGNED
1464 {
1465 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1466 $$->u.type_specifier.type = TYPESPEC_UNSIGNED;
1467 }
1468 | _BOOL
1469 {
1470 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1471 $$->u.type_specifier.type = TYPESPEC_BOOL;
1472 }
1473 | _COMPLEX
1474 {
1475 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1476 $$->u.type_specifier.type = TYPESPEC_COMPLEX;
1477 }
1478 | _IMAGINARY
1479 {
1480 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1481 $$->u.type_specifier.type = TYPESPEC_IMAGINARY;
1482 }
1483 | ID_TYPE
1484 {
1485 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1486 $$->u.type_specifier.type = TYPESPEC_ID_TYPE;
1487 $$->u.type_specifier.id_type = yylval.gs->s;
1488 }
1489 | FLOATING_POINT LBRAC RBRAC
1490 {
1491 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1492 $$->u.type_specifier.type = TYPESPEC_FLOATING_POINT;
1493 $$->u.type_specifier.node = make_node(scanner, NODE_FLOATING_POINT);
1494 }
1495 | FLOATING_POINT LBRAC ctf_assignment_expression_list RBRAC
1496 {
1497 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1498 $$->u.type_specifier.type = TYPESPEC_FLOATING_POINT;
1499 $$->u.type_specifier.node = make_node(scanner, NODE_FLOATING_POINT);
1500 if (set_parent_node($3, $$->u.type_specifier.node))
1501 reparent_error(scanner, "floating point reparent error");
1502 }
1503 | INTEGER LBRAC RBRAC
1504 {
1505 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1506 $$->u.type_specifier.type = TYPESPEC_INTEGER;
1507 $$->u.type_specifier.node = make_node(scanner, NODE_INTEGER);
1508 }
1509 | INTEGER LBRAC ctf_assignment_expression_list RBRAC
1510 {
1511 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1512 $$->u.type_specifier.type = TYPESPEC_INTEGER;
1513 $$->u.type_specifier.node = make_node(scanner, NODE_INTEGER);
1514 if (set_parent_node($3, $$->u.type_specifier.node))
1515 reparent_error(scanner, "integer reparent error");
1516 }
1517 | STRING
1518 {
1519 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1520 $$->u.type_specifier.type = TYPESPEC_STRING;
1521 $$->u.type_specifier.node = make_node(scanner, NODE_STRING);
1522 }
1523 | STRING LBRAC RBRAC
1524 {
1525 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1526 $$->u.type_specifier.type = TYPESPEC_STRING;
1527 $$->u.type_specifier.node = make_node(scanner, NODE_STRING);
1528 }
1529 | STRING LBRAC ctf_assignment_expression_list RBRAC
1530 {
1531 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1532 $$->u.type_specifier.type = TYPESPEC_STRING;
1533 $$->u.type_specifier.node = make_node(scanner, NODE_STRING);
1534 if (set_parent_node($3, $$->u.type_specifier.node))
1535 reparent_error(scanner, "string reparent error");
1536 }
1537 | ENUM enum_type_specifier
1538 {
1539 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1540 $$->u.type_specifier.type = TYPESPEC_ENUM;
1541 $$->u.type_specifier.node = $2;
1542 }
1543 | VARIANT variant_type_specifier
1544 {
1545 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1546 $$->u.type_specifier.type = TYPESPEC_VARIANT;
1547 $$->u.type_specifier.node = $2;
1548 }
1549 | STRUCT struct_type_specifier
1550 {
1551 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1552 $$->u.type_specifier.type = TYPESPEC_STRUCT;
1553 $$->u.type_specifier.node = $2;
1554 }
1555 ;
1556
1557 struct_type_specifier:
1558 struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
1559 {
1560 $$ = make_node(scanner, NODE_STRUCT);
1561 $$->u._struct.has_body = 1;
1562 if ($2 && set_parent_node($2, $$))
1563 reparent_error(scanner, "struct reparent error");
1564 }
1565 | IDENTIFIER struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
1566 {
1567 $$ = make_node(scanner, NODE_STRUCT);
1568 $$->u._struct.has_body = 1;
1569 $$->u._struct.name = $1->s;
1570 if ($3 && set_parent_node($3, $$))
1571 reparent_error(scanner, "struct reparent error");
1572 }
1573 | ID_TYPE struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
1574 {
1575 $$ = make_node(scanner, NODE_STRUCT);
1576 $$->u._struct.has_body = 1;
1577 $$->u._struct.name = $1->s;
1578 if ($3 && set_parent_node($3, $$))
1579 reparent_error(scanner, "struct reparent error");
1580 }
1581 | IDENTIFIER
1582 {
1583 $$ = make_node(scanner, NODE_STRUCT);
1584 $$->u._struct.has_body = 0;
1585 $$->u._struct.name = $1->s;
1586 }
1587 | ID_TYPE
1588 {
1589 $$ = make_node(scanner, NODE_STRUCT);
1590 $$->u._struct.has_body = 0;
1591 $$->u._struct.name = $1->s;
1592 }
1593 | struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end TOK_ALIGN LPAREN unary_expression RPAREN
1594 {
1595 $$ = make_node(scanner, NODE_STRUCT);
1596 $$->u._struct.has_body = 1;
1597 cds_list_add_tail(&($6)->siblings, &$$->u._struct.min_align);
1598 if ($2 && set_parent_node($2, $$))
1599 reparent_error(scanner, "struct reparent error");
1600 }
1601 | IDENTIFIER struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end TOK_ALIGN LPAREN unary_expression RPAREN
1602 {
1603 $$ = make_node(scanner, NODE_STRUCT);
1604 $$->u._struct.has_body = 1;
1605 $$->u._struct.name = $1->s;
1606 cds_list_add_tail(&($7)->siblings, &$$->u._struct.min_align);
1607 if ($3 && set_parent_node($3, $$))
1608 reparent_error(scanner, "struct reparent error");
1609 }
1610 | ID_TYPE struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end TOK_ALIGN LPAREN unary_expression RPAREN
1611 {
1612 $$ = make_node(scanner, NODE_STRUCT);
1613 $$->u._struct.has_body = 1;
1614 $$->u._struct.name = $1->s;
1615 cds_list_add_tail(&($7)->siblings, &$$->u._struct.min_align);
1616 if ($3 && set_parent_node($3, $$))
1617 reparent_error(scanner, "struct reparent error");
1618 }
1619 ;
1620
1621 struct_declaration_begin:
1622 LBRAC
1623 { push_scope(scanner); }
1624 ;
1625
1626 struct_declaration_end:
1627 RBRAC
1628 { pop_scope(scanner); }
1629 ;
1630
1631 variant_type_specifier:
1632 variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1633 {
1634 $$ = make_node(scanner, NODE_VARIANT);
1635 $$->u.variant.has_body = 1;
1636 if ($2 && set_parent_node($2, $$))
1637 reparent_error(scanner, "variant reparent error");
1638 }
1639 | LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1640 {
1641 $$ = make_node(scanner, NODE_VARIANT);
1642 $$->u.variant.has_body = 1;
1643 $$->u.variant.choice = $2->s;
1644 if ($5 && set_parent_node($5, $$))
1645 reparent_error(scanner, "variant reparent error");
1646 }
1647 | LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1648 {
1649 $$ = make_node(scanner, NODE_VARIANT);
1650 $$->u.variant.has_body = 1;
1651 $$->u.variant.choice = $2->s;
1652 if ($5 && set_parent_node($5, $$))
1653 reparent_error(scanner, "variant reparent error");
1654 }
1655 | IDENTIFIER variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1656 {
1657 $$ = make_node(scanner, NODE_VARIANT);
1658 $$->u.variant.has_body = 1;
1659 $$->u.variant.name = $1->s;
1660 if ($3 && set_parent_node($3, $$))
1661 reparent_error(scanner, "variant reparent error");
1662 }
1663 | IDENTIFIER LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1664 {
1665 $$ = make_node(scanner, NODE_VARIANT);
1666 $$->u.variant.has_body = 1;
1667 $$->u.variant.name = $1->s;
1668 $$->u.variant.choice = $3->s;
1669 if ($6 && set_parent_node($6, $$))
1670 reparent_error(scanner, "variant reparent error");
1671 }
1672 | IDENTIFIER LT IDENTIFIER GT
1673 {
1674 $$ = make_node(scanner, NODE_VARIANT);
1675 $$->u.variant.has_body = 0;
1676 $$->u.variant.name = $1->s;
1677 $$->u.variant.choice = $3->s;
1678 }
1679 | IDENTIFIER LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1680 {
1681 $$ = make_node(scanner, NODE_VARIANT);
1682 $$->u.variant.has_body = 1;
1683 $$->u.variant.name = $1->s;
1684 $$->u.variant.choice = $3->s;
1685 if ($6 && set_parent_node($6, $$))
1686 reparent_error(scanner, "variant reparent error");
1687 }
1688 | IDENTIFIER LT ID_TYPE GT
1689 {
1690 $$ = make_node(scanner, NODE_VARIANT);
1691 $$->u.variant.has_body = 0;
1692 $$->u.variant.name = $1->s;
1693 $$->u.variant.choice = $3->s;
1694 }
1695 | ID_TYPE variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1696 {
1697 $$ = make_node(scanner, NODE_VARIANT);
1698 $$->u.variant.has_body = 1;
1699 $$->u.variant.name = $1->s;
1700 if ($3 && set_parent_node($3, $$))
1701 reparent_error(scanner, "variant reparent error");
1702 }
1703 | ID_TYPE LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1704 {
1705 $$ = make_node(scanner, NODE_VARIANT);
1706 $$->u.variant.has_body = 1;
1707 $$->u.variant.name = $1->s;
1708 $$->u.variant.choice = $3->s;
1709 if ($6 && set_parent_node($6, $$))
1710 reparent_error(scanner, "variant reparent error");
1711 }
1712 | ID_TYPE LT IDENTIFIER GT
1713 {
1714 $$ = make_node(scanner, NODE_VARIANT);
1715 $$->u.variant.has_body = 0;
1716 $$->u.variant.name = $1->s;
1717 $$->u.variant.choice = $3->s;
1718 }
1719 | ID_TYPE LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1720 {
1721 $$ = make_node(scanner, NODE_VARIANT);
1722 $$->u.variant.has_body = 1;
1723 $$->u.variant.name = $1->s;
1724 $$->u.variant.choice = $3->s;
1725 if ($6 && set_parent_node($6, $$))
1726 reparent_error(scanner, "variant reparent error");
1727 }
1728 | ID_TYPE LT ID_TYPE GT
1729 {
1730 $$ = make_node(scanner, NODE_VARIANT);
1731 $$->u.variant.has_body = 0;
1732 $$->u.variant.name = $1->s;
1733 $$->u.variant.choice = $3->s;
1734 }
1735 ;
1736
1737 variant_declaration_begin:
1738 LBRAC
1739 { push_scope(scanner); }
1740 ;
1741
1742 variant_declaration_end:
1743 RBRAC
1744 { pop_scope(scanner); }
1745 ;
1746
1747 enum_type_specifier:
1748 LBRAC enumerator_list RBRAC
1749 {
1750 $$ = make_node(scanner, NODE_ENUM);
1751 $$->u._enum.has_body = 1;
1752 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u._enum.enumerator_list);
1753 }
1754 | COLON integer_declaration_specifiers LBRAC enumerator_list RBRAC
1755 {
1756 $$ = make_node(scanner, NODE_ENUM);
1757 $$->u._enum.has_body = 1;
1758 ($$)->u._enum.container_type = $2;
1759 _cds_list_splice_tail(&($4)->tmp_head, &($$)->u._enum.enumerator_list);
1760 }
1761 | IDENTIFIER LBRAC enumerator_list RBRAC
1762 {
1763 $$ = make_node(scanner, NODE_ENUM);
1764 $$->u._enum.has_body = 1;
1765 $$->u._enum.enum_id = $1->s;
1766 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._enum.enumerator_list);
1767 }
1768 | IDENTIFIER COLON integer_declaration_specifiers LBRAC enumerator_list RBRAC
1769 {
1770 $$ = make_node(scanner, NODE_ENUM);
1771 $$->u._enum.has_body = 1;
1772 $$->u._enum.enum_id = $1->s;
1773 ($$)->u._enum.container_type = $3;
1774 _cds_list_splice_tail(&($5)->tmp_head, &($$)->u._enum.enumerator_list);
1775 }
1776 | ID_TYPE LBRAC enumerator_list RBRAC
1777 {
1778 $$ = make_node(scanner, NODE_ENUM);
1779 $$->u._enum.has_body = 1;
1780 $$->u._enum.enum_id = $1->s;
1781 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._enum.enumerator_list);
1782 }
1783 | ID_TYPE COLON integer_declaration_specifiers LBRAC enumerator_list RBRAC
1784 {
1785 $$ = make_node(scanner, NODE_ENUM);
1786 $$->u._enum.has_body = 1;
1787 $$->u._enum.enum_id = $1->s;
1788 ($$)->u._enum.container_type = $3;
1789 _cds_list_splice_tail(&($5)->tmp_head, &($$)->u._enum.enumerator_list);
1790 }
1791 | LBRAC enumerator_list COMMA RBRAC
1792 {
1793 $$ = make_node(scanner, NODE_ENUM);
1794 $$->u._enum.has_body = 1;
1795 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u._enum.enumerator_list);
1796 }
1797 | COLON integer_declaration_specifiers LBRAC enumerator_list COMMA RBRAC
1798 {
1799 $$ = make_node(scanner, NODE_ENUM);
1800 $$->u._enum.has_body = 1;
1801 ($$)->u._enum.container_type = $2;
1802 _cds_list_splice_tail(&($4)->tmp_head, &($$)->u._enum.enumerator_list);
1803 }
1804 | IDENTIFIER LBRAC enumerator_list COMMA RBRAC
1805 {
1806 $$ = make_node(scanner, NODE_ENUM);
1807 $$->u._enum.has_body = 1;
1808 $$->u._enum.enum_id = $1->s;
1809 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._enum.enumerator_list);
1810 }
1811 | IDENTIFIER COLON integer_declaration_specifiers LBRAC enumerator_list COMMA RBRAC
1812 {
1813 $$ = make_node(scanner, NODE_ENUM);
1814 $$->u._enum.has_body = 1;
1815 $$->u._enum.enum_id = $1->s;
1816 ($$)->u._enum.container_type = $3;
1817 _cds_list_splice_tail(&($5)->tmp_head, &($$)->u._enum.enumerator_list);
1818 }
1819 | IDENTIFIER
1820 {
1821 $$ = make_node(scanner, NODE_ENUM);
1822 $$->u._enum.has_body = 0;
1823 $$->u._enum.enum_id = $1->s;
1824 }
1825 | ID_TYPE LBRAC enumerator_list COMMA RBRAC
1826 {
1827 $$ = make_node(scanner, NODE_ENUM);
1828 $$->u._enum.has_body = 1;
1829 $$->u._enum.enum_id = $1->s;
1830 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._enum.enumerator_list);
1831 }
1832 | ID_TYPE COLON integer_declaration_specifiers LBRAC enumerator_list COMMA RBRAC
1833 {
1834 $$ = make_node(scanner, NODE_ENUM);
1835 $$->u._enum.has_body = 1;
1836 $$->u._enum.enum_id = $1->s;
1837 ($$)->u._enum.container_type = $3;
1838 _cds_list_splice_tail(&($5)->tmp_head, &($$)->u._enum.enumerator_list);
1839 }
1840 | ID_TYPE
1841 {
1842 $$ = make_node(scanner, NODE_ENUM);
1843 $$->u._enum.has_body = 0;
1844 $$->u._enum.enum_id = $1->s;
1845 }
1846 ;
1847
1848 struct_or_variant_declaration_list:
1849 /* empty */
1850 { $$ = NULL; }
1851 | struct_or_variant_declaration_list struct_or_variant_declaration
1852 {
1853 if ($1) {
1854 $$ = $1;
1855 cds_list_add_tail(&($2)->siblings, &($$)->tmp_head);
1856 } else {
1857 $$ = $2;
1858 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1859 }
1860 }
1861 ;
1862
1863 struct_or_variant_declaration:
1864 declaration_specifiers struct_or_variant_declarator_list SEMICOLON
1865 {
1866 struct ctf_node *list;
1867
1868 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1869 _cds_list_splice_tail(&($1)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1870 $$ = make_node(scanner, NODE_STRUCT_OR_VARIANT_DECLARATION);
1871 ($$)->u.struct_or_variant_declaration.type_specifier_list = list;
1872 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u.struct_or_variant_declaration.type_declarators);
1873 }
1874 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
1875 {
1876 struct ctf_node *list;
1877
1878 $$ = make_node(scanner, NODE_TYPEDEF);
1879 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1880 $$->u._typedef.type_specifier_list = list;
1881 _cds_list_splice_tail(&($1)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1882 _cds_list_splice_tail(&($3)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1883 _cds_list_splice_tail(&($4)->tmp_head, &($$)->u._typedef.type_declarators);
1884 }
1885 | TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
1886 {
1887 struct ctf_node *list;
1888
1889 $$ = make_node(scanner, NODE_TYPEDEF);
1890 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1891 $$->u._typedef.type_specifier_list = list;
1892 _cds_list_splice_tail(&($2)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1893 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
1894 }
1895 | declaration_specifiers TYPEDEF type_declarator_list SEMICOLON
1896 {
1897 struct ctf_node *list;
1898
1899 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1900 _cds_list_splice_tail(&($1)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1901 $$ = make_node(scanner, NODE_TYPEDEF);
1902 ($$)->u.struct_or_variant_declaration.type_specifier_list = list;
1903 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
1904 }
1905 | TYPEALIAS declaration_specifiers abstract_declarator_list TYPEASSIGN alias_declaration_specifiers alias_abstract_declarator_list SEMICOLON
1906 {
1907 struct ctf_node *list;
1908
1909 $$ = make_node(scanner, NODE_TYPEALIAS);
1910 $$->u.typealias.target = make_node(scanner, NODE_TYPEALIAS_TARGET);
1911 $$->u.typealias.alias = make_node(scanner, NODE_TYPEALIAS_ALIAS);
1912
1913 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1914 $$->u.typealias.target->u.typealias_target.type_specifier_list = list;
1915 _cds_list_splice_tail(&($2)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1916 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.typealias.target->u.typealias_target.type_declarators);
1917
1918 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1919 $$->u.typealias.alias->u.typealias_alias.type_specifier_list = list;
1920 _cds_list_splice_tail(&($5)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1921 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.type_declarators);
1922 }
1923 ;
1924
1925 alias_declaration_specifiers:
1926 CONST
1927 {
1928 struct ctf_node *node;
1929
1930 $$ = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1931 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1932 node->u.type_specifier.type = TYPESPEC_CONST;
1933 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1934 }
1935 | type_specifier
1936 {
1937 struct ctf_node *node;
1938
1939 $$ = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1940 node = $1;
1941 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1942 }
1943 | IDENTIFIER
1944 {
1945 struct ctf_node *node;
1946
1947 add_type(scanner, $1);
1948 $$ = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1949 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1950 node->u.type_specifier.type = TYPESPEC_ID_TYPE;
1951 node->u.type_specifier.id_type = yylval.gs->s;
1952 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1953 }
1954 | alias_declaration_specifiers CONST
1955 {
1956 struct ctf_node *node;
1957
1958 $$ = $1;
1959 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1960 node->u.type_specifier.type = TYPESPEC_CONST;
1961 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1962 }
1963 | alias_declaration_specifiers type_specifier
1964 {
1965 $$ = $1;
1966 cds_list_add_tail(&($2)->siblings, &($$)->u.type_specifier_list.head);
1967 }
1968 | alias_declaration_specifiers IDENTIFIER
1969 {
1970 struct ctf_node *node;
1971
1972 add_type(scanner, $2);
1973 $$ = $1;
1974 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1975 node->u.type_specifier.type = TYPESPEC_ID_TYPE;
1976 node->u.type_specifier.id_type = yylval.gs->s;
1977 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1978 }
1979 ;
1980
1981 struct_or_variant_declarator_list:
1982 struct_or_variant_declarator
1983 { $$ = $1; }
1984 | struct_or_variant_declarator_list COMMA struct_or_variant_declarator
1985 {
1986 $$ = $1;
1987 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
1988 }
1989 ;
1990
1991 struct_or_variant_declarator:
1992 declarator
1993 { $$ = $1; }
1994 | COLON unary_expression
1995 { $$ = $2; }
1996 | declarator COLON unary_expression
1997 {
1998 $$ = $1;
1999 if (set_parent_node($3, $1))
2000 reparent_error(scanner, "struct_or_variant_declarator");
2001 }
2002 ;
2003
2004 enumerator_list:
2005 enumerator
2006 { $$ = $1; }
2007 | enumerator_list COMMA enumerator
2008 {
2009 $$ = $1;
2010 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
2011 }
2012 ;
2013
2014 enumerator:
2015 IDENTIFIER
2016 {
2017 $$ = make_node(scanner, NODE_ENUMERATOR);
2018 $$->u.enumerator.id = $1->s;
2019 }
2020 | ID_TYPE
2021 {
2022 $$ = make_node(scanner, NODE_ENUMERATOR);
2023 $$->u.enumerator.id = $1->s;
2024 }
2025 | keywords
2026 {
2027 $$ = make_node(scanner, NODE_ENUMERATOR);
2028 $$->u.enumerator.id = $1->s;
2029 }
2030 | STRING_LITERAL_START DQUOTE
2031 {
2032 $$ = make_node(scanner, NODE_ENUMERATOR);
2033 $$->u.enumerator.id = "";
2034 }
2035 | STRING_LITERAL_START s_char_sequence DQUOTE
2036 {
2037 $$ = make_node(scanner, NODE_ENUMERATOR);
2038 $$->u.enumerator.id = $2->s;
2039 }
2040 | IDENTIFIER EQUAL unary_expression_or_range
2041 {
2042 $$ = make_node(scanner, NODE_ENUMERATOR);
2043 $$->u.enumerator.id = $1->s;
2044 cds_list_splice(&($3)->tmp_head, &($$)->u.enumerator.values);
2045 }
2046 | ID_TYPE EQUAL unary_expression_or_range
2047 {
2048 $$ = make_node(scanner, NODE_ENUMERATOR);
2049 $$->u.enumerator.id = $1->s;
2050 cds_list_splice(&($3)->tmp_head, &($$)->u.enumerator.values);
2051 }
2052 | keywords EQUAL unary_expression_or_range
2053 {
2054 $$ = make_node(scanner, NODE_ENUMERATOR);
2055 $$->u.enumerator.id = $1->s;
2056 cds_list_splice(&($3)->tmp_head, &($$)->u.enumerator.values);
2057 }
2058 | STRING_LITERAL_START DQUOTE EQUAL unary_expression_or_range
2059 {
2060 $$ = make_node(scanner, NODE_ENUMERATOR);
2061 $$->u.enumerator.id = "";
2062 cds_list_splice(&($4)->tmp_head, &($$)->u.enumerator.values);
2063 }
2064 | STRING_LITERAL_START s_char_sequence DQUOTE EQUAL unary_expression_or_range
2065 {
2066 $$ = make_node(scanner, NODE_ENUMERATOR);
2067 $$->u.enumerator.id = $2->s;
2068 cds_list_splice(&($5)->tmp_head, &($$)->u.enumerator.values);
2069 }
2070 ;
2071
2072 abstract_declarator_list:
2073 abstract_declarator
2074 { $$ = $1; }
2075 | abstract_declarator_list COMMA abstract_declarator
2076 {
2077 $$ = $1;
2078 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
2079 }
2080 ;
2081
2082 abstract_declarator:
2083 direct_abstract_declarator
2084 { $$ = $1; }
2085 | pointer direct_abstract_declarator
2086 {
2087 $$ = $2;
2088 cds_list_splice(&($1)->tmp_head, &($$)->u.type_declarator.pointers);
2089 }
2090 ;
2091
2092 direct_abstract_declarator:
2093 /* empty */
2094 {
2095 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2096 $$->u.type_declarator.type = TYPEDEC_ID;
2097 /* id is NULL */
2098 }
2099 | IDENTIFIER
2100 {
2101 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2102 $$->u.type_declarator.type = TYPEDEC_ID;
2103 $$->u.type_declarator.u.id = $1->s;
2104 }
2105 | LPAREN abstract_declarator RPAREN
2106 {
2107 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2108 $$->u.type_declarator.type = TYPEDEC_NESTED;
2109 $$->u.type_declarator.u.nested.type_declarator = $2;
2110 }
2111 | direct_abstract_declarator LSBRAC unary_expression RSBRAC
2112 {
2113 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2114 $$->u.type_declarator.type = TYPEDEC_NESTED;
2115 $$->u.type_declarator.u.nested.type_declarator = $1;
2116 CDS_INIT_LIST_HEAD(&($$)->u.type_declarator.u.nested.length);
2117 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.type_declarator.u.nested.length);
2118 }
2119 | direct_abstract_declarator LSBRAC RSBRAC
2120 {
2121 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2122 $$->u.type_declarator.type = TYPEDEC_NESTED;
2123 $$->u.type_declarator.u.nested.type_declarator = $1;
2124 $$->u.type_declarator.u.nested.abstract_array = 1;
2125 }
2126 ;
2127
2128 alias_abstract_declarator_list:
2129 alias_abstract_declarator
2130 { $$ = $1; }
2131 | alias_abstract_declarator_list COMMA alias_abstract_declarator
2132 {
2133 $$ = $1;
2134 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
2135 }
2136 ;
2137
2138 alias_abstract_declarator:
2139 direct_alias_abstract_declarator
2140 { $$ = $1; }
2141 | pointer direct_alias_abstract_declarator
2142 {
2143 $$ = $2;
2144 cds_list_splice(&($1)->tmp_head, &($$)->u.type_declarator.pointers);
2145 }
2146 ;
2147
2148 direct_alias_abstract_declarator:
2149 /* empty */
2150 {
2151 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2152 $$->u.type_declarator.type = TYPEDEC_ID;
2153 /* id is NULL */
2154 }
2155 | LPAREN alias_abstract_declarator RPAREN
2156 {
2157 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2158 $$->u.type_declarator.type = TYPEDEC_NESTED;
2159 $$->u.type_declarator.u.nested.type_declarator = $2;
2160 }
2161 | direct_alias_abstract_declarator LSBRAC unary_expression RSBRAC
2162 {
2163 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2164 $$->u.type_declarator.type = TYPEDEC_NESTED;
2165 $$->u.type_declarator.u.nested.type_declarator = $1;
2166 CDS_INIT_LIST_HEAD(&($$)->u.type_declarator.u.nested.length);
2167 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.type_declarator.u.nested.length);
2168 }
2169 | direct_alias_abstract_declarator LSBRAC RSBRAC
2170 {
2171 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2172 $$->u.type_declarator.type = TYPEDEC_NESTED;
2173 $$->u.type_declarator.u.nested.type_declarator = $1;
2174 $$->u.type_declarator.u.nested.abstract_array = 1;
2175 }
2176 ;
2177
2178 declarator:
2179 direct_declarator
2180 { $$ = $1; }
2181 | pointer direct_declarator
2182 {
2183 $$ = $2;
2184 cds_list_splice(&($1)->tmp_head, &($$)->u.type_declarator.pointers);
2185 }
2186 ;
2187
2188 direct_declarator:
2189 IDENTIFIER
2190 {
2191 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2192 $$->u.type_declarator.type = TYPEDEC_ID;
2193 $$->u.type_declarator.u.id = $1->s;
2194 }
2195 | LPAREN declarator RPAREN
2196 {
2197 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2198 $$->u.type_declarator.type = TYPEDEC_NESTED;
2199 $$->u.type_declarator.u.nested.type_declarator = $2;
2200 }
2201 | direct_declarator LSBRAC unary_expression RSBRAC
2202 {
2203 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2204 $$->u.type_declarator.type = TYPEDEC_NESTED;
2205 $$->u.type_declarator.u.nested.type_declarator = $1;
2206 CDS_INIT_LIST_HEAD(&($$)->u.type_declarator.u.nested.length);
2207 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.type_declarator.u.nested.length);
2208 }
2209 ;
2210
2211 type_declarator:
2212 direct_type_declarator
2213 { $$ = $1; }
2214 | pointer direct_type_declarator
2215 {
2216 $$ = $2;
2217 cds_list_splice(&($1)->tmp_head, &($$)->u.type_declarator.pointers);
2218 }
2219 ;
2220
2221 direct_type_declarator:
2222 IDENTIFIER
2223 {
2224 add_type(scanner, $1);
2225 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2226 $$->u.type_declarator.type = TYPEDEC_ID;
2227 $$->u.type_declarator.u.id = $1->s;
2228 }
2229 | LPAREN type_declarator RPAREN
2230 {
2231 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2232 $$->u.type_declarator.type = TYPEDEC_NESTED;
2233 $$->u.type_declarator.u.nested.type_declarator = $2;
2234 }
2235 | direct_type_declarator LSBRAC unary_expression RSBRAC
2236 {
2237 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2238 $$->u.type_declarator.type = TYPEDEC_NESTED;
2239 $$->u.type_declarator.u.nested.type_declarator = $1;
2240 CDS_INIT_LIST_HEAD(&($$)->u.type_declarator.u.nested.length);
2241 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.type_declarator.u.nested.length);
2242 }
2243 ;
2244
2245 pointer:
2246 STAR
2247 {
2248 $$ = make_node(scanner, NODE_POINTER);
2249 }
2250 | STAR pointer
2251 {
2252 $$ = make_node(scanner, NODE_POINTER);
2253 cds_list_splice(&($2)->tmp_head, &($$)->tmp_head);
2254 }
2255 | STAR type_qualifier_list pointer
2256 {
2257 $$ = make_node(scanner, NODE_POINTER);
2258 $$->u.pointer.const_qualifier = 1;
2259 cds_list_splice(&($3)->tmp_head, &($$)->tmp_head);
2260 }
2261 ;
2262
2263 type_qualifier_list:
2264 /* pointer assumes only const type qualifier */
2265 CONST
2266 | type_qualifier_list CONST
2267 ;
2268
2269 /* 2.3: CTF-specific declarations */
2270
2271 ctf_assignment_expression_list:
2272 ctf_assignment_expression SEMICOLON
2273 { $$ = $1; }
2274 | ctf_assignment_expression_list ctf_assignment_expression SEMICOLON
2275 {
2276 $$ = $1;
2277 cds_list_add_tail(&($2)->siblings, &($$)->tmp_head);
2278 }
2279 ;
2280
2281 ctf_assignment_expression:
2282 unary_expression EQUAL unary_expression
2283 {
2284 /*
2285 * Because we have left and right, cannot use
2286 * set_parent_node.
2287 */
2288 $$ = make_node(scanner, NODE_CTF_EXPRESSION);
2289 _cds_list_splice_tail(&($1)->tmp_head, &($$)->u.ctf_expression.left);
2290 if ($1->u.unary_expression.type != UNARY_STRING)
2291 reparent_error(scanner, "ctf_assignment_expression left expects string");
2292 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.ctf_expression.right);
2293 }
2294 | unary_expression TYPEASSIGN declaration_specifiers /* Only allow struct */
2295 {
2296 /*
2297 * Because we have left and right, cannot use
2298 * set_parent_node.
2299 */
2300 $$ = make_node(scanner, NODE_CTF_EXPRESSION);
2301 _cds_list_splice_tail(&($1)->tmp_head, &($$)->u.ctf_expression.left);
2302 if ($1->u.unary_expression.type != UNARY_STRING)
2303 reparent_error(scanner, "ctf_assignment_expression left expects string");
2304 cds_list_add_tail(&($3)->siblings, &($$)->u.ctf_expression.right);
2305 }
2306 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list
2307 {
2308 struct ctf_node *list;
2309
2310 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
2311 _cds_list_splice_tail(&($1)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
2312 _cds_list_splice_tail(&($3)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
2313 $$ = make_node(scanner, NODE_TYPEDEF);
2314 ($$)->u.struct_or_variant_declaration.type_specifier_list = list;
2315 _cds_list_splice_tail(&($4)->tmp_head, &($$)->u._typedef.type_declarators);
2316 }
2317 | TYPEDEF declaration_specifiers type_declarator_list
2318 {
2319 struct ctf_node *list;
2320
2321 $$ = make_node(scanner, NODE_TYPEDEF);
2322 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
2323 $$->u._typedef.type_specifier_list = list;
2324 _cds_list_splice_tail(&($2)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
2325 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
2326 }
2327 | declaration_specifiers TYPEDEF type_declarator_list
2328 {
2329 struct ctf_node *list;
2330
2331 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
2332 _cds_list_splice_tail(&($1)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
2333 $$ = make_node(scanner, NODE_TYPEDEF);
2334 ($$)->u.struct_or_variant_declaration.type_specifier_list = list;
2335 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
2336 }
2337 | TYPEALIAS declaration_specifiers abstract_declarator_list TYPEASSIGN alias_declaration_specifiers alias_abstract_declarator_list
2338 {
2339 struct ctf_node *list;
2340
2341 $$ = make_node(scanner, NODE_TYPEALIAS);
2342 $$->u.typealias.target = make_node(scanner, NODE_TYPEALIAS_TARGET);
2343 $$->u.typealias.alias = make_node(scanner, NODE_TYPEALIAS_ALIAS);
2344
2345 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
2346 $$->u.typealias.target->u.typealias_target.type_specifier_list = list;
2347 _cds_list_splice_tail(&($2)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
2348 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.typealias.target->u.typealias_target.type_declarators);
2349
2350 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
2351 $$->u.typealias.alias->u.typealias_alias.type_specifier_list = list;
2352 _cds_list_splice_tail(&($5)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
2353 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.type_declarators);
2354 }
2355 ;
This page took 0.117572 seconds and 3 git commands to generate.