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