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