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