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