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