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