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