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