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