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