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