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