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