io struct generation debugging ongoing
[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 yyset_in(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 yyset_in(input, scanner);
804
805 scanner->ast = ctf_ast_alloc();
806 if (!scanner->ast)
807 goto cleanup_lexer;
808 init_scope(&scanner->root_scope, NULL);
809 scanner->cs = &scanner->root_scope;
810 CDS_INIT_LIST_HEAD(&scanner->allocated_strings);
811
812 return scanner;
813
814 cleanup_lexer:
815 ret = yylex_destroy(scanner->scanner);
816 if (!ret)
817 fprintf(stderr, "yylex_destroy error\n");
818 cleanup_scanner:
819 free(scanner);
820 return NULL;
821 }
822
823 void ctf_scanner_free(struct ctf_scanner *scanner)
824 {
825 int ret;
826
827 finalize_scope(&scanner->root_scope);
828 free_strings(&scanner->allocated_strings);
829 ctf_ast_free(scanner->ast);
830 ret = yylex_destroy(scanner->scanner);
831 if (ret)
832 fprintf(stderr, "yylex_destroy error\n");
833 free(scanner);
834 }
835
836 %}
837
838 %define api.pure
839 /* %locations */
840 %parse-param {struct ctf_scanner *scanner}
841 %lex-param {struct ctf_scanner *scanner}
842 %start file
843 %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
844 %token <gs> IDENTIFIER ID_TYPE
845 %token ERROR
846 %union
847 {
848 long long ll;
849 char c;
850 struct gc_string *gs;
851 struct ctf_node *n;
852 }
853
854 %type <gs> keywords
855 %type <gs> s_char s_char_sequence c_char c_char_sequence
856
857 %type <n> postfix_expression unary_expression unary_expression_or_range
858
859 %type <n> declaration
860 %type <n> event_declaration
861 %type <n> stream_declaration
862 %type <n> trace_declaration
863 %type <n> declaration_specifiers
864 %type <n> alias_declaration_specifiers
865
866 %type <n> type_declarator_list
867 %type <n> type_specifier
868 %type <n> struct_type_specifier
869 %type <n> variant_type_specifier
870 %type <n> declaration_specifiers_or_integer_constant
871 %type <n> enum_type_specifier
872 %type <n> struct_or_variant_declaration_list
873 %type <n> struct_or_variant_declaration
874 %type <n> struct_or_variant_declarator_list
875 %type <n> struct_or_variant_declarator
876 %type <n> enumerator_list
877 %type <n> enumerator
878 %type <n> abstract_declarator_list
879 %type <n> abstract_declarator
880 %type <n> direct_abstract_declarator
881 %type <n> alias_abstract_declarator_list
882 %type <n> alias_abstract_declarator
883 %type <n> direct_alias_abstract_declarator
884 %type <n> declarator
885 %type <n> direct_declarator
886 %type <n> type_declarator
887 %type <n> direct_type_declarator
888 %type <n> pointer
889 %type <n> ctf_assignment_expression_list
890 %type <n> ctf_assignment_expression
891
892 %%
893
894 file:
895 declaration
896 {
897 if (set_parent_node($1, &ctf_scanner_get_ast(scanner)->root))
898 reparent_error(scanner, "error reparenting to root");
899 }
900 | file declaration
901 {
902 if (set_parent_node($2, &ctf_scanner_get_ast(scanner)->root))
903 reparent_error(scanner, "error reparenting to root");
904 }
905 ;
906
907 keywords:
908 VOID
909 { $$ = yylval.gs; }
910 | CHAR
911 { $$ = yylval.gs; }
912 | SHORT
913 { $$ = yylval.gs; }
914 | INT
915 { $$ = yylval.gs; }
916 | LONG
917 { $$ = yylval.gs; }
918 | FLOAT
919 { $$ = yylval.gs; }
920 | DOUBLE
921 { $$ = yylval.gs; }
922 | SIGNED
923 { $$ = yylval.gs; }
924 | UNSIGNED
925 { $$ = yylval.gs; }
926 | _BOOL
927 { $$ = yylval.gs; }
928 | _COMPLEX
929 { $$ = yylval.gs; }
930 | _IMAGINARY
931 { $$ = yylval.gs; }
932 | FLOATING_POINT
933 { $$ = yylval.gs; }
934 | INTEGER
935 { $$ = yylval.gs; }
936 | STRING
937 { $$ = yylval.gs; }
938 | ENUM
939 { $$ = yylval.gs; }
940 | VARIANT
941 { $$ = yylval.gs; }
942 | STRUCT
943 { $$ = yylval.gs; }
944 | CONST
945 { $$ = yylval.gs; }
946 | TYPEDEF
947 { $$ = yylval.gs; }
948 | EVENT
949 { $$ = yylval.gs; }
950 | STREAM
951 { $$ = yylval.gs; }
952 | TRACE
953 { $$ = yylval.gs; }
954 ;
955
956 /* 1.5 Constants */
957
958 c_char_sequence:
959 c_char
960 { $$ = $1; }
961 | c_char_sequence c_char
962 { $$ = gc_string_append(scanner, $1, $2); }
963 ;
964
965 c_char:
966 CHAR_STRING_TOKEN
967 { $$ = yylval.gs; }
968 | ESCSEQ
969 {
970 reparent_error(scanner, "escape sequences not supported yet");
971 }
972 ;
973
974 /* 1.6 String literals */
975
976 s_char_sequence:
977 s_char
978 { $$ = $1; }
979 | s_char_sequence s_char
980 { $$ = gc_string_append(scanner, $1, $2); }
981 ;
982
983 s_char:
984 CHAR_STRING_TOKEN
985 { $$ = yylval.gs; }
986 | ESCSEQ
987 {
988 reparent_error(scanner, "escape sequences not supported yet");
989 }
990 ;
991
992 /* 2: Phrase structure grammar */
993
994 postfix_expression:
995 IDENTIFIER
996 {
997 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
998 $$->u.unary_expression.type = UNARY_STRING;
999 $$->u.unary_expression.u.string = yylval.gs->s;
1000 }
1001 | ID_TYPE
1002 {
1003 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1004 $$->u.unary_expression.type = UNARY_STRING;
1005 $$->u.unary_expression.u.string = yylval.gs->s;
1006 }
1007 | keywords
1008 {
1009 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1010 $$->u.unary_expression.type = UNARY_STRING;
1011 $$->u.unary_expression.u.string = yylval.gs->s;
1012 }
1013
1014 | DECIMAL_CONSTANT
1015 {
1016 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1017 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
1018 sscanf(yylval.gs->s, "%" PRIu64,
1019 &$$->u.unary_expression.u.unsigned_constant);
1020 }
1021 | OCTAL_CONSTANT
1022 {
1023 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1024 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
1025 sscanf(yylval.gs->s, "0%" PRIo64,
1026 &$$->u.unary_expression.u.unsigned_constant);
1027 }
1028 | HEXADECIMAL_CONSTANT
1029 {
1030 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1031 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
1032 sscanf(yylval.gs->s, "0x%" PRIx64,
1033 &$$->u.unary_expression.u.unsigned_constant);
1034 }
1035 | STRING_LITERAL_START DQUOTE
1036 {
1037 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1038 $$->u.unary_expression.type = UNARY_STRING;
1039 $$->u.unary_expression.u.string = "";
1040 }
1041 | STRING_LITERAL_START s_char_sequence DQUOTE
1042 {
1043 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1044 $$->u.unary_expression.type = UNARY_STRING;
1045 $$->u.unary_expression.u.string = $2->s;
1046 }
1047 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
1048 {
1049 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1050 $$->u.unary_expression.type = UNARY_STRING;
1051 $$->u.unary_expression.u.string = $2->s;
1052 }
1053 | LPAREN unary_expression RPAREN
1054 {
1055 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1056 $$->u.unary_expression.type = UNARY_NESTED;
1057 $$->u.unary_expression.u.nested_exp = $2;
1058 }
1059 | postfix_expression LSBRAC unary_expression RSBRAC
1060 {
1061 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1062 $$->u.unary_expression.type = UNARY_SBRAC;
1063 $$->u.unary_expression.u.sbrac_exp = $3;
1064 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1065 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1066 }
1067 | postfix_expression DOT IDENTIFIER
1068 {
1069 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1070 $$->u.unary_expression.type = UNARY_STRING;
1071 $$->u.unary_expression.u.string = yylval.gs->s;
1072 $$->u.unary_expression.link = UNARY_DOTLINK;
1073 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1074 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1075 }
1076 | postfix_expression DOT ID_TYPE
1077 {
1078 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1079 $$->u.unary_expression.type = UNARY_STRING;
1080 $$->u.unary_expression.u.string = yylval.gs->s;
1081 $$->u.unary_expression.link = UNARY_DOTLINK;
1082 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1083 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1084 }
1085 | postfix_expression RARROW IDENTIFIER
1086 {
1087 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1088 $$->u.unary_expression.type = UNARY_STRING;
1089 $$->u.unary_expression.u.string = yylval.gs->s;
1090 $$->u.unary_expression.link = UNARY_ARROWLINK;
1091 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1092 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1093 }
1094 | postfix_expression RARROW ID_TYPE
1095 {
1096 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1097 $$->u.unary_expression.type = UNARY_STRING;
1098 $$->u.unary_expression.u.string = yylval.gs->s;
1099 $$->u.unary_expression.link = UNARY_ARROWLINK;
1100 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1101 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1102 }
1103 ;
1104
1105 unary_expression:
1106 postfix_expression
1107 { $$ = $1; }
1108 | PLUS postfix_expression
1109 { $$ = $2; }
1110 | MINUS postfix_expression
1111 {
1112 $$ = $2;
1113 if ($$->u.unary_expression.type != UNARY_SIGNED_CONSTANT
1114 && $$->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT)
1115 reparent_error(scanner, "expecting numeric constant");
1116
1117 if ($$->u.unary_expression.type == UNARY_UNSIGNED_CONSTANT) {
1118 $$->u.unary_expression.type = UNARY_SIGNED_CONSTANT;
1119 $$->u.unary_expression.u.signed_constant =
1120 -($$->u.unary_expression.u.unsigned_constant);
1121 } else {
1122 $$->u.unary_expression.u.signed_constant =
1123 -($$->u.unary_expression.u.signed_constant);
1124 }
1125 }
1126 ;
1127
1128 unary_expression_or_range:
1129 unary_expression DOTDOTDOT unary_expression
1130 {
1131 $$ = $1;
1132 _cds_list_splice_tail(&($3)->tmp_head, &($$)->tmp_head);
1133 $3->u.unary_expression.link = UNARY_DOTDOTDOT;
1134 }
1135 | unary_expression
1136 { $$ = $1; }
1137 ;
1138
1139 /* 2.2: Declarations */
1140
1141 declaration:
1142 declaration_specifiers SEMICOLON
1143 { $$ = $1; }
1144 | event_declaration
1145 { $$ = $1; }
1146 | stream_declaration
1147 { $$ = $1; }
1148 | trace_declaration
1149 { $$ = $1; }
1150 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
1151 {
1152 struct ctf_node *list;
1153
1154 $$ = make_node(scanner, NODE_TYPEDEF);
1155 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1156 $$->u._typedef.type_specifier_list = list;
1157 _cds_list_splice_tail(&($1)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1158 _cds_list_splice_tail(&($3)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1159 _cds_list_splice_tail(&($4)->tmp_head, &($$)->u._typedef.type_declarators);
1160 }
1161 | TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
1162 {
1163 struct ctf_node *list;
1164
1165 $$ = make_node(scanner, NODE_TYPEDEF);
1166 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1167 $$->u._typedef.type_specifier_list = list;
1168 _cds_list_splice_tail(&($2)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1169 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
1170 }
1171 | declaration_specifiers TYPEDEF type_declarator_list SEMICOLON
1172 {
1173 struct ctf_node *list;
1174
1175 $$ = make_node(scanner, NODE_TYPEDEF);
1176 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1177 $$->u._typedef.type_specifier_list = list;
1178 _cds_list_splice_tail(&($1)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1179 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
1180 }
1181 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON alias_declaration_specifiers alias_abstract_declarator_list SEMICOLON
1182 {
1183 struct ctf_node *list;
1184
1185 $$ = make_node(scanner, NODE_TYPEALIAS);
1186 $$->u.typealias.target = make_node(scanner, NODE_TYPEALIAS_TARGET);
1187 $$->u.typealias.alias = make_node(scanner, NODE_TYPEALIAS_ALIAS);
1188
1189 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1190 $$->u.typealias.target->u.typealias_target.type_specifier_list = list;
1191 _cds_list_splice_tail(&($2)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1192 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.typealias.target->u.typealias_target.type_declarators);
1193
1194 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1195 $$->u.typealias.alias->u.typealias_alias.type_specifier_list = list;
1196 _cds_list_splice_tail(&($5)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1197 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.type_declarators);
1198 }
1199 ;
1200
1201 event_declaration:
1202 event_declaration_begin event_declaration_end
1203 {
1204 $$ = make_node(scanner, NODE_EVENT);
1205 }
1206 | event_declaration_begin ctf_assignment_expression_list event_declaration_end
1207 {
1208 $$ = make_node(scanner, NODE_EVENT);
1209 if (set_parent_node($2, $$))
1210 reparent_error(scanner, "event_declaration");
1211 }
1212 ;
1213
1214 event_declaration_begin:
1215 EVENT LBRAC
1216 { push_scope(scanner); }
1217 ;
1218
1219 event_declaration_end:
1220 RBRAC SEMICOLON
1221 { pop_scope(scanner); }
1222 ;
1223
1224
1225 stream_declaration:
1226 stream_declaration_begin stream_declaration_end
1227 {
1228 $$ = make_node(scanner, NODE_STREAM);
1229 }
1230 | stream_declaration_begin ctf_assignment_expression_list stream_declaration_end
1231 {
1232 $$ = make_node(scanner, NODE_STREAM);
1233 if (set_parent_node($2, $$))
1234 reparent_error(scanner, "stream_declaration");
1235 }
1236 ;
1237
1238 stream_declaration_begin:
1239 STREAM LBRAC
1240 { push_scope(scanner); }
1241 ;
1242
1243 stream_declaration_end:
1244 RBRAC SEMICOLON
1245 { pop_scope(scanner); }
1246 ;
1247
1248
1249 trace_declaration:
1250 trace_declaration_begin trace_declaration_end
1251 {
1252 $$ = make_node(scanner, NODE_TRACE);
1253 }
1254 | trace_declaration_begin ctf_assignment_expression_list trace_declaration_end
1255 {
1256 $$ = make_node(scanner, NODE_TRACE);
1257 if (set_parent_node($2, $$))
1258 reparent_error(scanner, "trace_declaration");
1259 }
1260 ;
1261
1262 trace_declaration_begin:
1263 TRACE LBRAC
1264 { push_scope(scanner); }
1265 ;
1266
1267 trace_declaration_end:
1268 RBRAC SEMICOLON
1269 { pop_scope(scanner); }
1270 ;
1271
1272 declaration_specifiers:
1273 CONST
1274 {
1275 struct ctf_node *node;
1276
1277 $$ = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1278 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1279 node->u.type_specifier.type = TYPESPEC_CONST;
1280 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1281 }
1282 | type_specifier
1283 {
1284 struct ctf_node *node;
1285
1286 $$ = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1287 node = $1;
1288 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1289 }
1290 | declaration_specifiers CONST
1291 {
1292 struct ctf_node *node;
1293
1294 $$ = $1;
1295 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1296 node->u.type_specifier.type = TYPESPEC_CONST;
1297 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1298 }
1299 | declaration_specifiers type_specifier
1300 {
1301 $$ = $1;
1302 cds_list_add_tail(&($2)->siblings, &($$)->u.type_specifier_list.head);
1303 }
1304 ;
1305
1306 type_declarator_list:
1307 type_declarator
1308 { $$ = $1; }
1309 | type_declarator_list COMMA type_declarator
1310 {
1311 $$ = $1;
1312 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
1313 }
1314 ;
1315
1316 type_specifier:
1317 VOID
1318 {
1319 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1320 $$->u.type_specifier.type = TYPESPEC_VOID;
1321 }
1322 | CHAR
1323 {
1324 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1325 $$->u.type_specifier.type = TYPESPEC_CHAR;
1326 }
1327 | SHORT
1328 {
1329 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1330 $$->u.type_specifier.type = TYPESPEC_SHORT;
1331 }
1332 | INT
1333 {
1334 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1335 $$->u.type_specifier.type = TYPESPEC_INT;
1336 }
1337 | LONG
1338 {
1339 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1340 $$->u.type_specifier.type = TYPESPEC_LONG;
1341 }
1342 | FLOAT
1343 {
1344 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1345 $$->u.type_specifier.type = TYPESPEC_FLOAT;
1346 }
1347 | DOUBLE
1348 {
1349 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1350 $$->u.type_specifier.type = TYPESPEC_DOUBLE;
1351 }
1352 | SIGNED
1353 {
1354 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1355 $$->u.type_specifier.type = TYPESPEC_SIGNED;
1356 }
1357 | UNSIGNED
1358 {
1359 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1360 $$->u.type_specifier.type = TYPESPEC_UNSIGNED;
1361 }
1362 | _BOOL
1363 {
1364 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1365 $$->u.type_specifier.type = TYPESPEC_BOOL;
1366 }
1367 | _COMPLEX
1368 {
1369 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1370 $$->u.type_specifier.type = TYPESPEC_COMPLEX;
1371 }
1372 | _IMAGINARY
1373 {
1374 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1375 $$->u.type_specifier.type = TYPESPEC_IMAGINARY;
1376 }
1377 | ID_TYPE
1378 {
1379 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1380 $$->u.type_specifier.type = TYPESPEC_ID_TYPE;
1381 $$->u.type_specifier.id_type = yylval.gs->s;
1382 }
1383 | FLOATING_POINT LBRAC RBRAC
1384 {
1385 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1386 $$->u.type_specifier.type = TYPESPEC_FLOATING_POINT;
1387 $$->u.type_specifier.node = make_node(scanner, NODE_FLOATING_POINT);
1388 }
1389 | FLOATING_POINT LBRAC ctf_assignment_expression_list RBRAC
1390 {
1391 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1392 $$->u.type_specifier.type = TYPESPEC_FLOATING_POINT;
1393 $$->u.type_specifier.node = make_node(scanner, NODE_FLOATING_POINT);
1394 if (set_parent_node($3, $$->u.type_specifier.node))
1395 reparent_error(scanner, "floating point reparent error");
1396 }
1397 | INTEGER LBRAC RBRAC
1398 {
1399 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1400 $$->u.type_specifier.type = TYPESPEC_INTEGER;
1401 $$->u.type_specifier.node = make_node(scanner, NODE_INTEGER);
1402 }
1403 | INTEGER LBRAC ctf_assignment_expression_list RBRAC
1404 {
1405 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1406 $$->u.type_specifier.type = TYPESPEC_INTEGER;
1407 $$->u.type_specifier.node = make_node(scanner, NODE_INTEGER);
1408 if (set_parent_node($3, $$->u.type_specifier.node))
1409 reparent_error(scanner, "integer reparent error");
1410 }
1411 | STRING LBRAC RBRAC
1412 {
1413 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1414 $$->u.type_specifier.type = TYPESPEC_STRING;
1415 $$->u.type_specifier.node = make_node(scanner, NODE_STRING);
1416 }
1417 | STRING LBRAC ctf_assignment_expression_list RBRAC
1418 {
1419 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1420 $$->u.type_specifier.type = TYPESPEC_STRING;
1421 $$->u.type_specifier.node = make_node(scanner, NODE_STRING);
1422 if (set_parent_node($3, $$->u.type_specifier.node))
1423 reparent_error(scanner, "string reparent error");
1424 }
1425 | ENUM enum_type_specifier
1426 {
1427 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1428 $$->u.type_specifier.type = TYPESPEC_ENUM;
1429 $$->u.type_specifier.node = $2;
1430 }
1431 | VARIANT variant_type_specifier
1432 {
1433 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1434 $$->u.type_specifier.type = TYPESPEC_VARIANT;
1435 $$->u.type_specifier.node = $2;
1436 }
1437 | STRUCT struct_type_specifier
1438 {
1439 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1440 $$->u.type_specifier.type = TYPESPEC_STRUCT;
1441 $$->u.type_specifier.node = $2;
1442 }
1443 ;
1444
1445 struct_type_specifier:
1446 struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
1447 {
1448 $$ = make_node(scanner, NODE_STRUCT);
1449 $$->u._struct.has_body = 1;
1450 if (set_parent_node($2, $$))
1451 reparent_error(scanner, "struct reparent error");
1452 }
1453 | IDENTIFIER struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
1454 {
1455 $$ = make_node(scanner, NODE_STRUCT);
1456 $$->u._struct.has_body = 1;
1457 $$->u._struct.name = $1->s;
1458 if (set_parent_node($3, $$))
1459 reparent_error(scanner, "struct reparent error");
1460 }
1461 | ID_TYPE struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
1462 {
1463 $$ = make_node(scanner, NODE_STRUCT);
1464 $$->u._struct.has_body = 1;
1465 $$->u._struct.name = $1->s;
1466 if (set_parent_node($3, $$))
1467 reparent_error(scanner, "struct reparent error");
1468 }
1469 | IDENTIFIER
1470 {
1471 $$ = make_node(scanner, NODE_STRUCT);
1472 $$->u._struct.has_body = 0;
1473 $$->u._struct.name = $1->s;
1474 }
1475 | ID_TYPE
1476 {
1477 $$ = make_node(scanner, NODE_STRUCT);
1478 $$->u._struct.has_body = 0;
1479 $$->u._struct.name = $1->s;
1480 }
1481 ;
1482
1483 struct_declaration_begin:
1484 LBRAC
1485 { push_scope(scanner); }
1486 ;
1487
1488 struct_declaration_end:
1489 RBRAC
1490 { pop_scope(scanner); }
1491 ;
1492
1493 variant_type_specifier:
1494 variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1495 {
1496 $$ = make_node(scanner, NODE_VARIANT);
1497 $$->u.variant.has_body = 1;
1498 if (set_parent_node($2, $$))
1499 reparent_error(scanner, "variant reparent error");
1500 }
1501 | LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1502 {
1503 $$ = make_node(scanner, NODE_VARIANT);
1504 $$->u.variant.has_body = 1;
1505 $$->u.variant.choice = $2->s;
1506 if (set_parent_node($5, $$))
1507 reparent_error(scanner, "variant reparent error");
1508 }
1509 | LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1510 {
1511 $$ = make_node(scanner, NODE_VARIANT);
1512 $$->u.variant.has_body = 1;
1513 $$->u.variant.choice = $2->s;
1514 if (set_parent_node($5, $$))
1515 reparent_error(scanner, "variant reparent error");
1516 }
1517 | IDENTIFIER variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1518 {
1519 $$ = make_node(scanner, NODE_VARIANT);
1520 $$->u.variant.has_body = 1;
1521 $$->u.variant.name = $1->s;
1522 if (set_parent_node($3, $$))
1523 reparent_error(scanner, "variant reparent error");
1524 }
1525 | IDENTIFIER LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1526 {
1527 $$ = make_node(scanner, NODE_VARIANT);
1528 $$->u.variant.has_body = 1;
1529 $$->u.variant.name = $1->s;
1530 $$->u.variant.choice = $3->s;
1531 if (set_parent_node($6, $$))
1532 reparent_error(scanner, "variant reparent error");
1533 }
1534 | IDENTIFIER LT IDENTIFIER GT
1535 {
1536 $$ = make_node(scanner, NODE_VARIANT);
1537 $$->u.variant.has_body = 0;
1538 $$->u.variant.name = $1->s;
1539 $$->u.variant.choice = $3->s;
1540 }
1541 | IDENTIFIER LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1542 {
1543 $$ = make_node(scanner, NODE_VARIANT);
1544 $$->u.variant.has_body = 1;
1545 $$->u.variant.name = $1->s;
1546 $$->u.variant.choice = $3->s;
1547 if (set_parent_node($6, $$))
1548 reparent_error(scanner, "variant reparent error");
1549 }
1550 | IDENTIFIER LT ID_TYPE GT
1551 {
1552 $$ = make_node(scanner, NODE_VARIANT);
1553 $$->u.variant.has_body = 0;
1554 $$->u.variant.name = $1->s;
1555 $$->u.variant.choice = $3->s;
1556 }
1557 | ID_TYPE variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1558 {
1559 $$ = make_node(scanner, NODE_VARIANT);
1560 $$->u.variant.has_body = 1;
1561 $$->u.variant.name = $1->s;
1562 if (set_parent_node($3, $$))
1563 reparent_error(scanner, "variant reparent error");
1564 }
1565 | ID_TYPE LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1566 {
1567 $$ = make_node(scanner, NODE_VARIANT);
1568 $$->u.variant.has_body = 1;
1569 $$->u.variant.name = $1->s;
1570 $$->u.variant.choice = $3->s;
1571 if (set_parent_node($6, $$))
1572 reparent_error(scanner, "variant reparent error");
1573 }
1574 | ID_TYPE LT IDENTIFIER GT
1575 {
1576 $$ = make_node(scanner, NODE_VARIANT);
1577 $$->u.variant.has_body = 0;
1578 $$->u.variant.name = $1->s;
1579 $$->u.variant.choice = $3->s;
1580 }
1581 | ID_TYPE LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1582 {
1583 $$ = make_node(scanner, NODE_VARIANT);
1584 $$->u.variant.has_body = 1;
1585 $$->u.variant.name = $1->s;
1586 $$->u.variant.choice = $3->s;
1587 if (set_parent_node($6, $$))
1588 reparent_error(scanner, "variant reparent error");
1589 }
1590 | ID_TYPE LT ID_TYPE GT
1591 {
1592 $$ = make_node(scanner, NODE_VARIANT);
1593 $$->u.variant.has_body = 0;
1594 $$->u.variant.name = $1->s;
1595 $$->u.variant.choice = $3->s;
1596 }
1597 ;
1598
1599 variant_declaration_begin:
1600 LBRAC
1601 { push_scope(scanner); }
1602 ;
1603
1604 variant_declaration_end:
1605 RBRAC
1606 { pop_scope(scanner); }
1607 ;
1608
1609 declaration_specifiers_or_integer_constant:
1610 declaration_specifiers
1611 { $$ = $1; }
1612 | DECIMAL_CONSTANT
1613 {
1614 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1615 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
1616 sscanf(yylval.gs->s, "%" PRIu64,
1617 &$$->u.unary_expression.u.unsigned_constant);
1618 }
1619 | OCTAL_CONSTANT
1620 {
1621 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1622 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
1623 sscanf(yylval.gs->s, "0%" PRIo64,
1624 &$$->u.unary_expression.u.unsigned_constant);
1625 }
1626 | HEXADECIMAL_CONSTANT
1627 {
1628 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1629 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
1630 sscanf(yylval.gs->s, "0x%" PRIx64,
1631 &$$->u.unary_expression.u.unsigned_constant);
1632 }
1633 ;
1634
1635 enum_type_specifier:
1636 LBRAC enumerator_list RBRAC
1637 {
1638 $$ = make_node(scanner, NODE_ENUM);
1639 $$->u._enum.has_body = 1;
1640 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u._enum.enumerator_list);
1641 }
1642 | LT declaration_specifiers GT LBRAC enumerator_list RBRAC
1643 {
1644 $$ = make_node(scanner, NODE_ENUM);
1645 $$->u._enum.has_body = 1;
1646 ($$)->u._enum.container_type = $2;
1647 _cds_list_splice_tail(&($5)->tmp_head, &($$)->u._enum.enumerator_list);
1648 }
1649 | IDENTIFIER LBRAC enumerator_list RBRAC
1650 {
1651 $$ = make_node(scanner, NODE_ENUM);
1652 $$->u._enum.has_body = 1;
1653 $$->u._enum.enum_id = $1->s;
1654 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._enum.enumerator_list);
1655 }
1656 | IDENTIFIER LT declaration_specifiers GT LBRAC enumerator_list RBRAC
1657 {
1658 $$ = make_node(scanner, NODE_ENUM);
1659 $$->u._enum.has_body = 1;
1660 $$->u._enum.enum_id = $1->s;
1661 ($$)->u._enum.container_type = $3;
1662 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u._enum.enumerator_list);
1663 }
1664 | ID_TYPE LBRAC enumerator_list RBRAC
1665 {
1666 $$ = make_node(scanner, NODE_ENUM);
1667 $$->u._enum.has_body = 1;
1668 $$->u._enum.enum_id = $1->s;
1669 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._enum.enumerator_list);
1670 }
1671 | ID_TYPE LT declaration_specifiers GT LBRAC enumerator_list RBRAC
1672 {
1673 $$ = make_node(scanner, NODE_ENUM);
1674 $$->u._enum.has_body = 1;
1675 $$->u._enum.enum_id = $1->s;
1676 ($$)->u._enum.container_type = $3;
1677 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u._enum.enumerator_list);
1678 }
1679 | LBRAC enumerator_list COMMA RBRAC
1680 {
1681 $$ = make_node(scanner, NODE_ENUM);
1682 $$->u._enum.has_body = 1;
1683 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u._enum.enumerator_list);
1684 }
1685 | LT declaration_specifiers GT LBRAC enumerator_list COMMA RBRAC
1686 {
1687 $$ = make_node(scanner, NODE_ENUM);
1688 $$->u._enum.has_body = 1;
1689 ($$)->u._enum.container_type = $2;
1690 _cds_list_splice_tail(&($5)->tmp_head, &($$)->u._enum.enumerator_list);
1691 }
1692 | IDENTIFIER LBRAC enumerator_list COMMA RBRAC
1693 {
1694 $$ = make_node(scanner, NODE_ENUM);
1695 $$->u._enum.has_body = 1;
1696 $$->u._enum.enum_id = $1->s;
1697 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._enum.enumerator_list);
1698 }
1699 | IDENTIFIER LT declaration_specifiers GT LBRAC enumerator_list COMMA RBRAC
1700 {
1701 $$ = make_node(scanner, NODE_ENUM);
1702 $$->u._enum.has_body = 1;
1703 $$->u._enum.enum_id = $1->s;
1704 ($$)->u._enum.container_type = $3;
1705 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u._enum.enumerator_list);
1706 }
1707 | IDENTIFIER
1708 {
1709 $$ = make_node(scanner, NODE_ENUM);
1710 $$->u._enum.has_body = 0;
1711 $$->u._enum.enum_id = $1->s;
1712 }
1713 | IDENTIFIER LT declaration_specifiers GT
1714 {
1715 $$ = make_node(scanner, NODE_ENUM);
1716 $$->u._enum.has_body = 0;
1717 $$->u._enum.enum_id = $1->s;
1718 ($$)->u._enum.container_type = $3;
1719 }
1720 | ID_TYPE LBRAC enumerator_list COMMA RBRAC
1721 {
1722 $$ = make_node(scanner, NODE_ENUM);
1723 $$->u._enum.has_body = 1;
1724 $$->u._enum.enum_id = $1->s;
1725 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._enum.enumerator_list);
1726 }
1727 | ID_TYPE LT declaration_specifiers GT LBRAC enumerator_list COMMA RBRAC
1728 {
1729 $$ = make_node(scanner, NODE_ENUM);
1730 $$->u._enum.has_body = 1;
1731 $$->u._enum.enum_id = $1->s;
1732 ($$)->u._enum.container_type = $3;
1733 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u._enum.enumerator_list);
1734 }
1735 | ID_TYPE
1736 {
1737 $$ = make_node(scanner, NODE_ENUM);
1738 $$->u._enum.has_body = 0;
1739 $$->u._enum.enum_id = $1->s;
1740 }
1741 | ID_TYPE LT declaration_specifiers GT
1742 {
1743 $$ = make_node(scanner, NODE_ENUM);
1744 $$->u._enum.has_body = 0;
1745 $$->u._enum.enum_id = $1->s;
1746 ($$)->u._enum.container_type = $3;
1747 }
1748 ;
1749
1750 struct_or_variant_declaration_list:
1751 /* empty */
1752 { $$ = NULL; }
1753 | struct_or_variant_declaration_list struct_or_variant_declaration
1754 {
1755 if ($1) {
1756 $$ = $1;
1757 cds_list_add_tail(&($2)->siblings, &($$)->tmp_head);
1758 } else {
1759 $$ = $2;
1760 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1761 }
1762 }
1763 ;
1764
1765 struct_or_variant_declaration:
1766 declaration_specifiers struct_or_variant_declarator_list SEMICOLON
1767 {
1768 struct ctf_node *list;
1769
1770 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1771 _cds_list_splice_tail(&($1)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1772 $$ = make_node(scanner, NODE_STRUCT_OR_VARIANT_DECLARATION);
1773 ($$)->u.struct_or_variant_declaration.type_specifier_list = list;
1774 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u.struct_or_variant_declaration.type_declarators);
1775 }
1776 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
1777 {
1778 struct ctf_node *list;
1779
1780 $$ = make_node(scanner, NODE_TYPEDEF);
1781 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1782 $$->u._typedef.type_specifier_list = list;
1783 _cds_list_splice_tail(&($1)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1784 _cds_list_splice_tail(&($3)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1785 _cds_list_splice_tail(&($4)->tmp_head, &($$)->u._typedef.type_declarators);
1786 }
1787 | TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
1788 {
1789 struct ctf_node *list;
1790
1791 $$ = make_node(scanner, NODE_TYPEDEF);
1792 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1793 $$->u._typedef.type_specifier_list = list;
1794 _cds_list_splice_tail(&($2)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1795 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
1796 }
1797 | declaration_specifiers TYPEDEF type_declarator_list SEMICOLON
1798 {
1799 struct ctf_node *list;
1800
1801 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1802 _cds_list_splice_tail(&($1)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1803 $$ = make_node(scanner, NODE_TYPEDEF);
1804 ($$)->u.struct_or_variant_declaration.type_specifier_list = list;
1805 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
1806 }
1807 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON alias_declaration_specifiers alias_abstract_declarator_list SEMICOLON
1808 {
1809 struct ctf_node *list;
1810
1811 $$ = make_node(scanner, NODE_TYPEALIAS);
1812 $$->u.typealias.target = make_node(scanner, NODE_TYPEALIAS_TARGET);
1813 $$->u.typealias.alias = make_node(scanner, NODE_TYPEALIAS_ALIAS);
1814
1815 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1816 $$->u.typealias.target->u.typealias_target.type_specifier_list = list;
1817 _cds_list_splice_tail(&($2)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1818 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.typealias.target->u.typealias_target.type_declarators);
1819
1820 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1821 $$->u.typealias.alias->u.typealias_alias.type_specifier_list = list;
1822 _cds_list_splice_tail(&($5)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
1823 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.type_declarators);
1824 }
1825 ;
1826
1827 alias_declaration_specifiers:
1828 CONST
1829 {
1830 struct ctf_node *node;
1831
1832 $$ = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1833 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1834 node->u.type_specifier.type = TYPESPEC_CONST;
1835 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1836 }
1837 | type_specifier
1838 {
1839 struct ctf_node *node;
1840
1841 $$ = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1842 node = $1;
1843 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1844 }
1845 | IDENTIFIER
1846 {
1847 struct ctf_node *node;
1848
1849 add_type(scanner, $1);
1850 $$ = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
1851 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1852 node->u.type_specifier.type = TYPESPEC_ID_TYPE;
1853 node->u.type_specifier.id_type = yylval.gs->s;
1854 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1855 }
1856 | alias_declaration_specifiers CONST
1857 {
1858 struct ctf_node *node;
1859
1860 $$ = $1;
1861 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1862 node->u.type_specifier.type = TYPESPEC_CONST;
1863 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1864 }
1865 | alias_declaration_specifiers type_specifier
1866 {
1867 $$ = $1;
1868 cds_list_add_tail(&($2)->siblings, &($$)->u.type_specifier_list.head);
1869 }
1870 | alias_declaration_specifiers IDENTIFIER
1871 {
1872 struct ctf_node *node;
1873
1874 add_type(scanner, $2);
1875 $$ = $1;
1876 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1877 node->u.type_specifier.type = TYPESPEC_ID_TYPE;
1878 node->u.type_specifier.id_type = yylval.gs->s;
1879 cds_list_add_tail(&node->siblings, &($$)->u.type_specifier_list.head);
1880 }
1881 ;
1882
1883 struct_or_variant_declarator_list:
1884 struct_or_variant_declarator
1885 { $$ = $1; }
1886 | struct_or_variant_declarator_list COMMA struct_or_variant_declarator
1887 {
1888 $$ = $1;
1889 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
1890 }
1891 ;
1892
1893 struct_or_variant_declarator:
1894 declarator
1895 { $$ = $1; }
1896 | COLON unary_expression
1897 { $$ = $2; }
1898 | declarator COLON unary_expression
1899 {
1900 $$ = $1;
1901 if (set_parent_node($3, $1))
1902 reparent_error(scanner, "struct_or_variant_declarator");
1903 }
1904 ;
1905
1906 enumerator_list:
1907 enumerator
1908 { $$ = $1; }
1909 | enumerator_list COMMA enumerator
1910 {
1911 $$ = $1;
1912 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
1913 }
1914 ;
1915
1916 enumerator:
1917 IDENTIFIER
1918 {
1919 $$ = make_node(scanner, NODE_ENUMERATOR);
1920 $$->u.enumerator.id = $1->s;
1921 }
1922 | ID_TYPE
1923 {
1924 $$ = make_node(scanner, NODE_ENUMERATOR);
1925 $$->u.enumerator.id = $1->s;
1926 }
1927 | keywords
1928 {
1929 $$ = make_node(scanner, NODE_ENUMERATOR);
1930 $$->u.enumerator.id = $1->s;
1931 }
1932 | STRING_LITERAL_START DQUOTE
1933 {
1934 $$ = make_node(scanner, NODE_ENUMERATOR);
1935 $$->u.enumerator.id = "";
1936 }
1937 | STRING_LITERAL_START s_char_sequence DQUOTE
1938 {
1939 $$ = make_node(scanner, NODE_ENUMERATOR);
1940 $$->u.enumerator.id = $2->s;
1941 }
1942 | IDENTIFIER EQUAL unary_expression_or_range
1943 {
1944 $$ = make_node(scanner, NODE_ENUMERATOR);
1945 $$->u.enumerator.id = $1->s;
1946 cds_list_splice(&($3)->tmp_head, &($$)->u.enumerator.values);
1947 }
1948 | ID_TYPE EQUAL unary_expression_or_range
1949 {
1950 $$ = make_node(scanner, NODE_ENUMERATOR);
1951 $$->u.enumerator.id = $1->s;
1952 cds_list_splice(&($3)->tmp_head, &($$)->u.enumerator.values);
1953 }
1954 | keywords EQUAL unary_expression_or_range
1955 {
1956 $$ = make_node(scanner, NODE_ENUMERATOR);
1957 $$->u.enumerator.id = $1->s;
1958 cds_list_splice(&($3)->tmp_head, &($$)->u.enumerator.values);
1959 }
1960 | STRING_LITERAL_START DQUOTE EQUAL unary_expression_or_range
1961 {
1962 $$ = make_node(scanner, NODE_ENUMERATOR);
1963 $$->u.enumerator.id = "";
1964 cds_list_splice(&($4)->tmp_head, &($$)->u.enumerator.values);
1965 }
1966 | STRING_LITERAL_START s_char_sequence DQUOTE EQUAL unary_expression_or_range
1967 {
1968 $$ = make_node(scanner, NODE_ENUMERATOR);
1969 $$->u.enumerator.id = $2->s;
1970 cds_list_splice(&($5)->tmp_head, &($$)->u.enumerator.values);
1971 }
1972 ;
1973
1974 abstract_declarator_list:
1975 abstract_declarator
1976 { $$ = $1; }
1977 | abstract_declarator_list COMMA abstract_declarator
1978 {
1979 $$ = $1;
1980 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
1981 }
1982 ;
1983
1984 abstract_declarator:
1985 direct_abstract_declarator
1986 { $$ = $1; }
1987 | pointer direct_abstract_declarator
1988 {
1989 $$ = $2;
1990 cds_list_splice(&($1)->tmp_head, &($$)->u.type_declarator.pointers);
1991 }
1992 ;
1993
1994 direct_abstract_declarator:
1995 /* empty */
1996 {
1997 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1998 $$->u.type_declarator.type = TYPEDEC_ID;
1999 /* id is NULL */
2000 }
2001 | IDENTIFIER
2002 {
2003 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2004 $$->u.type_declarator.type = TYPEDEC_ID;
2005 $$->u.type_declarator.u.id = $1->s;
2006 }
2007 | LPAREN abstract_declarator RPAREN
2008 {
2009 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2010 $$->u.type_declarator.type = TYPEDEC_NESTED;
2011 $$->u.type_declarator.u.nested.type_declarator = $2;
2012 }
2013 | direct_abstract_declarator LSBRAC declaration_specifiers_or_integer_constant RSBRAC
2014 {
2015 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2016 $$->u.type_declarator.type = TYPEDEC_NESTED;
2017 $$->u.type_declarator.u.nested.type_declarator = $1;
2018 ($$)->u.type_declarator.u.nested.length = $3;
2019 }
2020 | direct_abstract_declarator LSBRAC RSBRAC
2021 {
2022 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2023 $$->u.type_declarator.type = TYPEDEC_NESTED;
2024 $$->u.type_declarator.u.nested.type_declarator = $1;
2025 $$->u.type_declarator.u.nested.abstract_array = 1;
2026 }
2027 ;
2028
2029 alias_abstract_declarator_list:
2030 alias_abstract_declarator
2031 { $$ = $1; }
2032 | alias_abstract_declarator_list COMMA alias_abstract_declarator
2033 {
2034 $$ = $1;
2035 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
2036 }
2037 ;
2038
2039 alias_abstract_declarator:
2040 direct_alias_abstract_declarator
2041 { $$ = $1; }
2042 | pointer direct_alias_abstract_declarator
2043 {
2044 $$ = $2;
2045 cds_list_splice(&($1)->tmp_head, &($$)->u.type_declarator.pointers);
2046 }
2047 ;
2048
2049 direct_alias_abstract_declarator:
2050 /* empty */
2051 {
2052 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2053 $$->u.type_declarator.type = TYPEDEC_ID;
2054 /* id is NULL */
2055 }
2056 | LPAREN alias_abstract_declarator RPAREN
2057 {
2058 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2059 $$->u.type_declarator.type = TYPEDEC_NESTED;
2060 $$->u.type_declarator.u.nested.type_declarator = $2;
2061 }
2062 | direct_alias_abstract_declarator LSBRAC declaration_specifiers_or_integer_constant RSBRAC
2063 {
2064 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2065 $$->u.type_declarator.type = TYPEDEC_NESTED;
2066 $$->u.type_declarator.u.nested.type_declarator = $1;
2067 ($$)->u.type_declarator.u.nested.length = $3;
2068 }
2069 | direct_alias_abstract_declarator LSBRAC RSBRAC
2070 {
2071 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2072 $$->u.type_declarator.type = TYPEDEC_NESTED;
2073 $$->u.type_declarator.u.nested.type_declarator = $1;
2074 $$->u.type_declarator.u.nested.abstract_array = 1;
2075 }
2076 ;
2077
2078 declarator:
2079 direct_declarator
2080 { $$ = $1; }
2081 | pointer direct_declarator
2082 {
2083 $$ = $2;
2084 cds_list_splice(&($1)->tmp_head, &($$)->u.type_declarator.pointers);
2085 }
2086 ;
2087
2088 direct_declarator:
2089 IDENTIFIER
2090 {
2091 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2092 $$->u.type_declarator.type = TYPEDEC_ID;
2093 $$->u.type_declarator.u.id = $1->s;
2094 }
2095 | LPAREN declarator RPAREN
2096 {
2097 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2098 $$->u.type_declarator.type = TYPEDEC_NESTED;
2099 $$->u.type_declarator.u.nested.type_declarator = $2;
2100 }
2101 | direct_declarator LSBRAC declaration_specifiers_or_integer_constant RSBRAC
2102 {
2103 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2104 $$->u.type_declarator.type = TYPEDEC_NESTED;
2105 $$->u.type_declarator.u.nested.type_declarator = $1;
2106 ($$)->u.type_declarator.u.nested.length = $3;
2107 }
2108 ;
2109
2110 type_declarator:
2111 direct_type_declarator
2112 { $$ = $1; }
2113 | pointer direct_type_declarator
2114 {
2115 $$ = $2;
2116 cds_list_splice(&($1)->tmp_head, &($$)->u.type_declarator.pointers);
2117 }
2118 ;
2119
2120 direct_type_declarator:
2121 IDENTIFIER
2122 {
2123 add_type(scanner, $1);
2124 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2125 $$->u.type_declarator.type = TYPEDEC_ID;
2126 $$->u.type_declarator.u.id = $1->s;
2127 }
2128 | LPAREN type_declarator RPAREN
2129 {
2130 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2131 $$->u.type_declarator.type = TYPEDEC_NESTED;
2132 $$->u.type_declarator.u.nested.type_declarator = $2;
2133 }
2134 | direct_type_declarator LSBRAC declaration_specifiers_or_integer_constant RSBRAC
2135 {
2136 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
2137 $$->u.type_declarator.type = TYPEDEC_NESTED;
2138 $$->u.type_declarator.u.nested.type_declarator = $1;
2139 ($$)->u.type_declarator.u.nested.length = $3;
2140 }
2141 ;
2142
2143 pointer:
2144 STAR
2145 {
2146 $$ = make_node(scanner, NODE_POINTER);
2147 }
2148 | STAR pointer
2149 {
2150 $$ = make_node(scanner, NODE_POINTER);
2151 cds_list_splice(&($2)->tmp_head, &($$)->tmp_head);
2152 }
2153 | STAR type_qualifier_list pointer
2154 {
2155 $$ = make_node(scanner, NODE_POINTER);
2156 $$->u.pointer.const_qualifier = 1;
2157 cds_list_splice(&($3)->tmp_head, &($$)->tmp_head);
2158 }
2159 ;
2160
2161 type_qualifier_list:
2162 /* pointer assumes only const type qualifier */
2163 CONST
2164 | type_qualifier_list CONST
2165 ;
2166
2167 /* 2.3: CTF-specific declarations */
2168
2169 ctf_assignment_expression_list:
2170 ctf_assignment_expression SEMICOLON
2171 { $$ = $1; }
2172 | ctf_assignment_expression_list ctf_assignment_expression SEMICOLON
2173 {
2174 $$ = $1;
2175 cds_list_add_tail(&($2)->siblings, &($$)->tmp_head);
2176 }
2177 ;
2178
2179 ctf_assignment_expression:
2180 unary_expression EQUAL unary_expression
2181 {
2182 /*
2183 * Because we have left and right, cannot use
2184 * set_parent_node.
2185 */
2186 $$ = make_node(scanner, NODE_CTF_EXPRESSION);
2187 _cds_list_splice_tail(&($1)->tmp_head, &($$)->u.ctf_expression.left);
2188 if ($1->u.unary_expression.type != UNARY_STRING)
2189 reparent_error(scanner, "ctf_assignment_expression left expects string");
2190 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.ctf_expression.right);
2191 }
2192 | unary_expression TYPEASSIGN declaration_specifiers /* Only allow struct */
2193 {
2194 /*
2195 * Because we have left and right, cannot use
2196 * set_parent_node.
2197 */
2198 $$ = make_node(scanner, NODE_CTF_EXPRESSION);
2199 _cds_list_splice_tail(&($1)->tmp_head, &($$)->u.ctf_expression.left);
2200 if ($1->u.unary_expression.type != UNARY_STRING)
2201 reparent_error(scanner, "ctf_assignment_expression left expects string");
2202 cds_list_add_tail(&($3)->siblings, &($$)->u.ctf_expression.right);
2203 }
2204 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list
2205 {
2206 struct ctf_node *list;
2207
2208 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
2209 _cds_list_splice_tail(&($1)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
2210 _cds_list_splice_tail(&($3)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
2211 $$ = make_node(scanner, NODE_TYPEDEF);
2212 ($$)->u.struct_or_variant_declaration.type_specifier_list = list;
2213 _cds_list_splice_tail(&($4)->tmp_head, &($$)->u._typedef.type_declarators);
2214 }
2215 | TYPEDEF declaration_specifiers type_declarator_list
2216 {
2217 struct ctf_node *list;
2218
2219 $$ = make_node(scanner, NODE_TYPEDEF);
2220 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
2221 $$->u._typedef.type_specifier_list = list;
2222 _cds_list_splice_tail(&($2)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
2223 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
2224 }
2225 | declaration_specifiers TYPEDEF type_declarator_list
2226 {
2227 struct ctf_node *list;
2228
2229 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
2230 _cds_list_splice_tail(&($1)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
2231 $$ = make_node(scanner, NODE_TYPEDEF);
2232 ($$)->u.struct_or_variant_declaration.type_specifier_list = list;
2233 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
2234 }
2235 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON alias_declaration_specifiers alias_abstract_declarator_list
2236 {
2237 struct ctf_node *list;
2238
2239 $$ = make_node(scanner, NODE_TYPEALIAS);
2240 $$->u.typealias.target = make_node(scanner, NODE_TYPEALIAS_TARGET);
2241 $$->u.typealias.alias = make_node(scanner, NODE_TYPEALIAS_ALIAS);
2242
2243 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
2244 $$->u.typealias.target->u.typealias_target.type_specifier_list = list;
2245 _cds_list_splice_tail(&($2)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
2246 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.typealias.target->u.typealias_target.type_declarators);
2247
2248 list = make_node(scanner, NODE_TYPE_SPECIFIER_LIST);
2249 $$->u.typealias.alias->u.typealias_alias.type_specifier_list = list;
2250 _cds_list_splice_tail(&($5)->u.type_specifier_list.head, &list->u.type_specifier_list.head);
2251 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.type_declarators);
2252 }
2253 ;
This page took 0.103916 seconds and 5 git commands to generate.