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