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