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