cgram.y revision 1.218 1 %{
2 /* $NetBSD: cgram.y,v 1.218 2021/04/14 22:08:28 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.218 2021/04/14 22:08:28 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 opt_type_qualifier_list:
1136 /* empty */
1137 | type_qualifier_list
1138 ;
1139
1140 array_size:
1141 opt_type_qualifier_list T_SCLASS constant_expr {
1142 /* C11 6.7.6.3p7 */
1143 if ($2 != STATIC)
1144 yyerror("Bad attribute");
1145 /* static array size is a C11 extension */
1146 c11ism(343);
1147 $$ = $3;
1148 }
1149 | constant_expr {
1150 $$ = $1;
1151 }
1152 ;
1153
1154 direct_param_decl:
1155 identifier type_attribute_list {
1156 $$ = declarator_name(getsym($1));
1157 }
1158 | identifier {
1159 $$ = declarator_name(getsym($1));
1160 }
1161 | T_LPAREN notype_param_decl T_RPAREN {
1162 $$ = $2;
1163 }
1164 | direct_param_decl T_LBRACK T_RBRACK {
1165 $$ = add_array($1, false, 0);
1166 }
1167 | direct_param_decl T_LBRACK array_size T_RBRACK {
1168 $$ = add_array($1, true, to_int_constant($3, false));
1169 }
1170 | direct_param_decl param_list opt_asm_or_symbolrename {
1171 $$ = add_function(symbolrename($1, $3), $2);
1172 end_declaration_level();
1173 block_level--;
1174 }
1175 ;
1176
1177 notype_param_decl:
1178 direct_notype_param_decl {
1179 $$ = $1;
1180 }
1181 | pointer direct_notype_param_decl {
1182 $$ = add_pointer($2, $1);
1183 }
1184 ;
1185
1186 direct_notype_param_decl:
1187 identifier {
1188 $$ = declarator_name(getsym($1));
1189 }
1190 | T_LPAREN notype_param_decl T_RPAREN {
1191 $$ = $2;
1192 }
1193 | direct_notype_param_decl T_LBRACK T_RBRACK {
1194 $$ = add_array($1, false, 0);
1195 }
1196 | direct_notype_param_decl T_LBRACK array_size T_RBRACK {
1197 $$ = add_array($1, true, to_int_constant($3, false));
1198 }
1199 | direct_notype_param_decl param_list opt_asm_or_symbolrename {
1200 $$ = add_function(symbolrename($1, $3), $2);
1201 end_declaration_level();
1202 block_level--;
1203 }
1204 ;
1205
1206 pointer:
1207 asterisk {
1208 $$ = $1;
1209 }
1210 | asterisk type_qualifier_list {
1211 $$ = merge_pointers_and_qualifiers($1, $2);
1212 }
1213 | asterisk pointer {
1214 $$ = merge_pointers_and_qualifiers($1, $2);
1215 }
1216 | asterisk type_qualifier_list pointer {
1217 $$ = merge_pointers_and_qualifiers($1, $2);
1218 $$ = merge_pointers_and_qualifiers($$, $3);
1219 }
1220 ;
1221
1222 asterisk:
1223 T_ASTERISK {
1224 $$ = xcalloc(1, sizeof(*$$));
1225 $$->p_pcnt = 1;
1226 }
1227 ;
1228
1229 type_qualifier_list:
1230 type_qualifier {
1231 $$ = $1;
1232 }
1233 | type_qualifier_list type_qualifier {
1234 $$ = merge_pointers_and_qualifiers($1, $2);
1235 }
1236 ;
1237
1238 type_qualifier:
1239 T_QUAL {
1240 $$ = xcalloc(1, sizeof(*$$));
1241 if ($1 == CONST) {
1242 $$->p_const = true;
1243 } else if ($1 == VOLATILE) {
1244 $$->p_volatile = true;
1245 } else {
1246 lint_assert($1 == RESTRICT || $1 == THREAD);
1247 }
1248 }
1249 ;
1250
1251 param_list:
1252 id_list_lparen identifier_list T_RPAREN {
1253 $$ = $2;
1254 }
1255 | abstract_decl_param_list {
1256 $$ = $1;
1257 }
1258 ;
1259
1260 id_list_lparen:
1261 T_LPAREN {
1262 block_level++;
1263 begin_declaration_level(PROTO_ARG);
1264 }
1265 ;
1266
1267 identifier_list:
1268 T_NAME {
1269 $$ = old_style_function_name(getsym($1));
1270 }
1271 | identifier_list T_COMMA T_NAME {
1272 $$ = lnklst($1, old_style_function_name(getsym($3)));
1273 }
1274 | identifier_list error {
1275 $$ = $1;
1276 }
1277 ;
1278
1279 abstract_decl_param_list:
1280 abstract_decl_lparen T_RPAREN opt_type_attribute {
1281 $$ = NULL;
1282 }
1283 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN opt_type_attribute {
1284 dcs->d_proto = true;
1285 $$ = $2;
1286 }
1287 | abstract_decl_lparen error T_RPAREN opt_type_attribute {
1288 $$ = NULL;
1289 }
1290 ;
1291
1292 abstract_decl_lparen:
1293 T_LPAREN {
1294 block_level++;
1295 begin_declaration_level(PROTO_ARG);
1296 }
1297 ;
1298
1299 vararg_parameter_type_list:
1300 parameter_type_list {
1301 $$ = $1;
1302 }
1303 | parameter_type_list T_COMMA T_ELLIPSIS {
1304 dcs->d_vararg = true;
1305 $$ = $1;
1306 }
1307 | T_ELLIPSIS {
1308 if (sflag) {
1309 /* ANSI C requires formal parameter before '...' */
1310 error(84);
1311 } else if (!tflag) {
1312 /* ANSI C requires formal parameter before '...' */
1313 warning(84);
1314 }
1315 dcs->d_vararg = true;
1316 $$ = NULL;
1317 }
1318 ;
1319
1320 parameter_type_list:
1321 parameter_declaration {
1322 $$ = $1;
1323 }
1324 | parameter_type_list T_COMMA parameter_declaration {
1325 $$ = lnklst($1, $3);
1326 }
1327 ;
1328
1329 parameter_declaration:
1330 declmods deftyp {
1331 $$ = declare_argument(abstract_name(), false);
1332 }
1333 | declaration_specifiers deftyp {
1334 $$ = declare_argument(abstract_name(), false);
1335 }
1336 | declmods deftyp notype_param_decl {
1337 $$ = declare_argument($3, false);
1338 }
1339 /*
1340 * param_decl is needed because of following conflict:
1341 * "typedef int a; f(int (a));" could be parsed as
1342 * "function with argument a of type int", or
1343 * "function with an abstract argument of type function".
1344 * This grammar realizes the second case.
1345 */
1346 | declaration_specifiers deftyp param_decl {
1347 $$ = declare_argument($3, false);
1348 }
1349 | declmods deftyp abstract_decl {
1350 $$ = declare_argument($3, false);
1351 }
1352 | declaration_specifiers deftyp abstract_decl {
1353 $$ = declare_argument($3, false);
1354 }
1355 ;
1356
1357 opt_asm_or_symbolrename: /* expect only one */
1358 /* empty */ {
1359 $$ = NULL;
1360 }
1361 | T_ASM T_LPAREN T_STRING T_RPAREN {
1362 freeyyv(&$3, T_STRING);
1363 $$ = NULL;
1364 }
1365 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN {
1366 $$ = $3;
1367 }
1368 ;
1369
1370 initializer: /* C99 6.7.8 "Initialization" */
1371 expr %prec T_COMMA {
1372 init_expr($1);
1373 }
1374 | init_lbrace init_rbrace {
1375 /* XXX: Empty braces are not covered by C99 6.7.8. */
1376 }
1377 | init_lbrace initializer_list comma_opt init_rbrace
1378 | error
1379 ;
1380
1381 initializer_list: /* C99 6.7.8 "Initialization" */
1382 initializer_list_item
1383 | initializer_list T_COMMA initializer_list_item
1384 ;
1385
1386 initializer_list_item:
1387 designation initializer
1388 | initializer
1389 ;
1390
1391 range:
1392 constant_expr {
1393 $$.lo = to_int_constant($1, true);
1394 $$.hi = $$.lo;
1395 }
1396 | constant_expr T_ELLIPSIS constant_expr {
1397 $$.lo = to_int_constant($1, true);
1398 $$.hi = to_int_constant($3, true);
1399 /* initialization with '[a...b]' is a GNU extension */
1400 gnuism(340);
1401 }
1402 ;
1403
1404 designator: /* C99 6.7.8 "Initialization" */
1405 T_LBRACK range T_RBRACK {
1406 add_designator_subscript($2);
1407 if (!Sflag)
1408 /* array initializer with des.s is a C9X feature */
1409 warning(321);
1410 }
1411 | point identifier {
1412 if (!Sflag)
1413 /* struct or union member name in initializer is ... */
1414 warning(313);
1415 add_designator_member($2);
1416 }
1417 ;
1418
1419 designator_list: /* C99 6.7.8 "Initialization" */
1420 designator
1421 | designator_list designator
1422 ;
1423
1424 designation: /* C99 6.7.8 "Initialization" */
1425 designator_list T_ASSIGN
1426 | identifier T_COLON {
1427 /* GCC style struct or union member name in initializer */
1428 gnuism(315);
1429 add_designator_member($1);
1430 }
1431 ;
1432
1433 init_lbrace:
1434 T_LBRACE {
1435 init_lbrace();
1436 }
1437 ;
1438
1439 init_rbrace:
1440 T_RBRACE {
1441 init_rbrace();
1442 }
1443 ;
1444
1445 type_name:
1446 {
1447 begin_declaration_level(ABSTRACT);
1448 } abstract_declaration {
1449 end_declaration_level();
1450 $$ = $2->s_type;
1451 }
1452 ;
1453
1454 abstract_declaration:
1455 noclass_declmods deftyp {
1456 $$ = declare_1_abstract(abstract_name());
1457 }
1458 | noclass_declspecs deftyp {
1459 $$ = declare_1_abstract(abstract_name());
1460 }
1461 | noclass_declmods deftyp abstract_decl {
1462 $$ = declare_1_abstract($3);
1463 }
1464 | noclass_declspecs deftyp abstract_decl {
1465 $$ = declare_1_abstract($3);
1466 }
1467 ;
1468
1469 abstract_decl:
1470 pointer {
1471 $$ = add_pointer(abstract_name(), $1);
1472 }
1473 | direct_abstract_decl {
1474 $$ = $1;
1475 }
1476 | pointer direct_abstract_decl {
1477 $$ = add_pointer($2, $1);
1478 }
1479 | T_TYPEOF term {
1480 $$ = mktempsym($2->tn_type);
1481 }
1482 ;
1483
1484 direct_abstract_decl:
1485 T_LPAREN abstract_decl T_RPAREN {
1486 $$ = $2;
1487 }
1488 | T_LBRACK T_RBRACK {
1489 $$ = add_array(abstract_name(), false, 0);
1490 }
1491 | T_LBRACK array_size T_RBRACK {
1492 $$ = add_array(abstract_name(), true, to_int_constant($2, false));
1493 }
1494 | type_attribute direct_abstract_decl {
1495 $$ = $2;
1496 }
1497 | direct_abstract_decl T_LBRACK T_RBRACK {
1498 $$ = add_array($1, false, 0);
1499 }
1500 | direct_abstract_decl T_LBRACK array_size T_RBRACK {
1501 $$ = add_array($1, true, to_int_constant($3, false));
1502 }
1503 | abstract_decl_param_list opt_asm_or_symbolrename {
1504 $$ = add_function(symbolrename(abstract_name(), $2), $1);
1505 end_declaration_level();
1506 block_level--;
1507 }
1508 | direct_abstract_decl abstract_decl_param_list opt_asm_or_symbolrename {
1509 $$ = add_function(symbolrename($1, $3), $2);
1510 end_declaration_level();
1511 block_level--;
1512 }
1513 | direct_abstract_decl type_attribute_list
1514 ;
1515
1516 non_expr_statement:
1517 type_attribute T_SEMI
1518 | labeled_statement
1519 | compound_statement
1520 | selection_statement
1521 | iteration_statement
1522 | jump_statement {
1523 seen_fallthrough = false;
1524 }
1525 | asm_statement
1526 ;
1527
1528 statement: /* C99 6.8 */
1529 expr_statement
1530 | non_expr_statement
1531 ;
1532
1533 labeled_statement: /* C99 6.8.1 */
1534 label statement
1535 ;
1536
1537 label:
1538 T_NAME T_COLON {
1539 symtyp = FLABEL;
1540 named_label(getsym($1));
1541 }
1542 | T_CASE constant_expr T_COLON {
1543 case_label($2);
1544 seen_fallthrough = true;
1545 }
1546 | T_CASE constant_expr T_ELLIPSIS constant_expr T_COLON {
1547 /* XXX: We don't fill all cases */
1548 case_label($2);
1549 seen_fallthrough = true;
1550 }
1551 | T_DEFAULT T_COLON {
1552 default_label();
1553 seen_fallthrough = true;
1554 }
1555 ;
1556
1557 compound_statement: /* C99 6.8.2 */
1558 compound_statement_lbrace compound_statement_rbrace
1559 | compound_statement_lbrace block_item_list compound_statement_rbrace
1560 ;
1561
1562 compound_statement_lbrace:
1563 T_LBRACE {
1564 block_level++;
1565 mem_block_level++;
1566 begin_declaration_level(AUTO);
1567 }
1568 ;
1569
1570 compound_statement_rbrace:
1571 T_RBRACE {
1572 end_declaration_level();
1573 freeblk();
1574 mem_block_level--;
1575 block_level--;
1576 seen_fallthrough = false;
1577 }
1578 ;
1579
1580 block_item_list:
1581 block_item
1582 | block_item_list block_item {
1583 if (!Sflag && $1 && !$2)
1584 /* declarations after statements is a C99 feature */
1585 c99ism(327);
1586 }
1587 ;
1588
1589 block_item:
1590 statement {
1591 $$ = true;
1592 restore_warning_flags();
1593 }
1594 | declaration {
1595 $$ = false;
1596 restore_warning_flags();
1597 }
1598 ;
1599
1600 expr_statement:
1601 expr T_SEMI {
1602 expr($1, false, false, false, false);
1603 seen_fallthrough = false;
1604 }
1605 | T_SEMI {
1606 seen_fallthrough = false;
1607 }
1608 ;
1609
1610 /*
1611 * The following two productions are used to implement
1612 * ({ [[decl-list] stmt-list] }).
1613 * XXX: This is not well tested.
1614 */
1615 expr_statement_val:
1616 expr T_SEMI {
1617 /* XXX: We should really do that only on the last name */
1618 if ($1->tn_op == NAME)
1619 $1->tn_sym->s_used = true;
1620 $$ = $1;
1621 expr($1, false, false, false, false);
1622 seen_fallthrough = false;
1623 }
1624 | non_expr_statement {
1625 $$ = expr_zalloc_tnode();
1626 $$->tn_type = gettyp(VOID);
1627 }
1628 ;
1629
1630 expr_statement_list:
1631 expr_statement_val
1632 | expr_statement_list expr_statement_val {
1633 $$ = $2;
1634 }
1635 ;
1636
1637 selection_statement: /* C99 6.8.4 */
1638 if_without_else {
1639 save_warning_flags();
1640 if2();
1641 if3(false);
1642 }
1643 | if_without_else T_ELSE {
1644 save_warning_flags();
1645 if2();
1646 } statement {
1647 clear_warning_flags();
1648 if3(true);
1649 }
1650 | if_without_else T_ELSE error {
1651 clear_warning_flags();
1652 if3(false);
1653 }
1654 | switch_expr statement {
1655 clear_warning_flags();
1656 switch2();
1657 }
1658 | switch_expr error {
1659 clear_warning_flags();
1660 switch2();
1661 }
1662 ;
1663
1664 if_without_else:
1665 if_expr statement
1666 | if_expr error
1667 ;
1668
1669 if_expr:
1670 T_IF T_LPAREN expr T_RPAREN {
1671 if1($3);
1672 clear_warning_flags();
1673 }
1674 ;
1675
1676 switch_expr:
1677 T_SWITCH T_LPAREN expr T_RPAREN {
1678 switch1($3);
1679 clear_warning_flags();
1680 }
1681 ;
1682
1683 association:
1684 type_name T_COLON expr
1685 | T_DEFAULT T_COLON expr
1686 ;
1687
1688 association_list:
1689 association
1690 | association_list T_COMMA association
1691 ;
1692
1693 generic_expr:
1694 T_GENERIC T_LPAREN expr T_COMMA association_list T_RPAREN {
1695 $$ = $3;
1696 }
1697 ;
1698
1699 do_statement:
1700 do statement {
1701 clear_warning_flags();
1702 }
1703 ;
1704
1705 iteration_statement: /* C99 6.8.5 */
1706 while_expr statement {
1707 clear_warning_flags();
1708 while2();
1709 }
1710 | while_expr error {
1711 clear_warning_flags();
1712 while2();
1713 }
1714 | do_statement do_while_expr {
1715 do2($2);
1716 seen_fallthrough = false;
1717 }
1718 | do error {
1719 clear_warning_flags();
1720 do2(NULL);
1721 }
1722 | for_exprs statement {
1723 clear_warning_flags();
1724 for2();
1725 end_declaration_level();
1726 block_level--;
1727 }
1728 | for_exprs error {
1729 clear_warning_flags();
1730 for2();
1731 end_declaration_level();
1732 block_level--;
1733 }
1734 ;
1735
1736 while_expr:
1737 T_WHILE T_LPAREN expr T_RPAREN {
1738 while1($3);
1739 clear_warning_flags();
1740 }
1741 ;
1742
1743 do:
1744 T_DO {
1745 do1();
1746 }
1747 ;
1748
1749 do_while_expr:
1750 T_WHILE T_LPAREN expr T_RPAREN T_SEMI {
1751 $$ = $3;
1752 }
1753 ;
1754
1755 for_start:
1756 T_FOR T_LPAREN {
1757 begin_declaration_level(AUTO);
1758 block_level++;
1759 }
1760 ;
1761 for_exprs:
1762 for_start declaration_specifiers deftyp notype_init_decls T_SEMI
1763 opt_expr T_SEMI opt_expr T_RPAREN {
1764 /* variable declaration in for loop */
1765 c99ism(325);
1766 for1(NULL, $6, $8);
1767 clear_warning_flags();
1768 }
1769 | for_start opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPAREN {
1770 for1($2, $4, $6);
1771 clear_warning_flags();
1772 }
1773 ;
1774
1775 opt_expr:
1776 /* empty */ {
1777 $$ = NULL;
1778 }
1779 | expr {
1780 $$ = $1;
1781 }
1782 ;
1783
1784 jump_statement: /* C99 6.8.6 */
1785 goto identifier T_SEMI {
1786 do_goto(getsym($2));
1787 }
1788 | goto error T_SEMI {
1789 symtyp = FVFT;
1790 }
1791 | T_CONTINUE T_SEMI {
1792 do_continue();
1793 }
1794 | T_BREAK T_SEMI {
1795 do_break();
1796 }
1797 | T_RETURN T_SEMI {
1798 do_return(NULL);
1799 }
1800 | T_RETURN expr T_SEMI {
1801 do_return($2);
1802 }
1803 ;
1804
1805 goto:
1806 T_GOTO {
1807 symtyp = FLABEL;
1808 }
1809 ;
1810
1811 asm_statement:
1812 T_ASM T_LPAREN read_until_rparen T_SEMI {
1813 setasm();
1814 }
1815 | T_ASM T_QUAL T_LPAREN read_until_rparen T_SEMI {
1816 setasm();
1817 }
1818 | T_ASM error
1819 ;
1820
1821 read_until_rparen:
1822 /* empty */ {
1823 ignore_up_to_rparen();
1824 }
1825 ;
1826
1827 declaration_list:
1828 declaration {
1829 clear_warning_flags();
1830 }
1831 | declaration_list declaration {
1832 clear_warning_flags();
1833 }
1834 ;
1835
1836 constant_expr: /* C99 6.6 */
1837 expr %prec T_ASSIGN {
1838 $$ = $1;
1839 }
1840 ;
1841
1842 expr:
1843 expr T_ASTERISK expr {
1844 $$ = build(MULT, $1, $3);
1845 }
1846 | expr T_MULTIPLICATIVE expr {
1847 $$ = build($2, $1, $3);
1848 }
1849 | expr T_ADDITIVE expr {
1850 $$ = build($2, $1, $3);
1851 }
1852 | expr T_SHIFT expr {
1853 $$ = build($2, $1, $3);
1854 }
1855 | expr T_RELATIONAL expr {
1856 $$ = build($2, $1, $3);
1857 }
1858 | expr T_EQUALITY expr {
1859 $$ = build($2, $1, $3);
1860 }
1861 | expr T_AMPER expr {
1862 $$ = build(BITAND, $1, $3);
1863 }
1864 | expr T_BITXOR expr {
1865 $$ = build(BITXOR, $1, $3);
1866 }
1867 | expr T_BITOR expr {
1868 $$ = build(BITOR, $1, $3);
1869 }
1870 | expr T_LOGAND expr {
1871 $$ = build(LOGAND, $1, $3);
1872 }
1873 | expr T_LOGOR expr {
1874 $$ = build(LOGOR, $1, $3);
1875 }
1876 | expr T_QUEST expr T_COLON expr {
1877 $$ = build(QUEST, $1, build(COLON, $3, $5));
1878 }
1879 | expr T_ASSIGN expr {
1880 $$ = build(ASSIGN, $1, $3);
1881 }
1882 | expr T_OPASSIGN expr {
1883 $$ = build($2, $1, $3);
1884 }
1885 | expr T_COMMA expr {
1886 $$ = build(COMMA, $1, $3);
1887 }
1888 | term {
1889 $$ = $1;
1890 }
1891 | generic_expr {
1892 $$ = $1;
1893 }
1894 ;
1895
1896 term:
1897 T_NAME {
1898 /* XXX really necessary? */
1899 if (yychar < 0)
1900 yychar = yylex();
1901 $$ = new_name_node(getsym($1), yychar);
1902 }
1903 | string {
1904 $$ = new_string_node($1);
1905 }
1906 | T_CON {
1907 $$ = expr_new_constant(gettyp($1->v_tspec), $1);
1908 }
1909 | T_LPAREN expr T_RPAREN {
1910 if ($2 != NULL)
1911 $2->tn_parenthesized = true;
1912 $$ = $2;
1913 }
1914 | T_LPAREN compound_statement_lbrace declaration_list
1915 expr_statement_list {
1916 block_level--;
1917 mem_block_level--;
1918 begin_initialization(mktempsym(dup_type($4->tn_type)));
1919 mem_block_level++;
1920 block_level++;
1921 /* ({ }) is a GCC extension */
1922 gnuism(320);
1923 } compound_statement_rbrace T_RPAREN {
1924 $$ = new_name_node(*current_initsym(), 0);
1925 end_initialization();
1926 }
1927 | T_LPAREN compound_statement_lbrace expr_statement_list {
1928 block_level--;
1929 mem_block_level--;
1930 begin_initialization(mktempsym($3->tn_type));
1931 mem_block_level++;
1932 block_level++;
1933 /* ({ }) is a GCC extension */
1934 gnuism(320);
1935 } compound_statement_rbrace T_RPAREN {
1936 $$ = new_name_node(*current_initsym(), 0);
1937 end_initialization();
1938 }
1939 | term T_INCDEC {
1940 $$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL);
1941 }
1942 | T_INCDEC term {
1943 $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL);
1944 }
1945 | T_ASTERISK term {
1946 $$ = build(INDIR, $2, NULL);
1947 }
1948 | T_AMPER term {
1949 $$ = build(ADDR, $2, NULL);
1950 }
1951 | T_UNARY term {
1952 $$ = build($1, $2, NULL);
1953 }
1954 | T_ADDITIVE term {
1955 if (tflag && $1 == PLUS) {
1956 /* unary + is illegal in traditional C */
1957 warning(100);
1958 }
1959 $$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL);
1960 }
1961 | term T_LBRACK expr T_RBRACK {
1962 $$ = build(INDIR, build(PLUS, $1, $3), NULL);
1963 }
1964 | term T_LPAREN T_RPAREN {
1965 $$ = new_function_call_node($1, NULL);
1966 }
1967 | term T_LPAREN func_arg_list T_RPAREN {
1968 $$ = new_function_call_node($1, $3);
1969 }
1970 | term point_or_arrow T_NAME {
1971 if ($1 != NULL) {
1972 sym_t *msym;
1973 /*
1974 * XXX struct_or_union_member should be integrated
1975 * in build()
1976 */
1977 if ($2 == ARROW) {
1978 /*
1979 * must do this before struct_or_union_member
1980 * is called
1981 */
1982 $1 = cconv($1);
1983 }
1984 msym = struct_or_union_member($1, $2, getsym($3));
1985 $$ = build($2, $1, new_name_node(msym, 0));
1986 } else {
1987 $$ = NULL;
1988 }
1989 }
1990 | T_REAL term {
1991 $$ = build(REAL, $2, NULL);
1992 }
1993 | T_IMAG term {
1994 $$ = build(IMAG, $2, NULL);
1995 }
1996 | T_EXTENSION term {
1997 $$ = $2;
1998 }
1999 | T_REAL T_LPAREN term T_RPAREN {
2000 $$ = build(REAL, $3, NULL);
2001 }
2002 | T_IMAG T_LPAREN term T_RPAREN {
2003 $$ = build(IMAG, $3, NULL);
2004 }
2005 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA identifier T_RPAREN {
2006 symtyp = FMEMBER;
2007 $$ = build_offsetof($3, getsym($5));
2008 }
2009 | T_SIZEOF term {
2010 $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type);
2011 if ($$ != NULL)
2012 check_expr_misc($2, false, false, false, false, false, true);
2013 }
2014 | T_SIZEOF T_LPAREN type_name T_RPAREN %prec T_SIZEOF {
2015 $$ = build_sizeof($3);
2016 }
2017 | T_ALIGNOF T_LPAREN type_name T_RPAREN {
2018 $$ = build_alignof($3);
2019 }
2020 | T_LPAREN type_name T_RPAREN term %prec T_UNARY {
2021 $$ = cast($4, $2);
2022 }
2023 | T_LPAREN type_name T_RPAREN { /* C99 6.5.2.5 "Compound literals" */
2024 sym_t *tmp = mktempsym($2);
2025 begin_initialization(tmp);
2026 cgram_declare(tmp, true, NULL);
2027 } init_lbrace initializer_list comma_opt init_rbrace {
2028 if (!Sflag)
2029 /* compound literals are a C9X/GCC extension */
2030 gnuism(319);
2031 $$ = new_name_node(*current_initsym(), 0);
2032 end_initialization();
2033 }
2034 ;
2035
2036 string:
2037 T_STRING {
2038 $$ = $1;
2039 }
2040 | T_STRING string2 {
2041 $$ = cat_strings($1, $2);
2042 }
2043 ;
2044
2045 string2:
2046 T_STRING {
2047 if (tflag) {
2048 /* concatenated strings are illegal in traditional C */
2049 warning(219);
2050 }
2051 $$ = $1;
2052 }
2053 | string2 T_STRING {
2054 $$ = cat_strings($1, $2);
2055 }
2056 ;
2057
2058 func_arg_list:
2059 expr %prec T_COMMA {
2060 $$ = new_function_argument_node(NULL, $1);
2061 }
2062 | func_arg_list T_COMMA expr {
2063 $$ = new_function_argument_node($1, $3);
2064 }
2065 ;
2066
2067 point_or_arrow:
2068 T_MEMBACC {
2069 symtyp = FMEMBER;
2070 $$ = $1;
2071 }
2072 ;
2073
2074 point:
2075 T_MEMBACC {
2076 if ($1 != POINT) {
2077 /* syntax error '%s' */
2078 error(249, yytext);
2079 }
2080 }
2081 ;
2082
2083 identifier: /* C99 6.4.2.1 */
2084 T_NAME {
2085 $$ = $1;
2086 cgram_debug("name '%s'", $$->sb_name);
2087 }
2088 | T_TYPENAME {
2089 $$ = $1;
2090 cgram_debug("typename '%s'", $$->sb_name);
2091 }
2092 ;
2093
2094 comma_opt:
2095 T_COMMA
2096 | /* empty */
2097 ;
2098 %%
2099
2100 /* ARGSUSED */
2101 int
2102 yyerror(const char *msg)
2103 {
2104 /* syntax error '%s' */
2105 error(249, yytext);
2106 if (++sytxerr >= 5)
2107 norecover();
2108 return 0;
2109 }
2110
2111 static void
2112 cgram_declare(sym_t *decl, bool initflg, sbuf_t *renaming)
2113 {
2114 declare(decl, initflg, renaming);
2115 if (renaming != NULL)
2116 freeyyv(&renaming, T_NAME);
2117 }
2118
2119 /*
2120 * Discard all input tokens up to and including the next
2121 * unmatched right paren
2122 */
2123 static void
2124 ignore_up_to_rparen(void)
2125 {
2126 int level;
2127
2128 if (yychar < 0)
2129 yychar = yylex();
2130 freeyyv(&yylval, yychar);
2131
2132 level = 1;
2133 while (yychar != T_RPAREN || --level > 0) {
2134 if (yychar == T_LPAREN) {
2135 level++;
2136 } else if (yychar <= 0) {
2137 break;
2138 }
2139 freeyyv(&yylval, yychar = yylex());
2140 }
2141
2142 yyclearin;
2143 }
2144
2145 static sym_t *
2146 symbolrename(sym_t *s, sbuf_t *sb)
2147 {
2148 if (sb)
2149 s->s_rename = sb->sb_name;
2150 return s;
2151 }
2152