1 /* $NetBSD: decl.c,v 1.33 2004/06/20 22:20:16 jmc Exp $ */
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Jochen Pohl for
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 #if defined(__RCSID) && !defined(lint)
37 __RCSID("$NetBSD: decl.c,v 1.33 2004/06/20 22:20:16 jmc Exp $");
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
48 const char *unnamed = "<unnamed>";
50 /* shared type structures for arithmtic types and void */
51 static type_t *typetab;
53 /* value of next enumerator during declaration of enum types */
57 * pointer to top element of a stack which contains informations local
58 * to nested declarations
62 static type_t *tdeferr(type_t *, tspec_t);
63 static void settdsym(type_t *, sym_t *);
64 static tspec_t mrgtspec(tspec_t, tspec_t);
65 static void align(int, int);
66 static sym_t *newtag(sym_t *, scl_t, int, int);
67 static int eqargs(type_t *, type_t *, int *);
68 static int mnoarg(type_t *, int *);
69 static int chkosdef(sym_t *, sym_t *);
70 static int chkptdecl(sym_t *, sym_t *);
71 static sym_t *nsfunc(sym_t *, sym_t *);
72 static void osfunc(sym_t *, sym_t *);
73 static void ledecl(sym_t *);
74 static int chkinit(sym_t *);
75 static void chkausg(int, sym_t *);
76 static void chkvusg(int, sym_t *);
77 static void chklusg(sym_t *);
78 static void chktusg(sym_t *);
79 static void chkglvar(sym_t *);
80 static void glchksz(sym_t *);
83 * initializes all global vars used in declarations
90 /* declaration stack */
91 if ((dcs = calloc(1, sizeof (dinfo_t))) == NULL)
94 dcs->d_ldlsym = &dcs->d_dlsyms;
96 /* type information and classification */
99 /* shared type structures */
100 if ((typetab = calloc(NTSPEC, sizeof (type_t))) == NULL)
102 for (i = 0; i < NTSPEC; i++)
103 typetab[i].t_tspec = NOTSPEC;
104 typetab[CHAR].t_tspec = CHAR;
105 typetab[SCHAR].t_tspec = SCHAR;
106 typetab[UCHAR].t_tspec = UCHAR;
107 typetab[SHORT].t_tspec = SHORT;
108 typetab[USHORT].t_tspec = USHORT;
109 typetab[INT].t_tspec = INT;
110 typetab[UINT].t_tspec = UINT;
111 typetab[LONG].t_tspec = LONG;
112 typetab[ULONG].t_tspec = ULONG;
113 typetab[QUAD].t_tspec = QUAD;
114 typetab[UQUAD].t_tspec = UQUAD;
115 typetab[FLOAT].t_tspec = FLOAT;
116 typetab[DOUBLE].t_tspec = DOUBLE;
117 typetab[LDOUBLE].t_tspec = LDOUBLE;
118 typetab[VOID].t_tspec = VOID;
120 * Next two are not real types. They are only used by the parser
121 * to return keywords "signed" and "unsigned"
123 typetab[SIGNED].t_tspec = SIGNED;
124 typetab[UNSIGN].t_tspec = UNSIGN;
128 * Returns a shared type structure vor arithmetic types and void.
130 * It's important to duplicate this structure (using duptyp() or tdupdyp())
131 * if it is to be modified (adding qualifiers or anything else).
137 return (&typetab[t]);
141 duptyp(const type_t *tp)
145 ntp = getblk(sizeof (type_t));
146 STRUCT_ASSIGN(*ntp, *tp);
151 * Use tduptyp() instead of duptyp() inside expressions (if the
152 * allocated memory should be freed after the expr).
155 tduptyp(const type_t *tp)
159 ntp = tgetblk(sizeof (type_t));
160 STRUCT_ASSIGN(*ntp, *tp);
165 * Returns 1 if the argument is void or an incomplete array,
166 * struct, union or enum type.
173 if ((t = tp->t_tspec) == VOID) {
175 } else if (t == ARRAY) {
176 return (tp->t_aincompl);
177 } else if (t == STRUCT || t == UNION) {
178 return (tp->t_str->sincompl);
179 } else if (t == ENUM) {
180 return (tp->t_enum->eincompl);
186 * Set the flag for (in)complete array, struct, union or enum
190 setcompl(type_t *tp, int ic)
194 if ((t = tp->t_tspec) == ARRAY) {
196 } else if (t == STRUCT || t == UNION) {
197 tp->t_str->sincompl = ic;
200 LERROR("setcompl()");
201 tp->t_enum->eincompl = ic;
206 * Remember the storage class of the current declaration in dcs->d_scl
207 * (the top element of the declaration stack) and detect multiple
217 warning(10, "inline");
221 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
222 dcs->d_smod != NOTSPEC || dcs->d_lmod != NOTSPEC) {
223 /* storage class after type is obsolescent */
226 if (dcs->d_scl == NOSCL) {
230 * multiple storage classes. An error will be reported in
238 * Remember the type, modifier or typedef name returned by the parser
239 * in *dcs (top element of decl stack). This information is used in
240 * deftyp() to build the type used for all declarators in this
243 * Is tp->t_typedef 1, the type comes from a previously defined typename.
244 * Otherwise it comes from a type specifier (int, long, ...) or a
245 * struct/union/enum tag.
253 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
254 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) {
256 * something like "typedef int a; int a b;"
257 * This should not happen with current grammar.
267 if (t == STRUCT || t == UNION || t == ENUM) {
269 * something like "int struct a ..."
270 * struct/union/enum with anything else is not allowed
272 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
273 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) {
275 * remember that an error must be reported in
279 dcs->d_atyp = dcs->d_lmod = dcs->d_smod = NOTSPEC;
285 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
287 * something like "struct a int"
288 * struct/union/enum with anything else is not allowed
294 if (t == LONG && dcs->d_lmod == LONG) {
295 /* "long long" or "long ... long" */
297 dcs->d_lmod = NOTSPEC;
299 /* %s C does not support 'long long' */
300 (void)c99ism(265, tflag ? "traditional" : "c89");
303 if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
304 /* something like "typedef int a; a long ..." */
305 dcs->d_type = tdeferr(dcs->d_type, t);
309 /* now it can be only a combination of arithmetic types and void */
310 if (t == SIGNED || t == UNSIGN) {
311 /* remember specifiers "signed" and "unsigned" in dcs->d_smod */
312 if (dcs->d_smod != NOTSPEC)
314 * more than one "signed" and/or "unsigned"; print
315 * an error in deftyp()
319 } else if (t == SHORT || t == LONG || t == QUAD) {
321 * remember specifiers "short", "long" and "long long" in
324 if (dcs->d_lmod != NOTSPEC)
325 /* more than one, print error in deftyp() */
330 * remember specifiers "void", "char", "int", "float" or
331 * "double" int dcs->d_atyp
333 if (dcs->d_atyp != NOTSPEC)
334 /* more than one, print error in deftyp() */
341 * called if a list of declaration specifiers contains a typedef name
342 * and other specifiers (except struct, union, enum, typedef name)
345 tdeferr(type_t *td, tspec_t t)
354 if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG ||
357 /* modifying typedef with ... */
358 warning(5, ttab[t].tt_name);
359 td = duptyp(gettyp(mrgtspec(t2, t)));
365 if (t2 == INT || t2 == UINT) {
366 /* modifying typedef with ... */
368 td = duptyp(gettyp(t2 == INT ? SHORT : USHORT));
374 if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG ||
375 t2 == FLOAT || t2 == DOUBLE) {
376 /* modifying typedef with ... */
380 } else if (t2 == UINT) {
382 } else if (t2 == LONG) {
384 } else if (t2 == ULONG) {
386 } else if (t2 == FLOAT) {
388 } else if (t2 == DOUBLE) {
389 td = gettyp(LDOUBLE);
396 /* LINTED (enumeration values not handled in switch) */
420 /* Anything other is not accepted. */
427 * Remember the symbol of a typedef name (2nd arg) in a struct, union
428 * or enum tag if the typedef name is the first defined for this tag.
430 * If the tag is unnamed, the typdef name is used for identification
431 * of this tag in lint2. Although its possible that more than one typedef
432 * name is defined for one tag, the first name defined should be unique
433 * if the tag is unnamed.
436 settdsym(type_t *tp, sym_t *sym)
440 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
441 if (tp->t_str->stdef == NULL)
442 tp->t_str->stdef = sym;
443 } else if (t == ENUM) {
444 if (tp->t_enum->etdef == NULL)
445 tp->t_enum->etdef = sym;
450 * Remember a qualifier which is part of the declaration specifiers
451 * (and not the declarator) in the top element of the declaration stack.
452 * Also detect multiple qualifiers of the same kind.
454 * The remembered qualifier is used by deftyp() to construct the type
455 * for all declarators.
464 warning(10, "const");
470 if (dcs->d_volatile) {
472 warning(10, "volatile");
479 * Go to the next declaration level (structs, nested structs, blocks,
480 * argument declaration lists ...)
488 (void)printf("pushdecl(%d)\n", (int)sc);
490 /* put a new element on the declaration stack */
491 if ((di = calloc(1, sizeof (dinfo_t))) == NULL)
496 di->d_ldlsym = &di->d_dlsyms;
500 * Go back to previous declaration level
508 (void)printf("popdecl(%d)\n", (int)dcs->d_ctx);
510 if (dcs->d_nxt == NULL)
516 /* there is nothing after external declarations */
523 * Symbols declared in (nested) structs or enums are
524 * part of the next level (they are removed from the
525 * symbol table if the symbols of the outher level are
528 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
529 dcs->d_ldlsym = di->d_ldlsym;
533 * All symbols in dcs->d_dlsyms are introduced in old style
534 * argument declarations (it's not clean, but possible).
535 * They are appended to the list of symbols declared in
536 * an old style argument identifier list or a new style
537 * parameter type list.
539 if (di->d_dlsyms != NULL) {
540 *di->d_ldlsym = dcs->d_fpsyms;
541 dcs->d_fpsyms = di->d_dlsyms;
547 * Append all symbols declared in the abstract declaration
548 * to the list of symbols declared in the surrounding decl.
550 * XXX I'm not sure whether they should be removed from the
551 * symbol table now or later.
553 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
554 dcs->d_ldlsym = di->d_ldlsym;
557 /* check usage of local vars */
561 /* usage of arguments will be checked by funcend() */
562 rmsyms(di->d_dlsyms);
571 * Set flag d_asm in all declaration stack elements up to the
574 * This is used to mark compound statements which have, possibly in
575 * nested compound statements, asm statements. For these compound
576 * statements no warnings about unused or unitialized variables are
579 * There is no need to clear d_asm in dinfo structs with context AUTO,
580 * because these structs are freed at the end of the compound statement.
581 * But it must be cleard in the outermost dinfo struct, which has
582 * context EXTERN. This could be done in clrtyp() and would work for
583 * C, but not for C++ (due to mixed statements and declarations). Thus
584 * we clear it in glclup(), which is used to do some cleanup after
585 * global declarations/definitions.
592 for (di = dcs; di != NULL; di = di->d_nxt)
597 * Clean all elements of the top element of declaration stack which
598 * will be used by the next declaration
604 dcs->d_atyp = dcs->d_smod = dcs->d_lmod = NOTSPEC;
607 dcs->d_const = dcs->d_volatile = 0;
609 dcs->d_mscl = dcs->d_terr = 0;
615 * Create a type structure from the informations gathered in
616 * the declaration stack.
617 * Complain about storage classes which are not possible in current
627 t = dcs->d_atyp; /* CHAR, INT, FLOAT, DOUBLE, VOID */
628 s = dcs->d_smod; /* SIGNED, UNSIGNED */
629 l = dcs->d_lmod; /* SHORT, LONG, QUAD */
633 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && tp == NULL)
636 if (tp != NULL && (t != NOTSPEC || s != NOTSPEC || l != NOTSPEC)) {
637 /* should never happen */
661 /* use 'double' instead of ... */
670 /* 'long double' is illegal in ... */
679 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) {
685 dcs->d_type = gettyp(mrgtspec(t, s));
689 /* only one storage class allowed */
693 /* illegal type combination */
697 if (dcs->d_ctx == EXTERN) {
698 if (scl == REG || scl == AUTO) {
699 /* illegal storage class */
703 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PARG) {
704 if (scl != NOSCL && scl != REG) {
705 /* only "register" valid ... */
713 if (dcs->d_const && dcs->d_type->t_const) {
714 if (!dcs->d_type->t_typedef)
716 /* typedef already qualified with "%s" */
717 warning(68, "const");
719 if (dcs->d_volatile && dcs->d_type->t_volatile) {
720 if (!dcs->d_type->t_typedef)
722 /* typedef already qualified with "%s" */
723 warning(68, "volatile");
726 if (dcs->d_const || dcs->d_volatile) {
727 dcs->d_type = duptyp(dcs->d_type);
728 dcs->d_type->t_const |= dcs->d_const;
729 dcs->d_type->t_volatile |= dcs->d_volatile;
734 * Merge type specifiers (char, ..., long long, signed, unsigned).
737 mrgtspec(tspec_t t, tspec_t s)
740 if (s == SIGNED || s == UNSIGN) {
742 t = s == SIGNED ? SCHAR : UCHAR;
743 } else if (t == SHORT) {
744 t = s == SIGNED ? SHORT : USHORT;
745 } else if (t == INT) {
746 t = s == SIGNED ? INT : UINT;
747 } else if (t == LONG) {
748 t = s == SIGNED ? LONG : ULONG;
749 } else if (t == QUAD) {
750 t = s == SIGNED ? QUAD : UQUAD;
758 * Return the length of a type in bit.
760 * Printing a message if the outhermost dimension of an array is 0 must
761 * be done by the caller. All other problems are reported by length()
762 * if name is not NULL.
765 length(type_t *tp, const char *name)
770 while (tp && tp->t_tspec == ARRAY) {
777 switch (tp->t_tspec) {
779 /* compiler takes size of function */
780 LERROR("%s", msgs[12]);
784 if (incompl(tp) && name != NULL) {
785 /* incomplete structure or union %s: %s */
786 error(31, tp->t_str->stag->s_name, name);
788 elsz = tp->t_str->size;
791 if (incompl(tp) && name != NULL) {
792 /* incomplete enum type: %s */
797 elsz = size(tp->t_tspec);
802 return (elem * elsz);
806 * Get the alignment of the given type in bits.
814 while (tp && tp->t_tspec == ARRAY)
820 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
821 a = tp->t_str->align;
822 } else if (t == FUNC) {
823 /* compiler takes alignment of function */
825 a = LINT_ALIGN(1) * CHAR_BIT;
827 if ((a = size(t)) == 0) {
829 } else if (a > LINT_ALIGN(1) * CHAR_BIT) {
830 a = LINT_ALIGN(1) * CHAR_BIT;
833 if (a < CHAR_BIT || a > LINT_ALIGN(1) * CHAR_BIT)
834 LERROR("getbound()");
839 * Concatenate two lists of symbols by s_nxt. Used by declarations of
840 * struct/union/enum elements and parameters.
843 lnklst(sym_t *l1, sym_t *l2)
847 if ((l = l1) == NULL)
849 while (l1->s_nxt != NULL)
856 * Check if the type of the given symbol is valid and print an error
857 * message if it is not.
860 * - arrays of incomlete types or functions
861 * - functions returning arrays or functions
862 * - void types other than type of function or pointer
872 while ((tp = *tpp) != NULL) {
875 * If this is the type of an old style function definition,
876 * a better warning is printed in funcdef().
878 if (t == FUNC && !tp->t_proto &&
879 !(to == NOTSPEC && sym->s_osdef)) {
881 /* function declaration is not a prototype */
885 if (t == FUNC || t == ARRAY) {
886 /* function returns illegal type */
889 *tpp = incref(*tpp, PTR);
891 *tpp = incref((*tpp)->t_subt, PTR);
894 } else if (tp->t_const || tp->t_volatile) {
895 if (sflag) { /* XXX oder better !tflag ? */
896 /* function cannot return const... */
902 /* array of function is illegal */
906 } else if (t == ARRAY && tp->t_dim == 0) {
910 } else if (t == VOID) {
911 /* illegal use of void */
914 #if 0 /* errors are produced by length() */
915 } else if (incompl(tp)) {
916 /* array of incomplete type */
924 } else if (to == NOTSPEC && t == VOID) {
925 if (dcs->d_ctx == PARG) {
926 if (sym->s_scl != ABSTRACT) {
927 if (sym->s_name == unnamed)
929 /* void param cannot have name: %s */
930 error(61, sym->s_name);
933 } else if (dcs->d_ctx == ABSTRACT) {
935 } else if (sym->s_scl != TYPEDEF) {
936 /* void type for %s */
937 error(19, sym->s_name);
941 if (t == VOID && to != PTR) {
942 if (tp->t_const || tp->t_volatile) {
943 /* inappropriate qualifiers with "void" */
945 tp->t_const = tp->t_volatile = 0;
954 * Process the declarator of a struct/union element.
957 decl1str(sym_t *dsym)
962 int o = 0; /* Appease gcc */
965 if ((sc = dsym->s_scl) != MOS && sc != MOU)
966 LERROR("decl1str()");
968 if (dcs->d_rdcsym != NULL) {
969 if ((sc = dcs->d_rdcsym->s_scl) != MOS && sc != MOU)
970 /* should be ensured by storesym() */
971 LERROR("decl1str()");
972 if (dsym->s_styp == dcs->d_rdcsym->s_styp) {
973 /* duplicate member name: %s */
974 error(33, dsym->s_name);
975 rmsym(dcs->d_rdcsym);
981 t = (tp = dsym->s_type)->t_tspec;
987 * only unsigned and signed int are portable bit-field types
988 * (at least in ANSI C, in traditional C only unsigned int)
990 if (t == CHAR || t == UCHAR || t == SCHAR ||
991 t == SHORT || t == USHORT || t == ENUM) {
992 if (bitfieldtype_ok == 0) {
996 * bit-field type '%s' invalid in
1000 tyname(buf, sizeof(buf), tp));
1002 /* nonportable bit-field type */
1006 } else if (t == INT && dcs->d_smod == NOTSPEC) {
1007 if (pflag && bitfieldtype_ok == 0) {
1008 /* nonportable bit-field type */
1011 } else if (t != INT && t != UINT) {
1013 * Non-integer types are always illegal for
1014 * bitfields, regardless of BITFIELDTYPE.
1015 * Integer types not dealt with above are
1016 * okay only if BITFIELDTYPE is in effect.
1018 if (bitfieldtype_ok == 0 || isityp(t) == 0) {
1019 /* illegal bit-field type */
1022 dsym->s_type = tp = duptyp(gettyp(t = INT));
1023 if ((tp->t_flen = sz) > size(t))
1024 tp->t_flen = size(t);
1027 if ((len = tp->t_flen) < 0 || len > size(t)) {
1028 /* illegal bit-field size */
1030 tp->t_flen = size(t);
1031 } else if (len == 0 && dsym->s_name != unnamed) {
1032 /* zero size bit-field */
1034 tp->t_flen = size(t);
1036 if (dsym->s_scl == MOU) {
1037 /* illegal use of bit-field */
1039 dsym->s_type->t_isfield = 0;
1042 } else if (t == FUNC) {
1043 /* function illegal in structure or union */
1045 dsym->s_type = tp = incref(tp, t = PTR);
1049 * bit-fields of length 0 are not warned about because length()
1050 * does not return the length of the bit-field but the length
1051 * of the type the bit-field is packed in (its ok)
1053 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1054 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1055 /* illegal zero sized structure member: %s */
1056 c99ism(39, dsym->s_name);
1060 if (dcs->d_ctx == MOU) {
1064 if (dsym->s_field) {
1065 align(getbound(tp), tp->t_flen);
1066 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t);
1067 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1068 dcs->d_offset += tp->t_flen;
1070 align(getbound(tp), 0);
1071 dsym->s_value.v_quad = dcs->d_offset;
1072 dcs->d_offset += sz;
1074 if (dcs->d_ctx == MOU) {
1075 if (o > dcs->d_offset)
1082 * Clear the BITFIELDTYPE indicator after processing each
1083 * structure element.
1085 bitfieldtype_ok = 0;
1091 * Aligns next structure element as required.
1093 * al contains the required alignment, len the length of a bit-field.
1096 align(int al, int len)
1101 * The alignment of the current element becomes the alignment of
1102 * the struct/union if it is larger than the current alignment
1103 * of the struct/union.
1105 if (al > dcs->d_stralign)
1106 dcs->d_stralign = al;
1108 no = roundup2(dcs->d_offset, al);
1109 if (len == 0 || dcs->d_offset + len > no)
1114 * Remember the width of the field in its type structure.
1117 bitfield(sym_t *dsym, int len)
1121 dsym = getblk(sizeof (sym_t));
1122 dsym->s_name = unnamed;
1123 dsym->s_kind = FMOS;
1125 dsym->s_type = gettyp(UINT);
1126 dsym->s_blklev = -1;
1128 dsym->s_type = duptyp(dsym->s_type);
1129 dsym->s_type->t_isfield = 1;
1130 dsym->s_type->t_flen = len;
1136 * Collect informations about a sequence of asterisks and qualifiers
1137 * in a list of type pqinf_t.
1138 * Qualifiers refer always to the left asterisk. The rightmost asterisk
1139 * will be at the top of the list.
1142 mergepq(pqinf_t *p1, pqinf_t *p2)
1146 if (p2->p_pcnt != 0) {
1147 /* left '*' at the end of the list */
1148 for (p = p2; p->p_nxt != NULL; p = p->p_nxt)
1156 warning(10, "const");
1160 if (p2->p_volatile) {
1161 if (p1->p_volatile) {
1163 warning(10, "volatile");
1173 * Followint 3 functions extend the type of a declarator with
1174 * pointer, function and array types.
1176 * The current type is the type built by deftyp() (dcs->d_type) and
1177 * pointer, function and array types already added for this
1178 * declarator. The new type extension is inserted between both.
1181 addptr(sym_t *decl, pqinf_t *pi)
1186 tpp = &decl->s_type;
1187 while (*tpp && *tpp != dcs->d_type)
1188 tpp = &(*tpp)->t_subt;
1192 while (pi != NULL) {
1193 *tpp = tp = getblk(sizeof (type_t));
1195 tp->t_const = pi->p_const;
1196 tp->t_volatile = pi->p_volatile;
1197 *(tpp = &tp->t_subt) = dcs->d_type;
1206 * If a dimension was specified, dim is 1, otherwise 0
1207 * n is the specified dimension
1210 addarray(sym_t *decl, int dim, int n)
1214 tpp = &decl->s_type;
1215 while (*tpp && *tpp != dcs->d_type)
1216 tpp = &(*tpp)->t_subt;
1220 *tpp = tp = getblk(sizeof (type_t));
1221 tp->t_tspec = ARRAY;
1222 tp->t_subt = dcs->d_type;
1226 /* negative array dimension */
1229 } else if (n == 0 && dim) {
1230 /* zero array dimension */
1232 } else if (n == 0 && !dim) {
1233 /* is incomplete type */
1241 addfunc(sym_t *decl, sym_t *args)
1247 /* function prototypes are illegal in traditional C */
1249 args = nsfunc(decl, args);
1255 * The symbols are removed from the symbol table by popdecl() after
1256 * addfunc(). To be able to restore them if this is a function
1257 * definition, a pointer to the list of all symbols is stored in
1258 * dcs->d_nxt->d_fpsyms. Also a list of the arguments (concatenated
1259 * by s_nxt) is stored in dcs->d_nxt->d_fargs.
1260 * (dcs->d_nxt must be used because *dcs is the declaration stack
1261 * element created for the list of params and is removed after
1264 if (dcs->d_nxt->d_ctx == EXTERN &&
1265 decl->s_type == dcs->d_nxt->d_type) {
1266 dcs->d_nxt->d_fpsyms = dcs->d_dlsyms;
1267 dcs->d_nxt->d_fargs = args;
1270 tpp = &decl->s_type;
1271 while (*tpp && *tpp != dcs->d_nxt->d_type)
1272 tpp = &(*tpp)->t_subt;
1276 *tpp = tp = getblk(sizeof (type_t));
1278 tp->t_subt = dcs->d_nxt->d_type;
1279 if ((tp->t_proto = dcs->d_proto) != 0)
1281 tp->t_vararg = dcs->d_vararg;
1287 * Called for new style function declarations.
1291 nsfunc(sym_t *decl, sym_t *args)
1298 * Declarations of structs/unions/enums in param lists are legal,
1301 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1303 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) {
1304 /* dubious tag declaration: %s %s */
1305 warning(85, scltoa(sc), sym->s_name);
1310 for (arg = args; arg != NULL; arg = arg->s_nxt) {
1311 if (arg->s_type->t_tspec == VOID) {
1312 if (n > 1 || arg->s_nxt != NULL) {
1313 /* "void" must be sole parameter */
1315 arg->s_type = gettyp(INT);
1321 /* return NULL if first param is VOID */
1322 return (args != NULL && args->s_type->t_tspec != VOID ? args : NULL);
1326 * Called for old style function declarations.
1329 osfunc(sym_t *decl, sym_t *args)
1333 * Remember list of params only if this is really seams to be
1334 * a function definition.
1336 if (dcs->d_nxt->d_ctx == EXTERN &&
1337 decl->s_type == dcs->d_nxt->d_type) {
1339 * We assume that this becomes a function definition. If
1340 * we are wrong, its corrected in chkfdef().
1344 decl->s_args = args;
1348 /* function prototype parameters must have types */
1354 * Lists of Identifiers in functions declarations are allowed only if
1355 * its also a function definition. If this is not the case, print a
1359 chkfdef(sym_t *sym, int msg)
1364 /* incomplete or misplaced function definition */
1373 * Process the name in a declarator.
1374 * If the symbol does already exists, a new one is created.
1375 * The symbol becomes one of the storage classes EXTERN, STATIC, AUTO or
1377 * s_def and s_reg are valid after dname().
1384 if (sym->s_scl == NOSCL) {
1385 dcs->d_rdcsym = NULL;
1386 } else if (sym->s_defarg) {
1388 dcs->d_rdcsym = NULL;
1390 dcs->d_rdcsym = sym;
1391 sym = pushdown(sym);
1394 switch (dcs->d_ctx) {
1398 sym->s_styp = dcs->d_tagtyp->t_str;
1400 sym->s_value.v_tspec = INT;
1405 * static and external symbols without "extern" are
1406 * considered to be tentative defined, external
1407 * symbols with "extern" are declared, and typedef names
1408 * are defined. Tentative defined and declared symbols
1409 * may become defined if an initializer is present or
1410 * this is a function definition.
1412 if ((sc = dcs->d_scl) == NOSCL) {
1415 } else if (sc == STATIC) {
1417 } else if (sc == TYPEDEF) {
1419 } else if (sc == EXTERN) {
1429 if ((sc = dcs->d_scl) == NOSCL) {
1431 } else if (sc == REG) {
1440 if ((sc = dcs->d_scl) == NOSCL) {
1442 * XXX somewhat ugly because we dont know whether
1443 * this is AUTO or EXTERN (functions). If we are
1444 * wrong it must be corrected in decl1loc(), where
1445 * we have the necessary type information.
1449 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1451 } else if (sc == REG) {
1455 } else if (sc == EXTERN) {
1466 sym->s_type = dcs->d_type;
1468 dcs->d_fpsyms = NULL;
1474 * Process a name in the list of formal params in an old style function
1481 if (sym->s_scl != NOSCL) {
1482 if (blklev == sym->s_blklev) {
1483 /* redeclaration of formal parameter %s */
1484 error(21, sym->s_name);
1488 sym = pushdown(sym);
1490 sym->s_type = gettyp(INT);
1493 sym->s_defarg = sym->s_arg = 1;
1498 * Create the type of a tag.
1500 * tag points to the symbol table entry of the tag
1501 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1502 * decl is 1 if the type of the tag will be completed in this declaration
1503 * (the following token is T_LBRACE)
1504 * semi is 1 if the following token is T_SEMI
1507 mktag(sym_t *tag, tspec_t kind, int decl, int semi)
1512 if (kind == STRUCT) {
1514 } else if (kind == UNION) {
1516 } else if (kind == ENUM) {
1523 if (tag->s_scl != NOSCL) {
1524 tag = newtag(tag, scl, decl, semi);
1526 /* a new tag, no empty declaration */
1527 dcs->d_nxt->d_nedecl = 1;
1528 if (scl == ENUMTAG && !decl) {
1529 if (!tflag && (sflag || pflag))
1530 /* forward reference to enum type */
1534 if (tag->s_scl == NOSCL) {
1536 tag->s_type = tp = getblk(sizeof (type_t));
1541 tag = getblk(sizeof (sym_t));
1542 tag->s_name = unnamed;
1543 UNIQUE_CURR_POS(tag->s_dpos);
1547 tag->s_type = tp = getblk(sizeof (type_t));
1548 dcs->d_nxt->d_nedecl = 1;
1551 if (tp->t_tspec == NOTSPEC) {
1554 tp->t_str = getblk(sizeof (str_t));
1555 tp->t_str->align = CHAR_BIT;
1556 tp->t_str->stag = tag;
1559 tp->t_enum = getblk(sizeof (enum_t));
1560 tp->t_enum->etag = tag;
1562 /* is incomplete type */
1570 * Checks all possible cases of tag redeclarations.
1571 * decl is 1 if T_LBRACE follows
1572 * semi is 1 if T_SEMI follows
1575 newtag(sym_t *tag, scl_t scl, int decl, int semi)
1578 if (tag->s_blklev < blklev) {
1583 /* decl. introduces new type ... */
1584 warning(44, scltoa(scl), tag->s_name);
1585 tag = pushdown(tag);
1586 } else if (tag->s_scl != scl) {
1587 /* base type is really "%s %s" */
1588 warning(45, scltoa(tag->s_scl), tag->s_name);
1590 dcs->d_nxt->d_nedecl = 1;
1592 /* "struct a { ... } " */
1594 /* redefinition hides earlier one: %s */
1595 warning(43, tag->s_name);
1596 tag = pushdown(tag);
1597 dcs->d_nxt->d_nedecl = 1;
1598 } else if (tag->s_scl != scl) {
1599 /* base type is really "%s %s" */
1600 warning(45, scltoa(tag->s_scl), tag->s_name);
1601 /* declaration introduces new type in ANSI C: %s %s */
1603 warning(44, scltoa(scl), tag->s_name);
1604 tag = pushdown(tag);
1605 dcs->d_nxt->d_nedecl = 1;
1608 if (tag->s_scl != scl) {
1609 /* (%s) tag redeclared */
1610 error(46, scltoa(tag->s_scl));
1612 tag = pushdown(tag);
1613 dcs->d_nxt->d_nedecl = 1;
1614 } else if (decl && !incompl(tag->s_type)) {
1615 /* (%s) tag redeclared */
1616 error(46, scltoa(tag->s_scl));
1618 tag = pushdown(tag);
1619 dcs->d_nxt->d_nedecl = 1;
1620 } else if (semi || decl) {
1621 dcs->d_nxt->d_nedecl = 1;
1633 case EXTERN: s = "extern"; break;
1634 case STATIC: s = "static"; break;
1635 case AUTO: s = "auto"; break;
1636 case REG: s = "register"; break;
1637 case TYPEDEF: s = "typedef"; break;
1638 case STRTAG: s = "struct"; break;
1639 case UNIONTAG: s = "union"; break;
1640 case ENUMTAG: s = "enum"; break;
1641 default: LERROR("tagttoa()");
1647 * Completes the type of a tag in a struct/union/enum declaration.
1648 * tp points to the type of the, tag, fmem to the list of members/enums.
1651 compltag(type_t *tp, sym_t *fmem)
1658 /* from now a complete type */
1661 if ((t = tp->t_tspec) != ENUM) {
1662 align(dcs->d_stralign, 0);
1664 sp->align = dcs->d_stralign;
1665 sp->size = dcs->d_offset;
1667 if (sp->size == 0) {
1669 (void)c99ism(47, ttab[t].tt_name);
1672 for (mem = fmem; mem != NULL; mem = mem->s_nxt) {
1673 if (mem->s_name != unnamed)
1677 /* %s has no named members */
1679 t == STRUCT ? "structure" : "union");
1683 tp->t_enum->elem = fmem;
1689 * Processes the name of an enumerator in en enum declaration.
1691 * sym points to the enumerator
1692 * val is the value of the enumerator
1693 * impl is 1 if the value of the enumerator was not explicit specified.
1696 ename(sym_t *sym, int val, int impl)
1700 if (sym->s_blklev == blklev) {
1701 /* no hflag, because this is illegal!!! */
1703 /* enumeration constant hides parameter: %s */
1704 warning(57, sym->s_name);
1706 /* redeclaration of %s */
1707 error(27, sym->s_name);
1709 * inside blocks it should not too complicated
1710 * to find the position of the previous
1718 /* redefinition hides earlier one: %s */
1719 warning(43, sym->s_name);
1721 sym = pushdown(sym);
1723 sym->s_scl = ENUMCON;
1724 sym->s_type = dcs->d_tagtyp;
1725 sym->s_value.v_tspec = INT;
1726 sym->s_value.v_quad = val;
1727 if (impl && val - 1 == INT_MAX) {
1728 /* overflow in enumeration values: %s */
1729 warning(48, sym->s_name);
1736 * Process a single external declarator.
1739 decl1ext(sym_t *dsym, int initflg)
1741 int warn, rval, redec;
1748 if (initflg && !(initerr = chkinit(dsym)))
1752 * Declarations of functions are marked as "tentative" in dname().
1753 * This is wrong because there are no tentative function
1756 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1759 if (dcs->d_inline) {
1760 if (dsym->s_type->t_tspec == FUNC) {
1763 /* variable declared inline: %s */
1764 warning(268, dsym->s_name);
1768 /* Write the declaration into the output file */
1769 if (plibflg && llibflg &&
1770 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1772 * With both LINTLIBRARY and PROTOLIB the prototype is
1773 * written as a function definition to the output file.
1775 rval = dsym->s_type->t_subt->t_tspec != VOID;
1776 outfdef(dsym, &dsym->s_dpos, rval, 0, NULL);
1778 outsym(dsym, dsym->s_scl, dsym->s_def);
1781 if ((rdsym = dcs->d_rdcsym) != NULL) {
1784 * If the old symbol stems from an old style function definition
1785 * we have remembered the params in rdsmy->s_args and compare
1786 * them with the params of the prototype.
1788 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1789 redec = chkosdef(rdsym, dsym);
1794 if (!redec && !isredec(dsym, (warn = 0, &warn))) {
1797 /* redeclaration of %s */
1798 (*(sflag ? error : warning))(27, dsym->s_name);
1799 prevdecl(-1, rdsym);
1803 * Overtake the remembered params if the new symbol
1804 * is not a prototype.
1806 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1807 dsym->s_osdef = rdsym->s_osdef;
1808 dsym->s_args = rdsym->s_args;
1809 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1813 * Remember the position of the declaration if the
1814 * old symbol was a prototype and the new is not.
1815 * Also remember the position if the old symbol
1816 * was defined and the new is not.
1818 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1819 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1820 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1821 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1825 * Copy informations about usage of the name into
1828 cpuinfo(dsym, rdsym);
1830 /* Once a name is defined, it remains defined. */
1831 if (rdsym->s_def == DEF)
1834 /* once a function is inline, it remains inline */
1835 if (rdsym->s_inline)
1838 compltyp(dsym, rdsym);
1845 if (dsym->s_scl == TYPEDEF) {
1846 dsym->s_type = duptyp(dsym->s_type);
1847 dsym->s_type->t_typedef = 1;
1848 settdsym(dsym->s_type, dsym);
1854 * Copies informations about usage into a new symbol table entry of
1858 cpuinfo(sym_t *sym, sym_t *rdsym)
1861 sym->s_spos = rdsym->s_spos;
1862 sym->s_upos = rdsym->s_upos;
1863 sym->s_set = rdsym->s_set;
1864 sym->s_used = rdsym->s_used;
1868 * Prints an error and returns 1 if a symbol is redeclared/redefined.
1869 * Otherwise returns 0 and, in some cases of minor problems, prints
1873 isredec(sym_t *dsym, int *warn)
1877 if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
1878 /* redeclaration of %s */
1879 error(27, dsym->s_name);
1883 if (rsym->s_scl == TYPEDEF) {
1884 /* typedef redeclared: %s */
1885 error(89, dsym->s_name);
1889 if (dsym->s_scl == TYPEDEF) {
1890 /* redeclaration of %s */
1891 error(27, dsym->s_name);
1895 if (rsym->s_def == DEF && dsym->s_def == DEF) {
1896 /* redefinition of %s */
1897 error(28, dsym->s_name);
1901 if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, warn)) {
1902 /* redeclaration of %s */
1903 error(27, dsym->s_name);
1907 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
1909 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
1911 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
1913 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
1915 * All cases except "int a = 1; static int a;" are caught
1916 * above with or without a warning
1918 /* redeclaration of %s */
1919 error(27, dsym->s_name);
1923 if (rsym->s_scl == EXTERN) {
1924 /* previously declared extern, becomes static: %s */
1925 warning(29, dsym->s_name);
1931 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
1933 /* redeclaration of %s; ANSI C requires "static" */
1935 warning(30, dsym->s_name);
1938 dsym->s_scl = STATIC;
1943 * Checks if two types are compatible. Returns 0 if not, otherwise 1.
1945 * ignqual ignore qualifiers of type; used for function params
1946 * promot promote left type; used for comparison of params of
1947 * old style function definitions with params of prototypes.
1948 * *warn set to 1 if an old style function declaration is not
1949 * compatible with a prototype
1952 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *warn)
1956 while (tp1 != NULL && tp2 != NULL) {
1962 } else if (t == CHAR || t == SCHAR) {
1964 } else if (t == UCHAR) {
1965 t = tflag ? UINT : INT;
1966 } else if (t == SHORT) {
1968 } else if (t == USHORT) {
1970 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
1974 if (t != tp2->t_tspec)
1977 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
1980 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
1983 if (t == STRUCT || t == UNION)
1984 return (tp1->t_str == tp2->t_str);
1986 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
1987 if (tp1->t_dim != 0 && tp2->t_dim != 0)
1991 /* dont check prototypes for traditional */
1992 if (t == FUNC && !tflag) {
1993 if (tp1->t_proto && tp2->t_proto) {
1994 if (!eqargs(tp1, tp2, warn))
1996 } else if (tp1->t_proto) {
1997 if (!mnoarg(tp1, warn))
1999 } else if (tp2->t_proto) {
2000 if (!mnoarg(tp2, warn))
2007 ignqual = promot = 0;
2011 return (tp1 == tp2);
2015 * Compares the parameter types of two prototypes.
2018 eqargs(type_t *tp1, type_t *tp2, int *warn)
2022 if (tp1->t_vararg != tp2->t_vararg)
2028 while (a1 != NULL && a2 != NULL) {
2030 if (eqtype(a1->s_type, a2->s_type, 1, 0, warn) == 0)
2042 * mnoarg() (matches functions with no argument type information)
2043 * returns 1 if all parameters of a prototype are compatible with
2044 * and old style function declaration.
2045 * This is the case if following conditions are met:
2046 * 1. the prototype must have a fixed number of parameters
2047 * 2. no parameter is of type float
2048 * 3. no parameter is converted to another type if integer promotion
2052 mnoarg(type_t *tp, int *warn)
2061 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) {
2062 if ((t = arg->s_type->t_tspec) == FLOAT ||
2063 t == CHAR || t == SCHAR || t == UCHAR ||
2064 t == SHORT || t == USHORT) {
2073 * Compares a prototype declaration with the remembered arguments of
2074 * a previous old style function definition.
2077 chkosdef(sym_t *rdsym, sym_t *dsym)
2079 sym_t *args, *pargs, *arg, *parg;
2083 args = rdsym->s_args;
2084 pargs = dsym->s_type->t_args;
2089 for (arg = args; arg != NULL; arg = arg->s_nxt)
2091 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2093 if (narg != nparg) {
2094 /* prototype does not match old-style definition */
2106 * If it does not match due to promotion and sflag is
2107 * not set we print only a warning.
2109 if (!eqtype(arg->s_type, parg->s_type, 1, 1, &warn) || warn) {
2110 /* prototype does not match old-style def., arg #%d */
2121 /* old style definition */
2122 prevdecl(300, rdsym);
2128 * Completes a type by copying the dimension and prototype information
2129 * from a second compatible type.
2131 * Following lines are legal:
2132 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2133 * "typedef ft(); ft f; f(int); ft g; g(long);"
2134 * This means that, if a type is completed, the type structure must
2138 compltyp(sym_t *dsym, sym_t *ssym)
2140 type_t **dstp, *src;
2143 dstp = &dsym->s_type;
2146 while ((dst = *dstp) != NULL) {
2147 if (src == NULL || dst->t_tspec != src->t_tspec)
2148 LERROR("compltyp()");
2149 if (dst->t_tspec == ARRAY) {
2150 if (dst->t_dim == 0 && src->t_dim != 0) {
2151 *dstp = dst = duptyp(dst);
2152 dst->t_dim = src->t_dim;
2153 /* now a complete type */
2156 } else if (dst->t_tspec == FUNC) {
2157 if (!dst->t_proto && src->t_proto) {
2158 *dstp = dst = duptyp(dst);
2160 dst->t_args = src->t_args;
2163 dstp = &dst->t_subt;
2169 * Completes the declaration of a single argument.
2172 decl1arg(sym_t *sym, int initflg)
2180 if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) {
2181 /* redeclaration of formal parameter %s */
2182 error(237, sym->s_name);
2183 rmsym(dcs->d_rdcsym);
2188 /* declared argument %s is missing */
2189 error(53, sym->s_name);
2194 /* cannot initialize parameter: %s */
2195 error(52, sym->s_name);
2199 if ((t = sym->s_type->t_tspec) == ARRAY) {
2200 sym->s_type = incref(sym->s_type->t_subt, PTR);
2201 } else if (t == FUNC) {
2203 /* a function is declared as an argument: %s */
2204 warning(50, sym->s_name);
2205 sym->s_type = incref(sym->s_type, PTR);
2206 } else if (t == FLOAT) {
2208 sym->s_type = gettyp(DOUBLE);
2212 /* argument declared inline: %s */
2213 warning(269, sym->s_name);
2216 * Arguments must have complete types. lengths() prints the needed
2217 * error messages (null dimension is impossible because arrays are
2218 * converted to pointers).
2220 if (sym->s_type->t_tspec != VOID)
2221 (void)length(sym->s_type, sym->s_name);
2229 * Does some checks for lint directives which apply to functions.
2230 * Processes arguments in old style function definitions which default
2232 * Checks compatibility of old style function definition with previous
2238 sym_t *args, *arg, *pargs, *parg;
2239 int narg, nparg, n, msg;
2242 args = funcsym->s_args;
2243 pargs = funcsym->s_type->t_args;
2245 /* check for illegal combinations of lint directives */
2246 if (prflstrg != -1 && scflstrg != -1) {
2247 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2249 prflstrg = scflstrg = -1;
2251 if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) {
2252 /* dubious use of ** VARARGS ** with ** %s ** */
2253 warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2258 * check if the argument of a lint directive is compatible with the
2259 * number of arguments.
2262 for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_nxt)
2264 if (nargusg > narg) {
2265 /* argument number mismatch with directive: ** %s ** */
2266 warning(283, "ARGSUSED");
2269 if (nvararg > narg) {
2270 /* argument number mismatch with directive: ** %s ** */
2271 warning(283, "VARARGS");
2274 if (prflstrg > narg) {
2275 /* argument number mismatch with directive: ** %s ** */
2276 warning(283, "PRINTFLIKE");
2278 } else if (prflstrg == 0) {
2281 if (scflstrg > narg) {
2282 /* argument number mismatch with directive: ** %s ** */
2283 warning(283, "SCANFLIKE");
2285 } else if (scflstrg == 0) {
2288 if (prflstrg != -1 || scflstrg != -1) {
2289 narg = prflstrg != -1 ? prflstrg : scflstrg;
2291 for (n = 1; n < narg; n++)
2293 if (arg->s_type->t_tspec != PTR ||
2294 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2295 t != UCHAR && t != SCHAR)) {
2296 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2298 prflstrg = scflstrg = -1;
2303 * print a warning for each argument of an old style function
2304 * definition which defaults to int
2306 for (arg = args; arg != NULL; arg = arg->s_nxt) {
2307 if (arg->s_defarg) {
2308 /* argument type defaults to int: %s */
2309 warning(32, arg->s_name);
2316 * If this is an old style function definition and a prototyp
2317 * exists, compare the types of arguments.
2319 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2321 * If the number of arguments does not macht, we need not
2326 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2328 for (arg = args; arg != NULL; arg = arg->s_nxt)
2330 if (narg != nparg) {
2331 /* parameter mismatch: %d declared, %d defined */
2332 error(51, nparg, narg);
2338 msg |= chkptdecl(arg, parg);
2344 /* prototype declaration */
2345 prevdecl(285, dcs->d_rdcsym);
2347 /* from now the prototype is valid */
2348 funcsym->s_osdef = 0;
2349 funcsym->s_args = NULL;
2356 * Checks compatibility of an old style function definition with a previous
2357 * prototype declaration.
2358 * Returns 1 if the position of the previous declaration should be reported.
2361 chkptdecl(sym_t *arg, sym_t *parg)
2372 if (!eqtype(tp, ptp, 1, 1, &warn)) {
2373 if (eqtype(tp, ptp, 1, 0, &warn)) {
2374 /* type does not match prototype: %s */
2375 msg = gnuism(58, arg->s_name);
2377 /* type does not match prototype: %s */
2378 error(58, arg->s_name);
2382 /* type does not match prototype: %s */
2383 (*(sflag ? error : warning))(58, arg->s_name);
2391 * Completes a single local declaration/definition.
2394 decl1loc(sym_t *dsym, int initflg)
2397 /* Correct a mistake done in dname(). */
2398 if (dsym->s_type->t_tspec == FUNC) {
2400 if (dcs->d_scl == NOSCL)
2401 dsym->s_scl = EXTERN;
2404 if (dsym->s_type->t_tspec == FUNC) {
2405 if (dsym->s_scl == STATIC) {
2406 /* dubious static function at block level: %s */
2407 warning(93, dsym->s_name);
2408 dsym->s_scl = EXTERN;
2409 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2410 /* function has illegal storage class: %s */
2411 error(94, dsym->s_name);
2412 dsym->s_scl = EXTERN;
2417 * functions may be declared inline at local scope, although
2418 * this has no effect for a later definition of the same
2420 * XXX it should have an effect if tflag is set. this would
2421 * also be the way gcc behaves.
2423 if (dcs->d_inline) {
2424 if (dsym->s_type->t_tspec == FUNC) {
2427 /* variable declared inline: %s */
2428 warning(268, dsym->s_name);
2436 if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN)
2439 if (dsym->s_scl == EXTERN) {
2441 * XXX wenn die statische Variable auf Ebene 0 erst
2442 * spaeter definiert wird, haben wir die Brille auf.
2444 if (dsym->s_xsym == NULL) {
2445 outsym(dsym, EXTERN, dsym->s_def);
2447 outsym(dsym, dsym->s_xsym->s_scl, dsym->s_def);
2451 if (dcs->d_rdcsym != NULL) {
2453 if (dcs->d_rdcsym->s_blklev == 0) {
2455 switch (dsym->s_scl) {
2457 /* automatic hides external declaration: %s */
2459 warning(86, dsym->s_name);
2462 /* static hides external declaration: %s */
2464 warning(87, dsym->s_name);
2467 /* typedef hides external declaration: %s */
2469 warning(88, dsym->s_name);
2473 * Warnings and errors are printed in ledecl()
2477 LERROR("decl1loc()");
2480 } else if (dcs->d_rdcsym->s_blklev == blklev) {
2482 /* no hflag, because its illegal! */
2483 if (dcs->d_rdcsym->s_arg) {
2485 * if !tflag, a "redeclaration of %s" error
2490 /* decl. hides parameter: %s */
2491 warning(91, dsym->s_name);
2492 rmsym(dcs->d_rdcsym);
2496 } else if (dcs->d_rdcsym->s_blklev < blklev) {
2499 /* declaration hides earlier one: %s */
2500 warning(95, dsym->s_name);
2504 if (dcs->d_rdcsym->s_blklev == blklev) {
2506 /* redeclaration of %s */
2507 error(27, dsym->s_name);
2508 rmsym(dcs->d_rdcsym);
2514 if (initflg && !(initerr = chkinit(dsym))) {
2519 if (dsym->s_scl == TYPEDEF) {
2520 dsym->s_type = duptyp(dsym->s_type);
2521 dsym->s_type->t_typedef = 1;
2522 settdsym(dsym->s_type, dsym);
2526 * Before we can check the size we must wait for an initialisation
2532 * Processes (re)declarations of external Symbols inside blocks.
2540 /* look for a symbol with the same name */
2541 esym = dcs->d_rdcsym;
2542 while (esym != NULL && esym->s_blklev != 0) {
2543 while ((esym = esym->s_link) != NULL) {
2544 if (esym->s_kind != FVFT)
2546 if (strcmp(dsym->s_name, esym->s_name) == 0)
2552 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2553 /* gcc accepts this without a warning, pcc prints an error. */
2554 /* redeclaration of %s */
2555 warning(27, dsym->s_name);
2561 eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &warn);
2564 if (esym->s_scl == EXTERN) {
2565 /* inconsistent redeclaration of extern: %s */
2566 warning(90, dsym->s_name);
2569 /* inconsistent redeclaration of static: %s */
2570 warning(92, dsym->s_name);
2577 * Remember the external symbol so we can update usage
2578 * information at the end of the block.
2580 dsym->s_xsym = esym;
2585 * Print an error or a warning if the symbol can't be initialized due
2586 * to type/storage class. Return value is 1 if an error has been
2596 if (sym->s_type->t_tspec == FUNC) {
2597 /* cannot initialize function: %s */
2598 error(24, sym->s_name);
2600 } else if (sym->s_scl == TYPEDEF) {
2601 /* cannot initialize typedef: %s */
2602 error(25, sym->s_name);
2604 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2605 /* cannot initialize "extern" declaration: %s */
2606 if (dcs->d_ctx == EXTERN) {
2607 warning(26, sym->s_name);
2609 error(26, sym->s_name);
2618 * Create a symbol for an abstract declaration.
2625 if (dcs->d_ctx != ABSTRACT && dcs->d_ctx != PARG)
2628 sym = getblk(sizeof (sym_t));
2630 sym->s_name = unnamed;
2632 sym->s_scl = ABSTRACT;
2635 if (dcs->d_ctx == PARG)
2638 sym->s_type = dcs->d_type;
2639 dcs->d_rdcsym = NULL;
2646 * Removes anything which has nothing to do on global level.
2652 while (dcs->d_nxt != NULL)
2660 * remove all information about pending lint directives without
2667 * Process an abstract type declaration
2670 decl1abs(sym_t *sym)
2679 * Checks size after declarations of variables and their initialisation.
2686 * check size only for symbols which are defined and no function and
2689 if (dsym->s_def != DEF)
2691 if (dsym->s_scl == TYPEDEF)
2693 if (dsym->s_type->t_tspec == FUNC)
2696 if (length(dsym->s_type, dsym->s_name) == 0 &&
2697 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2698 /* empty array declaration: %s */
2700 warning(190, dsym->s_name);
2702 error(190, dsym->s_name);
2708 * Mark an object as set if it is not already
2716 UNIQUE_CURR_POS(sym->s_spos);
2721 * Mark an object as used if it is not already
2724 setuflg(sym_t *sym, int fcall, int szof)
2729 UNIQUE_CURR_POS(sym->s_upos);
2732 * for function calls another record is written
2734 * XXX Should symbols used in sizeof() treated as used or not?
2735 * Probably not, because there is no sense to declare an
2736 * external variable only to get their size.
2738 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2743 * Prints warnings for a list of variables and labels (concatenated
2744 * with s_dlnxt) if these are not used or only set.
2747 chkusage(dinfo_t *di)
2752 /* for this warnings LINTED has no effect */
2756 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
2757 chkusg1(di->d_asm, sym);
2763 * Prints a warning for a single variable or label if it is not used or
2767 chkusg1(int novar, sym_t *sym)
2771 if (sym->s_blklev == -1)
2774 STRUCT_ASSIGN(cpos, curr_pos);
2776 if (sym->s_kind == FVFT) {
2778 chkausg(novar, sym);
2780 chkvusg(novar, sym);
2782 } else if (sym->s_kind == FLAB) {
2784 } else if (sym->s_kind == FTAG) {
2788 STRUCT_ASSIGN(curr_pos, cpos);
2792 chkausg(int novar, sym_t *arg)
2796 LERROR("chkausg()");
2801 if (!arg->s_used && vflag) {
2802 STRUCT_ASSIGN(curr_pos, arg->s_dpos);
2803 /* argument %s unused in function %s */
2804 warning(231, arg->s_name, funcsym->s_name);
2809 chkvusg(int novar, sym_t *sym)
2814 if (blklev == 0 || sym->s_blklev == 0)
2815 LERROR("chkvusg()");
2817 /* errors in expressions easily cause lots of these warnings */
2822 * XXX Only variables are checkd, although types should
2823 * probably also be checked
2825 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
2826 sc != AUTO && sc != REG) {
2834 if (!sym->s_used && !sym->s_set) {
2835 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2836 /* %s unused in function %s */
2837 warning(192, sym->s_name, funcsym->s_name);
2840 if (sym->s_set && !sym->s_used) {
2841 STRUCT_ASSIGN(curr_pos, sym->s_spos);
2842 /* %s set but not used in function %s */
2843 warning(191, sym->s_name, funcsym->s_name);
2844 } else if (!sym->s_used) {
2845 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2846 /* %s unused in function %s */
2847 warning(192, sym->s_name, funcsym->s_name);
2853 * information about usage is taken over into the symbol
2854 * tabel entry at level 0 if the symbol was locally declared
2855 * as an external symbol.
2857 * XXX This is wrong for symbols declared static at level 0
2858 * if the usage information stems from sizeof(). This is
2859 * because symbols at level 0 only used in sizeof() are
2860 * considered to not be used.
2862 if ((xsym = sym->s_xsym) != NULL) {
2863 if (sym->s_used && !xsym->s_used) {
2865 STRUCT_ASSIGN(xsym->s_upos, sym->s_upos);
2867 if (sym->s_set && !xsym->s_set) {
2869 STRUCT_ASSIGN(xsym->s_spos, sym->s_spos);
2879 if (blklev != 1 || lab->s_blklev != 1)
2880 LERROR("chklusg()");
2882 if (lab->s_set && !lab->s_used) {
2883 STRUCT_ASSIGN(curr_pos, lab->s_spos);
2884 /* label %s unused in function %s */
2885 warning(192, lab->s_name, funcsym->s_name);
2886 } else if (!lab->s_set) {
2887 STRUCT_ASSIGN(curr_pos, lab->s_upos);
2888 /* undefined label %s */
2889 warning(23, lab->s_name);
2897 if (!incompl(sym->s_type))
2900 /* complain always about incomplete tags declared inside blocks */
2901 if (!zflag || dcs->d_ctx != EXTERN)
2904 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2905 switch (sym->s_type->t_tspec) {
2907 /* struct %s never defined */
2908 warning(233, sym->s_name);
2911 /* union %s never defined */
2912 warning(234, sym->s_name);
2915 /* enum %s never defined */
2916 warning(235, sym->s_name);
2919 LERROR("chktusg()");
2924 * Called after the entire translation unit has been parsed.
2925 * Changes tentative definitions in definitions.
2926 * Performs some tests on global Symbols. Detected Problems are:
2927 * - defined variables of incomplete type
2928 * - constant variables which are not initialized
2929 * - static symbols which are never used
2937 if (blklev != 0 || dcs->d_nxt != NULL)
2940 STRUCT_ASSIGN(cpos, curr_pos);
2942 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
2943 if (sym->s_blklev == -1)
2945 if (sym->s_kind == FVFT) {
2947 } else if (sym->s_kind == FTAG) {
2950 if (sym->s_kind != FMOS)
2951 LERROR("chkglsyms()");
2955 STRUCT_ASSIGN(curr_pos, cpos);
2959 chkglvar(sym_t *sym)
2962 if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON)
2965 if (sym->s_scl != EXTERN && sym->s_scl != STATIC)
2966 LERROR("chkglvar()");
2970 if (sym->s_scl == STATIC) {
2971 if (sym->s_type->t_tspec == FUNC) {
2972 if (sym->s_used && sym->s_def != DEF) {
2973 STRUCT_ASSIGN(curr_pos, sym->s_upos);
2974 /* static func. called but not def.. */
2975 error(225, sym->s_name);
2979 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2980 if (sym->s_type->t_tspec == FUNC) {
2981 if (sym->s_def == DEF) {
2983 /* static function %s unused */
2984 warning(236, sym->s_name);
2986 /* static function %s decl. but ... */
2987 warning(290, sym->s_name);
2989 } else if (!sym->s_set) {
2990 /* static variable %s unused */
2991 warning(226, sym->s_name);
2993 /* static variable %s set but not used */
2994 warning(307, sym->s_name);
2997 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
2998 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2999 /* const object %s should have initializer */
3000 warning(227, sym->s_name);
3009 if (sym->s_def == TDEF) {
3010 if (sym->s_type->t_tspec == FUNC)
3012 * this can happen if a syntax error occurred
3013 * after a function declaration
3016 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3017 if (length(sym->s_type, sym->s_name) == 0 &&
3018 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3019 /* empty array declaration: %s */
3020 if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3021 warning(190, sym->s_name);
3023 error(190, sym->s_name);
3030 * Prints information about location of previous definition/declaration.
3033 prevdecl(int msg, sym_t *psym)
3040 STRUCT_ASSIGN(cpos, curr_pos);
3041 STRUCT_ASSIGN(curr_pos, psym->s_dpos);
3043 message(msg, psym->s_name);
3044 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3045 /* previous definition of %s */
3046 message(261, psym->s_name);
3048 /* previous declaration of %s */
3049 message(260, psym->s_name);
3051 STRUCT_ASSIGN(curr_pos, cpos);