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