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