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