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