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