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