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