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