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