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