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