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