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