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