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