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