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