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