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