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