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