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