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