Home | History | Annotate | Line # | Download | only in lint1
decl.c revision 1.88
      1 /* $NetBSD: decl.c,v 1.88 2021/01/01 00:00:24 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.88 2021/01/01 00:00:24 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	merge_type_specifiers(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 tduptyp())
    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 	*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 	*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 add_storage_class(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 add_type(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("add_type()");
    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, basic_type_name(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(merge_type_specifiers(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 it's 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_next;
    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_next) {
    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 add_attr_used(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 add_qualifier(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("add_qualifier()");
    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_next = 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_next == NULL)
    614 		LERROR("popdecl()");
    615 	di = dcs;
    616 	dcs = di->d_next;
    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 global_clean_up_decl(), which is used to do some cleanup
    688  * after global declarations/definitions.
    689  */
    690 void
    691 setasm(void)
    692 {
    693 	dinfo_t	*di;
    694 
    695 	for (di = dcs; di != NULL; di = di->d_next)
    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 'long ... */
    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)", basic_type_name(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(merge_type_specifiers(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 merge_type_specifiers(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_next. 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_next != NULL)
    976 		l1 = l1->s_next;
    977 	l1->s_next = 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_bitfield) {
   1110 		/*
   1111 		 * only unsigned and signed int are portable bit-field types
   1112 		 *(at least in ANSI C, in traditional C only unsigned int)
   1113 		 */
   1114 		if (t == CHAR || t == UCHAR || t == SCHAR ||
   1115 		    t == SHORT || t == USHORT || t == ENUM) {
   1116 			if (bitfieldtype_ok == 0) {
   1117 				if (sflag) {
   1118 					char buf[64];
   1119 					/*
   1120 					 * bit-field type '%s' invalid in
   1121 					 * ANSI C
   1122 					 */
   1123 					warning(273,
   1124 					    tyname(buf, sizeof(buf), tp));
   1125 				} else if (pflag) {
   1126 					/* nonportable bit-field type */
   1127 					warning(34);
   1128 				}
   1129 			}
   1130 		} else if (t == INT && dcs->d_smod == NOTSPEC) {
   1131 			if (pflag && bitfieldtype_ok == 0) {
   1132 				/* nonportable bit-field type */
   1133 				warning(34);
   1134 			}
   1135 		} else if (t != INT && t != UINT) {
   1136 			/*
   1137 			 * Non-integer types are always illegal for
   1138 			 * bitfields, regardless of BITFIELDTYPE.
   1139 			 * Integer types not dealt with above are
   1140 			 * okay only if BITFIELDTYPE is in effect.
   1141 			 */
   1142 			if (bitfieldtype_ok == 0 || tspec_is_int(t) == 0) {
   1143 				/* illegal bit-field type */
   1144 				warning(35);
   1145 				sz = tp->t_flen;
   1146 				dsym->s_type = tp = duptyp(gettyp(t = INT));
   1147 				if ((tp->t_flen = sz) > size(t))
   1148 					tp->t_flen = size(t);
   1149 			}
   1150 		}
   1151 		if ((len = tp->t_flen) < 0 || len > (ssize_t)size(t)) {
   1152 			/* illegal bit-field size: %d */
   1153 			error(36, len);
   1154 			tp->t_flen = size(t);
   1155 		} else if (len == 0 && dsym->s_name != unnamed) {
   1156 			/* zero size bit-field */
   1157 			error(37);
   1158 			tp->t_flen = size(t);
   1159 		}
   1160 		if (dsym->s_scl == MOU) {
   1161 			/* illegal use of bit-field */
   1162 			error(41);
   1163 			dsym->s_type->t_isfield = 0;
   1164 			dsym->s_bitfield = 0;
   1165 		}
   1166 	} else if (t == FUNC) {
   1167 		/* function illegal in structure or union */
   1168 		error(38);
   1169 		dsym->s_type = tp = incref(tp, t = PTR);
   1170 	}
   1171 
   1172 	/*
   1173 	 * bit-fields of length 0 are not warned about because length()
   1174 	 * does not return the length of the bit-field but the length
   1175 	 * of the type the bit-field is packed in (it's ok)
   1176 	 */
   1177 	if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
   1178 		if (t == ARRAY && dsym->s_type->t_dim == 0) {
   1179 			/* illegal zero sized structure member: %s */
   1180 			c99ism(39, dsym->s_name);
   1181 		}
   1182 	}
   1183 
   1184 	if (dcs->d_ctx == MOU) {
   1185 		o = dcs->d_offset;
   1186 		dcs->d_offset = 0;
   1187 	}
   1188 	if (dsym->s_bitfield) {
   1189 		align(getbound(tp), tp->t_flen);
   1190 		dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t);
   1191 		tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
   1192 		dcs->d_offset += tp->t_flen;
   1193 	} else {
   1194 		align(getbound(tp), 0);
   1195 		dsym->s_value.v_quad = dcs->d_offset;
   1196 		dcs->d_offset += sz;
   1197 	}
   1198 	if (dcs->d_ctx == MOU) {
   1199 		if (o > dcs->d_offset)
   1200 			dcs->d_offset = o;
   1201 	}
   1202 
   1203 	check_function_definition(dsym, 0);
   1204 
   1205 	/*
   1206 	 * Clear the BITFIELDTYPE indicator after processing each
   1207 	 * structure element.
   1208 	 */
   1209 	bitfieldtype_ok = 0;
   1210 
   1211 	return dsym;
   1212 }
   1213 
   1214 /*
   1215  * Aligns next structure element as required.
   1216  *
   1217  * al contains the required alignment, len the length of a bit-field.
   1218  */
   1219 static void
   1220 align(int al, int len)
   1221 {
   1222 	int	no;
   1223 
   1224 	/*
   1225 	 * The alignment of the current element becomes the alignment of
   1226 	 * the struct/union if it is larger than the current alignment
   1227 	 * of the struct/union.
   1228 	 */
   1229 	if (al > dcs->d_stralign)
   1230 		dcs->d_stralign = al;
   1231 
   1232 	no = (dcs->d_offset + (al - 1)) & ~(al - 1);
   1233 	if (len == 0 || dcs->d_offset + len > no)
   1234 		dcs->d_offset = no;
   1235 }
   1236 
   1237 /*
   1238  * Remember the width of the field in its type structure.
   1239  */
   1240 sym_t *
   1241 bitfield(sym_t *dsym, int len)
   1242 {
   1243 
   1244 	if (dsym == NULL) {
   1245 		dsym = getblk(sizeof (sym_t));
   1246 		dsym->s_name = unnamed;
   1247 		dsym->s_kind = FMEMBER;
   1248 		dsym->s_scl = MOS;
   1249 		dsym->s_type = gettyp(UINT);
   1250 		dsym->s_blklev = -1;
   1251 	}
   1252 	dsym->s_type = duptyp(dsym->s_type);
   1253 	dsym->s_type->t_isfield = 1;
   1254 	dsym->s_type->t_flen = len;
   1255 	dsym->s_bitfield = 1;
   1256 	return dsym;
   1257 }
   1258 
   1259 /*
   1260  * Collect information about a sequence of asterisks and qualifiers in a
   1261  * list of type pqinf_t.
   1262  * Qualifiers always refer to the left asterisk.
   1263  * The rightmost asterisk will be at the top of the list.
   1264  */
   1265 pqinf_t *
   1266 merge_pointers_and_qualifiers(pqinf_t *p1, pqinf_t *p2)
   1267 {
   1268 	pqinf_t	*p;
   1269 
   1270 	if (p2->p_pcnt != 0) {
   1271 		/* left '*' at the end of the list */
   1272 		for (p = p2; p->p_next != NULL; p = p->p_next)
   1273 			continue;
   1274 		p->p_next = p1;
   1275 		return p2;
   1276 	} else {
   1277 		if (p2->p_const) {
   1278 			if (p1->p_const) {
   1279 				/* duplicate '%s' */
   1280 				warning(10, "const");
   1281 			}
   1282 			p1->p_const = 1;
   1283 		}
   1284 		if (p2->p_volatile) {
   1285 			if (p1->p_volatile) {
   1286 				/* duplicate '%s' */
   1287 				warning(10, "volatile");
   1288 			}
   1289 			p1->p_volatile = 1;
   1290 		}
   1291 		free(p2);
   1292 		return p1;
   1293 	}
   1294 }
   1295 
   1296 /*
   1297  * The following 3 functions extend the type of a declarator with
   1298  * pointer, function and array types.
   1299  *
   1300  * The current type is the Type built by deftyp() (dcs->d_type) and
   1301  * pointer, function and array types already added for this
   1302  * declarator. The new type extension is inserted between both.
   1303  */
   1304 sym_t *
   1305 add_pointer(sym_t *decl, pqinf_t *pi)
   1306 {
   1307 	type_t	**tpp, *tp;
   1308 	pqinf_t	*npi;
   1309 
   1310 	tpp = &decl->s_type;
   1311 	while (*tpp && *tpp != dcs->d_type)
   1312 		tpp = &(*tpp)->t_subt;
   1313 	if (*tpp == NULL)
   1314 		return decl;
   1315 
   1316 	while (pi != NULL) {
   1317 		*tpp = tp = getblk(sizeof (type_t));
   1318 		tp->t_tspec = PTR;
   1319 		tp->t_const = pi->p_const;
   1320 		tp->t_volatile = pi->p_volatile;
   1321 		*(tpp = &tp->t_subt) = dcs->d_type;
   1322 		npi = pi->p_next;
   1323 		free(pi);
   1324 		pi = npi;
   1325 	}
   1326 	return decl;
   1327 }
   1328 
   1329 /*
   1330  * If a dimension was specified, dim is 1, otherwise 0
   1331  * n is the specified dimension
   1332  */
   1333 sym_t *
   1334 add_array(sym_t *decl, int dim, int n)
   1335 {
   1336 	type_t	**tpp, *tp;
   1337 
   1338 	tpp = &decl->s_type;
   1339 	while (*tpp && *tpp != dcs->d_type)
   1340 		tpp = &(*tpp)->t_subt;
   1341 	if (*tpp == NULL)
   1342 	    return decl;
   1343 
   1344 	*tpp = tp = getblk(sizeof (type_t));
   1345 	tp->t_tspec = ARRAY;
   1346 	tp->t_subt = dcs->d_type;
   1347 	tp->t_dim = n;
   1348 
   1349 	if (n < 0) {
   1350 		/* negative array dimension (%d) */
   1351 		error(20, n);
   1352 		n = 0;
   1353 	} else if (n == 0 && dim) {
   1354 		/* zero array dimension */
   1355 		c99ism(322, dim);
   1356 	} else if (n == 0 && !dim) {
   1357 		setcomplete(tp, 0);
   1358 	}
   1359 
   1360 	return decl;
   1361 }
   1362 
   1363 sym_t *
   1364 add_function(sym_t *decl, sym_t *args)
   1365 {
   1366 	type_t	**tpp, *tp;
   1367 
   1368 	if (dcs->d_proto) {
   1369 		if (tflag)
   1370 			/* function prototypes are illegal in traditional C */
   1371 			warning(270);
   1372 		args = new_style_function(decl, args);
   1373 	} else {
   1374 		old_style_function(decl, args);
   1375 	}
   1376 
   1377 	/*
   1378 	 * The symbols are removed from the symbol table by popdecl() after
   1379 	 * add_function(). To be able to restore them if this is a function
   1380 	 * definition, a pointer to the list of all symbols is stored in
   1381 	 * dcs->d_next->d_fpsyms. Also a list of the arguments (concatenated
   1382 	 * by s_next) is stored in dcs->d_next->d_fargs.
   1383 	 * (dcs->d_next must be used because *dcs is the declaration stack
   1384 	 * element created for the list of params and is removed after
   1385 	 * add_function())
   1386 	 */
   1387 	if (dcs->d_next->d_ctx == EXTERN &&
   1388 	    decl->s_type == dcs->d_next->d_type) {
   1389 		dcs->d_next->d_fpsyms = dcs->d_dlsyms;
   1390 		dcs->d_next->d_fargs = args;
   1391 	}
   1392 
   1393 	tpp = &decl->s_type;
   1394 	while (*tpp && *tpp != dcs->d_next->d_type)
   1395 		tpp = &(*tpp)->t_subt;
   1396 	if (*tpp == NULL)
   1397 	    return decl;
   1398 
   1399 	*tpp = tp = getblk(sizeof (type_t));
   1400 	tp->t_tspec = FUNC;
   1401 	tp->t_subt = dcs->d_next->d_type;
   1402 	if ((tp->t_proto = dcs->d_proto) != 0)
   1403 		tp->t_args = args;
   1404 	tp->t_vararg = dcs->d_vararg;
   1405 
   1406 	return decl;
   1407 }
   1408 
   1409 /*
   1410  * Called for new style function declarations.
   1411  */
   1412 /* ARGSUSED */
   1413 static sym_t *
   1414 new_style_function(sym_t *decl, sym_t *args)
   1415 {
   1416 	sym_t	*arg, *sym;
   1417 	scl_t	sc;
   1418 	int	n;
   1419 
   1420 	/*
   1421 	 * Declarations of structs/unions/enums in param lists are legal,
   1422 	 * but senseless.
   1423 	 */
   1424 	for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
   1425 		sc = sym->s_scl;
   1426 		if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) {
   1427 			/* dubious tag declaration: %s %s */
   1428 			warning(85, storage_class_name(sc), sym->s_name);
   1429 		}
   1430 	}
   1431 
   1432 	n = 1;
   1433 	for (arg = args; arg != NULL; arg = arg->s_next) {
   1434 		if (arg->s_type->t_tspec == VOID) {
   1435 			if (n > 1 || arg->s_next != NULL) {
   1436 				/* void must be sole parameter */
   1437 				error(60);
   1438 				arg->s_type = gettyp(INT);
   1439 			}
   1440 		}
   1441 		n++;
   1442 	}
   1443 
   1444 	/* return NULL if first param is VOID */
   1445 	return args != NULL && args->s_type->t_tspec != VOID ? args : NULL;
   1446 }
   1447 
   1448 /*
   1449  * Called for old style function declarations.
   1450  */
   1451 static void
   1452 old_style_function(sym_t *decl, sym_t *args)
   1453 {
   1454 
   1455 	/*
   1456 	 * Remember list of params only if this is really seams to be
   1457 	 * a function definition.
   1458 	 */
   1459 	if (dcs->d_next->d_ctx == EXTERN &&
   1460 	    decl->s_type == dcs->d_next->d_type) {
   1461 		/*
   1462 		 * We assume that this becomes a function definition. If
   1463 		 * we are wrong, it's corrected in check_function_definition().
   1464 		 */
   1465 		if (args != NULL) {
   1466 			decl->s_osdef = 1;
   1467 			decl->s_args = args;
   1468 		}
   1469 	} else {
   1470 		if (args != NULL)
   1471 			/* function prototype parameters must have types */
   1472 			warning(62);
   1473 	}
   1474 }
   1475 
   1476 /*
   1477  * Lists of Identifiers in functions declarations are allowed only if
   1478  * it's also a function definition. If this is not the case, print a
   1479  * error message.
   1480  */
   1481 void
   1482 check_function_definition(sym_t *sym, int msg)
   1483 {
   1484 
   1485 	if (sym->s_osdef) {
   1486 		if (msg) {
   1487 			/* incomplete or misplaced function definition */
   1488 			error(22);
   1489 		}
   1490 		sym->s_osdef = 0;
   1491 		sym->s_args = NULL;
   1492 	}
   1493 }
   1494 
   1495 /*
   1496  * Process the name in a declarator.
   1497  * The symbol gets one of the storage classes EXTERN, STATIC, AUTO or
   1498  * TYPEDEF.
   1499  * s_def and s_reg are valid after declarator_name().
   1500  */
   1501 sym_t *
   1502 declarator_name(sym_t *sym)
   1503 {
   1504 	scl_t	sc = NOSCL;
   1505 
   1506 	if (sym->s_scl == NOSCL) {
   1507 		dcs->d_rdcsym = NULL;
   1508 	} else if (sym->s_defarg) {
   1509 		sym->s_defarg = 0;
   1510 		dcs->d_rdcsym = NULL;
   1511 	} else {
   1512 		dcs->d_rdcsym = sym;
   1513 		sym = pushdown(sym);
   1514 	}
   1515 
   1516 	switch (dcs->d_ctx) {
   1517 	case MOS:
   1518 	case MOU:
   1519 		/* Set parent */
   1520 		sym->s_styp = dcs->d_tagtyp->t_str;
   1521 		sym->s_def = DEF;
   1522 		sym->s_value.v_tspec = INT;
   1523 		sc = dcs->d_ctx;
   1524 		break;
   1525 	case EXTERN:
   1526 		/*
   1527 		 * static and external symbols without "extern" are
   1528 		 * considered to be tentative defined, external
   1529 		 * symbols with "extern" are declared, and typedef names
   1530 		 * are defined. Tentative defined and declared symbols
   1531 		 * may become defined if an initializer is present or
   1532 		 * this is a function definition.
   1533 		 */
   1534 		if ((sc = dcs->d_scl) == NOSCL) {
   1535 			sc = EXTERN;
   1536 			sym->s_def = TDEF;
   1537 		} else if (sc == STATIC) {
   1538 			sym->s_def = TDEF;
   1539 		} else if (sc == TYPEDEF) {
   1540 			sym->s_def = DEF;
   1541 		} else if (sc == EXTERN) {
   1542 			sym->s_def = DECL;
   1543 		} else {
   1544 			LERROR("declarator_name()");
   1545 		}
   1546 		break;
   1547 	case PARG:
   1548 		sym->s_arg = 1;
   1549 		/* FALLTHROUGH */
   1550 	case ARG:
   1551 		if ((sc = dcs->d_scl) == NOSCL) {
   1552 			sc = AUTO;
   1553 		} else if (sc == REG) {
   1554 			sym->s_reg = 1;
   1555 			sc = AUTO;
   1556 		} else {
   1557 			LERROR("declarator_name()");
   1558 		}
   1559 		sym->s_def = DEF;
   1560 		break;
   1561 	case AUTO:
   1562 		if ((sc = dcs->d_scl) == NOSCL) {
   1563 			/*
   1564 			 * XXX somewhat ugly because we dont know whether
   1565 			 * this is AUTO or EXTERN (functions). If we are
   1566 			 * wrong it must be corrected in decl1loc(), where
   1567 			 * we have the necessary type information.
   1568 			 */
   1569 			sc = AUTO;
   1570 			sym->s_def = DEF;
   1571 		} else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
   1572 			sym->s_def = DEF;
   1573 		} else if (sc == REG) {
   1574 			sym->s_reg = 1;
   1575 			sc = AUTO;
   1576 			sym->s_def = DEF;
   1577 		} else if (sc == EXTERN) {
   1578 			sym->s_def = DECL;
   1579 		} else {
   1580 			LERROR("declarator_name()");
   1581 		}
   1582 		break;
   1583 	default:
   1584 		LERROR("declarator_name()");
   1585 	}
   1586 	sym->s_scl = sc;
   1587 
   1588 	sym->s_type = dcs->d_type;
   1589 
   1590 	dcs->d_fpsyms = NULL;
   1591 
   1592 	return sym;
   1593 }
   1594 
   1595 /*
   1596  * Process a name in the list of formal params in an old style function
   1597  * definition.
   1598  */
   1599 sym_t *
   1600 old_style_function_name(sym_t *sym)
   1601 {
   1602 
   1603 	if (sym->s_scl != NOSCL) {
   1604 		if (blklev == sym->s_blklev) {
   1605 			/* redeclaration of formal parameter %s */
   1606 			error(21, sym->s_name);
   1607 			if (!sym->s_defarg)
   1608 				LERROR("old_style_function_name()");
   1609 		}
   1610 		sym = pushdown(sym);
   1611 	}
   1612 	sym->s_type = gettyp(INT);
   1613 	sym->s_scl = AUTO;
   1614 	sym->s_def = DEF;
   1615 	sym->s_defarg = sym->s_arg = 1;
   1616 	return sym;
   1617 }
   1618 
   1619 /*
   1620  * Create the type of a tag.
   1621  *
   1622  * tag points to the symbol table entry of the tag
   1623  * kind is the kind of the tag (STRUCT/UNION/ENUM)
   1624  * decl is 1 if the type of the tag will be completed in this declaration
   1625  * (the following token is T_LBRACE)
   1626  * semi is 1 if the following token is T_SEMI
   1627  */
   1628 type_t *
   1629 mktag(sym_t *tag, tspec_t kind, int decl, int semi)
   1630 {
   1631 	scl_t	scl = NOSCL;
   1632 	type_t	*tp;
   1633 
   1634 	if (kind == STRUCT) {
   1635 		scl = STRTAG;
   1636 	} else if (kind == UNION) {
   1637 		scl = UNIONTAG;
   1638 	} else if (kind == ENUM) {
   1639 		scl = ENUMTAG;
   1640 	} else {
   1641 		LERROR("mktag()");
   1642 	}
   1643 
   1644 	if (tag != NULL) {
   1645 		if (tag->s_scl != NOSCL) {
   1646 			tag = newtag(tag, scl, decl, semi);
   1647 		} else {
   1648 			/* a new tag, no empty declaration */
   1649 			dcs->d_next->d_nedecl = 1;
   1650 			if (scl == ENUMTAG && !decl) {
   1651 				if (!tflag && (sflag || pflag))
   1652 					/* forward reference to enum type */
   1653 					warning(42);
   1654 			}
   1655 		}
   1656 		if (tag->s_scl == NOSCL) {
   1657 			tag->s_scl = scl;
   1658 			tag->s_type = tp = getblk(sizeof (type_t));
   1659 			tp->t_ispacked = dcs->d_ispacked;
   1660 		} else {
   1661 			tp = tag->s_type;
   1662 		}
   1663 	} else {
   1664 		tag = getblk(sizeof (sym_t));
   1665 		tag->s_name = unnamed;
   1666 		UNIQUE_CURR_POS(tag->s_def_pos);
   1667 		tag->s_kind = FTAG;
   1668 		tag->s_scl = scl;
   1669 		tag->s_blklev = -1;
   1670 		tag->s_type = tp = getblk(sizeof (type_t));
   1671 		tp->t_ispacked = dcs->d_ispacked;
   1672 		dcs->d_next->d_nedecl = 1;
   1673 	}
   1674 
   1675 	if (tp->t_tspec == NOTSPEC) {
   1676 		tp->t_tspec = kind;
   1677 		if (kind != ENUM) {
   1678 			tp->t_str = getblk(sizeof (str_t));
   1679 			tp->t_str->align = CHAR_BIT;
   1680 			tp->t_str->stag = tag;
   1681 		} else {
   1682 			tp->t_isenum = 1;
   1683 			tp->t_enum = getblk(sizeof(*tp->t_enum));
   1684 			tp->t_enum->etag = tag;
   1685 		}
   1686 		setcomplete(tp, 0);
   1687 	}
   1688 	return tp;
   1689 }
   1690 
   1691 /*
   1692  * Checks all possible cases of tag redeclarations.
   1693  * decl is 1 if T_LBRACE follows
   1694  * semi is 1 if T_SEMI follows
   1695  */
   1696 static sym_t *
   1697 newtag(sym_t *tag, scl_t scl, int decl, int semi)
   1698 {
   1699 
   1700 	if (tag->s_blklev < blklev) {
   1701 		if (semi) {
   1702 			/* "struct a;" */
   1703 			if (!tflag) {
   1704 				if (!sflag)
   1705 					/* decl. introduces new type ... */
   1706 					warning(44, storage_class_name(scl),
   1707 					    tag->s_name);
   1708 				tag = pushdown(tag);
   1709 			} else if (tag->s_scl != scl) {
   1710 				/* base type is really '%s %s' */
   1711 				warning(45, storage_class_name(tag->s_scl),
   1712 				    tag->s_name);
   1713 			}
   1714 			dcs->d_next->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_next->d_nedecl = 1;
   1722 		} else if (tag->s_scl != scl) {
   1723 			/* base type is really '%s %s' */
   1724 			warning(45, storage_class_name(tag->s_scl),
   1725 			    tag->s_name);
   1726 			/* declaration introduces new type in ANSI C: %s %s */
   1727 			if (!sflag) {
   1728 				warning(44, storage_class_name(scl),
   1729 				    tag->s_name);
   1730 			}
   1731 			tag = pushdown(tag);
   1732 			dcs->d_next->d_nedecl = 1;
   1733 		}
   1734 	} else {
   1735 		if (tag->s_scl != scl) {
   1736 			/* (%s) tag redeclared */
   1737 			error(46, storage_class_name(tag->s_scl));
   1738 			print_previous_declaration(-1, tag);
   1739 			tag = pushdown(tag);
   1740 			dcs->d_next->d_nedecl = 1;
   1741 		} else if (decl && !incompl(tag->s_type)) {
   1742 			/* (%s) tag redeclared */
   1743 			error(46, storage_class_name(tag->s_scl));
   1744 			print_previous_declaration(-1, tag);
   1745 			tag = pushdown(tag);
   1746 			dcs->d_next->d_nedecl = 1;
   1747 		} else if (semi || decl) {
   1748 			dcs->d_next->d_nedecl = 1;
   1749 		}
   1750 	}
   1751 	return tag;
   1752 }
   1753 
   1754 const char *
   1755 storage_class_name(scl_t sc)
   1756 {
   1757 	const	char *s;
   1758 
   1759 	switch (sc) {
   1760 	case EXTERN:	s = "extern";	break;
   1761 	case STATIC:	s = "static";	break;
   1762 	case AUTO:	s = "auto";	break;
   1763 	case REG:	s = "register";	break;
   1764 	case TYPEDEF:	s = "typedef";	break;
   1765 	case STRTAG:	s = "struct";	break;
   1766 	case UNIONTAG:	s = "union";	break;
   1767 	case ENUMTAG:	s = "enum";	break;
   1768 	default:	LERROR("tagttoa()");
   1769 	}
   1770 	return s;
   1771 }
   1772 
   1773 /*
   1774  * tp points to the type of the tag, fmem to the list of members.
   1775  */
   1776 type_t *
   1777 complete_tag_struct_or_union(type_t *tp, sym_t *fmem)
   1778 {
   1779 	tspec_t	t;
   1780 	str_t	*sp;
   1781 	int	n;
   1782 	sym_t	*mem;
   1783 
   1784 	setcomplete(tp, 1);
   1785 
   1786 	t = tp->t_tspec;
   1787 	align(dcs->d_stralign, 0);
   1788 	sp = tp->t_str;
   1789 	sp->align = dcs->d_stralign;
   1790 	sp->memb = fmem;
   1791 	if (tp->t_ispacked)
   1792 		setpackedsize(tp);
   1793 	else
   1794 		sp->size = dcs->d_offset;
   1795 
   1796 	if (sp->size == 0) {
   1797 		/* zero sized %s */
   1798 		(void)c99ism(47, ttab[t].tt_name);
   1799 	}
   1800 
   1801 	n = 0;
   1802 	for (mem = fmem; mem != NULL; mem = mem->s_next) {
   1803 		/* bind anonymous members to the structure */
   1804 		if (mem->s_styp == NULL) {
   1805 			mem->s_styp = sp;
   1806 			if (mem->s_type->t_isfield) {
   1807 				sp->size += bitfieldsize(&mem);
   1808 				if (mem == NULL)
   1809 					break;
   1810 			}
   1811 			sp->size += tsize(mem->s_type);
   1812 		}
   1813 		if (mem->s_name != unnamed)
   1814 			n++;
   1815 	}
   1816 
   1817 	if (n == 0 && sp->size != 0) {
   1818 		/* %s has no named members */
   1819 		warning(65, t == STRUCT ? "structure" : "union");
   1820 	}
   1821 	return tp;
   1822 }
   1823 
   1824 type_t *
   1825 complete_tag_enum(type_t *tp, sym_t *fmem)
   1826 {
   1827 
   1828 	setcomplete(tp, 1);
   1829 	tp->t_enum->elem = fmem;
   1830 	return tp;
   1831 }
   1832 
   1833 /*
   1834  * Processes the name of an enumerator in an enum declaration.
   1835  *
   1836  * sym points to the enumerator
   1837  * val is the value of the enumerator
   1838  * impl is 1 if the value of the enumerator was not explicitly specified.
   1839  */
   1840 sym_t *
   1841 ename(sym_t *sym, int val, int impl)
   1842 {
   1843 
   1844 	if (sym->s_scl) {
   1845 		if (sym->s_blklev == blklev) {
   1846 			/* no hflag, because this is illegal!!! */
   1847 			if (sym->s_arg) {
   1848 				/* enumeration constant hides parameter: %s */
   1849 				warning(57, sym->s_name);
   1850 			} else {
   1851 				/* redeclaration of %s */
   1852 				error(27, sym->s_name);
   1853 				/*
   1854 				 * inside blocks it should not be too
   1855 				 * complicated to find the position of the
   1856 				 * previous declaration
   1857 				 */
   1858 				if (blklev == 0)
   1859 					print_previous_declaration(-1, sym);
   1860 			}
   1861 		} else {
   1862 			if (hflag)
   1863 				/* redefinition hides earlier one: %s */
   1864 				warning(43, sym->s_name);
   1865 		}
   1866 		sym = pushdown(sym);
   1867 	}
   1868 	sym->s_scl = ENUMCON;
   1869 	sym->s_type = dcs->d_tagtyp;
   1870 	sym->s_value.v_tspec = INT;
   1871 	sym->s_value.v_quad = val;
   1872 	if (impl && val - 1 == TARG_INT_MAX) {
   1873 		/* overflow in enumeration values: %s */
   1874 		warning(48, sym->s_name);
   1875 	}
   1876 	enumval = val + 1;
   1877 	return sym;
   1878 }
   1879 
   1880 /*
   1881  * Process a single external declarator.
   1882  */
   1883 void
   1884 decl1ext(sym_t *dsym, int initflg)
   1885 {
   1886 	int	dowarn, rval, redec;
   1887 	sym_t	*rdsym;
   1888 
   1889 	check_function_definition(dsym, 1);
   1890 
   1891 	check_type(dsym);
   1892 
   1893 	if (initflg && !(initerr = check_init(dsym)))
   1894 		dsym->s_def = DEF;
   1895 
   1896 	/*
   1897 	 * Declarations of functions are marked as "tentative" in
   1898 	 * declarator_name(). This is wrong because there are no
   1899 	 * tentative function definitions.
   1900 	 */
   1901 	if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
   1902 		dsym->s_def = DECL;
   1903 
   1904 	if (dcs->d_inline) {
   1905 		if (dsym->s_type->t_tspec == FUNC) {
   1906 			dsym->s_inline = 1;
   1907 		} else {
   1908 			/* variable declared inline: %s */
   1909 			warning(268, dsym->s_name);
   1910 		}
   1911 	}
   1912 
   1913 	/* Write the declaration into the output file */
   1914 	if (plibflg && llibflg &&
   1915 	    dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
   1916 		/*
   1917 		 * With both LINTLIBRARY and PROTOLIB the prototyp is
   1918 		 * written as a function definition to the output file.
   1919 		 */
   1920 		rval = dsym->s_type->t_subt->t_tspec != VOID;
   1921 		outfdef(dsym, &dsym->s_def_pos, rval, 0, NULL);
   1922 	} else {
   1923 		outsym(dsym, dsym->s_scl, dsym->s_def);
   1924 	}
   1925 
   1926 	if ((rdsym = dcs->d_rdcsym) != NULL) {
   1927 
   1928 		/*
   1929 		 * If the old symbol stems from an old style function
   1930 		 * definition, we have remembered the params in rdsmy->s_args
   1931 		 * and compare them with the params of the prototype.
   1932 		 */
   1933 		if (rdsym->s_osdef && dsym->s_type->t_proto) {
   1934 			redec = check_old_style_definition(rdsym, dsym);
   1935 		} else {
   1936 			redec = 0;
   1937 		}
   1938 
   1939 		if (!redec && !check_redeclaration(dsym, (dowarn = 0, &dowarn))) {
   1940 
   1941 			if (dowarn) {
   1942 				/* redeclaration of %s */
   1943 				(*(sflag ? error : warning))(27, dsym->s_name);
   1944 				print_previous_declaration(-1, rdsym);
   1945 			}
   1946 
   1947 			/*
   1948 			 * Take over the remembered params if the new symbol
   1949 			 * is not a prototype.
   1950 			 */
   1951 			if (rdsym->s_osdef && !dsym->s_type->t_proto) {
   1952 				dsym->s_osdef = rdsym->s_osdef;
   1953 				dsym->s_args = rdsym->s_args;
   1954 				dsym->s_def_pos = rdsym->s_def_pos;
   1955 			}
   1956 
   1957 			/*
   1958 			 * Remember the position of the declaration if the
   1959 			 * old symbol was a prototype and the new is not.
   1960 			 * Also remember the position if the old symbol
   1961 			 * was defined and the new is not.
   1962 			 */
   1963 			if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
   1964 				dsym->s_def_pos = rdsym->s_def_pos;
   1965 			} else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
   1966 				dsym->s_def_pos = rdsym->s_def_pos;
   1967 			}
   1968 
   1969 			/*
   1970 			 * Copy usage information of the name into the new
   1971 			 * symbol.
   1972 			 */
   1973 			copy_usage_info(dsym, rdsym);
   1974 
   1975 			/* Once a name is defined, it remains defined. */
   1976 			if (rdsym->s_def == DEF)
   1977 				dsym->s_def = DEF;
   1978 
   1979 			/* once a function is inline, it remains inline */
   1980 			if (rdsym->s_inline)
   1981 				dsym->s_inline = 1;
   1982 
   1983 			complete_type(dsym, rdsym);
   1984 
   1985 		}
   1986 
   1987 		rmsym(rdsym);
   1988 	}
   1989 
   1990 	if (dsym->s_scl == TYPEDEF) {
   1991 		dsym->s_type = duptyp(dsym->s_type);
   1992 		dsym->s_type->t_typedef = 1;
   1993 		settdsym(dsym->s_type, dsym);
   1994 	}
   1995 
   1996 }
   1997 
   1998 /*
   1999  * Copies information about usage into a new symbol table entry of
   2000  * the same symbol.
   2001  */
   2002 void
   2003 copy_usage_info(sym_t *sym, sym_t *rdsym)
   2004 {
   2005 
   2006 	sym->s_set_pos = rdsym->s_set_pos;
   2007 	sym->s_use_pos = rdsym->s_use_pos;
   2008 	sym->s_set = rdsym->s_set;
   2009 	sym->s_used = rdsym->s_used;
   2010 }
   2011 
   2012 /*
   2013  * Prints an error and returns 1 if a symbol is redeclared/redefined.
   2014  * Otherwise returns 0 and, in some cases of minor problems, prints
   2015  * a warning.
   2016  */
   2017 int
   2018 check_redeclaration(sym_t *dsym, int *dowarn)
   2019 {
   2020 	sym_t	*rsym;
   2021 
   2022 	if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
   2023 		/* redeclaration of %s */
   2024 		error(27, dsym->s_name);
   2025 		print_previous_declaration(-1, rsym);
   2026 		return 1;
   2027 	}
   2028 	if (rsym->s_scl == TYPEDEF) {
   2029 		/* typedef redeclared: %s */
   2030 		error(89, dsym->s_name);
   2031 		print_previous_declaration(-1, rsym);
   2032 		return 1;
   2033 	}
   2034 	if (dsym->s_scl == TYPEDEF) {
   2035 		/* redeclaration of %s */
   2036 		error(27, dsym->s_name);
   2037 		print_previous_declaration(-1, rsym);
   2038 		return 1;
   2039 	}
   2040 	if (rsym->s_def == DEF && dsym->s_def == DEF) {
   2041 		/* redefinition of %s */
   2042 		error(28, dsym->s_name);
   2043 		print_previous_declaration(-1, rsym);
   2044 		return(1);
   2045 	}
   2046 	if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, dowarn)) {
   2047 		/* redeclaration of %s */
   2048 		error(27, dsym->s_name);
   2049 		print_previous_declaration(-1, rsym);
   2050 		return(1);
   2051 	}
   2052 	if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
   2053 		return(0);
   2054 	if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
   2055 		return(0);
   2056 	if (rsym->s_scl == STATIC && dsym->s_def == DECL)
   2057 		return(0);
   2058 	if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
   2059 		/*
   2060 		 * All cases except "int a = 1; static int a;" are caught
   2061 		 * above with or without a warning
   2062 		 */
   2063 		/* redeclaration of %s */
   2064 		error(27, dsym->s_name);
   2065 		print_previous_declaration(-1, rsym);
   2066 		return(1);
   2067 	}
   2068 	if (rsym->s_scl == EXTERN) {
   2069 		/* previously declared extern, becomes static: %s */
   2070 		warning(29, dsym->s_name);
   2071 		print_previous_declaration(-1, rsym);
   2072 		return(0);
   2073 	}
   2074 	/*
   2075 	 * Now it's one of:
   2076 	 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
   2077 	 */
   2078 	/* redeclaration of %s; ANSI C requires "static" */
   2079 	if (sflag) {
   2080 		warning(30, dsym->s_name);
   2081 		print_previous_declaration(-1, rsym);
   2082 	}
   2083 	dsym->s_scl = STATIC;
   2084 	return 0;
   2085 }
   2086 
   2087 static int
   2088 chkqual(type_t *tp1, type_t *tp2, int ignqual)
   2089 {
   2090 	if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
   2091 		return 0;
   2092 
   2093 	if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
   2094 		return 0;
   2095 
   2096 	return 1;
   2097 }
   2098 
   2099 int
   2100 eqptrtype(type_t *tp1, type_t *tp2, int ignqual)
   2101 {
   2102 	if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
   2103 		return 0;
   2104 
   2105 	if (!chkqual(tp1, tp2, ignqual))
   2106 		return 0;
   2107 
   2108 	return 1;
   2109 }
   2110 
   2111 
   2112 /*
   2113  * Checks if two types are compatible. Returns 0 if not, otherwise 1.
   2114  *
   2115  * ignqual	ignore qualifiers of type; used for function params
   2116  * promot	promote left type; used for comparison of params of
   2117  *		old style function definitions with params of prototypes.
   2118  * *dowarn	set to 1 if an old style function declaration is not
   2119  *		compatible with a prototype
   2120  */
   2121 int
   2122 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *dowarn)
   2123 {
   2124 	tspec_t	t;
   2125 
   2126 	while (tp1 != NULL && tp2 != NULL) {
   2127 
   2128 		t = tp1->t_tspec;
   2129 		if (promot) {
   2130 			if (t == FLOAT) {
   2131 				t = DOUBLE;
   2132 			} else if (t == CHAR || t == SCHAR) {
   2133 				t = INT;
   2134 			} else if (t == UCHAR) {
   2135 				t = tflag ? UINT : INT;
   2136 			} else if (t == SHORT) {
   2137 				t = INT;
   2138 			} else if (t == USHORT) {
   2139 				/* CONSTCOND */
   2140 				t = TARG_INT_MAX < TARG_USHRT_MAX || tflag ? UINT : INT;
   2141 			}
   2142 		}
   2143 
   2144 		if (t != tp2->t_tspec)
   2145 			return 0;
   2146 
   2147 		if (!chkqual(tp1, tp2, ignqual))
   2148 			return 0;
   2149 
   2150 		if (t == STRUCT || t == UNION)
   2151 			return tp1->t_str == tp2->t_str;
   2152 
   2153 		if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
   2154 			if (tp1->t_dim != 0 && tp2->t_dim != 0)
   2155 				return 0;
   2156 		}
   2157 
   2158 		/* dont check prototypes for traditional */
   2159 		if (t == FUNC && !tflag) {
   2160 			if (tp1->t_proto && tp2->t_proto) {
   2161 				if (!eqargs(tp1, tp2, dowarn))
   2162 					return 0;
   2163 			} else if (tp1->t_proto) {
   2164 				if (!mnoarg(tp1, dowarn))
   2165 					return 0;
   2166 			} else if (tp2->t_proto) {
   2167 				if (!mnoarg(tp2, dowarn))
   2168 					return 0;
   2169 			}
   2170 		}
   2171 
   2172 		tp1 = tp1->t_subt;
   2173 		tp2 = tp2->t_subt;
   2174 		ignqual = promot = 0;
   2175 
   2176 	}
   2177 
   2178 	return tp1 == tp2;
   2179 }
   2180 
   2181 /*
   2182  * Compares the parameter types of two prototypes.
   2183  */
   2184 static int
   2185 eqargs(type_t *tp1, type_t *tp2, int *dowarn)
   2186 {
   2187 	sym_t	*a1, *a2;
   2188 
   2189 	if (tp1->t_vararg != tp2->t_vararg)
   2190 		return 0;
   2191 
   2192 	a1 = tp1->t_args;
   2193 	a2 = tp2->t_args;
   2194 
   2195 	while (a1 != NULL && a2 != NULL) {
   2196 
   2197 		if (eqtype(a1->s_type, a2->s_type, 1, 0, dowarn) == 0)
   2198 			return 0;
   2199 
   2200 		a1 = a1->s_next;
   2201 		a2 = a2->s_next;
   2202 
   2203 	}
   2204 
   2205 	return a1 == a2;
   2206 }
   2207 
   2208 /*
   2209  * mnoarg() (matches functions with no argument type information)
   2210  * returns 1 if all parameters of a prototype are compatible with
   2211  * an old style function declaration.
   2212  * This is the case if the following conditions are met:
   2213  *	1. the prototype must have a fixed number of parameters
   2214  *	2. no parameter is of type float
   2215  *	3. no parameter is converted to another type if integer promotion
   2216  *	   is applied on it
   2217  */
   2218 static int
   2219 mnoarg(type_t *tp, int *dowarn)
   2220 {
   2221 	sym_t	*arg;
   2222 	tspec_t	t;
   2223 
   2224 	if (tp->t_vararg) {
   2225 		if (dowarn != NULL)
   2226 			*dowarn = 1;
   2227 	}
   2228 	for (arg = tp->t_args; arg != NULL; arg = arg->s_next) {
   2229 		if ((t = arg->s_type->t_tspec) == FLOAT ||
   2230 		    t == CHAR || t == SCHAR || t == UCHAR ||
   2231 		    t == SHORT || t == USHORT) {
   2232 			if (dowarn != NULL)
   2233 				*dowarn = 1;
   2234 		}
   2235 	}
   2236 	return 1;
   2237 }
   2238 
   2239 /*
   2240  * Compares a prototype declaration with the remembered arguments of
   2241  * a previous old style function definition.
   2242  */
   2243 static int
   2244 check_old_style_definition(sym_t *rdsym, sym_t *dsym)
   2245 {
   2246 	sym_t	*args, *pargs, *arg, *parg;
   2247 	int	narg, nparg, n;
   2248 	int	dowarn, msg;
   2249 
   2250 	args = rdsym->s_args;
   2251 	pargs = dsym->s_type->t_args;
   2252 
   2253 	msg = 0;
   2254 
   2255 	narg = nparg = 0;
   2256 	for (arg = args; arg != NULL; arg = arg->s_next)
   2257 		narg++;
   2258 	for (parg = pargs; parg != NULL; parg = parg->s_next)
   2259 		nparg++;
   2260 	if (narg != nparg) {
   2261 		/* prototype does not match old-style definition */
   2262 		error(63);
   2263 		msg = 1;
   2264 		goto end;
   2265 	}
   2266 
   2267 	arg = args;
   2268 	parg = pargs;
   2269 	n = 1;
   2270 	while (narg--) {
   2271 		dowarn = 0;
   2272 		/*
   2273 		 * If it does not match due to promotion and sflag is
   2274 		 * not set we print only a warning.
   2275 		 */
   2276 		if (!eqtype(arg->s_type, parg->s_type, 1, 1, &dowarn) || dowarn) {
   2277 			/* prototype does not match old style defn., arg #%d */
   2278 			error(299, n);
   2279 			msg = 1;
   2280 		}
   2281 		arg = arg->s_next;
   2282 		parg = parg->s_next;
   2283 		n++;
   2284 	}
   2285 
   2286  end:
   2287 	if (msg)
   2288 		/* old style definition */
   2289 		print_previous_declaration(300, rdsym);
   2290 
   2291 	return msg;
   2292 }
   2293 
   2294 /*
   2295  * Completes a type by copying the dimension and prototype information
   2296  * from a second compatible type.
   2297  *
   2298  * Following lines are legal:
   2299  *  "typedef a[]; a b; a b[10]; a c; a c[20];"
   2300  *  "typedef ft(); ft f; f(int); ft g; g(long);"
   2301  * This means that, if a type is completed, the type structure must
   2302  * be duplicated.
   2303  */
   2304 void
   2305 complete_type(sym_t *dsym, sym_t *ssym)
   2306 {
   2307 	type_t	**dstp, *src;
   2308 	type_t	*dst;
   2309 
   2310 	dstp = &dsym->s_type;
   2311 	src = ssym->s_type;
   2312 
   2313 	while ((dst = *dstp) != NULL) {
   2314 		if (src == NULL || dst->t_tspec != src->t_tspec)
   2315 			LERROR("complete_type()");
   2316 		if (dst->t_tspec == ARRAY) {
   2317 			if (dst->t_dim == 0 && src->t_dim != 0) {
   2318 				*dstp = dst = duptyp(dst);
   2319 				dst->t_dim = src->t_dim;
   2320 				setcomplete(dst, 1);
   2321 			}
   2322 		} else if (dst->t_tspec == FUNC) {
   2323 			if (!dst->t_proto && src->t_proto) {
   2324 				*dstp = dst = duptyp(dst);
   2325 				dst->t_proto = 1;
   2326 				dst->t_args = src->t_args;
   2327 			}
   2328 		}
   2329 		dstp = &dst->t_subt;
   2330 		src = src->t_subt;
   2331 	}
   2332 }
   2333 
   2334 /*
   2335  * Completes the declaration of a single argument.
   2336  */
   2337 sym_t *
   2338 decl1arg(sym_t *sym, int initflg)
   2339 {
   2340 	tspec_t	t;
   2341 
   2342 	check_function_definition(sym, 1);
   2343 
   2344 	check_type(sym);
   2345 
   2346 	if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) {
   2347 		/* redeclaration of formal parameter %s */
   2348 		error(237, sym->s_name);
   2349 		rmsym(dcs->d_rdcsym);
   2350 		sym->s_arg = 1;
   2351 	}
   2352 
   2353 	if (!sym->s_arg) {
   2354 		/* declared argument %s is missing */
   2355 		error(53, sym->s_name);
   2356 		sym->s_arg = 1;
   2357 	}
   2358 
   2359 	if (initflg) {
   2360 		/* cannot initialize parameter: %s */
   2361 		error(52, sym->s_name);
   2362 		initerr = 1;
   2363 	}
   2364 
   2365 	if ((t = sym->s_type->t_tspec) == ARRAY) {
   2366 		sym->s_type = incref(sym->s_type->t_subt, PTR);
   2367 	} else if (t == FUNC) {
   2368 		if (tflag)
   2369 			/* a function is declared as an argument: %s */
   2370 			warning(50, sym->s_name);
   2371 		sym->s_type = incref(sym->s_type, PTR);
   2372 	} else if (t == FLOAT) {
   2373 		if (tflag)
   2374 			sym->s_type = gettyp(DOUBLE);
   2375 	}
   2376 
   2377 	if (dcs->d_inline)
   2378 		/* argument declared inline: %s */
   2379 		warning(269, sym->s_name);
   2380 
   2381 	/*
   2382 	 * Arguments must have complete types. lengths() prints the needed
   2383 	 * error messages (null dimension is impossible because arrays are
   2384 	 * converted to pointers).
   2385 	 */
   2386 	if (sym->s_type->t_tspec != VOID)
   2387 		(void)length(sym->s_type, sym->s_name);
   2388 
   2389 	sym->s_used = dcs->d_used;
   2390 	mark_as_set(sym);
   2391 
   2392 	return sym;
   2393 }
   2394 
   2395 void
   2396 check_func_lint_directives(void)
   2397 {
   2398 	sym_t *arg;
   2399 	int narg, n;
   2400 	tspec_t t;
   2401 
   2402 	/* check for illegal combinations of lint directives */
   2403 	if (prflstrg != -1 && scflstrg != -1) {
   2404 		/* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
   2405 		warning(289);
   2406 		prflstrg = scflstrg = -1;
   2407 	}
   2408 	if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) {
   2409 		/* dubious use of ** VARARGS ** with ** %s ** */
   2410 		warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE");
   2411 		nvararg = -1;
   2412 	}
   2413 
   2414 	/*
   2415 	 * check if the argument of a lint directive is compatible with the
   2416 	 * number of arguments.
   2417 	 */
   2418 	narg = 0;
   2419 	for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_next)
   2420 		narg++;
   2421 	if (nargusg > narg) {
   2422 		/* argument number mismatch with directive: ** %s ** */
   2423 		warning(283, "ARGSUSED");
   2424 		nargusg = 0;
   2425 	}
   2426 	if (nvararg > narg) {
   2427 		/* argument number mismatch with directive: ** %s ** */
   2428 		warning(283, "VARARGS");
   2429 		nvararg = 0;
   2430 	}
   2431 	if (prflstrg > narg) {
   2432 		/* argument number mismatch with directive: ** %s ** */
   2433 		warning(283, "PRINTFLIKE");
   2434 		prflstrg = -1;
   2435 	} else if (prflstrg == 0) {
   2436 		prflstrg = -1;
   2437 	}
   2438 	if (scflstrg > narg) {
   2439 		/* argument number mismatch with directive: ** %s ** */
   2440 		warning(283, "SCANFLIKE");
   2441 		scflstrg = -1;
   2442 	} else if (scflstrg == 0) {
   2443 		scflstrg = -1;
   2444 	}
   2445 	if (prflstrg != -1 || scflstrg != -1) {
   2446 		narg = prflstrg != -1 ? prflstrg : scflstrg;
   2447 		arg = dcs->d_fargs;
   2448 		for (n = 1; n < narg; n++)
   2449 			arg = arg->s_next;
   2450 		if (arg->s_type->t_tspec != PTR ||
   2451 		    ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
   2452 		     t != UCHAR && t != SCHAR)) {
   2453 			/* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
   2454 			warning(293, narg);
   2455 			prflstrg = scflstrg = -1;
   2456 		}
   2457 	}
   2458 }
   2459 
   2460 /*
   2461  * Warn about arguments in old style function definitions that default to int.
   2462  * Check that an old style function definition is compatible to a previous
   2463  * prototype.
   2464  */
   2465 void
   2466 check_func_old_style_arguments(void)
   2467 {
   2468 	sym_t *args, *arg, *pargs, *parg;
   2469 	int narg, nparg, msg;
   2470 
   2471 	args = funcsym->s_args;
   2472 	pargs = funcsym->s_type->t_args;
   2473 
   2474 	/*
   2475 	 * print a warning for each argument of an old style function
   2476 	 * definition which defaults to int
   2477 	 */
   2478 	for (arg = args; arg != NULL; arg = arg->s_next) {
   2479 		if (arg->s_defarg) {
   2480 			/* argument type defaults to 'int': %s */
   2481 			warning(32, arg->s_name);
   2482 			arg->s_defarg = 0;
   2483 			mark_as_set(arg);
   2484 		}
   2485 	}
   2486 
   2487 	/*
   2488 	 * If this is an old style function definition and a prototype
   2489 	 * exists, compare the types of arguments.
   2490 	 */
   2491 	if (funcsym->s_osdef && funcsym->s_type->t_proto) {
   2492 		/*
   2493 		 * If the number of arguments does not match, we need not
   2494 		 * continue.
   2495 		 */
   2496 		narg = nparg = 0;
   2497 		msg = 0;
   2498 		for (parg = pargs; parg != NULL; parg = parg->s_next)
   2499 			nparg++;
   2500 		for (arg = args; arg != NULL; arg = arg->s_next)
   2501 			narg++;
   2502 		if (narg != nparg) {
   2503 			/* parameter mismatch: %d declared, %d defined */
   2504 			error(51, nparg, narg);
   2505 			msg = 1;
   2506 		} else {
   2507 			parg = pargs;
   2508 			arg = args;
   2509 			while (narg--) {
   2510 				msg |= check_prototype_declaration(arg, parg);
   2511 				parg = parg->s_next;
   2512 				arg = arg->s_next;
   2513 			}
   2514 		}
   2515 		if (msg)
   2516 			/* prototype declaration */
   2517 			print_previous_declaration(285, dcs->d_rdcsym);
   2518 
   2519 		/* from now on the prototype is valid */
   2520 		funcsym->s_osdef = 0;
   2521 		funcsym->s_args = NULL;
   2522 	}
   2523 }
   2524 
   2525 /*
   2526  * Checks compatibility of an old style function definition with a previous
   2527  * prototype declaration.
   2528  * Returns 1 if the position of the previous declaration should be reported.
   2529  */
   2530 static int
   2531 check_prototype_declaration(sym_t *arg, sym_t *parg)
   2532 {
   2533 	type_t	*tp, *ptp;
   2534 	int	dowarn, msg;
   2535 
   2536 	tp = arg->s_type;
   2537 	ptp = parg->s_type;
   2538 
   2539 	msg = 0;
   2540 	dowarn = 0;
   2541 
   2542 	if (!eqtype(tp, ptp, 1, 1, &dowarn)) {
   2543 		if (eqtype(tp, ptp, 1, 0, &dowarn)) {
   2544 			/* type does not match prototype: %s */
   2545 			msg = gnuism(58, arg->s_name);
   2546 		} else {
   2547 			/* type does not match prototype: %s */
   2548 			error(58, arg->s_name);
   2549 			msg = 1;
   2550 		}
   2551 	} else if (dowarn) {
   2552 		/* type does not match prototype: %s */
   2553 		(*(sflag ? error : warning))(58, arg->s_name);
   2554 		msg = 1;
   2555 	}
   2556 
   2557 	return msg;
   2558 }
   2559 
   2560 /*
   2561  * Completes a single local declaration/definition.
   2562  */
   2563 void
   2564 decl1loc(sym_t *dsym, int initflg)
   2565 {
   2566 
   2567 	/* Correct a mistake done in declarator_name(). */
   2568 	if (dsym->s_type->t_tspec == FUNC) {
   2569 		dsym->s_def = DECL;
   2570 		if (dcs->d_scl == NOSCL)
   2571 			dsym->s_scl = EXTERN;
   2572 	}
   2573 
   2574 	if (dsym->s_type->t_tspec == FUNC) {
   2575 		if (dsym->s_scl == STATIC) {
   2576 			/* dubious static function at block level: %s */
   2577 			warning(93, dsym->s_name);
   2578 			dsym->s_scl = EXTERN;
   2579 		} else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
   2580 			/* function has illegal storage class: %s */
   2581 			error(94, dsym->s_name);
   2582 			dsym->s_scl = EXTERN;
   2583 		}
   2584 	}
   2585 
   2586 	/*
   2587 	 * functions may be declared inline at local scope, although
   2588 	 * this has no effect for a later definition of the same
   2589 	 * function.
   2590 	 * XXX it should have an effect if tflag is set. this would
   2591 	 * also be the way gcc behaves.
   2592 	 */
   2593 	if (dcs->d_inline) {
   2594 		if (dsym->s_type->t_tspec == FUNC) {
   2595 			dsym->s_inline = 1;
   2596 		} else {
   2597 			/* variable declared inline: %s */
   2598 			warning(268, dsym->s_name);
   2599 		}
   2600 	}
   2601 
   2602 	check_function_definition(dsym, 1);
   2603 
   2604 	check_type(dsym);
   2605 
   2606 	if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN)
   2607 		ledecl(dsym);
   2608 
   2609 	if (dsym->s_scl == EXTERN) {
   2610 		/*
   2611 		 * XXX if the static variable at level 0 is only defined
   2612 		 * later, checking will be possible.
   2613 		 */
   2614 		if (dsym->s_ext_sym == NULL) {
   2615 			outsym(dsym, EXTERN, dsym->s_def);
   2616 		} else {
   2617 			outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
   2618 		}
   2619 	}
   2620 
   2621 	if (dcs->d_rdcsym != NULL) {
   2622 
   2623 		if (dcs->d_rdcsym->s_blklev == 0) {
   2624 
   2625 			switch (dsym->s_scl) {
   2626 			case AUTO:
   2627 				/* automatic hides external declaration: %s */
   2628 				if (hflag)
   2629 					warning(86, dsym->s_name);
   2630 				break;
   2631 			case STATIC:
   2632 				/* static hides external declaration: %s */
   2633 				if (hflag)
   2634 					warning(87, dsym->s_name);
   2635 				break;
   2636 			case TYPEDEF:
   2637 				/* typedef hides external declaration: %s */
   2638 				if (hflag)
   2639 					warning(88, dsym->s_name);
   2640 				break;
   2641 			case EXTERN:
   2642 				/*
   2643 				 * Warnings and errors are printed in ledecl()
   2644 				 */
   2645 				break;
   2646 			default:
   2647 				LERROR("decl1loc()");
   2648 			}
   2649 
   2650 		} else if (dcs->d_rdcsym->s_blklev == blklev) {
   2651 
   2652 			/* no hflag, because it's illegal! */
   2653 			if (dcs->d_rdcsym->s_arg) {
   2654 				/*
   2655 				 * if !tflag, a "redeclaration of %s" error
   2656 				 * is produced below
   2657 				 */
   2658 				if (tflag) {
   2659 					if (hflag)
   2660 						/* decl. hides parameter: %s */
   2661 						warning(91, dsym->s_name);
   2662 					rmsym(dcs->d_rdcsym);
   2663 				}
   2664 			}
   2665 
   2666 		} else if (dcs->d_rdcsym->s_blklev < blklev) {
   2667 
   2668 			if (hflag)
   2669 				/* declaration hides earlier one: %s */
   2670 				warning(95, dsym->s_name);
   2671 
   2672 		}
   2673 
   2674 		if (dcs->d_rdcsym->s_blklev == blklev) {
   2675 
   2676 			/* redeclaration of %s */
   2677 			error(27, dsym->s_name);
   2678 			rmsym(dcs->d_rdcsym);
   2679 
   2680 		}
   2681 
   2682 	}
   2683 
   2684 	if (initflg && !(initerr = check_init(dsym))) {
   2685 		dsym->s_def = DEF;
   2686 		mark_as_set(dsym);
   2687 	}
   2688 
   2689 	if (dsym->s_scl == TYPEDEF) {
   2690 		dsym->s_type = duptyp(dsym->s_type);
   2691 		dsym->s_type->t_typedef = 1;
   2692 		settdsym(dsym->s_type, dsym);
   2693 	}
   2694 
   2695 	/*
   2696 	 * Before we can check the size we must wait for a initialization
   2697 	 * which may follow.
   2698 	 */
   2699 }
   2700 
   2701 /*
   2702  * Processes (re)declarations of external symbols inside blocks.
   2703  */
   2704 static void
   2705 ledecl(sym_t *dsym)
   2706 {
   2707 	int	eqt, dowarn;
   2708 	sym_t	*esym;
   2709 
   2710 	/* look for a symbol with the same name */
   2711 	esym = dcs->d_rdcsym;
   2712 	while (esym != NULL && esym->s_blklev != 0) {
   2713 		while ((esym = esym->s_link) != NULL) {
   2714 			if (esym->s_kind != FVFT)
   2715 				continue;
   2716 			if (strcmp(dsym->s_name, esym->s_name) == 0)
   2717 				break;
   2718 		}
   2719 	}
   2720 	if (esym == NULL)
   2721 		return;
   2722 	if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
   2723 		/* gcc accepts this without a warning, pcc prints an error. */
   2724 		/* redeclaration of %s */
   2725 		warning(27, dsym->s_name);
   2726 		print_previous_declaration(-1, esym);
   2727 		return;
   2728 	}
   2729 
   2730 	dowarn = 0;
   2731 	eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &dowarn);
   2732 
   2733 	if (!eqt || dowarn) {
   2734 		if (esym->s_scl == EXTERN) {
   2735 			/* inconsistent redeclaration of extern: %s */
   2736 			warning(90, dsym->s_name);
   2737 			print_previous_declaration(-1, esym);
   2738 		} else {
   2739 			/* inconsistent redeclaration of static: %s */
   2740 			warning(92, dsym->s_name);
   2741 			print_previous_declaration(-1, esym);
   2742 		}
   2743 	}
   2744 
   2745 	if (eqt) {
   2746 		/*
   2747 		 * Remember the external symbol so we can update usage
   2748 		 * information at the end of the block.
   2749 		 */
   2750 		dsym->s_ext_sym = esym;
   2751 	}
   2752 }
   2753 
   2754 /*
   2755  * Print an error or a warning if the symbol cannot be initialized due
   2756  * to type/storage class. Return 1 if an error has been detected.
   2757  */
   2758 static int
   2759 check_init(sym_t *sym)
   2760 {
   2761 	int	erred;
   2762 
   2763 	erred = 0;
   2764 
   2765 	if (sym->s_type->t_tspec == FUNC) {
   2766 		/* cannot initialize function: %s */
   2767 		error(24, sym->s_name);
   2768 		erred = 1;
   2769 	} else if (sym->s_scl == TYPEDEF) {
   2770 		/* cannot initialize typedef: %s */
   2771 		error(25, sym->s_name);
   2772 		erred = 1;
   2773 	} else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
   2774 		/* cannot initialize "extern" declaration: %s */
   2775 		if (dcs->d_ctx == EXTERN) {
   2776 			warning(26, sym->s_name);
   2777 		} else {
   2778 			error(26, sym->s_name);
   2779 			erred = 1;
   2780 		}
   2781 	}
   2782 
   2783 	return erred;
   2784 }
   2785 
   2786 /*
   2787  * Create a symbol for an abstract declaration.
   2788  */
   2789 sym_t *
   2790 abstract_name(void)
   2791 {
   2792 	sym_t	*sym;
   2793 
   2794 	if (dcs->d_ctx != ABSTRACT && dcs->d_ctx != PARG)
   2795 		LERROR("abstract_name()");
   2796 
   2797 	sym = getblk(sizeof (sym_t));
   2798 
   2799 	sym->s_name = unnamed;
   2800 	sym->s_def = DEF;
   2801 	sym->s_scl = ABSTRACT;
   2802 	sym->s_blklev = -1;
   2803 
   2804 	if (dcs->d_ctx == PARG)
   2805 		sym->s_arg = 1;
   2806 
   2807 	sym->s_type = dcs->d_type;
   2808 	dcs->d_rdcsym = NULL;
   2809 	dcs->d_vararg = 0;
   2810 
   2811 	return sym;
   2812 }
   2813 
   2814 /*
   2815  * Removes anything which has nothing to do on global level.
   2816  */
   2817 void
   2818 global_clean_up(void)
   2819 {
   2820 
   2821 	while (dcs->d_next != NULL)
   2822 		popdecl();
   2823 
   2824 	cleanup();
   2825 	blklev = 0;
   2826 	mblklev = 0;
   2827 
   2828 	/*
   2829 	 * remove all information about pending lint directives without
   2830 	 * warnings.
   2831 	 */
   2832 	global_clean_up_decl(1);
   2833 }
   2834 
   2835 /*
   2836  * Process an abstract type declaration
   2837  */
   2838 sym_t *
   2839 declare_1_abstract(sym_t *sym)
   2840 {
   2841 
   2842 	check_function_definition(sym, 1);
   2843 	check_type(sym);
   2844 	return sym;
   2845 }
   2846 
   2847 /*
   2848  * Checks size after declarations of variables and their initialisation.
   2849  */
   2850 void
   2851 check_size(sym_t *dsym)
   2852 {
   2853 
   2854 	if (dsym->s_def != DEF)
   2855 		return;
   2856 	if (dsym->s_scl == TYPEDEF)
   2857 		return;
   2858 	if (dsym->s_type->t_tspec == FUNC)
   2859 		return;
   2860 
   2861 	if (length(dsym->s_type, dsym->s_name) == 0 &&
   2862 	    dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
   2863 		/* empty array declaration: %s */
   2864 		if (tflag) {
   2865 			warning(190, dsym->s_name);
   2866 		} else {
   2867 			error(190, dsym->s_name);
   2868 		}
   2869 	}
   2870 }
   2871 
   2872 /*
   2873  * Mark an object as set if it is not already
   2874  */
   2875 void
   2876 mark_as_set(sym_t *sym)
   2877 {
   2878 
   2879 	if (!sym->s_set) {
   2880 		sym->s_set = 1;
   2881 		UNIQUE_CURR_POS(sym->s_set_pos);
   2882 	}
   2883 }
   2884 
   2885 /*
   2886  * Mark an object as used if it is not already
   2887  */
   2888 void
   2889 mark_as_used(sym_t *sym, int fcall, int szof)
   2890 {
   2891 
   2892 	if (!sym->s_used) {
   2893 		sym->s_used = 1;
   2894 		UNIQUE_CURR_POS(sym->s_use_pos);
   2895 	}
   2896 	/*
   2897 	 * for function calls another record is written
   2898 	 *
   2899 	 * XXX Should symbols used in sizeof() be treated as used or not?
   2900 	 * Probably not, because there is no sense to declare an
   2901 	 * external variable only to get their size.
   2902 	 */
   2903 	if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
   2904 		outusg(sym);
   2905 }
   2906 
   2907 /*
   2908  * Prints warnings for a list of variables and labels (concatenated
   2909  * with s_dlnxt) if these are not used or only set.
   2910  */
   2911 void
   2912 check_usage(dinfo_t *di)
   2913 {
   2914 	sym_t	*sym;
   2915 	int	mklwarn;
   2916 
   2917 	/* for this warning LINTED has no effect */
   2918 	mklwarn = lwarn;
   2919 	lwarn = LWARN_ALL;
   2920 
   2921 #ifdef DEBUG
   2922 	printf("%s, %d: >temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
   2923 	    lwarn);
   2924 #endif
   2925 	for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
   2926 		check_usage_sym(di->d_asm, sym);
   2927 	lwarn = mklwarn;
   2928 #ifdef DEBUG
   2929 	printf("%s, %d: <temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
   2930 	    lwarn);
   2931 #endif
   2932 }
   2933 
   2934 /*
   2935  * Prints a warning for a single variable or label if it is not used or
   2936  * only set.
   2937  */
   2938 void
   2939 check_usage_sym(int novar, sym_t *sym)
   2940 {
   2941 	pos_t	cpos;
   2942 
   2943 	if (sym->s_blklev == -1)
   2944 		return;
   2945 
   2946 	cpos = curr_pos;
   2947 
   2948 	if (sym->s_kind == FVFT) {
   2949 		if (sym->s_arg) {
   2950 			check_argument_usage(novar, sym);
   2951 		} else {
   2952 			check_variable_usage(novar, sym);
   2953 		}
   2954 	} else if (sym->s_kind == FLABEL) {
   2955 		check_label_usage(sym);
   2956 	} else if (sym->s_kind == FTAG) {
   2957 		check_tag_usage(sym);
   2958 	}
   2959 
   2960 	curr_pos = cpos;
   2961 }
   2962 
   2963 static void
   2964 check_argument_usage(int novar, sym_t *arg)
   2965 {
   2966 
   2967 	if (!arg->s_set)
   2968 		LERROR("check_argument_usage()");
   2969 
   2970 	if (novar)
   2971 		return;
   2972 
   2973 	if (!arg->s_used && vflag) {
   2974 		curr_pos = arg->s_def_pos;
   2975 		/* argument %s unused in function %s */
   2976 		warning(231, arg->s_name, funcsym->s_name);
   2977 	}
   2978 }
   2979 
   2980 static void
   2981 check_variable_usage(int novar, sym_t *sym)
   2982 {
   2983 	scl_t	sc;
   2984 	sym_t	*xsym;
   2985 
   2986 	if (blklev == 0 || sym->s_blklev == 0)
   2987 		LERROR("check_variable_usage()");
   2988 
   2989 	/* errors in expressions easily cause lots of these warnings */
   2990 	if (nerr != 0)
   2991 		return;
   2992 
   2993 	/*
   2994 	 * XXX Only variables are checked, although types should
   2995 	 * probably also be checked
   2996 	 */
   2997 	if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
   2998 	    sc != AUTO && sc != REG) {
   2999 		return;
   3000 	}
   3001 
   3002 	if (novar)
   3003 		return;
   3004 
   3005 	if (sc == EXTERN) {
   3006 		if (!sym->s_used && !sym->s_set) {
   3007 			curr_pos = sym->s_def_pos;
   3008 			/* %s unused in function %s */
   3009 			warning(192, sym->s_name, funcsym->s_name);
   3010 		}
   3011 	} else {
   3012 		if (sym->s_set && !sym->s_used) {
   3013 			curr_pos = sym->s_set_pos;
   3014 			/* %s set but not used in function %s */
   3015 			warning(191, sym->s_name, funcsym->s_name);
   3016 		} else if (!sym->s_used) {
   3017 			curr_pos = sym->s_def_pos;
   3018 			/* %s unused in function %s */
   3019 			warning(192, sym->s_name, funcsym->s_name);
   3020 		}
   3021 	}
   3022 
   3023 	if (sc == EXTERN) {
   3024 		/*
   3025 		 * information about usage is taken over into the symbol
   3026 		 * table entry at level 0 if the symbol was locally declared
   3027 		 * as an external symbol.
   3028 		 *
   3029 		 * XXX This is wrong for symbols declared static at level 0
   3030 		 * if the usage information stems from sizeof(). This is
   3031 		 * because symbols at level 0 only used in sizeof() are
   3032 		 * considered to not be used.
   3033 		 */
   3034 		if ((xsym = sym->s_ext_sym) != NULL) {
   3035 			if (sym->s_used && !xsym->s_used) {
   3036 				xsym->s_used = 1;
   3037 				xsym->s_use_pos = sym->s_use_pos;
   3038 			}
   3039 			if (sym->s_set && !xsym->s_set) {
   3040 				xsym->s_set = 1;
   3041 				xsym->s_set_pos = sym->s_set_pos;
   3042 			}
   3043 		}
   3044 	}
   3045 }
   3046 
   3047 static void
   3048 check_label_usage(sym_t *lab)
   3049 {
   3050 
   3051 	if (blklev != 1 || lab->s_blklev != 1)
   3052 		LERROR("check_label_usage()");
   3053 
   3054 	if (lab->s_set && !lab->s_used) {
   3055 		curr_pos = lab->s_set_pos;
   3056 		/* %s unused in function %s */
   3057 		warning(192, lab->s_name, funcsym->s_name);
   3058 	} else if (!lab->s_set) {
   3059 		curr_pos = lab->s_use_pos;
   3060 		/* undefined label %s */
   3061 		warning(23, lab->s_name);
   3062 	}
   3063 }
   3064 
   3065 static void
   3066 check_tag_usage(sym_t *sym)
   3067 {
   3068 
   3069 	if (!incompl(sym->s_type))
   3070 		return;
   3071 
   3072 	/* always complain about incomplete tags declared inside blocks */
   3073 	if (!zflag || dcs->d_ctx != EXTERN)
   3074 		return;
   3075 
   3076 	curr_pos = sym->s_def_pos;
   3077 	switch (sym->s_type->t_tspec) {
   3078 	case STRUCT:
   3079 		/* struct %s never defined */
   3080 		warning(233, sym->s_name);
   3081 		break;
   3082 	case UNION:
   3083 		/* union %s never defined */
   3084 		warning(234, sym->s_name);
   3085 		break;
   3086 	case ENUM:
   3087 		/* enum %s never defined */
   3088 		warning(235, sym->s_name);
   3089 		break;
   3090 	default:
   3091 		LERROR("check_tag_usage()");
   3092 	}
   3093 }
   3094 
   3095 /*
   3096  * Called after the entire translation unit has been parsed.
   3097  * Changes tentative definitions into definitions.
   3098  * Performs some tests on global symbols. Detected problems are:
   3099  * - defined variables of incomplete type
   3100  * - constant variables which are not initialized
   3101  * - static symbols which are never used
   3102  */
   3103 void
   3104 check_global_symbols(void)
   3105 {
   3106 	sym_t	*sym;
   3107 	pos_t	cpos;
   3108 
   3109 	if (blklev != 0 || dcs->d_next != NULL)
   3110 		norecover();
   3111 
   3112 	cpos = curr_pos;
   3113 
   3114 	for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
   3115 		if (sym->s_blklev == -1)
   3116 			continue;
   3117 		if (sym->s_kind == FVFT) {
   3118 			check_global_variable(sym);
   3119 		} else if (sym->s_kind == FTAG) {
   3120 			check_tag_usage(sym);
   3121 		} else {
   3122 			if (sym->s_kind != FMEMBER)
   3123 				LERROR("check_global_symbols()");
   3124 		}
   3125 	}
   3126 
   3127 	curr_pos = cpos;
   3128 }
   3129 
   3130 static void
   3131 check_global_variable(sym_t *sym)
   3132 {
   3133 
   3134 	if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON)
   3135 		return;
   3136 
   3137 	if (sym->s_scl != EXTERN && sym->s_scl != STATIC)
   3138 		LERROR("check_global_variable()");
   3139 
   3140 	check_global_variable_size(sym);
   3141 
   3142 	if (sym->s_scl == STATIC) {
   3143 		if (sym->s_type->t_tspec == FUNC) {
   3144 			if (sym->s_used && sym->s_def != DEF) {
   3145 				curr_pos = sym->s_use_pos;
   3146 				/* static func. called but not def... */
   3147 				error(225, sym->s_name);
   3148 			}
   3149 		}
   3150 		if (!sym->s_used) {
   3151 			curr_pos = sym->s_def_pos;
   3152 			if (sym->s_type->t_tspec == FUNC) {
   3153 				if (sym->s_def == DEF) {
   3154 					if (!sym->s_inline)
   3155 						/* static function %s unused */
   3156 						warning(236, sym->s_name);
   3157 				} else {
   3158 					/* static function %s declared but... */
   3159 					warning(290, sym->s_name);
   3160 				}
   3161 			} else if (!sym->s_set) {
   3162 				/* static variable %s unused */
   3163 				warning(226, sym->s_name);
   3164 			} else {
   3165 				/* static variable %s set but not used */
   3166 				warning(307, sym->s_name);
   3167 			}
   3168 		}
   3169 		if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
   3170 			curr_pos = sym->s_def_pos;
   3171 			/* const object %s should have initializer */
   3172 			warning(227, sym->s_name);
   3173 		}
   3174 	}
   3175 }
   3176 
   3177 static void
   3178 check_global_variable_size(sym_t *sym)
   3179 {
   3180 
   3181 	if (sym->s_def == TDEF) {
   3182 		if (sym->s_type->t_tspec == FUNC)
   3183 			/*
   3184 			 * this can happen if an syntax error occurred
   3185 			 * after a function declaration
   3186 			 */
   3187 			return;
   3188 		curr_pos = sym->s_def_pos;
   3189 		if (length(sym->s_type, sym->s_name) == 0 &&
   3190 		    sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
   3191 			/* empty array declaration: %s */
   3192 			if (tflag || (sym->s_scl == EXTERN && !sflag)) {
   3193 				warning(190, sym->s_name);
   3194 			} else {
   3195 				error(190, sym->s_name);
   3196 			}
   3197 		}
   3198 	}
   3199 }
   3200 
   3201 /*
   3202  * Prints information about location of previous definition/declaration.
   3203  */
   3204 void
   3205 print_previous_declaration(int msg, sym_t *psym)
   3206 {
   3207 	pos_t	cpos;
   3208 
   3209 	if (!rflag)
   3210 		return;
   3211 
   3212 	cpos = curr_pos;
   3213 	curr_pos = psym->s_def_pos;
   3214 	if (msg != -1) {
   3215 		message(msg, psym->s_name);
   3216 	} else if (psym->s_def == DEF || psym->s_def == TDEF) {
   3217 		/* previous definition of %s */
   3218 		message(261, psym->s_name);
   3219 	} else {
   3220 		/* previous declaration of %s */
   3221 		message(260, psym->s_name);
   3222 	}
   3223 	curr_pos = cpos;
   3224 }
   3225