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