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