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