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