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