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