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