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