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