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