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