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