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