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