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