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