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