1 /* $NetBSD: decl.c,v 1.11 1995/10/02 17:34:16 jpo Exp $ */
4 * Copyright (c) 1994, 1995 Jochen Pohl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * $FreeBSD: src/usr.bin/xlint/lint1/decl.c,v 1.2.8.2 2001/07/19 09:19:47 kris Exp $
34 * $DragonFly: src/usr.bin/xlint/lint1/decl.c,v 1.6 2004/07/07 12:24:00 asmodai Exp $
37 #include <sys/param.h>
44 const char *unnamed = "<unnamed>";
46 /* contains various information and classification on types */
49 /* shared type structures for arithmtic types and void */
50 static type_t *typetab;
52 /* value of next enumerator during declaration of enum types */
56 * pointer to top element of a stack which contains informations local
57 * to nested declarations
61 static type_t *tdeferr(type_t *, tspec_t);
62 static void settdsym(type_t *, sym_t *);
63 static tspec_t mrgtspec(tspec_t, tspec_t);
64 static void align(int, int);
65 static sym_t *newtag(sym_t *, scl_t, int, int);
66 static int eqargs(type_t *, type_t *, int *);
67 static int mnoarg(type_t *, int *);
68 static int chkosdef(sym_t *, sym_t *);
69 static int chkptdecl(sym_t *, sym_t *);
70 static sym_t *nsfunc(sym_t *, sym_t *);
71 static void osfunc(sym_t *, sym_t *);
72 static void ledecl(sym_t *);
73 static int chkinit(sym_t *);
74 static void chkausg(int, sym_t *);
75 static void chkvusg(int, sym_t *);
76 static void chklusg(sym_t *);
77 static void chktusg(sym_t *);
78 static void chkglvar(sym_t *);
79 static void glchksz(sym_t *);
82 * initializes all global vars used in declarations
94 0, 0, 0, 0, 0, "signed" } },
97 0, 0, 0, 0, 0, "unsigned" } },
98 { CHAR, { CHAR_BIT, CHAR_BIT,
100 1, 0, 0, 1, 1, "char" } },
101 { SCHAR, { CHAR_BIT, CHAR_BIT,
103 1, 0, 0, 1, 1, "signed char" } },
104 { UCHAR, { CHAR_BIT, CHAR_BIT,
106 1, 1, 0, 1, 1, "unsigned char" } },
107 { SHORT, { sizeof (short) * CHAR_BIT, 2 * CHAR_BIT,
109 1, 0, 0, 1, 1, "short" } },
110 { USHORT, { sizeof (u_short) * CHAR_BIT, 2 * CHAR_BIT,
112 1, 1, 0, 1, 1, "unsigned short" } },
113 { INT, { sizeof (int) * CHAR_BIT, 3 * CHAR_BIT,
115 1, 0, 0, 1, 1, "int" } },
116 { UINT, { sizeof (u_int) * CHAR_BIT, 3 * CHAR_BIT,
118 1, 1, 0, 1, 1, "unsigned int" } },
119 { LONG, { sizeof (long) * CHAR_BIT, 4 * CHAR_BIT,
121 1, 0, 0, 1, 1, "long" } },
122 { ULONG, { sizeof (u_long) * CHAR_BIT, 4 * CHAR_BIT,
124 1, 1, 0, 1, 1, "unsigned long" } },
125 { QUAD, { sizeof (quad_t) * CHAR_BIT, 8 * CHAR_BIT,
127 1, 0, 0, 1, 1, "long long" } },
128 { UQUAD, { sizeof (u_quad_t) * CHAR_BIT, 8 * CHAR_BIT,
130 1, 1, 0, 1, 1, "unsigned long long" } },
131 { FLOAT, { sizeof (float) * CHAR_BIT, 4 * CHAR_BIT,
133 0, 0, 1, 1, 1, "float" } },
134 { DOUBLE, { sizeof (double) * CHAR_BIT, 8 * CHAR_BIT,
136 0, 0, 1, 1, 1, "double" } },
137 { LDOUBLE, { sizeof (ldbl_t) * CHAR_BIT, 10 * CHAR_BIT,
139 0, 0, 1, 1, 1, "long double" } },
142 0, 0, 0, 0, 0, "void" } },
145 0, 0, 0, 0, 0, "struct" } },
148 0, 0, 0, 0, 0, "union" } },
149 { ENUM, { sizeof (int) * CHAR_BIT, 3 * CHAR_BIT,
151 1, 0, 0, 1, 1, "enum" } },
152 { PTR, { sizeof (void *) * CHAR_BIT, 4 * CHAR_BIT,
154 0, 1, 0, 0, 1, "pointer" } },
157 0, 0, 0, 0, 0, "array" } },
160 0, 0, 0, 0, 0, "function" } },
163 /* declaration stack */
164 dcs = xcalloc(1, sizeof (dinfo_t));
166 dcs->d_ldlsym = &dcs->d_dlsyms;
168 /* type information and classification */
169 for (i = 0; i < sizeof (ittab) / sizeof (ittab[0]); i++)
170 STRUCT_ASSIGN(ttab[ittab[i].it_tspec], ittab[i].it_ttab);
172 for (i = 0; i < NTSPEC; i++)
173 ttab[i].tt_psz = ttab[i].tt_sz;
176 /* shared type structures */
177 typetab = xcalloc(NTSPEC, sizeof (type_t));
178 for (i = 0; i < NTSPEC; i++)
179 typetab[i].t_tspec = NOTSPEC;
180 typetab[CHAR].t_tspec = CHAR;
181 typetab[SCHAR].t_tspec = SCHAR;
182 typetab[UCHAR].t_tspec = UCHAR;
183 typetab[SHORT].t_tspec = SHORT;
184 typetab[USHORT].t_tspec = USHORT;
185 typetab[INT].t_tspec = INT;
186 typetab[UINT].t_tspec = UINT;
187 typetab[LONG].t_tspec = LONG;
188 typetab[ULONG].t_tspec = ULONG;
189 typetab[QUAD].t_tspec = QUAD;
190 typetab[UQUAD].t_tspec = UQUAD;
191 typetab[FLOAT].t_tspec = FLOAT;
192 typetab[DOUBLE].t_tspec = DOUBLE;
193 typetab[LDOUBLE].t_tspec = LDOUBLE;
194 typetab[VOID].t_tspec = VOID;
196 * Next two are not real types. They are only used by the parser
197 * to return keywords "signed" and "unsigned"
199 typetab[SIGNED].t_tspec = SIGNED;
200 typetab[UNSIGN].t_tspec = UNSIGN;
204 * Returns a shared type structure vor arithmetic types and void.
206 * It's important do duplicate this structure (using duptyp() or tdupdyp())
207 * if it is to be modified (adding qualifiers or anything else).
212 return (&typetab[t]);
216 duptyp(const type_t *tp)
220 ntp = getblk(sizeof (type_t));
221 STRUCT_ASSIGN(*ntp, *tp);
226 * Use tduptyp() instead of duptyp() inside expressions (if the
227 * allocated memory should be freed after the expr).
230 tduptyp(const type_t *tp)
234 ntp = tgetblk(sizeof (type_t));
235 STRUCT_ASSIGN(*ntp, *tp);
240 * Returns 1 if the argument is void or an incomplete array,
241 * struct, union or enum type.
248 if ((t = tp->t_tspec) == VOID) {
250 } else if (t == ARRAY) {
251 return (tp->t_aincompl);
252 } else if (t == STRUCT || t == UNION) {
253 return (tp->t_str->sincompl);
254 } else if (t == ENUM) {
255 return (tp->t_enum->eincompl);
261 * Set the flag for (in)complete array, struct, union or enum
265 setcompl(type_t *tp, int ic)
269 if ((t = tp->t_tspec) == ARRAY) {
271 } else if (t == STRUCT || t == UNION) {
272 tp->t_str->sincompl = ic;
275 lerror("setcompl() 1");
276 tp->t_enum->eincompl = ic;
281 * Remember the storage class of the current declaration in dcs->d_scl
282 * (the top element of the declaration stack) and detect multiple
291 warning(10, "inline");
295 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
296 dcs->d_smod != NOTSPEC || dcs->d_lmod != NOTSPEC) {
297 /* storage class after type is obsolescent */
300 if (dcs->d_scl == NOSCL) {
304 * multiple storage classes. An error will be reported in
312 * Remember the type, modifier or typedef name returned by the parser
313 * in *dcs (top element of decl stack). This information is used in
314 * deftyp() to build the type used for all declarators in this
317 * Is tp->t_typedef 1, the type comes from a previously defined typename.
318 * Otherwise it comes from a type specifier (int, long, ...) or a
319 * struct/union/enum tag.
327 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
328 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) {
330 * something like "typedef int a; int a b;"
331 * This should not happen with current grammar.
341 if (t == STRUCT || t == UNION || t == ENUM) {
343 * something like "int struct a ..."
344 * struct/union/enum with anything else is not allowed
346 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
347 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) {
349 * remember that an error must be reported in
353 dcs->d_atyp = dcs->d_lmod = dcs->d_smod = NOTSPEC;
359 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
361 * something like "struct a int"
362 * struct/union/enum with anything else is not allowed
368 if (t == LONG && dcs->d_lmod == LONG) {
369 /* "long long" or "long ... long" */
371 dcs->d_lmod = NOTSPEC;
373 /* %s C does not support 'long long' */
374 (void)gnuism(265, tflag ? "traditional" : "ANSI");
377 if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
378 /* something like "typedef int a; a long ..." */
379 dcs->d_type = tdeferr(dcs->d_type, t);
383 /* now it can be only a combination of arithmetic types and void */
384 if (t == SIGNED || t == UNSIGN) {
385 /* remeber specifiers "signed" and "unsigned" in dcs->d_smod */
386 if (dcs->d_smod != NOTSPEC)
388 * more then one "signed" and/or "unsigned"; print
389 * an error in deftyp()
393 } else if (t == SHORT || t == LONG || t == QUAD) {
395 * remember specifiers "short", "long" and "long long" in
398 if (dcs->d_lmod != NOTSPEC)
399 /* more than one, print error in deftyp() */
404 * remember specifiers "void", "char", "int", "float" or
405 * "double" int dcs->d_atyp
407 if (dcs->d_atyp != NOTSPEC)
408 /* more than one, print error in deftyp() */
415 * called if a list of declaration specifiers contains a typedef name
416 * and other specifiers (except struct, union, enum, typedef name)
419 tdeferr(type_t *td, tspec_t t)
428 if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG ||
431 /* modifying typedef with ... */
432 warning(5, ttab[t].tt_name);
433 td = duptyp(gettyp(mrgtspec(t2, t)));
439 if (t2 == INT || t2 == UINT) {
440 /* modifying typedef with ... */
442 td = duptyp(gettyp(t2 == INT ? SHORT : USHORT));
448 if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG ||
449 t2 == FLOAT || t2 == DOUBLE) {
450 /* modifying typedef with ... */
454 } else if (t2 == UINT) {
456 } else if (t2 == LONG) {
458 } else if (t2 == ULONG) {
460 } else if (t2 == FLOAT) {
462 } else if (t2 == DOUBLE) {
463 td = gettyp(LDOUBLE);
470 /* LINTED (enumeration values not handled in switch) */
475 /* Anything other is not accepted. */
482 * Remember the symbol of a typedef name (2nd arg) in a struct, union
483 * or enum tag if the typedef name is the first defined for this tag.
485 * If the tag is unnamed, the typdef name is used for identification
486 * of this tag in lint2. Although its possible that more then one typedef
487 * name is defined for one tag, the first name defined should be unique
488 * if the tag is unnamed.
491 settdsym(type_t *tp, sym_t *sym)
495 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
496 if (tp->t_str->stdef == NULL)
497 tp->t_str->stdef = sym;
498 } else if (t == ENUM) {
499 if (tp->t_enum->etdef == NULL)
500 tp->t_enum->etdef = sym;
505 * Remember a qualifier which is part of the declaration specifiers
506 * (and not the declarator) in the top element of the declaration stack.
507 * Also detect multiple qualifiers of the same kind.
509 * The rememberd qualifier is used by deftyp() to construct the type
510 * for all declarators.
518 warning(10, "const");
523 lerror("addqual() 1");
524 if (dcs->d_volatile) {
526 warning(10, "volatile");
533 * Go to the next declaration level (structs, nested structs, blocks,
534 * argument declaration lists ...)
542 (void)printf("pushdecl(%d)\n", (int)sc);
544 /* put a new element on the declaration stack */
545 di = xcalloc(1, sizeof (dinfo_t));
549 di->d_ldlsym = &di->d_dlsyms;
553 * Go back to previous declaration level
561 (void)printf("popdecl(%d)\n", (int)dcs->d_ctx);
563 if (dcs->d_nxt == NULL)
564 lerror("popdecl() 1");
569 /* there is nothing after external declarations */
570 lerror("popdecl() 2");
576 * Symbols declared in (nested) structs or enums are
577 * part of the next level (they are removed from the
578 * symbol table if the symbols of the outher level are
581 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
582 dcs->d_ldlsym = di->d_ldlsym;
586 * All symbols in dcs->d_dlsyms are introduced in old style
587 * argument declarations (it's not clean, but possible).
588 * They are appended to the list of symbols declared in
589 * an old style argument identifier list or a new style
590 * parameter type list.
592 if (di->d_dlsyms != NULL) {
593 *di->d_ldlsym = dcs->d_fpsyms;
594 dcs->d_fpsyms = di->d_dlsyms;
600 * Append all symbols declared in the abstract declaration
601 * to the list of symbols declared in the surounding decl.
603 * XXX I'm not sure whether they should be removed from the
604 * symbol table now or later.
606 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
607 dcs->d_ldlsym = di->d_ldlsym;
610 /* check usage of local vars */
614 /* usage of arguments will be checked by funcend() */
615 rmsyms(di->d_dlsyms);
618 lerror("popdecl() 3");
624 * Set flag d_asm in all declaration stack elements up to the
627 * This is used to mark compound statements which have, possibly in
628 * nested compound statements, asm statements. For these compound
629 * statements no warnings about unused or unitialized variables are
632 * There is no need to clear d_asm in dinfo structs with context AUTO,
633 * because these structs are freed at the end of the compound statement.
634 * But it must be cleard in the outermost dinfo struct, which has
635 * context EXTERN. This could be done in clrtyp() and would work for
636 * C, but not for C++ (due to mixed statements and declarations). Thus
637 * we clear it in glclup(), which is used to do some cleanup after
638 * global declarations/definitions.
645 for (di = dcs; di != NULL; di = di->d_nxt)
650 * Clean all elements of the top element of declaration stack which
651 * will be used by the next declaration
656 dcs->d_atyp = dcs->d_smod = dcs->d_lmod = NOTSPEC;
659 dcs->d_const = dcs->d_volatile = 0;
661 dcs->d_mscl = dcs->d_terr = 0;
667 * Create a type structure from the informations gathered in
668 * the declaration stack.
669 * Complain about storage classes which are not possible in current
679 t = dcs->d_atyp; /* CHAR, INT, FLOAT, DOUBLE, VOID */
680 s = dcs->d_smod; /* SIGNED, UNSIGNED */
681 l = dcs->d_lmod; /* SHORT, LONG, QUAD */
685 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && tp == NULL)
688 if (tp != NULL && (t != NOTSPEC || s != NOTSPEC || l != NOTSPEC)) {
689 /* should never happen */
690 lerror("deftyp() 1");
713 /* use 'double' instead of ... */
722 /* 'long double' is illegal in ... */
729 lerror("deftyp() 2");
731 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) {
737 dcs->d_type = gettyp(mrgtspec(t, s));
741 /* only one storage class allowed */
745 /* illegal type combination */
749 if (dcs->d_ctx == EXTERN) {
750 if (scl == REG || scl == AUTO) {
751 /* illegal storage class */
755 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PARG) {
756 if (scl != NOSCL && scl != REG) {
757 /* only "register" valid ... */
765 if (dcs->d_const && dcs->d_type->t_const) {
766 if (!dcs->d_type->t_typedef)
767 lerror("deftyp() 3");
768 /* typedef already qualified with "%s" */
769 warning(68, "const");
771 if (dcs->d_volatile && dcs->d_type->t_volatile) {
772 if (!dcs->d_type->t_typedef)
773 lerror("deftyp() 4");
774 /* typedef already qualified with "%s" */
775 warning(68, "volatile");
778 if (dcs->d_const || dcs->d_volatile) {
779 dcs->d_type = duptyp(dcs->d_type);
780 dcs->d_type->t_const |= dcs->d_const;
781 dcs->d_type->t_volatile |= dcs->d_volatile;
786 * Merge type specifiers (char, ..., long long, signed, unsigned).
789 mrgtspec(tspec_t t, tspec_t s)
791 if (s == SIGNED || s == UNSIGN) {
793 t = s == SIGNED ? SCHAR : UCHAR;
794 } else if (t == SHORT) {
795 t = s == SIGNED ? SHORT : USHORT;
796 } else if (t == INT) {
797 t = s == SIGNED ? INT : UINT;
798 } else if (t == LONG) {
799 t = s == SIGNED ? LONG : ULONG;
800 } else if (t == QUAD) {
801 t = s == SIGNED ? QUAD : UQUAD;
809 * Return the length of a type in bit.
811 * Printing a message if the outhermost dimension of an array is 0 must
812 * be done by the caller. All other problems are reported by length()
813 * if name is not NULL.
816 length(type_t *tp, const char *name)
821 while (tp->t_tspec == ARRAY) {
825 switch (tp->t_tspec) {
827 /* compiler takes size of function */
828 lerror("%s", msgs[12]);
832 if (incompl(tp) && name != NULL) {
833 /* incomplete structure or union %s: %s */
834 error(31, tp->t_str->stag->s_name, name);
836 elsz = tp->t_str->size;
839 if (incompl(tp) && name != NULL) {
840 /* incomplete enum type: %s */
845 elsz = size(tp->t_tspec);
850 return (elem * elsz);
854 * Get the alignment of the given Type in bits.
862 while (tp->t_tspec == ARRAY)
865 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
866 a = tp->t_str->align;
867 } else if (t == FUNC) {
868 /* compiler takes alignment of function */
870 a = ALIGN(1) * CHAR_BIT;
872 if ((a = size(t)) == 0) {
874 } else if (a > ALIGN(1) * CHAR_BIT) {
875 a = ALIGN(1) * CHAR_BIT;
878 if (a < CHAR_BIT || a > ALIGN(1) * CHAR_BIT)
879 lerror("getbound() 1");
884 * Concatenate two lists of symbols by s_nxt. Used by declarations of
885 * struct/union/enum elements and parameters.
888 lnklst(sym_t *l1, sym_t *l2)
892 if ((l = l1) == NULL)
894 while (l1->s_nxt != NULL)
901 * Check if the type of the given symbol is valid and print an error
902 * message if it is not.
905 * - arrays of incomlete types or functions
906 * - functions returning arrays or functions
907 * - void types other than type of function or pointer
917 while ((tp = *tpp) != NULL) {
920 * If this is the type of an old style function definition,
921 * a better warning is printed in funcdef().
923 if (t == FUNC && !tp->t_proto &&
924 !(to == NOTSPEC && sym->s_osdef)) {
926 /* function declaration is not a prototype */
930 if (t == FUNC || t == ARRAY) {
931 /* function returns illegal type */
934 *tpp = incref(*tpp, PTR);
936 *tpp = incref((*tpp)->t_subt, PTR);
939 } else if (tp->t_const || tp->t_volatile) {
940 if (sflag) { /* XXX oder better !tflag ? */
941 /* function cannot return const... */
947 /* array of function is illegal */
951 } else if (t == ARRAY && tp->t_dim == 0) {
955 } else if (t == VOID) {
956 /* illegal use of void */
959 #if 0 /* errors are produced by length() */
960 } else if (incompl(tp)) {
961 /* array of incomplete type */
969 } else if (to == NOTSPEC && t == VOID) {
970 if (dcs->d_ctx == PARG) {
971 if (sym->s_scl != ABSTRACT) {
972 if (sym->s_name == unnamed)
974 /* void param cannot have name: %s */
975 error(61, sym->s_name);
978 } else if (dcs->d_ctx == ABSTRACT) {
980 } else if (sym->s_scl != TYPEDEF) {
981 /* void type for %s */
982 error(19, sym->s_name);
986 if (t == VOID && to != PTR) {
987 if (tp->t_const || tp->t_volatile) {
988 /* inappropriate qualifiers with "void" */
990 tp->t_const = tp->t_volatile = 0;
999 * Process the declarator of a struct/union element.
1002 decl1str(sym_t *dsym)
1009 if ((sc = dsym->s_scl) != MOS && sc != MOU)
1010 lerror("decl1str() 1");
1012 if (dcs->d_rdcsym != NULL) {
1013 if ((sc = dcs->d_rdcsym->s_scl) != MOS && sc != MOU)
1014 /* should be ensured by storesym() */
1015 lerror("decl1str() 2");
1016 if (dsym->s_styp == dcs->d_rdcsym->s_styp) {
1017 /* duplicate member name: %s */
1018 error(33, dsym->s_name);
1019 rmsym(dcs->d_rdcsym);
1025 t = (tp = dsym->s_type)->t_tspec;
1027 if (dsym->s_field) {
1031 * only unsigned und signed int are protable bit-field types
1032 *(at least in ANSI C, in traditional C only unsigned int)
1034 if (t == CHAR || t == UCHAR || t == SCHAR ||
1035 t == SHORT || t == USHORT || t == ENUM) {
1037 /* bit-field type '%s' invalid in ANSI C */
1038 warning(273, tyname(tp));
1040 /* nonportable bit-field type */
1043 } else if (t == INT && dcs->d_smod == NOTSPEC) {
1045 /* nonportable bit-field type */
1048 } else if (t != INT && t != UINT) {
1049 /* illegal bit-field type */
1052 dsym->s_type = tp = duptyp(gettyp(t = INT));
1053 if ((tp->t_flen = sz) > size(t))
1054 tp->t_flen = size(t);
1056 if ((len = tp->t_flen) < 0 || len > size(t)) {
1057 /* illegal bit-field size */
1059 tp->t_flen = size(t);
1060 } else if (len == 0 && dsym->s_name != unnamed) {
1061 /* zero size bit-field */
1063 tp->t_flen = size(t);
1065 if (dsym->s_scl == MOU) {
1066 /* illegal use of bit-field */
1068 dsym->s_type->t_isfield = 0;
1071 } else if (t == FUNC) {
1072 /* function illegal in structure or union */
1074 dsym->s_type = tp = incref(tp, t = PTR);
1078 * bit-fields of length 0 are not warned about because length()
1079 * does not return the length of the bit-field but the length
1080 * of the type the bit-field is packed in (its ok)
1082 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1083 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1084 /* illegal zero sized structure member: %s */
1085 warning(39, dsym->s_name);
1089 if (dcs->d_ctx == MOU) {
1093 if (dsym->s_field) {
1094 align(getbound(tp), tp->t_flen);
1095 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t);
1096 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1097 dcs->d_offset += tp->t_flen;
1099 align(getbound(tp), 0);
1100 dsym->s_value.v_quad = dcs->d_offset;
1101 dcs->d_offset += sz;
1103 if (dcs->d_ctx == MOU) {
1104 if (o > dcs->d_offset)
1114 * Aligns next structure element as required.
1116 * al contains the required alignment, len the length of a bit-field.
1119 align(int al, int len)
1124 * The alignment of the current element becomes the alignment of
1125 * the struct/union if it is larger than the current alignment
1126 * of the struct/union.
1128 if (al > dcs->d_stralign)
1129 dcs->d_stralign = al;
1131 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1132 if (len == 0 || dcs->d_offset + len > no)
1137 * Remember the width of the field in its type structure.
1140 bitfield(sym_t *dsym, int len)
1143 dsym = getblk(sizeof (sym_t));
1144 dsym->s_name = unnamed;
1145 dsym->s_kind = FMOS;
1147 dsym->s_type = gettyp(INT);
1148 dsym->s_blklev = -1;
1150 dsym->s_type = duptyp(dsym->s_type);
1151 dsym->s_type->t_isfield = 1;
1152 dsym->s_type->t_flen = len;
1158 * Collect informations about a sequence of asterisks and qualifiers
1159 * in a list of type pqinf_t.
1160 * Qualifiers refer always to the left asterisk. The rightmost asterisk
1161 * will be at the top of the list.
1164 mergepq(pqinf_t *p1, pqinf_t *p2)
1168 if (p2->p_pcnt != 0) {
1169 /* left '*' at the end of the list */
1170 for (p = p2; p->p_nxt != NULL; p = p->p_nxt) ;
1177 warning(10, "const");
1181 if (p2->p_volatile) {
1182 if (p1->p_volatile) {
1184 warning(10, "volatile");
1194 * Followint 3 functions extend the type of a declarator with
1195 * pointer, function and array types.
1197 * The current type is the Type built by deftyp() (dcs->d_type) and
1198 * pointer, function and array types already added for this
1199 * declarator. The new type extension is inserted between both.
1202 addptr(sym_t *decl, pqinf_t *pi)
1207 tpp = &decl->s_type;
1208 while (*tpp != dcs->d_type)
1209 tpp = &(*tpp)->t_subt;
1211 while (pi != NULL) {
1212 *tpp = tp = getblk(sizeof (type_t));
1214 tp->t_const = pi->p_const;
1215 tp->t_volatile = pi->p_volatile;
1216 *(tpp = &tp->t_subt) = dcs->d_type;
1225 * If a dimension was specified, dim is 1, otherwise 0
1226 * n is the specified dimension
1229 addarray(sym_t *decl, int dim, int n)
1233 tpp = &decl->s_type;
1234 while (*tpp != dcs->d_type)
1235 tpp = &(*tpp)->t_subt;
1237 *tpp = tp = getblk(sizeof (type_t));
1238 tp->t_tspec = ARRAY;
1239 tp->t_subt = dcs->d_type;
1243 /* zero or negative array dimension */
1246 } else if (n == 0 && dim) {
1247 /* zero or negative array dimension */
1249 } else if (n == 0 && !dim) {
1250 /* is incomplete type */
1258 addfunc(sym_t *decl, sym_t *args)
1264 /* function prototypes are illegal in traditional C */
1266 args = nsfunc(decl, args);
1272 * The symbols are removed from the symbol table by popdecl() after
1273 * addfunc(). To be able to restore them if this is a function
1274 * definition, a pointer to the list of all symbols is stored in
1275 * dcs->d_nxt->d_fpsyms. Also a list of the arguments (concatenated
1276 * by s_nxt) is stored in dcs->d_nxt->d_fargs.
1277 * (dcs->d_nxt must be used because *dcs is the declaration stack
1278 * element created for the list of params and is removed after
1281 if (dcs->d_nxt->d_ctx == EXTERN &&
1282 decl->s_type == dcs->d_nxt->d_type) {
1283 dcs->d_nxt->d_fpsyms = dcs->d_dlsyms;
1284 dcs->d_nxt->d_fargs = args;
1287 tpp = &decl->s_type;
1288 while (*tpp != dcs->d_nxt->d_type)
1289 tpp = &(*tpp)->t_subt;
1291 *tpp = tp = getblk(sizeof (type_t));
1293 tp->t_subt = dcs->d_nxt->d_type;
1294 if ((tp->t_proto = dcs->d_proto) != 0)
1296 tp->t_vararg = dcs->d_vararg;
1302 * Called for new style function declarations.
1306 nsfunc(sym_t *decl, sym_t *args)
1313 * Declarations of structs/unions/enums in param lists are legal,
1316 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1318 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) {
1319 /* dubious tag declaration: %s %s */
1320 warning(85, scltoa(sc), sym->s_name);
1325 for (arg = args; arg != NULL; arg = arg->s_nxt) {
1326 if (arg->s_type->t_tspec == VOID) {
1327 if (n > 1 || arg->s_nxt != NULL) {
1328 /* "void" must be sole parameter */
1330 arg->s_type = gettyp(INT);
1336 /* return NULL if first param is VOID */
1337 return (args != NULL && args->s_type->t_tspec != VOID ? args : NULL);
1341 * Called for old style function declarations.
1344 osfunc(sym_t *decl, sym_t *args)
1347 * Remember list of params only if this is really seams to be
1348 * a function definition.
1350 if (dcs->d_nxt->d_ctx == EXTERN &&
1351 decl->s_type == dcs->d_nxt->d_type) {
1353 * We assume that this becomes a function definition. If
1354 * we are wrong, its corrected in chkfdef().
1358 decl->s_args = args;
1362 /* function prototype parameters must have types */
1368 * Lists of Identifiers in functions declarations are allowed only if
1369 * its also a function definition. If this is not the case, print a
1373 chkfdef(sym_t *sym, int msg)
1377 /* incomplete or misplaced function definition */
1386 * Process the name in a declarator.
1387 * If the symbol does already exists, a new one is created.
1388 * The symbol becomes one of the storage classes EXTERN, STATIC, AUTO or
1390 * s_def and s_reg are valid after dname().
1397 if (sym->s_scl == NOSCL) {
1398 dcs->d_rdcsym = NULL;
1399 } else if (sym->s_defarg) {
1401 dcs->d_rdcsym = NULL;
1403 dcs->d_rdcsym = sym;
1404 sym = pushdown(sym);
1407 switch (dcs->d_ctx) {
1411 sym->s_styp = dcs->d_tagtyp->t_str;
1413 sym->s_value.v_tspec = INT;
1418 * static and external symbols without "extern" are
1419 * considered to be tentative defined, external
1420 * symbols with "extern" are declared, and typedef names
1421 * are defined. Tentative defined and declared symbols
1422 * may become defined if an initializer is present or
1423 * this is a function definition.
1425 if ((sc = dcs->d_scl) == NOSCL) {
1428 } else if (sc == STATIC) {
1430 } else if (sc == TYPEDEF) {
1432 } else if (sc == EXTERN) {
1435 lerror("dname() 1");
1442 if ((sc = dcs->d_scl) == NOSCL) {
1444 } else if (sc == REG) {
1448 lerror("dname() 2");
1453 if ((sc = dcs->d_scl) == NOSCL) {
1455 * XXX somewhat ugly because we dont know whether
1456 * this is AUTO or EXTERN (functions). If we are
1457 * wrong it must be corrected in decl1loc(), where
1458 * we have the neccessary type information.
1462 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1464 } else if (sc == REG) {
1468 } else if (sc == EXTERN) {
1471 lerror("dname() 3");
1475 lerror("dname() 4");
1479 sym->s_type = dcs->d_type;
1481 dcs->d_fpsyms = NULL;
1487 * Process a name in the list of formal params in an old style function
1493 if (sym->s_scl != NOSCL) {
1494 if (blklev == sym->s_blklev) {
1495 /* redeclaration of formal parameter %s */
1496 error(21, sym->s_name);
1500 sym = pushdown(sym);
1502 sym->s_type = gettyp(INT);
1505 sym->s_defarg = sym->s_arg = 1;
1510 * Create the type of a tag.
1512 * tag points to the symbol table entry of the tag
1513 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1514 * decl is 1 if the type of the tag will be completed in this declaration
1515 * (the following token is T_LBRACE)
1516 * semi is 1 if the following token is T_SEMI
1519 mktag(sym_t *tag, tspec_t kind, int decl, int semi)
1524 if (kind == STRUCT) {
1526 } else if (kind == UNION) {
1528 } else if (kind == ENUM) {
1535 if (tag->s_scl != NOSCL) {
1536 tag = newtag(tag, scl, decl, semi);
1538 /* a new tag, no empty declaration */
1539 dcs->d_nxt->d_nedecl = 1;
1540 if (scl == ENUMTAG && !decl) {
1541 if (!tflag && (sflag || pflag))
1542 /* forward reference to enum type */
1546 if (tag->s_scl == NOSCL) {
1548 tag->s_type = tp = getblk(sizeof (type_t));
1553 tag = getblk(sizeof (sym_t));
1554 tag->s_name = unnamed;
1555 STRUCT_ASSIGN(tag->s_dpos, curr_pos);
1559 tag->s_type = tp = getblk(sizeof (type_t));
1560 dcs->d_nxt->d_nedecl = 1;
1563 if (tp->t_tspec == NOTSPEC) {
1566 tp->t_str = getblk(sizeof (str_t));
1567 tp->t_str->align = CHAR_BIT;
1568 tp->t_str->stag = tag;
1571 tp->t_enum = getblk(sizeof (enum_t));
1572 tp->t_enum->etag = tag;
1574 /* ist unvollstaendiger Typ */
1582 * Checks all possible cases of tag redeclarations.
1583 * decl is 1 if T_LBRACE follows
1584 * semi is 1 if T_SEMI follows
1587 newtag(sym_t *tag, scl_t scl, int decl, int semi)
1589 if (tag->s_blklev < blklev) {
1594 /* decl. introduces new type ... */
1595 warning(44, scltoa(scl), tag->s_name);
1596 tag = pushdown(tag);
1597 } else if (tag->s_scl != scl) {
1598 /* base type is really "%s %s" */
1599 warning(45, scltoa(tag->s_scl), tag->s_name);
1601 dcs->d_nxt->d_nedecl = 1;
1603 /* "struct a { ..." */
1605 /* redefinition hides earlier one: %s */
1606 warning(43, tag->s_name);
1607 tag = pushdown(tag);
1608 dcs->d_nxt->d_nedecl = 1;
1609 } else if (tag->s_scl != scl) {
1610 /* base type is really "%s %s" */
1611 warning(45, scltoa(tag->s_scl), tag->s_name);
1612 /* declaration introduces new type in ANSI C: %s %s */
1614 warning(44, scltoa(scl), tag->s_name);
1615 tag = pushdown(tag);
1616 dcs->d_nxt->d_nedecl = 1;
1619 if (tag->s_scl != scl) {
1620 /* (%s) tag redeclared */
1621 error(46, scltoa(tag->s_scl));
1623 tag = pushdown(tag);
1624 dcs->d_nxt->d_nedecl = 1;
1625 } else if (decl && !incompl(tag->s_type)) {
1626 /* (%s) tag redeclared */
1627 error(46, scltoa(tag->s_scl));
1629 tag = pushdown(tag);
1630 dcs->d_nxt->d_nedecl = 1;
1631 } else if (semi || decl) {
1632 dcs->d_nxt->d_nedecl = 1;
1644 case EXTERN: s = "extern"; break;
1645 case STATIC: s = "static"; break;
1646 case AUTO: s = "auto"; break;
1647 case REG: s = "register"; break;
1648 case TYPEDEF: s = "typedef"; break;
1649 case STRTAG: s = "struct"; break;
1650 case UNIONTAG: s = "union"; break;
1651 case ENUMTAG: s = "enum"; break;
1652 default: lerror("tagttoa()");
1658 * Completes the type of a tag in a struct/union/enum declaration.
1659 * tp points to the type of the, tag, fmem to the list of members/enums.
1662 compltag(type_t *tp, sym_t *fmem)
1669 /* from now a complete type */
1672 if ((t = tp->t_tspec) != ENUM) {
1673 align(dcs->d_stralign, 0);
1675 sp->align = dcs->d_stralign;
1676 sp->size = dcs->d_offset;
1678 if (sp->size == 0) {
1680 (void)gnuism(47, ttab[t].tt_name);
1683 for (mem = fmem; mem != NULL; mem = mem->s_nxt) {
1684 if (mem->s_name != unnamed)
1688 /* %s has no named members */
1690 t == STRUCT ? "structure" : "union");
1694 tp->t_enum->elem = fmem;
1700 * Processes the name of an enumerator in en enum declaration.
1702 * sym points to the enumerator
1703 * val is the value of the enumerator
1704 * impl is 1 if the the value of the enumerator was not explicit specified.
1707 ename(sym_t *sym, int val, int impl)
1710 if (sym->s_blklev == blklev) {
1711 /* no hflag, because this is illegal!!! */
1713 /* enumeration constant hides parameter: %s */
1714 warning(57, sym->s_name);
1716 /* redeclaration of %s */
1717 error(27, sym->s_name);
1719 * inside blocks it should not too complicated
1720 * to find the position of the previous
1728 /* redefinition hides earlier one: %s */
1729 warning(43, sym->s_name);
1731 sym = pushdown(sym);
1733 sym->s_scl = ENUMCON;
1734 sym->s_type = dcs->d_tagtyp;
1735 sym->s_value.v_tspec = INT;
1736 sym->s_value.v_quad = val;
1737 if (impl && val - 1 == INT_MAX) {
1738 /* overflow in enumeration values: %s */
1739 warning(48, sym->s_name);
1746 * Process a single external declarator.
1749 decl1ext(sym_t *dsym, int initflg)
1751 int warn, rval, redec;
1758 if (initflg && !(initerr = chkinit(dsym)))
1762 * Declarations of functions are marked as "tentative" in dname().
1763 * This is wrong because there are no tentative function
1766 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1769 if (dcs->d_inline) {
1770 if (dsym->s_type->t_tspec == FUNC) {
1773 /* variable declared inline: %s */
1774 warning(268, dsym->s_name);
1778 /* Write the declaration into the output file */
1779 if (plibflg && llibflg &&
1780 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1782 * With both LINTLIBRARY and PROTOLIB the prototyp is
1783 * written as a function definition to the output file.
1785 rval = dsym->s_type->t_subt->t_tspec != VOID;
1786 outfdef(dsym, &dsym->s_dpos, rval, 0, NULL);
1788 outsym(dsym, dsym->s_scl, dsym->s_def);
1791 if ((rdsym = dcs->d_rdcsym) != NULL) {
1794 * If the old symbol stems from a old style function definition
1795 * we have remembered the params in rdsmy->s_args and compare
1796 * them with the params of the prototype.
1798 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1799 redec = chkosdef(rdsym, dsym);
1804 if (!redec && !isredec(dsym, (warn = 0, &warn))) {
1807 /* redeclaration of %s */
1808 (*(sflag ? error : warning))(27, dsym->s_name);
1809 prevdecl(-1, rdsym);
1813 * Overtake the rememberd params if the new symbol
1814 * is not a prototype.
1816 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1817 dsym->s_osdef = rdsym->s_osdef;
1818 dsym->s_args = rdsym->s_args;
1819 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1823 * Remember the position of the declaration if the
1824 * old symbol was a prototype and the new is not.
1825 * Also remember the position if the old symbol
1826 * was defined and the new is not.
1828 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1829 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1830 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1831 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1835 * Copy informations about usage of the name into
1838 cpuinfo(dsym, rdsym);
1840 /* Once a name is defined, it remains defined. */
1841 if (rdsym->s_def == DEF)
1844 /* once a function is inline, it remains inline */
1845 if (rdsym->s_inline)
1848 compltyp(dsym, rdsym);
1855 if (dsym->s_scl == TYPEDEF) {
1856 dsym->s_type = duptyp(dsym->s_type);
1857 dsym->s_type->t_typedef = 1;
1858 settdsym(dsym->s_type, dsym);
1864 * Copies informations about usage into a new symbol table entry of
1868 cpuinfo(sym_t *sym, sym_t *rdsym)
1870 sym->s_spos = rdsym->s_spos;
1871 sym->s_upos = rdsym->s_upos;
1872 sym->s_set = rdsym->s_set;
1873 sym->s_used = rdsym->s_used;
1877 * Prints an error and returns 1 if a symbol is redeclared/redefined.
1878 * Otherwise returns 0 and, in some cases of minor problems, prints
1882 isredec(sym_t *dsym, int *warn)
1886 if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
1887 /* redeclaration of %s */
1888 error(27, dsym->s_name);
1892 if (rsym->s_scl == TYPEDEF) {
1893 /* typedef redeclared: %s */
1894 error(89, dsym->s_name);
1898 if (dsym->s_scl == TYPEDEF) {
1899 /* redeclaration of %s */
1900 error(27, dsym->s_name);
1904 if (rsym->s_def == DEF && dsym->s_def == DEF) {
1905 /* redefinition of %s */
1906 error(28, dsym->s_name);
1910 if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, warn)) {
1911 /* redeclaration of %s */
1912 error(27, dsym->s_name);
1916 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
1918 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
1920 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
1922 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
1924 * All cases except "int a = 1; static int a;" are catched
1925 * above with or without a warning
1927 /* redeclaration of %s */
1928 error(27, dsym->s_name);
1932 if (rsym->s_scl == EXTERN) {
1933 /* previously declared extern, becomes static: %s */
1934 warning(29, dsym->s_name);
1940 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
1942 /* redeclaration of %s; ANSI C requires "static" */
1944 warning(30, dsym->s_name);
1947 dsym->s_scl = STATIC;
1952 * Checks if two types are compatible. Returns 0 if not, otherwise 1.
1954 * ignqual ignore qualifiers of type; used for function params
1955 * promot promote left type; used for comparision of params of
1956 * old style function definitions with params of prototypes.
1957 * *warn set to 1 if an old style function declaration is not
1958 * compatible with a prototype
1961 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *warn)
1965 while (tp1 != NULL && tp2 != NULL) {
1971 } else if (t == CHAR || t == SCHAR) {
1973 } else if (t == UCHAR) {
1974 t = tflag ? UINT : INT;
1975 } else if (t == SHORT) {
1977 } else if (t == USHORT) {
1979 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
1983 if (t != tp2->t_tspec)
1986 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
1989 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
1992 if (t == STRUCT || t == UNION)
1993 return (tp1->t_str == tp2->t_str);
1995 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
1996 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2000 /* dont check prototypes for traditional */
2001 if (t == FUNC && !tflag) {
2002 if (tp1->t_proto && tp2->t_proto) {
2003 if (!eqargs(tp1, tp2, warn))
2005 } else if (tp1->t_proto) {
2006 if (!mnoarg(tp1, warn))
2008 } else if (tp2->t_proto) {
2009 if (!mnoarg(tp2, warn))
2016 ignqual = promot = 0;
2020 return (tp1 == tp2);
2024 * Compares the parameter types of two prototypes.
2027 eqargs(type_t *tp1, type_t *tp2, int *warn)
2031 if (tp1->t_vararg != tp2->t_vararg)
2037 while (a1 != NULL && a2 != NULL) {
2039 if (eqtype(a1->s_type, a2->s_type, 1, 0, warn) == 0)
2051 * mnoarg() (matches functions with no argument type information)
2052 * returns 1 if all parameters of a prototype are compatible with
2053 * and old style function declaration.
2054 * This is the case if following conditions are met:
2055 * 1. the prototype must have a fixed number of parameters
2056 * 2. no parameter is of type float
2057 * 3. no parameter is converted to another type if integer promotion
2061 mnoarg(type_t *tp, int *warn)
2070 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) {
2071 if ((t = arg->s_type->t_tspec) == FLOAT ||
2072 t == CHAR || t == SCHAR || t == UCHAR ||
2073 t == SHORT || t == USHORT) {
2082 * Compares a prototype declaration with the remembered arguments of
2083 * a previous old style function definition.
2086 chkosdef(sym_t *rdsym, sym_t *dsym)
2088 sym_t *args, *pargs, *arg, *parg;
2092 args = rdsym->s_args;
2093 pargs = dsym->s_type->t_args;
2098 for (arg = args; arg != NULL; arg = arg->s_nxt)
2100 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2102 if (narg != nparg) {
2103 /* prototype does not match old-style definition */
2115 * If it does not match due to promotion and sflag is
2116 * not set we print only a warning.
2118 if (!eqtype(arg->s_type, parg->s_type, 1, 1, &warn) || warn) {
2119 /* prototype does not match old-style def., arg #%d */
2130 /* old style definition */
2131 prevdecl(300, rdsym);
2137 * Complets a type by copying the dimension and prototype information
2138 * from a second compatible type.
2140 * Following lines are legal:
2141 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2142 * "typedef ft(); ft f; f(int); ft g; g(long);"
2143 * This means that, if a type is completed, the type structure must
2147 compltyp(sym_t *dsym, sym_t *ssym)
2149 type_t **dstp, *src;
2152 dstp = &dsym->s_type;
2155 while ((dst = *dstp) != NULL) {
2156 if (src == NULL || dst->t_tspec != src->t_tspec)
2157 lerror("compltyp() 1");
2158 if (dst->t_tspec == ARRAY) {
2159 if (dst->t_dim == 0 && src->t_dim != 0) {
2160 *dstp = dst = duptyp(dst);
2161 dst->t_dim = src->t_dim;
2162 /* now a complete Typ */
2165 } else if (dst->t_tspec == FUNC) {
2166 if (!dst->t_proto && src->t_proto) {
2167 *dstp = dst = duptyp(dst);
2169 dst->t_args = src->t_args;
2172 dstp = &dst->t_subt;
2178 * Completes the declaration of a single argument.
2181 decl1arg(sym_t *sym, int initflg)
2189 if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) {
2190 /* redeclaration of formal parameter %s */
2191 error(237, sym->s_name);
2192 rmsym(dcs->d_rdcsym);
2197 /* declared argument %s is missing */
2198 error(53, sym->s_name);
2203 /* cannot initialize parameter: %s */
2204 error(52, sym->s_name);
2208 if ((t = sym->s_type->t_tspec) == ARRAY) {
2209 sym->s_type = incref(sym->s_type->t_subt, PTR);
2210 } else if (t == FUNC) {
2212 /* a function is declared as an argument: %s */
2213 warning(50, sym->s_name);
2214 sym->s_type = incref(sym->s_type, PTR);
2215 } else if (t == FLOAT) {
2217 sym->s_type = gettyp(DOUBLE);
2221 /* argument declared inline: %s */
2222 warning(269, sym->s_name);
2225 * Arguments must have complete types. lengths() prints the needed
2226 * error messages (null dimension is impossible because arrays are
2227 * converted to pointers).
2229 if (sym->s_type->t_tspec != VOID)
2230 (void)length(sym->s_type, sym->s_name);
2238 * Does some checks for lint directives which apply to functions.
2239 * Processes arguments in old style function definitions which default
2241 * Checks compatiblility of old style function definition with previous
2247 sym_t *args, *arg, *pargs, *parg;
2248 int narg, nparg, n, msg;
2251 args = funcsym->s_args;
2252 pargs = funcsym->s_type->t_args;
2254 /* check for illegal combinations of lint directives */
2255 if (prflstrg != -1 && scflstrg != -1) {
2256 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2258 prflstrg = scflstrg = -1;
2260 if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) {
2261 /* dubious use of ** VARARGS ** with ** %s ** */
2262 warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2267 * check if the argument of a lint directive is compatible with the
2268 * number of arguments.
2271 for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_nxt)
2273 if (nargusg > narg) {
2274 /* argument number mismatch with directive: ** %s ** */
2275 warning(283, "ARGSUSED");
2278 if (nvararg > narg) {
2279 /* argument number mismatch with directive: ** %s ** */
2280 warning(283, "VARARGS");
2283 if (prflstrg > narg) {
2284 /* argument number mismatch with directive: ** %s ** */
2285 warning(283, "PRINTFLIKE");
2287 } else if (prflstrg == 0) {
2290 if (scflstrg > narg) {
2291 /* argument number mismatch with directive: ** %s ** */
2292 warning(283, "SCANFLIKE");
2294 } else if (scflstrg == 0) {
2297 if (prflstrg != -1 || scflstrg != -1) {
2298 narg = prflstrg != -1 ? prflstrg : scflstrg;
2300 for (n = 1; n < narg; n++)
2302 if (arg->s_type->t_tspec != PTR ||
2303 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2304 t != UCHAR && t != SCHAR)) {
2305 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2307 prflstrg = scflstrg = -1;
2312 * print a warning for each argument off an old style function
2313 * definition which defaults to int
2315 for (arg = args; arg != NULL; arg = arg->s_nxt) {
2316 if (arg->s_defarg) {
2317 /* argument type defaults to int: %s */
2318 warning(32, arg->s_name);
2325 * If this is an old style function definition and a prototyp
2326 * exists, compare the types of arguments.
2328 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2330 * If the number of arguments does not macht, we need not
2335 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2337 for (arg = args; arg != NULL; arg = arg->s_nxt)
2339 if (narg != nparg) {
2340 /* parameter mismatch: %d declared, %d defined */
2341 error(51, nparg, narg);
2347 msg |= chkptdecl(arg, parg);
2353 /* prototype declaration */
2354 prevdecl(285, dcs->d_rdcsym);
2356 /* from now the prototype is valid */
2357 funcsym->s_osdef = 0;
2358 funcsym->s_args = NULL;
2365 * Checks compatibility of an old style function definition with a previous
2366 * prototype declaration.
2367 * Returns 1 if the position of the previous declaration should be reported.
2370 chkptdecl(sym_t *arg, sym_t *parg)
2381 if (!eqtype(tp, ptp, 1, 1, &warn)) {
2382 if (eqtype(tp, ptp, 1, 0, &warn)) {
2383 /* type does not match prototype: %s */
2384 msg = gnuism(58, arg->s_name);
2386 /* type does not match prototype: %s */
2387 error(58, arg->s_name);
2391 /* type does not match prototype: %s */
2392 (*(sflag ? error : warning))(58, arg->s_name);
2400 * Completes a single local declaration/definition.
2403 decl1loc(sym_t *dsym, int initflg)
2405 /* Correct a mistake done in dname(). */
2406 if (dsym->s_type->t_tspec == FUNC) {
2408 if (dcs->d_scl == NOSCL)
2409 dsym->s_scl = EXTERN;
2412 if (dsym->s_type->t_tspec == FUNC) {
2413 if (dsym->s_scl == STATIC) {
2414 /* dubious static function at block level: %s */
2415 warning(93, dsym->s_name);
2416 dsym->s_scl = EXTERN;
2417 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2418 /* function has illegal storage class: %s */
2419 error(94, dsym->s_name);
2420 dsym->s_scl = EXTERN;
2425 * functions may be declared inline at local scope, although
2426 * this has no effect for a later definition of the same
2428 * XXX it should have an effect if tflag is set. this would
2429 * also be the way gcc behaves.
2431 if (dcs->d_inline) {
2432 if (dsym->s_type->t_tspec == FUNC) {
2435 /* variable declared inline: %s */
2436 warning(268, dsym->s_name);
2444 if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN)
2447 if (dsym->s_scl == EXTERN) {
2449 * XXX wenn die statische Variable auf Ebene 0 erst
2450 * spaeter definiert wird, haben wir die Brille auf.
2452 if (dsym->s_xsym == NULL) {
2453 outsym(dsym, EXTERN, dsym->s_def);
2455 outsym(dsym, dsym->s_xsym->s_scl, dsym->s_def);
2459 if (dcs->d_rdcsym != NULL) {
2461 if (dcs->d_rdcsym->s_blklev == 0) {
2463 switch (dsym->s_scl) {
2465 /* automatic hides external declaration: %s */
2467 warning(86, dsym->s_name);
2470 /* static hides external declaration: %s */
2472 warning(87, dsym->s_name);
2475 /* typedef hides external declaration: %s */
2477 warning(88, dsym->s_name);
2481 * Warnings and errors are printed in ledecl()
2485 lerror("decl1loc() 1");
2488 } else if (dcs->d_rdcsym->s_blklev == blklev) {
2490 /* no hflag, because its illegal! */
2491 if (dcs->d_rdcsym->s_arg) {
2493 * if !tflag, a "redeclaration of %s" error
2498 /* decl. hides parameter: %s */
2499 warning(91, dsym->s_name);
2500 rmsym(dcs->d_rdcsym);
2504 } else if (dcs->d_rdcsym->s_blklev < blklev) {
2507 /* declaration hides earlier one: %s */
2508 warning(95, dsym->s_name);
2512 if (dcs->d_rdcsym->s_blklev == blklev) {
2514 /* redeclaration of %s */
2515 error(27, dsym->s_name);
2516 rmsym(dcs->d_rdcsym);
2522 if (initflg && !(initerr = chkinit(dsym))) {
2527 if (dsym->s_scl == TYPEDEF) {
2528 dsym->s_type = duptyp(dsym->s_type);
2529 dsym->s_type->t_typedef = 1;
2530 settdsym(dsym->s_type, dsym);
2534 * Before we can check the size we must wait for a initialisation
2540 * Processes (re)declarations of external Symbols inside blocks.
2548 /* look for a symbol with the same name */
2549 esym = dcs->d_rdcsym;
2550 while (esym != NULL && esym->s_blklev != 0) {
2551 while ((esym = esym->s_link) != NULL) {
2552 if (esym->s_kind != FVFT)
2554 if (strcmp(dsym->s_name, esym->s_name) == 0)
2560 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2561 /* gcc accepts this without a warning, pcc prints an error. */
2562 /* redeclaration of %s */
2563 warning(27, dsym->s_name);
2569 eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &warn);
2572 if (esym->s_scl == EXTERN) {
2573 /* inconsistent redeclaration of extern: %s */
2574 warning(90, dsym->s_name);
2577 /* inconsistent redeclaration of static: %s */
2578 warning(92, dsym->s_name);
2585 * Remember the external symbol so we can update usage
2586 * information at the end of the block.
2588 dsym->s_xsym = esym;
2593 * Print an error or a warning if the symbol cant be initialized due
2594 * to type/storage class. Returnvalue is 1 if an error has been
2604 if (sym->s_type->t_tspec == FUNC) {
2605 /* cannot initialize function: %s */
2606 error(24, sym->s_name);
2608 } else if (sym->s_scl == TYPEDEF) {
2609 /* cannot initialize typedef: %s */
2610 error(25, sym->s_name);
2612 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2613 /* cannot initialize "extern" declaration: %s */
2614 if (dcs->d_ctx == EXTERN) {
2615 warning(26, sym->s_name);
2617 error(26, sym->s_name);
2626 * Create a symbole for an abstract declaration.
2633 if (dcs->d_ctx != ABSTRACT && dcs->d_ctx != PARG)
2636 sym = getblk(sizeof (sym_t));
2638 sym->s_name = unnamed;
2640 sym->s_scl = ABSTRACT;
2643 if (dcs->d_ctx == PARG)
2646 sym->s_type = dcs->d_type;
2647 dcs->d_rdcsym = NULL;
2654 * Removes anything which has nothing to do on global level.
2659 while (dcs->d_nxt != NULL)
2667 * remove all informations about pending lint directives without
2674 * Process an abstract type declaration
2677 decl1abs(sym_t *sym)
2685 * Checks size after declarations of variables and their initialisation.
2691 * check size only for symbols which are defined and no function and
2694 if (dsym->s_def != DEF)
2696 if (dsym->s_scl == TYPEDEF)
2698 if (dsym->s_type->t_tspec == FUNC)
2701 if (length(dsym->s_type, dsym->s_name) == 0 &&
2702 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2703 /* empty array declaration: %s */
2705 warning(190, dsym->s_name);
2707 error(190, dsym->s_name);
2713 * Mark an object as set if it is not already
2720 STRUCT_ASSIGN(sym->s_spos, curr_pos);
2725 * Mark an object as used if it is not already
2728 setuflg(sym_t *sym, int fcall, int szof)
2732 STRUCT_ASSIGN(sym->s_upos, curr_pos);
2735 * for function calls another record is written
2737 * XXX Should symbols used in sizeof() treated as used or not?
2738 * Probably not, because there is no sense to declare an
2739 * external variable only to get their size.
2741 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2746 * Prints warnings for a list of variables and labels (concatenated
2747 * with s_dlnxt) if these are not used or only set.
2750 chkusage(dinfo_t *di)
2755 /* for this warnings LINTED has no effect */
2759 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
2760 chkusg1(di->d_asm, sym);
2766 * Prints a warning for a single variable or label if it is not used or
2770 chkusg1(int novar, sym_t *sym)
2774 if (sym->s_blklev == -1)
2777 STRUCT_ASSIGN(cpos, curr_pos);
2779 if (sym->s_kind == FVFT) {
2781 chkausg(novar, sym);
2783 chkvusg(novar, sym);
2785 } else if (sym->s_kind == FLAB) {
2787 } else if (sym->s_kind == FTAG) {
2791 STRUCT_ASSIGN(curr_pos, cpos);
2795 chkausg(int novar, sym_t *arg)
2798 lerror("chkausg() 1");
2803 if (!arg->s_used && vflag) {
2804 STRUCT_ASSIGN(curr_pos, arg->s_dpos);
2805 /* argument %s unused in function %s */
2806 warning(231, arg->s_name, funcsym->s_name);
2811 chkvusg(int novar, sym_t *sym)
2816 if (blklev == 0 || sym->s_blklev == 0)
2817 lerror("chkvusg() 1");
2819 /* errors in expressions easily cause lots of these warnings */
2824 * XXX Only variables are checkd, although types should
2825 * probably also be checked
2827 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
2828 sc != AUTO && sc != REG) {
2836 if (!sym->s_used && !sym->s_set) {
2837 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2838 /* %s unused in function %s */
2839 warning(192, sym->s_name, funcsym->s_name);
2842 if (sym->s_set && !sym->s_used) {
2843 STRUCT_ASSIGN(curr_pos, sym->s_spos);
2844 /* %s set but not used in function %s */
2845 warning(191, sym->s_name, funcsym->s_name);
2846 } else if (!sym->s_used) {
2847 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2848 /* %s unused in function %s */
2849 warning(192, sym->s_name, funcsym->s_name);
2855 * information about usage is taken over into the symbol
2856 * tabel entry at level 0 if the symbol was locally declared
2857 * as an external symbol.
2859 * XXX This is wrong for symbols declared static at level 0
2860 * if the usage information stems from sizeof(). This is
2861 * because symbols at level 0 only used in sizeof() are
2862 * considered to not be used.
2864 if ((xsym = sym->s_xsym) != NULL) {
2865 if (sym->s_used && !xsym->s_used) {
2867 STRUCT_ASSIGN(xsym->s_upos, sym->s_upos);
2869 if (sym->s_set && !xsym->s_set) {
2871 STRUCT_ASSIGN(xsym->s_spos, sym->s_spos);
2880 if (blklev != 1 || lab->s_blklev != 1)
2881 lerror("chklusg() 1");
2883 if (lab->s_set && !lab->s_used) {
2884 STRUCT_ASSIGN(curr_pos, lab->s_spos);
2885 /* label %s unused in function %s */
2886 warning(192, lab->s_name, funcsym->s_name);
2887 } else if (!lab->s_set) {
2888 STRUCT_ASSIGN(curr_pos, lab->s_upos);
2889 /* undefined label %s */
2890 warning(23, lab->s_name);
2897 if (!incompl(sym->s_type))
2900 /* complain alwasy about incomplet tags declared inside blocks */
2901 if (!zflag || dcs->d_ctx != EXTERN)
2904 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2905 switch (sym->s_type->t_tspec) {
2907 /* struct %s never defined */
2908 warning(233, sym->s_name);
2911 /* union %s never defined */
2912 warning(234, sym->s_name);
2915 /* enum %s never defined */
2916 warning(235, sym->s_name);
2919 lerror("chktusg() 1");
2924 * Called after the entire translation unit has been parsed.
2925 * Changes tentative definitions in definitions.
2926 * Performs some tests on global Symbols. Detected Problems are:
2927 * - defined variables of incomplete type
2928 * - constant variables which are not initialized
2929 * - static symbols which are never used
2937 if (blklev != 0 || dcs->d_nxt != NULL)
2940 STRUCT_ASSIGN(cpos, curr_pos);
2942 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
2943 if (sym->s_blklev == -1)
2945 if (sym->s_kind == FVFT) {
2947 } else if (sym->s_kind == FTAG) {
2950 if (sym->s_kind != FMOS)
2951 lerror("chkglsyms() 1");
2955 STRUCT_ASSIGN(curr_pos, cpos);
2959 chkglvar(sym_t *sym)
2961 if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON)
2964 if (sym->s_scl != EXTERN && sym->s_scl != STATIC)
2965 lerror("chkglvar() 1");
2969 if (sym->s_scl == STATIC) {
2970 if (sym->s_type->t_tspec == FUNC) {
2971 if (sym->s_used && sym->s_def != DEF) {
2972 STRUCT_ASSIGN(curr_pos, sym->s_upos);
2973 /* static func. called but not def.. */
2974 error(225, sym->s_name);
2978 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2979 if (sym->s_type->t_tspec == FUNC) {
2980 if (sym->s_def == DEF) {
2982 /* static function %s unused */
2983 warning(236, sym->s_name);
2985 /* static function %s decl. but ... */
2986 warning(290, sym->s_name);
2988 } else if (!sym->s_set) {
2989 /* static variable %s unused */
2990 warning(226, sym->s_name);
2992 /* static variable %s set but not used */
2993 warning(307, sym->s_name);
2996 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
2997 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2998 /* const object %s should have initializer */
2999 warning(227, sym->s_name);
3007 if (sym->s_def == TDEF) {
3008 if (sym->s_type->t_tspec == FUNC)
3010 * this can happen if an syntax error occured
3011 * after a function declaration
3014 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3015 if (length(sym->s_type, sym->s_name) == 0 &&
3016 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3017 /* empty array declaration: %s */
3018 if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3019 warning(190, sym->s_name);
3021 error(190, sym->s_name);
3028 * Prints information about location of previous definition/declaration.
3031 prevdecl(int msg, sym_t *psym)
3038 STRUCT_ASSIGN(cpos, curr_pos);
3039 STRUCT_ASSIGN(curr_pos, psym->s_dpos);
3041 message(msg, psym->s_name);
3042 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3043 /* previous definition of %s */
3044 message(261, psym->s_name);
3046 /* previous declaration of %s */
3047 message(260, psym->s_name);
3049 STRUCT_ASSIGN(curr_pos, cpos);