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