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