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