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