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