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