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