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