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