Home | History | Annotate | Line # | Download | only in lint1
decl.c revision 1.160
      1 /* $NetBSD: decl.c,v 1.160 2021/03/26 17:44:52 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.160 2021/03/26 17:44:52 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 (dinfo_t));
     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 (type_t));
    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 duptyp() or tduptyp())
    137  * if it is to be modified (adding qualifiers or anything else).
    138  */
    139 type_t *
    140 gettyp(tspec_t t)
    141 {
    142 
    143 	return &typetab[t];
    144 }
    145 
    146 type_t *
    147 duptyp(const type_t *tp)
    148 {
    149 	type_t	*ntp;
    150 
    151 	ntp = getblk(sizeof (type_t));
    152 	*ntp = *tp;
    153 	return ntp;
    154 }
    155 
    156 /*
    157  * Use tduptyp() instead of duptyp() inside expressions (if the
    158  * allocated memory should be freed after the expr).
    159  */
    160 type_t *
    161 tduptyp(const type_t *tp)
    162 {
    163 	type_t	*ntp;
    164 
    165 	ntp = tgetblk(sizeof (type_t));
    166 	*ntp = *tp;
    167 	return ntp;
    168 }
    169 
    170 /*
    171  * Returns whether the argument is void or an incomplete array,
    172  * struct, union or enum type.
    173  */
    174 bool
    175 is_incomplete(const type_t *tp)
    176 {
    177 	tspec_t	t;
    178 
    179 	if ((t = tp->t_tspec) == VOID) {
    180 		return true;
    181 	} else if (t == ARRAY) {
    182 		return tp->t_incomplete_array;
    183 	} else if (t == STRUCT || t == UNION) {
    184 		return tp->t_str->sou_incomplete;
    185 	} else if (t == ENUM) {
    186 		return tp->t_enum->en_incomplete;
    187 	}
    188 	return false;
    189 }
    190 
    191 /*
    192  * Mark an array, struct, union or enum type as complete or incomplete.
    193  */
    194 void
    195 setcomplete(type_t *tp, bool complete)
    196 {
    197 	tspec_t	t;
    198 
    199 	if ((t = tp->t_tspec) == ARRAY) {
    200 		tp->t_incomplete_array = !complete;
    201 	} else if (t == STRUCT || t == UNION) {
    202 		tp->t_str->sou_incomplete = !complete;
    203 	} else {
    204 		lint_assert(t == ENUM);
    205 		tp->t_enum->en_incomplete = !complete;
    206 	}
    207 }
    208 
    209 /*
    210  * Remember the storage class of the current declaration in dcs->d_scl
    211  * (the top element of the declaration stack) and detect multiple
    212  * storage classes.
    213  */
    214 void
    215 add_storage_class(scl_t sc)
    216 {
    217 
    218 	if (sc == INLINE) {
    219 		if (dcs->d_inline)
    220 			/* duplicate '%s' */
    221 			warning(10, "inline");
    222 		dcs->d_inline = true;
    223 		return;
    224 	}
    225 	if (dcs->d_type != NULL || dcs->d_abstract_type != NOTSPEC ||
    226 	    dcs->d_sign_mod != NOTSPEC || dcs->d_rank_mod != NOTSPEC) {
    227 		/* storage class after type is obsolescent */
    228 		warning(83);
    229 	}
    230 	if (dcs->d_scl == NOSCL) {
    231 		dcs->d_scl = sc;
    232 	} else {
    233 		/*
    234 		 * multiple storage classes. An error will be reported in
    235 		 * deftyp().
    236 		 */
    237 		dcs->d_mscl = true;
    238 	}
    239 }
    240 
    241 /*
    242  * Remember the type, modifier or typedef name returned by the parser
    243  * in *dcs (top element of decl stack). This information is used in
    244  * deftyp() to build the type used for all declarators in this
    245  * declaration.
    246  *
    247  * If tp->t_typedef is 1, the type comes from a previously defined typename.
    248  * Otherwise it comes from a type specifier (int, long, ...) or a
    249  * struct/union/enum tag.
    250  */
    251 void
    252 add_type(type_t *tp)
    253 {
    254 	tspec_t	t;
    255 #ifdef DEBUG
    256 	printf("%s: %s\n", __func__, type_name(tp));
    257 #endif
    258 	if (tp->t_typedef) {
    259 		/*
    260 		 * something like "typedef int a; int a b;"
    261 		 * This should not happen with current grammar.
    262 		 */
    263 		lint_assert(dcs->d_type == NULL);
    264 		lint_assert(dcs->d_abstract_type == NOTSPEC);
    265 		lint_assert(dcs->d_sign_mod == NOTSPEC);
    266 		lint_assert(dcs->d_rank_mod == NOTSPEC);
    267 
    268 		dcs->d_type = tp;
    269 		return;
    270 	}
    271 
    272 	t = tp->t_tspec;
    273 
    274 	if (t == STRUCT || t == UNION || t == ENUM) {
    275 		/*
    276 		 * something like "int struct a ..."
    277 		 * struct/union/enum with anything else is not allowed
    278 		 */
    279 		if (dcs->d_type != NULL || dcs->d_abstract_type != NOTSPEC ||
    280 		    dcs->d_rank_mod != NOTSPEC || dcs->d_sign_mod != NOTSPEC) {
    281 			/*
    282 			 * remember that an error must be reported in
    283 			 * deftyp().
    284 			 */
    285 			dcs->d_terr = true;
    286 			dcs->d_abstract_type = NOTSPEC;
    287 			dcs->d_sign_mod = NOTSPEC;
    288 			dcs->d_rank_mod = NOTSPEC;
    289 		}
    290 		dcs->d_type = tp;
    291 		return;
    292 	}
    293 
    294 	if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
    295 		/*
    296 		 * something like "struct a int"
    297 		 * struct/union/enum with anything else is not allowed
    298 		 */
    299 		dcs->d_terr = true;
    300 		return;
    301 	}
    302 
    303 	if (t == COMPLEX) {
    304 		if (dcs->d_complex_mod == FLOAT)
    305 			t = FCOMPLEX;
    306 		else if (dcs->d_complex_mod == DOUBLE)
    307 			t = DCOMPLEX;
    308 		else {
    309 			/* invalid type for _Complex */
    310 			error(308);
    311 			t = DCOMPLEX; /* just as a fallback */
    312 		}
    313 		dcs->d_complex_mod = NOTSPEC;
    314 	}
    315 
    316 	if (t == LONG && dcs->d_rank_mod == LONG) {
    317 		/* "long long" or "long ... long" */
    318 		t = QUAD;
    319 		dcs->d_rank_mod = NOTSPEC;
    320 		if (!quadflg)
    321 			/* %s C does not support 'long long' */
    322 			c99ism(265, tflag ? "traditional" : "c89");
    323 	}
    324 
    325 	if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
    326 		/* something like "typedef int a; a long ..." */
    327 		dcs->d_type = tdeferr(dcs->d_type, t);
    328 		return;
    329 	}
    330 
    331 	/* now it can be only a combination of arithmetic types and void */
    332 	if (t == SIGNED || t == UNSIGN) {
    333 		/*
    334 		 * remember specifiers "signed" & "unsigned" in
    335 		 * dcs->d_sign_mod
    336 		 */
    337 		if (dcs->d_sign_mod != NOTSPEC)
    338 			/*
    339 			 * more than one "signed" and/or "unsigned"; print
    340 			 * an error in deftyp()
    341 			 */
    342 			dcs->d_terr = true;
    343 		dcs->d_sign_mod = t;
    344 	} else if (t == SHORT || t == LONG || t == QUAD) {
    345 		/*
    346 		 * remember specifiers "short", "long" and "long long" in
    347 		 * dcs->d_rank_mod
    348 		 */
    349 		if (dcs->d_rank_mod != NOTSPEC)
    350 			/* more than one, print error in deftyp() */
    351 			dcs->d_terr = true;
    352 		dcs->d_rank_mod = t;
    353 	} else if (t == FLOAT || t == DOUBLE) {
    354 		if (dcs->d_rank_mod == NOTSPEC || dcs->d_rank_mod == LONG) {
    355 			if (dcs->d_complex_mod != NOTSPEC
    356 			    || (t == FLOAT && dcs->d_rank_mod == LONG))
    357 				dcs->d_terr = true;
    358 			dcs->d_complex_mod = t;
    359 		} else {
    360 			if (dcs->d_abstract_type != NOTSPEC)
    361 				dcs->d_terr = true;
    362 			dcs->d_abstract_type = t;
    363 		}
    364 	} else if (t == PTR) {
    365 		dcs->d_type = tp;
    366 	} else {
    367 		/*
    368 		 * remember specifiers "void", "char", "int",
    369 		 * or "_Complex" in dcs->d_abstract_type
    370 		 */
    371 		if (dcs->d_abstract_type != NOTSPEC)
    372 			/* more than one, print error in deftyp() */
    373 			dcs->d_terr = true;
    374 		dcs->d_abstract_type = t;
    375 	}
    376 }
    377 
    378 /*
    379  * called if a list of declaration specifiers contains a typedef name
    380  * and other specifiers (except struct, union, enum, typedef name)
    381  */
    382 static type_t *
    383 tdeferr(type_t *td, tspec_t t)
    384 {
    385 	tspec_t	t2;
    386 
    387 	t2 = td->t_tspec;
    388 
    389 	switch (t) {
    390 	case SIGNED:
    391 	case UNSIGN:
    392 		if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG ||
    393 		    t2 == QUAD) {
    394 			if (!tflag)
    395 				/* modifying typedef with '%s'; only ... */
    396 				warning(5, ttab[t].tt_name);
    397 			td = duptyp(gettyp(merge_type_specifiers(t2, t)));
    398 			td->t_typedef = true;
    399 			return td;
    400 		}
    401 		break;
    402 	case SHORT:
    403 		if (t2 == INT || t2 == UINT) {
    404 			/* modifying typedef with '%s'; only qualifiers ... */
    405 			warning(5, "short");
    406 			td = duptyp(gettyp(t2 == INT ? SHORT : USHORT));
    407 			td->t_typedef = true;
    408 			return td;
    409 		}
    410 		break;
    411 	case LONG:
    412 		if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG ||
    413 		    t2 == FLOAT || t2 == DOUBLE || t2 == DCOMPLEX) {
    414 			/* modifying typedef with '%s'; only qualifiers ... */
    415 			warning(5, "long");
    416 			if (t2 == INT) {
    417 				td = gettyp(LONG);
    418 			} else if (t2 == UINT) {
    419 				td = gettyp(ULONG);
    420 			} else if (t2 == LONG) {
    421 				td = gettyp(QUAD);
    422 			} else if (t2 == ULONG) {
    423 				td = gettyp(UQUAD);
    424 			} else if (t2 == FLOAT) {
    425 				td = gettyp(DOUBLE);
    426 			} else if (t2 == DOUBLE) {
    427 				td = gettyp(LDOUBLE);
    428 			} else if (t2 == DCOMPLEX) {
    429 				td = gettyp(LCOMPLEX);
    430 			}
    431 			td = duptyp(td);
    432 			td->t_typedef = true;
    433 			return td;
    434 		}
    435 		break;
    436 		/* LINTED206: (enumeration values not handled in switch) */
    437 	case NOTSPEC:
    438 	case USHORT:
    439 	case UCHAR:
    440 	case SCHAR:
    441 	case CHAR:
    442 	case BOOL:
    443 	case FUNC:
    444 	case ARRAY:
    445 	case PTR:
    446 	case ENUM:
    447 	case UNION:
    448 	case STRUCT:
    449 	case VOID:
    450 	case LDOUBLE:
    451 	case FLOAT:
    452 	case DOUBLE:
    453 	case UQUAD:
    454 	case QUAD:
    455 #ifdef INT128_SIZE
    456 	case UINT128:
    457 	case INT128:
    458 #endif
    459 	case ULONG:
    460 	case UINT:
    461 	case INT:
    462 	case FCOMPLEX:
    463 	case DCOMPLEX:
    464 	case LCOMPLEX:
    465 	case COMPLEX:
    466 		break;
    467 	}
    468 
    469 	/* Anything other is not accepted. */
    470 
    471 	dcs->d_terr = true;
    472 	return td;
    473 }
    474 
    475 /*
    476  * Remember the symbol of a typedef name (2nd arg) in a struct, union
    477  * or enum tag if the typedef name is the first defined for this tag.
    478  *
    479  * If the tag is unnamed, the typdef name is used for identification
    480  * of this tag in lint2. Although it's possible that more than one typedef
    481  * name is defined for one tag, the first name defined should be unique
    482  * if the tag is unnamed.
    483  */
    484 static void
    485 settdsym(type_t *tp, sym_t *sym)
    486 {
    487 	tspec_t	t;
    488 
    489 	if ((t = tp->t_tspec) == STRUCT || t == UNION) {
    490 		if (tp->t_str->sou_first_typedef == NULL)
    491 			tp->t_str->sou_first_typedef = sym;
    492 	} else if (t == ENUM) {
    493 		if (tp->t_enum->en_first_typedef == NULL)
    494 			tp->t_enum->en_first_typedef = sym;
    495 	}
    496 }
    497 
    498 static size_t
    499 bitfieldsize(sym_t **mem)
    500 {
    501 	size_t len = (*mem)->s_type->t_flen;
    502 	while (*mem != NULL && (*mem)->s_type->t_bitfield) {
    503 		len += (*mem)->s_type->t_flen;
    504 		*mem = (*mem)->s_next;
    505 	}
    506 	return ((len + INT_SIZE - 1) / INT_SIZE) * INT_SIZE;
    507 }
    508 
    509 static void
    510 setpackedsize(type_t *tp)
    511 {
    512 	struct_or_union *sp;
    513 	sym_t *mem;
    514 
    515 	switch (tp->t_tspec) {
    516 	case STRUCT:
    517 	case UNION:
    518 		sp = tp->t_str;
    519 		sp->sou_size_in_bits = 0;
    520 		for (mem = sp->sou_first_member;
    521 		     mem != NULL; mem = mem->s_next) {
    522 			if (mem->s_type->t_bitfield) {
    523 				sp->sou_size_in_bits += bitfieldsize(&mem);
    524 				if (mem == NULL)
    525 					break;
    526 			}
    527 			size_t x = (size_t)type_size_in_bits(mem->s_type);
    528 			if (tp->t_tspec == STRUCT)
    529 				sp->sou_size_in_bits += x;
    530 			else if (x > sp->sou_size_in_bits)
    531 				sp->sou_size_in_bits = x;
    532 		}
    533 		break;
    534 	default:
    535 		/* %s attribute ignored for %s */
    536 		warning(326, "packed", type_name(tp));
    537 		break;
    538 	}
    539 }
    540 
    541 void
    542 addpacked(void)
    543 {
    544 	if (dcs->d_type == NULL)
    545 		dcs->d_packed = true;
    546 	else
    547 		setpackedsize(dcs->d_type);
    548 }
    549 
    550 void
    551 add_attr_used(void)
    552 {
    553 	dcs->d_used = true;
    554 }
    555 
    556 /*
    557  * Remember a qualifier which is part of the declaration specifiers
    558  * (and not the declarator) in the top element of the declaration stack.
    559  * Also detect multiple qualifiers of the same kind.
    560 
    561  * The remembered qualifier is used by deftyp() to construct the type
    562  * for all declarators.
    563  */
    564 void
    565 add_qualifier(tqual_t q)
    566 {
    567 
    568 	if (q == CONST) {
    569 		if (dcs->d_const) {
    570 			/* duplicate '%s' */
    571 			warning(10, "const");
    572 		}
    573 		dcs->d_const = true;
    574 	} else if (q == VOLATILE) {
    575 		if (dcs->d_volatile) {
    576 			/* duplicate '%s' */
    577 			warning(10, "volatile");
    578 		}
    579 		dcs->d_volatile = true;
    580 	} else {
    581 		lint_assert(q == RESTRICT || q == THREAD);
    582 		/* Silently ignore these qualifiers. */
    583 	}
    584 }
    585 
    586 /*
    587  * Go to the next declaration level (structs, nested structs, blocks,
    588  * argument declaration lists ...)
    589  */
    590 void
    591 begin_declaration_level(scl_t sc)
    592 {
    593 	dinfo_t	*di;
    594 
    595 	/* put a new element on the declaration stack */
    596 	di = xcalloc(1, sizeof (dinfo_t));
    597 	di->d_next = dcs;
    598 	dcs = di;
    599 	di->d_ctx = sc;
    600 	di->d_ldlsym = &di->d_dlsyms;
    601 	if (dflag)
    602 		(void)printf("begin_declaration_level(%p %d)\n",
    603 		    dcs, (int)sc);
    604 
    605 }
    606 
    607 /*
    608  * Go back to previous declaration level
    609  */
    610 void
    611 end_declaration_level(void)
    612 {
    613 	dinfo_t	*di;
    614 
    615 	if (dflag)
    616 		(void)printf("end_declaration_level(%p %d)\n",
    617 		    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 			LERROR("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 = duptyp(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 		LERROR("%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 			LERROR("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 = incref(*tpp, PTR);
   1021 				} else {
   1022 					*tpp = incref((*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 = duptyp(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 = incref(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 (sym_t));
   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 = duptyp(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 (type_t));
   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 (type_t));
   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 (type_t));
   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 (type_t));
   1674 			tp->t_packed = dcs->d_packed;
   1675 		} else {
   1676 			tp = tag->s_type;
   1677 		}
   1678 	} else {
   1679 		tag = getblk(sizeof (sym_t));
   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 (type_t));
   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 (struct_or_union));
   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 void
   1897 declare(sym_t *decl, bool initflg, sbuf_t *renaming)
   1898 {
   1899 	char *s;
   1900 
   1901 	switch (dcs->d_ctx) {
   1902 	case EXTERN:
   1903 		if (renaming != NULL) {
   1904 			lint_assert(decl->s_rename == NULL);
   1905 
   1906 			s = getlblk(1, renaming->sb_len + 1);
   1907 			(void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
   1908 			decl->s_rename = s;
   1909 		}
   1910 		decl1ext(decl, initflg);
   1911 		break;
   1912 	case ARG:
   1913 		if (renaming != NULL) {
   1914 			/* symbol renaming can't be used on function arguments */
   1915 			error(310);
   1916 			break;
   1917 		}
   1918 		(void)declare_argument(decl, initflg);
   1919 		break;
   1920 	default:
   1921 		lint_assert(dcs->d_ctx == AUTO);
   1922 		if (renaming != NULL) {
   1923 			/* symbol renaming can't be used on automatic variables */
   1924 			error(311);
   1925 			break;
   1926 		}
   1927 		declare_local(decl, initflg);
   1928 		break;
   1929 	}
   1930 
   1931 	if (initflg && !*current_initerr())
   1932 		initstack_init();
   1933 }
   1934 
   1935 /*
   1936  * Process a single external declarator.
   1937  */
   1938 void
   1939 decl1ext(sym_t *dsym, bool initflg)
   1940 {
   1941 	bool	dowarn, rval, redec;
   1942 	sym_t	*rdsym;
   1943 
   1944 	check_function_definition(dsym, true);
   1945 
   1946 	check_type(dsym);
   1947 
   1948 	if (initflg && !(*current_initerr() = check_init(dsym)))
   1949 		dsym->s_def = DEF;
   1950 
   1951 	/*
   1952 	 * Declarations of functions are marked as "tentative" in
   1953 	 * declarator_name(). This is wrong because there are no
   1954 	 * tentative function definitions.
   1955 	 */
   1956 	if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
   1957 		dsym->s_def = DECL;
   1958 
   1959 	if (dcs->d_inline) {
   1960 		if (dsym->s_type->t_tspec == FUNC) {
   1961 			dsym->s_inline = true;
   1962 		} else {
   1963 			/* variable declared inline: %s */
   1964 			warning(268, dsym->s_name);
   1965 		}
   1966 	}
   1967 
   1968 	/* Write the declaration into the output file */
   1969 	if (plibflg && llibflg &&
   1970 	    dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
   1971 		/*
   1972 		 * With both LINTLIBRARY and PROTOLIB the prototype is
   1973 		 * written as a function definition to the output file.
   1974 		 */
   1975 		rval = dsym->s_type->t_subt->t_tspec != VOID;
   1976 		outfdef(dsym, &dsym->s_def_pos, rval, false, NULL);
   1977 	} else {
   1978 		outsym(dsym, dsym->s_scl, dsym->s_def);
   1979 	}
   1980 
   1981 	if ((rdsym = dcs->d_redeclared_symbol) != NULL) {
   1982 
   1983 		/*
   1984 		 * If the old symbol stems from an old style function
   1985 		 * definition, we have remembered the params in rdsmy->s_args
   1986 		 * and compare them with the params of the prototype.
   1987 		 */
   1988 		if (rdsym->s_osdef && dsym->s_type->t_proto) {
   1989 			redec = check_old_style_definition(rdsym, dsym);
   1990 		} else {
   1991 			redec = false;
   1992 		}
   1993 
   1994 		if (!redec &&
   1995 		    !check_redeclaration(dsym, (dowarn = false, &dowarn))) {
   1996 
   1997 			if (dowarn) {
   1998 				if (sflag)
   1999 					/* redeclaration of %s */
   2000 					error(27, dsym->s_name);
   2001 				else
   2002 					/* redeclaration of %s */
   2003 					warning(27, dsym->s_name);
   2004 				print_previous_declaration(-1, rdsym);
   2005 			}
   2006 
   2007 			/*
   2008 			 * Take over the remembered params if the new symbol
   2009 			 * is not a prototype.
   2010 			 */
   2011 			if (rdsym->s_osdef && !dsym->s_type->t_proto) {
   2012 				dsym->s_osdef = rdsym->s_osdef;
   2013 				dsym->s_args = rdsym->s_args;
   2014 				dsym->s_def_pos = rdsym->s_def_pos;
   2015 			}
   2016 
   2017 			/*
   2018 			 * Remember the position of the declaration if the
   2019 			 * old symbol was a prototype and the new is not.
   2020 			 * Also remember the position if the old symbol
   2021 			 * was defined and the new is not.
   2022 			 */
   2023 			if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
   2024 				dsym->s_def_pos = rdsym->s_def_pos;
   2025 			} else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
   2026 				dsym->s_def_pos = rdsym->s_def_pos;
   2027 			}
   2028 
   2029 			/*
   2030 			 * Copy usage information of the name into the new
   2031 			 * symbol.
   2032 			 */
   2033 			copy_usage_info(dsym, rdsym);
   2034 
   2035 			/* Once a name is defined, it remains defined. */
   2036 			if (rdsym->s_def == DEF)
   2037 				dsym->s_def = DEF;
   2038 
   2039 			/* once a function is inline, it remains inline */
   2040 			if (rdsym->s_inline)
   2041 				dsym->s_inline = true;
   2042 
   2043 			complete_type(dsym, rdsym);
   2044 
   2045 		}
   2046 
   2047 		rmsym(rdsym);
   2048 	}
   2049 
   2050 	if (dsym->s_scl == TYPEDEF) {
   2051 		dsym->s_type = duptyp(dsym->s_type);
   2052 		dsym->s_type->t_typedef = true;
   2053 		settdsym(dsym->s_type, dsym);
   2054 	}
   2055 
   2056 }
   2057 
   2058 /*
   2059  * Copies information about usage into a new symbol table entry of
   2060  * the same symbol.
   2061  */
   2062 void
   2063 copy_usage_info(sym_t *sym, sym_t *rdsym)
   2064 {
   2065 
   2066 	sym->s_set_pos = rdsym->s_set_pos;
   2067 	sym->s_use_pos = rdsym->s_use_pos;
   2068 	sym->s_set = rdsym->s_set;
   2069 	sym->s_used = rdsym->s_used;
   2070 }
   2071 
   2072 /*
   2073  * Prints an error and returns true if a symbol is redeclared/redefined.
   2074  * Otherwise returns false and, in some cases of minor problems, prints
   2075  * a warning.
   2076  */
   2077 bool
   2078 check_redeclaration(sym_t *dsym, bool *dowarn)
   2079 {
   2080 	sym_t	*rsym;
   2081 
   2082 	if ((rsym = dcs->d_redeclared_symbol)->s_scl == CTCONST) {
   2083 		/* redeclaration of %s */
   2084 		error(27, dsym->s_name);
   2085 		print_previous_declaration(-1, rsym);
   2086 		return true;
   2087 	}
   2088 	if (rsym->s_scl == TYPEDEF) {
   2089 		/* typedef redeclared: %s */
   2090 		error(89, dsym->s_name);
   2091 		print_previous_declaration(-1, rsym);
   2092 		return true;
   2093 	}
   2094 	if (dsym->s_scl == TYPEDEF) {
   2095 		/* redeclaration of %s */
   2096 		error(27, dsym->s_name);
   2097 		print_previous_declaration(-1, rsym);
   2098 		return true;
   2099 	}
   2100 	if (rsym->s_def == DEF && dsym->s_def == DEF) {
   2101 		/* redefinition of %s */
   2102 		error(28, dsym->s_name);
   2103 		print_previous_declaration(-1, rsym);
   2104 		return true;
   2105 	}
   2106 	if (!eqtype(rsym->s_type, dsym->s_type, false, false, dowarn)) {
   2107 		/* redeclaration of %s */
   2108 		error(27, dsym->s_name);
   2109 		print_previous_declaration(-1, rsym);
   2110 		return true;
   2111 	}
   2112 	if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
   2113 		return false;
   2114 	if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
   2115 		return false;
   2116 	if (rsym->s_scl == STATIC && dsym->s_def == DECL)
   2117 		return false;
   2118 	if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
   2119 		/*
   2120 		 * All cases except "int a = 1; static int a;" are caught
   2121 		 * above with or without a warning
   2122 		 */
   2123 		/* redeclaration of %s */
   2124 		error(27, dsym->s_name);
   2125 		print_previous_declaration(-1, rsym);
   2126 		return true;
   2127 	}
   2128 	if (rsym->s_scl == EXTERN) {
   2129 		/* previously declared extern, becomes static: %s */
   2130 		warning(29, dsym->s_name);
   2131 		print_previous_declaration(-1, rsym);
   2132 		return false;
   2133 	}
   2134 	/*
   2135 	 * Now it's one of:
   2136 	 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
   2137 	 */
   2138 	/* redeclaration of %s; ANSI C requires "static" */
   2139 	if (sflag) {
   2140 		/* redeclaration of %s; ANSI C requires static */
   2141 		warning(30, dsym->s_name);
   2142 		print_previous_declaration(-1, rsym);
   2143 	}
   2144 	dsym->s_scl = STATIC;
   2145 	return false;
   2146 }
   2147 
   2148 static bool
   2149 qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual)
   2150 {
   2151 	if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
   2152 		return false;
   2153 
   2154 	if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
   2155 		return false;
   2156 
   2157 	return true;
   2158 }
   2159 
   2160 bool
   2161 eqptrtype(const type_t *tp1, const type_t *tp2, bool ignqual)
   2162 {
   2163 	if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
   2164 		return false;
   2165 
   2166 	if (!qualifiers_correspond(tp1, tp2, ignqual))
   2167 		return false;
   2168 
   2169 	return true;
   2170 }
   2171 
   2172 
   2173 /*
   2174  * Checks if two types are compatible.
   2175  *
   2176  * ignqual	ignore qualifiers of type; used for function params
   2177  * promot	promote left type; used for comparison of params of
   2178  *		old style function definitions with params of prototypes.
   2179  * *dowarn	set to true if an old style function declaration is not
   2180  *		compatible with a prototype
   2181  */
   2182 bool
   2183 eqtype(const type_t *tp1, const type_t *tp2,
   2184        bool ignqual, bool promot, bool *dowarn)
   2185 {
   2186 	tspec_t	t;
   2187 
   2188 	while (tp1 != NULL && tp2 != NULL) {
   2189 
   2190 		t = tp1->t_tspec;
   2191 		if (promot) {
   2192 			if (t == FLOAT) {
   2193 				t = DOUBLE;
   2194 			} else if (t == CHAR || t == SCHAR) {
   2195 				t = INT;
   2196 			} else if (t == UCHAR) {
   2197 				t = tflag ? UINT : INT;
   2198 			} else if (t == SHORT) {
   2199 				t = INT;
   2200 			} else if (t == USHORT) {
   2201 				/* CONSTCOND */
   2202 				t = TARG_INT_MAX < TARG_USHRT_MAX || tflag ? UINT : INT;
   2203 			}
   2204 		}
   2205 
   2206 		if (t != tp2->t_tspec)
   2207 			return false;
   2208 
   2209 		if (!qualifiers_correspond(tp1, tp2, ignqual))
   2210 			return false;
   2211 
   2212 		if (t == STRUCT || t == UNION)
   2213 			return tp1->t_str == tp2->t_str;
   2214 
   2215 		if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
   2216 			if (tp1->t_dim != 0 && tp2->t_dim != 0)
   2217 				return false;
   2218 		}
   2219 
   2220 		/* dont check prototypes for traditional */
   2221 		if (t == FUNC && !tflag) {
   2222 			if (tp1->t_proto && tp2->t_proto) {
   2223 				if (!eqargs(tp1, tp2, dowarn))
   2224 					return false;
   2225 			} else if (tp1->t_proto) {
   2226 				if (!mnoarg(tp1, dowarn))
   2227 					return false;
   2228 			} else if (tp2->t_proto) {
   2229 				if (!mnoarg(tp2, dowarn))
   2230 					return false;
   2231 			}
   2232 		}
   2233 
   2234 		tp1 = tp1->t_subt;
   2235 		tp2 = tp2->t_subt;
   2236 		ignqual = promot = false;
   2237 
   2238 	}
   2239 
   2240 	return tp1 == tp2;
   2241 }
   2242 
   2243 /*
   2244  * Compares the parameter types of two prototypes.
   2245  */
   2246 static bool
   2247 eqargs(const type_t *tp1, const type_t *tp2, bool *dowarn)
   2248 {
   2249 	sym_t	*a1, *a2;
   2250 
   2251 	if (tp1->t_vararg != tp2->t_vararg)
   2252 		return false;
   2253 
   2254 	a1 = tp1->t_args;
   2255 	a2 = tp2->t_args;
   2256 
   2257 	while (a1 != NULL && a2 != NULL) {
   2258 
   2259 		if (!eqtype(a1->s_type, a2->s_type, true, false, dowarn))
   2260 			return false;
   2261 
   2262 		a1 = a1->s_next;
   2263 		a2 = a2->s_next;
   2264 
   2265 	}
   2266 
   2267 	return a1 == a2;
   2268 }
   2269 
   2270 /*
   2271  * mnoarg() (matches functions with no argument type information)
   2272  * returns whether all parameters of a prototype are compatible with
   2273  * an old style function declaration.
   2274  * This is the case if the following conditions are met:
   2275  *	1. the prototype has a fixed number of parameters
   2276  *	2. no parameter is of type float
   2277  *	3. no parameter is converted to another type if integer promotion
   2278  *	   is applied on it
   2279  */
   2280 static bool
   2281 mnoarg(const type_t *tp, bool *dowarn)
   2282 {
   2283 	sym_t	*arg;
   2284 	tspec_t	t;
   2285 
   2286 	if (tp->t_vararg) {
   2287 		if (dowarn != NULL)
   2288 			*dowarn = true;
   2289 	}
   2290 	for (arg = tp->t_args; arg != NULL; arg = arg->s_next) {
   2291 		if ((t = arg->s_type->t_tspec) == FLOAT ||
   2292 		    t == CHAR || t == SCHAR || t == UCHAR ||
   2293 		    t == SHORT || t == USHORT) {
   2294 			if (dowarn != NULL)
   2295 				*dowarn = true;
   2296 		}
   2297 	}
   2298 	return true;
   2299 }
   2300 
   2301 /*
   2302  * Compares a prototype declaration with the remembered arguments of
   2303  * a previous old style function definition.
   2304  */
   2305 static bool
   2306 check_old_style_definition(sym_t *rdsym, sym_t *dsym)
   2307 {
   2308 	sym_t	*args, *pargs, *arg, *parg;
   2309 	int	narg, nparg, n;
   2310 	bool	dowarn, msg;
   2311 
   2312 	args = rdsym->s_args;
   2313 	pargs = dsym->s_type->t_args;
   2314 
   2315 	msg = false;
   2316 
   2317 	narg = nparg = 0;
   2318 	for (arg = args; arg != NULL; arg = arg->s_next)
   2319 		narg++;
   2320 	for (parg = pargs; parg != NULL; parg = parg->s_next)
   2321 		nparg++;
   2322 	if (narg != nparg) {
   2323 		/* prototype does not match old-style definition */
   2324 		error(63);
   2325 		msg = true;
   2326 		goto end;
   2327 	}
   2328 
   2329 	arg = args;
   2330 	parg = pargs;
   2331 	n = 1;
   2332 	while (narg-- > 0) {
   2333 		dowarn = false;
   2334 		/*
   2335 		 * If it does not match due to promotion and sflag is
   2336 		 * not set we print only a warning.
   2337 		 */
   2338 		if (!eqtype(arg->s_type, parg->s_type, true, true, &dowarn) ||
   2339 		    dowarn) {
   2340 			/* prototype does not match old style defn., arg #%d */
   2341 			error(299, n);
   2342 			msg = true;
   2343 		}
   2344 		arg = arg->s_next;
   2345 		parg = parg->s_next;
   2346 		n++;
   2347 	}
   2348 
   2349  end:
   2350 	if (msg)
   2351 		/* old style definition */
   2352 		print_previous_declaration(300, rdsym);
   2353 
   2354 	return msg;
   2355 }
   2356 
   2357 /*
   2358  * Completes a type by copying the dimension and prototype information
   2359  * from a second compatible type.
   2360  *
   2361  * Following lines are legal:
   2362  *  "typedef a[]; a b; a b[10]; a c; a c[20];"
   2363  *  "typedef ft(); ft f; f(int); ft g; g(long);"
   2364  * This means that, if a type is completed, the type structure must
   2365  * be duplicated.
   2366  */
   2367 void
   2368 complete_type(sym_t *dsym, sym_t *ssym)
   2369 {
   2370 	type_t	**dstp, *src;
   2371 	type_t	*dst;
   2372 
   2373 	dstp = &dsym->s_type;
   2374 	src = ssym->s_type;
   2375 
   2376 	while ((dst = *dstp) != NULL) {
   2377 		lint_assert(src != NULL);
   2378 		lint_assert(dst->t_tspec == src->t_tspec);
   2379 		if (dst->t_tspec == ARRAY) {
   2380 			if (dst->t_dim == 0 && src->t_dim != 0) {
   2381 				*dstp = dst = duptyp(dst);
   2382 				dst->t_dim = src->t_dim;
   2383 				setcomplete(dst, true);
   2384 			}
   2385 		} else if (dst->t_tspec == FUNC) {
   2386 			if (!dst->t_proto && src->t_proto) {
   2387 				*dstp = dst = duptyp(dst);
   2388 				dst->t_proto = true;
   2389 				dst->t_args = src->t_args;
   2390 			}
   2391 		}
   2392 		dstp = &dst->t_subt;
   2393 		src = src->t_subt;
   2394 	}
   2395 }
   2396 
   2397 /*
   2398  * Completes the declaration of a single argument.
   2399  */
   2400 sym_t *
   2401 declare_argument(sym_t *sym, bool initflg)
   2402 {
   2403 	tspec_t	t;
   2404 
   2405 	check_function_definition(sym, true);
   2406 
   2407 	check_type(sym);
   2408 
   2409 	if (dcs->d_redeclared_symbol != NULL &&
   2410 	    dcs->d_redeclared_symbol->s_block_level == block_level) {
   2411 		/* redeclaration of formal parameter %s */
   2412 		error(237, sym->s_name);
   2413 		rmsym(dcs->d_redeclared_symbol);
   2414 		sym->s_arg = true;
   2415 	}
   2416 
   2417 	if (!sym->s_arg) {
   2418 		/* declared argument %s is missing */
   2419 		error(53, sym->s_name);
   2420 		sym->s_arg = true;
   2421 	}
   2422 
   2423 	if (initflg) {
   2424 		/* cannot initialize parameter: %s */
   2425 		error(52, sym->s_name);
   2426 		*current_initerr() = true;
   2427 	}
   2428 
   2429 	if ((t = sym->s_type->t_tspec) == ARRAY) {
   2430 		sym->s_type = incref(sym->s_type->t_subt, PTR);
   2431 	} else if (t == FUNC) {
   2432 		if (tflag)
   2433 			/* a function is declared as an argument: %s */
   2434 			warning(50, sym->s_name);
   2435 		sym->s_type = incref(sym->s_type, PTR);
   2436 	} else if (t == FLOAT) {
   2437 		if (tflag)
   2438 			sym->s_type = gettyp(DOUBLE);
   2439 	}
   2440 
   2441 	if (dcs->d_inline)
   2442 		/* argument declared inline: %s */
   2443 		warning(269, sym->s_name);
   2444 
   2445 	/*
   2446 	 * Arguments must have complete types. lengths() prints the needed
   2447 	 * error messages (null dimension is impossible because arrays are
   2448 	 * converted to pointers).
   2449 	 */
   2450 	if (sym->s_type->t_tspec != VOID)
   2451 		(void)length(sym->s_type, sym->s_name);
   2452 
   2453 	sym->s_used = dcs->d_used;
   2454 	mark_as_set(sym);
   2455 
   2456 	return sym;
   2457 }
   2458 
   2459 void
   2460 check_func_lint_directives(void)
   2461 {
   2462 	sym_t *arg;
   2463 	int narg, n;
   2464 	tspec_t t;
   2465 
   2466 	/* check for illegal combinations of lint directives */
   2467 	if (printflike_argnum != -1 && scanflike_argnum != -1) {
   2468 		/* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
   2469 		warning(289);
   2470 		printflike_argnum = scanflike_argnum = -1;
   2471 	}
   2472 	if (nvararg != -1 &&
   2473 	    (printflike_argnum != -1 || scanflike_argnum != -1)) {
   2474 		/* dubious use of ** VARARGS ** with ** %s ** */
   2475 		warning(288,
   2476 		    printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE");
   2477 		nvararg = -1;
   2478 	}
   2479 
   2480 	/*
   2481 	 * check if the argument of a lint directive is compatible with the
   2482 	 * number of arguments.
   2483 	 */
   2484 	narg = 0;
   2485 	for (arg = dcs->d_func_args; arg != NULL; arg = arg->s_next)
   2486 		narg++;
   2487 	if (nargusg > narg) {
   2488 		/* argument number mismatch with directive: ** %s ** */
   2489 		warning(283, "ARGSUSED");
   2490 		nargusg = 0;
   2491 	}
   2492 	if (nvararg > narg) {
   2493 		/* argument number mismatch with directive: ** %s ** */
   2494 		warning(283, "VARARGS");
   2495 		nvararg = 0;
   2496 	}
   2497 	if (printflike_argnum > narg) {
   2498 		/* argument number mismatch with directive: ** %s ** */
   2499 		warning(283, "PRINTFLIKE");
   2500 		printflike_argnum = -1;
   2501 	} else if (printflike_argnum == 0) {
   2502 		printflike_argnum = -1;
   2503 	}
   2504 	if (scanflike_argnum > narg) {
   2505 		/* argument number mismatch with directive: ** %s ** */
   2506 		warning(283, "SCANFLIKE");
   2507 		scanflike_argnum = -1;
   2508 	} else if (scanflike_argnum == 0) {
   2509 		scanflike_argnum = -1;
   2510 	}
   2511 	if (printflike_argnum != -1 || scanflike_argnum != -1) {
   2512 		narg = printflike_argnum != -1
   2513 		    ? printflike_argnum : scanflike_argnum;
   2514 		arg = dcs->d_func_args;
   2515 		for (n = 1; n < narg; n++)
   2516 			arg = arg->s_next;
   2517 		if (arg->s_type->t_tspec != PTR ||
   2518 		    ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
   2519 		     t != UCHAR && t != SCHAR)) {
   2520 			/* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
   2521 			warning(293, narg);
   2522 			printflike_argnum = scanflike_argnum = -1;
   2523 		}
   2524 	}
   2525 }
   2526 
   2527 /*
   2528  * Warn about arguments in old style function definitions that default to int.
   2529  * Check that an old style function definition is compatible to a previous
   2530  * prototype.
   2531  */
   2532 void
   2533 check_func_old_style_arguments(void)
   2534 {
   2535 	sym_t *args, *arg, *pargs, *parg;
   2536 	int narg, nparg;
   2537 	bool msg;
   2538 
   2539 	args = funcsym->s_args;
   2540 	pargs = funcsym->s_type->t_args;
   2541 
   2542 	/*
   2543 	 * print a warning for each argument of an old style function
   2544 	 * definition which defaults to int
   2545 	 */
   2546 	for (arg = args; arg != NULL; arg = arg->s_next) {
   2547 		if (arg->s_defarg) {
   2548 			/* argument type defaults to 'int': %s */
   2549 			warning(32, arg->s_name);
   2550 			arg->s_defarg = false;
   2551 			mark_as_set(arg);
   2552 		}
   2553 	}
   2554 
   2555 	/*
   2556 	 * If this is an old style function definition and a prototype
   2557 	 * exists, compare the types of arguments.
   2558 	 */
   2559 	if (funcsym->s_osdef && funcsym->s_type->t_proto) {
   2560 		/*
   2561 		 * If the number of arguments does not match, we need not
   2562 		 * continue.
   2563 		 */
   2564 		narg = nparg = 0;
   2565 		msg = false;
   2566 		for (parg = pargs; parg != NULL; parg = parg->s_next)
   2567 			nparg++;
   2568 		for (arg = args; arg != NULL; arg = arg->s_next)
   2569 			narg++;
   2570 		if (narg != nparg) {
   2571 			/* parameter mismatch: %d declared, %d defined */
   2572 			error(51, nparg, narg);
   2573 			msg = true;
   2574 		} else {
   2575 			parg = pargs;
   2576 			arg = args;
   2577 			while (narg-- > 0) {
   2578 				msg |= check_prototype_declaration(arg, parg);
   2579 				parg = parg->s_next;
   2580 				arg = arg->s_next;
   2581 			}
   2582 		}
   2583 		if (msg)
   2584 			/* prototype declaration */
   2585 			print_previous_declaration(285,
   2586 			    dcs->d_redeclared_symbol);
   2587 
   2588 		/* from now on the prototype is valid */
   2589 		funcsym->s_osdef = false;
   2590 		funcsym->s_args = NULL;
   2591 	}
   2592 }
   2593 
   2594 /*
   2595  * Checks compatibility of an old style function definition with a previous
   2596  * prototype declaration.
   2597  * Returns true if the position of the previous declaration should be reported.
   2598  */
   2599 static bool
   2600 check_prototype_declaration(sym_t *arg, sym_t *parg)
   2601 {
   2602 	type_t	*tp, *ptp;
   2603 	bool	dowarn, msg;
   2604 
   2605 	tp = arg->s_type;
   2606 	ptp = parg->s_type;
   2607 
   2608 	msg = false;
   2609 	dowarn = false;
   2610 
   2611 	if (!eqtype(tp, ptp, true, true, &dowarn)) {
   2612 		if (eqtype(tp, ptp, true, false, &dowarn)) {
   2613 			/* type does not match prototype: %s */
   2614 			gnuism(58, arg->s_name);
   2615 			msg = sflag || !gflag;
   2616 		} else {
   2617 			/* type does not match prototype: %s */
   2618 			error(58, arg->s_name);
   2619 			msg = true;
   2620 		}
   2621 	} else if (dowarn) {
   2622 		if (sflag)
   2623 			/* type does not match prototype: %s */
   2624 			error(58, arg->s_name);
   2625 		else
   2626 			/* type does not match prototype: %s */
   2627 			warning(58, arg->s_name);
   2628 		msg = true;
   2629 	}
   2630 
   2631 	return msg;
   2632 }
   2633 
   2634 /*
   2635  * Completes a single local declaration/definition.
   2636  */
   2637 void
   2638 declare_local(sym_t *dsym, bool initflg)
   2639 {
   2640 
   2641 	/* Correct a mistake done in declarator_name(). */
   2642 	if (dsym->s_type->t_tspec == FUNC) {
   2643 		dsym->s_def = DECL;
   2644 		if (dcs->d_scl == NOSCL)
   2645 			dsym->s_scl = EXTERN;
   2646 	}
   2647 
   2648 	if (dsym->s_type->t_tspec == FUNC) {
   2649 		if (dsym->s_scl == STATIC) {
   2650 			/* dubious static function at block level: %s */
   2651 			warning(93, dsym->s_name);
   2652 			dsym->s_scl = EXTERN;
   2653 		} else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
   2654 			/* function has illegal storage class: %s */
   2655 			error(94, dsym->s_name);
   2656 			dsym->s_scl = EXTERN;
   2657 		}
   2658 	}
   2659 
   2660 	/*
   2661 	 * functions may be declared inline at local scope, although
   2662 	 * this has no effect for a later definition of the same
   2663 	 * function.
   2664 	 * XXX it should have an effect if tflag is set. this would
   2665 	 * also be the way gcc behaves.
   2666 	 */
   2667 	if (dcs->d_inline) {
   2668 		if (dsym->s_type->t_tspec == FUNC) {
   2669 			dsym->s_inline = true;
   2670 		} else {
   2671 			/* variable declared inline: %s */
   2672 			warning(268, dsym->s_name);
   2673 		}
   2674 	}
   2675 
   2676 	check_function_definition(dsym, true);
   2677 
   2678 	check_type(dsym);
   2679 
   2680 	if (dcs->d_redeclared_symbol != NULL && dsym->s_scl == EXTERN)
   2681 		declare_external_in_block(dsym);
   2682 
   2683 	if (dsym->s_scl == EXTERN) {
   2684 		/*
   2685 		 * XXX if the static variable at level 0 is only defined
   2686 		 * later, checking will be possible.
   2687 		 */
   2688 		if (dsym->s_ext_sym == NULL) {
   2689 			outsym(dsym, EXTERN, dsym->s_def);
   2690 		} else {
   2691 			outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
   2692 		}
   2693 	}
   2694 
   2695 	if (dcs->d_redeclared_symbol != NULL) {
   2696 
   2697 		if (dcs->d_redeclared_symbol->s_block_level == 0) {
   2698 
   2699 			switch (dsym->s_scl) {
   2700 			case AUTO:
   2701 				if (hflag)
   2702 					/* automatic hides external decl.: %s */
   2703 					warning(86, dsym->s_name);
   2704 				break;
   2705 			case STATIC:
   2706 				if (hflag)
   2707 					/* static hides external decl.: %s */
   2708 					warning(87, dsym->s_name);
   2709 				break;
   2710 			case TYPEDEF:
   2711 				if (hflag)
   2712 					/* typedef hides external decl.: %s */
   2713 					warning(88, dsym->s_name);
   2714 				break;
   2715 			case EXTERN:
   2716 				/*
   2717 				 * Warnings and errors are printed in
   2718 				 * declare_external_in_block()
   2719 				 */
   2720 				break;
   2721 			default:
   2722 				lint_assert(/*CONSTCOND*/false);
   2723 			}
   2724 
   2725 		} else if (dcs->d_redeclared_symbol->s_block_level ==
   2726 			   block_level) {
   2727 
   2728 			/* no hflag, because it's illegal! */
   2729 			if (dcs->d_redeclared_symbol->s_arg) {
   2730 				/*
   2731 				 * if !tflag, a "redeclaration of %s" error
   2732 				 * is produced below
   2733 				 */
   2734 				if (tflag) {
   2735 					if (hflag)
   2736 						/* decl. hides parameter: %s */
   2737 						warning(91, dsym->s_name);
   2738 					rmsym(dcs->d_redeclared_symbol);
   2739 				}
   2740 			}
   2741 
   2742 		} else if (dcs->d_redeclared_symbol->s_block_level <
   2743 			   block_level) {
   2744 
   2745 			if (hflag)
   2746 				/* declaration hides earlier one: %s */
   2747 				warning(95, dsym->s_name);
   2748 
   2749 		}
   2750 
   2751 		if (dcs->d_redeclared_symbol->s_block_level == block_level) {
   2752 
   2753 			/* redeclaration of %s */
   2754 			error(27, dsym->s_name);
   2755 			rmsym(dcs->d_redeclared_symbol);
   2756 
   2757 		}
   2758 
   2759 	}
   2760 
   2761 	if (initflg && !(*current_initerr() = check_init(dsym))) {
   2762 		dsym->s_def = DEF;
   2763 		mark_as_set(dsym);
   2764 	}
   2765 
   2766 	if (dsym->s_scl == TYPEDEF) {
   2767 		dsym->s_type = duptyp(dsym->s_type);
   2768 		dsym->s_type->t_typedef = true;
   2769 		settdsym(dsym->s_type, dsym);
   2770 	}
   2771 
   2772 	/*
   2773 	 * Before we can check the size we must wait for a initialization
   2774 	 * which may follow.
   2775 	 */
   2776 }
   2777 
   2778 /*
   2779  * Processes (re)declarations of external symbols inside blocks.
   2780  */
   2781 static void
   2782 declare_external_in_block(sym_t *dsym)
   2783 {
   2784 	bool	eqt, dowarn;
   2785 	sym_t	*esym;
   2786 
   2787 	/* look for a symbol with the same name */
   2788 	esym = dcs->d_redeclared_symbol;
   2789 	while (esym != NULL && esym->s_block_level != 0) {
   2790 		while ((esym = esym->s_link) != NULL) {
   2791 			if (esym->s_kind != FVFT)
   2792 				continue;
   2793 			if (strcmp(dsym->s_name, esym->s_name) == 0)
   2794 				break;
   2795 		}
   2796 	}
   2797 	if (esym == NULL)
   2798 		return;
   2799 	if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
   2800 		/* gcc accepts this without a warning, pcc prints an error. */
   2801 		/* redeclaration of %s */
   2802 		warning(27, dsym->s_name);
   2803 		print_previous_declaration(-1, esym);
   2804 		return;
   2805 	}
   2806 
   2807 	dowarn = false;
   2808 	eqt = eqtype(esym->s_type, dsym->s_type, false, false, &dowarn);
   2809 
   2810 	if (!eqt || dowarn) {
   2811 		if (esym->s_scl == EXTERN) {
   2812 			/* inconsistent redeclaration of extern: %s */
   2813 			warning(90, dsym->s_name);
   2814 			print_previous_declaration(-1, esym);
   2815 		} else {
   2816 			/* inconsistent redeclaration of static: %s */
   2817 			warning(92, dsym->s_name);
   2818 			print_previous_declaration(-1, esym);
   2819 		}
   2820 	}
   2821 
   2822 	if (eqt) {
   2823 		/*
   2824 		 * Remember the external symbol so we can update usage
   2825 		 * information at the end of the block.
   2826 		 */
   2827 		dsym->s_ext_sym = esym;
   2828 	}
   2829 }
   2830 
   2831 /*
   2832  * Print an error or a warning if the symbol cannot be initialized due
   2833  * to type/storage class. Return whether an error has been detected.
   2834  */
   2835 static bool
   2836 check_init(sym_t *sym)
   2837 {
   2838 	bool	erred;
   2839 
   2840 	erred = false;
   2841 
   2842 	if (sym->s_type->t_tspec == FUNC) {
   2843 		/* cannot initialize function: %s */
   2844 		error(24, sym->s_name);
   2845 		erred = true;
   2846 	} else if (sym->s_scl == TYPEDEF) {
   2847 		/* cannot initialize typedef: %s */
   2848 		error(25, sym->s_name);
   2849 		erred = true;
   2850 	} else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
   2851 		/* cannot initialize "extern" declaration: %s */
   2852 		if (dcs->d_ctx == EXTERN) {
   2853 			/* cannot initialize extern declaration: %s */
   2854 			warning(26, sym->s_name);
   2855 		} else {
   2856 			/* cannot initialize extern declaration: %s */
   2857 			error(26, sym->s_name);
   2858 			erred = true;
   2859 		}
   2860 	}
   2861 
   2862 	return erred;
   2863 }
   2864 
   2865 /*
   2866  * Create a symbol for an abstract declaration.
   2867  */
   2868 sym_t *
   2869 abstract_name(void)
   2870 {
   2871 	sym_t	*sym;
   2872 
   2873 	lint_assert(dcs->d_ctx == ABSTRACT || dcs->d_ctx == PROTO_ARG);
   2874 
   2875 	sym = getblk(sizeof (sym_t));
   2876 
   2877 	sym->s_name = unnamed;
   2878 	sym->s_def = DEF;
   2879 	sym->s_scl = ABSTRACT;
   2880 	sym->s_block_level = -1;
   2881 
   2882 	if (dcs->d_ctx == PROTO_ARG)
   2883 		sym->s_arg = true;
   2884 
   2885 	sym->s_type = dcs->d_type;
   2886 	dcs->d_redeclared_symbol = NULL;
   2887 	dcs->d_vararg = false;
   2888 
   2889 	return sym;
   2890 }
   2891 
   2892 /*
   2893  * Removes anything which has nothing to do on global level.
   2894  */
   2895 void
   2896 global_clean_up(void)
   2897 {
   2898 
   2899 	while (dcs->d_next != NULL)
   2900 		end_declaration_level();
   2901 
   2902 	cleanup();
   2903 	block_level = 0;
   2904 	mem_block_level = 0;
   2905 
   2906 	/*
   2907 	 * remove all information about pending lint directives without
   2908 	 * warnings.
   2909 	 */
   2910 	global_clean_up_decl(true);
   2911 }
   2912 
   2913 /*
   2914  * Process an abstract type declaration
   2915  */
   2916 sym_t *
   2917 declare_1_abstract(sym_t *sym)
   2918 {
   2919 
   2920 	check_function_definition(sym, true);
   2921 	check_type(sym);
   2922 	return sym;
   2923 }
   2924 
   2925 /*
   2926  * Checks size after declarations of variables and their initialization.
   2927  */
   2928 void
   2929 check_size(sym_t *dsym)
   2930 {
   2931 
   2932 	if (dsym->s_def != DEF)
   2933 		return;
   2934 	if (dsym->s_scl == TYPEDEF)
   2935 		return;
   2936 	if (dsym->s_type->t_tspec == FUNC)
   2937 		return;
   2938 
   2939 	if (length(dsym->s_type, dsym->s_name) == 0 &&
   2940 	    dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
   2941 		if (tflag) {
   2942 			/* empty array declaration: %s */
   2943 			warning(190, dsym->s_name);
   2944 		} else {
   2945 			/* empty array declaration: %s */
   2946 			error(190, dsym->s_name);
   2947 		}
   2948 	}
   2949 }
   2950 
   2951 /*
   2952  * Mark an object as set if it is not already
   2953  */
   2954 void
   2955 mark_as_set(sym_t *sym)
   2956 {
   2957 
   2958 	if (!sym->s_set) {
   2959 		sym->s_set = true;
   2960 		UNIQUE_CURR_POS(sym->s_set_pos);
   2961 	}
   2962 }
   2963 
   2964 /*
   2965  * Mark an object as used if it is not already
   2966  */
   2967 void
   2968 mark_as_used(sym_t *sym, bool fcall, bool szof)
   2969 {
   2970 
   2971 	if (!sym->s_used) {
   2972 		sym->s_used = true;
   2973 		UNIQUE_CURR_POS(sym->s_use_pos);
   2974 	}
   2975 	/*
   2976 	 * for function calls another record is written
   2977 	 *
   2978 	 * XXX Should symbols used in sizeof() be treated as used or not?
   2979 	 * Probably not, because there is no sense to declare an
   2980 	 * external variable only to get their size.
   2981 	 */
   2982 	if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
   2983 		outusg(sym);
   2984 }
   2985 
   2986 /*
   2987  * Prints warnings for a list of variables and labels (concatenated
   2988  * with s_dlnxt) if these are not used or only set.
   2989  */
   2990 void
   2991 check_usage(dinfo_t *di)
   2992 {
   2993 	sym_t	*sym;
   2994 	int	mklwarn;
   2995 
   2996 	/* for this warning LINTED has no effect */
   2997 	mklwarn = lwarn;
   2998 	lwarn = LWARN_ALL;
   2999 
   3000 #ifdef DEBUG
   3001 	printf("%s, %d: >temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
   3002 	    lwarn);
   3003 #endif
   3004 	for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
   3005 		check_usage_sym(di->d_asm, sym);
   3006 	lwarn = mklwarn;
   3007 #ifdef DEBUG
   3008 	printf("%s, %d: <temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
   3009 	    lwarn);
   3010 #endif
   3011 }
   3012 
   3013 /*
   3014  * Prints a warning for a single variable or label if it is not used or
   3015  * only set.
   3016  */
   3017 void
   3018 check_usage_sym(bool novar, sym_t *sym)
   3019 {
   3020 	pos_t	cpos;
   3021 
   3022 	if (sym->s_block_level == -1)
   3023 		return;
   3024 
   3025 	cpos = curr_pos;
   3026 
   3027 	if (sym->s_kind == FVFT) {
   3028 		if (sym->s_arg) {
   3029 			check_argument_usage(novar, sym);
   3030 		} else {
   3031 			check_variable_usage(novar, sym);
   3032 		}
   3033 	} else if (sym->s_kind == FLABEL) {
   3034 		check_label_usage(sym);
   3035 	} else if (sym->s_kind == FTAG) {
   3036 		check_tag_usage(sym);
   3037 	}
   3038 
   3039 	curr_pos = cpos;
   3040 }
   3041 
   3042 static void
   3043 check_argument_usage(bool novar, sym_t *arg)
   3044 {
   3045 
   3046 	lint_assert(arg->s_set);
   3047 
   3048 	if (novar)
   3049 		return;
   3050 
   3051 	if (!arg->s_used && vflag) {
   3052 		curr_pos = arg->s_def_pos;
   3053 		/* argument %s unused in function %s */
   3054 		warning(231, arg->s_name, funcsym->s_name);
   3055 	}
   3056 }
   3057 
   3058 static void
   3059 check_variable_usage(bool novar, sym_t *sym)
   3060 {
   3061 	scl_t	sc;
   3062 	sym_t	*xsym;
   3063 
   3064 	lint_assert(block_level != 0);
   3065 	lint_assert(sym->s_block_level != 0);
   3066 
   3067 	/* errors in expressions easily cause lots of these warnings */
   3068 	if (nerr != 0)
   3069 		return;
   3070 
   3071 	/*
   3072 	 * XXX Only variables are checked, although types should
   3073 	 * probably also be checked
   3074 	 */
   3075 	if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
   3076 	    sc != AUTO && sc != REG) {
   3077 		return;
   3078 	}
   3079 
   3080 	if (novar)
   3081 		return;
   3082 
   3083 	if (sc == EXTERN) {
   3084 		if (!sym->s_used && !sym->s_set) {
   3085 			curr_pos = sym->s_def_pos;
   3086 			/* %s unused in function %s */
   3087 			warning(192, sym->s_name, funcsym->s_name);
   3088 		}
   3089 	} else {
   3090 		if (sym->s_set && !sym->s_used) {
   3091 			curr_pos = sym->s_set_pos;
   3092 			/* %s set but not used in function %s */
   3093 			warning(191, sym->s_name, funcsym->s_name);
   3094 		} else if (!sym->s_used) {
   3095 			curr_pos = sym->s_def_pos;
   3096 			/* %s unused in function %s */
   3097 			warning(192, sym->s_name, funcsym->s_name);
   3098 		}
   3099 	}
   3100 
   3101 	if (sc == EXTERN) {
   3102 		/*
   3103 		 * information about usage is taken over into the symbol
   3104 		 * table entry at level 0 if the symbol was locally declared
   3105 		 * as an external symbol.
   3106 		 *
   3107 		 * XXX This is wrong for symbols declared static at level 0
   3108 		 * if the usage information stems from sizeof(). This is
   3109 		 * because symbols at level 0 only used in sizeof() are
   3110 		 * considered to not be used.
   3111 		 */
   3112 		if ((xsym = sym->s_ext_sym) != NULL) {
   3113 			if (sym->s_used && !xsym->s_used) {
   3114 				xsym->s_used = true;
   3115 				xsym->s_use_pos = sym->s_use_pos;
   3116 			}
   3117 			if (sym->s_set && !xsym->s_set) {
   3118 				xsym->s_set = true;
   3119 				xsym->s_set_pos = sym->s_set_pos;
   3120 			}
   3121 		}
   3122 	}
   3123 }
   3124 
   3125 static void
   3126 check_label_usage(sym_t *lab)
   3127 {
   3128 
   3129 	lint_assert(block_level == 1);
   3130 	lint_assert(lab->s_block_level == 1);
   3131 
   3132 	if (lab->s_set && !lab->s_used) {
   3133 		curr_pos = lab->s_set_pos;
   3134 		/* label %s unused in function %s */
   3135 		warning(232, lab->s_name, funcsym->s_name);
   3136 	} else if (!lab->s_set) {
   3137 		curr_pos = lab->s_use_pos;
   3138 		/* undefined label %s */
   3139 		warning(23, lab->s_name);
   3140 	}
   3141 }
   3142 
   3143 static void
   3144 check_tag_usage(sym_t *sym)
   3145 {
   3146 
   3147 	if (!is_incomplete(sym->s_type))
   3148 		return;
   3149 
   3150 	/* always complain about incomplete tags declared inside blocks */
   3151 	if (!zflag || dcs->d_ctx != EXTERN)
   3152 		return;
   3153 
   3154 	curr_pos = sym->s_def_pos;
   3155 	switch (sym->s_type->t_tspec) {
   3156 	case STRUCT:
   3157 		/* struct %s never defined */
   3158 		warning(233, sym->s_name);
   3159 		break;
   3160 	case UNION:
   3161 		/* union %s never defined */
   3162 		warning(234, sym->s_name);
   3163 		break;
   3164 	case ENUM:
   3165 		/* enum %s never defined */
   3166 		warning(235, sym->s_name);
   3167 		break;
   3168 	default:
   3169 		lint_assert(/*CONSTCOND*/false);
   3170 	}
   3171 }
   3172 
   3173 /*
   3174  * Called after the entire translation unit has been parsed.
   3175  * Changes tentative definitions into definitions.
   3176  * Performs some tests on global symbols. Detected problems are:
   3177  * - defined variables of incomplete type
   3178  * - constant variables which are not initialized
   3179  * - static symbols which are never used
   3180  */
   3181 void
   3182 check_global_symbols(void)
   3183 {
   3184 	sym_t	*sym;
   3185 	pos_t	cpos;
   3186 
   3187 	if (block_level != 0 || dcs->d_next != NULL)
   3188 		norecover();
   3189 
   3190 	cpos = curr_pos;
   3191 
   3192 	for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
   3193 		if (sym->s_block_level == -1)
   3194 			continue;
   3195 		if (sym->s_kind == FVFT) {
   3196 			check_global_variable(sym);
   3197 		} else if (sym->s_kind == FTAG) {
   3198 			check_tag_usage(sym);
   3199 		} else {
   3200 			lint_assert(sym->s_kind == FMEMBER);
   3201 		}
   3202 	}
   3203 
   3204 	curr_pos = cpos;
   3205 }
   3206 
   3207 static void
   3208 check_unused_static_global_variable(const sym_t *sym)
   3209 {
   3210 	curr_pos = sym->s_def_pos;
   3211 	if (sym->s_type->t_tspec == FUNC) {
   3212 		if (sym->s_def == DEF) {
   3213 			if (!sym->s_inline)
   3214 				/* static function %s unused */
   3215 				warning(236, sym->s_name);
   3216 		} else {
   3217 			/* static function %s declared but not defined */
   3218 			warning(290, sym->s_name);
   3219 		}
   3220 	} else if (!sym->s_set) {
   3221 		/* static variable %s unused */
   3222 		warning(226, sym->s_name);
   3223 	} else {
   3224 		/* static variable %s set but not used */
   3225 		warning(307, sym->s_name);
   3226 	}
   3227 }
   3228 
   3229 static void
   3230 check_static_global_variable(const sym_t *sym)
   3231 {
   3232 	if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) {
   3233 		curr_pos = sym->s_use_pos;
   3234 		/* static function called but not defined: %s() */
   3235 		error(225, sym->s_name);
   3236 	}
   3237 
   3238 	if (!sym->s_used)
   3239 		check_unused_static_global_variable(sym);
   3240 
   3241 	if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
   3242 		curr_pos = sym->s_def_pos;
   3243 		/* const object %s should have initializer */
   3244 		warning(227, sym->s_name);
   3245 	}
   3246 }
   3247 
   3248 static void
   3249 check_global_variable(const sym_t *sym)
   3250 {
   3251 
   3252 	if (sym->s_scl == TYPEDEF || sym->s_scl == CTCONST)
   3253 		return;
   3254 
   3255 	if (sym->s_scl == NOSCL)
   3256 		return;		/* May be caused by a syntax error. */
   3257 
   3258 	lint_assert(sym->s_scl == EXTERN || sym->s_scl == STATIC);
   3259 
   3260 	check_global_variable_size(sym);
   3261 
   3262 	if (sym->s_scl == STATIC)
   3263 		check_static_global_variable(sym);
   3264 }
   3265 
   3266 static void
   3267 check_global_variable_size(const sym_t *sym)
   3268 {
   3269 
   3270 	if (sym->s_def != TDEF)
   3271 		return;
   3272 	if (sym->s_type->t_tspec == FUNC)
   3273 		/*
   3274 		 * this can happen if a syntax error occurred after a
   3275 		 * function declaration
   3276 		 */
   3277 		return;
   3278 
   3279 	curr_pos = sym->s_def_pos;
   3280 	if (length(sym->s_type, sym->s_name) == 0 &&
   3281 	    sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
   3282 		if (tflag || (sym->s_scl == EXTERN && !sflag)) {
   3283 			/* empty array declaration: %s */
   3284 			warning(190, sym->s_name);
   3285 		} else {
   3286 			/* empty array declaration: %s */
   3287 			error(190, sym->s_name);
   3288 		}
   3289 	}
   3290 }
   3291 
   3292 /*
   3293  * Prints information about location of previous definition/declaration.
   3294  */
   3295 void
   3296 print_previous_declaration(int msg, const sym_t *psym)
   3297 {
   3298 	pos_t	cpos;
   3299 
   3300 	if (!rflag)
   3301 		return;
   3302 
   3303 	cpos = curr_pos;
   3304 	curr_pos = psym->s_def_pos;
   3305 	if (msg != -1) {
   3306 		(message)(msg);
   3307 	} else if (psym->s_def == DEF || psym->s_def == TDEF) {
   3308 		/* previous definition of %s */
   3309 		message(261, psym->s_name);
   3310 	} else {
   3311 		/* previous declaration of %s */
   3312 		message(260, psym->s_name);
   3313 	}
   3314 	curr_pos = cpos;
   3315 }
   3316 
   3317 /*
   3318  * Gets a node for a constant and returns the value of this constant
   3319  * as integer.
   3320  *
   3321  * If the node is not constant or too large for int or of type float,
   3322  * a warning will be printed.
   3323  *
   3324  * to_int_constant() should be used only inside declarations. If it is used in
   3325  * expressions, it frees the memory used for the expression.
   3326  */
   3327 int
   3328 to_int_constant(tnode_t *tn, bool required)
   3329 {
   3330 	int	i;
   3331 	tspec_t	t;
   3332 	val_t	*v;
   3333 
   3334 	v = constant(tn, required);
   3335 
   3336 	if (tn == NULL) {
   3337 		i = 1;
   3338 		goto done;
   3339 	}
   3340 
   3341 	/*
   3342 	 * Abstract declarations are used inside expression. To free
   3343 	 * the memory would be a fatal error.
   3344 	 * We don't free blocks that are inside casts because these
   3345 	 * will be used later to match types.
   3346 	 */
   3347 	if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT)
   3348 		tfreeblk();
   3349 
   3350 	if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) {
   3351 		i = (int)v->v_ldbl;
   3352 		/* integral constant expression expected */
   3353 		error(55);
   3354 	} else {
   3355 		i = (int)v->v_quad;
   3356 		if (is_uinteger(t)) {
   3357 			if ((uint64_t)v->v_quad > (uint64_t)TARG_INT_MAX) {
   3358 				/* integral constant too large */
   3359 				warning(56);
   3360 			}
   3361 		} else {
   3362 			if (v->v_quad > (int64_t)TARG_INT_MAX ||
   3363 			    v->v_quad < (int64_t)TARG_INT_MIN) {
   3364 				/* integral constant too large */
   3365 				warning(56);
   3366 			}
   3367 		}
   3368 	}
   3369 
   3370 done:
   3371 	free(v);
   3372 	return i;
   3373 }
   3374