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