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