Home | History | Annotate | Line # | Download | only in lint1
decl.c revision 1.46
      1 /* $NetBSD: decl.c,v 1.46 2009/04/15 01:20:57 christos Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
      5  * Copyright (c) 1994, 1995 Jochen Pohl
      6  * All Rights Reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Jochen Pohl for
     19  *	The NetBSD Project.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #if HAVE_NBTOOL_CONFIG_H
     36 #include "nbtool_config.h"
     37 #endif
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(__RCSID) && !defined(lint)
     41 __RCSID("$NetBSD: decl.c,v 1.46 2009/04/15 01:20:57 christos Exp $");
     42 #endif
     43 
     44 #include <sys/param.h>
     45 #include <limits.h>
     46 #include <stdlib.h>
     47 #include <string.h>
     48 
     49 #include "lint1.h"
     50 
     51 const	char *unnamed = "<unnamed>";
     52 
     53 /* shared type structures for arithmtic types and void */
     54 static	type_t	*typetab;
     55 
     56 /* value of next enumerator during declaration of enum types */
     57 int	enumval;
     58 
     59 /*
     60  * pointer to top element of a stack which contains informations local
     61  * to nested declarations
     62  */
     63 dinfo_t	*dcs;
     64 
     65 static	type_t	*tdeferr(type_t *, tspec_t);
     66 static	void	settdsym(type_t *, sym_t *);
     67 static	tspec_t	mrgtspec(tspec_t, tspec_t);
     68 static	void	align(int, int);
     69 static	sym_t	*newtag(sym_t *, scl_t, int, int);
     70 static	int	eqargs(type_t *, type_t *, int *);
     71 static	int	mnoarg(type_t *, int *);
     72 static	int	chkosdef(sym_t *, sym_t *);
     73 static	int	chkptdecl(sym_t *, sym_t *);
     74 static	sym_t	*nsfunc(sym_t *, sym_t *);
     75 static	void	osfunc(sym_t *, sym_t *);
     76 static	void	ledecl(sym_t *);
     77 static	int	chkinit(sym_t *);
     78 static	void	chkausg(int, sym_t *);
     79 static	void	chkvusg(int, sym_t *);
     80 static	void	chklusg(sym_t *);
     81 static	void	chktusg(sym_t *);
     82 static	void	chkglvar(sym_t *);
     83 static	void	glchksz(sym_t *);
     84 
     85 /*
     86  * initializes all global vars used in declarations
     87  */
     88 void
     89 initdecl(void)
     90 {
     91 	int i;
     92 
     93 	/* declaration stack */
     94 	dcs = xcalloc(1, sizeof (dinfo_t));
     95 	dcs->d_ctx = EXTERN;
     96 	dcs->d_ldlsym = &dcs->d_dlsyms;
     97 
     98 	/* type information and classification */
     99 	inittyp();
    100 
    101 	/* shared type structures */
    102 	typetab = xcalloc(NTSPEC, sizeof (type_t));
    103 	for (i = 0; i < NTSPEC; i++)
    104 		typetab[i].t_tspec = NOTSPEC;
    105 	typetab[BOOL].t_tspec = BOOL;
    106 	typetab[CHAR].t_tspec = CHAR;
    107 	typetab[SCHAR].t_tspec = SCHAR;
    108 	typetab[UCHAR].t_tspec = UCHAR;
    109 	typetab[SHORT].t_tspec = SHORT;
    110 	typetab[USHORT].t_tspec = USHORT;
    111 	typetab[INT].t_tspec = INT;
    112 	typetab[UINT].t_tspec = UINT;
    113 	typetab[LONG].t_tspec = LONG;
    114 	typetab[ULONG].t_tspec = ULONG;
    115 	typetab[QUAD].t_tspec = QUAD;
    116 	typetab[UQUAD].t_tspec = UQUAD;
    117 	typetab[FLOAT].t_tspec = FLOAT;
    118 	typetab[DOUBLE].t_tspec = DOUBLE;
    119 	typetab[LDOUBLE].t_tspec = LDOUBLE;
    120 	typetab[FCOMPLEX].t_tspec = FCOMPLEX;
    121 	typetab[DCOMPLEX].t_tspec = DCOMPLEX;
    122 	typetab[LCOMPLEX].t_tspec = LCOMPLEX;
    123 	typetab[COMPLEX].t_tspec = COMPLEX;
    124 	typetab[VOID].t_tspec = VOID;
    125 	/*
    126 	 * Next two are not real types. They are only used by the parser
    127 	 * to return keywords "signed" and "unsigned"
    128 	 */
    129 	typetab[SIGNED].t_tspec = SIGNED;
    130 	typetab[UNSIGN].t_tspec = UNSIGN;
    131 }
    132 
    133 /*
    134  * Returns a shared type structure vor arithmetic types and void.
    135  *
    136  * It's important do duplicate this structure (using duptyp() or tdupdyp())
    137  * if it is to be modified (adding qualifiers or anything else).
    138  */
    139 type_t *
    140 gettyp(tspec_t t)
    141 {
    142 
    143 	return (&typetab[t]);
    144 }
    145 
    146 type_t *
    147 duptyp(const type_t *tp)
    148 {
    149 	type_t	*ntp;
    150 
    151 	ntp = getblk(sizeof (type_t));
    152 	STRUCT_ASSIGN(*ntp, *tp);
    153 	return (ntp);
    154 }
    155 
    156 /*
    157  * Use tduptyp() instead of duptyp() inside expressions (if the
    158  * allocated memory should be freed after the expr).
    159  */
    160 type_t *
    161 tduptyp(const type_t *tp)
    162 {
    163 	type_t	*ntp;
    164 
    165 	ntp = tgetblk(sizeof (type_t));
    166 	STRUCT_ASSIGN(*ntp, *tp);
    167 	return (ntp);
    168 }
    169 
    170 /*
    171  * Returns 1 if the argument is void or an incomplete array,
    172  * struct, union or enum type.
    173  */
    174 int
    175 incompl(type_t *tp)
    176 {
    177 	tspec_t	t;
    178 
    179 	if ((t = tp->t_tspec) == VOID) {
    180 		return (1);
    181 	} else if (t == ARRAY) {
    182 		return (tp->t_aincompl);
    183 	} else if (t == STRUCT || t == UNION) {
    184 		return (tp->t_str->sincompl);
    185 	} else if (t == ENUM) {
    186 		return (tp->t_enum->eincompl);
    187 	}
    188 	return (0);
    189 }
    190 
    191 /*
    192  * Set the flag for (in)complete array, struct, union or enum
    193  * types.
    194  */
    195 void
    196 setcompl(type_t *tp, int ic)
    197 {
    198 	tspec_t	t;
    199 
    200 	if ((t = tp->t_tspec) == ARRAY) {
    201 		tp->t_aincompl = ic;
    202 	} else if (t == STRUCT || t == UNION) {
    203 		tp->t_str->sincompl = ic;
    204 	} else {
    205 		if (t != ENUM)
    206 			LERROR("setcompl()");
    207 		tp->t_enum->eincompl = ic;
    208 	}
    209 }
    210 
    211 /*
    212  * Remember the storage class of the current declaration in dcs->d_scl
    213  * (the top element of the declaration stack) and detect multiple
    214  * storage classes.
    215  */
    216 void
    217 addscl(scl_t sc)
    218 {
    219 
    220 	if (sc == INLINE) {
    221 		if (dcs->d_inline)
    222 			/* duplicate '%s' */
    223 			warning(10, "inline");
    224 		dcs->d_inline = 1;
    225 		return;
    226 	}
    227 	if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
    228 	    dcs->d_smod != NOTSPEC || dcs->d_lmod != NOTSPEC) {
    229 		/* storage class after type is obsolescent */
    230 		warning(83);
    231 	}
    232 	if (dcs->d_scl == NOSCL) {
    233 		dcs->d_scl = sc;
    234 	} else {
    235 		/*
    236 		 * multiple storage classes. An error will be reported in
    237 		 * deftyp().
    238 		 */
    239 		dcs->d_mscl = 1;
    240 	}
    241 }
    242 
    243 /*
    244  * Remember the type, modifier or typedef name returned by the parser
    245  * in *dcs (top element of decl stack). This information is used in
    246  * deftyp() to build the type used for all declarators in this
    247  * declaration.
    248  *
    249  * Is tp->t_typedef 1, the type comes from a previously defined typename.
    250  * Otherwise it comes from a type specifier (int, long, ...) or a
    251  * struct/union/enum tag.
    252  */
    253 void
    254 addtype(type_t *tp)
    255 {
    256 	tspec_t	t;
    257 //	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 	size_t	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 > (ssize_t)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	dowarn, 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, (dowarn = 0, &dowarn))) {
   1850 
   1851 			if (dowarn) {
   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 *dowarn)
   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, dowarn)) {
   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 static int
   1998 chkqual(type_t *tp1, type_t *tp2, int ignqual)
   1999 {
   2000 	if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
   2001 		return 0;
   2002 
   2003 	if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
   2004 		return 0;
   2005 
   2006 	return 1;
   2007 }
   2008 
   2009 int
   2010 eqptrtype(type_t *tp1, type_t *tp2, int ignqual)
   2011 {
   2012 	if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
   2013 		return 0;
   2014 
   2015 	if (!chkqual(tp1, tp2, ignqual))
   2016 		return 0;
   2017 
   2018 	return 1;
   2019 }
   2020 
   2021 
   2022 /*
   2023  * Checks if two types are compatible. Returns 0 if not, otherwise 1.
   2024  *
   2025  * ignqual	ignore qualifiers of type; used for function params
   2026  * promot	promote left type; used for comparison of params of
   2027  *		old style function definitions with params of prototypes.
   2028  * *dowarn	set to 1 if an old style function declaration is not
   2029  *		compatible with a prototype
   2030  */
   2031 int
   2032 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *dowarn)
   2033 {
   2034 	tspec_t	t;
   2035 
   2036 	while (tp1 != NULL && tp2 != NULL) {
   2037 
   2038 		t = tp1->t_tspec;
   2039 		if (promot) {
   2040 			if (t == FLOAT) {
   2041 				t = DOUBLE;
   2042 			} else if (t == CHAR || t == SCHAR) {
   2043 				t = INT;
   2044 			} else if (t == UCHAR) {
   2045 				t = tflag ? UINT : INT;
   2046 			} else if (t == SHORT) {
   2047 				t = INT;
   2048 			} else if (t == USHORT) {
   2049 				/* CONSTCOND */
   2050 				t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
   2051 			}
   2052 		}
   2053 
   2054 		if (t != tp2->t_tspec)
   2055 			return (0);
   2056 
   2057 		if (!chkqual(tp1, tp2, ignqual))
   2058 			return 0;
   2059 
   2060 		if (t == STRUCT || t == UNION)
   2061 			return (tp1->t_str == tp2->t_str);
   2062 
   2063 		if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
   2064 			if (tp1->t_dim != 0 && tp2->t_dim != 0)
   2065 				return (0);
   2066 		}
   2067 
   2068 		/* dont check prototypes for traditional */
   2069 		if (t == FUNC && !tflag) {
   2070 			if (tp1->t_proto && tp2->t_proto) {
   2071 				if (!eqargs(tp1, tp2, dowarn))
   2072 					return (0);
   2073 			} else if (tp1->t_proto) {
   2074 				if (!mnoarg(tp1, dowarn))
   2075 					return (0);
   2076 			} else if (tp2->t_proto) {
   2077 				if (!mnoarg(tp2, dowarn))
   2078 					return (0);
   2079 			}
   2080 		}
   2081 
   2082 		tp1 = tp1->t_subt;
   2083 		tp2 = tp2->t_subt;
   2084 		ignqual = promot = 0;
   2085 
   2086 	}
   2087 
   2088 	return (tp1 == tp2);
   2089 }
   2090 
   2091 /*
   2092  * Compares the parameter types of two prototypes.
   2093  */
   2094 static int
   2095 eqargs(type_t *tp1, type_t *tp2, int *dowarn)
   2096 {
   2097 	sym_t	*a1, *a2;
   2098 
   2099 	if (tp1->t_vararg != tp2->t_vararg)
   2100 		return (0);
   2101 
   2102 	a1 = tp1->t_args;
   2103 	a2 = tp2->t_args;
   2104 
   2105 	while (a1 != NULL && a2 != NULL) {
   2106 
   2107 		if (eqtype(a1->s_type, a2->s_type, 1, 0, dowarn) == 0)
   2108 			return (0);
   2109 
   2110 		a1 = a1->s_nxt;
   2111 		a2 = a2->s_nxt;
   2112 
   2113 	}
   2114 
   2115 	return (a1 == a2);
   2116 }
   2117 
   2118 /*
   2119  * mnoarg() (matches functions with no argument type information)
   2120  * returns 1 if all parameters of a prototype are compatible with
   2121  * and old style function declaration.
   2122  * This is the case if following conditions are met:
   2123  *	1. the prototype must have a fixed number of parameters
   2124  *	2. no parameter is of type float
   2125  *	3. no parameter is converted to another type if integer promotion
   2126  *	   is applied on it
   2127  */
   2128 static int
   2129 mnoarg(type_t *tp, int *dowarn)
   2130 {
   2131 	sym_t	*arg;
   2132 	tspec_t	t;
   2133 
   2134 	if (tp->t_vararg) {
   2135 		if (dowarn != NULL)
   2136 			*dowarn = 1;
   2137 	}
   2138 	for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) {
   2139 		if ((t = arg->s_type->t_tspec) == FLOAT ||
   2140 		    t == CHAR || t == SCHAR || t == UCHAR ||
   2141 		    t == SHORT || t == USHORT) {
   2142 			if (dowarn != NULL)
   2143 				*dowarn = 1;
   2144 		}
   2145 	}
   2146 	return (1);
   2147 }
   2148 
   2149 /*
   2150  * Compares a prototype declaration with the remembered arguments of
   2151  * a previous old style function definition.
   2152  */
   2153 static int
   2154 chkosdef(sym_t *rdsym, sym_t *dsym)
   2155 {
   2156 	sym_t	*args, *pargs, *arg, *parg;
   2157 	int	narg, nparg, n;
   2158 	int	dowarn, msg;
   2159 
   2160 	args = rdsym->s_args;
   2161 	pargs = dsym->s_type->t_args;
   2162 
   2163 	msg = 0;
   2164 
   2165 	narg = nparg = 0;
   2166 	for (arg = args; arg != NULL; arg = arg->s_nxt)
   2167 		narg++;
   2168 	for (parg = pargs; parg != NULL; parg = parg->s_nxt)
   2169 		nparg++;
   2170 	if (narg != nparg) {
   2171 		/* prototype does not match old-style definition */
   2172 		error(63);
   2173 		msg = 1;
   2174 		goto end;
   2175 	}
   2176 
   2177 	arg = args;
   2178 	parg = pargs;
   2179 	n = 1;
   2180 	while (narg--) {
   2181 		dowarn = 0;
   2182 		/*
   2183 		 * If it does not match due to promotion and sflag is
   2184 		 * not set we print only a warning.
   2185 		 */
   2186 		if (!eqtype(arg->s_type, parg->s_type, 1, 1, &dowarn) || dowarn) {
   2187 			/* prototype does not match old-style def., arg #%d */
   2188 			error(299, n);
   2189 			msg = 1;
   2190 		}
   2191 		arg = arg->s_nxt;
   2192 		parg = parg->s_nxt;
   2193 		n++;
   2194 	}
   2195 
   2196  end:
   2197 	if (msg)
   2198 		/* old style definition */
   2199 		prevdecl(300, rdsym);
   2200 
   2201 	return (msg);
   2202 }
   2203 
   2204 /*
   2205  * Complets a type by copying the dimension and prototype information
   2206  * from a second compatible type.
   2207  *
   2208  * Following lines are legal:
   2209  *  "typedef a[]; a b; a b[10]; a c; a c[20];"
   2210  *  "typedef ft(); ft f; f(int); ft g; g(long);"
   2211  * This means that, if a type is completed, the type structure must
   2212  * be duplicated.
   2213  */
   2214 void
   2215 compltyp(sym_t *dsym, sym_t *ssym)
   2216 {
   2217 	type_t	**dstp, *src;
   2218 	type_t	*dst;
   2219 
   2220 	dstp = &dsym->s_type;
   2221 	src = ssym->s_type;
   2222 
   2223 	while ((dst = *dstp) != NULL) {
   2224 		if (src == NULL || dst->t_tspec != src->t_tspec)
   2225 			LERROR("compltyp()");
   2226 		if (dst->t_tspec == ARRAY) {
   2227 			if (dst->t_dim == 0 && src->t_dim != 0) {
   2228 				*dstp = dst = duptyp(dst);
   2229 				dst->t_dim = src->t_dim;
   2230 				/* now a complete Typ */
   2231 				setcompl(dst, 0);
   2232 			}
   2233 		} else if (dst->t_tspec == FUNC) {
   2234 			if (!dst->t_proto && src->t_proto) {
   2235 				*dstp = dst = duptyp(dst);
   2236 				dst->t_proto = 1;
   2237 				dst->t_args = src->t_args;
   2238 			}
   2239 		}
   2240 		dstp = &dst->t_subt;
   2241 		src = src->t_subt;
   2242 	}
   2243 }
   2244 
   2245 /*
   2246  * Completes the declaration of a single argument.
   2247  */
   2248 sym_t *
   2249 decl1arg(sym_t *sym, int initflg)
   2250 {
   2251 	tspec_t	t;
   2252 
   2253 	chkfdef(sym, 1);
   2254 
   2255 	chktyp(sym);
   2256 
   2257 	if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) {
   2258 		/* redeclaration of formal parameter %s */
   2259 		error(237, sym->s_name);
   2260 		rmsym(dcs->d_rdcsym);
   2261 		sym->s_arg = 1;
   2262 	}
   2263 
   2264 	if (!sym->s_arg) {
   2265 		/* declared argument %s is missing */
   2266 		error(53, sym->s_name);
   2267 		sym->s_arg = 1;
   2268 	}
   2269 
   2270 	if (initflg) {
   2271 		/* cannot initialize parameter: %s */
   2272 		error(52, sym->s_name);
   2273 		initerr = 1;
   2274 	}
   2275 
   2276 	if ((t = sym->s_type->t_tspec) == ARRAY) {
   2277 		sym->s_type = incref(sym->s_type->t_subt, PTR);
   2278 	} else if (t == FUNC) {
   2279 		if (tflag)
   2280 			/* a function is declared as an argument: %s */
   2281 			warning(50, sym->s_name);
   2282 		sym->s_type = incref(sym->s_type, PTR);
   2283 	} else if (t == FLOAT) {
   2284 		if (tflag)
   2285 			sym->s_type = gettyp(DOUBLE);
   2286 	}
   2287 
   2288 	if (dcs->d_inline)
   2289 		/* argument declared inline: %s */
   2290 		warning(269, sym->s_name);
   2291 
   2292 	/*
   2293 	 * Arguments must have complete types. lengths() prints the needed
   2294 	 * error messages (null dimension is impossible because arrays are
   2295 	 * converted to pointers).
   2296 	 */
   2297 	if (sym->s_type->t_tspec != VOID)
   2298 		(void)length(sym->s_type, sym->s_name);
   2299 
   2300 	setsflg(sym);
   2301 
   2302 	return (sym);
   2303 }
   2304 
   2305 /*
   2306  * Does some checks for lint directives which apply to functions.
   2307  * Processes arguments in old style function definitions which default
   2308  * to int.
   2309  * Checks compatiblility of old style function definition with previous
   2310  * prototype.
   2311  */
   2312 void
   2313 cluparg(void)
   2314 {
   2315 	sym_t	*args, *arg, *pargs, *parg;
   2316 	int	narg, nparg, n, msg;
   2317 	tspec_t	t;
   2318 
   2319 	args = funcsym->s_args;
   2320 	pargs = funcsym->s_type->t_args;
   2321 
   2322 	/* check for illegal combinations of lint directives */
   2323 	if (prflstrg != -1 && scflstrg != -1) {
   2324 		/* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
   2325 		warning(289);
   2326 		prflstrg = scflstrg = -1;
   2327 	}
   2328 	if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) {
   2329 		/* dubious use of ** VARARGS ** with ** %s ** */
   2330 		warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE");
   2331 		nvararg = -1;
   2332 	}
   2333 
   2334 	/*
   2335 	 * check if the argument of a lint directive is compatible with the
   2336 	 * number of arguments.
   2337 	 */
   2338 	narg = 0;
   2339 	for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_nxt)
   2340 		narg++;
   2341 	if (nargusg > narg) {
   2342 		/* argument number mismatch with directive: ** %s ** */
   2343 		warning(283, "ARGSUSED");
   2344 		nargusg = 0;
   2345 	}
   2346 	if (nvararg > narg) {
   2347 		/* argument number mismatch with directive: ** %s ** */
   2348 		warning(283, "VARARGS");
   2349 		nvararg = 0;
   2350 	}
   2351 	if (prflstrg > narg) {
   2352 		/* argument number mismatch with directive: ** %s ** */
   2353 		warning(283, "PRINTFLIKE");
   2354 		prflstrg = -1;
   2355 	} else if (prflstrg == 0) {
   2356 		prflstrg = -1;
   2357 	}
   2358 	if (scflstrg > narg) {
   2359 		/* argument number mismatch with directive: ** %s ** */
   2360 		warning(283, "SCANFLIKE");
   2361 		scflstrg = -1;
   2362 	} else if (scflstrg == 0) {
   2363 		scflstrg = -1;
   2364 	}
   2365 	if (prflstrg != -1 || scflstrg != -1) {
   2366 		narg = prflstrg != -1 ? prflstrg : scflstrg;
   2367 		arg = dcs->d_fargs;
   2368 		for (n = 1; n < narg; n++)
   2369 			arg = arg->s_nxt;
   2370 		if (arg->s_type->t_tspec != PTR ||
   2371 		    ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
   2372 		     t != UCHAR && t != SCHAR)) {
   2373 			/* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
   2374 			warning(293, narg);
   2375 			prflstrg = scflstrg = -1;
   2376 		}
   2377 	}
   2378 
   2379 	/*
   2380 	 * print a warning for each argument off an old style function
   2381 	 * definition which defaults to int
   2382 	 */
   2383 	for (arg = args; arg != NULL; arg = arg->s_nxt) {
   2384 		if (arg->s_defarg) {
   2385 			/* argument type defaults to int: %s */
   2386 			warning(32, arg->s_name);
   2387 			arg->s_defarg = 0;
   2388 			setsflg(arg);
   2389 		}
   2390 	}
   2391 
   2392 	/*
   2393 	 * If this is an old style function definition and a prototyp
   2394 	 * exists, compare the types of arguments.
   2395 	 */
   2396 	if (funcsym->s_osdef && funcsym->s_type->t_proto) {
   2397 		/*
   2398 		 * If the number of arguments does not macht, we need not
   2399 		 * continue.
   2400 		 */
   2401 		narg = nparg = 0;
   2402 		msg = 0;
   2403 		for (parg = pargs; parg != NULL; parg = parg->s_nxt)
   2404 			nparg++;
   2405 		for (arg = args; arg != NULL; arg = arg->s_nxt)
   2406 			narg++;
   2407 		if (narg != nparg) {
   2408 			/* parameter mismatch: %d declared, %d defined */
   2409 			error(51, nparg, narg);
   2410 			msg = 1;
   2411 		} else {
   2412 			parg = pargs;
   2413 			arg = args;
   2414 			while (narg--) {
   2415 				msg |= chkptdecl(arg, parg);
   2416 				parg = parg->s_nxt;
   2417 				arg = arg->s_nxt;
   2418 			}
   2419 		}
   2420 		if (msg)
   2421 			/* prototype declaration */
   2422 			prevdecl(285, dcs->d_rdcsym);
   2423 
   2424 		/* from now the prototype is valid */
   2425 		funcsym->s_osdef = 0;
   2426 		funcsym->s_args = NULL;
   2427 
   2428 	}
   2429 
   2430 }
   2431 
   2432 /*
   2433  * Checks compatibility of an old style function definition with a previous
   2434  * prototype declaration.
   2435  * Returns 1 if the position of the previous declaration should be reported.
   2436  */
   2437 static int
   2438 chkptdecl(sym_t *arg, sym_t *parg)
   2439 {
   2440 	type_t	*tp, *ptp;
   2441 	int	dowarn, msg;
   2442 
   2443 	tp = arg->s_type;
   2444 	ptp = parg->s_type;
   2445 
   2446 	msg = 0;
   2447 	dowarn = 0;
   2448 
   2449 	if (!eqtype(tp, ptp, 1, 1, &dowarn)) {
   2450 		if (eqtype(tp, ptp, 1, 0, &dowarn)) {
   2451 			/* type does not match prototype: %s */
   2452 			msg = gnuism(58, arg->s_name);
   2453 		} else {
   2454 			/* type does not match prototype: %s */
   2455 			error(58, arg->s_name);
   2456 			msg = 1;
   2457 		}
   2458 	} else if (dowarn) {
   2459 		/* type does not match prototype: %s */
   2460 		(*(sflag ? error : warning))(58, arg->s_name);
   2461 		msg = 1;
   2462 	}
   2463 
   2464 	return (msg);
   2465 }
   2466 
   2467 /*
   2468  * Completes a single local declaration/definition.
   2469  */
   2470 void
   2471 decl1loc(sym_t *dsym, int initflg)
   2472 {
   2473 
   2474 	/* Correct a mistake done in dname(). */
   2475 	if (dsym->s_type->t_tspec == FUNC) {
   2476 		dsym->s_def = DECL;
   2477 		if (dcs->d_scl == NOSCL)
   2478 			dsym->s_scl = EXTERN;
   2479 	}
   2480 
   2481 	if (dsym->s_type->t_tspec == FUNC) {
   2482 		if (dsym->s_scl == STATIC) {
   2483 			/* dubious static function at block level: %s */
   2484 			warning(93, dsym->s_name);
   2485 			dsym->s_scl = EXTERN;
   2486 		} else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
   2487 			/* function has illegal storage class: %s */
   2488 			error(94, dsym->s_name);
   2489 			dsym->s_scl = EXTERN;
   2490 		}
   2491 	}
   2492 
   2493 	/*
   2494 	 * functions may be declared inline at local scope, although
   2495 	 * this has no effect for a later definition of the same
   2496 	 * function.
   2497 	 * XXX it should have an effect if tflag is set. this would
   2498 	 * also be the way gcc behaves.
   2499 	 */
   2500 	if (dcs->d_inline) {
   2501 		if (dsym->s_type->t_tspec == FUNC) {
   2502 			dsym->s_inline = 1;
   2503 		} else {
   2504 			/* variable declared inline: %s */
   2505 			warning(268, dsym->s_name);
   2506 		}
   2507 	}
   2508 
   2509 	chkfdef(dsym, 1);
   2510 
   2511 	chktyp(dsym);
   2512 
   2513 	if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN)
   2514 		ledecl(dsym);
   2515 
   2516 	if (dsym->s_scl == EXTERN) {
   2517 		/*
   2518 		 * XXX wenn die statische Variable auf Ebene 0 erst
   2519 		 * spaeter definiert wird, haben wir die Brille auf.
   2520 		 */
   2521 		if (dsym->s_xsym == NULL) {
   2522 			outsym(dsym, EXTERN, dsym->s_def);
   2523 		} else {
   2524 			outsym(dsym, dsym->s_xsym->s_scl, dsym->s_def);
   2525 		}
   2526 	}
   2527 
   2528 	if (dcs->d_rdcsym != NULL) {
   2529 
   2530 		if (dcs->d_rdcsym->s_blklev == 0) {
   2531 
   2532 			switch (dsym->s_scl) {
   2533 			case AUTO:
   2534 				/* automatic hides external declaration: %s */
   2535 				if (hflag)
   2536 					warning(86, dsym->s_name);
   2537 				break;
   2538 			case STATIC:
   2539 				/* static hides external declaration: %s */
   2540 				if (hflag)
   2541 					warning(87, dsym->s_name);
   2542 				break;
   2543 			case TYPEDEF:
   2544 				/* typedef hides  external declaration: %s */
   2545 				if (hflag)
   2546 					warning(88, dsym->s_name);
   2547 				break;
   2548 			case EXTERN:
   2549 				/*
   2550 				 * Warnings and errors are printed in ledecl()
   2551 				 */
   2552 				break;
   2553 			default:
   2554 				LERROR("decl1loc()");
   2555 			}
   2556 
   2557 		} else if (dcs->d_rdcsym->s_blklev == blklev) {
   2558 
   2559 			/* no hflag, because its illegal! */
   2560 			if (dcs->d_rdcsym->s_arg) {
   2561 				/*
   2562 				 * if !tflag, a "redeclaration of %s" error
   2563 				 * is produced below
   2564 				 */
   2565 				if (tflag) {
   2566 					if (hflag)
   2567 						/* decl. hides parameter: %s */
   2568 						warning(91, dsym->s_name);
   2569 					rmsym(dcs->d_rdcsym);
   2570 				}
   2571 			}
   2572 
   2573 		} else if (dcs->d_rdcsym->s_blklev < blklev) {
   2574 
   2575 			if (hflag)
   2576 				/* declaration hides earlier one: %s */
   2577 				warning(95, dsym->s_name);
   2578 
   2579 		}
   2580 
   2581 		if (dcs->d_rdcsym->s_blklev == blklev) {
   2582 
   2583 			/* redeclaration of %s */
   2584 			error(27, dsym->s_name);
   2585 			rmsym(dcs->d_rdcsym);
   2586 
   2587 		}
   2588 
   2589 	}
   2590 
   2591 	if (initflg && !(initerr = chkinit(dsym))) {
   2592 		dsym->s_def = DEF;
   2593 		setsflg(dsym);
   2594 	}
   2595 
   2596 	if (dsym->s_scl == TYPEDEF) {
   2597 		dsym->s_type = duptyp(dsym->s_type);
   2598 		dsym->s_type->t_typedef = 1;
   2599 		settdsym(dsym->s_type, dsym);
   2600 	}
   2601 
   2602 	/*
   2603 	 * Before we can check the size we must wait for a initialisation
   2604 	 * which may follow.
   2605 	 */
   2606 }
   2607 
   2608 /*
   2609  * Processes (re)declarations of external Symbols inside blocks.
   2610  */
   2611 static void
   2612 ledecl(sym_t *dsym)
   2613 {
   2614 	int	eqt, dowarn;
   2615 	sym_t	*esym;
   2616 
   2617 	/* look for a symbol with the same name */
   2618 	esym = dcs->d_rdcsym;
   2619 	while (esym != NULL && esym->s_blklev != 0) {
   2620 		while ((esym = esym->s_link) != NULL) {
   2621 			if (esym->s_kind != FVFT)
   2622 				continue;
   2623 			if (strcmp(dsym->s_name, esym->s_name) == 0)
   2624 				break;
   2625 		}
   2626 	}
   2627 	if (esym == NULL)
   2628 		return;
   2629 	if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
   2630 		/* gcc accepts this without a warning, pcc prints an error. */
   2631 		/* redeclaration of %s */
   2632 		warning(27, dsym->s_name);
   2633 		prevdecl(-1, esym);
   2634 		return;
   2635 	}
   2636 
   2637 	dowarn = 0;
   2638 	eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &dowarn);
   2639 
   2640 	if (!eqt || dowarn) {
   2641 		if (esym->s_scl == EXTERN) {
   2642 			/* inconsistent redeclaration of extern: %s */
   2643 			warning(90, dsym->s_name);
   2644 			prevdecl(-1, esym);
   2645 		} else {
   2646 			/* inconsistent redeclaration of static: %s */
   2647 			warning(92, dsym->s_name);
   2648 			prevdecl(-1, esym);
   2649 		}
   2650 	}
   2651 
   2652 	if (eqt) {
   2653 		/*
   2654 		 * Remember the external symbol so we can update usage
   2655 		 * information at the end of the block.
   2656 		 */
   2657 		dsym->s_xsym = esym;
   2658 	}
   2659 }
   2660 
   2661 /*
   2662  * Print an error or a warning if the symbol cant be initialized due
   2663  * to type/storage class. Returnvalue is 1 if an error has been
   2664  * detected.
   2665  */
   2666 static int
   2667 chkinit(sym_t *sym)
   2668 {
   2669 	int	erred;
   2670 
   2671 	erred = 0;
   2672 
   2673 	if (sym->s_type->t_tspec == FUNC) {
   2674 		/* cannot initialize function: %s */
   2675 		error(24, sym->s_name);
   2676 		erred = 1;
   2677 	} else if (sym->s_scl == TYPEDEF) {
   2678 		/* cannot initialize typedef: %s */
   2679 		error(25, sym->s_name);
   2680 		erred = 1;
   2681 	} else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
   2682 		/* cannot initialize "extern" declaration: %s */
   2683 		if (dcs->d_ctx == EXTERN) {
   2684 			warning(26, sym->s_name);
   2685 		} else {
   2686 			error(26, sym->s_name);
   2687 			erred = 1;
   2688 		}
   2689 	}
   2690 
   2691 	return (erred);
   2692 }
   2693 
   2694 /*
   2695  * Create a symbole for an abstract declaration.
   2696  */
   2697 sym_t *
   2698 aname(void)
   2699 {
   2700 	sym_t	*sym;
   2701 
   2702 	if (dcs->d_ctx != ABSTRACT && dcs->d_ctx != PARG)
   2703 		LERROR("aname()");
   2704 
   2705 	sym = getblk(sizeof (sym_t));
   2706 
   2707 	sym->s_name = unnamed;
   2708 	sym->s_def = DEF;
   2709 	sym->s_scl = ABSTRACT;
   2710 	sym->s_blklev = -1;
   2711 
   2712 	if (dcs->d_ctx == PARG)
   2713 		sym->s_arg = 1;
   2714 
   2715 	sym->s_type = dcs->d_type;
   2716 	dcs->d_rdcsym = NULL;
   2717 	dcs->d_vararg = 0;
   2718 
   2719 	return (sym);
   2720 }
   2721 
   2722 /*
   2723  * Removes anything which has nothing to do on global level.
   2724  */
   2725 void
   2726 globclup(void)
   2727 {
   2728 
   2729 	while (dcs->d_nxt != NULL)
   2730 		popdecl();
   2731 
   2732 	cleanup();
   2733 	blklev = 0;
   2734 	mblklev = 0;
   2735 
   2736 	/*
   2737 	 * remove all informations about pending lint directives without
   2738 	 * warnings.
   2739 	 */
   2740 	glclup(1);
   2741 }
   2742 
   2743 /*
   2744  * Process an abstract type declaration
   2745  */
   2746 sym_t *
   2747 decl1abs(sym_t *sym)
   2748 {
   2749 
   2750 	chkfdef(sym, 1);
   2751 	chktyp(sym);
   2752 	return (sym);
   2753 }
   2754 
   2755 /*
   2756  * Checks size after declarations of variables and their initialisation.
   2757  */
   2758 void
   2759 chksz(sym_t *dsym)
   2760 {
   2761 
   2762 	/*
   2763 	 * check size only for symbols which are defined and no function and
   2764 	 * not typedef name
   2765 	 */
   2766 	if (dsym->s_def != DEF)
   2767 		return;
   2768 	if (dsym->s_scl == TYPEDEF)
   2769 		return;
   2770 	if (dsym->s_type->t_tspec == FUNC)
   2771 		return;
   2772 
   2773 	if (length(dsym->s_type, dsym->s_name) == 0 &&
   2774 	    dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
   2775 		/* empty array declaration: %s */
   2776 		if (tflag) {
   2777 			warning(190, dsym->s_name);
   2778 		} else {
   2779 			error(190, dsym->s_name);
   2780 		}
   2781 	}
   2782 }
   2783 
   2784 /*
   2785  * Mark an object as set if it is not already
   2786  */
   2787 void
   2788 setsflg(sym_t *sym)
   2789 {
   2790 
   2791 	if (!sym->s_set) {
   2792 		sym->s_set = 1;
   2793 		UNIQUE_CURR_POS(sym->s_spos);
   2794 	}
   2795 }
   2796 
   2797 /*
   2798  * Mark an object as used if it is not already
   2799  */
   2800 void
   2801 setuflg(sym_t *sym, int fcall, int szof)
   2802 {
   2803 
   2804 	if (!sym->s_used) {
   2805 		sym->s_used = 1;
   2806 		UNIQUE_CURR_POS(sym->s_upos);
   2807 	}
   2808 	/*
   2809 	 * for function calls another record is written
   2810 	 *
   2811 	 * XXX Should symbols used in sizeof() treated as used or not?
   2812 	 * Probably not, because there is no sense to declare an
   2813 	 * external variable only to get their size.
   2814 	 */
   2815 	if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
   2816 		outusg(sym);
   2817 }
   2818 
   2819 /*
   2820  * Prints warnings for a list of variables and labels (concatenated
   2821  * with s_dlnxt) if these are not used or only set.
   2822  */
   2823 void
   2824 chkusage(dinfo_t *di)
   2825 {
   2826 	sym_t	*sym;
   2827 	int	mknowarn;
   2828 
   2829 	/* for this warnings LINTED has no effect */
   2830 	mknowarn = nowarn;
   2831 	nowarn = 0;
   2832 
   2833 #ifdef DEBUG
   2834 	printf("%s, %d: >temp nowarn = 0\n", curr_pos.p_file, curr_pos.p_line);
   2835 #endif
   2836 	for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
   2837 		chkusg1(di->d_asm, sym);
   2838 	nowarn = mknowarn;
   2839 #ifdef DEBUG
   2840 	printf("%s, %d: <temp nowarn = %d\n", curr_pos.p_file, curr_pos.p_line,
   2841 	    nowarn);
   2842 #endif
   2843 }
   2844 
   2845 /*
   2846  * Prints a warning for a single variable or label if it is not used or
   2847  * only set.
   2848  */
   2849 void
   2850 chkusg1(int novar, sym_t *sym)
   2851 {
   2852 	pos_t	cpos;
   2853 
   2854 	if (sym->s_blklev == -1)
   2855 		return;
   2856 
   2857 	STRUCT_ASSIGN(cpos, curr_pos);
   2858 
   2859 	if (sym->s_kind == FVFT) {
   2860 		if (sym->s_arg) {
   2861 			chkausg(novar, sym);
   2862 		} else {
   2863 			chkvusg(novar, sym);
   2864 		}
   2865 	} else if (sym->s_kind == FLAB) {
   2866 		chklusg(sym);
   2867 	} else if (sym->s_kind == FTAG) {
   2868 		chktusg(sym);
   2869 	}
   2870 
   2871 	STRUCT_ASSIGN(curr_pos, cpos);
   2872 }
   2873 
   2874 static void
   2875 chkausg(int novar, sym_t *arg)
   2876 {
   2877 
   2878 	if (!arg->s_set)
   2879 		LERROR("chkausg()");
   2880 
   2881 	if (novar)
   2882 		return;
   2883 
   2884 	if (!arg->s_used && vflag) {
   2885 		STRUCT_ASSIGN(curr_pos, arg->s_dpos);
   2886 		/* argument %s unused in function %s */
   2887 		warning(231, arg->s_name, funcsym->s_name);
   2888 	}
   2889 }
   2890 
   2891 static void
   2892 chkvusg(int novar, sym_t *sym)
   2893 {
   2894 	scl_t	sc;
   2895 	sym_t	*xsym;
   2896 
   2897 	if (blklev == 0 || sym->s_blklev == 0)
   2898 		LERROR("chkvusg()");
   2899 
   2900 	/* errors in expressions easily cause lots of these warnings */
   2901 	if (nerr != 0)
   2902 		return;
   2903 
   2904 	/*
   2905 	 * XXX Only variables are checkd, although types should
   2906 	 * probably also be checked
   2907 	 */
   2908 	if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
   2909 	    sc != AUTO && sc != REG) {
   2910 		return;
   2911 	}
   2912 
   2913 	if (novar)
   2914 		return;
   2915 
   2916 	if (sc == EXTERN) {
   2917 		if (!sym->s_used && !sym->s_set) {
   2918 			STRUCT_ASSIGN(curr_pos, sym->s_dpos);
   2919 			/* %s unused in function %s */
   2920 			warning(192, sym->s_name, funcsym->s_name);
   2921 		}
   2922 	} else {
   2923 		if (sym->s_set && !sym->s_used) {
   2924 			STRUCT_ASSIGN(curr_pos, sym->s_spos);
   2925 			/* %s set but not used in function %s */
   2926 			warning(191, sym->s_name, funcsym->s_name);
   2927 		} else if (!sym->s_used) {
   2928 			STRUCT_ASSIGN(curr_pos, sym->s_dpos);
   2929 			/* %s unused in function %s */
   2930 			warning(192, sym->s_name, funcsym->s_name);
   2931 		}
   2932 	}
   2933 
   2934 	if (sc == EXTERN) {
   2935 		/*
   2936 		 * information about usage is taken over into the symbol
   2937 		 * tabel entry at level 0 if the symbol was locally declared
   2938 		 * as an external symbol.
   2939 		 *
   2940 		 * XXX This is wrong for symbols declared static at level 0
   2941 		 * if the usage information stems from sizeof(). This is
   2942 		 * because symbols at level 0 only used in sizeof() are
   2943 		 * considered to not be used.
   2944 		 */
   2945 		if ((xsym = sym->s_xsym) != NULL) {
   2946 			if (sym->s_used && !xsym->s_used) {
   2947 				xsym->s_used = 1;
   2948 				STRUCT_ASSIGN(xsym->s_upos, sym->s_upos);
   2949 			}
   2950 			if (sym->s_set && !xsym->s_set) {
   2951 				xsym->s_set = 1;
   2952 				STRUCT_ASSIGN(xsym->s_spos, sym->s_spos);
   2953 			}
   2954 		}
   2955 	}
   2956 }
   2957 
   2958 static void
   2959 chklusg(sym_t *lab)
   2960 {
   2961 
   2962 	if (blklev != 1 || lab->s_blklev != 1)
   2963 		LERROR("chklusg()");
   2964 
   2965 	if (lab->s_set && !lab->s_used) {
   2966 		STRUCT_ASSIGN(curr_pos, lab->s_spos);
   2967 		/* label %s unused in function %s */
   2968 		warning(192, lab->s_name, funcsym->s_name);
   2969 	} else if (!lab->s_set) {
   2970 		STRUCT_ASSIGN(curr_pos, lab->s_upos);
   2971 		/* undefined label %s */
   2972 		warning(23, lab->s_name);
   2973 	}
   2974 }
   2975 
   2976 static void
   2977 chktusg(sym_t *sym)
   2978 {
   2979 
   2980 	if (!incompl(sym->s_type))
   2981 		return;
   2982 
   2983 	/* complain alwasy about incomplet tags declared inside blocks */
   2984 	if (!zflag || dcs->d_ctx != EXTERN)
   2985 		return;
   2986 
   2987 	STRUCT_ASSIGN(curr_pos, sym->s_dpos);
   2988 	switch (sym->s_type->t_tspec) {
   2989 	case STRUCT:
   2990 		/* struct %s never defined */
   2991 		warning(233, sym->s_name);
   2992 		break;
   2993 	case UNION:
   2994 		/* union %s never defined */
   2995 		warning(234, sym->s_name);
   2996 		break;
   2997 	case ENUM:
   2998 		/* enum %s never defined */
   2999 		warning(235, sym->s_name);
   3000 		break;
   3001 	default:
   3002 		LERROR("chktusg()");
   3003 	}
   3004 }
   3005 
   3006 /*
   3007  * Called after the entire translation unit has been parsed.
   3008  * Changes tentative definitions in definitions.
   3009  * Performs some tests on global Symbols. Detected Problems are:
   3010  * - defined variables of incomplete type
   3011  * - constant variables which are not initialized
   3012  * - static symbols which are never used
   3013  */
   3014 void
   3015 chkglsyms(void)
   3016 {
   3017 	sym_t	*sym;
   3018 	pos_t	cpos;
   3019 
   3020 	if (blklev != 0 || dcs->d_nxt != NULL)
   3021 		norecover();
   3022 
   3023 	STRUCT_ASSIGN(cpos, curr_pos);
   3024 
   3025 	for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
   3026 		if (sym->s_blklev == -1)
   3027 			continue;
   3028 		if (sym->s_kind == FVFT) {
   3029 			chkglvar(sym);
   3030 		} else if (sym->s_kind == FTAG) {
   3031 			chktusg(sym);
   3032 		} else {
   3033 			if (sym->s_kind != FMOS)
   3034 				LERROR("chkglsyms()");
   3035 		}
   3036 	}
   3037 
   3038 	STRUCT_ASSIGN(curr_pos, cpos);
   3039 }
   3040 
   3041 static void
   3042 chkglvar(sym_t *sym)
   3043 {
   3044 
   3045 	if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON)
   3046 		return;
   3047 
   3048 	if (sym->s_scl != EXTERN && sym->s_scl != STATIC)
   3049 		LERROR("chkglvar()");
   3050 
   3051 	glchksz(sym);
   3052 
   3053 	if (sym->s_scl == STATIC) {
   3054 		if (sym->s_type->t_tspec == FUNC) {
   3055 			if (sym->s_used && sym->s_def != DEF) {
   3056 				STRUCT_ASSIGN(curr_pos, sym->s_upos);
   3057 				/* static func. called but not def.. */
   3058 				error(225, sym->s_name);
   3059 			}
   3060 		}
   3061 		if (!sym->s_used) {
   3062 			STRUCT_ASSIGN(curr_pos, sym->s_dpos);
   3063 			if (sym->s_type->t_tspec == FUNC) {
   3064 				if (sym->s_def == DEF) {
   3065 					if (!sym->s_inline)
   3066 						/* static function %s unused */
   3067 						warning(236, sym->s_name);
   3068 				} else {
   3069 					/* static function %s decl. but ... */
   3070 					warning(290, sym->s_name);
   3071 				}
   3072 			} else if (!sym->s_set) {
   3073 				/* static variable %s unused */
   3074 				warning(226, sym->s_name);
   3075 			} else {
   3076 				/* static variable %s set but not used */
   3077 				warning(307, sym->s_name);
   3078 			}
   3079 		}
   3080 		if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
   3081 			STRUCT_ASSIGN(curr_pos, sym->s_dpos);
   3082 			/* const object %s should have initializer */
   3083 			warning(227, sym->s_name);
   3084 		}
   3085 	}
   3086 }
   3087 
   3088 static void
   3089 glchksz(sym_t *sym)
   3090 {
   3091 
   3092 	if (sym->s_def == TDEF) {
   3093 		if (sym->s_type->t_tspec == FUNC)
   3094 			/*
   3095 			 * this can happen if an syntax error occurred
   3096 			 * after a function declaration
   3097 			 */
   3098 			return;
   3099 		STRUCT_ASSIGN(curr_pos, sym->s_dpos);
   3100 		if (length(sym->s_type, sym->s_name) == 0 &&
   3101 		    sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
   3102 			/* empty array declaration: %s */
   3103 			if (tflag || (sym->s_scl == EXTERN && !sflag)) {
   3104 				warning(190, sym->s_name);
   3105 			} else {
   3106 				error(190, sym->s_name);
   3107 			}
   3108 		}
   3109 	}
   3110 }
   3111 
   3112 /*
   3113  * Prints information about location of previous definition/declaration.
   3114  */
   3115 void
   3116 prevdecl(int msg, sym_t *psym)
   3117 {
   3118 	pos_t	cpos;
   3119 
   3120 	if (!rflag)
   3121 		return;
   3122 
   3123 	STRUCT_ASSIGN(cpos, curr_pos);
   3124 	STRUCT_ASSIGN(curr_pos, psym->s_dpos);
   3125 	if (msg != -1) {
   3126 		message(msg, psym->s_name);
   3127 	} else if (psym->s_def == DEF || psym->s_def == TDEF) {
   3128 		/* previous definition of %s */
   3129 		message(261, psym->s_name);
   3130 	} else {
   3131 		/* previous declaration of %s */
   3132 		message(260, psym->s_name);
   3133 	}
   3134 	STRUCT_ASSIGN(curr_pos, cpos);
   3135 }
   3136