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