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