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