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