xref: /netbsd-src/usr.bin/xlint/lint1/decl.c (revision a24efa7dea9f1f56c3bdb15a927d3516792ace1c)
1 /* $NetBSD: decl.c,v 1.61 2016/01/02 17:44:21 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.61 2016/01/02 17:44:21 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(%d)", elsz);
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 				/* bind anonymous members to the structure */
1782 				if (mem->s_styp == NULL)
1783 					mem->s_styp = sp;
1784 				if (mem->s_name != unnamed)
1785 					n++;
1786 			}
1787 			if (n == 0) {
1788 				/* %s has no named members */
1789 				warning(65,
1790 					t == STRUCT ? "structure" : "union");
1791 			}
1792 		}
1793 	} else {
1794 		tp->t_enum->elem = fmem;
1795 	}
1796 	return (tp);
1797 }
1798 
1799 /*
1800  * Processes the name of an enumerator in en enum declaration.
1801  *
1802  * sym points to the enumerator
1803  * val is the value of the enumerator
1804  * impl is 1 if the value of the enumerator was not explicit specified.
1805  */
1806 sym_t *
1807 ename(sym_t *sym, int val, int impl)
1808 {
1809 
1810 	if (sym->s_scl) {
1811 		if (sym->s_blklev == blklev) {
1812 			/* no hflag, because this is illegal!!! */
1813 			if (sym->s_arg) {
1814 				/* enumeration constant hides parameter: %s */
1815 				warning(57, sym->s_name);
1816 			} else {
1817 				/* redeclaration of %s */
1818 				error(27, sym->s_name);
1819 				/*
1820 				 * inside blocks it should not too complicated
1821 				 * to find the position of the previous
1822 				 * declaration
1823 				 */
1824 				if (blklev == 0)
1825 					prevdecl(-1, sym);
1826 			}
1827 		} else {
1828 			if (hflag)
1829 				/* redefinition hides earlier one: %s */
1830 				warning(43, sym->s_name);
1831 		}
1832 		sym = pushdown(sym);
1833 	}
1834 	sym->s_scl = ENUMCON;
1835 	sym->s_type = dcs->d_tagtyp;
1836 	sym->s_value.v_tspec = INT;
1837 	sym->s_value.v_quad = val;
1838 	if (impl && val - 1 == TARG_INT_MAX) {
1839 		/* overflow in enumeration values: %s */
1840 		warning(48, sym->s_name);
1841 	}
1842 	enumval = val + 1;
1843 	return (sym);
1844 }
1845 
1846 /*
1847  * Process a single external declarator.
1848  */
1849 void
1850 decl1ext(sym_t *dsym, int initflg)
1851 {
1852 	int	dowarn, rval, redec;
1853 	sym_t	*rdsym;
1854 
1855 	chkfdef(dsym, 1);
1856 
1857 	chktyp(dsym);
1858 
1859 	if (initflg && !(initerr = chkinit(dsym)))
1860 		dsym->s_def = DEF;
1861 
1862 	/*
1863 	 * Declarations of functions are marked as "tentative" in dname().
1864 	 * This is wrong because there are no tentative function
1865 	 * definitions.
1866 	 */
1867 	if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1868 		dsym->s_def = DECL;
1869 
1870 	if (dcs->d_inline) {
1871 		if (dsym->s_type->t_tspec == FUNC) {
1872 			dsym->s_inline = 1;
1873 		} else {
1874 			/* variable declared inline: %s */
1875 			warning(268, dsym->s_name);
1876 		}
1877 	}
1878 
1879 	/* Write the declaration into the output file */
1880 	if (plibflg && llibflg &&
1881 	    dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1882 		/*
1883 		 * With both LINTLIBRARY and PROTOLIB the prototyp is
1884 		 * written as a function definition to the output file.
1885 		 */
1886 		rval = dsym->s_type->t_subt->t_tspec != VOID;
1887 		outfdef(dsym, &dsym->s_dpos, rval, 0, NULL);
1888 	} else {
1889 		outsym(dsym, dsym->s_scl, dsym->s_def);
1890 	}
1891 
1892 	if ((rdsym = dcs->d_rdcsym) != NULL) {
1893 
1894 		/*
1895 		 * If the old symbol stems from a old style function definition
1896 		 * we have remembered the params in rdsmy->s_args and compare
1897 		 * them with the params of the prototype.
1898 		 */
1899 		if (rdsym->s_osdef && dsym->s_type->t_proto) {
1900 			redec = chkosdef(rdsym, dsym);
1901 		} else {
1902 			redec = 0;
1903 		}
1904 
1905 		if (!redec && !isredec(dsym, (dowarn = 0, &dowarn))) {
1906 
1907 			if (dowarn) {
1908 				/* redeclaration of %s */
1909 				(*(sflag ? error : warning))(27, dsym->s_name);
1910 				prevdecl(-1, rdsym);
1911 			}
1912 
1913 			/*
1914 			 * Overtake the rememberd params if the new symbol
1915 			 * is not a prototype.
1916 			 */
1917 			if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1918 				dsym->s_osdef = rdsym->s_osdef;
1919 				dsym->s_args = rdsym->s_args;
1920 				STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1921 			}
1922 
1923 			/*
1924 			 * Remember the position of the declaration if the
1925 			 * old symbol was a prototype and the new is not.
1926 			 * Also remember the position if the old symbol
1927 			 * was defined and the new is not.
1928 			 */
1929 			if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1930 				STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1931 			} else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1932 				STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1933 			}
1934 
1935 			/*
1936 			 * Copy informations about usage of the name into
1937 			 * the new symbol.
1938 			 */
1939 			cpuinfo(dsym, rdsym);
1940 
1941 			/* Once a name is defined, it remains defined. */
1942 			if (rdsym->s_def == DEF)
1943 				dsym->s_def = DEF;
1944 
1945 			/* once a function is inline, it remains inline */
1946 			if (rdsym->s_inline)
1947 				dsym->s_inline = 1;
1948 
1949 			compltyp(dsym, rdsym);
1950 
1951 		}
1952 
1953 		rmsym(rdsym);
1954 	}
1955 
1956 	if (dsym->s_scl == TYPEDEF) {
1957 		dsym->s_type = duptyp(dsym->s_type);
1958 		dsym->s_type->t_typedef = 1;
1959 		settdsym(dsym->s_type, dsym);
1960 	}
1961 
1962 }
1963 
1964 /*
1965  * Copies informations about usage into a new symbol table entry of
1966  * the same symbol.
1967  */
1968 void
1969 cpuinfo(sym_t *sym, sym_t *rdsym)
1970 {
1971 
1972 	sym->s_spos = rdsym->s_spos;
1973 	sym->s_upos = rdsym->s_upos;
1974 	sym->s_set = rdsym->s_set;
1975 	sym->s_used = rdsym->s_used;
1976 }
1977 
1978 /*
1979  * Prints an error and returns 1 if a symbol is redeclared/redefined.
1980  * Otherwise returns 0 and, in some cases of minor problems, prints
1981  * a warning.
1982  */
1983 int
1984 isredec(sym_t *dsym, int *dowarn)
1985 {
1986 	sym_t	*rsym;
1987 
1988 	if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
1989 		/* redeclaration of %s */
1990 		error(27, dsym->s_name);
1991 		prevdecl(-1, rsym);
1992 		return (1);
1993 	}
1994 	if (rsym->s_scl == TYPEDEF) {
1995 		/* typedef redeclared: %s */
1996 		error(89, dsym->s_name);
1997 		prevdecl(-1, rsym);
1998 		return (1);
1999 	}
2000 	if (dsym->s_scl == TYPEDEF) {
2001 		/* redeclaration of %s */
2002 		error(27, dsym->s_name);
2003 		prevdecl(-1, rsym);
2004 		return (1);
2005 	}
2006 	if (rsym->s_def == DEF && dsym->s_def == DEF) {
2007 		/* redefinition of %s */
2008 		error(28, dsym->s_name);
2009 		prevdecl(-1, rsym);
2010 		return(1);
2011 	}
2012 	if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, dowarn)) {
2013 		/* redeclaration of %s */
2014 		error(27, dsym->s_name);
2015 		prevdecl(-1, rsym);
2016 		return(1);
2017 	}
2018 	if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
2019 		return(0);
2020 	if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
2021 		return(0);
2022 	if (rsym->s_scl == STATIC && dsym->s_def == DECL)
2023 		return(0);
2024 	if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
2025 		/*
2026 		 * All cases except "int a = 1; static int a;" are catched
2027 		 * above with or without a warning
2028 		 */
2029 		/* redeclaration of %s */
2030 		error(27, dsym->s_name);
2031 		prevdecl(-1, rsym);
2032 		return(1);
2033 	}
2034 	if (rsym->s_scl == EXTERN) {
2035 		/* previously declared extern, becomes static: %s */
2036 		warning(29, dsym->s_name);
2037 		prevdecl(-1, rsym);
2038 		return(0);
2039 	}
2040 	/*
2041 	 * Now its on of:
2042 	 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2043 	 */
2044 	/* redeclaration of %s; ANSI C requires "static" */
2045 	if (sflag) {
2046 		warning(30, dsym->s_name);
2047 		prevdecl(-1, rsym);
2048 	}
2049 	dsym->s_scl = STATIC;
2050 	return (0);
2051 }
2052 
2053 static int
2054 chkqual(type_t *tp1, type_t *tp2, int ignqual)
2055 {
2056 	if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
2057 		return 0;
2058 
2059 	if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
2060 		return 0;
2061 
2062 	return 1;
2063 }
2064 
2065 int
2066 eqptrtype(type_t *tp1, type_t *tp2, int ignqual)
2067 {
2068 	if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
2069 		return 0;
2070 
2071 	if (!chkqual(tp1, tp2, ignqual))
2072 		return 0;
2073 
2074 	return 1;
2075 }
2076 
2077 
2078 /*
2079  * Checks if two types are compatible. Returns 0 if not, otherwise 1.
2080  *
2081  * ignqual	ignore qualifiers of type; used for function params
2082  * promot	promote left type; used for comparison of params of
2083  *		old style function definitions with params of prototypes.
2084  * *dowarn	set to 1 if an old style function declaration is not
2085  *		compatible with a prototype
2086  */
2087 int
2088 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *dowarn)
2089 {
2090 	tspec_t	t;
2091 
2092 	while (tp1 != NULL && tp2 != NULL) {
2093 
2094 		t = tp1->t_tspec;
2095 		if (promot) {
2096 			if (t == FLOAT) {
2097 				t = DOUBLE;
2098 			} else if (t == CHAR || t == SCHAR) {
2099 				t = INT;
2100 			} else if (t == UCHAR) {
2101 				t = tflag ? UINT : INT;
2102 			} else if (t == SHORT) {
2103 				t = INT;
2104 			} else if (t == USHORT) {
2105 				/* CONSTCOND */
2106 				t = TARG_INT_MAX < TARG_USHRT_MAX || tflag ? UINT : INT;
2107 			}
2108 		}
2109 
2110 		if (t != tp2->t_tspec)
2111 			return (0);
2112 
2113 		if (!chkqual(tp1, tp2, ignqual))
2114 			return 0;
2115 
2116 		if (t == STRUCT || t == UNION)
2117 			return (tp1->t_str == tp2->t_str);
2118 
2119 		if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2120 			if (tp1->t_dim != 0 && tp2->t_dim != 0)
2121 				return (0);
2122 		}
2123 
2124 		/* dont check prototypes for traditional */
2125 		if (t == FUNC && !tflag) {
2126 			if (tp1->t_proto && tp2->t_proto) {
2127 				if (!eqargs(tp1, tp2, dowarn))
2128 					return (0);
2129 			} else if (tp1->t_proto) {
2130 				if (!mnoarg(tp1, dowarn))
2131 					return (0);
2132 			} else if (tp2->t_proto) {
2133 				if (!mnoarg(tp2, dowarn))
2134 					return (0);
2135 			}
2136 		}
2137 
2138 		tp1 = tp1->t_subt;
2139 		tp2 = tp2->t_subt;
2140 		ignqual = promot = 0;
2141 
2142 	}
2143 
2144 	return (tp1 == tp2);
2145 }
2146 
2147 /*
2148  * Compares the parameter types of two prototypes.
2149  */
2150 static int
2151 eqargs(type_t *tp1, type_t *tp2, int *dowarn)
2152 {
2153 	sym_t	*a1, *a2;
2154 
2155 	if (tp1->t_vararg != tp2->t_vararg)
2156 		return (0);
2157 
2158 	a1 = tp1->t_args;
2159 	a2 = tp2->t_args;
2160 
2161 	while (a1 != NULL && a2 != NULL) {
2162 
2163 		if (eqtype(a1->s_type, a2->s_type, 1, 0, dowarn) == 0)
2164 			return (0);
2165 
2166 		a1 = a1->s_nxt;
2167 		a2 = a2->s_nxt;
2168 
2169 	}
2170 
2171 	return (a1 == a2);
2172 }
2173 
2174 /*
2175  * mnoarg() (matches functions with no argument type information)
2176  * returns 1 if all parameters of a prototype are compatible with
2177  * and old style function declaration.
2178  * This is the case if following conditions are met:
2179  *	1. the prototype must have a fixed number of parameters
2180  *	2. no parameter is of type float
2181  *	3. no parameter is converted to another type if integer promotion
2182  *	   is applied on it
2183  */
2184 static int
2185 mnoarg(type_t *tp, int *dowarn)
2186 {
2187 	sym_t	*arg;
2188 	tspec_t	t;
2189 
2190 	if (tp->t_vararg) {
2191 		if (dowarn != NULL)
2192 			*dowarn = 1;
2193 	}
2194 	for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) {
2195 		if ((t = arg->s_type->t_tspec) == FLOAT ||
2196 		    t == CHAR || t == SCHAR || t == UCHAR ||
2197 		    t == SHORT || t == USHORT) {
2198 			if (dowarn != NULL)
2199 				*dowarn = 1;
2200 		}
2201 	}
2202 	return (1);
2203 }
2204 
2205 /*
2206  * Compares a prototype declaration with the remembered arguments of
2207  * a previous old style function definition.
2208  */
2209 static int
2210 chkosdef(sym_t *rdsym, sym_t *dsym)
2211 {
2212 	sym_t	*args, *pargs, *arg, *parg;
2213 	int	narg, nparg, n;
2214 	int	dowarn, msg;
2215 
2216 	args = rdsym->s_args;
2217 	pargs = dsym->s_type->t_args;
2218 
2219 	msg = 0;
2220 
2221 	narg = nparg = 0;
2222 	for (arg = args; arg != NULL; arg = arg->s_nxt)
2223 		narg++;
2224 	for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2225 		nparg++;
2226 	if (narg != nparg) {
2227 		/* prototype does not match old-style definition */
2228 		error(63);
2229 		msg = 1;
2230 		goto end;
2231 	}
2232 
2233 	arg = args;
2234 	parg = pargs;
2235 	n = 1;
2236 	while (narg--) {
2237 		dowarn = 0;
2238 		/*
2239 		 * If it does not match due to promotion and sflag is
2240 		 * not set we print only a warning.
2241 		 */
2242 		if (!eqtype(arg->s_type, parg->s_type, 1, 1, &dowarn) || dowarn) {
2243 			/* prototype does not match old-style def., arg #%d */
2244 			error(299, n);
2245 			msg = 1;
2246 		}
2247 		arg = arg->s_nxt;
2248 		parg = parg->s_nxt;
2249 		n++;
2250 	}
2251 
2252  end:
2253 	if (msg)
2254 		/* old style definition */
2255 		prevdecl(300, rdsym);
2256 
2257 	return (msg);
2258 }
2259 
2260 /*
2261  * Complets a type by copying the dimension and prototype information
2262  * from a second compatible type.
2263  *
2264  * Following lines are legal:
2265  *  "typedef a[]; a b; a b[10]; a c; a c[20];"
2266  *  "typedef ft(); ft f; f(int); ft g; g(long);"
2267  * This means that, if a type is completed, the type structure must
2268  * be duplicated.
2269  */
2270 void
2271 compltyp(sym_t *dsym, sym_t *ssym)
2272 {
2273 	type_t	**dstp, *src;
2274 	type_t	*dst;
2275 
2276 	dstp = &dsym->s_type;
2277 	src = ssym->s_type;
2278 
2279 	while ((dst = *dstp) != NULL) {
2280 		if (src == NULL || dst->t_tspec != src->t_tspec)
2281 			LERROR("compltyp()");
2282 		if (dst->t_tspec == ARRAY) {
2283 			if (dst->t_dim == 0 && src->t_dim != 0) {
2284 				*dstp = dst = duptyp(dst);
2285 				dst->t_dim = src->t_dim;
2286 				/* now a complete Typ */
2287 				setcompl(dst, 0);
2288 			}
2289 		} else if (dst->t_tspec == FUNC) {
2290 			if (!dst->t_proto && src->t_proto) {
2291 				*dstp = dst = duptyp(dst);
2292 				dst->t_proto = 1;
2293 				dst->t_args = src->t_args;
2294 			}
2295 		}
2296 		dstp = &dst->t_subt;
2297 		src = src->t_subt;
2298 	}
2299 }
2300 
2301 /*
2302  * Completes the declaration of a single argument.
2303  */
2304 sym_t *
2305 decl1arg(sym_t *sym, int initflg)
2306 {
2307 	tspec_t	t;
2308 
2309 	chkfdef(sym, 1);
2310 
2311 	chktyp(sym);
2312 
2313 	if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) {
2314 		/* redeclaration of formal parameter %s */
2315 		error(237, sym->s_name);
2316 		rmsym(dcs->d_rdcsym);
2317 		sym->s_arg = 1;
2318 	}
2319 
2320 	if (!sym->s_arg) {
2321 		/* declared argument %s is missing */
2322 		error(53, sym->s_name);
2323 		sym->s_arg = 1;
2324 	}
2325 
2326 	if (initflg) {
2327 		/* cannot initialize parameter: %s */
2328 		error(52, sym->s_name);
2329 		initerr = 1;
2330 	}
2331 
2332 	if ((t = sym->s_type->t_tspec) == ARRAY) {
2333 		sym->s_type = incref(sym->s_type->t_subt, PTR);
2334 	} else if (t == FUNC) {
2335 		if (tflag)
2336 			/* a function is declared as an argument: %s */
2337 			warning(50, sym->s_name);
2338 		sym->s_type = incref(sym->s_type, PTR);
2339 	} else if (t == FLOAT) {
2340 		if (tflag)
2341 			sym->s_type = gettyp(DOUBLE);
2342 	}
2343 
2344 	if (dcs->d_inline)
2345 		/* argument declared inline: %s */
2346 		warning(269, sym->s_name);
2347 
2348 	/*
2349 	 * Arguments must have complete types. lengths() prints the needed
2350 	 * error messages (null dimension is impossible because arrays are
2351 	 * converted to pointers).
2352 	 */
2353 	if (sym->s_type->t_tspec != VOID)
2354 		(void)length(sym->s_type, sym->s_name);
2355 
2356 	setsflg(sym);
2357 
2358 	return (sym);
2359 }
2360 
2361 /*
2362  * Does some checks for lint directives which apply to functions.
2363  * Processes arguments in old style function definitions which default
2364  * to int.
2365  * Checks compatiblility of old style function definition with previous
2366  * prototype.
2367  */
2368 void
2369 cluparg(void)
2370 {
2371 	sym_t	*args, *arg, *pargs, *parg;
2372 	int	narg, nparg, n, msg;
2373 	tspec_t	t;
2374 
2375 	args = funcsym->s_args;
2376 	pargs = funcsym->s_type->t_args;
2377 
2378 	/* check for illegal combinations of lint directives */
2379 	if (prflstrg != -1 && scflstrg != -1) {
2380 		/* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2381 		warning(289);
2382 		prflstrg = scflstrg = -1;
2383 	}
2384 	if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) {
2385 		/* dubious use of ** VARARGS ** with ** %s ** */
2386 		warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2387 		nvararg = -1;
2388 	}
2389 
2390 	/*
2391 	 * check if the argument of a lint directive is compatible with the
2392 	 * number of arguments.
2393 	 */
2394 	narg = 0;
2395 	for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_nxt)
2396 		narg++;
2397 	if (nargusg > narg) {
2398 		/* argument number mismatch with directive: ** %s ** */
2399 		warning(283, "ARGSUSED");
2400 		nargusg = 0;
2401 	}
2402 	if (nvararg > narg) {
2403 		/* argument number mismatch with directive: ** %s ** */
2404 		warning(283, "VARARGS");
2405 		nvararg = 0;
2406 	}
2407 	if (prflstrg > narg) {
2408 		/* argument number mismatch with directive: ** %s ** */
2409 		warning(283, "PRINTFLIKE");
2410 		prflstrg = -1;
2411 	} else if (prflstrg == 0) {
2412 		prflstrg = -1;
2413 	}
2414 	if (scflstrg > narg) {
2415 		/* argument number mismatch with directive: ** %s ** */
2416 		warning(283, "SCANFLIKE");
2417 		scflstrg = -1;
2418 	} else if (scflstrg == 0) {
2419 		scflstrg = -1;
2420 	}
2421 	if (prflstrg != -1 || scflstrg != -1) {
2422 		narg = prflstrg != -1 ? prflstrg : scflstrg;
2423 		arg = dcs->d_fargs;
2424 		for (n = 1; n < narg; n++)
2425 			arg = arg->s_nxt;
2426 		if (arg->s_type->t_tspec != PTR ||
2427 		    ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2428 		     t != UCHAR && t != SCHAR)) {
2429 			/* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2430 			warning(293, narg);
2431 			prflstrg = scflstrg = -1;
2432 		}
2433 	}
2434 
2435 	/*
2436 	 * print a warning for each argument off an old style function
2437 	 * definition which defaults to int
2438 	 */
2439 	for (arg = args; arg != NULL; arg = arg->s_nxt) {
2440 		if (arg->s_defarg) {
2441 			/* argument type defaults to int: %s */
2442 			warning(32, arg->s_name);
2443 			arg->s_defarg = 0;
2444 			setsflg(arg);
2445 		}
2446 	}
2447 
2448 	/*
2449 	 * If this is an old style function definition and a prototyp
2450 	 * exists, compare the types of arguments.
2451 	 */
2452 	if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2453 		/*
2454 		 * If the number of arguments does not macht, we need not
2455 		 * continue.
2456 		 */
2457 		narg = nparg = 0;
2458 		msg = 0;
2459 		for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2460 			nparg++;
2461 		for (arg = args; arg != NULL; arg = arg->s_nxt)
2462 			narg++;
2463 		if (narg != nparg) {
2464 			/* parameter mismatch: %d declared, %d defined */
2465 			error(51, nparg, narg);
2466 			msg = 1;
2467 		} else {
2468 			parg = pargs;
2469 			arg = args;
2470 			while (narg--) {
2471 				msg |= chkptdecl(arg, parg);
2472 				parg = parg->s_nxt;
2473 				arg = arg->s_nxt;
2474 			}
2475 		}
2476 		if (msg)
2477 			/* prototype declaration */
2478 			prevdecl(285, dcs->d_rdcsym);
2479 
2480 		/* from now the prototype is valid */
2481 		funcsym->s_osdef = 0;
2482 		funcsym->s_args = NULL;
2483 
2484 	}
2485 
2486 }
2487 
2488 /*
2489  * Checks compatibility of an old style function definition with a previous
2490  * prototype declaration.
2491  * Returns 1 if the position of the previous declaration should be reported.
2492  */
2493 static int
2494 chkptdecl(sym_t *arg, sym_t *parg)
2495 {
2496 	type_t	*tp, *ptp;
2497 	int	dowarn, msg;
2498 
2499 	tp = arg->s_type;
2500 	ptp = parg->s_type;
2501 
2502 	msg = 0;
2503 	dowarn = 0;
2504 
2505 	if (!eqtype(tp, ptp, 1, 1, &dowarn)) {
2506 		if (eqtype(tp, ptp, 1, 0, &dowarn)) {
2507 			/* type does not match prototype: %s */
2508 			msg = gnuism(58, arg->s_name);
2509 		} else {
2510 			/* type does not match prototype: %s */
2511 			error(58, arg->s_name);
2512 			msg = 1;
2513 		}
2514 	} else if (dowarn) {
2515 		/* type does not match prototype: %s */
2516 		(*(sflag ? error : warning))(58, arg->s_name);
2517 		msg = 1;
2518 	}
2519 
2520 	return (msg);
2521 }
2522 
2523 /*
2524  * Completes a single local declaration/definition.
2525  */
2526 void
2527 decl1loc(sym_t *dsym, int initflg)
2528 {
2529 
2530 	/* Correct a mistake done in dname(). */
2531 	if (dsym->s_type->t_tspec == FUNC) {
2532 		dsym->s_def = DECL;
2533 		if (dcs->d_scl == NOSCL)
2534 			dsym->s_scl = EXTERN;
2535 	}
2536 
2537 	if (dsym->s_type->t_tspec == FUNC) {
2538 		if (dsym->s_scl == STATIC) {
2539 			/* dubious static function at block level: %s */
2540 			warning(93, dsym->s_name);
2541 			dsym->s_scl = EXTERN;
2542 		} else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2543 			/* function has illegal storage class: %s */
2544 			error(94, dsym->s_name);
2545 			dsym->s_scl = EXTERN;
2546 		}
2547 	}
2548 
2549 	/*
2550 	 * functions may be declared inline at local scope, although
2551 	 * this has no effect for a later definition of the same
2552 	 * function.
2553 	 * XXX it should have an effect if tflag is set. this would
2554 	 * also be the way gcc behaves.
2555 	 */
2556 	if (dcs->d_inline) {
2557 		if (dsym->s_type->t_tspec == FUNC) {
2558 			dsym->s_inline = 1;
2559 		} else {
2560 			/* variable declared inline: %s */
2561 			warning(268, dsym->s_name);
2562 		}
2563 	}
2564 
2565 	chkfdef(dsym, 1);
2566 
2567 	chktyp(dsym);
2568 
2569 	if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN)
2570 		ledecl(dsym);
2571 
2572 	if (dsym->s_scl == EXTERN) {
2573 		/*
2574 		 * XXX wenn die statische Variable auf Ebene 0 erst
2575 		 * spaeter definiert wird, haben wir die Brille auf.
2576 		 */
2577 		if (dsym->s_xsym == NULL) {
2578 			outsym(dsym, EXTERN, dsym->s_def);
2579 		} else {
2580 			outsym(dsym, dsym->s_xsym->s_scl, dsym->s_def);
2581 		}
2582 	}
2583 
2584 	if (dcs->d_rdcsym != NULL) {
2585 
2586 		if (dcs->d_rdcsym->s_blklev == 0) {
2587 
2588 			switch (dsym->s_scl) {
2589 			case AUTO:
2590 				/* automatic hides external declaration: %s */
2591 				if (hflag)
2592 					warning(86, dsym->s_name);
2593 				break;
2594 			case STATIC:
2595 				/* static hides external declaration: %s */
2596 				if (hflag)
2597 					warning(87, dsym->s_name);
2598 				break;
2599 			case TYPEDEF:
2600 				/* typedef hides  external declaration: %s */
2601 				if (hflag)
2602 					warning(88, dsym->s_name);
2603 				break;
2604 			case EXTERN:
2605 				/*
2606 				 * Warnings and errors are printed in ledecl()
2607 				 */
2608 				break;
2609 			default:
2610 				LERROR("decl1loc()");
2611 			}
2612 
2613 		} else if (dcs->d_rdcsym->s_blklev == blklev) {
2614 
2615 			/* no hflag, because its illegal! */
2616 			if (dcs->d_rdcsym->s_arg) {
2617 				/*
2618 				 * if !tflag, a "redeclaration of %s" error
2619 				 * is produced below
2620 				 */
2621 				if (tflag) {
2622 					if (hflag)
2623 						/* decl. hides parameter: %s */
2624 						warning(91, dsym->s_name);
2625 					rmsym(dcs->d_rdcsym);
2626 				}
2627 			}
2628 
2629 		} else if (dcs->d_rdcsym->s_blklev < blklev) {
2630 
2631 			if (hflag)
2632 				/* declaration hides earlier one: %s */
2633 				warning(95, dsym->s_name);
2634 
2635 		}
2636 
2637 		if (dcs->d_rdcsym->s_blklev == blklev) {
2638 
2639 			/* redeclaration of %s */
2640 			error(27, dsym->s_name);
2641 			rmsym(dcs->d_rdcsym);
2642 
2643 		}
2644 
2645 	}
2646 
2647 	if (initflg && !(initerr = chkinit(dsym))) {
2648 		dsym->s_def = DEF;
2649 		setsflg(dsym);
2650 	}
2651 
2652 	if (dsym->s_scl == TYPEDEF) {
2653 		dsym->s_type = duptyp(dsym->s_type);
2654 		dsym->s_type->t_typedef = 1;
2655 		settdsym(dsym->s_type, dsym);
2656 	}
2657 
2658 	/*
2659 	 * Before we can check the size we must wait for a initialisation
2660 	 * which may follow.
2661 	 */
2662 }
2663 
2664 /*
2665  * Processes (re)declarations of external Symbols inside blocks.
2666  */
2667 static void
2668 ledecl(sym_t *dsym)
2669 {
2670 	int	eqt, dowarn;
2671 	sym_t	*esym;
2672 
2673 	/* look for a symbol with the same name */
2674 	esym = dcs->d_rdcsym;
2675 	while (esym != NULL && esym->s_blklev != 0) {
2676 		while ((esym = esym->s_link) != NULL) {
2677 			if (esym->s_kind != FVFT)
2678 				continue;
2679 			if (strcmp(dsym->s_name, esym->s_name) == 0)
2680 				break;
2681 		}
2682 	}
2683 	if (esym == NULL)
2684 		return;
2685 	if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2686 		/* gcc accepts this without a warning, pcc prints an error. */
2687 		/* redeclaration of %s */
2688 		warning(27, dsym->s_name);
2689 		prevdecl(-1, esym);
2690 		return;
2691 	}
2692 
2693 	dowarn = 0;
2694 	eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &dowarn);
2695 
2696 	if (!eqt || dowarn) {
2697 		if (esym->s_scl == EXTERN) {
2698 			/* inconsistent redeclaration of extern: %s */
2699 			warning(90, dsym->s_name);
2700 			prevdecl(-1, esym);
2701 		} else {
2702 			/* inconsistent redeclaration of static: %s */
2703 			warning(92, dsym->s_name);
2704 			prevdecl(-1, esym);
2705 		}
2706 	}
2707 
2708 	if (eqt) {
2709 		/*
2710 		 * Remember the external symbol so we can update usage
2711 		 * information at the end of the block.
2712 		 */
2713 		dsym->s_xsym = esym;
2714 	}
2715 }
2716 
2717 /*
2718  * Print an error or a warning if the symbol cant be initialized due
2719  * to type/storage class. Returnvalue is 1 if an error has been
2720  * detected.
2721  */
2722 static int
2723 chkinit(sym_t *sym)
2724 {
2725 	int	erred;
2726 
2727 	erred = 0;
2728 
2729 	if (sym->s_type->t_tspec == FUNC) {
2730 		/* cannot initialize function: %s */
2731 		error(24, sym->s_name);
2732 		erred = 1;
2733 	} else if (sym->s_scl == TYPEDEF) {
2734 		/* cannot initialize typedef: %s */
2735 		error(25, sym->s_name);
2736 		erred = 1;
2737 	} else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2738 		/* cannot initialize "extern" declaration: %s */
2739 		if (dcs->d_ctx == EXTERN) {
2740 			warning(26, sym->s_name);
2741 		} else {
2742 			error(26, sym->s_name);
2743 			erred = 1;
2744 		}
2745 	}
2746 
2747 	return (erred);
2748 }
2749 
2750 /*
2751  * Create a symbole for an abstract declaration.
2752  */
2753 sym_t *
2754 aname(void)
2755 {
2756 	sym_t	*sym;
2757 
2758 	if (dcs->d_ctx != ABSTRACT && dcs->d_ctx != PARG)
2759 		LERROR("aname()");
2760 
2761 	sym = getblk(sizeof (sym_t));
2762 
2763 	sym->s_name = unnamed;
2764 	sym->s_def = DEF;
2765 	sym->s_scl = ABSTRACT;
2766 	sym->s_blklev = -1;
2767 
2768 	if (dcs->d_ctx == PARG)
2769 		sym->s_arg = 1;
2770 
2771 	sym->s_type = dcs->d_type;
2772 	dcs->d_rdcsym = NULL;
2773 	dcs->d_vararg = 0;
2774 
2775 	return (sym);
2776 }
2777 
2778 /*
2779  * Removes anything which has nothing to do on global level.
2780  */
2781 void
2782 globclup(void)
2783 {
2784 
2785 	while (dcs->d_nxt != NULL)
2786 		popdecl();
2787 
2788 	cleanup();
2789 	blklev = 0;
2790 	mblklev = 0;
2791 
2792 	/*
2793 	 * remove all informations about pending lint directives without
2794 	 * warnings.
2795 	 */
2796 	glclup(1);
2797 }
2798 
2799 /*
2800  * Process an abstract type declaration
2801  */
2802 sym_t *
2803 decl1abs(sym_t *sym)
2804 {
2805 
2806 	chkfdef(sym, 1);
2807 	chktyp(sym);
2808 	return (sym);
2809 }
2810 
2811 /*
2812  * Checks size after declarations of variables and their initialisation.
2813  */
2814 void
2815 chksz(sym_t *dsym)
2816 {
2817 
2818 	/*
2819 	 * check size only for symbols which are defined and no function and
2820 	 * not typedef name
2821 	 */
2822 	if (dsym->s_def != DEF)
2823 		return;
2824 	if (dsym->s_scl == TYPEDEF)
2825 		return;
2826 	if (dsym->s_type->t_tspec == FUNC)
2827 		return;
2828 
2829 	if (length(dsym->s_type, dsym->s_name) == 0 &&
2830 	    dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2831 		/* empty array declaration: %s */
2832 		if (tflag) {
2833 			warning(190, dsym->s_name);
2834 		} else {
2835 			error(190, dsym->s_name);
2836 		}
2837 	}
2838 }
2839 
2840 /*
2841  * Mark an object as set if it is not already
2842  */
2843 void
2844 setsflg(sym_t *sym)
2845 {
2846 
2847 	if (!sym->s_set) {
2848 		sym->s_set = 1;
2849 		UNIQUE_CURR_POS(sym->s_spos);
2850 	}
2851 }
2852 
2853 /*
2854  * Mark an object as used if it is not already
2855  */
2856 void
2857 setuflg(sym_t *sym, int fcall, int szof)
2858 {
2859 
2860 	if (!sym->s_used) {
2861 		sym->s_used = 1;
2862 		UNIQUE_CURR_POS(sym->s_upos);
2863 	}
2864 	/*
2865 	 * for function calls another record is written
2866 	 *
2867 	 * XXX Should symbols used in sizeof() treated as used or not?
2868 	 * Probably not, because there is no sense to declare an
2869 	 * external variable only to get their size.
2870 	 */
2871 	if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2872 		outusg(sym);
2873 }
2874 
2875 /*
2876  * Prints warnings for a list of variables and labels (concatenated
2877  * with s_dlnxt) if these are not used or only set.
2878  */
2879 void
2880 chkusage(dinfo_t *di)
2881 {
2882 	sym_t	*sym;
2883 	int	mklwarn;
2884 
2885 	/* for this warning LINTED has no effect */
2886 	mklwarn = lwarn;
2887 	lwarn = LWARN_ALL;
2888 
2889 #ifdef DEBUG
2890 	printf("%s, %d: >temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
2891 	    lwarn);
2892 #endif
2893 	for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
2894 		chkusg1(di->d_asm, sym);
2895 	lwarn = mklwarn;
2896 #ifdef DEBUG
2897 	printf("%s, %d: <temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
2898 	    lwarn);
2899 #endif
2900 }
2901 
2902 /*
2903  * Prints a warning for a single variable or label if it is not used or
2904  * only set.
2905  */
2906 void
2907 chkusg1(int novar, sym_t *sym)
2908 {
2909 	pos_t	cpos;
2910 
2911 	if (sym->s_blklev == -1)
2912 		return;
2913 
2914 	STRUCT_ASSIGN(cpos, curr_pos);
2915 
2916 	if (sym->s_kind == FVFT) {
2917 		if (sym->s_arg) {
2918 			chkausg(novar, sym);
2919 		} else {
2920 			chkvusg(novar, sym);
2921 		}
2922 	} else if (sym->s_kind == FLAB) {
2923 		chklusg(sym);
2924 	} else if (sym->s_kind == FTAG) {
2925 		chktusg(sym);
2926 	}
2927 
2928 	STRUCT_ASSIGN(curr_pos, cpos);
2929 }
2930 
2931 static void
2932 chkausg(int novar, sym_t *arg)
2933 {
2934 
2935 	if (!arg->s_set)
2936 		LERROR("chkausg()");
2937 
2938 	if (novar)
2939 		return;
2940 
2941 	if (!arg->s_used && vflag) {
2942 		STRUCT_ASSIGN(curr_pos, arg->s_dpos);
2943 		/* argument %s unused in function %s */
2944 		warning(231, arg->s_name, funcsym->s_name);
2945 	}
2946 }
2947 
2948 static void
2949 chkvusg(int novar, sym_t *sym)
2950 {
2951 	scl_t	sc;
2952 	sym_t	*xsym;
2953 
2954 	if (blklev == 0 || sym->s_blklev == 0)
2955 		LERROR("chkvusg()");
2956 
2957 	/* errors in expressions easily cause lots of these warnings */
2958 	if (nerr != 0)
2959 		return;
2960 
2961 	/*
2962 	 * XXX Only variables are checkd, although types should
2963 	 * probably also be checked
2964 	 */
2965 	if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
2966 	    sc != AUTO && sc != REG) {
2967 		return;
2968 	}
2969 
2970 	if (novar)
2971 		return;
2972 
2973 	if (sc == EXTERN) {
2974 		if (!sym->s_used && !sym->s_set) {
2975 			STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2976 			/* %s unused in function %s */
2977 			warning(192, sym->s_name, funcsym->s_name);
2978 		}
2979 	} else {
2980 		if (sym->s_set && !sym->s_used) {
2981 			STRUCT_ASSIGN(curr_pos, sym->s_spos);
2982 			/* %s set but not used in function %s */
2983 			warning(191, sym->s_name, funcsym->s_name);
2984 		} else if (!sym->s_used) {
2985 			STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2986 			/* %s unused in function %s */
2987 			warning(192, sym->s_name, funcsym->s_name);
2988 		}
2989 	}
2990 
2991 	if (sc == EXTERN) {
2992 		/*
2993 		 * information about usage is taken over into the symbol
2994 		 * tabel entry at level 0 if the symbol was locally declared
2995 		 * as an external symbol.
2996 		 *
2997 		 * XXX This is wrong for symbols declared static at level 0
2998 		 * if the usage information stems from sizeof(). This is
2999 		 * because symbols at level 0 only used in sizeof() are
3000 		 * considered to not be used.
3001 		 */
3002 		if ((xsym = sym->s_xsym) != NULL) {
3003 			if (sym->s_used && !xsym->s_used) {
3004 				xsym->s_used = 1;
3005 				STRUCT_ASSIGN(xsym->s_upos, sym->s_upos);
3006 			}
3007 			if (sym->s_set && !xsym->s_set) {
3008 				xsym->s_set = 1;
3009 				STRUCT_ASSIGN(xsym->s_spos, sym->s_spos);
3010 			}
3011 		}
3012 	}
3013 }
3014 
3015 static void
3016 chklusg(sym_t *lab)
3017 {
3018 
3019 	if (blklev != 1 || lab->s_blklev != 1)
3020 		LERROR("chklusg()");
3021 
3022 	if (lab->s_set && !lab->s_used) {
3023 		STRUCT_ASSIGN(curr_pos, lab->s_spos);
3024 		/* label %s unused in function %s */
3025 		warning(192, lab->s_name, funcsym->s_name);
3026 	} else if (!lab->s_set) {
3027 		STRUCT_ASSIGN(curr_pos, lab->s_upos);
3028 		/* undefined label %s */
3029 		warning(23, lab->s_name);
3030 	}
3031 }
3032 
3033 static void
3034 chktusg(sym_t *sym)
3035 {
3036 
3037 	if (!incompl(sym->s_type))
3038 		return;
3039 
3040 	/* complain alwasy about incomplet tags declared inside blocks */
3041 	if (!zflag || dcs->d_ctx != EXTERN)
3042 		return;
3043 
3044 	STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3045 	switch (sym->s_type->t_tspec) {
3046 	case STRUCT:
3047 		/* struct %s never defined */
3048 		warning(233, sym->s_name);
3049 		break;
3050 	case UNION:
3051 		/* union %s never defined */
3052 		warning(234, sym->s_name);
3053 		break;
3054 	case ENUM:
3055 		/* enum %s never defined */
3056 		warning(235, sym->s_name);
3057 		break;
3058 	default:
3059 		LERROR("chktusg()");
3060 	}
3061 }
3062 
3063 /*
3064  * Called after the entire translation unit has been parsed.
3065  * Changes tentative definitions in definitions.
3066  * Performs some tests on global Symbols. Detected Problems are:
3067  * - defined variables of incomplete type
3068  * - constant variables which are not initialized
3069  * - static symbols which are never used
3070  */
3071 void
3072 chkglsyms(void)
3073 {
3074 	sym_t	*sym;
3075 	pos_t	cpos;
3076 
3077 	if (blklev != 0 || dcs->d_nxt != NULL)
3078 		norecover();
3079 
3080 	STRUCT_ASSIGN(cpos, curr_pos);
3081 
3082 	for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
3083 		if (sym->s_blklev == -1)
3084 			continue;
3085 		if (sym->s_kind == FVFT) {
3086 			chkglvar(sym);
3087 		} else if (sym->s_kind == FTAG) {
3088 			chktusg(sym);
3089 		} else {
3090 			if (sym->s_kind != FMOS)
3091 				LERROR("chkglsyms()");
3092 		}
3093 	}
3094 
3095 	STRUCT_ASSIGN(curr_pos, cpos);
3096 }
3097 
3098 static void
3099 chkglvar(sym_t *sym)
3100 {
3101 
3102 	if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON)
3103 		return;
3104 
3105 	if (sym->s_scl != EXTERN && sym->s_scl != STATIC)
3106 		LERROR("chkglvar()");
3107 
3108 	glchksz(sym);
3109 
3110 	if (sym->s_scl == STATIC) {
3111 		if (sym->s_type->t_tspec == FUNC) {
3112 			if (sym->s_used && sym->s_def != DEF) {
3113 				STRUCT_ASSIGN(curr_pos, sym->s_upos);
3114 				/* static func. called but not def.. */
3115 				error(225, sym->s_name);
3116 			}
3117 		}
3118 		if (!sym->s_used) {
3119 			STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3120 			if (sym->s_type->t_tspec == FUNC) {
3121 				if (sym->s_def == DEF) {
3122 					if (!sym->s_inline)
3123 						/* static function %s unused */
3124 						warning(236, sym->s_name);
3125 				} else {
3126 					/* static function %s decl. but ... */
3127 					warning(290, sym->s_name);
3128 				}
3129 			} else if (!sym->s_set) {
3130 				/* static variable %s unused */
3131 				warning(226, sym->s_name);
3132 			} else {
3133 				/* static variable %s set but not used */
3134 				warning(307, sym->s_name);
3135 			}
3136 		}
3137 		if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
3138 			STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3139 			/* const object %s should have initializer */
3140 			warning(227, sym->s_name);
3141 		}
3142 	}
3143 }
3144 
3145 static void
3146 glchksz(sym_t *sym)
3147 {
3148 
3149 	if (sym->s_def == TDEF) {
3150 		if (sym->s_type->t_tspec == FUNC)
3151 			/*
3152 			 * this can happen if an syntax error occurred
3153 			 * after a function declaration
3154 			 */
3155 			return;
3156 		STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3157 		if (length(sym->s_type, sym->s_name) == 0 &&
3158 		    sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3159 			/* empty array declaration: %s */
3160 			if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3161 				warning(190, sym->s_name);
3162 			} else {
3163 				error(190, sym->s_name);
3164 			}
3165 		}
3166 	}
3167 }
3168 
3169 /*
3170  * Prints information about location of previous definition/declaration.
3171  */
3172 void
3173 prevdecl(int msg, sym_t *psym)
3174 {
3175 	pos_t	cpos;
3176 
3177 	if (!rflag)
3178 		return;
3179 
3180 	STRUCT_ASSIGN(cpos, curr_pos);
3181 	STRUCT_ASSIGN(curr_pos, psym->s_dpos);
3182 	if (msg != -1) {
3183 		message(msg, psym->s_name);
3184 	} else if (psym->s_def == DEF || psym->s_def == TDEF) {
3185 		/* previous definition of %s */
3186 		message(261, psym->s_name);
3187 	} else {
3188 		/* previous declaration of %s */
3189 		message(260, psym->s_name);
3190 	}
3191 	STRUCT_ASSIGN(curr_pos, cpos);
3192 }
3193