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