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