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