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