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