cgram.y revision 1.284 1 %{
2 /* $NetBSD: cgram.y,v 1.284 2021/07/10 09:48:41 rillig Exp $ */
3
4 /*
5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
6 * Copyright (c) 1994, 1995 Jochen Pohl
7 * All Rights Reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Jochen Pohl for
20 * The NetBSD Project.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 __RCSID("$NetBSD: cgram.y,v 1.284 2021/07/10 09:48:41 rillig Exp $");
39 #endif
40
41 #include <limits.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "lint1.h"
46
47 extern char *yytext;
48
49 /*
50 * Contains the level of current declaration, used for symbol table entries.
51 * 0 is the top-level, > 0 is inside a function body.
52 */
53 int block_level;
54
55 /*
56 * level for memory allocation. Normally the same as block_level.
57 * An exception is the declaration of arguments in prototypes. Memory
58 * for these can't be freed after the declaration, but symbols must
59 * be removed from the symbol table after the declaration.
60 */
61 int mem_block_level;
62
63 /*
64 * Save the no-warns state and restore it to avoid the problem where
65 * if (expr) { stmt } / * NOLINT * / stmt;
66 */
67 static int olwarn = LWARN_BAD;
68
69 static void cgram_declare(sym_t *, bool, sbuf_t *);
70 static void ignore_up_to_rparen(void);
71 static sym_t *symbolrename(sym_t *, sbuf_t *);
72
73
74 #ifdef DEBUG
75 static void
76 CLEAR_WARN_FLAGS(const char *file, size_t line)
77 {
78 printf("%s:%d: %s:%zu: clearing flags\n",
79 curr_pos.p_file, curr_pos.p_line, file, line);
80 clear_warn_flags();
81 olwarn = LWARN_BAD;
82 }
83
84 static void
85 SAVE_WARN_FLAGS(const char *file, size_t line)
86 {
87 lint_assert(olwarn == LWARN_BAD);
88 printf("%s:%d: %s:%zu: saving flags %d\n",
89 curr_pos.p_file, curr_pos.p_line, file, line, lwarn);
90 olwarn = lwarn;
91 }
92
93 static void
94 RESTORE_WARN_FLAGS(const char *file, size_t line)
95 {
96 if (olwarn != LWARN_BAD) {
97 lwarn = olwarn;
98 printf("%s:%d: %s:%zu: restoring flags %d\n",
99 curr_pos.p_file, curr_pos.p_line, file, line, lwarn);
100 olwarn = LWARN_BAD;
101 } else
102 CLEAR_WARN_FLAGS(file, line);
103 }
104 #define cgram_debug(fmt, args...) printf("cgram_debug: " fmt "\n", ##args)
105 #else
106 #define CLEAR_WARN_FLAGS(f, l) clear_warn_flags(), olwarn = LWARN_BAD
107 #define SAVE_WARN_FLAGS(f, l) olwarn = lwarn
108 #define RESTORE_WARN_FLAGS(f, l) \
109 (void)(olwarn == LWARN_BAD ? (clear_warn_flags(), 0) : (lwarn = olwarn))
110 #define cgram_debug(fmt, args...) do { } while (false)
111 #endif
112
113 #define clear_warning_flags() CLEAR_WARN_FLAGS(__FILE__, __LINE__)
114 #define save_warning_flags() SAVE_WARN_FLAGS(__FILE__, __LINE__)
115 #define restore_warning_flags() RESTORE_WARN_FLAGS(__FILE__, __LINE__)
116
117 /* unbind the anonymous struct members from the struct */
118 static void
119 anonymize(sym_t *s)
120 {
121 for ( ; s != NULL; s = s->s_next)
122 s->s_styp = NULL;
123 }
124 %}
125
126 %expect 136
127
128 %union {
129 val_t *y_val;
130 sbuf_t *y_sb;
131 sym_t *y_sym;
132 op_t y_op;
133 scl_t y_scl;
134 tspec_t y_tspec;
135 tqual_t y_tqual;
136 type_t *y_type;
137 tnode_t *y_tnode;
138 range_t y_range;
139 strg_t *y_string;
140 qual_ptr *y_qual_ptr;
141 bool y_seen_statement;
142 struct generic_association *y_generic;
143 };
144
145 %token T_LBRACE T_RBRACE T_LBRACK T_RBRACK T_LPAREN T_RPAREN
146 %token T_POINT T_ARROW
147 %token <y_op> T_UNARY
148 %token <y_op> T_INCDEC
149 %token T_SIZEOF
150 %token T_BUILTIN_OFFSETOF
151 %token T_TYPEOF
152 %token T_EXTENSION
153 %token T_ALIGNAS
154 %token T_ALIGNOF
155 %token T_ASTERISK
156 %token <y_op> T_MULTIPLICATIVE
157 %token <y_op> T_ADDITIVE
158 %token <y_op> T_SHIFT
159 %token <y_op> T_RELATIONAL
160 %token <y_op> T_EQUALITY
161 %token T_AMPER
162 %token T_BITXOR
163 %token T_BITOR
164 %token T_LOGAND
165 %token T_LOGOR
166 %token T_QUEST
167 %token T_COLON
168 %token T_ASSIGN
169 %token <y_op> T_OPASSIGN
170 %token T_COMMA
171 %token T_SEMI
172 %token T_ELLIPSIS
173 %token T_REAL
174 %token T_IMAG
175 %token T_GENERIC
176 %token T_NORETURN
177
178 /* storage classes (extern, static, auto, register and typedef) */
179 %token <y_scl> T_SCLASS
180
181 /*
182 * predefined type keywords (char, int, short, long, unsigned, signed,
183 * float, double, void); see T_TYPENAME
184 */
185 %token <y_tspec> T_TYPE
186
187 /* qualifiers (const, volatile, restrict, _Thread_local) */
188 %token <y_tqual> T_QUAL
189
190 /* struct or union */
191 %token <y_tspec> T_STRUCT_OR_UNION
192
193 /* remaining keywords */
194 %token T_ASM
195 %token T_BREAK
196 %token T_CASE
197 %token T_CONTINUE
198 %token T_DEFAULT
199 %token T_DO
200 %token T_ELSE
201 %token T_ENUM
202 %token T_FOR
203 %token T_GOTO
204 %token T_IF
205 %token T_PACKED
206 %token T_RETURN
207 %token T_SWITCH
208 %token T_SYMBOLRENAME
209 %token T_WHILE
210
211 %token T_ATTRIBUTE
212 %token T_AT_ALIAS
213 %token T_AT_ALIGNED
214 %token T_AT_ALLOC_SIZE
215 %token T_AT_ALWAYS_INLINE
216 %token T_AT_BOUNDED
217 %token T_AT_BUFFER
218 %token T_AT_COLD
219 %token T_AT_COMMON
220 %token T_AT_CONSTRUCTOR
221 %token T_AT_DEPRECATED
222 %token T_AT_DESTRUCTOR
223 %token T_AT_FALLTHROUGH
224 %token T_AT_FORMAT
225 %token T_AT_FORMAT_ARG
226 %token T_AT_FORMAT_GNU_PRINTF
227 %token T_AT_FORMAT_PRINTF
228 %token T_AT_FORMAT_SCANF
229 %token T_AT_FORMAT_STRFMON
230 %token T_AT_FORMAT_STRFTIME
231 %token T_AT_FORMAT_SYSLOG
232 %token T_AT_GNU_INLINE
233 %token T_AT_MALLOC
234 %token T_AT_MAY_ALIAS
235 %token T_AT_MINBYTES
236 %token T_AT_MODE
237 %token T_AT_NOINLINE
238 %token T_AT_NONNULL
239 %token T_AT_NONSTRING
240 %token T_AT_NORETURN
241 %token T_AT_NOTHROW
242 %token T_AT_NO_INSTRUMENT_FUNCTION
243 %token T_AT_OPTIMIZE
244 %token T_AT_PACKED
245 %token T_AT_PCS
246 %token T_AT_PURE
247 %token T_AT_RETURNS_TWICE
248 %token T_AT_SECTION
249 %token T_AT_SENTINEL
250 %token T_AT_STRING
251 %token T_AT_TLS_MODEL
252 %token T_AT_TUNION
253 %token T_AT_UNUSED
254 %token T_AT_USED
255 %token T_AT_VISIBILITY
256 %token T_AT_WARN_UNUSED_RESULT
257 %token T_AT_WEAK
258
259 %left T_COMMA
260 %right T_ASSIGN T_OPASSIGN
261 %right T_QUEST T_COLON
262 %left T_LOGOR
263 %left T_LOGAND
264 %left T_BITOR
265 %left T_BITXOR
266 %left T_AMPER
267 %left T_EQUALITY
268 %left T_RELATIONAL
269 %left T_SHIFT
270 %left T_ADDITIVE
271 %left T_ASTERISK T_MULTIPLICATIVE
272 %right T_UNARY T_INCDEC T_SIZEOF T_REAL T_IMAG
273 %left T_LPAREN T_LBRACK T_POINT T_ARROW
274
275 %token <y_sb> T_NAME
276 %token <y_sb> T_TYPENAME
277 %token <y_val> T_CON
278 %token <y_string> T_STRING
279
280 %type <y_sym> func_decl
281 %type <y_sym> notype_decl
282 %type <y_sym> type_decl
283 %type <y_type> typespec
284 %type <y_type> clrtyp_typespec
285 %type <y_type> notype_typespec
286 %type <y_type> struct_spec
287 %type <y_type> enum_spec
288 %type <y_sym> struct_tag
289 %type <y_sym> enum_tag
290 %type <y_tspec> struct
291 %type <y_sym> struct_declaration
292 %type <y_sb> identifier
293 %type <y_sym> member_declaration_list_semi
294 %type <y_sym> member_declaration_list
295 %type <y_sym> member_declaration
296 %type <y_sym> notype_member_decls
297 %type <y_sym> type_member_decls
298 %type <y_sym> notype_member_decl
299 %type <y_sym> type_member_decl
300 %type <y_tnode> constant_expr
301 %type <y_tnode> array_size
302 %type <y_sym> enum_declaration
303 %type <y_sym> enumerator_list
304 %type <y_sym> enumerator
305 %type <y_sym> enumeration_constant
306 %type <y_sym> notype_direct_decl
307 %type <y_sym> type_direct_decl
308 %type <y_qual_ptr> pointer
309 %type <y_qual_ptr> asterisk
310 %type <y_sym> type_param_decl
311 %type <y_sym> param_list
312 %type <y_sym> abstract_decl_param_list
313 %type <y_sym> direct_param_decl
314 %type <y_sym> notype_param_decl
315 %type <y_sym> direct_notype_param_decl
316 %type <y_qual_ptr> type_qualifier_list_opt
317 %type <y_qual_ptr> type_qualifier_list
318 %type <y_qual_ptr> type_qualifier
319 %type <y_sym> identifier_list
320 %type <y_sym> abstract_declarator
321 %type <y_sym> direct_abstract_declarator
322 %type <y_sym> direct_abstract_declarator_postfix
323 %type <y_sym> vararg_parameter_type_list
324 %type <y_sym> parameter_type_list
325 %type <y_sym> parameter_declaration
326 %type <y_tnode> expr
327 %type <y_tnode> assignment_expression
328 %type <y_tnode> gcc_statement_expr_list
329 %type <y_tnode> gcc_statement_expr_item
330 %type <y_tnode> term
331 %type <y_tnode> generic_selection
332 %type <y_tnode> func_arg_list
333 %type <y_op> point_or_arrow
334 %type <y_type> type_name
335 %type <y_sym> abstract_declaration
336 %type <y_tnode> expr_opt
337 %type <y_string> string
338 %type <y_string> string2
339 %type <y_sb> asm_or_symbolrename_opt
340 %type <y_range> range
341 %type <y_seen_statement> block_item_list
342 %type <y_seen_statement> block_item
343 %type <y_generic> generic_assoc_list
344 %type <y_generic> generic_association
345
346 %%
347
348 program:
349 /* empty */ {
350 if (sflag) {
351 /* empty translation unit */
352 error(272);
353 } else if (!tflag) {
354 /* empty translation unit */
355 warning(272);
356 }
357 }
358 | translation_unit
359 ;
360
361 translation_unit: /* C99 6.9 */
362 external_declaration
363 | translation_unit external_declaration
364 ;
365
366 external_declaration: /* C99 6.9 */
367 asm_statement
368 | function_definition {
369 global_clean_up_decl(false);
370 clear_warning_flags();
371 }
372 | top_level_declaration {
373 global_clean_up_decl(false);
374 clear_warning_flags();
375 }
376 ;
377
378 /*
379 * On the top level, lint allows several forms of declarations that it doesn't
380 * allow in functions. For example, a single ';' is an empty declaration and
381 * is supported by some compilers, but in a function it would be an empty
382 * statement, not a declaration. This makes a difference in C90 mode, where
383 * a statement must not be followed by a declaration.
384 *
385 * See 'declaration' for all other declarations.
386 */
387 top_level_declaration: /* C99 6.9 calls this 'declaration' */
388 T_SEMI {
389 if (sflag) {
390 /* empty declaration */
391 error(0);
392 } else if (!tflag) {
393 /* empty declaration */
394 warning(0);
395 }
396 }
397 | clrtyp deftyp notype_init_decls T_SEMI {
398 if (sflag) {
399 /* old style declaration; add 'int' */
400 error(1);
401 } else if (!tflag) {
402 /* old style declaration; add 'int' */
403 warning(1);
404 }
405 }
406 | declaration_noerror
407 | error T_SEMI {
408 global_clean_up();
409 }
410 | error T_RBRACE {
411 global_clean_up();
412 }
413 ;
414
415 function_definition: /* C99 6.9.1 */
416 func_decl {
417 if ($1->s_type->t_tspec != FUNC) {
418 /* syntax error '%s' */
419 error(249, yytext);
420 YYERROR;
421 }
422 if ($1->s_type->t_typedef) {
423 /* ()-less function definition */
424 error(64);
425 YYERROR;
426 }
427 funcdef($1);
428 block_level++;
429 begin_declaration_level(ARG);
430 if (lwarn == LWARN_NONE)
431 $1->s_used = true;
432 } arg_declaration_list_opt {
433 end_declaration_level();
434 block_level--;
435 check_func_lint_directives();
436 check_func_old_style_arguments();
437 begin_control_statement(CS_FUNCTION_BODY);
438 } compound_statement {
439 funcend();
440 end_control_statement(CS_FUNCTION_BODY);
441 }
442 ;
443
444 func_decl:
445 clrtyp deftyp notype_decl {
446 $$ = $3;
447 }
448 | declmods deftyp notype_decl {
449 $$ = $3;
450 }
451 | declaration_specifiers deftyp type_decl {
452 $$ = $3;
453 }
454 ;
455
456 arg_declaration_list_opt: /* C99 6.9.1p13 example 1 */
457 /* empty */
458 | arg_declaration_list
459 ;
460
461 arg_declaration_list: /* C99 6.9.1p13 example 1 */
462 arg_declaration
463 | arg_declaration_list arg_declaration
464 /* XXX or better "arg_declaration error" ? */
465 | error
466 ;
467
468 /*
469 * "arg_declaration" is separated from "declaration" because it
470 * needs other error handling.
471 */
472 arg_declaration:
473 declmods deftyp T_SEMI {
474 /* empty declaration */
475 warning(2);
476 }
477 | declmods deftyp notype_init_decls T_SEMI
478 | declaration_specifiers deftyp T_SEMI {
479 if (!dcs->d_nonempty_decl) {
480 /* empty declaration */
481 warning(2);
482 } else {
483 /* '%s' declared in argument declaration list */
484 warning(3, type_name(dcs->d_type));
485 }
486 }
487 | declaration_specifiers deftyp type_init_decls T_SEMI {
488 if (dcs->d_nonempty_decl) {
489 /* '%s' declared in argument declaration list */
490 warning(3, type_name(dcs->d_type));
491 }
492 }
493 | declmods error
494 | declaration_specifiers error
495 ;
496
497 declaration: /* C99 6.7 */
498 declaration_noerror
499 | error T_SEMI
500 ;
501
502 declaration_noerror: /* see C99 6.7 'declaration' */
503 declmods deftyp T_SEMI {
504 if (dcs->d_scl == TYPEDEF) {
505 /* typedef declares no type name */
506 warning(72);
507 } else {
508 /* empty declaration */
509 warning(2);
510 }
511 }
512 | declmods deftyp notype_init_decls T_SEMI
513 | declaration_specifiers deftyp T_SEMI {
514 if (dcs->d_scl == TYPEDEF) {
515 /* typedef declares no type name */
516 warning(72);
517 } else if (!dcs->d_nonempty_decl) {
518 /* empty declaration */
519 warning(2);
520 }
521 }
522 | declaration_specifiers deftyp type_init_decls T_SEMI
523 ;
524
525 clrtyp:
526 /* empty */ {
527 clrtyp();
528 }
529 ;
530
531 deftyp:
532 /* empty */ {
533 deftyp();
534 }
535 ;
536
537 declaration_specifiers: /* C99 6.7 */
538 clrtyp_typespec {
539 add_type($1);
540 }
541 | declmods typespec {
542 add_type($2);
543 }
544 | type_attribute declaration_specifiers
545 | declaration_specifiers declmod
546 | declaration_specifiers notype_typespec {
547 add_type($2);
548 }
549 ;
550
551 declmods:
552 clrtyp qualifier_or_storage_class
553 | declmods declmod
554 ;
555
556 declmod:
557 qualifier_or_storage_class
558 | type_attribute
559 ;
560
561 qualifier_or_storage_class:
562 T_QUAL {
563 add_qualifier($1);
564 }
565 | T_SCLASS {
566 add_storage_class($1);
567 }
568 ;
569
570 clrtyp_typespec:
571 clrtyp notype_typespec {
572 $$ = $2;
573 }
574 | T_TYPENAME clrtyp {
575 $$ = getsym($1)->s_type;
576 }
577 ;
578
579 typespec:
580 notype_typespec
581 | T_TYPENAME {
582 $$ = getsym($1)->s_type;
583 }
584 ;
585
586 notype_typespec:
587 T_TYPE {
588 $$ = gettyp($1);
589 }
590 | T_TYPEOF term {
591 $$ = $2->tn_type;
592 }
593 | struct_spec {
594 end_declaration_level();
595 $$ = $1;
596 }
597 | enum_spec {
598 end_declaration_level();
599 $$ = $1;
600 }
601 ;
602
603 struct_spec:
604 struct struct_tag {
605 /*
606 * STDC requires that "struct a;" always introduces
607 * a new tag if "a" is not declared at current level
608 *
609 * yychar is valid because otherwise the parser would not
610 * have been able to decide if it must shift or reduce
611 */
612 $$ = mktag($2, $1, false, yychar == T_SEMI);
613 }
614 | struct struct_tag {
615 dcs->d_tagtyp = mktag($2, $1, true, false);
616 } struct_declaration {
617 $$ = complete_tag_struct_or_union(dcs->d_tagtyp, $4);
618 }
619 | struct {
620 dcs->d_tagtyp = mktag(NULL, $1, true, false);
621 } struct_declaration {
622 $$ = complete_tag_struct_or_union(dcs->d_tagtyp, $3);
623 }
624 | struct error {
625 symtyp = FVFT;
626 $$ = gettyp(INT);
627 }
628 ;
629
630 struct:
631 T_STRUCT_OR_UNION {
632 symtyp = FTAG;
633 begin_declaration_level($1 == STRUCT ? MOS : MOU);
634 dcs->d_offset = 0;
635 dcs->d_sou_align_in_bits = CHAR_SIZE;
636 } type_attribute_list_opt
637 ;
638
639 struct_tag:
640 identifier {
641 $$ = getsym($1);
642 }
643 ;
644
645 struct_declaration:
646 T_LBRACE {
647 symtyp = FVFT;
648 } member_declaration_list_semi T_RBRACE {
649 $$ = $3;
650 }
651 ;
652
653 member_declaration_list_semi:
654 /* empty */ {
655 $$ = NULL;
656 }
657 | member_declaration_list T_SEMI
658 | member_declaration_list {
659 if (sflag) {
660 /* syntax req. ';' after last struct/union member */
661 error(66);
662 } else {
663 /* syntax req. ';' after last struct/union member */
664 warning(66);
665 }
666 $$ = $1;
667 }
668 ;
669
670 member_declaration_list:
671 member_declaration
672 | member_declaration_list T_SEMI member_declaration {
673 $$ = lnklst($1, $3);
674 }
675 ;
676
677 member_declaration:
678 noclass_declmods deftyp {
679 /* too late, i know, but getsym() compensates it */
680 symtyp = FMEMBER;
681 } notype_member_decls type_attribute_opt {
682 symtyp = FVFT;
683 $$ = $4;
684 }
685 | noclass_declspecs deftyp {
686 symtyp = FMEMBER;
687 } type_member_decls type_attribute_opt {
688 symtyp = FVFT;
689 $$ = $4;
690 }
691 | noclass_declmods deftyp type_attribute_opt {
692 symtyp = FVFT;
693 /* struct or union member must be named */
694 if (!Sflag)
695 /* anonymous struct/union members is a C9X feature */
696 warning(49);
697 /* add all the members of the anonymous struct/union */
698 lint_assert(is_struct_or_union(dcs->d_type->t_tspec));
699 $$ = dcs->d_type->t_str->sou_first_member;
700 anonymize($$);
701 }
702 | noclass_declspecs deftyp type_attribute_opt {
703 symtyp = FVFT;
704 /* struct or union member must be named */
705 if (!Sflag)
706 /* anonymous struct/union members is a C9X feature */
707 warning(49);
708 if (is_struct_or_union(dcs->d_type->t_tspec)) {
709 $$ = dcs->d_type->t_str->sou_first_member;
710 /* add all the members of the anonymous struct/union */
711 anonymize($$);
712 } else {
713 /* syntax error '%s' */
714 error(249, "unnamed member");
715 $$ = NULL;
716 }
717 }
718 | error {
719 symtyp = FVFT;
720 $$ = NULL;
721 }
722 ;
723
724 noclass_declspecs:
725 noclass_declspecs_postfix
726 | type_attribute noclass_declspecs_postfix
727 ;
728
729 noclass_declspecs_postfix:
730 clrtyp_typespec {
731 add_type($1);
732 }
733 | noclass_declmods typespec {
734 add_type($2);
735 }
736 | noclass_declspecs_postfix T_QUAL {
737 add_qualifier($2);
738 }
739 | noclass_declspecs_postfix notype_typespec {
740 add_type($2);
741 }
742 | noclass_declspecs_postfix type_attribute
743 ;
744
745 noclass_declmods:
746 clrtyp T_QUAL {
747 add_qualifier($2);
748 }
749 | noclass_declmods T_QUAL {
750 add_qualifier($2);
751 }
752 ;
753
754 notype_member_decls:
755 notype_member_decl {
756 $$ = declarator_1_struct_union($1);
757 }
758 | notype_member_decls {
759 symtyp = FMEMBER;
760 } T_COMMA type_member_decl {
761 $$ = lnklst($1, declarator_1_struct_union($4));
762 }
763 ;
764
765 type_member_decls:
766 type_member_decl {
767 $$ = declarator_1_struct_union($1);
768 }
769 | type_member_decls {
770 symtyp = FMEMBER;
771 } T_COMMA type_member_decl {
772 $$ = lnklst($1, declarator_1_struct_union($4));
773 }
774 ;
775
776 notype_member_decl:
777 notype_decl
778 | notype_decl T_COLON constant_expr { /* C99 6.7.2.1 */
779 $$ = bitfield($1, to_int_constant($3, true));
780 }
781 | {
782 symtyp = FVFT;
783 } T_COLON constant_expr { /* C99 6.7.2.1 */
784 $$ = bitfield(NULL, to_int_constant($3, true));
785 }
786 ;
787
788 type_member_decl:
789 type_decl
790 | type_decl T_COLON constant_expr {
791 $$ = bitfield($1, to_int_constant($3, true));
792 }
793 | {
794 symtyp = FVFT;
795 } T_COLON constant_expr {
796 $$ = bitfield(NULL, to_int_constant($3, true));
797 }
798 ;
799
800 enum_spec:
801 enum enum_tag {
802 $$ = mktag($2, ENUM, false, false);
803 }
804 | enum enum_tag {
805 dcs->d_tagtyp = mktag($2, ENUM, true, false);
806 } enum_declaration {
807 $$ = complete_tag_enum(dcs->d_tagtyp, $4);
808 }
809 | enum {
810 dcs->d_tagtyp = mktag(NULL, ENUM, true, false);
811 } enum_declaration {
812 $$ = complete_tag_enum(dcs->d_tagtyp, $3);
813 }
814 | enum error {
815 symtyp = FVFT;
816 $$ = gettyp(INT);
817 }
818 ;
819
820 enum:
821 T_ENUM {
822 symtyp = FTAG;
823 begin_declaration_level(CTCONST);
824 }
825 ;
826
827 enum_tag:
828 identifier {
829 $$ = getsym($1);
830 }
831 ;
832
833 enum_declaration:
834 T_LBRACE {
835 symtyp = FVFT;
836 enumval = 0;
837 } enumerator_list enumerator_list_comma_opt T_RBRACE {
838 $$ = $3;
839 }
840 ;
841
842 enumerator_list: /* C99 6.7.2.2 */
843 enumerator
844 | enumerator_list T_COMMA enumerator {
845 $$ = lnklst($1, $3);
846 }
847 | error {
848 $$ = NULL;
849 }
850 ;
851
852 enumerator_list_comma_opt:
853 /* empty */
854 | T_COMMA {
855 if (sflag) {
856 /* trailing ',' prohibited in enum declaration */
857 error(54);
858 } else {
859 /* trailing ',' prohibited in enum declaration */
860 c99ism(54);
861 }
862 }
863 ;
864
865 enumerator: /* C99 6.7.2.2 */
866 enumeration_constant {
867 $$ = enumeration_constant($1, enumval, true);
868 }
869 | enumeration_constant T_ASSIGN constant_expr {
870 $$ = enumeration_constant($1, to_int_constant($3, true), false);
871 }
872 ;
873
874 enumeration_constant: /* C99 6.4.4.3 */
875 identifier {
876 $$ = getsym($1);
877 }
878 ;
879
880
881 /*
882 * For an explanation of 'notype' in the following rules, see the Bison
883 * manual, section 7.1 "Semantic Info in Token Kinds".
884 */
885
886 notype_init_decls:
887 notype_init_decl
888 | notype_init_decls T_COMMA type_init_decl
889 ;
890
891 type_init_decls:
892 type_init_decl
893 | type_init_decls T_COMMA type_init_decl
894 ;
895
896 notype_init_decl:
897 notype_decl asm_or_symbolrename_opt {
898 cgram_declare($1, false, $2);
899 check_size($1);
900 }
901 | notype_decl asm_or_symbolrename_opt {
902 begin_initialization($1);
903 cgram_declare($1, true, $2);
904 } T_ASSIGN initializer {
905 check_size($1);
906 end_initialization();
907 }
908 ;
909
910 type_init_decl:
911 type_decl asm_or_symbolrename_opt {
912 cgram_declare($1, false, $2);
913 check_size($1);
914 }
915 | type_decl asm_or_symbolrename_opt {
916 begin_initialization($1);
917 cgram_declare($1, true, $2);
918 } T_ASSIGN initializer {
919 check_size($1);
920 end_initialization();
921 }
922 ;
923
924 notype_decl:
925 /* TODO: removing type_attribute_list_opt here removes another 18 conflicts */
926 type_attribute_list_opt notype_direct_decl {
927 $$ = $2;
928 }
929 | pointer type_attribute_list_opt notype_direct_decl {
930 $$ = add_pointer($3, $1);
931 }
932 ;
933
934 type_decl:
935 /* TODO: removing type_attribute_list_opt here removes another 16 conflicts */
936 type_attribute_list_opt type_direct_decl {
937 $$ = $2;
938 }
939 | pointer type_attribute_list_opt type_direct_decl {
940 $$ = add_pointer($3, $1);
941 }
942 ;
943
944 notype_direct_decl:
945 T_NAME {
946 $$ = declarator_name(getsym($1));
947 }
948 | T_LPAREN type_decl T_RPAREN {
949 $$ = $2;
950 }
951 | notype_direct_decl T_LBRACK T_RBRACK {
952 $$ = add_array($1, false, 0);
953 }
954 | notype_direct_decl T_LBRACK array_size T_RBRACK {
955 $$ = add_array($1, true, to_int_constant($3, false));
956 }
957 | notype_direct_decl param_list asm_or_symbolrename_opt {
958 $$ = add_function(symbolrename($1, $3), $2);
959 end_declaration_level();
960 block_level--;
961 }
962 | notype_direct_decl type_attribute
963 ;
964
965 type_direct_decl:
966 identifier {
967 $$ = declarator_name(getsym($1));
968 }
969 | T_LPAREN type_decl T_RPAREN {
970 $$ = $2;
971 }
972 | type_direct_decl T_LBRACK T_RBRACK {
973 $$ = add_array($1, false, 0);
974 }
975 | type_direct_decl T_LBRACK array_size T_RBRACK {
976 $$ = add_array($1, true, to_int_constant($3, false));
977 }
978 | type_direct_decl param_list asm_or_symbolrename_opt {
979 $$ = add_function(symbolrename($1, $3), $2);
980 end_declaration_level();
981 block_level--;
982 }
983 | type_direct_decl type_attribute
984 ;
985
986 /*
987 * The two distinct rules type_param_decl and notype_param_decl avoid a
988 * conflict in argument lists. A typename enclosed in parentheses is always
989 * treated as a typename, not an argument name. For example, after
990 * "typedef double a;", the declaration "f(int (a));" is interpreted as
991 * "f(int (double));", not "f(int a);".
992 */
993 type_param_decl:
994 direct_param_decl
995 | pointer direct_param_decl {
996 $$ = add_pointer($2, $1);
997 }
998 ;
999
1000 notype_param_decl:
1001 direct_notype_param_decl
1002 | pointer direct_notype_param_decl {
1003 $$ = add_pointer($2, $1);
1004 }
1005 ;
1006
1007 direct_param_decl:
1008 identifier type_attribute_list_opt {
1009 $$ = declarator_name(getsym($1));
1010 }
1011 | T_LPAREN notype_param_decl T_RPAREN {
1012 $$ = $2;
1013 }
1014 | direct_param_decl T_LBRACK T_RBRACK {
1015 $$ = add_array($1, false, 0);
1016 }
1017 | direct_param_decl T_LBRACK array_size T_RBRACK {
1018 $$ = add_array($1, true, to_int_constant($3, false));
1019 }
1020 | direct_param_decl param_list asm_or_symbolrename_opt {
1021 $$ = add_function(symbolrename($1, $3), $2);
1022 end_declaration_level();
1023 block_level--;
1024 }
1025 ;
1026
1027 direct_notype_param_decl:
1028 identifier /* XXX: missing type_attribute_list_opt? */ {
1029 $$ = declarator_name(getsym($1));
1030 }
1031 | T_LPAREN notype_param_decl T_RPAREN {
1032 $$ = $2;
1033 }
1034 | direct_notype_param_decl T_LBRACK T_RBRACK {
1035 $$ = add_array($1, false, 0);
1036 }
1037 | direct_notype_param_decl T_LBRACK array_size T_RBRACK {
1038 $$ = add_array($1, true, to_int_constant($3, false));
1039 }
1040 | direct_notype_param_decl param_list asm_or_symbolrename_opt {
1041 $$ = add_function(symbolrename($1, $3), $2);
1042 end_declaration_level();
1043 block_level--;
1044 }
1045 ;
1046
1047 pointer: /* C99 6.7.5 */
1048 asterisk type_qualifier_list_opt {
1049 $$ = merge_qualified_pointer($1, $2);
1050 }
1051 | asterisk type_qualifier_list_opt pointer {
1052 $$ = merge_qualified_pointer($1, $2);
1053 $$ = merge_qualified_pointer($$, $3);
1054 }
1055 ;
1056
1057 asterisk:
1058 T_ASTERISK {
1059 $$ = xcalloc(1, sizeof(*$$));
1060 $$->p_pointer = true;
1061 }
1062 ;
1063
1064 type_qualifier_list_opt:
1065 /* empty */ {
1066 $$ = NULL;
1067 }
1068 | type_qualifier_list
1069 ;
1070
1071 type_qualifier_list: /* C99 6.7.5 */
1072 type_qualifier
1073 | type_qualifier_list type_qualifier {
1074 $$ = merge_qualified_pointer($1, $2);
1075 }
1076 ;
1077
1078 type_qualifier:
1079 T_QUAL {
1080 $$ = xcalloc(1, sizeof(*$$));
1081 if ($1 == CONST) {
1082 $$->p_const = true;
1083 } else if ($1 == VOLATILE) {
1084 $$->p_volatile = true;
1085 } else {
1086 lint_assert($1 == RESTRICT || $1 == THREAD);
1087 }
1088 }
1089 ;
1090
1091 align_as: /* See alignment-specifier in C11 6.7.5 */
1092 typespec
1093 | constant_expr
1094 ;
1095
1096 param_list:
1097 id_list_lparen identifier_list T_RPAREN {
1098 $$ = $2;
1099 }
1100 | abstract_decl_param_list
1101 ;
1102
1103 id_list_lparen:
1104 T_LPAREN {
1105 block_level++;
1106 begin_declaration_level(PROTO_ARG);
1107 }
1108 ;
1109
1110 identifier_list:
1111 T_NAME {
1112 $$ = old_style_function_name(getsym($1));
1113 }
1114 | identifier_list T_COMMA T_NAME {
1115 $$ = lnklst($1, old_style_function_name(getsym($3)));
1116 }
1117 | identifier_list error
1118 ;
1119
1120 abstract_decl_param_list:
1121 abstract_decl_lparen T_RPAREN type_attribute_opt {
1122 $$ = NULL;
1123 }
1124 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN type_attribute_opt {
1125 dcs->d_proto = true;
1126 $$ = $2;
1127 }
1128 | abstract_decl_lparen error T_RPAREN type_attribute_opt {
1129 $$ = NULL;
1130 }
1131 ;
1132
1133 abstract_decl_lparen:
1134 T_LPAREN {
1135 block_level++;
1136 begin_declaration_level(PROTO_ARG);
1137 }
1138 ;
1139
1140 vararg_parameter_type_list:
1141 parameter_type_list
1142 | parameter_type_list T_COMMA T_ELLIPSIS {
1143 dcs->d_vararg = true;
1144 $$ = $1;
1145 }
1146 | T_ELLIPSIS {
1147 if (sflag) {
1148 /* ANSI C requires formal parameter before '...' */
1149 error(84);
1150 } else if (!tflag) {
1151 /* ANSI C requires formal parameter before '...' */
1152 warning(84);
1153 }
1154 dcs->d_vararg = true;
1155 $$ = NULL;
1156 }
1157 ;
1158
1159 parameter_type_list:
1160 parameter_declaration
1161 | parameter_type_list T_COMMA parameter_declaration {
1162 $$ = lnklst($1, $3);
1163 }
1164 ;
1165
1166 /* XXX: C99 6.7.5 defines the same name, but it looks completely different. */
1167 parameter_declaration:
1168 declmods deftyp {
1169 $$ = declare_argument(abstract_name(), false);
1170 }
1171 | declaration_specifiers deftyp {
1172 $$ = declare_argument(abstract_name(), false);
1173 }
1174 | declmods deftyp notype_param_decl {
1175 $$ = declare_argument($3, false);
1176 }
1177 | declaration_specifiers deftyp type_param_decl {
1178 $$ = declare_argument($3, false);
1179 }
1180 | declmods deftyp abstract_declarator {
1181 $$ = declare_argument($3, false);
1182 }
1183 | declaration_specifiers deftyp abstract_declarator {
1184 $$ = declare_argument($3, false);
1185 }
1186 ;
1187
1188 asm_or_symbolrename_opt: /* expect only one */
1189 /* empty */ {
1190 $$ = NULL;
1191 }
1192 | T_ASM T_LPAREN T_STRING T_RPAREN {
1193 freeyyv(&$3, T_STRING);
1194 $$ = NULL;
1195 }
1196 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN {
1197 $$ = $3;
1198 }
1199 ;
1200
1201 initializer: /* C99 6.7.8 "Initialization" */
1202 expr %prec T_COMMA {
1203 init_expr($1);
1204 }
1205 | init_lbrace init_rbrace {
1206 /* XXX: Empty braces are not covered by C99 6.7.8. */
1207 }
1208 | init_lbrace initializer_list comma_opt init_rbrace
1209 | error
1210 ;
1211
1212 initializer_list: /* C99 6.7.8 "Initialization" */
1213 initializer_list_item
1214 | initializer_list T_COMMA initializer_list_item
1215 ;
1216
1217 initializer_list_item:
1218 designation initializer
1219 | initializer
1220 ;
1221
1222 designation: /* C99 6.7.8 "Initialization" */
1223 designator_list T_ASSIGN
1224 | identifier T_COLON {
1225 /* GCC style struct or union member name in initializer */
1226 gnuism(315);
1227 add_designator_member($1);
1228 }
1229 ;
1230
1231 designator_list: /* C99 6.7.8 "Initialization" */
1232 designator
1233 | designator_list designator
1234 ;
1235
1236 designator: /* C99 6.7.8 "Initialization" */
1237 T_LBRACK range T_RBRACK {
1238 add_designator_subscript($2);
1239 if (!Sflag)
1240 /* array initializer with des.s is a C9X feature */
1241 warning(321);
1242 }
1243 | T_POINT identifier {
1244 if (!Sflag)
1245 /* struct or union member name in initializer is ... */
1246 warning(313);
1247 add_designator_member($2);
1248 }
1249 ;
1250
1251 range:
1252 constant_expr {
1253 $$.lo = to_int_constant($1, true);
1254 $$.hi = $$.lo;
1255 }
1256 | constant_expr T_ELLIPSIS constant_expr {
1257 $$.lo = to_int_constant($1, true);
1258 $$.hi = to_int_constant($3, true);
1259 /* initialization with '[a...b]' is a GCC extension */
1260 gnuism(340);
1261 }
1262 ;
1263
1264 init_lbrace:
1265 T_LBRACE {
1266 init_lbrace();
1267 }
1268 ;
1269
1270 init_rbrace:
1271 T_RBRACE {
1272 init_rbrace();
1273 }
1274 ;
1275
1276 type_name: /* C99 6.7.6 */
1277 {
1278 begin_declaration_level(ABSTRACT);
1279 } abstract_declaration {
1280 end_declaration_level();
1281 $$ = $2->s_type;
1282 }
1283 ;
1284
1285 abstract_declaration:
1286 noclass_declmods deftyp {
1287 $$ = declare_1_abstract(abstract_name());
1288 }
1289 | noclass_declspecs deftyp {
1290 $$ = declare_1_abstract(abstract_name());
1291 }
1292 | noclass_declmods deftyp abstract_declarator {
1293 $$ = declare_1_abstract($3);
1294 }
1295 | noclass_declspecs deftyp abstract_declarator {
1296 $$ = declare_1_abstract($3);
1297 }
1298 ;
1299
1300 abstract_declarator: /* C99 6.7.6 */
1301 pointer {
1302 $$ = add_pointer(abstract_name(), $1);
1303 }
1304 | direct_abstract_declarator
1305 | pointer direct_abstract_declarator {
1306 $$ = add_pointer($2, $1);
1307 }
1308 | T_TYPEOF term { /* GCC extension */
1309 $$ = mktempsym($2->tn_type);
1310 }
1311 ;
1312
1313 direct_abstract_declarator: /* C99 6.7.6 */
1314 direct_abstract_declarator_postfix
1315 | type_attribute direct_abstract_declarator_postfix {
1316 $$ = $2;
1317 }
1318 ;
1319
1320 direct_abstract_declarator_postfix: /* C99 6.7.6 */
1321 T_LPAREN abstract_declarator T_RPAREN {
1322 $$ = $2;
1323 }
1324 | T_LBRACK T_RBRACK {
1325 $$ = add_array(abstract_name(), false, 0);
1326 }
1327 | T_LBRACK array_size T_RBRACK {
1328 $$ = add_array(abstract_name(), true, to_int_constant($2, false));
1329 }
1330 | direct_abstract_declarator_postfix T_LBRACK T_RBRACK {
1331 $$ = add_array($1, false, 0);
1332 }
1333 | direct_abstract_declarator_postfix
1334 T_LBRACK T_ASTERISK T_RBRACK { /* C99 */
1335 $$ = add_array($1, false, 0);
1336 }
1337 | direct_abstract_declarator_postfix T_LBRACK array_size T_RBRACK {
1338 $$ = add_array($1, true, to_int_constant($3, false));
1339 }
1340 | abstract_decl_param_list asm_or_symbolrename_opt {
1341 $$ = add_function(symbolrename(abstract_name(), $2), $1);
1342 end_declaration_level();
1343 block_level--;
1344 }
1345 | direct_abstract_declarator_postfix abstract_decl_param_list
1346 asm_or_symbolrename_opt {
1347 $$ = add_function(symbolrename($1, $3), $2);
1348 end_declaration_level();
1349 block_level--;
1350 }
1351 | direct_abstract_declarator_postfix type_attribute
1352 ;
1353
1354 array_size:
1355 type_qualifier_list_opt T_SCLASS constant_expr {
1356 /* C11 6.7.6.3p7 */
1357 if ($2 != STATIC)
1358 yyerror("Bad attribute");
1359 /* static array size is a C11 extension */
1360 c11ism(343);
1361 $$ = $3;
1362 }
1363 | constant_expr
1364 ;
1365
1366 non_expr_statement:
1367 gcc_attribute T_SEMI
1368 | labeled_statement
1369 | compound_statement
1370 | selection_statement
1371 | iteration_statement
1372 | jump_statement {
1373 seen_fallthrough = false;
1374 }
1375 | asm_statement
1376 ;
1377
1378 statement: /* C99 6.8 */
1379 expression_statement
1380 | non_expr_statement
1381 ;
1382
1383 labeled_statement: /* C99 6.8.1 */
1384 label gcc_attribute_list_opt statement
1385 ;
1386
1387 label:
1388 T_NAME T_COLON {
1389 symtyp = FLABEL;
1390 named_label(getsym($1));
1391 }
1392 | T_CASE constant_expr T_COLON {
1393 case_label($2);
1394 seen_fallthrough = true;
1395 }
1396 | T_CASE constant_expr T_ELLIPSIS constant_expr T_COLON {
1397 /* XXX: We don't fill all cases */
1398 case_label($2);
1399 seen_fallthrough = true;
1400 }
1401 | T_DEFAULT T_COLON {
1402 default_label();
1403 seen_fallthrough = true;
1404 }
1405 ;
1406
1407 compound_statement: /* C99 6.8.2 */
1408 compound_statement_lbrace block_item_list_opt
1409 compound_statement_rbrace
1410 ;
1411
1412 compound_statement_lbrace:
1413 T_LBRACE {
1414 block_level++;
1415 mem_block_level++;
1416 begin_declaration_level(AUTO);
1417 }
1418 ;
1419
1420 compound_statement_rbrace:
1421 T_RBRACE {
1422 end_declaration_level();
1423 freeblk();
1424 mem_block_level--;
1425 block_level--;
1426 seen_fallthrough = false;
1427 }
1428 ;
1429
1430 block_item_list_opt: /* C99 6.8.2 */
1431 /* empty */
1432 | block_item_list
1433 ;
1434
1435 block_item_list:
1436 block_item
1437 | block_item_list block_item {
1438 if (!Sflag && $1 && !$2)
1439 /* declarations after statements is a C99 feature */
1440 c99ism(327);
1441 $$ = $1 || $2;
1442 }
1443 ;
1444
1445 block_item:
1446 statement {
1447 $$ = true;
1448 restore_warning_flags();
1449 }
1450 | declaration {
1451 $$ = false;
1452 restore_warning_flags();
1453 }
1454 ;
1455
1456 expression_statement: /* C99 6.8.3 */
1457 expr T_SEMI {
1458 expr($1, false, false, false, false);
1459 seen_fallthrough = false;
1460 }
1461 | T_SEMI {
1462 seen_fallthrough = false;
1463 }
1464 ;
1465
1466 selection_statement: /* C99 6.8.4 */
1467 if_without_else {
1468 save_warning_flags();
1469 if2();
1470 if3(false);
1471 }
1472 | if_without_else T_ELSE {
1473 save_warning_flags();
1474 if2();
1475 } statement {
1476 clear_warning_flags();
1477 if3(true);
1478 }
1479 | if_without_else T_ELSE error {
1480 clear_warning_flags();
1481 if3(false);
1482 }
1483 | switch_expr statement {
1484 clear_warning_flags();
1485 switch2();
1486 }
1487 | switch_expr error {
1488 clear_warning_flags();
1489 switch2();
1490 }
1491 ;
1492
1493 if_without_else: /* see C99 6.8.4 */
1494 if_expr statement
1495 | if_expr error
1496 ;
1497
1498 if_expr: /* see C99 6.8.4 */
1499 T_IF T_LPAREN expr T_RPAREN {
1500 if1($3);
1501 clear_warning_flags();
1502 }
1503 ;
1504
1505 switch_expr: /* see C99 6.8.4 */
1506 T_SWITCH T_LPAREN expr T_RPAREN {
1507 switch1($3);
1508 clear_warning_flags();
1509 }
1510 ;
1511
1512 iteration_statement: /* C99 6.8.5 */
1513 T_WHILE T_LPAREN expr T_RPAREN {
1514 while1($3);
1515 clear_warning_flags();
1516 } while_body {
1517 clear_warning_flags();
1518 while2();
1519 }
1520 | do statement {
1521 clear_warning_flags();
1522 } T_WHILE T_LPAREN expr T_RPAREN T_SEMI {
1523 do2($6);
1524 seen_fallthrough = false;
1525 }
1526 | do error {
1527 clear_warning_flags();
1528 do2(NULL);
1529 }
1530 | for_exprs statement {
1531 clear_warning_flags();
1532 for2();
1533 end_declaration_level();
1534 block_level--;
1535 }
1536 | for_exprs error {
1537 clear_warning_flags();
1538 for2();
1539 end_declaration_level();
1540 block_level--;
1541 }
1542 ;
1543
1544 while_body:
1545 statement
1546 | error
1547 ;
1548
1549 do: /* see C99 6.8.5 */
1550 T_DO {
1551 do1();
1552 }
1553 ;
1554
1555 for_start: /* see C99 6.8.5 */
1556 T_FOR T_LPAREN {
1557 begin_declaration_level(AUTO);
1558 block_level++;
1559 }
1560 ;
1561
1562 for_exprs: /* see C99 6.8.5 */
1563 for_start declaration_specifiers deftyp notype_init_decls T_SEMI
1564 expr_opt T_SEMI expr_opt T_RPAREN {
1565 /* variable declaration in for loop */
1566 c99ism(325);
1567 for1(NULL, $6, $8);
1568 clear_warning_flags();
1569 }
1570 | for_start expr_opt T_SEMI expr_opt T_SEMI expr_opt T_RPAREN {
1571 for1($2, $4, $6);
1572 clear_warning_flags();
1573 }
1574 ;
1575
1576 jump_statement: /* C99 6.8.6 */
1577 goto identifier T_SEMI {
1578 do_goto(getsym($2));
1579 }
1580 | goto error T_SEMI {
1581 symtyp = FVFT;
1582 }
1583 | T_CONTINUE T_SEMI {
1584 do_continue();
1585 }
1586 | T_BREAK T_SEMI {
1587 do_break();
1588 }
1589 | T_RETURN T_SEMI {
1590 do_return(NULL);
1591 }
1592 | T_RETURN expr T_SEMI {
1593 do_return($2);
1594 }
1595 ;
1596
1597 goto:
1598 T_GOTO {
1599 symtyp = FLABEL;
1600 }
1601 ;
1602
1603 asm_statement:
1604 T_ASM T_LPAREN read_until_rparen T_SEMI {
1605 setasm();
1606 }
1607 | T_ASM T_QUAL T_LPAREN read_until_rparen T_SEMI {
1608 setasm();
1609 }
1610 | T_ASM error
1611 ;
1612
1613 read_until_rparen:
1614 /* empty */ {
1615 ignore_up_to_rparen();
1616 }
1617 ;
1618
1619 constant_expr_list_opt:
1620 /* empty */
1621 | constant_expr_list
1622 ;
1623
1624 constant_expr_list:
1625 constant_expr
1626 | constant_expr_list T_COMMA constant_expr
1627 ;
1628
1629 constant_expr: /* C99 6.6 */
1630 expr %prec T_ASSIGN
1631 ;
1632
1633 expr_opt:
1634 /* empty */ {
1635 $$ = NULL;
1636 }
1637 | expr
1638 ;
1639
1640 expr: /* C99 6.5 */
1641 expr T_ASTERISK expr {
1642 $$ = build(MULT, $1, $3);
1643 }
1644 | expr T_MULTIPLICATIVE expr {
1645 $$ = build($2, $1, $3);
1646 }
1647 | expr T_ADDITIVE expr {
1648 $$ = build($2, $1, $3);
1649 }
1650 | expr T_SHIFT expr {
1651 $$ = build($2, $1, $3);
1652 }
1653 | expr T_RELATIONAL expr {
1654 $$ = build($2, $1, $3);
1655 }
1656 | expr T_EQUALITY expr {
1657 $$ = build($2, $1, $3);
1658 }
1659 | expr T_AMPER expr {
1660 $$ = build(BITAND, $1, $3);
1661 }
1662 | expr T_BITXOR expr {
1663 $$ = build(BITXOR, $1, $3);
1664 }
1665 | expr T_BITOR expr {
1666 $$ = build(BITOR, $1, $3);
1667 }
1668 | expr T_LOGAND expr {
1669 $$ = build(LOGAND, $1, $3);
1670 }
1671 | expr T_LOGOR expr {
1672 $$ = build(LOGOR, $1, $3);
1673 }
1674 | expr T_QUEST expr T_COLON expr {
1675 $$ = build(QUEST, $1, build(COLON, $3, $5));
1676 }
1677 | expr T_ASSIGN expr {
1678 $$ = build(ASSIGN, $1, $3);
1679 }
1680 | expr T_OPASSIGN expr {
1681 $$ = build($2, $1, $3);
1682 }
1683 | expr T_COMMA expr {
1684 $$ = build(COMMA, $1, $3);
1685 }
1686 | term
1687 | generic_selection
1688 ;
1689
1690 assignment_expression: /* C99 6.5.16 */
1691 expr %prec T_ASSIGN
1692 ;
1693
1694 term: /* see C99 6.5.1 */
1695 T_NAME {
1696 /* XXX really necessary? */
1697 if (yychar < 0)
1698 yychar = yylex();
1699 $$ = new_name_node(getsym($1), yychar);
1700 }
1701 | string {
1702 $$ = new_string_node($1);
1703 }
1704 | T_CON {
1705 $$ = expr_new_constant(gettyp($1->v_tspec), $1);
1706 }
1707 | T_LPAREN expr T_RPAREN {
1708 if ($2 != NULL)
1709 $2->tn_parenthesized = true;
1710 $$ = $2;
1711 }
1712 | T_LPAREN compound_statement_lbrace gcc_statement_expr_list {
1713 block_level--;
1714 mem_block_level--;
1715 begin_initialization(mktempsym(dup_type($3->tn_type)));
1716 mem_block_level++;
1717 block_level++;
1718 /* ({ }) is a GCC extension */
1719 gnuism(320);
1720 } compound_statement_rbrace T_RPAREN {
1721 $$ = new_name_node(*current_initsym(), 0);
1722 end_initialization();
1723 }
1724 | term T_INCDEC {
1725 $$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL);
1726 }
1727 | T_INCDEC term {
1728 $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL);
1729 }
1730 | T_ASTERISK term {
1731 $$ = build(INDIR, $2, NULL);
1732 }
1733 | T_AMPER term {
1734 $$ = build(ADDR, $2, NULL);
1735 }
1736 | T_UNARY term {
1737 $$ = build($1, $2, NULL);
1738 }
1739 | T_ADDITIVE term {
1740 if (tflag && $1 == PLUS) {
1741 /* unary + is illegal in traditional C */
1742 warning(100);
1743 }
1744 $$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL);
1745 }
1746 | term T_LBRACK expr T_RBRACK {
1747 $$ = build(INDIR, build(PLUS, $1, $3), NULL);
1748 }
1749 | term T_LPAREN T_RPAREN {
1750 $$ = new_function_call_node($1, NULL);
1751 }
1752 | term T_LPAREN func_arg_list T_RPAREN {
1753 $$ = new_function_call_node($1, $3);
1754 }
1755 | term point_or_arrow T_NAME {
1756 if ($1 != NULL) {
1757 sym_t *msym;
1758 /*
1759 * XXX struct_or_union_member should be integrated
1760 * in build()
1761 */
1762 if ($2 == ARROW) {
1763 /*
1764 * must do this before struct_or_union_member
1765 * is called
1766 */
1767 $1 = cconv($1);
1768 }
1769 msym = struct_or_union_member($1, $2, getsym($3));
1770 $$ = build($2, $1, new_name_node(msym, 0));
1771 } else {
1772 $$ = NULL;
1773 }
1774 }
1775 | T_REAL term {
1776 $$ = build(REAL, $2, NULL);
1777 }
1778 | T_IMAG term {
1779 $$ = build(IMAG, $2, NULL);
1780 }
1781 | T_EXTENSION term {
1782 $$ = $2;
1783 }
1784 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA identifier T_RPAREN {
1785 symtyp = FMEMBER;
1786 $$ = build_offsetof($3, getsym($5));
1787 }
1788 | T_SIZEOF term {
1789 $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type);
1790 if ($$ != NULL)
1791 check_expr_misc($2, false, false, false, false, false, true);
1792 }
1793 | T_SIZEOF T_LPAREN type_name T_RPAREN %prec T_SIZEOF {
1794 $$ = build_sizeof($3);
1795 }
1796 | T_ALIGNOF T_LPAREN type_name T_RPAREN {
1797 $$ = build_alignof($3);
1798 }
1799 | T_LPAREN type_name T_RPAREN term %prec T_UNARY {
1800 $$ = cast($4, $2);
1801 }
1802 | T_LPAREN type_name T_RPAREN { /* C99 6.5.2.5 "Compound literals" */
1803 sym_t *tmp = mktempsym($2);
1804 begin_initialization(tmp);
1805 cgram_declare(tmp, true, NULL);
1806 } init_lbrace initializer_list comma_opt init_rbrace {
1807 if (!Sflag)
1808 /* compound literals are a C9X/GCC extension */
1809 gnuism(319);
1810 $$ = new_name_node(*current_initsym(), 0);
1811 end_initialization();
1812 }
1813 ;
1814
1815 generic_selection: /* C11 6.5.1.1 */
1816 T_GENERIC T_LPAREN assignment_expression T_COMMA
1817 generic_assoc_list T_RPAREN {
1818 /* generic selection requires C11 or later */
1819 c11ism(345);
1820 $$ = build_generic_selection($3, $5);
1821 }
1822 ;
1823
1824 generic_assoc_list: /* C11 6.5.1.1 */
1825 generic_association
1826 | generic_assoc_list T_COMMA generic_association {
1827 $3->ga_prev = $1;
1828 $$ = $3;
1829 }
1830 ;
1831
1832 generic_association: /* C11 6.5.1.1 */
1833 type_name T_COLON assignment_expression {
1834 $$ = getblk(sizeof(*$$));
1835 $$->ga_arg = $1;
1836 $$->ga_result = $3;
1837 }
1838 | T_DEFAULT T_COLON assignment_expression {
1839 $$ = getblk(sizeof(*$$));
1840 $$->ga_arg = NULL;
1841 $$->ga_result = $3;
1842 }
1843 ;
1844
1845 /*
1846 * The inner part of a GCC statement-expression of the form ({ ... }).
1847 *
1848 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
1849 */
1850 gcc_statement_expr_list:
1851 gcc_statement_expr_item
1852 | gcc_statement_expr_list gcc_statement_expr_item {
1853 $$ = $2;
1854 }
1855 ;
1856
1857 gcc_statement_expr_item:
1858 declaration {
1859 clear_warning_flags();
1860 $$ = NULL;
1861 }
1862 | non_expr_statement {
1863 $$ = expr_zalloc_tnode();
1864 $$->tn_type = gettyp(VOID);
1865 }
1866 | expr T_SEMI {
1867 if ($1 == NULL) { /* in case of syntax errors */
1868 $$ = expr_zalloc_tnode();
1869 $$->tn_type = gettyp(VOID);
1870 } else {
1871 /* XXX: do that only on the last name */
1872 if ($1->tn_op == NAME)
1873 $1->tn_sym->s_used = true;
1874 $$ = $1;
1875 expr($1, false, false, false, false);
1876 seen_fallthrough = false;
1877 }
1878 }
1879 ;
1880
1881 string:
1882 T_STRING
1883 | T_STRING string2 {
1884 $$ = cat_strings($1, $2);
1885 }
1886 ;
1887
1888 string2:
1889 T_STRING {
1890 if (tflag) {
1891 /* concatenated strings are illegal in traditional C */
1892 warning(219);
1893 }
1894 $$ = $1;
1895 }
1896 | string2 T_STRING {
1897 $$ = cat_strings($1, $2);
1898 }
1899 ;
1900
1901 func_arg_list:
1902 expr %prec T_COMMA {
1903 $$ = new_function_argument_node(NULL, $1);
1904 }
1905 | func_arg_list T_COMMA expr {
1906 $$ = new_function_argument_node($1, $3);
1907 }
1908 ;
1909
1910 point_or_arrow:
1911 T_POINT {
1912 symtyp = FMEMBER;
1913 $$ = POINT;
1914 }
1915 | T_ARROW {
1916 symtyp = FMEMBER;
1917 $$ = ARROW;
1918 }
1919 ;
1920
1921 identifier: /* C99 6.4.2.1 */
1922 T_NAME {
1923 $$ = $1;
1924 cgram_debug("name '%s'", $$->sb_name);
1925 }
1926 | T_TYPENAME {
1927 $$ = $1;
1928 cgram_debug("typename '%s'", $$->sb_name);
1929 }
1930 ;
1931
1932 comma_opt:
1933 /* empty */
1934 | T_COMMA
1935 ;
1936
1937 /* GCC extensions */
1938
1939 type_attribute_list_opt:
1940 /* empty */
1941 | type_attribute_list
1942 ;
1943
1944 type_attribute_list:
1945 type_attribute
1946 | type_attribute_list type_attribute
1947 ;
1948
1949 type_attribute_opt:
1950 /* empty */
1951 | type_attribute
1952 ;
1953
1954 type_attribute:
1955 gcc_attribute
1956 | T_ALIGNAS T_LPAREN align_as T_RPAREN
1957 | T_PACKED {
1958 addpacked();
1959 }
1960 | T_NORETURN
1961 ;
1962
1963 gcc_attribute_list_opt:
1964 /* empty */
1965 | gcc_attribute_list
1966 ;
1967
1968 gcc_attribute_list:
1969 gcc_attribute
1970 | gcc_attribute_list gcc_attribute
1971 ;
1972
1973 /* https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html */
1974 gcc_attribute:
1975 T_ATTRIBUTE T_LPAREN T_LPAREN {
1976 attron = true;
1977 } gcc_attribute_spec_list {
1978 attron = false;
1979 } T_RPAREN T_RPAREN
1980 ;
1981
1982 gcc_attribute_spec_list:
1983 gcc_attribute_spec
1984 | gcc_attribute_spec_list T_COMMA gcc_attribute_spec
1985 ;
1986
1987 gcc_attribute_spec:
1988 /* empty */
1989 | T_AT_ALWAYS_INLINE
1990 | T_AT_ALIAS T_LPAREN string T_RPAREN
1991 | T_AT_ALIGNED T_LPAREN constant_expr T_RPAREN
1992 | T_AT_ALIGNED
1993 | T_AT_ALLOC_SIZE T_LPAREN constant_expr T_COMMA constant_expr T_RPAREN
1994 | T_AT_ALLOC_SIZE T_LPAREN constant_expr T_RPAREN
1995 | T_AT_BOUNDED T_LPAREN gcc_attribute_bounded
1996 T_COMMA constant_expr T_COMMA constant_expr T_RPAREN
1997 | T_AT_COLD
1998 | T_AT_COMMON
1999 | T_AT_CONSTRUCTOR T_LPAREN constant_expr T_RPAREN
2000 | T_AT_CONSTRUCTOR
2001 | T_AT_DEPRECATED T_LPAREN string T_RPAREN
2002 | T_AT_DEPRECATED
2003 | T_AT_DESTRUCTOR T_LPAREN constant_expr T_RPAREN
2004 | T_AT_DESTRUCTOR
2005 | T_AT_FALLTHROUGH {
2006 fallthru(1);
2007 }
2008 | T_AT_FORMAT T_LPAREN gcc_attribute_format T_COMMA
2009 constant_expr T_COMMA constant_expr T_RPAREN
2010 | T_AT_FORMAT_ARG T_LPAREN constant_expr T_RPAREN
2011 | T_AT_GNU_INLINE
2012 | T_AT_MALLOC
2013 | T_AT_MAY_ALIAS
2014 | T_AT_MODE T_LPAREN T_NAME T_RPAREN
2015 | T_AT_NOINLINE
2016 | T_AT_NONNULL T_LPAREN constant_expr_list_opt T_RPAREN
2017 | T_AT_NONNULL
2018 | T_AT_NONSTRING
2019 | T_AT_NORETURN
2020 | T_AT_NOTHROW
2021 | T_AT_NO_INSTRUMENT_FUNCTION
2022 | T_AT_OPTIMIZE T_LPAREN string T_RPAREN
2023 | T_AT_PACKED {
2024 addpacked();
2025 }
2026 | T_AT_PCS T_LPAREN string T_RPAREN
2027 | T_AT_PURE
2028 | T_AT_RETURNS_TWICE
2029 | T_AT_SECTION T_LPAREN string T_RPAREN
2030 | T_AT_SENTINEL T_LPAREN constant_expr T_RPAREN
2031 | T_AT_SENTINEL
2032 | T_AT_TLS_MODEL T_LPAREN string T_RPAREN
2033 | T_AT_TUNION
2034 | T_AT_UNUSED {
2035 add_attr_used();
2036 }
2037 | T_AT_USED {
2038 add_attr_used();
2039 }
2040 | T_AT_VISIBILITY T_LPAREN constant_expr T_RPAREN
2041 | T_AT_WARN_UNUSED_RESULT
2042 | T_AT_WEAK
2043 | T_QUAL {
2044 if ($1 != CONST)
2045 yyerror("Bad attribute");
2046 }
2047 ;
2048
2049 gcc_attribute_bounded:
2050 T_AT_MINBYTES
2051 | T_AT_STRING
2052 | T_AT_BUFFER
2053 ;
2054
2055 gcc_attribute_format:
2056 T_AT_FORMAT_GNU_PRINTF
2057 | T_AT_FORMAT_PRINTF
2058 | T_AT_FORMAT_SCANF
2059 | T_AT_FORMAT_STRFMON
2060 | T_AT_FORMAT_STRFTIME
2061 | T_AT_FORMAT_SYSLOG
2062 ;
2063
2064 %%
2065
2066 /* ARGSUSED */
2067 int
2068 yyerror(const char *msg)
2069 {
2070 /* syntax error '%s' */
2071 error(249, yytext);
2072 if (++sytxerr >= 5)
2073 norecover();
2074 return 0;
2075 }
2076
2077 static void
2078 cgram_declare(sym_t *decl, bool initflg, sbuf_t *renaming)
2079 {
2080 declare(decl, initflg, renaming);
2081 if (renaming != NULL)
2082 freeyyv(&renaming, T_NAME);
2083 }
2084
2085 /*
2086 * Discard all input tokens up to and including the next
2087 * unmatched right paren
2088 */
2089 static void
2090 ignore_up_to_rparen(void)
2091 {
2092 int level;
2093
2094 if (yychar < 0)
2095 yychar = yylex();
2096 freeyyv(&yylval, yychar);
2097
2098 level = 1;
2099 while (yychar != T_RPAREN || --level > 0) {
2100 if (yychar == T_LPAREN) {
2101 level++;
2102 } else if (yychar <= 0) {
2103 break;
2104 }
2105 freeyyv(&yylval, yychar = yylex());
2106 }
2107
2108 yyclearin;
2109 }
2110
2111 static sym_t *
2112 symbolrename(sym_t *s, sbuf_t *sb)
2113 {
2114 if (sb != NULL)
2115 s->s_rename = sb->sb_name;
2116 return s;
2117 }
2118