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