cgram.y revision 1.250 1 %{
2 /* $NetBSD: cgram.y,v 1.250 2021/07/05 19:48:32 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.250 2021/07/05 19:48:32 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_declarator
322 %type <y_sym> direct_abstract_declarator
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_sou_align_in_bits = 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 lint_assert(is_struct_or_union(dcs->d_type->t_tspec));
836 $$ = dcs->d_type->t_str->sou_first_member;
837 anonymize($$);
838 }
839 | noclass_declspecs deftyp type_attribute_opt {
840 symtyp = FVFT;
841 /* struct or union member must be named */
842 if (!Sflag)
843 /* anonymous struct/union members is a C9X feature */
844 warning(49);
845 if (is_struct_or_union(dcs->d_type->t_tspec)) {
846 $$ = dcs->d_type->t_str->sou_first_member;
847 /* add all the members of the anonymous struct/union */
848 anonymize($$);
849 } else {
850 /* syntax error '%s' */
851 error(249, "unnamed member");
852 $$ = NULL;
853 }
854 }
855 | error {
856 symtyp = FVFT;
857 $$ = NULL;
858 }
859 ;
860
861 noclass_declspecs:
862 clrtyp_typespec {
863 add_type($1);
864 }
865 | type_attribute noclass_declspecs
866 | noclass_declmods typespec {
867 add_type($2);
868 }
869 | noclass_declspecs T_QUAL {
870 add_qualifier($2);
871 }
872 | noclass_declspecs notype_typespec {
873 add_type($2);
874 }
875 | noclass_declspecs type_attribute
876 ;
877
878 noclass_declmods:
879 clrtyp T_QUAL {
880 add_qualifier($2);
881 }
882 | noclass_declmods T_QUAL {
883 add_qualifier($2);
884 }
885 ;
886
887 notype_member_decls:
888 notype_member_decl {
889 $$ = declarator_1_struct_union($1);
890 }
891 | notype_member_decls {
892 symtyp = FMEMBER;
893 } T_COMMA type_member_decl {
894 $$ = lnklst($1, declarator_1_struct_union($4));
895 }
896 ;
897
898 type_member_decls:
899 type_member_decl {
900 $$ = declarator_1_struct_union($1);
901 }
902 | type_member_decls {
903 symtyp = FMEMBER;
904 } T_COMMA type_member_decl {
905 $$ = lnklst($1, declarator_1_struct_union($4));
906 }
907 ;
908
909 notype_member_decl:
910 notype_decl
911 | notype_decl T_COLON constant_expr { /* C99 6.7.2.1 */
912 $$ = bitfield($1, to_int_constant($3, true));
913 }
914 | {
915 symtyp = FVFT;
916 } T_COLON constant_expr { /* C99 6.7.2.1 */
917 $$ = bitfield(NULL, to_int_constant($3, true));
918 }
919 ;
920
921 type_member_decl:
922 type_decl
923 | type_decl T_COLON constant_expr {
924 $$ = bitfield($1, to_int_constant($3, true));
925 }
926 | {
927 symtyp = FVFT;
928 } T_COLON constant_expr {
929 $$ = bitfield(NULL, to_int_constant($3, true));
930 }
931 ;
932
933 enum_spec:
934 enum enum_tag {
935 $$ = mktag($2, ENUM, false, false);
936 }
937 | enum enum_tag {
938 dcs->d_tagtyp = mktag($2, ENUM, true, false);
939 } enum_declaration {
940 $$ = complete_tag_enum(dcs->d_tagtyp, $4);
941 }
942 | enum {
943 dcs->d_tagtyp = mktag(NULL, ENUM, true, false);
944 } enum_declaration {
945 $$ = complete_tag_enum(dcs->d_tagtyp, $3);
946 }
947 | enum error {
948 symtyp = FVFT;
949 $$ = gettyp(INT);
950 }
951 ;
952
953 enum:
954 T_ENUM {
955 symtyp = FTAG;
956 begin_declaration_level(CTCONST);
957 }
958 ;
959
960 enum_tag:
961 identifier {
962 $$ = getsym($1);
963 }
964 ;
965
966 enum_declaration:
967 enum_decl_lbrace enums_with_opt_comma T_RBRACE {
968 $$ = $2;
969 }
970 ;
971
972 enum_decl_lbrace:
973 T_LBRACE {
974 symtyp = FVFT;
975 enumval = 0;
976 }
977 ;
978
979 enums_with_opt_comma:
980 enums
981 | enums T_COMMA {
982 if (sflag) {
983 /* trailing ',' prohibited in enum declaration */
984 error(54);
985 } else {
986 /* trailing ',' prohibited in enum declaration */
987 c99ism(54);
988 }
989 $$ = $1;
990 }
991 ;
992
993 enums:
994 enumerator
995 | enums T_COMMA enumerator {
996 $$ = lnklst($1, $3);
997 }
998 | error {
999 $$ = NULL;
1000 }
1001 ;
1002
1003 enumerator:
1004 enumeration_constant {
1005 $$ = enumeration_constant($1, enumval, true);
1006 }
1007 | enumeration_constant T_ASSIGN constant_expr {
1008 $$ = enumeration_constant($1, to_int_constant($3, true), false);
1009 }
1010 ;
1011
1012 enumeration_constant: /* C99 6.4.4.3 */
1013 identifier {
1014 $$ = getsym($1);
1015 }
1016 ;
1017
1018
1019 notype_init_decls:
1020 notype_init_decl
1021 | notype_init_decls T_COMMA type_init_decl
1022 ;
1023
1024 type_init_decls:
1025 type_init_decl
1026 | type_init_decls T_COMMA type_init_decl
1027 ;
1028
1029 notype_init_decl:
1030 notype_decl asm_or_symbolrename_opt {
1031 cgram_declare($1, false, $2);
1032 check_size($1);
1033 }
1034 | notype_decl asm_or_symbolrename_opt {
1035 begin_initialization($1);
1036 cgram_declare($1, true, $2);
1037 } T_ASSIGN initializer {
1038 check_size($1);
1039 end_initialization();
1040 }
1041 ;
1042
1043 type_init_decl:
1044 type_decl asm_or_symbolrename_opt {
1045 cgram_declare($1, false, $2);
1046 check_size($1);
1047 }
1048 | type_decl asm_or_symbolrename_opt {
1049 begin_initialization($1);
1050 cgram_declare($1, true, $2);
1051 } T_ASSIGN initializer {
1052 check_size($1);
1053 end_initialization();
1054 }
1055 ;
1056
1057 notype_decl:
1058 notype_direct_decl
1059 | pointer notype_direct_decl {
1060 $$ = add_pointer($2, $1);
1061 }
1062 ;
1063
1064 type_decl:
1065 type_direct_decl
1066 | pointer type_direct_decl {
1067 $$ = add_pointer($2, $1);
1068 }
1069 ;
1070
1071 notype_direct_decl:
1072 T_NAME {
1073 $$ = declarator_name(getsym($1));
1074 }
1075 | T_LPAREN type_decl T_RPAREN {
1076 $$ = $2;
1077 }
1078 | type_attribute notype_direct_decl {
1079 $$ = $2;
1080 }
1081 | notype_direct_decl T_LBRACK T_RBRACK {
1082 $$ = add_array($1, false, 0);
1083 }
1084 | notype_direct_decl T_LBRACK array_size T_RBRACK {
1085 $$ = add_array($1, true, to_int_constant($3, false));
1086 }
1087 | notype_direct_decl param_list asm_or_symbolrename_opt {
1088 $$ = add_function(symbolrename($1, $3), $2);
1089 end_declaration_level();
1090 block_level--;
1091 }
1092 | notype_direct_decl type_attribute_list
1093 ;
1094
1095 type_direct_decl:
1096 identifier {
1097 $$ = declarator_name(getsym($1));
1098 }
1099 | T_LPAREN type_decl T_RPAREN {
1100 $$ = $2;
1101 }
1102 | type_attribute type_direct_decl {
1103 $$ = $2;
1104 }
1105 | type_direct_decl T_LBRACK T_RBRACK {
1106 $$ = add_array($1, false, 0);
1107 }
1108 | type_direct_decl T_LBRACK array_size T_RBRACK {
1109 $$ = add_array($1, true, to_int_constant($3, false));
1110 }
1111 | type_direct_decl param_list asm_or_symbolrename_opt {
1112 $$ = add_function(symbolrename($1, $3), $2);
1113 end_declaration_level();
1114 block_level--;
1115 }
1116 | type_direct_decl type_attribute_list
1117 ;
1118
1119 /*
1120 * param_decl and notype_param_decl exist to avoid a conflict in
1121 * argument lists. A typename enclosed in parens should always be
1122 * treated as a typename, not an argument.
1123 * "typedef int a; f(int (a));" is "typedef int a; f(int foo(a));"
1124 * not "typedef int a; f(int a);"
1125 */
1126 param_decl:
1127 direct_param_decl
1128 | pointer direct_param_decl {
1129 $$ = add_pointer($2, $1);
1130 }
1131 ;
1132
1133 array_size:
1134 type_qualifier_list_opt T_SCLASS constant_expr {
1135 /* C11 6.7.6.3p7 */
1136 if ($2 != STATIC)
1137 yyerror("Bad attribute");
1138 /* static array size is a C11 extension */
1139 c11ism(343);
1140 $$ = $3;
1141 }
1142 | constant_expr
1143 ;
1144
1145 direct_param_decl:
1146 identifier type_attribute_list {
1147 $$ = declarator_name(getsym($1));
1148 }
1149 | identifier {
1150 $$ = declarator_name(getsym($1));
1151 }
1152 | T_LPAREN notype_param_decl T_RPAREN {
1153 $$ = $2;
1154 }
1155 | direct_param_decl T_LBRACK T_RBRACK {
1156 $$ = add_array($1, false, 0);
1157 }
1158 | direct_param_decl T_LBRACK array_size T_RBRACK {
1159 $$ = add_array($1, true, to_int_constant($3, false));
1160 }
1161 | direct_param_decl param_list asm_or_symbolrename_opt {
1162 $$ = add_function(symbolrename($1, $3), $2);
1163 end_declaration_level();
1164 block_level--;
1165 }
1166 ;
1167
1168 notype_param_decl:
1169 direct_notype_param_decl
1170 | pointer direct_notype_param_decl {
1171 $$ = add_pointer($2, $1);
1172 }
1173 ;
1174
1175 direct_notype_param_decl:
1176 identifier {
1177 $$ = declarator_name(getsym($1));
1178 }
1179 | T_LPAREN notype_param_decl T_RPAREN {
1180 $$ = $2;
1181 }
1182 | direct_notype_param_decl T_LBRACK T_RBRACK {
1183 $$ = add_array($1, false, 0);
1184 }
1185 | direct_notype_param_decl T_LBRACK array_size T_RBRACK {
1186 $$ = add_array($1, true, to_int_constant($3, false));
1187 }
1188 | direct_notype_param_decl param_list asm_or_symbolrename_opt {
1189 $$ = add_function(symbolrename($1, $3), $2);
1190 end_declaration_level();
1191 block_level--;
1192 }
1193 ;
1194
1195 pointer: /* C99 6.7.5 */
1196 asterisk type_qualifier_list_opt {
1197 $$ = merge_qualified_pointer($1, $2);
1198 }
1199 | asterisk type_qualifier_list_opt pointer {
1200 $$ = merge_qualified_pointer($1, $2);
1201 $$ = merge_qualified_pointer($$, $3);
1202 }
1203 ;
1204
1205 asterisk:
1206 T_ASTERISK {
1207 $$ = xcalloc(1, sizeof(*$$));
1208 $$->p_pointer = true;
1209 }
1210 ;
1211
1212 type_qualifier_list_opt:
1213 /* empty */ {
1214 $$ = NULL;
1215 }
1216 | type_qualifier_list
1217 ;
1218
1219 type_qualifier_list: /* C99 6.7.5 */
1220 type_qualifier
1221 | type_qualifier_list type_qualifier {
1222 $$ = merge_qualified_pointer($1, $2);
1223 }
1224 ;
1225
1226 type_qualifier:
1227 T_QUAL {
1228 $$ = xcalloc(1, sizeof(*$$));
1229 if ($1 == CONST) {
1230 $$->p_const = true;
1231 } else if ($1 == VOLATILE) {
1232 $$->p_volatile = true;
1233 } else {
1234 lint_assert($1 == RESTRICT || $1 == THREAD);
1235 }
1236 }
1237 ;
1238
1239 param_list:
1240 id_list_lparen identifier_list T_RPAREN {
1241 $$ = $2;
1242 }
1243 | abstract_decl_param_list
1244 ;
1245
1246 id_list_lparen:
1247 T_LPAREN {
1248 block_level++;
1249 begin_declaration_level(PROTO_ARG);
1250 }
1251 ;
1252
1253 identifier_list:
1254 T_NAME {
1255 $$ = old_style_function_name(getsym($1));
1256 }
1257 | identifier_list T_COMMA T_NAME {
1258 $$ = lnklst($1, old_style_function_name(getsym($3)));
1259 }
1260 | identifier_list error
1261 ;
1262
1263 abstract_decl_param_list:
1264 abstract_decl_lparen T_RPAREN type_attribute_opt {
1265 $$ = NULL;
1266 }
1267 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN type_attribute_opt {
1268 dcs->d_proto = true;
1269 $$ = $2;
1270 }
1271 | abstract_decl_lparen error T_RPAREN type_attribute_opt {
1272 $$ = NULL;
1273 }
1274 ;
1275
1276 abstract_decl_lparen:
1277 T_LPAREN {
1278 block_level++;
1279 begin_declaration_level(PROTO_ARG);
1280 }
1281 ;
1282
1283 vararg_parameter_type_list:
1284 parameter_type_list
1285 | parameter_type_list T_COMMA T_ELLIPSIS {
1286 dcs->d_vararg = true;
1287 $$ = $1;
1288 }
1289 | T_ELLIPSIS {
1290 if (sflag) {
1291 /* ANSI C requires formal parameter before '...' */
1292 error(84);
1293 } else if (!tflag) {
1294 /* ANSI C requires formal parameter before '...' */
1295 warning(84);
1296 }
1297 dcs->d_vararg = true;
1298 $$ = NULL;
1299 }
1300 ;
1301
1302 parameter_type_list:
1303 parameter_declaration
1304 | parameter_type_list T_COMMA parameter_declaration {
1305 $$ = lnklst($1, $3);
1306 }
1307 ;
1308
1309 parameter_declaration:
1310 declmods deftyp {
1311 $$ = declare_argument(abstract_name(), false);
1312 }
1313 | declaration_specifiers deftyp {
1314 $$ = declare_argument(abstract_name(), false);
1315 }
1316 | declmods deftyp notype_param_decl {
1317 $$ = declare_argument($3, false);
1318 }
1319 /*
1320 * param_decl is needed because of following conflict:
1321 * "typedef int a; f(int (a));" could be parsed as
1322 * "function with argument a of type int", or
1323 * "function with an abstract argument of type function".
1324 * This grammar realizes the second case.
1325 */
1326 | declaration_specifiers deftyp param_decl {
1327 $$ = declare_argument($3, false);
1328 }
1329 | declmods deftyp abstract_declarator {
1330 $$ = declare_argument($3, false);
1331 }
1332 | declaration_specifiers deftyp abstract_declarator {
1333 $$ = declare_argument($3, false);
1334 }
1335 ;
1336
1337 asm_or_symbolrename_opt: /* expect only one */
1338 /* empty */ {
1339 $$ = NULL;
1340 }
1341 | T_ASM T_LPAREN T_STRING T_RPAREN {
1342 freeyyv(&$3, T_STRING);
1343 $$ = NULL;
1344 }
1345 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN {
1346 $$ = $3;
1347 }
1348 ;
1349
1350 initializer: /* C99 6.7.8 "Initialization" */
1351 expr %prec T_COMMA {
1352 init_expr($1);
1353 }
1354 | init_lbrace init_rbrace {
1355 /* XXX: Empty braces are not covered by C99 6.7.8. */
1356 }
1357 | init_lbrace initializer_list comma_opt init_rbrace
1358 | error
1359 ;
1360
1361 initializer_list: /* C99 6.7.8 "Initialization" */
1362 initializer_list_item
1363 | initializer_list T_COMMA initializer_list_item
1364 ;
1365
1366 initializer_list_item:
1367 designation initializer
1368 | initializer
1369 ;
1370
1371 designation: /* C99 6.7.8 "Initialization" */
1372 designator_list T_ASSIGN
1373 | identifier T_COLON {
1374 /* GCC style struct or union member name in initializer */
1375 gnuism(315);
1376 add_designator_member($1);
1377 }
1378 ;
1379
1380 designator_list: /* C99 6.7.8 "Initialization" */
1381 designator
1382 | designator_list designator
1383 ;
1384
1385 designator: /* C99 6.7.8 "Initialization" */
1386 T_LBRACK range T_RBRACK {
1387 add_designator_subscript($2);
1388 if (!Sflag)
1389 /* array initializer with des.s is a C9X feature */
1390 warning(321);
1391 }
1392 | T_POINT identifier {
1393 if (!Sflag)
1394 /* struct or union member name in initializer is ... */
1395 warning(313);
1396 add_designator_member($2);
1397 }
1398 ;
1399
1400 range:
1401 constant_expr {
1402 $$.lo = to_int_constant($1, true);
1403 $$.hi = $$.lo;
1404 }
1405 | constant_expr T_ELLIPSIS constant_expr {
1406 $$.lo = to_int_constant($1, true);
1407 $$.hi = to_int_constant($3, true);
1408 /* initialization with '[a...b]' is a GNU extension */
1409 gnuism(340);
1410 }
1411 ;
1412
1413 init_lbrace:
1414 T_LBRACE {
1415 init_lbrace();
1416 }
1417 ;
1418
1419 init_rbrace:
1420 T_RBRACE {
1421 init_rbrace();
1422 }
1423 ;
1424
1425 type_name: /* C99 6.7.6 */
1426 {
1427 begin_declaration_level(ABSTRACT);
1428 } abstract_declaration {
1429 end_declaration_level();
1430 $$ = $2->s_type;
1431 }
1432 ;
1433
1434 abstract_declaration:
1435 noclass_declmods deftyp {
1436 $$ = declare_1_abstract(abstract_name());
1437 }
1438 | noclass_declspecs deftyp {
1439 $$ = declare_1_abstract(abstract_name());
1440 }
1441 | noclass_declmods deftyp abstract_declarator {
1442 $$ = declare_1_abstract($3);
1443 }
1444 | noclass_declspecs deftyp abstract_declarator {
1445 $$ = declare_1_abstract($3);
1446 }
1447 ;
1448
1449 abstract_declarator: /* C99 6.7.6 */
1450 pointer {
1451 $$ = add_pointer(abstract_name(), $1);
1452 }
1453 | direct_abstract_declarator
1454 | pointer direct_abstract_declarator {
1455 $$ = add_pointer($2, $1);
1456 }
1457 | T_TYPEOF term { /* GCC extension */
1458 $$ = mktempsym($2->tn_type);
1459 }
1460 ;
1461
1462 direct_abstract_declarator: /* C99 6.7.6 */
1463 T_LPAREN abstract_declarator T_RPAREN {
1464 $$ = $2;
1465 }
1466 | T_LBRACK T_RBRACK {
1467 $$ = add_array(abstract_name(), false, 0);
1468 }
1469 | T_LBRACK array_size T_RBRACK {
1470 $$ = add_array(abstract_name(), true, to_int_constant($2, false));
1471 }
1472 | type_attribute direct_abstract_declarator {
1473 $$ = $2;
1474 }
1475 | direct_abstract_declarator T_LBRACK T_RBRACK {
1476 $$ = add_array($1, false, 0);
1477 }
1478 | direct_abstract_declarator T_LBRACK T_ASTERISK T_RBRACK { /* C99 */
1479 $$ = add_array($1, false, 0);
1480 }
1481 | direct_abstract_declarator T_LBRACK array_size T_RBRACK {
1482 $$ = add_array($1, true, to_int_constant($3, false));
1483 }
1484 | abstract_decl_param_list asm_or_symbolrename_opt {
1485 $$ = add_function(symbolrename(abstract_name(), $2), $1);
1486 end_declaration_level();
1487 block_level--;
1488 }
1489 | direct_abstract_declarator abstract_decl_param_list asm_or_symbolrename_opt {
1490 $$ = add_function(symbolrename($1, $3), $2);
1491 end_declaration_level();
1492 block_level--;
1493 }
1494 | direct_abstract_declarator type_attribute_list
1495 ;
1496
1497 non_expr_statement:
1498 type_attribute T_SEMI
1499 | labeled_statement
1500 | compound_statement
1501 | selection_statement
1502 | iteration_statement
1503 | jump_statement {
1504 seen_fallthrough = false;
1505 }
1506 | asm_statement
1507 ;
1508
1509 statement: /* C99 6.8 */
1510 expr_statement
1511 | non_expr_statement
1512 ;
1513
1514 labeled_statement: /* C99 6.8.1 */
1515 label type_attribute_opt statement
1516 ;
1517
1518 label:
1519 T_NAME T_COLON {
1520 symtyp = FLABEL;
1521 named_label(getsym($1));
1522 }
1523 | T_CASE constant_expr T_COLON {
1524 case_label($2);
1525 seen_fallthrough = true;
1526 }
1527 | T_CASE constant_expr T_ELLIPSIS constant_expr T_COLON {
1528 /* XXX: We don't fill all cases */
1529 case_label($2);
1530 seen_fallthrough = true;
1531 }
1532 | T_DEFAULT T_COLON {
1533 default_label();
1534 seen_fallthrough = true;
1535 }
1536 ;
1537
1538 compound_statement: /* C99 6.8.2 */
1539 compound_statement_lbrace compound_statement_rbrace
1540 | compound_statement_lbrace block_item_list compound_statement_rbrace
1541 ;
1542
1543 compound_statement_lbrace:
1544 T_LBRACE {
1545 block_level++;
1546 mem_block_level++;
1547 begin_declaration_level(AUTO);
1548 }
1549 ;
1550
1551 compound_statement_rbrace:
1552 T_RBRACE {
1553 end_declaration_level();
1554 freeblk();
1555 mem_block_level--;
1556 block_level--;
1557 seen_fallthrough = false;
1558 }
1559 ;
1560
1561 block_item_list:
1562 block_item
1563 | block_item_list block_item {
1564 if (!Sflag && $1 && !$2)
1565 /* declarations after statements is a C99 feature */
1566 c99ism(327);
1567 $$ = $1 || $2;
1568 }
1569 ;
1570
1571 block_item:
1572 statement {
1573 $$ = true;
1574 restore_warning_flags();
1575 }
1576 | declaration {
1577 $$ = false;
1578 restore_warning_flags();
1579 }
1580 ;
1581
1582 expr_statement:
1583 expr T_SEMI {
1584 expr($1, false, false, false, false);
1585 seen_fallthrough = false;
1586 }
1587 | T_SEMI {
1588 seen_fallthrough = false;
1589 }
1590 ;
1591
1592 selection_statement: /* C99 6.8.4 */
1593 if_without_else {
1594 save_warning_flags();
1595 if2();
1596 if3(false);
1597 }
1598 | if_without_else T_ELSE {
1599 save_warning_flags();
1600 if2();
1601 } statement {
1602 clear_warning_flags();
1603 if3(true);
1604 }
1605 | if_without_else T_ELSE error {
1606 clear_warning_flags();
1607 if3(false);
1608 }
1609 | switch_expr statement {
1610 clear_warning_flags();
1611 switch2();
1612 }
1613 | switch_expr error {
1614 clear_warning_flags();
1615 switch2();
1616 }
1617 ;
1618
1619 if_without_else:
1620 if_expr statement
1621 | if_expr error
1622 ;
1623
1624 if_expr:
1625 T_IF T_LPAREN expr T_RPAREN {
1626 if1($3);
1627 clear_warning_flags();
1628 }
1629 ;
1630
1631 switch_expr:
1632 T_SWITCH T_LPAREN expr T_RPAREN {
1633 switch1($3);
1634 clear_warning_flags();
1635 }
1636 ;
1637
1638 generic_selection: /* C11 6.5.1.1 */
1639 T_GENERIC T_LPAREN assignment_expression T_COMMA
1640 generic_assoc_list T_RPAREN {
1641 /* generic selection requires C11 or later */
1642 c11ism(345);
1643 $$ = build_generic_selection($3, $5);
1644 }
1645 ;
1646
1647 generic_assoc_list: /* C11 6.5.1.1 */
1648 generic_association
1649 | generic_assoc_list T_COMMA generic_association {
1650 $3->gat_prev = $1;
1651 $$ = $3;
1652 }
1653 ;
1654
1655 generic_association: /* C11 6.5.1.1 */
1656 type_name T_COLON assignment_expression {
1657 $$ = getblk(sizeof(*$$));
1658 $$->gat_arg = $1;
1659 $$->gat_result = $3;
1660 }
1661 | T_DEFAULT T_COLON assignment_expression {
1662 $$ = getblk(sizeof(*$$));
1663 $$->gat_arg = NULL;
1664 $$->gat_result = $3;
1665 }
1666 ;
1667
1668 do_statement: /* C99 6.8.5 */
1669 do statement {
1670 clear_warning_flags();
1671 }
1672 ;
1673
1674 iteration_statement: /* C99 6.8.5 */
1675 while_expr statement {
1676 clear_warning_flags();
1677 while2();
1678 }
1679 | while_expr error {
1680 clear_warning_flags();
1681 while2();
1682 }
1683 | do_statement do_while_expr {
1684 do2($2);
1685 seen_fallthrough = false;
1686 }
1687 | do error {
1688 clear_warning_flags();
1689 do2(NULL);
1690 }
1691 | for_exprs statement {
1692 clear_warning_flags();
1693 for2();
1694 end_declaration_level();
1695 block_level--;
1696 }
1697 | for_exprs error {
1698 clear_warning_flags();
1699 for2();
1700 end_declaration_level();
1701 block_level--;
1702 }
1703 ;
1704
1705 while_expr:
1706 T_WHILE T_LPAREN expr T_RPAREN {
1707 while1($3);
1708 clear_warning_flags();
1709 }
1710 ;
1711
1712 do:
1713 T_DO {
1714 do1();
1715 }
1716 ;
1717
1718 do_while_expr:
1719 T_WHILE T_LPAREN expr T_RPAREN T_SEMI {
1720 $$ = $3;
1721 }
1722 ;
1723
1724 for_start:
1725 T_FOR T_LPAREN {
1726 begin_declaration_level(AUTO);
1727 block_level++;
1728 }
1729 ;
1730 for_exprs:
1731 for_start declaration_specifiers deftyp notype_init_decls T_SEMI
1732 expr_opt T_SEMI expr_opt T_RPAREN {
1733 /* variable declaration in for loop */
1734 c99ism(325);
1735 for1(NULL, $6, $8);
1736 clear_warning_flags();
1737 }
1738 | for_start expr_opt T_SEMI expr_opt T_SEMI expr_opt T_RPAREN {
1739 for1($2, $4, $6);
1740 clear_warning_flags();
1741 }
1742 ;
1743
1744 expr_opt:
1745 /* empty */ {
1746 $$ = NULL;
1747 }
1748 | expr
1749 ;
1750
1751 jump_statement: /* C99 6.8.6 */
1752 goto identifier T_SEMI {
1753 do_goto(getsym($2));
1754 }
1755 | goto error T_SEMI {
1756 symtyp = FVFT;
1757 }
1758 | T_CONTINUE T_SEMI {
1759 do_continue();
1760 }
1761 | T_BREAK T_SEMI {
1762 do_break();
1763 }
1764 | T_RETURN T_SEMI {
1765 do_return(NULL);
1766 }
1767 | T_RETURN expr T_SEMI {
1768 do_return($2);
1769 }
1770 ;
1771
1772 goto:
1773 T_GOTO {
1774 symtyp = FLABEL;
1775 }
1776 ;
1777
1778 asm_statement:
1779 T_ASM T_LPAREN read_until_rparen T_SEMI {
1780 setasm();
1781 }
1782 | T_ASM T_QUAL T_LPAREN read_until_rparen T_SEMI {
1783 setasm();
1784 }
1785 | T_ASM error
1786 ;
1787
1788 read_until_rparen:
1789 /* empty */ {
1790 ignore_up_to_rparen();
1791 }
1792 ;
1793
1794 constant_expr_list_opt:
1795 /* empty */
1796 | constant_expr_list
1797 ;
1798
1799 constant_expr_list:
1800 constant_expr
1801 | constant_expr_list T_COMMA constant_expr
1802 ;
1803
1804 constant_expr: /* C99 6.6 */
1805 expr %prec T_ASSIGN
1806 ;
1807
1808 expr:
1809 expr T_ASTERISK expr {
1810 $$ = build(MULT, $1, $3);
1811 }
1812 | expr T_MULTIPLICATIVE expr {
1813 $$ = build($2, $1, $3);
1814 }
1815 | expr T_ADDITIVE expr {
1816 $$ = build($2, $1, $3);
1817 }
1818 | expr T_SHIFT expr {
1819 $$ = build($2, $1, $3);
1820 }
1821 | expr T_RELATIONAL expr {
1822 $$ = build($2, $1, $3);
1823 }
1824 | expr T_EQUALITY expr {
1825 $$ = build($2, $1, $3);
1826 }
1827 | expr T_AMPER expr {
1828 $$ = build(BITAND, $1, $3);
1829 }
1830 | expr T_BITXOR expr {
1831 $$ = build(BITXOR, $1, $3);
1832 }
1833 | expr T_BITOR expr {
1834 $$ = build(BITOR, $1, $3);
1835 }
1836 | expr T_LOGAND expr {
1837 $$ = build(LOGAND, $1, $3);
1838 }
1839 | expr T_LOGOR expr {
1840 $$ = build(LOGOR, $1, $3);
1841 }
1842 | expr T_QUEST expr T_COLON expr {
1843 $$ = build(QUEST, $1, build(COLON, $3, $5));
1844 }
1845 | expr T_ASSIGN expr {
1846 $$ = build(ASSIGN, $1, $3);
1847 }
1848 | expr T_OPASSIGN expr {
1849 $$ = build($2, $1, $3);
1850 }
1851 | expr T_COMMA expr {
1852 $$ = build(COMMA, $1, $3);
1853 }
1854 | term
1855 | generic_selection
1856 ;
1857
1858 assignment_expression: /* C99 6.5.16 */
1859 expr %prec T_ASSIGN
1860 ;
1861
1862 term:
1863 T_NAME {
1864 /* XXX really necessary? */
1865 if (yychar < 0)
1866 yychar = yylex();
1867 $$ = new_name_node(getsym($1), yychar);
1868 }
1869 | string {
1870 $$ = new_string_node($1);
1871 }
1872 | T_CON {
1873 $$ = expr_new_constant(gettyp($1->v_tspec), $1);
1874 }
1875 | T_LPAREN expr T_RPAREN {
1876 if ($2 != NULL)
1877 $2->tn_parenthesized = true;
1878 $$ = $2;
1879 }
1880 | T_LPAREN compound_statement_lbrace gcc_statement_expr_list {
1881 block_level--;
1882 mem_block_level--;
1883 begin_initialization(mktempsym(dup_type($3->tn_type)));
1884 mem_block_level++;
1885 block_level++;
1886 /* ({ }) is a GCC extension */
1887 gnuism(320);
1888 } compound_statement_rbrace T_RPAREN {
1889 $$ = new_name_node(*current_initsym(), 0);
1890 end_initialization();
1891 }
1892 | term T_INCDEC {
1893 $$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL);
1894 }
1895 | T_INCDEC term {
1896 $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL);
1897 }
1898 | T_ASTERISK term {
1899 $$ = build(INDIR, $2, NULL);
1900 }
1901 | T_AMPER term {
1902 $$ = build(ADDR, $2, NULL);
1903 }
1904 | T_UNARY term {
1905 $$ = build($1, $2, NULL);
1906 }
1907 | T_ADDITIVE term {
1908 if (tflag && $1 == PLUS) {
1909 /* unary + is illegal in traditional C */
1910 warning(100);
1911 }
1912 $$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL);
1913 }
1914 | term T_LBRACK expr T_RBRACK {
1915 $$ = build(INDIR, build(PLUS, $1, $3), NULL);
1916 }
1917 | term T_LPAREN T_RPAREN {
1918 $$ = new_function_call_node($1, NULL);
1919 }
1920 | term T_LPAREN func_arg_list T_RPAREN {
1921 $$ = new_function_call_node($1, $3);
1922 }
1923 | term point_or_arrow T_NAME {
1924 if ($1 != NULL) {
1925 sym_t *msym;
1926 /*
1927 * XXX struct_or_union_member should be integrated
1928 * in build()
1929 */
1930 if ($2 == ARROW) {
1931 /*
1932 * must do this before struct_or_union_member
1933 * is called
1934 */
1935 $1 = cconv($1);
1936 }
1937 msym = struct_or_union_member($1, $2, getsym($3));
1938 $$ = build($2, $1, new_name_node(msym, 0));
1939 } else {
1940 $$ = NULL;
1941 }
1942 }
1943 | T_REAL term {
1944 $$ = build(REAL, $2, NULL);
1945 }
1946 | T_IMAG term {
1947 $$ = build(IMAG, $2, NULL);
1948 }
1949 | T_EXTENSION term {
1950 $$ = $2;
1951 }
1952 | T_REAL T_LPAREN term T_RPAREN {
1953 $$ = build(REAL, $3, NULL);
1954 }
1955 | T_IMAG T_LPAREN term T_RPAREN {
1956 $$ = build(IMAG, $3, NULL);
1957 }
1958 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA identifier T_RPAREN {
1959 symtyp = FMEMBER;
1960 $$ = build_offsetof($3, getsym($5));
1961 }
1962 | T_SIZEOF term {
1963 $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type);
1964 if ($$ != NULL)
1965 check_expr_misc($2, false, false, false, false, false, true);
1966 }
1967 | T_SIZEOF T_LPAREN type_name T_RPAREN %prec T_SIZEOF {
1968 $$ = build_sizeof($3);
1969 }
1970 | T_ALIGNOF T_LPAREN type_name T_RPAREN {
1971 $$ = build_alignof($3);
1972 }
1973 | T_LPAREN type_name T_RPAREN term %prec T_UNARY {
1974 $$ = cast($4, $2);
1975 }
1976 | T_LPAREN type_name T_RPAREN { /* C99 6.5.2.5 "Compound literals" */
1977 sym_t *tmp = mktempsym($2);
1978 begin_initialization(tmp);
1979 cgram_declare(tmp, true, NULL);
1980 } init_lbrace initializer_list comma_opt init_rbrace {
1981 if (!Sflag)
1982 /* compound literals are a C9X/GCC extension */
1983 gnuism(319);
1984 $$ = new_name_node(*current_initsym(), 0);
1985 end_initialization();
1986 }
1987 ;
1988
1989 /*
1990 * The inner part of a GCC statement-expression of the form ({ ... }).
1991 *
1992 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
1993 */
1994 gcc_statement_expr_list:
1995 gcc_statement_expr_item
1996 | gcc_statement_expr_list gcc_statement_expr_item {
1997 $$ = $2;
1998 }
1999 ;
2000
2001 gcc_statement_expr_item:
2002 declaration {
2003 clear_warning_flags();
2004 $$ = NULL;
2005 }
2006 | non_expr_statement {
2007 $$ = expr_zalloc_tnode();
2008 $$->tn_type = gettyp(VOID);
2009 }
2010 | expr T_SEMI {
2011 if ($1 == NULL) { /* in case of syntax errors */
2012 $$ = expr_zalloc_tnode();
2013 $$->tn_type = gettyp(VOID);
2014 } else {
2015 /* XXX: do that only on the last name */
2016 if ($1->tn_op == NAME)
2017 $1->tn_sym->s_used = true;
2018 $$ = $1;
2019 expr($1, false, false, false, false);
2020 seen_fallthrough = false;
2021 }
2022 }
2023 ;
2024
2025 string:
2026 T_STRING
2027 | T_STRING string2 {
2028 $$ = cat_strings($1, $2);
2029 }
2030 ;
2031
2032 string2:
2033 T_STRING {
2034 if (tflag) {
2035 /* concatenated strings are illegal in traditional C */
2036 warning(219);
2037 }
2038 $$ = $1;
2039 }
2040 | string2 T_STRING {
2041 $$ = cat_strings($1, $2);
2042 }
2043 ;
2044
2045 func_arg_list:
2046 expr %prec T_COMMA {
2047 $$ = new_function_argument_node(NULL, $1);
2048 }
2049 | func_arg_list T_COMMA expr {
2050 $$ = new_function_argument_node($1, $3);
2051 }
2052 ;
2053
2054 point_or_arrow:
2055 T_POINT {
2056 symtyp = FMEMBER;
2057 $$ = POINT;
2058 }
2059 | T_ARROW {
2060 symtyp = FMEMBER;
2061 $$ = ARROW;
2062 }
2063 ;
2064
2065 identifier: /* C99 6.4.2.1 */
2066 T_NAME {
2067 $$ = $1;
2068 cgram_debug("name '%s'", $$->sb_name);
2069 }
2070 | T_TYPENAME {
2071 $$ = $1;
2072 cgram_debug("typename '%s'", $$->sb_name);
2073 }
2074 ;
2075
2076 comma_opt:
2077 T_COMMA
2078 | /* empty */
2079 ;
2080 %%
2081
2082 /* ARGSUSED */
2083 int
2084 yyerror(const char *msg)
2085 {
2086 /* syntax error '%s' */
2087 error(249, yytext);
2088 if (++sytxerr >= 5)
2089 norecover();
2090 return 0;
2091 }
2092
2093 static void
2094 cgram_declare(sym_t *decl, bool initflg, sbuf_t *renaming)
2095 {
2096 declare(decl, initflg, renaming);
2097 if (renaming != NULL)
2098 freeyyv(&renaming, T_NAME);
2099 }
2100
2101 /*
2102 * Discard all input tokens up to and including the next
2103 * unmatched right paren
2104 */
2105 static void
2106 ignore_up_to_rparen(void)
2107 {
2108 int level;
2109
2110 if (yychar < 0)
2111 yychar = yylex();
2112 freeyyv(&yylval, yychar);
2113
2114 level = 1;
2115 while (yychar != T_RPAREN || --level > 0) {
2116 if (yychar == T_LPAREN) {
2117 level++;
2118 } else if (yychar <= 0) {
2119 break;
2120 }
2121 freeyyv(&yylval, yychar = yylex());
2122 }
2123
2124 yyclearin;
2125 }
2126
2127 static sym_t *
2128 symbolrename(sym_t *s, sbuf_t *sb)
2129 {
2130 if (sb != NULL)
2131 s->s_rename = sb->sb_name;
2132 return s;
2133 }
2134