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