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