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.2 2003/06/17 04:29:34 dillon 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 __P((type_t *, tspec_t));
62 static void settdsym __P((type_t *, sym_t *));
63 static tspec_t mrgtspec __P((tspec_t, tspec_t));
64 static void align __P((int, int));
65 static sym_t *newtag __P((sym_t *, scl_t, int, int));
66 static int eqargs __P((type_t *, type_t *, int *));
67 static int mnoarg __P((type_t *, int *));
68 static int chkosdef __P((sym_t *, sym_t *));
69 static int chkptdecl __P((sym_t *, sym_t *));
70 static sym_t *nsfunc __P((sym_t *, sym_t *));
71 static void osfunc __P((sym_t *, sym_t *));
72 static void ledecl __P((sym_t *));
73 static int chkinit __P((sym_t *));
74 static void chkausg __P((int, sym_t *));
75 static void chkvusg __P((int, sym_t *));
76 static void chklusg __P((sym_t *));
77 static void chktusg __P((sym_t *));
78 static void chkglvar __P((sym_t *));
79 static void glchksz __P((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).
213 return (&typetab[t]);
222 ntp = getblk(sizeof (type_t));
223 STRUCT_ASSIGN(*ntp, *tp);
228 * Use tduptyp() instead of duptyp() inside expressions (if the
229 * allocated memory should be freed after the expr).
237 ntp = tgetblk(sizeof (type_t));
238 STRUCT_ASSIGN(*ntp, *tp);
243 * Returns 1 if the argument is void or an incomplete array,
244 * struct, union or enum type.
252 if ((t = tp->t_tspec) == VOID) {
254 } else if (t == ARRAY) {
255 return (tp->t_aincompl);
256 } else if (t == STRUCT || t == UNION) {
257 return (tp->t_str->sincompl);
258 } else if (t == ENUM) {
259 return (tp->t_enum->eincompl);
265 * Set the flag for (in)complete array, struct, union or enum
275 if ((t = tp->t_tspec) == ARRAY) {
277 } else if (t == STRUCT || t == UNION) {
278 tp->t_str->sincompl = ic;
281 lerror("setcompl() 1");
282 tp->t_enum->eincompl = ic;
287 * Remember the storage class of the current declaration in dcs->d_scl
288 * (the top element of the declaration stack) and detect multiple
298 warning(10, "inline");
302 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
303 dcs->d_smod != NOTSPEC || dcs->d_lmod != NOTSPEC) {
304 /* storage class after type is obsolescent */
307 if (dcs->d_scl == NOSCL) {
311 * multiple storage classes. An error will be reported in
319 * Remember the type, modifier or typedef name returned by the parser
320 * in *dcs (top element of decl stack). This information is used in
321 * deftyp() to build the type used for all declarators in this
324 * Is tp->t_typedef 1, the type comes from a previously defined typename.
325 * Otherwise it comes from a type specifier (int, long, ...) or a
326 * struct/union/enum tag.
335 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
336 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) {
338 * something like "typedef int a; int a b;"
339 * This should not happen with current grammar.
349 if (t == STRUCT || t == UNION || t == ENUM) {
351 * something like "int struct a ..."
352 * struct/union/enum with anything else is not allowed
354 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
355 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) {
357 * remember that an error must be reported in
361 dcs->d_atyp = dcs->d_lmod = dcs->d_smod = NOTSPEC;
367 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
369 * something like "struct a int"
370 * struct/union/enum with anything else is not allowed
376 if (t == LONG && dcs->d_lmod == LONG) {
377 /* "long long" or "long ... long" */
379 dcs->d_lmod = NOTSPEC;
381 /* %s C does not support 'long long' */
382 (void)gnuism(265, tflag ? "traditional" : "ANSI");
385 if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
386 /* something like "typedef int a; a long ..." */
387 dcs->d_type = tdeferr(dcs->d_type, t);
391 /* now it can be only a combination of arithmetic types and void */
392 if (t == SIGNED || t == UNSIGN) {
393 /* remeber specifiers "signed" and "unsigned" in dcs->d_smod */
394 if (dcs->d_smod != NOTSPEC)
396 * more then one "signed" and/or "unsigned"; print
397 * an error in deftyp()
401 } else if (t == SHORT || t == LONG || t == QUAD) {
403 * remember specifiers "short", "long" and "long long" in
406 if (dcs->d_lmod != NOTSPEC)
407 /* more than one, print error in deftyp() */
412 * remember specifiers "void", "char", "int", "float" or
413 * "double" int dcs->d_atyp
415 if (dcs->d_atyp != NOTSPEC)
416 /* more than one, print error in deftyp() */
423 * called if a list of declaration specifiers contains a typedef name
424 * and other specifiers (except struct, union, enum, typedef name)
438 if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG ||
441 /* modifying typedef with ... */
442 warning(5, ttab[t].tt_name);
443 td = duptyp(gettyp(mrgtspec(t2, t)));
449 if (t2 == INT || t2 == UINT) {
450 /* modifying typedef with ... */
452 td = duptyp(gettyp(t2 == INT ? SHORT : USHORT));
458 if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG ||
459 t2 == FLOAT || t2 == DOUBLE) {
460 /* modifying typedef with ... */
464 } else if (t2 == UINT) {
466 } else if (t2 == LONG) {
468 } else if (t2 == ULONG) {
470 } else if (t2 == FLOAT) {
472 } else if (t2 == DOUBLE) {
473 td = gettyp(LDOUBLE);
480 /* LINTED (enumeration values not handled in switch) */
484 /* Anything other is not accepted. */
491 * Remember the symbol of a typedef name (2nd arg) in a struct, union
492 * or enum tag if the typedef name is the first defined for this tag.
494 * If the tag is unnamed, the typdef name is used for identification
495 * of this tag in lint2. Although its possible that more then one typedef
496 * name is defined for one tag, the first name defined should be unique
497 * if the tag is unnamed.
506 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
507 if (tp->t_str->stdef == NULL)
508 tp->t_str->stdef = sym;
509 } else if (t == ENUM) {
510 if (tp->t_enum->etdef == NULL)
511 tp->t_enum->etdef = sym;
516 * Remember a qualifier which is part of the declaration specifiers
517 * (and not the declarator) in the top element of the declaration stack.
518 * Also detect multiple qualifiers of the same kind.
520 * The rememberd qualifier is used by deftyp() to construct the type
521 * for all declarators.
530 warning(10, "const");
535 lerror("addqual() 1");
536 if (dcs->d_volatile) {
538 warning(10, "volatile");
545 * Go to the next declaration level (structs, nested structs, blocks,
546 * argument declaration lists ...)
555 (void)printf("pushdecl(%d)\n", (int)sc);
557 /* put a new element on the declaration stack */
558 di = xcalloc(1, sizeof (dinfo_t));
562 di->d_ldlsym = &di->d_dlsyms;
566 * Go back to previous declaration level
574 (void)printf("popdecl(%d)\n", (int)dcs->d_ctx);
576 if (dcs->d_nxt == NULL)
577 lerror("popdecl() 1");
582 /* there is nothing after external declarations */
583 lerror("popdecl() 2");
589 * Symbols declared in (nested) structs or enums are
590 * part of the next level (they are removed from the
591 * symbol table if the symbols of the outher level are
594 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
595 dcs->d_ldlsym = di->d_ldlsym;
599 * All symbols in dcs->d_dlsyms are introduced in old style
600 * argument declarations (it's not clean, but possible).
601 * They are appended to the list of symbols declared in
602 * an old style argument identifier list or a new style
603 * parameter type list.
605 if (di->d_dlsyms != NULL) {
606 *di->d_ldlsym = dcs->d_fpsyms;
607 dcs->d_fpsyms = di->d_dlsyms;
613 * Append all symbols declared in the abstract declaration
614 * to the list of symbols declared in the surounding decl.
616 * XXX I'm not sure whether they should be removed from the
617 * symbol table now or later.
619 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
620 dcs->d_ldlsym = di->d_ldlsym;
623 /* check usage of local vars */
627 /* usage of arguments will be checked by funcend() */
628 rmsyms(di->d_dlsyms);
631 lerror("popdecl() 3");
637 * Set flag d_asm in all declaration stack elements up to the
640 * This is used to mark compound statements which have, possibly in
641 * nested compound statements, asm statements. For these compound
642 * statements no warnings about unused or unitialized variables are
645 * There is no need to clear d_asm in dinfo structs with context AUTO,
646 * because these structs are freed at the end of the compound statement.
647 * But it must be cleard in the outermost dinfo struct, which has
648 * context EXTERN. This could be done in clrtyp() and would work for
649 * C, but not for C++ (due to mixed statements and declarations). Thus
650 * we clear it in glclup(), which is used to do some cleanup after
651 * global declarations/definitions.
658 for (di = dcs; di != NULL; di = di->d_nxt)
663 * Clean all elements of the top element of declaration stack which
664 * will be used by the next declaration
669 dcs->d_atyp = dcs->d_smod = dcs->d_lmod = NOTSPEC;
672 dcs->d_const = dcs->d_volatile = 0;
674 dcs->d_mscl = dcs->d_terr = 0;
680 * Create a type structure from the informations gathered in
681 * the declaration stack.
682 * Complain about storage classes which are not possible in current
692 t = dcs->d_atyp; /* CHAR, INT, FLOAT, DOUBLE, VOID */
693 s = dcs->d_smod; /* SIGNED, UNSIGNED */
694 l = dcs->d_lmod; /* SHORT, LONG, QUAD */
698 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && tp == NULL)
701 if (tp != NULL && (t != NOTSPEC || s != NOTSPEC || l != NOTSPEC)) {
702 /* should never happen */
703 lerror("deftyp() 1");
726 /* use 'double' instead of ... */
735 /* 'long double' is illegal in ... */
742 lerror("deftyp() 2");
744 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) {
750 dcs->d_type = gettyp(mrgtspec(t, s));
754 /* only one storage class allowed */
758 /* illegal type combination */
762 if (dcs->d_ctx == EXTERN) {
763 if (scl == REG || scl == AUTO) {
764 /* illegal storage class */
768 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PARG) {
769 if (scl != NOSCL && scl != REG) {
770 /* only "register" valid ... */
778 if (dcs->d_const && dcs->d_type->t_const) {
779 if (!dcs->d_type->t_typedef)
780 lerror("deftyp() 3");
781 /* typedef already qualified with "%s" */
782 warning(68, "const");
784 if (dcs->d_volatile && dcs->d_type->t_volatile) {
785 if (!dcs->d_type->t_typedef)
786 lerror("deftyp() 4");
787 /* typedef already qualified with "%s" */
788 warning(68, "volatile");
791 if (dcs->d_const || dcs->d_volatile) {
792 dcs->d_type = duptyp(dcs->d_type);
793 dcs->d_type->t_const |= dcs->d_const;
794 dcs->d_type->t_volatile |= dcs->d_volatile;
799 * Merge type specifiers (char, ..., long long, signed, unsigned).
805 if (s == SIGNED || s == UNSIGN) {
807 t = s == SIGNED ? SCHAR : UCHAR;
808 } else if (t == SHORT) {
809 t = s == SIGNED ? SHORT : USHORT;
810 } else if (t == INT) {
811 t = s == SIGNED ? INT : UINT;
812 } else if (t == LONG) {
813 t = s == SIGNED ? LONG : ULONG;
814 } else if (t == QUAD) {
815 t = s == SIGNED ? QUAD : UQUAD;
823 * Return the length of a type in bit.
825 * Printing a message if the outhermost dimension of an array is 0 must
826 * be done by the caller. All other problems are reported by length()
827 * if name is not NULL.
837 while (tp->t_tspec == ARRAY) {
841 switch (tp->t_tspec) {
843 /* compiler takes size of function */
844 lerror("%s", msgs[12]);
848 if (incompl(tp) && name != NULL) {
849 /* incomplete structure or union %s: %s */
850 error(31, tp->t_str->stag->s_name, name);
852 elsz = tp->t_str->size;
855 if (incompl(tp) && name != NULL) {
856 /* incomplete enum type: %s */
861 elsz = size(tp->t_tspec);
866 return (elem * elsz);
870 * Get the alignment of the given Type in bits.
879 while (tp->t_tspec == ARRAY)
882 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
883 a = tp->t_str->align;
884 } else if (t == FUNC) {
885 /* compiler takes alignment of function */
887 a = ALIGN(1) * CHAR_BIT;
889 if ((a = size(t)) == 0) {
891 } else if (a > ALIGN(1) * CHAR_BIT) {
892 a = ALIGN(1) * CHAR_BIT;
895 if (a < CHAR_BIT || a > ALIGN(1) * CHAR_BIT)
896 lerror("getbound() 1");
901 * Concatenate two lists of symbols by s_nxt. Used by declarations of
902 * struct/union/enum elements and parameters.
910 if ((l = l1) == NULL)
912 while (l1->s_nxt != NULL)
919 * Check if the type of the given symbol is valid and print an error
920 * message if it is not.
923 * - arrays of incomlete types or functions
924 * - functions returning arrays or functions
925 * - void types other than type of function or pointer
936 while ((tp = *tpp) != NULL) {
939 * If this is the type of an old style function definition,
940 * a better warning is printed in funcdef().
942 if (t == FUNC && !tp->t_proto &&
943 !(to == NOTSPEC && sym->s_osdef)) {
945 /* function declaration is not a prototype */
949 if (t == FUNC || t == ARRAY) {
950 /* function returns illegal type */
953 *tpp = incref(*tpp, PTR);
955 *tpp = incref((*tpp)->t_subt, PTR);
958 } else if (tp->t_const || tp->t_volatile) {
959 if (sflag) { /* XXX oder better !tflag ? */
960 /* function cannot return const... */
966 /* array of function is illegal */
970 } else if (t == ARRAY && tp->t_dim == 0) {
974 } else if (t == VOID) {
975 /* illegal use of void */
978 #if 0 /* errors are produced by length() */
979 } else if (incompl(tp)) {
980 /* array of incomplete type */
988 } else if (to == NOTSPEC && t == VOID) {
989 if (dcs->d_ctx == PARG) {
990 if (sym->s_scl != ABSTRACT) {
991 if (sym->s_name == unnamed)
993 /* void param cannot have name: %s */
994 error(61, sym->s_name);
997 } else if (dcs->d_ctx == ABSTRACT) {
999 } else if (sym->s_scl != TYPEDEF) {
1000 /* void type for %s */
1001 error(19, sym->s_name);
1005 if (t == VOID && to != PTR) {
1006 if (tp->t_const || tp->t_volatile) {
1007 /* inappropriate qualifiers with "void" */
1009 tp->t_const = tp->t_volatile = 0;
1018 * Process the declarator of a struct/union element.
1029 if ((sc = dsym->s_scl) != MOS && sc != MOU)
1030 lerror("decl1str() 1");
1032 if (dcs->d_rdcsym != NULL) {
1033 if ((sc = dcs->d_rdcsym->s_scl) != MOS && sc != MOU)
1034 /* should be ensured by storesym() */
1035 lerror("decl1str() 2");
1036 if (dsym->s_styp == dcs->d_rdcsym->s_styp) {
1037 /* duplicate member name: %s */
1038 error(33, dsym->s_name);
1039 rmsym(dcs->d_rdcsym);
1045 t = (tp = dsym->s_type)->t_tspec;
1047 if (dsym->s_field) {
1051 * only unsigned und signed int are protable bit-field types
1052 *(at least in ANSI C, in traditional C only unsigned int)
1054 if (t == CHAR || t == UCHAR || t == SCHAR ||
1055 t == SHORT || t == USHORT || t == ENUM) {
1057 /* bit-field type '%s' invalid in ANSI C */
1058 warning(273, tyname(tp));
1060 /* nonportable bit-field type */
1063 } else if (t == INT && dcs->d_smod == NOTSPEC) {
1065 /* nonportable bit-field type */
1068 } else if (t != INT && t != UINT) {
1069 /* illegal bit-field type */
1072 dsym->s_type = tp = duptyp(gettyp(t = INT));
1073 if ((tp->t_flen = sz) > size(t))
1074 tp->t_flen = size(t);
1076 if ((len = tp->t_flen) < 0 || len > size(t)) {
1077 /* illegal bit-field size */
1079 tp->t_flen = size(t);
1080 } else if (len == 0 && dsym->s_name != unnamed) {
1081 /* zero size bit-field */
1083 tp->t_flen = size(t);
1085 if (dsym->s_scl == MOU) {
1086 /* illegal use of bit-field */
1088 dsym->s_type->t_isfield = 0;
1091 } else if (t == FUNC) {
1092 /* function illegal in structure or union */
1094 dsym->s_type = tp = incref(tp, t = PTR);
1098 * bit-fields of length 0 are not warned about because length()
1099 * does not return the length of the bit-field but the length
1100 * of the type the bit-field is packed in (its ok)
1102 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1103 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1104 /* illegal zero sized structure member: %s */
1105 warning(39, dsym->s_name);
1109 if (dcs->d_ctx == MOU) {
1113 if (dsym->s_field) {
1114 align(getbound(tp), tp->t_flen);
1115 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t);
1116 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1117 dcs->d_offset += tp->t_flen;
1119 align(getbound(tp), 0);
1120 dsym->s_value.v_quad = dcs->d_offset;
1121 dcs->d_offset += sz;
1123 if (dcs->d_ctx == MOU) {
1124 if (o > dcs->d_offset)
1134 * Aligns next structure element as required.
1136 * al contains the required alignment, len the length of a bit-field.
1145 * The alignment of the current element becomes the alignment of
1146 * the struct/union if it is larger than the current alignment
1147 * of the struct/union.
1149 if (al > dcs->d_stralign)
1150 dcs->d_stralign = al;
1152 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1153 if (len == 0 || dcs->d_offset + len > no)
1158 * Remember the width of the field in its type structure.
1166 dsym = getblk(sizeof (sym_t));
1167 dsym->s_name = unnamed;
1168 dsym->s_kind = FMOS;
1170 dsym->s_type = gettyp(INT);
1171 dsym->s_blklev = -1;
1173 dsym->s_type = duptyp(dsym->s_type);
1174 dsym->s_type->t_isfield = 1;
1175 dsym->s_type->t_flen = len;
1181 * Collect informations about a sequence of asterisks and qualifiers
1182 * in a list of type pqinf_t.
1183 * Qualifiers refer always to the left asterisk. The rightmost asterisk
1184 * will be at the top of the list.
1192 if (p2->p_pcnt != 0) {
1193 /* left '*' at the end of the list */
1194 for (p = p2; p->p_nxt != NULL; p = p->p_nxt) ;
1201 warning(10, "const");
1205 if (p2->p_volatile) {
1206 if (p1->p_volatile) {
1208 warning(10, "volatile");
1218 * Followint 3 functions extend the type of a declarator with
1219 * pointer, function and array types.
1221 * The current type is the Type built by deftyp() (dcs->d_type) and
1222 * pointer, function and array types already added for this
1223 * declarator. The new type extension is inserted between both.
1233 tpp = &decl->s_type;
1234 while (*tpp != dcs->d_type)
1235 tpp = &(*tpp)->t_subt;
1237 while (pi != NULL) {
1238 *tpp = tp = getblk(sizeof (type_t));
1240 tp->t_const = pi->p_const;
1241 tp->t_volatile = pi->p_volatile;
1242 *(tpp = &tp->t_subt) = dcs->d_type;
1251 * If a dimension was specified, dim is 1, otherwise 0
1252 * n is the specified dimension
1255 addarray(decl, dim, n)
1261 tpp = &decl->s_type;
1262 while (*tpp != dcs->d_type)
1263 tpp = &(*tpp)->t_subt;
1265 *tpp = tp = getblk(sizeof (type_t));
1266 tp->t_tspec = ARRAY;
1267 tp->t_subt = dcs->d_type;
1271 /* zero or negative array dimension */
1274 } else if (n == 0 && dim) {
1275 /* zero or negative array dimension */
1277 } else if (n == 0 && !dim) {
1278 /* is incomplete type */
1293 /* function prototypes are illegal in traditional C */
1295 args = nsfunc(decl, args);
1301 * The symbols are removed from the symbol table by popdecl() after
1302 * addfunc(). To be able to restore them if this is a function
1303 * definition, a pointer to the list of all symbols is stored in
1304 * dcs->d_nxt->d_fpsyms. Also a list of the arguments (concatenated
1305 * by s_nxt) is stored in dcs->d_nxt->d_fargs.
1306 * (dcs->d_nxt must be used because *dcs is the declaration stack
1307 * element created for the list of params and is removed after
1310 if (dcs->d_nxt->d_ctx == EXTERN &&
1311 decl->s_type == dcs->d_nxt->d_type) {
1312 dcs->d_nxt->d_fpsyms = dcs->d_dlsyms;
1313 dcs->d_nxt->d_fargs = args;
1316 tpp = &decl->s_type;
1317 while (*tpp != dcs->d_nxt->d_type)
1318 tpp = &(*tpp)->t_subt;
1320 *tpp = tp = getblk(sizeof (type_t));
1322 tp->t_subt = dcs->d_nxt->d_type;
1323 if ((tp->t_proto = dcs->d_proto) != 0)
1325 tp->t_vararg = dcs->d_vararg;
1331 * Called for new style function declarations.
1343 * Declarations of structs/unions/enums in param lists are legal,
1346 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1348 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) {
1349 /* dubious tag declaration: %s %s */
1350 warning(85, scltoa(sc), sym->s_name);
1355 for (arg = args; arg != NULL; arg = arg->s_nxt) {
1356 if (arg->s_type->t_tspec == VOID) {
1357 if (n > 1 || arg->s_nxt != NULL) {
1358 /* "void" must be sole parameter */
1360 arg->s_type = gettyp(INT);
1366 /* return NULL if first param is VOID */
1367 return (args != NULL && args->s_type->t_tspec != VOID ? args : NULL);
1371 * Called for old style function declarations.
1378 * Remember list of params only if this is really seams to be
1379 * a function definition.
1381 if (dcs->d_nxt->d_ctx == EXTERN &&
1382 decl->s_type == dcs->d_nxt->d_type) {
1384 * We assume that this becomes a function definition. If
1385 * we are wrong, its corrected in chkfdef().
1389 decl->s_args = args;
1393 /* function prototype parameters must have types */
1399 * Lists of Identifiers in functions declarations are allowed only if
1400 * its also a function definition. If this is not the case, print a
1410 /* incomplete or misplaced function definition */
1419 * Process the name in a declarator.
1420 * If the symbol does already exists, a new one is created.
1421 * The symbol becomes one of the storage classes EXTERN, STATIC, AUTO or
1423 * s_def and s_reg are valid after dname().
1431 if (sym->s_scl == NOSCL) {
1432 dcs->d_rdcsym = NULL;
1433 } else if (sym->s_defarg) {
1435 dcs->d_rdcsym = NULL;
1437 dcs->d_rdcsym = sym;
1438 sym = pushdown(sym);
1441 switch (dcs->d_ctx) {
1445 sym->s_styp = dcs->d_tagtyp->t_str;
1447 sym->s_value.v_tspec = INT;
1452 * static and external symbols without "extern" are
1453 * considered to be tentative defined, external
1454 * symbols with "extern" are declared, and typedef names
1455 * are defined. Tentative defined and declared symbols
1456 * may become defined if an initializer is present or
1457 * this is a function definition.
1459 if ((sc = dcs->d_scl) == NOSCL) {
1462 } else if (sc == STATIC) {
1464 } else if (sc == TYPEDEF) {
1466 } else if (sc == EXTERN) {
1469 lerror("dname() 1");
1476 if ((sc = dcs->d_scl) == NOSCL) {
1478 } else if (sc == REG) {
1482 lerror("dname() 2");
1487 if ((sc = dcs->d_scl) == NOSCL) {
1489 * XXX somewhat ugly because we dont know whether
1490 * this is AUTO or EXTERN (functions). If we are
1491 * wrong it must be corrected in decl1loc(), where
1492 * we have the neccessary type information.
1496 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1498 } else if (sc == REG) {
1502 } else if (sc == EXTERN) {
1505 lerror("dname() 3");
1509 lerror("dname() 4");
1513 sym->s_type = dcs->d_type;
1515 dcs->d_fpsyms = NULL;
1521 * Process a name in the list of formal params in an old style function
1528 if (sym->s_scl != NOSCL) {
1529 if (blklev == sym->s_blklev) {
1530 /* redeclaration of formal parameter %s */
1531 error(21, sym->s_name);
1535 sym = pushdown(sym);
1537 sym->s_type = gettyp(INT);
1540 sym->s_defarg = sym->s_arg = 1;
1545 * Create the type of a tag.
1547 * tag points to the symbol table entry of the tag
1548 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1549 * decl is 1 if the type of the tag will be completed in this declaration
1550 * (the following token is T_LBRACE)
1551 * semi is 1 if the following token is T_SEMI
1554 mktag(tag, kind, decl, semi)
1562 if (kind == STRUCT) {
1564 } else if (kind == UNION) {
1566 } else if (kind == ENUM) {
1573 if (tag->s_scl != NOSCL) {
1574 tag = newtag(tag, scl, decl, semi);
1576 /* a new tag, no empty declaration */
1577 dcs->d_nxt->d_nedecl = 1;
1578 if (scl == ENUMTAG && !decl) {
1579 if (!tflag && (sflag || pflag))
1580 /* forward reference to enum type */
1584 if (tag->s_scl == NOSCL) {
1586 tag->s_type = tp = getblk(sizeof (type_t));
1591 tag = getblk(sizeof (sym_t));
1592 tag->s_name = unnamed;
1593 STRUCT_ASSIGN(tag->s_dpos, curr_pos);
1597 tag->s_type = tp = getblk(sizeof (type_t));
1598 dcs->d_nxt->d_nedecl = 1;
1601 if (tp->t_tspec == NOTSPEC) {
1604 tp->t_str = getblk(sizeof (str_t));
1605 tp->t_str->align = CHAR_BIT;
1606 tp->t_str->stag = tag;
1609 tp->t_enum = getblk(sizeof (enum_t));
1610 tp->t_enum->etag = tag;
1612 /* ist unvollstaendiger Typ */
1620 * Checks all possible cases of tag redeclarations.
1621 * decl is 1 if T_LBRACE follows
1622 * semi is 1 if T_SEMI follows
1625 newtag(tag, scl, decl, semi)
1630 if (tag->s_blklev < blklev) {
1635 /* decl. introduces new type ... */
1636 warning(44, scltoa(scl), tag->s_name);
1637 tag = pushdown(tag);
1638 } else if (tag->s_scl != scl) {
1639 /* base type is really "%s %s" */
1640 warning(45, scltoa(tag->s_scl), tag->s_name);
1642 dcs->d_nxt->d_nedecl = 1;
1644 /* "struct a { ..." */
1646 /* redefinition hides earlier one: %s */
1647 warning(43, tag->s_name);
1648 tag = pushdown(tag);
1649 dcs->d_nxt->d_nedecl = 1;
1650 } else if (tag->s_scl != scl) {
1651 /* base type is really "%s %s" */
1652 warning(45, scltoa(tag->s_scl), tag->s_name);
1653 /* declaration introduces new type in ANSI C: %s %s */
1655 warning(44, scltoa(scl), tag->s_name);
1656 tag = pushdown(tag);
1657 dcs->d_nxt->d_nedecl = 1;
1660 if (tag->s_scl != scl) {
1661 /* (%s) tag redeclared */
1662 error(46, scltoa(tag->s_scl));
1664 tag = pushdown(tag);
1665 dcs->d_nxt->d_nedecl = 1;
1666 } else if (decl && !incompl(tag->s_type)) {
1667 /* (%s) tag redeclared */
1668 error(46, scltoa(tag->s_scl));
1670 tag = pushdown(tag);
1671 dcs->d_nxt->d_nedecl = 1;
1672 } else if (semi || decl) {
1673 dcs->d_nxt->d_nedecl = 1;
1686 case EXTERN: s = "extern"; break;
1687 case STATIC: s = "static"; break;
1688 case AUTO: s = "auto"; break;
1689 case REG: s = "register"; break;
1690 case TYPEDEF: s = "typedef"; break;
1691 case STRTAG: s = "struct"; break;
1692 case UNIONTAG: s = "union"; break;
1693 case ENUMTAG: s = "enum"; break;
1694 default: lerror("tagttoa()");
1700 * Completes the type of a tag in a struct/union/enum declaration.
1701 * tp points to the type of the, tag, fmem to the list of members/enums.
1713 /* from now a complete type */
1716 if ((t = tp->t_tspec) != ENUM) {
1717 align(dcs->d_stralign, 0);
1719 sp->align = dcs->d_stralign;
1720 sp->size = dcs->d_offset;
1722 if (sp->size == 0) {
1724 (void)gnuism(47, ttab[t].tt_name);
1727 for (mem = fmem; mem != NULL; mem = mem->s_nxt) {
1728 if (mem->s_name != unnamed)
1732 /* %s has no named members */
1734 t == STRUCT ? "structure" : "union");
1738 tp->t_enum->elem = fmem;
1744 * Processes the name of an enumerator in en enum declaration.
1746 * sym points to the enumerator
1747 * val is the value of the enumerator
1748 * impl is 1 if the the value of the enumerator was not explicit specified.
1751 ename(sym, val, impl)
1756 if (sym->s_blklev == blklev) {
1757 /* no hflag, because this is illegal!!! */
1759 /* enumeration constant hides parameter: %s */
1760 warning(57, sym->s_name);
1762 /* redeclaration of %s */
1763 error(27, sym->s_name);
1765 * inside blocks it should not too complicated
1766 * to find the position of the previous
1774 /* redefinition hides earlier one: %s */
1775 warning(43, sym->s_name);
1777 sym = pushdown(sym);
1779 sym->s_scl = ENUMCON;
1780 sym->s_type = dcs->d_tagtyp;
1781 sym->s_value.v_tspec = INT;
1782 sym->s_value.v_quad = val;
1783 if (impl && val - 1 == INT_MAX) {
1784 /* overflow in enumeration values: %s */
1785 warning(48, sym->s_name);
1792 * Process a single external declarator.
1795 decl1ext(dsym, initflg)
1799 int warn, rval, redec;
1806 if (initflg && !(initerr = chkinit(dsym)))
1810 * Declarations of functions are marked as "tentative" in dname().
1811 * This is wrong because there are no tentative function
1814 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1817 if (dcs->d_inline) {
1818 if (dsym->s_type->t_tspec == FUNC) {
1821 /* variable declared inline: %s */
1822 warning(268, dsym->s_name);
1826 /* Write the declaration into the output file */
1827 if (plibflg && llibflg &&
1828 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1830 * With both LINTLIBRARY and PROTOLIB the prototyp is
1831 * written as a function definition to the output file.
1833 rval = dsym->s_type->t_subt->t_tspec != VOID;
1834 outfdef(dsym, &dsym->s_dpos, rval, 0, NULL);
1836 outsym(dsym, dsym->s_scl, dsym->s_def);
1839 if ((rdsym = dcs->d_rdcsym) != NULL) {
1842 * If the old symbol stems from a old style function definition
1843 * we have remembered the params in rdsmy->s_args and compare
1844 * them with the params of the prototype.
1846 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1847 redec = chkosdef(rdsym, dsym);
1852 if (!redec && !isredec(dsym, (warn = 0, &warn))) {
1855 /* redeclaration of %s */
1856 (*(sflag ? error : warning))(27, dsym->s_name);
1857 prevdecl(-1, rdsym);
1861 * Overtake the rememberd params if the new symbol
1862 * is not a prototype.
1864 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1865 dsym->s_osdef = rdsym->s_osdef;
1866 dsym->s_args = rdsym->s_args;
1867 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1871 * Remember the position of the declaration if the
1872 * old symbol was a prototype and the new is not.
1873 * Also remember the position if the old symbol
1874 * was defined and the new is not.
1876 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1877 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1878 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1879 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1883 * Copy informations about usage of the name into
1886 cpuinfo(dsym, rdsym);
1888 /* Once a name is defined, it remains defined. */
1889 if (rdsym->s_def == DEF)
1892 /* once a function is inline, it remains inline */
1893 if (rdsym->s_inline)
1896 compltyp(dsym, rdsym);
1903 if (dsym->s_scl == TYPEDEF) {
1904 dsym->s_type = duptyp(dsym->s_type);
1905 dsym->s_type->t_typedef = 1;
1906 settdsym(dsym->s_type, dsym);
1912 * Copies informations about usage into a new symbol table entry of
1919 sym->s_spos = rdsym->s_spos;
1920 sym->s_upos = rdsym->s_upos;
1921 sym->s_set = rdsym->s_set;
1922 sym->s_used = rdsym->s_used;
1926 * Prints an error and returns 1 if a symbol is redeclared/redefined.
1927 * Otherwise returns 0 and, in some cases of minor problems, prints
1937 if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
1938 /* redeclaration of %s */
1939 error(27, dsym->s_name);
1943 if (rsym->s_scl == TYPEDEF) {
1944 /* typedef redeclared: %s */
1945 error(89, dsym->s_name);
1949 if (dsym->s_scl == TYPEDEF) {
1950 /* redeclaration of %s */
1951 error(27, dsym->s_name);
1955 if (rsym->s_def == DEF && dsym->s_def == DEF) {
1956 /* redefinition of %s */
1957 error(28, dsym->s_name);
1961 if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, warn)) {
1962 /* redeclaration of %s */
1963 error(27, dsym->s_name);
1967 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
1969 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
1971 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
1973 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
1975 * All cases except "int a = 1; static int a;" are catched
1976 * above with or without a warning
1978 /* redeclaration of %s */
1979 error(27, dsym->s_name);
1983 if (rsym->s_scl == EXTERN) {
1984 /* previously declared extern, becomes static: %s */
1985 warning(29, dsym->s_name);
1991 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
1993 /* redeclaration of %s; ANSI C requires "static" */
1995 warning(30, dsym->s_name);
1998 dsym->s_scl = STATIC;
2003 * Checks if two types are compatible. Returns 0 if not, otherwise 1.
2005 * ignqual ignore qualifiers of type; used for function params
2006 * promot promote left type; used for comparision of params of
2007 * old style function definitions with params of prototypes.
2008 * *warn set to 1 if an old style function declaration is not
2009 * compatible with a prototype
2012 eqtype(tp1, tp2, ignqual, promot, warn)
2014 int ignqual, promot, *warn;
2018 while (tp1 != NULL && tp2 != NULL) {
2024 } else if (t == CHAR || t == SCHAR) {
2026 } else if (t == UCHAR) {
2027 t = tflag ? UINT : INT;
2028 } else if (t == SHORT) {
2030 } else if (t == USHORT) {
2032 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
2036 if (t != tp2->t_tspec)
2039 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
2042 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
2045 if (t == STRUCT || t == UNION)
2046 return (tp1->t_str == tp2->t_str);
2048 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2049 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2053 /* dont check prototypes for traditional */
2054 if (t == FUNC && !tflag) {
2055 if (tp1->t_proto && tp2->t_proto) {
2056 if (!eqargs(tp1, tp2, warn))
2058 } else if (tp1->t_proto) {
2059 if (!mnoarg(tp1, warn))
2061 } else if (tp2->t_proto) {
2062 if (!mnoarg(tp2, warn))
2069 ignqual = promot = 0;
2073 return (tp1 == tp2);
2077 * Compares the parameter types of two prototypes.
2080 eqargs(tp1, tp2, warn)
2086 if (tp1->t_vararg != tp2->t_vararg)
2092 while (a1 != NULL && a2 != NULL) {
2094 if (eqtype(a1->s_type, a2->s_type, 1, 0, warn) == 0)
2106 * mnoarg() (matches functions with no argument type information)
2107 * returns 1 if all parameters of a prototype are compatible with
2108 * and old style function declaration.
2109 * This is the case if following conditions are met:
2110 * 1. the prototype must have a fixed number of parameters
2111 * 2. no parameter is of type float
2112 * 3. no parameter is converted to another type if integer promotion
2127 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) {
2128 if ((t = arg->s_type->t_tspec) == FLOAT ||
2129 t == CHAR || t == SCHAR || t == UCHAR ||
2130 t == SHORT || t == USHORT) {
2139 * Compares a prototype declaration with the remembered arguments of
2140 * a previous old style function definition.
2143 chkosdef(rdsym, dsym)
2144 sym_t *rdsym, *dsym;
2146 sym_t *args, *pargs, *arg, *parg;
2150 args = rdsym->s_args;
2151 pargs = dsym->s_type->t_args;
2156 for (arg = args; arg != NULL; arg = arg->s_nxt)
2158 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2160 if (narg != nparg) {
2161 /* prototype does not match old-style definition */
2173 * If it does not match due to promotion and sflag is
2174 * not set we print only a warning.
2176 if (!eqtype(arg->s_type, parg->s_type, 1, 1, &warn) || warn) {
2177 /* prototype does not match old-style def., arg #%d */
2188 /* old style definition */
2189 prevdecl(300, rdsym);
2195 * Complets a type by copying the dimension and prototype information
2196 * from a second compatible type.
2198 * Following lines are legal:
2199 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2200 * "typedef ft(); ft f; f(int); ft g; g(long);"
2201 * This means that, if a type is completed, the type structure must
2205 compltyp(dsym, ssym)
2208 type_t **dstp, *src;
2211 dstp = &dsym->s_type;
2214 while ((dst = *dstp) != NULL) {
2215 if (src == NULL || dst->t_tspec != src->t_tspec)
2216 lerror("compltyp() 1");
2217 if (dst->t_tspec == ARRAY) {
2218 if (dst->t_dim == 0 && src->t_dim != 0) {
2219 *dstp = dst = duptyp(dst);
2220 dst->t_dim = src->t_dim;
2221 /* now a complete Typ */
2224 } else if (dst->t_tspec == FUNC) {
2225 if (!dst->t_proto && src->t_proto) {
2226 *dstp = dst = duptyp(dst);
2228 dst->t_args = src->t_args;
2231 dstp = &dst->t_subt;
2237 * Completes the declaration of a single argument.
2240 decl1arg(sym, initflg)
2250 if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) {
2251 /* redeclaration of formal parameter %s */
2252 error(237, sym->s_name);
2253 rmsym(dcs->d_rdcsym);
2258 /* declared argument %s is missing */
2259 error(53, sym->s_name);
2264 /* cannot initialize parameter: %s */
2265 error(52, sym->s_name);
2269 if ((t = sym->s_type->t_tspec) == ARRAY) {
2270 sym->s_type = incref(sym->s_type->t_subt, PTR);
2271 } else if (t == FUNC) {
2273 /* a function is declared as an argument: %s */
2274 warning(50, sym->s_name);
2275 sym->s_type = incref(sym->s_type, PTR);
2276 } else if (t == FLOAT) {
2278 sym->s_type = gettyp(DOUBLE);
2282 /* argument declared inline: %s */
2283 warning(269, sym->s_name);
2286 * Arguments must have complete types. lengths() prints the needed
2287 * error messages (null dimension is impossible because arrays are
2288 * converted to pointers).
2290 if (sym->s_type->t_tspec != VOID)
2291 (void)length(sym->s_type, sym->s_name);
2299 * Does some checks for lint directives which apply to functions.
2300 * Processes arguments in old style function definitions which default
2302 * Checks compatiblility of old style function definition with previous
2308 sym_t *args, *arg, *pargs, *parg;
2309 int narg, nparg, n, msg;
2312 args = funcsym->s_args;
2313 pargs = funcsym->s_type->t_args;
2315 /* check for illegal combinations of lint directives */
2316 if (prflstrg != -1 && scflstrg != -1) {
2317 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2319 prflstrg = scflstrg = -1;
2321 if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) {
2322 /* dubious use of ** VARARGS ** with ** %s ** */
2323 warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2328 * check if the argument of a lint directive is compatible with the
2329 * number of arguments.
2332 for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_nxt)
2334 if (nargusg > narg) {
2335 /* argument number mismatch with directive: ** %s ** */
2336 warning(283, "ARGSUSED");
2339 if (nvararg > narg) {
2340 /* argument number mismatch with directive: ** %s ** */
2341 warning(283, "VARARGS");
2344 if (prflstrg > narg) {
2345 /* argument number mismatch with directive: ** %s ** */
2346 warning(283, "PRINTFLIKE");
2348 } else if (prflstrg == 0) {
2351 if (scflstrg > narg) {
2352 /* argument number mismatch with directive: ** %s ** */
2353 warning(283, "SCANFLIKE");
2355 } else if (scflstrg == 0) {
2358 if (prflstrg != -1 || scflstrg != -1) {
2359 narg = prflstrg != -1 ? prflstrg : scflstrg;
2361 for (n = 1; n < narg; n++)
2363 if (arg->s_type->t_tspec != PTR ||
2364 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2365 t != UCHAR && t != SCHAR)) {
2366 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2368 prflstrg = scflstrg = -1;
2373 * print a warning for each argument off an old style function
2374 * definition which defaults to int
2376 for (arg = args; arg != NULL; arg = arg->s_nxt) {
2377 if (arg->s_defarg) {
2378 /* argument type defaults to int: %s */
2379 warning(32, arg->s_name);
2386 * If this is an old style function definition and a prototyp
2387 * exists, compare the types of arguments.
2389 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2391 * If the number of arguments does not macht, we need not
2396 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2398 for (arg = args; arg != NULL; arg = arg->s_nxt)
2400 if (narg != nparg) {
2401 /* parameter mismatch: %d declared, %d defined */
2402 error(51, nparg, narg);
2408 msg |= chkptdecl(arg, parg);
2414 /* prototype declaration */
2415 prevdecl(285, dcs->d_rdcsym);
2417 /* from now the prototype is valid */
2418 funcsym->s_osdef = 0;
2419 funcsym->s_args = NULL;
2426 * Checks compatibility of an old style function definition with a previous
2427 * prototype declaration.
2428 * Returns 1 if the position of the previous declaration should be reported.
2431 chkptdecl(arg, parg)
2443 if (!eqtype(tp, ptp, 1, 1, &warn)) {
2444 if (eqtype(tp, ptp, 1, 0, &warn)) {
2445 /* type does not match prototype: %s */
2446 msg = gnuism(58, arg->s_name);
2448 /* type does not match prototype: %s */
2449 error(58, arg->s_name);
2453 /* type does not match prototype: %s */
2454 (*(sflag ? error : warning))(58, arg->s_name);
2462 * Completes a single local declaration/definition.
2465 decl1loc(dsym, initflg)
2469 /* Correct a mistake done in dname(). */
2470 if (dsym->s_type->t_tspec == FUNC) {
2472 if (dcs->d_scl == NOSCL)
2473 dsym->s_scl = EXTERN;
2476 if (dsym->s_type->t_tspec == FUNC) {
2477 if (dsym->s_scl == STATIC) {
2478 /* dubious static function at block level: %s */
2479 warning(93, dsym->s_name);
2480 dsym->s_scl = EXTERN;
2481 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2482 /* function has illegal storage class: %s */
2483 error(94, dsym->s_name);
2484 dsym->s_scl = EXTERN;
2489 * functions may be declared inline at local scope, although
2490 * this has no effect for a later definition of the same
2492 * XXX it should have an effect if tflag is set. this would
2493 * also be the way gcc behaves.
2495 if (dcs->d_inline) {
2496 if (dsym->s_type->t_tspec == FUNC) {
2499 /* variable declared inline: %s */
2500 warning(268, dsym->s_name);
2508 if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN)
2511 if (dsym->s_scl == EXTERN) {
2513 * XXX wenn die statische Variable auf Ebene 0 erst
2514 * spaeter definiert wird, haben wir die Brille auf.
2516 if (dsym->s_xsym == NULL) {
2517 outsym(dsym, EXTERN, dsym->s_def);
2519 outsym(dsym, dsym->s_xsym->s_scl, dsym->s_def);
2523 if (dcs->d_rdcsym != NULL) {
2525 if (dcs->d_rdcsym->s_blklev == 0) {
2527 switch (dsym->s_scl) {
2529 /* automatic hides external declaration: %s */
2531 warning(86, dsym->s_name);
2534 /* static hides external declaration: %s */
2536 warning(87, dsym->s_name);
2539 /* typedef hides external declaration: %s */
2541 warning(88, dsym->s_name);
2545 * Warnings and errors are printed in ledecl()
2549 lerror("decl1loc() 1");
2552 } else if (dcs->d_rdcsym->s_blklev == blklev) {
2554 /* no hflag, because its illegal! */
2555 if (dcs->d_rdcsym->s_arg) {
2557 * if !tflag, a "redeclaration of %s" error
2562 /* decl. hides parameter: %s */
2563 warning(91, dsym->s_name);
2564 rmsym(dcs->d_rdcsym);
2568 } else if (dcs->d_rdcsym->s_blklev < blklev) {
2571 /* declaration hides earlier one: %s */
2572 warning(95, dsym->s_name);
2576 if (dcs->d_rdcsym->s_blklev == blklev) {
2578 /* redeclaration of %s */
2579 error(27, dsym->s_name);
2580 rmsym(dcs->d_rdcsym);
2586 if (initflg && !(initerr = chkinit(dsym))) {
2591 if (dsym->s_scl == TYPEDEF) {
2592 dsym->s_type = duptyp(dsym->s_type);
2593 dsym->s_type->t_typedef = 1;
2594 settdsym(dsym->s_type, dsym);
2598 * Before we can check the size we must wait for a initialisation
2604 * Processes (re)declarations of external Symbols inside blocks.
2613 /* look for a symbol with the same name */
2614 esym = dcs->d_rdcsym;
2615 while (esym != NULL && esym->s_blklev != 0) {
2616 while ((esym = esym->s_link) != NULL) {
2617 if (esym->s_kind != FVFT)
2619 if (strcmp(dsym->s_name, esym->s_name) == 0)
2625 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2626 /* gcc accepts this without a warning, pcc prints an error. */
2627 /* redeclaration of %s */
2628 warning(27, dsym->s_name);
2634 eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &warn);
2637 if (esym->s_scl == EXTERN) {
2638 /* inconsistent redeclaration of extern: %s */
2639 warning(90, dsym->s_name);
2642 /* inconsistent redeclaration of static: %s */
2643 warning(92, dsym->s_name);
2650 * Remember the external symbol so we can update usage
2651 * information at the end of the block.
2653 dsym->s_xsym = esym;
2658 * Print an error or a warning if the symbol cant be initialized due
2659 * to type/storage class. Returnvalue is 1 if an error has been
2670 if (sym->s_type->t_tspec == FUNC) {
2671 /* cannot initialize function: %s */
2672 error(24, sym->s_name);
2674 } else if (sym->s_scl == TYPEDEF) {
2675 /* cannot initialize typedef: %s */
2676 error(25, sym->s_name);
2678 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2679 /* cannot initialize "extern" declaration: %s */
2680 if (dcs->d_ctx == EXTERN) {
2681 warning(26, sym->s_name);
2683 error(26, sym->s_name);
2692 * Create a symbole for an abstract declaration.
2699 if (dcs->d_ctx != ABSTRACT && dcs->d_ctx != PARG)
2702 sym = getblk(sizeof (sym_t));
2704 sym->s_name = unnamed;
2706 sym->s_scl = ABSTRACT;
2709 if (dcs->d_ctx == PARG)
2712 sym->s_type = dcs->d_type;
2713 dcs->d_rdcsym = NULL;
2720 * Removes anything which has nothing to do on global level.
2725 while (dcs->d_nxt != NULL)
2733 * remove all informations about pending lint directives without
2740 * Process an abstract type declaration
2752 * Checks size after declarations of variables and their initialisation.
2759 * check size only for symbols which are defined and no function and
2762 if (dsym->s_def != DEF)
2764 if (dsym->s_scl == TYPEDEF)
2766 if (dsym->s_type->t_tspec == FUNC)
2769 if (length(dsym->s_type, dsym->s_name) == 0 &&
2770 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2771 /* empty array declaration: %s */
2773 warning(190, dsym->s_name);
2775 error(190, dsym->s_name);
2781 * Mark an object as set if it is not already
2789 STRUCT_ASSIGN(sym->s_spos, curr_pos);
2794 * Mark an object as used if it is not already
2797 setuflg(sym, fcall, szof)
2803 STRUCT_ASSIGN(sym->s_upos, curr_pos);
2806 * for function calls another record is written
2808 * XXX Should symbols used in sizeof() treated as used or not?
2809 * Probably not, because there is no sense to declare an
2810 * external variable only to get their size.
2812 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2817 * Prints warnings for a list of variables and labels (concatenated
2818 * with s_dlnxt) if these are not used or only set.
2827 /* for this warnings LINTED has no effect */
2831 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
2832 chkusg1(di->d_asm, sym);
2838 * Prints a warning for a single variable or label if it is not used or
2848 if (sym->s_blklev == -1)
2851 STRUCT_ASSIGN(cpos, curr_pos);
2853 if (sym->s_kind == FVFT) {
2855 chkausg(novar, sym);
2857 chkvusg(novar, sym);
2859 } else if (sym->s_kind == FLAB) {
2861 } else if (sym->s_kind == FTAG) {
2865 STRUCT_ASSIGN(curr_pos, cpos);
2874 lerror("chkausg() 1");
2879 if (!arg->s_used && vflag) {
2880 STRUCT_ASSIGN(curr_pos, arg->s_dpos);
2881 /* argument %s unused in function %s */
2882 warning(231, arg->s_name, funcsym->s_name);
2894 if (blklev == 0 || sym->s_blklev == 0)
2895 lerror("chkvusg() 1");
2897 /* errors in expressions easily cause lots of these warnings */
2902 * XXX Only variables are checkd, although types should
2903 * probably also be checked
2905 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
2906 sc != AUTO && sc != REG) {
2914 if (!sym->s_used && !sym->s_set) {
2915 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2916 /* %s unused in function %s */
2917 warning(192, sym->s_name, funcsym->s_name);
2920 if (sym->s_set && !sym->s_used) {
2921 STRUCT_ASSIGN(curr_pos, sym->s_spos);
2922 /* %s set but not used in function %s */
2923 warning(191, sym->s_name, funcsym->s_name);
2924 } else if (!sym->s_used) {
2925 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2926 /* %s unused in function %s */
2927 warning(192, sym->s_name, funcsym->s_name);
2933 * information about usage is taken over into the symbol
2934 * tabel entry at level 0 if the symbol was locally declared
2935 * as an external symbol.
2937 * XXX This is wrong for symbols declared static at level 0
2938 * if the usage information stems from sizeof(). This is
2939 * because symbols at level 0 only used in sizeof() are
2940 * considered to not be used.
2942 if ((xsym = sym->s_xsym) != NULL) {
2943 if (sym->s_used && !xsym->s_used) {
2945 STRUCT_ASSIGN(xsym->s_upos, sym->s_upos);
2947 if (sym->s_set && !xsym->s_set) {
2949 STRUCT_ASSIGN(xsym->s_spos, sym->s_spos);
2959 if (blklev != 1 || lab->s_blklev != 1)
2960 lerror("chklusg() 1");
2962 if (lab->s_set && !lab->s_used) {
2963 STRUCT_ASSIGN(curr_pos, lab->s_spos);
2964 /* label %s unused in function %s */
2965 warning(192, lab->s_name, funcsym->s_name);
2966 } else if (!lab->s_set) {
2967 STRUCT_ASSIGN(curr_pos, lab->s_upos);
2968 /* undefined label %s */
2969 warning(23, lab->s_name);
2977 if (!incompl(sym->s_type))
2980 /* complain alwasy about incomplet tags declared inside blocks */
2981 if (!zflag || dcs->d_ctx != EXTERN)
2984 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2985 switch (sym->s_type->t_tspec) {
2987 /* struct %s never defined */
2988 warning(233, sym->s_name);
2991 /* union %s never defined */
2992 warning(234, sym->s_name);
2995 /* enum %s never defined */
2996 warning(235, sym->s_name);
2999 lerror("chktusg() 1");
3004 * Called after the entire translation unit has been parsed.
3005 * Changes tentative definitions in definitions.
3006 * Performs some tests on global Symbols. Detected Problems are:
3007 * - defined variables of incomplete type
3008 * - constant variables which are not initialized
3009 * - static symbols which are never used
3017 if (blklev != 0 || dcs->d_nxt != NULL)
3020 STRUCT_ASSIGN(cpos, curr_pos);
3022 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
3023 if (sym->s_blklev == -1)
3025 if (sym->s_kind == FVFT) {
3027 } else if (sym->s_kind == FTAG) {
3030 if (sym->s_kind != FMOS)
3031 lerror("chkglsyms() 1");
3035 STRUCT_ASSIGN(curr_pos, cpos);
3042 if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON)
3045 if (sym->s_scl != EXTERN && sym->s_scl != STATIC)
3046 lerror("chkglvar() 1");
3050 if (sym->s_scl == STATIC) {
3051 if (sym->s_type->t_tspec == FUNC) {
3052 if (sym->s_used && sym->s_def != DEF) {
3053 STRUCT_ASSIGN(curr_pos, sym->s_upos);
3054 /* static func. called but not def.. */
3055 error(225, sym->s_name);
3059 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3060 if (sym->s_type->t_tspec == FUNC) {
3061 if (sym->s_def == DEF) {
3063 /* static function %s unused */
3064 warning(236, sym->s_name);
3066 /* static function %s decl. but ... */
3067 warning(290, sym->s_name);
3069 } else if (!sym->s_set) {
3070 /* static variable %s unused */
3071 warning(226, sym->s_name);
3073 /* static variable %s set but not used */
3074 warning(307, sym->s_name);
3077 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
3078 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3079 /* const object %s should have initializer */
3080 warning(227, sym->s_name);
3089 if (sym->s_def == TDEF) {
3090 if (sym->s_type->t_tspec == FUNC)
3092 * this can happen if an syntax error occured
3093 * after a function declaration
3096 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3097 if (length(sym->s_type, sym->s_name) == 0 &&
3098 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3099 /* empty array declaration: %s */
3100 if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3101 warning(190, sym->s_name);
3103 error(190, sym->s_name);
3110 * Prints information about location of previous definition/declaration.
3122 STRUCT_ASSIGN(cpos, curr_pos);
3123 STRUCT_ASSIGN(curr_pos, psym->s_dpos);
3125 message(msg, psym->s_name);
3126 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3127 /* previous definition of %s */
3128 message(261, psym->s_name);
3130 /* previous declaration of %s */
3131 message(260, psym->s_name);
3133 STRUCT_ASSIGN(curr_pos, cpos);