2ded0aeff0c9945368dbec4a9c307a7627519fb2
[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 | _IMAGINARY
881 { $$ = yylval.gs; }
882 | FLOATING_POINT
883 { $$ = yylval.gs; }
884 | INTEGER
885 { $$ = yylval.gs; }
886 | STRING
887 { $$ = yylval.gs; }
888 | ENUM
889 { $$ = yylval.gs; }
890 | VARIANT
891 { $$ = yylval.gs; }
892 | STRUCT
893 { $$ = yylval.gs; }
894 | CONST
895 { $$ = yylval.gs; }
896 | TYPEDEF
897 { $$ = yylval.gs; }
898 | EVENT
899 { $$ = yylval.gs; }
900 | STREAM
901 { $$ = yylval.gs; }
902 | TRACE
903 { $$ = yylval.gs; }
904 ;
905
906 /* 1.5 Constants */
907
908 c_char_sequence:
909 c_char
910 { $$ = $1; }
911 | c_char_sequence c_char
912 { $$ = gc_string_append(scanner, $1, $2); }
913 ;
914
915 c_char:
916 CHAR_STRING_TOKEN
917 { $$ = yylval.gs; }
918 | ESCSEQ
919 {
920 reparent_error(scanner, "escape sequences not supported yet");
921 }
922 ;
923
924 /* 1.6 String literals */
925
926 s_char_sequence:
927 s_char
928 { $$ = $1; }
929 | s_char_sequence s_char
930 { $$ = gc_string_append(scanner, $1, $2); }
931 ;
932
933 s_char:
934 CHAR_STRING_TOKEN
935 { $$ = yylval.gs; }
936 | ESCSEQ
937 {
938 reparent_error(scanner, "escape sequences not supported yet");
939 }
940 ;
941
942 /* 2: Phrase structure grammar */
943
944 postfix_expression:
945 IDENTIFIER
946 {
947 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
948 $$->u.unary_expression.type = UNARY_STRING;
949 $$->u.unary_expression.u.string = yylval.gs->s;
950 }
951 | ID_TYPE
952 {
953 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
954 $$->u.unary_expression.type = UNARY_STRING;
955 $$->u.unary_expression.u.string = yylval.gs->s;
956 }
957 | keywords
958 {
959 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
960 $$->u.unary_expression.type = UNARY_STRING;
961 $$->u.unary_expression.u.string = yylval.gs->s;
962 }
963
964 | DECIMAL_CONSTANT
965 {
966 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
967 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
968 sscanf(yylval.gs->s, "%" PRIu64,
969 &$$->u.unary_expression.u.unsigned_constant);
970 }
971 | OCTAL_CONSTANT
972 {
973 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
974 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
975 sscanf(yylval.gs->s, "0%" PRIo64,
976 &$$->u.unary_expression.u.unsigned_constant);
977 }
978 | HEXADECIMAL_CONSTANT
979 {
980 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
981 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
982 sscanf(yylval.gs->s, "0x%" PRIx64,
983 &$$->u.unary_expression.u.unsigned_constant);
984 }
985 | STRING_LITERAL_START DQUOTE
986 {
987 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
988 $$->u.unary_expression.type = UNARY_STRING;
989 $$->u.unary_expression.u.string = "";
990 }
991 | STRING_LITERAL_START s_char_sequence DQUOTE
992 {
993 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
994 $$->u.unary_expression.type = UNARY_STRING;
995 $$->u.unary_expression.u.string = $2->s;
996 }
997 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
998 {
999 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1000 $$->u.unary_expression.type = UNARY_STRING;
1001 $$->u.unary_expression.u.string = $2->s;
1002 }
1003 | LPAREN unary_expression RPAREN
1004 {
1005 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1006 $$->u.unary_expression.type = UNARY_NESTED;
1007 $$->u.unary_expression.u.nested_exp = $2;
1008 }
1009 | postfix_expression LSBRAC unary_expression RSBRAC
1010 {
1011 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1012 $$->u.unary_expression.type = UNARY_SBRAC;
1013 $$->u.unary_expression.u.sbrac_exp = $3;
1014 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1015 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1016 }
1017 | postfix_expression DOT IDENTIFIER
1018 {
1019 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1020 $$->u.unary_expression.type = UNARY_STRING;
1021 $$->u.unary_expression.u.string = yylval.gs->s;
1022 $$->u.unary_expression.link = UNARY_DOTLINK;
1023 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1024 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1025 }
1026 | postfix_expression DOT ID_TYPE
1027 {
1028 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1029 $$->u.unary_expression.type = UNARY_STRING;
1030 $$->u.unary_expression.u.string = yylval.gs->s;
1031 $$->u.unary_expression.link = UNARY_DOTLINK;
1032 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1033 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1034 }
1035 | postfix_expression RARROW IDENTIFIER
1036 {
1037 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1038 $$->u.unary_expression.type = UNARY_STRING;
1039 $$->u.unary_expression.u.string = yylval.gs->s;
1040 $$->u.unary_expression.link = UNARY_ARROWLINK;
1041 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1042 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1043 }
1044 | postfix_expression RARROW ID_TYPE
1045 {
1046 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1047 $$->u.unary_expression.type = UNARY_STRING;
1048 $$->u.unary_expression.u.string = yylval.gs->s;
1049 $$->u.unary_expression.link = UNARY_ARROWLINK;
1050 cds_list_splice(&($1)->tmp_head, &($$)->tmp_head);
1051 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1052 }
1053 ;
1054
1055 unary_expression:
1056 postfix_expression
1057 { $$ = $1; }
1058 | PLUS postfix_expression
1059 { $$ = $2; }
1060 | MINUS postfix_expression
1061 {
1062 $$ = $2;
1063 if ($$->u.unary_expression.type != UNARY_SIGNED_CONSTANT
1064 && $$->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT)
1065 reparent_error(scanner, "expecting numeric constant");
1066
1067 if ($$->u.unary_expression.type == UNARY_UNSIGNED_CONSTANT) {
1068 $$->u.unary_expression.type = UNARY_SIGNED_CONSTANT;
1069 $$->u.unary_expression.u.signed_constant =
1070 -($$->u.unary_expression.u.unsigned_constant);
1071 } else {
1072 $$->u.unary_expression.u.signed_constant =
1073 -($$->u.unary_expression.u.signed_constant);
1074 }
1075 }
1076 ;
1077
1078 unary_expression_or_range:
1079 unary_expression DOTDOTDOT unary_expression
1080 {
1081 $$ = $1;
1082 _cds_list_splice_tail(&($3)->tmp_head, &($$)->tmp_head);
1083 $3->u.unary_expression.link = UNARY_DOTDOTDOT;
1084 }
1085 | unary_expression
1086 { $$ = $1; }
1087 ;
1088
1089 /* 2.2: Declarations */
1090
1091 declaration:
1092 declaration_specifiers SEMICOLON
1093 { $$ = $1; }
1094 | event_declaration
1095 { $$ = $1; }
1096 | stream_declaration
1097 { $$ = $1; }
1098 | trace_declaration
1099 { $$ = $1; }
1100 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
1101 {
1102 $$ = make_node(scanner, NODE_TYPEDEF);
1103 _cds_list_splice_tail(&($1)->tmp_head, &($$)->u._typedef.declaration_specifier);
1104 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.declaration_specifier);
1105 _cds_list_splice_tail(&($4)->tmp_head, &($$)->u._typedef.type_declarators);
1106 }
1107 | TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
1108 {
1109 $$ = make_node(scanner, NODE_TYPEDEF);
1110 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u._typedef.declaration_specifier);
1111 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
1112 }
1113 | declaration_specifiers TYPEDEF type_declarator_list SEMICOLON
1114 {
1115 $$ = make_node(scanner, NODE_TYPEDEF);
1116 _cds_list_splice_tail(&($1)->tmp_head, &($$)->u._typedef.declaration_specifier);
1117 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
1118 }
1119 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON declaration_specifiers abstract_type_declarator_list SEMICOLON
1120 {
1121 $$ = make_node(scanner, NODE_TYPEALIAS);
1122 $$->u.typealias.target = make_node(scanner, NODE_TYPEALIAS_TARGET);
1123 $$->u.typealias.alias = make_node(scanner, NODE_TYPEALIAS_ALIAS);
1124 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u.typealias.target->u.typealias_target.declaration_specifier);
1125 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.typealias.target->u.typealias_target.type_declarators);
1126 _cds_list_splice_tail(&($5)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.declaration_specifier);
1127 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.type_declarators);
1128 }
1129 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON type_declarator_list SEMICOLON
1130 {
1131 $$ = make_node(scanner, NODE_TYPEALIAS);
1132 $$->u.typealias.target = make_node(scanner, NODE_TYPEALIAS_TARGET);
1133 $$->u.typealias.alias = make_node(scanner, NODE_TYPEALIAS_ALIAS);
1134 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u.typealias.target->u.typealias_target.declaration_specifier);
1135 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.declaration_specifier);
1136 _cds_list_splice_tail(&($5)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.type_declarators);
1137 }
1138 ;
1139
1140 event_declaration:
1141 event_declaration_begin event_declaration_end
1142 {
1143 $$ = make_node(scanner, NODE_EVENT);
1144 }
1145 | event_declaration_begin ctf_assignment_expression_list event_declaration_end
1146 {
1147 $$ = make_node(scanner, NODE_EVENT);
1148 if (set_parent_node($2, $$))
1149 reparent_error(scanner, "event_declaration");
1150 }
1151 ;
1152
1153 event_declaration_begin:
1154 EVENT LBRAC
1155 { push_scope(scanner); }
1156 ;
1157
1158 event_declaration_end:
1159 RBRAC SEMICOLON
1160 { pop_scope(scanner); }
1161 ;
1162
1163
1164 stream_declaration:
1165 stream_declaration_begin stream_declaration_end
1166 {
1167 $$ = make_node(scanner, NODE_STREAM);
1168 }
1169 | stream_declaration_begin ctf_assignment_expression_list stream_declaration_end
1170 {
1171 $$ = make_node(scanner, NODE_STREAM);
1172 if (set_parent_node($2, $$))
1173 reparent_error(scanner, "stream_declaration");
1174 }
1175 ;
1176
1177 stream_declaration_begin:
1178 STREAM LBRAC
1179 { push_scope(scanner); }
1180 ;
1181
1182 stream_declaration_end:
1183 RBRAC SEMICOLON
1184 { pop_scope(scanner); }
1185 ;
1186
1187
1188 trace_declaration:
1189 trace_declaration_begin trace_declaration_end
1190 {
1191 $$ = make_node(scanner, NODE_TRACE);
1192 }
1193 | trace_declaration_begin ctf_assignment_expression_list trace_declaration_end
1194 {
1195 $$ = make_node(scanner, NODE_TRACE);
1196 if (set_parent_node($2, $$))
1197 reparent_error(scanner, "trace_declaration");
1198 }
1199 ;
1200
1201 trace_declaration_begin:
1202 TRACE LBRAC
1203 { push_scope(scanner); }
1204 ;
1205
1206 trace_declaration_end:
1207 RBRAC SEMICOLON
1208 { pop_scope(scanner); }
1209 ;
1210
1211 declaration_specifiers:
1212 CONST
1213 {
1214 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1215 $$->u.type_specifier.type = TYPESPEC_CONST;
1216 }
1217 | type_specifier
1218 { $$ = $1; }
1219 | declaration_specifiers CONST
1220 {
1221 struct ctf_node *node;
1222
1223 $$ = $1;
1224 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1225 node->u.type_specifier.type = TYPESPEC_CONST;
1226 cds_list_add_tail(&node->siblings, &($$)->tmp_head);
1227 }
1228 | declaration_specifiers type_specifier
1229 {
1230 $$ = $1;
1231 cds_list_add_tail(&($2)->siblings, &($$)->tmp_head);
1232 }
1233 ;
1234
1235 type_declarator_list:
1236 type_declarator
1237 { $$ = $1; }
1238 | type_declarator_list COMMA type_declarator
1239 {
1240 $$ = $1;
1241 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
1242 }
1243 ;
1244
1245 abstract_type_declarator_list:
1246 abstract_type_declarator
1247 { $$ = $1; }
1248 | abstract_type_declarator_list COMMA abstract_type_declarator
1249 {
1250 $$ = $1;
1251 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
1252 }
1253 ;
1254
1255 type_specifier:
1256 VOID
1257 {
1258 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1259 $$->u.type_specifier.type = TYPESPEC_VOID;
1260 }
1261 | CHAR
1262 {
1263 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1264 $$->u.type_specifier.type = TYPESPEC_CHAR;
1265 }
1266 | SHORT
1267 {
1268 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1269 $$->u.type_specifier.type = TYPESPEC_SHORT;
1270 }
1271 | INT
1272 {
1273 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1274 $$->u.type_specifier.type = TYPESPEC_INT;
1275 }
1276 | LONG
1277 {
1278 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1279 $$->u.type_specifier.type = TYPESPEC_LONG;
1280 }
1281 | FLOAT
1282 {
1283 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1284 $$->u.type_specifier.type = TYPESPEC_FLOAT;
1285 }
1286 | DOUBLE
1287 {
1288 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1289 $$->u.type_specifier.type = TYPESPEC_DOUBLE;
1290 }
1291 | SIGNED
1292 {
1293 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1294 $$->u.type_specifier.type = TYPESPEC_SIGNED;
1295 }
1296 | UNSIGNED
1297 {
1298 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1299 $$->u.type_specifier.type = TYPESPEC_UNSIGNED;
1300 }
1301 | _BOOL
1302 {
1303 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1304 $$->u.type_specifier.type = TYPESPEC_BOOL;
1305 }
1306 | _COMPLEX
1307 {
1308 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1309 $$->u.type_specifier.type = TYPESPEC_COMPLEX;
1310 }
1311 | _IMAGINARY
1312 {
1313 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1314 $$->u.type_specifier.type = TYPESPEC_IMAGINARY;
1315 }
1316 | ID_TYPE
1317 {
1318 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1319 $$->u.type_specifier.type = TYPESPEC_ID_TYPE;
1320 $$->u.type_specifier.id_type = yylval.gs->s;
1321 }
1322 | FLOATING_POINT LBRAC RBRAC
1323 {
1324 $$ = make_node(scanner, NODE_FLOATING_POINT);
1325 }
1326 | FLOATING_POINT LBRAC ctf_assignment_expression_list RBRAC
1327 {
1328 $$ = make_node(scanner, NODE_FLOATING_POINT);
1329 if (set_parent_node($3, $$))
1330 reparent_error(scanner, "floating point reparent error");
1331 }
1332 | INTEGER LBRAC RBRAC
1333 {
1334 $$ = make_node(scanner, NODE_INTEGER);
1335 }
1336 | INTEGER LBRAC ctf_assignment_expression_list RBRAC
1337 {
1338 $$ = make_node(scanner, NODE_INTEGER);
1339 if (set_parent_node($3, $$))
1340 reparent_error(scanner, "integer reparent error");
1341 }
1342 | STRING LBRAC RBRAC
1343 {
1344 $$ = make_node(scanner, NODE_STRING);
1345 }
1346 | STRING LBRAC ctf_assignment_expression_list RBRAC
1347 {
1348 $$ = make_node(scanner, NODE_STRING);
1349 if (set_parent_node($3, $$))
1350 reparent_error(scanner, "string reparent error");
1351 }
1352 | ENUM enum_type_specifier
1353 { $$ = $2; }
1354 | VARIANT variant_type_specifier
1355 { $$ = $2; }
1356 | STRUCT struct_type_specifier
1357 { $$ = $2; }
1358 ;
1359
1360 struct_type_specifier:
1361 struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
1362 {
1363 $$ = make_node(scanner, NODE_STRUCT);
1364 if (set_parent_node($2, $$))
1365 reparent_error(scanner, "struct reparent error");
1366 }
1367 | IDENTIFIER 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 | ID_TYPE struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
1375 {
1376 $$ = make_node(scanner, NODE_STRUCT);
1377 $$->u._struct.name = $1->s;
1378 if (set_parent_node($3, $$))
1379 reparent_error(scanner, "struct reparent error");
1380 }
1381 | IDENTIFIER
1382 {
1383 $$ = make_node(scanner, NODE_STRUCT);
1384 $$->u._struct.name = $1->s;
1385 }
1386 | ID_TYPE
1387 {
1388 $$ = make_node(scanner, NODE_STRUCT);
1389 $$->u._struct.name = $1->s;
1390 }
1391 ;
1392
1393 struct_declaration_begin:
1394 LBRAC
1395 { push_scope(scanner); }
1396 ;
1397
1398 struct_declaration_end:
1399 RBRAC
1400 { pop_scope(scanner); }
1401 ;
1402
1403 variant_type_specifier:
1404 variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1405 {
1406 $$ = make_node(scanner, NODE_VARIANT);
1407 if (set_parent_node($2, $$))
1408 reparent_error(scanner, "variant reparent error");
1409 }
1410 | LT IDENTIFIER 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 | LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1418 {
1419 $$ = make_node(scanner, NODE_VARIANT);
1420 $$->u.variant.choice = $2->s;
1421 if (set_parent_node($5, $$))
1422 reparent_error(scanner, "variant reparent error");
1423 }
1424 | IDENTIFIER 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 if (set_parent_node($3, $$))
1429 reparent_error(scanner, "variant reparent error");
1430 }
1431 | IDENTIFIER LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1432 {
1433 $$ = make_node(scanner, NODE_VARIANT);
1434 $$->u.variant.name = $1->s;
1435 $$->u.variant.choice = $3->s;
1436 if (set_parent_node($6, $$))
1437 reparent_error(scanner, "variant reparent error");
1438 }
1439 | IDENTIFIER LT IDENTIFIER GT
1440 {
1441 $$ = make_node(scanner, NODE_VARIANT);
1442 $$->u.variant.name = $1->s;
1443 $$->u.variant.choice = $3->s;
1444 }
1445 | IDENTIFIER LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1446 {
1447 $$ = make_node(scanner, NODE_VARIANT);
1448 $$->u.variant.name = $1->s;
1449 $$->u.variant.choice = $3->s;
1450 if (set_parent_node($6, $$))
1451 reparent_error(scanner, "variant reparent error");
1452 }
1453 | IDENTIFIER LT ID_TYPE GT
1454 {
1455 $$ = make_node(scanner, NODE_VARIANT);
1456 $$->u.variant.name = $1->s;
1457 $$->u.variant.choice = $3->s;
1458 }
1459 | ID_TYPE 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 if (set_parent_node($3, $$))
1464 reparent_error(scanner, "variant reparent error");
1465 }
1466 | ID_TYPE LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1467 {
1468 $$ = make_node(scanner, NODE_VARIANT);
1469 $$->u.variant.name = $1->s;
1470 $$->u.variant.choice = $3->s;
1471 if (set_parent_node($6, $$))
1472 reparent_error(scanner, "variant reparent error");
1473 }
1474 | ID_TYPE LT IDENTIFIER GT
1475 {
1476 $$ = make_node(scanner, NODE_VARIANT);
1477 $$->u.variant.name = $1->s;
1478 $$->u.variant.choice = $3->s;
1479 }
1480 | ID_TYPE LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
1481 {
1482 $$ = make_node(scanner, NODE_VARIANT);
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.name = $1->s;
1492 $$->u.variant.choice = $3->s;
1493 }
1494 ;
1495
1496 variant_declaration_begin:
1497 LBRAC
1498 { push_scope(scanner); }
1499 ;
1500
1501 variant_declaration_end:
1502 RBRAC
1503 { pop_scope(scanner); }
1504 ;
1505
1506 type_specifier_or_integer_constant:
1507 declaration_specifiers
1508 { $$ = $1; }
1509 | DECIMAL_CONSTANT
1510 {
1511 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1512 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
1513 sscanf(yylval.gs->s, "%" PRIu64,
1514 &$$->u.unary_expression.u.unsigned_constant);
1515 }
1516 | OCTAL_CONSTANT
1517 {
1518 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1519 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
1520 sscanf(yylval.gs->s, "0%" PRIo64,
1521 &$$->u.unary_expression.u.unsigned_constant);
1522 }
1523 | HEXADECIMAL_CONSTANT
1524 {
1525 $$ = make_node(scanner, NODE_UNARY_EXPRESSION);
1526 $$->u.unary_expression.type = UNARY_UNSIGNED_CONSTANT;
1527 sscanf(yylval.gs->s, "0x%" PRIx64,
1528 &$$->u.unary_expression.u.unsigned_constant);
1529 }
1530 ;
1531
1532 enum_type_specifier:
1533 LBRAC enumerator_list RBRAC
1534 {
1535 $$ = make_node(scanner, NODE_ENUM);
1536 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u._enum.enumerator_list);
1537 }
1538 | LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
1539 {
1540 $$ = make_node(scanner, NODE_ENUM);
1541 $$->u._enum.container_type = $2;
1542 _cds_list_splice_tail(&($5)->tmp_head, &($$)->u._enum.enumerator_list);
1543 }
1544 | IDENTIFIER LBRAC enumerator_list RBRAC
1545 {
1546 $$ = make_node(scanner, NODE_ENUM);
1547 $$->u._enum.enum_id = $1->s;
1548 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._enum.enumerator_list);
1549 }
1550 | IDENTIFIER LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
1551 {
1552 $$ = make_node(scanner, NODE_ENUM);
1553 $$->u._enum.enum_id = $1->s;
1554 $$->u._enum.container_type = $3;
1555 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u._enum.enumerator_list);
1556 }
1557 | ID_TYPE LBRAC enumerator_list RBRAC
1558 {
1559 $$ = make_node(scanner, NODE_ENUM);
1560 $$->u._enum.enum_id = $1->s;
1561 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._enum.enumerator_list);
1562 }
1563 | ID_TYPE LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
1564 {
1565 $$ = make_node(scanner, NODE_ENUM);
1566 $$->u._enum.enum_id = $1->s;
1567 $$->u._enum.container_type = $3;
1568 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u._enum.enumerator_list);
1569 }
1570 | LBRAC enumerator_list COMMA RBRAC
1571 {
1572 $$ = make_node(scanner, NODE_ENUM);
1573 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u._enum.enumerator_list);
1574 }
1575 | LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
1576 {
1577 $$ = make_node(scanner, NODE_ENUM);
1578 $$->u._enum.container_type = $2;
1579 _cds_list_splice_tail(&($5)->tmp_head, &($$)->u._enum.enumerator_list);
1580 }
1581 | IDENTIFIER LBRAC enumerator_list COMMA RBRAC
1582 {
1583 $$ = make_node(scanner, NODE_ENUM);
1584 $$->u._enum.enum_id = $1->s;
1585 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._enum.enumerator_list);
1586 }
1587 | IDENTIFIER LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
1588 {
1589 $$ = make_node(scanner, NODE_ENUM);
1590 $$->u._enum.enum_id = $1->s;
1591 $$->u._enum.container_type = $3;
1592 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u._enum.enumerator_list);
1593 }
1594 | IDENTIFIER
1595 {
1596 $$ = make_node(scanner, NODE_ENUM);
1597 $$->u._enum.enum_id = $1->s;
1598 }
1599 | IDENTIFIER LT type_specifier_or_integer_constant GT
1600 {
1601 $$ = make_node(scanner, NODE_ENUM);
1602 $$->u._enum.enum_id = $1->s;
1603 $$->u._enum.container_type = $3;
1604 }
1605 | ID_TYPE LBRAC enumerator_list COMMA RBRAC
1606 {
1607 $$ = make_node(scanner, NODE_ENUM);
1608 $$->u._enum.enum_id = $1->s;
1609 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._enum.enumerator_list);
1610 }
1611 | ID_TYPE LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
1612 {
1613 $$ = make_node(scanner, NODE_ENUM);
1614 $$->u._enum.enum_id = $1->s;
1615 $$->u._enum.container_type = $3;
1616 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u._enum.enumerator_list);
1617 }
1618 | ID_TYPE
1619 {
1620 $$ = make_node(scanner, NODE_ENUM);
1621 $$->u._enum.enum_id = $1->s;
1622 }
1623 | ID_TYPE LT type_specifier_or_integer_constant GT
1624 {
1625 $$ = make_node(scanner, NODE_ENUM);
1626 $$->u._enum.enum_id = $1->s;
1627 $$->u._enum.container_type = $3;
1628 }
1629 ;
1630
1631 struct_or_variant_declaration_list:
1632 /* empty */
1633 { $$ = NULL; }
1634 | struct_or_variant_declaration_list struct_or_variant_declaration
1635 {
1636 if ($1) {
1637 $$ = $1;
1638 cds_list_add_tail(&($2)->siblings, &($$)->tmp_head);
1639 } else {
1640 $$ = $2;
1641 cds_list_add_tail(&($$)->siblings, &($$)->tmp_head);
1642 }
1643 }
1644 ;
1645
1646 struct_or_variant_declaration:
1647 specifier_qualifier_list struct_or_variant_declarator_list SEMICOLON
1648 {
1649 $$ = make_node(scanner, NODE_STRUCT_OR_VARIANT_DECLARATION);
1650 _cds_list_splice_tail(&($1)->tmp_head, &($$)->u.struct_or_variant_declaration.declaration_specifier);
1651 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u.struct_or_variant_declaration.type_declarators);
1652 }
1653 | specifier_qualifier_list TYPEDEF specifier_qualifier_list type_declarator_list SEMICOLON
1654 {
1655 $$ = make_node(scanner, NODE_TYPEDEF);
1656 _cds_list_splice_tail(&($1)->tmp_head, &($$)->u._typedef.declaration_specifier);
1657 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.declaration_specifier);
1658 _cds_list_splice_tail(&($4)->tmp_head, &($$)->u._typedef.type_declarators);
1659 }
1660 | TYPEDEF specifier_qualifier_list type_declarator_list SEMICOLON
1661 {
1662 $$ = make_node(scanner, NODE_TYPEDEF);
1663 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u._typedef.declaration_specifier);
1664 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
1665 }
1666 | specifier_qualifier_list TYPEDEF type_declarator_list SEMICOLON
1667 {
1668 $$ = make_node(scanner, NODE_TYPEDEF);
1669 _cds_list_splice_tail(&($1)->tmp_head, &($$)->u._typedef.declaration_specifier);
1670 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
1671 }
1672 | TYPEALIAS specifier_qualifier_list abstract_declarator_list COLON specifier_qualifier_list abstract_type_declarator_list SEMICOLON
1673 {
1674 $$ = make_node(scanner, NODE_TYPEALIAS);
1675 $$->u.typealias.target = make_node(scanner, NODE_TYPEALIAS_TARGET);
1676 $$->u.typealias.alias = make_node(scanner, NODE_TYPEALIAS_ALIAS);
1677 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u.typealias.target->u.typealias_target.declaration_specifier);
1678 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.typealias.target->u.typealias_target.type_declarators);
1679 _cds_list_splice_tail(&($5)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.declaration_specifier);
1680 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.type_declarators);
1681 }
1682 | TYPEALIAS specifier_qualifier_list abstract_declarator_list COLON type_declarator_list SEMICOLON
1683 {
1684 $$ = make_node(scanner, NODE_TYPEALIAS);
1685 $$->u.typealias.target = make_node(scanner, NODE_TYPEALIAS_TARGET);
1686 $$->u.typealias.alias = make_node(scanner, NODE_TYPEALIAS_ALIAS);
1687 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u.typealias.target->u.typealias_target.declaration_specifier);
1688 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.declaration_specifier);
1689 _cds_list_splice_tail(&($5)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.type_declarators);
1690 }
1691 ;
1692
1693 specifier_qualifier_list:
1694 CONST
1695 {
1696 $$ = make_node(scanner, NODE_TYPE_SPECIFIER);
1697 $$->u.type_specifier.type = TYPESPEC_CONST;
1698 }
1699 | type_specifier
1700 { $$ = $1; }
1701 | specifier_qualifier_list CONST
1702 {
1703 struct ctf_node *node;
1704
1705 $$ = $1;
1706 node = make_node(scanner, NODE_TYPE_SPECIFIER);
1707 node->u.type_specifier.type = TYPESPEC_CONST;
1708 cds_list_add_tail(&node->siblings, &($$)->tmp_head);
1709 }
1710 | specifier_qualifier_list type_specifier
1711 {
1712 $$ = $1;
1713 cds_list_add_tail(&($2)->siblings, &($$)->tmp_head);
1714 }
1715 ;
1716
1717 struct_or_variant_declarator_list:
1718 struct_or_variant_declarator
1719 { $$ = $1; }
1720 | struct_or_variant_declarator_list COMMA struct_or_variant_declarator
1721 {
1722 $$ = $1;
1723 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
1724 }
1725 ;
1726
1727 struct_or_variant_declarator:
1728 declarator
1729 { $$ = $1; }
1730 | COLON unary_expression
1731 { $$ = $2; }
1732 | declarator COLON unary_expression
1733 {
1734 $$ = $1;
1735 if (set_parent_node($3, $1))
1736 reparent_error(scanner, "struct_or_variant_declarator");
1737 }
1738 ;
1739
1740 enumerator_list:
1741 enumerator
1742 { $$ = $1; }
1743 | enumerator_list COMMA enumerator
1744 {
1745 $$ = $1;
1746 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
1747 }
1748 ;
1749
1750 enumerator:
1751 IDENTIFIER
1752 {
1753 $$ = make_node(scanner, NODE_ENUMERATOR);
1754 $$->u.enumerator.id = $1->s;
1755 }
1756 | ID_TYPE
1757 {
1758 $$ = make_node(scanner, NODE_ENUMERATOR);
1759 $$->u.enumerator.id = $1->s;
1760 }
1761 | keywords
1762 {
1763 $$ = make_node(scanner, NODE_ENUMERATOR);
1764 $$->u.enumerator.id = $1->s;
1765 }
1766 | STRING_LITERAL_START DQUOTE
1767 {
1768 $$ = make_node(scanner, NODE_ENUMERATOR);
1769 $$->u.enumerator.id = "";
1770 }
1771 | STRING_LITERAL_START s_char_sequence DQUOTE
1772 {
1773 $$ = make_node(scanner, NODE_ENUMERATOR);
1774 $$->u.enumerator.id = $2->s;
1775 }
1776 | IDENTIFIER EQUAL unary_expression_or_range
1777 {
1778 $$ = make_node(scanner, NODE_ENUMERATOR);
1779 $$->u.enumerator.id = $1->s;
1780 cds_list_splice(&($3)->tmp_head, &($$)->u.enumerator.values);
1781 }
1782 | ID_TYPE EQUAL unary_expression_or_range
1783 {
1784 $$ = make_node(scanner, NODE_ENUMERATOR);
1785 $$->u.enumerator.id = $1->s;
1786 cds_list_splice(&($3)->tmp_head, &($$)->u.enumerator.values);
1787 }
1788 | keywords EQUAL unary_expression_or_range
1789 {
1790 $$ = make_node(scanner, NODE_ENUMERATOR);
1791 $$->u.enumerator.id = $1->s;
1792 cds_list_splice(&($3)->tmp_head, &($$)->u.enumerator.values);
1793 }
1794 | STRING_LITERAL_START DQUOTE EQUAL unary_expression_or_range
1795 {
1796 $$ = make_node(scanner, NODE_ENUMERATOR);
1797 $$->u.enumerator.id = "";
1798 cds_list_splice(&($4)->tmp_head, &($$)->u.enumerator.values);
1799 }
1800 | STRING_LITERAL_START s_char_sequence DQUOTE EQUAL unary_expression_or_range
1801 {
1802 $$ = make_node(scanner, NODE_ENUMERATOR);
1803 $$->u.enumerator.id = $2->s;
1804 cds_list_splice(&($5)->tmp_head, &($$)->u.enumerator.values);
1805 }
1806 ;
1807
1808 abstract_declarator_list:
1809 abstract_declarator
1810 { $$ = $1; }
1811 | abstract_declarator_list COMMA abstract_declarator
1812 {
1813 $$ = $1;
1814 cds_list_add_tail(&($3)->siblings, &($$)->tmp_head);
1815 }
1816 ;
1817
1818 abstract_declarator:
1819 direct_abstract_declarator
1820 { $$ = $1; }
1821 | pointer direct_abstract_declarator
1822 {
1823 $$ = $2;
1824 cds_list_splice(&($1)->tmp_head, &($$)->u.type_declarator.pointers);
1825 }
1826 ;
1827
1828 direct_abstract_declarator:
1829 /* empty */
1830 {
1831 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1832 $$->u.type_declarator.type = TYPEDEC_ID;
1833 /* id is NULL */
1834 }
1835 | IDENTIFIER
1836 {
1837 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1838 $$->u.type_declarator.type = TYPEDEC_ID;
1839 $$->u.type_declarator.u.id = $1->s;
1840 }
1841 | LPAREN abstract_declarator RPAREN
1842 {
1843 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1844 $$->u.type_declarator.type = TYPEDEC_NESTED;
1845 $$->u.type_declarator.u.nested.type_declarator = $2;
1846 }
1847 | direct_abstract_declarator LSBRAC type_specifier_or_integer_constant 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.length = $3;
1853 }
1854 | direct_abstract_declarator LSBRAC RSBRAC
1855 {
1856 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1857 $$->u.type_declarator.type = TYPEDEC_NESTED;
1858 $$->u.type_declarator.u.nested.type_declarator = $1;
1859 $$->u.type_declarator.u.nested.abstract_array = 1;
1860 }
1861 ;
1862
1863 declarator:
1864 direct_declarator
1865 { $$ = $1; }
1866 | pointer direct_declarator
1867 {
1868 $$ = $2;
1869 cds_list_splice(&($1)->tmp_head, &($$)->u.type_declarator.pointers);
1870 }
1871 ;
1872
1873 direct_declarator:
1874 IDENTIFIER
1875 {
1876 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1877 $$->u.type_declarator.type = TYPEDEC_ID;
1878 $$->u.type_declarator.u.id = $1->s;
1879 }
1880 | LPAREN declarator RPAREN
1881 {
1882 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1883 $$->u.type_declarator.type = TYPEDEC_NESTED;
1884 $$->u.type_declarator.u.nested.type_declarator = $2;
1885 }
1886 | direct_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
1887 {
1888 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1889 $$->u.type_declarator.type = TYPEDEC_NESTED;
1890 $$->u.type_declarator.u.nested.type_declarator = $1;
1891 $$->u.type_declarator.u.nested.length = $3;
1892 }
1893 ;
1894
1895 type_declarator:
1896 direct_type_declarator
1897 { $$ = $1; }
1898 | pointer direct_type_declarator
1899 {
1900 $$ = $2;
1901 cds_list_splice(&($1)->tmp_head, &($$)->u.type_declarator.pointers);
1902 }
1903 ;
1904
1905 direct_type_declarator:
1906 IDENTIFIER
1907 {
1908 add_type(scanner, $1);
1909 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1910 $$->u.type_declarator.type = TYPEDEC_ID;
1911 $$->u.type_declarator.u.id = $1->s;
1912 }
1913 | LPAREN type_declarator RPAREN
1914 {
1915 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1916 $$->u.type_declarator.type = TYPEDEC_NESTED;
1917 $$->u.type_declarator.u.nested.type_declarator = $2;
1918 }
1919 | direct_type_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
1920 {
1921 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1922 $$->u.type_declarator.type = TYPEDEC_NESTED;
1923 $$->u.type_declarator.u.nested.type_declarator = $1;
1924 $$->u.type_declarator.u.nested.length = $3;
1925 }
1926 ;
1927
1928 abstract_type_declarator:
1929 direct_abstract_type_declarator
1930 { $$ = $1; }
1931 | pointer direct_abstract_type_declarator
1932 {
1933 $$ = $2;
1934 cds_list_splice(&($1)->tmp_head, &($$)->u.type_declarator.pointers);
1935 }
1936 ;
1937
1938 direct_abstract_type_declarator:
1939 /* empty */
1940 {
1941 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1942 $$->u.type_declarator.type = TYPEDEC_ID;
1943 /* id is NULL */
1944 }
1945 | IDENTIFIER
1946 {
1947 add_type(scanner, $1);
1948 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1949 $$->u.type_declarator.type = TYPEDEC_ID;
1950 $$->u.type_declarator.u.id = $1->s;
1951 }
1952 | LPAREN abstract_type_declarator RPAREN
1953 {
1954 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1955 $$->u.type_declarator.type = TYPEDEC_NESTED;
1956 $$->u.type_declarator.u.nested.type_declarator = $2;
1957 }
1958 | direct_abstract_type_declarator LSBRAC type_specifier_or_integer_constant 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.length = $3;
1964 }
1965 | direct_abstract_type_declarator LSBRAC RSBRAC
1966 {
1967 $$ = make_node(scanner, NODE_TYPE_DECLARATOR);
1968 $$->u.type_declarator.type = TYPEDEC_NESTED;
1969 $$->u.type_declarator.u.nested.type_declarator = $1;
1970 $$->u.type_declarator.u.nested.abstract_array = 1;
1971 }
1972 ;
1973
1974 pointer:
1975 STAR
1976 {
1977 $$ = make_node(scanner, NODE_POINTER);
1978 }
1979 | STAR pointer
1980 {
1981 $$ = make_node(scanner, NODE_POINTER);
1982 cds_list_splice(&($2)->tmp_head, &($$)->tmp_head);
1983 }
1984 | STAR type_qualifier_list pointer
1985 {
1986 $$ = make_node(scanner, NODE_POINTER);
1987 $$->u.pointer.const_qualifier = 1;
1988 cds_list_splice(&($3)->tmp_head, &($$)->tmp_head);
1989 }
1990 ;
1991
1992 type_qualifier_list:
1993 /* pointer assumes only const type qualifier */
1994 CONST
1995 | type_qualifier_list CONST
1996 ;
1997
1998 /* 2.3: CTF-specific declarations */
1999
2000 ctf_assignment_expression_list:
2001 ctf_assignment_expression SEMICOLON
2002 { $$ = $1; }
2003 | ctf_assignment_expression_list ctf_assignment_expression SEMICOLON
2004 {
2005 $$ = $1;
2006 cds_list_add_tail(&($2)->siblings, &($$)->tmp_head);
2007 }
2008 ;
2009
2010 ctf_assignment_expression:
2011 unary_expression EQUAL unary_expression
2012 {
2013 /*
2014 * Because we have left and right, cannot use
2015 * set_parent_node.
2016 */
2017 $$ = make_node(scanner, NODE_CTF_EXPRESSION);
2018 _cds_list_splice_tail(&($1)->tmp_head, &($$)->u.ctf_expression.left);
2019 if ($1->u.unary_expression.type != UNARY_STRING)
2020 reparent_error(scanner, "ctf_assignment_expression left expects string");
2021 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.ctf_expression.right);
2022 }
2023 | unary_expression TYPEASSIGN type_specifier
2024 {
2025 /*
2026 * Because we have left and right, cannot use
2027 * set_parent_node.
2028 */
2029 $$ = make_node(scanner, NODE_CTF_EXPRESSION);
2030 _cds_list_splice_tail(&($1)->tmp_head, &($$)->u.ctf_expression.left);
2031 if ($1->u.unary_expression.type != UNARY_STRING)
2032 reparent_error(scanner, "ctf_assignment_expression left expects string");
2033 cds_list_add(&($3)->siblings, &($3)->tmp_head);
2034 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.ctf_expression.right);
2035 }
2036 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list
2037 {
2038 $$ = make_node(scanner, NODE_TYPEDEF);
2039 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.declaration_specifier);
2040 _cds_list_splice_tail(&($1)->tmp_head, &($$)->u._typedef.declaration_specifier);
2041 _cds_list_splice_tail(&($4)->tmp_head, &($$)->u._typedef.type_declarators);
2042 }
2043 | TYPEDEF declaration_specifiers type_declarator_list
2044 {
2045 $$ = make_node(scanner, NODE_TYPEDEF);
2046 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u._typedef.declaration_specifier);
2047 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
2048 }
2049 | declaration_specifiers TYPEDEF type_declarator_list
2050 {
2051 $$ = make_node(scanner, NODE_TYPEDEF);
2052 _cds_list_splice_tail(&($1)->tmp_head, &($$)->u._typedef.declaration_specifier);
2053 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u._typedef.type_declarators);
2054 }
2055 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON declaration_specifiers abstract_type_declarator_list
2056 {
2057 $$ = make_node(scanner, NODE_TYPEALIAS);
2058 $$->u.typealias.target = make_node(scanner, NODE_TYPEALIAS_TARGET);
2059 $$->u.typealias.alias = make_node(scanner, NODE_TYPEALIAS_ALIAS);
2060 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u.typealias.target->u.typealias_target.declaration_specifier);
2061 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.typealias.target->u.typealias_target.type_declarators);
2062 _cds_list_splice_tail(&($5)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.declaration_specifier);
2063 _cds_list_splice_tail(&($6)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.type_declarators);
2064 }
2065 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON type_declarator_list
2066 {
2067 $$ = make_node(scanner, NODE_TYPEALIAS);
2068 $$->u.typealias.target = make_node(scanner, NODE_TYPEALIAS_TARGET);
2069 $$->u.typealias.alias = make_node(scanner, NODE_TYPEALIAS_ALIAS);
2070 _cds_list_splice_tail(&($2)->tmp_head, &($$)->u.typealias.target->u.typealias_target.declaration_specifier);
2071 _cds_list_splice_tail(&($3)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.declaration_specifier);
2072 _cds_list_splice_tail(&($5)->tmp_head, &($$)->u.typealias.alias->u.typealias_alias.type_declarators);
2073 }
2074 ;
This page took 0.067882 seconds and 3 git commands to generate.