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