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