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