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