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