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