Home | History | Annotate | Line # | Download | only in lint1
decl.c revision 1.29
      1 /* $NetBSD: decl.c,v 1.29 2002/01/18 21:01:39 thorpej 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 #ifndef lint
     37 __RCSID("$NetBSD: decl.c,v 1.29 2002/01/18 21:01:39 thorpej 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() 1");
    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)gnuism(265, tflag ? "traditional" : "ANSI");
    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() 1");
    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() 1");
    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() 2");
    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() 3");
    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() 1");
    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() 2");
    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() 3");
    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() 4");
    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() 1");
    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() 1");
    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() 2");
    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 					/*
    994 					 * bit-field type '%s' invalid in
    995 					 * ANSI C
    996 					 */
    997 					warning(273, tyname(tp));
    998 				} else if (pflag) {
    999 					/* nonportable bit-field type */
   1000 					warning(34);
   1001 				}
   1002 			}
   1003 		} else if (t == INT && dcs->d_smod == NOTSPEC) {
   1004 			if (pflag && bitfieldtype_ok == 0) {
   1005 				/* nonportable bit-field type */
   1006 				warning(34);
   1007 			}
   1008 		} else if (t != INT && t != UINT) {
   1009 			/*
   1010 			 * Non-integer types are always illegal for
   1011 			 * bitfields, regardless of BITFIELDTYPE.
   1012 			 * Integer types not dealt with above are
   1013 			 * okay only if BITFIELDTYPE is in effect.
   1014 			 */
   1015 			if (bitfieldtype_ok == 0 || isityp(t) == 0) {
   1016 				/* illegal bit-field type */
   1017 				error(35);
   1018 				sz = tp->t_flen;
   1019 				dsym->s_type = tp = duptyp(gettyp(t = INT));
   1020 				if ((tp->t_flen = sz) > size(t))
   1021 					tp->t_flen = size(t);
   1022 			}
   1023 		}
   1024 		if ((len = tp->t_flen) < 0 || len > size(t)) {
   1025 			/* illegal bit-field size */
   1026 			error(36);
   1027 			tp->t_flen = size(t);
   1028 		} else if (len == 0 && dsym->s_name != unnamed) {
   1029 			/* zero size bit-field */
   1030 			error(37);
   1031 			tp->t_flen = size(t);
   1032 		}
   1033 		if (dsym->s_scl == MOU) {
   1034 			/* illegal use of bit-field */
   1035 			error(41);
   1036 			dsym->s_type->t_isfield = 0;
   1037 			dsym->s_field = 0;
   1038 		}
   1039 	} else if (t == FUNC) {
   1040 		/* function illegal in structure or union */
   1041 		error(38);
   1042 		dsym->s_type = tp = incref(tp, t = PTR);
   1043 	}
   1044 
   1045 	/*
   1046 	 * bit-fields of length 0 are not warned about because length()
   1047 	 * does not return the length of the bit-field but the length
   1048 	 * of the type the bit-field is packed in (its ok)
   1049 	 */
   1050 	if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
   1051 		if (t == ARRAY && dsym->s_type->t_dim == 0) {
   1052 			/* illegal zero sized structure member: %s */
   1053 			warning(39, dsym->s_name);
   1054 		}
   1055 	}
   1056 
   1057 	if (dcs->d_ctx == MOU) {
   1058 		o = dcs->d_offset;
   1059 		dcs->d_offset = 0;
   1060 	}
   1061 	if (dsym->s_field) {
   1062 		align(getbound(tp), tp->t_flen);
   1063 		dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t);
   1064 		tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
   1065 		dcs->d_offset += tp->t_flen;
   1066 	} else {
   1067 		align(getbound(tp), 0);
   1068 		dsym->s_value.v_quad = dcs->d_offset;
   1069 		dcs->d_offset += sz;
   1070 	}
   1071 	if (dcs->d_ctx == MOU) {
   1072 		if (o > dcs->d_offset)
   1073 			dcs->d_offset = o;
   1074 	}
   1075 
   1076 	chkfdef(dsym, 0);
   1077 
   1078 	/*
   1079 	 * Clear the BITFIELDTYPE indicator after processing each
   1080 	 * structure element.
   1081 	 */
   1082 	bitfieldtype_ok = 0;
   1083 
   1084 	return (dsym);
   1085 }
   1086 
   1087 /*
   1088  * Aligns next structure element as required.
   1089  *
   1090  * al contains the required alignment, len the length of a bit-field.
   1091  */
   1092 static void
   1093 align(int al, int len)
   1094 {
   1095 	int	no;
   1096 
   1097 	/*
   1098 	 * The alignment of the current element becomes the alignment of
   1099 	 * the struct/union if it is larger than the current alignment
   1100 	 * of the struct/union.
   1101 	 */
   1102 	if (al > dcs->d_stralign)
   1103 		dcs->d_stralign = al;
   1104 
   1105 	no = (dcs->d_offset + (al - 1)) & ~(al - 1);
   1106 	if (len == 0 || dcs->d_offset + len > no)
   1107 		dcs->d_offset = no;
   1108 }
   1109 
   1110 /*
   1111  * Remember the width of the field in its type structure.
   1112  */
   1113 sym_t *
   1114 bitfield(sym_t *dsym, int len)
   1115 {
   1116 
   1117 	if (dsym == NULL) {
   1118 		dsym = getblk(sizeof (sym_t));
   1119 		dsym->s_name = unnamed;
   1120 		dsym->s_kind = FMOS;
   1121 		dsym->s_scl = MOS;
   1122 		dsym->s_type = gettyp(UINT);
   1123 		dsym->s_blklev = -1;
   1124 	}
   1125 	dsym->s_type = duptyp(dsym->s_type);
   1126 	dsym->s_type->t_isfield = 1;
   1127 	dsym->s_type->t_flen = len;
   1128 	dsym->s_field = 1;
   1129 	return (dsym);
   1130 }
   1131 
   1132 /*
   1133  * Collect informations about a sequence of asterisks and qualifiers
   1134  * in a list of type pqinf_t.
   1135  * Qualifiers refer always to the left asterisk. The rightmost asterisk
   1136  * will be at the top of the list.
   1137  */
   1138 pqinf_t *
   1139 mergepq(pqinf_t *p1, pqinf_t *p2)
   1140 {
   1141 	pqinf_t	*p;
   1142 
   1143 	if (p2->p_pcnt != 0) {
   1144 		/* left '*' at the end of the list */
   1145 		for (p = p2; p->p_nxt != NULL; p = p->p_nxt)
   1146 			continue;
   1147 		p->p_nxt = p1;
   1148 		return (p2);
   1149 	} else {
   1150 		if (p2->p_const) {
   1151 			if (p1->p_const) {
   1152 				/* duplicate %s */
   1153 				warning(10, "const");
   1154 			}
   1155 			p1->p_const = 1;
   1156 		}
   1157 		if (p2->p_volatile) {
   1158 			if (p1->p_volatile) {
   1159 				/* duplicate %s */
   1160 				warning(10, "volatile");
   1161 			}
   1162 			p1->p_volatile = 1;
   1163 		}
   1164 		free(p2);
   1165 		return (p1);
   1166 	}
   1167 }
   1168 
   1169 /*
   1170  * Followint 3 functions extend the type of a declarator with
   1171  * pointer, function and array types.
   1172  *
   1173  * The current type is the Type built by deftyp() (dcs->d_type) and
   1174  * pointer, function and array types already added for this
   1175  * declarator. The new type extension is inserted between both.
   1176  */
   1177 sym_t *
   1178 addptr(sym_t *decl, pqinf_t *pi)
   1179 {
   1180 	type_t	**tpp, *tp;
   1181 	pqinf_t	*npi;
   1182 
   1183 	tpp = &decl->s_type;
   1184 	while (*tpp && *tpp != dcs->d_type)
   1185 		tpp = &(*tpp)->t_subt;
   1186 	if (*tpp == NULL)
   1187 		return decl;
   1188 
   1189 	while (pi != NULL) {
   1190 		*tpp = tp = getblk(sizeof (type_t));
   1191 		tp->t_tspec = PTR;
   1192 		tp->t_const = pi->p_const;
   1193 		tp->t_volatile = pi->p_volatile;
   1194 		*(tpp = &tp->t_subt) = dcs->d_type;
   1195 		npi = pi->p_nxt;
   1196 		free(pi);
   1197 		pi = npi;
   1198 	}
   1199 	return (decl);
   1200 }
   1201 
   1202 /*
   1203  * If a dimension was specified, dim is 1, otherwise 0
   1204  * n is the specified dimension
   1205  */
   1206 sym_t *
   1207 addarray(sym_t *decl, int dim, int n)
   1208 {
   1209 	type_t	**tpp, *tp;
   1210 
   1211 	tpp = &decl->s_type;
   1212 	while (*tpp && *tpp != dcs->d_type)
   1213 		tpp = &(*tpp)->t_subt;
   1214 	if (*tpp == NULL)
   1215 	    return decl;
   1216 
   1217 	*tpp = tp = getblk(sizeof (type_t));
   1218 	tp->t_tspec = ARRAY;
   1219 	tp->t_subt = dcs->d_type;
   1220 	tp->t_dim = n;
   1221 
   1222 	if (n < 0) {
   1223 		/* zero or negative array dimension */
   1224 		error(20);
   1225 		n = 0;
   1226 	} else if (n == 0 && dim) {
   1227 		/* zero or negative array dimension */
   1228 		warning(20);
   1229 	} else if (n == 0 && !dim) {
   1230 		/* is incomplete type */
   1231 		setcompl(tp, 1);
   1232 	}
   1233 
   1234 	return (decl);
   1235 }
   1236 
   1237 sym_t *
   1238 addfunc(sym_t *decl, sym_t *args)
   1239 {
   1240 	type_t	**tpp, *tp;
   1241 
   1242 	if (dcs->d_proto) {
   1243 		if (tflag)
   1244 			/* function prototypes are illegal in traditional C */
   1245 			warning(270);
   1246 		args = nsfunc(decl, args);
   1247 	} else {
   1248 		osfunc(decl, args);
   1249 	}
   1250 
   1251 	/*
   1252 	 * The symbols are removed from the symbol table by popdecl() after
   1253 	 * addfunc(). To be able to restore them if this is a function
   1254 	 * definition, a pointer to the list of all symbols is stored in
   1255 	 * dcs->d_nxt->d_fpsyms. Also a list of the arguments (concatenated
   1256 	 * by s_nxt) is stored in dcs->d_nxt->d_fargs.
   1257 	 * (dcs->d_nxt must be used because *dcs is the declaration stack
   1258 	 * element created for the list of params and is removed after
   1259 	 * addfunc())
   1260 	 */
   1261 	if (dcs->d_nxt->d_ctx == EXTERN &&
   1262 	    decl->s_type == dcs->d_nxt->d_type) {
   1263 		dcs->d_nxt->d_fpsyms = dcs->d_dlsyms;
   1264 		dcs->d_nxt->d_fargs = args;
   1265 	}
   1266 
   1267 	tpp = &decl->s_type;
   1268 	while (*tpp && *tpp != dcs->d_nxt->d_type)
   1269 		tpp = &(*tpp)->t_subt;
   1270 	if (*tpp == NULL)
   1271 	    return decl;
   1272 
   1273 	*tpp = tp = getblk(sizeof (type_t));
   1274 	tp->t_tspec = FUNC;
   1275 	tp->t_subt = dcs->d_nxt->d_type;
   1276 	if ((tp->t_proto = dcs->d_proto) != 0)
   1277 		tp->t_args = args;
   1278 	tp->t_vararg = dcs->d_vararg;
   1279 
   1280 	return (decl);
   1281 }
   1282 
   1283 /*
   1284  * Called for new style function declarations.
   1285  */
   1286 /* ARGSUSED */
   1287 static sym_t *
   1288 nsfunc(sym_t *decl, sym_t *args)
   1289 {
   1290 	sym_t	*arg, *sym;
   1291 	scl_t	sc;
   1292 	int	n;
   1293 
   1294 	/*
   1295 	 * Declarations of structs/unions/enums in param lists are legal,
   1296 	 * but senseless.
   1297 	 */
   1298 	for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
   1299 		sc = sym->s_scl;
   1300 		if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) {
   1301 			/* dubious tag declaration: %s %s */
   1302 			warning(85, scltoa(sc), sym->s_name);
   1303 		}
   1304 	}
   1305 
   1306 	n = 1;
   1307 	for (arg = args; arg != NULL; arg = arg->s_nxt) {
   1308 		if (arg->s_type->t_tspec == VOID) {
   1309 			if (n > 1 || arg->s_nxt != NULL) {
   1310 				/* "void" must be sole parameter */
   1311 				error(60);
   1312 				arg->s_type = gettyp(INT);
   1313 			}
   1314 		}
   1315 		n++;
   1316 	}
   1317 
   1318 	/* return NULL if first param is VOID */
   1319 	return (args != NULL && args->s_type->t_tspec != VOID ? args : NULL);
   1320 }
   1321 
   1322 /*
   1323  * Called for old style function declarations.
   1324  */
   1325 static void
   1326 osfunc(sym_t *decl, sym_t *args)
   1327 {
   1328 
   1329 	/*
   1330 	 * Remember list of params only if this is really seams to be
   1331 	 * a function definition.
   1332 	 */
   1333 	if (dcs->d_nxt->d_ctx == EXTERN &&
   1334 	    decl->s_type == dcs->d_nxt->d_type) {
   1335 		/*
   1336 		 * We assume that this becomes a function definition. If
   1337 		 * we are wrong, its corrected in chkfdef().
   1338 		 */
   1339 		if (args != NULL) {
   1340 			decl->s_osdef = 1;
   1341 			decl->s_args = args;
   1342 		}
   1343 	} else {
   1344 		if (args != NULL)
   1345 			/* function prototype parameters must have types */
   1346 			warning(62);
   1347 	}
   1348 }
   1349 
   1350 /*
   1351  * Lists of Identifiers in functions declarations are allowed only if
   1352  * its also a function definition. If this is not the case, print a
   1353  * error message.
   1354  */
   1355 void
   1356 chkfdef(sym_t *sym, int msg)
   1357 {
   1358 
   1359 	if (sym->s_osdef) {
   1360 		if (msg) {
   1361 			/* incomplete or misplaced function definition */
   1362 			error(22);
   1363 		}
   1364 		sym->s_osdef = 0;
   1365 		sym->s_args = NULL;
   1366 	}
   1367 }
   1368 
   1369 /*
   1370  * Process the name in a declarator.
   1371  * If the symbol does already exists, a new one is created.
   1372  * The symbol becomes one of the storage classes EXTERN, STATIC, AUTO or
   1373  * TYPEDEF.
   1374  * s_def and s_reg are valid after dname().
   1375  */
   1376 sym_t *
   1377 dname(sym_t *sym)
   1378 {
   1379 	scl_t	sc = NOSCL;
   1380 
   1381 	if (sym->s_scl == NOSCL) {
   1382 		dcs->d_rdcsym = NULL;
   1383 	} else if (sym->s_defarg) {
   1384 		sym->s_defarg = 0;
   1385 		dcs->d_rdcsym = NULL;
   1386 	} else {
   1387 		dcs->d_rdcsym = sym;
   1388 		sym = pushdown(sym);
   1389 	}
   1390 
   1391 	switch (dcs->d_ctx) {
   1392 	case MOS:
   1393 	case MOU:
   1394 		/* Parent setzen */
   1395 		sym->s_styp = dcs->d_tagtyp->t_str;
   1396 		sym->s_def = DEF;
   1397 		sym->s_value.v_tspec = INT;
   1398 		sc = dcs->d_ctx;
   1399 		break;
   1400 	case EXTERN:
   1401 		/*
   1402 		 * static and external symbols without "extern" are
   1403 		 * considered to be tentative defined, external
   1404 		 * symbols with "extern" are declared, and typedef names
   1405 		 * are defined. Tentative defined and declared symbols
   1406 		 * may become defined if an initializer is present or
   1407 		 * this is a function definition.
   1408 		 */
   1409 		if ((sc = dcs->d_scl) == NOSCL) {
   1410 			sc = EXTERN;
   1411 			sym->s_def = TDEF;
   1412 		} else if (sc == STATIC) {
   1413 			sym->s_def = TDEF;
   1414 		} else if (sc == TYPEDEF) {
   1415 			sym->s_def = DEF;
   1416 		} else if (sc == EXTERN) {
   1417 			sym->s_def = DECL;
   1418 		} else {
   1419 			lerror("dname() 1");
   1420 		}
   1421 		break;
   1422 	case PARG:
   1423 		sym->s_arg = 1;
   1424 		/* FALLTHROUGH */
   1425 	case ARG:
   1426 		if ((sc = dcs->d_scl) == NOSCL) {
   1427 			sc = AUTO;
   1428 		} else if (sc == REG) {
   1429 			sym->s_reg = 1;
   1430 			sc = AUTO;
   1431 		} else {
   1432 			lerror("dname() 2");
   1433 		}
   1434 		sym->s_def = DEF;
   1435 		break;
   1436 	case AUTO:
   1437 		if ((sc = dcs->d_scl) == NOSCL) {
   1438 			/*
   1439 			 * XXX somewhat ugly because we dont know whether
   1440 			 * this is AUTO or EXTERN (functions). If we are
   1441 			 * wrong it must be corrected in decl1loc(), where
   1442 			 * we have the necessary type information.
   1443 			 */
   1444 			sc = AUTO;
   1445 			sym->s_def = DEF;
   1446 		} else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
   1447 			sym->s_def = DEF;
   1448 		} else if (sc == REG) {
   1449 			sym->s_reg = 1;
   1450 			sc = AUTO;
   1451 			sym->s_def = DEF;
   1452 		} else if (sc == EXTERN) {
   1453 			sym->s_def = DECL;
   1454 		} else {
   1455 			lerror("dname() 3");
   1456 		}
   1457 		break;
   1458 	default:
   1459 		lerror("dname() 4");
   1460 	}
   1461 	sym->s_scl = sc;
   1462 
   1463 	sym->s_type = dcs->d_type;
   1464 
   1465 	dcs->d_fpsyms = NULL;
   1466 
   1467 	return (sym);
   1468 }
   1469 
   1470 /*
   1471  * Process a name in the list of formal params in an old style function
   1472  * definition.
   1473  */
   1474 sym_t *
   1475 iname(sym_t *sym)
   1476 {
   1477 
   1478 	if (sym->s_scl != NOSCL) {
   1479 		if (blklev == sym->s_blklev) {
   1480 			/* redeclaration of formal parameter %s */
   1481 			error(21, sym->s_name);
   1482 			if (!sym->s_defarg)
   1483 				lerror("iname()");
   1484 		}
   1485 		sym = pushdown(sym);
   1486 	}
   1487 	sym->s_type = gettyp(INT);
   1488 	sym->s_scl = AUTO;
   1489 	sym->s_def = DEF;
   1490 	sym->s_defarg = sym->s_arg = 1;
   1491 	return (sym);
   1492 }
   1493 
   1494 /*
   1495  * Create the type of a tag.
   1496  *
   1497  * tag points to the symbol table entry of the tag
   1498  * kind is the kind of the tag (STRUCT/UNION/ENUM)
   1499  * decl is 1 if the type of the tag will be completed in this declaration
   1500  * (the following token is T_LBRACE)
   1501  * semi is 1 if the following token is T_SEMI
   1502  */
   1503 type_t *
   1504 mktag(sym_t *tag, tspec_t kind, int decl, int semi)
   1505 {
   1506 	scl_t	scl = NOSCL;
   1507 	type_t	*tp;
   1508 
   1509 	if (kind == STRUCT) {
   1510 		scl = STRTAG;
   1511 	} else if (kind == UNION) {
   1512 		scl = UNIONTAG;
   1513 	} else if (kind == ENUM) {
   1514 		scl = ENUMTAG;
   1515 	} else {
   1516 		lerror("mktag()");
   1517 	}
   1518 
   1519 	if (tag != NULL) {
   1520 		if (tag->s_scl != NOSCL) {
   1521 			tag = newtag(tag, scl, decl, semi);
   1522 		} else {
   1523 			/* a new tag, no empty declaration */
   1524 			dcs->d_nxt->d_nedecl = 1;
   1525 			if (scl == ENUMTAG && !decl) {
   1526 				if (!tflag && (sflag || pflag))
   1527 					/* forward reference to enum type */
   1528 					warning(42);
   1529 			}
   1530 		}
   1531 		if (tag->s_scl == NOSCL) {
   1532 			tag->s_scl = scl;
   1533 			tag->s_type = tp = getblk(sizeof (type_t));
   1534 		} else {
   1535 			tp = tag->s_type;
   1536 		}
   1537 	} else {
   1538 		tag = getblk(sizeof (sym_t));
   1539 		tag->s_name = unnamed;
   1540 		UNIQUE_CURR_POS(tag->s_dpos);
   1541 		tag->s_kind = FTAG;
   1542 		tag->s_scl = scl;
   1543 		tag->s_blklev = -1;
   1544 		tag->s_type = tp = getblk(sizeof (type_t));
   1545 		dcs->d_nxt->d_nedecl = 1;
   1546 	}
   1547 
   1548 	if (tp->t_tspec == NOTSPEC) {
   1549 		tp->t_tspec = kind;
   1550 		if (kind != ENUM) {
   1551 			tp->t_str = getblk(sizeof (str_t));
   1552 			tp->t_str->align = CHAR_BIT;
   1553 			tp->t_str->stag = tag;
   1554 		} else {
   1555 			tp->t_isenum = 1;
   1556 			tp->t_enum = getblk(sizeof (enum_t));
   1557 			tp->t_enum->etag = tag;
   1558 		}
   1559 		/* ist unvollstaendiger Typ */
   1560 		setcompl(tp, 1);
   1561 	}
   1562 
   1563 	return (tp);
   1564 }
   1565 
   1566 /*
   1567  * Checks all possible cases of tag redeclarations.
   1568  * decl is 1 if T_LBRACE follows
   1569  * semi is 1 if T_SEMI follows
   1570  */
   1571 static sym_t *
   1572 newtag(sym_t *tag, scl_t scl, int decl, int semi)
   1573 {
   1574 
   1575 	if (tag->s_blklev < blklev) {
   1576 		if (semi) {
   1577 			/* "struct a;" */
   1578 			if (!tflag) {
   1579 				if (!sflag)
   1580 					/* decl. introduces new type ... */
   1581 					warning(44, scltoa(scl), tag->s_name);
   1582 				tag = pushdown(tag);
   1583 			} else if (tag->s_scl != scl) {
   1584 				/* base type is really "%s %s" */
   1585 				warning(45, scltoa(tag->s_scl), tag->s_name);
   1586 			}
   1587 			dcs->d_nxt->d_nedecl = 1;
   1588 		} else if (decl) {
   1589 			/* "struct a { ... } " */
   1590 			if (hflag)
   1591 				/* redefinition hides earlier one: %s */
   1592 				warning(43, tag->s_name);
   1593 			tag = pushdown(tag);
   1594 			dcs->d_nxt->d_nedecl = 1;
   1595 		} else if (tag->s_scl != scl) {
   1596 			/* base type is really "%s %s" */
   1597 			warning(45, scltoa(tag->s_scl), tag->s_name);
   1598 			/* declaration introduces new type in ANSI C: %s %s */
   1599 			if (!sflag)
   1600 				warning(44, scltoa(scl), tag->s_name);
   1601 			tag = pushdown(tag);
   1602 			dcs->d_nxt->d_nedecl = 1;
   1603 		}
   1604 	} else {
   1605 		if (tag->s_scl != scl) {
   1606 			/* (%s) tag redeclared */
   1607 			error(46, scltoa(tag->s_scl));
   1608 			prevdecl(-1, tag);
   1609 			tag = pushdown(tag);
   1610 			dcs->d_nxt->d_nedecl = 1;
   1611 		} else if (decl && !incompl(tag->s_type)) {
   1612 			/* (%s) tag redeclared */
   1613 			error(46, scltoa(tag->s_scl));
   1614 			prevdecl(-1, tag);
   1615 			tag = pushdown(tag);
   1616 			dcs->d_nxt->d_nedecl = 1;
   1617 		} else if (semi || decl) {
   1618 			dcs->d_nxt->d_nedecl = 1;
   1619 		}
   1620 	}
   1621 	return (tag);
   1622 }
   1623 
   1624 const char *
   1625 scltoa(scl_t sc)
   1626 {
   1627 	const	char *s;
   1628 
   1629 	switch (sc) {
   1630 	case EXTERN:	s = "extern";	break;
   1631 	case STATIC:	s = "static";	break;
   1632 	case AUTO:	s = "auto";	break;
   1633 	case REG:	s = "register";	break;
   1634 	case TYPEDEF:	s = "typedef";	break;
   1635 	case STRTAG:	s = "struct";	break;
   1636 	case UNIONTAG:	s = "union";	break;
   1637 	case ENUMTAG:	s = "enum";	break;
   1638 	default:	lerror("tagttoa()");
   1639 	}
   1640 	return (s);
   1641 }
   1642 
   1643 /*
   1644  * Completes the type of a tag in a struct/union/enum declaration.
   1645  * tp points to the type of the, tag, fmem to the list of members/enums.
   1646  */
   1647 type_t *
   1648 compltag(type_t *tp, sym_t *fmem)
   1649 {
   1650 	tspec_t	t;
   1651 	str_t	*sp;
   1652 	int	n;
   1653 	sym_t	*mem;
   1654 
   1655 	/* from now a complete type */
   1656 	setcompl(tp, 0);
   1657 
   1658 	if ((t = tp->t_tspec) != ENUM) {
   1659 		align(dcs->d_stralign, 0);
   1660 		sp = tp->t_str;
   1661 		sp->align = dcs->d_stralign;
   1662 		sp->size = dcs->d_offset;
   1663 		sp->memb = fmem;
   1664 		if (sp->size == 0) {
   1665 			/* zero sized %s */
   1666 			(void)gnuism(47, ttab[t].tt_name);
   1667 		} else {
   1668 			n = 0;
   1669 			for (mem = fmem; mem != NULL; mem = mem->s_nxt) {
   1670 				if (mem->s_name != unnamed)
   1671 					n++;
   1672 			}
   1673 			if (n == 0) {
   1674 				/* %s has no named members */
   1675 				warning(65,
   1676 					t == STRUCT ? "structure" : "union");
   1677 			}
   1678 		}
   1679 	} else {
   1680 		tp->t_enum->elem = fmem;
   1681 	}
   1682 	return (tp);
   1683 }
   1684 
   1685 /*
   1686  * Processes the name of an enumerator in en enum declaration.
   1687  *
   1688  * sym points to the enumerator
   1689  * val is the value of the enumerator
   1690  * impl is 1 if the value of the enumerator was not explicit specified.
   1691  */
   1692 sym_t *
   1693 ename(sym_t *sym, int val, int impl)
   1694 {
   1695 
   1696 	if (sym->s_scl) {
   1697 		if (sym->s_blklev == blklev) {
   1698 			/* no hflag, because this is illegal!!! */
   1699 			if (sym->s_arg) {
   1700 				/* enumeration constant hides parameter: %s */
   1701 				warning(57, sym->s_name);
   1702 			} else {
   1703 				/* redeclaration of %s */
   1704 				error(27, sym->s_name);
   1705 				/*
   1706 				 * inside blocks it should not too complicated
   1707 				 * to find the position of the previous
   1708 				 * declaration
   1709 				 */
   1710 				if (blklev == 0)
   1711 					prevdecl(-1, sym);
   1712 			}
   1713 		} else {
   1714 			if (hflag)
   1715 				/* redefinition hides earlier one: %s */
   1716 				warning(43, sym->s_name);
   1717 		}
   1718 		sym = pushdown(sym);
   1719 	}
   1720 	sym->s_scl = ENUMCON;
   1721 	sym->s_type = dcs->d_tagtyp;
   1722 	sym->s_value.v_tspec = INT;
   1723 	sym->s_value.v_quad = val;
   1724 	if (impl && val - 1 == INT_MAX) {
   1725 		/* overflow in enumeration values: %s */
   1726 		warning(48, sym->s_name);
   1727 	}
   1728 	enumval = val + 1;
   1729 	return (sym);
   1730 }
   1731 
   1732 /*
   1733  * Process a single external declarator.
   1734  */
   1735 void
   1736 decl1ext(sym_t *dsym, int initflg)
   1737 {
   1738 	int	warn, rval, redec;
   1739 	sym_t	*rdsym;
   1740 
   1741 	chkfdef(dsym, 1);
   1742 
   1743 	chktyp(dsym);
   1744 
   1745 	if (initflg && !(initerr = chkinit(dsym)))
   1746 		dsym->s_def = DEF;
   1747 
   1748 	/*
   1749 	 * Declarations of functions are marked as "tentative" in dname().
   1750 	 * This is wrong because there are no tentative function
   1751 	 * definitions.
   1752 	 */
   1753 	if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
   1754 		dsym->s_def = DECL;
   1755 
   1756 	if (dcs->d_inline) {
   1757 		if (dsym->s_type->t_tspec == FUNC) {
   1758 			dsym->s_inline = 1;
   1759 		} else {
   1760 			/* variable declared inline: %s */
   1761 			warning(268, dsym->s_name);
   1762 		}
   1763 	}
   1764 
   1765 	/* Write the declaration into the output file */
   1766 	if (plibflg && llibflg &&
   1767 	    dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
   1768 		/*
   1769 		 * With both LINTLIBRARY and PROTOLIB the prototyp is
   1770 		 * written as a function definition to the output file.
   1771 		 */
   1772 		rval = dsym->s_type->t_subt->t_tspec != VOID;
   1773 		outfdef(dsym, &dsym->s_dpos, rval, 0, NULL);
   1774 	} else {
   1775 		outsym(dsym, dsym->s_scl, dsym->s_def);
   1776 	}
   1777 
   1778 	if ((rdsym = dcs->d_rdcsym) != NULL) {
   1779 
   1780 		/*
   1781 		 * If the old symbol stems from a old style function definition
   1782 		 * we have remembered the params in rdsmy->s_args and compare
   1783 		 * them with the params of the prototype.
   1784 		 */
   1785 		if (rdsym->s_osdef && dsym->s_type->t_proto) {
   1786 			redec = chkosdef(rdsym, dsym);
   1787 		} else {
   1788 			redec = 0;
   1789 		}
   1790 
   1791 		if (!redec && !isredec(dsym, (warn = 0, &warn))) {
   1792 
   1793 			if (warn) {
   1794 				/* redeclaration of %s */
   1795 				(*(sflag ? error : warning))(27, dsym->s_name);
   1796 				prevdecl(-1, rdsym);
   1797 			}
   1798 
   1799 			/*
   1800 			 * Overtake the rememberd params if the new symbol
   1801 			 * is not a prototype.
   1802 			 */
   1803 			if (rdsym->s_osdef && !dsym->s_type->t_proto) {
   1804 				dsym->s_osdef = rdsym->s_osdef;
   1805 				dsym->s_args = rdsym->s_args;
   1806 				STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
   1807 			}
   1808 
   1809 			/*
   1810 			 * Remember the position of the declaration if the
   1811 			 * old symbol was a prototype and the new is not.
   1812 			 * Also remember the position if the old symbol
   1813 			 * was defined and the new is not.
   1814 			 */
   1815 			if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
   1816 				STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
   1817 			} else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
   1818 				STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
   1819 			}
   1820 
   1821 			/*
   1822 			 * Copy informations about usage of the name into
   1823 			 * the new symbol.
   1824 			 */
   1825 			cpuinfo(dsym, rdsym);
   1826 
   1827 			/* Once a name is defined, it remains defined. */
   1828 			if (rdsym->s_def == DEF)
   1829 				dsym->s_def = DEF;
   1830 
   1831 			/* once a function is inline, it remains inline */
   1832 			if (rdsym->s_inline)
   1833 				dsym->s_inline = 1;
   1834 
   1835 			compltyp(dsym, rdsym);
   1836 
   1837 		}
   1838 
   1839 		rmsym(rdsym);
   1840 	}
   1841 
   1842 	if (dsym->s_scl == TYPEDEF) {
   1843 		dsym->s_type = duptyp(dsym->s_type);
   1844 		dsym->s_type->t_typedef = 1;
   1845 		settdsym(dsym->s_type, dsym);
   1846 	}
   1847 
   1848 }
   1849 
   1850 /*
   1851  * Copies informations about usage into a new symbol table entry of
   1852  * the same symbol.
   1853  */
   1854 void
   1855 cpuinfo(sym_t *sym, sym_t *rdsym)
   1856 {
   1857 
   1858 	sym->s_spos = rdsym->s_spos;
   1859 	sym->s_upos = rdsym->s_upos;
   1860 	sym->s_set = rdsym->s_set;
   1861 	sym->s_used = rdsym->s_used;
   1862 }
   1863 
   1864 /*
   1865  * Prints an error and returns 1 if a symbol is redeclared/redefined.
   1866  * Otherwise returns 0 and, in some cases of minor problems, prints
   1867  * a warning.
   1868  */
   1869 int
   1870 isredec(sym_t *dsym, int *warn)
   1871 {
   1872 	sym_t	*rsym;
   1873 
   1874 	if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
   1875 		/* redeclaration of %s */
   1876 		error(27, dsym->s_name);
   1877 		prevdecl(-1, rsym);
   1878 		return (1);
   1879 	}
   1880 	if (rsym->s_scl == TYPEDEF) {
   1881 		/* typedef redeclared: %s */
   1882 		error(89, dsym->s_name);
   1883 		prevdecl(-1, rsym);
   1884 		return (1);
   1885 	}
   1886 	if (dsym->s_scl == TYPEDEF) {
   1887 		/* redeclaration of %s */
   1888 		error(27, dsym->s_name);
   1889 		prevdecl(-1, rsym);
   1890 		return (1);
   1891 	}
   1892 	if (rsym->s_def == DEF && dsym->s_def == DEF) {
   1893 		/* redefinition of %s */
   1894 		error(28, dsym->s_name);
   1895 		prevdecl(-1, rsym);
   1896 		return(1);
   1897 	}
   1898 	if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, warn)) {
   1899 		/* redeclaration of %s */
   1900 		error(27, dsym->s_name);
   1901 		prevdecl(-1, rsym);
   1902 		return(1);
   1903 	}
   1904 	if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
   1905 		return(0);
   1906 	if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
   1907 		return(0);
   1908 	if (rsym->s_scl == STATIC && dsym->s_def == DECL)
   1909 		return(0);
   1910 	if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
   1911 		/*
   1912 		 * All cases except "int a = 1; static int a;" are catched
   1913 		 * above with or without a warning
   1914 		 */
   1915 		/* redeclaration of %s */
   1916 		error(27, dsym->s_name);
   1917 		prevdecl(-1, rsym);
   1918 		return(1);
   1919 	}
   1920 	if (rsym->s_scl == EXTERN) {
   1921 		/* previously declared extern, becomes static: %s */
   1922 		warning(29, dsym->s_name);
   1923 		prevdecl(-1, rsym);
   1924 		return(0);
   1925 	}
   1926 	/*
   1927 	 * Now its on of:
   1928 	 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
   1929 	 */
   1930 	/* redeclaration of %s; ANSI C requires "static" */
   1931 	if (sflag) {
   1932 		warning(30, dsym->s_name);
   1933 		prevdecl(-1, rsym);
   1934 	}
   1935 	dsym->s_scl = STATIC;
   1936 	return (0);
   1937 }
   1938 
   1939 /*
   1940  * Checks if two types are compatible. Returns 0 if not, otherwise 1.
   1941  *
   1942  * ignqual	ignore qualifiers of type; used for function params
   1943  * promot	promote left type; used for comparison of params of
   1944  *		old style function definitions with params of prototypes.
   1945  * *warn	set to 1 if an old style function declaration is not
   1946  *		compatible with a prototype
   1947  */
   1948 int
   1949 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *warn)
   1950 {
   1951 	tspec_t	t;
   1952 
   1953 	while (tp1 != NULL && tp2 != NULL) {
   1954 
   1955 		t = tp1->t_tspec;
   1956 		if (promot) {
   1957 			if (t == FLOAT) {
   1958 				t = DOUBLE;
   1959 			} else if (t == CHAR || t == SCHAR) {
   1960 				t = INT;
   1961 			} else if (t == UCHAR) {
   1962 				t = tflag ? UINT : INT;
   1963 			} else if (t == SHORT) {
   1964 				t = INT;
   1965 			} else if (t == USHORT) {
   1966 				/* CONSTCOND */
   1967 				t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
   1968 			}
   1969 		}
   1970 
   1971 		if (t != tp2->t_tspec)
   1972 			return (0);
   1973 
   1974 		if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
   1975 			return (0);
   1976 
   1977 		if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
   1978 			return (0);
   1979 
   1980 		if (t == STRUCT || t == UNION)
   1981 			return (tp1->t_str == tp2->t_str);
   1982 
   1983 		if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
   1984 			if (tp1->t_dim != 0 && tp2->t_dim != 0)
   1985 				return (0);
   1986 		}
   1987 
   1988 		/* dont check prototypes for traditional */
   1989 		if (t == FUNC && !tflag) {
   1990 			if (tp1->t_proto && tp2->t_proto) {
   1991 				if (!eqargs(tp1, tp2, warn))
   1992 					return (0);
   1993 			} else if (tp1->t_proto) {
   1994 				if (!mnoarg(tp1, warn))
   1995 					return (0);
   1996 			} else if (tp2->t_proto) {
   1997 				if (!mnoarg(tp2, warn))
   1998 					return (0);
   1999 			}
   2000 		}
   2001 
   2002 		tp1 = tp1->t_subt;
   2003 		tp2 = tp2->t_subt;
   2004 		ignqual = promot = 0;
   2005 
   2006 	}
   2007 
   2008 	return (tp1 == tp2);
   2009 }
   2010 
   2011 /*
   2012  * Compares the parameter types of two prototypes.
   2013  */
   2014 static int
   2015 eqargs(type_t *tp1, type_t *tp2, int *warn)
   2016 {
   2017 	sym_t	*a1, *a2;
   2018 
   2019 	if (tp1->t_vararg != tp2->t_vararg)
   2020 		return (0);
   2021 
   2022 	a1 = tp1->t_args;
   2023 	a2 = tp2->t_args;
   2024 
   2025 	while (a1 != NULL && a2 != NULL) {
   2026 
   2027 		if (eqtype(a1->s_type, a2->s_type, 1, 0, warn) == 0)
   2028 			return (0);
   2029 
   2030 		a1 = a1->s_nxt;
   2031 		a2 = a2->s_nxt;
   2032 
   2033 	}
   2034 
   2035 	return (a1 == a2);
   2036 }
   2037 
   2038 /*
   2039  * mnoarg() (matches functions with no argument type information)
   2040  * returns 1 if all parameters of a prototype are compatible with
   2041  * and old style function declaration.
   2042  * This is the case if following conditions are met:
   2043  *	1. the prototype must have a fixed number of parameters
   2044  *	2. no parameter is of type float
   2045  *	3. no parameter is converted to another type if integer promotion
   2046  *	   is applied on it
   2047  */
   2048 static int
   2049 mnoarg(type_t *tp, int *warn)
   2050 {
   2051 	sym_t	*arg;
   2052 	tspec_t	t;
   2053 
   2054 	if (tp->t_vararg) {
   2055 		if (warn != NULL)
   2056 			*warn = 1;
   2057 	}
   2058 	for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) {
   2059 		if ((t = arg->s_type->t_tspec) == FLOAT ||
   2060 		    t == CHAR || t == SCHAR || t == UCHAR ||
   2061 		    t == SHORT || t == USHORT) {
   2062 			if (warn != NULL)
   2063 				*warn = 1;
   2064 		}
   2065 	}
   2066 	return (1);
   2067 }
   2068 
   2069 /*
   2070  * Compares a prototype declaration with the remembered arguments of
   2071  * a previous old style function definition.
   2072  */
   2073 static int
   2074 chkosdef(sym_t *rdsym, sym_t *dsym)
   2075 {
   2076 	sym_t	*args, *pargs, *arg, *parg;
   2077 	int	narg, nparg, n;
   2078 	int	warn, msg;
   2079 
   2080 	args = rdsym->s_args;
   2081 	pargs = dsym->s_type->t_args;
   2082 
   2083 	msg = 0;
   2084 
   2085 	narg = nparg = 0;
   2086 	for (arg = args; arg != NULL; arg = arg->s_nxt)
   2087 		narg++;
   2088 	for (parg = pargs; parg != NULL; parg = parg->s_nxt)
   2089 		nparg++;
   2090 	if (narg != nparg) {
   2091 		/* prototype does not match old-style definition */
   2092 		error(63);
   2093 		msg = 1;
   2094 		goto end;
   2095 	}
   2096 
   2097 	arg = args;
   2098 	parg = pargs;
   2099 	n = 1;
   2100 	while (narg--) {
   2101 		warn = 0;
   2102 		/*
   2103 		 * If it does not match due to promotion and sflag is
   2104 		 * not set we print only a warning.
   2105 		 */
   2106 		if (!eqtype(arg->s_type, parg->s_type, 1, 1, &warn) || warn) {
   2107 			/* prototype does not match old-style def., arg #%d */
   2108 			error(299, n);
   2109 			msg = 1;
   2110 		}
   2111 		arg = arg->s_nxt;
   2112 		parg = parg->s_nxt;
   2113 		n++;
   2114 	}
   2115 
   2116  end:
   2117 	if (msg)
   2118 		/* old style definition */
   2119 		prevdecl(300, rdsym);
   2120 
   2121 	return (msg);
   2122 }
   2123 
   2124 /*
   2125  * Complets a type by copying the dimension and prototype information
   2126  * from a second compatible type.
   2127  *
   2128  * Following lines are legal:
   2129  *  "typedef a[]; a b; a b[10]; a c; a c[20];"
   2130  *  "typedef ft(); ft f; f(int); ft g; g(long);"
   2131  * This means that, if a type is completed, the type structure must
   2132  * be duplicated.
   2133  */
   2134 void
   2135 compltyp(sym_t *dsym, sym_t *ssym)
   2136 {
   2137 	type_t	**dstp, *src;
   2138 	type_t	*dst;
   2139 
   2140 	dstp = &dsym->s_type;
   2141 	src = ssym->s_type;
   2142 
   2143 	while ((dst = *dstp) != NULL) {
   2144 		if (src == NULL || dst->t_tspec != src->t_tspec)
   2145 			lerror("compltyp() 1");
   2146 		if (dst->t_tspec == ARRAY) {
   2147 			if (dst->t_dim == 0 && src->t_dim != 0) {
   2148 				*dstp = dst = duptyp(dst);
   2149 				dst->t_dim = src->t_dim;
   2150 				/* now a complete Typ */
   2151 				setcompl(dst, 0);
   2152 			}
   2153 		} else if (dst->t_tspec == FUNC) {
   2154 			if (!dst->t_proto && src->t_proto) {
   2155 				*dstp = dst = duptyp(dst);
   2156 				dst->t_proto = 1;
   2157 				dst->t_args = src->t_args;
   2158 			}
   2159 		}
   2160 		dstp = &dst->t_subt;
   2161 		src = src->t_subt;
   2162 	}
   2163 }
   2164 
   2165 /*
   2166  * Completes the declaration of a single argument.
   2167  */
   2168 sym_t *
   2169 decl1arg(sym_t *sym, int initflg)
   2170 {
   2171 	tspec_t	t;
   2172 
   2173 	chkfdef(sym, 1);
   2174 
   2175 	chktyp(sym);
   2176 
   2177 	if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) {
   2178 		/* redeclaration of formal parameter %s */
   2179 		error(237, sym->s_name);
   2180 		rmsym(dcs->d_rdcsym);
   2181 		sym->s_arg = 1;
   2182 	}
   2183 
   2184 	if (!sym->s_arg) {
   2185 		/* declared argument %s is missing */
   2186 		error(53, sym->s_name);
   2187 		sym->s_arg = 1;
   2188 	}
   2189 
   2190 	if (initflg) {
   2191 		/* cannot initialize parameter: %s */
   2192 		error(52, sym->s_name);
   2193 		initerr = 1;
   2194 	}
   2195 
   2196 	if ((t = sym->s_type->t_tspec) == ARRAY) {
   2197 		sym->s_type = incref(sym->s_type->t_subt, PTR);
   2198 	} else if (t == FUNC) {
   2199 		if (tflag)
   2200 			/* a function is declared as an argument: %s */
   2201 			warning(50, sym->s_name);
   2202 		sym->s_type = incref(sym->s_type, PTR);
   2203 	} else if (t == FLOAT) {
   2204 		if (tflag)
   2205 			sym->s_type = gettyp(DOUBLE);
   2206 	}
   2207 
   2208 	if (dcs->d_inline)
   2209 		/* argument declared inline: %s */
   2210 		warning(269, sym->s_name);
   2211 
   2212 	/*
   2213 	 * Arguments must have complete types. lengths() prints the needed
   2214 	 * error messages (null dimension is impossible because arrays are
   2215 	 * converted to pointers).
   2216 	 */
   2217 	if (sym->s_type->t_tspec != VOID)
   2218 		(void)length(sym->s_type, sym->s_name);
   2219 
   2220 	setsflg(sym);
   2221 
   2222 	return (sym);
   2223 }
   2224 
   2225 /*
   2226  * Does some checks for lint directives which apply to functions.
   2227  * Processes arguments in old style function definitions which default
   2228  * to int.
   2229  * Checks compatiblility of old style function definition with previous
   2230  * prototype.
   2231  */
   2232 void
   2233 cluparg(void)
   2234 {
   2235 	sym_t	*args, *arg, *pargs, *parg;
   2236 	int	narg, nparg, n, msg;
   2237 	tspec_t	t;
   2238 
   2239 	args = funcsym->s_args;
   2240 	pargs = funcsym->s_type->t_args;
   2241 
   2242 	/* check for illegal combinations of lint directives */
   2243 	if (prflstrg != -1 && scflstrg != -1) {
   2244 		/* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
   2245 		warning(289);
   2246 		prflstrg = scflstrg = -1;
   2247 	}
   2248 	if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) {
   2249 		/* dubious use of ** VARARGS ** with ** %s ** */
   2250 		warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE");
   2251 		nvararg = -1;
   2252 	}
   2253 
   2254 	/*
   2255 	 * check if the argument of a lint directive is compatible with the
   2256 	 * number of arguments.
   2257 	 */
   2258 	narg = 0;
   2259 	for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_nxt)
   2260 		narg++;
   2261 	if (nargusg > narg) {
   2262 		/* argument number mismatch with directive: ** %s ** */
   2263 		warning(283, "ARGSUSED");
   2264 		nargusg = 0;
   2265 	}
   2266 	if (nvararg > narg) {
   2267 		/* argument number mismatch with directive: ** %s ** */
   2268 		warning(283, "VARARGS");
   2269 		nvararg = 0;
   2270 	}
   2271 	if (prflstrg > narg) {
   2272 		/* argument number mismatch with directive: ** %s ** */
   2273 		warning(283, "PRINTFLIKE");
   2274 		prflstrg = -1;
   2275 	} else if (prflstrg == 0) {
   2276 		prflstrg = -1;
   2277 	}
   2278 	if (scflstrg > narg) {
   2279 		/* argument number mismatch with directive: ** %s ** */
   2280 		warning(283, "SCANFLIKE");
   2281 		scflstrg = -1;
   2282 	} else if (scflstrg == 0) {
   2283 		scflstrg = -1;
   2284 	}
   2285 	if (prflstrg != -1 || scflstrg != -1) {
   2286 		narg = prflstrg != -1 ? prflstrg : scflstrg;
   2287 		arg = dcs->d_fargs;
   2288 		for (n = 1; n < narg; n++)
   2289 			arg = arg->s_nxt;
   2290 		if (arg->s_type->t_tspec != PTR ||
   2291 		    ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
   2292 		     t != UCHAR && t != SCHAR)) {
   2293 			/* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
   2294 			warning(293, narg);
   2295 			prflstrg = scflstrg = -1;
   2296 		}
   2297 	}
   2298 
   2299 	/*
   2300 	 * print a warning for each argument off an old style function
   2301 	 * definition which defaults to int
   2302 	 */
   2303 	for (arg = args; arg != NULL; arg = arg->s_nxt) {
   2304 		if (arg->s_defarg) {
   2305 			/* argument type defaults to int: %s */
   2306 			warning(32, arg->s_name);
   2307 			arg->s_defarg = 0;
   2308 			setsflg(arg);
   2309 		}
   2310 	}
   2311 
   2312 	/*
   2313 	 * If this is an old style function definition and a prototyp
   2314 	 * exists, compare the types of arguments.
   2315 	 */
   2316 	if (funcsym->s_osdef && funcsym->s_type->t_proto) {
   2317 		/*
   2318 		 * If the number of arguments does not macht, we need not
   2319 		 * continue.
   2320 		 */
   2321 		narg = nparg = 0;
   2322 		msg = 0;
   2323 		for (parg = pargs; parg != NULL; parg = parg->s_nxt)
   2324 			nparg++;
   2325 		for (arg = args; arg != NULL; arg = arg->s_nxt)
   2326 			narg++;
   2327 		if (narg != nparg) {
   2328 			/* parameter mismatch: %d declared, %d defined */
   2329 			error(51, nparg, narg);
   2330 			msg = 1;
   2331 		} else {
   2332 			parg = pargs;
   2333 			arg = args;
   2334 			while (narg--) {
   2335 				msg |= chkptdecl(arg, parg);
   2336 				parg = parg->s_nxt;
   2337 				arg = arg->s_nxt;
   2338 			}
   2339 		}
   2340 		if (msg)
   2341 			/* prototype declaration */
   2342 			prevdecl(285, dcs->d_rdcsym);
   2343 
   2344 		/* from now the prototype is valid */
   2345 		funcsym->s_osdef = 0;
   2346 		funcsym->s_args = NULL;
   2347 
   2348 	}
   2349 
   2350 }
   2351 
   2352 /*
   2353  * Checks compatibility of an old style function definition with a previous
   2354  * prototype declaration.
   2355  * Returns 1 if the position of the previous declaration should be reported.
   2356  */
   2357 static int
   2358 chkptdecl(sym_t *arg, sym_t *parg)
   2359 {
   2360 	type_t	*tp, *ptp;
   2361 	int	warn, msg;
   2362 
   2363 	tp = arg->s_type;
   2364 	ptp = parg->s_type;
   2365 
   2366 	msg = 0;
   2367 	warn = 0;
   2368 
   2369 	if (!eqtype(tp, ptp, 1, 1, &warn)) {
   2370 		if (eqtype(tp, ptp, 1, 0, &warn)) {
   2371 			/* type does not match prototype: %s */
   2372 			msg = gnuism(58, arg->s_name);
   2373 		} else {
   2374 			/* type does not match prototype: %s */
   2375 			error(58, arg->s_name);
   2376 			msg = 1;
   2377 		}
   2378 	} else if (warn) {
   2379 		/* type does not match prototype: %s */
   2380 		(*(sflag ? error : warning))(58, arg->s_name);
   2381 		msg = 1;
   2382 	}
   2383 
   2384 	return (msg);
   2385 }
   2386 
   2387 /*
   2388  * Completes a single local declaration/definition.
   2389  */
   2390 void
   2391 decl1loc(sym_t *dsym, int initflg)
   2392 {
   2393 
   2394 	/* Correct a mistake done in dname(). */
   2395 	if (dsym->s_type->t_tspec == FUNC) {
   2396 		dsym->s_def = DECL;
   2397 		if (dcs->d_scl == NOSCL)
   2398 			dsym->s_scl = EXTERN;
   2399 	}
   2400 
   2401 	if (dsym->s_type->t_tspec == FUNC) {
   2402 		if (dsym->s_scl == STATIC) {
   2403 			/* dubious static function at block level: %s */
   2404 			warning(93, dsym->s_name);
   2405 			dsym->s_scl = EXTERN;
   2406 		} else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
   2407 			/* function has illegal storage class: %s */
   2408 			error(94, dsym->s_name);
   2409 			dsym->s_scl = EXTERN;
   2410 		}
   2411 	}
   2412 
   2413 	/*
   2414 	 * functions may be declared inline at local scope, although
   2415 	 * this has no effect for a later definition of the same
   2416 	 * function.
   2417 	 * XXX it should have an effect if tflag is set. this would
   2418 	 * also be the way gcc behaves.
   2419 	 */
   2420 	if (dcs->d_inline) {
   2421 		if (dsym->s_type->t_tspec == FUNC) {
   2422 			dsym->s_inline = 1;
   2423 		} else {
   2424 			/* variable declared inline: %s */
   2425 			warning(268, dsym->s_name);
   2426 		}
   2427 	}
   2428 
   2429 	chkfdef(dsym, 1);
   2430 
   2431 	chktyp(dsym);
   2432 
   2433 	if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN)
   2434 		ledecl(dsym);
   2435 
   2436 	if (dsym->s_scl == EXTERN) {
   2437 		/*
   2438 		 * XXX wenn die statische Variable auf Ebene 0 erst
   2439 		 * spaeter definiert wird, haben wir die Brille auf.
   2440 		 */
   2441 		if (dsym->s_xsym == NULL) {
   2442 			outsym(dsym, EXTERN, dsym->s_def);
   2443 		} else {
   2444 			outsym(dsym, dsym->s_xsym->s_scl, dsym->s_def);
   2445 		}
   2446 	}
   2447 
   2448 	if (dcs->d_rdcsym != NULL) {
   2449 
   2450 		if (dcs->d_rdcsym->s_blklev == 0) {
   2451 
   2452 			switch (dsym->s_scl) {
   2453 			case AUTO:
   2454 				/* automatic hides external declaration: %s */
   2455 				if (hflag)
   2456 					warning(86, dsym->s_name);
   2457 				break;
   2458 			case STATIC:
   2459 				/* static hides external declaration: %s */
   2460 				if (hflag)
   2461 					warning(87, dsym->s_name);
   2462 				break;
   2463 			case TYPEDEF:
   2464 				/* typedef hides  external declaration: %s */
   2465 				if (hflag)
   2466 					warning(88, dsym->s_name);
   2467 				break;
   2468 			case EXTERN:
   2469 				/*
   2470 				 * Warnings and errors are printed in ledecl()
   2471 				 */
   2472 				break;
   2473 			default:
   2474 				lerror("decl1loc() 1");
   2475 			}
   2476 
   2477 		} else if (dcs->d_rdcsym->s_blklev == blklev) {
   2478 
   2479 			/* no hflag, because its illegal! */
   2480 			if (dcs->d_rdcsym->s_arg) {
   2481 				/*
   2482 				 * if !tflag, a "redeclaration of %s" error
   2483 				 * is produced below
   2484 				 */
   2485 				if (tflag) {
   2486 					if (hflag)
   2487 						/* decl. hides parameter: %s */
   2488 						warning(91, dsym->s_name);
   2489 					rmsym(dcs->d_rdcsym);
   2490 				}
   2491 			}
   2492 
   2493 		} else if (dcs->d_rdcsym->s_blklev < blklev) {
   2494 
   2495 			if (hflag)
   2496 				/* declaration hides earlier one: %s */
   2497 				warning(95, dsym->s_name);
   2498 
   2499 		}
   2500 
   2501 		if (dcs->d_rdcsym->s_blklev == blklev) {
   2502 
   2503 			/* redeclaration of %s */
   2504 			error(27, dsym->s_name);
   2505 			rmsym(dcs->d_rdcsym);
   2506 
   2507 		}
   2508 
   2509 	}
   2510 
   2511 	if (initflg && !(initerr = chkinit(dsym))) {
   2512 		dsym->s_def = DEF;
   2513 		setsflg(dsym);
   2514 	}
   2515 
   2516 	if (dsym->s_scl == TYPEDEF) {
   2517 		dsym->s_type = duptyp(dsym->s_type);
   2518 		dsym->s_type->t_typedef = 1;
   2519 		settdsym(dsym->s_type, dsym);
   2520 	}
   2521 
   2522 	/*
   2523 	 * Before we can check the size we must wait for a initialisation
   2524 	 * which may follow.
   2525 	 */
   2526 }
   2527 
   2528 /*
   2529  * Processes (re)declarations of external Symbols inside blocks.
   2530  */
   2531 static void
   2532 ledecl(sym_t *dsym)
   2533 {
   2534 	int	eqt, warn;
   2535 	sym_t	*esym;
   2536 
   2537 	/* look for a symbol with the same name */
   2538 	esym = dcs->d_rdcsym;
   2539 	while (esym != NULL && esym->s_blklev != 0) {
   2540 		while ((esym = esym->s_link) != NULL) {
   2541 			if (esym->s_kind != FVFT)
   2542 				continue;
   2543 			if (strcmp(dsym->s_name, esym->s_name) == 0)
   2544 				break;
   2545 		}
   2546 	}
   2547 	if (esym == NULL)
   2548 		return;
   2549 	if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
   2550 		/* gcc accepts this without a warning, pcc prints an error. */
   2551 		/* redeclaration of %s */
   2552 		warning(27, dsym->s_name);
   2553 		prevdecl(-1, esym);
   2554 		return;
   2555 	}
   2556 
   2557 	warn = 0;
   2558 	eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &warn);
   2559 
   2560 	if (!eqt || warn) {
   2561 		if (esym->s_scl == EXTERN) {
   2562 			/* inconsistent redeclaration of extern: %s */
   2563 			warning(90, dsym->s_name);
   2564 			prevdecl(-1, esym);
   2565 		} else {
   2566 			/* inconsistent redeclaration of static: %s */
   2567 			warning(92, dsym->s_name);
   2568 			prevdecl(-1, esym);
   2569 		}
   2570 	}
   2571 
   2572 	if (eqt) {
   2573 		/*
   2574 		 * Remember the external symbol so we can update usage
   2575 		 * information at the end of the block.
   2576 		 */
   2577 		dsym->s_xsym = esym;
   2578 	}
   2579 }
   2580 
   2581 /*
   2582  * Print an error or a warning if the symbol cant be initialized due
   2583  * to type/storage class. Returnvalue is 1 if an error has been
   2584  * detected.
   2585  */
   2586 static int
   2587 chkinit(sym_t *sym)
   2588 {
   2589 	int	err;
   2590 
   2591 	err = 0;
   2592 
   2593 	if (sym->s_type->t_tspec == FUNC) {
   2594 		/* cannot initialize function: %s */
   2595 		error(24, sym->s_name);
   2596 		err = 1;
   2597 	} else if (sym->s_scl == TYPEDEF) {
   2598 		/* cannot initialize typedef: %s */
   2599 		error(25, sym->s_name);
   2600 		err = 1;
   2601 	} else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
   2602 		/* cannot initialize "extern" declaration: %s */
   2603 		if (dcs->d_ctx == EXTERN) {
   2604 			warning(26, sym->s_name);
   2605 		} else {
   2606 			error(26, sym->s_name);
   2607 			err = 1;
   2608 		}
   2609 	}
   2610 
   2611 	return (err);
   2612 }
   2613 
   2614 /*
   2615  * Create a symbole for an abstract declaration.
   2616  */
   2617 sym_t *
   2618 aname(void)
   2619 {
   2620 	sym_t	*sym;
   2621 
   2622 	if (dcs->d_ctx != ABSTRACT && dcs->d_ctx != PARG)
   2623 		lerror("aname()");
   2624 
   2625 	sym = getblk(sizeof (sym_t));
   2626 
   2627 	sym->s_name = unnamed;
   2628 	sym->s_def = DEF;
   2629 	sym->s_scl = ABSTRACT;
   2630 	sym->s_blklev = -1;
   2631 
   2632 	if (dcs->d_ctx == PARG)
   2633 		sym->s_arg = 1;
   2634 
   2635 	sym->s_type = dcs->d_type;
   2636 	dcs->d_rdcsym = NULL;
   2637 	dcs->d_vararg = 0;
   2638 
   2639 	return (sym);
   2640 }
   2641 
   2642 /*
   2643  * Removes anything which has nothing to do on global level.
   2644  */
   2645 void
   2646 globclup(void)
   2647 {
   2648 
   2649 	while (dcs->d_nxt != NULL)
   2650 		popdecl();
   2651 
   2652 	cleanup();
   2653 	blklev = 0;
   2654 	mblklev = 0;
   2655 
   2656 	/*
   2657 	 * remove all informations about pending lint directives without
   2658 	 * warnings.
   2659 	 */
   2660 	glclup(1);
   2661 }
   2662 
   2663 /*
   2664  * Process an abstract type declaration
   2665  */
   2666 sym_t *
   2667 decl1abs(sym_t *sym)
   2668 {
   2669 
   2670 	chkfdef(sym, 1);
   2671 	chktyp(sym);
   2672 	return (sym);
   2673 }
   2674 
   2675 /*
   2676  * Checks size after declarations of variables and their initialisation.
   2677  */
   2678 void
   2679 chksz(sym_t *dsym)
   2680 {
   2681 
   2682 	/*
   2683 	 * check size only for symbols which are defined and no function and
   2684 	 * not typedef name
   2685 	 */
   2686 	if (dsym->s_def != DEF)
   2687 		return;
   2688 	if (dsym->s_scl == TYPEDEF)
   2689 		return;
   2690 	if (dsym->s_type->t_tspec == FUNC)
   2691 		return;
   2692 
   2693 	if (length(dsym->s_type, dsym->s_name) == 0 &&
   2694 	    dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
   2695 		/* empty array declaration: %s */
   2696 		if (tflag) {
   2697 			warning(190, dsym->s_name);
   2698 		} else {
   2699 			error(190, dsym->s_name);
   2700 		}
   2701 	}
   2702 }
   2703 
   2704 /*
   2705  * Mark an object as set if it is not already
   2706  */
   2707 void
   2708 setsflg(sym_t *sym)
   2709 {
   2710 
   2711 	if (!sym->s_set) {
   2712 		sym->s_set = 1;
   2713 		UNIQUE_CURR_POS(sym->s_spos);
   2714 	}
   2715 }
   2716 
   2717 /*
   2718  * Mark an object as used if it is not already
   2719  */
   2720 void
   2721 setuflg(sym_t *sym, int fcall, int szof)
   2722 {
   2723 
   2724 	if (!sym->s_used) {
   2725 		sym->s_used = 1;
   2726 		UNIQUE_CURR_POS(sym->s_upos);
   2727 	}
   2728 	/*
   2729 	 * for function calls another record is written
   2730 	 *
   2731 	 * XXX Should symbols used in sizeof() treated as used or not?
   2732 	 * Probably not, because there is no sense to declare an
   2733 	 * external variable only to get their size.
   2734 	 */
   2735 	if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
   2736 		outusg(sym);
   2737 }
   2738 
   2739 /*
   2740  * Prints warnings for a list of variables and labels (concatenated
   2741  * with s_dlnxt) if these are not used or only set.
   2742  */
   2743 void
   2744 chkusage(dinfo_t *di)
   2745 {
   2746 	sym_t	*sym;
   2747 	int	mknowarn;
   2748 
   2749 	/* for this warnings LINTED has no effect */
   2750 	mknowarn = nowarn;
   2751 	nowarn = 0;
   2752 
   2753 	for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
   2754 		chkusg1(di->d_asm, sym);
   2755 
   2756 	nowarn = mknowarn;
   2757 }
   2758 
   2759 /*
   2760  * Prints a warning for a single variable or label if it is not used or
   2761  * only set.
   2762  */
   2763 void
   2764 chkusg1(int novar, sym_t *sym)
   2765 {
   2766 	pos_t	cpos;
   2767 
   2768 	if (sym->s_blklev == -1)
   2769 		return;
   2770 
   2771 	STRUCT_ASSIGN(cpos, curr_pos);
   2772 
   2773 	if (sym->s_kind == FVFT) {
   2774 		if (sym->s_arg) {
   2775 			chkausg(novar, sym);
   2776 		} else {
   2777 			chkvusg(novar, sym);
   2778 		}
   2779 	} else if (sym->s_kind == FLAB) {
   2780 		chklusg(sym);
   2781 	} else if (sym->s_kind == FTAG) {
   2782 		chktusg(sym);
   2783 	}
   2784 
   2785 	STRUCT_ASSIGN(curr_pos, cpos);
   2786 }
   2787 
   2788 static void
   2789 chkausg(int novar, sym_t *arg)
   2790 {
   2791 
   2792 	if (!arg->s_set)
   2793 		lerror("chkausg() 1");
   2794 
   2795 	if (novar)
   2796 		return;
   2797 
   2798 	if (!arg->s_used && vflag) {
   2799 		STRUCT_ASSIGN(curr_pos, arg->s_dpos);
   2800 		/* argument %s unused in function %s */
   2801 		warning(231, arg->s_name, funcsym->s_name);
   2802 	}
   2803 }
   2804 
   2805 static void
   2806 chkvusg(int novar, sym_t *sym)
   2807 {
   2808 	scl_t	sc;
   2809 	sym_t	*xsym;
   2810 
   2811 	if (blklev == 0 || sym->s_blklev == 0)
   2812 		lerror("chkvusg() 1");
   2813 
   2814 	/* errors in expressions easily cause lots of these warnings */
   2815 	if (nerr != 0)
   2816 		return;
   2817 
   2818 	/*
   2819 	 * XXX Only variables are checkd, although types should
   2820 	 * probably also be checked
   2821 	 */
   2822 	if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
   2823 	    sc != AUTO && sc != REG) {
   2824 		return;
   2825 	}
   2826 
   2827 	if (novar)
   2828 		return;
   2829 
   2830 	if (sc == EXTERN) {
   2831 		if (!sym->s_used && !sym->s_set) {
   2832 			STRUCT_ASSIGN(curr_pos, sym->s_dpos);
   2833 			/* %s unused in function %s */
   2834 			warning(192, sym->s_name, funcsym->s_name);
   2835 		}
   2836 	} else {
   2837 		if (sym->s_set && !sym->s_used) {
   2838 			STRUCT_ASSIGN(curr_pos, sym->s_spos);
   2839 			/* %s set but not used in function %s */
   2840 			warning(191, sym->s_name, funcsym->s_name);
   2841 		} else if (!sym->s_used) {
   2842 			STRUCT_ASSIGN(curr_pos, sym->s_dpos);
   2843 			/* %s unused in function %s */
   2844 			warning(192, sym->s_name, funcsym->s_name);
   2845 		}
   2846 	}
   2847 
   2848 	if (sc == EXTERN) {
   2849 		/*
   2850 		 * information about usage is taken over into the symbol
   2851 		 * tabel entry at level 0 if the symbol was locally declared
   2852 		 * as an external symbol.
   2853 		 *
   2854 		 * XXX This is wrong for symbols declared static at level 0
   2855 		 * if the usage information stems from sizeof(). This is
   2856 		 * because symbols at level 0 only used in sizeof() are
   2857 		 * considered to not be used.
   2858 		 */
   2859 		if ((xsym = sym->s_xsym) != NULL) {
   2860 			if (sym->s_used && !xsym->s_used) {
   2861 				xsym->s_used = 1;
   2862 				STRUCT_ASSIGN(xsym->s_upos, sym->s_upos);
   2863 			}
   2864 			if (sym->s_set && !xsym->s_set) {
   2865 				xsym->s_set = 1;
   2866 				STRUCT_ASSIGN(xsym->s_spos, sym->s_spos);
   2867 			}
   2868 		}
   2869 	}
   2870 }
   2871 
   2872 static void
   2873 chklusg(sym_t *lab)
   2874 {
   2875 
   2876 	if (blklev != 1 || lab->s_blklev != 1)
   2877 		lerror("chklusg() 1");
   2878 
   2879 	if (lab->s_set && !lab->s_used) {
   2880 		STRUCT_ASSIGN(curr_pos, lab->s_spos);
   2881 		/* label %s unused in function %s */
   2882 		warning(192, lab->s_name, funcsym->s_name);
   2883 	} else if (!lab->s_set) {
   2884 		STRUCT_ASSIGN(curr_pos, lab->s_upos);
   2885 		/* undefined label %s */
   2886 		warning(23, lab->s_name);
   2887 	}
   2888 }
   2889 
   2890 static void
   2891 chktusg(sym_t *sym)
   2892 {
   2893 
   2894 	if (!incompl(sym->s_type))
   2895 		return;
   2896 
   2897 	/* complain alwasy about incomplet tags declared inside blocks */
   2898 	if (!zflag || dcs->d_ctx != EXTERN)
   2899 		return;
   2900 
   2901 	STRUCT_ASSIGN(curr_pos, sym->s_dpos);
   2902 	switch (sym->s_type->t_tspec) {
   2903 	case STRUCT:
   2904 		/* struct %s never defined */
   2905 		warning(233, sym->s_name);
   2906 		break;
   2907 	case UNION:
   2908 		/* union %s never defined */
   2909 		warning(234, sym->s_name);
   2910 		break;
   2911 	case ENUM:
   2912 		/* enum %s never defined */
   2913 		warning(235, sym->s_name);
   2914 		break;
   2915 	default:
   2916 		lerror("chktusg() 1");
   2917 	}
   2918 }
   2919 
   2920 /*
   2921  * Called after the entire translation unit has been parsed.
   2922  * Changes tentative definitions in definitions.
   2923  * Performs some tests on global Symbols. Detected Problems are:
   2924  * - defined variables of incomplete type
   2925  * - constant variables which are not initialized
   2926  * - static symbols which are never used
   2927  */
   2928 void
   2929 chkglsyms(void)
   2930 {
   2931 	sym_t	*sym;
   2932 	pos_t	cpos;
   2933 
   2934 	if (blklev != 0 || dcs->d_nxt != NULL)
   2935 		norecover();
   2936 
   2937 	STRUCT_ASSIGN(cpos, curr_pos);
   2938 
   2939 	for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
   2940 		if (sym->s_blklev == -1)
   2941 			continue;
   2942 		if (sym->s_kind == FVFT) {
   2943 			chkglvar(sym);
   2944 		} else if (sym->s_kind == FTAG) {
   2945 			chktusg(sym);
   2946 		} else {
   2947 			if (sym->s_kind != FMOS)
   2948 				lerror("chkglsyms() 1");
   2949 		}
   2950 	}
   2951 
   2952 	STRUCT_ASSIGN(curr_pos, cpos);
   2953 }
   2954 
   2955 static void
   2956 chkglvar(sym_t *sym)
   2957 {
   2958 
   2959 	if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON)
   2960 		return;
   2961 
   2962 	if (sym->s_scl != EXTERN && sym->s_scl != STATIC)
   2963 		lerror("chkglvar() 1");
   2964 
   2965 	glchksz(sym);
   2966 
   2967 	if (sym->s_scl == STATIC) {
   2968 		if (sym->s_type->t_tspec == FUNC) {
   2969 			if (sym->s_used && sym->s_def != DEF) {
   2970 				STRUCT_ASSIGN(curr_pos, sym->s_upos);
   2971 				/* static func. called but not def.. */
   2972 				error(225, sym->s_name);
   2973 			}
   2974 		}
   2975 		if (!sym->s_used) {
   2976 			STRUCT_ASSIGN(curr_pos, sym->s_dpos);
   2977 			if (sym->s_type->t_tspec == FUNC) {
   2978 				if (sym->s_def == DEF) {
   2979 					if (!sym->s_inline)
   2980 						/* static function %s unused */
   2981 						warning(236, sym->s_name);
   2982 				} else {
   2983 					/* static function %s decl. but ... */
   2984 					warning(290, sym->s_name);
   2985 				}
   2986 			} else if (!sym->s_set) {
   2987 				/* static variable %s unused */
   2988 				warning(226, sym->s_name);
   2989 			} else {
   2990 				/* static variable %s set but not used */
   2991 				warning(307, sym->s_name);
   2992 			}
   2993 		}
   2994 		if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
   2995 			STRUCT_ASSIGN(curr_pos, sym->s_dpos);
   2996 			/* const object %s should have initializer */
   2997 			warning(227, sym->s_name);
   2998 		}
   2999 	}
   3000 }
   3001 
   3002 static void
   3003 glchksz(sym_t *sym)
   3004 {
   3005 
   3006 	if (sym->s_def == TDEF) {
   3007 		if (sym->s_type->t_tspec == FUNC)
   3008 			/*
   3009 			 * this can happen if an syntax error occurred
   3010 			 * after a function declaration
   3011 			 */
   3012 			return;
   3013 		STRUCT_ASSIGN(curr_pos, sym->s_dpos);
   3014 		if (length(sym->s_type, sym->s_name) == 0 &&
   3015 		    sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
   3016 			/* empty array declaration: %s */
   3017 			if (tflag || (sym->s_scl == EXTERN && !sflag)) {
   3018 				warning(190, sym->s_name);
   3019 			} else {
   3020 				error(190, sym->s_name);
   3021 			}
   3022 		}
   3023 	}
   3024 }
   3025 
   3026 /*
   3027  * Prints information about location of previous definition/declaration.
   3028  */
   3029 void
   3030 prevdecl(int msg, sym_t *psym)
   3031 {
   3032 	pos_t	cpos;
   3033 
   3034 	if (!rflag)
   3035 		return;
   3036 
   3037 	STRUCT_ASSIGN(cpos, curr_pos);
   3038 	STRUCT_ASSIGN(curr_pos, psym->s_dpos);
   3039 	if (msg != -1) {
   3040 		message(msg, psym->s_name);
   3041 	} else if (psym->s_def == DEF || psym->s_def == TDEF) {
   3042 		/* previous definition of %s */
   3043 		message(261, psym->s_name);
   3044 	} else {
   3045 		/* previous declaration of %s */
   3046 		message(260, psym->s_name);
   3047 	}
   3048 	STRUCT_ASSIGN(curr_pos, cpos);
   3049 }
   3050