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