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