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