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