Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.bin / xlint / lint1 / decl.c
blob94660c5f520a24bf6eda7259321452948b3abd30
1 /* $NetBSD: decl.c,v 1.50 2009/10/02 21:04:03 christos Exp $ */
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved.
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.
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.
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
39 #include <sys/cdefs.h>
40 #if defined(__RCSID) && !defined(lint)
41 __RCSID("$NetBSD: decl.c,v 1.50 2009/10/02 21:04:03 christos Exp $");
42 #endif
44 #include <sys/param.h>
45 #include <limits.h>
46 #include <stdlib.h>
47 #include <string.h>
49 #include "lint1.h"
51 const char *unnamed = "<unnamed>";
53 /* shared type structures for arithmtic types and void */
54 static type_t *typetab;
56 /* value of next enumerator during declaration of enum types */
57 int enumval;
60 * pointer to top element of a stack which contains informations local
61 * to nested declarations
63 dinfo_t *dcs;
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 *);
86 * initializes all global vars used in declarations
88 void
89 initdecl(void)
91 int i;
93 /* declaration stack */
94 dcs = xcalloc(1, sizeof (dinfo_t));
95 dcs->d_ctx = EXTERN;
96 dcs->d_ldlsym = &dcs->d_dlsyms;
98 /* type information and classification */
99 inittyp();
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;
126 * Next two are not real types. They are only used by the parser
127 * to return keywords "signed" and "unsigned"
129 typetab[SIGNED].t_tspec = SIGNED;
130 typetab[UNSIGN].t_tspec = UNSIGN;
134 * Returns a shared type structure vor arithmetic types and void.
136 * It's important do duplicate this structure (using duptyp() or tdupdyp())
137 * if it is to be modified (adding qualifiers or anything else).
139 type_t *
140 gettyp(tspec_t t)
143 return (&typetab[t]);
146 type_t *
147 duptyp(const type_t *tp)
149 type_t *ntp;
151 ntp = getblk(sizeof (type_t));
152 STRUCT_ASSIGN(*ntp, *tp);
153 return (ntp);
157 * Use tduptyp() instead of duptyp() inside expressions (if the
158 * allocated memory should be freed after the expr).
160 type_t *
161 tduptyp(const type_t *tp)
163 type_t *ntp;
165 ntp = tgetblk(sizeof (type_t));
166 STRUCT_ASSIGN(*ntp, *tp);
167 return (ntp);
171 * Returns 1 if the argument is void or an incomplete array,
172 * struct, union or enum type.
175 incompl(type_t *tp)
177 tspec_t t;
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);
188 return (0);
192 * Set the flag for (in)complete array, struct, union or enum
193 * types.
195 void
196 setcompl(type_t *tp, int ic)
198 tspec_t t;
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;
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.
216 void
217 addscl(scl_t sc)
220 if (sc == INLINE) {
221 if (dcs->d_inline)
222 /* duplicate '%s' */
223 warning(10, "inline");
224 dcs->d_inline = 1;
225 return;
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);
232 if (dcs->d_scl == NOSCL) {
233 dcs->d_scl = sc;
234 } else {
236 * multiple storage classes. An error will be reported in
237 * deftyp().
239 dcs->d_mscl = 1;
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.
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.
253 void
254 addtype(type_t *tp)
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) {
265 * something like "typedef int a; int a b;"
266 * This should not happen with current grammar.
268 LERROR("addtype()");
270 dcs->d_type = tp;
271 return;
274 t = tp->t_tspec;
276 if (t == STRUCT || t == UNION || t == ENUM) {
278 * something like "int struct a ..."
279 * struct/union/enum with anything else is not allowed
281 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
282 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) {
284 * remember that an error must be reported in
285 * deftyp().
287 dcs->d_terr = 1;
288 dcs->d_atyp = dcs->d_lmod = dcs->d_smod = NOTSPEC;
290 dcs->d_type = tp;
291 return;
294 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
296 * something like "struct a int"
297 * struct/union/enum with anything else is not allowed
299 dcs->d_terr = 1;
300 return;
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;
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");
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;
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)
333 * more than one "signed" and/or "unsigned"; print
334 * an error in deftyp()
336 dcs->d_terr = 1;
337 dcs->d_smod = t;
338 } else if (t == SHORT || t == LONG || t == QUAD) {
340 * remember specifiers "short", "long" and "long long" in
341 * dcs->d_lmod
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;
358 } else {
360 * remember specifiers "void", "char", "int",
361 * or "_Complex" int dcs->d_atyp
363 if (dcs->d_atyp != NOTSPEC)
364 /* more than one, print error in deftyp() */
365 dcs->d_terr = 1;
366 dcs->d_atyp = t;
371 * called if a list of declaration specifiers contains a typedef name
372 * and other specifiers (except struct, union, enum, typedef name)
374 static type_t *
375 tdeferr(type_t *td, tspec_t t)
377 tspec_t t2;
379 t2 = td->t_tspec;
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);
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);
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);
423 td = duptyp(td);
424 td->t_typedef = 1;
425 return (td);
427 break;
428 /* LINTED (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;
456 case NTSPEC: /* this value unused */
457 break;
460 /* Anything other is not accepted. */
462 dcs->d_terr = 1;
463 return (td);
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.
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.
475 static void
476 settdsym(type_t *tp, sym_t *sym)
478 tspec_t t;
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;
489 static void
490 setpackedsize(type_t *tp)
492 str_t *sp;
493 sym_t *mem;
494 char buf[256];
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;
508 len = ((len + INT_SIZE - 1) /
509 INT_SIZE) * INT_SIZE;
510 sp->size += len;
511 if (mem == NULL)
512 break;
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;
520 break;
521 default:
522 warning(326, "packed", tyname(buf, sizeof(buf), tp));
523 break;
527 void
528 addpacked(void)
530 if (dcs->d_type == NULL)
531 dcs->d_ispacked = 1;
532 else
533 setpackedsize(dcs->d_type);
537 * Remember a qualifier which is part of the declaration specifiers
538 * (and not the declarator) in the top element of the declaration stack.
539 * Also detect multiple qualifiers of the same kind.
541 * The remembered qualifier is used by deftyp() to construct the type
542 * for all declarators.
544 void
545 addqual(tqual_t q)
548 if (q == CONST) {
549 if (dcs->d_const) {
550 /* duplicate "%s" */
551 warning(10, "const");
553 dcs->d_const = 1;
554 } else {
555 if (q != VOLATILE)
556 LERROR("addqual()");
557 if (dcs->d_volatile) {
558 /* duplicate "%s" */
559 warning(10, "volatile");
561 dcs->d_volatile = 1;
566 * Go to the next declaration level (structs, nested structs, blocks,
567 * argument declaration lists ...)
569 void
570 pushdecl(scl_t sc)
572 dinfo_t *di;
574 /* put a new element on the declaration stack */
575 di = xcalloc(1, sizeof (dinfo_t));
576 di->d_nxt = dcs;
577 dcs = di;
578 di->d_ctx = sc;
579 di->d_ldlsym = &di->d_dlsyms;
580 if (dflag)
581 (void)printf("pushdecl(%p %d)\n", dcs, (int)sc);
586 * Go back to previous declaration level
588 void
589 popdecl(void)
591 dinfo_t *di;
593 if (dflag)
594 (void)printf("popdecl(%p %d)\n", dcs, (int)dcs->d_ctx);
596 if (dcs->d_nxt == NULL)
597 LERROR("popdecl()");
598 di = dcs;
599 dcs = di->d_nxt;
600 switch (di->d_ctx) {
601 case EXTERN:
602 /* there is nothing after external declarations */
603 LERROR("popdecl()");
604 /* NOTREACHED */
605 case MOS:
606 case MOU:
607 case ENUMCON:
609 * Symbols declared in (nested) structs or enums are
610 * part of the next level (they are removed from the
611 * symbol table if the symbols of the outher level are
612 * removed)
614 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
615 dcs->d_ldlsym = di->d_ldlsym;
616 break;
617 case ARG:
619 * All symbols in dcs->d_dlsyms are introduced in old style
620 * argument declarations (it's not clean, but possible).
621 * They are appended to the list of symbols declared in
622 * an old style argument identifier list or a new style
623 * parameter type list.
625 if (di->d_dlsyms != NULL) {
626 *di->d_ldlsym = dcs->d_fpsyms;
627 dcs->d_fpsyms = di->d_dlsyms;
629 break;
630 case ABSTRACT:
632 * casts and sizeof
633 * Append all symbols declared in the abstract declaration
634 * to the list of symbols declared in the surounding decl.
635 * or block.
636 * XXX I'm not sure whether they should be removed from the
637 * symbol table now or later.
639 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
640 dcs->d_ldlsym = di->d_ldlsym;
641 break;
642 case AUTO:
643 /* check usage of local vars */
644 chkusage(di);
645 /* FALLTHROUGH */
646 case PARG:
647 /* usage of arguments will be checked by funcend() */
648 rmsyms(di->d_dlsyms);
649 break;
650 default:
651 LERROR("popdecl()");
653 free(di);
657 * Set flag d_asm in all declaration stack elements up to the
658 * outermost one.
660 * This is used to mark compound statements which have, possibly in
661 * nested compound statements, asm statements. For these compound
662 * statements no warnings about unused or unitialized variables are
663 * printed.
665 * There is no need to clear d_asm in dinfo structs with context AUTO,
666 * because these structs are freed at the end of the compound statement.
667 * But it must be cleard in the outermost dinfo struct, which has
668 * context EXTERN. This could be done in clrtyp() and would work for
669 * C, but not for C++ (due to mixed statements and declarations). Thus
670 * we clear it in glclup(), which is used to do some cleanup after
671 * global declarations/definitions.
673 void
674 setasm(void)
676 dinfo_t *di;
678 for (di = dcs; di != NULL; di = di->d_nxt)
679 di->d_asm = 1;
683 * Clean all elements of the top element of declaration stack which
684 * will be used by the next declaration
686 void
687 clrtyp(void)
690 dcs->d_atyp = dcs->d_cmod = dcs->d_smod = dcs->d_lmod = NOTSPEC;
691 dcs->d_scl = NOSCL;
692 dcs->d_type = NULL;
693 dcs->d_const = dcs->d_volatile = 0;
694 dcs->d_inline = 0;
695 dcs->d_mscl = dcs->d_terr = 0;
696 dcs->d_nedecl = 0;
697 dcs->d_notyp = 0;
701 * Create a type structure from the informations gathered in
702 * the declaration stack.
703 * Complain about storage classes which are not possible in current
704 * context.
706 void
707 deftyp(void)
709 tspec_t t, s, l, c;
710 type_t *tp;
711 scl_t scl;
713 t = dcs->d_atyp; /* BOOL, CHAR, INT, COMPLEX, VOID */
714 s = dcs->d_smod; /* SIGNED, UNSIGNED */
715 l = dcs->d_lmod; /* SHORT, LONG, QUAD */
716 c = dcs->d_cmod; /* FLOAT, DOUBLE */
717 tp = dcs->d_type;
718 scl = dcs->d_scl;
720 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC &&
721 tp == NULL)
722 dcs->d_notyp = 1;
723 if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) &&
724 tp == NULL)
725 t = c;
727 if (tp != NULL && (t != NOTSPEC || s != NOTSPEC || l != NOTSPEC)) {
728 /* should never happen */
729 LERROR("deftyp()");
732 if (tp == NULL) {
733 switch (t) {
734 case BOOL:
735 break;
736 case NOTSPEC:
737 t = INT;
738 /* FALLTHROUGH */
739 case INT:
740 if (s == NOTSPEC)
741 s = SIGNED;
742 break;
743 case CHAR:
744 if (l != NOTSPEC) {
745 dcs->d_terr = 1;
746 l = NOTSPEC;
748 break;
749 case FLOAT:
750 if (l == LONG) {
751 l = NOTSPEC;
752 t = DOUBLE;
753 if (!tflag)
754 /* use 'double' instead of ... */
755 warning(6);
757 break;
758 case DOUBLE:
759 if (l == LONG) {
760 l = NOTSPEC;
761 t = LDOUBLE;
762 if (tflag)
763 /* 'long double' is illegal in ... */
764 warning(266);
766 break;
767 case DCOMPLEX:
768 if (l == LONG) {
769 l = NOTSPEC;
770 t = LCOMPLEX;
771 if (tflag)
772 /* 'long double' is illegal in ... */
773 warning(266);
775 break;
776 case VOID:
777 case FCOMPLEX:
778 case LCOMPLEX:
779 break;
780 default:
781 LERROR("deftyp()");
783 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) {
784 dcs->d_terr = 1;
785 l = s = NOTSPEC;
787 if (l != NOTSPEC)
788 t = l;
789 dcs->d_type = gettyp(mrgtspec(t, s));
792 if (dcs->d_mscl) {
793 /* only one storage class allowed */
794 error(7);
796 if (dcs->d_terr) {
797 /* illegal type combination */
798 error(4);
801 if (dcs->d_ctx == EXTERN) {
802 if (scl == REG || scl == AUTO) {
803 /* illegal storage class */
804 error(8);
805 scl = NOSCL;
807 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PARG) {
808 if (scl != NOSCL && scl != REG) {
809 /* only "register" valid ... */
810 error(9);
811 scl = NOSCL;
815 dcs->d_scl = scl;
817 if (dcs->d_const && dcs->d_type->t_const) {
818 if (!dcs->d_type->t_typedef)
819 LERROR("deftyp()");
820 /* typedef already qualified with "%s" */
821 warning(68, "const");
823 if (dcs->d_volatile && dcs->d_type->t_volatile) {
824 if (!dcs->d_type->t_typedef)
825 LERROR("deftyp()");
826 /* typedef already qualified with "%s" */
827 warning(68, "volatile");
830 if (dcs->d_const || dcs->d_volatile) {
831 dcs->d_type = duptyp(dcs->d_type);
832 dcs->d_type->t_const |= dcs->d_const;
833 dcs->d_type->t_volatile |= dcs->d_volatile;
838 * Merge type specifiers (char, ..., long long, signed, unsigned).
840 static tspec_t
841 mrgtspec(tspec_t t, tspec_t s)
844 if (s == SIGNED || s == UNSIGN) {
845 if (t == CHAR) {
846 t = s == SIGNED ? SCHAR : UCHAR;
847 } else if (t == SHORT) {
848 t = s == SIGNED ? SHORT : USHORT;
849 } else if (t == INT) {
850 t = s == SIGNED ? INT : UINT;
851 } else if (t == LONG) {
852 t = s == SIGNED ? LONG : ULONG;
853 } else if (t == QUAD) {
854 t = s == SIGNED ? QUAD : UQUAD;
858 return (t);
862 * Return the length of a type in bit.
864 * Printing a message if the outhermost dimension of an array is 0 must
865 * be done by the caller. All other problems are reported by length()
866 * if name is not NULL.
869 length(type_t *tp, const char *name)
871 int elem, elsz;
873 elem = 1;
874 while (tp && tp->t_tspec == ARRAY) {
875 elem *= tp->t_dim;
876 tp = tp->t_subt;
878 if (tp == NULL)
879 return -1;
881 switch (tp->t_tspec) {
882 case FUNC:
883 /* compiler takes size of function */
884 LERROR(msgs[12]);
885 /* NOTREACHED */
886 case STRUCT:
887 case UNION:
888 if (incompl(tp) && name != NULL) {
889 /* incomplete structure or union %s: %s */
890 error(31, tp->t_str->stag->s_name, name);
892 elsz = tp->t_str->size;
893 break;
894 case ENUM:
895 if (incompl(tp) && name != NULL) {
896 /* incomplete enum type: %s */
897 warning(13, name);
899 /* FALLTHROUGH */
900 default:
901 elsz = size(tp->t_tspec);
902 if (elsz <= 0)
903 LERROR("length()");
904 break;
906 return (elem * elsz);
910 * Get the alignment of the given Type in bits.
913 getbound(type_t *tp)
915 size_t a;
916 tspec_t t;
918 while (tp && tp->t_tspec == ARRAY)
919 tp = tp->t_subt;
921 if (tp == NULL)
922 return -1;
924 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
925 a = tp->t_str->align;
926 } else if (t == FUNC) {
927 /* compiler takes alignment of function */
928 error(14);
929 a = ALIGN(1) * CHAR_BIT;
930 } else {
931 if ((a = size(t)) == 0) {
932 a = CHAR_BIT;
933 } else if (a > ALIGN(1) * CHAR_BIT) {
934 a = ALIGN(1) * CHAR_BIT;
937 if (a < CHAR_BIT || a > ALIGN(1) * CHAR_BIT)
938 LERROR("getbound()");
939 return (a);
943 * Concatenate two lists of symbols by s_nxt. Used by declarations of
944 * struct/union/enum elements and parameters.
946 sym_t *
947 lnklst(sym_t *l1, sym_t *l2)
949 sym_t *l;
951 if ((l = l1) == NULL)
952 return (l2);
953 while (l1->s_nxt != NULL)
954 l1 = l1->s_nxt;
955 l1->s_nxt = l2;
956 return (l);
960 * Check if the type of the given symbol is valid and print an error
961 * message if it is not.
963 * Invalid types are:
964 * - arrays of incomlete types or functions
965 * - functions returning arrays or functions
966 * - void types other than type of function or pointer
968 void
969 chktyp(sym_t *sym)
971 tspec_t to, t;
972 type_t **tpp, *tp;
974 tpp = &sym->s_type;
975 to = NOTSPEC;
976 while ((tp = *tpp) != NULL) {
977 t = tp->t_tspec;
979 * If this is the type of an old style function definition,
980 * a better warning is printed in funcdef().
982 if (t == FUNC && !tp->t_proto &&
983 !(to == NOTSPEC && sym->s_osdef)) {
984 if (sflag && hflag)
985 /* function declaration is not a prototype */
986 warning(287);
988 if (to == FUNC) {
989 if (t == FUNC || t == ARRAY) {
990 /* function returns illegal type */
991 error(15);
992 if (t == FUNC) {
993 *tpp = incref(*tpp, PTR);
994 } else {
995 *tpp = incref((*tpp)->t_subt, PTR);
997 return;
998 } else if (tp->t_const || tp->t_volatile) {
999 if (sflag) { /* XXX oder better !tflag ? */
1000 /* function cannot return const... */
1001 warning(228);
1004 } if (to == ARRAY) {
1005 if (t == FUNC) {
1006 /* array of function is illegal */
1007 error(16);
1008 *tpp = gettyp(INT);
1009 return;
1010 } else if (t == ARRAY && tp->t_dim == 0) {
1011 /* null dimension */
1012 error(17);
1013 return;
1014 } else if (t == VOID) {
1015 /* illegal use of void */
1016 error(18);
1017 *tpp = gettyp(INT);
1018 #if 0 /* errors are produced by length() */
1019 } else if (incompl(tp)) {
1020 /* array of incomplete type */
1021 if (sflag) {
1022 error(301);
1023 } else {
1024 warning(301);
1026 #endif
1028 } else if (to == NOTSPEC && t == VOID) {
1029 if (dcs->d_ctx == PARG) {
1030 if (sym->s_scl != ABSTRACT) {
1031 if (sym->s_name == unnamed)
1032 LERROR("chktyp()");
1033 /* void param cannot have name: %s */
1034 error(61, sym->s_name);
1035 *tpp = gettyp(INT);
1037 } else if (dcs->d_ctx == ABSTRACT) {
1038 /* ok */
1039 } else if (sym->s_scl != TYPEDEF) {
1040 /* void type for %s */
1041 error(19, sym->s_name);
1042 *tpp = gettyp(INT);
1045 if (t == VOID && to != PTR) {
1046 if (tp->t_const || tp->t_volatile) {
1047 /* inappropriate qualifiers with "void" */
1048 warning(69);
1049 tp->t_const = tp->t_volatile = 0;
1052 tpp = &tp->t_subt;
1053 to = t;
1058 * Process the declarator of a struct/union element.
1060 sym_t *
1061 decl1str(sym_t *dsym)
1063 type_t *tp;
1064 tspec_t t;
1065 int sz, len;
1066 int o = 0; /* Appease gcc */
1067 scl_t sc;
1069 if ((sc = dsym->s_scl) != MOS && sc != MOU)
1070 LERROR("decl1str()");
1072 if (dcs->d_rdcsym != NULL) {
1073 if ((sc = dcs->d_rdcsym->s_scl) != MOS && sc != MOU)
1074 /* should be ensured by storesym() */
1075 LERROR("decl1str()");
1076 if (dsym->s_styp == dcs->d_rdcsym->s_styp) {
1077 /* duplicate member name: %s */
1078 error(33, dsym->s_name);
1079 rmsym(dcs->d_rdcsym);
1083 chktyp(dsym);
1085 t = (tp = dsym->s_type)->t_tspec;
1087 if (dsym->s_field) {
1089 * bit field
1091 * only unsigned und signed int are protable bit-field types
1092 *(at least in ANSI C, in traditional C only unsigned int)
1094 if (t == CHAR || t == UCHAR || t == SCHAR ||
1095 t == SHORT || t == USHORT || t == ENUM) {
1096 if (bitfieldtype_ok == 0) {
1097 if (sflag) {
1098 char buf[64];
1100 * bit-field type '%s' invalid in
1101 * ANSI C
1103 warning(273,
1104 tyname(buf, sizeof(buf), tp));
1105 } else if (pflag) {
1106 /* nonportable bit-field type */
1107 warning(34);
1110 } else if (t == INT && dcs->d_smod == NOTSPEC) {
1111 if (pflag && bitfieldtype_ok == 0) {
1112 /* nonportable bit-field type */
1113 warning(34);
1115 } else if (t != INT && t != UINT) {
1117 * Non-integer types are always illegal for
1118 * bitfields, regardless of BITFIELDTYPE.
1119 * Integer types not dealt with above are
1120 * okay only if BITFIELDTYPE is in effect.
1122 if (bitfieldtype_ok == 0 || isityp(t) == 0) {
1123 /* illegal bit-field type */
1124 error(35);
1125 sz = tp->t_flen;
1126 dsym->s_type = tp = duptyp(gettyp(t = INT));
1127 if ((tp->t_flen = sz) > size(t))
1128 tp->t_flen = size(t);
1131 if ((len = tp->t_flen) < 0 || len > (ssize_t)size(t)) {
1132 /* illegal bit-field size */
1133 error(36);
1134 tp->t_flen = size(t);
1135 } else if (len == 0 && dsym->s_name != unnamed) {
1136 /* zero size bit-field */
1137 error(37);
1138 tp->t_flen = size(t);
1140 if (dsym->s_scl == MOU) {
1141 /* illegal use of bit-field */
1142 error(41);
1143 dsym->s_type->t_isfield = 0;
1144 dsym->s_field = 0;
1146 } else if (t == FUNC) {
1147 /* function illegal in structure or union */
1148 error(38);
1149 dsym->s_type = tp = incref(tp, t = PTR);
1153 * bit-fields of length 0 are not warned about because length()
1154 * does not return the length of the bit-field but the length
1155 * of the type the bit-field is packed in (its ok)
1157 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1158 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1159 /* illegal zero sized structure member: %s */
1160 c99ism(39, dsym->s_name);
1164 if (dcs->d_ctx == MOU) {
1165 o = dcs->d_offset;
1166 dcs->d_offset = 0;
1168 if (dsym->s_field) {
1169 align(getbound(tp), tp->t_flen);
1170 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t);
1171 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1172 dcs->d_offset += tp->t_flen;
1173 } else {
1174 align(getbound(tp), 0);
1175 dsym->s_value.v_quad = dcs->d_offset;
1176 dcs->d_offset += sz;
1178 if (dcs->d_ctx == MOU) {
1179 if (o > dcs->d_offset)
1180 dcs->d_offset = o;
1183 chkfdef(dsym, 0);
1186 * Clear the BITFIELDTYPE indicator after processing each
1187 * structure element.
1189 bitfieldtype_ok = 0;
1191 return (dsym);
1195 * Aligns next structure element as required.
1197 * al contains the required alignment, len the length of a bit-field.
1199 static void
1200 align(int al, int len)
1202 int no;
1205 * The alignment of the current element becomes the alignment of
1206 * the struct/union if it is larger than the current alignment
1207 * of the struct/union.
1209 if (al > dcs->d_stralign)
1210 dcs->d_stralign = al;
1212 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1213 if (len == 0 || dcs->d_offset + len > no)
1214 dcs->d_offset = no;
1218 * Remember the width of the field in its type structure.
1220 sym_t *
1221 bitfield(sym_t *dsym, int len)
1224 if (dsym == NULL) {
1225 dsym = getblk(sizeof (sym_t));
1226 dsym->s_name = unnamed;
1227 dsym->s_kind = FMOS;
1228 dsym->s_scl = MOS;
1229 dsym->s_type = gettyp(UINT);
1230 dsym->s_blklev = -1;
1232 dsym->s_type = duptyp(dsym->s_type);
1233 dsym->s_type->t_isfield = 1;
1234 dsym->s_type->t_flen = len;
1235 dsym->s_field = 1;
1236 return (dsym);
1240 * Collect informations about a sequence of asterisks and qualifiers
1241 * in a list of type pqinf_t.
1242 * Qualifiers refer always to the left asterisk. The rightmost asterisk
1243 * will be at the top of the list.
1245 pqinf_t *
1246 mergepq(pqinf_t *p1, pqinf_t *p2)
1248 pqinf_t *p;
1250 if (p2->p_pcnt != 0) {
1251 /* left '*' at the end of the list */
1252 for (p = p2; p->p_nxt != NULL; p = p->p_nxt)
1253 continue;
1254 p->p_nxt = p1;
1255 return (p2);
1256 } else {
1257 if (p2->p_const) {
1258 if (p1->p_const) {
1259 /* duplicate %s */
1260 warning(10, "const");
1262 p1->p_const = 1;
1264 if (p2->p_volatile) {
1265 if (p1->p_volatile) {
1266 /* duplicate %s */
1267 warning(10, "volatile");
1269 p1->p_volatile = 1;
1271 free(p2);
1272 return (p1);
1277 * Followint 3 functions extend the type of a declarator with
1278 * pointer, function and array types.
1280 * The current type is the Type built by deftyp() (dcs->d_type) and
1281 * pointer, function and array types already added for this
1282 * declarator. The new type extension is inserted between both.
1284 sym_t *
1285 addptr(sym_t *decl, pqinf_t *pi)
1287 type_t **tpp, *tp;
1288 pqinf_t *npi;
1290 tpp = &decl->s_type;
1291 while (*tpp && *tpp != dcs->d_type)
1292 tpp = &(*tpp)->t_subt;
1293 if (*tpp == NULL)
1294 return decl;
1296 while (pi != NULL) {
1297 *tpp = tp = getblk(sizeof (type_t));
1298 tp->t_tspec = PTR;
1299 tp->t_const = pi->p_const;
1300 tp->t_volatile = pi->p_volatile;
1301 *(tpp = &tp->t_subt) = dcs->d_type;
1302 npi = pi->p_nxt;
1303 free(pi);
1304 pi = npi;
1306 return (decl);
1310 * If a dimension was specified, dim is 1, otherwise 0
1311 * n is the specified dimension
1313 sym_t *
1314 addarray(sym_t *decl, int dim, int n)
1316 type_t **tpp, *tp;
1318 tpp = &decl->s_type;
1319 while (*tpp && *tpp != dcs->d_type)
1320 tpp = &(*tpp)->t_subt;
1321 if (*tpp == NULL)
1322 return decl;
1324 *tpp = tp = getblk(sizeof (type_t));
1325 tp->t_tspec = ARRAY;
1326 tp->t_subt = dcs->d_type;
1327 tp->t_dim = n;
1329 if (n < 0) {
1330 /* negative array dimension */
1331 error(20, n);
1332 n = 0;
1333 } else if (n == 0 && dim) {
1334 /* zero array dimension */
1335 c99ism(322, dim);
1336 } else if (n == 0 && !dim) {
1337 /* is incomplete type */
1338 setcompl(tp, 1);
1341 return (decl);
1344 sym_t *
1345 addfunc(sym_t *decl, sym_t *args)
1347 type_t **tpp, *tp;
1349 if (dcs->d_proto) {
1350 if (tflag)
1351 /* function prototypes are illegal in traditional C */
1352 warning(270);
1353 args = nsfunc(decl, args);
1354 } else {
1355 osfunc(decl, args);
1359 * The symbols are removed from the symbol table by popdecl() after
1360 * addfunc(). To be able to restore them if this is a function
1361 * definition, a pointer to the list of all symbols is stored in
1362 * dcs->d_nxt->d_fpsyms. Also a list of the arguments (concatenated
1363 * by s_nxt) is stored in dcs->d_nxt->d_fargs.
1364 * (dcs->d_nxt must be used because *dcs is the declaration stack
1365 * element created for the list of params and is removed after
1366 * addfunc())
1368 if (dcs->d_nxt->d_ctx == EXTERN &&
1369 decl->s_type == dcs->d_nxt->d_type) {
1370 dcs->d_nxt->d_fpsyms = dcs->d_dlsyms;
1371 dcs->d_nxt->d_fargs = args;
1374 tpp = &decl->s_type;
1375 while (*tpp && *tpp != dcs->d_nxt->d_type)
1376 tpp = &(*tpp)->t_subt;
1377 if (*tpp == NULL)
1378 return decl;
1380 *tpp = tp = getblk(sizeof (type_t));
1381 tp->t_tspec = FUNC;
1382 tp->t_subt = dcs->d_nxt->d_type;
1383 if ((tp->t_proto = dcs->d_proto) != 0)
1384 tp->t_args = args;
1385 tp->t_vararg = dcs->d_vararg;
1387 return (decl);
1391 * Called for new style function declarations.
1393 /* ARGSUSED */
1394 static sym_t *
1395 nsfunc(sym_t *decl, sym_t *args)
1397 sym_t *arg, *sym;
1398 scl_t sc;
1399 int n;
1402 * Declarations of structs/unions/enums in param lists are legal,
1403 * but senseless.
1405 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1406 sc = sym->s_scl;
1407 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) {
1408 /* dubious tag declaration: %s %s */
1409 warning(85, scltoa(sc), sym->s_name);
1413 n = 1;
1414 for (arg = args; arg != NULL; arg = arg->s_nxt) {
1415 if (arg->s_type->t_tspec == VOID) {
1416 if (n > 1 || arg->s_nxt != NULL) {
1417 /* "void" must be sole parameter */
1418 error(60);
1419 arg->s_type = gettyp(INT);
1422 n++;
1425 /* return NULL if first param is VOID */
1426 return (args != NULL && args->s_type->t_tspec != VOID ? args : NULL);
1430 * Called for old style function declarations.
1432 static void
1433 osfunc(sym_t *decl, sym_t *args)
1437 * Remember list of params only if this is really seams to be
1438 * a function definition.
1440 if (dcs->d_nxt->d_ctx == EXTERN &&
1441 decl->s_type == dcs->d_nxt->d_type) {
1443 * We assume that this becomes a function definition. If
1444 * we are wrong, its corrected in chkfdef().
1446 if (args != NULL) {
1447 decl->s_osdef = 1;
1448 decl->s_args = args;
1450 } else {
1451 if (args != NULL)
1452 /* function prototype parameters must have types */
1453 warning(62);
1458 * Lists of Identifiers in functions declarations are allowed only if
1459 * its also a function definition. If this is not the case, print a
1460 * error message.
1462 void
1463 chkfdef(sym_t *sym, int msg)
1466 if (sym->s_osdef) {
1467 if (msg) {
1468 /* incomplete or misplaced function definition */
1469 error(22);
1471 sym->s_osdef = 0;
1472 sym->s_args = NULL;
1477 * Process the name in a declarator.
1478 * If the symbol does already exists, a new one is created.
1479 * The symbol becomes one of the storage classes EXTERN, STATIC, AUTO or
1480 * TYPEDEF.
1481 * s_def and s_reg are valid after dname().
1483 sym_t *
1484 dname(sym_t *sym)
1486 scl_t sc = NOSCL;
1488 if (sym->s_scl == NOSCL) {
1489 dcs->d_rdcsym = NULL;
1490 } else if (sym->s_defarg) {
1491 sym->s_defarg = 0;
1492 dcs->d_rdcsym = NULL;
1493 } else {
1494 dcs->d_rdcsym = sym;
1495 sym = pushdown(sym);
1498 switch (dcs->d_ctx) {
1499 case MOS:
1500 case MOU:
1501 /* Parent setzen */
1502 sym->s_styp = dcs->d_tagtyp->t_str;
1503 sym->s_def = DEF;
1504 sym->s_value.v_tspec = INT;
1505 sc = dcs->d_ctx;
1506 break;
1507 case EXTERN:
1509 * static and external symbols without "extern" are
1510 * considered to be tentative defined, external
1511 * symbols with "extern" are declared, and typedef names
1512 * are defined. Tentative defined and declared symbols
1513 * may become defined if an initializer is present or
1514 * this is a function definition.
1516 if ((sc = dcs->d_scl) == NOSCL) {
1517 sc = EXTERN;
1518 sym->s_def = TDEF;
1519 } else if (sc == STATIC) {
1520 sym->s_def = TDEF;
1521 } else if (sc == TYPEDEF) {
1522 sym->s_def = DEF;
1523 } else if (sc == EXTERN) {
1524 sym->s_def = DECL;
1525 } else {
1526 LERROR("dname()");
1528 break;
1529 case PARG:
1530 sym->s_arg = 1;
1531 /* FALLTHROUGH */
1532 case ARG:
1533 if ((sc = dcs->d_scl) == NOSCL) {
1534 sc = AUTO;
1535 } else if (sc == REG) {
1536 sym->s_reg = 1;
1537 sc = AUTO;
1538 } else {
1539 LERROR("dname()");
1541 sym->s_def = DEF;
1542 break;
1543 case AUTO:
1544 if ((sc = dcs->d_scl) == NOSCL) {
1546 * XXX somewhat ugly because we dont know whether
1547 * this is AUTO or EXTERN (functions). If we are
1548 * wrong it must be corrected in decl1loc(), where
1549 * we have the necessary type information.
1551 sc = AUTO;
1552 sym->s_def = DEF;
1553 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1554 sym->s_def = DEF;
1555 } else if (sc == REG) {
1556 sym->s_reg = 1;
1557 sc = AUTO;
1558 sym->s_def = DEF;
1559 } else if (sc == EXTERN) {
1560 sym->s_def = DECL;
1561 } else {
1562 LERROR("dname()");
1564 break;
1565 default:
1566 LERROR("dname()");
1568 sym->s_scl = sc;
1570 sym->s_type = dcs->d_type;
1572 dcs->d_fpsyms = NULL;
1574 return (sym);
1578 * Process a name in the list of formal params in an old style function
1579 * definition.
1581 sym_t *
1582 iname(sym_t *sym)
1585 if (sym->s_scl != NOSCL) {
1586 if (blklev == sym->s_blklev) {
1587 /* redeclaration of formal parameter %s */
1588 error(21, sym->s_name);
1589 if (!sym->s_defarg)
1590 LERROR("iname()");
1592 sym = pushdown(sym);
1594 sym->s_type = gettyp(INT);
1595 sym->s_scl = AUTO;
1596 sym->s_def = DEF;
1597 sym->s_defarg = sym->s_arg = 1;
1598 return (sym);
1602 * Create the type of a tag.
1604 * tag points to the symbol table entry of the tag
1605 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1606 * decl is 1 if the type of the tag will be completed in this declaration
1607 * (the following token is T_LBRACE)
1608 * semi is 1 if the following token is T_SEMI
1610 type_t *
1611 mktag(sym_t *tag, tspec_t kind, int decl, int semi)
1613 scl_t scl = NOSCL;
1614 type_t *tp;
1616 if (kind == STRUCT) {
1617 scl = STRTAG;
1618 } else if (kind == UNION) {
1619 scl = UNIONTAG;
1620 } else if (kind == ENUM) {
1621 scl = ENUMTAG;
1622 } else {
1623 LERROR("mktag()");
1626 if (tag != NULL) {
1627 if (tag->s_scl != NOSCL) {
1628 tag = newtag(tag, scl, decl, semi);
1629 } else {
1630 /* a new tag, no empty declaration */
1631 dcs->d_nxt->d_nedecl = 1;
1632 if (scl == ENUMTAG && !decl) {
1633 if (!tflag && (sflag || pflag))
1634 /* forward reference to enum type */
1635 warning(42);
1638 if (tag->s_scl == NOSCL) {
1639 tag->s_scl = scl;
1640 tag->s_type = tp = getblk(sizeof (type_t));
1641 tp->t_ispacked = dcs->d_ispacked;
1642 } else {
1643 tp = tag->s_type;
1645 } else {
1646 tag = getblk(sizeof (sym_t));
1647 tag->s_name = unnamed;
1648 UNIQUE_CURR_POS(tag->s_dpos);
1649 tag->s_kind = FTAG;
1650 tag->s_scl = scl;
1651 tag->s_blklev = -1;
1652 tag->s_type = tp = getblk(sizeof (type_t));
1653 tp->t_ispacked = dcs->d_ispacked;
1654 dcs->d_nxt->d_nedecl = 1;
1657 if (tp->t_tspec == NOTSPEC) {
1658 tp->t_tspec = kind;
1659 if (kind != ENUM) {
1660 tp->t_str = getblk(sizeof (str_t));
1661 tp->t_str->align = CHAR_BIT;
1662 tp->t_str->stag = tag;
1663 } else {
1664 tp->t_isenum = 1;
1665 tp->t_enum = getblk(sizeof (enum_t));
1666 tp->t_enum->etag = tag;
1668 /* ist unvollstaendiger Typ */
1669 setcompl(tp, 1);
1671 return (tp);
1675 * Checks all possible cases of tag redeclarations.
1676 * decl is 1 if T_LBRACE follows
1677 * semi is 1 if T_SEMI follows
1679 static sym_t *
1680 newtag(sym_t *tag, scl_t scl, int decl, int semi)
1683 if (tag->s_blklev < blklev) {
1684 if (semi) {
1685 /* "struct a;" */
1686 if (!tflag) {
1687 if (!sflag)
1688 /* decl. introduces new type ... */
1689 warning(44, scltoa(scl), tag->s_name);
1690 tag = pushdown(tag);
1691 } else if (tag->s_scl != scl) {
1692 /* base type is really "%s %s" */
1693 warning(45, scltoa(tag->s_scl), tag->s_name);
1695 dcs->d_nxt->d_nedecl = 1;
1696 } else if (decl) {
1697 /* "struct a { ... } " */
1698 if (hflag)
1699 /* redefinition hides earlier one: %s */
1700 warning(43, tag->s_name);
1701 tag = pushdown(tag);
1702 dcs->d_nxt->d_nedecl = 1;
1703 } else if (tag->s_scl != scl) {
1704 /* base type is really "%s %s" */
1705 warning(45, scltoa(tag->s_scl), tag->s_name);
1706 /* declaration introduces new type in ANSI C: %s %s */
1707 if (!sflag)
1708 warning(44, scltoa(scl), tag->s_name);
1709 tag = pushdown(tag);
1710 dcs->d_nxt->d_nedecl = 1;
1712 } else {
1713 if (tag->s_scl != scl) {
1714 /* (%s) tag redeclared */
1715 error(46, scltoa(tag->s_scl));
1716 prevdecl(-1, tag);
1717 tag = pushdown(tag);
1718 dcs->d_nxt->d_nedecl = 1;
1719 } else if (decl && !incompl(tag->s_type)) {
1720 /* (%s) tag redeclared */
1721 error(46, scltoa(tag->s_scl));
1722 prevdecl(-1, tag);
1723 tag = pushdown(tag);
1724 dcs->d_nxt->d_nedecl = 1;
1725 } else if (semi || decl) {
1726 dcs->d_nxt->d_nedecl = 1;
1729 return (tag);
1732 const char *
1733 scltoa(scl_t sc)
1735 const char *s;
1737 switch (sc) {
1738 case EXTERN: s = "extern"; break;
1739 case STATIC: s = "static"; break;
1740 case AUTO: s = "auto"; break;
1741 case REG: s = "register"; break;
1742 case TYPEDEF: s = "typedef"; break;
1743 case STRTAG: s = "struct"; break;
1744 case UNIONTAG: s = "union"; break;
1745 case ENUMTAG: s = "enum"; break;
1746 default: LERROR("tagttoa()");
1748 return (s);
1752 * tp points to the type of the, tag, fmem to the list of members/enums.
1754 type_t *
1755 compltag(type_t *tp, sym_t *fmem)
1757 tspec_t t;
1758 str_t *sp;
1759 int n;
1760 sym_t *mem;
1762 /* from now a complete type */
1763 setcompl(tp, 0);
1765 if ((t = tp->t_tspec) != ENUM) {
1766 align(dcs->d_stralign, 0);
1767 sp = tp->t_str;
1768 sp->align = dcs->d_stralign;
1769 sp->memb = fmem;
1770 if (tp->t_ispacked)
1771 setpackedsize(tp);
1772 else
1773 sp->size = dcs->d_offset;
1774 if (sp->size == 0) {
1775 /* zero sized %s */
1776 (void)c99ism(47, ttab[t].tt_name);
1777 } else {
1778 n = 0;
1779 for (mem = fmem; mem != NULL; mem = mem->s_nxt) {
1780 if (mem->s_name != unnamed)
1781 n++;
1783 if (n == 0) {
1784 /* %s has no named members */
1785 warning(65,
1786 t == STRUCT ? "structure" : "union");
1789 } else {
1790 tp->t_enum->elem = fmem;
1792 return (tp);
1796 * Processes the name of an enumerator in en enum declaration.
1798 * sym points to the enumerator
1799 * val is the value of the enumerator
1800 * impl is 1 if the value of the enumerator was not explicit specified.
1802 sym_t *
1803 ename(sym_t *sym, int val, int impl)
1806 if (sym->s_scl) {
1807 if (sym->s_blklev == blklev) {
1808 /* no hflag, because this is illegal!!! */
1809 if (sym->s_arg) {
1810 /* enumeration constant hides parameter: %s */
1811 warning(57, sym->s_name);
1812 } else {
1813 /* redeclaration of %s */
1814 error(27, sym->s_name);
1816 * inside blocks it should not too complicated
1817 * to find the position of the previous
1818 * declaration
1820 if (blklev == 0)
1821 prevdecl(-1, sym);
1823 } else {
1824 if (hflag)
1825 /* redefinition hides earlier one: %s */
1826 warning(43, sym->s_name);
1828 sym = pushdown(sym);
1830 sym->s_scl = ENUMCON;
1831 sym->s_type = dcs->d_tagtyp;
1832 sym->s_value.v_tspec = INT;
1833 sym->s_value.v_quad = val;
1834 if (impl && val - 1 == INT_MAX) {
1835 /* overflow in enumeration values: %s */
1836 warning(48, sym->s_name);
1838 enumval = val + 1;
1839 return (sym);
1843 * Process a single external declarator.
1845 void
1846 decl1ext(sym_t *dsym, int initflg)
1848 int dowarn, rval, redec;
1849 sym_t *rdsym;
1851 chkfdef(dsym, 1);
1853 chktyp(dsym);
1855 if (initflg && !(initerr = chkinit(dsym)))
1856 dsym->s_def = DEF;
1859 * Declarations of functions are marked as "tentative" in dname().
1860 * This is wrong because there are no tentative function
1861 * definitions.
1863 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1864 dsym->s_def = DECL;
1866 if (dcs->d_inline) {
1867 if (dsym->s_type->t_tspec == FUNC) {
1868 dsym->s_inline = 1;
1869 } else {
1870 /* variable declared inline: %s */
1871 warning(268, dsym->s_name);
1875 /* Write the declaration into the output file */
1876 if (plibflg && llibflg &&
1877 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1879 * With both LINTLIBRARY and PROTOLIB the prototyp is
1880 * written as a function definition to the output file.
1882 rval = dsym->s_type->t_subt->t_tspec != VOID;
1883 outfdef(dsym, &dsym->s_dpos, rval, 0, NULL);
1884 } else {
1885 outsym(dsym, dsym->s_scl, dsym->s_def);
1888 if ((rdsym = dcs->d_rdcsym) != NULL) {
1891 * If the old symbol stems from a old style function definition
1892 * we have remembered the params in rdsmy->s_args and compare
1893 * them with the params of the prototype.
1895 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1896 redec = chkosdef(rdsym, dsym);
1897 } else {
1898 redec = 0;
1901 if (!redec && !isredec(dsym, (dowarn = 0, &dowarn))) {
1903 if (dowarn) {
1904 /* redeclaration of %s */
1905 (*(sflag ? error : warning))(27, dsym->s_name);
1906 prevdecl(-1, rdsym);
1910 * Overtake the rememberd params if the new symbol
1911 * is not a prototype.
1913 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1914 dsym->s_osdef = rdsym->s_osdef;
1915 dsym->s_args = rdsym->s_args;
1916 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1920 * Remember the position of the declaration if the
1921 * old symbol was a prototype and the new is not.
1922 * Also remember the position if the old symbol
1923 * was defined and the new is not.
1925 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1926 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1927 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1928 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1932 * Copy informations about usage of the name into
1933 * the new symbol.
1935 cpuinfo(dsym, rdsym);
1937 /* Once a name is defined, it remains defined. */
1938 if (rdsym->s_def == DEF)
1939 dsym->s_def = DEF;
1941 /* once a function is inline, it remains inline */
1942 if (rdsym->s_inline)
1943 dsym->s_inline = 1;
1945 compltyp(dsym, rdsym);
1949 rmsym(rdsym);
1952 if (dsym->s_scl == TYPEDEF) {
1953 dsym->s_type = duptyp(dsym->s_type);
1954 dsym->s_type->t_typedef = 1;
1955 settdsym(dsym->s_type, dsym);
1961 * Copies informations about usage into a new symbol table entry of
1962 * the same symbol.
1964 void
1965 cpuinfo(sym_t *sym, sym_t *rdsym)
1968 sym->s_spos = rdsym->s_spos;
1969 sym->s_upos = rdsym->s_upos;
1970 sym->s_set = rdsym->s_set;
1971 sym->s_used = rdsym->s_used;
1975 * Prints an error and returns 1 if a symbol is redeclared/redefined.
1976 * Otherwise returns 0 and, in some cases of minor problems, prints
1977 * a warning.
1980 isredec(sym_t *dsym, int *dowarn)
1982 sym_t *rsym;
1984 if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
1985 /* redeclaration of %s */
1986 error(27, dsym->s_name);
1987 prevdecl(-1, rsym);
1988 return (1);
1990 if (rsym->s_scl == TYPEDEF) {
1991 /* typedef redeclared: %s */
1992 error(89, dsym->s_name);
1993 prevdecl(-1, rsym);
1994 return (1);
1996 if (dsym->s_scl == TYPEDEF) {
1997 /* redeclaration of %s */
1998 error(27, dsym->s_name);
1999 prevdecl(-1, rsym);
2000 return (1);
2002 if (rsym->s_def == DEF && dsym->s_def == DEF) {
2003 /* redefinition of %s */
2004 error(28, dsym->s_name);
2005 prevdecl(-1, rsym);
2006 return(1);
2008 if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, dowarn)) {
2009 /* redeclaration of %s */
2010 error(27, dsym->s_name);
2011 prevdecl(-1, rsym);
2012 return(1);
2014 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
2015 return(0);
2016 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
2017 return(0);
2018 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
2019 return(0);
2020 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
2022 * All cases except "int a = 1; static int a;" are catched
2023 * above with or without a warning
2025 /* redeclaration of %s */
2026 error(27, dsym->s_name);
2027 prevdecl(-1, rsym);
2028 return(1);
2030 if (rsym->s_scl == EXTERN) {
2031 /* previously declared extern, becomes static: %s */
2032 warning(29, dsym->s_name);
2033 prevdecl(-1, rsym);
2034 return(0);
2037 * Now its on of:
2038 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2040 /* redeclaration of %s; ANSI C requires "static" */
2041 if (sflag) {
2042 warning(30, dsym->s_name);
2043 prevdecl(-1, rsym);
2045 dsym->s_scl = STATIC;
2046 return (0);
2049 static int
2050 chkqual(type_t *tp1, type_t *tp2, int ignqual)
2052 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
2053 return 0;
2055 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
2056 return 0;
2058 return 1;
2062 eqptrtype(type_t *tp1, type_t *tp2, int ignqual)
2064 if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
2065 return 0;
2067 if (!chkqual(tp1, tp2, ignqual))
2068 return 0;
2070 return 1;
2075 * Checks if two types are compatible. Returns 0 if not, otherwise 1.
2077 * ignqual ignore qualifiers of type; used for function params
2078 * promot promote left type; used for comparison of params of
2079 * old style function definitions with params of prototypes.
2080 * *dowarn set to 1 if an old style function declaration is not
2081 * compatible with a prototype
2084 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *dowarn)
2086 tspec_t t;
2088 while (tp1 != NULL && tp2 != NULL) {
2090 t = tp1->t_tspec;
2091 if (promot) {
2092 if (t == FLOAT) {
2093 t = DOUBLE;
2094 } else if (t == CHAR || t == SCHAR) {
2095 t = INT;
2096 } else if (t == UCHAR) {
2097 t = tflag ? UINT : INT;
2098 } else if (t == SHORT) {
2099 t = INT;
2100 } else if (t == USHORT) {
2101 /* CONSTCOND */
2102 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
2106 if (t != tp2->t_tspec)
2107 return (0);
2109 if (!chkqual(tp1, tp2, ignqual))
2110 return 0;
2112 if (t == STRUCT || t == UNION)
2113 return (tp1->t_str == tp2->t_str);
2115 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2116 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2117 return (0);
2120 /* dont check prototypes for traditional */
2121 if (t == FUNC && !tflag) {
2122 if (tp1->t_proto && tp2->t_proto) {
2123 if (!eqargs(tp1, tp2, dowarn))
2124 return (0);
2125 } else if (tp1->t_proto) {
2126 if (!mnoarg(tp1, dowarn))
2127 return (0);
2128 } else if (tp2->t_proto) {
2129 if (!mnoarg(tp2, dowarn))
2130 return (0);
2134 tp1 = tp1->t_subt;
2135 tp2 = tp2->t_subt;
2136 ignqual = promot = 0;
2140 return (tp1 == tp2);
2144 * Compares the parameter types of two prototypes.
2146 static int
2147 eqargs(type_t *tp1, type_t *tp2, int *dowarn)
2149 sym_t *a1, *a2;
2151 if (tp1->t_vararg != tp2->t_vararg)
2152 return (0);
2154 a1 = tp1->t_args;
2155 a2 = tp2->t_args;
2157 while (a1 != NULL && a2 != NULL) {
2159 if (eqtype(a1->s_type, a2->s_type, 1, 0, dowarn) == 0)
2160 return (0);
2162 a1 = a1->s_nxt;
2163 a2 = a2->s_nxt;
2167 return (a1 == a2);
2171 * mnoarg() (matches functions with no argument type information)
2172 * returns 1 if all parameters of a prototype are compatible with
2173 * and old style function declaration.
2174 * This is the case if following conditions are met:
2175 * 1. the prototype must have a fixed number of parameters
2176 * 2. no parameter is of type float
2177 * 3. no parameter is converted to another type if integer promotion
2178 * is applied on it
2180 static int
2181 mnoarg(type_t *tp, int *dowarn)
2183 sym_t *arg;
2184 tspec_t t;
2186 if (tp->t_vararg) {
2187 if (dowarn != NULL)
2188 *dowarn = 1;
2190 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) {
2191 if ((t = arg->s_type->t_tspec) == FLOAT ||
2192 t == CHAR || t == SCHAR || t == UCHAR ||
2193 t == SHORT || t == USHORT) {
2194 if (dowarn != NULL)
2195 *dowarn = 1;
2198 return (1);
2202 * Compares a prototype declaration with the remembered arguments of
2203 * a previous old style function definition.
2205 static int
2206 chkosdef(sym_t *rdsym, sym_t *dsym)
2208 sym_t *args, *pargs, *arg, *parg;
2209 int narg, nparg, n;
2210 int dowarn, msg;
2212 args = rdsym->s_args;
2213 pargs = dsym->s_type->t_args;
2215 msg = 0;
2217 narg = nparg = 0;
2218 for (arg = args; arg != NULL; arg = arg->s_nxt)
2219 narg++;
2220 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2221 nparg++;
2222 if (narg != nparg) {
2223 /* prototype does not match old-style definition */
2224 error(63);
2225 msg = 1;
2226 goto end;
2229 arg = args;
2230 parg = pargs;
2231 n = 1;
2232 while (narg--) {
2233 dowarn = 0;
2235 * If it does not match due to promotion and sflag is
2236 * not set we print only a warning.
2238 if (!eqtype(arg->s_type, parg->s_type, 1, 1, &dowarn) || dowarn) {
2239 /* prototype does not match old-style def., arg #%d */
2240 error(299, n);
2241 msg = 1;
2243 arg = arg->s_nxt;
2244 parg = parg->s_nxt;
2245 n++;
2248 end:
2249 if (msg)
2250 /* old style definition */
2251 prevdecl(300, rdsym);
2253 return (msg);
2257 * Complets a type by copying the dimension and prototype information
2258 * from a second compatible type.
2260 * Following lines are legal:
2261 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2262 * "typedef ft(); ft f; f(int); ft g; g(long);"
2263 * This means that, if a type is completed, the type structure must
2264 * be duplicated.
2266 void
2267 compltyp(sym_t *dsym, sym_t *ssym)
2269 type_t **dstp, *src;
2270 type_t *dst;
2272 dstp = &dsym->s_type;
2273 src = ssym->s_type;
2275 while ((dst = *dstp) != NULL) {
2276 if (src == NULL || dst->t_tspec != src->t_tspec)
2277 LERROR("compltyp()");
2278 if (dst->t_tspec == ARRAY) {
2279 if (dst->t_dim == 0 && src->t_dim != 0) {
2280 *dstp = dst = duptyp(dst);
2281 dst->t_dim = src->t_dim;
2282 /* now a complete Typ */
2283 setcompl(dst, 0);
2285 } else if (dst->t_tspec == FUNC) {
2286 if (!dst->t_proto && src->t_proto) {
2287 *dstp = dst = duptyp(dst);
2288 dst->t_proto = 1;
2289 dst->t_args = src->t_args;
2292 dstp = &dst->t_subt;
2293 src = src->t_subt;
2298 * Completes the declaration of a single argument.
2300 sym_t *
2301 decl1arg(sym_t *sym, int initflg)
2303 tspec_t t;
2305 chkfdef(sym, 1);
2307 chktyp(sym);
2309 if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) {
2310 /* redeclaration of formal parameter %s */
2311 error(237, sym->s_name);
2312 rmsym(dcs->d_rdcsym);
2313 sym->s_arg = 1;
2316 if (!sym->s_arg) {
2317 /* declared argument %s is missing */
2318 error(53, sym->s_name);
2319 sym->s_arg = 1;
2322 if (initflg) {
2323 /* cannot initialize parameter: %s */
2324 error(52, sym->s_name);
2325 initerr = 1;
2328 if ((t = sym->s_type->t_tspec) == ARRAY) {
2329 sym->s_type = incref(sym->s_type->t_subt, PTR);
2330 } else if (t == FUNC) {
2331 if (tflag)
2332 /* a function is declared as an argument: %s */
2333 warning(50, sym->s_name);
2334 sym->s_type = incref(sym->s_type, PTR);
2335 } else if (t == FLOAT) {
2336 if (tflag)
2337 sym->s_type = gettyp(DOUBLE);
2340 if (dcs->d_inline)
2341 /* argument declared inline: %s */
2342 warning(269, sym->s_name);
2345 * Arguments must have complete types. lengths() prints the needed
2346 * error messages (null dimension is impossible because arrays are
2347 * converted to pointers).
2349 if (sym->s_type->t_tspec != VOID)
2350 (void)length(sym->s_type, sym->s_name);
2352 setsflg(sym);
2354 return (sym);
2358 * Does some checks for lint directives which apply to functions.
2359 * Processes arguments in old style function definitions which default
2360 * to int.
2361 * Checks compatiblility of old style function definition with previous
2362 * prototype.
2364 void
2365 cluparg(void)
2367 sym_t *args, *arg, *pargs, *parg;
2368 int narg, nparg, n, msg;
2369 tspec_t t;
2371 args = funcsym->s_args;
2372 pargs = funcsym->s_type->t_args;
2374 /* check for illegal combinations of lint directives */
2375 if (prflstrg != -1 && scflstrg != -1) {
2376 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2377 warning(289);
2378 prflstrg = scflstrg = -1;
2380 if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) {
2381 /* dubious use of ** VARARGS ** with ** %s ** */
2382 warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2383 nvararg = -1;
2387 * check if the argument of a lint directive is compatible with the
2388 * number of arguments.
2390 narg = 0;
2391 for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_nxt)
2392 narg++;
2393 if (nargusg > narg) {
2394 /* argument number mismatch with directive: ** %s ** */
2395 warning(283, "ARGSUSED");
2396 nargusg = 0;
2398 if (nvararg > narg) {
2399 /* argument number mismatch with directive: ** %s ** */
2400 warning(283, "VARARGS");
2401 nvararg = 0;
2403 if (prflstrg > narg) {
2404 /* argument number mismatch with directive: ** %s ** */
2405 warning(283, "PRINTFLIKE");
2406 prflstrg = -1;
2407 } else if (prflstrg == 0) {
2408 prflstrg = -1;
2410 if (scflstrg > narg) {
2411 /* argument number mismatch with directive: ** %s ** */
2412 warning(283, "SCANFLIKE");
2413 scflstrg = -1;
2414 } else if (scflstrg == 0) {
2415 scflstrg = -1;
2417 if (prflstrg != -1 || scflstrg != -1) {
2418 narg = prflstrg != -1 ? prflstrg : scflstrg;
2419 arg = dcs->d_fargs;
2420 for (n = 1; n < narg; n++)
2421 arg = arg->s_nxt;
2422 if (arg->s_type->t_tspec != PTR ||
2423 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2424 t != UCHAR && t != SCHAR)) {
2425 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2426 warning(293, narg);
2427 prflstrg = scflstrg = -1;
2432 * print a warning for each argument off an old style function
2433 * definition which defaults to int
2435 for (arg = args; arg != NULL; arg = arg->s_nxt) {
2436 if (arg->s_defarg) {
2437 /* argument type defaults to int: %s */
2438 warning(32, arg->s_name);
2439 arg->s_defarg = 0;
2440 setsflg(arg);
2445 * If this is an old style function definition and a prototyp
2446 * exists, compare the types of arguments.
2448 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2450 * If the number of arguments does not macht, we need not
2451 * continue.
2453 narg = nparg = 0;
2454 msg = 0;
2455 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2456 nparg++;
2457 for (arg = args; arg != NULL; arg = arg->s_nxt)
2458 narg++;
2459 if (narg != nparg) {
2460 /* parameter mismatch: %d declared, %d defined */
2461 error(51, nparg, narg);
2462 msg = 1;
2463 } else {
2464 parg = pargs;
2465 arg = args;
2466 while (narg--) {
2467 msg |= chkptdecl(arg, parg);
2468 parg = parg->s_nxt;
2469 arg = arg->s_nxt;
2472 if (msg)
2473 /* prototype declaration */
2474 prevdecl(285, dcs->d_rdcsym);
2476 /* from now the prototype is valid */
2477 funcsym->s_osdef = 0;
2478 funcsym->s_args = NULL;
2485 * Checks compatibility of an old style function definition with a previous
2486 * prototype declaration.
2487 * Returns 1 if the position of the previous declaration should be reported.
2489 static int
2490 chkptdecl(sym_t *arg, sym_t *parg)
2492 type_t *tp, *ptp;
2493 int dowarn, msg;
2495 tp = arg->s_type;
2496 ptp = parg->s_type;
2498 msg = 0;
2499 dowarn = 0;
2501 if (!eqtype(tp, ptp, 1, 1, &dowarn)) {
2502 if (eqtype(tp, ptp, 1, 0, &dowarn)) {
2503 /* type does not match prototype: %s */
2504 msg = gnuism(58, arg->s_name);
2505 } else {
2506 /* type does not match prototype: %s */
2507 error(58, arg->s_name);
2508 msg = 1;
2510 } else if (dowarn) {
2511 /* type does not match prototype: %s */
2512 (*(sflag ? error : warning))(58, arg->s_name);
2513 msg = 1;
2516 return (msg);
2520 * Completes a single local declaration/definition.
2522 void
2523 decl1loc(sym_t *dsym, int initflg)
2526 /* Correct a mistake done in dname(). */
2527 if (dsym->s_type->t_tspec == FUNC) {
2528 dsym->s_def = DECL;
2529 if (dcs->d_scl == NOSCL)
2530 dsym->s_scl = EXTERN;
2533 if (dsym->s_type->t_tspec == FUNC) {
2534 if (dsym->s_scl == STATIC) {
2535 /* dubious static function at block level: %s */
2536 warning(93, dsym->s_name);
2537 dsym->s_scl = EXTERN;
2538 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2539 /* function has illegal storage class: %s */
2540 error(94, dsym->s_name);
2541 dsym->s_scl = EXTERN;
2546 * functions may be declared inline at local scope, although
2547 * this has no effect for a later definition of the same
2548 * function.
2549 * XXX it should have an effect if tflag is set. this would
2550 * also be the way gcc behaves.
2552 if (dcs->d_inline) {
2553 if (dsym->s_type->t_tspec == FUNC) {
2554 dsym->s_inline = 1;
2555 } else {
2556 /* variable declared inline: %s */
2557 warning(268, dsym->s_name);
2561 chkfdef(dsym, 1);
2563 chktyp(dsym);
2565 if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN)
2566 ledecl(dsym);
2568 if (dsym->s_scl == EXTERN) {
2570 * XXX wenn die statische Variable auf Ebene 0 erst
2571 * spaeter definiert wird, haben wir die Brille auf.
2573 if (dsym->s_xsym == NULL) {
2574 outsym(dsym, EXTERN, dsym->s_def);
2575 } else {
2576 outsym(dsym, dsym->s_xsym->s_scl, dsym->s_def);
2580 if (dcs->d_rdcsym != NULL) {
2582 if (dcs->d_rdcsym->s_blklev == 0) {
2584 switch (dsym->s_scl) {
2585 case AUTO:
2586 /* automatic hides external declaration: %s */
2587 if (hflag)
2588 warning(86, dsym->s_name);
2589 break;
2590 case STATIC:
2591 /* static hides external declaration: %s */
2592 if (hflag)
2593 warning(87, dsym->s_name);
2594 break;
2595 case TYPEDEF:
2596 /* typedef hides external declaration: %s */
2597 if (hflag)
2598 warning(88, dsym->s_name);
2599 break;
2600 case EXTERN:
2602 * Warnings and errors are printed in ledecl()
2604 break;
2605 default:
2606 LERROR("decl1loc()");
2609 } else if (dcs->d_rdcsym->s_blklev == blklev) {
2611 /* no hflag, because its illegal! */
2612 if (dcs->d_rdcsym->s_arg) {
2614 * if !tflag, a "redeclaration of %s" error
2615 * is produced below
2617 if (tflag) {
2618 if (hflag)
2619 /* decl. hides parameter: %s */
2620 warning(91, dsym->s_name);
2621 rmsym(dcs->d_rdcsym);
2625 } else if (dcs->d_rdcsym->s_blklev < blklev) {
2627 if (hflag)
2628 /* declaration hides earlier one: %s */
2629 warning(95, dsym->s_name);
2633 if (dcs->d_rdcsym->s_blklev == blklev) {
2635 /* redeclaration of %s */
2636 error(27, dsym->s_name);
2637 rmsym(dcs->d_rdcsym);
2643 if (initflg && !(initerr = chkinit(dsym))) {
2644 dsym->s_def = DEF;
2645 setsflg(dsym);
2648 if (dsym->s_scl == TYPEDEF) {
2649 dsym->s_type = duptyp(dsym->s_type);
2650 dsym->s_type->t_typedef = 1;
2651 settdsym(dsym->s_type, dsym);
2655 * Before we can check the size we must wait for a initialisation
2656 * which may follow.
2661 * Processes (re)declarations of external Symbols inside blocks.
2663 static void
2664 ledecl(sym_t *dsym)
2666 int eqt, dowarn;
2667 sym_t *esym;
2669 /* look for a symbol with the same name */
2670 esym = dcs->d_rdcsym;
2671 while (esym != NULL && esym->s_blklev != 0) {
2672 while ((esym = esym->s_link) != NULL) {
2673 if (esym->s_kind != FVFT)
2674 continue;
2675 if (strcmp(dsym->s_name, esym->s_name) == 0)
2676 break;
2679 if (esym == NULL)
2680 return;
2681 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2682 /* gcc accepts this without a warning, pcc prints an error. */
2683 /* redeclaration of %s */
2684 warning(27, dsym->s_name);
2685 prevdecl(-1, esym);
2686 return;
2689 dowarn = 0;
2690 eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &dowarn);
2692 if (!eqt || dowarn) {
2693 if (esym->s_scl == EXTERN) {
2694 /* inconsistent redeclaration of extern: %s */
2695 warning(90, dsym->s_name);
2696 prevdecl(-1, esym);
2697 } else {
2698 /* inconsistent redeclaration of static: %s */
2699 warning(92, dsym->s_name);
2700 prevdecl(-1, esym);
2704 if (eqt) {
2706 * Remember the external symbol so we can update usage
2707 * information at the end of the block.
2709 dsym->s_xsym = esym;
2714 * Print an error or a warning if the symbol cant be initialized due
2715 * to type/storage class. Returnvalue is 1 if an error has been
2716 * detected.
2718 static int
2719 chkinit(sym_t *sym)
2721 int erred;
2723 erred = 0;
2725 if (sym->s_type->t_tspec == FUNC) {
2726 /* cannot initialize function: %s */
2727 error(24, sym->s_name);
2728 erred = 1;
2729 } else if (sym->s_scl == TYPEDEF) {
2730 /* cannot initialize typedef: %s */
2731 error(25, sym->s_name);
2732 erred = 1;
2733 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2734 /* cannot initialize "extern" declaration: %s */
2735 if (dcs->d_ctx == EXTERN) {
2736 warning(26, sym->s_name);
2737 } else {
2738 error(26, sym->s_name);
2739 erred = 1;
2743 return (erred);
2747 * Create a symbole for an abstract declaration.
2749 sym_t *
2750 aname(void)
2752 sym_t *sym;
2754 if (dcs->d_ctx != ABSTRACT && dcs->d_ctx != PARG)
2755 LERROR("aname()");
2757 sym = getblk(sizeof (sym_t));
2759 sym->s_name = unnamed;
2760 sym->s_def = DEF;
2761 sym->s_scl = ABSTRACT;
2762 sym->s_blklev = -1;
2764 if (dcs->d_ctx == PARG)
2765 sym->s_arg = 1;
2767 sym->s_type = dcs->d_type;
2768 dcs->d_rdcsym = NULL;
2769 dcs->d_vararg = 0;
2771 return (sym);
2775 * Removes anything which has nothing to do on global level.
2777 void
2778 globclup(void)
2781 while (dcs->d_nxt != NULL)
2782 popdecl();
2784 cleanup();
2785 blklev = 0;
2786 mblklev = 0;
2789 * remove all informations about pending lint directives without
2790 * warnings.
2792 glclup(1);
2796 * Process an abstract type declaration
2798 sym_t *
2799 decl1abs(sym_t *sym)
2802 chkfdef(sym, 1);
2803 chktyp(sym);
2804 return (sym);
2808 * Checks size after declarations of variables and their initialisation.
2810 void
2811 chksz(sym_t *dsym)
2815 * check size only for symbols which are defined and no function and
2816 * not typedef name
2818 if (dsym->s_def != DEF)
2819 return;
2820 if (dsym->s_scl == TYPEDEF)
2821 return;
2822 if (dsym->s_type->t_tspec == FUNC)
2823 return;
2825 if (length(dsym->s_type, dsym->s_name) == 0 &&
2826 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2827 /* empty array declaration: %s */
2828 if (tflag) {
2829 warning(190, dsym->s_name);
2830 } else {
2831 error(190, dsym->s_name);
2837 * Mark an object as set if it is not already
2839 void
2840 setsflg(sym_t *sym)
2843 if (!sym->s_set) {
2844 sym->s_set = 1;
2845 UNIQUE_CURR_POS(sym->s_spos);
2850 * Mark an object as used if it is not already
2852 void
2853 setuflg(sym_t *sym, int fcall, int szof)
2856 if (!sym->s_used) {
2857 sym->s_used = 1;
2858 UNIQUE_CURR_POS(sym->s_upos);
2861 * for function calls another record is written
2863 * XXX Should symbols used in sizeof() treated as used or not?
2864 * Probably not, because there is no sense to declare an
2865 * external variable only to get their size.
2867 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2868 outusg(sym);
2872 * Prints warnings for a list of variables and labels (concatenated
2873 * with s_dlnxt) if these are not used or only set.
2875 void
2876 chkusage(dinfo_t *di)
2878 sym_t *sym;
2879 int mknowarn;
2881 /* for this warnings LINTED has no effect */
2882 mknowarn = nowarn;
2883 nowarn = 0;
2885 #ifdef DEBUG
2886 printf("%s, %d: >temp nowarn = 0\n", curr_pos.p_file, curr_pos.p_line);
2887 #endif
2888 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
2889 chkusg1(di->d_asm, sym);
2890 nowarn = mknowarn;
2891 #ifdef DEBUG
2892 printf("%s, %d: <temp nowarn = %d\n", curr_pos.p_file, curr_pos.p_line,
2893 nowarn);
2894 #endif
2898 * Prints a warning for a single variable or label if it is not used or
2899 * only set.
2901 void
2902 chkusg1(int novar, sym_t *sym)
2904 pos_t cpos;
2906 if (sym->s_blklev == -1)
2907 return;
2909 STRUCT_ASSIGN(cpos, curr_pos);
2911 if (sym->s_kind == FVFT) {
2912 if (sym->s_arg) {
2913 chkausg(novar, sym);
2914 } else {
2915 chkvusg(novar, sym);
2917 } else if (sym->s_kind == FLAB) {
2918 chklusg(sym);
2919 } else if (sym->s_kind == FTAG) {
2920 chktusg(sym);
2923 STRUCT_ASSIGN(curr_pos, cpos);
2926 static void
2927 chkausg(int novar, sym_t *arg)
2930 if (!arg->s_set)
2931 LERROR("chkausg()");
2933 if (novar)
2934 return;
2936 if (!arg->s_used && vflag) {
2937 STRUCT_ASSIGN(curr_pos, arg->s_dpos);
2938 /* argument %s unused in function %s */
2939 warning(231, arg->s_name, funcsym->s_name);
2943 static void
2944 chkvusg(int novar, sym_t *sym)
2946 scl_t sc;
2947 sym_t *xsym;
2949 if (blklev == 0 || sym->s_blklev == 0)
2950 LERROR("chkvusg()");
2952 /* errors in expressions easily cause lots of these warnings */
2953 if (nerr != 0)
2954 return;
2957 * XXX Only variables are checkd, although types should
2958 * probably also be checked
2960 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
2961 sc != AUTO && sc != REG) {
2962 return;
2965 if (novar)
2966 return;
2968 if (sc == EXTERN) {
2969 if (!sym->s_used && !sym->s_set) {
2970 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2971 /* %s unused in function %s */
2972 warning(192, sym->s_name, funcsym->s_name);
2974 } else {
2975 if (sym->s_set && !sym->s_used) {
2976 STRUCT_ASSIGN(curr_pos, sym->s_spos);
2977 /* %s set but not used in function %s */
2978 warning(191, sym->s_name, funcsym->s_name);
2979 } else if (!sym->s_used) {
2980 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2981 /* %s unused in function %s */
2982 warning(192, sym->s_name, funcsym->s_name);
2986 if (sc == EXTERN) {
2988 * information about usage is taken over into the symbol
2989 * tabel entry at level 0 if the symbol was locally declared
2990 * as an external symbol.
2992 * XXX This is wrong for symbols declared static at level 0
2993 * if the usage information stems from sizeof(). This is
2994 * because symbols at level 0 only used in sizeof() are
2995 * considered to not be used.
2997 if ((xsym = sym->s_xsym) != NULL) {
2998 if (sym->s_used && !xsym->s_used) {
2999 xsym->s_used = 1;
3000 STRUCT_ASSIGN(xsym->s_upos, sym->s_upos);
3002 if (sym->s_set && !xsym->s_set) {
3003 xsym->s_set = 1;
3004 STRUCT_ASSIGN(xsym->s_spos, sym->s_spos);
3010 static void
3011 chklusg(sym_t *lab)
3014 if (blklev != 1 || lab->s_blklev != 1)
3015 LERROR("chklusg()");
3017 if (lab->s_set && !lab->s_used) {
3018 STRUCT_ASSIGN(curr_pos, lab->s_spos);
3019 /* label %s unused in function %s */
3020 warning(192, lab->s_name, funcsym->s_name);
3021 } else if (!lab->s_set) {
3022 STRUCT_ASSIGN(curr_pos, lab->s_upos);
3023 /* undefined label %s */
3024 warning(23, lab->s_name);
3028 static void
3029 chktusg(sym_t *sym)
3032 if (!incompl(sym->s_type))
3033 return;
3035 /* complain alwasy about incomplet tags declared inside blocks */
3036 if (!zflag || dcs->d_ctx != EXTERN)
3037 return;
3039 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3040 switch (sym->s_type->t_tspec) {
3041 case STRUCT:
3042 /* struct %s never defined */
3043 warning(233, sym->s_name);
3044 break;
3045 case UNION:
3046 /* union %s never defined */
3047 warning(234, sym->s_name);
3048 break;
3049 case ENUM:
3050 /* enum %s never defined */
3051 warning(235, sym->s_name);
3052 break;
3053 default:
3054 LERROR("chktusg()");
3059 * Called after the entire translation unit has been parsed.
3060 * Changes tentative definitions in definitions.
3061 * Performs some tests on global Symbols. Detected Problems are:
3062 * - defined variables of incomplete type
3063 * - constant variables which are not initialized
3064 * - static symbols which are never used
3066 void
3067 chkglsyms(void)
3069 sym_t *sym;
3070 pos_t cpos;
3072 if (blklev != 0 || dcs->d_nxt != NULL)
3073 norecover();
3075 STRUCT_ASSIGN(cpos, curr_pos);
3077 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
3078 if (sym->s_blklev == -1)
3079 continue;
3080 if (sym->s_kind == FVFT) {
3081 chkglvar(sym);
3082 } else if (sym->s_kind == FTAG) {
3083 chktusg(sym);
3084 } else {
3085 if (sym->s_kind != FMOS)
3086 LERROR("chkglsyms()");
3090 STRUCT_ASSIGN(curr_pos, cpos);
3093 static void
3094 chkglvar(sym_t *sym)
3097 if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON)
3098 return;
3100 if (sym->s_scl != EXTERN && sym->s_scl != STATIC)
3101 LERROR("chkglvar()");
3103 glchksz(sym);
3105 if (sym->s_scl == STATIC) {
3106 if (sym->s_type->t_tspec == FUNC) {
3107 if (sym->s_used && sym->s_def != DEF) {
3108 STRUCT_ASSIGN(curr_pos, sym->s_upos);
3109 /* static func. called but not def.. */
3110 error(225, sym->s_name);
3113 if (!sym->s_used) {
3114 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3115 if (sym->s_type->t_tspec == FUNC) {
3116 if (sym->s_def == DEF) {
3117 if (!sym->s_inline)
3118 /* static function %s unused */
3119 warning(236, sym->s_name);
3120 } else {
3121 /* static function %s decl. but ... */
3122 warning(290, sym->s_name);
3124 } else if (!sym->s_set) {
3125 /* static variable %s unused */
3126 warning(226, sym->s_name);
3127 } else {
3128 /* static variable %s set but not used */
3129 warning(307, sym->s_name);
3132 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
3133 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3134 /* const object %s should have initializer */
3135 warning(227, sym->s_name);
3140 static void
3141 glchksz(sym_t *sym)
3144 if (sym->s_def == TDEF) {
3145 if (sym->s_type->t_tspec == FUNC)
3147 * this can happen if an syntax error occurred
3148 * after a function declaration
3150 return;
3151 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3152 if (length(sym->s_type, sym->s_name) == 0 &&
3153 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3154 /* empty array declaration: %s */
3155 if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3156 warning(190, sym->s_name);
3157 } else {
3158 error(190, sym->s_name);
3165 * Prints information about location of previous definition/declaration.
3167 void
3168 prevdecl(int msg, sym_t *psym)
3170 pos_t cpos;
3172 if (!rflag)
3173 return;
3175 STRUCT_ASSIGN(cpos, curr_pos);
3176 STRUCT_ASSIGN(curr_pos, psym->s_dpos);
3177 if (msg != -1) {
3178 message(msg, psym->s_name);
3179 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3180 /* previous definition of %s */
3181 message(261, psym->s_name);
3182 } else {
3183 /* previous declaration of %s */
3184 message(260, psym->s_name);
3186 STRUCT_ASSIGN(curr_pos, cpos);