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