cgram.y revision 1.260 1 %{
2 /* $NetBSD: cgram.y,v 1.260 2021/07/06 18:22:40 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.260 2021/07/06 18:22:40 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 166
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 /* Type Attributes */
211 %token <y_type> T_ATTRIBUTE
212 %token <y_type> T_AT_ALIAS
213 %token <y_type> T_AT_ALIGNED
214 %token <y_type> T_AT_ALLOC_SIZE
215 %token <y_type> T_AT_ALWAYS_INLINE
216 %token <y_type> T_AT_BOUNDED
217 %token <y_type> T_AT_BUFFER
218 %token <y_type> T_AT_COLD
219 %token <y_type> T_AT_COMMON
220 %token <y_type> T_AT_CONSTRUCTOR
221 %token <y_type> T_AT_DEPRECATED
222 %token <y_type> T_AT_DESTRUCTOR
223 %token <y_type> T_AT_FALLTHROUGH
224 %token <y_type> T_AT_FORMAT
225 %token <y_type> T_AT_FORMAT_ARG
226 %token <y_type> T_AT_FORMAT_GNU_PRINTF
227 %token <y_type> T_AT_FORMAT_PRINTF
228 %token <y_type> T_AT_FORMAT_SCANF
229 %token <y_type> T_AT_FORMAT_STRFMON
230 %token <y_type> T_AT_FORMAT_STRFTIME
231 %token <y_type> T_AT_FORMAT_SYSLOG
232 %token <y_type> T_AT_GNU_INLINE
233 %token <y_type> T_AT_MALLOC
234 %token <y_type> T_AT_MAY_ALIAS
235 %token <y_type> T_AT_MINBYTES
236 %token <y_type> T_AT_MODE
237 %token <y_type> T_AT_NOINLINE
238 %token <y_type> T_AT_NONNULL
239 %token <y_type> T_AT_NONSTRING
240 %token <y_type> T_AT_NORETURN
241 %token <y_type> T_AT_NOTHROW
242 %token <y_type> T_AT_NO_INSTRUMENT_FUNCTION
243 %token <y_type> T_AT_OPTIMIZE
244 %token <y_type> T_AT_PACKED
245 %token <y_type> T_AT_PCS
246 %token <y_type> T_AT_PURE
247 %token <y_type> T_AT_RETURNS_TWICE
248 %token <y_type> T_AT_SECTION
249 %token <y_type> T_AT_SENTINEL
250 %token <y_type> T_AT_STRING
251 %token <y_type> T_AT_TLS_MODEL
252 %token <y_type> T_AT_TUNION
253 %token <y_type> T_AT_UNUSED
254 %token <y_type> T_AT_USED
255 %token <y_type> T_AT_VISIBILITY
256 %token <y_type> T_AT_WARN_UNUSED_RESULT
257 %token <y_type> 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 noclass_declspecs:
753 clrtyp_typespec {
754 add_type($1);
755 }
756 | type_attribute noclass_declspecs
757 | noclass_declmods typespec {
758 add_type($2);
759 }
760 | noclass_declspecs T_QUAL {
761 add_qualifier($2);
762 }
763 | noclass_declspecs notype_typespec {
764 add_type($2);
765 }
766 | noclass_declspecs type_attribute
767 ;
768
769 noclass_declmods:
770 clrtyp T_QUAL {
771 add_qualifier($2);
772 }
773 | noclass_declmods T_QUAL {
774 add_qualifier($2);
775 }
776 ;
777
778 notype_member_decls:
779 notype_member_decl {
780 $$ = declarator_1_struct_union($1);
781 }
782 | notype_member_decls {
783 symtyp = FMEMBER;
784 } T_COMMA type_member_decl {
785 $$ = lnklst($1, declarator_1_struct_union($4));
786 }
787 ;
788
789 type_member_decls:
790 type_member_decl {
791 $$ = declarator_1_struct_union($1);
792 }
793 | type_member_decls {
794 symtyp = FMEMBER;
795 } T_COMMA type_member_decl {
796 $$ = lnklst($1, declarator_1_struct_union($4));
797 }
798 ;
799
800 notype_member_decl:
801 notype_decl
802 | notype_decl T_COLON constant_expr { /* C99 6.7.2.1 */
803 $$ = bitfield($1, to_int_constant($3, true));
804 }
805 | {
806 symtyp = FVFT;
807 } T_COLON constant_expr { /* C99 6.7.2.1 */
808 $$ = bitfield(NULL, to_int_constant($3, true));
809 }
810 ;
811
812 type_member_decl:
813 type_decl
814 | type_decl T_COLON constant_expr {
815 $$ = bitfield($1, to_int_constant($3, true));
816 }
817 | {
818 symtyp = FVFT;
819 } T_COLON constant_expr {
820 $$ = bitfield(NULL, to_int_constant($3, true));
821 }
822 ;
823
824 enum_spec:
825 enum enum_tag {
826 $$ = mktag($2, ENUM, false, false);
827 }
828 | enum enum_tag {
829 dcs->d_tagtyp = mktag($2, ENUM, true, false);
830 } enum_declaration {
831 $$ = complete_tag_enum(dcs->d_tagtyp, $4);
832 }
833 | enum {
834 dcs->d_tagtyp = mktag(NULL, ENUM, true, false);
835 } enum_declaration {
836 $$ = complete_tag_enum(dcs->d_tagtyp, $3);
837 }
838 | enum error {
839 symtyp = FVFT;
840 $$ = gettyp(INT);
841 }
842 ;
843
844 enum:
845 T_ENUM {
846 symtyp = FTAG;
847 begin_declaration_level(CTCONST);
848 }
849 ;
850
851 enum_tag:
852 identifier {
853 $$ = getsym($1);
854 }
855 ;
856
857 enum_declaration:
858 enum_decl_lbrace enumerator_list enumerator_list_comma_opt T_RBRACE {
859 $$ = $2;
860 }
861 ;
862
863 enum_decl_lbrace:
864 T_LBRACE {
865 symtyp = FVFT;
866 enumval = 0;
867 }
868 ;
869
870 enumerator_list: /* C99 6.7.2.2 */
871 enumerator
872 | enumerator_list T_COMMA enumerator {
873 $$ = lnklst($1, $3);
874 }
875 | error {
876 $$ = NULL;
877 }
878 ;
879
880 enumerator_list_comma_opt:
881 /* empty */
882 | T_COMMA {
883 if (sflag) {
884 /* trailing ',' prohibited in enum declaration */
885 error(54);
886 } else {
887 /* trailing ',' prohibited in enum declaration */
888 c99ism(54);
889 }
890 }
891 ;
892
893 enumerator: /* C99 6.7.2.2 */
894 enumeration_constant {
895 $$ = enumeration_constant($1, enumval, true);
896 }
897 | enumeration_constant T_ASSIGN constant_expr {
898 $$ = enumeration_constant($1, to_int_constant($3, true), false);
899 }
900 ;
901
902 enumeration_constant: /* C99 6.4.4.3 */
903 identifier {
904 $$ = getsym($1);
905 }
906 ;
907
908
909 /*
910 * For an explanation of 'notype' in the following rules, see the Bison
911 * manual, section 7.1 "Semantic Info in Token Kinds".
912 */
913
914 notype_init_decls:
915 notype_init_decl
916 | notype_init_decls T_COMMA type_init_decl
917 ;
918
919 type_init_decls:
920 type_init_decl
921 | type_init_decls T_COMMA type_init_decl
922 ;
923
924 notype_init_decl:
925 notype_decl asm_or_symbolrename_opt {
926 cgram_declare($1, false, $2);
927 check_size($1);
928 }
929 | notype_decl asm_or_symbolrename_opt {
930 begin_initialization($1);
931 cgram_declare($1, true, $2);
932 } T_ASSIGN initializer {
933 check_size($1);
934 end_initialization();
935 }
936 ;
937
938 type_init_decl:
939 type_decl asm_or_symbolrename_opt {
940 cgram_declare($1, false, $2);
941 check_size($1);
942 }
943 | type_decl asm_or_symbolrename_opt {
944 begin_initialization($1);
945 cgram_declare($1, true, $2);
946 } T_ASSIGN initializer {
947 check_size($1);
948 end_initialization();
949 }
950 ;
951
952 notype_decl:
953 notype_direct_decl
954 | pointer notype_direct_decl {
955 $$ = add_pointer($2, $1);
956 }
957 ;
958
959 type_decl:
960 type_direct_decl
961 | pointer type_direct_decl {
962 $$ = add_pointer($2, $1);
963 }
964 ;
965
966 notype_direct_decl:
967 T_NAME {
968 $$ = declarator_name(getsym($1));
969 }
970 | T_LPAREN type_decl T_RPAREN {
971 $$ = $2;
972 }
973 | type_attribute notype_direct_decl {
974 $$ = $2;
975 }
976 | notype_direct_decl T_LBRACK T_RBRACK {
977 $$ = add_array($1, false, 0);
978 }
979 | notype_direct_decl T_LBRACK array_size T_RBRACK {
980 $$ = add_array($1, true, to_int_constant($3, false));
981 }
982 | notype_direct_decl param_list asm_or_symbolrename_opt {
983 $$ = add_function(symbolrename($1, $3), $2);
984 end_declaration_level();
985 block_level--;
986 }
987 | notype_direct_decl type_attribute
988 ;
989
990 type_direct_decl:
991 identifier {
992 $$ = declarator_name(getsym($1));
993 }
994 | T_LPAREN type_decl T_RPAREN {
995 $$ = $2;
996 }
997 | type_attribute type_direct_decl {
998 $$ = $2;
999 }
1000 | type_direct_decl T_LBRACK T_RBRACK {
1001 $$ = add_array($1, false, 0);
1002 }
1003 | type_direct_decl T_LBRACK array_size T_RBRACK {
1004 $$ = add_array($1, true, to_int_constant($3, false));
1005 }
1006 | type_direct_decl param_list asm_or_symbolrename_opt {
1007 $$ = add_function(symbolrename($1, $3), $2);
1008 end_declaration_level();
1009 block_level--;
1010 }
1011 | type_direct_decl type_attribute
1012 ;
1013
1014 /*
1015 * The two distinct rules type_param_decl and notype_param_decl avoid a
1016 * conflict in argument lists. A typename enclosed in parentheses is always
1017 * treated as a typename, not an argument name. For example, after
1018 * "typedef double a;", the declaration "f(int (a));" is interpreted as
1019 * "f(int (double));", not "f(int a);".
1020 */
1021 type_param_decl:
1022 direct_param_decl
1023 | pointer direct_param_decl {
1024 $$ = add_pointer($2, $1);
1025 }
1026 ;
1027
1028 notype_param_decl:
1029 direct_notype_param_decl
1030 | pointer direct_notype_param_decl {
1031 $$ = add_pointer($2, $1);
1032 }
1033 ;
1034
1035 direct_param_decl:
1036 identifier type_attribute_list {
1037 $$ = declarator_name(getsym($1));
1038 }
1039 | identifier {
1040 $$ = declarator_name(getsym($1));
1041 }
1042 | T_LPAREN notype_param_decl T_RPAREN {
1043 $$ = $2;
1044 }
1045 | direct_param_decl T_LBRACK T_RBRACK {
1046 $$ = add_array($1, false, 0);
1047 }
1048 | direct_param_decl T_LBRACK array_size T_RBRACK {
1049 $$ = add_array($1, true, to_int_constant($3, false));
1050 }
1051 | direct_param_decl param_list asm_or_symbolrename_opt {
1052 $$ = add_function(symbolrename($1, $3), $2);
1053 end_declaration_level();
1054 block_level--;
1055 }
1056 ;
1057
1058 direct_notype_param_decl:
1059 /* XXX: missing identifier type_attribute_list? */
1060 identifier {
1061 $$ = declarator_name(getsym($1));
1062 }
1063 | T_LPAREN notype_param_decl T_RPAREN {
1064 $$ = $2;
1065 }
1066 | direct_notype_param_decl T_LBRACK T_RBRACK {
1067 $$ = add_array($1, false, 0);
1068 }
1069 | direct_notype_param_decl T_LBRACK array_size T_RBRACK {
1070 $$ = add_array($1, true, to_int_constant($3, false));
1071 }
1072 | direct_notype_param_decl param_list asm_or_symbolrename_opt {
1073 $$ = add_function(symbolrename($1, $3), $2);
1074 end_declaration_level();
1075 block_level--;
1076 }
1077 ;
1078
1079 pointer: /* C99 6.7.5 */
1080 asterisk type_qualifier_list_opt {
1081 $$ = merge_qualified_pointer($1, $2);
1082 }
1083 | asterisk type_qualifier_list_opt pointer {
1084 $$ = merge_qualified_pointer($1, $2);
1085 $$ = merge_qualified_pointer($$, $3);
1086 }
1087 ;
1088
1089 asterisk:
1090 T_ASTERISK {
1091 $$ = xcalloc(1, sizeof(*$$));
1092 $$->p_pointer = true;
1093 }
1094 ;
1095
1096 type_qualifier_list_opt:
1097 /* empty */ {
1098 $$ = NULL;
1099 }
1100 | type_qualifier_list
1101 ;
1102
1103 type_qualifier_list: /* C99 6.7.5 */
1104 type_qualifier
1105 | type_qualifier_list type_qualifier {
1106 $$ = merge_qualified_pointer($1, $2);
1107 }
1108 ;
1109
1110 type_qualifier:
1111 T_QUAL {
1112 $$ = xcalloc(1, sizeof(*$$));
1113 if ($1 == CONST) {
1114 $$->p_const = true;
1115 } else if ($1 == VOLATILE) {
1116 $$->p_volatile = true;
1117 } else {
1118 lint_assert($1 == RESTRICT || $1 == THREAD);
1119 }
1120 }
1121 ;
1122
1123 align_as: /* See alignment-specifier in C11 6.7.5 */
1124 typespec
1125 | constant_expr
1126 ;
1127
1128 param_list:
1129 id_list_lparen identifier_list T_RPAREN {
1130 $$ = $2;
1131 }
1132 | abstract_decl_param_list
1133 ;
1134
1135 id_list_lparen:
1136 T_LPAREN {
1137 block_level++;
1138 begin_declaration_level(PROTO_ARG);
1139 }
1140 ;
1141
1142 identifier_list:
1143 T_NAME {
1144 $$ = old_style_function_name(getsym($1));
1145 }
1146 | identifier_list T_COMMA T_NAME {
1147 $$ = lnklst($1, old_style_function_name(getsym($3)));
1148 }
1149 | identifier_list error
1150 ;
1151
1152 abstract_decl_param_list:
1153 abstract_decl_lparen T_RPAREN type_attribute_opt {
1154 $$ = NULL;
1155 }
1156 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN type_attribute_opt {
1157 dcs->d_proto = true;
1158 $$ = $2;
1159 }
1160 | abstract_decl_lparen error T_RPAREN type_attribute_opt {
1161 $$ = NULL;
1162 }
1163 ;
1164
1165 abstract_decl_lparen:
1166 T_LPAREN {
1167 block_level++;
1168 begin_declaration_level(PROTO_ARG);
1169 }
1170 ;
1171
1172 vararg_parameter_type_list:
1173 parameter_type_list
1174 | parameter_type_list T_COMMA T_ELLIPSIS {
1175 dcs->d_vararg = true;
1176 $$ = $1;
1177 }
1178 | T_ELLIPSIS {
1179 if (sflag) {
1180 /* ANSI C requires formal parameter before '...' */
1181 error(84);
1182 } else if (!tflag) {
1183 /* ANSI C requires formal parameter before '...' */
1184 warning(84);
1185 }
1186 dcs->d_vararg = true;
1187 $$ = NULL;
1188 }
1189 ;
1190
1191 parameter_type_list:
1192 parameter_declaration
1193 | parameter_type_list T_COMMA parameter_declaration {
1194 $$ = lnklst($1, $3);
1195 }
1196 ;
1197
1198 /* XXX: C99 6.7.5 defines the same name, but it looks completely different. */
1199 parameter_declaration:
1200 declmods deftyp {
1201 $$ = declare_argument(abstract_name(), false);
1202 }
1203 | declaration_specifiers deftyp {
1204 $$ = declare_argument(abstract_name(), false);
1205 }
1206 | declmods deftyp notype_param_decl {
1207 $$ = declare_argument($3, false);
1208 }
1209 | declaration_specifiers deftyp type_param_decl {
1210 $$ = declare_argument($3, false);
1211 }
1212 | declmods deftyp abstract_declarator {
1213 $$ = declare_argument($3, false);
1214 }
1215 | declaration_specifiers deftyp abstract_declarator {
1216 $$ = declare_argument($3, false);
1217 }
1218 ;
1219
1220 asm_or_symbolrename_opt: /* expect only one */
1221 /* empty */ {
1222 $$ = NULL;
1223 }
1224 | T_ASM T_LPAREN T_STRING T_RPAREN {
1225 freeyyv(&$3, T_STRING);
1226 $$ = NULL;
1227 }
1228 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN {
1229 $$ = $3;
1230 }
1231 ;
1232
1233 initializer: /* C99 6.7.8 "Initialization" */
1234 expr %prec T_COMMA {
1235 init_expr($1);
1236 }
1237 | init_lbrace init_rbrace {
1238 /* XXX: Empty braces are not covered by C99 6.7.8. */
1239 }
1240 | init_lbrace initializer_list comma_opt init_rbrace
1241 | error
1242 ;
1243
1244 initializer_list: /* C99 6.7.8 "Initialization" */
1245 initializer_list_item
1246 | initializer_list T_COMMA initializer_list_item
1247 ;
1248
1249 initializer_list_item:
1250 designation initializer
1251 | initializer
1252 ;
1253
1254 designation: /* C99 6.7.8 "Initialization" */
1255 designator_list T_ASSIGN
1256 | identifier T_COLON {
1257 /* GCC style struct or union member name in initializer */
1258 gnuism(315);
1259 add_designator_member($1);
1260 }
1261 ;
1262
1263 designator_list: /* C99 6.7.8 "Initialization" */
1264 designator
1265 | designator_list designator
1266 ;
1267
1268 designator: /* C99 6.7.8 "Initialization" */
1269 T_LBRACK range T_RBRACK {
1270 add_designator_subscript($2);
1271 if (!Sflag)
1272 /* array initializer with des.s is a C9X feature */
1273 warning(321);
1274 }
1275 | T_POINT identifier {
1276 if (!Sflag)
1277 /* struct or union member name in initializer is ... */
1278 warning(313);
1279 add_designator_member($2);
1280 }
1281 ;
1282
1283 range:
1284 constant_expr {
1285 $$.lo = to_int_constant($1, true);
1286 $$.hi = $$.lo;
1287 }
1288 | constant_expr T_ELLIPSIS constant_expr {
1289 $$.lo = to_int_constant($1, true);
1290 $$.hi = to_int_constant($3, true);
1291 /* initialization with '[a...b]' is a GCC extension */
1292 gnuism(340);
1293 }
1294 ;
1295
1296 init_lbrace:
1297 T_LBRACE {
1298 init_lbrace();
1299 }
1300 ;
1301
1302 init_rbrace:
1303 T_RBRACE {
1304 init_rbrace();
1305 }
1306 ;
1307
1308 type_name: /* C99 6.7.6 */
1309 {
1310 begin_declaration_level(ABSTRACT);
1311 } abstract_declaration {
1312 end_declaration_level();
1313 $$ = $2->s_type;
1314 }
1315 ;
1316
1317 abstract_declaration:
1318 noclass_declmods deftyp {
1319 $$ = declare_1_abstract(abstract_name());
1320 }
1321 | noclass_declspecs deftyp {
1322 $$ = declare_1_abstract(abstract_name());
1323 }
1324 | noclass_declmods deftyp abstract_declarator {
1325 $$ = declare_1_abstract($3);
1326 }
1327 | noclass_declspecs deftyp abstract_declarator {
1328 $$ = declare_1_abstract($3);
1329 }
1330 ;
1331
1332 abstract_declarator: /* C99 6.7.6 */
1333 pointer {
1334 $$ = add_pointer(abstract_name(), $1);
1335 }
1336 | direct_abstract_declarator
1337 | pointer direct_abstract_declarator {
1338 $$ = add_pointer($2, $1);
1339 }
1340 | T_TYPEOF term { /* GCC extension */
1341 $$ = mktempsym($2->tn_type);
1342 }
1343 ;
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 expr_statement
1406 | non_expr_statement
1407 ;
1408
1409 labeled_statement: /* C99 6.8.1 */
1410 label type_attribute_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 compound_statement_rbrace
1435 | compound_statement_lbrace block_item_list 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:
1457 block_item
1458 | block_item_list block_item {
1459 if (!Sflag && $1 && !$2)
1460 /* declarations after statements is a C99 feature */
1461 c99ism(327);
1462 $$ = $1 || $2;
1463 }
1464 ;
1465
1466 block_item:
1467 statement {
1468 $$ = true;
1469 restore_warning_flags();
1470 }
1471 | declaration {
1472 $$ = false;
1473 restore_warning_flags();
1474 }
1475 ;
1476
1477 expr_statement:
1478 expr T_SEMI {
1479 expr($1, false, false, false, false);
1480 seen_fallthrough = false;
1481 }
1482 | T_SEMI {
1483 seen_fallthrough = false;
1484 }
1485 ;
1486
1487 selection_statement: /* C99 6.8.4 */
1488 if_without_else {
1489 save_warning_flags();
1490 if2();
1491 if3(false);
1492 }
1493 | if_without_else T_ELSE {
1494 save_warning_flags();
1495 if2();
1496 } statement {
1497 clear_warning_flags();
1498 if3(true);
1499 }
1500 | if_without_else T_ELSE error {
1501 clear_warning_flags();
1502 if3(false);
1503 }
1504 | switch_expr statement {
1505 clear_warning_flags();
1506 switch2();
1507 }
1508 | switch_expr error {
1509 clear_warning_flags();
1510 switch2();
1511 }
1512 ;
1513
1514 if_without_else:
1515 if_expr statement
1516 | if_expr error
1517 ;
1518
1519 if_expr:
1520 T_IF T_LPAREN expr T_RPAREN {
1521 if1($3);
1522 clear_warning_flags();
1523 }
1524 ;
1525
1526 switch_expr:
1527 T_SWITCH T_LPAREN expr T_RPAREN {
1528 switch1($3);
1529 clear_warning_flags();
1530 }
1531 ;
1532
1533 generic_selection: /* C11 6.5.1.1 */
1534 T_GENERIC T_LPAREN assignment_expression T_COMMA
1535 generic_assoc_list T_RPAREN {
1536 /* generic selection requires C11 or later */
1537 c11ism(345);
1538 $$ = build_generic_selection($3, $5);
1539 }
1540 ;
1541
1542 generic_assoc_list: /* C11 6.5.1.1 */
1543 generic_association
1544 | generic_assoc_list T_COMMA generic_association {
1545 $3->ga_prev = $1;
1546 $$ = $3;
1547 }
1548 ;
1549
1550 generic_association: /* C11 6.5.1.1 */
1551 type_name T_COLON assignment_expression {
1552 $$ = getblk(sizeof(*$$));
1553 $$->ga_arg = $1;
1554 $$->ga_result = $3;
1555 }
1556 | T_DEFAULT T_COLON assignment_expression {
1557 $$ = getblk(sizeof(*$$));
1558 $$->ga_arg = NULL;
1559 $$->ga_result = $3;
1560 }
1561 ;
1562
1563 do_statement: /* C99 6.8.5 */
1564 do statement {
1565 clear_warning_flags();
1566 }
1567 ;
1568
1569 iteration_statement: /* C99 6.8.5 */
1570 while_expr statement {
1571 clear_warning_flags();
1572 while2();
1573 }
1574 | while_expr error {
1575 clear_warning_flags();
1576 while2();
1577 }
1578 | do_statement do_while_expr {
1579 do2($2);
1580 seen_fallthrough = false;
1581 }
1582 | do error {
1583 clear_warning_flags();
1584 do2(NULL);
1585 }
1586 | for_exprs statement {
1587 clear_warning_flags();
1588 for2();
1589 end_declaration_level();
1590 block_level--;
1591 }
1592 | for_exprs error {
1593 clear_warning_flags();
1594 for2();
1595 end_declaration_level();
1596 block_level--;
1597 }
1598 ;
1599
1600 while_expr:
1601 T_WHILE T_LPAREN expr T_RPAREN {
1602 while1($3);
1603 clear_warning_flags();
1604 }
1605 ;
1606
1607 do:
1608 T_DO {
1609 do1();
1610 }
1611 ;
1612
1613 do_while_expr:
1614 T_WHILE T_LPAREN expr T_RPAREN T_SEMI {
1615 $$ = $3;
1616 }
1617 ;
1618
1619 for_start:
1620 T_FOR T_LPAREN {
1621 begin_declaration_level(AUTO);
1622 block_level++;
1623 }
1624 ;
1625
1626 for_exprs:
1627 for_start declaration_specifiers deftyp notype_init_decls T_SEMI
1628 expr_opt T_SEMI expr_opt T_RPAREN {
1629 /* variable declaration in for loop */
1630 c99ism(325);
1631 for1(NULL, $6, $8);
1632 clear_warning_flags();
1633 }
1634 | for_start expr_opt T_SEMI expr_opt T_SEMI expr_opt T_RPAREN {
1635 for1($2, $4, $6);
1636 clear_warning_flags();
1637 }
1638 ;
1639
1640 expr_opt:
1641 /* empty */ {
1642 $$ = NULL;
1643 }
1644 | expr
1645 ;
1646
1647 jump_statement: /* C99 6.8.6 */
1648 goto identifier T_SEMI {
1649 do_goto(getsym($2));
1650 }
1651 | goto error T_SEMI {
1652 symtyp = FVFT;
1653 }
1654 | T_CONTINUE T_SEMI {
1655 do_continue();
1656 }
1657 | T_BREAK T_SEMI {
1658 do_break();
1659 }
1660 | T_RETURN T_SEMI {
1661 do_return(NULL);
1662 }
1663 | T_RETURN expr T_SEMI {
1664 do_return($2);
1665 }
1666 ;
1667
1668 goto:
1669 T_GOTO {
1670 symtyp = FLABEL;
1671 }
1672 ;
1673
1674 asm_statement:
1675 T_ASM T_LPAREN read_until_rparen T_SEMI {
1676 setasm();
1677 }
1678 | T_ASM T_QUAL T_LPAREN read_until_rparen T_SEMI {
1679 setasm();
1680 }
1681 | T_ASM error
1682 ;
1683
1684 read_until_rparen:
1685 /* empty */ {
1686 ignore_up_to_rparen();
1687 }
1688 ;
1689
1690 constant_expr_list_opt:
1691 /* empty */
1692 | constant_expr_list
1693 ;
1694
1695 constant_expr_list:
1696 constant_expr
1697 | constant_expr_list T_COMMA constant_expr
1698 ;
1699
1700 constant_expr: /* C99 6.6 */
1701 expr %prec T_ASSIGN
1702 ;
1703
1704 expr:
1705 expr T_ASTERISK expr {
1706 $$ = build(MULT, $1, $3);
1707 }
1708 | expr T_MULTIPLICATIVE expr {
1709 $$ = build($2, $1, $3);
1710 }
1711 | expr T_ADDITIVE expr {
1712 $$ = build($2, $1, $3);
1713 }
1714 | expr T_SHIFT expr {
1715 $$ = build($2, $1, $3);
1716 }
1717 | expr T_RELATIONAL expr {
1718 $$ = build($2, $1, $3);
1719 }
1720 | expr T_EQUALITY expr {
1721 $$ = build($2, $1, $3);
1722 }
1723 | expr T_AMPER expr {
1724 $$ = build(BITAND, $1, $3);
1725 }
1726 | expr T_BITXOR expr {
1727 $$ = build(BITXOR, $1, $3);
1728 }
1729 | expr T_BITOR expr {
1730 $$ = build(BITOR, $1, $3);
1731 }
1732 | expr T_LOGAND expr {
1733 $$ = build(LOGAND, $1, $3);
1734 }
1735 | expr T_LOGOR expr {
1736 $$ = build(LOGOR, $1, $3);
1737 }
1738 | expr T_QUEST expr T_COLON expr {
1739 $$ = build(QUEST, $1, build(COLON, $3, $5));
1740 }
1741 | expr T_ASSIGN expr {
1742 $$ = build(ASSIGN, $1, $3);
1743 }
1744 | expr T_OPASSIGN expr {
1745 $$ = build($2, $1, $3);
1746 }
1747 | expr T_COMMA expr {
1748 $$ = build(COMMA, $1, $3);
1749 }
1750 | term
1751 | generic_selection
1752 ;
1753
1754 assignment_expression: /* C99 6.5.16 */
1755 expr %prec T_ASSIGN
1756 ;
1757
1758 term:
1759 T_NAME {
1760 /* XXX really necessary? */
1761 if (yychar < 0)
1762 yychar = yylex();
1763 $$ = new_name_node(getsym($1), yychar);
1764 }
1765 | string {
1766 $$ = new_string_node($1);
1767 }
1768 | T_CON {
1769 $$ = expr_new_constant(gettyp($1->v_tspec), $1);
1770 }
1771 | T_LPAREN expr T_RPAREN {
1772 if ($2 != NULL)
1773 $2->tn_parenthesized = true;
1774 $$ = $2;
1775 }
1776 | T_LPAREN compound_statement_lbrace gcc_statement_expr_list {
1777 block_level--;
1778 mem_block_level--;
1779 begin_initialization(mktempsym(dup_type($3->tn_type)));
1780 mem_block_level++;
1781 block_level++;
1782 /* ({ }) is a GCC extension */
1783 gnuism(320);
1784 } compound_statement_rbrace T_RPAREN {
1785 $$ = new_name_node(*current_initsym(), 0);
1786 end_initialization();
1787 }
1788 | term T_INCDEC {
1789 $$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL);
1790 }
1791 | T_INCDEC term {
1792 $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL);
1793 }
1794 | T_ASTERISK term {
1795 $$ = build(INDIR, $2, NULL);
1796 }
1797 | T_AMPER term {
1798 $$ = build(ADDR, $2, NULL);
1799 }
1800 | T_UNARY term {
1801 $$ = build($1, $2, NULL);
1802 }
1803 | T_ADDITIVE term {
1804 if (tflag && $1 == PLUS) {
1805 /* unary + is illegal in traditional C */
1806 warning(100);
1807 }
1808 $$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL);
1809 }
1810 | term T_LBRACK expr T_RBRACK {
1811 $$ = build(INDIR, build(PLUS, $1, $3), NULL);
1812 }
1813 | term T_LPAREN T_RPAREN {
1814 $$ = new_function_call_node($1, NULL);
1815 }
1816 | term T_LPAREN func_arg_list T_RPAREN {
1817 $$ = new_function_call_node($1, $3);
1818 }
1819 | term point_or_arrow T_NAME {
1820 if ($1 != NULL) {
1821 sym_t *msym;
1822 /*
1823 * XXX struct_or_union_member should be integrated
1824 * in build()
1825 */
1826 if ($2 == ARROW) {
1827 /*
1828 * must do this before struct_or_union_member
1829 * is called
1830 */
1831 $1 = cconv($1);
1832 }
1833 msym = struct_or_union_member($1, $2, getsym($3));
1834 $$ = build($2, $1, new_name_node(msym, 0));
1835 } else {
1836 $$ = NULL;
1837 }
1838 }
1839 | T_REAL term {
1840 $$ = build(REAL, $2, NULL);
1841 }
1842 | T_IMAG term {
1843 $$ = build(IMAG, $2, NULL);
1844 }
1845 | T_EXTENSION term {
1846 $$ = $2;
1847 }
1848 | T_REAL T_LPAREN term T_RPAREN {
1849 $$ = build(REAL, $3, NULL);
1850 }
1851 | T_IMAG T_LPAREN term T_RPAREN {
1852 $$ = build(IMAG, $3, NULL);
1853 }
1854 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA identifier T_RPAREN {
1855 symtyp = FMEMBER;
1856 $$ = build_offsetof($3, getsym($5));
1857 }
1858 | T_SIZEOF term {
1859 $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type);
1860 if ($$ != NULL)
1861 check_expr_misc($2, false, false, false, false, false, true);
1862 }
1863 | T_SIZEOF T_LPAREN type_name T_RPAREN %prec T_SIZEOF {
1864 $$ = build_sizeof($3);
1865 }
1866 | T_ALIGNOF T_LPAREN type_name T_RPAREN {
1867 $$ = build_alignof($3);
1868 }
1869 | T_LPAREN type_name T_RPAREN term %prec T_UNARY {
1870 $$ = cast($4, $2);
1871 }
1872 | T_LPAREN type_name T_RPAREN { /* C99 6.5.2.5 "Compound literals" */
1873 sym_t *tmp = mktempsym($2);
1874 begin_initialization(tmp);
1875 cgram_declare(tmp, true, NULL);
1876 } init_lbrace initializer_list comma_opt init_rbrace {
1877 if (!Sflag)
1878 /* compound literals are a C9X/GCC extension */
1879 gnuism(319);
1880 $$ = new_name_node(*current_initsym(), 0);
1881 end_initialization();
1882 }
1883 ;
1884
1885 /*
1886 * The inner part of a GCC statement-expression of the form ({ ... }).
1887 *
1888 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
1889 */
1890 gcc_statement_expr_list:
1891 gcc_statement_expr_item
1892 | gcc_statement_expr_list gcc_statement_expr_item {
1893 $$ = $2;
1894 }
1895 ;
1896
1897 gcc_statement_expr_item:
1898 declaration {
1899 clear_warning_flags();
1900 $$ = NULL;
1901 }
1902 | non_expr_statement {
1903 $$ = expr_zalloc_tnode();
1904 $$->tn_type = gettyp(VOID);
1905 }
1906 | expr T_SEMI {
1907 if ($1 == NULL) { /* in case of syntax errors */
1908 $$ = expr_zalloc_tnode();
1909 $$->tn_type = gettyp(VOID);
1910 } else {
1911 /* XXX: do that only on the last name */
1912 if ($1->tn_op == NAME)
1913 $1->tn_sym->s_used = true;
1914 $$ = $1;
1915 expr($1, false, false, false, false);
1916 seen_fallthrough = false;
1917 }
1918 }
1919 ;
1920
1921 string:
1922 T_STRING
1923 | T_STRING string2 {
1924 $$ = cat_strings($1, $2);
1925 }
1926 ;
1927
1928 string2:
1929 T_STRING {
1930 if (tflag) {
1931 /* concatenated strings are illegal in traditional C */
1932 warning(219);
1933 }
1934 $$ = $1;
1935 }
1936 | string2 T_STRING {
1937 $$ = cat_strings($1, $2);
1938 }
1939 ;
1940
1941 func_arg_list:
1942 expr %prec T_COMMA {
1943 $$ = new_function_argument_node(NULL, $1);
1944 }
1945 | func_arg_list T_COMMA expr {
1946 $$ = new_function_argument_node($1, $3);
1947 }
1948 ;
1949
1950 point_or_arrow:
1951 T_POINT {
1952 symtyp = FMEMBER;
1953 $$ = POINT;
1954 }
1955 | T_ARROW {
1956 symtyp = FMEMBER;
1957 $$ = ARROW;
1958 }
1959 ;
1960
1961 identifier: /* C99 6.4.2.1 */
1962 T_NAME {
1963 $$ = $1;
1964 cgram_debug("name '%s'", $$->sb_name);
1965 }
1966 | T_TYPENAME {
1967 $$ = $1;
1968 cgram_debug("typename '%s'", $$->sb_name);
1969 }
1970 ;
1971
1972 comma_opt:
1973 /* empty */
1974 | T_COMMA
1975 ;
1976
1977 /* GCC extensions */
1978
1979 /* TODO: split into type_attribute and gcc_attribute */
1980
1981 type_attribute_list:
1982 type_attribute
1983 | type_attribute_list type_attribute
1984 ;
1985
1986 type_attribute:
1987 T_ATTRIBUTE T_LPAREN T_LPAREN {
1988 attron = true;
1989 } type_attribute_spec_list {
1990 attron = false;
1991 } T_RPAREN T_RPAREN
1992 | T_ALIGNAS T_LPAREN align_as T_RPAREN {
1993 }
1994 | T_PACKED {
1995 addpacked();
1996 }
1997 | T_NORETURN {
1998 }
1999 ;
2000
2001 /* https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html */
2002 type_attribute_spec_list:
2003 type_attribute_spec
2004 | type_attribute_spec_list T_COMMA type_attribute_spec
2005 ;
2006
2007 type_attribute_spec:
2008 /* empty */
2009 | T_AT_ALWAYS_INLINE
2010 | T_AT_ALIAS T_LPAREN string T_RPAREN
2011 | T_AT_ALIGNED T_LPAREN constant_expr T_RPAREN
2012 | T_AT_ALIGNED
2013 | T_AT_ALLOC_SIZE T_LPAREN constant_expr T_COMMA constant_expr T_RPAREN
2014 | T_AT_ALLOC_SIZE T_LPAREN constant_expr T_RPAREN
2015 | T_AT_BOUNDED T_LPAREN type_attribute_bounded_type
2016 T_COMMA constant_expr T_COMMA constant_expr T_RPAREN
2017 | T_AT_COLD
2018 | T_AT_COMMON
2019 | T_AT_CONSTRUCTOR T_LPAREN constant_expr T_RPAREN
2020 | T_AT_CONSTRUCTOR
2021 | T_AT_DEPRECATED T_LPAREN string T_RPAREN
2022 | T_AT_DEPRECATED
2023 | T_AT_DESTRUCTOR T_LPAREN constant_expr T_RPAREN
2024 | T_AT_DESTRUCTOR
2025 | T_AT_FALLTHROUGH {
2026 fallthru(1);
2027 }
2028 | T_AT_FORMAT T_LPAREN type_attribute_format_type T_COMMA
2029 constant_expr T_COMMA constant_expr T_RPAREN
2030 | T_AT_FORMAT_ARG T_LPAREN constant_expr T_RPAREN
2031 | T_AT_GNU_INLINE
2032 | T_AT_MALLOC
2033 | T_AT_MAY_ALIAS
2034 | T_AT_MODE T_LPAREN T_NAME T_RPAREN
2035 | T_AT_NOINLINE
2036 | T_AT_NONNULL T_LPAREN constant_expr_list_opt T_RPAREN
2037 | T_AT_NONNULL
2038 | T_AT_NONSTRING
2039 | T_AT_NORETURN
2040 | T_AT_NOTHROW
2041 | T_AT_NO_INSTRUMENT_FUNCTION
2042 | T_AT_OPTIMIZE T_LPAREN string T_RPAREN
2043 | T_AT_PACKED {
2044 addpacked();
2045 }
2046 | T_AT_PCS T_LPAREN string T_RPAREN
2047 | T_AT_PURE
2048 | T_AT_RETURNS_TWICE
2049 | T_AT_SECTION T_LPAREN string T_RPAREN
2050 | T_AT_SENTINEL T_LPAREN constant_expr T_RPAREN
2051 | T_AT_SENTINEL
2052 | T_AT_TLS_MODEL T_LPAREN string T_RPAREN
2053 | T_AT_TUNION
2054 | T_AT_UNUSED {
2055 add_attr_used();
2056 }
2057 | T_AT_USED {
2058 add_attr_used();
2059 }
2060 | T_AT_VISIBILITY T_LPAREN constant_expr T_RPAREN
2061 | T_AT_WARN_UNUSED_RESULT
2062 | T_AT_WEAK
2063 | T_QUAL {
2064 if ($1 != CONST)
2065 yyerror("Bad attribute");
2066 }
2067 ;
2068
2069 type_attribute_bounded_type:
2070 T_AT_MINBYTES
2071 | T_AT_STRING
2072 | T_AT_BUFFER
2073 ;
2074
2075 type_attribute_format_type:
2076 T_AT_FORMAT_GNU_PRINTF
2077 | T_AT_FORMAT_PRINTF
2078 | T_AT_FORMAT_SCANF
2079 | T_AT_FORMAT_STRFMON
2080 | T_AT_FORMAT_STRFTIME
2081 | T_AT_FORMAT_SYSLOG
2082 ;
2083
2084 %%
2085
2086 /* ARGSUSED */
2087 int
2088 yyerror(const char *msg)
2089 {
2090 /* syntax error '%s' */
2091 error(249, yytext);
2092 if (++sytxerr >= 5)
2093 norecover();
2094 return 0;
2095 }
2096
2097 static void
2098 cgram_declare(sym_t *decl, bool initflg, sbuf_t *renaming)
2099 {
2100 declare(decl, initflg, renaming);
2101 if (renaming != NULL)
2102 freeyyv(&renaming, T_NAME);
2103 }
2104
2105 /*
2106 * Discard all input tokens up to and including the next
2107 * unmatched right paren
2108 */
2109 static void
2110 ignore_up_to_rparen(void)
2111 {
2112 int level;
2113
2114 if (yychar < 0)
2115 yychar = yylex();
2116 freeyyv(&yylval, yychar);
2117
2118 level = 1;
2119 while (yychar != T_RPAREN || --level > 0) {
2120 if (yychar == T_LPAREN) {
2121 level++;
2122 } else if (yychar <= 0) {
2123 break;
2124 }
2125 freeyyv(&yylval, yychar = yylex());
2126 }
2127
2128 yyclearin;
2129 }
2130
2131 static sym_t *
2132 symbolrename(sym_t *s, sbuf_t *sb)
2133 {
2134 if (sb != NULL)
2135 s->s_rename = sb->sb_name;
2136 return s;
2137 }
2138