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