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