cgram.y revision 1.504 1 %{
2 /* $NetBSD: cgram.y,v 1.504 2024/06/17 04:14:02 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.504 2024/06/17 04:14:02 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 }
1061 ;
1062
1063 end_type:
1064 /* empty */ {
1065 dcs_end_type();
1066 }
1067 ;
1068
1069 /* TODO: Implement 'declaration_specifier' from C23 6.7.1. */
1070
1071 /*
1072 * For an explanation of 'type' and 'notype' prefixes in the following rules,
1073 * see https://www.gnu.org/software/bison/manual/bison.html#Semantic-Tokens.
1074 */
1075
1076 /* C23 6.7.1 */
1077 /* The rule 'init_declarator_list' is split into the 'notype' and 'type' variants. */
1078
1079 notype_init_declarator_list:
1080 notype_init_declarator
1081 | notype_init_declarator_list T_COMMA type_init_declarator
1082 ;
1083
1084 type_init_declarator_list:
1085 type_init_declarator
1086 | type_init_declarator_list T_COMMA type_init_declarator
1087 ;
1088
1089 /* C23 6.7.1 */
1090 /* The rule 'init_declarator' is split into the 'notype' and 'type' variants. */
1091
1092 notype_init_declarator:
1093 notype_declarator asm_or_symbolrename_opt {
1094 cgram_declare($1, false, $2);
1095 check_size($1);
1096 }
1097 | notype_declarator asm_or_symbolrename_opt {
1098 begin_initialization($1);
1099 cgram_declare($1, true, $2);
1100 } T_ASSIGN initializer {
1101 check_size($1);
1102 end_initialization();
1103 }
1104 ;
1105
1106 type_init_declarator:
1107 type_declarator asm_or_symbolrename_opt {
1108 cgram_declare($1, false, $2);
1109 check_size($1);
1110 }
1111 | type_declarator asm_or_symbolrename_opt {
1112 begin_initialization($1);
1113 cgram_declare($1, true, $2);
1114 } T_ASSIGN initializer {
1115 check_size($1);
1116 end_initialization();
1117 }
1118 ;
1119
1120
1121 /* TODO: Implement 'attribute_declaration' from C23 6.7.1. */
1122
1123 /* K&R ???, C90 ???, C99 ???, C11 ???, C23 6.7.2 */
1124 storage_class_specifier:
1125 T_SCLASS
1126 ;
1127
1128 /* C99 6.7.2, C23 6.7.3.1 */
1129 /* The rule 'type_specifier' is split into the 'notype' and 'type' variants. */
1130
1131 type_type_specifier:
1132 notype_type_specifier
1133 | T_TYPENAME {
1134 $$ = getsym($1)->s_type;
1135 }
1136 ;
1137
1138 notype_type_specifier: /* see C99 6.7.2 */
1139 T_TYPE {
1140 $$ = gettyp($1);
1141 }
1142 | T_TYPEOF T_LPAREN expression T_RPAREN { /* GCC extension */
1143 $$ = $3 != NULL ? block_dup_type($3->tn_type) : gettyp(INT);
1144 $$->t_typeof = true;
1145 }
1146 | atomic_type_specifier
1147 | struct_or_union_specifier {
1148 end_declaration_level();
1149 $$ = $1;
1150 }
1151 | enum_specifier {
1152 end_declaration_level();
1153 $$ = $1;
1154 }
1155 ;
1156
1157 /* K&R ---, C90 ---, C99 6.7.2.1, C11 ???, C23 6.7.3.2 */
1158 struct_or_union_specifier:
1159 struct_or_union identifier_sym {
1160 /*
1161 * STDC requires that "struct a;" always introduces
1162 * a new tag if "a" is not declared at current level
1163 *
1164 * yychar is valid because otherwise the parser would not
1165 * have been able to decide if it must shift or reduce
1166 */
1167 $$ = make_tag_type($2, $1, false, yychar == T_SEMI);
1168 }
1169 | struct_or_union identifier_sym {
1170 dcs->d_tag_type = make_tag_type($2, $1, true, false);
1171 } braced_member_declaration_list {
1172 $$ = complete_struct_or_union($4);
1173 }
1174 | struct_or_union {
1175 dcs->d_tag_type = make_tag_type(NULL, $1, true, false);
1176 } braced_member_declaration_list {
1177 $$ = complete_struct_or_union($3);
1178 }
1179 | struct_or_union error {
1180 set_sym_kind(SK_VCFT);
1181 $$ = gettyp(INT);
1182 }
1183 ;
1184
1185 /* K&R ---, C90 ---, C99 6.7.2.1, C11 ???, C23 6.7.3.2 */
1186 struct_or_union:
1187 T_STRUCT_OR_UNION {
1188 set_sym_kind(SK_TAG);
1189 begin_declaration_level($1 == STRUCT ? DLK_STRUCT : DLK_UNION);
1190 dcs->d_sou_size_in_bits = 0;
1191 dcs->d_sou_align = 1;
1192 $$ = $1;
1193 }
1194 | struct_or_union type_attribute
1195 ;
1196
1197 braced_member_declaration_list: /* see C99 6.7.2.1 */
1198 T_LBRACE {
1199 set_sym_kind(SK_VCFT);
1200 } member_declaration_list_with_rbrace {
1201 $$ = $3;
1202 }
1203 ;
1204
1205 member_declaration_list_with_rbrace: /* see C99 6.7.2.1 */
1206 member_declaration_list T_RBRACE
1207 | T_RBRACE {
1208 /* XXX: Allowed since C23. */
1209 $$ = NULL;
1210 }
1211 ;
1212
1213 /* K&R ???, C90 ???, C99 6.7.2.1, C11 6.7.2.1, C23 6.7.3.2 */
1214 /* Was named struct_declaration_list until C11. */
1215 member_declaration_list:
1216 member_declaration
1217 | member_declaration_list member_declaration {
1218 $$ = concat_symbols($1, $2);
1219 }
1220 ;
1221
1222 /* K&R ???, C90 ???, C99 6.7.2.1, C11 6.7.2.1, C23 6.7.3.2 */
1223 /* Was named struct_declaration until C11. */
1224 member_declaration:
1225 begin_type_qualifier_list end_type {
1226 /* ^^ There is no check for the missing type-specifier. */
1227 /* too late, i know, but getsym() compensates it */
1228 set_sym_kind(SK_MEMBER);
1229 } notype_member_declarator_list T_SEMI {
1230 set_sym_kind(SK_VCFT);
1231 $$ = $4;
1232 }
1233 | begin_type_specifier_qualifier_list end_type {
1234 set_sym_kind(SK_MEMBER);
1235 } type_member_declarator_list T_SEMI {
1236 set_sym_kind(SK_VCFT);
1237 $$ = $4;
1238 }
1239 | begin_type_qualifier_list end_type type_attribute_opt T_SEMI {
1240 /* syntax error '%s' */
1241 error(249, "member without type");
1242 $$ = NULL;
1243 }
1244 | begin_type_specifier_qualifier_list end_type type_attribute_opt
1245 T_SEMI {
1246 set_sym_kind(SK_VCFT);
1247 if (!allow_c11 && !allow_gcc)
1248 /* anonymous struct/union members is a C11 feature */
1249 warning(49);
1250 if (is_struct_or_union(dcs->d_type->t_tspec))
1251 $$ = declare_unnamed_member();
1252 else {
1253 /* syntax error '%s' */
1254 error(249, "unnamed member");
1255 $$ = NULL;
1256 }
1257 }
1258 | static_assert_declaration {
1259 $$ = NULL;
1260 }
1261 | error T_SEMI {
1262 set_sym_kind(SK_VCFT);
1263 $$ = NULL;
1264 }
1265 ;
1266
1267 /* TODO: Implement 'specifier_qualifier_list' from C23 6.7.3.2. */
1268
1269 /* TODO: Implement 'type_specifier_qualifier' from C23 6.7.3.2. */
1270
1271 /* C23 6.7.3.2 */
1272 /* The rule 'member_declarator_list' is split into the 'type' and 'notype' variants. */
1273 /* Was named struct_declarator_list until C11. */
1274
1275 notype_member_declarator_list:
1276 notype_member_declarator {
1277 $$ = declare_member($1);
1278 }
1279 | notype_member_declarator_list {
1280 set_sym_kind(SK_MEMBER);
1281 } T_COMMA type_member_declarator {
1282 $$ = concat_symbols($1, declare_member($4));
1283 }
1284 ;
1285
1286 type_member_declarator_list:
1287 type_member_declarator {
1288 $$ = declare_member($1);
1289 }
1290 | type_member_declarator_list {
1291 set_sym_kind(SK_MEMBER);
1292 } T_COMMA type_member_declarator {
1293 $$ = concat_symbols($1, declare_member($4));
1294 }
1295 ;
1296
1297 /* C23 6.7.3.2 */
1298 /* The rule 'member_declarator' is split into the 'type' and 'notype' variants. */
1299 /* Was named struct_declarator until C11. */
1300
1301 notype_member_declarator:
1302 notype_declarator
1303 /* C99 6.7.2.1 */
1304 | notype_declarator T_COLON constant_expression {
1305 $$ = set_bit_field_width($1, to_int_constant($3, true));
1306 }
1307 /* C99 6.7.2.1 */
1308 | {
1309 set_sym_kind(SK_VCFT);
1310 } T_COLON constant_expression {
1311 $$ = set_bit_field_width(NULL, to_int_constant($3, true));
1312 }
1313 ;
1314
1315 type_member_declarator:
1316 type_declarator
1317 | type_declarator T_COLON constant_expression type_attribute_list_opt {
1318 $$ = set_bit_field_width($1, to_int_constant($3, true));
1319 }
1320 | {
1321 set_sym_kind(SK_VCFT);
1322 } T_COLON constant_expression type_attribute_list_opt {
1323 $$ = set_bit_field_width(NULL, to_int_constant($3, true));
1324 }
1325 ;
1326
1327 /* K&R ---, C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2, C23 6.7.3.5 */
1328 enum_specifier:
1329 enum gcc_attribute_specifier_list_opt identifier_sym {
1330 $$ = make_tag_type($3, ENUM, false, false);
1331 }
1332 | enum gcc_attribute_specifier_list_opt identifier_sym {
1333 dcs->d_tag_type = make_tag_type($3, ENUM, true, false);
1334 } enum_declaration /*gcc_attribute_specifier_list_opt*/ {
1335 $$ = complete_enum($5);
1336 }
1337 | enum gcc_attribute_specifier_list_opt {
1338 dcs->d_tag_type = make_tag_type(NULL, ENUM, true, false);
1339 } enum_declaration /*gcc_attribute_specifier_list_opt*/ {
1340 $$ = complete_enum($4);
1341 }
1342 | enum error {
1343 set_sym_kind(SK_VCFT);
1344 $$ = gettyp(INT);
1345 }
1346 ;
1347
1348 enum: /* helper for C99 6.7.2.2 */
1349 T_ENUM {
1350 set_sym_kind(SK_TAG);
1351 begin_declaration_level(DLK_ENUM);
1352 }
1353 ;
1354
1355 enum_declaration: /* helper for C99 6.7.2.2 */
1356 T_LBRACE {
1357 set_sym_kind(SK_VCFT);
1358 enumval = 0;
1359 } enums_with_opt_comma T_RBRACE {
1360 $$ = $3;
1361 }
1362 ;
1363
1364 enums_with_opt_comma: /* helper for C99 6.7.2.2 */
1365 enumerator_list
1366 | enumerator_list T_COMMA {
1367 if (!allow_c99 && !allow_trad)
1368 /* trailing ',' in enum declaration requires C99 ... */
1369 error(54);
1370 else
1371 /* trailing ',' in enum declaration requires C99 ... */
1372 c99ism(54);
1373 $$ = $1;
1374 }
1375 ;
1376
1377 /* C99 6.7.2.2, C23 6.7.3.3 */
1378 enumerator_list:
1379 enumerator
1380 | enumerator_list T_COMMA enumerator {
1381 $$ = concat_symbols($1, $3);
1382 }
1383 | error {
1384 $$ = NULL;
1385 }
1386 ;
1387
1388 /* C99 6.7.2.2, C23 6.7.3.3 */
1389 enumerator:
1390 identifier_sym gcc_attribute_specifier_list_opt {
1391 $$ = enumeration_constant($1, enumval, true);
1392 }
1393 | identifier_sym gcc_attribute_specifier_list_opt
1394 T_ASSIGN constant_expression {
1395 $$ = enumeration_constant($1, to_int_constant($4, true),
1396 false);
1397 }
1398 ;
1399
1400 /* TODO: Implement 'enum_type_specifier' from C23 6.7.3.3. */
1401
1402 /* K&R ---, C90 ---, C99 ---, C11 6.7.2.4, C23 6.7.3.5 */
1403 atomic_type_specifier:
1404 atomic T_LPAREN type_name T_RPAREN {
1405 $$ = $3;
1406 }
1407 ;
1408
1409 atomic: /* helper */
1410 T_ATOMIC {
1411 /* TODO: First fix c11ism, then use it here. */
1412 if (!allow_c11)
1413 /* '_Atomic' requires C11 or later */
1414 error(350);
1415 }
1416 ;
1417
1418 /* TODO: Implement 'typeof_specifier' from C23 6.7.3.6. */
1419
1420 /* TODO: Implement 'typeof_specifier_argument' from C23 6.7.3.6. */
1421
1422 /* C99 6.7.3, C23 6.7.4.1 */
1423 type_qualifier:
1424 T_QUAL
1425 | atomic {
1426 $$ = (type_qualifiers){ .tq_atomic = true };
1427 }
1428 ;
1429
1430 /* TODO: Implement 'function_specifier' from C23 6.7.5. */
1431
1432 /* TODO: Implement 'alignment_specifier' from C23 6.7.6. */
1433
1434 /* C23 6.7.7.1 */
1435 /* The rule 'declarator' is split into the 'notype' and 'type' variants. */
1436
1437 notype_declarator:
1438 notype_direct_declarator
1439 | pointer notype_direct_declarator {
1440 $$ = add_pointer($2, $1);
1441 }
1442 ;
1443
1444 type_declarator:
1445 type_direct_declarator
1446 | pointer type_direct_declarator {
1447 $$ = add_pointer($2, $1);
1448 }
1449 ;
1450
1451 /* C23 6.7.7.1 */
1452 /* The rule 'direct_declarator' is split into the 'notype' and 'type' variants. */
1453
1454 notype_direct_declarator:
1455 type_attribute_list_opt T_NAME {
1456 $$ = declarator_name(getsym($2));
1457 }
1458 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN {
1459 $$ = $3;
1460 }
1461 | notype_direct_declarator T_LBRACK array_size_opt T_RBRACK {
1462 $$ = add_array($1, $3.has_dim, $3.dim);
1463 }
1464 | notype_direct_declarator param_list asm_or_symbolrename_opt {
1465 $$ = add_function(symbolrename($1, $3), $2);
1466 end_declaration_level();
1467 block_level--;
1468 }
1469 | notype_direct_declarator type_attribute
1470 ;
1471
1472 type_direct_declarator:
1473 type_attribute_list_opt identifier {
1474 $$ = declarator_name(getsym($2));
1475 }
1476 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN {
1477 $$ = $3;
1478 }
1479 | type_direct_declarator T_LBRACK array_size_opt T_RBRACK {
1480 $$ = add_array($1, $3.has_dim, $3.dim);
1481 }
1482 | type_direct_declarator param_list asm_or_symbolrename_opt {
1483 $$ = add_function(symbolrename($1, $3), $2);
1484 end_declaration_level();
1485 block_level--;
1486 }
1487 | type_direct_declarator type_attribute
1488 ;
1489
1490
1491 /* TODO: Implement 'array_declarator' from C23 6.7.7.1. */
1492
1493 /* TODO: Implement 'function_declarator' from C23 6.7.7.1. */
1494
1495 /* C99 6.7.5, C23 6.7.7.1 */
1496 pointer:
1497 T_ASTERISK type_qualifier_list_opt {
1498 $$ = xcalloc(1, sizeof(*$$));
1499 add_type_qualifiers(&$$->qualifiers, $2);
1500 }
1501 | T_ASTERISK type_qualifier_list_opt pointer {
1502 $$ = xcalloc(1, sizeof(*$$));
1503 add_type_qualifiers(&$$->qualifiers, $2);
1504 $$ = append_qualified_pointer($$, $3);
1505 }
1506 ;
1507
1508 /* see C99 6.7.5, C23 6.7.7.1 */
1509 type_qualifier_list_opt:
1510 /* empty */ {
1511 $$ = (type_qualifiers){ .tq_const = false };
1512 }
1513 | type_qualifier_list
1514 ;
1515
1516 /* C99 6.7.5 */
1517 type_qualifier_list:
1518 type_qualifier
1519 | type_qualifier_list type_qualifier {
1520 $$ = $1;
1521 add_type_qualifiers(&$$, $2);
1522 }
1523 ;
1524
1525 /* TODO: Implement 'parameter_type_list' from C23 6.7.7.1. */
1526
1527 /* TODO: Implement 'parameter_list' from C23 6.7.7.1. */
1528
1529 /* C23 6.7.7.1 */
1530 /* XXX: C99 6.7.5 defines the same name, but it looks completely different. */
1531 parameter_declaration:
1532 begin_type_declmods end_type {
1533 /* ^^ There is no check for the missing type-specifier. */
1534 $$ = declare_parameter(abstract_name(), false);
1535 }
1536 | begin_type_declaration_specifiers end_type {
1537 $$ = declare_parameter(abstract_name(), false);
1538 }
1539 | begin_type_declmods end_type notype_param_declarator {
1540 /* ^^ There is no check for the missing type-specifier. */
1541 $$ = declare_parameter($3, false);
1542 }
1543 /*
1544 * type_param_declarator is needed because of following conflict:
1545 * "typedef int a; f(int (a));" could be parsed as
1546 * "function with argument a of type int", or
1547 * "function with an unnamed (abstract) argument of type function".
1548 * This grammar realizes the second case.
1549 */
1550 | begin_type_declaration_specifiers end_type type_param_declarator {
1551 $$ = declare_parameter($3, false);
1552 }
1553 | begin_type_declmods end_type abstract_declarator {
1554 /* ^^ There is no check for the missing type-specifier. */
1555 $$ = declare_parameter($3, false);
1556 }
1557 | begin_type_declaration_specifiers end_type abstract_declarator {
1558 $$ = declare_parameter($3, false);
1559 }
1560 ;
1561
1562 /*
1563 * The two distinct rules type_param_declarator and notype_param_declarator
1564 * avoid a conflict in parameter lists. A typename enclosed in parentheses is
1565 * always treated as a typename, not an argument name. For example, after
1566 * "typedef double a;", the declaration "f(int (a));" is interpreted as
1567 * "f(int (double));", not "f(int a);".
1568 */
1569 type_param_declarator:
1570 direct_param_declarator
1571 | pointer direct_param_declarator {
1572 $$ = add_pointer($2, $1);
1573 }
1574 ;
1575
1576 notype_param_declarator:
1577 direct_notype_param_declarator
1578 | pointer direct_notype_param_declarator {
1579 $$ = add_pointer($2, $1);
1580 }
1581 ;
1582
1583 direct_param_declarator:
1584 identifier type_attribute_list {
1585 $$ = declarator_name(getsym($1));
1586 }
1587 | identifier {
1588 $$ = declarator_name(getsym($1));
1589 }
1590 | T_LPAREN notype_param_declarator T_RPAREN {
1591 $$ = $2;
1592 }
1593 | direct_param_declarator T_LBRACK array_size_opt T_RBRACK
1594 gcc_attribute_specifier_list_opt {
1595 $$ = add_array($1, $3.has_dim, $3.dim);
1596 }
1597 | direct_param_declarator param_list asm_or_symbolrename_opt {
1598 $$ = add_function(symbolrename($1, $3), $2);
1599 end_declaration_level();
1600 block_level--;
1601 }
1602 ;
1603
1604 direct_notype_param_declarator:
1605 identifier {
1606 $$ = declarator_name(getsym($1));
1607 }
1608 | T_LPAREN notype_param_declarator T_RPAREN {
1609 $$ = $2;
1610 }
1611 | direct_notype_param_declarator T_LBRACK array_size_opt T_RBRACK {
1612 $$ = add_array($1, $3.has_dim, $3.dim);
1613 }
1614 | direct_notype_param_declarator param_list asm_or_symbolrename_opt {
1615 $$ = add_function(symbolrename($1, $3), $2);
1616 end_declaration_level();
1617 block_level--;
1618 }
1619 ;
1620
1621 param_list:
1622 T_LPAREN {
1623 block_level++;
1624 begin_declaration_level(DLK_PROTO_PARAMS);
1625 } identifier_list T_RPAREN {
1626 $$ = (parameter_list){ .first = $3 };
1627 }
1628 | abstract_decl_param_list
1629 ;
1630
1631 array_size_opt:
1632 /* empty */ {
1633 $$.has_dim = false;
1634 $$.dim = 0;
1635 }
1636 | T_ASTERISK {
1637 /* since C99; variable length array of unspecified size */
1638 $$.has_dim = false; /* TODO: maybe change to true */
1639 $$.dim = 0; /* just as a placeholder */
1640 }
1641 | type_qualifier_list_opt T_SCLASS constant_expression {
1642 /* C11 6.7.6.3p7 */
1643 if ($2 != STATIC)
1644 yyerror("Bad attribute");
1645 /* static array size requires C11 or later */
1646 c11ism(343);
1647 $$.has_dim = true;
1648 $$.dim = $3 == NULL ? 0 : to_int_constant($3, false);
1649 }
1650 | type_qualifier {
1651 /* C11 6.7.6.2 */
1652 if (!$1.tq_restrict)
1653 yyerror("Bad attribute");
1654 $$.has_dim = true;
1655 $$.dim = 0;
1656 }
1657 | constant_expression {
1658 $$.has_dim = true;
1659 $$.dim = $1 == NULL ? 0 : to_int_constant($1, false);
1660 }
1661 ;
1662
1663 identifier_list: /* C99 6.7.5 */
1664 T_NAME {
1665 $$ = old_style_function_parameter_name(getsym($1));
1666 }
1667 | identifier_list T_COMMA T_NAME {
1668 $$ = concat_symbols($1,
1669 old_style_function_parameter_name(getsym($3)));
1670 }
1671 | identifier_list error
1672 ;
1673
1674 /* C99 6.7.6, C23 6.7.8 */
1675 /* XXX: C99 requires an additional specifier-qualifier-list. */
1676 type_name:
1677 {
1678 begin_declaration_level(DLK_ABSTRACT);
1679 } abstract_declaration {
1680 end_declaration_level();
1681 $$ = $2->s_type;
1682 }
1683 ;
1684
1685 abstract_declaration: /* specific to lint */
1686 begin_type_qualifier_list end_type {
1687 $$ = declare_abstract_type(abstract_name());
1688 }
1689 | begin_type_specifier_qualifier_list end_type {
1690 $$ = declare_abstract_type(abstract_name());
1691 }
1692 | begin_type_qualifier_list end_type abstract_declarator {
1693 $$ = declare_abstract_type($3);
1694 }
1695 | begin_type_specifier_qualifier_list end_type abstract_declarator {
1696 $$ = declare_abstract_type($3);
1697 }
1698 ;
1699
1700 abstract_decl_param_list: /* specific to lint */
1701 abstract_decl_lparen T_RPAREN type_attribute_opt {
1702 $$ = (parameter_list){ .first = NULL };
1703 }
1704 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN
1705 type_attribute_opt {
1706 $$ = $2;
1707 $$.prototype = true;
1708 }
1709 | abstract_decl_lparen error T_RPAREN type_attribute_opt {
1710 $$ = (parameter_list){ .first = NULL };
1711 }
1712 ;
1713
1714 abstract_decl_lparen: /* specific to lint */
1715 T_LPAREN {
1716 block_level++;
1717 begin_declaration_level(DLK_PROTO_PARAMS);
1718 }
1719 ;
1720
1721 vararg_parameter_type_list: /* specific to lint */
1722 parameter_type_list
1723 | parameter_type_list T_COMMA T_ELLIPSIS {
1724 $$ = $1;
1725 $$.vararg = true;
1726 }
1727 | T_ELLIPSIS {
1728 /* TODO: C99 6.7.5 makes this an error as well. */
1729 if (!allow_trad && !allow_c99)
1730 /* C90 to C17 require formal parameter before '...' */
1731 error(84);
1732 else if (allow_c90)
1733 /* C90 to C17 require formal parameter before '...' */
1734 warning(84);
1735 $$ = (parameter_list){ .vararg = true };
1736 }
1737 ;
1738
1739 /* XXX: C99 6.7.5 defines the same name, but it looks different. */
1740 parameter_type_list:
1741 parameter_declaration {
1742 $$ = (parameter_list){ .first = $1 };
1743 }
1744 | parameter_type_list T_COMMA parameter_declaration {
1745 $$ = $1;
1746 $$.first = concat_symbols($1.first, $3);
1747 }
1748 ;
1749
1750 /* K&R 8.7, C90 ???, C99 6.7.6, C11 6.7.7, C23 6.7.8 */
1751 /* In K&R, abstract-declarator could be empty and was still simpler. */
1752 abstract_declarator:
1753 pointer {
1754 $$ = add_pointer(abstract_name(), $1);
1755 }
1756 | direct_abstract_declarator
1757 | pointer direct_abstract_declarator {
1758 $$ = add_pointer($2, $1);
1759 }
1760 | type_attribute_list direct_abstract_declarator {
1761 $$ = $2;
1762 }
1763 | pointer type_attribute_list direct_abstract_declarator {
1764 $$ = add_pointer($3, $1);
1765 }
1766 ;
1767
1768 /* K&R ---, C90 ???, C99 6.7.6, C11 6.7.7, C23 6.7.8 */
1769 direct_abstract_declarator:
1770 /* TODO: sort rules according to C99 */
1771 T_LPAREN abstract_declarator T_RPAREN {
1772 $$ = $2;
1773 }
1774 | T_LBRACK array_size_opt T_RBRACK {
1775 $$ = add_array(abstract_name(), $2.has_dim, $2.dim);
1776 }
1777 | direct_abstract_declarator T_LBRACK array_size_opt T_RBRACK {
1778 $$ = add_array($1, $3.has_dim, $3.dim);
1779 }
1780 | abstract_decl_param_list asm_or_symbolrename_opt {
1781 sym_t *name = abstract_enclosing_name();
1782 $$ = add_function(symbolrename(name, $2), $1);
1783 end_declaration_level();
1784 block_level--;
1785 }
1786 | direct_abstract_declarator abstract_decl_param_list
1787 asm_or_symbolrename_opt {
1788 $$ = add_function(symbolrename($1, $3), $2);
1789 end_declaration_level();
1790 block_level--;
1791 }
1792 | direct_abstract_declarator type_attribute_list
1793 ;
1794
1795 /* TODO: Implement 'array_abstract_declarator' from C23 6.7.8. */
1796
1797 /* TODO: Implement 'function_abstract_declarator' from C23 6.7.8. */
1798
1799 /* TODO: Implement 'typedef_name' from C23 6.7.9. */
1800
1801 /* C23 6.7.11 */
1802 /* K&R ---, C90 ---, C99 6.7.8, C11 6.7.9, C23 6.7.10 */
1803 braced_initializer:
1804 init_lbrace init_rbrace {
1805 /* empty initializer braces require C23 or later */
1806 c23ism(353);
1807 }
1808 | init_lbrace initializer_list init_rbrace
1809 | init_lbrace initializer_list T_COMMA init_rbrace
1810 ;
1811
1812 /* C99 6.7.8, C23 6.7.11 */
1813 initializer:
1814 assignment_expression {
1815 init_expr($1);
1816 }
1817 | init_lbrace init_rbrace {
1818 /* XXX: Empty braces are not covered by C99 6.7.8. */
1819 }
1820 | init_lbrace initializer_list init_rbrace
1821 | init_lbrace initializer_list T_COMMA init_rbrace
1822 /* XXX: What is this error handling for? */
1823 | error
1824 ;
1825
1826 /* C99 6.7.8, C23 6.7.11 */
1827 initializer_list:
1828 initializer
1829 | designation initializer
1830 | initializer_list T_COMMA initializer
1831 | initializer_list T_COMMA designation initializer
1832 ;
1833
1834 /* C99 6.7.8, C23 6.7.11 */
1835 designation:
1836 {
1837 begin_designation();
1838 } designator_list T_ASSIGN
1839 | identifier T_COLON {
1840 /* GCC style struct or union member name in initializer */
1841 gnuism(315);
1842 begin_designation();
1843 add_designator_member($1);
1844 }
1845 ;
1846
1847 /* C99 6.7.8, C23 6.7.11 */
1848 designator_list:
1849 designator
1850 | designator_list designator
1851 ;
1852
1853 /* C99 6.7.8, C23 6.7.11 */
1854 designator:
1855 T_LBRACK range T_RBRACK {
1856 if (!allow_c99)
1857 /* array initializer with designators is a C99 ... */
1858 warning(321);
1859 add_designator_subscript($2);
1860 }
1861 | T_POINT identifier {
1862 if (!allow_c99)
1863 /* struct or union member name in initializer is ... */
1864 warning(313);
1865 add_designator_member($2);
1866 }
1867 ;
1868
1869 /* C23 6.7.12 */
1870 static_assert_declaration:
1871 T_STATIC_ASSERT T_LPAREN constant_expression T_COMMA T_STRING
1872 T_RPAREN T_SEMI {
1873 /* '_Static_assert' requires C11 or later */
1874 c11ism(354);
1875 }
1876 | T_STATIC_ASSERT T_LPAREN constant_expression T_RPAREN T_SEMI {
1877 /* '_Static_assert' without message requires C23 or later */
1878 c23ism(355);
1879 }
1880 ;
1881
1882 range:
1883 constant_expression {
1884 $$.lo = to_int_constant($1, true);
1885 $$.hi = $$.lo;
1886 }
1887 | constant_expression T_ELLIPSIS constant_expression {
1888 $$.lo = to_int_constant($1, true);
1889 $$.hi = to_int_constant($3, true);
1890 /* initialization with '[a...b]' is a GCC extension */
1891 gnuism(340);
1892 }
1893 ;
1894
1895 init_lbrace: /* helper */
1896 T_LBRACE {
1897 init_lbrace();
1898 }
1899 ;
1900
1901 init_rbrace: /* helper */
1902 T_RBRACE {
1903 init_rbrace();
1904 }
1905 ;
1906
1907 /* C23 6.7.13.2 */
1908 attribute_specifier_sequence:
1909 attribute_specifier {
1910 $$ = (attribute_list) { NULL, 0, 0 };
1911 attribute_list_add_all(&$$, $1);
1912 }
1913 | attribute_specifier_sequence attribute_specifier {
1914 $$ = $1;
1915 attribute_list_add_all(&$$, $2);
1916 }
1917 ;
1918
1919 /* C23 6.7.13.2 */
1920 attribute_specifier:
1921 T_LBRACK T_LBRACK attribute_list T_RBRACK T_RBRACK {
1922 $$ = $3;
1923 }
1924 ;
1925
1926 /* C23 6.7.13.2 */
1927 attribute_list:
1928 /* empty */ {
1929 $$ = (attribute_list) { NULL, 0, 0 };
1930 }
1931 | attribute {
1932 $$ = (attribute_list) { NULL, 0, 0 };
1933 attribute_list_add(&$$, $1);
1934 }
1935 | attribute_list T_COMMA
1936 | attribute_list T_COMMA attribute {
1937 $$ = $1;
1938 attribute_list_add(&$$, $3);
1939 }
1940 ;
1941
1942 /* C23 6.7.13.2 */
1943 attribute:
1944 identifier {
1945 $$ = new_attribute(NULL, $1, NULL);
1946 }
1947 | identifier T_DCOLON identifier {
1948 $$ = new_attribute($1, $3, NULL);
1949 }
1950 | identifier attribute_argument_clause {
1951 $$ = new_attribute(NULL, $1, &$2);
1952 }
1953 | identifier T_DCOLON identifier attribute_argument_clause {
1954 $$ = new_attribute($1, $3, &$4);
1955 }
1956 ;
1957
1958 /* The rule 'attribute_token' is inlined into 'attribute'. */
1959 /* The rule 'standard_attribute' is inlined into 'attribute_token'. */
1960 /* The rule 'attribute_prefixed_token' is inlined into 'attribute_token'. */
1961 /* The rule 'attribute_prefix' is inlined into 'attribute_token'. */
1962
1963 /* C23 6.7.13.2 */
1964 attribute_argument_clause:
1965 T_LPAREN {
1966 $$ = read_balanced_token_sequence();
1967 }
1968 ;
1969
1970 /* The rule 'balanced_token_sequence' is inlined into 'attribute_argument_clause'. */
1971 /* The rule 'balanced_token' is inlined into 'balanced_token_sequence'. */
1972
1973 asm_or_symbolrename_opt: /* GCC extensions */
1974 /* empty */ {
1975 $$ = NULL;
1976 }
1977 | T_ASM T_LPAREN T_STRING T_RPAREN gcc_attribute_specifier_list_opt {
1978 freeyyv(&$3, T_STRING);
1979 $$ = NULL;
1980 }
1981 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN
1982 gcc_attribute_specifier_list_opt {
1983 $$ = $3;
1984 }
1985 ;
1986
1987 /* K&R ???, C90 ???, C99 6.8, C11 ???, C23 6.8.1 */
1988 statement:
1989 expression_statement
1990 | non_expr_statement
1991 ;
1992
1993 /* Helper to avoid shift/reduce conflict in 'label: __attribute__ ;'. */
1994 no_attr_statement:
1995 expression_statement
1996 | no_attr_non_expr_statement
1997 ;
1998
1999 non_expr_statement: /* helper for C99 6.8 */
2000 gcc_attribute_specifier /* ((__fallthrough__)) */ T_SEMI
2001 | no_attr_non_expr_statement
2002 ;
2003
2004 /* Helper to avoid shift/reduce conflict in 'label: __attribute__ ;'. */
2005 no_attr_non_expr_statement:
2006 labeled_statement
2007 | compound_statement
2008 | selection_statement
2009 | iteration_statement
2010 | jump_statement {
2011 suppress_fallthrough = false;
2012 }
2013 | asm_statement
2014 ;
2015
2016 /* TODO: Implement 'unlabeled_statement' from C23 6.8.1. */
2017
2018 /* TODO: Implement 'primary_block' from C23 6.8.1. */
2019
2020 /* TODO: Implement 'secondary_block' from C23 6.8.1. */
2021
2022 /* C23 6.8.2 */
2023 label:
2024 T_NAME T_COLON {
2025 set_sym_kind(SK_LABEL);
2026 named_label(getsym($1));
2027 }
2028 | T_CASE constant_expression T_COLON {
2029 case_label($2);
2030 suppress_fallthrough = true;
2031 }
2032 | T_CASE constant_expression T_ELLIPSIS constant_expression T_COLON {
2033 /* XXX: We don't fill all cases */
2034 case_label($2);
2035 suppress_fallthrough = true;
2036 }
2037 | T_DEFAULT T_COLON {
2038 default_label();
2039 suppress_fallthrough = true;
2040 }
2041 ;
2042
2043 /* C99 6.8.1, C23 6.8.2 */
2044 labeled_statement:
2045 label gcc_attribute_specifier_list_opt no_attr_statement
2046 ;
2047
2048 /* C99 6.8.2, C23 6.8.3 */
2049 compound_statement:
2050 compound_statement_lbrace compound_statement_rbrace
2051 | compound_statement_lbrace block_item_list compound_statement_rbrace
2052 ;
2053
2054 compound_statement_lbrace:
2055 T_LBRACE {
2056 block_level++;
2057 mem_block_level++;
2058 debug_step("%s: mem_block_level = %zu",
2059 "compound_statement_lbrace", mem_block_level);
2060 begin_declaration_level(DLK_AUTO);
2061 }
2062 ;
2063
2064 compound_statement_rbrace:
2065 T_RBRACE {
2066 end_declaration_level();
2067 if (!in_statement_expr())
2068 level_free_all(mem_block_level); /* leak */
2069 mem_block_level--;
2070 debug_step("%s: mem_block_level = %zu",
2071 "compound_statement_rbrace", mem_block_level);
2072 block_level--;
2073 suppress_fallthrough = false;
2074 }
2075 ;
2076
2077 /* C99 6.8.2, C23 6.8.3 */
2078 block_item_list:
2079 block_item
2080 | block_item_list block_item {
2081 if ($1 && !$2)
2082 /* declarations after statements is a C99 feature */
2083 c99ism(327);
2084 $$ = $1 || $2;
2085 }
2086 ;
2087
2088 /* C99 6.8.2, C23 6.8.3 */
2089 block_item:
2090 declaration_or_error {
2091 $$ = false;
2092 restore_warning_flags();
2093 }
2094 | statement {
2095 $$ = true;
2096 restore_warning_flags();
2097 }
2098 ;
2099
2100 /* C99 6.8.3, C23 6.8.4 */
2101 expression_statement:
2102 expression T_SEMI {
2103 expr($1, false, false, false, false);
2104 suppress_fallthrough = false;
2105 }
2106 | T_SEMI {
2107 check_statement_reachable();
2108 suppress_fallthrough = false;
2109 }
2110 | attribute_specifier_sequence expression T_SEMI {
2111 debug_attribute_list(&$1);
2112 expr($2, false, false, false, false);
2113 suppress_fallthrough = false;
2114 }
2115 ;
2116
2117 /* C99 6.8.4, C23 6.8.5.1 */
2118 selection_statement:
2119 if_without_else %prec T_THEN {
2120 save_warning_flags();
2121 stmt_if_then_stmt();
2122 stmt_if_else_stmt(false);
2123 }
2124 | if_without_else T_ELSE {
2125 save_warning_flags();
2126 stmt_if_then_stmt();
2127 } statement {
2128 clear_warning_flags();
2129 stmt_if_else_stmt(true);
2130 }
2131 | if_without_else T_ELSE error {
2132 clear_warning_flags();
2133 stmt_if_else_stmt(false);
2134 }
2135 | switch_expr statement {
2136 clear_warning_flags();
2137 stmt_switch_expr_stmt();
2138 }
2139 | switch_expr error {
2140 clear_warning_flags();
2141 stmt_switch_expr_stmt();
2142 }
2143 ;
2144
2145 if_without_else: /* see C99 6.8.4 */
2146 if_expr statement
2147 | if_expr error
2148 ;
2149
2150 if_expr: /* see C99 6.8.4 */
2151 T_IF T_LPAREN expression T_RPAREN {
2152 stmt_if_expr($3);
2153 clear_warning_flags();
2154 }
2155 ;
2156
2157 switch_expr: /* see C99 6.8.4 */
2158 T_SWITCH T_LPAREN expression T_RPAREN {
2159 stmt_switch_expr($3);
2160 clear_warning_flags();
2161 }
2162 ;
2163
2164 /* C99 6.8.5, C23 6.8.6.1 */
2165 iteration_statement:
2166 while_expr statement {
2167 clear_warning_flags();
2168 stmt_while_expr_stmt();
2169 }
2170 | while_expr error {
2171 clear_warning_flags();
2172 stmt_while_expr_stmt();
2173 }
2174 | do_statement T_WHILE T_LPAREN expression T_RPAREN T_SEMI {
2175 stmt_do_while_expr($4);
2176 suppress_fallthrough = false;
2177 }
2178 | do error {
2179 clear_warning_flags();
2180 stmt_do_while_expr(NULL);
2181 }
2182 | for_exprs statement {
2183 clear_warning_flags();
2184 stmt_for_exprs_stmt();
2185 end_declaration_level();
2186 block_level--;
2187 }
2188 | for_exprs error {
2189 clear_warning_flags();
2190 stmt_for_exprs_stmt();
2191 end_declaration_level();
2192 block_level--;
2193 }
2194 ;
2195
2196 while_expr: /* see C99 6.8.5 */
2197 T_WHILE T_LPAREN expression T_RPAREN {
2198 stmt_while_expr($3);
2199 clear_warning_flags();
2200 }
2201 ;
2202
2203 do_statement: /* see C99 6.8.5 */
2204 do statement {
2205 clear_warning_flags();
2206 }
2207 ;
2208
2209 do: /* see C99 6.8.5 */
2210 T_DO {
2211 stmt_do();
2212 }
2213 ;
2214
2215 for_start: /* see C99 6.8.5 */
2216 T_FOR T_LPAREN {
2217 begin_declaration_level(DLK_AUTO);
2218 block_level++;
2219 }
2220 ;
2221
2222 for_exprs: /* see C99 6.8.5 */
2223 for_start
2224 begin_type_declaration_specifiers end_type
2225 notype_init_declarator_list T_SEMI
2226 expression_opt T_SEMI
2227 expression_opt T_RPAREN {
2228 /* variable declaration in for loop */
2229 c99ism(325);
2230 stmt_for_exprs(NULL, $6, $8);
2231 clear_warning_flags();
2232 }
2233 | for_start
2234 expression_opt T_SEMI
2235 expression_opt T_SEMI
2236 expression_opt T_RPAREN {
2237 stmt_for_exprs($2, $4, $6);
2238 clear_warning_flags();
2239 }
2240 ;
2241
2242 /* C99 6.8.6, C23 6.8.7.1 */
2243 jump_statement:
2244 goto identifier T_SEMI {
2245 stmt_goto(getsym($2));
2246 }
2247 | goto error T_SEMI {
2248 set_sym_kind(SK_VCFT);
2249 }
2250 | T_CONTINUE T_SEMI {
2251 stmt_continue();
2252 }
2253 | T_BREAK T_SEMI {
2254 stmt_break();
2255 }
2256 | T_RETURN sys T_SEMI {
2257 stmt_return($2, NULL);
2258 }
2259 | T_RETURN sys expression T_SEMI {
2260 stmt_return($2, $3);
2261 }
2262 ;
2263
2264 goto: /* see C99 6.8.6 */
2265 T_GOTO {
2266 set_sym_kind(SK_LABEL);
2267 }
2268 ;
2269
2270 asm_statement: /* GCC extension */
2271 T_ASM T_LPAREN read_until_rparen T_SEMI {
2272 dcs_set_asm();
2273 }
2274 | T_ASM type_qualifier T_LPAREN read_until_rparen T_SEMI {
2275 dcs_set_asm();
2276 }
2277 | T_ASM error
2278 ;
2279
2280 read_until_rparen: /* helper for 'asm_statement' */
2281 /* empty */ {
2282 read_until_rparen();
2283 }
2284 ;
2285
2286 /* C99 6.9, C23 6.9.1 */
2287 translation_unit:
2288 external_declaration
2289 | translation_unit external_declaration
2290 ;
2291
2292 /* C99 6.9, C23 6.9.1 */
2293 external_declaration:
2294 function_definition {
2295 global_clean_up_decl(false);
2296 clear_warning_flags();
2297 }
2298 | top_level_declaration {
2299 global_clean_up_decl(false);
2300 clear_warning_flags();
2301 }
2302 | asm_statement /* GCC extension */
2303 | T_SEMI { /* GCC extension */
2304 /*
2305 * TODO: Only allow this in GCC mode, not in plain C99.
2306 * This is one of the top 10 warnings in the NetBSD build.
2307 */
2308 if (!allow_trad && !allow_c99)
2309 /* empty declaration */
2310 error(0);
2311 else if (allow_c90)
2312 /* empty declaration */
2313 warning(0);
2314 }
2315 ;
2316
2317 /*
2318 * On the top level, lint allows several forms of declarations that it doesn't
2319 * allow in functions. For example, a single ';' is an empty declaration and
2320 * is supported by some compilers, but in a function it would be an empty
2321 * statement, not a declaration. This makes a difference in C90 mode, where
2322 * a statement must not be followed by a declaration.
2323 *
2324 * See 'declaration' for all other declarations.
2325 */
2326 top_level_declaration: /* C99 6.9 calls this 'declaration' */
2327 begin_type end_type notype_init_declarator_list T_SEMI {
2328 /* TODO: Make this an error in C99 mode as well. */
2329 if (!allow_trad && !allow_c99)
2330 /* old-style declaration; add 'int' */
2331 error(1);
2332 else if (allow_c90)
2333 /* old-style declaration; add 'int' */
2334 warning(1);
2335 }
2336 | declaration
2337 | error T_SEMI {
2338 global_clean_up();
2339 }
2340 | error T_RBRACE {
2341 global_clean_up();
2342 }
2343 ;
2344
2345 /* C99 6.9.1, C23 6.9.2 */
2346 function_definition:
2347 func_declarator {
2348 if ($1->s_type->t_tspec != FUNC) {
2349 /* syntax error '%s' */
2350 error(249, yytext);
2351 YYERROR;
2352 }
2353 if ($1->s_type->t_typedef) {
2354 /* ()-less function definition */
2355 error(64);
2356 YYERROR;
2357 }
2358 check_extern_declaration($1);
2359 begin_function($1);
2360 block_level++;
2361 begin_declaration_level(DLK_OLD_STYLE_PARAMS);
2362 if (lwarn == LWARN_NONE)
2363 $1->s_used = true;
2364 } arg_declaration_list_opt {
2365 end_declaration_level();
2366 block_level--;
2367 check_func_lint_directives();
2368 check_func_old_style_parameters();
2369 begin_control_statement(CS_FUNCTION_BODY);
2370 } compound_statement {
2371 end_function();
2372 end_control_statement(CS_FUNCTION_BODY);
2373 }
2374 ;
2375
2376 func_declarator:
2377 begin_type end_type notype_declarator {
2378 if (!allow_trad)
2379 /* old-style declaration; add 'int' */
2380 error(1);
2381 $$ = $3;
2382 }
2383 | begin_type_declmods end_type notype_declarator {
2384 if (!allow_trad)
2385 /* old-style declaration; add 'int' */
2386 error(1);
2387 $$ = $3;
2388 }
2389 | begin_type_declaration_specifiers end_type type_declarator {
2390 $$ = $3;
2391 }
2392 ;
2393
2394 arg_declaration_list_opt: /* C99 6.9.1p13 example 1 */
2395 /* empty */
2396 | arg_declaration_list
2397 ;
2398
2399 arg_declaration_list: /* C99 6.9.1p13 example 1 */
2400 arg_declaration
2401 | arg_declaration_list arg_declaration
2402 /* XXX or better "arg_declaration error" ? */
2403 | error
2404 ;
2405
2406 /*
2407 * "arg_declaration" is separated from "declaration" because it
2408 * needs other error handling.
2409 */
2410 arg_declaration:
2411 begin_type_declmods end_type T_SEMI {
2412 /* empty declaration */
2413 warning(2);
2414 }
2415 | begin_type_declmods end_type notype_init_declarator_list T_SEMI
2416 | begin_type_declaration_specifiers end_type T_SEMI {
2417 if (!dcs->d_nonempty_decl)
2418 /* empty declaration */
2419 warning(2);
2420 else
2421 /* '%s' declared in parameter declaration list */
2422 warning(3, type_name(dcs->d_type));
2423 }
2424 | begin_type_declaration_specifiers end_type
2425 type_init_declarator_list T_SEMI {
2426 if (dcs->d_nonempty_decl)
2427 /* '%s' declared in parameter declaration list */
2428 warning(3, type_name(dcs->d_type));
2429 }
2430 | begin_type_declmods error
2431 | begin_type_declaration_specifiers error
2432 ;
2433
2434 /* https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html */
2435 gcc_attribute_specifier_list_opt:
2436 /* empty */
2437 | gcc_attribute_specifier_list
2438 ;
2439
2440 gcc_attribute_specifier_list:
2441 gcc_attribute_specifier
2442 | gcc_attribute_specifier_list gcc_attribute_specifier
2443 ;
2444
2445 gcc_attribute_specifier:
2446 T_ATTRIBUTE T_LPAREN T_LPAREN {
2447 in_gcc_attribute = true;
2448 } gcc_attribute_list {
2449 in_gcc_attribute = false;
2450 } T_RPAREN T_RPAREN
2451 ;
2452
2453 gcc_attribute_list:
2454 gcc_attribute
2455 | gcc_attribute_list T_COMMA gcc_attribute
2456 ;
2457
2458 gcc_attribute:
2459 /* empty */
2460 | T_NAME {
2461 const char *name = $1->sb_name;
2462 if (is_either(name, "packed", "__packed__"))
2463 dcs_add_packed();
2464 else if (is_either(name, "used", "__used__") ||
2465 is_either(name, "unused", "__unused__"))
2466 dcs_set_used();
2467 else if (is_either(name, "fallthrough", "__fallthrough__"))
2468 suppress_fallthrough = true;
2469 }
2470 | T_NAME T_LPAREN T_RPAREN
2471 | T_NAME T_LPAREN argument_expression_list T_RPAREN {
2472 const char *name = $1->sb_name;
2473 if (is_either(name, "aligned", "__aligned__")
2474 && $3->args_len == 1)
2475 dcs_add_alignas($3->args[0]);
2476 }
2477 | type_qualifier {
2478 if (!$1.tq_const)
2479 yyerror("Bad attribute");
2480 }
2481 ;
2482
2483 /* The rule 'function_body' from C23 6.9.2 is inlined into 'function_definition'. */
2484
2485 sys:
2486 /* empty */ {
2487 $$ = in_system_header;
2488 }
2489 ;
2490
2491 %%
2492
2493 /* ARGSUSED */
2494 int
2495 yyerror(const char *msg)
2496 {
2497 /* syntax error '%s' */
2498 error(249, yytext);
2499 if (++sytxerr >= 5)
2500 norecover();
2501 return 0;
2502 }
2503
2504 #if YYDEBUG && YYBYACC
2505 static const char *
2506 cgram_to_string(int tok, YYSTYPE val)
2507 {
2508
2509 switch (tok) {
2510 case T_INCDEC:
2511 return val.y_inc ? "++" : "--";
2512 case T_MULTIPLICATIVE:
2513 case T_ADDITIVE:
2514 case T_SHIFT:
2515 case T_RELATIONAL:
2516 case T_EQUALITY:
2517 case T_OPASSIGN:
2518 return op_name(val.y_op);
2519 case T_SCLASS:
2520 return scl_name(val.y_scl);
2521 case T_TYPE:
2522 case T_STRUCT_OR_UNION:
2523 return tspec_name(val.y_tspec);
2524 case T_QUAL:
2525 return type_qualifiers_string(val.y_type_qualifiers);
2526 case T_FUNCTION_SPECIFIER:
2527 return function_specifier_name(val.y_function_specifier);
2528 case T_NAME:
2529 return val.y_name->sb_name;
2530 default:
2531 return "<none>";
2532 }
2533 }
2534 #endif
2535
2536 static void
2537 cgram_declare(sym_t *decl, bool has_initializer, sbuf_t *renaming)
2538 {
2539 declare(decl, has_initializer, renaming);
2540 if (renaming != NULL)
2541 freeyyv(&renaming, T_NAME);
2542 }
2543
2544 /*
2545 * Discard all input tokens up to and including the next unmatched right
2546 * parenthesis.
2547 */
2548 static void
2549 read_until_rparen(void)
2550 {
2551 int level;
2552
2553 if (yychar < 0)
2554 yychar = yylex();
2555 freeyyv(&yylval, yychar);
2556
2557 level = 1;
2558 while (yychar > 0) {
2559 if (yychar == T_LPAREN)
2560 level++;
2561 if (yychar == T_RPAREN && --level == 0)
2562 break;
2563 freeyyv(&yylval, yychar = yylex());
2564 }
2565
2566 yyclearin;
2567 }
2568
2569 static balanced_token_sequence
2570 read_balanced_token_sequence(void)
2571 {
2572 lint_assert(yychar < 0);
2573 balanced_token_sequence seq = lex_balanced();
2574 yyclearin;
2575 return seq;
2576 }
2577
2578 static sym_t *
2579 symbolrename(sym_t *s, sbuf_t *sb)
2580 {
2581 if (sb != NULL)
2582 s->s_rename = sb->sb_name;
2583 return s;
2584 }
2585