cgram.y revision 1.254 1 %{
2 /* $NetBSD: cgram.y,v 1.254 2021/07/06 04:48:17 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.254 2021/07/06 04:48:17 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 *y_generic;
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_generic> generic_assoc_list
345 %type <y_generic> 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 /* See the Bison manual, section 7.1 "Semantic Info in Token Kinds". */
1030 notype_init_decl:
1031 notype_decl asm_or_symbolrename_opt {
1032 cgram_declare($1, false, $2);
1033 check_size($1);
1034 }
1035 | notype_decl asm_or_symbolrename_opt {
1036 begin_initialization($1);
1037 cgram_declare($1, true, $2);
1038 } T_ASSIGN initializer {
1039 check_size($1);
1040 end_initialization();
1041 }
1042 ;
1043
1044 type_init_decl:
1045 type_decl asm_or_symbolrename_opt {
1046 cgram_declare($1, false, $2);
1047 check_size($1);
1048 }
1049 | type_decl asm_or_symbolrename_opt {
1050 begin_initialization($1);
1051 cgram_declare($1, true, $2);
1052 } T_ASSIGN initializer {
1053 check_size($1);
1054 end_initialization();
1055 }
1056 ;
1057
1058 notype_decl:
1059 notype_direct_decl
1060 | pointer notype_direct_decl {
1061 $$ = add_pointer($2, $1);
1062 }
1063 ;
1064
1065 type_decl:
1066 type_direct_decl
1067 | pointer type_direct_decl {
1068 $$ = add_pointer($2, $1);
1069 }
1070 ;
1071
1072 notype_direct_decl:
1073 T_NAME {
1074 $$ = declarator_name(getsym($1));
1075 }
1076 | T_LPAREN type_decl T_RPAREN {
1077 $$ = $2;
1078 }
1079 | type_attribute notype_direct_decl {
1080 $$ = $2;
1081 }
1082 | notype_direct_decl T_LBRACK T_RBRACK {
1083 $$ = add_array($1, false, 0);
1084 }
1085 | notype_direct_decl T_LBRACK array_size T_RBRACK {
1086 $$ = add_array($1, true, to_int_constant($3, false));
1087 }
1088 | notype_direct_decl param_list asm_or_symbolrename_opt {
1089 $$ = add_function(symbolrename($1, $3), $2);
1090 end_declaration_level();
1091 block_level--;
1092 }
1093 | notype_direct_decl type_attribute_list
1094 ;
1095
1096 type_direct_decl:
1097 identifier {
1098 $$ = declarator_name(getsym($1));
1099 }
1100 | T_LPAREN type_decl T_RPAREN {
1101 $$ = $2;
1102 }
1103 | type_attribute type_direct_decl {
1104 $$ = $2;
1105 }
1106 | type_direct_decl T_LBRACK T_RBRACK {
1107 $$ = add_array($1, false, 0);
1108 }
1109 | type_direct_decl T_LBRACK array_size T_RBRACK {
1110 $$ = add_array($1, true, to_int_constant($3, false));
1111 }
1112 | type_direct_decl param_list asm_or_symbolrename_opt {
1113 $$ = add_function(symbolrename($1, $3), $2);
1114 end_declaration_level();
1115 block_level--;
1116 }
1117 | type_direct_decl type_attribute_list
1118 ;
1119
1120 /*
1121 * param_decl and notype_param_decl exist to avoid a conflict in
1122 * argument lists. A typename enclosed in parens should always be
1123 * treated as a typename, not an argument.
1124 * "typedef int a; f(int (a));" is "typedef int a; f(int foo(a));"
1125 * not "typedef int a; f(int a);"
1126 */
1127 param_decl:
1128 direct_param_decl
1129 | pointer direct_param_decl {
1130 $$ = add_pointer($2, $1);
1131 }
1132 ;
1133
1134 array_size:
1135 type_qualifier_list_opt T_SCLASS constant_expr {
1136 /* C11 6.7.6.3p7 */
1137 if ($2 != STATIC)
1138 yyerror("Bad attribute");
1139 /* static array size is a C11 extension */
1140 c11ism(343);
1141 $$ = $3;
1142 }
1143 | constant_expr
1144 ;
1145
1146 direct_param_decl:
1147 identifier type_attribute_list {
1148 $$ = declarator_name(getsym($1));
1149 }
1150 | identifier {
1151 $$ = declarator_name(getsym($1));
1152 }
1153 | T_LPAREN notype_param_decl T_RPAREN {
1154 $$ = $2;
1155 }
1156 | direct_param_decl T_LBRACK T_RBRACK {
1157 $$ = add_array($1, false, 0);
1158 }
1159 | direct_param_decl T_LBRACK array_size T_RBRACK {
1160 $$ = add_array($1, true, to_int_constant($3, false));
1161 }
1162 | direct_param_decl param_list asm_or_symbolrename_opt {
1163 $$ = add_function(symbolrename($1, $3), $2);
1164 end_declaration_level();
1165 block_level--;
1166 }
1167 ;
1168
1169 notype_param_decl:
1170 direct_notype_param_decl
1171 | pointer direct_notype_param_decl {
1172 $$ = add_pointer($2, $1);
1173 }
1174 ;
1175
1176 direct_notype_param_decl:
1177 identifier {
1178 $$ = declarator_name(getsym($1));
1179 }
1180 | T_LPAREN notype_param_decl T_RPAREN {
1181 $$ = $2;
1182 }
1183 | direct_notype_param_decl T_LBRACK T_RBRACK {
1184 $$ = add_array($1, false, 0);
1185 }
1186 | direct_notype_param_decl T_LBRACK array_size T_RBRACK {
1187 $$ = add_array($1, true, to_int_constant($3, false));
1188 }
1189 | direct_notype_param_decl param_list asm_or_symbolrename_opt {
1190 $$ = add_function(symbolrename($1, $3), $2);
1191 end_declaration_level();
1192 block_level--;
1193 }
1194 ;
1195
1196 pointer: /* C99 6.7.5 */
1197 asterisk type_qualifier_list_opt {
1198 $$ = merge_qualified_pointer($1, $2);
1199 }
1200 | asterisk type_qualifier_list_opt pointer {
1201 $$ = merge_qualified_pointer($1, $2);
1202 $$ = merge_qualified_pointer($$, $3);
1203 }
1204 ;
1205
1206 asterisk:
1207 T_ASTERISK {
1208 $$ = xcalloc(1, sizeof(*$$));
1209 $$->p_pointer = true;
1210 }
1211 ;
1212
1213 type_qualifier_list_opt:
1214 /* empty */ {
1215 $$ = NULL;
1216 }
1217 | type_qualifier_list
1218 ;
1219
1220 type_qualifier_list: /* C99 6.7.5 */
1221 type_qualifier
1222 | type_qualifier_list type_qualifier {
1223 $$ = merge_qualified_pointer($1, $2);
1224 }
1225 ;
1226
1227 type_qualifier:
1228 T_QUAL {
1229 $$ = xcalloc(1, sizeof(*$$));
1230 if ($1 == CONST) {
1231 $$->p_const = true;
1232 } else if ($1 == VOLATILE) {
1233 $$->p_volatile = true;
1234 } else {
1235 lint_assert($1 == RESTRICT || $1 == THREAD);
1236 }
1237 }
1238 ;
1239
1240 param_list:
1241 id_list_lparen identifier_list T_RPAREN {
1242 $$ = $2;
1243 }
1244 | abstract_decl_param_list
1245 ;
1246
1247 id_list_lparen:
1248 T_LPAREN {
1249 block_level++;
1250 begin_declaration_level(PROTO_ARG);
1251 }
1252 ;
1253
1254 identifier_list:
1255 T_NAME {
1256 $$ = old_style_function_name(getsym($1));
1257 }
1258 | identifier_list T_COMMA T_NAME {
1259 $$ = lnklst($1, old_style_function_name(getsym($3)));
1260 }
1261 | identifier_list error
1262 ;
1263
1264 abstract_decl_param_list:
1265 abstract_decl_lparen T_RPAREN type_attribute_opt {
1266 $$ = NULL;
1267 }
1268 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN type_attribute_opt {
1269 dcs->d_proto = true;
1270 $$ = $2;
1271 }
1272 | abstract_decl_lparen error T_RPAREN type_attribute_opt {
1273 $$ = NULL;
1274 }
1275 ;
1276
1277 abstract_decl_lparen:
1278 T_LPAREN {
1279 block_level++;
1280 begin_declaration_level(PROTO_ARG);
1281 }
1282 ;
1283
1284 vararg_parameter_type_list:
1285 parameter_type_list
1286 | parameter_type_list T_COMMA T_ELLIPSIS {
1287 dcs->d_vararg = true;
1288 $$ = $1;
1289 }
1290 | T_ELLIPSIS {
1291 if (sflag) {
1292 /* ANSI C requires formal parameter before '...' */
1293 error(84);
1294 } else if (!tflag) {
1295 /* ANSI C requires formal parameter before '...' */
1296 warning(84);
1297 }
1298 dcs->d_vararg = true;
1299 $$ = NULL;
1300 }
1301 ;
1302
1303 parameter_type_list:
1304 parameter_declaration
1305 | parameter_type_list T_COMMA parameter_declaration {
1306 $$ = lnklst($1, $3);
1307 }
1308 ;
1309
1310 parameter_declaration:
1311 declmods deftyp {
1312 $$ = declare_argument(abstract_name(), false);
1313 }
1314 | declaration_specifiers deftyp {
1315 $$ = declare_argument(abstract_name(), false);
1316 }
1317 | declmods deftyp notype_param_decl {
1318 $$ = declare_argument($3, false);
1319 }
1320 /*
1321 * param_decl is needed because of following conflict:
1322 * "typedef int a; f(int (a));" could be parsed as
1323 * "function with argument a of type int", or
1324 * "function with an abstract argument of type function".
1325 * This grammar realizes the second case.
1326 */
1327 | declaration_specifiers deftyp param_decl {
1328 $$ = declare_argument($3, false);
1329 }
1330 | declmods deftyp abstract_declarator {
1331 $$ = declare_argument($3, false);
1332 }
1333 | declaration_specifiers deftyp abstract_declarator {
1334 $$ = declare_argument($3, false);
1335 }
1336 ;
1337
1338 asm_or_symbolrename_opt: /* expect only one */
1339 /* empty */ {
1340 $$ = NULL;
1341 }
1342 | T_ASM T_LPAREN T_STRING T_RPAREN {
1343 freeyyv(&$3, T_STRING);
1344 $$ = NULL;
1345 }
1346 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN {
1347 $$ = $3;
1348 }
1349 ;
1350
1351 initializer: /* C99 6.7.8 "Initialization" */
1352 expr %prec T_COMMA {
1353 init_expr($1);
1354 }
1355 | init_lbrace init_rbrace {
1356 /* XXX: Empty braces are not covered by C99 6.7.8. */
1357 }
1358 | init_lbrace initializer_list comma_opt init_rbrace
1359 | error
1360 ;
1361
1362 initializer_list: /* C99 6.7.8 "Initialization" */
1363 initializer_list_item
1364 | initializer_list T_COMMA initializer_list_item
1365 ;
1366
1367 initializer_list_item:
1368 designation initializer
1369 | initializer
1370 ;
1371
1372 designation: /* C99 6.7.8 "Initialization" */
1373 designator_list T_ASSIGN
1374 | identifier T_COLON {
1375 /* GCC style struct or union member name in initializer */
1376 gnuism(315);
1377 add_designator_member($1);
1378 }
1379 ;
1380
1381 designator_list: /* C99 6.7.8 "Initialization" */
1382 designator
1383 | designator_list designator
1384 ;
1385
1386 designator: /* C99 6.7.8 "Initialization" */
1387 T_LBRACK range T_RBRACK {
1388 add_designator_subscript($2);
1389 if (!Sflag)
1390 /* array initializer with des.s is a C9X feature */
1391 warning(321);
1392 }
1393 | T_POINT identifier {
1394 if (!Sflag)
1395 /* struct or union member name in initializer is ... */
1396 warning(313);
1397 add_designator_member($2);
1398 }
1399 ;
1400
1401 range:
1402 constant_expr {
1403 $$.lo = to_int_constant($1, true);
1404 $$.hi = $$.lo;
1405 }
1406 | constant_expr T_ELLIPSIS constant_expr {
1407 $$.lo = to_int_constant($1, true);
1408 $$.hi = to_int_constant($3, true);
1409 /* initialization with '[a...b]' is a GCC extension */
1410 gnuism(340);
1411 }
1412 ;
1413
1414 init_lbrace:
1415 T_LBRACE {
1416 init_lbrace();
1417 }
1418 ;
1419
1420 init_rbrace:
1421 T_RBRACE {
1422 init_rbrace();
1423 }
1424 ;
1425
1426 type_name: /* C99 6.7.6 */
1427 {
1428 begin_declaration_level(ABSTRACT);
1429 } abstract_declaration {
1430 end_declaration_level();
1431 $$ = $2->s_type;
1432 }
1433 ;
1434
1435 abstract_declaration:
1436 noclass_declmods deftyp {
1437 $$ = declare_1_abstract(abstract_name());
1438 }
1439 | noclass_declspecs deftyp {
1440 $$ = declare_1_abstract(abstract_name());
1441 }
1442 | noclass_declmods deftyp abstract_declarator {
1443 $$ = declare_1_abstract($3);
1444 }
1445 | noclass_declspecs deftyp abstract_declarator {
1446 $$ = declare_1_abstract($3);
1447 }
1448 ;
1449
1450 abstract_declarator: /* C99 6.7.6 */
1451 pointer {
1452 $$ = add_pointer(abstract_name(), $1);
1453 }
1454 | direct_abstract_declarator
1455 | pointer direct_abstract_declarator {
1456 $$ = add_pointer($2, $1);
1457 }
1458 | T_TYPEOF term { /* GCC extension */
1459 $$ = mktempsym($2->tn_type);
1460 }
1461 ;
1462
1463 direct_abstract_declarator: /* C99 6.7.6 */
1464 T_LPAREN abstract_declarator T_RPAREN {
1465 $$ = $2;
1466 }
1467 | T_LBRACK T_RBRACK {
1468 $$ = add_array(abstract_name(), false, 0);
1469 }
1470 | T_LBRACK array_size T_RBRACK {
1471 $$ = add_array(abstract_name(), true, to_int_constant($2, false));
1472 }
1473 | type_attribute direct_abstract_declarator {
1474 $$ = $2;
1475 }
1476 | direct_abstract_declarator T_LBRACK T_RBRACK {
1477 $$ = add_array($1, false, 0);
1478 }
1479 | direct_abstract_declarator T_LBRACK T_ASTERISK T_RBRACK { /* C99 */
1480 $$ = add_array($1, false, 0);
1481 }
1482 | direct_abstract_declarator 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_declarator 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_declarator 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->ga_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 $$->ga_arg = $1;
1660 $$->ga_result = $3;
1661 }
1662 | T_DEFAULT T_COLON assignment_expression {
1663 $$ = getblk(sizeof(*$$));
1664 $$->ga_arg = NULL;
1665 $$->ga_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
1732 for_exprs:
1733 for_start declaration_specifiers deftyp notype_init_decls T_SEMI
1734 expr_opt T_SEMI expr_opt T_RPAREN {
1735 /* variable declaration in for loop */
1736 c99ism(325);
1737 for1(NULL, $6, $8);
1738 clear_warning_flags();
1739 }
1740 | for_start expr_opt T_SEMI expr_opt T_SEMI expr_opt T_RPAREN {
1741 for1($2, $4, $6);
1742 clear_warning_flags();
1743 }
1744 ;
1745
1746 expr_opt:
1747 /* empty */ {
1748 $$ = NULL;
1749 }
1750 | expr
1751 ;
1752
1753 jump_statement: /* C99 6.8.6 */
1754 goto identifier T_SEMI {
1755 do_goto(getsym($2));
1756 }
1757 | goto error T_SEMI {
1758 symtyp = FVFT;
1759 }
1760 | T_CONTINUE T_SEMI {
1761 do_continue();
1762 }
1763 | T_BREAK T_SEMI {
1764 do_break();
1765 }
1766 | T_RETURN T_SEMI {
1767 do_return(NULL);
1768 }
1769 | T_RETURN expr T_SEMI {
1770 do_return($2);
1771 }
1772 ;
1773
1774 goto:
1775 T_GOTO {
1776 symtyp = FLABEL;
1777 }
1778 ;
1779
1780 asm_statement:
1781 T_ASM T_LPAREN read_until_rparen T_SEMI {
1782 setasm();
1783 }
1784 | T_ASM T_QUAL T_LPAREN read_until_rparen T_SEMI {
1785 setasm();
1786 }
1787 | T_ASM error
1788 ;
1789
1790 read_until_rparen:
1791 /* empty */ {
1792 ignore_up_to_rparen();
1793 }
1794 ;
1795
1796 constant_expr_list_opt:
1797 /* empty */
1798 | constant_expr_list
1799 ;
1800
1801 constant_expr_list:
1802 constant_expr
1803 | constant_expr_list T_COMMA constant_expr
1804 ;
1805
1806 constant_expr: /* C99 6.6 */
1807 expr %prec T_ASSIGN
1808 ;
1809
1810 expr:
1811 expr T_ASTERISK expr {
1812 $$ = build(MULT, $1, $3);
1813 }
1814 | expr T_MULTIPLICATIVE expr {
1815 $$ = build($2, $1, $3);
1816 }
1817 | expr T_ADDITIVE expr {
1818 $$ = build($2, $1, $3);
1819 }
1820 | expr T_SHIFT expr {
1821 $$ = build($2, $1, $3);
1822 }
1823 | expr T_RELATIONAL expr {
1824 $$ = build($2, $1, $3);
1825 }
1826 | expr T_EQUALITY expr {
1827 $$ = build($2, $1, $3);
1828 }
1829 | expr T_AMPER expr {
1830 $$ = build(BITAND, $1, $3);
1831 }
1832 | expr T_BITXOR expr {
1833 $$ = build(BITXOR, $1, $3);
1834 }
1835 | expr T_BITOR expr {
1836 $$ = build(BITOR, $1, $3);
1837 }
1838 | expr T_LOGAND expr {
1839 $$ = build(LOGAND, $1, $3);
1840 }
1841 | expr T_LOGOR expr {
1842 $$ = build(LOGOR, $1, $3);
1843 }
1844 | expr T_QUEST expr T_COLON expr {
1845 $$ = build(QUEST, $1, build(COLON, $3, $5));
1846 }
1847 | expr T_ASSIGN expr {
1848 $$ = build(ASSIGN, $1, $3);
1849 }
1850 | expr T_OPASSIGN expr {
1851 $$ = build($2, $1, $3);
1852 }
1853 | expr T_COMMA expr {
1854 $$ = build(COMMA, $1, $3);
1855 }
1856 | term
1857 | generic_selection
1858 ;
1859
1860 assignment_expression: /* C99 6.5.16 */
1861 expr %prec T_ASSIGN
1862 ;
1863
1864 term:
1865 T_NAME {
1866 /* XXX really necessary? */
1867 if (yychar < 0)
1868 yychar = yylex();
1869 $$ = new_name_node(getsym($1), yychar);
1870 }
1871 | string {
1872 $$ = new_string_node($1);
1873 }
1874 | T_CON {
1875 $$ = expr_new_constant(gettyp($1->v_tspec), $1);
1876 }
1877 | T_LPAREN expr T_RPAREN {
1878 if ($2 != NULL)
1879 $2->tn_parenthesized = true;
1880 $$ = $2;
1881 }
1882 | T_LPAREN compound_statement_lbrace gcc_statement_expr_list {
1883 block_level--;
1884 mem_block_level--;
1885 begin_initialization(mktempsym(dup_type($3->tn_type)));
1886 mem_block_level++;
1887 block_level++;
1888 /* ({ }) is a GCC extension */
1889 gnuism(320);
1890 } compound_statement_rbrace T_RPAREN {
1891 $$ = new_name_node(*current_initsym(), 0);
1892 end_initialization();
1893 }
1894 | term T_INCDEC {
1895 $$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL);
1896 }
1897 | T_INCDEC term {
1898 $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL);
1899 }
1900 | T_ASTERISK term {
1901 $$ = build(INDIR, $2, NULL);
1902 }
1903 | T_AMPER term {
1904 $$ = build(ADDR, $2, NULL);
1905 }
1906 | T_UNARY term {
1907 $$ = build($1, $2, NULL);
1908 }
1909 | T_ADDITIVE term {
1910 if (tflag && $1 == PLUS) {
1911 /* unary + is illegal in traditional C */
1912 warning(100);
1913 }
1914 $$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL);
1915 }
1916 | term T_LBRACK expr T_RBRACK {
1917 $$ = build(INDIR, build(PLUS, $1, $3), NULL);
1918 }
1919 | term T_LPAREN T_RPAREN {
1920 $$ = new_function_call_node($1, NULL);
1921 }
1922 | term T_LPAREN func_arg_list T_RPAREN {
1923 $$ = new_function_call_node($1, $3);
1924 }
1925 | term point_or_arrow T_NAME {
1926 if ($1 != NULL) {
1927 sym_t *msym;
1928 /*
1929 * XXX struct_or_union_member should be integrated
1930 * in build()
1931 */
1932 if ($2 == ARROW) {
1933 /*
1934 * must do this before struct_or_union_member
1935 * is called
1936 */
1937 $1 = cconv($1);
1938 }
1939 msym = struct_or_union_member($1, $2, getsym($3));
1940 $$ = build($2, $1, new_name_node(msym, 0));
1941 } else {
1942 $$ = NULL;
1943 }
1944 }
1945 | T_REAL term {
1946 $$ = build(REAL, $2, NULL);
1947 }
1948 | T_IMAG term {
1949 $$ = build(IMAG, $2, NULL);
1950 }
1951 | T_EXTENSION term {
1952 $$ = $2;
1953 }
1954 | T_REAL T_LPAREN term T_RPAREN {
1955 $$ = build(REAL, $3, NULL);
1956 }
1957 | T_IMAG T_LPAREN term T_RPAREN {
1958 $$ = build(IMAG, $3, NULL);
1959 }
1960 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA identifier T_RPAREN {
1961 symtyp = FMEMBER;
1962 $$ = build_offsetof($3, getsym($5));
1963 }
1964 | T_SIZEOF term {
1965 $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type);
1966 if ($$ != NULL)
1967 check_expr_misc($2, false, false, false, false, false, true);
1968 }
1969 | T_SIZEOF T_LPAREN type_name T_RPAREN %prec T_SIZEOF {
1970 $$ = build_sizeof($3);
1971 }
1972 | T_ALIGNOF T_LPAREN type_name T_RPAREN {
1973 $$ = build_alignof($3);
1974 }
1975 | T_LPAREN type_name T_RPAREN term %prec T_UNARY {
1976 $$ = cast($4, $2);
1977 }
1978 | T_LPAREN type_name T_RPAREN { /* C99 6.5.2.5 "Compound literals" */
1979 sym_t *tmp = mktempsym($2);
1980 begin_initialization(tmp);
1981 cgram_declare(tmp, true, NULL);
1982 } init_lbrace initializer_list comma_opt init_rbrace {
1983 if (!Sflag)
1984 /* compound literals are a C9X/GCC extension */
1985 gnuism(319);
1986 $$ = new_name_node(*current_initsym(), 0);
1987 end_initialization();
1988 }
1989 ;
1990
1991 /*
1992 * The inner part of a GCC statement-expression of the form ({ ... }).
1993 *
1994 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
1995 */
1996 gcc_statement_expr_list:
1997 gcc_statement_expr_item
1998 | gcc_statement_expr_list gcc_statement_expr_item {
1999 $$ = $2;
2000 }
2001 ;
2002
2003 gcc_statement_expr_item:
2004 declaration {
2005 clear_warning_flags();
2006 $$ = NULL;
2007 }
2008 | non_expr_statement {
2009 $$ = expr_zalloc_tnode();
2010 $$->tn_type = gettyp(VOID);
2011 }
2012 | expr T_SEMI {
2013 if ($1 == NULL) { /* in case of syntax errors */
2014 $$ = expr_zalloc_tnode();
2015 $$->tn_type = gettyp(VOID);
2016 } else {
2017 /* XXX: do that only on the last name */
2018 if ($1->tn_op == NAME)
2019 $1->tn_sym->s_used = true;
2020 $$ = $1;
2021 expr($1, false, false, false, false);
2022 seen_fallthrough = false;
2023 }
2024 }
2025 ;
2026
2027 string:
2028 T_STRING
2029 | T_STRING string2 {
2030 $$ = cat_strings($1, $2);
2031 }
2032 ;
2033
2034 string2:
2035 T_STRING {
2036 if (tflag) {
2037 /* concatenated strings are illegal in traditional C */
2038 warning(219);
2039 }
2040 $$ = $1;
2041 }
2042 | string2 T_STRING {
2043 $$ = cat_strings($1, $2);
2044 }
2045 ;
2046
2047 func_arg_list:
2048 expr %prec T_COMMA {
2049 $$ = new_function_argument_node(NULL, $1);
2050 }
2051 | func_arg_list T_COMMA expr {
2052 $$ = new_function_argument_node($1, $3);
2053 }
2054 ;
2055
2056 point_or_arrow:
2057 T_POINT {
2058 symtyp = FMEMBER;
2059 $$ = POINT;
2060 }
2061 | T_ARROW {
2062 symtyp = FMEMBER;
2063 $$ = ARROW;
2064 }
2065 ;
2066
2067 identifier: /* C99 6.4.2.1 */
2068 T_NAME {
2069 $$ = $1;
2070 cgram_debug("name '%s'", $$->sb_name);
2071 }
2072 | T_TYPENAME {
2073 $$ = $1;
2074 cgram_debug("typename '%s'", $$->sb_name);
2075 }
2076 ;
2077
2078 comma_opt:
2079 /* empty */
2080 | T_COMMA
2081 ;
2082
2083 %%
2084
2085 /* ARGSUSED */
2086 int
2087 yyerror(const char *msg)
2088 {
2089 /* syntax error '%s' */
2090 error(249, yytext);
2091 if (++sytxerr >= 5)
2092 norecover();
2093 return 0;
2094 }
2095
2096 static void
2097 cgram_declare(sym_t *decl, bool initflg, sbuf_t *renaming)
2098 {
2099 declare(decl, initflg, renaming);
2100 if (renaming != NULL)
2101 freeyyv(&renaming, T_NAME);
2102 }
2103
2104 /*
2105 * Discard all input tokens up to and including the next
2106 * unmatched right paren
2107 */
2108 static void
2109 ignore_up_to_rparen(void)
2110 {
2111 int level;
2112
2113 if (yychar < 0)
2114 yychar = yylex();
2115 freeyyv(&yylval, yychar);
2116
2117 level = 1;
2118 while (yychar != T_RPAREN || --level > 0) {
2119 if (yychar == T_LPAREN) {
2120 level++;
2121 } else if (yychar <= 0) {
2122 break;
2123 }
2124 freeyyv(&yylval, yychar = yylex());
2125 }
2126
2127 yyclearin;
2128 }
2129
2130 static sym_t *
2131 symbolrename(sym_t *s, sbuf_t *sb)
2132 {
2133 if (sb != NULL)
2134 s->s_rename = sb->sb_name;
2135 return s;
2136 }
2137