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