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