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