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