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