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