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