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