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