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