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