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