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