decl.c revision 1.27 1 /* $NetBSD: decl.c,v 1.27 2002/01/03 05:26:53 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.27 2002/01/03 05:26:53 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 * Integer types not dealt with above are
1092 * okay only if BITFIELDTYPE is in effect.
1093 */
1094 if (bitfieldtype_ok == 0 || isityp(t) == 0) {
1095 /* illegal bit-field type */
1096 error(35);
1097 sz = tp->t_flen;
1098 dsym->s_type = tp = duptyp(gettyp(t = INT));
1099 if ((tp->t_flen = sz) > size(t))
1100 tp->t_flen = size(t);
1101 }
1102 }
1103 if ((len = tp->t_flen) < 0 || len > size(t)) {
1104 /* illegal bit-field size */
1105 error(36);
1106 tp->t_flen = size(t);
1107 } else if (len == 0 && dsym->s_name != unnamed) {
1108 /* zero size bit-field */
1109 error(37);
1110 tp->t_flen = size(t);
1111 }
1112 if (dsym->s_scl == MOU) {
1113 /* illegal use of bit-field */
1114 error(41);
1115 dsym->s_type->t_isfield = 0;
1116 dsym->s_field = 0;
1117 }
1118 } else if (t == FUNC) {
1119 /* function illegal in structure or union */
1120 error(38);
1121 dsym->s_type = tp = incref(tp, t = PTR);
1122 }
1123
1124 /*
1125 * bit-fields of length 0 are not warned about because length()
1126 * does not return the length of the bit-field but the length
1127 * of the type the bit-field is packed in (its ok)
1128 */
1129 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1130 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1131 /* illegal zero sized structure member: %s */
1132 warning(39, dsym->s_name);
1133 }
1134 }
1135
1136 if (dcs->d_ctx == MOU) {
1137 o = dcs->d_offset;
1138 dcs->d_offset = 0;
1139 }
1140 if (dsym->s_field) {
1141 align(getbound(tp), tp->t_flen);
1142 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t);
1143 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1144 dcs->d_offset += tp->t_flen;
1145 } else {
1146 align(getbound(tp), 0);
1147 dsym->s_value.v_quad = dcs->d_offset;
1148 dcs->d_offset += sz;
1149 }
1150 if (dcs->d_ctx == MOU) {
1151 if (o > dcs->d_offset)
1152 dcs->d_offset = o;
1153 }
1154
1155 chkfdef(dsym, 0);
1156
1157 /*
1158 * Clear the BITFIELDTYPE indicator after processing each
1159 * structure element.
1160 */
1161 bitfieldtype_ok = 0;
1162
1163 return (dsym);
1164 }
1165
1166 /*
1167 * Aligns next structure element as required.
1168 *
1169 * al contains the required alignment, len the length of a bit-field.
1170 */
1171 static void
1172 align(int al, int len)
1173 {
1174 int no;
1175
1176 /*
1177 * The alignment of the current element becomes the alignment of
1178 * the struct/union if it is larger than the current alignment
1179 * of the struct/union.
1180 */
1181 if (al > dcs->d_stralign)
1182 dcs->d_stralign = al;
1183
1184 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1185 if (len == 0 || dcs->d_offset + len > no)
1186 dcs->d_offset = no;
1187 }
1188
1189 /*
1190 * Remember the width of the field in its type structure.
1191 */
1192 sym_t *
1193 bitfield(sym_t *dsym, int len)
1194 {
1195
1196 if (dsym == NULL) {
1197 dsym = getblk(sizeof (sym_t));
1198 dsym->s_name = unnamed;
1199 dsym->s_kind = FMOS;
1200 dsym->s_scl = MOS;
1201 dsym->s_type = gettyp(UINT);
1202 dsym->s_blklev = -1;
1203 }
1204 dsym->s_type = duptyp(dsym->s_type);
1205 dsym->s_type->t_isfield = 1;
1206 dsym->s_type->t_flen = len;
1207 dsym->s_field = 1;
1208 return (dsym);
1209 }
1210
1211 /*
1212 * Collect informations about a sequence of asterisks and qualifiers
1213 * in a list of type pqinf_t.
1214 * Qualifiers refer always to the left asterisk. The rightmost asterisk
1215 * will be at the top of the list.
1216 */
1217 pqinf_t *
1218 mergepq(pqinf_t *p1, pqinf_t *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 continue;
1226 p->p_nxt = p1;
1227 return (p2);
1228 } else {
1229 if (p2->p_const) {
1230 if (p1->p_const) {
1231 /* duplicate %s */
1232 warning(10, "const");
1233 }
1234 p1->p_const = 1;
1235 }
1236 if (p2->p_volatile) {
1237 if (p1->p_volatile) {
1238 /* duplicate %s */
1239 warning(10, "volatile");
1240 }
1241 p1->p_volatile = 1;
1242 }
1243 free(p2);
1244 return (p1);
1245 }
1246 }
1247
1248 /*
1249 * Followint 3 functions extend the type of a declarator with
1250 * pointer, function and array types.
1251 *
1252 * The current type is the Type built by deftyp() (dcs->d_type) and
1253 * pointer, function and array types already added for this
1254 * declarator. The new type extension is inserted between both.
1255 */
1256 sym_t *
1257 addptr(sym_t *decl, pqinf_t *pi)
1258 {
1259 type_t **tpp, *tp;
1260 pqinf_t *npi;
1261
1262 tpp = &decl->s_type;
1263 while (*tpp && *tpp != dcs->d_type)
1264 tpp = &(*tpp)->t_subt;
1265 if (*tpp == NULL)
1266 return decl;
1267
1268 while (pi != NULL) {
1269 *tpp = tp = getblk(sizeof (type_t));
1270 tp->t_tspec = PTR;
1271 tp->t_const = pi->p_const;
1272 tp->t_volatile = pi->p_volatile;
1273 *(tpp = &tp->t_subt) = dcs->d_type;
1274 npi = pi->p_nxt;
1275 free(pi);
1276 pi = npi;
1277 }
1278 return (decl);
1279 }
1280
1281 /*
1282 * If a dimension was specified, dim is 1, otherwise 0
1283 * n is the specified dimension
1284 */
1285 sym_t *
1286 addarray(sym_t *decl, int dim, int n)
1287 {
1288 type_t **tpp, *tp;
1289
1290 tpp = &decl->s_type;
1291 while (*tpp && *tpp != dcs->d_type)
1292 tpp = &(*tpp)->t_subt;
1293 if (*tpp == NULL)
1294 return decl;
1295
1296 *tpp = tp = getblk(sizeof (type_t));
1297 tp->t_tspec = ARRAY;
1298 tp->t_subt = dcs->d_type;
1299 tp->t_dim = n;
1300
1301 if (n < 0) {
1302 /* zero or negative array dimension */
1303 error(20);
1304 n = 0;
1305 } else if (n == 0 && dim) {
1306 /* zero or negative array dimension */
1307 warning(20);
1308 } else if (n == 0 && !dim) {
1309 /* is incomplete type */
1310 setcompl(tp, 1);
1311 }
1312
1313 return (decl);
1314 }
1315
1316 sym_t *
1317 addfunc(sym_t *decl, sym_t *args)
1318 {
1319 type_t **tpp, *tp;
1320
1321 if (dcs->d_proto) {
1322 if (tflag)
1323 /* function prototypes are illegal in traditional C */
1324 warning(270);
1325 args = nsfunc(decl, args);
1326 } else {
1327 osfunc(decl, args);
1328 }
1329
1330 /*
1331 * The symbols are removed from the symbol table by popdecl() after
1332 * addfunc(). To be able to restore them if this is a function
1333 * definition, a pointer to the list of all symbols is stored in
1334 * dcs->d_nxt->d_fpsyms. Also a list of the arguments (concatenated
1335 * by s_nxt) is stored in dcs->d_nxt->d_fargs.
1336 * (dcs->d_nxt must be used because *dcs is the declaration stack
1337 * element created for the list of params and is removed after
1338 * addfunc())
1339 */
1340 if (dcs->d_nxt->d_ctx == EXTERN &&
1341 decl->s_type == dcs->d_nxt->d_type) {
1342 dcs->d_nxt->d_fpsyms = dcs->d_dlsyms;
1343 dcs->d_nxt->d_fargs = args;
1344 }
1345
1346 tpp = &decl->s_type;
1347 while (*tpp && *tpp != dcs->d_nxt->d_type)
1348 tpp = &(*tpp)->t_subt;
1349 if (*tpp == NULL)
1350 return decl;
1351
1352 *tpp = tp = getblk(sizeof (type_t));
1353 tp->t_tspec = FUNC;
1354 tp->t_subt = dcs->d_nxt->d_type;
1355 if ((tp->t_proto = dcs->d_proto) != 0)
1356 tp->t_args = args;
1357 tp->t_vararg = dcs->d_vararg;
1358
1359 return (decl);
1360 }
1361
1362 /*
1363 * Called for new style function declarations.
1364 */
1365 /* ARGSUSED */
1366 static sym_t *
1367 nsfunc(sym_t *decl, sym_t *args)
1368 {
1369 sym_t *arg, *sym;
1370 scl_t sc;
1371 int n;
1372
1373 /*
1374 * Declarations of structs/unions/enums in param lists are legal,
1375 * but senseless.
1376 */
1377 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1378 sc = sym->s_scl;
1379 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) {
1380 /* dubious tag declaration: %s %s */
1381 warning(85, scltoa(sc), sym->s_name);
1382 }
1383 }
1384
1385 n = 1;
1386 for (arg = args; arg != NULL; arg = arg->s_nxt) {
1387 if (arg->s_type->t_tspec == VOID) {
1388 if (n > 1 || arg->s_nxt != NULL) {
1389 /* "void" must be sole parameter */
1390 error(60);
1391 arg->s_type = gettyp(INT);
1392 }
1393 }
1394 n++;
1395 }
1396
1397 /* return NULL if first param is VOID */
1398 return (args != NULL && args->s_type->t_tspec != VOID ? args : NULL);
1399 }
1400
1401 /*
1402 * Called for old style function declarations.
1403 */
1404 static void
1405 osfunc(sym_t *decl, sym_t *args)
1406 {
1407
1408 /*
1409 * Remember list of params only if this is really seams to be
1410 * a function definition.
1411 */
1412 if (dcs->d_nxt->d_ctx == EXTERN &&
1413 decl->s_type == dcs->d_nxt->d_type) {
1414 /*
1415 * We assume that this becomes a function definition. If
1416 * we are wrong, its corrected in chkfdef().
1417 */
1418 if (args != NULL) {
1419 decl->s_osdef = 1;
1420 decl->s_args = args;
1421 }
1422 } else {
1423 if (args != NULL)
1424 /* function prototype parameters must have types */
1425 warning(62);
1426 }
1427 }
1428
1429 /*
1430 * Lists of Identifiers in functions declarations are allowed only if
1431 * its also a function definition. If this is not the case, print a
1432 * error message.
1433 */
1434 void
1435 chkfdef(sym_t *sym, int msg)
1436 {
1437
1438 if (sym->s_osdef) {
1439 if (msg) {
1440 /* incomplete or misplaced function definition */
1441 error(22);
1442 }
1443 sym->s_osdef = 0;
1444 sym->s_args = NULL;
1445 }
1446 }
1447
1448 /*
1449 * Process the name in a declarator.
1450 * If the symbol does already exists, a new one is created.
1451 * The symbol becomes one of the storage classes EXTERN, STATIC, AUTO or
1452 * TYPEDEF.
1453 * s_def and s_reg are valid after dname().
1454 */
1455 sym_t *
1456 dname(sym_t *sym)
1457 {
1458 scl_t sc = NOSCL;
1459
1460 if (sym->s_scl == NOSCL) {
1461 dcs->d_rdcsym = NULL;
1462 } else if (sym->s_defarg) {
1463 sym->s_defarg = 0;
1464 dcs->d_rdcsym = NULL;
1465 } else {
1466 dcs->d_rdcsym = sym;
1467 sym = pushdown(sym);
1468 }
1469
1470 switch (dcs->d_ctx) {
1471 case MOS:
1472 case MOU:
1473 /* Parent setzen */
1474 sym->s_styp = dcs->d_tagtyp->t_str;
1475 sym->s_def = DEF;
1476 sym->s_value.v_tspec = INT;
1477 sc = dcs->d_ctx;
1478 break;
1479 case EXTERN:
1480 /*
1481 * static and external symbols without "extern" are
1482 * considered to be tentative defined, external
1483 * symbols with "extern" are declared, and typedef names
1484 * are defined. Tentative defined and declared symbols
1485 * may become defined if an initializer is present or
1486 * this is a function definition.
1487 */
1488 if ((sc = dcs->d_scl) == NOSCL) {
1489 sc = EXTERN;
1490 sym->s_def = TDEF;
1491 } else if (sc == STATIC) {
1492 sym->s_def = TDEF;
1493 } else if (sc == TYPEDEF) {
1494 sym->s_def = DEF;
1495 } else if (sc == EXTERN) {
1496 sym->s_def = DECL;
1497 } else {
1498 lerror("dname() 1");
1499 }
1500 break;
1501 case PARG:
1502 sym->s_arg = 1;
1503 /* FALLTHROUGH */
1504 case ARG:
1505 if ((sc = dcs->d_scl) == NOSCL) {
1506 sc = AUTO;
1507 } else if (sc == REG) {
1508 sym->s_reg = 1;
1509 sc = AUTO;
1510 } else {
1511 lerror("dname() 2");
1512 }
1513 sym->s_def = DEF;
1514 break;
1515 case AUTO:
1516 if ((sc = dcs->d_scl) == NOSCL) {
1517 /*
1518 * XXX somewhat ugly because we dont know whether
1519 * this is AUTO or EXTERN (functions). If we are
1520 * wrong it must be corrected in decl1loc(), where
1521 * we have the necessary type information.
1522 */
1523 sc = AUTO;
1524 sym->s_def = DEF;
1525 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1526 sym->s_def = DEF;
1527 } else if (sc == REG) {
1528 sym->s_reg = 1;
1529 sc = AUTO;
1530 sym->s_def = DEF;
1531 } else if (sc == EXTERN) {
1532 sym->s_def = DECL;
1533 } else {
1534 lerror("dname() 3");
1535 }
1536 break;
1537 default:
1538 lerror("dname() 4");
1539 }
1540 sym->s_scl = sc;
1541
1542 sym->s_type = dcs->d_type;
1543
1544 dcs->d_fpsyms = NULL;
1545
1546 return (sym);
1547 }
1548
1549 /*
1550 * Process a name in the list of formal params in an old style function
1551 * definition.
1552 */
1553 sym_t *
1554 iname(sym_t *sym)
1555 {
1556
1557 if (sym->s_scl != NOSCL) {
1558 if (blklev == sym->s_blklev) {
1559 /* redeclaration of formal parameter %s */
1560 error(21, sym->s_name);
1561 if (!sym->s_defarg)
1562 lerror("iname()");
1563 }
1564 sym = pushdown(sym);
1565 }
1566 sym->s_type = gettyp(INT);
1567 sym->s_scl = AUTO;
1568 sym->s_def = DEF;
1569 sym->s_defarg = sym->s_arg = 1;
1570 return (sym);
1571 }
1572
1573 /*
1574 * Create the type of a tag.
1575 *
1576 * tag points to the symbol table entry of the tag
1577 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1578 * decl is 1 if the type of the tag will be completed in this declaration
1579 * (the following token is T_LBRACE)
1580 * semi is 1 if the following token is T_SEMI
1581 */
1582 type_t *
1583 mktag(sym_t *tag, tspec_t kind, int decl, int semi)
1584 {
1585 scl_t scl = NOSCL;
1586 type_t *tp;
1587
1588 if (kind == STRUCT) {
1589 scl = STRTAG;
1590 } else if (kind == UNION) {
1591 scl = UNIONTAG;
1592 } else if (kind == ENUM) {
1593 scl = ENUMTAG;
1594 } else {
1595 lerror("mktag()");
1596 }
1597
1598 if (tag != NULL) {
1599 if (tag->s_scl != NOSCL) {
1600 tag = newtag(tag, scl, decl, semi);
1601 } else {
1602 /* a new tag, no empty declaration */
1603 dcs->d_nxt->d_nedecl = 1;
1604 if (scl == ENUMTAG && !decl) {
1605 if (!tflag && (sflag || pflag))
1606 /* forward reference to enum type */
1607 warning(42);
1608 }
1609 }
1610 if (tag->s_scl == NOSCL) {
1611 tag->s_scl = scl;
1612 tag->s_type = tp = getblk(sizeof (type_t));
1613 } else {
1614 tp = tag->s_type;
1615 }
1616 } else {
1617 tag = getblk(sizeof (sym_t));
1618 tag->s_name = unnamed;
1619 UNIQUE_CURR_POS(tag->s_dpos);
1620 tag->s_kind = FTAG;
1621 tag->s_scl = scl;
1622 tag->s_blklev = -1;
1623 tag->s_type = tp = getblk(sizeof (type_t));
1624 dcs->d_nxt->d_nedecl = 1;
1625 }
1626
1627 if (tp->t_tspec == NOTSPEC) {
1628 tp->t_tspec = kind;
1629 if (kind != ENUM) {
1630 tp->t_str = getblk(sizeof (str_t));
1631 tp->t_str->align = CHAR_BIT;
1632 tp->t_str->stag = tag;
1633 } else {
1634 tp->t_isenum = 1;
1635 tp->t_enum = getblk(sizeof (enum_t));
1636 tp->t_enum->etag = tag;
1637 }
1638 /* ist unvollstaendiger Typ */
1639 setcompl(tp, 1);
1640 }
1641
1642 return (tp);
1643 }
1644
1645 /*
1646 * Checks all possible cases of tag redeclarations.
1647 * decl is 1 if T_LBRACE follows
1648 * semi is 1 if T_SEMI follows
1649 */
1650 static sym_t *
1651 newtag(sym_t *tag, scl_t scl, int decl, int semi)
1652 {
1653
1654 if (tag->s_blklev < blklev) {
1655 if (semi) {
1656 /* "struct a;" */
1657 if (!tflag) {
1658 if (!sflag)
1659 /* decl. introduces new type ... */
1660 warning(44, scltoa(scl), tag->s_name);
1661 tag = pushdown(tag);
1662 } else if (tag->s_scl != scl) {
1663 /* base type is really "%s %s" */
1664 warning(45, scltoa(tag->s_scl), tag->s_name);
1665 }
1666 dcs->d_nxt->d_nedecl = 1;
1667 } else if (decl) {
1668 /* "struct a { ... } " */
1669 if (hflag)
1670 /* redefinition hides earlier one: %s */
1671 warning(43, tag->s_name);
1672 tag = pushdown(tag);
1673 dcs->d_nxt->d_nedecl = 1;
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 /* declaration introduces new type in ANSI C: %s %s */
1678 if (!sflag)
1679 warning(44, scltoa(scl), tag->s_name);
1680 tag = pushdown(tag);
1681 dcs->d_nxt->d_nedecl = 1;
1682 }
1683 } else {
1684 if (tag->s_scl != scl) {
1685 /* (%s) tag redeclared */
1686 error(46, scltoa(tag->s_scl));
1687 prevdecl(-1, tag);
1688 tag = pushdown(tag);
1689 dcs->d_nxt->d_nedecl = 1;
1690 } else if (decl && !incompl(tag->s_type)) {
1691 /* (%s) tag redeclared */
1692 error(46, scltoa(tag->s_scl));
1693 prevdecl(-1, tag);
1694 tag = pushdown(tag);
1695 dcs->d_nxt->d_nedecl = 1;
1696 } else if (semi || decl) {
1697 dcs->d_nxt->d_nedecl = 1;
1698 }
1699 }
1700 return (tag);
1701 }
1702
1703 const char *
1704 scltoa(scl_t sc)
1705 {
1706 const char *s;
1707
1708 switch (sc) {
1709 case EXTERN: s = "extern"; break;
1710 case STATIC: s = "static"; break;
1711 case AUTO: s = "auto"; break;
1712 case REG: s = "register"; break;
1713 case TYPEDEF: s = "typedef"; break;
1714 case STRTAG: s = "struct"; break;
1715 case UNIONTAG: s = "union"; break;
1716 case ENUMTAG: s = "enum"; break;
1717 default: lerror("tagttoa()");
1718 }
1719 return (s);
1720 }
1721
1722 /*
1723 * Completes the type of a tag in a struct/union/enum declaration.
1724 * tp points to the type of the, tag, fmem to the list of members/enums.
1725 */
1726 type_t *
1727 compltag(type_t *tp, sym_t *fmem)
1728 {
1729 tspec_t t;
1730 str_t *sp;
1731 int n;
1732 sym_t *mem;
1733
1734 /* from now a complete type */
1735 setcompl(tp, 0);
1736
1737 if ((t = tp->t_tspec) != ENUM) {
1738 align(dcs->d_stralign, 0);
1739 sp = tp->t_str;
1740 sp->align = dcs->d_stralign;
1741 sp->size = dcs->d_offset;
1742 sp->memb = fmem;
1743 if (sp->size == 0) {
1744 /* zero sized %s */
1745 (void)gnuism(47, ttab[t].tt_name);
1746 } else {
1747 n = 0;
1748 for (mem = fmem; mem != NULL; mem = mem->s_nxt) {
1749 if (mem->s_name != unnamed)
1750 n++;
1751 }
1752 if (n == 0) {
1753 /* %s has no named members */
1754 warning(65,
1755 t == STRUCT ? "structure" : "union");
1756 }
1757 }
1758 } else {
1759 tp->t_enum->elem = fmem;
1760 }
1761 return (tp);
1762 }
1763
1764 /*
1765 * Processes the name of an enumerator in en enum declaration.
1766 *
1767 * sym points to the enumerator
1768 * val is the value of the enumerator
1769 * impl is 1 if the value of the enumerator was not explicit specified.
1770 */
1771 sym_t *
1772 ename(sym_t *sym, int val, int impl)
1773 {
1774
1775 if (sym->s_scl) {
1776 if (sym->s_blklev == blklev) {
1777 /* no hflag, because this is illegal!!! */
1778 if (sym->s_arg) {
1779 /* enumeration constant hides parameter: %s */
1780 warning(57, sym->s_name);
1781 } else {
1782 /* redeclaration of %s */
1783 error(27, sym->s_name);
1784 /*
1785 * inside blocks it should not too complicated
1786 * to find the position of the previous
1787 * declaration
1788 */
1789 if (blklev == 0)
1790 prevdecl(-1, sym);
1791 }
1792 } else {
1793 if (hflag)
1794 /* redefinition hides earlier one: %s */
1795 warning(43, sym->s_name);
1796 }
1797 sym = pushdown(sym);
1798 }
1799 sym->s_scl = ENUMCON;
1800 sym->s_type = dcs->d_tagtyp;
1801 sym->s_value.v_tspec = INT;
1802 sym->s_value.v_quad = val;
1803 if (impl && val - 1 == INT_MAX) {
1804 /* overflow in enumeration values: %s */
1805 warning(48, sym->s_name);
1806 }
1807 enumval = val + 1;
1808 return (sym);
1809 }
1810
1811 /*
1812 * Process a single external declarator.
1813 */
1814 void
1815 decl1ext(sym_t *dsym, int initflg)
1816 {
1817 int warn, rval, redec;
1818 sym_t *rdsym;
1819
1820 chkfdef(dsym, 1);
1821
1822 chktyp(dsym);
1823
1824 if (initflg && !(initerr = chkinit(dsym)))
1825 dsym->s_def = DEF;
1826
1827 /*
1828 * Declarations of functions are marked as "tentative" in dname().
1829 * This is wrong because there are no tentative function
1830 * definitions.
1831 */
1832 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1833 dsym->s_def = DECL;
1834
1835 if (dcs->d_inline) {
1836 if (dsym->s_type->t_tspec == FUNC) {
1837 dsym->s_inline = 1;
1838 } else {
1839 /* variable declared inline: %s */
1840 warning(268, dsym->s_name);
1841 }
1842 }
1843
1844 /* Write the declaration into the output file */
1845 if (plibflg && llibflg &&
1846 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1847 /*
1848 * With both LINTLIBRARY and PROTOLIB the prototyp is
1849 * written as a function definition to the output file.
1850 */
1851 rval = dsym->s_type->t_subt->t_tspec != VOID;
1852 outfdef(dsym, &dsym->s_dpos, rval, 0, NULL);
1853 } else {
1854 outsym(dsym, dsym->s_scl, dsym->s_def);
1855 }
1856
1857 if ((rdsym = dcs->d_rdcsym) != NULL) {
1858
1859 /*
1860 * If the old symbol stems from a old style function definition
1861 * we have remembered the params in rdsmy->s_args and compare
1862 * them with the params of the prototype.
1863 */
1864 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1865 redec = chkosdef(rdsym, dsym);
1866 } else {
1867 redec = 0;
1868 }
1869
1870 if (!redec && !isredec(dsym, (warn = 0, &warn))) {
1871
1872 if (warn) {
1873 /* redeclaration of %s */
1874 (*(sflag ? error : warning))(27, dsym->s_name);
1875 prevdecl(-1, rdsym);
1876 }
1877
1878 /*
1879 * Overtake the rememberd params if the new symbol
1880 * is not a prototype.
1881 */
1882 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1883 dsym->s_osdef = rdsym->s_osdef;
1884 dsym->s_args = rdsym->s_args;
1885 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1886 }
1887
1888 /*
1889 * Remember the position of the declaration if the
1890 * old symbol was a prototype and the new is not.
1891 * Also remember the position if the old symbol
1892 * was defined and the new is not.
1893 */
1894 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1895 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1896 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1897 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1898 }
1899
1900 /*
1901 * Copy informations about usage of the name into
1902 * the new symbol.
1903 */
1904 cpuinfo(dsym, rdsym);
1905
1906 /* Once a name is defined, it remains defined. */
1907 if (rdsym->s_def == DEF)
1908 dsym->s_def = DEF;
1909
1910 /* once a function is inline, it remains inline */
1911 if (rdsym->s_inline)
1912 dsym->s_inline = 1;
1913
1914 compltyp(dsym, rdsym);
1915
1916 }
1917
1918 rmsym(rdsym);
1919 }
1920
1921 if (dsym->s_scl == TYPEDEF) {
1922 dsym->s_type = duptyp(dsym->s_type);
1923 dsym->s_type->t_typedef = 1;
1924 settdsym(dsym->s_type, dsym);
1925 }
1926
1927 }
1928
1929 /*
1930 * Copies informations about usage into a new symbol table entry of
1931 * the same symbol.
1932 */
1933 void
1934 cpuinfo(sym_t *sym, sym_t *rdsym)
1935 {
1936
1937 sym->s_spos = rdsym->s_spos;
1938 sym->s_upos = rdsym->s_upos;
1939 sym->s_set = rdsym->s_set;
1940 sym->s_used = rdsym->s_used;
1941 }
1942
1943 /*
1944 * Prints an error and returns 1 if a symbol is redeclared/redefined.
1945 * Otherwise returns 0 and, in some cases of minor problems, prints
1946 * a warning.
1947 */
1948 int
1949 isredec(sym_t *dsym, int *warn)
1950 {
1951 sym_t *rsym;
1952
1953 if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
1954 /* redeclaration of %s */
1955 error(27, dsym->s_name);
1956 prevdecl(-1, rsym);
1957 return (1);
1958 }
1959 if (rsym->s_scl == TYPEDEF) {
1960 /* typedef redeclared: %s */
1961 error(89, dsym->s_name);
1962 prevdecl(-1, rsym);
1963 return (1);
1964 }
1965 if (dsym->s_scl == TYPEDEF) {
1966 /* redeclaration of %s */
1967 error(27, dsym->s_name);
1968 prevdecl(-1, rsym);
1969 return (1);
1970 }
1971 if (rsym->s_def == DEF && dsym->s_def == DEF) {
1972 /* redefinition of %s */
1973 error(28, dsym->s_name);
1974 prevdecl(-1, rsym);
1975 return(1);
1976 }
1977 if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, warn)) {
1978 /* redeclaration of %s */
1979 error(27, dsym->s_name);
1980 prevdecl(-1, rsym);
1981 return(1);
1982 }
1983 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
1984 return(0);
1985 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
1986 return(0);
1987 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
1988 return(0);
1989 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
1990 /*
1991 * All cases except "int a = 1; static int a;" are catched
1992 * above with or without a warning
1993 */
1994 /* redeclaration of %s */
1995 error(27, dsym->s_name);
1996 prevdecl(-1, rsym);
1997 return(1);
1998 }
1999 if (rsym->s_scl == EXTERN) {
2000 /* previously declared extern, becomes static: %s */
2001 warning(29, dsym->s_name);
2002 prevdecl(-1, rsym);
2003 return(0);
2004 }
2005 /*
2006 * Now its on of:
2007 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2008 */
2009 /* redeclaration of %s; ANSI C requires "static" */
2010 if (sflag) {
2011 warning(30, dsym->s_name);
2012 prevdecl(-1, rsym);
2013 }
2014 dsym->s_scl = STATIC;
2015 return (0);
2016 }
2017
2018 /*
2019 * Checks if two types are compatible. Returns 0 if not, otherwise 1.
2020 *
2021 * ignqual ignore qualifiers of type; used for function params
2022 * promot promote left type; used for comparison of params of
2023 * old style function definitions with params of prototypes.
2024 * *warn set to 1 if an old style function declaration is not
2025 * compatible with a prototype
2026 */
2027 int
2028 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *warn)
2029 {
2030 tspec_t t;
2031
2032 while (tp1 != NULL && tp2 != NULL) {
2033
2034 t = tp1->t_tspec;
2035 if (promot) {
2036 if (t == FLOAT) {
2037 t = DOUBLE;
2038 } else if (t == CHAR || t == SCHAR) {
2039 t = INT;
2040 } else if (t == UCHAR) {
2041 t = tflag ? UINT : INT;
2042 } else if (t == SHORT) {
2043 t = INT;
2044 } else if (t == USHORT) {
2045 /* CONSTCOND */
2046 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
2047 }
2048 }
2049
2050 if (t != tp2->t_tspec)
2051 return (0);
2052
2053 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
2054 return (0);
2055
2056 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
2057 return (0);
2058
2059 if (t == STRUCT || t == UNION)
2060 return (tp1->t_str == tp2->t_str);
2061
2062 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2063 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2064 return (0);
2065 }
2066
2067 /* dont check prototypes for traditional */
2068 if (t == FUNC && !tflag) {
2069 if (tp1->t_proto && tp2->t_proto) {
2070 if (!eqargs(tp1, tp2, warn))
2071 return (0);
2072 } else if (tp1->t_proto) {
2073 if (!mnoarg(tp1, warn))
2074 return (0);
2075 } else if (tp2->t_proto) {
2076 if (!mnoarg(tp2, warn))
2077 return (0);
2078 }
2079 }
2080
2081 tp1 = tp1->t_subt;
2082 tp2 = tp2->t_subt;
2083 ignqual = promot = 0;
2084
2085 }
2086
2087 return (tp1 == tp2);
2088 }
2089
2090 /*
2091 * Compares the parameter types of two prototypes.
2092 */
2093 static int
2094 eqargs(type_t *tp1, type_t *tp2, int *warn)
2095 {
2096 sym_t *a1, *a2;
2097
2098 if (tp1->t_vararg != tp2->t_vararg)
2099 return (0);
2100
2101 a1 = tp1->t_args;
2102 a2 = tp2->t_args;
2103
2104 while (a1 != NULL && a2 != NULL) {
2105
2106 if (eqtype(a1->s_type, a2->s_type, 1, 0, warn) == 0)
2107 return (0);
2108
2109 a1 = a1->s_nxt;
2110 a2 = a2->s_nxt;
2111
2112 }
2113
2114 return (a1 == a2);
2115 }
2116
2117 /*
2118 * mnoarg() (matches functions with no argument type information)
2119 * returns 1 if all parameters of a prototype are compatible with
2120 * and old style function declaration.
2121 * This is the case if following conditions are met:
2122 * 1. the prototype must have a fixed number of parameters
2123 * 2. no parameter is of type float
2124 * 3. no parameter is converted to another type if integer promotion
2125 * is applied on it
2126 */
2127 static int
2128 mnoarg(type_t *tp, int *warn)
2129 {
2130 sym_t *arg;
2131 tspec_t t;
2132
2133 if (tp->t_vararg) {
2134 if (warn != NULL)
2135 *warn = 1;
2136 }
2137 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) {
2138 if ((t = arg->s_type->t_tspec) == FLOAT ||
2139 t == CHAR || t == SCHAR || t == UCHAR ||
2140 t == SHORT || t == USHORT) {
2141 if (warn != NULL)
2142 *warn = 1;
2143 }
2144 }
2145 return (1);
2146 }
2147
2148 /*
2149 * Compares a prototype declaration with the remembered arguments of
2150 * a previous old style function definition.
2151 */
2152 static int
2153 chkosdef(sym_t *rdsym, sym_t *dsym)
2154 {
2155 sym_t *args, *pargs, *arg, *parg;
2156 int narg, nparg, n;
2157 int warn, msg;
2158
2159 args = rdsym->s_args;
2160 pargs = dsym->s_type->t_args;
2161
2162 msg = 0;
2163
2164 narg = nparg = 0;
2165 for (arg = args; arg != NULL; arg = arg->s_nxt)
2166 narg++;
2167 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2168 nparg++;
2169 if (narg != nparg) {
2170 /* prototype does not match old-style definition */
2171 error(63);
2172 msg = 1;
2173 goto end;
2174 }
2175
2176 arg = args;
2177 parg = pargs;
2178 n = 1;
2179 while (narg--) {
2180 warn = 0;
2181 /*
2182 * If it does not match due to promotion and sflag is
2183 * not set we print only a warning.
2184 */
2185 if (!eqtype(arg->s_type, parg->s_type, 1, 1, &warn) || warn) {
2186 /* prototype does not match old-style def., arg #%d */
2187 error(299, n);
2188 msg = 1;
2189 }
2190 arg = arg->s_nxt;
2191 parg = parg->s_nxt;
2192 n++;
2193 }
2194
2195 end:
2196 if (msg)
2197 /* old style definition */
2198 prevdecl(300, rdsym);
2199
2200 return (msg);
2201 }
2202
2203 /*
2204 * Complets a type by copying the dimension and prototype information
2205 * from a second compatible type.
2206 *
2207 * Following lines are legal:
2208 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2209 * "typedef ft(); ft f; f(int); ft g; g(long);"
2210 * This means that, if a type is completed, the type structure must
2211 * be duplicated.
2212 */
2213 void
2214 compltyp(sym_t *dsym, sym_t *ssym)
2215 {
2216 type_t **dstp, *src;
2217 type_t *dst;
2218
2219 dstp = &dsym->s_type;
2220 src = ssym->s_type;
2221
2222 while ((dst = *dstp) != NULL) {
2223 if (src == NULL || dst->t_tspec != src->t_tspec)
2224 lerror("compltyp() 1");
2225 if (dst->t_tspec == ARRAY) {
2226 if (dst->t_dim == 0 && src->t_dim != 0) {
2227 *dstp = dst = duptyp(dst);
2228 dst->t_dim = src->t_dim;
2229 /* now a complete Typ */
2230 setcompl(dst, 0);
2231 }
2232 } else if (dst->t_tspec == FUNC) {
2233 if (!dst->t_proto && src->t_proto) {
2234 *dstp = dst = duptyp(dst);
2235 dst->t_proto = 1;
2236 dst->t_args = src->t_args;
2237 }
2238 }
2239 dstp = &dst->t_subt;
2240 src = src->t_subt;
2241 }
2242 }
2243
2244 /*
2245 * Completes the declaration of a single argument.
2246 */
2247 sym_t *
2248 decl1arg(sym_t *sym, int initflg)
2249 {
2250 tspec_t t;
2251
2252 chkfdef(sym, 1);
2253
2254 chktyp(sym);
2255
2256 if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) {
2257 /* redeclaration of formal parameter %s */
2258 error(237, sym->s_name);
2259 rmsym(dcs->d_rdcsym);
2260 sym->s_arg = 1;
2261 }
2262
2263 if (!sym->s_arg) {
2264 /* declared argument %s is missing */
2265 error(53, sym->s_name);
2266 sym->s_arg = 1;
2267 }
2268
2269 if (initflg) {
2270 /* cannot initialize parameter: %s */
2271 error(52, sym->s_name);
2272 initerr = 1;
2273 }
2274
2275 if ((t = sym->s_type->t_tspec) == ARRAY) {
2276 sym->s_type = incref(sym->s_type->t_subt, PTR);
2277 } else if (t == FUNC) {
2278 if (tflag)
2279 /* a function is declared as an argument: %s */
2280 warning(50, sym->s_name);
2281 sym->s_type = incref(sym->s_type, PTR);
2282 } else if (t == FLOAT) {
2283 if (tflag)
2284 sym->s_type = gettyp(DOUBLE);
2285 }
2286
2287 if (dcs->d_inline)
2288 /* argument declared inline: %s */
2289 warning(269, sym->s_name);
2290
2291 /*
2292 * Arguments must have complete types. lengths() prints the needed
2293 * error messages (null dimension is impossible because arrays are
2294 * converted to pointers).
2295 */
2296 if (sym->s_type->t_tspec != VOID)
2297 (void)length(sym->s_type, sym->s_name);
2298
2299 setsflg(sym);
2300
2301 return (sym);
2302 }
2303
2304 /*
2305 * Does some checks for lint directives which apply to functions.
2306 * Processes arguments in old style function definitions which default
2307 * to int.
2308 * Checks compatiblility of old style function definition with previous
2309 * prototype.
2310 */
2311 void
2312 cluparg(void)
2313 {
2314 sym_t *args, *arg, *pargs, *parg;
2315 int narg, nparg, n, msg;
2316 tspec_t t;
2317
2318 args = funcsym->s_args;
2319 pargs = funcsym->s_type->t_args;
2320
2321 /* check for illegal combinations of lint directives */
2322 if (prflstrg != -1 && scflstrg != -1) {
2323 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2324 warning(289);
2325 prflstrg = scflstrg = -1;
2326 }
2327 if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) {
2328 /* dubious use of ** VARARGS ** with ** %s ** */
2329 warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2330 nvararg = -1;
2331 }
2332
2333 /*
2334 * check if the argument of a lint directive is compatible with the
2335 * number of arguments.
2336 */
2337 narg = 0;
2338 for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_nxt)
2339 narg++;
2340 if (nargusg > narg) {
2341 /* argument number mismatch with directive: ** %s ** */
2342 warning(283, "ARGSUSED");
2343 nargusg = 0;
2344 }
2345 if (nvararg > narg) {
2346 /* argument number mismatch with directive: ** %s ** */
2347 warning(283, "VARARGS");
2348 nvararg = 0;
2349 }
2350 if (prflstrg > narg) {
2351 /* argument number mismatch with directive: ** %s ** */
2352 warning(283, "PRINTFLIKE");
2353 prflstrg = -1;
2354 } else if (prflstrg == 0) {
2355 prflstrg = -1;
2356 }
2357 if (scflstrg > narg) {
2358 /* argument number mismatch with directive: ** %s ** */
2359 warning(283, "SCANFLIKE");
2360 scflstrg = -1;
2361 } else if (scflstrg == 0) {
2362 scflstrg = -1;
2363 }
2364 if (prflstrg != -1 || scflstrg != -1) {
2365 narg = prflstrg != -1 ? prflstrg : scflstrg;
2366 arg = dcs->d_fargs;
2367 for (n = 1; n < narg; n++)
2368 arg = arg->s_nxt;
2369 if (arg->s_type->t_tspec != PTR ||
2370 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2371 t != UCHAR && t != SCHAR)) {
2372 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2373 warning(293, narg);
2374 prflstrg = scflstrg = -1;
2375 }
2376 }
2377
2378 /*
2379 * print a warning for each argument off an old style function
2380 * definition which defaults to int
2381 */
2382 for (arg = args; arg != NULL; arg = arg->s_nxt) {
2383 if (arg->s_defarg) {
2384 /* argument type defaults to int: %s */
2385 warning(32, arg->s_name);
2386 arg->s_defarg = 0;
2387 setsflg(arg);
2388 }
2389 }
2390
2391 /*
2392 * If this is an old style function definition and a prototyp
2393 * exists, compare the types of arguments.
2394 */
2395 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2396 /*
2397 * If the number of arguments does not macht, we need not
2398 * continue.
2399 */
2400 narg = nparg = 0;
2401 msg = 0;
2402 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2403 nparg++;
2404 for (arg = args; arg != NULL; arg = arg->s_nxt)
2405 narg++;
2406 if (narg != nparg) {
2407 /* parameter mismatch: %d declared, %d defined */
2408 error(51, nparg, narg);
2409 msg = 1;
2410 } else {
2411 parg = pargs;
2412 arg = args;
2413 while (narg--) {
2414 msg |= chkptdecl(arg, parg);
2415 parg = parg->s_nxt;
2416 arg = arg->s_nxt;
2417 }
2418 }
2419 if (msg)
2420 /* prototype declaration */
2421 prevdecl(285, dcs->d_rdcsym);
2422
2423 /* from now the prototype is valid */
2424 funcsym->s_osdef = 0;
2425 funcsym->s_args = NULL;
2426
2427 }
2428
2429 }
2430
2431 /*
2432 * Checks compatibility of an old style function definition with a previous
2433 * prototype declaration.
2434 * Returns 1 if the position of the previous declaration should be reported.
2435 */
2436 static int
2437 chkptdecl(sym_t *arg, sym_t *parg)
2438 {
2439 type_t *tp, *ptp;
2440 int warn, msg;
2441
2442 tp = arg->s_type;
2443 ptp = parg->s_type;
2444
2445 msg = 0;
2446 warn = 0;
2447
2448 if (!eqtype(tp, ptp, 1, 1, &warn)) {
2449 if (eqtype(tp, ptp, 1, 0, &warn)) {
2450 /* type does not match prototype: %s */
2451 msg = gnuism(58, arg->s_name);
2452 } else {
2453 /* type does not match prototype: %s */
2454 error(58, arg->s_name);
2455 msg = 1;
2456 }
2457 } else if (warn) {
2458 /* type does not match prototype: %s */
2459 (*(sflag ? error : warning))(58, arg->s_name);
2460 msg = 1;
2461 }
2462
2463 return (msg);
2464 }
2465
2466 /*
2467 * Completes a single local declaration/definition.
2468 */
2469 void
2470 decl1loc(sym_t *dsym, int initflg)
2471 {
2472
2473 /* Correct a mistake done in dname(). */
2474 if (dsym->s_type->t_tspec == FUNC) {
2475 dsym->s_def = DECL;
2476 if (dcs->d_scl == NOSCL)
2477 dsym->s_scl = EXTERN;
2478 }
2479
2480 if (dsym->s_type->t_tspec == FUNC) {
2481 if (dsym->s_scl == STATIC) {
2482 /* dubious static function at block level: %s */
2483 warning(93, dsym->s_name);
2484 dsym->s_scl = EXTERN;
2485 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2486 /* function has illegal storage class: %s */
2487 error(94, dsym->s_name);
2488 dsym->s_scl = EXTERN;
2489 }
2490 }
2491
2492 /*
2493 * functions may be declared inline at local scope, although
2494 * this has no effect for a later definition of the same
2495 * function.
2496 * XXX it should have an effect if tflag is set. this would
2497 * also be the way gcc behaves.
2498 */
2499 if (dcs->d_inline) {
2500 if (dsym->s_type->t_tspec == FUNC) {
2501 dsym->s_inline = 1;
2502 } else {
2503 /* variable declared inline: %s */
2504 warning(268, dsym->s_name);
2505 }
2506 }
2507
2508 chkfdef(dsym, 1);
2509
2510 chktyp(dsym);
2511
2512 if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN)
2513 ledecl(dsym);
2514
2515 if (dsym->s_scl == EXTERN) {
2516 /*
2517 * XXX wenn die statische Variable auf Ebene 0 erst
2518 * spaeter definiert wird, haben wir die Brille auf.
2519 */
2520 if (dsym->s_xsym == NULL) {
2521 outsym(dsym, EXTERN, dsym->s_def);
2522 } else {
2523 outsym(dsym, dsym->s_xsym->s_scl, dsym->s_def);
2524 }
2525 }
2526
2527 if (dcs->d_rdcsym != NULL) {
2528
2529 if (dcs->d_rdcsym->s_blklev == 0) {
2530
2531 switch (dsym->s_scl) {
2532 case AUTO:
2533 /* automatic hides external declaration: %s */
2534 if (hflag)
2535 warning(86, dsym->s_name);
2536 break;
2537 case STATIC:
2538 /* static hides external declaration: %s */
2539 if (hflag)
2540 warning(87, dsym->s_name);
2541 break;
2542 case TYPEDEF:
2543 /* typedef hides external declaration: %s */
2544 if (hflag)
2545 warning(88, dsym->s_name);
2546 break;
2547 case EXTERN:
2548 /*
2549 * Warnings and errors are printed in ledecl()
2550 */
2551 break;
2552 default:
2553 lerror("decl1loc() 1");
2554 }
2555
2556 } else if (dcs->d_rdcsym->s_blklev == blklev) {
2557
2558 /* no hflag, because its illegal! */
2559 if (dcs->d_rdcsym->s_arg) {
2560 /*
2561 * if !tflag, a "redeclaration of %s" error
2562 * is produced below
2563 */
2564 if (tflag) {
2565 if (hflag)
2566 /* decl. hides parameter: %s */
2567 warning(91, dsym->s_name);
2568 rmsym(dcs->d_rdcsym);
2569 }
2570 }
2571
2572 } else if (dcs->d_rdcsym->s_blklev < blklev) {
2573
2574 if (hflag)
2575 /* declaration hides earlier one: %s */
2576 warning(95, dsym->s_name);
2577
2578 }
2579
2580 if (dcs->d_rdcsym->s_blklev == blklev) {
2581
2582 /* redeclaration of %s */
2583 error(27, dsym->s_name);
2584 rmsym(dcs->d_rdcsym);
2585
2586 }
2587
2588 }
2589
2590 if (initflg && !(initerr = chkinit(dsym))) {
2591 dsym->s_def = DEF;
2592 setsflg(dsym);
2593 }
2594
2595 if (dsym->s_scl == TYPEDEF) {
2596 dsym->s_type = duptyp(dsym->s_type);
2597 dsym->s_type->t_typedef = 1;
2598 settdsym(dsym->s_type, dsym);
2599 }
2600
2601 /*
2602 * Before we can check the size we must wait for a initialisation
2603 * which may follow.
2604 */
2605 }
2606
2607 /*
2608 * Processes (re)declarations of external Symbols inside blocks.
2609 */
2610 static void
2611 ledecl(sym_t *dsym)
2612 {
2613 int eqt, warn;
2614 sym_t *esym;
2615
2616 /* look for a symbol with the same name */
2617 esym = dcs->d_rdcsym;
2618 while (esym != NULL && esym->s_blklev != 0) {
2619 while ((esym = esym->s_link) != NULL) {
2620 if (esym->s_kind != FVFT)
2621 continue;
2622 if (strcmp(dsym->s_name, esym->s_name) == 0)
2623 break;
2624 }
2625 }
2626 if (esym == NULL)
2627 return;
2628 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2629 /* gcc accepts this without a warning, pcc prints an error. */
2630 /* redeclaration of %s */
2631 warning(27, dsym->s_name);
2632 prevdecl(-1, esym);
2633 return;
2634 }
2635
2636 warn = 0;
2637 eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &warn);
2638
2639 if (!eqt || warn) {
2640 if (esym->s_scl == EXTERN) {
2641 /* inconsistent redeclaration of extern: %s */
2642 warning(90, dsym->s_name);
2643 prevdecl(-1, esym);
2644 } else {
2645 /* inconsistent redeclaration of static: %s */
2646 warning(92, dsym->s_name);
2647 prevdecl(-1, esym);
2648 }
2649 }
2650
2651 if (eqt) {
2652 /*
2653 * Remember the external symbol so we can update usage
2654 * information at the end of the block.
2655 */
2656 dsym->s_xsym = esym;
2657 }
2658 }
2659
2660 /*
2661 * Print an error or a warning if the symbol cant be initialized due
2662 * to type/storage class. Returnvalue is 1 if an error has been
2663 * detected.
2664 */
2665 static int
2666 chkinit(sym_t *sym)
2667 {
2668 int err;
2669
2670 err = 0;
2671
2672 if (sym->s_type->t_tspec == FUNC) {
2673 /* cannot initialize function: %s */
2674 error(24, sym->s_name);
2675 err = 1;
2676 } else if (sym->s_scl == TYPEDEF) {
2677 /* cannot initialize typedef: %s */
2678 error(25, sym->s_name);
2679 err = 1;
2680 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2681 /* cannot initialize "extern" declaration: %s */
2682 if (dcs->d_ctx == EXTERN) {
2683 warning(26, sym->s_name);
2684 } else {
2685 error(26, sym->s_name);
2686 err = 1;
2687 }
2688 }
2689
2690 return (err);
2691 }
2692
2693 /*
2694 * Create a symbole for an abstract declaration.
2695 */
2696 sym_t *
2697 aname(void)
2698 {
2699 sym_t *sym;
2700
2701 if (dcs->d_ctx != ABSTRACT && dcs->d_ctx != PARG)
2702 lerror("aname()");
2703
2704 sym = getblk(sizeof (sym_t));
2705
2706 sym->s_name = unnamed;
2707 sym->s_def = DEF;
2708 sym->s_scl = ABSTRACT;
2709 sym->s_blklev = -1;
2710
2711 if (dcs->d_ctx == PARG)
2712 sym->s_arg = 1;
2713
2714 sym->s_type = dcs->d_type;
2715 dcs->d_rdcsym = NULL;
2716 dcs->d_vararg = 0;
2717
2718 return (sym);
2719 }
2720
2721 /*
2722 * Removes anything which has nothing to do on global level.
2723 */
2724 void
2725 globclup(void)
2726 {
2727
2728 while (dcs->d_nxt != NULL)
2729 popdecl();
2730
2731 cleanup();
2732 blklev = 0;
2733 mblklev = 0;
2734
2735 /*
2736 * remove all informations about pending lint directives without
2737 * warnings.
2738 */
2739 glclup(1);
2740 }
2741
2742 /*
2743 * Process an abstract type declaration
2744 */
2745 sym_t *
2746 decl1abs(sym_t *sym)
2747 {
2748
2749 chkfdef(sym, 1);
2750 chktyp(sym);
2751 return (sym);
2752 }
2753
2754 /*
2755 * Checks size after declarations of variables and their initialisation.
2756 */
2757 void
2758 chksz(sym_t *dsym)
2759 {
2760
2761 /*
2762 * check size only for symbols which are defined and no function and
2763 * not typedef name
2764 */
2765 if (dsym->s_def != DEF)
2766 return;
2767 if (dsym->s_scl == TYPEDEF)
2768 return;
2769 if (dsym->s_type->t_tspec == FUNC)
2770 return;
2771
2772 if (length(dsym->s_type, dsym->s_name) == 0 &&
2773 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2774 /* empty array declaration: %s */
2775 if (tflag) {
2776 warning(190, dsym->s_name);
2777 } else {
2778 error(190, dsym->s_name);
2779 }
2780 }
2781 }
2782
2783 /*
2784 * Mark an object as set if it is not already
2785 */
2786 void
2787 setsflg(sym_t *sym)
2788 {
2789
2790 if (!sym->s_set) {
2791 sym->s_set = 1;
2792 UNIQUE_CURR_POS(sym->s_spos);
2793 }
2794 }
2795
2796 /*
2797 * Mark an object as used if it is not already
2798 */
2799 void
2800 setuflg(sym_t *sym, int fcall, int szof)
2801 {
2802
2803 if (!sym->s_used) {
2804 sym->s_used = 1;
2805 UNIQUE_CURR_POS(sym->s_upos);
2806 }
2807 /*
2808 * for function calls another record is written
2809 *
2810 * XXX Should symbols used in sizeof() treated as used or not?
2811 * Probably not, because there is no sense to declare an
2812 * external variable only to get their size.
2813 */
2814 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2815 outusg(sym);
2816 }
2817
2818 /*
2819 * Prints warnings for a list of variables and labels (concatenated
2820 * with s_dlnxt) if these are not used or only set.
2821 */
2822 void
2823 chkusage(dinfo_t *di)
2824 {
2825 sym_t *sym;
2826 int mknowarn;
2827
2828 /* for this warnings LINTED has no effect */
2829 mknowarn = nowarn;
2830 nowarn = 0;
2831
2832 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
2833 chkusg1(di->d_asm, sym);
2834
2835 nowarn = mknowarn;
2836 }
2837
2838 /*
2839 * Prints a warning for a single variable or label if it is not used or
2840 * only set.
2841 */
2842 void
2843 chkusg1(int novar, sym_t *sym)
2844 {
2845 pos_t cpos;
2846
2847 if (sym->s_blklev == -1)
2848 return;
2849
2850 STRUCT_ASSIGN(cpos, curr_pos);
2851
2852 if (sym->s_kind == FVFT) {
2853 if (sym->s_arg) {
2854 chkausg(novar, sym);
2855 } else {
2856 chkvusg(novar, sym);
2857 }
2858 } else if (sym->s_kind == FLAB) {
2859 chklusg(sym);
2860 } else if (sym->s_kind == FTAG) {
2861 chktusg(sym);
2862 }
2863
2864 STRUCT_ASSIGN(curr_pos, cpos);
2865 }
2866
2867 static void
2868 chkausg(int novar, sym_t *arg)
2869 {
2870
2871 if (!arg->s_set)
2872 lerror("chkausg() 1");
2873
2874 if (novar)
2875 return;
2876
2877 if (!arg->s_used && vflag) {
2878 STRUCT_ASSIGN(curr_pos, arg->s_dpos);
2879 /* argument %s unused in function %s */
2880 warning(231, arg->s_name, funcsym->s_name);
2881 }
2882 }
2883
2884 static void
2885 chkvusg(int novar, sym_t *sym)
2886 {
2887 scl_t sc;
2888 sym_t *xsym;
2889
2890 if (blklev == 0 || sym->s_blklev == 0)
2891 lerror("chkvusg() 1");
2892
2893 /* errors in expressions easily cause lots of these warnings */
2894 if (nerr != 0)
2895 return;
2896
2897 /*
2898 * XXX Only variables are checkd, although types should
2899 * probably also be checked
2900 */
2901 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
2902 sc != AUTO && sc != REG) {
2903 return;
2904 }
2905
2906 if (novar)
2907 return;
2908
2909 if (sc == EXTERN) {
2910 if (!sym->s_used && !sym->s_set) {
2911 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2912 /* %s unused in function %s */
2913 warning(192, sym->s_name, funcsym->s_name);
2914 }
2915 } else {
2916 if (sym->s_set && !sym->s_used) {
2917 STRUCT_ASSIGN(curr_pos, sym->s_spos);
2918 /* %s set but not used in function %s */
2919 warning(191, sym->s_name, funcsym->s_name);
2920 } else if (!sym->s_used) {
2921 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2922 /* %s unused in function %s */
2923 warning(192, sym->s_name, funcsym->s_name);
2924 }
2925 }
2926
2927 if (sc == EXTERN) {
2928 /*
2929 * information about usage is taken over into the symbol
2930 * tabel entry at level 0 if the symbol was locally declared
2931 * as an external symbol.
2932 *
2933 * XXX This is wrong for symbols declared static at level 0
2934 * if the usage information stems from sizeof(). This is
2935 * because symbols at level 0 only used in sizeof() are
2936 * considered to not be used.
2937 */
2938 if ((xsym = sym->s_xsym) != NULL) {
2939 if (sym->s_used && !xsym->s_used) {
2940 xsym->s_used = 1;
2941 STRUCT_ASSIGN(xsym->s_upos, sym->s_upos);
2942 }
2943 if (sym->s_set && !xsym->s_set) {
2944 xsym->s_set = 1;
2945 STRUCT_ASSIGN(xsym->s_spos, sym->s_spos);
2946 }
2947 }
2948 }
2949 }
2950
2951 static void
2952 chklusg(sym_t *lab)
2953 {
2954
2955 if (blklev != 1 || lab->s_blklev != 1)
2956 lerror("chklusg() 1");
2957
2958 if (lab->s_set && !lab->s_used) {
2959 STRUCT_ASSIGN(curr_pos, lab->s_spos);
2960 /* label %s unused in function %s */
2961 warning(192, lab->s_name, funcsym->s_name);
2962 } else if (!lab->s_set) {
2963 STRUCT_ASSIGN(curr_pos, lab->s_upos);
2964 /* undefined label %s */
2965 warning(23, lab->s_name);
2966 }
2967 }
2968
2969 static void
2970 chktusg(sym_t *sym)
2971 {
2972
2973 if (!incompl(sym->s_type))
2974 return;
2975
2976 /* complain alwasy about incomplet tags declared inside blocks */
2977 if (!zflag || dcs->d_ctx != EXTERN)
2978 return;
2979
2980 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2981 switch (sym->s_type->t_tspec) {
2982 case STRUCT:
2983 /* struct %s never defined */
2984 warning(233, sym->s_name);
2985 break;
2986 case UNION:
2987 /* union %s never defined */
2988 warning(234, sym->s_name);
2989 break;
2990 case ENUM:
2991 /* enum %s never defined */
2992 warning(235, sym->s_name);
2993 break;
2994 default:
2995 lerror("chktusg() 1");
2996 }
2997 }
2998
2999 /*
3000 * Called after the entire translation unit has been parsed.
3001 * Changes tentative definitions in definitions.
3002 * Performs some tests on global Symbols. Detected Problems are:
3003 * - defined variables of incomplete type
3004 * - constant variables which are not initialized
3005 * - static symbols which are never used
3006 */
3007 void
3008 chkglsyms(void)
3009 {
3010 sym_t *sym;
3011 pos_t cpos;
3012
3013 if (blklev != 0 || dcs->d_nxt != NULL)
3014 norecover();
3015
3016 STRUCT_ASSIGN(cpos, curr_pos);
3017
3018 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
3019 if (sym->s_blklev == -1)
3020 continue;
3021 if (sym->s_kind == FVFT) {
3022 chkglvar(sym);
3023 } else if (sym->s_kind == FTAG) {
3024 chktusg(sym);
3025 } else {
3026 if (sym->s_kind != FMOS)
3027 lerror("chkglsyms() 1");
3028 }
3029 }
3030
3031 STRUCT_ASSIGN(curr_pos, cpos);
3032 }
3033
3034 static void
3035 chkglvar(sym_t *sym)
3036 {
3037
3038 if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON)
3039 return;
3040
3041 if (sym->s_scl != EXTERN && sym->s_scl != STATIC)
3042 lerror("chkglvar() 1");
3043
3044 glchksz(sym);
3045
3046 if (sym->s_scl == STATIC) {
3047 if (sym->s_type->t_tspec == FUNC) {
3048 if (sym->s_used && sym->s_def != DEF) {
3049 STRUCT_ASSIGN(curr_pos, sym->s_upos);
3050 /* static func. called but not def.. */
3051 error(225, sym->s_name);
3052 }
3053 }
3054 if (!sym->s_used) {
3055 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3056 if (sym->s_type->t_tspec == FUNC) {
3057 if (sym->s_def == DEF) {
3058 if (!sym->s_inline)
3059 /* static function %s unused */
3060 warning(236, sym->s_name);
3061 } else {
3062 /* static function %s decl. but ... */
3063 warning(290, sym->s_name);
3064 }
3065 } else if (!sym->s_set) {
3066 /* static variable %s unused */
3067 warning(226, sym->s_name);
3068 } else {
3069 /* static variable %s set but not used */
3070 warning(307, sym->s_name);
3071 }
3072 }
3073 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
3074 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3075 /* const object %s should have initializer */
3076 warning(227, sym->s_name);
3077 }
3078 }
3079 }
3080
3081 static void
3082 glchksz(sym_t *sym)
3083 {
3084
3085 if (sym->s_def == TDEF) {
3086 if (sym->s_type->t_tspec == FUNC)
3087 /*
3088 * this can happen if an syntax error occurred
3089 * after a function declaration
3090 */
3091 return;
3092 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3093 if (length(sym->s_type, sym->s_name) == 0 &&
3094 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3095 /* empty array declaration: %s */
3096 if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3097 warning(190, sym->s_name);
3098 } else {
3099 error(190, sym->s_name);
3100 }
3101 }
3102 }
3103 }
3104
3105 /*
3106 * Prints information about location of previous definition/declaration.
3107 */
3108 void
3109 prevdecl(int msg, sym_t *psym)
3110 {
3111 pos_t cpos;
3112
3113 if (!rflag)
3114 return;
3115
3116 STRUCT_ASSIGN(cpos, curr_pos);
3117 STRUCT_ASSIGN(curr_pos, psym->s_dpos);
3118 if (msg != -1) {
3119 message(msg, psym->s_name);
3120 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3121 /* previous definition of %s */
3122 message(261, psym->s_name);
3123 } else {
3124 /* previous declaration of %s */
3125 message(260, psym->s_name);
3126 }
3127 STRUCT_ASSIGN(curr_pos, cpos);
3128 }
3129