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