1 /* $NetBSD: decl.c,v 1.29 2002/01/18 21:01:39 thorpej 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.29 2002/01/18 21:01:39 thorpej 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 do 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() 1");
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)gnuism(265, tflag ? "traditional" : "ANSI");
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 /* remeber 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) */
419 case NTSPEC: /* this value unused */
423 /* Anything other is not accepted. */
430 * Remember the symbol of a typedef name (2nd arg) in a struct, union
431 * or enum tag if the typedef name is the first defined for this tag.
433 * If the tag is unnamed, the typdef name is used for identification
434 * of this tag in lint2. Although its possible that more than one typedef
435 * name is defined for one tag, the first name defined should be unique
436 * if the tag is unnamed.
439 settdsym(type_t *tp, sym_t *sym)
443 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
444 if (tp->t_str->stdef == NULL)
445 tp->t_str->stdef = sym;
446 } else if (t == ENUM) {
447 if (tp->t_enum->etdef == NULL)
448 tp->t_enum->etdef = sym;
453 * Remember a qualifier which is part of the declaration specifiers
454 * (and not the declarator) in the top element of the declaration stack.
455 * Also detect multiple qualifiers of the same kind.
457 * The remembered qualifier is used by deftyp() to construct the type
458 * for all declarators.
467 warning(10, "const");
472 lerror("addqual() 1");
473 if (dcs->d_volatile) {
475 warning(10, "volatile");
482 * Go to the next declaration level (structs, nested structs, blocks,
483 * argument declaration lists ...)
491 (void)printf("pushdecl(%d)\n", (int)sc);
493 /* put a new element on the declaration stack */
494 if ((di = calloc(1, sizeof (dinfo_t))) == NULL)
499 di->d_ldlsym = &di->d_dlsyms;
503 * Go back to previous declaration level
511 (void)printf("popdecl(%d)\n", (int)dcs->d_ctx);
513 if (dcs->d_nxt == NULL)
514 lerror("popdecl() 1");
519 /* there is nothing after external declarations */
520 lerror("popdecl() 2");
526 * Symbols declared in (nested) structs or enums are
527 * part of the next level (they are removed from the
528 * symbol table if the symbols of the outher level are
531 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
532 dcs->d_ldlsym = di->d_ldlsym;
536 * All symbols in dcs->d_dlsyms are introduced in old style
537 * argument declarations (it's not clean, but possible).
538 * They are appended to the list of symbols declared in
539 * an old style argument identifier list or a new style
540 * parameter type list.
542 if (di->d_dlsyms != NULL) {
543 *di->d_ldlsym = dcs->d_fpsyms;
544 dcs->d_fpsyms = di->d_dlsyms;
550 * Append all symbols declared in the abstract declaration
551 * to the list of symbols declared in the surounding decl.
553 * XXX I'm not sure whether they should be removed from the
554 * symbol table now or later.
556 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
557 dcs->d_ldlsym = di->d_ldlsym;
560 /* check usage of local vars */
564 /* usage of arguments will be checked by funcend() */
565 rmsyms(di->d_dlsyms);
568 lerror("popdecl() 3");
574 * Set flag d_asm in all declaration stack elements up to the
577 * This is used to mark compound statements which have, possibly in
578 * nested compound statements, asm statements. For these compound
579 * statements no warnings about unused or unitialized variables are
582 * There is no need to clear d_asm in dinfo structs with context AUTO,
583 * because these structs are freed at the end of the compound statement.
584 * But it must be cleard in the outermost dinfo struct, which has
585 * context EXTERN. This could be done in clrtyp() and would work for
586 * C, but not for C++ (due to mixed statements and declarations). Thus
587 * we clear it in glclup(), which is used to do some cleanup after
588 * global declarations/definitions.
595 for (di = dcs; di != NULL; di = di->d_nxt)
600 * Clean all elements of the top element of declaration stack which
601 * will be used by the next declaration
607 dcs->d_atyp = dcs->d_smod = dcs->d_lmod = NOTSPEC;
610 dcs->d_const = dcs->d_volatile = 0;
612 dcs->d_mscl = dcs->d_terr = 0;
618 * Create a type structure from the informations gathered in
619 * the declaration stack.
620 * Complain about storage classes which are not possible in current
630 t = dcs->d_atyp; /* CHAR, INT, FLOAT, DOUBLE, VOID */
631 s = dcs->d_smod; /* SIGNED, UNSIGNED */
632 l = dcs->d_lmod; /* SHORT, LONG, QUAD */
636 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && tp == NULL)
639 if (tp != NULL && (t != NOTSPEC || s != NOTSPEC || l != NOTSPEC)) {
640 /* should never happen */
641 lerror("deftyp() 1");
664 /* use 'double' instead of ... */
673 /* 'long double' is illegal in ... */
680 lerror("deftyp() 2");
682 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) {
688 dcs->d_type = gettyp(mrgtspec(t, s));
692 /* only one storage class allowed */
696 /* illegal type combination */
700 if (dcs->d_ctx == EXTERN) {
701 if (scl == REG || scl == AUTO) {
702 /* illegal storage class */
706 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PARG) {
707 if (scl != NOSCL && scl != REG) {
708 /* only "register" valid ... */
716 if (dcs->d_const && dcs->d_type->t_const) {
717 if (!dcs->d_type->t_typedef)
718 lerror("deftyp() 3");
719 /* typedef already qualified with "%s" */
720 warning(68, "const");
722 if (dcs->d_volatile && dcs->d_type->t_volatile) {
723 if (!dcs->d_type->t_typedef)
724 lerror("deftyp() 4");
725 /* typedef already qualified with "%s" */
726 warning(68, "volatile");
729 if (dcs->d_const || dcs->d_volatile) {
730 dcs->d_type = duptyp(dcs->d_type);
731 dcs->d_type->t_const |= dcs->d_const;
732 dcs->d_type->t_volatile |= dcs->d_volatile;
737 * Merge type specifiers (char, ..., long long, signed, unsigned).
740 mrgtspec(tspec_t t, tspec_t s)
743 if (s == SIGNED || s == UNSIGN) {
745 t = s == SIGNED ? SCHAR : UCHAR;
746 } else if (t == SHORT) {
747 t = s == SIGNED ? SHORT : USHORT;
748 } else if (t == INT) {
749 t = s == SIGNED ? INT : UINT;
750 } else if (t == LONG) {
751 t = s == SIGNED ? LONG : ULONG;
752 } else if (t == QUAD) {
753 t = s == SIGNED ? QUAD : UQUAD;
761 * Return the length of a type in bit.
763 * Printing a message if the outhermost dimension of an array is 0 must
764 * be done by the caller. All other problems are reported by length()
765 * if name is not NULL.
768 length(type_t *tp, const char *name)
773 while (tp && tp->t_tspec == ARRAY) {
780 switch (tp->t_tspec) {
782 /* compiler takes size of function */
783 lerror("%s", msgs[12]);
787 if (incompl(tp) && name != NULL) {
788 /* incomplete structure or union %s: %s */
789 error(31, tp->t_str->stag->s_name, name);
791 elsz = tp->t_str->size;
794 if (incompl(tp) && name != NULL) {
795 /* incomplete enum type: %s */
800 elsz = size(tp->t_tspec);
805 return (elem * elsz);
809 * Get the alignment of the given Type in bits.
817 while (tp && tp->t_tspec == ARRAY)
823 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
824 a = tp->t_str->align;
825 } else if (t == FUNC) {
826 /* compiler takes alignment of function */
828 a = ALIGN(1) * CHAR_BIT;
830 if ((a = size(t)) == 0) {
832 } else if (a > ALIGN(1) * CHAR_BIT) {
833 a = ALIGN(1) * CHAR_BIT;
836 if (a < CHAR_BIT || a > ALIGN(1) * CHAR_BIT)
837 lerror("getbound() 1");
842 * Concatenate two lists of symbols by s_nxt. Used by declarations of
843 * struct/union/enum elements and parameters.
846 lnklst(sym_t *l1, sym_t *l2)
850 if ((l = l1) == NULL)
852 while (l1->s_nxt != NULL)
859 * Check if the type of the given symbol is valid and print an error
860 * message if it is not.
863 * - arrays of incomlete types or functions
864 * - functions returning arrays or functions
865 * - void types other than type of function or pointer
875 while ((tp = *tpp) != NULL) {
878 * If this is the type of an old style function definition,
879 * a better warning is printed in funcdef().
881 if (t == FUNC && !tp->t_proto &&
882 !(to == NOTSPEC && sym->s_osdef)) {
884 /* function declaration is not a prototype */
888 if (t == FUNC || t == ARRAY) {
889 /* function returns illegal type */
892 *tpp = incref(*tpp, PTR);
894 *tpp = incref((*tpp)->t_subt, PTR);
897 } else if (tp->t_const || tp->t_volatile) {
898 if (sflag) { /* XXX oder better !tflag ? */
899 /* function cannot return const... */
905 /* array of function is illegal */
909 } else if (t == ARRAY && tp->t_dim == 0) {
913 } else if (t == VOID) {
914 /* illegal use of void */
917 #if 0 /* errors are produced by length() */
918 } else if (incompl(tp)) {
919 /* array of incomplete type */
927 } else if (to == NOTSPEC && t == VOID) {
928 if (dcs->d_ctx == PARG) {
929 if (sym->s_scl != ABSTRACT) {
930 if (sym->s_name == unnamed)
932 /* void param cannot have name: %s */
933 error(61, sym->s_name);
936 } else if (dcs->d_ctx == ABSTRACT) {
938 } else if (sym->s_scl != TYPEDEF) {
939 /* void type for %s */
940 error(19, sym->s_name);
944 if (t == VOID && to != PTR) {
945 if (tp->t_const || tp->t_volatile) {
946 /* inappropriate qualifiers with "void" */
948 tp->t_const = tp->t_volatile = 0;
957 * Process the declarator of a struct/union element.
960 decl1str(sym_t *dsym)
965 int o = 0; /* Appease gcc */
968 if ((sc = dsym->s_scl) != MOS && sc != MOU)
969 lerror("decl1str() 1");
971 if (dcs->d_rdcsym != NULL) {
972 if ((sc = dcs->d_rdcsym->s_scl) != MOS && sc != MOU)
973 /* should be ensured by storesym() */
974 lerror("decl1str() 2");
975 if (dsym->s_styp == dcs->d_rdcsym->s_styp) {
976 /* duplicate member name: %s */
977 error(33, dsym->s_name);
978 rmsym(dcs->d_rdcsym);
984 t = (tp = dsym->s_type)->t_tspec;
990 * only unsigned und signed int are protable bit-field types
991 *(at least in ANSI C, in traditional C only unsigned int)
993 if (t == CHAR || t == UCHAR || t == SCHAR ||
994 t == SHORT || t == USHORT || t == ENUM) {
995 if (bitfieldtype_ok == 0) {
998 * bit-field type '%s' invalid in
1001 warning(273, tyname(tp));
1003 /* nonportable bit-field type */
1007 } else if (t == INT && dcs->d_smod == NOTSPEC) {
1008 if (pflag && bitfieldtype_ok == 0) {
1009 /* nonportable bit-field type */
1012 } else if (t != INT && t != UINT) {
1014 * Non-integer types are always illegal for
1015 * bitfields, regardless of BITFIELDTYPE.
1016 * Integer types not dealt with above are
1017 * okay only if BITFIELDTYPE is in effect.
1019 if (bitfieldtype_ok == 0 || isityp(t) == 0) {
1020 /* illegal bit-field type */
1023 dsym->s_type = tp = duptyp(gettyp(t = INT));
1024 if ((tp->t_flen = sz) > size(t))
1025 tp->t_flen = size(t);
1028 if ((len = tp->t_flen) < 0 || len > size(t)) {
1029 /* illegal bit-field size */
1031 tp->t_flen = size(t);
1032 } else if (len == 0 && dsym->s_name != unnamed) {
1033 /* zero size bit-field */
1035 tp->t_flen = size(t);
1037 if (dsym->s_scl == MOU) {
1038 /* illegal use of bit-field */
1040 dsym->s_type->t_isfield = 0;
1043 } else if (t == FUNC) {
1044 /* function illegal in structure or union */
1046 dsym->s_type = tp = incref(tp, t = PTR);
1050 * bit-fields of length 0 are not warned about because length()
1051 * does not return the length of the bit-field but the length
1052 * of the type the bit-field is packed in (its ok)
1054 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1055 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1056 /* illegal zero sized structure member: %s */
1057 warning(39, dsym->s_name);
1061 if (dcs->d_ctx == MOU) {
1065 if (dsym->s_field) {
1066 align(getbound(tp), tp->t_flen);
1067 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t);
1068 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1069 dcs->d_offset += tp->t_flen;
1071 align(getbound(tp), 0);
1072 dsym->s_value.v_quad = dcs->d_offset;
1073 dcs->d_offset += sz;
1075 if (dcs->d_ctx == MOU) {
1076 if (o > dcs->d_offset)
1083 * Clear the BITFIELDTYPE indicator after processing each
1084 * structure element.
1086 bitfieldtype_ok = 0;
1092 * Aligns next structure element as required.
1094 * al contains the required alignment, len the length of a bit-field.
1097 align(int al, int len)
1102 * The alignment of the current element becomes the alignment of
1103 * the struct/union if it is larger than the current alignment
1104 * of the struct/union.
1106 if (al > dcs->d_stralign)
1107 dcs->d_stralign = al;
1109 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1110 if (len == 0 || dcs->d_offset + len > no)
1115 * Remember the width of the field in its type structure.
1118 bitfield(sym_t *dsym, int len)
1122 dsym = getblk(sizeof (sym_t));
1123 dsym->s_name = unnamed;
1124 dsym->s_kind = FMOS;
1126 dsym->s_type = gettyp(UINT);
1127 dsym->s_blklev = -1;
1129 dsym->s_type = duptyp(dsym->s_type);
1130 dsym->s_type->t_isfield = 1;
1131 dsym->s_type->t_flen = len;
1137 * Collect informations about a sequence of asterisks and qualifiers
1138 * in a list of type pqinf_t.
1139 * Qualifiers refer always to the left asterisk. The rightmost asterisk
1140 * will be at the top of the list.
1143 mergepq(pqinf_t *p1, pqinf_t *p2)
1147 if (p2->p_pcnt != 0) {
1148 /* left '*' at the end of the list */
1149 for (p = p2; p->p_nxt != NULL; p = p->p_nxt)
1157 warning(10, "const");
1161 if (p2->p_volatile) {
1162 if (p1->p_volatile) {
1164 warning(10, "volatile");
1174 * Followint 3 functions extend the type of a declarator with
1175 * pointer, function and array types.
1177 * The current type is the Type built by deftyp() (dcs->d_type) and
1178 * pointer, function and array types already added for this
1179 * declarator. The new type extension is inserted between both.
1182 addptr(sym_t *decl, pqinf_t *pi)
1187 tpp = &decl->s_type;
1188 while (*tpp && *tpp != dcs->d_type)
1189 tpp = &(*tpp)->t_subt;
1193 while (pi != NULL) {
1194 *tpp = tp = getblk(sizeof (type_t));
1196 tp->t_const = pi->p_const;
1197 tp->t_volatile = pi->p_volatile;
1198 *(tpp = &tp->t_subt) = dcs->d_type;
1207 * If a dimension was specified, dim is 1, otherwise 0
1208 * n is the specified dimension
1211 addarray(sym_t *decl, int dim, int n)
1215 tpp = &decl->s_type;
1216 while (*tpp && *tpp != dcs->d_type)
1217 tpp = &(*tpp)->t_subt;
1221 *tpp = tp = getblk(sizeof (type_t));
1222 tp->t_tspec = ARRAY;
1223 tp->t_subt = dcs->d_type;
1227 /* zero or negative array dimension */
1230 } else if (n == 0 && dim) {
1231 /* zero or negative array dimension */
1233 } else if (n == 0 && !dim) {
1234 /* is incomplete type */
1242 addfunc(sym_t *decl, sym_t *args)
1248 /* function prototypes are illegal in traditional C */
1250 args = nsfunc(decl, args);
1256 * The symbols are removed from the symbol table by popdecl() after
1257 * addfunc(). To be able to restore them if this is a function
1258 * definition, a pointer to the list of all symbols is stored in
1259 * dcs->d_nxt->d_fpsyms. Also a list of the arguments (concatenated
1260 * by s_nxt) is stored in dcs->d_nxt->d_fargs.
1261 * (dcs->d_nxt must be used because *dcs is the declaration stack
1262 * element created for the list of params and is removed after
1265 if (dcs->d_nxt->d_ctx == EXTERN &&
1266 decl->s_type == dcs->d_nxt->d_type) {
1267 dcs->d_nxt->d_fpsyms = dcs->d_dlsyms;
1268 dcs->d_nxt->d_fargs = args;
1271 tpp = &decl->s_type;
1272 while (*tpp && *tpp != dcs->d_nxt->d_type)
1273 tpp = &(*tpp)->t_subt;
1277 *tpp = tp = getblk(sizeof (type_t));
1279 tp->t_subt = dcs->d_nxt->d_type;
1280 if ((tp->t_proto = dcs->d_proto) != 0)
1282 tp->t_vararg = dcs->d_vararg;
1288 * Called for new style function declarations.
1292 nsfunc(sym_t *decl, sym_t *args)
1299 * Declarations of structs/unions/enums in param lists are legal,
1302 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1304 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) {
1305 /* dubious tag declaration: %s %s */
1306 warning(85, scltoa(sc), sym->s_name);
1311 for (arg = args; arg != NULL; arg = arg->s_nxt) {
1312 if (arg->s_type->t_tspec == VOID) {
1313 if (n > 1 || arg->s_nxt != NULL) {
1314 /* "void" must be sole parameter */
1316 arg->s_type = gettyp(INT);
1322 /* return NULL if first param is VOID */
1323 return (args != NULL && args->s_type->t_tspec != VOID ? args : NULL);
1327 * Called for old style function declarations.
1330 osfunc(sym_t *decl, sym_t *args)
1334 * Remember list of params only if this is really seams to be
1335 * a function definition.
1337 if (dcs->d_nxt->d_ctx == EXTERN &&
1338 decl->s_type == dcs->d_nxt->d_type) {
1340 * We assume that this becomes a function definition. If
1341 * we are wrong, its corrected in chkfdef().
1345 decl->s_args = args;
1349 /* function prototype parameters must have types */
1355 * Lists of Identifiers in functions declarations are allowed only if
1356 * its also a function definition. If this is not the case, print a
1360 chkfdef(sym_t *sym, int msg)
1365 /* incomplete or misplaced function definition */
1374 * Process the name in a declarator.
1375 * If the symbol does already exists, a new one is created.
1376 * The symbol becomes one of the storage classes EXTERN, STATIC, AUTO or
1378 * s_def and s_reg are valid after dname().
1385 if (sym->s_scl == NOSCL) {
1386 dcs->d_rdcsym = NULL;
1387 } else if (sym->s_defarg) {
1389 dcs->d_rdcsym = NULL;
1391 dcs->d_rdcsym = sym;
1392 sym = pushdown(sym);
1395 switch (dcs->d_ctx) {
1399 sym->s_styp = dcs->d_tagtyp->t_str;
1401 sym->s_value.v_tspec = INT;
1406 * static and external symbols without "extern" are
1407 * considered to be tentative defined, external
1408 * symbols with "extern" are declared, and typedef names
1409 * are defined. Tentative defined and declared symbols
1410 * may become defined if an initializer is present or
1411 * this is a function definition.
1413 if ((sc = dcs->d_scl) == NOSCL) {
1416 } else if (sc == STATIC) {
1418 } else if (sc == TYPEDEF) {
1420 } else if (sc == EXTERN) {
1423 lerror("dname() 1");
1430 if ((sc = dcs->d_scl) == NOSCL) {
1432 } else if (sc == REG) {
1436 lerror("dname() 2");
1441 if ((sc = dcs->d_scl) == NOSCL) {
1443 * XXX somewhat ugly because we dont know whether
1444 * this is AUTO or EXTERN (functions). If we are
1445 * wrong it must be corrected in decl1loc(), where
1446 * we have the necessary type information.
1450 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1452 } else if (sc == REG) {
1456 } else if (sc == EXTERN) {
1459 lerror("dname() 3");
1463 lerror("dname() 4");
1467 sym->s_type = dcs->d_type;
1469 dcs->d_fpsyms = NULL;
1475 * Process a name in the list of formal params in an old style function
1482 if (sym->s_scl != NOSCL) {
1483 if (blklev == sym->s_blklev) {
1484 /* redeclaration of formal parameter %s */
1485 error(21, sym->s_name);
1489 sym = pushdown(sym);
1491 sym->s_type = gettyp(INT);
1494 sym->s_defarg = sym->s_arg = 1;
1499 * Create the type of a tag.
1501 * tag points to the symbol table entry of the tag
1502 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1503 * decl is 1 if the type of the tag will be completed in this declaration
1504 * (the following token is T_LBRACE)
1505 * semi is 1 if the following token is T_SEMI
1508 mktag(sym_t *tag, tspec_t kind, int decl, int semi)
1513 if (kind == STRUCT) {
1515 } else if (kind == UNION) {
1517 } else if (kind == ENUM) {
1524 if (tag->s_scl != NOSCL) {
1525 tag = newtag(tag, scl, decl, semi);
1527 /* a new tag, no empty declaration */
1528 dcs->d_nxt->d_nedecl = 1;
1529 if (scl == ENUMTAG && !decl) {
1530 if (!tflag && (sflag || pflag))
1531 /* forward reference to enum type */
1535 if (tag->s_scl == NOSCL) {
1537 tag->s_type = tp = getblk(sizeof (type_t));
1542 tag = getblk(sizeof (sym_t));
1543 tag->s_name = unnamed;
1544 UNIQUE_CURR_POS(tag->s_dpos);
1548 tag->s_type = tp = getblk(sizeof (type_t));
1549 dcs->d_nxt->d_nedecl = 1;
1552 if (tp->t_tspec == NOTSPEC) {
1555 tp->t_str = getblk(sizeof (str_t));
1556 tp->t_str->align = CHAR_BIT;
1557 tp->t_str->stag = tag;
1560 tp->t_enum = getblk(sizeof (enum_t));
1561 tp->t_enum->etag = tag;
1563 /* ist unvollstaendiger Typ */
1571 * Checks all possible cases of tag redeclarations.
1572 * decl is 1 if T_LBRACE follows
1573 * semi is 1 if T_SEMI follows
1576 newtag(sym_t *tag, scl_t scl, int decl, int semi)
1579 if (tag->s_blklev < blklev) {
1584 /* decl. introduces new type ... */
1585 warning(44, scltoa(scl), tag->s_name);
1586 tag = pushdown(tag);
1587 } else if (tag->s_scl != scl) {
1588 /* base type is really "%s %s" */
1589 warning(45, scltoa(tag->s_scl), tag->s_name);
1591 dcs->d_nxt->d_nedecl = 1;
1593 /* "struct a { ... } " */
1595 /* redefinition hides earlier one: %s */
1596 warning(43, tag->s_name);
1597 tag = pushdown(tag);
1598 dcs->d_nxt->d_nedecl = 1;
1599 } else if (tag->s_scl != scl) {
1600 /* base type is really "%s %s" */
1601 warning(45, scltoa(tag->s_scl), tag->s_name);
1602 /* declaration introduces new type in ANSI C: %s %s */
1604 warning(44, scltoa(scl), tag->s_name);
1605 tag = pushdown(tag);
1606 dcs->d_nxt->d_nedecl = 1;
1609 if (tag->s_scl != scl) {
1610 /* (%s) tag redeclared */
1611 error(46, scltoa(tag->s_scl));
1613 tag = pushdown(tag);
1614 dcs->d_nxt->d_nedecl = 1;
1615 } else if (decl && !incompl(tag->s_type)) {
1616 /* (%s) tag redeclared */
1617 error(46, scltoa(tag->s_scl));
1619 tag = pushdown(tag);
1620 dcs->d_nxt->d_nedecl = 1;
1621 } else if (semi || decl) {
1622 dcs->d_nxt->d_nedecl = 1;
1634 case EXTERN: s = "extern"; break;
1635 case STATIC: s = "static"; break;
1636 case AUTO: s = "auto"; break;
1637 case REG: s = "register"; break;
1638 case TYPEDEF: s = "typedef"; break;
1639 case STRTAG: s = "struct"; break;
1640 case UNIONTAG: s = "union"; break;
1641 case ENUMTAG: s = "enum"; break;
1642 default: lerror("tagttoa()");
1648 * Completes the type of a tag in a struct/union/enum declaration.
1649 * tp points to the type of the, tag, fmem to the list of members/enums.
1652 compltag(type_t *tp, sym_t *fmem)
1659 /* from now a complete type */
1662 if ((t = tp->t_tspec) != ENUM) {
1663 align(dcs->d_stralign, 0);
1665 sp->align = dcs->d_stralign;
1666 sp->size = dcs->d_offset;
1668 if (sp->size == 0) {
1670 (void)gnuism(47, ttab[t].tt_name);
1673 for (mem = fmem; mem != NULL; mem = mem->s_nxt) {
1674 if (mem->s_name != unnamed)
1678 /* %s has no named members */
1680 t == STRUCT ? "structure" : "union");
1684 tp->t_enum->elem = fmem;
1690 * Processes the name of an enumerator in en enum declaration.
1692 * sym points to the enumerator
1693 * val is the value of the enumerator
1694 * impl is 1 if the value of the enumerator was not explicit specified.
1697 ename(sym_t *sym, int val, int impl)
1701 if (sym->s_blklev == blklev) {
1702 /* no hflag, because this is illegal!!! */
1704 /* enumeration constant hides parameter: %s */
1705 warning(57, sym->s_name);
1707 /* redeclaration of %s */
1708 error(27, sym->s_name);
1710 * inside blocks it should not too complicated
1711 * to find the position of the previous
1719 /* redefinition hides earlier one: %s */
1720 warning(43, sym->s_name);
1722 sym = pushdown(sym);
1724 sym->s_scl = ENUMCON;
1725 sym->s_type = dcs->d_tagtyp;
1726 sym->s_value.v_tspec = INT;
1727 sym->s_value.v_quad = val;
1728 if (impl && val - 1 == INT_MAX) {
1729 /* overflow in enumeration values: %s */
1730 warning(48, sym->s_name);
1737 * Process a single external declarator.
1740 decl1ext(sym_t *dsym, int initflg)
1742 int warn, rval, redec;
1749 if (initflg && !(initerr = chkinit(dsym)))
1753 * Declarations of functions are marked as "tentative" in dname().
1754 * This is wrong because there are no tentative function
1757 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1760 if (dcs->d_inline) {
1761 if (dsym->s_type->t_tspec == FUNC) {
1764 /* variable declared inline: %s */
1765 warning(268, dsym->s_name);
1769 /* Write the declaration into the output file */
1770 if (plibflg && llibflg &&
1771 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1773 * With both LINTLIBRARY and PROTOLIB the prototyp is
1774 * written as a function definition to the output file.
1776 rval = dsym->s_type->t_subt->t_tspec != VOID;
1777 outfdef(dsym, &dsym->s_dpos, rval, 0, NULL);
1779 outsym(dsym, dsym->s_scl, dsym->s_def);
1782 if ((rdsym = dcs->d_rdcsym) != NULL) {
1785 * If the old symbol stems from an old style function definition
1786 * we have remembered the params in rdsmy->s_args and compare
1787 * them with the params of the prototype.
1789 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1790 redec = chkosdef(rdsym, dsym);
1795 if (!redec && !isredec(dsym, (warn = 0, &warn))) {
1798 /* redeclaration of %s */
1799 (*(sflag ? error : warning))(27, dsym->s_name);
1800 prevdecl(-1, rdsym);
1804 * Overtake the rememberd params if the new symbol
1805 * is not a prototype.
1807 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1808 dsym->s_osdef = rdsym->s_osdef;
1809 dsym->s_args = rdsym->s_args;
1810 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1814 * Remember the position of the declaration if the
1815 * old symbol was a prototype and the new is not.
1816 * Also remember the position if the old symbol
1817 * was defined and the new is not.
1819 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1820 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1821 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1822 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1826 * Copy informations about usage of the name into
1829 cpuinfo(dsym, rdsym);
1831 /* Once a name is defined, it remains defined. */
1832 if (rdsym->s_def == DEF)
1835 /* once a function is inline, it remains inline */
1836 if (rdsym->s_inline)
1839 compltyp(dsym, rdsym);
1846 if (dsym->s_scl == TYPEDEF) {
1847 dsym->s_type = duptyp(dsym->s_type);
1848 dsym->s_type->t_typedef = 1;
1849 settdsym(dsym->s_type, dsym);
1855 * Copies informations about usage into a new symbol table entry of
1859 cpuinfo(sym_t *sym, sym_t *rdsym)
1862 sym->s_spos = rdsym->s_spos;
1863 sym->s_upos = rdsym->s_upos;
1864 sym->s_set = rdsym->s_set;
1865 sym->s_used = rdsym->s_used;
1869 * Prints an error and returns 1 if a symbol is redeclared/redefined.
1870 * Otherwise returns 0 and, in some cases of minor problems, prints
1874 isredec(sym_t *dsym, int *warn)
1878 if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
1879 /* redeclaration of %s */
1880 error(27, dsym->s_name);
1884 if (rsym->s_scl == TYPEDEF) {
1885 /* typedef redeclared: %s */
1886 error(89, dsym->s_name);
1890 if (dsym->s_scl == TYPEDEF) {
1891 /* redeclaration of %s */
1892 error(27, dsym->s_name);
1896 if (rsym->s_def == DEF && dsym->s_def == DEF) {
1897 /* redefinition of %s */
1898 error(28, dsym->s_name);
1902 if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, warn)) {
1903 /* redeclaration of %s */
1904 error(27, dsym->s_name);
1908 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
1910 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
1912 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
1914 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
1916 * All cases except "int a = 1; static int a;" are catched
1917 * above with or without a warning
1919 /* redeclaration of %s */
1920 error(27, dsym->s_name);
1924 if (rsym->s_scl == EXTERN) {
1925 /* previously declared extern, becomes static: %s */
1926 warning(29, dsym->s_name);
1932 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
1934 /* redeclaration of %s; ANSI C requires "static" */
1936 warning(30, dsym->s_name);
1939 dsym->s_scl = STATIC;
1944 * Checks if two types are compatible. Returns 0 if not, otherwise 1.
1946 * ignqual ignore qualifiers of type; used for function params
1947 * promot promote left type; used for comparison of params of
1948 * old style function definitions with params of prototypes.
1949 * *warn set to 1 if an old style function declaration is not
1950 * compatible with a prototype
1953 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *warn)
1957 while (tp1 != NULL && tp2 != NULL) {
1963 } else if (t == CHAR || t == SCHAR) {
1965 } else if (t == UCHAR) {
1966 t = tflag ? UINT : INT;
1967 } else if (t == SHORT) {
1969 } else if (t == USHORT) {
1971 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
1975 if (t != tp2->t_tspec)
1978 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
1981 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
1984 if (t == STRUCT || t == UNION)
1985 return (tp1->t_str == tp2->t_str);
1987 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
1988 if (tp1->t_dim != 0 && tp2->t_dim != 0)
1992 /* dont check prototypes for traditional */
1993 if (t == FUNC && !tflag) {
1994 if (tp1->t_proto && tp2->t_proto) {
1995 if (!eqargs(tp1, tp2, warn))
1997 } else if (tp1->t_proto) {
1998 if (!mnoarg(tp1, warn))
2000 } else if (tp2->t_proto) {
2001 if (!mnoarg(tp2, warn))
2008 ignqual = promot = 0;
2012 return (tp1 == tp2);
2016 * Compares the parameter types of two prototypes.
2019 eqargs(type_t *tp1, type_t *tp2, int *warn)
2023 if (tp1->t_vararg != tp2->t_vararg)
2029 while (a1 != NULL && a2 != NULL) {
2031 if (eqtype(a1->s_type, a2->s_type, 1, 0, warn) == 0)
2043 * mnoarg() (matches functions with no argument type information)
2044 * returns 1 if all parameters of a prototype are compatible with
2045 * and old style function declaration.
2046 * This is the case if following conditions are met:
2047 * 1. the prototype must have a fixed number of parameters
2048 * 2. no parameter is of type float
2049 * 3. no parameter is converted to another type if integer promotion
2053 mnoarg(type_t *tp, int *warn)
2062 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) {
2063 if ((t = arg->s_type->t_tspec) == FLOAT ||
2064 t == CHAR || t == SCHAR || t == UCHAR ||
2065 t == SHORT || t == USHORT) {
2074 * Compares a prototype declaration with the remembered arguments of
2075 * a previous old style function definition.
2078 chkosdef(sym_t *rdsym, sym_t *dsym)
2080 sym_t *args, *pargs, *arg, *parg;
2084 args = rdsym->s_args;
2085 pargs = dsym->s_type->t_args;
2090 for (arg = args; arg != NULL; arg = arg->s_nxt)
2092 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2094 if (narg != nparg) {
2095 /* prototype does not match old-style definition */
2107 * If it does not match due to promotion and sflag is
2108 * not set we print only a warning.
2110 if (!eqtype(arg->s_type, parg->s_type, 1, 1, &warn) || warn) {
2111 /* prototype does not match old-style def., arg #%d */
2122 /* old style definition */
2123 prevdecl(300, rdsym);
2129 * Complets a type by copying the dimension and prototype information
2130 * from a second compatible type.
2132 * Following lines are legal:
2133 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2134 * "typedef ft(); ft f; f(int); ft g; g(long);"
2135 * This means that, if a type is completed, the type structure must
2139 compltyp(sym_t *dsym, sym_t *ssym)
2141 type_t **dstp, *src;
2144 dstp = &dsym->s_type;
2147 while ((dst = *dstp) != NULL) {
2148 if (src == NULL || dst->t_tspec != src->t_tspec)
2149 lerror("compltyp() 1");
2150 if (dst->t_tspec == ARRAY) {
2151 if (dst->t_dim == 0 && src->t_dim != 0) {
2152 *dstp = dst = duptyp(dst);
2153 dst->t_dim = src->t_dim;
2154 /* now a complete Typ */
2157 } else if (dst->t_tspec == FUNC) {
2158 if (!dst->t_proto && src->t_proto) {
2159 *dstp = dst = duptyp(dst);
2161 dst->t_args = src->t_args;
2164 dstp = &dst->t_subt;
2170 * Completes the declaration of a single argument.
2173 decl1arg(sym_t *sym, int initflg)
2181 if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) {
2182 /* redeclaration of formal parameter %s */
2183 error(237, sym->s_name);
2184 rmsym(dcs->d_rdcsym);
2189 /* declared argument %s is missing */
2190 error(53, sym->s_name);
2195 /* cannot initialize parameter: %s */
2196 error(52, sym->s_name);
2200 if ((t = sym->s_type->t_tspec) == ARRAY) {
2201 sym->s_type = incref(sym->s_type->t_subt, PTR);
2202 } else if (t == FUNC) {
2204 /* a function is declared as an argument: %s */
2205 warning(50, sym->s_name);
2206 sym->s_type = incref(sym->s_type, PTR);
2207 } else if (t == FLOAT) {
2209 sym->s_type = gettyp(DOUBLE);
2213 /* argument declared inline: %s */
2214 warning(269, sym->s_name);
2217 * Arguments must have complete types. lengths() prints the needed
2218 * error messages (null dimension is impossible because arrays are
2219 * converted to pointers).
2221 if (sym->s_type->t_tspec != VOID)
2222 (void)length(sym->s_type, sym->s_name);
2230 * Does some checks for lint directives which apply to functions.
2231 * Processes arguments in old style function definitions which default
2233 * Checks compatiblility of old style function definition with previous
2239 sym_t *args, *arg, *pargs, *parg;
2240 int narg, nparg, n, msg;
2243 args = funcsym->s_args;
2244 pargs = funcsym->s_type->t_args;
2246 /* check for illegal combinations of lint directives */
2247 if (prflstrg != -1 && scflstrg != -1) {
2248 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2250 prflstrg = scflstrg = -1;
2252 if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) {
2253 /* dubious use of ** VARARGS ** with ** %s ** */
2254 warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2259 * check if the argument of a lint directive is compatible with the
2260 * number of arguments.
2263 for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_nxt)
2265 if (nargusg > narg) {
2266 /* argument number mismatch with directive: ** %s ** */
2267 warning(283, "ARGSUSED");
2270 if (nvararg > narg) {
2271 /* argument number mismatch with directive: ** %s ** */
2272 warning(283, "VARARGS");
2275 if (prflstrg > narg) {
2276 /* argument number mismatch with directive: ** %s ** */
2277 warning(283, "PRINTFLIKE");
2279 } else if (prflstrg == 0) {
2282 if (scflstrg > narg) {
2283 /* argument number mismatch with directive: ** %s ** */
2284 warning(283, "SCANFLIKE");
2286 } else if (scflstrg == 0) {
2289 if (prflstrg != -1 || scflstrg != -1) {
2290 narg = prflstrg != -1 ? prflstrg : scflstrg;
2292 for (n = 1; n < narg; n++)
2294 if (arg->s_type->t_tspec != PTR ||
2295 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2296 t != UCHAR && t != SCHAR)) {
2297 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2299 prflstrg = scflstrg = -1;
2304 * print a warning for each argument off an old style function
2305 * definition which defaults to int
2307 for (arg = args; arg != NULL; arg = arg->s_nxt) {
2308 if (arg->s_defarg) {
2309 /* argument type defaults to int: %s */
2310 warning(32, arg->s_name);
2317 * If this is an old style function definition and a prototyp
2318 * exists, compare the types of arguments.
2320 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2322 * If the number of arguments does not macht, we need not
2327 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2329 for (arg = args; arg != NULL; arg = arg->s_nxt)
2331 if (narg != nparg) {
2332 /* parameter mismatch: %d declared, %d defined */
2333 error(51, nparg, narg);
2339 msg |= chkptdecl(arg, parg);
2345 /* prototype declaration */
2346 prevdecl(285, dcs->d_rdcsym);
2348 /* from now the prototype is valid */
2349 funcsym->s_osdef = 0;
2350 funcsym->s_args = NULL;
2357 * Checks compatibility of an old style function definition with a previous
2358 * prototype declaration.
2359 * Returns 1 if the position of the previous declaration should be reported.
2362 chkptdecl(sym_t *arg, sym_t *parg)
2373 if (!eqtype(tp, ptp, 1, 1, &warn)) {
2374 if (eqtype(tp, ptp, 1, 0, &warn)) {
2375 /* type does not match prototype: %s */
2376 msg = gnuism(58, arg->s_name);
2378 /* type does not match prototype: %s */
2379 error(58, arg->s_name);
2383 /* type does not match prototype: %s */
2384 (*(sflag ? error : warning))(58, arg->s_name);
2392 * Completes a single local declaration/definition.
2395 decl1loc(sym_t *dsym, int initflg)
2398 /* Correct a mistake done in dname(). */
2399 if (dsym->s_type->t_tspec == FUNC) {
2401 if (dcs->d_scl == NOSCL)
2402 dsym->s_scl = EXTERN;
2405 if (dsym->s_type->t_tspec == FUNC) {
2406 if (dsym->s_scl == STATIC) {
2407 /* dubious static function at block level: %s */
2408 warning(93, dsym->s_name);
2409 dsym->s_scl = EXTERN;
2410 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2411 /* function has illegal storage class: %s */
2412 error(94, dsym->s_name);
2413 dsym->s_scl = EXTERN;
2418 * functions may be declared inline at local scope, although
2419 * this has no effect for a later definition of the same
2421 * XXX it should have an effect if tflag is set. this would
2422 * also be the way gcc behaves.
2424 if (dcs->d_inline) {
2425 if (dsym->s_type->t_tspec == FUNC) {
2428 /* variable declared inline: %s */
2429 warning(268, dsym->s_name);
2437 if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN)
2440 if (dsym->s_scl == EXTERN) {
2442 * XXX wenn die statische Variable auf Ebene 0 erst
2443 * spaeter definiert wird, haben wir die Brille auf.
2445 if (dsym->s_xsym == NULL) {
2446 outsym(dsym, EXTERN, dsym->s_def);
2448 outsym(dsym, dsym->s_xsym->s_scl, dsym->s_def);
2452 if (dcs->d_rdcsym != NULL) {
2454 if (dcs->d_rdcsym->s_blklev == 0) {
2456 switch (dsym->s_scl) {
2458 /* automatic hides external declaration: %s */
2460 warning(86, dsym->s_name);
2463 /* static hides external declaration: %s */
2465 warning(87, dsym->s_name);
2468 /* typedef hides external declaration: %s */
2470 warning(88, dsym->s_name);
2474 * Warnings and errors are printed in ledecl()
2478 lerror("decl1loc() 1");
2481 } else if (dcs->d_rdcsym->s_blklev == blklev) {
2483 /* no hflag, because its illegal! */
2484 if (dcs->d_rdcsym->s_arg) {
2486 * if !tflag, a "redeclaration of %s" error
2491 /* decl. hides parameter: %s */
2492 warning(91, dsym->s_name);
2493 rmsym(dcs->d_rdcsym);
2497 } else if (dcs->d_rdcsym->s_blklev < blklev) {
2500 /* declaration hides earlier one: %s */
2501 warning(95, dsym->s_name);
2505 if (dcs->d_rdcsym->s_blklev == blklev) {
2507 /* redeclaration of %s */
2508 error(27, dsym->s_name);
2509 rmsym(dcs->d_rdcsym);
2515 if (initflg && !(initerr = chkinit(dsym))) {
2520 if (dsym->s_scl == TYPEDEF) {
2521 dsym->s_type = duptyp(dsym->s_type);
2522 dsym->s_type->t_typedef = 1;
2523 settdsym(dsym->s_type, dsym);
2527 * Before we can check the size we must wait for an initialisation
2533 * Processes (re)declarations of external Symbols inside blocks.
2541 /* look for a symbol with the same name */
2542 esym = dcs->d_rdcsym;
2543 while (esym != NULL && esym->s_blklev != 0) {
2544 while ((esym = esym->s_link) != NULL) {
2545 if (esym->s_kind != FVFT)
2547 if (strcmp(dsym->s_name, esym->s_name) == 0)
2553 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2554 /* gcc accepts this without a warning, pcc prints an error. */
2555 /* redeclaration of %s */
2556 warning(27, dsym->s_name);
2562 eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &warn);
2565 if (esym->s_scl == EXTERN) {
2566 /* inconsistent redeclaration of extern: %s */
2567 warning(90, dsym->s_name);
2570 /* inconsistent redeclaration of static: %s */
2571 warning(92, dsym->s_name);
2578 * Remember the external symbol so we can update usage
2579 * information at the end of the block.
2581 dsym->s_xsym = esym;
2586 * Print an error or a warning if the symbol cant be initialized due
2587 * to type/storage class. Returnvalue is 1 if an error has been
2597 if (sym->s_type->t_tspec == FUNC) {
2598 /* cannot initialize function: %s */
2599 error(24, sym->s_name);
2601 } else if (sym->s_scl == TYPEDEF) {
2602 /* cannot initialize typedef: %s */
2603 error(25, sym->s_name);
2605 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2606 /* cannot initialize "extern" declaration: %s */
2607 if (dcs->d_ctx == EXTERN) {
2608 warning(26, sym->s_name);
2610 error(26, sym->s_name);
2619 * Create a symbole for an abstract declaration.
2626 if (dcs->d_ctx != ABSTRACT && dcs->d_ctx != PARG)
2629 sym = getblk(sizeof (sym_t));
2631 sym->s_name = unnamed;
2633 sym->s_scl = ABSTRACT;
2636 if (dcs->d_ctx == PARG)
2639 sym->s_type = dcs->d_type;
2640 dcs->d_rdcsym = NULL;
2647 * Removes anything which has nothing to do on global level.
2653 while (dcs->d_nxt != NULL)
2661 * remove all informations about pending lint directives without
2668 * Process an abstract type declaration
2671 decl1abs(sym_t *sym)
2680 * Checks size after declarations of variables and their initialisation.
2687 * check size only for symbols which are defined and no function and
2690 if (dsym->s_def != DEF)
2692 if (dsym->s_scl == TYPEDEF)
2694 if (dsym->s_type->t_tspec == FUNC)
2697 if (length(dsym->s_type, dsym->s_name) == 0 &&
2698 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2699 /* empty array declaration: %s */
2701 warning(190, dsym->s_name);
2703 error(190, dsym->s_name);
2709 * Mark an object as set if it is not already
2717 UNIQUE_CURR_POS(sym->s_spos);
2722 * Mark an object as used if it is not already
2725 setuflg(sym_t *sym, int fcall, int szof)
2730 UNIQUE_CURR_POS(sym->s_upos);
2733 * for function calls another record is written
2735 * XXX Should symbols used in sizeof() treated as used or not?
2736 * Probably not, because there is no sense to declare an
2737 * external variable only to get their size.
2739 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2744 * Prints warnings for a list of variables and labels (concatenated
2745 * with s_dlnxt) if these are not used or only set.
2748 chkusage(dinfo_t *di)
2753 /* for this warnings LINTED has no effect */
2757 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
2758 chkusg1(di->d_asm, sym);
2764 * Prints a warning for a single variable or label if it is not used or
2768 chkusg1(int novar, sym_t *sym)
2772 if (sym->s_blklev == -1)
2775 STRUCT_ASSIGN(cpos, curr_pos);
2777 if (sym->s_kind == FVFT) {
2779 chkausg(novar, sym);
2781 chkvusg(novar, sym);
2783 } else if (sym->s_kind == FLAB) {
2785 } else if (sym->s_kind == FTAG) {
2789 STRUCT_ASSIGN(curr_pos, cpos);
2793 chkausg(int novar, sym_t *arg)
2797 lerror("chkausg() 1");
2802 if (!arg->s_used && vflag) {
2803 STRUCT_ASSIGN(curr_pos, arg->s_dpos);
2804 /* argument %s unused in function %s */
2805 warning(231, arg->s_name, funcsym->s_name);
2810 chkvusg(int novar, sym_t *sym)
2815 if (blklev == 0 || sym->s_blklev == 0)
2816 lerror("chkvusg() 1");
2818 /* errors in expressions easily cause lots of these warnings */
2823 * XXX Only variables are checkd, although types should
2824 * probably also be checked
2826 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
2827 sc != AUTO && sc != REG) {
2835 if (!sym->s_used && !sym->s_set) {
2836 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2837 /* %s unused in function %s */
2838 warning(192, sym->s_name, funcsym->s_name);
2841 if (sym->s_set && !sym->s_used) {
2842 STRUCT_ASSIGN(curr_pos, sym->s_spos);
2843 /* %s set but not used in function %s */
2844 warning(191, sym->s_name, funcsym->s_name);
2845 } else if (!sym->s_used) {
2846 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2847 /* %s unused in function %s */
2848 warning(192, sym->s_name, funcsym->s_name);
2854 * information about usage is taken over into the symbol
2855 * tabel entry at level 0 if the symbol was locally declared
2856 * as an external symbol.
2858 * XXX This is wrong for symbols declared static at level 0
2859 * if the usage information stems from sizeof(). This is
2860 * because symbols at level 0 only used in sizeof() are
2861 * considered to not be used.
2863 if ((xsym = sym->s_xsym) != NULL) {
2864 if (sym->s_used && !xsym->s_used) {
2866 STRUCT_ASSIGN(xsym->s_upos, sym->s_upos);
2868 if (sym->s_set && !xsym->s_set) {
2870 STRUCT_ASSIGN(xsym->s_spos, sym->s_spos);
2880 if (blklev != 1 || lab->s_blklev != 1)
2881 lerror("chklusg() 1");
2883 if (lab->s_set && !lab->s_used) {
2884 STRUCT_ASSIGN(curr_pos, lab->s_spos);
2885 /* label %s unused in function %s */
2886 warning(192, lab->s_name, funcsym->s_name);
2887 } else if (!lab->s_set) {
2888 STRUCT_ASSIGN(curr_pos, lab->s_upos);
2889 /* undefined label %s */
2890 warning(23, lab->s_name);
2898 if (!incompl(sym->s_type))
2901 /* complain alwasy about incomplet tags declared inside blocks */
2902 if (!zflag || dcs->d_ctx != EXTERN)
2905 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2906 switch (sym->s_type->t_tspec) {
2908 /* struct %s never defined */
2909 warning(233, sym->s_name);
2912 /* union %s never defined */
2913 warning(234, sym->s_name);
2916 /* enum %s never defined */
2917 warning(235, sym->s_name);
2920 lerror("chktusg() 1");
2925 * Called after the entire translation unit has been parsed.
2926 * Changes tentative definitions in definitions.
2927 * Performs some tests on global Symbols. Detected Problems are:
2928 * - defined variables of incomplete type
2929 * - constant variables which are not initialized
2930 * - static symbols which are never used
2938 if (blklev != 0 || dcs->d_nxt != NULL)
2941 STRUCT_ASSIGN(cpos, curr_pos);
2943 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
2944 if (sym->s_blklev == -1)
2946 if (sym->s_kind == FVFT) {
2948 } else if (sym->s_kind == FTAG) {
2951 if (sym->s_kind != FMOS)
2952 lerror("chkglsyms() 1");
2956 STRUCT_ASSIGN(curr_pos, cpos);
2960 chkglvar(sym_t *sym)
2963 if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON)
2966 if (sym->s_scl != EXTERN && sym->s_scl != STATIC)
2967 lerror("chkglvar() 1");
2971 if (sym->s_scl == STATIC) {
2972 if (sym->s_type->t_tspec == FUNC) {
2973 if (sym->s_used && sym->s_def != DEF) {
2974 STRUCT_ASSIGN(curr_pos, sym->s_upos);
2975 /* static func. called but not def.. */
2976 error(225, sym->s_name);
2980 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2981 if (sym->s_type->t_tspec == FUNC) {
2982 if (sym->s_def == DEF) {
2984 /* static function %s unused */
2985 warning(236, sym->s_name);
2987 /* static function %s decl. but ... */
2988 warning(290, sym->s_name);
2990 } else if (!sym->s_set) {
2991 /* static variable %s unused */
2992 warning(226, sym->s_name);
2994 /* static variable %s set but not used */
2995 warning(307, sym->s_name);
2998 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
2999 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3000 /* const object %s should have initializer */
3001 warning(227, sym->s_name);
3010 if (sym->s_def == TDEF) {
3011 if (sym->s_type->t_tspec == FUNC)
3013 * this can happen if a syntax error occurred
3014 * after a function declaration
3017 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3018 if (length(sym->s_type, sym->s_name) == 0 &&
3019 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3020 /* empty array declaration: %s */
3021 if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3022 warning(190, sym->s_name);
3024 error(190, sym->s_name);
3031 * Prints information about location of previous definition/declaration.
3034 prevdecl(int msg, sym_t *psym)
3041 STRUCT_ASSIGN(cpos, curr_pos);
3042 STRUCT_ASSIGN(curr_pos, psym->s_dpos);
3044 message(msg, psym->s_name);
3045 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3046 /* previous definition of %s */
3047 message(261, psym->s_name);
3049 /* previous declaration of %s */
3050 message(260, psym->s_name);
3052 STRUCT_ASSIGN(curr_pos, cpos);