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