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