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