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