Home | History | Annotate | Line # | Download | only in lint1
decl.c revision 1.228
      1 /* $NetBSD: decl.c,v 1.228 2021/08/29 15:49:04 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.228 2021/08/29 15:49:04 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 param. cannot have name: %s */
   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 					/* decl. introduces new type ... */
   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 			/* declaration introduces new type in ANSI C: %s %s */
   1766 			if (!sflag) {
   1767 				/* decl. introduces new type in ANSI C: %s %s */
   1768 				warning(44, storage_class_name(scl),
   1769 				    tag->s_name);
   1770 			}
   1771 			tag = pushdown(tag);
   1772 			dcs->d_next->d_nonempty_decl = true;
   1773 		}
   1774 	} else {
   1775 		if (tag->s_scl != scl) {
   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 (decl && !is_incomplete(tag->s_type)) {
   1783 			/* %s tag '%s' redeclared as %s */
   1784 			error(46, storage_class_name(tag->s_scl),
   1785 			    tag->s_name, storage_class_name(scl));
   1786 			print_previous_declaration(-1, tag);
   1787 			tag = pushdown(tag);
   1788 			dcs->d_next->d_nonempty_decl = true;
   1789 		} else if (semi || decl) {
   1790 			dcs->d_next->d_nonempty_decl = true;
   1791 		}
   1792 	}
   1793 	return tag;
   1794 }
   1795 
   1796 const char *
   1797 storage_class_name(scl_t sc)
   1798 {
   1799 	switch (sc) {
   1800 	case EXTERN:	return "extern";
   1801 	case STATIC:	return "static";
   1802 	case AUTO:	return "auto";
   1803 	case REG:	return "register";
   1804 	case TYPEDEF:	return "typedef";
   1805 	case STRUCT_TAG:return "struct";
   1806 	case UNION_TAG:	return "union";
   1807 	case ENUM_TAG:	return "enum";
   1808 	default:	lint_assert(/*CONSTCOND*/false);
   1809 	}
   1810 	/* NOTREACHED */
   1811 }
   1812 
   1813 /*
   1814  * tp points to the type of the tag, fmem to the list of members.
   1815  */
   1816 type_t *
   1817 complete_tag_struct_or_union(type_t *tp, sym_t *fmem)
   1818 {
   1819 	tspec_t	t;
   1820 	struct_or_union	*sp;
   1821 	int	n;
   1822 	sym_t	*mem;
   1823 
   1824 	if (tp == NULL)		/* in case of syntax errors */
   1825 		return gettyp(INT);
   1826 
   1827 	setcomplete(tp, true);
   1828 
   1829 	t = tp->t_tspec;
   1830 	align((u_int)dcs->d_sou_align_in_bits, 0);
   1831 	sp = tp->t_str;
   1832 	sp->sou_align_in_bits = dcs->d_sou_align_in_bits;
   1833 	sp->sou_first_member = fmem;
   1834 	if (tp->t_packed)
   1835 		setpackedsize(tp);
   1836 	else
   1837 		sp->sou_size_in_bits = dcs->d_offset;
   1838 
   1839 	if (sp->sou_size_in_bits == 0) {
   1840 		/* zero sized %s is a C9X feature */
   1841 		c99ism(47, ttab[t].tt_name);
   1842 	}
   1843 
   1844 	n = 0;
   1845 	for (mem = fmem; mem != NULL; mem = mem->s_next) {
   1846 		/* bind anonymous members to the structure */
   1847 		if (mem->s_styp == NULL) {
   1848 			mem->s_styp = sp;
   1849 			if (mem->s_type->t_bitfield) {
   1850 				sp->sou_size_in_bits += bitfieldsize(&mem);
   1851 				if (mem == NULL)
   1852 					break;
   1853 			}
   1854 			sp->sou_size_in_bits +=
   1855 			    type_size_in_bits(mem->s_type);
   1856 		}
   1857 		if (mem->s_name != unnamed)
   1858 			n++;
   1859 	}
   1860 
   1861 	if (n == 0 && sp->sou_size_in_bits != 0) {
   1862 		/* %s has no named members */
   1863 		warning(65, t == STRUCT ? "structure" : "union");
   1864 	}
   1865 	return tp;
   1866 }
   1867 
   1868 type_t *
   1869 complete_tag_enum(type_t *tp, sym_t *fmem)
   1870 {
   1871 
   1872 	setcomplete(tp, true);
   1873 	tp->t_enum->en_first_enumerator = fmem;
   1874 	return tp;
   1875 }
   1876 
   1877 /*
   1878  * Processes the name of an enumerator in an enum declaration.
   1879  *
   1880  * sym points to the enumerator
   1881  * val is the value of the enumerator
   1882  * impl is true if the value of the enumerator was not explicitly specified.
   1883  */
   1884 sym_t *
   1885 enumeration_constant(sym_t *sym, int val, bool impl)
   1886 {
   1887 
   1888 	if (sym->s_scl != NOSCL) {
   1889 		if (sym->s_block_level == block_level) {
   1890 			/* no hflag, because this is illegal!!! */
   1891 			if (sym->s_arg) {
   1892 				/* enumeration constant hides parameter: %s */
   1893 				warning(57, sym->s_name);
   1894 			} else {
   1895 				/* redeclaration of %s */
   1896 				error(27, sym->s_name);
   1897 				/*
   1898 				 * inside blocks it should not be too
   1899 				 * complicated to find the position of the
   1900 				 * previous declaration
   1901 				 */
   1902 				if (block_level == 0)
   1903 					print_previous_declaration(-1, sym);
   1904 			}
   1905 		} else {
   1906 			if (hflag)
   1907 				/* redefinition hides earlier one: %s */
   1908 				warning(43, sym->s_name);
   1909 		}
   1910 		sym = pushdown(sym);
   1911 	}
   1912 	sym->s_scl = CTCONST;
   1913 	sym->s_type = dcs->d_tagtyp;
   1914 	sym->s_value.v_tspec = INT;
   1915 	sym->s_value.v_quad = val;
   1916 	if (impl && val - 1 == TARG_INT_MAX) {
   1917 		/* overflow in enumeration values: %s */
   1918 		warning(48, sym->s_name);
   1919 	}
   1920 	enumval = val + 1;
   1921 	return sym;
   1922 }
   1923 
   1924 /*
   1925  * Process a single external declarator.
   1926  */
   1927 static void
   1928 declare_extern(sym_t *dsym, bool initflg, sbuf_t *renaming)
   1929 {
   1930 	bool	dowarn, rval, redec;
   1931 	sym_t	*rdsym;
   1932 	char	*s;
   1933 
   1934 	if (renaming != NULL) {
   1935 		lint_assert(dsym->s_rename == NULL);
   1936 
   1937 		s = getlblk(1, renaming->sb_len + 1);
   1938 		(void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
   1939 		dsym->s_rename = s;
   1940 	}
   1941 
   1942 	check_function_definition(dsym, true);
   1943 
   1944 	check_type(dsym);
   1945 
   1946 	if (initflg && !check_init(dsym))
   1947 		dsym->s_def = DEF;
   1948 
   1949 	/*
   1950 	 * Declarations of functions are marked as "tentative" in
   1951 	 * declarator_name(). This is wrong because there are no
   1952 	 * tentative function definitions.
   1953 	 */
   1954 	if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
   1955 		dsym->s_def = DECL;
   1956 
   1957 	if (dcs->d_inline) {
   1958 		if (dsym->s_type->t_tspec == FUNC) {
   1959 			dsym->s_inline = true;
   1960 		} else {
   1961 			/* variable declared inline: %s */
   1962 			warning(268, dsym->s_name);
   1963 		}
   1964 	}
   1965 
   1966 	/* Write the declaration into the output file */
   1967 	if (plibflg && llibflg &&
   1968 	    dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
   1969 		/*
   1970 		 * With both LINTLIBRARY and PROTOLIB the prototype is
   1971 		 * written as a function definition to the output file.
   1972 		 */
   1973 		rval = dsym->s_type->t_subt->t_tspec != VOID;
   1974 		outfdef(dsym, &dsym->s_def_pos, rval, false, NULL);
   1975 	} else if (!is_compiler_builtin(dsym->s_name)) {
   1976 		outsym(dsym, dsym->s_scl, dsym->s_def);
   1977 	}
   1978 
   1979 	if ((rdsym = dcs->d_redeclared_symbol) != NULL) {
   1980 
   1981 		/*
   1982 		 * If the old symbol stems from an old style function
   1983 		 * definition, we have remembered the params in rdsmy->s_args
   1984 		 * and compare them with the params of the prototype.
   1985 		 */
   1986 		if (rdsym->s_osdef && dsym->s_type->t_proto) {
   1987 			redec = check_old_style_definition(rdsym, dsym);
   1988 		} else {
   1989 			redec = false;
   1990 		}
   1991 
   1992 		if (!redec &&
   1993 		    !check_redeclaration(dsym, (dowarn = false, &dowarn))) {
   1994 
   1995 			if (dowarn) {
   1996 				if (sflag)
   1997 					/* redeclaration of %s */
   1998 					error(27, dsym->s_name);
   1999 				else
   2000 					/* redeclaration of %s */
   2001 					warning(27, dsym->s_name);
   2002 				print_previous_declaration(-1, rdsym);
   2003 			}
   2004 
   2005 			/*
   2006 			 * Take over the remembered params if the new symbol
   2007 			 * is not a prototype.
   2008 			 */
   2009 			if (rdsym->s_osdef && !dsym->s_type->t_proto) {
   2010 				dsym->s_osdef = rdsym->s_osdef;
   2011 				dsym->s_args = rdsym->s_args;
   2012 				dsym->s_def_pos = rdsym->s_def_pos;
   2013 			}
   2014 
   2015 			/*
   2016 			 * Remember the position of the declaration if the
   2017 			 * old symbol was a prototype and the new is not.
   2018 			 * Also remember the position if the old symbol
   2019 			 * was defined and the new is not.
   2020 			 */
   2021 			if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
   2022 				dsym->s_def_pos = rdsym->s_def_pos;
   2023 			} else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
   2024 				dsym->s_def_pos = rdsym->s_def_pos;
   2025 			}
   2026 
   2027 			/*
   2028 			 * Copy usage information of the name into the new
   2029 			 * symbol.
   2030 			 */
   2031 			copy_usage_info(dsym, rdsym);
   2032 
   2033 			/* Once a name is defined, it remains defined. */
   2034 			if (rdsym->s_def == DEF)
   2035 				dsym->s_def = DEF;
   2036 
   2037 			/* once a function is inline, it remains inline */
   2038 			if (rdsym->s_inline)
   2039 				dsym->s_inline = true;
   2040 
   2041 			complete_type(dsym, rdsym);
   2042 
   2043 		}
   2044 
   2045 		rmsym(rdsym);
   2046 	}
   2047 
   2048 	if (dsym->s_scl == TYPEDEF) {
   2049 		dsym->s_type = dup_type(dsym->s_type);
   2050 		dsym->s_type->t_typedef = true;
   2051 		settdsym(dsym->s_type, dsym);
   2052 	}
   2053 
   2054 }
   2055 
   2056 void
   2057 declare(sym_t *decl, bool initflg, sbuf_t *renaming)
   2058 {
   2059 
   2060 	if (dcs->d_ctx == EXTERN) {
   2061 		declare_extern(decl, initflg, renaming);
   2062 	} else if (dcs->d_ctx == ARG || dcs->d_ctx == PROTO_ARG) {
   2063 		if (renaming != NULL) {
   2064 			/* symbol renaming can't be used on function arguments */
   2065 			error(310);
   2066 		} else
   2067 			(void)declare_argument(decl, initflg);
   2068 	} else {
   2069 		lint_assert(dcs->d_ctx == AUTO);
   2070 		if (renaming != NULL) {
   2071 			/* symbol renaming can't be used on automatic variables */
   2072 			error(311);
   2073 		} else
   2074 			declare_local(decl, initflg);
   2075 	}
   2076 }
   2077 
   2078 /*
   2079  * Copies information about usage into a new symbol table entry of
   2080  * the same symbol.
   2081  */
   2082 void
   2083 copy_usage_info(sym_t *sym, sym_t *rdsym)
   2084 {
   2085 
   2086 	sym->s_set_pos = rdsym->s_set_pos;
   2087 	sym->s_use_pos = rdsym->s_use_pos;
   2088 	sym->s_set = rdsym->s_set;
   2089 	sym->s_used = rdsym->s_used;
   2090 }
   2091 
   2092 /*
   2093  * Prints an error and returns true if a symbol is redeclared/redefined.
   2094  * Otherwise returns false and, in some cases of minor problems, prints
   2095  * a warning.
   2096  */
   2097 bool
   2098 check_redeclaration(sym_t *dsym, bool *dowarn)
   2099 {
   2100 	sym_t	*rsym;
   2101 
   2102 	if ((rsym = dcs->d_redeclared_symbol)->s_scl == CTCONST) {
   2103 		/* redeclaration of %s */
   2104 		error(27, dsym->s_name);
   2105 		print_previous_declaration(-1, rsym);
   2106 		return true;
   2107 	}
   2108 	if (rsym->s_scl == TYPEDEF) {
   2109 		/* typedef redeclared: %s */
   2110 		error(89, dsym->s_name);
   2111 		print_previous_declaration(-1, rsym);
   2112 		return true;
   2113 	}
   2114 	if (dsym->s_scl == TYPEDEF) {
   2115 		/* redeclaration of %s */
   2116 		error(27, dsym->s_name);
   2117 		print_previous_declaration(-1, rsym);
   2118 		return true;
   2119 	}
   2120 	if (rsym->s_def == DEF && dsym->s_def == DEF) {
   2121 		/* redefinition of %s */
   2122 		error(28, dsym->s_name);
   2123 		print_previous_declaration(-1, rsym);
   2124 		return true;
   2125 	}
   2126 	if (!eqtype(rsym->s_type, dsym->s_type, false, false, dowarn)) {
   2127 		/* redeclaration of %s */
   2128 		error(27, dsym->s_name);
   2129 		print_previous_declaration(-1, rsym);
   2130 		return true;
   2131 	}
   2132 	if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
   2133 		return false;
   2134 	if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
   2135 		return false;
   2136 	if (rsym->s_scl == STATIC && dsym->s_def == DECL)
   2137 		return false;
   2138 	if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
   2139 		/*
   2140 		 * All cases except "int a = 1; static int a;" are caught
   2141 		 * above with or without a warning
   2142 		 */
   2143 		/* redeclaration of %s */
   2144 		error(27, dsym->s_name);
   2145 		print_previous_declaration(-1, rsym);
   2146 		return true;
   2147 	}
   2148 	if (rsym->s_scl == EXTERN) {
   2149 		/* previously declared extern, becomes static: %s */
   2150 		warning(29, dsym->s_name);
   2151 		print_previous_declaration(-1, rsym);
   2152 		return false;
   2153 	}
   2154 	/*
   2155 	 * Now it's one of:
   2156 	 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
   2157 	 */
   2158 	/* redeclaration of %s; ANSI C requires "static" */
   2159 	if (sflag) {
   2160 		/* redeclaration of %s; ANSI C requires static */
   2161 		warning(30, dsym->s_name);
   2162 		print_previous_declaration(-1, rsym);
   2163 	}
   2164 	dsym->s_scl = STATIC;
   2165 	return false;
   2166 }
   2167 
   2168 static bool
   2169 qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual)
   2170 {
   2171 	if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
   2172 		return false;
   2173 
   2174 	if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
   2175 		return false;
   2176 
   2177 	return true;
   2178 }
   2179 
   2180 bool
   2181 eqptrtype(const type_t *tp1, const type_t *tp2, bool ignqual)
   2182 {
   2183 	if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
   2184 		return false;
   2185 
   2186 	if (!qualifiers_correspond(tp1, tp2, ignqual))
   2187 		return false;
   2188 
   2189 	return true;
   2190 }
   2191 
   2192 
   2193 /*
   2194  * Checks if two types are compatible.
   2195  *
   2196  * ignqual	ignore qualifiers of type; used for function params
   2197  * promot	promote left type; used for comparison of params of
   2198  *		old style function definitions with params of prototypes.
   2199  * *dowarn	set to true if an old style function declaration is not
   2200  *		compatible with a prototype
   2201  */
   2202 bool
   2203 eqtype(const type_t *tp1, const type_t *tp2,
   2204        bool ignqual, bool promot, bool *dowarn)
   2205 {
   2206 	tspec_t	t;
   2207 
   2208 	while (tp1 != NULL && tp2 != NULL) {
   2209 
   2210 		t = tp1->t_tspec;
   2211 		if (promot) {
   2212 			if (t == FLOAT) {
   2213 				t = DOUBLE;
   2214 			} else if (t == CHAR || t == SCHAR) {
   2215 				t = INT;
   2216 			} else if (t == UCHAR) {
   2217 				t = tflag ? UINT : INT;
   2218 			} else if (t == SHORT) {
   2219 				t = INT;
   2220 			} else if (t == USHORT) {
   2221 				/* CONSTCOND */
   2222 				t = TARG_INT_MAX < TARG_USHRT_MAX || tflag ? UINT : INT;
   2223 			}
   2224 		}
   2225 
   2226 		if (t != tp2->t_tspec)
   2227 			return false;
   2228 
   2229 		if (!qualifiers_correspond(tp1, tp2, ignqual))
   2230 			return false;
   2231 
   2232 		if (t == STRUCT || t == UNION)
   2233 			return tp1->t_str == tp2->t_str;
   2234 
   2235 		if (t == ENUM && eflag)
   2236 			return tp1->t_enum == tp2->t_enum;
   2237 
   2238 		if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
   2239 			if (tp1->t_dim != 0 && tp2->t_dim != 0)
   2240 				return false;
   2241 		}
   2242 
   2243 		/* don't check prototypes for traditional */
   2244 		if (t == FUNC && !tflag) {
   2245 			if (tp1->t_proto && tp2->t_proto) {
   2246 				if (!eqargs(tp1, tp2, dowarn))
   2247 					return false;
   2248 			} else if (tp1->t_proto) {
   2249 				if (!mnoarg(tp1, dowarn))
   2250 					return false;
   2251 			} else if (tp2->t_proto) {
   2252 				if (!mnoarg(tp2, dowarn))
   2253 					return false;
   2254 			}
   2255 		}
   2256 
   2257 		tp1 = tp1->t_subt;
   2258 		tp2 = tp2->t_subt;
   2259 		ignqual = promot = false;
   2260 
   2261 	}
   2262 
   2263 	return tp1 == tp2;
   2264 }
   2265 
   2266 /*
   2267  * Compares the parameter types of two prototypes.
   2268  */
   2269 static bool
   2270 eqargs(const type_t *tp1, const type_t *tp2, bool *dowarn)
   2271 {
   2272 	sym_t	*a1, *a2;
   2273 
   2274 	if (tp1->t_vararg != tp2->t_vararg)
   2275 		return false;
   2276 
   2277 	a1 = tp1->t_args;
   2278 	a2 = tp2->t_args;
   2279 
   2280 	while (a1 != NULL && a2 != NULL) {
   2281 
   2282 		if (!eqtype(a1->s_type, a2->s_type, true, false, dowarn))
   2283 			return false;
   2284 
   2285 		a1 = a1->s_next;
   2286 		a2 = a2->s_next;
   2287 
   2288 	}
   2289 
   2290 	return a1 == a2;
   2291 }
   2292 
   2293 /*
   2294  * mnoarg() (matches functions with no argument type information)
   2295  * returns whether all parameters of a prototype are compatible with
   2296  * an old style function declaration.
   2297  * This is the case if the following conditions are met:
   2298  *	1. the prototype has a fixed number of parameters
   2299  *	2. no parameter is of type float
   2300  *	3. no parameter is converted to another type if integer promotion
   2301  *	   is applied on it
   2302  */
   2303 static bool
   2304 mnoarg(const type_t *tp, bool *dowarn)
   2305 {
   2306 	sym_t	*arg;
   2307 	tspec_t	t;
   2308 
   2309 	if (tp->t_vararg) {
   2310 		if (dowarn != NULL)
   2311 			*dowarn = true;
   2312 	}
   2313 	for (arg = tp->t_args; arg != NULL; arg = arg->s_next) {
   2314 		if ((t = arg->s_type->t_tspec) == FLOAT ||
   2315 		    t == CHAR || t == SCHAR || t == UCHAR ||
   2316 		    t == SHORT || t == USHORT) {
   2317 			if (dowarn != NULL)
   2318 				*dowarn = true;
   2319 		}
   2320 	}
   2321 	return true;
   2322 }
   2323 
   2324 /*
   2325  * Compares a prototype declaration with the remembered arguments of
   2326  * a previous old style function definition.
   2327  */
   2328 static bool
   2329 check_old_style_definition(sym_t *rdsym, sym_t *dsym)
   2330 {
   2331 	sym_t	*args, *pargs, *arg, *parg;
   2332 	int	narg, nparg, n;
   2333 	bool	dowarn, msg;
   2334 
   2335 	args = rdsym->s_args;
   2336 	pargs = dsym->s_type->t_args;
   2337 
   2338 	msg = false;
   2339 
   2340 	narg = nparg = 0;
   2341 	for (arg = args; arg != NULL; arg = arg->s_next)
   2342 		narg++;
   2343 	for (parg = pargs; parg != NULL; parg = parg->s_next)
   2344 		nparg++;
   2345 	if (narg != nparg) {
   2346 		/* prototype does not match old-style definition */
   2347 		error(63);
   2348 		msg = true;
   2349 		goto end;
   2350 	}
   2351 
   2352 	arg = args;
   2353 	parg = pargs;
   2354 	n = 1;
   2355 	while (narg-- > 0) {
   2356 		dowarn = false;
   2357 		/*
   2358 		 * If it does not match due to promotion and sflag is
   2359 		 * not set we print only a warning.
   2360 		 */
   2361 		if (!eqtype(arg->s_type, parg->s_type, true, true, &dowarn) ||
   2362 		    dowarn) {
   2363 			/* prototype does not match old style defn., arg #%d */
   2364 			error(299, n);
   2365 			msg = true;
   2366 		}
   2367 		arg = arg->s_next;
   2368 		parg = parg->s_next;
   2369 		n++;
   2370 	}
   2371 
   2372  end:
   2373 	if (msg)
   2374 		/* old style definition */
   2375 		print_previous_declaration(300, rdsym);
   2376 
   2377 	return msg;
   2378 }
   2379 
   2380 /*
   2381  * Completes a type by copying the dimension and prototype information
   2382  * from a second compatible type.
   2383  *
   2384  * Following lines are legal:
   2385  *  "typedef a[]; a b; a b[10]; a c; a c[20];"
   2386  *  "typedef ft(); ft f; f(int); ft g; g(long);"
   2387  * This means that, if a type is completed, the type structure must
   2388  * be duplicated.
   2389  */
   2390 void
   2391 complete_type(sym_t *dsym, sym_t *ssym)
   2392 {
   2393 	type_t	**dstp, *src;
   2394 	type_t	*dst;
   2395 
   2396 	dstp = &dsym->s_type;
   2397 	src = ssym->s_type;
   2398 
   2399 	while ((dst = *dstp) != NULL) {
   2400 		lint_assert(src != NULL);
   2401 		lint_assert(dst->t_tspec == src->t_tspec);
   2402 		if (dst->t_tspec == ARRAY) {
   2403 			if (dst->t_dim == 0 && src->t_dim != 0) {
   2404 				*dstp = dst = dup_type(dst);
   2405 				dst->t_dim = src->t_dim;
   2406 				setcomplete(dst, true);
   2407 			}
   2408 		} else if (dst->t_tspec == FUNC) {
   2409 			if (!dst->t_proto && src->t_proto) {
   2410 				*dstp = dst = dup_type(dst);
   2411 				dst->t_proto = true;
   2412 				dst->t_args = src->t_args;
   2413 			}
   2414 		}
   2415 		dstp = &dst->t_subt;
   2416 		src = src->t_subt;
   2417 	}
   2418 }
   2419 
   2420 /*
   2421  * Completes the declaration of a single argument.
   2422  */
   2423 sym_t *
   2424 declare_argument(sym_t *sym, bool initflg)
   2425 {
   2426 	tspec_t	t;
   2427 
   2428 	check_function_definition(sym, true);
   2429 
   2430 	check_type(sym);
   2431 
   2432 	if (dcs->d_redeclared_symbol != NULL &&
   2433 	    dcs->d_redeclared_symbol->s_block_level == block_level) {
   2434 		/* redeclaration of formal parameter %s */
   2435 		error(237, sym->s_name);
   2436 		rmsym(dcs->d_redeclared_symbol);
   2437 		sym->s_arg = true;
   2438 	}
   2439 
   2440 	if (!sym->s_arg) {
   2441 		/* declared argument %s is missing */
   2442 		error(53, sym->s_name);
   2443 		sym->s_arg = true;
   2444 	}
   2445 
   2446 	if (initflg) {
   2447 		/* cannot initialize parameter: %s */
   2448 		error(52, sym->s_name);
   2449 	}
   2450 
   2451 	if (sym->s_type == NULL)	/* for c(void()) */
   2452 		sym->s_type = gettyp(VOID);
   2453 
   2454 	if ((t = sym->s_type->t_tspec) == ARRAY) {
   2455 		sym->s_type = derive_type(sym->s_type->t_subt, PTR);
   2456 	} else if (t == FUNC) {
   2457 		if (tflag)
   2458 			/* a function is declared as an argument: %s */
   2459 			warning(50, sym->s_name);
   2460 		sym->s_type = derive_type(sym->s_type, PTR);
   2461 	} else if (t == FLOAT) {
   2462 		if (tflag)
   2463 			sym->s_type = gettyp(DOUBLE);
   2464 	}
   2465 
   2466 	if (dcs->d_inline)
   2467 		/* argument declared inline: %s */
   2468 		warning(269, sym->s_name);
   2469 
   2470 	/*
   2471 	 * Arguments must have complete types. length() prints the needed
   2472 	 * error messages (null dimension is impossible because arrays are
   2473 	 * converted to pointers).
   2474 	 */
   2475 	if (sym->s_type->t_tspec != VOID)
   2476 		(void)length(sym->s_type, sym->s_name);
   2477 
   2478 	sym->s_used = dcs->d_used;
   2479 	mark_as_set(sym);
   2480 
   2481 	return sym;
   2482 }
   2483 
   2484 void
   2485 check_func_lint_directives(void)
   2486 {
   2487 	sym_t *arg;
   2488 	int narg, n;
   2489 	tspec_t t;
   2490 
   2491 	/* check for illegal combinations of lint directives */
   2492 	if (printflike_argnum != -1 && scanflike_argnum != -1) {
   2493 		/* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
   2494 		warning(289);
   2495 		printflike_argnum = scanflike_argnum = -1;
   2496 	}
   2497 	if (nvararg != -1 &&
   2498 	    (printflike_argnum != -1 || scanflike_argnum != -1)) {
   2499 		/* dubious use of ** VARARGS ** with ** %s ** */
   2500 		warning(288,
   2501 		    printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE");
   2502 		nvararg = -1;
   2503 	}
   2504 
   2505 	/*
   2506 	 * check if the argument of a lint directive is compatible with the
   2507 	 * number of arguments.
   2508 	 */
   2509 	narg = 0;
   2510 	for (arg = dcs->d_func_args; arg != NULL; arg = arg->s_next)
   2511 		narg++;
   2512 	if (nargusg > narg) {
   2513 		/* argument number mismatch with directive: ** %s ** */
   2514 		warning(283, "ARGSUSED");
   2515 		nargusg = 0;
   2516 	}
   2517 	if (nvararg > narg) {
   2518 		/* argument number mismatch with directive: ** %s ** */
   2519 		warning(283, "VARARGS");
   2520 		nvararg = 0;
   2521 	}
   2522 	if (printflike_argnum > narg) {
   2523 		/* argument number mismatch with directive: ** %s ** */
   2524 		warning(283, "PRINTFLIKE");
   2525 		printflike_argnum = -1;
   2526 	} else if (printflike_argnum == 0) {
   2527 		printflike_argnum = -1;
   2528 	}
   2529 	if (scanflike_argnum > narg) {
   2530 		/* argument number mismatch with directive: ** %s ** */
   2531 		warning(283, "SCANFLIKE");
   2532 		scanflike_argnum = -1;
   2533 	} else if (scanflike_argnum == 0) {
   2534 		scanflike_argnum = -1;
   2535 	}
   2536 	if (printflike_argnum != -1 || scanflike_argnum != -1) {
   2537 		narg = printflike_argnum != -1
   2538 		    ? printflike_argnum : scanflike_argnum;
   2539 		arg = dcs->d_func_args;
   2540 		for (n = 1; n < narg; n++)
   2541 			arg = arg->s_next;
   2542 		if (arg->s_type->t_tspec != PTR ||
   2543 		    ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
   2544 		     t != UCHAR && t != SCHAR)) {
   2545 			/* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
   2546 			warning(293, narg);
   2547 			printflike_argnum = scanflike_argnum = -1;
   2548 		}
   2549 	}
   2550 }
   2551 
   2552 /*
   2553  * Warn about arguments in old style function definitions that default to int.
   2554  * Check that an old style function definition is compatible to a previous
   2555  * prototype.
   2556  */
   2557 void
   2558 check_func_old_style_arguments(void)
   2559 {
   2560 	sym_t *args, *arg, *pargs, *parg;
   2561 	int narg, nparg;
   2562 	bool msg;
   2563 
   2564 	args = funcsym->s_args;
   2565 	pargs = funcsym->s_type->t_args;
   2566 
   2567 	/*
   2568 	 * print a warning for each argument of an old style function
   2569 	 * definition which defaults to int
   2570 	 */
   2571 	for (arg = args; arg != NULL; arg = arg->s_next) {
   2572 		if (arg->s_defarg) {
   2573 			/* argument type defaults to 'int': %s */
   2574 			warning(32, arg->s_name);
   2575 			arg->s_defarg = false;
   2576 			mark_as_set(arg);
   2577 		}
   2578 	}
   2579 
   2580 	/*
   2581 	 * If this is an old style function definition and a prototype
   2582 	 * exists, compare the types of arguments.
   2583 	 */
   2584 	if (funcsym->s_osdef && funcsym->s_type->t_proto) {
   2585 		/*
   2586 		 * If the number of arguments does not match, we need not
   2587 		 * continue.
   2588 		 */
   2589 		narg = nparg = 0;
   2590 		msg = false;
   2591 		for (parg = pargs; parg != NULL; parg = parg->s_next)
   2592 			nparg++;
   2593 		for (arg = args; arg != NULL; arg = arg->s_next)
   2594 			narg++;
   2595 		if (narg != nparg) {
   2596 			/* parameter mismatch: %d declared, %d defined */
   2597 			error(51, nparg, narg);
   2598 			msg = true;
   2599 		} else {
   2600 			parg = pargs;
   2601 			arg = args;
   2602 			while (narg-- > 0) {
   2603 				msg |= check_prototype_declaration(arg, parg);
   2604 				parg = parg->s_next;
   2605 				arg = arg->s_next;
   2606 			}
   2607 		}
   2608 		if (msg)
   2609 			/* prototype declaration */
   2610 			print_previous_declaration(285,
   2611 			    dcs->d_redeclared_symbol);
   2612 
   2613 		/* from now on the prototype is valid */
   2614 		funcsym->s_osdef = false;
   2615 		funcsym->s_args = NULL;
   2616 	}
   2617 }
   2618 
   2619 /*
   2620  * Checks compatibility of an old style function definition with a previous
   2621  * prototype declaration.
   2622  * Returns true if the position of the previous declaration should be reported.
   2623  */
   2624 static bool
   2625 check_prototype_declaration(sym_t *arg, sym_t *parg)
   2626 {
   2627 	type_t	*tp, *ptp;
   2628 	bool	dowarn, msg;
   2629 
   2630 	tp = arg->s_type;
   2631 	ptp = parg->s_type;
   2632 
   2633 	msg = false;
   2634 	dowarn = false;
   2635 
   2636 	if (!eqtype(tp, ptp, true, true, &dowarn)) {
   2637 		if (eqtype(tp, ptp, true, false, &dowarn)) {
   2638 			/* type does not match prototype: %s */
   2639 			gnuism(58, arg->s_name);
   2640 			msg = sflag || !gflag;
   2641 		} else {
   2642 			/* type does not match prototype: %s */
   2643 			error(58, arg->s_name);
   2644 			msg = true;
   2645 		}
   2646 	} else if (dowarn) {
   2647 		if (sflag)
   2648 			/* type does not match prototype: %s */
   2649 			error(58, arg->s_name);
   2650 		else
   2651 			/* type does not match prototype: %s */
   2652 			warning(58, arg->s_name);
   2653 		msg = true;
   2654 	}
   2655 
   2656 	return msg;
   2657 }
   2658 
   2659 /*
   2660  * Completes a single local declaration/definition.
   2661  */
   2662 void
   2663 declare_local(sym_t *dsym, bool initflg)
   2664 {
   2665 
   2666 	/* Correct a mistake done in declarator_name(). */
   2667 	if (dsym->s_type->t_tspec == FUNC) {
   2668 		dsym->s_def = DECL;
   2669 		if (dcs->d_scl == NOSCL)
   2670 			dsym->s_scl = EXTERN;
   2671 	}
   2672 
   2673 	if (dsym->s_type->t_tspec == FUNC) {
   2674 		if (dsym->s_scl == STATIC) {
   2675 			/* dubious static function at block level: %s */
   2676 			warning(93, dsym->s_name);
   2677 			dsym->s_scl = EXTERN;
   2678 		} else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
   2679 			/* function has illegal storage class: %s */
   2680 			error(94, dsym->s_name);
   2681 			dsym->s_scl = EXTERN;
   2682 		}
   2683 	}
   2684 
   2685 	/*
   2686 	 * functions may be declared inline at local scope, although
   2687 	 * this has no effect for a later definition of the same
   2688 	 * function.
   2689 	 * XXX it should have an effect if tflag is set. this would
   2690 	 * also be the way gcc behaves.
   2691 	 */
   2692 	if (dcs->d_inline) {
   2693 		if (dsym->s_type->t_tspec == FUNC) {
   2694 			dsym->s_inline = true;
   2695 		} else {
   2696 			/* variable declared inline: %s */
   2697 			warning(268, dsym->s_name);
   2698 		}
   2699 	}
   2700 
   2701 	check_function_definition(dsym, true);
   2702 
   2703 	check_type(dsym);
   2704 
   2705 	if (dcs->d_redeclared_symbol != NULL && dsym->s_scl == EXTERN)
   2706 		declare_external_in_block(dsym);
   2707 
   2708 	if (dsym->s_scl == EXTERN) {
   2709 		/*
   2710 		 * XXX if the static variable at level 0 is only defined
   2711 		 * later, checking will be possible.
   2712 		 */
   2713 		if (dsym->s_ext_sym == NULL) {
   2714 			outsym(dsym, EXTERN, dsym->s_def);
   2715 		} else {
   2716 			outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
   2717 		}
   2718 	}
   2719 
   2720 	if (dcs->d_redeclared_symbol != NULL) {
   2721 
   2722 		if (dcs->d_redeclared_symbol->s_block_level == 0) {
   2723 
   2724 			switch (dsym->s_scl) {
   2725 			case AUTO:
   2726 				if (hflag)
   2727 					/* automatic hides external decl.: %s */
   2728 					warning(86, dsym->s_name);
   2729 				break;
   2730 			case STATIC:
   2731 				if (hflag)
   2732 					/* static hides external decl.: %s */
   2733 					warning(87, dsym->s_name);
   2734 				break;
   2735 			case TYPEDEF:
   2736 				if (hflag)
   2737 					/* typedef hides external decl.: %s */
   2738 					warning(88, dsym->s_name);
   2739 				break;
   2740 			case EXTERN:
   2741 				/*
   2742 				 * Warnings and errors are printed in
   2743 				 * declare_external_in_block()
   2744 				 */
   2745 				break;
   2746 			default:
   2747 				lint_assert(/*CONSTCOND*/false);
   2748 			}
   2749 
   2750 		} else if (dcs->d_redeclared_symbol->s_block_level ==
   2751 			   block_level) {
   2752 
   2753 			/* no hflag, because it's illegal! */
   2754 			if (dcs->d_redeclared_symbol->s_arg) {
   2755 				/*
   2756 				 * if !tflag, a "redeclaration of %s" error
   2757 				 * is produced below
   2758 				 */
   2759 				if (tflag) {
   2760 					if (hflag)
   2761 						/* decl. hides parameter: %s */
   2762 						warning(91, dsym->s_name);
   2763 					rmsym(dcs->d_redeclared_symbol);
   2764 				}
   2765 			}
   2766 
   2767 		} else if (dcs->d_redeclared_symbol->s_block_level <
   2768 			   block_level) {
   2769 
   2770 			if (hflag)
   2771 				/* declaration hides earlier one: %s */
   2772 				warning(95, dsym->s_name);
   2773 
   2774 		}
   2775 
   2776 		if (dcs->d_redeclared_symbol->s_block_level == block_level) {
   2777 
   2778 			/* redeclaration of %s */
   2779 			error(27, dsym->s_name);
   2780 			rmsym(dcs->d_redeclared_symbol);
   2781 
   2782 		}
   2783 
   2784 	}
   2785 
   2786 	if (initflg && !check_init(dsym)) {
   2787 		dsym->s_def = DEF;
   2788 		mark_as_set(dsym);
   2789 	}
   2790 
   2791 	if (dsym->s_scl == TYPEDEF) {
   2792 		dsym->s_type = dup_type(dsym->s_type);
   2793 		dsym->s_type->t_typedef = true;
   2794 		settdsym(dsym->s_type, dsym);
   2795 	}
   2796 
   2797 	/*
   2798 	 * Before we can check the size we must wait for a initialization
   2799 	 * which may follow.
   2800 	 */
   2801 }
   2802 
   2803 /*
   2804  * Processes (re)declarations of external symbols inside blocks.
   2805  */
   2806 static void
   2807 declare_external_in_block(sym_t *dsym)
   2808 {
   2809 	bool	eqt, dowarn;
   2810 	sym_t	*esym;
   2811 
   2812 	/* look for a symbol with the same name */
   2813 	esym = dcs->d_redeclared_symbol;
   2814 	while (esym != NULL && esym->s_block_level != 0) {
   2815 		while ((esym = esym->s_link) != NULL) {
   2816 			if (esym->s_kind != FVFT)
   2817 				continue;
   2818 			if (strcmp(dsym->s_name, esym->s_name) == 0)
   2819 				break;
   2820 		}
   2821 	}
   2822 	if (esym == NULL)
   2823 		return;
   2824 	if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
   2825 		/* gcc accepts this without a warning, pcc prints an error. */
   2826 		/* redeclaration of %s */
   2827 		warning(27, dsym->s_name);
   2828 		print_previous_declaration(-1, esym);
   2829 		return;
   2830 	}
   2831 
   2832 	dowarn = false;
   2833 	eqt = eqtype(esym->s_type, dsym->s_type, false, false, &dowarn);
   2834 
   2835 	if (!eqt || dowarn) {
   2836 		if (esym->s_scl == EXTERN) {
   2837 			/* inconsistent redeclaration of extern: %s */
   2838 			warning(90, dsym->s_name);
   2839 			print_previous_declaration(-1, esym);
   2840 		} else {
   2841 			/* inconsistent redeclaration of static: %s */
   2842 			warning(92, dsym->s_name);
   2843 			print_previous_declaration(-1, esym);
   2844 		}
   2845 	}
   2846 
   2847 	if (eqt) {
   2848 		/*
   2849 		 * Remember the external symbol so we can update usage
   2850 		 * information at the end of the block.
   2851 		 */
   2852 		dsym->s_ext_sym = esym;
   2853 	}
   2854 }
   2855 
   2856 /*
   2857  * Print an error or a warning if the symbol cannot be initialized due
   2858  * to type/storage class. Return whether an error has been detected.
   2859  */
   2860 static bool
   2861 check_init(sym_t *sym)
   2862 {
   2863 	bool	erred;
   2864 
   2865 	erred = false;
   2866 
   2867 	if (sym->s_type->t_tspec == FUNC) {
   2868 		/* cannot initialize function: %s */
   2869 		error(24, sym->s_name);
   2870 		erred = true;
   2871 	} else if (sym->s_scl == TYPEDEF) {
   2872 		/* cannot initialize typedef: %s */
   2873 		error(25, sym->s_name);
   2874 		erred = true;
   2875 	} else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
   2876 		/* cannot initialize "extern" declaration: %s */
   2877 		if (dcs->d_ctx == EXTERN) {
   2878 			/* cannot initialize extern declaration: %s */
   2879 			warning(26, sym->s_name);
   2880 		} else {
   2881 			/* cannot initialize extern declaration: %s */
   2882 			error(26, sym->s_name);
   2883 			erred = true;
   2884 		}
   2885 	}
   2886 
   2887 	return erred;
   2888 }
   2889 
   2890 /*
   2891  * Create a symbol for an abstract declaration.
   2892  */
   2893 sym_t *
   2894 abstract_name(void)
   2895 {
   2896 	sym_t	*sym;
   2897 
   2898 	lint_assert(dcs->d_ctx == ABSTRACT || dcs->d_ctx == PROTO_ARG);
   2899 
   2900 	sym = getblk(sizeof(*sym));
   2901 
   2902 	sym->s_name = unnamed;
   2903 	sym->s_def = DEF;
   2904 	sym->s_scl = ABSTRACT;
   2905 	sym->s_block_level = -1;
   2906 
   2907 	if (dcs->d_ctx == PROTO_ARG)
   2908 		sym->s_arg = true;
   2909 
   2910 	sym->s_type = dcs->d_type;
   2911 	dcs->d_redeclared_symbol = NULL;
   2912 	dcs->d_vararg = false;
   2913 
   2914 	return sym;
   2915 }
   2916 
   2917 /*
   2918  * Removes anything which has nothing to do on global level.
   2919  */
   2920 void
   2921 global_clean_up(void)
   2922 {
   2923 
   2924 	while (dcs->d_next != NULL)
   2925 		end_declaration_level();
   2926 
   2927 	cleanup();
   2928 	block_level = 0;
   2929 	mem_block_level = 0;
   2930 
   2931 	/*
   2932 	 * remove all information about pending lint directives without
   2933 	 * warnings.
   2934 	 */
   2935 	global_clean_up_decl(true);
   2936 }
   2937 
   2938 /*
   2939  * Process an abstract type declaration
   2940  */
   2941 sym_t *
   2942 declare_1_abstract(sym_t *sym)
   2943 {
   2944 
   2945 	check_function_definition(sym, true);
   2946 	check_type(sym);
   2947 	return sym;
   2948 }
   2949 
   2950 /*
   2951  * Checks size after declarations of variables and their initialization.
   2952  */
   2953 void
   2954 check_size(sym_t *dsym)
   2955 {
   2956 
   2957 	if (dsym->s_def != DEF)
   2958 		return;
   2959 	if (dsym->s_scl == TYPEDEF)
   2960 		return;
   2961 	if (dsym->s_type->t_tspec == FUNC)
   2962 		return;
   2963 
   2964 	if (length(dsym->s_type, dsym->s_name) == 0 &&
   2965 	    dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
   2966 		if (tflag) {
   2967 			/* empty array declaration: %s */
   2968 			warning(190, dsym->s_name);
   2969 		} else {
   2970 			/* empty array declaration: %s */
   2971 			error(190, dsym->s_name);
   2972 		}
   2973 	}
   2974 }
   2975 
   2976 /*
   2977  * Mark an object as set if it is not already
   2978  */
   2979 void
   2980 mark_as_set(sym_t *sym)
   2981 {
   2982 
   2983 	if (!sym->s_set) {
   2984 		sym->s_set = true;
   2985 		UNIQUE_CURR_POS(sym->s_set_pos);
   2986 	}
   2987 }
   2988 
   2989 /*
   2990  * Mark an object as used if it is not already
   2991  */
   2992 void
   2993 mark_as_used(sym_t *sym, bool fcall, bool szof)
   2994 {
   2995 
   2996 	if (!sym->s_used) {
   2997 		sym->s_used = true;
   2998 		UNIQUE_CURR_POS(sym->s_use_pos);
   2999 	}
   3000 	/*
   3001 	 * for function calls another record is written
   3002 	 *
   3003 	 * XXX Should symbols used in sizeof() be treated as used or not?
   3004 	 * Probably not, because there is no sense to declare an
   3005 	 * external variable only to get their size.
   3006 	 */
   3007 	if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
   3008 		outusg(sym);
   3009 }
   3010 
   3011 /*
   3012  * Prints warnings for a list of variables and labels (concatenated
   3013  * with s_dlnxt) if these are not used or only set.
   3014  */
   3015 void
   3016 check_usage(dinfo_t *di)
   3017 {
   3018 	sym_t	*sym;
   3019 	int	mklwarn;
   3020 
   3021 	/* for this warning LINTED has no effect */
   3022 	mklwarn = lwarn;
   3023 	lwarn = LWARN_ALL;
   3024 
   3025 	debug_step("begin lwarn %d", lwarn);
   3026 	for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
   3027 		check_usage_sym(di->d_asm, sym);
   3028 	lwarn = mklwarn;
   3029 	debug_step("end lwarn %d", lwarn);
   3030 }
   3031 
   3032 /*
   3033  * Prints a warning for a single variable or label if it is not used or
   3034  * only set.
   3035  */
   3036 void
   3037 check_usage_sym(bool novar, sym_t *sym)
   3038 {
   3039 
   3040 	if (sym->s_block_level == -1)
   3041 		return;
   3042 
   3043 	if (sym->s_kind == FVFT && sym->s_arg)
   3044 		check_argument_usage(novar, sym);
   3045 	else if (sym->s_kind == FVFT)
   3046 		check_variable_usage(novar, sym);
   3047 	else if (sym->s_kind == FLABEL)
   3048 		check_label_usage(sym);
   3049 	else if (sym->s_kind == FTAG)
   3050 		check_tag_usage(sym);
   3051 }
   3052 
   3053 static void
   3054 check_argument_usage(bool novar, sym_t *arg)
   3055 {
   3056 
   3057 	lint_assert(arg->s_set);
   3058 
   3059 	if (novar)
   3060 		return;
   3061 
   3062 	if (!arg->s_used && vflag) {
   3063 		/* argument '%s' unused in function '%s' */
   3064 		warning_at(231, &arg->s_def_pos, arg->s_name, funcsym->s_name);
   3065 	}
   3066 }
   3067 
   3068 static void
   3069 check_variable_usage(bool novar, sym_t *sym)
   3070 {
   3071 	scl_t	sc;
   3072 	sym_t	*xsym;
   3073 
   3074 	lint_assert(block_level != 0);
   3075 
   3076 	/* example at file scope: int c = ({ return 3; }); */
   3077 	if (sym->s_block_level == 0 && ch_isdigit(sym->s_name[0]))
   3078 		return;
   3079 
   3080 	/* errors in expressions easily cause lots of these warnings */
   3081 	if (nerr != 0)
   3082 		return;
   3083 
   3084 	/*
   3085 	 * XXX Only variables are checked, although types should
   3086 	 * probably also be checked
   3087 	 */
   3088 	if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
   3089 	    sc != AUTO && sc != REG) {
   3090 		return;
   3091 	}
   3092 
   3093 	if (novar)
   3094 		return;
   3095 
   3096 	if (sc == EXTERN) {
   3097 		if (!sym->s_used && !sym->s_set) {
   3098 			/* '%s' unused in function '%s' */
   3099 			warning_at(192, &sym->s_def_pos,
   3100 			    sym->s_name, funcsym->s_name);
   3101 		}
   3102 	} else {
   3103 		if (sym->s_set && !sym->s_used) {
   3104 			/* '%s' set but not used in function '%s' */
   3105 			warning_at(191, &sym->s_set_pos,
   3106 			    sym->s_name, funcsym->s_name);
   3107 		} else if (!sym->s_used) {
   3108 			/* '%s' unused in function '%s' */
   3109 			warning_at(192, &sym->s_def_pos,
   3110 			    sym->s_name, funcsym->s_name);
   3111 		}
   3112 	}
   3113 
   3114 	if (sc == EXTERN) {
   3115 		/*
   3116 		 * information about usage is taken over into the symbol
   3117 		 * table entry at level 0 if the symbol was locally declared
   3118 		 * as an external symbol.
   3119 		 *
   3120 		 * XXX This is wrong for symbols declared static at level 0
   3121 		 * if the usage information stems from sizeof(). This is
   3122 		 * because symbols at level 0 only used in sizeof() are
   3123 		 * considered to not be used.
   3124 		 */
   3125 		if ((xsym = sym->s_ext_sym) != NULL) {
   3126 			if (sym->s_used && !xsym->s_used) {
   3127 				xsym->s_used = true;
   3128 				xsym->s_use_pos = sym->s_use_pos;
   3129 			}
   3130 			if (sym->s_set && !xsym->s_set) {
   3131 				xsym->s_set = true;
   3132 				xsym->s_set_pos = sym->s_set_pos;
   3133 			}
   3134 		}
   3135 	}
   3136 }
   3137 
   3138 static void
   3139 check_label_usage(sym_t *lab)
   3140 {
   3141 
   3142 	lint_assert(block_level == 1);
   3143 	lint_assert(lab->s_block_level == 1);
   3144 
   3145 	if (lab->s_set && !lab->s_used) {
   3146 		/* label '%s' unused in function '%s' */
   3147 		warning_at(232, &lab->s_set_pos, lab->s_name, funcsym->s_name);
   3148 	} else if (!lab->s_set) {
   3149 		/* undefined label '%s' */
   3150 		warning_at(23, &lab->s_use_pos, lab->s_name);
   3151 	}
   3152 }
   3153 
   3154 static void
   3155 check_tag_usage(sym_t *sym)
   3156 {
   3157 
   3158 	if (!is_incomplete(sym->s_type))
   3159 		return;
   3160 
   3161 	/* always complain about incomplete tags declared inside blocks */
   3162 	if (!zflag || dcs->d_ctx != EXTERN)
   3163 		return;
   3164 
   3165 	switch (sym->s_type->t_tspec) {
   3166 	case STRUCT:
   3167 		/* struct %s never defined */
   3168 		warning_at(233, &sym->s_def_pos, sym->s_name);
   3169 		break;
   3170 	case UNION:
   3171 		/* union %s never defined */
   3172 		warning_at(234, &sym->s_def_pos, sym->s_name);
   3173 		break;
   3174 	case ENUM:
   3175 		/* enum %s never defined */
   3176 		warning_at(235, &sym->s_def_pos, sym->s_name);
   3177 		break;
   3178 	default:
   3179 		lint_assert(/*CONSTCOND*/false);
   3180 	}
   3181 }
   3182 
   3183 /*
   3184  * Called after the entire translation unit has been parsed.
   3185  * Changes tentative definitions into definitions.
   3186  * Performs some tests on global symbols. Detected problems are:
   3187  * - defined variables of incomplete type
   3188  * - constant variables which are not initialized
   3189  * - static symbols which are never used
   3190  */
   3191 void
   3192 check_global_symbols(void)
   3193 {
   3194 	sym_t	*sym;
   3195 
   3196 	if (block_level != 0 || dcs->d_next != NULL)
   3197 		norecover();
   3198 
   3199 	for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
   3200 		if (sym->s_block_level == -1)
   3201 			continue;
   3202 		if (sym->s_kind == FVFT) {
   3203 			check_global_variable(sym);
   3204 		} else if (sym->s_kind == FTAG) {
   3205 			check_tag_usage(sym);
   3206 		} else {
   3207 			lint_assert(sym->s_kind == FMEMBER);
   3208 		}
   3209 	}
   3210 }
   3211 
   3212 static void
   3213 check_unused_static_global_variable(const sym_t *sym)
   3214 {
   3215 	if (sym->s_type->t_tspec == FUNC) {
   3216 		if (sym->s_def == DEF) {
   3217 			if (!sym->s_inline)
   3218 				/* static function %s unused */
   3219 				warning_at(236, &sym->s_def_pos, sym->s_name);
   3220 		} else {
   3221 			/* static function %s declared but not defined */
   3222 			warning_at(290, &sym->s_def_pos, sym->s_name);
   3223 		}
   3224 	} else if (!sym->s_set) {
   3225 		/* static variable %s unused */
   3226 		warning_at(226, &sym->s_def_pos, sym->s_name);
   3227 	} else {
   3228 		/* static variable %s set but not used */
   3229 		warning_at(307, &sym->s_def_pos, sym->s_name);
   3230 	}
   3231 }
   3232 
   3233 static void
   3234 check_static_global_variable(const sym_t *sym)
   3235 {
   3236 	if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) {
   3237 		/* static function called but not defined: %s() */
   3238 		error_at(225, &sym->s_use_pos, sym->s_name);
   3239 	}
   3240 
   3241 	if (!sym->s_used)
   3242 		check_unused_static_global_variable(sym);
   3243 
   3244 	if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
   3245 		/* const object %s should have initializer */
   3246 		warning_at(227, &sym->s_def_pos, sym->s_name);
   3247 	}
   3248 }
   3249 
   3250 static void
   3251 check_global_variable(const sym_t *sym)
   3252 {
   3253 
   3254 	if (sym->s_scl == TYPEDEF || sym->s_scl == CTCONST)
   3255 		return;
   3256 
   3257 	if (sym->s_scl == NOSCL)
   3258 		return;		/* May be caused by a syntax error. */
   3259 
   3260 	lint_assert(sym->s_scl == EXTERN || sym->s_scl == STATIC);
   3261 
   3262 	check_global_variable_size(sym);
   3263 
   3264 	if (sym->s_scl == STATIC)
   3265 		check_static_global_variable(sym);
   3266 }
   3267 
   3268 static void
   3269 check_global_variable_size(const sym_t *sym)
   3270 {
   3271 	pos_t cpos;
   3272 	int length_in_bits;
   3273 
   3274 	if (sym->s_def != TDEF)
   3275 		return;
   3276 	if (sym->s_type->t_tspec == FUNC)
   3277 		/*
   3278 		 * this can happen if a syntax error occurred after a
   3279 		 * function declaration
   3280 		 */
   3281 		return;
   3282 	if (sym->s_def == TDEF && sym->s_type->t_tspec == VOID)
   3283 		return;		/* prevent internal error in length() below */
   3284 
   3285 	cpos = curr_pos;
   3286 	curr_pos = sym->s_def_pos;
   3287 	length_in_bits = length(sym->s_type, sym->s_name);
   3288 	curr_pos = cpos;
   3289 
   3290 	if (length_in_bits == 0 &&
   3291 	    sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
   3292 		if (tflag || (sym->s_scl == EXTERN && !sflag)) {
   3293 			/* empty array declaration: %s */
   3294 			warning_at(190, &sym->s_def_pos, sym->s_name);
   3295 		} else {
   3296 			/* empty array declaration: %s */
   3297 			error_at(190, &sym->s_def_pos, sym->s_name);
   3298 		}
   3299 	}
   3300 }
   3301 
   3302 /*
   3303  * Prints information about location of previous definition/declaration.
   3304  */
   3305 void
   3306 print_previous_declaration(int msg, const sym_t *psym)
   3307 {
   3308 
   3309 	if (!rflag)
   3310 		return;
   3311 
   3312 	if (msg != -1) {
   3313 		(message_at)(msg, &psym->s_def_pos);
   3314 	} else if (psym->s_def == DEF || psym->s_def == TDEF) {
   3315 		/* previous definition of %s */
   3316 		message_at(261, &psym->s_def_pos, psym->s_name);
   3317 	} else {
   3318 		/* previous declaration of %s */
   3319 		message_at(260, &psym->s_def_pos, psym->s_name);
   3320 	}
   3321 }
   3322 
   3323 /*
   3324  * Gets a node for a constant and returns the value of this constant
   3325  * as integer.
   3326  *
   3327  * If the node is not constant or too large for int or of type float,
   3328  * a warning will be printed.
   3329  *
   3330  * to_int_constant() should be used only inside declarations. If it is used in
   3331  * expressions, it frees the memory used for the expression.
   3332  */
   3333 int
   3334 to_int_constant(tnode_t *tn, bool required)
   3335 {
   3336 	int	i;
   3337 	tspec_t	t;
   3338 	val_t	*v;
   3339 
   3340 	v = constant(tn, required);
   3341 
   3342 	if (tn == NULL) {
   3343 		i = 1;
   3344 		goto done;
   3345 	}
   3346 
   3347 	/*
   3348 	 * Abstract declarations are used inside expression. To free
   3349 	 * the memory would be a fatal error.
   3350 	 * We don't free blocks that are inside casts because these
   3351 	 * will be used later to match types.
   3352 	 */
   3353 	if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT)
   3354 		expr_free_all();
   3355 
   3356 	if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) {
   3357 		i = (int)v->v_ldbl;
   3358 		/* integral constant expression expected */
   3359 		error(55);
   3360 	} else {
   3361 		i = (int)v->v_quad;
   3362 		if (is_uinteger(t)) {
   3363 			if ((uint64_t)v->v_quad > (uint64_t)TARG_INT_MAX) {
   3364 				/* integral constant too large */
   3365 				warning(56);
   3366 			}
   3367 		} else {
   3368 			if (v->v_quad > (int64_t)TARG_INT_MAX ||
   3369 			    v->v_quad < (int64_t)TARG_INT_MIN) {
   3370 				/* integral constant too large */
   3371 				warning(56);
   3372 			}
   3373 		}
   3374 	}
   3375 
   3376 done:
   3377 	free(v);
   3378 	return i;
   3379 }
   3380