cgram.y revision 1.239 1 %{
2 /* $NetBSD: cgram.y,v 1.239 2021/06/27 21:36:14 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.239 2021/06/27 21:36:14 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 type_decl:
1079 type_direct_decl {
1080 $$ = $1;
1081 }
1082 | pointer type_direct_decl {
1083 $$ = add_pointer($2, $1);
1084 }
1085 ;
1086
1087 notype_direct_decl:
1088 T_NAME {
1089 $$ = declarator_name(getsym($1));
1090 }
1091 | T_LPAREN type_decl T_RPAREN {
1092 $$ = $2;
1093 }
1094 | type_attribute notype_direct_decl {
1095 $$ = $2;
1096 }
1097 | notype_direct_decl T_LBRACK T_RBRACK {
1098 $$ = add_array($1, false, 0);
1099 }
1100 | notype_direct_decl T_LBRACK array_size T_RBRACK {
1101 $$ = add_array($1, true, to_int_constant($3, false));
1102 }
1103 | notype_direct_decl param_list opt_asm_or_symbolrename {
1104 $$ = add_function(symbolrename($1, $3), $2);
1105 end_declaration_level();
1106 block_level--;
1107 }
1108 | notype_direct_decl type_attribute_list
1109 /* TODO: either add { $$ = $1 } everywhere or remove it everywhere. */
1110 ;
1111
1112 /*
1113 * TODO: document whether the subtle difference between 'identifier' here and
1114 * 'T_NAME' in notype_direct_decl above is on purpose.
1115 */
1116 type_direct_decl:
1117 identifier {
1118 $$ = declarator_name(getsym($1));
1119 }
1120 | T_LPAREN type_decl T_RPAREN {
1121 $$ = $2;
1122 }
1123 | type_attribute type_direct_decl {
1124 $$ = $2;
1125 }
1126 | type_direct_decl T_LBRACK T_RBRACK {
1127 $$ = add_array($1, false, 0);
1128 }
1129 | type_direct_decl T_LBRACK array_size T_RBRACK {
1130 $$ = add_array($1, true, to_int_constant($3, false));
1131 }
1132 | type_direct_decl param_list opt_asm_or_symbolrename {
1133 $$ = add_function(symbolrename($1, $3), $2);
1134 end_declaration_level();
1135 block_level--;
1136 }
1137 | type_direct_decl type_attribute_list
1138 ;
1139
1140 /*
1141 * param_decl and notype_param_decl exist to avoid a conflict in
1142 * argument lists. A typename enclosed in parens should always be
1143 * treated as a typename, not an argument.
1144 * "typedef int a; f(int (a));" is "typedef int a; f(int foo(a));"
1145 * not "typedef int a; f(int a);"
1146 */
1147 param_decl:
1148 direct_param_decl {
1149 $$ = $1;
1150 }
1151 | pointer direct_param_decl {
1152 $$ = add_pointer($2, $1);
1153 }
1154 ;
1155
1156 /* TODO: consistently use 'opt' either as prefix or as suffix */
1157 opt_type_qualifier_list:
1158 /* empty */
1159 | type_qualifier_list
1160 ;
1161
1162 array_size:
1163 opt_type_qualifier_list T_SCLASS constant_expr {
1164 /* C11 6.7.6.3p7 */
1165 if ($2 != STATIC)
1166 yyerror("Bad attribute");
1167 /* static array size is a C11 extension */
1168 c11ism(343);
1169 $$ = $3;
1170 }
1171 | constant_expr {
1172 $$ = $1;
1173 }
1174 ;
1175
1176 direct_param_decl:
1177 identifier type_attribute_list {
1178 $$ = declarator_name(getsym($1));
1179 }
1180 | identifier {
1181 $$ = declarator_name(getsym($1));
1182 }
1183 | T_LPAREN notype_param_decl T_RPAREN {
1184 $$ = $2;
1185 }
1186 | direct_param_decl T_LBRACK T_RBRACK {
1187 $$ = add_array($1, false, 0);
1188 }
1189 | direct_param_decl T_LBRACK array_size T_RBRACK {
1190 $$ = add_array($1, true, to_int_constant($3, false));
1191 }
1192 | direct_param_decl param_list opt_asm_or_symbolrename {
1193 $$ = add_function(symbolrename($1, $3), $2);
1194 end_declaration_level();
1195 block_level--;
1196 }
1197 ;
1198
1199 notype_param_decl:
1200 direct_notype_param_decl {
1201 $$ = $1;
1202 }
1203 | pointer direct_notype_param_decl {
1204 $$ = add_pointer($2, $1);
1205 }
1206 ;
1207
1208 direct_notype_param_decl:
1209 identifier {
1210 $$ = declarator_name(getsym($1));
1211 }
1212 | T_LPAREN notype_param_decl T_RPAREN {
1213 $$ = $2;
1214 }
1215 | direct_notype_param_decl T_LBRACK T_RBRACK {
1216 $$ = add_array($1, false, 0);
1217 }
1218 | direct_notype_param_decl T_LBRACK array_size T_RBRACK {
1219 $$ = add_array($1, true, to_int_constant($3, false));
1220 }
1221 | direct_notype_param_decl param_list opt_asm_or_symbolrename {
1222 $$ = add_function(symbolrename($1, $3), $2);
1223 end_declaration_level();
1224 block_level--;
1225 }
1226 ;
1227
1228 /* TODO: rename 'pointer' to something less ambiguous, maybe 'pointer_level' */
1229 pointer:
1230 asterisk {
1231 $$ = $1;
1232 }
1233 | asterisk type_qualifier_list {
1234 /* TODO: rename pqinf_t to be more expressive */
1235 /* TODO: then rename the merge function */
1236 $$ = merge_pointers_and_qualifiers($1, $2);
1237 }
1238 | asterisk pointer {
1239 $$ = merge_pointers_and_qualifiers($1, $2);
1240 }
1241 | asterisk type_qualifier_list pointer {
1242 $$ = merge_pointers_and_qualifiers($1, $2);
1243 $$ = merge_pointers_and_qualifiers($$, $3);
1244 }
1245 ;
1246
1247 asterisk:
1248 T_ASTERISK {
1249 $$ = xcalloc(1, sizeof(*$$));
1250 $$->p_pointer = true;
1251 }
1252 ;
1253
1254 /* TODO: try whether type_qualifier_list_opt makes the code simpler */
1255 type_qualifier_list:
1256 type_qualifier {
1257 $$ = $1;
1258 }
1259 | type_qualifier_list type_qualifier {
1260 $$ = merge_pointers_and_qualifiers($1, $2);
1261 }
1262 ;
1263
1264 type_qualifier:
1265 T_QUAL {
1266 $$ = xcalloc(1, sizeof(*$$));
1267 if ($1 == CONST) {
1268 $$->p_const = true;
1269 } else if ($1 == VOLATILE) {
1270 $$->p_volatile = true;
1271 } else {
1272 lint_assert($1 == RESTRICT || $1 == THREAD);
1273 }
1274 }
1275 ;
1276
1277 param_list:
1278 id_list_lparen identifier_list T_RPAREN {
1279 $$ = $2;
1280 }
1281 | abstract_decl_param_list {
1282 $$ = $1;
1283 }
1284 ;
1285
1286 id_list_lparen:
1287 T_LPAREN {
1288 block_level++;
1289 begin_declaration_level(PROTO_ARG);
1290 }
1291 ;
1292
1293 identifier_list:
1294 T_NAME {
1295 $$ = old_style_function_name(getsym($1));
1296 }
1297 | identifier_list T_COMMA T_NAME {
1298 $$ = lnklst($1, old_style_function_name(getsym($3)));
1299 }
1300 | identifier_list error {
1301 $$ = $1;
1302 }
1303 ;
1304
1305 abstract_decl_param_list:
1306 abstract_decl_lparen T_RPAREN opt_type_attribute {
1307 $$ = NULL;
1308 }
1309 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN opt_type_attribute {
1310 dcs->d_proto = true;
1311 $$ = $2;
1312 }
1313 | abstract_decl_lparen error T_RPAREN opt_type_attribute {
1314 $$ = NULL;
1315 }
1316 ;
1317
1318 abstract_decl_lparen:
1319 T_LPAREN {
1320 block_level++;
1321 begin_declaration_level(PROTO_ARG);
1322 }
1323 ;
1324
1325 vararg_parameter_type_list:
1326 parameter_type_list {
1327 $$ = $1;
1328 }
1329 | parameter_type_list T_COMMA T_ELLIPSIS {
1330 dcs->d_vararg = true;
1331 $$ = $1;
1332 }
1333 | T_ELLIPSIS {
1334 if (sflag) {
1335 /* ANSI C requires formal parameter before '...' */
1336 error(84);
1337 } else if (!tflag) {
1338 /* ANSI C requires formal parameter before '...' */
1339 warning(84);
1340 }
1341 dcs->d_vararg = true;
1342 $$ = NULL;
1343 }
1344 ;
1345
1346 parameter_type_list:
1347 parameter_declaration {
1348 $$ = $1;
1349 }
1350 | parameter_type_list T_COMMA parameter_declaration {
1351 $$ = lnklst($1, $3);
1352 }
1353 ;
1354
1355 parameter_declaration:
1356 declmods deftyp {
1357 $$ = declare_argument(abstract_name(), false);
1358 }
1359 | declaration_specifiers deftyp {
1360 $$ = declare_argument(abstract_name(), false);
1361 }
1362 | declmods deftyp notype_param_decl {
1363 $$ = declare_argument($3, false);
1364 }
1365 /*
1366 * param_decl is needed because of following conflict:
1367 * "typedef int a; f(int (a));" could be parsed as
1368 * "function with argument a of type int", or
1369 * "function with an abstract argument of type function".
1370 * This grammar realizes the second case.
1371 */
1372 | declaration_specifiers deftyp param_decl {
1373 $$ = declare_argument($3, false);
1374 }
1375 | declmods deftyp abstract_decl {
1376 $$ = declare_argument($3, false);
1377 }
1378 | declaration_specifiers deftyp abstract_decl {
1379 $$ = declare_argument($3, false);
1380 }
1381 ;
1382
1383 opt_asm_or_symbolrename: /* expect only one */
1384 /* empty */ {
1385 $$ = NULL;
1386 }
1387 | T_ASM T_LPAREN T_STRING T_RPAREN {
1388 freeyyv(&$3, T_STRING);
1389 $$ = NULL;
1390 }
1391 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN {
1392 $$ = $3;
1393 }
1394 ;
1395
1396 initializer: /* C99 6.7.8 "Initialization" */
1397 expr %prec T_COMMA {
1398 init_expr($1);
1399 }
1400 | init_lbrace init_rbrace {
1401 /* XXX: Empty braces are not covered by C99 6.7.8. */
1402 }
1403 | init_lbrace initializer_list comma_opt init_rbrace
1404 | error
1405 ;
1406
1407 initializer_list: /* C99 6.7.8 "Initialization" */
1408 initializer_list_item
1409 | initializer_list T_COMMA initializer_list_item
1410 ;
1411
1412 initializer_list_item:
1413 designation initializer
1414 | initializer
1415 ;
1416
1417 designation: /* C99 6.7.8 "Initialization" */
1418 designator_list T_ASSIGN
1419 | identifier T_COLON {
1420 /* GCC style struct or union member name in initializer */
1421 gnuism(315);
1422 add_designator_member($1);
1423 }
1424 ;
1425
1426 designator_list: /* C99 6.7.8 "Initialization" */
1427 designator
1428 | designator_list designator
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 range:
1447 constant_expr {
1448 $$.lo = to_int_constant($1, true);
1449 $$.hi = $$.lo;
1450 }
1451 | constant_expr T_ELLIPSIS constant_expr {
1452 $$.lo = to_int_constant($1, true);
1453 $$.hi = to_int_constant($3, true);
1454 /* initialization with '[a...b]' is a GNU extension */
1455 gnuism(340);
1456 }
1457 ;
1458
1459 init_lbrace:
1460 T_LBRACE {
1461 init_lbrace();
1462 }
1463 ;
1464
1465 init_rbrace:
1466 T_RBRACE {
1467 init_rbrace();
1468 }
1469 ;
1470
1471 type_name:
1472 {
1473 begin_declaration_level(ABSTRACT);
1474 } abstract_declaration {
1475 end_declaration_level();
1476 $$ = $2->s_type;
1477 }
1478 ;
1479
1480 abstract_declaration:
1481 noclass_declmods deftyp {
1482 $$ = declare_1_abstract(abstract_name());
1483 }
1484 | noclass_declspecs deftyp {
1485 $$ = declare_1_abstract(abstract_name());
1486 }
1487 | noclass_declmods deftyp abstract_decl {
1488 $$ = declare_1_abstract($3);
1489 }
1490 | noclass_declspecs deftyp abstract_decl {
1491 $$ = declare_1_abstract($3);
1492 }
1493 ;
1494
1495 /* TODO: abstract_declaration and abstract_decl sound too similar */
1496 abstract_decl:
1497 pointer {
1498 $$ = add_pointer(abstract_name(), $1);
1499 }
1500 | direct_abstract_decl {
1501 $$ = $1;
1502 }
1503 | pointer direct_abstract_decl {
1504 $$ = add_pointer($2, $1);
1505 }
1506 | T_TYPEOF term {
1507 $$ = mktempsym($2->tn_type);
1508 }
1509 ;
1510
1511 direct_abstract_decl:
1512 T_LPAREN abstract_decl T_RPAREN {
1513 $$ = $2;
1514 }
1515 | T_LBRACK T_RBRACK {
1516 $$ = add_array(abstract_name(), false, 0);
1517 }
1518 | T_LBRACK array_size T_RBRACK {
1519 $$ = add_array(abstract_name(), true, to_int_constant($2, false));
1520 }
1521 | type_attribute direct_abstract_decl {
1522 $$ = $2;
1523 }
1524 | direct_abstract_decl T_LBRACK T_RBRACK {
1525 $$ = add_array($1, false, 0);
1526 }
1527 | direct_abstract_decl T_LBRACK array_size T_RBRACK {
1528 $$ = add_array($1, true, to_int_constant($3, false));
1529 }
1530 | abstract_decl_param_list opt_asm_or_symbolrename {
1531 $$ = add_function(symbolrename(abstract_name(), $2), $1);
1532 end_declaration_level();
1533 block_level--;
1534 }
1535 | direct_abstract_decl abstract_decl_param_list opt_asm_or_symbolrename {
1536 $$ = add_function(symbolrename($1, $3), $2);
1537 end_declaration_level();
1538 block_level--;
1539 }
1540 | direct_abstract_decl type_attribute_list
1541 ;
1542
1543 non_expr_statement:
1544 type_attribute T_SEMI
1545 | labeled_statement
1546 | compound_statement
1547 | selection_statement
1548 | iteration_statement
1549 | jump_statement {
1550 seen_fallthrough = false;
1551 }
1552 | asm_statement
1553 ;
1554
1555 statement: /* C99 6.8 */
1556 expr_statement
1557 | non_expr_statement
1558 ;
1559
1560 labeled_statement: /* C99 6.8.1 */
1561 label opt_type_attribute statement
1562 ;
1563
1564 label:
1565 T_NAME T_COLON {
1566 symtyp = FLABEL;
1567 named_label(getsym($1));
1568 }
1569 | T_CASE constant_expr T_COLON {
1570 case_label($2);
1571 seen_fallthrough = true;
1572 }
1573 | T_CASE constant_expr T_ELLIPSIS constant_expr T_COLON {
1574 /* XXX: We don't fill all cases */
1575 case_label($2);
1576 seen_fallthrough = true;
1577 }
1578 | T_DEFAULT T_COLON {
1579 default_label();
1580 seen_fallthrough = true;
1581 }
1582 ;
1583
1584 compound_statement: /* C99 6.8.2 */
1585 compound_statement_lbrace compound_statement_rbrace
1586 | compound_statement_lbrace block_item_list compound_statement_rbrace
1587 ;
1588
1589 compound_statement_lbrace:
1590 T_LBRACE {
1591 block_level++;
1592 mem_block_level++;
1593 begin_declaration_level(AUTO);
1594 }
1595 ;
1596
1597 compound_statement_rbrace:
1598 T_RBRACE {
1599 end_declaration_level();
1600 freeblk();
1601 mem_block_level--;
1602 block_level--;
1603 seen_fallthrough = false;
1604 }
1605 ;
1606
1607 block_item_list:
1608 block_item
1609 | block_item_list block_item {
1610 if (!Sflag && $1 && !$2)
1611 /* declarations after statements is a C99 feature */
1612 c99ism(327);
1613 $$ = $1 || $2;
1614 }
1615 ;
1616
1617 block_item:
1618 statement {
1619 $$ = true;
1620 restore_warning_flags();
1621 }
1622 | declaration {
1623 $$ = false;
1624 restore_warning_flags();
1625 }
1626 ;
1627
1628 expr_statement:
1629 expr T_SEMI {
1630 expr($1, false, false, false, false);
1631 seen_fallthrough = false;
1632 }
1633 | T_SEMI {
1634 seen_fallthrough = false;
1635 }
1636 ;
1637
1638 selection_statement: /* C99 6.8.4 */
1639 if_without_else {
1640 save_warning_flags();
1641 if2();
1642 if3(false);
1643 }
1644 | if_without_else T_ELSE {
1645 save_warning_flags();
1646 if2();
1647 } statement {
1648 clear_warning_flags();
1649 if3(true);
1650 }
1651 | if_without_else T_ELSE error {
1652 clear_warning_flags();
1653 if3(false);
1654 }
1655 | switch_expr statement {
1656 clear_warning_flags();
1657 switch2();
1658 }
1659 | switch_expr error {
1660 clear_warning_flags();
1661 switch2();
1662 }
1663 ;
1664
1665 if_without_else:
1666 if_expr statement
1667 | if_expr error
1668 ;
1669
1670 if_expr:
1671 T_IF T_LPAREN expr T_RPAREN {
1672 if1($3);
1673 clear_warning_flags();
1674 }
1675 ;
1676
1677 switch_expr:
1678 T_SWITCH T_LPAREN expr T_RPAREN {
1679 switch1($3);
1680 clear_warning_flags();
1681 }
1682 ;
1683
1684 generic_selection: /* C11 6.5.1.1 */
1685 T_GENERIC T_LPAREN assignment_expression T_COMMA
1686 generic_assoc_list T_RPAREN {
1687 /* generic selection requires C11 or later */
1688 c11ism(345);
1689 $$ = build_generic_selection($3, $5);
1690 }
1691 ;
1692
1693 generic_assoc_list: /* C11 6.5.1.1 */
1694 generic_association
1695 | generic_assoc_list T_COMMA generic_association {
1696 $3->gat_prev = $1;
1697 $$ = $3;
1698 }
1699 ;
1700
1701 generic_association: /* C11 6.5.1.1 */
1702 type_name T_COLON expr {
1703 $$ = getblk(sizeof(*$$));
1704 $$->gat_arg = $1;
1705 $$->gat_result = $3;
1706 }
1707 | T_DEFAULT T_COLON expr {
1708 $$ = getblk(sizeof(*$$));
1709 $$->gat_arg = NULL;
1710 $$->gat_result = $3;
1711 }
1712 ;
1713
1714 do_statement: /* C99 6.8.5 */
1715 do statement {
1716 clear_warning_flags();
1717 }
1718 ;
1719
1720 iteration_statement: /* C99 6.8.5 */
1721 while_expr statement {
1722 clear_warning_flags();
1723 while2();
1724 }
1725 | while_expr error {
1726 clear_warning_flags();
1727 while2();
1728 }
1729 | do_statement do_while_expr {
1730 do2($2);
1731 seen_fallthrough = false;
1732 }
1733 | do error {
1734 clear_warning_flags();
1735 do2(NULL);
1736 }
1737 | for_exprs statement {
1738 clear_warning_flags();
1739 for2();
1740 end_declaration_level();
1741 block_level--;
1742 }
1743 | for_exprs error {
1744 clear_warning_flags();
1745 for2();
1746 end_declaration_level();
1747 block_level--;
1748 }
1749 ;
1750
1751 while_expr:
1752 T_WHILE T_LPAREN expr T_RPAREN {
1753 while1($3);
1754 clear_warning_flags();
1755 }
1756 ;
1757
1758 do:
1759 T_DO {
1760 do1();
1761 }
1762 ;
1763
1764 do_while_expr:
1765 T_WHILE T_LPAREN expr T_RPAREN T_SEMI {
1766 $$ = $3;
1767 }
1768 ;
1769
1770 for_start:
1771 T_FOR T_LPAREN {
1772 begin_declaration_level(AUTO);
1773 block_level++;
1774 }
1775 ;
1776 for_exprs:
1777 for_start declaration_specifiers deftyp notype_init_decls T_SEMI
1778 opt_expr T_SEMI opt_expr T_RPAREN {
1779 /* variable declaration in for loop */
1780 c99ism(325);
1781 for1(NULL, $6, $8);
1782 clear_warning_flags();
1783 }
1784 | for_start opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPAREN {
1785 for1($2, $4, $6);
1786 clear_warning_flags();
1787 }
1788 ;
1789
1790 opt_expr:
1791 /* empty */ {
1792 $$ = NULL;
1793 }
1794 | expr {
1795 $$ = $1;
1796 }
1797 ;
1798
1799 jump_statement: /* C99 6.8.6 */
1800 goto identifier T_SEMI {
1801 do_goto(getsym($2));
1802 }
1803 | goto error T_SEMI {
1804 symtyp = FVFT;
1805 }
1806 | T_CONTINUE T_SEMI {
1807 do_continue();
1808 }
1809 | T_BREAK T_SEMI {
1810 do_break();
1811 }
1812 | T_RETURN T_SEMI {
1813 do_return(NULL);
1814 }
1815 | T_RETURN expr T_SEMI {
1816 do_return($2);
1817 }
1818 ;
1819
1820 goto:
1821 T_GOTO {
1822 symtyp = FLABEL;
1823 }
1824 ;
1825
1826 asm_statement:
1827 T_ASM T_LPAREN read_until_rparen T_SEMI {
1828 setasm();
1829 }
1830 | T_ASM T_QUAL T_LPAREN read_until_rparen T_SEMI {
1831 setasm();
1832 }
1833 | T_ASM error
1834 ;
1835
1836 read_until_rparen:
1837 /* empty */ {
1838 ignore_up_to_rparen();
1839 }
1840 ;
1841
1842 constant_expr_list_opt:
1843 /* empty */
1844 | constant_expr_list
1845 ;
1846
1847 constant_expr_list:
1848 constant_expr
1849 | constant_expr_list T_COMMA constant_expr
1850 ;
1851
1852 constant_expr: /* C99 6.6 */
1853 expr %prec T_ASSIGN {
1854 $$ = $1;
1855 }
1856 ;
1857
1858 expr:
1859 expr T_ASTERISK expr {
1860 $$ = build(MULT, $1, $3);
1861 }
1862 | expr T_MULTIPLICATIVE expr {
1863 $$ = build($2, $1, $3);
1864 }
1865 | expr T_ADDITIVE expr {
1866 $$ = build($2, $1, $3);
1867 }
1868 | expr T_SHIFT expr {
1869 $$ = build($2, $1, $3);
1870 }
1871 | expr T_RELATIONAL expr {
1872 $$ = build($2, $1, $3);
1873 }
1874 | expr T_EQUALITY expr {
1875 $$ = build($2, $1, $3);
1876 }
1877 | expr T_AMPER expr {
1878 $$ = build(BITAND, $1, $3);
1879 }
1880 | expr T_BITXOR expr {
1881 $$ = build(BITXOR, $1, $3);
1882 }
1883 | expr T_BITOR expr {
1884 $$ = build(BITOR, $1, $3);
1885 }
1886 | expr T_LOGAND expr {
1887 $$ = build(LOGAND, $1, $3);
1888 }
1889 | expr T_LOGOR expr {
1890 $$ = build(LOGOR, $1, $3);
1891 }
1892 | expr T_QUEST expr T_COLON expr {
1893 $$ = build(QUEST, $1, build(COLON, $3, $5));
1894 }
1895 | expr T_ASSIGN expr {
1896 $$ = build(ASSIGN, $1, $3);
1897 }
1898 | expr T_OPASSIGN expr {
1899 $$ = build($2, $1, $3);
1900 }
1901 | expr T_COMMA expr {
1902 $$ = build(COMMA, $1, $3);
1903 }
1904 | term {
1905 $$ = $1;
1906 }
1907 | generic_selection {
1908 $$ = $1;
1909 }
1910 ;
1911
1912 assignment_expression: /* C99 6.5.16 */
1913 expr %prec T_ASSIGN
1914 ;
1915
1916 term:
1917 T_NAME {
1918 /* XXX really necessary? */
1919 if (yychar < 0)
1920 yychar = yylex();
1921 $$ = new_name_node(getsym($1), yychar);
1922 }
1923 | string {
1924 $$ = new_string_node($1);
1925 }
1926 | T_CON {
1927 $$ = expr_new_constant(gettyp($1->v_tspec), $1);
1928 }
1929 | T_LPAREN expr T_RPAREN {
1930 if ($2 != NULL)
1931 $2->tn_parenthesized = true;
1932 $$ = $2;
1933 }
1934 | T_LPAREN compound_statement_lbrace gcc_statement_expr_list {
1935 block_level--;
1936 mem_block_level--;
1937 begin_initialization(mktempsym(dup_type($3->tn_type)));
1938 mem_block_level++;
1939 block_level++;
1940 /* ({ }) is a GCC extension */
1941 gnuism(320);
1942 } compound_statement_rbrace T_RPAREN {
1943 $$ = new_name_node(*current_initsym(), 0);
1944 end_initialization();
1945 }
1946 | term T_INCDEC {
1947 $$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL);
1948 }
1949 | T_INCDEC term {
1950 $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL);
1951 }
1952 | T_ASTERISK term {
1953 $$ = build(INDIR, $2, NULL);
1954 }
1955 | T_AMPER term {
1956 $$ = build(ADDR, $2, NULL);
1957 }
1958 | T_UNARY term {
1959 $$ = build($1, $2, NULL);
1960 }
1961 | T_ADDITIVE term {
1962 if (tflag && $1 == PLUS) {
1963 /* unary + is illegal in traditional C */
1964 warning(100);
1965 }
1966 $$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL);
1967 }
1968 | term T_LBRACK expr T_RBRACK {
1969 $$ = build(INDIR, build(PLUS, $1, $3), NULL);
1970 }
1971 | term T_LPAREN T_RPAREN {
1972 $$ = new_function_call_node($1, NULL);
1973 }
1974 | term T_LPAREN func_arg_list T_RPAREN {
1975 $$ = new_function_call_node($1, $3);
1976 }
1977 | term point_or_arrow T_NAME {
1978 if ($1 != NULL) {
1979 sym_t *msym;
1980 /*
1981 * XXX struct_or_union_member should be integrated
1982 * in build()
1983 */
1984 if ($2 == ARROW) {
1985 /*
1986 * must do this before struct_or_union_member
1987 * is called
1988 */
1989 $1 = cconv($1);
1990 }
1991 msym = struct_or_union_member($1, $2, getsym($3));
1992 $$ = build($2, $1, new_name_node(msym, 0));
1993 } else {
1994 $$ = NULL;
1995 }
1996 }
1997 | T_REAL term {
1998 $$ = build(REAL, $2, NULL);
1999 }
2000 | T_IMAG term {
2001 $$ = build(IMAG, $2, NULL);
2002 }
2003 | T_EXTENSION term {
2004 $$ = $2;
2005 }
2006 | T_REAL T_LPAREN term T_RPAREN {
2007 $$ = build(REAL, $3, NULL);
2008 }
2009 | T_IMAG T_LPAREN term T_RPAREN {
2010 $$ = build(IMAG, $3, NULL);
2011 }
2012 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA identifier T_RPAREN {
2013 symtyp = FMEMBER;
2014 $$ = build_offsetof($3, getsym($5));
2015 }
2016 | T_SIZEOF term {
2017 $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type);
2018 if ($$ != NULL)
2019 check_expr_misc($2, false, false, false, false, false, true);
2020 }
2021 | T_SIZEOF T_LPAREN type_name T_RPAREN %prec T_SIZEOF {
2022 $$ = build_sizeof($3);
2023 }
2024 | T_ALIGNOF T_LPAREN type_name T_RPAREN {
2025 $$ = build_alignof($3);
2026 }
2027 | T_LPAREN type_name T_RPAREN term %prec T_UNARY {
2028 $$ = cast($4, $2);
2029 }
2030 | T_LPAREN type_name T_RPAREN { /* C99 6.5.2.5 "Compound literals" */
2031 sym_t *tmp = mktempsym($2);
2032 begin_initialization(tmp);
2033 cgram_declare(tmp, true, NULL);
2034 } init_lbrace initializer_list comma_opt init_rbrace {
2035 if (!Sflag)
2036 /* compound literals are a C9X/GCC extension */
2037 gnuism(319);
2038 $$ = new_name_node(*current_initsym(), 0);
2039 end_initialization();
2040 }
2041 ;
2042
2043 /*
2044 * The inner part of a GCC statement-expression of the form ({ ... }).
2045 *
2046 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
2047 */
2048 gcc_statement_expr_list:
2049 gcc_statement_expr_item
2050 | gcc_statement_expr_list gcc_statement_expr_item {
2051 $$ = $2;
2052 }
2053 ;
2054
2055 gcc_statement_expr_item:
2056 declaration {
2057 clear_warning_flags();
2058 $$ = NULL;
2059 }
2060 | non_expr_statement {
2061 $$ = expr_zalloc_tnode();
2062 $$->tn_type = gettyp(VOID);
2063 }
2064 | expr T_SEMI {
2065 if ($1 == NULL) { /* in case of syntax errors */
2066 $$ = expr_zalloc_tnode();
2067 $$->tn_type = gettyp(VOID);
2068 } else {
2069 /* XXX: do that only on the last name */
2070 if ($1->tn_op == NAME)
2071 $1->tn_sym->s_used = true;
2072 $$ = $1;
2073 expr($1, false, false, false, false);
2074 seen_fallthrough = false;
2075 }
2076 }
2077 ;
2078
2079 string:
2080 T_STRING {
2081 $$ = $1;
2082 }
2083 | T_STRING string2 {
2084 $$ = cat_strings($1, $2);
2085 }
2086 ;
2087
2088 string2:
2089 T_STRING {
2090 if (tflag) {
2091 /* concatenated strings are illegal in traditional C */
2092 warning(219);
2093 }
2094 $$ = $1;
2095 }
2096 | string2 T_STRING {
2097 $$ = cat_strings($1, $2);
2098 }
2099 ;
2100
2101 func_arg_list:
2102 expr %prec T_COMMA {
2103 $$ = new_function_argument_node(NULL, $1);
2104 }
2105 | func_arg_list T_COMMA expr {
2106 $$ = new_function_argument_node($1, $3);
2107 }
2108 ;
2109
2110 point_or_arrow:
2111 T_POINT {
2112 symtyp = FMEMBER;
2113 $$ = POINT;
2114 }
2115 | T_ARROW {
2116 symtyp = FMEMBER;
2117 $$ = ARROW;
2118 }
2119 ;
2120
2121 identifier: /* C99 6.4.2.1 */
2122 T_NAME {
2123 $$ = $1;
2124 cgram_debug("name '%s'", $$->sb_name);
2125 }
2126 | T_TYPENAME {
2127 $$ = $1;
2128 cgram_debug("typename '%s'", $$->sb_name);
2129 }
2130 ;
2131
2132 comma_opt:
2133 T_COMMA
2134 | /* empty */
2135 ;
2136 %%
2137
2138 /* ARGSUSED */
2139 int
2140 yyerror(const char *msg)
2141 {
2142 /* syntax error '%s' */
2143 error(249, yytext);
2144 if (++sytxerr >= 5)
2145 norecover();
2146 return 0;
2147 }
2148
2149 static void
2150 cgram_declare(sym_t *decl, bool initflg, sbuf_t *renaming)
2151 {
2152 declare(decl, initflg, renaming);
2153 if (renaming != NULL)
2154 freeyyv(&renaming, T_NAME);
2155 }
2156
2157 /*
2158 * Discard all input tokens up to and including the next
2159 * unmatched right paren
2160 */
2161 static void
2162 ignore_up_to_rparen(void)
2163 {
2164 int level;
2165
2166 if (yychar < 0)
2167 yychar = yylex();
2168 freeyyv(&yylval, yychar);
2169
2170 level = 1;
2171 while (yychar != T_RPAREN || --level > 0) {
2172 if (yychar == T_LPAREN) {
2173 level++;
2174 } else if (yychar <= 0) {
2175 break;
2176 }
2177 freeyyv(&yylval, yychar = yylex());
2178 }
2179
2180 yyclearin;
2181 }
2182
2183 static sym_t *
2184 symbolrename(sym_t *s, sbuf_t *sb)
2185 {
2186 if (sb != NULL)
2187 s->s_rename = sb->sb_name;
2188 return s;
2189 }
2190