Home | History | Annotate | Line # | Download | only in lint1
decl.c revision 1.349
      1 /* $NetBSD: decl.c,v 1.349 2023/07/12 16:07:35 rillig Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
      5  * Copyright (c) 1994, 1995 Jochen Pohl
      6  * All Rights Reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Jochen Pohl for
     19  *	The NetBSD Project.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #if HAVE_NBTOOL_CONFIG_H
     36 #include "nbtool_config.h"
     37 #endif
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(__RCSID)
     41 __RCSID("$NetBSD: decl.c,v 1.349 2023/07/12 16:07:35 rillig Exp $");
     42 #endif
     43 
     44 #include <sys/param.h>
     45 #include <limits.h>
     46 #include <stdlib.h>
     47 #include <string.h>
     48 
     49 #include "lint1.h"
     50 
     51 const char unnamed[] = "<unnamed>";
     52 
     53 /* shared type structures for arithmetic types and void */
     54 static	type_t	typetab[NTSPEC];
     55 
     56 /* value of next enumerator during declaration of enum types */
     57 int	enumval;
     58 
     59 /*
     60  * Points to the innermost element of a stack that contains information about
     61  * nested declarations, such as struct declarations, function prototypes,
     62  * local variables.
     63  */
     64 decl_level	*dcs;
     65 
     66 static	type_t	*typedef_error(type_t *, tspec_t);
     67 static	void	set_first_typedef(type_t *, sym_t *);
     68 static	void	dcs_align(unsigned int, unsigned int);
     69 static	sym_t	*new_tag(sym_t *, scl_t, bool, bool);
     70 static	bool	prototypes_compatible(const type_t *, const type_t *, bool *);
     71 static	bool	matches_no_arg_function(const type_t *, bool *);
     72 static	bool	check_old_style_definition(sym_t *, sym_t *);
     73 static	bool	check_prototype_declaration(sym_t *, sym_t *);
     74 static	void	check_prototype_parameters(sym_t *);
     75 static	void	old_style_function(sym_t *, sym_t *);
     76 static	void	declare_external_in_block(sym_t *);
     77 static	bool	check_init(sym_t *);
     78 static	void	check_argument_usage(bool, sym_t *);
     79 static	void	check_variable_usage(bool, sym_t *);
     80 static	void	check_label_usage(sym_t *);
     81 static	void	check_tag_usage(sym_t *);
     82 static	void	check_global_variable(const sym_t *);
     83 static	void	check_global_variable_size(const sym_t *);
     84 
     85 /*
     86  * initializes all global vars used in declarations
     87  */
     88 void
     89 #ifdef __sh3__
     90 /* XXX port-sh3/56311 */
     91 __attribute__((optimize("O0")))
     92 #endif
     93 initdecl(void)
     94 {
     95 
     96 	/* declaration stack */
     97 	dcs = xcalloc(1, sizeof(*dcs));
     98 	dcs->d_kind = DLK_EXTERN;
     99 	dcs->d_last_dlsym = &dcs->d_first_dlsym;
    100 
    101 	if (!pflag) {
    102 		for (size_t i = 0; i < NTSPEC; i++) {
    103 			if (ttab[i].tt_rank_kind != RK_NONE)
    104 				ttab[i].tt_rank_value =
    105 				    ttab[i].tt_size_in_bits;
    106 		}
    107 		ttab[BOOL].tt_rank_kind = RK_INTEGER;
    108 		ttab[BOOL].tt_rank_value = 1;
    109 	}
    110 
    111 	if (Tflag) {
    112 		ttab[BOOL].tt_is_integer = false;
    113 		ttab[BOOL].tt_is_uinteger = false;
    114 		ttab[BOOL].tt_is_arithmetic = false;
    115 	}
    116 
    117 	/* struct, union, enum, ptr, array and func are not shared. */
    118 	for (int i = (int)SIGNED; i < (int)STRUCT; i++)
    119 		typetab[i].t_tspec = (tspec_t)i;
    120 }
    121 
    122 /*
    123  * Returns a shared type structure for arithmetic types and void.
    124  *
    125  * It's important to duplicate this structure using block_dup_type or
    126  * expr_dup_type if it is to be modified (adding qualifiers or anything
    127  * else).
    128  */
    129 type_t *
    130 gettyp(tspec_t t)
    131 {
    132 
    133 	lint_assert((int)t < (int)STRUCT);
    134 	/* TODO: make the return type 'const' */
    135 	return &typetab[t];
    136 }
    137 
    138 type_t *
    139 block_dup_type(const type_t *tp)
    140 {
    141 
    142 	type_t *ntp = block_zero_alloc(sizeof(*ntp));
    143 	*ntp = *tp;
    144 	return ntp;
    145 }
    146 
    147 /* Duplicate a type, free the allocated memory after the expression. */
    148 type_t *
    149 expr_dup_type(const type_t *tp)
    150 {
    151 
    152 	type_t *ntp = expr_zero_alloc(sizeof(*ntp));
    153 	*ntp = *tp;
    154 	return ntp;
    155 }
    156 
    157 /*
    158  * Return the unqualified version of the type.  The returned type is freed at
    159  * the end of the current expression.
    160  *
    161  * See C99 6.2.5p25.
    162  */
    163 type_t *
    164 expr_unqualified_type(const type_t *tp)
    165 {
    166 
    167 	type_t *ntp = expr_zero_alloc(sizeof(*ntp));
    168 	*ntp = *tp;
    169 	ntp->t_const = false;
    170 	ntp->t_volatile = false;
    171 
    172 	/*
    173 	 * In case of a struct or union type, the members should lose their
    174 	 * qualifiers as well, but that would require a deep copy of the
    175 	 * struct or union type.  This in turn would defeat the type
    176 	 * comparison in types_compatible, which simply tests whether
    177 	 * tp1->t_sou == tp2->t_sou.
    178 	 */
    179 
    180 	return ntp;
    181 }
    182 
    183 /*
    184  * Returns whether the argument is void or an incomplete array, struct, union
    185  * or enum type.
    186  */
    187 bool
    188 is_incomplete(const type_t *tp)
    189 {
    190 	tspec_t t = tp->t_tspec;
    191 
    192 	if (t == VOID)
    193 		return true;
    194 	if (t == ARRAY)
    195 		return tp->t_incomplete_array;
    196 	if (is_struct_or_union(t))
    197 		return tp->t_sou->sou_incomplete;
    198 	if (t == ENUM)
    199 		return tp->t_enum->en_incomplete;
    200 	return false;
    201 }
    202 
    203 void
    204 dcs_add_function_specifier(function_specifier fs)
    205 {
    206 	debug_step("%s: %s", __func__, function_specifier_name(fs));
    207 	if (fs == FS_INLINE) {
    208 		if (dcs->d_inline)
    209 			/* duplicate '%s' */
    210 			warning(10, "inline");
    211 		dcs->d_inline = true;
    212 	}
    213 }
    214 
    215 /*
    216  * Remember the storage class of the current declaration and detect multiple
    217  * storage classes.
    218  */
    219 void
    220 dcs_add_storage_class(scl_t sc)
    221 {
    222 
    223 	if (dcs->d_type != NULL || dcs->d_abstract_type != NO_TSPEC ||
    224 	    dcs->d_sign_mod != NO_TSPEC || dcs->d_rank_mod != NO_TSPEC) {
    225 		/* storage class after type is obsolescent */
    226 		warning(83);
    227 	}
    228 
    229 	if (dcs->d_scl == NOSCL)
    230 		dcs->d_scl = sc;
    231 	else
    232 		dcs->d_multiple_storage_classes = true;
    233 }
    234 
    235 /*
    236  * Remember the type, modifier or typedef name returned by the parser in the
    237  * top element of the declaration stack. This information is used in
    238  * dcs_end_type to build the type used for all declarators in this declaration.
    239  *
    240  * If tp->t_typedef is true, the type comes from a previously defined typename.
    241  * Otherwise, it comes from a type specifier (int, long, ...) or a
    242  * struct/union/enum tag.
    243  */
    244 void
    245 dcs_add_type(type_t *tp)
    246 {
    247 
    248 	debug_step("%s: %s", __func__, type_name(tp));
    249 	if (tp->t_typedef) {
    250 		/*
    251 		 * something like "typedef int a; int a b;"
    252 		 * This should not happen with current grammar.
    253 		 */
    254 		lint_assert(dcs->d_type == NULL);
    255 		lint_assert(dcs->d_abstract_type == NO_TSPEC);
    256 		lint_assert(dcs->d_sign_mod == NO_TSPEC);
    257 		lint_assert(dcs->d_rank_mod == NO_TSPEC);
    258 
    259 		dcs->d_type = tp;
    260 		return;
    261 	}
    262 
    263 	tspec_t t = tp->t_tspec;
    264 	if (is_struct_or_union(t) || t == ENUM) {
    265 		/*
    266 		 * something like "int struct a ..."
    267 		 * struct/union/enum with anything else is not allowed
    268 		 */
    269 		if (dcs->d_type != NULL || dcs->d_abstract_type != NO_TSPEC ||
    270 		    dcs->d_rank_mod != NO_TSPEC || dcs->d_sign_mod != NO_TSPEC) {
    271 			dcs->d_invalid_type_combination = true;
    272 			dcs->d_abstract_type = NO_TSPEC;
    273 			dcs->d_sign_mod = NO_TSPEC;
    274 			dcs->d_rank_mod = NO_TSPEC;
    275 		}
    276 		dcs->d_type = tp;
    277 		return;
    278 	}
    279 
    280 	if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
    281 		/*
    282 		 * something like "struct a int"
    283 		 * struct/union/enum with anything else is not allowed
    284 		 */
    285 		dcs->d_invalid_type_combination = true;
    286 		return;
    287 	}
    288 
    289 	if (t == COMPLEX) {
    290 		if (dcs->d_complex_mod == FLOAT)
    291 			t = FCOMPLEX;
    292 		else if (dcs->d_complex_mod == DOUBLE)
    293 			t = DCOMPLEX;
    294 		else {
    295 			/* invalid type for _Complex */
    296 			error(308);
    297 			t = DCOMPLEX; /* just as a fallback */
    298 		}
    299 		dcs->d_complex_mod = NO_TSPEC;
    300 	}
    301 
    302 	if (t == LONG && dcs->d_rank_mod == LONG) {
    303 		/* "long long" or "long ... long" */
    304 		t = LLONG;
    305 		dcs->d_rank_mod = NO_TSPEC;
    306 		if (!long_long_flag)
    307 			/* %s does not support 'long long' */
    308 			c99ism(265, allow_c90 ? "C90" : "traditional C");
    309 	}
    310 
    311 	if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
    312 		/* something like "typedef int a; a long ..." */
    313 		dcs->d_type = typedef_error(dcs->d_type, t);
    314 		return;
    315 	}
    316 
    317 	/* now it can be only a combination of arithmetic types and void */
    318 	if (t == SIGNED || t == UNSIGN) {
    319 		if (dcs->d_sign_mod != NO_TSPEC)
    320 			dcs->d_invalid_type_combination = true;
    321 		dcs->d_sign_mod = t;
    322 	} else if (t == SHORT || t == LONG || t == LLONG) {
    323 		if (dcs->d_rank_mod != NO_TSPEC)
    324 			dcs->d_invalid_type_combination = true;
    325 		dcs->d_rank_mod = t;
    326 	} else if (t == FLOAT || t == DOUBLE) {
    327 		if (dcs->d_rank_mod == NO_TSPEC || dcs->d_rank_mod == LONG) {
    328 			if (dcs->d_complex_mod != NO_TSPEC
    329 			    || (t == FLOAT && dcs->d_rank_mod == LONG))
    330 				dcs->d_invalid_type_combination = true;
    331 			dcs->d_complex_mod = t;
    332 		} else {
    333 			if (dcs->d_abstract_type != NO_TSPEC)
    334 				dcs->d_invalid_type_combination = true;
    335 			dcs->d_abstract_type = t;
    336 		}
    337 	} else if (t == PTR) {
    338 		dcs->d_type = tp;
    339 	} else {
    340 		if (dcs->d_abstract_type != NO_TSPEC)
    341 			dcs->d_invalid_type_combination = true;
    342 		dcs->d_abstract_type = t;
    343 	}
    344 }
    345 
    346 /* Merge the signedness into the abstract type. */
    347 static tspec_t
    348 merge_signedness(tspec_t t, tspec_t s)
    349 {
    350 
    351 	if (s == SIGNED)
    352 		return t == CHAR ? SCHAR : t;
    353 	if (s != UNSIGN)
    354 		return t;
    355 	return t == CHAR ? UCHAR
    356 	    : t == SHORT ? USHORT
    357 	    : t == INT ? UINT
    358 	    : t == LONG ? ULONG
    359 	    : t == LLONG ? ULLONG
    360 	    : t;
    361 }
    362 
    363 /*
    364  * Called if a list of declaration specifiers contains a typedef name
    365  * and other specifiers (except struct, union, enum, typedef name).
    366  */
    367 static type_t *
    368 typedef_error(type_t *td, tspec_t t)
    369 {
    370 
    371 	tspec_t t2 = td->t_tspec;
    372 
    373 	if ((t == SIGNED || t == UNSIGN) &&
    374 	    (t2 == CHAR || t2 == SHORT || t2 == INT ||
    375 	     t2 == LONG || t2 == LLONG)) {
    376 		if (allow_c90)
    377 			/* modifying typedef with '%s'; only qualifiers... */
    378 			warning(5, tspec_name(t));
    379 		td = block_dup_type(gettyp(merge_signedness(t2, t)));
    380 		td->t_typedef = true;
    381 		return td;
    382 	}
    383 
    384 	if (t == SHORT && (t2 == INT || t2 == UINT)) {
    385 		/* modifying typedef with '%s'; only qualifiers allowed */
    386 		warning(5, "short");
    387 		td = block_dup_type(gettyp(t2 == INT ? SHORT : USHORT));
    388 		td->t_typedef = true;
    389 		return td;
    390 	}
    391 
    392 	if (t != LONG)
    393 		goto invalid;
    394 
    395 	if (t2 == INT)
    396 		td = gettyp(LONG);
    397 	else if (t2 == UINT)
    398 		td = gettyp(ULONG);
    399 	else if (t2 == LONG)
    400 		td = gettyp(LLONG);
    401 	else if (t2 == ULONG)
    402 		td = gettyp(ULLONG);
    403 	else if (t2 == FLOAT)
    404 		td = gettyp(DOUBLE);
    405 	else if (t2 == DOUBLE)
    406 		td = gettyp(LDOUBLE);
    407 	else if (t2 == DCOMPLEX)
    408 		td = gettyp(LCOMPLEX);
    409 	else
    410 		goto invalid;
    411 
    412 	/* modifying typedef with '%s'; only qualifiers allowed */
    413 	warning(5, "long");
    414 	td = block_dup_type(td);
    415 	td->t_typedef = true;
    416 	return td;
    417 
    418 invalid:
    419 	/* Anything else is not accepted. */
    420 	dcs->d_invalid_type_combination = true;
    421 	return td;
    422 }
    423 
    424 static void
    425 set_first_typedef(type_t *tp, sym_t *sym)
    426 {
    427 
    428 	tspec_t t = tp->t_tspec;
    429 	if (is_struct_or_union(t) && tp->t_sou->sou_first_typedef == NULL)
    430 		tp->t_sou->sou_first_typedef = sym;
    431 	if (t == ENUM && tp->t_enum->en_first_typedef == NULL)
    432 		tp->t_enum->en_first_typedef = sym;
    433 }
    434 
    435 static unsigned int
    436 bit_fields_width(const sym_t **mem, bool *named)
    437 {
    438 	unsigned int width = 0;
    439 	unsigned int align = 0;
    440 	while (*mem != NULL && (*mem)->s_type->t_bitfield) {
    441 		if ((*mem)->s_name != unnamed)
    442 			*named = true;
    443 		width += (*mem)->s_type->t_bit_field_width;
    444 		unsigned int mem_align = alignment_in_bits((*mem)->s_type);
    445 		if (mem_align > align)
    446 			align = mem_align;
    447 		*mem = (*mem)->s_next;
    448 	}
    449 	return (width + align - 1) & -align;
    450 }
    451 
    452 static void
    453 pack_struct_or_union(type_t *tp)
    454 {
    455 
    456 	if (!is_struct_or_union(tp->t_tspec)) {
    457 		/* attribute '%s' ignored for '%s' */
    458 		warning(326, "packed", type_name(tp));
    459 		return;
    460 	}
    461 
    462 	unsigned int bits = 0;
    463 	bool named = false;
    464 	for (const sym_t *mem = tp->t_sou->sou_first_member;
    465 	     mem != NULL; mem = mem->s_next) {
    466 		// TODO: Maybe update mem->u.s_member.sm_offset_in_bits.
    467 		if (mem->s_type->t_bitfield) {
    468 			bits += bit_fields_width(&mem, &named);
    469 			if (mem == NULL)
    470 				break;
    471 		}
    472 		unsigned int mem_bits = type_size_in_bits(mem->s_type);
    473 		if (tp->t_tspec == STRUCT)
    474 			bits += mem_bits;
    475 		else if (mem_bits > bits)
    476 			bits = mem_bits;
    477 	}
    478 	tp->t_sou->sou_size_in_bits = bits;
    479 }
    480 
    481 void
    482 dcs_add_packed(void)
    483 {
    484 	if (dcs->d_type == NULL)
    485 		dcs->d_packed = true;
    486 	else
    487 		pack_struct_or_union(dcs->d_type);
    488 }
    489 
    490 void
    491 dcs_set_used(void)
    492 {
    493 	dcs->d_used = true;
    494 }
    495 
    496 /*
    497  * Remember a qualifier that is part of the declaration specifiers (and not the
    498  * declarator). The remembered qualifier is used by dcs_end_type for all
    499  * declarators.
    500  */
    501 void
    502 dcs_add_qualifier(tqual_t q)
    503 {
    504 
    505 	if (q == CONST) {
    506 		if (dcs->d_const) {
    507 			/* duplicate '%s' */
    508 			warning(10, "const");
    509 		}
    510 		dcs->d_const = true;
    511 	} else if (q == VOLATILE) {
    512 		if (dcs->d_volatile) {
    513 			/* duplicate '%s' */
    514 			warning(10, "volatile");
    515 		}
    516 		dcs->d_volatile = true;
    517 	} else {
    518 		lint_assert(q == RESTRICT || q == THREAD || q == ATOMIC);
    519 		/* Silently ignore these qualifiers. */
    520 	}
    521 }
    522 
    523 void
    524 begin_declaration_level(decl_level_kind kind)
    525 {
    526 
    527 	decl_level *dl = xcalloc(1, sizeof(*dl));
    528 	dl->d_enclosing = dcs;
    529 	dl->d_kind = kind;
    530 	dl->d_last_dlsym = &dl->d_first_dlsym;
    531 	dcs = dl;
    532 	debug_enter();
    533 	debug_dcs(true);
    534 }
    535 
    536 void
    537 end_declaration_level(void)
    538 {
    539 
    540 	debug_dcs(true);
    541 	debug_leave();
    542 
    543 	decl_level *dl = dcs;
    544 	dcs = dl->d_enclosing;
    545 	lint_assert(dcs != NULL);
    546 
    547 	switch (dl->d_kind) {
    548 	case DLK_STRUCT:
    549 	case DLK_UNION:
    550 	case DLK_ENUM:
    551 		/*
    552 		 * Symbols declared in (nested) structs or enums are part of
    553 		 * the next level (they are removed from the symbol table if
    554 		 * the symbols of the outer level are removed).
    555 		 */
    556 		if ((*dcs->d_last_dlsym = dl->d_first_dlsym) != NULL)
    557 			dcs->d_last_dlsym = dl->d_last_dlsym;
    558 		break;
    559 	case DLK_OLD_STYLE_ARGS:
    560 		/*
    561 		 * All symbols in dcs->d_first_dlsym are introduced in
    562 		 * old-style argument declarations (it's not clean, but
    563 		 * possible). They are appended to the list of symbols declared
    564 		 * in an old-style argument identifier list or a new-style
    565 		 * parameter type list.
    566 		 */
    567 		if (dl->d_first_dlsym != NULL) {
    568 			*dl->d_last_dlsym = dcs->d_func_proto_syms;
    569 			dcs->d_func_proto_syms = dl->d_first_dlsym;
    570 		}
    571 		break;
    572 	case DLK_ABSTRACT:
    573 		/*
    574 		 * Append all symbols declared in the abstract declaration to
    575 		 * the list of symbols declared in the surrounding declaration
    576 		 * or block.
    577 		 *
    578 		 * XXX I'm not sure whether they should be removed from the
    579 		 * symbol table now or later.
    580 		 */
    581 		if ((*dcs->d_last_dlsym = dl->d_first_dlsym) != NULL)
    582 			dcs->d_last_dlsym = dl->d_last_dlsym;
    583 		break;
    584 	case DLK_AUTO:
    585 		check_usage(dl);
    586 		/* FALLTHROUGH */
    587 	case DLK_PROTO_PARAMS:
    588 		/* usage of arguments will be checked by end_function() */
    589 		symtab_remove_level(dl->d_first_dlsym);
    590 		break;
    591 	case DLK_EXTERN:
    592 		/* there is nothing around an external declaration */
    593 		/* FALLTHROUGH */
    594 	default:
    595 		lint_assert(/*CONSTCOND*/false);
    596 	}
    597 	free(dl);
    598 }
    599 
    600 /*
    601  * Set flag d_asm in all declaration stack elements up to the outermost one.
    602  *
    603  * This is used to mark compound statements which have, possibly in nested
    604  * compound statements, asm statements. For these compound statements, no
    605  * warnings about unused or uninitialized variables are printed.
    606  *
    607  * There is no need to clear d_asm in decl_level structs with context AUTO, as
    608  * these structs are freed at the end of the compound statement. But it must be
    609  * cleared in the outermost decl_level struct, which has context EXTERN. This
    610  * could be done in dcs_begin_type and would work for C90, but not for C99 or
    611  * C++ (due to mixed statements and declarations). Thus, we clear it in
    612  * global_clean_up_decl.
    613  */
    614 void
    615 dcs_set_asm(void)
    616 {
    617 
    618 	for (decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing)
    619 		dl->d_asm = true;
    620 }
    621 
    622 void
    623 dcs_begin_type(void)
    624 {
    625 
    626 	debug_step("%s", __func__);
    627 	dcs->d_abstract_type = NO_TSPEC;
    628 	dcs->d_complex_mod = NO_TSPEC;
    629 	dcs->d_sign_mod = NO_TSPEC;
    630 	dcs->d_rank_mod = NO_TSPEC;
    631 	dcs->d_scl = NOSCL;
    632 	dcs->d_type = NULL;
    633 	dcs->d_const = false;
    634 	dcs->d_volatile = false;
    635 	dcs->d_inline = false;
    636 	dcs->d_multiple_storage_classes = false;
    637 	dcs->d_invalid_type_combination = false;
    638 	dcs->d_nonempty_decl = false;
    639 	dcs->d_no_type_specifier = false;
    640 }
    641 
    642 static void
    643 dcs_adjust_storage_class(void)
    644 {
    645 	if (dcs->d_kind == DLK_EXTERN) {
    646 		if (dcs->d_scl == REG || dcs->d_scl == AUTO) {
    647 			/* illegal storage class */
    648 			error(8);
    649 			dcs->d_scl = NOSCL;
    650 		}
    651 	} else if (dcs->d_kind == DLK_OLD_STYLE_ARGS ||
    652 		   dcs->d_kind == DLK_PROTO_PARAMS) {
    653 		if (dcs->d_scl != NOSCL && dcs->d_scl != REG) {
    654 			/* only 'register' is valid as storage class ... */
    655 			error(9);
    656 			dcs->d_scl = NOSCL;
    657 		}
    658 	}
    659 }
    660 
    661 /*
    662  * Merge the declaration specifiers from dcs into dcs->d_type.
    663  *
    664  * See C99 6.7.2 "Type specifiers".
    665  */
    666 static void
    667 dcs_merge_declaration_specifiers(void)
    668 {
    669 	tspec_t t = dcs->d_abstract_type;
    670 	tspec_t c = dcs->d_complex_mod;
    671 	tspec_t s = dcs->d_sign_mod;
    672 	tspec_t l = dcs->d_rank_mod;
    673 	type_t *tp = dcs->d_type;
    674 
    675 	if (tp != NULL) {
    676 		lint_assert(t == NO_TSPEC);
    677 		lint_assert(s == NO_TSPEC);
    678 		lint_assert(l == NO_TSPEC);
    679 		return;
    680 	}
    681 
    682 	debug_step("%s: %s", __func__, type_name(tp));
    683 
    684 	if (t == NO_TSPEC && s == NO_TSPEC && l == NO_TSPEC && c == NO_TSPEC)
    685 		dcs->d_no_type_specifier = true;
    686 	if (t == NO_TSPEC && s == NO_TSPEC && (l == NO_TSPEC || l == LONG))
    687 		t = c;
    688 
    689 	if (t == NO_TSPEC)
    690 		t = INT;
    691 	if (s == NO_TSPEC && t == INT)
    692 		s = SIGNED;
    693 	if (l != NO_TSPEC && t == CHAR) {
    694 		dcs->d_invalid_type_combination = true;
    695 		l = NO_TSPEC;
    696 	}
    697 	if (l == LONG && t == FLOAT) {
    698 		l = NO_TSPEC;
    699 		t = DOUBLE;
    700 		if (allow_c90)
    701 			/* use 'double' instead of 'long float' */
    702 			warning(6);
    703 	}
    704 	if ((l == LONG && t == DOUBLE) || t == LDOUBLE) {
    705 		l = NO_TSPEC;
    706 		t = LDOUBLE;
    707 	}
    708 	if (t == LDOUBLE && !allow_c90) {
    709 		/* 'long double' is illegal in traditional C */
    710 		warning(266);
    711 	}
    712 	if (l == LONG && t == DCOMPLEX) {
    713 		l = NO_TSPEC;
    714 		t = LCOMPLEX;
    715 	}
    716 
    717 	if (t != INT && t != CHAR && (s != NO_TSPEC || l != NO_TSPEC)) {
    718 		dcs->d_invalid_type_combination = true;
    719 		l = s = NO_TSPEC;
    720 	}
    721 	if (l != NO_TSPEC)
    722 		t = l;
    723 	dcs->d_type = gettyp(merge_signedness(t, s));
    724 }
    725 
    726 /* Create a type in 'dcs->d_type' from the information gathered in 'dcs'. */
    727 void
    728 dcs_end_type(void)
    729 {
    730 
    731 	debug_step("%s", __func__);
    732 	dcs_merge_declaration_specifiers();
    733 
    734 	if (dcs->d_multiple_storage_classes) {
    735 		/* only one storage class allowed */
    736 		error(7);
    737 	}
    738 	if (dcs->d_invalid_type_combination) {
    739 		/* illegal type combination */
    740 		error(4);
    741 	}
    742 
    743 	dcs_adjust_storage_class();
    744 
    745 	if (dcs->d_const && dcs->d_type->t_const && !dcs->d_type->t_typeof) {
    746 		lint_assert(dcs->d_type->t_typedef);
    747 		/* typedef already qualified with '%s' */
    748 		warning(68, "const");
    749 	}
    750 	if (dcs->d_volatile && dcs->d_type->t_volatile &&
    751 	    !dcs->d_type->t_typeof) {
    752 		lint_assert(dcs->d_type->t_typedef);
    753 		/* typedef already qualified with '%s' */
    754 		warning(68, "volatile");
    755 	}
    756 
    757 	if (dcs->d_const || dcs->d_volatile) {
    758 		dcs->d_type = block_dup_type(dcs->d_type);
    759 		dcs->d_type->t_const |= dcs->d_const;
    760 		dcs->d_type->t_volatile |= dcs->d_volatile;
    761 	}
    762 }
    763 
    764 /*
    765  * Return the length of a type in bits. For bit-fields, return the length of
    766  * the underlying storage type.
    767  *
    768  * Printing a message if the outermost dimension of an array is 0 must
    769  * be done by the caller. All other problems are reported by this function
    770  * if name is not NULL.
    771  */
    772 int
    773 length_in_bits(const type_t *tp, const char *name)
    774 {
    775 
    776 	if (tp == NULL)
    777 		return -1;
    778 
    779 	unsigned int elem = 1;
    780 	while (tp->t_tspec == ARRAY) {
    781 		elem *= tp->t_dim;
    782 		tp = tp->t_subt;
    783 	}
    784 
    785 	if (is_struct_or_union(tp->t_tspec)) {
    786 		if (is_incomplete(tp) && name != NULL) {
    787 			/* '%s' has incomplete type '%s' */
    788 			error(31, name, type_name(tp));
    789 		}
    790 		return (int)(elem * tp->t_sou->sou_size_in_bits);
    791 	}
    792 
    793 	if (tp->t_tspec == ENUM && is_incomplete(tp) && name != NULL)
    794 		/* incomplete enum type '%s' */
    795 		warning(13, name);
    796 
    797 	lint_assert(tp->t_tspec != FUNC);
    798 
    799 	unsigned int elsz = size_in_bits(tp->t_tspec);
    800 	/*
    801 	 * Workaround until the type parser (see add_function, add_array,
    802 	 * add_pointer) does not construct the invalid intermediate declaration
    803 	 * 'void b[4]' for the legitimate declaration 'void *b[4]'.
    804 	 */
    805 	if (sytxerr > 0 && elsz == 0)
    806 		elsz = CHAR_SIZE;
    807 	lint_assert(elsz > 0);
    808 	return (int)(elem * elsz);
    809 }
    810 
    811 unsigned int
    812 alignment_in_bits(const type_t *tp)
    813 {
    814 
    815 	/* Super conservative so that it works for most systems. */
    816 	unsigned int worst_align_in_bits = 2 * LONG_SIZE;
    817 
    818 	while (tp->t_tspec == ARRAY)
    819 		tp = tp->t_subt;
    820 
    821 	tspec_t t = tp->t_tspec;
    822 	unsigned int a;
    823 	if (is_struct_or_union(t))
    824 		a = tp->t_sou->sou_align_in_bits;
    825 	else {
    826 		lint_assert(t != FUNC);
    827 		if ((a = size_in_bits(t)) == 0)
    828 			a = CHAR_SIZE;
    829 		else if (a > worst_align_in_bits)
    830 			a = worst_align_in_bits;
    831 	}
    832 	lint_assert(a >= CHAR_SIZE);
    833 	lint_assert(a <= worst_align_in_bits);
    834 	return a;
    835 }
    836 
    837 /*
    838  * Concatenate two lists of symbols by s_next. Used by declarations of
    839  * struct/union/enum elements and parameters.
    840  */
    841 sym_t *
    842 concat_symbols(sym_t *l1, sym_t *l2)
    843 {
    844 
    845 	if (l1 == NULL)
    846 		return l2;
    847 	sym_t *l = l1;
    848 	while (l->s_next != NULL)
    849 		l = l->s_next;
    850 	l->s_next = l2;
    851 	return l1;
    852 }
    853 
    854 /*
    855  * Check if the type of the given symbol is valid.
    856  *
    857  * Invalid types are:
    858  * - arrays of incomplete types or functions
    859  * - functions returning arrays or functions
    860  * - void types other than type of function or pointer
    861  */
    862 void
    863 check_type(sym_t *sym)
    864 {
    865 
    866 	type_t **tpp = &sym->s_type;
    867 	tspec_t to = NO_TSPEC;
    868 	while (*tpp != NULL) {
    869 		type_t *tp = *tpp;
    870 		tspec_t t = tp->t_tspec;
    871 		/*
    872 		 * If this is the type of an old-style function definition,
    873 		 * a better warning is printed in begin_function().
    874 		 */
    875 		if (t == FUNC && !tp->t_proto &&
    876 		    !(to == NO_TSPEC && sym->s_osdef)) {
    877 			/* TODO: Make this an error in C99 mode as well. */
    878 			if ((!allow_trad && !allow_c99) && hflag)
    879 				/* function declaration is not a prototype */
    880 				warning(287);
    881 		}
    882 		if (to == FUNC) {
    883 			if (t == FUNC || t == ARRAY) {
    884 				/* function returns illegal type '%s' */
    885 				error(15, type_name(tp));
    886 				*tpp = block_derive_type(
    887 				    t == FUNC ? *tpp : (*tpp)->t_subt, PTR);
    888 				return;
    889 			}
    890 			if (tp->t_const || tp->t_volatile) {
    891 				/* TODO: Make this a warning in C99 mode as well. */
    892 				if (!allow_trad && !allow_c99) {	/* XXX or better allow_c90? */
    893 					/* function cannot return const... */
    894 					warning(228);
    895 				}
    896 			}
    897 		} else if (to == ARRAY) {
    898 			if (t == FUNC) {
    899 				/* array of function is illegal */
    900 				error(16);
    901 				*tpp = gettyp(INT);
    902 				return;
    903 			}
    904 			if (t == ARRAY && tp->t_dim == 0) {
    905 				/* null dimension */
    906 				error(17);
    907 				return;
    908 			}
    909 			if (t == VOID) {
    910 				/* illegal use of 'void' */
    911 				error(18);
    912 				*tpp = gettyp(INT);
    913 			}
    914 			/*
    915 			 * No need to check for incomplete types here as
    916 			 * length_in_bits already does this.
    917 			 */
    918 		} else if (to == NO_TSPEC && t == VOID) {
    919 			if (dcs->d_kind == DLK_PROTO_PARAMS) {
    920 				if (sym->s_scl != ABSTRACT) {
    921 					lint_assert(sym->s_name != unnamed);
    922 					/* void parameter '%s' cannot ... */
    923 					error(61, sym->s_name);
    924 					*tpp = gettyp(INT);
    925 				}
    926 			} else if (dcs->d_kind == DLK_ABSTRACT) {
    927 				/* ok */
    928 			} else if (sym->s_scl != TYPEDEF) {
    929 				/* void type for '%s' */
    930 				error(19, sym->s_name);
    931 				*tpp = gettyp(INT);
    932 			}
    933 		}
    934 		if (t == VOID && to != PTR) {
    935 			if (tp->t_const || tp->t_volatile) {
    936 				/* inappropriate qualifiers with 'void' */
    937 				warning(69);
    938 				tp->t_const = tp->t_volatile = false;
    939 			}
    940 		}
    941 		tpp = &tp->t_subt;
    942 		to = t;
    943 	}
    944 }
    945 
    946 /*
    947  * In traditional C, the only portable type for bit-fields is unsigned int.
    948  *
    949  * In C90, the only allowed types for bit-fields are int, signed int and
    950  * unsigned int (3.5.2.1).  There is no mention of implementation-defined
    951  * types.
    952  *
    953  * In C99, the only portable types for bit-fields are _Bool, signed int and
    954  * unsigned int (6.7.2.1p4).  In addition, C99 allows "or some other
    955  * implementation-defined type".
    956  */
    957 static void
    958 check_bit_field_type(sym_t *dsym, type_t **const inout_tp, tspec_t *inout_t)
    959 {
    960 	type_t *tp = *inout_tp;
    961 	tspec_t t = *inout_t;
    962 
    963 	if (t == CHAR || t == UCHAR || t == SCHAR ||
    964 	    t == SHORT || t == USHORT || t == ENUM) {
    965 		if (!bitfieldtype_ok) {
    966 			/* TODO: Make this an error in C99 mode as well. */
    967 			if (!allow_trad && !allow_c99) {
    968 				type_t *btp = block_dup_type(tp);
    969 				btp->t_bitfield = false;
    970 				/* bit-field type '%s' invalid in ANSI C */
    971 				warning(273, type_name(btp));
    972 			} else if (pflag) {
    973 				type_t *btp = block_dup_type(tp);
    974 				btp->t_bitfield = false;
    975 				/* nonportable bit-field type '%s' */
    976 				warning(34, type_name(btp));
    977 			}
    978 		}
    979 	} else if (t == INT && dcs->d_sign_mod == NO_TSPEC) {
    980 		if (pflag && !bitfieldtype_ok) {
    981 			/* bit-field of type plain 'int' has ... */
    982 			warning(344);
    983 		}
    984 	} else if (!(t == INT || t == UINT || t == BOOL ||
    985 		     (is_integer(t) && (bitfieldtype_ok || allow_gcc)))) {
    986 
    987 		type_t *btp = block_dup_type(tp);
    988 		btp->t_bitfield = false;
    989 		/* illegal bit-field type '%s' */
    990 		warning(35, type_name(btp));
    991 
    992 		// XXX: What about _Bool bit-fields since C99 6.7.2.1?
    993 		unsigned int width = tp->t_bit_field_width;
    994 		dsym->s_type = tp = block_dup_type(gettyp(t = INT));
    995 		if ((tp->t_bit_field_width = width) > size_in_bits(t))
    996 			tp->t_bit_field_width = size_in_bits(t);
    997 		*inout_t = t;
    998 		*inout_tp = tp;
    999 	}
   1000 }
   1001 
   1002 static void
   1003 check_bit_field(sym_t *dsym, tspec_t *inout_t, type_t **const inout_tp)
   1004 {
   1005 
   1006 	check_bit_field_type(dsym, inout_tp, inout_t);
   1007 
   1008 	type_t *tp = *inout_tp;
   1009 	tspec_t t = *inout_t;
   1010 	unsigned int t_width = size_in_bits(t);
   1011 	if (tp->t_bit_field_width > t_width) {
   1012 		/* illegal bit-field size: %d */
   1013 		error(36, (int)tp->t_bit_field_width);
   1014 		tp->t_bit_field_width = t_width;
   1015 	} else if (tp->t_bit_field_width == 0 && dsym->s_name != unnamed) {
   1016 		/* zero size bit-field */
   1017 		error(37);
   1018 		tp->t_bit_field_width = t_width;
   1019 	}
   1020 	if (dsym->s_scl == UNION_MEMBER) {
   1021 		/* bit-field in union is very unusual */
   1022 		warning(41);
   1023 		dsym->s_type->t_bitfield = false;
   1024 		dsym->s_bitfield = false;
   1025 	}
   1026 }
   1027 
   1028 /* Add a member to the struct or union type that is being built in 'dcs'. */
   1029 static void
   1030 dcs_add_member(sym_t *mem)
   1031 {
   1032 	type_t *tp = mem->s_type;
   1033 
   1034 	unsigned int union_size = 0;
   1035 	if (dcs->d_kind == DLK_UNION) {
   1036 		union_size = dcs->d_sou_size_in_bits;
   1037 		dcs->d_sou_size_in_bits = 0;
   1038 	}
   1039 
   1040 	if (mem->s_bitfield) {
   1041 		dcs_align(alignment_in_bits(tp), tp->t_bit_field_width);
   1042 		// XXX: Why round down?
   1043 		mem->u.s_member.sm_offset_in_bits = dcs->d_sou_size_in_bits
   1044 		    - dcs->d_sou_size_in_bits % size_in_bits(tp->t_tspec);
   1045 		tp->t_bit_field_offset = dcs->d_sou_size_in_bits
   1046 		    - mem->u.s_member.sm_offset_in_bits;
   1047 		dcs->d_sou_size_in_bits += tp->t_bit_field_width;
   1048 	} else {
   1049 		dcs_align(alignment_in_bits(tp), 0);
   1050 		mem->u.s_member.sm_offset_in_bits = dcs->d_sou_size_in_bits;
   1051 		dcs->d_sou_size_in_bits += type_size_in_bits(tp);
   1052 	}
   1053 
   1054 	if (union_size > dcs->d_sou_size_in_bits)
   1055 		dcs->d_sou_size_in_bits = union_size;
   1056 }
   1057 
   1058 sym_t *
   1059 declare_unnamed_member(void)
   1060 {
   1061 
   1062 	sym_t *mem = block_zero_alloc(sizeof(*mem));
   1063 	mem->s_name = unnamed;
   1064 	mem->s_kind = FMEMBER;
   1065 	mem->s_scl = STRUCT_MEMBER;
   1066 	mem->s_type = dcs->d_type;
   1067 	mem->s_block_level = -1;
   1068 
   1069 	dcs_add_member(mem);
   1070 	bitfieldtype_ok = false;
   1071 	return mem;
   1072 }
   1073 
   1074 sym_t *
   1075 declare_member(sym_t *dsym)
   1076 {
   1077 
   1078 	lint_assert(is_member(dsym));
   1079 
   1080 	if (dcs->d_redeclared_symbol != NULL) {
   1081 		lint_assert(is_member(dcs->d_redeclared_symbol));
   1082 
   1083 		if (dsym->u.s_member.sm_containing_type ==
   1084 		    dcs->d_redeclared_symbol->u.s_member.sm_containing_type) {
   1085 			/* duplicate member name '%s' */
   1086 			error(33, dsym->s_name);
   1087 			rmsym(dcs->d_redeclared_symbol);
   1088 		}
   1089 	}
   1090 
   1091 	check_type(dsym);
   1092 
   1093 	type_t *tp = dsym->s_type;
   1094 	tspec_t t = tp->t_tspec;
   1095 	if (dsym->s_bitfield)
   1096 		check_bit_field(dsym, &t, &tp);
   1097 	else if (t == FUNC) {
   1098 		/* function illegal in structure or union */
   1099 		error(38);
   1100 		dsym->s_type = tp = block_derive_type(tp, t = PTR);
   1101 	}
   1102 
   1103 	/*
   1104 	 * bit-fields of length 0 are not warned about because length_in_bits
   1105 	 * does not return the length of the bit-field but the length
   1106 	 * of the type the bit-field is packed in (it's ok)
   1107 	 */
   1108 	int sz = length_in_bits(dsym->s_type, dsym->s_name);
   1109 	if (sz == 0 && t == ARRAY && dsym->s_type->t_dim == 0) {
   1110 		/* zero-sized array '%s' in struct is a C99 extension */
   1111 		c99ism(39, dsym->s_name);
   1112 	}
   1113 
   1114 	dcs_add_member(dsym);
   1115 
   1116 	check_function_definition(dsym, false);
   1117 
   1118 	/*
   1119 	 * Clear the BITFIELDTYPE indicator after processing each
   1120 	 * structure element.
   1121 	 */
   1122 	bitfieldtype_ok = false;
   1123 
   1124 	return dsym;
   1125 }
   1126 
   1127 /* Aligns the next structure element as required. */
   1128 static void
   1129 dcs_align(unsigned int member_alignment, unsigned int bit_field_width)
   1130 {
   1131 
   1132 	if (member_alignment > dcs->d_sou_align_in_bits)
   1133 		dcs->d_sou_align_in_bits = member_alignment;
   1134 
   1135 	unsigned int offset = (dcs->d_sou_size_in_bits + member_alignment - 1)
   1136 	    & ~(member_alignment - 1);
   1137 	if (bit_field_width == 0
   1138 	    || dcs->d_sou_size_in_bits + bit_field_width > offset)
   1139 		dcs->d_sou_size_in_bits = offset;
   1140 }
   1141 
   1142 sym_t *
   1143 set_bit_field_width(sym_t *dsym, int bit_field_width)
   1144 {
   1145 
   1146 	if (dsym == NULL) {
   1147 		dsym = block_zero_alloc(sizeof(*dsym));
   1148 		dsym->s_name = unnamed;
   1149 		dsym->s_kind = FMEMBER;
   1150 		dsym->s_scl = STRUCT_MEMBER;
   1151 		dsym->s_type = gettyp(UINT);
   1152 		dsym->s_block_level = -1;
   1153 	}
   1154 	dsym->s_type = block_dup_type(dsym->s_type);
   1155 	dsym->s_type->t_bitfield = true;
   1156 	dsym->s_type->t_bit_field_width = bit_field_width;
   1157 	dsym->s_bitfield = true;
   1158 	return dsym;
   1159 }
   1160 
   1161 /*
   1162  * A sequence of asterisks and qualifiers, from right to left.  For example,
   1163  * 'const ***volatile **const volatile' results in [cvp, p, vp, p, p].  The
   1164  * leftmost 'const' is not included in this list, it is stored in dcs->d_const
   1165  * instead.
   1166  */
   1167 qual_ptr *
   1168 merge_qualified_pointer(qual_ptr *p1, qual_ptr *p2)
   1169 {
   1170 
   1171 	if (p2 == NULL)
   1172 		return p1;	/* for optional qualifiers */
   1173 
   1174 	if (p2->p_pointer) {
   1175 		/* append p1 to p2, keeping p2 */
   1176 		qual_ptr *tail = p2;
   1177 		while (tail->p_next != NULL)
   1178 			tail = tail->p_next;
   1179 		tail->p_next = p1;
   1180 		return p2;
   1181 	}
   1182 
   1183 	/* merge p2 into p1, keeping p1 */
   1184 	if (p2->p_const) {
   1185 		if (p1->p_const) {
   1186 			/* duplicate '%s' */
   1187 			warning(10, "const");
   1188 		}
   1189 		p1->p_const = true;
   1190 	}
   1191 	if (p2->p_volatile) {
   1192 		if (p1->p_volatile) {
   1193 			/* duplicate '%s' */
   1194 			warning(10, "volatile");
   1195 		}
   1196 		p1->p_volatile = true;
   1197 	}
   1198 	free(p2);
   1199 	return p1;
   1200 }
   1201 
   1202 static type_t *
   1203 block_derive_pointer(type_t *stp, bool is_const, bool is_volatile)
   1204 {
   1205 
   1206 	type_t *tp = block_derive_type(stp, PTR);
   1207 	tp->t_const = is_const;
   1208 	tp->t_volatile = is_volatile;
   1209 	return tp;
   1210 }
   1211 
   1212 /*
   1213  * The following 3 functions extend the type of a declarator with
   1214  * pointer, function and array types.
   1215  *
   1216  * The current type is the type built by dcs_end_type (dcs->d_type) and
   1217  * pointer, function and array types already added for this
   1218  * declarator. The new type extension is inserted between both.
   1219  */
   1220 sym_t *
   1221 add_pointer(sym_t *decl, qual_ptr *p)
   1222 {
   1223 
   1224 	debug_dcs(false);
   1225 
   1226 	type_t **tpp = &decl->s_type;
   1227 	while (*tpp != NULL && *tpp != dcs->d_type)
   1228 		tpp = &(*tpp)->t_subt;
   1229 	if (*tpp == NULL) {
   1230 		debug_step("add_pointer: unchanged '%s'",
   1231 		    type_name(decl->s_type));
   1232 		return decl;
   1233 	}
   1234 
   1235 	while (p != NULL) {
   1236 		*tpp = block_derive_pointer(dcs->d_type,
   1237 		    p->p_const, p->p_volatile);
   1238 
   1239 		tpp = &(*tpp)->t_subt;
   1240 
   1241 		qual_ptr *next = p->p_next;
   1242 		free(p);
   1243 		p = next;
   1244 	}
   1245 	debug_step("add_pointer: '%s'", type_name(decl->s_type));
   1246 	return decl;
   1247 }
   1248 
   1249 static type_t *
   1250 block_derive_array(type_t *stp, bool dim, int len)
   1251 {
   1252 
   1253 	type_t *tp = block_derive_type(stp, ARRAY);
   1254 	tp->t_dim = len;
   1255 
   1256 #if 0
   1257 	/*
   1258 	 * As of 2022-04-03, the implementation of the type parser (see
   1259 	 * add_function, add_array, add_pointer) is strange.  When it sees
   1260 	 * the type 'void *b[4]', it first creates 'void b[4]' and only later
   1261 	 * inserts the '*' in the middle of the type.  Late modifications like
   1262 	 * these should not be done at all, instead the parser should be fixed
   1263 	 * to process the type names in the proper syntactical order.
   1264 	 *
   1265 	 * Since the intermediate type would be an array of void, but the
   1266 	 * final type is valid, this check cannot be enabled yet.
   1267 	 */
   1268 	if (stp->t_tspec == VOID) {
   1269 		/* array of incomplete type */
   1270 		error(301);
   1271 		tp->t_subt = gettyp(CHAR);
   1272 	}
   1273 #endif
   1274 	if (len < 0) {
   1275 		/* negative array dimension (%d) */
   1276 		error(20, len);
   1277 	} else if (len == 0 && dim) {
   1278 		/* zero sized array is a C99 extension */
   1279 		c99ism(322);
   1280 	} else if (len == 0 && !dim)
   1281 		tp->t_incomplete_array = true;
   1282 
   1283 	return tp;
   1284 }
   1285 
   1286 /*
   1287  * If a dimension was specified, dim is true, otherwise false
   1288  * n is the specified dimension
   1289  */
   1290 sym_t *
   1291 add_array(sym_t *decl, bool dim, int n)
   1292 {
   1293 
   1294 	debug_dcs(false);
   1295 
   1296 	type_t **tpp = &decl->s_type;
   1297 	while (*tpp != NULL && *tpp != dcs->d_type)
   1298 		tpp = &(*tpp)->t_subt;
   1299 	if (*tpp == NULL) {
   1300 		debug_step("add_array: unchanged '%s'",
   1301 		    type_name(decl->s_type));
   1302 		return decl;
   1303 	}
   1304 
   1305 	*tpp = block_derive_array(dcs->d_type, dim, n);
   1306 
   1307 	debug_step("add_array: '%s'", type_name(decl->s_type));
   1308 	return decl;
   1309 }
   1310 
   1311 static type_t *
   1312 block_derive_function(type_t *ret, bool proto, sym_t *args, bool vararg)
   1313 {
   1314 
   1315 	type_t *tp = block_derive_type(ret, FUNC);
   1316 	tp->t_proto = proto;
   1317 	if (proto)
   1318 		tp->t_args = args;
   1319 	tp->t_vararg = vararg;
   1320 	return tp;
   1321 }
   1322 
   1323 sym_t *
   1324 add_function(sym_t *decl, sym_t *args)
   1325 {
   1326 
   1327 	debug_enter();
   1328 	debug_dcs(true);
   1329 	debug_sym("decl: ", decl, "\n");
   1330 #ifdef DEBUG
   1331 	for (const sym_t *arg = args; arg != NULL; arg = arg->s_next)
   1332 		debug_sym("arg: ", arg, "\n");
   1333 #endif
   1334 
   1335 	if (dcs->d_prototype) {
   1336 		if (!allow_c90)
   1337 			/* function prototypes are illegal in traditional C */
   1338 			warning(270);
   1339 		check_prototype_parameters(args);
   1340 		if (args != NULL && args->s_type->t_tspec == VOID)
   1341 			args = NULL;
   1342 	} else
   1343 		old_style_function(decl, args);
   1344 
   1345 	/*
   1346 	 * The symbols are removed from the symbol table by
   1347 	 * end_declaration_level after add_function. To be able to restore
   1348 	 * them if this is a function definition, a pointer to the list of
   1349 	 * all symbols is stored in dcs->d_enclosing->d_func_proto_syms. Also,
   1350 	 * a list of the arguments (concatenated by s_next) is stored in
   1351 	 * dcs->d_enclosing->d_func_args. (dcs->d_enclosing must be used
   1352 	 * because *dcs is the declaration stack element created for the list
   1353 	 * of params and is removed after add_function.)
   1354 	 */
   1355 	if (dcs->d_enclosing->d_kind == DLK_EXTERN &&
   1356 	    decl->s_type == dcs->d_enclosing->d_type) {
   1357 		dcs->d_enclosing->d_func_proto_syms = dcs->d_first_dlsym;
   1358 		dcs->d_enclosing->d_func_args = args;
   1359 	}
   1360 
   1361 	/*
   1362 	 * XXX: What is this code doing on a semantic level, and why?
   1363 	 * Returning decl leads to the wrong function types in msg_347.
   1364 	 */
   1365 	type_t **tpp = &decl->s_type;
   1366 	if (*tpp == NULL)
   1367 		decl->s_type = dcs->d_enclosing->d_type;
   1368 	while (*tpp != NULL && *tpp != dcs->d_enclosing->d_type)
   1369 		/*
   1370 		 * XXX: accessing INT->t_subt feels strange, even though it
   1371 		 * may even be guaranteed to be NULL.
   1372 		 */
   1373 		tpp = &(*tpp)->t_subt;
   1374 	if (*tpp == NULL) {
   1375 		debug_step("add_function: unchanged '%s'",
   1376 		    type_name(decl->s_type));
   1377 		debug_leave();
   1378 		return decl;	/* see msg_347 */
   1379 	}
   1380 
   1381 	*tpp = block_derive_function(dcs->d_enclosing->d_type,
   1382 	    dcs->d_prototype, args, dcs->d_vararg);
   1383 
   1384 	debug_step("add_function: '%s'", type_name(decl->s_type));
   1385 	debug_dcs(true);
   1386 	debug_leave();
   1387 	return decl;
   1388 }
   1389 
   1390 static void
   1391 check_prototype_parameters(sym_t *args)
   1392 {
   1393 
   1394 	for (sym_t *sym = dcs->d_first_dlsym;
   1395 	     sym != NULL; sym = sym->s_level_next) {
   1396 		scl_t sc = sym->s_scl;
   1397 		if (sc == STRUCT_TAG || sc == UNION_TAG || sc == ENUM_TAG) {
   1398 			/* dubious tag declaration '%s %s' */
   1399 			warning(85, storage_class_name(sc), sym->s_name);
   1400 		}
   1401 	}
   1402 
   1403 	for (sym_t *arg = args; arg != NULL; arg = arg->s_next) {
   1404 		if (arg->s_type->t_tspec == VOID &&
   1405 		    !(arg == args && arg->s_next == NULL)) {
   1406 			/* void must be sole parameter */
   1407 			error(60);
   1408 			arg->s_type = gettyp(INT);
   1409 		}
   1410 	}
   1411 }
   1412 
   1413 static void
   1414 old_style_function(sym_t *decl, sym_t *args)
   1415 {
   1416 
   1417 	/*
   1418 	 * Remember the list of parameters only if this really seems to be a
   1419 	 * function definition.
   1420 	 */
   1421 	if (dcs->d_enclosing->d_kind == DLK_EXTERN &&
   1422 	    decl->s_type == dcs->d_enclosing->d_type) {
   1423 		/*
   1424 		 * Assume that this becomes a function definition. If not, it
   1425 		 * will be corrected in check_function_definition.
   1426 		 */
   1427 		if (args != NULL) {
   1428 			decl->s_osdef = true;
   1429 			decl->u.s_old_style_args = args;
   1430 		}
   1431 	} else {
   1432 		if (args != NULL)
   1433 			/* function prototype parameters must have types */
   1434 			warning(62);
   1435 	}
   1436 }
   1437 
   1438 /*
   1439  * In a function declaration, a list of identifiers (as opposed to a list of
   1440  * types) is allowed only if it's also a function definition.
   1441  */
   1442 void
   1443 check_function_definition(sym_t *sym, bool msg)
   1444 {
   1445 
   1446 	if (sym->s_osdef) {
   1447 		if (msg) {
   1448 			/* incomplete or misplaced function definition */
   1449 			error(22);
   1450 		}
   1451 		sym->s_osdef = false;
   1452 		sym->u.s_old_style_args = NULL;
   1453 	}
   1454 }
   1455 
   1456 /*
   1457  * Process the name in a declarator. The symbol gets one of the storage classes
   1458  * EXTERN, STATIC, AUTO or TYPEDEF, as well as a definedness in 's_def'.
   1459  */
   1460 sym_t *
   1461 declarator_name(sym_t *sym)
   1462 {
   1463 	scl_t sc = NOSCL;
   1464 
   1465 	if (sym->s_scl == NOSCL)
   1466 		dcs->d_redeclared_symbol = NULL;
   1467 	else if (sym->s_defarg) {
   1468 		sym->s_defarg = false;
   1469 		dcs->d_redeclared_symbol = NULL;
   1470 	} else {
   1471 		dcs->d_redeclared_symbol = sym;
   1472 		sym = pushdown(sym);
   1473 	}
   1474 
   1475 	switch (dcs->d_kind) {
   1476 	case DLK_STRUCT:
   1477 	case DLK_UNION:
   1478 		sym->u.s_member.sm_containing_type = dcs->d_tag_type->t_sou;
   1479 		sym->s_def = DEF;
   1480 		sc = dcs->d_kind == DLK_STRUCT ? STRUCT_MEMBER : UNION_MEMBER;
   1481 		break;
   1482 	case DLK_EXTERN:
   1483 		/*
   1484 		 * static and external symbols without "extern" are considered
   1485 		 * to be tentatively defined, external symbols with "extern"
   1486 		 * are declared, and typedef names are defined. Tentatively
   1487 		 * defined and declared symbols may become defined if an
   1488 		 * initializer is present or this is a function definition.
   1489 		 */
   1490 		if ((sc = dcs->d_scl) == NOSCL) {
   1491 			sc = EXTERN;
   1492 			sym->s_def = TDEF;
   1493 		} else if (sc == STATIC)
   1494 			sym->s_def = TDEF;
   1495 		else if (sc == TYPEDEF)
   1496 			sym->s_def = DEF;
   1497 		else {
   1498 			lint_assert(sc == EXTERN);
   1499 			sym->s_def = DECL;
   1500 		}
   1501 		break;
   1502 	case DLK_PROTO_PARAMS:
   1503 		sym->s_arg = true;
   1504 		/* FALLTHROUGH */
   1505 	case DLK_OLD_STYLE_ARGS:
   1506 		if ((sc = dcs->d_scl) == NOSCL)
   1507 			sc = AUTO;
   1508 		else {
   1509 			lint_assert(sc == REG);
   1510 			sym->s_register = true;
   1511 			sc = AUTO;
   1512 		}
   1513 		sym->s_def = DEF;
   1514 		break;
   1515 	case DLK_AUTO:
   1516 		if ((sc = dcs->d_scl) == NOSCL) {
   1517 			/*
   1518 			 * XXX somewhat ugly because we don't know whether this
   1519 			 * is AUTO or EXTERN (functions). If we are wrong, it
   1520 			 * must be corrected in declare_local, when the
   1521 			 * necessary type information is available.
   1522 			 */
   1523 			sc = AUTO;
   1524 			sym->s_def = DEF;
   1525 		} else if (sc == AUTO || sc == STATIC || sc == TYPEDEF)
   1526 			sym->s_def = DEF;
   1527 		else if (sc == REG) {
   1528 			sym->s_register = true;
   1529 			sc = AUTO;
   1530 			sym->s_def = DEF;
   1531 		} else {
   1532 			lint_assert(sc == EXTERN);
   1533 			sym->s_def = DECL;
   1534 		}
   1535 		break;
   1536 	default:
   1537 		lint_assert(dcs->d_kind == DLK_ABSTRACT);
   1538 		/* try to continue after syntax errors */
   1539 		sc = NOSCL;
   1540 	}
   1541 	sym->s_scl = sc;
   1542 
   1543 	sym->s_type = dcs->d_type;
   1544 
   1545 	dcs->d_func_proto_syms = NULL;
   1546 
   1547 	return sym;
   1548 }
   1549 
   1550 sym_t *
   1551 old_style_function_parameter_name(sym_t *sym)
   1552 {
   1553 
   1554 	if (sym->s_scl != NOSCL) {
   1555 		if (block_level == sym->s_block_level) {
   1556 			/* redeclaration of formal parameter '%s' */
   1557 			error(21, sym->s_name);
   1558 			lint_assert(sym->s_defarg);
   1559 		}
   1560 		sym = pushdown(sym);
   1561 	}
   1562 	sym->s_type = gettyp(INT);
   1563 	sym->s_scl = AUTO;
   1564 	sym->s_def = DEF;
   1565 	sym->s_defarg = sym->s_arg = true;
   1566 	return sym;
   1567 }
   1568 
   1569 /*-
   1570  * tag		the symbol table entry of the tag
   1571  * kind		the kind of the tag (STRUCT/UNION/ENUM)
   1572  * decl		whether the tag type will be completed in this declaration
   1573  *		(when the following token is T_LBRACE)
   1574  * semi		whether the following token is T_SEMI
   1575  */
   1576 type_t *
   1577 make_tag_type(sym_t *tag, tspec_t kind, bool decl, bool semi)
   1578 {
   1579 	scl_t scl;
   1580 	type_t *tp;
   1581 
   1582 	if (kind == STRUCT)
   1583 		scl = STRUCT_TAG;
   1584 	else if (kind == UNION)
   1585 		scl = UNION_TAG;
   1586 	else {
   1587 		lint_assert(kind == ENUM);
   1588 		scl = ENUM_TAG;
   1589 	}
   1590 
   1591 	if (tag != NULL) {
   1592 		if (tag->s_scl != NOSCL)
   1593 			tag = new_tag(tag, scl, decl, semi);
   1594 		else {
   1595 			/* a new tag, no empty declaration */
   1596 			dcs->d_enclosing->d_nonempty_decl = true;
   1597 			if (scl == ENUM_TAG && !decl) {
   1598 				/* TODO: Make this an error in C99 mode as well. */
   1599 				if (allow_c90 &&
   1600 				    ((!allow_trad && !allow_c99) || pflag))
   1601 					/* forward reference to enum type */
   1602 					warning(42);
   1603 			}
   1604 		}
   1605 		if (tag->s_scl == NOSCL) {
   1606 			tag->s_scl = scl;
   1607 			tag->s_type = tp = block_zero_alloc(sizeof(*tp));
   1608 			tp->t_packed = dcs->d_packed;
   1609 		} else
   1610 			tp = tag->s_type;
   1611 
   1612 	} else {
   1613 		tag = block_zero_alloc(sizeof(*tag));
   1614 		tag->s_name = unnamed;
   1615 		tag->s_def_pos = unique_curr_pos();
   1616 		tag->s_kind = FTAG;
   1617 		tag->s_scl = scl;
   1618 		tag->s_block_level = -1;
   1619 		tag->s_type = tp = block_zero_alloc(sizeof(*tp));
   1620 		tp->t_packed = dcs->d_packed;
   1621 		dcs->d_enclosing->d_nonempty_decl = true;
   1622 	}
   1623 
   1624 	if (tp->t_tspec == NO_TSPEC) {
   1625 		tp->t_tspec = kind;
   1626 		if (kind != ENUM) {
   1627 			tp->t_sou = block_zero_alloc(sizeof(*tp->t_sou));
   1628 			tp->t_sou->sou_align_in_bits = CHAR_SIZE;
   1629 			tp->t_sou->sou_tag = tag;
   1630 			tp->t_sou->sou_incomplete = true;
   1631 		} else {
   1632 			tp->t_is_enum = true;
   1633 			tp->t_enum = block_zero_alloc(sizeof(*tp->t_enum));
   1634 			tp->t_enum->en_tag = tag;
   1635 			tp->t_enum->en_incomplete = true;
   1636 		}
   1637 	}
   1638 	return tp;
   1639 }
   1640 
   1641 /*-
   1642  * Checks all possible cases of tag redeclarations.
   1643  *
   1644  * decl		whether T_LBRACE follows
   1645  * semi		whether T_SEMI follows
   1646  */
   1647 static sym_t *
   1648 new_tag(sym_t *tag, scl_t scl, bool decl, bool semi)
   1649 {
   1650 
   1651 	if (tag->s_block_level < block_level) {
   1652 		if (semi) {
   1653 			/* "struct a;" */
   1654 			if (allow_c90) {
   1655 				/* XXX: Why is this warning suppressed in C90 mode? */
   1656 				if (allow_trad || allow_c99)
   1657 					/* declaration of '%s %s' intro... */
   1658 					warning(44, storage_class_name(scl),
   1659 					    tag->s_name);
   1660 				tag = pushdown(tag);
   1661 			} else if (tag->s_scl != scl) {
   1662 				/* base type is really '%s %s' */
   1663 				warning(45, storage_class_name(tag->s_scl),
   1664 				    tag->s_name);
   1665 			}
   1666 			dcs->d_enclosing->d_nonempty_decl = true;
   1667 		} else if (decl) {
   1668 			/* "struct a { ... } " */
   1669 			if (hflag)
   1670 				/* redefinition of '%s' hides earlier one */
   1671 				warning(43, tag->s_name);
   1672 			tag = pushdown(tag);
   1673 			dcs->d_enclosing->d_nonempty_decl = true;
   1674 		} else if (tag->s_scl != scl) {
   1675 			/* base type is really '%s %s' */
   1676 			warning(45, storage_class_name(tag->s_scl),
   1677 			    tag->s_name);
   1678 			/* XXX: Why is this warning suppressed in C90 mode? */
   1679 			if (allow_trad || allow_c99) {
   1680 				/* declaration of '%s %s' introduces ... */
   1681 				warning(44, storage_class_name(scl),
   1682 				    tag->s_name);
   1683 			}
   1684 			tag = pushdown(tag);
   1685 			dcs->d_enclosing->d_nonempty_decl = true;
   1686 		}
   1687 	} else {
   1688 		if (tag->s_scl != scl ||
   1689 		    (decl && !is_incomplete(tag->s_type))) {
   1690 			/* %s tag '%s' redeclared as %s */
   1691 			error(46, storage_class_name(tag->s_scl),
   1692 			    tag->s_name, storage_class_name(scl));
   1693 			print_previous_declaration(tag);
   1694 			tag = pushdown(tag);
   1695 			dcs->d_enclosing->d_nonempty_decl = true;
   1696 		} else if (semi || decl)
   1697 			dcs->d_enclosing->d_nonempty_decl = true;
   1698 	}
   1699 	return tag;
   1700 }
   1701 
   1702 const char *
   1703 storage_class_name(scl_t sc)
   1704 {
   1705 	switch (sc) {
   1706 	case EXTERN:	return "extern";
   1707 	case STATIC:	return "static";
   1708 	case AUTO:	return "auto";
   1709 	case REG:	return "register";
   1710 	case TYPEDEF:	return "typedef";
   1711 	case STRUCT_TAG:return "struct";
   1712 	case UNION_TAG:	return "union";
   1713 	case ENUM_TAG:	return "enum";
   1714 	default:	lint_assert(/*CONSTCOND*/false);
   1715 	}
   1716 	/* NOTREACHED */
   1717 }
   1718 
   1719 static bool
   1720 has_named_member(const type_t *tp)
   1721 {
   1722 	for (const sym_t *mem = tp->t_sou->sou_first_member;
   1723 	     mem != NULL; mem = mem->s_next) {
   1724 		if (mem->s_name != unnamed)
   1725 			return true;
   1726 		if (is_struct_or_union(mem->s_type->t_tspec)
   1727 		    && has_named_member(mem->s_type))
   1728 			return true;
   1729 	}
   1730 	return false;
   1731 }
   1732 
   1733 type_t *
   1734 complete_struct_or_union(sym_t *first_member)
   1735 {
   1736 
   1737 	type_t *tp = dcs->d_tag_type;
   1738 	if (tp == NULL)		/* in case of syntax errors */
   1739 		return gettyp(INT);
   1740 
   1741 	dcs_align(dcs->d_sou_align_in_bits, 0);
   1742 
   1743 	struct_or_union *sou = tp->t_sou;
   1744 	sou->sou_align_in_bits = dcs->d_sou_align_in_bits;
   1745 	sou->sou_incomplete = false;
   1746 	sou->sou_first_member = first_member;
   1747 	if (tp->t_packed)
   1748 		pack_struct_or_union(tp);
   1749 	else
   1750 		sou->sou_size_in_bits = dcs->d_sou_size_in_bits;
   1751 
   1752 	if (sou->sou_size_in_bits == 0) {
   1753 		/* zero sized %s is a C99 feature */
   1754 		c99ism(47, tspec_name(tp->t_tspec));
   1755 	} else if (!has_named_member(tp)) {
   1756 		/* '%s' has no named members */
   1757 		warning(65, type_name(tp));
   1758 	}
   1759 	return tp;
   1760 }
   1761 
   1762 type_t *
   1763 complete_enum(sym_t *first_enumerator)
   1764 {
   1765 
   1766 	type_t *tp = dcs->d_tag_type;
   1767 	tp->t_enum->en_incomplete = false;
   1768 	tp->t_enum->en_first_enumerator = first_enumerator;
   1769 	return tp;
   1770 }
   1771 
   1772 /*
   1773  * Processes the name of an enumerator in an enum declaration.
   1774  *
   1775  * sym points to the enumerator
   1776  * val is the value of the enumerator
   1777  * impl is true if the value of the enumerator was not explicitly specified.
   1778  */
   1779 sym_t *
   1780 enumeration_constant(sym_t *sym, int val, bool impl)
   1781 {
   1782 
   1783 	if (sym->s_scl != NOSCL) {
   1784 		if (sym->s_block_level == block_level) {
   1785 			/* no hflag, because this is illegal */
   1786 			if (sym->s_arg) {
   1787 				/* enumeration constant '%s' hides parameter */
   1788 				warning(57, sym->s_name);
   1789 			} else {
   1790 				/* redeclaration of '%s' */
   1791 				error(27, sym->s_name);
   1792 				/*
   1793 				 * Inside blocks, it should not be too
   1794 				 * complicated to find the position of the
   1795 				 * previous declaration
   1796 				 */
   1797 				if (block_level == 0)
   1798 					print_previous_declaration(sym);
   1799 			}
   1800 		} else {
   1801 			if (hflag)
   1802 				/* redefinition of '%s' hides earlier one */
   1803 				warning(43, sym->s_name);
   1804 		}
   1805 		sym = pushdown(sym);
   1806 	}
   1807 
   1808 	sym->s_scl = ENUM_CONST;
   1809 	sym->s_type = dcs->d_tag_type;
   1810 	sym->u.s_enum_constant = val;
   1811 
   1812 	if (impl && val == TARG_INT_MIN) {
   1813 		/* enumeration value '%s' overflows */
   1814 		warning(48, sym->s_name);
   1815 	}
   1816 
   1817 	enumval = val == TARG_INT_MAX ? TARG_INT_MIN : val + 1;
   1818 	return sym;
   1819 }
   1820 
   1821 static bool
   1822 ends_with(const char *s, const char *suffix)
   1823 {
   1824 	size_t s_len = strlen(s);
   1825 	size_t suffix_len = strlen(suffix);
   1826 	return s_len >= suffix_len &&
   1827 	       memcmp(s + s_len - suffix_len, suffix, suffix_len) == 0;
   1828 }
   1829 
   1830 void
   1831 check_extern_declaration(const sym_t *sym)
   1832 {
   1833 
   1834 	if (sym->s_scl == EXTERN &&
   1835 	    dcs->d_redeclared_symbol == NULL &&
   1836 	    ends_with(curr_pos.p_file, ".c") &&
   1837 	    allow_c90 &&
   1838 	    !ch_isdigit(sym->s_name[0]) &&	/* see mktempsym */
   1839 	    strcmp(sym->s_name, "main") != 0) {
   1840 		/* missing%s header declaration for '%s' */
   1841 		warning(351, sym->s_type->t_tspec == FUNC ? "" : " 'extern'",
   1842 		    sym->s_name);
   1843 	}
   1844 	if (any_query_enabled &&
   1845 	    sym->s_type->t_tspec == FUNC &&
   1846 	    sym->s_scl == EXTERN &&
   1847 	    sym->s_def == DECL &&
   1848 	    !in_system_header) {
   1849 		/* redundant 'extern' in function declaration of '%s' */
   1850 		query_message(13, sym->s_name);
   1851 	}
   1852 }
   1853 
   1854 /* Process a single external or 'static' declarator. */
   1855 static void
   1856 declare_extern(sym_t *dsym, bool has_initializer, sbuf_t *renaming)
   1857 {
   1858 
   1859 	if (renaming != NULL) {
   1860 		lint_assert(dsym->s_rename == NULL);
   1861 
   1862 		char *s = level_zero_alloc(1, renaming->sb_len + 1);
   1863 		(void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
   1864 		dsym->s_rename = s;
   1865 	}
   1866 
   1867 	check_extern_declaration(dsym);
   1868 
   1869 	check_function_definition(dsym, true);
   1870 
   1871 	check_type(dsym);
   1872 
   1873 	if (has_initializer && !check_init(dsym))
   1874 		dsym->s_def = DEF;
   1875 
   1876 	/*
   1877 	 * Declarations of functions are marked as "tentative" in
   1878 	 * declarator_name(). This is wrong because there are no
   1879 	 * tentative function definitions.
   1880 	 */
   1881 	if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
   1882 		dsym->s_def = DECL;
   1883 
   1884 	if (dcs->d_inline) {
   1885 		if (dsym->s_type->t_tspec == FUNC) {
   1886 			dsym->s_inline = true;
   1887 		} else {
   1888 			/* variable '%s' declared inline */
   1889 			warning(268, dsym->s_name);
   1890 		}
   1891 	}
   1892 
   1893 	/* Write the declaration into the output file */
   1894 	if (plibflg && llibflg &&
   1895 	    dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
   1896 		/*
   1897 		 * With both LINTLIBRARY and PROTOLIB the prototype is
   1898 		 * written as a function definition to the output file.
   1899 		 */
   1900 		bool rval = dsym->s_type->t_subt->t_tspec != VOID;
   1901 		outfdef(dsym, &dsym->s_def_pos, rval, false, NULL);
   1902 	} else if (!is_compiler_builtin(dsym->s_name)
   1903 	    && !(has_initializer && dsym->s_type->t_incomplete_array)) {
   1904 		outsym(dsym, dsym->s_scl, dsym->s_def);
   1905 	}
   1906 
   1907 	sym_t *rdsym = dcs->d_redeclared_symbol;
   1908 	if (rdsym != NULL) {
   1909 
   1910 		/*
   1911 		 * If the old symbol stems from an old-style function
   1912 		 * definition, we have remembered the params in
   1913 		 * rdsym->s_old_style_args and compare them with the params
   1914 		 * of the prototype.
   1915 		 */
   1916 		bool redec = rdsym->s_osdef && dsym->s_type->t_proto &&
   1917 		    check_old_style_definition(rdsym, dsym);
   1918 
   1919 		bool dowarn = false;
   1920 		if (!redec && !check_redeclaration(dsym, &dowarn)) {
   1921 			if (dowarn) {
   1922 				/* TODO: Make this an error in C99 mode as well. */
   1923 				if (!allow_trad && !allow_c99)
   1924 					/* redeclaration of '%s' */
   1925 					error(27, dsym->s_name);
   1926 				else
   1927 					/* redeclaration of '%s' */
   1928 					warning(27, dsym->s_name);
   1929 				print_previous_declaration(rdsym);
   1930 			}
   1931 
   1932 			/*
   1933 			 * Take over the remembered params if the new symbol
   1934 			 * is not a prototype.
   1935 			 */
   1936 			if (rdsym->s_osdef && !dsym->s_type->t_proto) {
   1937 				dsym->s_osdef = rdsym->s_osdef;
   1938 				dsym->u.s_old_style_args =
   1939 				    rdsym->u.s_old_style_args;
   1940 				dsym->s_def_pos = rdsym->s_def_pos;
   1941 			}
   1942 
   1943 			if (rdsym->s_type->t_proto && !dsym->s_type->t_proto)
   1944 				dsym->s_def_pos = rdsym->s_def_pos;
   1945 			else if (rdsym->s_def == DEF && dsym->s_def != DEF)
   1946 				dsym->s_def_pos = rdsym->s_def_pos;
   1947 
   1948 			copy_usage_info(dsym, rdsym);
   1949 
   1950 			/* Once a name is defined, it remains defined. */
   1951 			if (rdsym->s_def == DEF)
   1952 				dsym->s_def = DEF;
   1953 
   1954 			/* once a function is inline, it remains inline */
   1955 			if (rdsym->s_inline)
   1956 				dsym->s_inline = true;
   1957 
   1958 			complete_type(dsym, rdsym);
   1959 		}
   1960 
   1961 		rmsym(rdsym);
   1962 	}
   1963 
   1964 	if (dsym->s_scl == TYPEDEF) {
   1965 		dsym->s_type = block_dup_type(dsym->s_type);
   1966 		dsym->s_type->t_typedef = true;
   1967 		set_first_typedef(dsym->s_type, dsym);
   1968 	}
   1969 }
   1970 
   1971 void
   1972 declare(sym_t *decl, bool has_initializer, sbuf_t *renaming)
   1973 {
   1974 
   1975 	if (dcs->d_kind == DLK_EXTERN)
   1976 		declare_extern(decl, has_initializer, renaming);
   1977 	else if (dcs->d_kind == DLK_OLD_STYLE_ARGS ||
   1978 		 dcs->d_kind == DLK_PROTO_PARAMS) {
   1979 		if (renaming != NULL) {
   1980 			/* symbol renaming can't be used on function arguments */
   1981 			error(310);
   1982 		} else
   1983 			(void)declare_argument(decl, has_initializer);
   1984 	} else {
   1985 		lint_assert(dcs->d_kind == DLK_AUTO);
   1986 		if (renaming != NULL) {
   1987 			/* symbol renaming can't be used on automatic variables */
   1988 			error(311);
   1989 		} else
   1990 			declare_local(decl, has_initializer);
   1991 	}
   1992 }
   1993 
   1994 /*
   1995  * Copies information about usage into a new symbol table entry of
   1996  * the same symbol.
   1997  */
   1998 void
   1999 copy_usage_info(sym_t *sym, sym_t *rdsym)
   2000 {
   2001 
   2002 	sym->s_set_pos = rdsym->s_set_pos;
   2003 	sym->s_use_pos = rdsym->s_use_pos;
   2004 	sym->s_set = rdsym->s_set;
   2005 	sym->s_used = rdsym->s_used;
   2006 }
   2007 
   2008 /*
   2009  * Prints an error and returns true if a symbol is redeclared/redefined.
   2010  * Otherwise, returns false and, in some cases of minor problems, prints
   2011  * a warning.
   2012  */
   2013 bool
   2014 check_redeclaration(sym_t *dsym, bool *dowarn)
   2015 {
   2016 
   2017 	sym_t *rdsym = dcs->d_redeclared_symbol;
   2018 	if (rdsym->s_scl == ENUM_CONST) {
   2019 		/* redeclaration of '%s' */
   2020 		error(27, dsym->s_name);
   2021 		print_previous_declaration(rdsym);
   2022 		return true;
   2023 	}
   2024 	if (rdsym->s_scl == TYPEDEF) {
   2025 		/* typedef '%s' redeclared */
   2026 		error(89, dsym->s_name);
   2027 		print_previous_declaration(rdsym);
   2028 		return true;
   2029 	}
   2030 	if (dsym->s_scl == TYPEDEF) {
   2031 		/* redeclaration of '%s' */
   2032 		error(27, dsym->s_name);
   2033 		print_previous_declaration(rdsym);
   2034 		return true;
   2035 	}
   2036 	if (rdsym->s_def == DEF && dsym->s_def == DEF) {
   2037 		/* redefinition of '%s' */
   2038 		error(28, dsym->s_name);
   2039 		print_previous_declaration(rdsym);
   2040 		return true;
   2041 	}
   2042 	if (!types_compatible(rdsym->s_type, dsym->s_type,
   2043 	    false, false, dowarn)) {
   2044 		/* redeclaration of '%s' with type '%s', expected '%s' */
   2045 		error(347, dsym->s_name,
   2046 		    type_name(dsym->s_type), type_name(rdsym->s_type));
   2047 		print_previous_declaration(rdsym);
   2048 		return true;
   2049 	}
   2050 	if (rdsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
   2051 		return false;
   2052 	if (rdsym->s_scl == STATIC && dsym->s_scl == STATIC)
   2053 		return false;
   2054 	if (rdsym->s_scl == STATIC && dsym->s_def == DECL)
   2055 		return false;
   2056 	if (rdsym->s_scl == EXTERN && rdsym->s_def == DEF) {
   2057 		/*
   2058 		 * All cases except "int a = 1; static int a;" are caught
   2059 		 * above with or without a warning
   2060 		 */
   2061 		/* redeclaration of '%s' */
   2062 		error(27, dsym->s_name);
   2063 		print_previous_declaration(rdsym);
   2064 		return true;
   2065 	}
   2066 	if (rdsym->s_scl == EXTERN) {
   2067 		/* '%s' was previously declared extern, becomes static */
   2068 		warning(29, dsym->s_name);
   2069 		print_previous_declaration(rdsym);
   2070 		return false;
   2071 	}
   2072 	/*
   2073 	 * Now it's one of:
   2074 	 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
   2075 	 */
   2076 	/* TODO: Make this an error in C99 mode as well. */
   2077 	if (!allow_trad && !allow_c99) {
   2078 		/* redeclaration of '%s'; ANSI C requires static */
   2079 		warning(30, dsym->s_name);
   2080 		print_previous_declaration(rdsym);
   2081 	}
   2082 	dsym->s_scl = STATIC;
   2083 	return false;
   2084 }
   2085 
   2086 static bool
   2087 qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual)
   2088 {
   2089 
   2090 	if (tp1->t_const != tp2->t_const && !ignqual && allow_c90)
   2091 		return false;
   2092 	if (tp1->t_volatile != tp2->t_volatile && !ignqual && allow_c90)
   2093 		return false;
   2094 	return true;
   2095 }
   2096 
   2097 bool
   2098 pointer_types_are_compatible(const type_t *tp1, const type_t *tp2, bool ignqual)
   2099 {
   2100 
   2101 	return tp1->t_tspec == VOID || tp2->t_tspec == VOID ||
   2102 	       qualifiers_correspond(tp1, tp2, ignqual);
   2103 }
   2104 
   2105 /*-
   2106  * ignqual	ignore type qualifiers; used for function parameters
   2107  * promot	promote the left type; used for comparison of parameters of
   2108  *		old-style function definitions with parameters of prototypes.
   2109  * *dowarn	is set to true if an old-style function declaration is not
   2110  *		compatible with a prototype
   2111  */
   2112 bool
   2113 types_compatible(const type_t *tp1, const type_t *tp2,
   2114 		     bool ignqual, bool promot, bool *dowarn)
   2115 {
   2116 
   2117 	while (tp1 != NULL && tp2 != NULL) {
   2118 		tspec_t t = tp1->t_tspec;
   2119 		if (promot) {
   2120 			if (t == FLOAT)
   2121 				t = DOUBLE;
   2122 			else if (t == CHAR || t == SCHAR)
   2123 				t = INT;
   2124 			else if (t == UCHAR)
   2125 				t = allow_c90 ? INT : UINT;
   2126 			else if (t == SHORT)
   2127 				t = INT;
   2128 			else if (t == USHORT) {
   2129 				/* CONSTCOND */
   2130 				t = TARG_INT_MAX < TARG_USHRT_MAX || !allow_c90
   2131 				    ? UINT : INT;
   2132 			}
   2133 		}
   2134 
   2135 		if (t != tp2->t_tspec)
   2136 			return false;
   2137 
   2138 		if (!qualifiers_correspond(tp1, tp2, ignqual))
   2139 			return false;
   2140 
   2141 		if (is_struct_or_union(t))
   2142 			return tp1->t_sou == tp2->t_sou;
   2143 
   2144 		if (t == ENUM && eflag)
   2145 			return tp1->t_enum == tp2->t_enum;
   2146 
   2147 		if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
   2148 			if (tp1->t_dim != 0 && tp2->t_dim != 0)
   2149 				return false;
   2150 		}
   2151 
   2152 		/* don't check prototypes for traditional */
   2153 		if (t == FUNC && allow_c90) {
   2154 			if (tp1->t_proto && tp2->t_proto) {
   2155 				if (!prototypes_compatible(tp1, tp2, dowarn))
   2156 					return false;
   2157 			} else if (tp1->t_proto) {
   2158 				if (!matches_no_arg_function(tp1, dowarn))
   2159 					return false;
   2160 			} else if (tp2->t_proto) {
   2161 				if (!matches_no_arg_function(tp2, dowarn))
   2162 					return false;
   2163 			}
   2164 		}
   2165 
   2166 		tp1 = tp1->t_subt;
   2167 		tp2 = tp2->t_subt;
   2168 		ignqual = promot = false;
   2169 	}
   2170 
   2171 	return tp1 == tp2;
   2172 }
   2173 
   2174 static bool
   2175 prototypes_compatible(const type_t *tp1, const type_t *tp2, bool *dowarn)
   2176 {
   2177 
   2178 	if (tp1->t_vararg != tp2->t_vararg)
   2179 		return false;
   2180 
   2181 	sym_t *a1 = tp1->t_args;
   2182 	sym_t *a2 = tp2->t_args;
   2183 
   2184 	for (; a1 != NULL && a2 != NULL; a1 = a1->s_next, a2 = a2->s_next) {
   2185 		if (!types_compatible(a1->s_type, a2->s_type,
   2186 		    true, false, dowarn))
   2187 			return false;
   2188 	}
   2189 	return a1 == a2;
   2190 }
   2191 
   2192 /*
   2193  * Returns whether all parameters of a prototype are compatible with an
   2194  * old-style function declaration.
   2195  *
   2196  * This is the case if the following conditions are met:
   2197  *	1. the prototype has a fixed number of parameters
   2198  *	2. no parameter is of type float
   2199  *	3. no parameter is converted to another type if integer promotion
   2200  *	   is applied on it
   2201  */
   2202 static bool
   2203 matches_no_arg_function(const type_t *tp, bool *dowarn)
   2204 {
   2205 
   2206 	if (tp->t_vararg && dowarn != NULL)
   2207 		*dowarn = true;
   2208 	for (sym_t *arg = tp->t_args; arg != NULL; arg = arg->s_next) {
   2209 		tspec_t t = arg->s_type->t_tspec;
   2210 		if (t == FLOAT ||
   2211 		    t == CHAR || t == SCHAR || t == UCHAR ||
   2212 		    t == SHORT || t == USHORT) {
   2213 			if (dowarn != NULL)
   2214 				*dowarn = true;
   2215 		}
   2216 	}
   2217 	/* FIXME: Always returning true cannot be correct. */
   2218 	return true;
   2219 }
   2220 
   2221 /*
   2222  * Compares a prototype declaration with the remembered arguments of a previous
   2223  * old-style function definition.
   2224  */
   2225 static bool
   2226 check_old_style_definition(sym_t *rdsym, sym_t *dsym)
   2227 {
   2228 
   2229 	sym_t *args = rdsym->u.s_old_style_args;
   2230 	sym_t *pargs = dsym->s_type->t_args;
   2231 
   2232 	bool msg = false;
   2233 
   2234 	int narg = 0;
   2235 	for (sym_t *arg = args; arg != NULL; arg = arg->s_next)
   2236 		narg++;
   2237 	int nparg = 0;
   2238 	for (sym_t *parg = pargs; parg != NULL; parg = parg->s_next)
   2239 		nparg++;
   2240 	if (narg != nparg) {
   2241 		/* prototype does not match old-style definition */
   2242 		error(63);
   2243 		msg = true;
   2244 		goto end;
   2245 	}
   2246 
   2247 	sym_t *arg = args;
   2248 	sym_t *parg = pargs;
   2249 	int n = 1;
   2250 	while (narg-- > 0) {
   2251 		bool dowarn = false;
   2252 		/*
   2253 		 * If it does not match due to promotion and lint runs in
   2254 		 * "traditional to C90" migration mode, print only a warning.
   2255 		 *
   2256 		 * XXX: Where is this "only a warning"?
   2257 		 */
   2258 		if (!types_compatible(arg->s_type, parg->s_type,
   2259 		    true, true, &dowarn) ||
   2260 		    dowarn) {
   2261 			/* prototype does not match old-style ... */
   2262 			error(299, n);
   2263 			msg = true;
   2264 		}
   2265 		arg = arg->s_next;
   2266 		parg = parg->s_next;
   2267 		n++;
   2268 	}
   2269 
   2270 end:
   2271 	if (msg && rflag) {
   2272 		/* old-style definition */
   2273 		message_at(300, &rdsym->s_def_pos);
   2274 	}
   2275 
   2276 	return msg;
   2277 }
   2278 
   2279 /*
   2280  * Completes a type by copying the dimension and prototype information from a
   2281  * second compatible type.
   2282  *
   2283  * Following lines are legal:
   2284  *  "typedef a[]; a b; a b[10]; a c; a c[20];"
   2285  *  "typedef ft(); ft f; f(int); ft g; g(long);"
   2286  * This means that, if a type is completed, the type structure must be
   2287  * duplicated.
   2288  */
   2289 void
   2290 complete_type(sym_t *dsym, sym_t *ssym)
   2291 {
   2292 	type_t **dstp = &dsym->s_type;
   2293 	type_t *src = ssym->s_type;
   2294 
   2295 	while (*dstp != NULL) {
   2296 		type_t *dst = *dstp;
   2297 		lint_assert(src != NULL);
   2298 		lint_assert(dst->t_tspec == src->t_tspec);
   2299 		if (dst->t_tspec == ARRAY) {
   2300 			if (dst->t_dim == 0 && src->t_dim != 0) {
   2301 				*dstp = dst = block_dup_type(dst);
   2302 				dst->t_dim = src->t_dim;
   2303 				dst->t_incomplete_array = false;
   2304 			}
   2305 		} else if (dst->t_tspec == FUNC) {
   2306 			if (!dst->t_proto && src->t_proto) {
   2307 				*dstp = dst = block_dup_type(dst);
   2308 				dst->t_proto = true;
   2309 				dst->t_args = src->t_args;
   2310 			}
   2311 		}
   2312 		dstp = &dst->t_subt;
   2313 		src = src->t_subt;
   2314 	}
   2315 }
   2316 
   2317 /*
   2318  * Completes the declaration of a single argument.
   2319  */
   2320 sym_t *
   2321 declare_argument(sym_t *sym, bool has_initializer)
   2322 {
   2323 
   2324 	check_function_definition(sym, true);
   2325 
   2326 	check_type(sym);
   2327 
   2328 	if (dcs->d_redeclared_symbol != NULL &&
   2329 	    dcs->d_redeclared_symbol->s_block_level == block_level) {
   2330 		/* redeclaration of formal parameter '%s' */
   2331 		error(237, sym->s_name);
   2332 		rmsym(dcs->d_redeclared_symbol);
   2333 		sym->s_arg = true;
   2334 	}
   2335 
   2336 	if (!sym->s_arg) {
   2337 		/* declared argument '%s' is missing */
   2338 		error(53, sym->s_name);
   2339 		sym->s_arg = true;
   2340 	}
   2341 
   2342 	if (has_initializer) {
   2343 		/* cannot initialize parameter '%s' */
   2344 		error(52, sym->s_name);
   2345 	}
   2346 
   2347 	if (sym->s_type == NULL)	/* for c(void()) */
   2348 		sym->s_type = gettyp(VOID);
   2349 
   2350 	tspec_t t = sym->s_type->t_tspec;
   2351 	if (t == ARRAY)
   2352 		sym->s_type = block_derive_type(sym->s_type->t_subt, PTR);
   2353 	if (t == FUNC) {
   2354 		if (!allow_c90)
   2355 			/* parameter '%s' has function type, should be ... */
   2356 			warning(50, sym->s_name);
   2357 		sym->s_type = block_derive_type(sym->s_type, PTR);
   2358 	}
   2359 	if (t == FLOAT && !allow_c90)
   2360 		sym->s_type = gettyp(DOUBLE);
   2361 
   2362 	if (dcs->d_inline)
   2363 		/* parameter '%s' declared inline */
   2364 		warning(269, sym->s_name);
   2365 
   2366 	/*
   2367 	 * Arguments must have complete types. length_in_bits prints the
   2368 	 * needed error messages (null dimension is impossible because arrays
   2369 	 * are converted to pointers).
   2370 	 */
   2371 	if (sym->s_type->t_tspec != VOID)
   2372 		(void)length_in_bits(sym->s_type, sym->s_name);
   2373 
   2374 	sym->s_used = dcs->d_used;
   2375 	mark_as_set(sym);
   2376 
   2377 	return sym;
   2378 }
   2379 
   2380 static bool
   2381 is_character_pointer(const type_t *tp)
   2382 {
   2383 	tspec_t st;
   2384 
   2385 	return tp->t_tspec == PTR &&
   2386 	       (st = tp->t_subt->t_tspec,
   2387 		   st == CHAR || st == SCHAR || st == UCHAR);
   2388 }
   2389 
   2390 void
   2391 check_func_lint_directives(void)
   2392 {
   2393 
   2394 	/* check for illegal combinations of lint directives */
   2395 	if (printflike_argnum != -1 && scanflike_argnum != -1) {
   2396 		/* ** PRINTFLIKE ** and ** SCANFLIKE ** cannot be combined */
   2397 		warning(289);
   2398 		printflike_argnum = scanflike_argnum = -1;
   2399 	}
   2400 	if (nvararg != -1 &&
   2401 	    (printflike_argnum != -1 || scanflike_argnum != -1)) {
   2402 		/* dubious use of ** VARARGS ** with ** %s ** */
   2403 		warning(288,
   2404 		    printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE");
   2405 		nvararg = -1;
   2406 	}
   2407 
   2408 	/*
   2409 	 * check if the argument of a lint directive is compatible with the
   2410 	 * number of arguments.
   2411 	 */
   2412 	int narg = 0;
   2413 	for (sym_t *arg = dcs->d_func_args; arg != NULL; arg = arg->s_next)
   2414 		narg++;
   2415 	if (nargusg > narg) {
   2416 		/* argument number mismatch in comment ** %s ** */
   2417 		warning(283, "ARGSUSED");
   2418 		nargusg = 0;
   2419 	}
   2420 	if (nvararg > narg) {
   2421 		/* argument number mismatch in comment ** %s ** */
   2422 		warning(283, "VARARGS");
   2423 		nvararg = 0;
   2424 	}
   2425 	if (printflike_argnum > narg) {
   2426 		/* argument number mismatch in comment ** %s ** */
   2427 		warning(283, "PRINTFLIKE");
   2428 		printflike_argnum = -1;
   2429 	} else if (printflike_argnum == 0) {
   2430 		printflike_argnum = -1;
   2431 	}
   2432 	if (scanflike_argnum > narg) {
   2433 		/* argument number mismatch in comment ** %s ** */
   2434 		warning(283, "SCANFLIKE");
   2435 		scanflike_argnum = -1;
   2436 	} else if (scanflike_argnum == 0) {
   2437 		scanflike_argnum = -1;
   2438 	}
   2439 	if (printflike_argnum != -1 || scanflike_argnum != -1) {
   2440 		narg = printflike_argnum != -1
   2441 		    ? printflike_argnum : scanflike_argnum;
   2442 		sym_t *arg = dcs->d_func_args;
   2443 		for (int n = 1; n < narg; n++)
   2444 			arg = arg->s_next;
   2445 		if (!is_character_pointer(arg->s_type)) {
   2446 			/* argument %d must be 'char *' for PRINTFLIKE/... */
   2447 			warning(293, narg);
   2448 			printflike_argnum = scanflike_argnum = -1;
   2449 		}
   2450 	}
   2451 }
   2452 
   2453 /*
   2454  * Warn about arguments in old-style function definitions that default to int.
   2455  * Check that an old-style function definition is compatible to a previous
   2456  * prototype.
   2457  */
   2458 void
   2459 check_func_old_style_arguments(void)
   2460 {
   2461 	int narg;
   2462 	int nparg;
   2463 	bool msg;
   2464 
   2465 	sym_t *args = funcsym->u.s_old_style_args;
   2466 	sym_t *pargs = funcsym->s_type->t_args;
   2467 
   2468 	/*
   2469 	 * print a warning for each argument of an old-style function
   2470 	 * definition which defaults to int
   2471 	 */
   2472 	for (sym_t *arg = args; arg != NULL; arg = arg->s_next) {
   2473 		if (arg->s_defarg) {
   2474 			/* type of argument '%s' defaults to 'int' */
   2475 			warning(32, arg->s_name);
   2476 			arg->s_defarg = false;
   2477 			mark_as_set(arg);
   2478 		}
   2479 	}
   2480 
   2481 	/*
   2482 	 * If this is an old-style function definition and a prototype
   2483 	 * exists, compare the types of arguments.
   2484 	 */
   2485 	if (funcsym->s_osdef && funcsym->s_type->t_proto) {
   2486 		/*
   2487 		 * If the number of arguments does not match, we need not
   2488 		 * continue.
   2489 		 */
   2490 		narg = nparg = 0;
   2491 		msg = false;
   2492 		for (sym_t *parg = pargs; parg != NULL; parg = parg->s_next)
   2493 			nparg++;
   2494 		for (sym_t *arg = args; arg != NULL; arg = arg->s_next)
   2495 			narg++;
   2496 		if (narg != nparg) {
   2497 			/* parameter mismatch: %d declared, %d defined */
   2498 			error(51, nparg, narg);
   2499 			msg = true;
   2500 		} else {
   2501 			sym_t *parg = pargs;
   2502 			sym_t *arg = args;
   2503 			while (narg-- > 0) {
   2504 				msg |= check_prototype_declaration(arg, parg);
   2505 				parg = parg->s_next;
   2506 				arg = arg->s_next;
   2507 			}
   2508 		}
   2509 		if (msg && rflag) {
   2510 			/* prototype declaration */
   2511 			message_at(285, &dcs->d_redeclared_symbol->s_def_pos);
   2512 		}
   2513 
   2514 		/* from now on the prototype is valid */
   2515 		funcsym->s_osdef = false;
   2516 		funcsym->u.s_old_style_args = NULL;
   2517 	}
   2518 }
   2519 
   2520 /*
   2521  * Checks compatibility of an old-style function definition with a previous
   2522  * prototype declaration.
   2523  * Returns true if the position of the previous declaration should be reported.
   2524  */
   2525 static bool
   2526 check_prototype_declaration(sym_t *arg, sym_t *parg)
   2527 {
   2528 	type_t *tp = arg->s_type;
   2529 	type_t *ptp = parg->s_type;
   2530 	bool dowarn = false;
   2531 
   2532 	if (!types_compatible(tp, ptp, true, true, &dowarn)) {
   2533 		if (types_compatible(tp, ptp, true, false, &dowarn)) {
   2534 			/* type of '%s' does not match prototype */
   2535 			return gnuism(58, arg->s_name);
   2536 		} else {
   2537 			/* type of '%s' does not match prototype */
   2538 			error(58, arg->s_name);
   2539 			return true;
   2540 		}
   2541 	}
   2542 	if (dowarn) {
   2543 		/* TODO: Make this an error in C99 mode as well. */
   2544 		if (!allow_trad && !allow_c99)
   2545 			/* type of '%s' does not match prototype */
   2546 			error(58, arg->s_name);
   2547 		else
   2548 			/* type of '%s' does not match prototype */
   2549 			warning(58, arg->s_name);
   2550 		return true;
   2551 	}
   2552 
   2553 	return false;
   2554 }
   2555 
   2556 static void
   2557 check_local_hiding(const sym_t *dsym)
   2558 {
   2559 	switch (dsym->s_scl) {
   2560 	case AUTO:
   2561 		/* automatic '%s' hides external declaration */
   2562 		warning(86, dsym->s_name);
   2563 		break;
   2564 	case STATIC:
   2565 		/* static '%s' hides external declaration */
   2566 		warning(87, dsym->s_name);
   2567 		break;
   2568 	case TYPEDEF:
   2569 		/* typedef '%s' hides external declaration */
   2570 		warning(88, dsym->s_name);
   2571 		break;
   2572 	case EXTERN:
   2573 		/* Already checked in declare_external_in_block. */
   2574 		break;
   2575 	default:
   2576 		lint_assert(/*CONSTCOND*/false);
   2577 	}
   2578 }
   2579 
   2580 static void
   2581 check_local_redeclaration(const sym_t *dsym, sym_t *rdsym)
   2582 {
   2583 	if (rdsym->s_block_level == 0) {
   2584 		if (hflag)
   2585 			check_local_hiding(dsym);
   2586 
   2587 	} else if (rdsym->s_block_level == block_level) {
   2588 
   2589 		/* no hflag, because it's illegal! */
   2590 		if (rdsym->s_arg) {
   2591 			/*
   2592 			 * if allow_c90, a "redeclaration of '%s'" error
   2593 			 * is produced below
   2594 			 */
   2595 			if (!allow_c90) {
   2596 				if (hflag) {
   2597 					/* declaration of '%s' hides ... */
   2598 					warning(91, dsym->s_name);
   2599 				}
   2600 				rmsym(rdsym);
   2601 			}
   2602 		}
   2603 
   2604 	} else if (rdsym->s_block_level < block_level) {
   2605 		if (hflag) {
   2606 			/* declaration of '%s' hides earlier one */
   2607 			warning(95, dsym->s_name);
   2608 		}
   2609 	}
   2610 
   2611 	if (rdsym->s_block_level == block_level) {
   2612 		/* redeclaration of '%s' */
   2613 		error(27, dsym->s_name);
   2614 		rmsym(rdsym);
   2615 	}
   2616 }
   2617 
   2618 /*
   2619  * Completes a single local declaration/definition.
   2620  */
   2621 void
   2622 declare_local(sym_t *dsym, bool has_initializer)
   2623 {
   2624 
   2625 	/* Correct a mistake done in declarator_name(). */
   2626 	if (dsym->s_type->t_tspec == FUNC) {
   2627 		dsym->s_def = DECL;
   2628 		if (dcs->d_scl == NOSCL)
   2629 			dsym->s_scl = EXTERN;
   2630 	}
   2631 
   2632 	if (dsym->s_scl == EXTERN) {
   2633 		/* nested 'extern' declaration of '%s' */
   2634 		warning(352, dsym->s_name);
   2635 	}
   2636 
   2637 	if (dsym->s_type->t_tspec == FUNC) {
   2638 		if (dsym->s_scl == STATIC) {
   2639 			/* dubious static function '%s' at block level */
   2640 			warning(93, dsym->s_name);
   2641 			dsym->s_scl = EXTERN;
   2642 		} else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
   2643 			/* function '%s' has illegal storage class */
   2644 			error(94, dsym->s_name);
   2645 			dsym->s_scl = EXTERN;
   2646 		}
   2647 	}
   2648 
   2649 	/*
   2650 	 * functions may be declared inline at local scope, although
   2651 	 * this has no effect for a later definition of the same
   2652 	 * function.
   2653 	 * XXX it should have an effect if !allow_c90 is set. this would
   2654 	 * also be the way gcc behaves.
   2655 	 */
   2656 	if (dcs->d_inline) {
   2657 		if (dsym->s_type->t_tspec == FUNC)
   2658 			dsym->s_inline = true;
   2659 		else {
   2660 			/* variable '%s' declared inline */
   2661 			warning(268, dsym->s_name);
   2662 		}
   2663 	}
   2664 
   2665 	check_function_definition(dsym, true);
   2666 
   2667 	check_type(dsym);
   2668 
   2669 	if (dcs->d_redeclared_symbol != NULL && dsym->s_scl == EXTERN)
   2670 		declare_external_in_block(dsym);
   2671 
   2672 	if (dsym->s_scl == EXTERN) {
   2673 		/*
   2674 		 * XXX if the static variable at level 0 is only defined
   2675 		 * later, checking will be possible.
   2676 		 */
   2677 		if (dsym->s_ext_sym == NULL)
   2678 			outsym(dsym, EXTERN, dsym->s_def);
   2679 		else
   2680 			outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
   2681 	}
   2682 
   2683 	if (dcs->d_redeclared_symbol != NULL)
   2684 		check_local_redeclaration(dsym, dcs->d_redeclared_symbol);
   2685 
   2686 	if (has_initializer && !check_init(dsym)) {
   2687 		dsym->s_def = DEF;
   2688 		mark_as_set(dsym);
   2689 	}
   2690 
   2691 	if (dsym->s_scl == TYPEDEF) {
   2692 		dsym->s_type = block_dup_type(dsym->s_type);
   2693 		dsym->s_type->t_typedef = true;
   2694 		set_first_typedef(dsym->s_type, dsym);
   2695 	}
   2696 
   2697 	if (dsym->s_scl == STATIC && any_query_enabled) {
   2698 		/* static variable '%s' in function */
   2699 		query_message(11, dsym->s_name);
   2700 	}
   2701 }
   2702 
   2703 /* Processes (re)declarations of external symbols inside blocks. */
   2704 static void
   2705 declare_external_in_block(sym_t *dsym)
   2706 {
   2707 
   2708 	/* look for a symbol with the same name */
   2709 	sym_t *esym = dcs->d_redeclared_symbol;
   2710 	while (esym != NULL && esym->s_block_level != 0) {
   2711 		while ((esym = esym->s_symtab_next) != NULL) {
   2712 			if (esym->s_kind != FVFT)
   2713 				continue;
   2714 			if (strcmp(dsym->s_name, esym->s_name) == 0)
   2715 				break;
   2716 		}
   2717 	}
   2718 	if (esym == NULL)
   2719 		return;
   2720 	if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
   2721 		/* gcc accepts this without a warning, pcc prints an error. */
   2722 		/* redeclaration of '%s' */
   2723 		warning(27, dsym->s_name);
   2724 		print_previous_declaration(esym);
   2725 		return;
   2726 	}
   2727 
   2728 	bool dowarn = false;
   2729 	bool compatible = types_compatible(esym->s_type, dsym->s_type,
   2730 	    false, false, &dowarn);
   2731 
   2732 	if (!compatible || dowarn) {
   2733 		if (esym->s_scl == EXTERN) {
   2734 			/* inconsistent redeclaration of extern '%s' */
   2735 			warning(90, dsym->s_name);
   2736 			print_previous_declaration(esym);
   2737 		} else {
   2738 			/* inconsistent redeclaration of static '%s' */
   2739 			warning(92, dsym->s_name);
   2740 			print_previous_declaration(esym);
   2741 		}
   2742 	}
   2743 
   2744 	if (compatible) {
   2745 		/*
   2746 		 * Remember the external symbol, so we can update usage
   2747 		 * information at the end of the block.
   2748 		 */
   2749 		dsym->s_ext_sym = esym;
   2750 	}
   2751 }
   2752 
   2753 /*
   2754  * Check whether the symbol cannot be initialized due to type/storage class.
   2755  * Return whether an error has been detected.
   2756  */
   2757 static bool
   2758 check_init(sym_t *sym)
   2759 {
   2760 
   2761 	if (sym->s_type->t_tspec == FUNC) {
   2762 		/* cannot initialize function '%s' */
   2763 		error(24, sym->s_name);
   2764 		return true;
   2765 	}
   2766 	if (sym->s_scl == TYPEDEF) {
   2767 		/* cannot initialize typedef '%s' */
   2768 		error(25, sym->s_name);
   2769 		return true;
   2770 	}
   2771 	if (sym->s_scl == EXTERN && sym->s_def == DECL) {
   2772 		if (dcs->d_kind == DLK_EXTERN) {
   2773 			/* cannot initialize extern declaration '%s' */
   2774 			warning(26, sym->s_name);
   2775 		} else {
   2776 			/* cannot initialize extern declaration '%s' */
   2777 			error(26, sym->s_name);
   2778 			return true;
   2779 		}
   2780 	}
   2781 
   2782 	return false;
   2783 }
   2784 
   2785 /* Create a symbol for an abstract declaration. */
   2786 sym_t *
   2787 abstract_name(void)
   2788 {
   2789 
   2790 	lint_assert(dcs->d_kind == DLK_ABSTRACT
   2791 	    || dcs->d_kind == DLK_PROTO_PARAMS);
   2792 
   2793 	sym_t *sym = block_zero_alloc(sizeof(*sym));
   2794 	sym->s_name = unnamed;
   2795 	sym->s_def = DEF;
   2796 	sym->s_scl = ABSTRACT;
   2797 	sym->s_block_level = -1;
   2798 	sym->s_arg = dcs->d_kind == DLK_PROTO_PARAMS;
   2799 
   2800 	/*
   2801 	 * At this point, dcs->d_type contains only the basic type.  That
   2802 	 * type will be updated later, adding pointers, arrays and functions
   2803 	 * as necessary.
   2804 	 */
   2805 	/*
   2806 	 * XXX: This is not the correct type.  For example in msg_347, it is
   2807 	 * the type of the last prototype parameter, but it should rather be
   2808 	 * the return type of the function.
   2809 	 */
   2810 	sym->s_type = dcs->d_type;
   2811 	dcs->d_redeclared_symbol = NULL;
   2812 	dcs->d_vararg = false;
   2813 
   2814 	return sym;
   2815 }
   2816 
   2817 /* Removes anything which has nothing to do on global level. */
   2818 void
   2819 global_clean_up(void)
   2820 {
   2821 
   2822 	while (dcs->d_enclosing != NULL)
   2823 		end_declaration_level();
   2824 
   2825 	clean_up_after_error();
   2826 	block_level = 0;
   2827 	mem_block_level = 0;
   2828 	global_clean_up_decl(true);
   2829 }
   2830 
   2831 sym_t *
   2832 declare_abstract_type(sym_t *sym)
   2833 {
   2834 
   2835 	check_function_definition(sym, true);
   2836 	check_type(sym);
   2837 	return sym;
   2838 }
   2839 
   2840 /* Checks size after declarations of variables and their initialization. */
   2841 void
   2842 check_size(sym_t *dsym)
   2843 {
   2844 
   2845 	if (dsym->s_def == DEF &&
   2846 	    dsym->s_scl != TYPEDEF &&
   2847 	    dsym->s_type->t_tspec != FUNC &&
   2848 	    length_in_bits(dsym->s_type, dsym->s_name) == 0 &&
   2849 	    dsym->s_type->t_tspec == ARRAY &&
   2850 	    dsym->s_type->t_dim == 0) {
   2851 		if (!allow_c90)
   2852 			/* empty array declaration for '%s' */
   2853 			warning(190, dsym->s_name);
   2854 		else
   2855 			/* empty array declaration for '%s' */
   2856 			error(190, dsym->s_name);
   2857 	}
   2858 }
   2859 
   2860 /* Mark an object as set if it is not already. */
   2861 void
   2862 mark_as_set(sym_t *sym)
   2863 {
   2864 
   2865 	if (!sym->s_set) {
   2866 		sym->s_set = true;
   2867 		sym->s_set_pos = unique_curr_pos();
   2868 	}
   2869 }
   2870 
   2871 /* Mark an object as used if it is not already. */
   2872 void
   2873 mark_as_used(sym_t *sym, bool fcall, bool szof)
   2874 {
   2875 
   2876 	if (!sym->s_used) {
   2877 		sym->s_used = true;
   2878 		sym->s_use_pos = unique_curr_pos();
   2879 	}
   2880 	/*
   2881 	 * For function calls, another record is written.
   2882 	 *
   2883 	 * XXX: Should symbols used in sizeof() be treated as used or not?
   2884 	 * Probably not, because there is no point in declaring an external
   2885 	 * variable only to get its size.
   2886 	 */
   2887 	if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
   2888 		outusg(sym);
   2889 }
   2890 
   2891 /* Warns about variables and labels that are not used or only set. */
   2892 void
   2893 check_usage(decl_level *dl)
   2894 {
   2895 	/* for this warning LINTED has no effect */
   2896 	int saved_lwarn = lwarn;
   2897 	lwarn = LWARN_ALL;
   2898 
   2899 	debug_step("begin lwarn %d", lwarn);
   2900 	for (sym_t *sym = dl->d_first_dlsym;
   2901 	     sym != NULL; sym = sym->s_level_next)
   2902 		check_usage_sym(dl->d_asm, sym);
   2903 	lwarn = saved_lwarn;
   2904 	debug_step("end lwarn %d", lwarn);
   2905 }
   2906 
   2907 /* Warns about a variable or a label that is not used or only set. */
   2908 void
   2909 check_usage_sym(bool novar, sym_t *sym)
   2910 {
   2911 
   2912 	if (sym->s_block_level == -1)
   2913 		return;
   2914 
   2915 	if (sym->s_kind == FVFT && sym->s_arg)
   2916 		check_argument_usage(novar, sym);
   2917 	else if (sym->s_kind == FVFT)
   2918 		check_variable_usage(novar, sym);
   2919 	else if (sym->s_kind == FLABEL)
   2920 		check_label_usage(sym);
   2921 	else if (sym->s_kind == FTAG)
   2922 		check_tag_usage(sym);
   2923 }
   2924 
   2925 static void
   2926 check_argument_usage(bool novar, sym_t *arg)
   2927 {
   2928 
   2929 	lint_assert(arg->s_set);
   2930 
   2931 	if (novar)
   2932 		return;
   2933 
   2934 	if (!arg->s_used && !vflag) {
   2935 		/* parameter '%s' unused in function '%s' */
   2936 		warning_at(231, &arg->s_def_pos, arg->s_name, funcsym->s_name);
   2937 	}
   2938 }
   2939 
   2940 static void
   2941 check_variable_usage(bool novar, sym_t *sym)
   2942 {
   2943 
   2944 	lint_assert(block_level != 0);
   2945 
   2946 	/* example at file scope: int c = ({ return 3; }); */
   2947 	if (sym->s_block_level == 0 && ch_isdigit(sym->s_name[0]))
   2948 		return;
   2949 
   2950 	/* errors in expressions easily cause lots of these warnings */
   2951 	if (seen_error)
   2952 		return;
   2953 
   2954 	/*
   2955 	 * XXX Only variables are checked, although types should
   2956 	 * probably also be checked
   2957 	 */
   2958 	scl_t sc = sym->s_scl;
   2959 	if (sc != EXTERN && sc != STATIC && sc != AUTO && sc != REG)
   2960 		return;
   2961 
   2962 	if (novar)
   2963 		return;
   2964 
   2965 	if (sc == EXTERN) {
   2966 		if (!sym->s_used && !sym->s_set) {
   2967 			/* '%s' unused in function '%s' */
   2968 			warning_at(192, &sym->s_def_pos,
   2969 			    sym->s_name, funcsym->s_name);
   2970 		}
   2971 	} else {
   2972 		if (sym->s_set && !sym->s_used) {
   2973 			/* '%s' set but not used in function '%s' */
   2974 			warning_at(191, &sym->s_set_pos,
   2975 			    sym->s_name, funcsym->s_name);
   2976 		} else if (!sym->s_used) {
   2977 			/* '%s' unused in function '%s' */
   2978 			warning_at(192, &sym->s_def_pos,
   2979 			    sym->s_name, funcsym->s_name);
   2980 		}
   2981 	}
   2982 
   2983 	if (sc == EXTERN) {
   2984 		/*
   2985 		 * information about usage is taken over into the symbol
   2986 		 * table entry at level 0 if the symbol was locally declared
   2987 		 * as an external symbol.
   2988 		 *
   2989 		 * XXX This is wrong for symbols declared static at level 0
   2990 		 * if the usage information stems from sizeof(). This is
   2991 		 * because symbols at level 0 only used in sizeof() are
   2992 		 * considered to not be used.
   2993 		 */
   2994 		sym_t *xsym = sym->s_ext_sym;
   2995 		if (xsym != NULL) {
   2996 			if (sym->s_used && !xsym->s_used) {
   2997 				xsym->s_used = true;
   2998 				xsym->s_use_pos = sym->s_use_pos;
   2999 			}
   3000 			if (sym->s_set && !xsym->s_set) {
   3001 				xsym->s_set = true;
   3002 				xsym->s_set_pos = sym->s_set_pos;
   3003 			}
   3004 		}
   3005 	}
   3006 }
   3007 
   3008 static void
   3009 check_label_usage(sym_t *lab)
   3010 {
   3011 
   3012 	lint_assert(block_level == 1);
   3013 	lint_assert(lab->s_block_level == 1);
   3014 
   3015 	if (funcsym == NULL)
   3016 		/* syntax error '%s' */
   3017 		error(249, "labels are only valid inside a function");
   3018 	else if (lab->s_set && !lab->s_used)
   3019 		/* label '%s' unused in function '%s' */
   3020 		warning_at(232, &lab->s_set_pos, lab->s_name, funcsym->s_name);
   3021 	else if (!lab->s_set)
   3022 		/* undefined label '%s' */
   3023 		warning_at(23, &lab->s_use_pos, lab->s_name);
   3024 }
   3025 
   3026 static void
   3027 check_tag_usage(sym_t *sym)
   3028 {
   3029 
   3030 	if (!is_incomplete(sym->s_type))
   3031 		return;
   3032 
   3033 	/* always complain about incomplete tags declared inside blocks */
   3034 	if (zflag || dcs->d_kind != DLK_EXTERN)
   3035 		return;
   3036 
   3037 	switch (sym->s_type->t_tspec) {
   3038 	case STRUCT:
   3039 		/* struct '%s' never defined */
   3040 		warning_at(233, &sym->s_def_pos, sym->s_name);
   3041 		break;
   3042 	case UNION:
   3043 		/* union '%s' never defined */
   3044 		warning_at(234, &sym->s_def_pos, sym->s_name);
   3045 		break;
   3046 	default:
   3047 		lint_assert(sym->s_type->t_tspec == ENUM);
   3048 		/* enum '%s' never defined */
   3049 		warning_at(235, &sym->s_def_pos, sym->s_name);
   3050 		break;
   3051 	}
   3052 }
   3053 
   3054 /*
   3055  * Called after the entire translation unit has been parsed.
   3056  * Changes tentative definitions into definitions.
   3057  * Performs some tests on global symbols. Detected problems are:
   3058  * - defined variables of incomplete type
   3059  * - constant variables which are not initialized
   3060  * - static symbols which are never used
   3061  */
   3062 void
   3063 check_global_symbols(void)
   3064 {
   3065 	sym_t *sym;
   3066 
   3067 	if (block_level != 0 || dcs->d_enclosing != NULL)
   3068 		norecover();
   3069 
   3070 	for (sym = dcs->d_first_dlsym; sym != NULL; sym = sym->s_level_next) {
   3071 		if (sym->s_block_level == -1)
   3072 			continue;
   3073 		if (sym->s_kind == FVFT)
   3074 			check_global_variable(sym);
   3075 		else if (sym->s_kind == FTAG)
   3076 			check_tag_usage(sym);
   3077 		else
   3078 			lint_assert(sym->s_kind == FMEMBER);
   3079 	}
   3080 }
   3081 
   3082 static void
   3083 check_unused_static_global_variable(const sym_t *sym)
   3084 {
   3085 	if (sym->s_type->t_tspec == FUNC) {
   3086 		if (sym->s_def == DEF) {
   3087 			if (!sym->s_inline)
   3088 				/* static function '%s' unused */
   3089 				warning_at(236, &sym->s_def_pos, sym->s_name);
   3090 		} else {
   3091 			/* static function '%s' declared but not defined */
   3092 			warning_at(290, &sym->s_def_pos, sym->s_name);
   3093 		}
   3094 	} else if (!sym->s_set) {
   3095 		/* static variable '%s' unused */
   3096 		warning_at(226, &sym->s_def_pos, sym->s_name);
   3097 	} else {
   3098 		/* static variable '%s' set but not used */
   3099 		warning_at(307, &sym->s_def_pos, sym->s_name);
   3100 	}
   3101 }
   3102 
   3103 static void
   3104 check_static_global_variable(const sym_t *sym)
   3105 {
   3106 	if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) {
   3107 		/* static function '%s' called but not defined */
   3108 		error_at(225, &sym->s_use_pos, sym->s_name);
   3109 	}
   3110 
   3111 	if (!sym->s_used)
   3112 		check_unused_static_global_variable(sym);
   3113 
   3114 	if (allow_c90 && sym->s_def == TDEF && sym->s_type->t_const) {
   3115 		/* const object '%s' should have initializer */
   3116 		warning_at(227, &sym->s_def_pos, sym->s_name);
   3117 	}
   3118 }
   3119 
   3120 static void
   3121 check_global_variable(const sym_t *sym)
   3122 {
   3123 	scl_t scl = sym->s_scl;
   3124 
   3125 	if (scl == TYPEDEF || scl == BOOL_CONST || scl == ENUM_CONST)
   3126 		return;
   3127 
   3128 	if (scl == NOSCL)
   3129 		return;		/* May be caused by a syntax error. */
   3130 
   3131 	lint_assert(scl == EXTERN || scl == STATIC);
   3132 
   3133 	check_global_variable_size(sym);
   3134 
   3135 	if (scl == STATIC)
   3136 		check_static_global_variable(sym);
   3137 }
   3138 
   3139 static void
   3140 check_global_variable_size(const sym_t *sym)
   3141 {
   3142 
   3143 	if (sym->s_def != TDEF)
   3144 		return;
   3145 	if (sym->s_type->t_tspec == FUNC) {
   3146 		/* Maybe a syntax error after a function declaration. */
   3147 		return;
   3148 	}
   3149 	if (sym->s_def == TDEF && sym->s_type->t_tspec == VOID) {
   3150 		/* Prevent an internal error in length_in_bits below. */
   3151 		return;
   3152 	}
   3153 
   3154 	pos_t cpos = curr_pos;
   3155 	curr_pos = sym->s_def_pos;
   3156 	int len_in_bits = length_in_bits(sym->s_type, sym->s_name);
   3157 	curr_pos = cpos;
   3158 
   3159 	if (len_in_bits == 0 &&
   3160 	    sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
   3161 		/* TODO: C99 6.7.5.2p1 defines this as an error as well. */
   3162 		if (!allow_c90 ||
   3163 		    (sym->s_scl == EXTERN && (allow_trad || allow_c99))) {
   3164 			/* empty array declaration for '%s' */
   3165 			warning_at(190, &sym->s_def_pos, sym->s_name);
   3166 		} else {
   3167 			/* empty array declaration for '%s' */
   3168 			error_at(190, &sym->s_def_pos, sym->s_name);
   3169 		}
   3170 	}
   3171 }
   3172 
   3173 /*
   3174  * Prints information about location of previous definition/declaration.
   3175  */
   3176 void
   3177 print_previous_declaration(const sym_t *psym)
   3178 {
   3179 
   3180 	if (!rflag)
   3181 		return;
   3182 
   3183 	if (psym->s_def == DEF || psym->s_def == TDEF) {
   3184 		/* previous definition of '%s' */
   3185 		message_at(261, &psym->s_def_pos, psym->s_name);
   3186 	} else {
   3187 		/* previous declaration of '%s' */
   3188 		message_at(260, &psym->s_def_pos, psym->s_name);
   3189 	}
   3190 }
   3191 
   3192 /*
   3193  * Gets a node for a constant and returns the value of this constant
   3194  * as integer.
   3195  *
   3196  * If the node is not constant or too large for int or of type float,
   3197  * a warning will be printed.
   3198  *
   3199  * to_int_constant() should be used only inside declarations. If it is used in
   3200  * expressions, it frees the memory used for the expression.
   3201  */
   3202 int
   3203 to_int_constant(tnode_t *tn, bool required)
   3204 {
   3205 
   3206 	if (tn == NULL)
   3207 		return 1;
   3208 
   3209 	val_t *v = integer_constant(tn, required);
   3210 	bool is_unsigned = is_uinteger(v->v_tspec);
   3211 	int64_t val = v->u.integer;
   3212 	free(v);
   3213 
   3214 	/*
   3215 	 * Abstract declarations are used inside expression. To free
   3216 	 * the memory would be a fatal error.
   3217 	 * We don't free blocks that are inside casts because these
   3218 	 * will be used later to match types.
   3219 	 */
   3220 	if (tn->tn_op != CON && dcs->d_kind != DLK_ABSTRACT)
   3221 		expr_free_all();
   3222 
   3223 	bool out_of_bounds = is_unsigned
   3224 	    ? (uint64_t)val > (uint64_t)TARG_INT_MAX
   3225 	    : val > (int64_t)TARG_INT_MAX || val < (int64_t)TARG_INT_MIN;
   3226 	if (out_of_bounds)
   3227 		/* integral constant too large */
   3228 		warning(56);
   3229 	return (int)val;
   3230 }
   3231