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