cgram.y revision 1.448 1 %{
2 /* $NetBSD: cgram.y,v 1.448 2023/07/10 11:46: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)
38 __RCSID("$NetBSD: cgram.y,v 1.448 2023/07/10 11:46: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 size_t 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 #define LWARN_NOTHING_SAVED (-3)
68 static int saved_lwarn = LWARN_NOTHING_SAVED;
69
70 static void cgram_declare(sym_t *, bool, sbuf_t *);
71 static void read_until_rparen(void);
72 static sym_t *symbolrename(sym_t *, sbuf_t *);
73
74
75 /* ARGSUSED */
76 static void
77 clear_warning_flags_loc(const char *file, size_t line)
78 {
79 debug_step("%s:%zu: clearing flags", file, line);
80 clear_warn_flags();
81 saved_lwarn = LWARN_NOTHING_SAVED;
82 }
83
84 /* ARGSUSED */
85 static void
86 save_warning_flags_loc(const char *file, size_t line)
87 {
88 /*
89 * There used to be an assertion that saved_lwarn is
90 * LWARN_NOTHING_SAVED here, but that triggered for the following
91 * code:
92 *
93 * void function(int x) { if (x > 0) if (x > 1) return; }
94 *
95 * It didn't trigger if the inner 'if' was enclosed in braces though.
96 *
97 * TODO: If actually needed, add support for nested suppression of
98 * warnings.
99 */
100 debug_step("%s:%zu: saving flags %d", file, line, lwarn);
101 saved_lwarn = lwarn;
102 }
103
104 /* ARGSUSED */
105 static void
106 restore_warning_flags_loc(const char *file, size_t line)
107 {
108 if (saved_lwarn != LWARN_NOTHING_SAVED) {
109 lwarn = saved_lwarn;
110 debug_step("%s:%zu: restoring flags %d", file, line, lwarn);
111 /*
112 * Do not set 'saved_lwarn = LWARN_NOTHING_SAVED' here, to
113 * avoid triggering the assertion in save_warning_flags_loc.
114 */
115 } else
116 clear_warning_flags_loc(file, line);
117 }
118
119 #define clear_warning_flags() clear_warning_flags_loc(__FILE__, __LINE__)
120 #define save_warning_flags() save_warning_flags_loc(__FILE__, __LINE__)
121 #define restore_warning_flags() restore_warning_flags_loc(__FILE__, __LINE__)
122
123 static bool
124 is_either(const char *s, const char *a, const char *b)
125 {
126 return strcmp(s, a) == 0 || strcmp(s, b) == 0;
127 }
128
129 #if YYDEBUG && (YYBYACC || YYBISON)
130 #define YYSTYPE_TOSTRING cgram_to_string
131 #endif
132 #if YYDEBUG && YYBISON
133 #define YYPRINT cgram_print
134 #endif
135
136 %}
137
138 %expect 131
139
140 %union {
141 val_t *y_val;
142 sbuf_t *y_name;
143 sym_t *y_sym;
144 op_t y_op;
145 scl_t y_scl;
146 tspec_t y_tspec;
147 tqual_t y_tqual;
148 type_t *y_type;
149 tnode_t *y_tnode;
150 range_t y_range;
151 strg_t *y_string;
152 qual_ptr *y_qual_ptr;
153 bool y_seen_statement;
154 struct generic_association *y_generic;
155 struct array_size y_array_size;
156 bool y_in_system_header;
157 };
158
159 %token T_LBRACE T_RBRACE T_LBRACK T_RBRACK T_LPAREN T_RPAREN
160 %token T_POINT T_ARROW
161 %token T_COMPLEMENT T_LOGNOT
162 %token <y_op> T_INCDEC
163 %token T_SIZEOF
164 %token T_BUILTIN_OFFSETOF
165 %token T_TYPEOF
166 %token T_EXTENSION
167 %token T_ALIGNAS
168 %token T_ALIGNOF
169 %token T_ASTERISK
170 %token <y_op> T_MULTIPLICATIVE
171 %token <y_op> T_ADDITIVE
172 %token <y_op> T_SHIFT
173 %token <y_op> T_RELATIONAL
174 %token <y_op> T_EQUALITY
175 %token T_AMPER
176 %token T_BITXOR
177 %token T_BITOR
178 %token T_LOGAND
179 %token T_LOGOR
180 %token T_QUEST
181 %token T_COLON
182 %token T_ASSIGN
183 %token <y_op> T_OPASSIGN
184 %token T_COMMA
185 %token T_SEMI
186 %token T_ELLIPSIS
187 %token T_REAL
188 %token T_IMAG
189 %token T_GENERIC
190 %token T_NORETURN
191
192 /* storage classes (extern, static, auto, register and typedef) */
193 %token <y_scl> T_SCLASS
194
195 /*
196 * predefined type keywords (char, int, short, long, unsigned, signed,
197 * float, double, void); see T_TYPENAME for types from typedef
198 */
199 %token <y_tspec> T_TYPE
200
201 /* qualifiers (const, volatile, restrict, _Thread_local) */
202 %token <y_tqual> T_QUAL
203 %token <y_tqual> T_ATOMIC
204
205 /* struct or union */
206 %token <y_tspec> T_STRUCT_OR_UNION
207
208 /* remaining keywords */
209 %token T_ASM
210 %token T_BREAK
211 %token T_CASE
212 %token T_CONTINUE
213 %token T_DEFAULT
214 %token T_DO
215 %token T_ELSE
216 %token T_ENUM
217 %token T_FOR
218 %token T_GOTO
219 %token T_IF
220 %token T_PACKED
221 %token T_RETURN
222 %token T_SWITCH
223 %token T_SYMBOLRENAME
224 %token T_WHILE
225 %token T_STATIC_ASSERT
226
227 %token T_ATTRIBUTE
228
229 %left T_THEN
230 %left T_ELSE
231 %right T_QUEST T_COLON
232 %left T_LOGOR
233 %left T_LOGAND
234 %left T_BITOR
235 %left T_BITXOR
236 %left T_AMPER
237 %left T_EQUALITY
238 %left T_RELATIONAL
239 %left T_SHIFT
240 %left T_ADDITIVE
241 %left T_ASTERISK T_MULTIPLICATIVE
242
243 %token <y_name> T_NAME
244 %token <y_name> T_TYPENAME
245 %token <y_val> T_CON
246 %token <y_string> T_STRING
247
248 /* No type for program. */
249 %type <y_sym> identifier_sym
250 %type <y_name> identifier
251 %type <y_string> string
252 %type <y_tnode> primary_expression
253 %type <y_tnode> generic_selection
254 %type <y_generic> generic_assoc_list
255 %type <y_generic> generic_association
256 %type <y_tnode> postfix_expression
257 /* No type for comma_opt. */
258 %type <y_tnode> gcc_statement_expr_list
259 %type <y_tnode> gcc_statement_expr_item
260 %type <y_op> point_or_arrow
261 %type <y_tnode> argument_expression_list
262 %type <y_tnode> unary_expression
263 %type <y_tnode> cast_expression
264 %type <y_tnode> expression_opt
265 %type <y_tnode> conditional_expression
266 %type <y_tnode> assignment_expression
267 %type <y_tnode> expression
268 %type <y_tnode> constant_expr
269 /* No type for declaration_or_error. */
270 /* No type for declaration. */
271 /* No type for begin_type_declaration_specifiers. */
272 /* No type for begin_type_declmods. */
273 /* No type for begin_type_specifier_qualifier_list. */
274 /* No type for begin_type_specifier_qualifier_list_postfix. */
275 %type <y_type> begin_type_typespec
276 /* No type for begin_type_qualifier_list. */
277 /* No type for declmod. */
278 /* No type for type_attribute_list_opt. */
279 /* No type for type_attribute_list. */
280 /* No type for type_attribute_opt. */
281 /* No type for type_attribute. */
282 /* No type for begin_type. */
283 /* No type for end_type. */
284 %type <y_type> type_specifier
285 %type <y_type> notype_type_specifier
286 %type <y_type> atomic_type_specifier
287 %type <y_type> struct_or_union_specifier
288 %type <y_tspec> struct_or_union
289 %type <y_sym> braced_struct_declaration_list
290 /* No type for struct_declaration_lbrace. */
291 %type <y_sym> struct_declaration_list_with_rbrace
292 %type <y_sym> struct_declaration_list
293 %type <y_sym> struct_declaration
294 %type <y_sym> notype_struct_declarators
295 %type <y_sym> type_struct_declarators
296 %type <y_sym> notype_struct_declarator
297 %type <y_sym> type_struct_declarator
298 %type <y_type> enum_specifier
299 /* No type for enum. */
300 %type <y_sym> enum_declaration
301 /* No type for enum_decl_lbrace. */
302 %type <y_sym> enums_with_opt_comma
303 %type <y_sym> enumerator_list
304 %type <y_sym> enumerator
305 %type <y_tqual> type_qualifier
306 /* No type for atomic. */
307 %type <y_qual_ptr> pointer
308 %type <y_qual_ptr> asterisk
309 %type <y_qual_ptr> type_qualifier_list_opt
310 %type <y_qual_ptr> type_qualifier_list
311 %type <y_qual_ptr> type_qualifier_list_elem
312 /* No type for notype_init_declarators. */
313 /* No type for type_init_declarators. */
314 /* No type for notype_init_declarator. */
315 /* No type for type_init_declarator. */
316 %type <y_sym> notype_declarator
317 %type <y_sym> type_declarator
318 %type <y_sym> notype_direct_declarator
319 %type <y_sym> type_direct_declarator
320 %type <y_sym> type_param_declarator
321 %type <y_sym> notype_param_declarator
322 %type <y_sym> direct_param_declarator
323 %type <y_sym> direct_notype_param_declarator
324 %type <y_sym> param_list
325 /* No type for id_list_lparen. */
326 /* No type for abstract_decl_lparen. */
327 %type <y_array_size> array_size_opt
328 %type <y_tnode> array_size
329 %type <y_sym> identifier_list
330 %type <y_type> type_name
331 %type <y_sym> abstract_declaration
332 %type <y_sym> abstract_declarator
333 %type <y_sym> direct_abstract_declarator
334 %type <y_sym> abstract_decl_param_list
335 /* No type for abstract_decl_lparen. */
336 %type <y_sym> vararg_parameter_type_list
337 %type <y_sym> parameter_type_list
338 %type <y_sym> parameter_declaration
339 /* No type for braced_initializer. */
340 /* No type for initializer. */
341 /* No type for initializer_list. */
342 /* No type for initializer_list_item. */
343 /* No type for designation. */
344 /* No type for begin_designation. */
345 /* No type for designator_list. */
346 /* No type for designator. */
347 /* No type for static_assert_declaration. */
348 %type <y_range> range
349 /* No type for init_lbrace. */
350 /* No type for init_rbrace. */
351 %type <y_name> asm_or_symbolrename_opt
352 /* No type for statement. */
353 /* No type for non_expr_statement. */
354 /* No type for labeled_statement. */
355 /* No type for label. */
356 /* No type for compound_statement. */
357 /* No type for compound_statement_lbrace. */
358 /* No type for compound_statement_rbrace. */
359 %type <y_seen_statement> block_item_list
360 %type <y_seen_statement> block_item
361 /* No type for expression_statement. */
362 /* No type for selection_statement. */
363 /* No type for if_without_else. */
364 /* No type for if_expr. */
365 /* No type for switch_expr. */
366 /* No type for iteration_statement. */
367 /* No type for while_expr. */
368 /* No type for do_statement. */
369 /* No type for do. */
370 %type <y_tnode> do_while_expr
371 /* No type for for_start. */
372 /* No type for for_exprs. */
373 /* No type for jump_statement. */
374 /* No type for goto. */
375 /* No type for asm_statement. */
376 /* No type for read_until_rparen. */
377 /* No type for translation_unit. */
378 /* No type for external_declaration. */
379 /* No type for top_level_declaration. */
380 /* No type for function_definition. */
381 %type <y_sym> func_declarator
382 /* No type for arg_declaration_list_opt. */
383 /* No type for arg_declaration_list. */
384 /* No type for arg_declaration. */
385 /* No type for gcc_attribute_specifier_list_opt. */
386 /* No type for gcc_attribute_specifier_list. */
387 /* No type for gcc_attribute_specifier. */
388 /* No type for gcc_attribute_list. */
389 /* No type for gcc_attribute. */
390 /* No type for gcc_attribute_parameters. */
391 %type <y_in_system_header> sys
392
393 %{
394 #if YYDEBUG && YYBISON
395 static inline void cgram_print(FILE *, int, YYSTYPE);
396 #endif
397 %}
398
399 %%
400
401 program:
402 /* empty */ {
403 /* TODO: Make this an error in C99 mode as well. */
404 if (!allow_trad && !allow_c99) {
405 /* empty translation unit */
406 error(272);
407 } else if (allow_c90) {
408 /* empty translation unit */
409 warning(272);
410 }
411 }
412 | translation_unit
413 ;
414
415 identifier_sym: /* helper for struct/union/enum */
416 identifier {
417 $$ = getsym($1);
418 }
419 ;
420
421 /* K&R ???, C90 ???, C99 6.4.2.1, C11 ??? */
422 identifier:
423 T_NAME {
424 debug_step("cgram: name '%s'", $1->sb_name);
425 $$ = $1;
426 }
427 | T_TYPENAME {
428 debug_step("cgram: typename '%s'", $1->sb_name);
429 $$ = $1;
430 }
431 ;
432
433 /* see C99 6.4.5, string literals are joined by 5.1.1.2 */
434 string:
435 T_STRING
436 | string T_STRING {
437 if (!allow_c90) {
438 /* concatenated strings are illegal in traditional C */
439 warning(219);
440 }
441 $$ = cat_strings($1, $2);
442 }
443 ;
444
445 /* K&R 7.1, C90 ???, C99 6.5.1, C11 6.5.1 */
446 primary_expression:
447 T_NAME {
448 bool sys_name, sys_next;
449 sys_name = in_system_header;
450 if (yychar < 0)
451 yychar = yylex();
452 sys_next = in_system_header;
453 in_system_header = sys_name;
454 $$ = build_name(getsym($1), yychar == T_LPAREN);
455 in_system_header = sys_next;
456 }
457 | T_CON {
458 $$ = build_constant(gettyp($1->v_tspec), $1);
459 }
460 | string {
461 $$ = build_string($1);
462 }
463 | T_LPAREN expression T_RPAREN {
464 if ($2 != NULL)
465 $2->tn_parenthesized = true;
466 $$ = $2;
467 }
468 | generic_selection
469 /* GCC primary-expression, see c_parser_postfix_expression */
470 /* TODO: C99 7.17p3 allows not only an identifier but a designator. */
471 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA identifier T_RPAREN {
472 symtyp = FMEMBER;
473 $$ = build_offsetof($3, getsym($5));
474 }
475 ;
476
477 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1 */
478 generic_selection:
479 T_GENERIC T_LPAREN assignment_expression T_COMMA
480 generic_assoc_list T_RPAREN {
481 /* generic selection requires C11 or later */
482 c11ism(345);
483 $$ = build_generic_selection($3, $5);
484 }
485 ;
486
487 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1 */
488 generic_assoc_list:
489 generic_association
490 | generic_assoc_list T_COMMA generic_association {
491 $3->ga_prev = $1;
492 $$ = $3;
493 }
494 ;
495
496 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1 */
497 generic_association:
498 type_name T_COLON assignment_expression {
499 $$ = block_zero_alloc(sizeof(*$$));
500 $$->ga_arg = $1;
501 $$->ga_result = $3;
502 }
503 | T_DEFAULT T_COLON assignment_expression {
504 $$ = block_zero_alloc(sizeof(*$$));
505 $$->ga_arg = NULL;
506 $$->ga_result = $3;
507 }
508 ;
509
510 /* K&R 7.1, C90 ???, C99 6.5.2, C11 6.5.2 */
511 postfix_expression:
512 primary_expression
513 | postfix_expression T_LBRACK sys expression T_RBRACK {
514 $$ = build_unary(INDIR, $3, build_binary($1, PLUS, $3, $4));
515 }
516 | postfix_expression T_LPAREN sys T_RPAREN {
517 $$ = build_function_call($1, $3, NULL);
518 }
519 | postfix_expression T_LPAREN sys argument_expression_list T_RPAREN {
520 $$ = build_function_call($1, $3, $4);
521 }
522 | postfix_expression point_or_arrow sys T_NAME {
523 $$ = build_member_access($1, $2, $3, $4);
524 }
525 | postfix_expression T_INCDEC sys {
526 $$ = build_unary($2 == INC ? INCAFT : DECAFT, $3, $1);
527 }
528 | T_LPAREN type_name T_RPAREN { /* C99 6.5.2.5 "Compound literals" */
529 sym_t *tmp = mktempsym($2);
530 begin_initialization(tmp);
531 cgram_declare(tmp, true, NULL);
532 } braced_initializer {
533 if (!allow_c99)
534 /* compound literals are a C99/GCC extension */
535 gnuism(319);
536 $$ = build_name(current_initsym(), false);
537 end_initialization();
538 }
539 | T_LPAREN compound_statement_lbrace {
540 begin_statement_expr();
541 } gcc_statement_expr_list {
542 do_statement_expr($4);
543 } compound_statement_rbrace T_RPAREN {
544 $$ = end_statement_expr();
545 }
546 ;
547
548 comma_opt: /* helper for 'postfix_expression' */
549 /* empty */
550 | T_COMMA
551 ;
552
553 /*
554 * The inner part of a GCC statement-expression of the form ({ ... }).
555 *
556 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
557 */
558 gcc_statement_expr_list:
559 gcc_statement_expr_item
560 | gcc_statement_expr_list gcc_statement_expr_item {
561 $$ = $2;
562 }
563 ;
564
565 gcc_statement_expr_item:
566 declaration_or_error {
567 clear_warning_flags();
568 $$ = NULL;
569 }
570 | non_expr_statement {
571 $$ = expr_alloc_tnode();
572 $$->tn_type = gettyp(VOID);
573 }
574 | T_SEMI {
575 $$ = expr_alloc_tnode();
576 $$->tn_type = gettyp(VOID);
577 }
578 | expression T_SEMI {
579 if ($1 == NULL) { /* in case of syntax errors */
580 $$ = expr_alloc_tnode();
581 $$->tn_type = gettyp(VOID);
582 } else {
583 /* XXX: do that only on the last name */
584 if ($1->tn_op == NAME)
585 $1->tn_sym->s_used = true;
586 expr($1, false, false, false, false);
587 seen_fallthrough = false;
588 $$ = $1;
589 }
590 }
591 ;
592
593 point_or_arrow: /* helper for 'postfix_expression' */
594 T_POINT {
595 symtyp = FMEMBER;
596 $$ = POINT;
597 }
598 | T_ARROW {
599 symtyp = FMEMBER;
600 $$ = ARROW;
601 }
602 ;
603
604 /* K&R 7.1, C90 ???, C99 6.5.2, C11 6.5.2 */
605 argument_expression_list:
606 assignment_expression {
607 $$ = build_function_argument(NULL, $1);
608 }
609 | argument_expression_list T_COMMA assignment_expression {
610 $$ = build_function_argument($1, $3);
611 }
612 ;
613
614 /* K&R 7.2, C90 ???, C99 6.5.3, C11 6.5.3 */
615 unary_expression:
616 postfix_expression
617 | T_INCDEC sys unary_expression {
618 $$ = build_unary($1 == INC ? INCBEF : DECBEF, $2, $3);
619 }
620 | T_AMPER sys cast_expression {
621 $$ = build_unary(ADDR, $2, $3);
622 }
623 | T_ASTERISK sys cast_expression {
624 $$ = build_unary(INDIR, $2, $3);
625 }
626 | T_ADDITIVE sys cast_expression {
627 if (!allow_c90 && $1 == PLUS) {
628 /* unary '+' is illegal in traditional C */
629 warning(100);
630 }
631 $$ = build_unary($1 == PLUS ? UPLUS : UMINUS, $2, $3);
632 }
633 | T_COMPLEMENT sys cast_expression {
634 $$ = build_unary(COMPL, $2, $3);
635 }
636 | T_LOGNOT sys cast_expression {
637 $$ = build_unary(NOT, $2, $3);
638 }
639 | T_REAL sys cast_expression { /* GCC c_parser_unary_expression */
640 $$ = build_unary(REAL, $2, $3);
641 }
642 | T_IMAG sys cast_expression { /* GCC c_parser_unary_expression */
643 $$ = build_unary(IMAG, $2, $3);
644 }
645 | T_EXTENSION cast_expression { /* GCC c_parser_unary_expression */
646 $$ = $2;
647 }
648 | T_SIZEOF unary_expression {
649 $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type);
650 if ($$ != NULL)
651 check_expr_misc($2,
652 false, false, false, false, false, true);
653 }
654 | T_SIZEOF T_LPAREN type_name T_RPAREN {
655 $$ = build_sizeof($3);
656 }
657 | T_ALIGNOF unary_expression {
658 /* non type argument to alignof is a GCC extension */
659 gnuism(349);
660 lint_assert($2 != NULL);
661 $$ = build_alignof($2->tn_type);
662 }
663 /* K&R ---, C90 ---, C99 ---, C11 6.5.3 */
664 | T_ALIGNOF T_LPAREN type_name T_RPAREN {
665 /* TODO: c11ism */
666 $$ = build_alignof($3);
667 }
668 ;
669
670 /* The rule 'unary_operator' is inlined into unary_expression. */
671
672 /* K&R 7.2, C90 ???, C99 6.5.4, C11 6.5.4 */
673 cast_expression:
674 unary_expression
675 | T_LPAREN type_name T_RPAREN cast_expression {
676 $$ = cast($4, $2);
677 }
678 ;
679
680 expression_opt:
681 /* empty */ {
682 $$ = NULL;
683 }
684 | expression
685 ;
686
687 /* 'conditional_expression' also implements 'multiplicative_expression'. */
688 /* 'conditional_expression' also implements 'additive_expression'. */
689 /* 'conditional_expression' also implements 'shift_expression'. */
690 /* 'conditional_expression' also implements 'relational_expression'. */
691 /* 'conditional_expression' also implements 'equality_expression'. */
692 /* 'conditional_expression' also implements 'AND_expression'. */
693 /* 'conditional_expression' also implements 'exclusive_OR_expression'. */
694 /* 'conditional_expression' also implements 'inclusive_OR_expression'. */
695 /* 'conditional_expression' also implements 'logical_AND_expression'. */
696 /* 'conditional_expression' also implements 'logical_OR_expression'. */
697 /* K&R ???, C90 ???, C99 6.5.5 to 6.5.15, C11 6.5.5 to 6.5.15 */
698 conditional_expression:
699 cast_expression
700 | conditional_expression T_ASTERISK sys conditional_expression {
701 $$ = build_binary($1, MULT, $3, $4);
702 }
703 | conditional_expression T_MULTIPLICATIVE sys conditional_expression {
704 $$ = build_binary($1, $2, $3, $4);
705 }
706 | conditional_expression T_ADDITIVE sys conditional_expression {
707 $$ = build_binary($1, $2, $3, $4);
708 }
709 | conditional_expression T_SHIFT sys conditional_expression {
710 $$ = build_binary($1, $2, $3, $4);
711 }
712 | conditional_expression T_RELATIONAL sys conditional_expression {
713 $$ = build_binary($1, $2, $3, $4);
714 }
715 | conditional_expression T_EQUALITY sys conditional_expression {
716 $$ = build_binary($1, $2, $3, $4);
717 }
718 | conditional_expression T_AMPER sys conditional_expression {
719 $$ = build_binary($1, BITAND, $3, $4);
720 }
721 | conditional_expression T_BITXOR sys conditional_expression {
722 $$ = build_binary($1, BITXOR, $3, $4);
723 }
724 | conditional_expression T_BITOR sys conditional_expression {
725 $$ = build_binary($1, BITOR, $3, $4);
726 }
727 | conditional_expression T_LOGAND sys conditional_expression {
728 $$ = build_binary($1, LOGAND, $3, $4);
729 }
730 | conditional_expression T_LOGOR sys conditional_expression {
731 $$ = build_binary($1, LOGOR, $3, $4);
732 }
733 | conditional_expression T_QUEST sys
734 expression T_COLON sys conditional_expression {
735 $$ = build_binary($1, QUEST, $3,
736 build_binary($4, COLON, $6, $7));
737 }
738 ;
739
740 /* K&R ???, C90 ???, C99 6.5.16, C11 6.5.16 */
741 assignment_expression:
742 conditional_expression
743 | unary_expression T_ASSIGN sys assignment_expression {
744 $$ = build_binary($1, ASSIGN, $3, $4);
745 }
746 | unary_expression T_OPASSIGN sys assignment_expression {
747 $$ = build_binary($1, $2, $3, $4);
748 }
749 ;
750
751 /* K&R ???, C90 ???, C99 6.5.17, C11 6.5.17 */
752 expression:
753 assignment_expression
754 | expression T_COMMA sys assignment_expression {
755 $$ = build_binary($1, COMMA, $3, $4);
756 }
757 ;
758
759 constant_expr: /* C99 6.6 */
760 conditional_expression
761 ;
762
763 declaration_or_error:
764 declaration
765 | error T_SEMI
766 ;
767
768 declaration: /* C99 6.7 */
769 begin_type_declmods end_type T_SEMI {
770 if (dcs->d_scl == TYPEDEF) {
771 /* typedef declares no type name */
772 warning(72);
773 } else {
774 /* empty declaration */
775 warning(2);
776 }
777 }
778 | begin_type_declmods end_type notype_init_declarators T_SEMI {
779 if (dcs->d_scl == TYPEDEF) {
780 /* syntax error '%s' */
781 error(249, "missing base type for typedef");
782 } else {
783 /* old-style declaration; add 'int' */
784 error(1);
785 }
786 }
787 | begin_type_declaration_specifiers end_type T_SEMI {
788 if (dcs->d_scl == TYPEDEF) {
789 /* typedef declares no type name */
790 warning(72);
791 } else if (!dcs->d_nonempty_decl) {
792 /* empty declaration */
793 warning(2);
794 }
795 }
796 | begin_type_declaration_specifiers end_type
797 type_init_declarators T_SEMI
798 | static_assert_declaration
799 ;
800
801 begin_type_declaration_specifiers: /* see C99 6.7 */
802 begin_type_typespec {
803 dcs_add_type($1);
804 }
805 | begin_type_declmods type_specifier {
806 dcs_add_type($2);
807 }
808 | type_attribute begin_type_declaration_specifiers
809 | begin_type_declaration_specifiers declmod
810 | begin_type_declaration_specifiers notype_type_specifier {
811 dcs_add_type($2);
812 }
813 ;
814
815 begin_type_declmods: /* see C99 6.7 */
816 begin_type type_qualifier {
817 dcs_add_qualifier($2);
818 }
819 | begin_type T_SCLASS {
820 dcs_add_storage_class($2);
821 }
822 | begin_type_declmods declmod
823 ;
824
825 begin_type_specifier_qualifier_list: /* see C11 6.7.2.1 */
826 begin_type_specifier_qualifier_list_postfix
827 | type_attribute_list begin_type_specifier_qualifier_list_postfix
828 ;
829
830 begin_type_specifier_qualifier_list_postfix:
831 begin_type_typespec {
832 dcs_add_type($1);
833 }
834 | begin_type_qualifier_list type_specifier {
835 dcs_add_type($2);
836 }
837 | begin_type_specifier_qualifier_list_postfix type_qualifier {
838 dcs_add_qualifier($2);
839 }
840 | begin_type_specifier_qualifier_list_postfix notype_type_specifier {
841 dcs_add_type($2);
842 }
843 | begin_type_specifier_qualifier_list_postfix type_attribute
844 ;
845
846 begin_type_typespec:
847 begin_type notype_type_specifier {
848 $$ = $2;
849 }
850 | begin_type T_TYPENAME {
851 $$ = getsym($2)->s_type;
852 }
853 ;
854
855 begin_type_qualifier_list:
856 begin_type type_qualifier {
857 dcs_add_qualifier($2);
858 }
859 | begin_type_qualifier_list type_qualifier {
860 dcs_add_qualifier($2);
861 }
862 ;
863
864 declmod:
865 type_qualifier {
866 dcs_add_qualifier($1);
867 }
868 | T_SCLASS {
869 dcs_add_storage_class($1);
870 }
871 | type_attribute_list
872 ;
873
874 type_attribute_list_opt:
875 /* empty */
876 | type_attribute_list
877 ;
878
879 type_attribute_list:
880 type_attribute
881 | type_attribute_list type_attribute
882 ;
883
884 type_attribute_opt:
885 /* empty */
886 | type_attribute
887 ;
888
889 type_attribute: /* See C11 6.7 declaration-specifiers */
890 gcc_attribute_specifier
891 | T_ALIGNAS T_LPAREN type_specifier T_RPAREN /* C11 6.7.5 */
892 | T_ALIGNAS T_LPAREN constant_expr T_RPAREN /* C11 6.7.5 */
893 | T_PACKED {
894 dcs_add_packed();
895 }
896 | T_NORETURN
897 ;
898
899 begin_type:
900 /* empty */ {
901 dcs_begin_type();
902 }
903 ;
904
905 end_type:
906 /* empty */ {
907 dcs_end_type();
908 }
909 ;
910
911 type_specifier: /* C99 6.7.2 */
912 notype_type_specifier
913 | T_TYPENAME {
914 $$ = getsym($1)->s_type;
915 }
916 ;
917
918 notype_type_specifier: /* see C99 6.7.2 */
919 T_TYPE {
920 $$ = gettyp($1);
921 }
922 | T_TYPEOF T_LPAREN expression T_RPAREN { /* GCC extension */
923 $$ = $3 != NULL ? block_dup_type($3->tn_type) : gettyp(INT);
924 $$->t_typeof = true;
925 }
926 | atomic_type_specifier
927 | struct_or_union_specifier {
928 end_declaration_level();
929 $$ = $1;
930 }
931 | enum_specifier {
932 end_declaration_level();
933 $$ = $1;
934 }
935 ;
936
937 /* K&R ---, C90 ---, C99 ---, C11 6.7.2.4 */
938 atomic_type_specifier:
939 atomic T_LPAREN type_name T_RPAREN {
940 $$ = $3;
941 }
942 ;
943
944 struct_or_union_specifier: /* C99 6.7.2.1 */
945 struct_or_union identifier_sym {
946 /*
947 * STDC requires that "struct a;" always introduces
948 * a new tag if "a" is not declared at current level
949 *
950 * yychar is valid because otherwise the parser would not
951 * have been able to decide if it must shift or reduce
952 */
953 $$ = make_tag_type($2, $1, false, yychar == T_SEMI);
954 }
955 | struct_or_union identifier_sym {
956 dcs->d_tag_type = make_tag_type($2, $1, true, false);
957 } braced_struct_declaration_list {
958 $$ = complete_struct_or_union($4);
959 }
960 | struct_or_union {
961 dcs->d_tag_type = make_tag_type(NULL, $1, true, false);
962 } braced_struct_declaration_list {
963 $$ = complete_struct_or_union($3);
964 }
965 | struct_or_union error {
966 symtyp = FVFT;
967 $$ = gettyp(INT);
968 }
969 ;
970
971 struct_or_union: /* C99 6.7.2.1 */
972 T_STRUCT_OR_UNION {
973 symtyp = FTAG;
974 begin_declaration_level($1 == STRUCT ? DLK_STRUCT : DLK_UNION);
975 dcs->d_sou_size_in_bits = 0;
976 dcs->d_sou_align_in_bits = CHAR_SIZE;
977 $$ = $1;
978 }
979 | struct_or_union type_attribute
980 ;
981
982 braced_struct_declaration_list: /* see C99 6.7.2.1 */
983 struct_declaration_lbrace struct_declaration_list_with_rbrace {
984 $$ = $2;
985 }
986 ;
987
988 struct_declaration_lbrace: /* see C99 6.7.2.1 */
989 T_LBRACE {
990 symtyp = FVFT;
991 }
992 ;
993
994 struct_declaration_list_with_rbrace: /* see C99 6.7.2.1 */
995 struct_declaration_list T_RBRACE
996 | T_RBRACE {
997 /* XXX: This is not allowed by any C standard. */
998 $$ = NULL;
999 }
1000 ;
1001
1002 struct_declaration_list: /* C99 6.7.2.1 */
1003 struct_declaration
1004 | struct_declaration_list struct_declaration {
1005 $$ = concat_symbols($1, $2);
1006 }
1007 ;
1008
1009 struct_declaration: /* C99 6.7.2.1 */
1010 begin_type_qualifier_list end_type {
1011 /* ^^ There is no check for the missing type-specifier. */
1012 /* too late, i know, but getsym() compensates it */
1013 symtyp = FMEMBER;
1014 } notype_struct_declarators type_attribute_opt T_SEMI {
1015 symtyp = FVFT;
1016 $$ = $4;
1017 }
1018 | begin_type_specifier_qualifier_list end_type {
1019 symtyp = FMEMBER;
1020 } type_struct_declarators type_attribute_opt T_SEMI {
1021 symtyp = FVFT;
1022 $$ = $4;
1023 }
1024 | begin_type_qualifier_list end_type type_attribute_opt T_SEMI {
1025 /* syntax error '%s' */
1026 error(249, "member without type");
1027 $$ = NULL;
1028 }
1029 | begin_type_specifier_qualifier_list end_type type_attribute_opt
1030 T_SEMI {
1031 symtyp = FVFT;
1032 if (!allow_c11 && !allow_gcc)
1033 /* anonymous struct/union members is a C11 feature */
1034 warning(49);
1035 if (is_struct_or_union(dcs->d_type->t_tspec))
1036 $$ = declare_unnamed_member();
1037 else {
1038 /* syntax error '%s' */
1039 error(249, "unnamed member");
1040 $$ = NULL;
1041 }
1042 }
1043 | static_assert_declaration {
1044 $$ = NULL;
1045 }
1046 | error T_SEMI {
1047 symtyp = FVFT;
1048 $$ = NULL;
1049 }
1050 ;
1051
1052 notype_struct_declarators:
1053 notype_struct_declarator {
1054 $$ = declare_member($1);
1055 }
1056 | notype_struct_declarators {
1057 symtyp = FMEMBER;
1058 } T_COMMA type_struct_declarator {
1059 $$ = concat_symbols($1, declare_member($4));
1060 }
1061 ;
1062
1063 type_struct_declarators:
1064 type_struct_declarator {
1065 $$ = declare_member($1);
1066 }
1067 | type_struct_declarators {
1068 symtyp = FMEMBER;
1069 } T_COMMA type_struct_declarator {
1070 $$ = concat_symbols($1, declare_member($4));
1071 }
1072 ;
1073
1074 notype_struct_declarator:
1075 notype_declarator
1076 | notype_declarator T_COLON constant_expr { /* C99 6.7.2.1 */
1077 $$ = set_bit_field_width($1, to_int_constant($3, true));
1078 }
1079 | {
1080 symtyp = FVFT;
1081 } T_COLON constant_expr { /* C99 6.7.2.1 */
1082 $$ = set_bit_field_width(NULL, to_int_constant($3, true));
1083 }
1084 ;
1085
1086 type_struct_declarator:
1087 type_declarator
1088 | type_declarator T_COLON constant_expr {
1089 $$ = set_bit_field_width($1, to_int_constant($3, true));
1090 }
1091 | {
1092 symtyp = FVFT;
1093 } T_COLON constant_expr {
1094 $$ = set_bit_field_width(NULL, to_int_constant($3, true));
1095 }
1096 ;
1097
1098 /* K&R ---, C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2 */
1099 enum_specifier: /* C99 6.7.2.2 */
1100 enum gcc_attribute_specifier_list_opt identifier_sym {
1101 $$ = make_tag_type($3, ENUM, false, false);
1102 }
1103 | enum gcc_attribute_specifier_list_opt identifier_sym {
1104 dcs->d_tag_type = make_tag_type($3, ENUM, true, false);
1105 } enum_declaration /*gcc_attribute_specifier_list_opt*/ {
1106 $$ = complete_enum($5);
1107 }
1108 | enum gcc_attribute_specifier_list_opt {
1109 dcs->d_tag_type = make_tag_type(NULL, ENUM, true, false);
1110 } enum_declaration /*gcc_attribute_specifier_list_opt*/ {
1111 $$ = complete_enum($4);
1112 }
1113 | enum error {
1114 symtyp = FVFT;
1115 $$ = gettyp(INT);
1116 }
1117 ;
1118
1119 enum: /* helper for C99 6.7.2.2 */
1120 T_ENUM {
1121 symtyp = FTAG;
1122 begin_declaration_level(DLK_ENUM);
1123 }
1124 ;
1125
1126 enum_declaration: /* helper for C99 6.7.2.2 */
1127 enum_decl_lbrace enums_with_opt_comma T_RBRACE {
1128 $$ = $2;
1129 }
1130 ;
1131
1132 enum_decl_lbrace: /* helper for C99 6.7.2.2 */
1133 T_LBRACE {
1134 symtyp = FVFT;
1135 enumval = 0;
1136 }
1137 ;
1138
1139 enums_with_opt_comma: /* helper for C99 6.7.2.2 */
1140 enumerator_list
1141 | enumerator_list T_COMMA {
1142 if (!allow_c99 && !allow_trad) {
1143 /* trailing ',' prohibited in enum declaration */
1144 error(54);
1145 } else {
1146 /* trailing ',' prohibited in enum declaration */
1147 c99ism(54);
1148 }
1149 $$ = $1;
1150 }
1151 ;
1152
1153 enumerator_list: /* C99 6.7.2.2 */
1154 enumerator
1155 | enumerator_list T_COMMA enumerator {
1156 $$ = concat_symbols($1, $3);
1157 }
1158 | error {
1159 $$ = NULL;
1160 }
1161 ;
1162
1163 enumerator: /* C99 6.7.2.2 */
1164 identifier_sym gcc_attribute_specifier_list_opt {
1165 $$ = enumeration_constant($1, enumval, true);
1166 }
1167 | identifier_sym gcc_attribute_specifier_list_opt
1168 T_ASSIGN constant_expr {
1169 $$ = enumeration_constant($1, to_int_constant($4, true),
1170 false);
1171 }
1172 ;
1173
1174 type_qualifier: /* C99 6.7.3 */
1175 T_QUAL
1176 | atomic {
1177 $$ = ATOMIC;
1178 }
1179 ;
1180
1181 atomic: /* helper */
1182 T_ATOMIC {
1183 /* TODO: First fix c11ism, then use it here. */
1184 if (!allow_c11)
1185 /* '_Atomic' requires C11 or later */
1186 error(350);
1187 }
1188 ;
1189
1190 pointer: /* C99 6.7.5 */
1191 asterisk type_qualifier_list_opt {
1192 $$ = merge_qualified_pointer($1, $2);
1193 }
1194 | asterisk type_qualifier_list_opt pointer {
1195 $$ = merge_qualified_pointer($1, $2);
1196 $$ = merge_qualified_pointer($$, $3);
1197 }
1198 ;
1199
1200 asterisk: /* helper for 'pointer' */
1201 T_ASTERISK {
1202 $$ = xcalloc(1, sizeof(*$$));
1203 $$->p_pointer = true;
1204 }
1205 ;
1206
1207 type_qualifier_list_opt: /* see C99 6.7.5 */
1208 /* empty */ {
1209 $$ = NULL;
1210 }
1211 | type_qualifier_list
1212 ;
1213
1214 type_qualifier_list: /* C99 6.7.5 */
1215 type_qualifier_list_elem
1216 | type_qualifier_list type_qualifier_list_elem {
1217 $$ = merge_qualified_pointer($1, $2);
1218 }
1219 ;
1220
1221 type_qualifier_list_elem: /* helper for 'pointer' */
1222 type_qualifier {
1223 $$ = xcalloc(1, sizeof(*$$));
1224 if ($1 == CONST)
1225 $$->p_const = true;
1226 if ($1 == VOLATILE)
1227 $$->p_volatile = true;
1228 }
1229 ;
1230
1231 /*
1232 * For an explanation of 'notype' in the following rules, see
1233 * https://www.gnu.org/software/bison/manual/bison.html#Semantic-Tokens.
1234 */
1235
1236 notype_init_declarators:
1237 notype_init_declarator
1238 | notype_init_declarators T_COMMA type_init_declarator
1239 ;
1240
1241 type_init_declarators:
1242 type_init_declarator
1243 | type_init_declarators T_COMMA type_init_declarator
1244 ;
1245
1246 notype_init_declarator:
1247 notype_declarator asm_or_symbolrename_opt {
1248 cgram_declare($1, false, $2);
1249 check_size($1);
1250 }
1251 | notype_declarator asm_or_symbolrename_opt {
1252 begin_initialization($1);
1253 cgram_declare($1, true, $2);
1254 } T_ASSIGN initializer {
1255 check_size($1);
1256 end_initialization();
1257 }
1258 ;
1259
1260 type_init_declarator:
1261 type_declarator asm_or_symbolrename_opt {
1262 cgram_declare($1, false, $2);
1263 check_size($1);
1264 }
1265 | type_declarator asm_or_symbolrename_opt {
1266 begin_initialization($1);
1267 cgram_declare($1, true, $2);
1268 } T_ASSIGN initializer {
1269 check_size($1);
1270 end_initialization();
1271 }
1272 ;
1273
1274 notype_declarator:
1275 notype_direct_declarator
1276 | pointer notype_direct_declarator {
1277 $$ = add_pointer($2, $1);
1278 }
1279 ;
1280
1281 type_declarator:
1282 type_direct_declarator
1283 | pointer type_direct_declarator {
1284 $$ = add_pointer($2, $1);
1285 }
1286 ;
1287
1288 notype_direct_declarator:
1289 type_attribute_list_opt T_NAME {
1290 $$ = declarator_name(getsym($2));
1291 }
1292 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN {
1293 $$ = $3;
1294 }
1295 | notype_direct_declarator T_LBRACK array_size_opt T_RBRACK {
1296 $$ = add_array($1, $3.has_dim, $3.dim);
1297 }
1298 | notype_direct_declarator param_list asm_or_symbolrename_opt {
1299 $$ = add_function(symbolrename($1, $3), $2);
1300 end_declaration_level();
1301 block_level--;
1302 }
1303 | notype_direct_declarator type_attribute
1304 ;
1305
1306 type_direct_declarator:
1307 type_attribute_list_opt identifier {
1308 $$ = declarator_name(getsym($2));
1309 }
1310 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN {
1311 $$ = $3;
1312 }
1313 | type_direct_declarator T_LBRACK array_size_opt T_RBRACK {
1314 $$ = add_array($1, $3.has_dim, $3.dim);
1315 }
1316 | type_direct_declarator param_list asm_or_symbolrename_opt {
1317 $$ = add_function(symbolrename($1, $3), $2);
1318 end_declaration_level();
1319 block_level--;
1320 }
1321 | type_direct_declarator type_attribute
1322 ;
1323
1324 /*
1325 * The two distinct rules type_param_declarator and notype_param_declarator
1326 * avoid a conflict in argument lists. A typename enclosed in parentheses is
1327 * always treated as a typename, not an argument name. For example, after
1328 * "typedef double a;", the declaration "f(int (a));" is interpreted as
1329 * "f(int (double));", not "f(int a);".
1330 */
1331 type_param_declarator:
1332 direct_param_declarator
1333 | pointer direct_param_declarator {
1334 $$ = add_pointer($2, $1);
1335 }
1336 ;
1337
1338 notype_param_declarator:
1339 direct_notype_param_declarator
1340 | pointer direct_notype_param_declarator {
1341 $$ = add_pointer($2, $1);
1342 }
1343 ;
1344
1345 direct_param_declarator:
1346 identifier type_attribute_list {
1347 $$ = declarator_name(getsym($1));
1348 }
1349 | identifier {
1350 $$ = declarator_name(getsym($1));
1351 }
1352 | T_LPAREN notype_param_declarator T_RPAREN {
1353 $$ = $2;
1354 }
1355 | direct_param_declarator T_LBRACK array_size_opt T_RBRACK
1356 gcc_attribute_specifier_list_opt {
1357 $$ = add_array($1, $3.has_dim, $3.dim);
1358 }
1359 | direct_param_declarator param_list asm_or_symbolrename_opt {
1360 $$ = add_function(symbolrename($1, $3), $2);
1361 end_declaration_level();
1362 block_level--;
1363 }
1364 ;
1365
1366 direct_notype_param_declarator:
1367 identifier {
1368 $$ = declarator_name(getsym($1));
1369 }
1370 | T_LPAREN notype_param_declarator T_RPAREN {
1371 $$ = $2;
1372 }
1373 | direct_notype_param_declarator T_LBRACK array_size_opt T_RBRACK {
1374 $$ = add_array($1, $3.has_dim, $3.dim);
1375 }
1376 | direct_notype_param_declarator param_list asm_or_symbolrename_opt {
1377 $$ = add_function(symbolrename($1, $3), $2);
1378 end_declaration_level();
1379 block_level--;
1380 }
1381 ;
1382
1383 param_list:
1384 id_list_lparen identifier_list T_RPAREN {
1385 $$ = $2;
1386 }
1387 | abstract_decl_param_list
1388 ;
1389
1390 id_list_lparen:
1391 T_LPAREN {
1392 block_level++;
1393 begin_declaration_level(DLK_PROTO_PARAMS);
1394 }
1395 ;
1396
1397 array_size_opt:
1398 /* empty */ {
1399 $$.has_dim = false;
1400 $$.dim = 0;
1401 }
1402 | T_ASTERISK {
1403 /* since C99; variable length array of unspecified size */
1404 $$.has_dim = false; /* TODO: maybe change to true */
1405 $$.dim = 0; /* just as a placeholder */
1406 }
1407 | array_size {
1408 $$.has_dim = true;
1409 $$.dim = $1 == NULL ? 0 : to_int_constant($1, false);
1410 }
1411 ;
1412
1413 array_size:
1414 type_qualifier_list_opt T_SCLASS constant_expr {
1415 /* C11 6.7.6.3p7 */
1416 if ($2 != STATIC)
1417 yyerror("Bad attribute");
1418 /* static array size is a C11 extension */
1419 c11ism(343);
1420 $$ = $3;
1421 }
1422 | type_qualifier {
1423 /* C11 6.7.6.2 */
1424 if ($1 != RESTRICT)
1425 yyerror("Bad attribute");
1426 $$ = NULL;
1427 }
1428 | constant_expr
1429 ;
1430
1431 identifier_list: /* C99 6.7.5 */
1432 T_NAME {
1433 $$ = old_style_function_parameter_name(getsym($1));
1434 }
1435 | identifier_list T_COMMA T_NAME {
1436 $$ = concat_symbols($1,
1437 old_style_function_parameter_name(getsym($3)));
1438 }
1439 | identifier_list error
1440 ;
1441
1442 /* XXX: C99 requires an additional specifier-qualifier-list. */
1443 type_name: /* C99 6.7.6 */
1444 {
1445 begin_declaration_level(DLK_ABSTRACT);
1446 } abstract_declaration {
1447 end_declaration_level();
1448 $$ = $2->s_type;
1449 }
1450 ;
1451
1452 abstract_declaration: /* specific to lint */
1453 begin_type_qualifier_list end_type {
1454 $$ = declare_abstract_type(abstract_name());
1455 }
1456 | begin_type_specifier_qualifier_list end_type {
1457 $$ = declare_abstract_type(abstract_name());
1458 }
1459 | begin_type_qualifier_list end_type abstract_declarator {
1460 $$ = declare_abstract_type($3);
1461 }
1462 | begin_type_specifier_qualifier_list end_type abstract_declarator {
1463 $$ = declare_abstract_type($3);
1464 }
1465 ;
1466
1467 /* K&R 8.7, C90 ???, C99 6.7.6, C11 6.7.7 */
1468 /* In K&R, abstract-declarator could be empty and was still simpler. */
1469 abstract_declarator:
1470 pointer {
1471 $$ = add_pointer(abstract_name(), $1);
1472 }
1473 | direct_abstract_declarator
1474 | pointer direct_abstract_declarator {
1475 $$ = add_pointer($2, $1);
1476 }
1477 | type_attribute_list direct_abstract_declarator {
1478 $$ = $2;
1479 }
1480 | pointer type_attribute_list direct_abstract_declarator {
1481 $$ = add_pointer($3, $1);
1482 }
1483 ;
1484
1485 /* K&R ---, C90 ???, C99 6.7.6, C11 6.7.7 */
1486 direct_abstract_declarator:
1487 /* TODO: sort rules according to C99 */
1488 T_LPAREN abstract_declarator T_RPAREN {
1489 $$ = $2;
1490 }
1491 | T_LBRACK array_size_opt T_RBRACK {
1492 $$ = add_array(abstract_name(), $2.has_dim, $2.dim);
1493 }
1494 | direct_abstract_declarator T_LBRACK array_size_opt T_RBRACK {
1495 $$ = add_array($1, $3.has_dim, $3.dim);
1496 }
1497 | abstract_decl_param_list asm_or_symbolrename_opt {
1498 $$ = add_function(symbolrename(abstract_name(), $2), $1);
1499 end_declaration_level();
1500 block_level--;
1501 }
1502 | direct_abstract_declarator abstract_decl_param_list
1503 asm_or_symbolrename_opt {
1504 $$ = add_function(symbolrename($1, $3), $2);
1505 end_declaration_level();
1506 block_level--;
1507 }
1508 | direct_abstract_declarator type_attribute_list
1509 ;
1510
1511 abstract_decl_param_list: /* specific to lint */
1512 abstract_decl_lparen T_RPAREN type_attribute_opt {
1513 $$ = NULL;
1514 }
1515 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN
1516 type_attribute_opt {
1517 dcs->d_prototype = true;
1518 $$ = $2;
1519 }
1520 | abstract_decl_lparen error T_RPAREN type_attribute_opt {
1521 $$ = NULL;
1522 }
1523 ;
1524
1525 abstract_decl_lparen: /* specific to lint */
1526 T_LPAREN {
1527 block_level++;
1528 begin_declaration_level(DLK_PROTO_PARAMS);
1529 }
1530 ;
1531
1532 vararg_parameter_type_list: /* specific to lint */
1533 parameter_type_list
1534 | parameter_type_list T_COMMA T_ELLIPSIS {
1535 dcs->d_vararg = true;
1536 $$ = $1;
1537 }
1538 | T_ELLIPSIS {
1539 /* TODO: C99 6.7.5 makes this an error as well. */
1540 if (!allow_trad && !allow_c99) {
1541 /* ANSI C requires formal parameter before '...' */
1542 error(84);
1543 } else if (allow_c90) {
1544 /* ANSI C requires formal parameter before '...' */
1545 warning(84);
1546 }
1547 dcs->d_vararg = true;
1548 $$ = NULL;
1549 }
1550 ;
1551
1552 /* XXX: C99 6.7.5 defines the same name, but it looks different. */
1553 parameter_type_list:
1554 parameter_declaration
1555 | parameter_type_list T_COMMA parameter_declaration {
1556 $$ = concat_symbols($1, $3);
1557 }
1558 ;
1559
1560 /* XXX: C99 6.7.5 defines the same name, but it looks completely different. */
1561 parameter_declaration:
1562 begin_type_declmods end_type {
1563 /* ^^ There is no check for the missing type-specifier. */
1564 $$ = declare_argument(abstract_name(), false);
1565 }
1566 | begin_type_declaration_specifiers end_type {
1567 $$ = declare_argument(abstract_name(), false);
1568 }
1569 | begin_type_declmods end_type notype_param_declarator {
1570 /* ^^ There is no check for the missing type-specifier. */
1571 $$ = declare_argument($3, false);
1572 }
1573 /*
1574 * type_param_declarator is needed because of following conflict:
1575 * "typedef int a; f(int (a));" could be parsed as
1576 * "function with argument a of type int", or
1577 * "function with an abstract argument of type function".
1578 * This grammar realizes the second case.
1579 */
1580 | begin_type_declaration_specifiers end_type type_param_declarator {
1581 $$ = declare_argument($3, false);
1582 }
1583 | begin_type_declmods end_type abstract_declarator {
1584 /* ^^ There is no check for the missing type-specifier. */
1585 $$ = declare_argument($3, false);
1586 }
1587 | begin_type_declaration_specifiers end_type abstract_declarator {
1588 $$ = declare_argument($3, false);
1589 }
1590 ;
1591
1592 braced_initializer:
1593 /* K&R ---, C90 ---, C99 ---, C11 ---, C23 6.7.10 */
1594 init_lbrace init_rbrace {
1595 /* empty initializer braces require C23 or later */
1596 c23ism(353);
1597 }
1598 /* K&R ---, C90 ---, C99 6.7.8, C11 6.7.9, C23 6.7.10 */
1599 | init_lbrace initializer_list comma_opt init_rbrace
1600 ;
1601
1602 initializer: /* C99 6.7.8 "Initialization" */
1603 assignment_expression {
1604 init_expr($1);
1605 }
1606 | init_lbrace init_rbrace {
1607 /* XXX: Empty braces are not covered by C99 6.7.8. */
1608 }
1609 | init_lbrace initializer_list comma_opt init_rbrace
1610 /* XXX: What is this error handling for? */
1611 | error
1612 ;
1613
1614 initializer_list: /* C99 6.7.8 "Initialization" */
1615 initializer_list_item
1616 | initializer_list T_COMMA initializer_list_item
1617 ;
1618
1619 initializer_list_item: /* helper */
1620 designation initializer
1621 | initializer
1622 ;
1623
1624 designation: /* C99 6.7.8 "Initialization" */
1625 begin_designation designator_list T_ASSIGN
1626 | identifier T_COLON {
1627 /* GCC style struct or union member name in initializer */
1628 gnuism(315);
1629 begin_designation();
1630 add_designator_member($1);
1631 }
1632 ;
1633
1634 begin_designation: /* lint-specific helper */
1635 /* empty */ {
1636 begin_designation();
1637 }
1638 ;
1639
1640 designator_list: /* C99 6.7.8 "Initialization" */
1641 designator
1642 | designator_list designator
1643 ;
1644
1645 designator: /* C99 6.7.8 "Initialization" */
1646 T_LBRACK range T_RBRACK {
1647 if (!allow_c99)
1648 /* array initializer with designators is a C99 ... */
1649 warning(321);
1650 add_designator_subscript($2);
1651 }
1652 | T_POINT identifier {
1653 if (!allow_c99)
1654 /* struct or union member name in initializer is ... */
1655 warning(313);
1656 add_designator_member($2);
1657 }
1658 ;
1659
1660 static_assert_declaration:
1661 T_STATIC_ASSERT T_LPAREN constant_expr T_COMMA T_STRING T_RPAREN T_SEMI /* C11 */
1662 | T_STATIC_ASSERT T_LPAREN constant_expr T_RPAREN T_SEMI /* C23 */
1663 ;
1664
1665 range:
1666 constant_expr {
1667 $$.lo = to_int_constant($1, true);
1668 $$.hi = $$.lo;
1669 }
1670 | constant_expr T_ELLIPSIS constant_expr {
1671 $$.lo = to_int_constant($1, true);
1672 $$.hi = to_int_constant($3, true);
1673 /* initialization with '[a...b]' is a GCC extension */
1674 gnuism(340);
1675 }
1676 ;
1677
1678 init_lbrace: /* helper */
1679 T_LBRACE {
1680 init_lbrace();
1681 }
1682 ;
1683
1684 init_rbrace: /* helper */
1685 T_RBRACE {
1686 init_rbrace();
1687 }
1688 ;
1689
1690 asm_or_symbolrename_opt: /* GCC extensions */
1691 /* empty */ {
1692 $$ = NULL;
1693 }
1694 | T_ASM T_LPAREN T_STRING T_RPAREN gcc_attribute_specifier_list_opt {
1695 freeyyv(&$3, T_STRING);
1696 $$ = NULL;
1697 }
1698 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN
1699 gcc_attribute_specifier_list_opt {
1700 $$ = $3;
1701 }
1702 ;
1703
1704 statement: /* C99 6.8 */
1705 expression_statement
1706 | non_expr_statement
1707 ;
1708
1709 non_expr_statement: /* helper for C99 6.8 */
1710 gcc_attribute_specifier /* ((__fallthrough__)) */ T_SEMI
1711 | labeled_statement
1712 | compound_statement
1713 | selection_statement
1714 | iteration_statement
1715 | jump_statement {
1716 seen_fallthrough = false;
1717 }
1718 | asm_statement
1719 ;
1720
1721 labeled_statement: /* C99 6.8.1 */
1722 label gcc_attribute_specifier_list_opt statement
1723 ;
1724
1725 label:
1726 T_NAME T_COLON {
1727 symtyp = FLABEL;
1728 named_label(getsym($1));
1729 }
1730 | T_CASE constant_expr T_COLON {
1731 case_label($2);
1732 seen_fallthrough = true;
1733 }
1734 | T_CASE constant_expr T_ELLIPSIS constant_expr T_COLON {
1735 /* XXX: We don't fill all cases */
1736 case_label($2);
1737 seen_fallthrough = true;
1738 }
1739 | T_DEFAULT T_COLON {
1740 default_label();
1741 seen_fallthrough = true;
1742 }
1743 ;
1744
1745 compound_statement: /* C99 6.8.2 */
1746 compound_statement_lbrace compound_statement_rbrace
1747 | compound_statement_lbrace block_item_list compound_statement_rbrace
1748 ;
1749
1750 compound_statement_lbrace:
1751 T_LBRACE {
1752 block_level++;
1753 mem_block_level++;
1754 begin_declaration_level(DLK_AUTO);
1755 }
1756 ;
1757
1758 compound_statement_rbrace:
1759 T_RBRACE {
1760 end_declaration_level();
1761 level_free_all(mem_block_level);
1762 mem_block_level--;
1763 block_level--;
1764 seen_fallthrough = false;
1765 }
1766 ;
1767
1768 block_item_list: /* C99 6.8.2 */
1769 block_item
1770 | block_item_list block_item {
1771 if ($1 && !$2)
1772 /* declarations after statements is a C99 feature */
1773 c99ism(327);
1774 $$ = $1 || $2;
1775 }
1776 ;
1777
1778 block_item: /* C99 6.8.2 */
1779 declaration_or_error {
1780 $$ = false;
1781 restore_warning_flags();
1782 }
1783 | statement {
1784 $$ = true;
1785 restore_warning_flags();
1786 }
1787 ;
1788
1789 expression_statement: /* C99 6.8.3 */
1790 expression T_SEMI {
1791 expr($1, false, false, false, false);
1792 seen_fallthrough = false;
1793 }
1794 | T_SEMI {
1795 check_statement_reachable();
1796 seen_fallthrough = false;
1797 }
1798 ;
1799
1800 selection_statement: /* C99 6.8.4 */
1801 if_without_else %prec T_THEN {
1802 save_warning_flags();
1803 stmt_if_then_stmt();
1804 stmt_if_else_stmt(false);
1805 }
1806 | if_without_else T_ELSE {
1807 save_warning_flags();
1808 stmt_if_then_stmt();
1809 } statement {
1810 clear_warning_flags();
1811 stmt_if_else_stmt(true);
1812 }
1813 | if_without_else T_ELSE error {
1814 clear_warning_flags();
1815 stmt_if_else_stmt(false);
1816 }
1817 | switch_expr statement {
1818 clear_warning_flags();
1819 stmt_switch_expr_stmt();
1820 }
1821 | switch_expr error {
1822 clear_warning_flags();
1823 stmt_switch_expr_stmt();
1824 }
1825 ;
1826
1827 if_without_else: /* see C99 6.8.4 */
1828 if_expr statement
1829 | if_expr error
1830 ;
1831
1832 if_expr: /* see C99 6.8.4 */
1833 T_IF T_LPAREN expression T_RPAREN {
1834 stmt_if_expr($3);
1835 clear_warning_flags();
1836 }
1837 ;
1838
1839 switch_expr: /* see C99 6.8.4 */
1840 T_SWITCH T_LPAREN expression T_RPAREN {
1841 stmt_switch_expr($3);
1842 clear_warning_flags();
1843 }
1844 ;
1845
1846 iteration_statement: /* C99 6.8.5 */
1847 while_expr statement {
1848 clear_warning_flags();
1849 stmt_while_expr_stmt();
1850 }
1851 | while_expr error {
1852 clear_warning_flags();
1853 stmt_while_expr_stmt();
1854 }
1855 | do_statement do_while_expr {
1856 stmt_do_while_expr($2);
1857 seen_fallthrough = false;
1858 }
1859 | do error {
1860 clear_warning_flags();
1861 stmt_do_while_expr(NULL);
1862 }
1863 | for_exprs statement {
1864 clear_warning_flags();
1865 stmt_for_exprs_stmt();
1866 end_declaration_level();
1867 block_level--;
1868 }
1869 | for_exprs error {
1870 clear_warning_flags();
1871 stmt_for_exprs_stmt();
1872 end_declaration_level();
1873 block_level--;
1874 }
1875 ;
1876
1877 while_expr: /* see C99 6.8.5 */
1878 T_WHILE T_LPAREN expression T_RPAREN {
1879 stmt_while_expr($3);
1880 clear_warning_flags();
1881 }
1882 ;
1883
1884 do_statement: /* see C99 6.8.5 */
1885 do statement {
1886 clear_warning_flags();
1887 }
1888 ;
1889
1890 do: /* see C99 6.8.5 */
1891 T_DO {
1892 stmt_do();
1893 }
1894 ;
1895
1896 do_while_expr: /* see C99 6.8.5 */
1897 T_WHILE T_LPAREN expression T_RPAREN T_SEMI {
1898 $$ = $3;
1899 }
1900 ;
1901
1902 for_start: /* see C99 6.8.5 */
1903 T_FOR T_LPAREN {
1904 begin_declaration_level(DLK_AUTO);
1905 block_level++;
1906 }
1907 ;
1908
1909 for_exprs: /* see C99 6.8.5 */
1910 for_start
1911 begin_type_declaration_specifiers end_type
1912 notype_init_declarators T_SEMI
1913 expression_opt T_SEMI
1914 expression_opt T_RPAREN {
1915 /* variable declaration in for loop */
1916 c99ism(325);
1917 stmt_for_exprs(NULL, $6, $8);
1918 clear_warning_flags();
1919 }
1920 | for_start
1921 expression_opt T_SEMI
1922 expression_opt T_SEMI
1923 expression_opt T_RPAREN {
1924 stmt_for_exprs($2, $4, $6);
1925 clear_warning_flags();
1926 }
1927 ;
1928
1929 jump_statement: /* C99 6.8.6 */
1930 goto identifier T_SEMI {
1931 stmt_goto(getsym($2));
1932 }
1933 | goto error T_SEMI {
1934 symtyp = FVFT;
1935 }
1936 | T_CONTINUE T_SEMI {
1937 stmt_continue();
1938 }
1939 | T_BREAK T_SEMI {
1940 stmt_break();
1941 }
1942 | T_RETURN sys T_SEMI {
1943 stmt_return($2, NULL);
1944 }
1945 | T_RETURN sys expression T_SEMI {
1946 stmt_return($2, $3);
1947 }
1948 ;
1949
1950 goto: /* see C99 6.8.6 */
1951 T_GOTO {
1952 symtyp = FLABEL;
1953 }
1954 ;
1955
1956 asm_statement: /* GCC extension */
1957 T_ASM T_LPAREN read_until_rparen T_SEMI {
1958 dcs_set_asm();
1959 }
1960 | T_ASM type_qualifier T_LPAREN read_until_rparen T_SEMI {
1961 dcs_set_asm();
1962 }
1963 | T_ASM error
1964 ;
1965
1966 read_until_rparen: /* helper for 'asm_statement' */
1967 /* empty */ {
1968 read_until_rparen();
1969 }
1970 ;
1971
1972 translation_unit: /* C99 6.9 */
1973 external_declaration
1974 | translation_unit external_declaration
1975 ;
1976
1977 external_declaration: /* C99 6.9 */
1978 function_definition {
1979 global_clean_up_decl(false);
1980 clear_warning_flags();
1981 }
1982 | top_level_declaration {
1983 global_clean_up_decl(false);
1984 clear_warning_flags();
1985 }
1986 | asm_statement /* GCC extension */
1987 | T_SEMI { /* GCC extension */
1988 /*
1989 * TODO: Only allow this in GCC mode, not in plain C99.
1990 * This is one of the top 10 warnings in the NetBSD build.
1991 */
1992 if (!allow_trad && !allow_c99) {
1993 /* empty declaration */
1994 error(0);
1995 } else if (allow_c90) {
1996 /* empty declaration */
1997 warning(0);
1998 }
1999 }
2000 ;
2001
2002 /*
2003 * On the top level, lint allows several forms of declarations that it doesn't
2004 * allow in functions. For example, a single ';' is an empty declaration and
2005 * is supported by some compilers, but in a function it would be an empty
2006 * statement, not a declaration. This makes a difference in C90 mode, where
2007 * a statement must not be followed by a declaration.
2008 *
2009 * See 'declaration' for all other declarations.
2010 */
2011 top_level_declaration: /* C99 6.9 calls this 'declaration' */
2012 begin_type end_type notype_init_declarators T_SEMI {
2013 /* TODO: Make this an error in C99 mode as well. */
2014 if (!allow_trad && !allow_c99) {
2015 /* old-style declaration; add 'int' */
2016 error(1);
2017 } else if (allow_c90) {
2018 /* old-style declaration; add 'int' */
2019 warning(1);
2020 }
2021 }
2022 | declaration
2023 | error T_SEMI {
2024 global_clean_up();
2025 }
2026 | error T_RBRACE {
2027 global_clean_up();
2028 }
2029 ;
2030
2031 function_definition: /* C99 6.9.1 */
2032 func_declarator {
2033 if ($1->s_type->t_tspec != FUNC) {
2034 /* syntax error '%s' */
2035 error(249, yytext);
2036 YYERROR;
2037 }
2038 if ($1->s_type->t_typedef) {
2039 /* ()-less function definition */
2040 error(64);
2041 YYERROR;
2042 }
2043 check_extern_declaration($1);
2044 begin_function($1);
2045 block_level++;
2046 begin_declaration_level(DLK_OLD_STYLE_ARGS);
2047 if (lwarn == LWARN_NONE)
2048 $1->s_used = true;
2049 } arg_declaration_list_opt {
2050 end_declaration_level();
2051 block_level--;
2052 check_func_lint_directives();
2053 check_func_old_style_arguments();
2054 begin_control_statement(CS_FUNCTION_BODY);
2055 } compound_statement {
2056 end_function();
2057 end_control_statement(CS_FUNCTION_BODY);
2058 }
2059 ;
2060
2061 func_declarator:
2062 begin_type end_type notype_declarator {
2063 if (!allow_trad) {
2064 /* old-style declaration; add 'int' */
2065 error(1);
2066 }
2067 $$ = $3;
2068 }
2069 | begin_type_declmods end_type notype_declarator {
2070 if (!allow_trad) {
2071 /* old-style declaration; add 'int' */
2072 error(1);
2073 }
2074 $$ = $3;
2075 }
2076 | begin_type_declaration_specifiers end_type type_declarator {
2077 $$ = $3;
2078 }
2079 ;
2080
2081 arg_declaration_list_opt: /* C99 6.9.1p13 example 1 */
2082 /* empty */
2083 | arg_declaration_list
2084 ;
2085
2086 arg_declaration_list: /* C99 6.9.1p13 example 1 */
2087 arg_declaration
2088 | arg_declaration_list arg_declaration
2089 /* XXX or better "arg_declaration error" ? */
2090 | error
2091 ;
2092
2093 /*
2094 * "arg_declaration" is separated from "declaration" because it
2095 * needs other error handling.
2096 */
2097 arg_declaration:
2098 begin_type_declmods end_type T_SEMI {
2099 /* empty declaration */
2100 warning(2);
2101 }
2102 | begin_type_declmods end_type notype_init_declarators T_SEMI
2103 | begin_type_declaration_specifiers end_type T_SEMI {
2104 if (!dcs->d_nonempty_decl) {
2105 /* empty declaration */
2106 warning(2);
2107 } else {
2108 /* '%s' declared in argument declaration list */
2109 warning(3, type_name(dcs->d_type));
2110 }
2111 }
2112 | begin_type_declaration_specifiers end_type
2113 type_init_declarators T_SEMI {
2114 if (dcs->d_nonempty_decl) {
2115 /* '%s' declared in argument declaration list */
2116 warning(3, type_name(dcs->d_type));
2117 }
2118 }
2119 | begin_type_declmods error
2120 | begin_type_declaration_specifiers error
2121 ;
2122
2123 /* https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html */
2124 gcc_attribute_specifier_list_opt:
2125 /* empty */
2126 | gcc_attribute_specifier_list
2127 ;
2128
2129 gcc_attribute_specifier_list:
2130 gcc_attribute_specifier
2131 | gcc_attribute_specifier_list gcc_attribute_specifier
2132 ;
2133
2134 gcc_attribute_specifier:
2135 T_ATTRIBUTE T_LPAREN T_LPAREN {
2136 in_gcc_attribute = true;
2137 } gcc_attribute_list {
2138 in_gcc_attribute = false;
2139 } T_RPAREN T_RPAREN
2140 ;
2141
2142 gcc_attribute_list:
2143 gcc_attribute
2144 | gcc_attribute_list T_COMMA gcc_attribute
2145 ;
2146
2147 gcc_attribute:
2148 /* empty */
2149 | T_NAME {
2150 const char *name = $1->sb_name;
2151 if (is_either(name, "packed", "__packed__"))
2152 dcs_add_packed();
2153 else if (is_either(name, "used", "__used__") ||
2154 is_either(name, "unused", "__unused__"))
2155 dcs_set_used();
2156 else if (is_either(name, "fallthrough", "__fallthrough__"))
2157 fallthru(1);
2158 }
2159 | T_NAME T_LPAREN T_RPAREN
2160 | T_NAME T_LPAREN gcc_attribute_parameters T_RPAREN
2161 | type_qualifier {
2162 if ($1 != CONST)
2163 yyerror("Bad attribute");
2164 }
2165 ;
2166
2167 gcc_attribute_parameters:
2168 constant_expr
2169 | gcc_attribute_parameters T_COMMA constant_expr
2170 ;
2171
2172 sys:
2173 /* empty */ {
2174 $$ = in_system_header;
2175 }
2176 ;
2177
2178 %%
2179
2180 /* ARGSUSED */
2181 int
2182 yyerror(const char *msg)
2183 {
2184 /* syntax error '%s' */
2185 error(249, yytext);
2186 if (++sytxerr >= 5)
2187 norecover();
2188 return 0;
2189 }
2190
2191 #if YYDEBUG && (YYBYACC || YYBISON)
2192 static const char *
2193 cgram_to_string(int token, YYSTYPE val)
2194 {
2195
2196 switch (token) {
2197 case T_INCDEC:
2198 case T_MULTIPLICATIVE:
2199 case T_ADDITIVE:
2200 case T_SHIFT:
2201 case T_RELATIONAL:
2202 case T_EQUALITY:
2203 case T_OPASSIGN:
2204 return modtab[val.y_op].m_name;
2205 case T_SCLASS:
2206 return scl_name(val.y_scl);
2207 case T_TYPE:
2208 case T_STRUCT_OR_UNION:
2209 return tspec_name(val.y_tspec);
2210 case T_QUAL:
2211 return tqual_name(val.y_tqual);
2212 case T_NAME:
2213 return val.y_name->sb_name;
2214 default:
2215 return "<none>";
2216 }
2217 }
2218 #endif
2219
2220 #if YYDEBUG && YYBISON
2221 static inline void
2222 cgram_print(FILE *output, int token, YYSTYPE val)
2223 {
2224 (void)fprintf(output, "%s", cgram_to_string(token, val));
2225 }
2226 #endif
2227
2228 static void
2229 cgram_declare(sym_t *decl, bool has_initializer, sbuf_t *renaming)
2230 {
2231 declare(decl, has_initializer, renaming);
2232 if (renaming != NULL)
2233 freeyyv(&renaming, T_NAME);
2234 }
2235
2236 /*
2237 * Discard all input tokens up to and including the next
2238 * unmatched right paren
2239 */
2240 static void
2241 read_until_rparen(void)
2242 {
2243 int level;
2244
2245 if (yychar < 0)
2246 yychar = yylex();
2247 freeyyv(&yylval, yychar);
2248
2249 level = 1;
2250 while (yychar > 0) {
2251 if (yychar == T_LPAREN)
2252 level++;
2253 if (yychar == T_RPAREN && --level == 0)
2254 break;
2255 freeyyv(&yylval, yychar = yylex());
2256 }
2257
2258 yyclearin;
2259 }
2260
2261 static sym_t *
2262 symbolrename(sym_t *s, sbuf_t *sb)
2263 {
2264 if (sb != NULL)
2265 s->s_rename = sb->sb_name;
2266 return s;
2267 }
2268