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