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