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