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