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