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