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.
35 static const char rcsid[] =
36 "$FreeBSD: src/usr.bin/xlint/lint1/decl.c,v 1.2.8.2 2001/07/19 09:19:47 kris Exp $";
39 #include <sys/param.h>
46 const char *unnamed = "<unnamed>";
48 /* contains various information and classification on types */
51 /* shared type structures for arithmtic types and void */
52 static type_t *typetab;
54 /* value of next enumerator during declaration of enum types */
58 * pointer to top element of a stack which contains informations local
59 * to nested declarations
63 static type_t *tdeferr __P((type_t *, tspec_t));
64 static void settdsym __P((type_t *, sym_t *));
65 static tspec_t mrgtspec __P((tspec_t, tspec_t));
66 static void align __P((int, int));
67 static sym_t *newtag __P((sym_t *, scl_t, int, int));
68 static int eqargs __P((type_t *, type_t *, int *));
69 static int mnoarg __P((type_t *, int *));
70 static int chkosdef __P((sym_t *, sym_t *));
71 static int chkptdecl __P((sym_t *, sym_t *));
72 static sym_t *nsfunc __P((sym_t *, sym_t *));
73 static void osfunc __P((sym_t *, sym_t *));
74 static void ledecl __P((sym_t *));
75 static int chkinit __P((sym_t *));
76 static void chkausg __P((int, sym_t *));
77 static void chkvusg __P((int, sym_t *));
78 static void chklusg __P((sym_t *));
79 static void chktusg __P((sym_t *));
80 static void chkglvar __P((sym_t *));
81 static void glchksz __P((sym_t *));
84 * initializes all global vars used in declarations
96 0, 0, 0, 0, 0, "signed" } },
99 0, 0, 0, 0, 0, "unsigned" } },
100 { CHAR, { CHAR_BIT, CHAR_BIT,
102 1, 0, 0, 1, 1, "char" } },
103 { SCHAR, { CHAR_BIT, CHAR_BIT,
105 1, 0, 0, 1, 1, "signed char" } },
106 { UCHAR, { CHAR_BIT, CHAR_BIT,
108 1, 1, 0, 1, 1, "unsigned char" } },
109 { SHORT, { sizeof (short) * CHAR_BIT, 2 * CHAR_BIT,
111 1, 0, 0, 1, 1, "short" } },
112 { USHORT, { sizeof (u_short) * CHAR_BIT, 2 * CHAR_BIT,
114 1, 1, 0, 1, 1, "unsigned short" } },
115 { INT, { sizeof (int) * CHAR_BIT, 3 * CHAR_BIT,
117 1, 0, 0, 1, 1, "int" } },
118 { UINT, { sizeof (u_int) * CHAR_BIT, 3 * CHAR_BIT,
120 1, 1, 0, 1, 1, "unsigned int" } },
121 { LONG, { sizeof (long) * CHAR_BIT, 4 * CHAR_BIT,
123 1, 0, 0, 1, 1, "long" } },
124 { ULONG, { sizeof (u_long) * CHAR_BIT, 4 * CHAR_BIT,
126 1, 1, 0, 1, 1, "unsigned long" } },
127 { QUAD, { sizeof (quad_t) * CHAR_BIT, 8 * CHAR_BIT,
129 1, 0, 0, 1, 1, "long long" } },
130 { UQUAD, { sizeof (u_quad_t) * CHAR_BIT, 8 * CHAR_BIT,
132 1, 1, 0, 1, 1, "unsigned long long" } },
133 { FLOAT, { sizeof (float) * CHAR_BIT, 4 * CHAR_BIT,
135 0, 0, 1, 1, 1, "float" } },
136 { DOUBLE, { sizeof (double) * CHAR_BIT, 8 * CHAR_BIT,
138 0, 0, 1, 1, 1, "double" } },
139 { LDOUBLE, { sizeof (ldbl_t) * CHAR_BIT, 10 * CHAR_BIT,
141 0, 0, 1, 1, 1, "long double" } },
144 0, 0, 0, 0, 0, "void" } },
147 0, 0, 0, 0, 0, "struct" } },
150 0, 0, 0, 0, 0, "union" } },
151 { ENUM, { sizeof (int) * CHAR_BIT, 3 * CHAR_BIT,
153 1, 0, 0, 1, 1, "enum" } },
154 { PTR, { sizeof (void *) * CHAR_BIT, 4 * CHAR_BIT,
156 0, 1, 0, 0, 1, "pointer" } },
159 0, 0, 0, 0, 0, "array" } },
162 0, 0, 0, 0, 0, "function" } },
165 /* declaration stack */
166 dcs = xcalloc(1, sizeof (dinfo_t));
168 dcs->d_ldlsym = &dcs->d_dlsyms;
170 /* type information and classification */
171 for (i = 0; i < sizeof (ittab) / sizeof (ittab[0]); i++)
172 STRUCT_ASSIGN(ttab[ittab[i].it_tspec], ittab[i].it_ttab);
174 for (i = 0; i < NTSPEC; i++)
175 ttab[i].tt_psz = ttab[i].tt_sz;
178 /* shared type structures */
179 typetab = xcalloc(NTSPEC, sizeof (type_t));
180 for (i = 0; i < NTSPEC; i++)
181 typetab[i].t_tspec = NOTSPEC;
182 typetab[CHAR].t_tspec = CHAR;
183 typetab[SCHAR].t_tspec = SCHAR;
184 typetab[UCHAR].t_tspec = UCHAR;
185 typetab[SHORT].t_tspec = SHORT;
186 typetab[USHORT].t_tspec = USHORT;
187 typetab[INT].t_tspec = INT;
188 typetab[UINT].t_tspec = UINT;
189 typetab[LONG].t_tspec = LONG;
190 typetab[ULONG].t_tspec = ULONG;
191 typetab[QUAD].t_tspec = QUAD;
192 typetab[UQUAD].t_tspec = UQUAD;
193 typetab[FLOAT].t_tspec = FLOAT;
194 typetab[DOUBLE].t_tspec = DOUBLE;
195 typetab[LDOUBLE].t_tspec = LDOUBLE;
196 typetab[VOID].t_tspec = VOID;
198 * Next two are not real types. They are only used by the parser
199 * to return keywords "signed" and "unsigned"
201 typetab[SIGNED].t_tspec = SIGNED;
202 typetab[UNSIGN].t_tspec = UNSIGN;
206 * Returns a shared type structure vor arithmetic types and void.
208 * It's important do duplicate this structure (using duptyp() or tdupdyp())
209 * if it is to be modified (adding qualifiers or anything else).
215 return (&typetab[t]);
224 ntp = getblk(sizeof (type_t));
225 STRUCT_ASSIGN(*ntp, *tp);
230 * Use tduptyp() instead of duptyp() inside expressions (if the
231 * allocated memory should be freed after the expr).
239 ntp = tgetblk(sizeof (type_t));
240 STRUCT_ASSIGN(*ntp, *tp);
245 * Returns 1 if the argument is void or an incomplete array,
246 * struct, union or enum type.
254 if ((t = tp->t_tspec) == VOID) {
256 } else if (t == ARRAY) {
257 return (tp->t_aincompl);
258 } else if (t == STRUCT || t == UNION) {
259 return (tp->t_str->sincompl);
260 } else if (t == ENUM) {
261 return (tp->t_enum->eincompl);
267 * Set the flag for (in)complete array, struct, union or enum
277 if ((t = tp->t_tspec) == ARRAY) {
279 } else if (t == STRUCT || t == UNION) {
280 tp->t_str->sincompl = ic;
283 lerror("setcompl() 1");
284 tp->t_enum->eincompl = ic;
289 * Remember the storage class of the current declaration in dcs->d_scl
290 * (the top element of the declaration stack) and detect multiple
300 warning(10, "inline");
304 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
305 dcs->d_smod != NOTSPEC || dcs->d_lmod != NOTSPEC) {
306 /* storage class after type is obsolescent */
309 if (dcs->d_scl == NOSCL) {
313 * multiple storage classes. An error will be reported in
321 * Remember the type, modifier or typedef name returned by the parser
322 * in *dcs (top element of decl stack). This information is used in
323 * deftyp() to build the type used for all declarators in this
326 * Is tp->t_typedef 1, the type comes from a previously defined typename.
327 * Otherwise it comes from a type specifier (int, long, ...) or a
328 * struct/union/enum tag.
337 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
338 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) {
340 * something like "typedef int a; int a b;"
341 * This should not happen with current grammar.
351 if (t == STRUCT || t == UNION || t == ENUM) {
353 * something like "int struct a ..."
354 * struct/union/enum with anything else is not allowed
356 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
357 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) {
359 * remember that an error must be reported in
363 dcs->d_atyp = dcs->d_lmod = dcs->d_smod = NOTSPEC;
369 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
371 * something like "struct a int"
372 * struct/union/enum with anything else is not allowed
378 if (t == LONG && dcs->d_lmod == LONG) {
379 /* "long long" or "long ... long" */
381 dcs->d_lmod = NOTSPEC;
383 /* %s C does not support 'long long' */
384 (void)gnuism(265, tflag ? "traditional" : "ANSI");
387 if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
388 /* something like "typedef int a; a long ..." */
389 dcs->d_type = tdeferr(dcs->d_type, t);
393 /* now it can be only a combination of arithmetic types and void */
394 if (t == SIGNED || t == UNSIGN) {
395 /* remeber specifiers "signed" and "unsigned" in dcs->d_smod */
396 if (dcs->d_smod != NOTSPEC)
398 * more then one "signed" and/or "unsigned"; print
399 * an error in deftyp()
403 } else if (t == SHORT || t == LONG || t == QUAD) {
405 * remember specifiers "short", "long" and "long long" in
408 if (dcs->d_lmod != NOTSPEC)
409 /* more than one, print error in deftyp() */
414 * remember specifiers "void", "char", "int", "float" or
415 * "double" int dcs->d_atyp
417 if (dcs->d_atyp != NOTSPEC)
418 /* more than one, print error in deftyp() */
425 * called if a list of declaration specifiers contains a typedef name
426 * and other specifiers (except struct, union, enum, typedef name)
440 if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG ||
443 /* modifying typedef with ... */
444 warning(5, ttab[t].tt_name);
445 td = duptyp(gettyp(mrgtspec(t2, t)));
451 if (t2 == INT || t2 == UINT) {
452 /* modifying typedef with ... */
454 td = duptyp(gettyp(t2 == INT ? SHORT : USHORT));
460 if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG ||
461 t2 == FLOAT || t2 == DOUBLE) {
462 /* modifying typedef with ... */
466 } else if (t2 == UINT) {
468 } else if (t2 == LONG) {
470 } else if (t2 == ULONG) {
472 } else if (t2 == FLOAT) {
474 } else if (t2 == DOUBLE) {
475 td = gettyp(LDOUBLE);
482 /* LINTED (enumeration values not handled in switch) */
486 /* Anything other is not accepted. */
493 * Remember the symbol of a typedef name (2nd arg) in a struct, union
494 * or enum tag if the typedef name is the first defined for this tag.
496 * If the tag is unnamed, the typdef name is used for identification
497 * of this tag in lint2. Although its possible that more then one typedef
498 * name is defined for one tag, the first name defined should be unique
499 * if the tag is unnamed.
508 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
509 if (tp->t_str->stdef == NULL)
510 tp->t_str->stdef = sym;
511 } else if (t == ENUM) {
512 if (tp->t_enum->etdef == NULL)
513 tp->t_enum->etdef = sym;
518 * Remember a qualifier which is part of the declaration specifiers
519 * (and not the declarator) in the top element of the declaration stack.
520 * Also detect multiple qualifiers of the same kind.
522 * The rememberd qualifier is used by deftyp() to construct the type
523 * for all declarators.
532 warning(10, "const");
537 lerror("addqual() 1");
538 if (dcs->d_volatile) {
540 warning(10, "volatile");
547 * Go to the next declaration level (structs, nested structs, blocks,
548 * argument declaration lists ...)
557 (void)printf("pushdecl(%d)\n", (int)sc);
559 /* put a new element on the declaration stack */
560 di = xcalloc(1, sizeof (dinfo_t));
564 di->d_ldlsym = &di->d_dlsyms;
568 * Go back to previous declaration level
576 (void)printf("popdecl(%d)\n", (int)dcs->d_ctx);
578 if (dcs->d_nxt == NULL)
579 lerror("popdecl() 1");
584 /* there is nothing after external declarations */
585 lerror("popdecl() 2");
591 * Symbols declared in (nested) structs or enums are
592 * part of the next level (they are removed from the
593 * symbol table if the symbols of the outher level are
596 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
597 dcs->d_ldlsym = di->d_ldlsym;
601 * All symbols in dcs->d_dlsyms are introduced in old style
602 * argument declarations (it's not clean, but possible).
603 * They are appended to the list of symbols declared in
604 * an old style argument identifier list or a new style
605 * parameter type list.
607 if (di->d_dlsyms != NULL) {
608 *di->d_ldlsym = dcs->d_fpsyms;
609 dcs->d_fpsyms = di->d_dlsyms;
615 * Append all symbols declared in the abstract declaration
616 * to the list of symbols declared in the surounding decl.
618 * XXX I'm not sure whether they should be removed from the
619 * symbol table now or later.
621 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
622 dcs->d_ldlsym = di->d_ldlsym;
625 /* check usage of local vars */
629 /* usage of arguments will be checked by funcend() */
630 rmsyms(di->d_dlsyms);
633 lerror("popdecl() 3");
639 * Set flag d_asm in all declaration stack elements up to the
642 * This is used to mark compound statements which have, possibly in
643 * nested compound statements, asm statements. For these compound
644 * statements no warnings about unused or unitialized variables are
647 * There is no need to clear d_asm in dinfo structs with context AUTO,
648 * because these structs are freed at the end of the compound statement.
649 * But it must be cleard in the outermost dinfo struct, which has
650 * context EXTERN. This could be done in clrtyp() and would work for
651 * C, but not for C++ (due to mixed statements and declarations). Thus
652 * we clear it in glclup(), which is used to do some cleanup after
653 * global declarations/definitions.
660 for (di = dcs; di != NULL; di = di->d_nxt)
665 * Clean all elements of the top element of declaration stack which
666 * will be used by the next declaration
671 dcs->d_atyp = dcs->d_smod = dcs->d_lmod = NOTSPEC;
674 dcs->d_const = dcs->d_volatile = 0;
676 dcs->d_mscl = dcs->d_terr = 0;
682 * Create a type structure from the informations gathered in
683 * the declaration stack.
684 * Complain about storage classes which are not possible in current
694 t = dcs->d_atyp; /* CHAR, INT, FLOAT, DOUBLE, VOID */
695 s = dcs->d_smod; /* SIGNED, UNSIGNED */
696 l = dcs->d_lmod; /* SHORT, LONG, QUAD */
700 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && tp == NULL)
703 if (tp != NULL && (t != NOTSPEC || s != NOTSPEC || l != NOTSPEC)) {
704 /* should never happen */
705 lerror("deftyp() 1");
728 /* use 'double' instead of ... */
737 /* 'long double' is illegal in ... */
744 lerror("deftyp() 2");
746 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) {
752 dcs->d_type = gettyp(mrgtspec(t, s));
756 /* only one storage class allowed */
760 /* illegal type combination */
764 if (dcs->d_ctx == EXTERN) {
765 if (scl == REG || scl == AUTO) {
766 /* illegal storage class */
770 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PARG) {
771 if (scl != NOSCL && scl != REG) {
772 /* only "register" valid ... */
780 if (dcs->d_const && dcs->d_type->t_const) {
781 if (!dcs->d_type->t_typedef)
782 lerror("deftyp() 3");
783 /* typedef already qualified with "%s" */
784 warning(68, "const");
786 if (dcs->d_volatile && dcs->d_type->t_volatile) {
787 if (!dcs->d_type->t_typedef)
788 lerror("deftyp() 4");
789 /* typedef already qualified with "%s" */
790 warning(68, "volatile");
793 if (dcs->d_const || dcs->d_volatile) {
794 dcs->d_type = duptyp(dcs->d_type);
795 dcs->d_type->t_const |= dcs->d_const;
796 dcs->d_type->t_volatile |= dcs->d_volatile;
801 * Merge type specifiers (char, ..., long long, signed, unsigned).
807 if (s == SIGNED || s == UNSIGN) {
809 t = s == SIGNED ? SCHAR : UCHAR;
810 } else if (t == SHORT) {
811 t = s == SIGNED ? SHORT : USHORT;
812 } else if (t == INT) {
813 t = s == SIGNED ? INT : UINT;
814 } else if (t == LONG) {
815 t = s == SIGNED ? LONG : ULONG;
816 } else if (t == QUAD) {
817 t = s == SIGNED ? QUAD : UQUAD;
825 * Return the length of a type in bit.
827 * Printing a message if the outhermost dimension of an array is 0 must
828 * be done by the caller. All other problems are reported by length()
829 * if name is not NULL.
839 while (tp->t_tspec == ARRAY) {
843 switch (tp->t_tspec) {
845 /* compiler takes size of function */
846 lerror("%s", msgs[12]);
850 if (incompl(tp) && name != NULL) {
851 /* incomplete structure or union %s: %s */
852 error(31, tp->t_str->stag->s_name, name);
854 elsz = tp->t_str->size;
857 if (incompl(tp) && name != NULL) {
858 /* incomplete enum type: %s */
863 elsz = size(tp->t_tspec);
868 return (elem * elsz);
872 * Get the alignment of the given Type in bits.
881 while (tp->t_tspec == ARRAY)
884 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
885 a = tp->t_str->align;
886 } else if (t == FUNC) {
887 /* compiler takes alignment of function */
889 a = ALIGN(1) * CHAR_BIT;
891 if ((a = size(t)) == 0) {
893 } else if (a > ALIGN(1) * CHAR_BIT) {
894 a = ALIGN(1) * CHAR_BIT;
897 if (a < CHAR_BIT || a > ALIGN(1) * CHAR_BIT)
898 lerror("getbound() 1");
903 * Concatenate two lists of symbols by s_nxt. Used by declarations of
904 * struct/union/enum elements and parameters.
912 if ((l = l1) == NULL)
914 while (l1->s_nxt != NULL)
921 * Check if the type of the given symbol is valid and print an error
922 * message if it is not.
925 * - arrays of incomlete types or functions
926 * - functions returning arrays or functions
927 * - void types other than type of function or pointer
938 while ((tp = *tpp) != NULL) {
941 * If this is the type of an old style function definition,
942 * a better warning is printed in funcdef().
944 if (t == FUNC && !tp->t_proto &&
945 !(to == NOTSPEC && sym->s_osdef)) {
947 /* function declaration is not a prototype */
951 if (t == FUNC || t == ARRAY) {
952 /* function returns illegal type */
955 *tpp = incref(*tpp, PTR);
957 *tpp = incref((*tpp)->t_subt, PTR);
960 } else if (tp->t_const || tp->t_volatile) {
961 if (sflag) { /* XXX oder better !tflag ? */
962 /* function cannot return const... */
968 /* array of function is illegal */
972 } else if (t == ARRAY && tp->t_dim == 0) {
976 } else if (t == VOID) {
977 /* illegal use of void */
980 #if 0 /* errors are produced by length() */
981 } else if (incompl(tp)) {
982 /* array of incomplete type */
990 } else if (to == NOTSPEC && t == VOID) {
991 if (dcs->d_ctx == PARG) {
992 if (sym->s_scl != ABSTRACT) {
993 if (sym->s_name == unnamed)
995 /* void param cannot have name: %s */
996 error(61, sym->s_name);
999 } else if (dcs->d_ctx == ABSTRACT) {
1001 } else if (sym->s_scl != TYPEDEF) {
1002 /* void type for %s */
1003 error(19, sym->s_name);
1007 if (t == VOID && to != PTR) {
1008 if (tp->t_const || tp->t_volatile) {
1009 /* inappropriate qualifiers with "void" */
1011 tp->t_const = tp->t_volatile = 0;
1020 * Process the declarator of a struct/union element.
1031 if ((sc = dsym->s_scl) != MOS && sc != MOU)
1032 lerror("decl1str() 1");
1034 if (dcs->d_rdcsym != NULL) {
1035 if ((sc = dcs->d_rdcsym->s_scl) != MOS && sc != MOU)
1036 /* should be ensured by storesym() */
1037 lerror("decl1str() 2");
1038 if (dsym->s_styp == dcs->d_rdcsym->s_styp) {
1039 /* duplicate member name: %s */
1040 error(33, dsym->s_name);
1041 rmsym(dcs->d_rdcsym);
1047 t = (tp = dsym->s_type)->t_tspec;
1049 if (dsym->s_field) {
1053 * only unsigned und signed int are protable bit-field types
1054 *(at least in ANSI C, in traditional C only unsigned int)
1056 if (t == CHAR || t == UCHAR || t == SCHAR ||
1057 t == SHORT || t == USHORT || t == ENUM) {
1059 /* bit-field type '%s' invalid in ANSI C */
1060 warning(273, tyname(tp));
1062 /* nonportable bit-field type */
1065 } else if (t == INT && dcs->d_smod == NOTSPEC) {
1067 /* nonportable bit-field type */
1070 } else if (t != INT && t != UINT) {
1071 /* illegal bit-field type */
1074 dsym->s_type = tp = duptyp(gettyp(t = INT));
1075 if ((tp->t_flen = sz) > size(t))
1076 tp->t_flen = size(t);
1078 if ((len = tp->t_flen) < 0 || len > size(t)) {
1079 /* illegal bit-field size */
1081 tp->t_flen = size(t);
1082 } else if (len == 0 && dsym->s_name != unnamed) {
1083 /* zero size bit-field */
1085 tp->t_flen = size(t);
1087 if (dsym->s_scl == MOU) {
1088 /* illegal use of bit-field */
1090 dsym->s_type->t_isfield = 0;
1093 } else if (t == FUNC) {
1094 /* function illegal in structure or union */
1096 dsym->s_type = tp = incref(tp, t = PTR);
1100 * bit-fields of length 0 are not warned about because length()
1101 * does not return the length of the bit-field but the length
1102 * of the type the bit-field is packed in (its ok)
1104 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1105 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1106 /* illegal zero sized structure member: %s */
1107 warning(39, dsym->s_name);
1111 if (dcs->d_ctx == MOU) {
1115 if (dsym->s_field) {
1116 align(getbound(tp), tp->t_flen);
1117 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t);
1118 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1119 dcs->d_offset += tp->t_flen;
1121 align(getbound(tp), 0);
1122 dsym->s_value.v_quad = dcs->d_offset;
1123 dcs->d_offset += sz;
1125 if (dcs->d_ctx == MOU) {
1126 if (o > dcs->d_offset)
1136 * Aligns next structure element as required.
1138 * al contains the required alignment, len the length of a bit-field.
1147 * The alignment of the current element becomes the alignment of
1148 * the struct/union if it is larger than the current alignment
1149 * of the struct/union.
1151 if (al > dcs->d_stralign)
1152 dcs->d_stralign = al;
1154 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1155 if (len == 0 || dcs->d_offset + len > no)
1160 * Remember the width of the field in its type structure.
1168 dsym = getblk(sizeof (sym_t));
1169 dsym->s_name = unnamed;
1170 dsym->s_kind = FMOS;
1172 dsym->s_type = gettyp(INT);
1173 dsym->s_blklev = -1;
1175 dsym->s_type = duptyp(dsym->s_type);
1176 dsym->s_type->t_isfield = 1;
1177 dsym->s_type->t_flen = len;
1183 * Collect informations about a sequence of asterisks and qualifiers
1184 * in a list of type pqinf_t.
1185 * Qualifiers refer always to the left asterisk. The rightmost asterisk
1186 * will be at the top of the list.
1194 if (p2->p_pcnt != 0) {
1195 /* left '*' at the end of the list */
1196 for (p = p2; p->p_nxt != NULL; p = p->p_nxt) ;
1203 warning(10, "const");
1207 if (p2->p_volatile) {
1208 if (p1->p_volatile) {
1210 warning(10, "volatile");
1220 * Followint 3 functions extend the type of a declarator with
1221 * pointer, function and array types.
1223 * The current type is the Type built by deftyp() (dcs->d_type) and
1224 * pointer, function and array types already added for this
1225 * declarator. The new type extension is inserted between both.
1235 tpp = &decl->s_type;
1236 while (*tpp != dcs->d_type)
1237 tpp = &(*tpp)->t_subt;
1239 while (pi != NULL) {
1240 *tpp = tp = getblk(sizeof (type_t));
1242 tp->t_const = pi->p_const;
1243 tp->t_volatile = pi->p_volatile;
1244 *(tpp = &tp->t_subt) = dcs->d_type;
1253 * If a dimension was specified, dim is 1, otherwise 0
1254 * n is the specified dimension
1257 addarray(decl, dim, n)
1263 tpp = &decl->s_type;
1264 while (*tpp != dcs->d_type)
1265 tpp = &(*tpp)->t_subt;
1267 *tpp = tp = getblk(sizeof (type_t));
1268 tp->t_tspec = ARRAY;
1269 tp->t_subt = dcs->d_type;
1273 /* zero or negative array dimension */
1276 } else if (n == 0 && dim) {
1277 /* zero or negative array dimension */
1279 } else if (n == 0 && !dim) {
1280 /* is incomplete type */
1295 /* function prototypes are illegal in traditional C */
1297 args = nsfunc(decl, args);
1303 * The symbols are removed from the symbol table by popdecl() after
1304 * addfunc(). To be able to restore them if this is a function
1305 * definition, a pointer to the list of all symbols is stored in
1306 * dcs->d_nxt->d_fpsyms. Also a list of the arguments (concatenated
1307 * by s_nxt) is stored in dcs->d_nxt->d_fargs.
1308 * (dcs->d_nxt must be used because *dcs is the declaration stack
1309 * element created for the list of params and is removed after
1312 if (dcs->d_nxt->d_ctx == EXTERN &&
1313 decl->s_type == dcs->d_nxt->d_type) {
1314 dcs->d_nxt->d_fpsyms = dcs->d_dlsyms;
1315 dcs->d_nxt->d_fargs = args;
1318 tpp = &decl->s_type;
1319 while (*tpp != dcs->d_nxt->d_type)
1320 tpp = &(*tpp)->t_subt;
1322 *tpp = tp = getblk(sizeof (type_t));
1324 tp->t_subt = dcs->d_nxt->d_type;
1325 if ((tp->t_proto = dcs->d_proto) != 0)
1327 tp->t_vararg = dcs->d_vararg;
1333 * Called for new style function declarations.
1345 * Declarations of structs/unions/enums in param lists are legal,
1348 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1350 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) {
1351 /* dubious tag declaration: %s %s */
1352 warning(85, scltoa(sc), sym->s_name);
1357 for (arg = args; arg != NULL; arg = arg->s_nxt) {
1358 if (arg->s_type->t_tspec == VOID) {
1359 if (n > 1 || arg->s_nxt != NULL) {
1360 /* "void" must be sole parameter */
1362 arg->s_type = gettyp(INT);
1368 /* return NULL if first param is VOID */
1369 return (args != NULL && args->s_type->t_tspec != VOID ? args : NULL);
1373 * Called for old style function declarations.
1380 * Remember list of params only if this is really seams to be
1381 * a function definition.
1383 if (dcs->d_nxt->d_ctx == EXTERN &&
1384 decl->s_type == dcs->d_nxt->d_type) {
1386 * We assume that this becomes a function definition. If
1387 * we are wrong, its corrected in chkfdef().
1391 decl->s_args = args;
1395 /* function prototype parameters must have types */
1401 * Lists of Identifiers in functions declarations are allowed only if
1402 * its also a function definition. If this is not the case, print a
1412 /* incomplete or misplaced function definition */
1421 * Process the name in a declarator.
1422 * If the symbol does already exists, a new one is created.
1423 * The symbol becomes one of the storage classes EXTERN, STATIC, AUTO or
1425 * s_def and s_reg are valid after dname().
1433 if (sym->s_scl == NOSCL) {
1434 dcs->d_rdcsym = NULL;
1435 } else if (sym->s_defarg) {
1437 dcs->d_rdcsym = NULL;
1439 dcs->d_rdcsym = sym;
1440 sym = pushdown(sym);
1443 switch (dcs->d_ctx) {
1447 sym->s_styp = dcs->d_tagtyp->t_str;
1449 sym->s_value.v_tspec = INT;
1454 * static and external symbols without "extern" are
1455 * considered to be tentative defined, external
1456 * symbols with "extern" are declared, and typedef names
1457 * are defined. Tentative defined and declared symbols
1458 * may become defined if an initializer is present or
1459 * this is a function definition.
1461 if ((sc = dcs->d_scl) == NOSCL) {
1464 } else if (sc == STATIC) {
1466 } else if (sc == TYPEDEF) {
1468 } else if (sc == EXTERN) {
1471 lerror("dname() 1");
1478 if ((sc = dcs->d_scl) == NOSCL) {
1480 } else if (sc == REG) {
1484 lerror("dname() 2");
1489 if ((sc = dcs->d_scl) == NOSCL) {
1491 * XXX somewhat ugly because we dont know whether
1492 * this is AUTO or EXTERN (functions). If we are
1493 * wrong it must be corrected in decl1loc(), where
1494 * we have the neccessary type information.
1498 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1500 } else if (sc == REG) {
1504 } else if (sc == EXTERN) {
1507 lerror("dname() 3");
1511 lerror("dname() 4");
1515 sym->s_type = dcs->d_type;
1517 dcs->d_fpsyms = NULL;
1523 * Process a name in the list of formal params in an old style function
1530 if (sym->s_scl != NOSCL) {
1531 if (blklev == sym->s_blklev) {
1532 /* redeclaration of formal parameter %s */
1533 error(21, sym->s_name);
1537 sym = pushdown(sym);
1539 sym->s_type = gettyp(INT);
1542 sym->s_defarg = sym->s_arg = 1;
1547 * Create the type of a tag.
1549 * tag points to the symbol table entry of the tag
1550 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1551 * decl is 1 if the type of the tag will be completed in this declaration
1552 * (the following token is T_LBRACE)
1553 * semi is 1 if the following token is T_SEMI
1556 mktag(tag, kind, decl, semi)
1564 if (kind == STRUCT) {
1566 } else if (kind == UNION) {
1568 } else if (kind == ENUM) {
1575 if (tag->s_scl != NOSCL) {
1576 tag = newtag(tag, scl, decl, semi);
1578 /* a new tag, no empty declaration */
1579 dcs->d_nxt->d_nedecl = 1;
1580 if (scl == ENUMTAG && !decl) {
1581 if (!tflag && (sflag || pflag))
1582 /* forward reference to enum type */
1586 if (tag->s_scl == NOSCL) {
1588 tag->s_type = tp = getblk(sizeof (type_t));
1593 tag = getblk(sizeof (sym_t));
1594 tag->s_name = unnamed;
1595 STRUCT_ASSIGN(tag->s_dpos, curr_pos);
1599 tag->s_type = tp = getblk(sizeof (type_t));
1600 dcs->d_nxt->d_nedecl = 1;
1603 if (tp->t_tspec == NOTSPEC) {
1606 tp->t_str = getblk(sizeof (str_t));
1607 tp->t_str->align = CHAR_BIT;
1608 tp->t_str->stag = tag;
1611 tp->t_enum = getblk(sizeof (enum_t));
1612 tp->t_enum->etag = tag;
1614 /* ist unvollstaendiger Typ */
1622 * Checks all possible cases of tag redeclarations.
1623 * decl is 1 if T_LBRACE follows
1624 * semi is 1 if T_SEMI follows
1627 newtag(tag, scl, decl, semi)
1632 if (tag->s_blklev < blklev) {
1637 /* decl. introduces new type ... */
1638 warning(44, scltoa(scl), tag->s_name);
1639 tag = pushdown(tag);
1640 } else if (tag->s_scl != scl) {
1641 /* base type is really "%s %s" */
1642 warning(45, scltoa(tag->s_scl), tag->s_name);
1644 dcs->d_nxt->d_nedecl = 1;
1646 /* "struct a { ..." */
1648 /* redefinition hides earlier one: %s */
1649 warning(43, tag->s_name);
1650 tag = pushdown(tag);
1651 dcs->d_nxt->d_nedecl = 1;
1652 } else if (tag->s_scl != scl) {
1653 /* base type is really "%s %s" */
1654 warning(45, scltoa(tag->s_scl), tag->s_name);
1655 /* declaration introduces new type in ANSI C: %s %s */
1657 warning(44, scltoa(scl), tag->s_name);
1658 tag = pushdown(tag);
1659 dcs->d_nxt->d_nedecl = 1;
1662 if (tag->s_scl != scl) {
1663 /* (%s) tag redeclared */
1664 error(46, scltoa(tag->s_scl));
1666 tag = pushdown(tag);
1667 dcs->d_nxt->d_nedecl = 1;
1668 } else if (decl && !incompl(tag->s_type)) {
1669 /* (%s) tag redeclared */
1670 error(46, scltoa(tag->s_scl));
1672 tag = pushdown(tag);
1673 dcs->d_nxt->d_nedecl = 1;
1674 } else if (semi || decl) {
1675 dcs->d_nxt->d_nedecl = 1;
1688 case EXTERN: s = "extern"; break;
1689 case STATIC: s = "static"; break;
1690 case AUTO: s = "auto"; break;
1691 case REG: s = "register"; break;
1692 case TYPEDEF: s = "typedef"; break;
1693 case STRTAG: s = "struct"; break;
1694 case UNIONTAG: s = "union"; break;
1695 case ENUMTAG: s = "enum"; break;
1696 default: lerror("tagttoa()");
1702 * Completes the type of a tag in a struct/union/enum declaration.
1703 * tp points to the type of the, tag, fmem to the list of members/enums.
1715 /* from now a complete type */
1718 if ((t = tp->t_tspec) != ENUM) {
1719 align(dcs->d_stralign, 0);
1721 sp->align = dcs->d_stralign;
1722 sp->size = dcs->d_offset;
1724 if (sp->size == 0) {
1726 (void)gnuism(47, ttab[t].tt_name);
1729 for (mem = fmem; mem != NULL; mem = mem->s_nxt) {
1730 if (mem->s_name != unnamed)
1734 /* %s has no named members */
1736 t == STRUCT ? "structure" : "union");
1740 tp->t_enum->elem = fmem;
1746 * Processes the name of an enumerator in en enum declaration.
1748 * sym points to the enumerator
1749 * val is the value of the enumerator
1750 * impl is 1 if the the value of the enumerator was not explicit specified.
1753 ename(sym, val, impl)
1758 if (sym->s_blklev == blklev) {
1759 /* no hflag, because this is illegal!!! */
1761 /* enumeration constant hides parameter: %s */
1762 warning(57, sym->s_name);
1764 /* redeclaration of %s */
1765 error(27, sym->s_name);
1767 * inside blocks it should not too complicated
1768 * to find the position of the previous
1776 /* redefinition hides earlier one: %s */
1777 warning(43, sym->s_name);
1779 sym = pushdown(sym);
1781 sym->s_scl = ENUMCON;
1782 sym->s_type = dcs->d_tagtyp;
1783 sym->s_value.v_tspec = INT;
1784 sym->s_value.v_quad = val;
1785 if (impl && val - 1 == INT_MAX) {
1786 /* overflow in enumeration values: %s */
1787 warning(48, sym->s_name);
1794 * Process a single external declarator.
1797 decl1ext(dsym, initflg)
1801 int warn, rval, redec;
1808 if (initflg && !(initerr = chkinit(dsym)))
1812 * Declarations of functions are marked as "tentative" in dname().
1813 * This is wrong because there are no tentative function
1816 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1819 if (dcs->d_inline) {
1820 if (dsym->s_type->t_tspec == FUNC) {
1823 /* variable declared inline: %s */
1824 warning(268, dsym->s_name);
1828 /* Write the declaration into the output file */
1829 if (plibflg && llibflg &&
1830 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1832 * With both LINTLIBRARY and PROTOLIB the prototyp is
1833 * written as a function definition to the output file.
1835 rval = dsym->s_type->t_subt->t_tspec != VOID;
1836 outfdef(dsym, &dsym->s_dpos, rval, 0, NULL);
1838 outsym(dsym, dsym->s_scl, dsym->s_def);
1841 if ((rdsym = dcs->d_rdcsym) != NULL) {
1844 * If the old symbol stems from a old style function definition
1845 * we have remembered the params in rdsmy->s_args and compare
1846 * them with the params of the prototype.
1848 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1849 redec = chkosdef(rdsym, dsym);
1854 if (!redec && !isredec(dsym, (warn = 0, &warn))) {
1857 /* redeclaration of %s */
1858 (*(sflag ? error : warning))(27, dsym->s_name);
1859 prevdecl(-1, rdsym);
1863 * Overtake the rememberd params if the new symbol
1864 * is not a prototype.
1866 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1867 dsym->s_osdef = rdsym->s_osdef;
1868 dsym->s_args = rdsym->s_args;
1869 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1873 * Remember the position of the declaration if the
1874 * old symbol was a prototype and the new is not.
1875 * Also remember the position if the old symbol
1876 * was defined and the new is not.
1878 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1879 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1880 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1881 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1885 * Copy informations about usage of the name into
1888 cpuinfo(dsym, rdsym);
1890 /* Once a name is defined, it remains defined. */
1891 if (rdsym->s_def == DEF)
1894 /* once a function is inline, it remains inline */
1895 if (rdsym->s_inline)
1898 compltyp(dsym, rdsym);
1905 if (dsym->s_scl == TYPEDEF) {
1906 dsym->s_type = duptyp(dsym->s_type);
1907 dsym->s_type->t_typedef = 1;
1908 settdsym(dsym->s_type, dsym);
1914 * Copies informations about usage into a new symbol table entry of
1921 sym->s_spos = rdsym->s_spos;
1922 sym->s_upos = rdsym->s_upos;
1923 sym->s_set = rdsym->s_set;
1924 sym->s_used = rdsym->s_used;
1928 * Prints an error and returns 1 if a symbol is redeclared/redefined.
1929 * Otherwise returns 0 and, in some cases of minor problems, prints
1939 if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
1940 /* redeclaration of %s */
1941 error(27, dsym->s_name);
1945 if (rsym->s_scl == TYPEDEF) {
1946 /* typedef redeclared: %s */
1947 error(89, dsym->s_name);
1951 if (dsym->s_scl == TYPEDEF) {
1952 /* redeclaration of %s */
1953 error(27, dsym->s_name);
1957 if (rsym->s_def == DEF && dsym->s_def == DEF) {
1958 /* redefinition of %s */
1959 error(28, dsym->s_name);
1963 if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, warn)) {
1964 /* redeclaration of %s */
1965 error(27, dsym->s_name);
1969 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
1971 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
1973 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
1975 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
1977 * All cases except "int a = 1; static int a;" are catched
1978 * above with or without a warning
1980 /* redeclaration of %s */
1981 error(27, dsym->s_name);
1985 if (rsym->s_scl == EXTERN) {
1986 /* previously declared extern, becomes static: %s */
1987 warning(29, dsym->s_name);
1993 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
1995 /* redeclaration of %s; ANSI C requires "static" */
1997 warning(30, dsym->s_name);
2000 dsym->s_scl = STATIC;
2005 * Checks if two types are compatible. Returns 0 if not, otherwise 1.
2007 * ignqual ignore qualifiers of type; used for function params
2008 * promot promote left type; used for comparision of params of
2009 * old style function definitions with params of prototypes.
2010 * *warn set to 1 if an old style function declaration is not
2011 * compatible with a prototype
2014 eqtype(tp1, tp2, ignqual, promot, warn)
2016 int ignqual, promot, *warn;
2020 while (tp1 != NULL && tp2 != NULL) {
2026 } else if (t == CHAR || t == SCHAR) {
2028 } else if (t == UCHAR) {
2029 t = tflag ? UINT : INT;
2030 } else if (t == SHORT) {
2032 } else if (t == USHORT) {
2034 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
2038 if (t != tp2->t_tspec)
2041 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
2044 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
2047 if (t == STRUCT || t == UNION)
2048 return (tp1->t_str == tp2->t_str);
2050 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2051 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2055 /* dont check prototypes for traditional */
2056 if (t == FUNC && !tflag) {
2057 if (tp1->t_proto && tp2->t_proto) {
2058 if (!eqargs(tp1, tp2, warn))
2060 } else if (tp1->t_proto) {
2061 if (!mnoarg(tp1, warn))
2063 } else if (tp2->t_proto) {
2064 if (!mnoarg(tp2, warn))
2071 ignqual = promot = 0;
2075 return (tp1 == tp2);
2079 * Compares the parameter types of two prototypes.
2082 eqargs(tp1, tp2, warn)
2088 if (tp1->t_vararg != tp2->t_vararg)
2094 while (a1 != NULL && a2 != NULL) {
2096 if (eqtype(a1->s_type, a2->s_type, 1, 0, warn) == 0)
2108 * mnoarg() (matches functions with no argument type information)
2109 * returns 1 if all parameters of a prototype are compatible with
2110 * and old style function declaration.
2111 * This is the case if following conditions are met:
2112 * 1. the prototype must have a fixed number of parameters
2113 * 2. no parameter is of type float
2114 * 3. no parameter is converted to another type if integer promotion
2129 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) {
2130 if ((t = arg->s_type->t_tspec) == FLOAT ||
2131 t == CHAR || t == SCHAR || t == UCHAR ||
2132 t == SHORT || t == USHORT) {
2141 * Compares a prototype declaration with the remembered arguments of
2142 * a previous old style function definition.
2145 chkosdef(rdsym, dsym)
2146 sym_t *rdsym, *dsym;
2148 sym_t *args, *pargs, *arg, *parg;
2152 args = rdsym->s_args;
2153 pargs = dsym->s_type->t_args;
2158 for (arg = args; arg != NULL; arg = arg->s_nxt)
2160 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2162 if (narg != nparg) {
2163 /* prototype does not match old-style definition */
2175 * If it does not match due to promotion and sflag is
2176 * not set we print only a warning.
2178 if (!eqtype(arg->s_type, parg->s_type, 1, 1, &warn) || warn) {
2179 /* prototype does not match old-style def., arg #%d */
2190 /* old style definition */
2191 prevdecl(300, rdsym);
2197 * Complets a type by copying the dimension and prototype information
2198 * from a second compatible type.
2200 * Following lines are legal:
2201 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2202 * "typedef ft(); ft f; f(int); ft g; g(long);"
2203 * This means that, if a type is completed, the type structure must
2207 compltyp(dsym, ssym)
2210 type_t **dstp, *src;
2213 dstp = &dsym->s_type;
2216 while ((dst = *dstp) != NULL) {
2217 if (src == NULL || dst->t_tspec != src->t_tspec)
2218 lerror("compltyp() 1");
2219 if (dst->t_tspec == ARRAY) {
2220 if (dst->t_dim == 0 && src->t_dim != 0) {
2221 *dstp = dst = duptyp(dst);
2222 dst->t_dim = src->t_dim;
2223 /* now a complete Typ */
2226 } else if (dst->t_tspec == FUNC) {
2227 if (!dst->t_proto && src->t_proto) {
2228 *dstp = dst = duptyp(dst);
2230 dst->t_args = src->t_args;
2233 dstp = &dst->t_subt;
2239 * Completes the declaration of a single argument.
2242 decl1arg(sym, initflg)
2252 if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) {
2253 /* redeclaration of formal parameter %s */
2254 error(237, sym->s_name);
2255 rmsym(dcs->d_rdcsym);
2260 /* declared argument %s is missing */
2261 error(53, sym->s_name);
2266 /* cannot initialize parameter: %s */
2267 error(52, sym->s_name);
2271 if ((t = sym->s_type->t_tspec) == ARRAY) {
2272 sym->s_type = incref(sym->s_type->t_subt, PTR);
2273 } else if (t == FUNC) {
2275 /* a function is declared as an argument: %s */
2276 warning(50, sym->s_name);
2277 sym->s_type = incref(sym->s_type, PTR);
2278 } else if (t == FLOAT) {
2280 sym->s_type = gettyp(DOUBLE);
2284 /* argument declared inline: %s */
2285 warning(269, sym->s_name);
2288 * Arguments must have complete types. lengths() prints the needed
2289 * error messages (null dimension is impossible because arrays are
2290 * converted to pointers).
2292 if (sym->s_type->t_tspec != VOID)
2293 (void)length(sym->s_type, sym->s_name);
2301 * Does some checks for lint directives which apply to functions.
2302 * Processes arguments in old style function definitions which default
2304 * Checks compatiblility of old style function definition with previous
2310 sym_t *args, *arg, *pargs, *parg;
2311 int narg, nparg, n, msg;
2314 args = funcsym->s_args;
2315 pargs = funcsym->s_type->t_args;
2317 /* check for illegal combinations of lint directives */
2318 if (prflstrg != -1 && scflstrg != -1) {
2319 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2321 prflstrg = scflstrg = -1;
2323 if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) {
2324 /* dubious use of ** VARARGS ** with ** %s ** */
2325 warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2330 * check if the argument of a lint directive is compatible with the
2331 * number of arguments.
2334 for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_nxt)
2336 if (nargusg > narg) {
2337 /* argument number mismatch with directive: ** %s ** */
2338 warning(283, "ARGSUSED");
2341 if (nvararg > narg) {
2342 /* argument number mismatch with directive: ** %s ** */
2343 warning(283, "VARARGS");
2346 if (prflstrg > narg) {
2347 /* argument number mismatch with directive: ** %s ** */
2348 warning(283, "PRINTFLIKE");
2350 } else if (prflstrg == 0) {
2353 if (scflstrg > narg) {
2354 /* argument number mismatch with directive: ** %s ** */
2355 warning(283, "SCANFLIKE");
2357 } else if (scflstrg == 0) {
2360 if (prflstrg != -1 || scflstrg != -1) {
2361 narg = prflstrg != -1 ? prflstrg : scflstrg;
2363 for (n = 1; n < narg; n++)
2365 if (arg->s_type->t_tspec != PTR ||
2366 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2367 t != UCHAR && t != SCHAR)) {
2368 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2370 prflstrg = scflstrg = -1;
2375 * print a warning for each argument off an old style function
2376 * definition which defaults to int
2378 for (arg = args; arg != NULL; arg = arg->s_nxt) {
2379 if (arg->s_defarg) {
2380 /* argument type defaults to int: %s */
2381 warning(32, arg->s_name);
2388 * If this is an old style function definition and a prototyp
2389 * exists, compare the types of arguments.
2391 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2393 * If the number of arguments does not macht, we need not
2398 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2400 for (arg = args; arg != NULL; arg = arg->s_nxt)
2402 if (narg != nparg) {
2403 /* parameter mismatch: %d declared, %d defined */
2404 error(51, nparg, narg);
2410 msg |= chkptdecl(arg, parg);
2416 /* prototype declaration */
2417 prevdecl(285, dcs->d_rdcsym);
2419 /* from now the prototype is valid */
2420 funcsym->s_osdef = 0;
2421 funcsym->s_args = NULL;
2428 * Checks compatibility of an old style function definition with a previous
2429 * prototype declaration.
2430 * Returns 1 if the position of the previous declaration should be reported.
2433 chkptdecl(arg, parg)
2445 if (!eqtype(tp, ptp, 1, 1, &warn)) {
2446 if (eqtype(tp, ptp, 1, 0, &warn)) {
2447 /* type does not match prototype: %s */
2448 msg = gnuism(58, arg->s_name);
2450 /* type does not match prototype: %s */
2451 error(58, arg->s_name);
2455 /* type does not match prototype: %s */
2456 (*(sflag ? error : warning))(58, arg->s_name);
2464 * Completes a single local declaration/definition.
2467 decl1loc(dsym, initflg)
2471 /* Correct a mistake done in dname(). */
2472 if (dsym->s_type->t_tspec == FUNC) {
2474 if (dcs->d_scl == NOSCL)
2475 dsym->s_scl = EXTERN;
2478 if (dsym->s_type->t_tspec == FUNC) {
2479 if (dsym->s_scl == STATIC) {
2480 /* dubious static function at block level: %s */
2481 warning(93, dsym->s_name);
2482 dsym->s_scl = EXTERN;
2483 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2484 /* function has illegal storage class: %s */
2485 error(94, dsym->s_name);
2486 dsym->s_scl = EXTERN;
2491 * functions may be declared inline at local scope, although
2492 * this has no effect for a later definition of the same
2494 * XXX it should have an effect if tflag is set. this would
2495 * also be the way gcc behaves.
2497 if (dcs->d_inline) {
2498 if (dsym->s_type->t_tspec == FUNC) {
2501 /* variable declared inline: %s */
2502 warning(268, dsym->s_name);
2510 if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN)
2513 if (dsym->s_scl == EXTERN) {
2515 * XXX wenn die statische Variable auf Ebene 0 erst
2516 * spaeter definiert wird, haben wir die Brille auf.
2518 if (dsym->s_xsym == NULL) {
2519 outsym(dsym, EXTERN, dsym->s_def);
2521 outsym(dsym, dsym->s_xsym->s_scl, dsym->s_def);
2525 if (dcs->d_rdcsym != NULL) {
2527 if (dcs->d_rdcsym->s_blklev == 0) {
2529 switch (dsym->s_scl) {
2531 /* automatic hides external declaration: %s */
2533 warning(86, dsym->s_name);
2536 /* static hides external declaration: %s */
2538 warning(87, dsym->s_name);
2541 /* typedef hides external declaration: %s */
2543 warning(88, dsym->s_name);
2547 * Warnings and errors are printed in ledecl()
2551 lerror("decl1loc() 1");
2554 } else if (dcs->d_rdcsym->s_blklev == blklev) {
2556 /* no hflag, because its illegal! */
2557 if (dcs->d_rdcsym->s_arg) {
2559 * if !tflag, a "redeclaration of %s" error
2564 /* decl. hides parameter: %s */
2565 warning(91, dsym->s_name);
2566 rmsym(dcs->d_rdcsym);
2570 } else if (dcs->d_rdcsym->s_blklev < blklev) {
2573 /* declaration hides earlier one: %s */
2574 warning(95, dsym->s_name);
2578 if (dcs->d_rdcsym->s_blklev == blklev) {
2580 /* redeclaration of %s */
2581 error(27, dsym->s_name);
2582 rmsym(dcs->d_rdcsym);
2588 if (initflg && !(initerr = chkinit(dsym))) {
2593 if (dsym->s_scl == TYPEDEF) {
2594 dsym->s_type = duptyp(dsym->s_type);
2595 dsym->s_type->t_typedef = 1;
2596 settdsym(dsym->s_type, dsym);
2600 * Before we can check the size we must wait for a initialisation
2606 * Processes (re)declarations of external Symbols inside blocks.
2615 /* look for a symbol with the same name */
2616 esym = dcs->d_rdcsym;
2617 while (esym != NULL && esym->s_blklev != 0) {
2618 while ((esym = esym->s_link) != NULL) {
2619 if (esym->s_kind != FVFT)
2621 if (strcmp(dsym->s_name, esym->s_name) == 0)
2627 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2628 /* gcc accepts this without a warning, pcc prints an error. */
2629 /* redeclaration of %s */
2630 warning(27, dsym->s_name);
2636 eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &warn);
2639 if (esym->s_scl == EXTERN) {
2640 /* inconsistent redeclaration of extern: %s */
2641 warning(90, dsym->s_name);
2644 /* inconsistent redeclaration of static: %s */
2645 warning(92, dsym->s_name);
2652 * Remember the external symbol so we can update usage
2653 * information at the end of the block.
2655 dsym->s_xsym = esym;
2660 * Print an error or a warning if the symbol cant be initialized due
2661 * to type/storage class. Returnvalue is 1 if an error has been
2672 if (sym->s_type->t_tspec == FUNC) {
2673 /* cannot initialize function: %s */
2674 error(24, sym->s_name);
2676 } else if (sym->s_scl == TYPEDEF) {
2677 /* cannot initialize typedef: %s */
2678 error(25, sym->s_name);
2680 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2681 /* cannot initialize "extern" declaration: %s */
2682 if (dcs->d_ctx == EXTERN) {
2683 warning(26, sym->s_name);
2685 error(26, sym->s_name);
2694 * Create a symbole for an abstract declaration.
2701 if (dcs->d_ctx != ABSTRACT && dcs->d_ctx != PARG)
2704 sym = getblk(sizeof (sym_t));
2706 sym->s_name = unnamed;
2708 sym->s_scl = ABSTRACT;
2711 if (dcs->d_ctx == PARG)
2714 sym->s_type = dcs->d_type;
2715 dcs->d_rdcsym = NULL;
2722 * Removes anything which has nothing to do on global level.
2727 while (dcs->d_nxt != NULL)
2735 * remove all informations about pending lint directives without
2742 * Process an abstract type declaration
2754 * Checks size after declarations of variables and their initialisation.
2761 * check size only for symbols which are defined and no function and
2764 if (dsym->s_def != DEF)
2766 if (dsym->s_scl == TYPEDEF)
2768 if (dsym->s_type->t_tspec == FUNC)
2771 if (length(dsym->s_type, dsym->s_name) == 0 &&
2772 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2773 /* empty array declaration: %s */
2775 warning(190, dsym->s_name);
2777 error(190, dsym->s_name);
2783 * Mark an object as set if it is not already
2791 STRUCT_ASSIGN(sym->s_spos, curr_pos);
2796 * Mark an object as used if it is not already
2799 setuflg(sym, fcall, szof)
2805 STRUCT_ASSIGN(sym->s_upos, curr_pos);
2808 * for function calls another record is written
2810 * XXX Should symbols used in sizeof() treated as used or not?
2811 * Probably not, because there is no sense to declare an
2812 * external variable only to get their size.
2814 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2819 * Prints warnings for a list of variables and labels (concatenated
2820 * with s_dlnxt) if these are not used or only set.
2829 /* for this warnings LINTED has no effect */
2833 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
2834 chkusg1(di->d_asm, sym);
2840 * Prints a warning for a single variable or label if it is not used or
2850 if (sym->s_blklev == -1)
2853 STRUCT_ASSIGN(cpos, curr_pos);
2855 if (sym->s_kind == FVFT) {
2857 chkausg(novar, sym);
2859 chkvusg(novar, sym);
2861 } else if (sym->s_kind == FLAB) {
2863 } else if (sym->s_kind == FTAG) {
2867 STRUCT_ASSIGN(curr_pos, cpos);
2876 lerror("chkausg() 1");
2881 if (!arg->s_used && vflag) {
2882 STRUCT_ASSIGN(curr_pos, arg->s_dpos);
2883 /* argument %s unused in function %s */
2884 warning(231, arg->s_name, funcsym->s_name);
2896 if (blklev == 0 || sym->s_blklev == 0)
2897 lerror("chkvusg() 1");
2899 /* errors in expressions easily cause lots of these warnings */
2904 * XXX Only variables are checkd, although types should
2905 * probably also be checked
2907 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
2908 sc != AUTO && sc != REG) {
2916 if (!sym->s_used && !sym->s_set) {
2917 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2918 /* %s unused in function %s */
2919 warning(192, sym->s_name, funcsym->s_name);
2922 if (sym->s_set && !sym->s_used) {
2923 STRUCT_ASSIGN(curr_pos, sym->s_spos);
2924 /* %s set but not used in function %s */
2925 warning(191, sym->s_name, funcsym->s_name);
2926 } else if (!sym->s_used) {
2927 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2928 /* %s unused in function %s */
2929 warning(192, sym->s_name, funcsym->s_name);
2935 * information about usage is taken over into the symbol
2936 * tabel entry at level 0 if the symbol was locally declared
2937 * as an external symbol.
2939 * XXX This is wrong for symbols declared static at level 0
2940 * if the usage information stems from sizeof(). This is
2941 * because symbols at level 0 only used in sizeof() are
2942 * considered to not be used.
2944 if ((xsym = sym->s_xsym) != NULL) {
2945 if (sym->s_used && !xsym->s_used) {
2947 STRUCT_ASSIGN(xsym->s_upos, sym->s_upos);
2949 if (sym->s_set && !xsym->s_set) {
2951 STRUCT_ASSIGN(xsym->s_spos, sym->s_spos);
2961 if (blklev != 1 || lab->s_blklev != 1)
2962 lerror("chklusg() 1");
2964 if (lab->s_set && !lab->s_used) {
2965 STRUCT_ASSIGN(curr_pos, lab->s_spos);
2966 /* label %s unused in function %s */
2967 warning(192, lab->s_name, funcsym->s_name);
2968 } else if (!lab->s_set) {
2969 STRUCT_ASSIGN(curr_pos, lab->s_upos);
2970 /* undefined label %s */
2971 warning(23, lab->s_name);
2979 if (!incompl(sym->s_type))
2982 /* complain alwasy about incomplet tags declared inside blocks */
2983 if (!zflag || dcs->d_ctx != EXTERN)
2986 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2987 switch (sym->s_type->t_tspec) {
2989 /* struct %s never defined */
2990 warning(233, sym->s_name);
2993 /* union %s never defined */
2994 warning(234, sym->s_name);
2997 /* enum %s never defined */
2998 warning(235, sym->s_name);
3001 lerror("chktusg() 1");
3006 * Called after the entire translation unit has been parsed.
3007 * Changes tentative definitions in definitions.
3008 * Performs some tests on global Symbols. Detected Problems are:
3009 * - defined variables of incomplete type
3010 * - constant variables which are not initialized
3011 * - static symbols which are never used
3019 if (blklev != 0 || dcs->d_nxt != NULL)
3022 STRUCT_ASSIGN(cpos, curr_pos);
3024 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
3025 if (sym->s_blklev == -1)
3027 if (sym->s_kind == FVFT) {
3029 } else if (sym->s_kind == FTAG) {
3032 if (sym->s_kind != FMOS)
3033 lerror("chkglsyms() 1");
3037 STRUCT_ASSIGN(curr_pos, cpos);
3044 if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON)
3047 if (sym->s_scl != EXTERN && sym->s_scl != STATIC)
3048 lerror("chkglvar() 1");
3052 if (sym->s_scl == STATIC) {
3053 if (sym->s_type->t_tspec == FUNC) {
3054 if (sym->s_used && sym->s_def != DEF) {
3055 STRUCT_ASSIGN(curr_pos, sym->s_upos);
3056 /* static func. called but not def.. */
3057 error(225, sym->s_name);
3061 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3062 if (sym->s_type->t_tspec == FUNC) {
3063 if (sym->s_def == DEF) {
3065 /* static function %s unused */
3066 warning(236, sym->s_name);
3068 /* static function %s decl. but ... */
3069 warning(290, sym->s_name);
3071 } else if (!sym->s_set) {
3072 /* static variable %s unused */
3073 warning(226, sym->s_name);
3075 /* static variable %s set but not used */
3076 warning(307, sym->s_name);
3079 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
3080 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3081 /* const object %s should have initializer */
3082 warning(227, sym->s_name);
3091 if (sym->s_def == TDEF) {
3092 if (sym->s_type->t_tspec == FUNC)
3094 * this can happen if an syntax error occured
3095 * after a function declaration
3098 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3099 if (length(sym->s_type, sym->s_name) == 0 &&
3100 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3101 /* empty array declaration: %s */
3102 if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3103 warning(190, sym->s_name);
3105 error(190, sym->s_name);
3112 * Prints information about location of previous definition/declaration.
3124 STRUCT_ASSIGN(cpos, curr_pos);
3125 STRUCT_ASSIGN(curr_pos, psym->s_dpos);
3127 message(msg, psym->s_name);
3128 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3129 /* previous definition of %s */
3130 message(261, psym->s_name);
3132 /* previous declaration of %s */
3133 message(260, psym->s_name);
3135 STRUCT_ASSIGN(curr_pos, cpos);