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