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