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