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