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) */
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");
469 lerror("addqual() 1");
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)
511 lerror("popdecl() 1");
516 /* there is nothing after external declarations */
517 lerror("popdecl() 2");
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 surounding 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);
565 lerror("popdecl() 3");
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 */
638 lerror("deftyp() 1");
661 /* use 'double' instead of ... */
670 /* 'long double' is illegal in ... */
677 lerror("deftyp() 2");
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)
715 lerror("deftyp() 3");
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)
721 lerror("deftyp() 4");
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() 1");
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() 1");
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() 2");
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 und signed int are protable 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) {
995 * bit-field type '%s' invalid in
998 warning(273, tyname(tp));
1000 /* nonportable bit-field type */
1004 } else if (t == INT && dcs->d_smod == NOTSPEC) {
1005 if (pflag && bitfieldtype_ok == 0) {
1006 /* nonportable bit-field type */
1009 } else if (t != INT && t != UINT) {
1011 * Non-integer types are always illegal for
1012 * bitfields, regardless of BITFIELDTYPE.
1013 * Integer types not dealt with above are
1014 * okay only if BITFIELDTYPE is in effect.
1016 if (bitfieldtype_ok == 0 || isityp(t) == 0) {
1017 /* illegal bit-field type */
1020 dsym->s_type = tp = duptyp(gettyp(t = INT));
1021 if ((tp->t_flen = sz) > size(t))
1022 tp->t_flen = size(t);
1025 if ((len = tp->t_flen) < 0 || len > size(t)) {
1026 /* illegal bit-field size */
1028 tp->t_flen = size(t);
1029 } else if (len == 0 && dsym->s_name != unnamed) {
1030 /* zero size bit-field */
1032 tp->t_flen = size(t);
1034 if (dsym->s_scl == MOU) {
1035 /* illegal use of bit-field */
1037 dsym->s_type->t_isfield = 0;
1040 } else if (t == FUNC) {
1041 /* function illegal in structure or union */
1043 dsym->s_type = tp = incref(tp, t = PTR);
1047 * bit-fields of length 0 are not warned about because length()
1048 * does not return the length of the bit-field but the length
1049 * of the type the bit-field is packed in (its ok)
1051 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1052 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1053 /* illegal zero sized structure member: %s */
1054 warning(39, dsym->s_name);
1058 if (dcs->d_ctx == MOU) {
1062 if (dsym->s_field) {
1063 align(getbound(tp), tp->t_flen);
1064 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t);
1065 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1066 dcs->d_offset += tp->t_flen;
1068 align(getbound(tp), 0);
1069 dsym->s_value.v_quad = dcs->d_offset;
1070 dcs->d_offset += sz;
1072 if (dcs->d_ctx == MOU) {
1073 if (o > dcs->d_offset)
1080 * Clear the BITFIELDTYPE indicator after processing each
1081 * structure element.
1083 bitfieldtype_ok = 0;
1089 * Aligns next structure element as required.
1091 * al contains the required alignment, len the length of a bit-field.
1094 align(int al, int len)
1099 * The alignment of the current element becomes the alignment of
1100 * the struct/union if it is larger than the current alignment
1101 * of the struct/union.
1103 if (al > dcs->d_stralign)
1104 dcs->d_stralign = al;
1106 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1107 if (len == 0 || dcs->d_offset + len > no)
1112 * Remember the width of the field in its type structure.
1115 bitfield(sym_t *dsym, int len)
1119 dsym = getblk(sizeof (sym_t));
1120 dsym->s_name = unnamed;
1121 dsym->s_kind = FMOS;
1123 dsym->s_type = gettyp(UINT);
1124 dsym->s_blklev = -1;
1126 dsym->s_type = duptyp(dsym->s_type);
1127 dsym->s_type->t_isfield = 1;
1128 dsym->s_type->t_flen = len;
1134 * Collect informations about a sequence of asterisks and qualifiers
1135 * in a list of type pqinf_t.
1136 * Qualifiers refer always to the left asterisk. The rightmost asterisk
1137 * will be at the top of the list.
1140 mergepq(pqinf_t *p1, pqinf_t *p2)
1144 if (p2->p_pcnt != 0) {
1145 /* left '*' at the end of the list */
1146 for (p = p2; p->p_nxt != NULL; p = p->p_nxt)
1154 warning(10, "const");
1158 if (p2->p_volatile) {
1159 if (p1->p_volatile) {
1161 warning(10, "volatile");
1171 * Followint 3 functions extend the type of a declarator with
1172 * pointer, function and array types.
1174 * The current type is the Type built by deftyp() (dcs->d_type) and
1175 * pointer, function and array types already added for this
1176 * declarator. The new type extension is inserted between both.
1179 addptr(sym_t *decl, pqinf_t *pi)
1184 tpp = &decl->s_type;
1185 while (*tpp && *tpp != dcs->d_type)
1186 tpp = &(*tpp)->t_subt;
1190 while (pi != NULL) {
1191 *tpp = tp = getblk(sizeof (type_t));
1193 tp->t_const = pi->p_const;
1194 tp->t_volatile = pi->p_volatile;
1195 *(tpp = &tp->t_subt) = dcs->d_type;
1204 * If a dimension was specified, dim is 1, otherwise 0
1205 * n is the specified dimension
1208 addarray(sym_t *decl, int dim, int n)
1212 tpp = &decl->s_type;
1213 while (*tpp && *tpp != dcs->d_type)
1214 tpp = &(*tpp)->t_subt;
1218 *tpp = tp = getblk(sizeof (type_t));
1219 tp->t_tspec = ARRAY;
1220 tp->t_subt = dcs->d_type;
1224 /* zero or negative array dimension */
1227 } else if (n == 0 && dim) {
1228 /* zero or negative array dimension */
1230 } else if (n == 0 && !dim) {
1231 /* is incomplete type */
1239 addfunc(sym_t *decl, sym_t *args)
1245 /* function prototypes are illegal in traditional C */
1247 args = nsfunc(decl, args);
1253 * The symbols are removed from the symbol table by popdecl() after
1254 * addfunc(). To be able to restore them if this is a function
1255 * definition, a pointer to the list of all symbols is stored in
1256 * dcs->d_nxt->d_fpsyms. Also a list of the arguments (concatenated
1257 * by s_nxt) is stored in dcs->d_nxt->d_fargs.
1258 * (dcs->d_nxt must be used because *dcs is the declaration stack
1259 * element created for the list of params and is removed after
1262 if (dcs->d_nxt->d_ctx == EXTERN &&
1263 decl->s_type == dcs->d_nxt->d_type) {
1264 dcs->d_nxt->d_fpsyms = dcs->d_dlsyms;
1265 dcs->d_nxt->d_fargs = args;
1268 tpp = &decl->s_type;
1269 while (*tpp && *tpp != dcs->d_nxt->d_type)
1270 tpp = &(*tpp)->t_subt;
1274 *tpp = tp = getblk(sizeof (type_t));
1276 tp->t_subt = dcs->d_nxt->d_type;
1277 if ((tp->t_proto = dcs->d_proto) != 0)
1279 tp->t_vararg = dcs->d_vararg;
1285 * Called for new style function declarations.
1289 nsfunc(sym_t *decl, sym_t *args)
1296 * Declarations of structs/unions/enums in param lists are legal,
1299 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1301 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) {
1302 /* dubious tag declaration: %s %s */
1303 warning(85, scltoa(sc), sym->s_name);
1308 for (arg = args; arg != NULL; arg = arg->s_nxt) {
1309 if (arg->s_type->t_tspec == VOID) {
1310 if (n > 1 || arg->s_nxt != NULL) {
1311 /* "void" must be sole parameter */
1313 arg->s_type = gettyp(INT);
1319 /* return NULL if first param is VOID */
1320 return (args != NULL && args->s_type->t_tspec != VOID ? args : NULL);
1324 * Called for old style function declarations.
1327 osfunc(sym_t *decl, sym_t *args)
1331 * Remember list of params only if this is really seams to be
1332 * a function definition.
1334 if (dcs->d_nxt->d_ctx == EXTERN &&
1335 decl->s_type == dcs->d_nxt->d_type) {
1337 * We assume that this becomes a function definition. If
1338 * we are wrong, its corrected in chkfdef().
1342 decl->s_args = args;
1346 /* function prototype parameters must have types */
1352 * Lists of Identifiers in functions declarations are allowed only if
1353 * its also a function definition. If this is not the case, print a
1357 chkfdef(sym_t *sym, int msg)
1362 /* incomplete or misplaced function definition */
1371 * Process the name in a declarator.
1372 * If the symbol does already exists, a new one is created.
1373 * The symbol becomes one of the storage classes EXTERN, STATIC, AUTO or
1375 * s_def and s_reg are valid after dname().
1382 if (sym->s_scl == NOSCL) {
1383 dcs->d_rdcsym = NULL;
1384 } else if (sym->s_defarg) {
1386 dcs->d_rdcsym = NULL;
1388 dcs->d_rdcsym = sym;
1389 sym = pushdown(sym);
1392 switch (dcs->d_ctx) {
1396 sym->s_styp = dcs->d_tagtyp->t_str;
1398 sym->s_value.v_tspec = INT;
1403 * static and external symbols without "extern" are
1404 * considered to be tentative defined, external
1405 * symbols with "extern" are declared, and typedef names
1406 * are defined. Tentative defined and declared symbols
1407 * may become defined if an initializer is present or
1408 * this is a function definition.
1410 if ((sc = dcs->d_scl) == NOSCL) {
1413 } else if (sc == STATIC) {
1415 } else if (sc == TYPEDEF) {
1417 } else if (sc == EXTERN) {
1420 lerror("dname() 1");
1427 if ((sc = dcs->d_scl) == NOSCL) {
1429 } else if (sc == REG) {
1433 lerror("dname() 2");
1438 if ((sc = dcs->d_scl) == NOSCL) {
1440 * XXX somewhat ugly because we dont know whether
1441 * this is AUTO or EXTERN (functions). If we are
1442 * wrong it must be corrected in decl1loc(), where
1443 * we have the necessary type information.
1447 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1449 } else if (sc == REG) {
1453 } else if (sc == EXTERN) {
1456 lerror("dname() 3");
1460 lerror("dname() 4");
1464 sym->s_type = dcs->d_type;
1466 dcs->d_fpsyms = NULL;
1472 * Process a name in the list of formal params in an old style function
1479 if (sym->s_scl != NOSCL) {
1480 if (blklev == sym->s_blklev) {
1481 /* redeclaration of formal parameter %s */
1482 error(21, sym->s_name);
1486 sym = pushdown(sym);
1488 sym->s_type = gettyp(INT);
1491 sym->s_defarg = sym->s_arg = 1;
1496 * Create the type of a tag.
1498 * tag points to the symbol table entry of the tag
1499 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1500 * decl is 1 if the type of the tag will be completed in this declaration
1501 * (the following token is T_LBRACE)
1502 * semi is 1 if the following token is T_SEMI
1505 mktag(sym_t *tag, tspec_t kind, int decl, int semi)
1510 if (kind == STRUCT) {
1512 } else if (kind == UNION) {
1514 } else if (kind == ENUM) {
1521 if (tag->s_scl != NOSCL) {
1522 tag = newtag(tag, scl, decl, semi);
1524 /* a new tag, no empty declaration */
1525 dcs->d_nxt->d_nedecl = 1;
1526 if (scl == ENUMTAG && !decl) {
1527 if (!tflag && (sflag || pflag))
1528 /* forward reference to enum type */
1532 if (tag->s_scl == NOSCL) {
1534 tag->s_type = tp = getblk(sizeof (type_t));
1539 tag = getblk(sizeof (sym_t));
1540 tag->s_name = unnamed;
1541 UNIQUE_CURR_POS(tag->s_dpos);
1545 tag->s_type = tp = getblk(sizeof (type_t));
1546 dcs->d_nxt->d_nedecl = 1;
1549 if (tp->t_tspec == NOTSPEC) {
1552 tp->t_str = getblk(sizeof (str_t));
1553 tp->t_str->align = CHAR_BIT;
1554 tp->t_str->stag = tag;
1557 tp->t_enum = getblk(sizeof (enum_t));
1558 tp->t_enum->etag = tag;
1560 /* ist unvollstaendiger Typ */
1568 * Checks all possible cases of tag redeclarations.
1569 * decl is 1 if T_LBRACE follows
1570 * semi is 1 if T_SEMI follows
1573 newtag(sym_t *tag, scl_t scl, int decl, int semi)
1576 if (tag->s_blklev < blklev) {
1581 /* decl. introduces new type ... */
1582 warning(44, scltoa(scl), tag->s_name);
1583 tag = pushdown(tag);
1584 } else if (tag->s_scl != scl) {
1585 /* base type is really "%s %s" */
1586 warning(45, scltoa(tag->s_scl), tag->s_name);
1588 dcs->d_nxt->d_nedecl = 1;
1590 /* "struct a { ... } " */
1592 /* redefinition hides earlier one: %s */
1593 warning(43, tag->s_name);
1594 tag = pushdown(tag);
1595 dcs->d_nxt->d_nedecl = 1;
1596 } else if (tag->s_scl != scl) {
1597 /* base type is really "%s %s" */
1598 warning(45, scltoa(tag->s_scl), tag->s_name);
1599 /* declaration introduces new type in ANSI C: %s %s */
1601 warning(44, scltoa(scl), tag->s_name);
1602 tag = pushdown(tag);
1603 dcs->d_nxt->d_nedecl = 1;
1606 if (tag->s_scl != scl) {
1607 /* (%s) tag redeclared */
1608 error(46, scltoa(tag->s_scl));
1610 tag = pushdown(tag);
1611 dcs->d_nxt->d_nedecl = 1;
1612 } else if (decl && !incompl(tag->s_type)) {
1613 /* (%s) tag redeclared */
1614 error(46, scltoa(tag->s_scl));
1616 tag = pushdown(tag);
1617 dcs->d_nxt->d_nedecl = 1;
1618 } else if (semi || decl) {
1619 dcs->d_nxt->d_nedecl = 1;
1631 case EXTERN: s = "extern"; break;
1632 case STATIC: s = "static"; break;
1633 case AUTO: s = "auto"; break;
1634 case REG: s = "register"; break;
1635 case TYPEDEF: s = "typedef"; break;
1636 case STRTAG: s = "struct"; break;
1637 case UNIONTAG: s = "union"; break;
1638 case ENUMTAG: s = "enum"; break;
1639 default: lerror("tagttoa()");
1645 * Completes the type of a tag in a struct/union/enum declaration.
1646 * tp points to the type of the, tag, fmem to the list of members/enums.
1649 compltag(type_t *tp, sym_t *fmem)
1656 /* from now a complete type */
1659 if ((t = tp->t_tspec) != ENUM) {
1660 align(dcs->d_stralign, 0);
1662 sp->align = dcs->d_stralign;
1663 sp->size = dcs->d_offset;
1665 if (sp->size == 0) {
1667 (void)gnuism(47, ttab[t].tt_name);
1670 for (mem = fmem; mem != NULL; mem = mem->s_nxt) {
1671 if (mem->s_name != unnamed)
1675 /* %s has no named members */
1677 t == STRUCT ? "structure" : "union");
1681 tp->t_enum->elem = fmem;
1687 * Processes the name of an enumerator in en enum declaration.
1689 * sym points to the enumerator
1690 * val is the value of the enumerator
1691 * impl is 1 if the value of the enumerator was not explicit specified.
1694 ename(sym_t *sym, int val, int impl)
1698 if (sym->s_blklev == blklev) {
1699 /* no hflag, because this is illegal!!! */
1701 /* enumeration constant hides parameter: %s */
1702 warning(57, sym->s_name);
1704 /* redeclaration of %s */
1705 error(27, sym->s_name);
1707 * inside blocks it should not too complicated
1708 * to find the position of the previous
1716 /* redefinition hides earlier one: %s */
1717 warning(43, sym->s_name);
1719 sym = pushdown(sym);
1721 sym->s_scl = ENUMCON;
1722 sym->s_type = dcs->d_tagtyp;
1723 sym->s_value.v_tspec = INT;
1724 sym->s_value.v_quad = val;
1725 if (impl && val - 1 == INT_MAX) {
1726 /* overflow in enumeration values: %s */
1727 warning(48, sym->s_name);
1734 * Process a single external declarator.
1737 decl1ext(sym_t *dsym, int initflg)
1739 int warn, rval, redec;
1746 if (initflg && !(initerr = chkinit(dsym)))
1750 * Declarations of functions are marked as "tentative" in dname().
1751 * This is wrong because there are no tentative function
1754 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1757 if (dcs->d_inline) {
1758 if (dsym->s_type->t_tspec == FUNC) {
1761 /* variable declared inline: %s */
1762 warning(268, dsym->s_name);
1766 /* Write the declaration into the output file */
1767 if (plibflg && llibflg &&
1768 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1770 * With both LINTLIBRARY and PROTOLIB the prototyp is
1771 * written as a function definition to the output file.
1773 rval = dsym->s_type->t_subt->t_tspec != VOID;
1774 outfdef(dsym, &dsym->s_dpos, rval, 0, NULL);
1776 outsym(dsym, dsym->s_scl, dsym->s_def);
1779 if ((rdsym = dcs->d_rdcsym) != NULL) {
1782 * If the old symbol stems from an old style function definition
1783 * we have remembered the params in rdsmy->s_args and compare
1784 * them with the params of the prototype.
1786 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1787 redec = chkosdef(rdsym, dsym);
1792 if (!redec && !isredec(dsym, (warn = 0, &warn))) {
1795 /* redeclaration of %s */
1796 (*(sflag ? error : warning))(27, dsym->s_name);
1797 prevdecl(-1, rdsym);
1801 * Overtake the rememberd params if the new symbol
1802 * is not a prototype.
1804 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1805 dsym->s_osdef = rdsym->s_osdef;
1806 dsym->s_args = rdsym->s_args;
1807 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1811 * Remember the position of the declaration if the
1812 * old symbol was a prototype and the new is not.
1813 * Also remember the position if the old symbol
1814 * was defined and the new is not.
1816 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1817 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1818 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1819 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1823 * Copy informations about usage of the name into
1826 cpuinfo(dsym, rdsym);
1828 /* Once a name is defined, it remains defined. */
1829 if (rdsym->s_def == DEF)
1832 /* once a function is inline, it remains inline */
1833 if (rdsym->s_inline)
1836 compltyp(dsym, rdsym);
1843 if (dsym->s_scl == TYPEDEF) {
1844 dsym->s_type = duptyp(dsym->s_type);
1845 dsym->s_type->t_typedef = 1;
1846 settdsym(dsym->s_type, dsym);
1852 * Copies informations about usage into a new symbol table entry of
1856 cpuinfo(sym_t *sym, sym_t *rdsym)
1859 sym->s_spos = rdsym->s_spos;
1860 sym->s_upos = rdsym->s_upos;
1861 sym->s_set = rdsym->s_set;
1862 sym->s_used = rdsym->s_used;
1866 * Prints an error and returns 1 if a symbol is redeclared/redefined.
1867 * Otherwise returns 0 and, in some cases of minor problems, prints
1871 isredec(sym_t *dsym, int *warn)
1875 if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
1876 /* redeclaration of %s */
1877 error(27, dsym->s_name);
1881 if (rsym->s_scl == TYPEDEF) {
1882 /* typedef redeclared: %s */
1883 error(89, dsym->s_name);
1887 if (dsym->s_scl == TYPEDEF) {
1888 /* redeclaration of %s */
1889 error(27, dsym->s_name);
1893 if (rsym->s_def == DEF && dsym->s_def == DEF) {
1894 /* redefinition of %s */
1895 error(28, dsym->s_name);
1899 if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, warn)) {
1900 /* redeclaration of %s */
1901 error(27, dsym->s_name);
1905 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
1907 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
1909 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
1911 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
1913 * All cases except "int a = 1; static int a;" are catched
1914 * above with or without a warning
1916 /* redeclaration of %s */
1917 error(27, dsym->s_name);
1921 if (rsym->s_scl == EXTERN) {
1922 /* previously declared extern, becomes static: %s */
1923 warning(29, dsym->s_name);
1929 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
1931 /* redeclaration of %s; ANSI C requires "static" */
1933 warning(30, dsym->s_name);
1936 dsym->s_scl = STATIC;
1941 * Checks if two types are compatible. Returns 0 if not, otherwise 1.
1943 * ignqual ignore qualifiers of type; used for function params
1944 * promot promote left type; used for comparison of params of
1945 * old style function definitions with params of prototypes.
1946 * *warn set to 1 if an old style function declaration is not
1947 * compatible with a prototype
1950 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *warn)
1954 while (tp1 != NULL && tp2 != NULL) {
1960 } else if (t == CHAR || t == SCHAR) {
1962 } else if (t == UCHAR) {
1963 t = tflag ? UINT : INT;
1964 } else if (t == SHORT) {
1966 } else if (t == USHORT) {
1968 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT;
1972 if (t != tp2->t_tspec)
1975 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
1978 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
1981 if (t == STRUCT || t == UNION)
1982 return (tp1->t_str == tp2->t_str);
1984 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
1985 if (tp1->t_dim != 0 && tp2->t_dim != 0)
1989 /* dont check prototypes for traditional */
1990 if (t == FUNC && !tflag) {
1991 if (tp1->t_proto && tp2->t_proto) {
1992 if (!eqargs(tp1, tp2, warn))
1994 } else if (tp1->t_proto) {
1995 if (!mnoarg(tp1, warn))
1997 } else if (tp2->t_proto) {
1998 if (!mnoarg(tp2, warn))
2005 ignqual = promot = 0;
2009 return (tp1 == tp2);
2013 * Compares the parameter types of two prototypes.
2016 eqargs(type_t *tp1, type_t *tp2, int *warn)
2020 if (tp1->t_vararg != tp2->t_vararg)
2026 while (a1 != NULL && a2 != NULL) {
2028 if (eqtype(a1->s_type, a2->s_type, 1, 0, warn) == 0)
2040 * mnoarg() (matches functions with no argument type information)
2041 * returns 1 if all parameters of a prototype are compatible with
2042 * and old style function declaration.
2043 * This is the case if following conditions are met:
2044 * 1. the prototype must have a fixed number of parameters
2045 * 2. no parameter is of type float
2046 * 3. no parameter is converted to another type if integer promotion
2050 mnoarg(type_t *tp, int *warn)
2059 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) {
2060 if ((t = arg->s_type->t_tspec) == FLOAT ||
2061 t == CHAR || t == SCHAR || t == UCHAR ||
2062 t == SHORT || t == USHORT) {
2071 * Compares a prototype declaration with the remembered arguments of
2072 * a previous old style function definition.
2075 chkosdef(sym_t *rdsym, sym_t *dsym)
2077 sym_t *args, *pargs, *arg, *parg;
2081 args = rdsym->s_args;
2082 pargs = dsym->s_type->t_args;
2087 for (arg = args; arg != NULL; arg = arg->s_nxt)
2089 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2091 if (narg != nparg) {
2092 /* prototype does not match old-style definition */
2104 * If it does not match due to promotion and sflag is
2105 * not set we print only a warning.
2107 if (!eqtype(arg->s_type, parg->s_type, 1, 1, &warn) || warn) {
2108 /* prototype does not match old-style def., arg #%d */
2119 /* old style definition */
2120 prevdecl(300, rdsym);
2126 * Complets a type by copying the dimension and prototype information
2127 * from a second compatible type.
2129 * Following lines are legal:
2130 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2131 * "typedef ft(); ft f; f(int); ft g; g(long);"
2132 * This means that, if a type is completed, the type structure must
2136 compltyp(sym_t *dsym, sym_t *ssym)
2138 type_t **dstp, *src;
2141 dstp = &dsym->s_type;
2144 while ((dst = *dstp) != NULL) {
2145 if (src == NULL || dst->t_tspec != src->t_tspec)
2146 lerror("compltyp() 1");
2147 if (dst->t_tspec == ARRAY) {
2148 if (dst->t_dim == 0 && src->t_dim != 0) {
2149 *dstp = dst = duptyp(dst);
2150 dst->t_dim = src->t_dim;
2151 /* now a complete Typ */
2154 } else if (dst->t_tspec == FUNC) {
2155 if (!dst->t_proto && src->t_proto) {
2156 *dstp = dst = duptyp(dst);
2158 dst->t_args = src->t_args;
2161 dstp = &dst->t_subt;
2167 * Completes the declaration of a single argument.
2170 decl1arg(sym_t *sym, int initflg)
2178 if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) {
2179 /* redeclaration of formal parameter %s */
2180 error(237, sym->s_name);
2181 rmsym(dcs->d_rdcsym);
2186 /* declared argument %s is missing */
2187 error(53, sym->s_name);
2192 /* cannot initialize parameter: %s */
2193 error(52, sym->s_name);
2197 if ((t = sym->s_type->t_tspec) == ARRAY) {
2198 sym->s_type = incref(sym->s_type->t_subt, PTR);
2199 } else if (t == FUNC) {
2201 /* a function is declared as an argument: %s */
2202 warning(50, sym->s_name);
2203 sym->s_type = incref(sym->s_type, PTR);
2204 } else if (t == FLOAT) {
2206 sym->s_type = gettyp(DOUBLE);
2210 /* argument declared inline: %s */
2211 warning(269, sym->s_name);
2214 * Arguments must have complete types. lengths() prints the needed
2215 * error messages (null dimension is impossible because arrays are
2216 * converted to pointers).
2218 if (sym->s_type->t_tspec != VOID)
2219 (void)length(sym->s_type, sym->s_name);
2227 * Does some checks for lint directives which apply to functions.
2228 * Processes arguments in old style function definitions which default
2230 * Checks compatiblility of old style function definition with previous
2236 sym_t *args, *arg, *pargs, *parg;
2237 int narg, nparg, n, msg;
2240 args = funcsym->s_args;
2241 pargs = funcsym->s_type->t_args;
2243 /* check for illegal combinations of lint directives */
2244 if (prflstrg != -1 && scflstrg != -1) {
2245 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2247 prflstrg = scflstrg = -1;
2249 if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) {
2250 /* dubious use of ** VARARGS ** with ** %s ** */
2251 warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2256 * check if the argument of a lint directive is compatible with the
2257 * number of arguments.
2260 for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_nxt)
2262 if (nargusg > narg) {
2263 /* argument number mismatch with directive: ** %s ** */
2264 warning(283, "ARGSUSED");
2267 if (nvararg > narg) {
2268 /* argument number mismatch with directive: ** %s ** */
2269 warning(283, "VARARGS");
2272 if (prflstrg > narg) {
2273 /* argument number mismatch with directive: ** %s ** */
2274 warning(283, "PRINTFLIKE");
2276 } else if (prflstrg == 0) {
2279 if (scflstrg > narg) {
2280 /* argument number mismatch with directive: ** %s ** */
2281 warning(283, "SCANFLIKE");
2283 } else if (scflstrg == 0) {
2286 if (prflstrg != -1 || scflstrg != -1) {
2287 narg = prflstrg != -1 ? prflstrg : scflstrg;
2289 for (n = 1; n < narg; n++)
2291 if (arg->s_type->t_tspec != PTR ||
2292 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2293 t != UCHAR && t != SCHAR)) {
2294 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2296 prflstrg = scflstrg = -1;
2301 * print a warning for each argument off an old style function
2302 * definition which defaults to int
2304 for (arg = args; arg != NULL; arg = arg->s_nxt) {
2305 if (arg->s_defarg) {
2306 /* argument type defaults to int: %s */
2307 warning(32, arg->s_name);
2314 * If this is an old style function definition and a prototyp
2315 * exists, compare the types of arguments.
2317 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2319 * If the number of arguments does not macht, we need not
2324 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2326 for (arg = args; arg != NULL; arg = arg->s_nxt)
2328 if (narg != nparg) {
2329 /* parameter mismatch: %d declared, %d defined */
2330 error(51, nparg, narg);
2336 msg |= chkptdecl(arg, parg);
2342 /* prototype declaration */
2343 prevdecl(285, dcs->d_rdcsym);
2345 /* from now the prototype is valid */
2346 funcsym->s_osdef = 0;
2347 funcsym->s_args = NULL;
2354 * Checks compatibility of an old style function definition with a previous
2355 * prototype declaration.
2356 * Returns 1 if the position of the previous declaration should be reported.
2359 chkptdecl(sym_t *arg, sym_t *parg)
2370 if (!eqtype(tp, ptp, 1, 1, &warn)) {
2371 if (eqtype(tp, ptp, 1, 0, &warn)) {
2372 /* type does not match prototype: %s */
2373 msg = gnuism(58, arg->s_name);
2375 /* type does not match prototype: %s */
2376 error(58, arg->s_name);
2380 /* type does not match prototype: %s */
2381 (*(sflag ? error : warning))(58, arg->s_name);
2389 * Completes a single local declaration/definition.
2392 decl1loc(sym_t *dsym, int initflg)
2395 /* Correct a mistake done in dname(). */
2396 if (dsym->s_type->t_tspec == FUNC) {
2398 if (dcs->d_scl == NOSCL)
2399 dsym->s_scl = EXTERN;
2402 if (dsym->s_type->t_tspec == FUNC) {
2403 if (dsym->s_scl == STATIC) {
2404 /* dubious static function at block level: %s */
2405 warning(93, dsym->s_name);
2406 dsym->s_scl = EXTERN;
2407 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2408 /* function has illegal storage class: %s */
2409 error(94, dsym->s_name);
2410 dsym->s_scl = EXTERN;
2415 * functions may be declared inline at local scope, although
2416 * this has no effect for a later definition of the same
2418 * XXX it should have an effect if tflag is set. this would
2419 * also be the way gcc behaves.
2421 if (dcs->d_inline) {
2422 if (dsym->s_type->t_tspec == FUNC) {
2425 /* variable declared inline: %s */
2426 warning(268, dsym->s_name);
2434 if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN)
2437 if (dsym->s_scl == EXTERN) {
2439 * XXX wenn die statische Variable auf Ebene 0 erst
2440 * spaeter definiert wird, haben wir die Brille auf.
2442 if (dsym->s_xsym == NULL) {
2443 outsym(dsym, EXTERN, dsym->s_def);
2445 outsym(dsym, dsym->s_xsym->s_scl, dsym->s_def);
2449 if (dcs->d_rdcsym != NULL) {
2451 if (dcs->d_rdcsym->s_blklev == 0) {
2453 switch (dsym->s_scl) {
2455 /* automatic hides external declaration: %s */
2457 warning(86, dsym->s_name);
2460 /* static hides external declaration: %s */
2462 warning(87, dsym->s_name);
2465 /* typedef hides external declaration: %s */
2467 warning(88, dsym->s_name);
2471 * Warnings and errors are printed in ledecl()
2475 lerror("decl1loc() 1");
2478 } else if (dcs->d_rdcsym->s_blklev == blklev) {
2480 /* no hflag, because its illegal! */
2481 if (dcs->d_rdcsym->s_arg) {
2483 * if !tflag, a "redeclaration of %s" error
2488 /* decl. hides parameter: %s */
2489 warning(91, dsym->s_name);
2490 rmsym(dcs->d_rdcsym);
2494 } else if (dcs->d_rdcsym->s_blklev < blklev) {
2497 /* declaration hides earlier one: %s */
2498 warning(95, dsym->s_name);
2502 if (dcs->d_rdcsym->s_blklev == blklev) {
2504 /* redeclaration of %s */
2505 error(27, dsym->s_name);
2506 rmsym(dcs->d_rdcsym);
2512 if (initflg && !(initerr = chkinit(dsym))) {
2517 if (dsym->s_scl == TYPEDEF) {
2518 dsym->s_type = duptyp(dsym->s_type);
2519 dsym->s_type->t_typedef = 1;
2520 settdsym(dsym->s_type, dsym);
2524 * Before we can check the size we must wait for an initialisation
2530 * Processes (re)declarations of external Symbols inside blocks.
2538 /* look for a symbol with the same name */
2539 esym = dcs->d_rdcsym;
2540 while (esym != NULL && esym->s_blklev != 0) {
2541 while ((esym = esym->s_link) != NULL) {
2542 if (esym->s_kind != FVFT)
2544 if (strcmp(dsym->s_name, esym->s_name) == 0)
2550 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2551 /* gcc accepts this without a warning, pcc prints an error. */
2552 /* redeclaration of %s */
2553 warning(27, dsym->s_name);
2559 eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &warn);
2562 if (esym->s_scl == EXTERN) {
2563 /* inconsistent redeclaration of extern: %s */
2564 warning(90, dsym->s_name);
2567 /* inconsistent redeclaration of static: %s */
2568 warning(92, dsym->s_name);
2575 * Remember the external symbol so we can update usage
2576 * information at the end of the block.
2578 dsym->s_xsym = esym;
2583 * Print an error or a warning if the symbol cant be initialized due
2584 * to type/storage class. Returnvalue is 1 if an error has been
2594 if (sym->s_type->t_tspec == FUNC) {
2595 /* cannot initialize function: %s */
2596 error(24, sym->s_name);
2598 } else if (sym->s_scl == TYPEDEF) {
2599 /* cannot initialize typedef: %s */
2600 error(25, sym->s_name);
2602 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2603 /* cannot initialize "extern" declaration: %s */
2604 if (dcs->d_ctx == EXTERN) {
2605 warning(26, sym->s_name);
2607 error(26, sym->s_name);
2616 * Create a symbole for an abstract declaration.
2623 if (dcs->d_ctx != ABSTRACT && dcs->d_ctx != PARG)
2626 sym = getblk(sizeof (sym_t));
2628 sym->s_name = unnamed;
2630 sym->s_scl = ABSTRACT;
2633 if (dcs->d_ctx == PARG)
2636 sym->s_type = dcs->d_type;
2637 dcs->d_rdcsym = NULL;
2644 * Removes anything which has nothing to do on global level.
2650 while (dcs->d_nxt != NULL)
2658 * remove all informations about pending lint directives without
2665 * Process an abstract type declaration
2668 decl1abs(sym_t *sym)
2677 * Checks size after declarations of variables and their initialisation.
2684 * check size only for symbols which are defined and no function and
2687 if (dsym->s_def != DEF)
2689 if (dsym->s_scl == TYPEDEF)
2691 if (dsym->s_type->t_tspec == FUNC)
2694 if (length(dsym->s_type, dsym->s_name) == 0 &&
2695 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2696 /* empty array declaration: %s */
2698 warning(190, dsym->s_name);
2700 error(190, dsym->s_name);
2706 * Mark an object as set if it is not already
2714 UNIQUE_CURR_POS(sym->s_spos);
2719 * Mark an object as used if it is not already
2722 setuflg(sym_t *sym, int fcall, int szof)
2727 UNIQUE_CURR_POS(sym->s_upos);
2730 * for function calls another record is written
2732 * XXX Should symbols used in sizeof() treated as used or not?
2733 * Probably not, because there is no sense to declare an
2734 * external variable only to get their size.
2736 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2741 * Prints warnings for a list of variables and labels (concatenated
2742 * with s_dlnxt) if these are not used or only set.
2745 chkusage(dinfo_t *di)
2750 /* for this warnings LINTED has no effect */
2754 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
2755 chkusg1(di->d_asm, sym);
2761 * Prints a warning for a single variable or label if it is not used or
2765 chkusg1(int novar, sym_t *sym)
2769 if (sym->s_blklev == -1)
2772 STRUCT_ASSIGN(cpos, curr_pos);
2774 if (sym->s_kind == FVFT) {
2776 chkausg(novar, sym);
2778 chkvusg(novar, sym);
2780 } else if (sym->s_kind == FLAB) {
2782 } else if (sym->s_kind == FTAG) {
2786 STRUCT_ASSIGN(curr_pos, cpos);
2790 chkausg(int novar, sym_t *arg)
2794 lerror("chkausg() 1");
2799 if (!arg->s_used && vflag) {
2800 STRUCT_ASSIGN(curr_pos, arg->s_dpos);
2801 /* argument %s unused in function %s */
2802 warning(231, arg->s_name, funcsym->s_name);
2807 chkvusg(int novar, sym_t *sym)
2812 if (blklev == 0 || sym->s_blklev == 0)
2813 lerror("chkvusg() 1");
2815 /* errors in expressions easily cause lots of these warnings */
2820 * XXX Only variables are checkd, although types should
2821 * probably also be checked
2823 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
2824 sc != AUTO && sc != REG) {
2832 if (!sym->s_used && !sym->s_set) {
2833 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2834 /* %s unused in function %s */
2835 warning(192, sym->s_name, funcsym->s_name);
2838 if (sym->s_set && !sym->s_used) {
2839 STRUCT_ASSIGN(curr_pos, sym->s_spos);
2840 /* %s set but not used in function %s */
2841 warning(191, sym->s_name, funcsym->s_name);
2842 } else if (!sym->s_used) {
2843 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2844 /* %s unused in function %s */
2845 warning(192, sym->s_name, funcsym->s_name);
2851 * information about usage is taken over into the symbol
2852 * tabel entry at level 0 if the symbol was locally declared
2853 * as an external symbol.
2855 * XXX This is wrong for symbols declared static at level 0
2856 * if the usage information stems from sizeof(). This is
2857 * because symbols at level 0 only used in sizeof() are
2858 * considered to not be used.
2860 if ((xsym = sym->s_xsym) != NULL) {
2861 if (sym->s_used && !xsym->s_used) {
2863 STRUCT_ASSIGN(xsym->s_upos, sym->s_upos);
2865 if (sym->s_set && !xsym->s_set) {
2867 STRUCT_ASSIGN(xsym->s_spos, sym->s_spos);
2877 if (blklev != 1 || lab->s_blklev != 1)
2878 lerror("chklusg() 1");
2880 if (lab->s_set && !lab->s_used) {
2881 STRUCT_ASSIGN(curr_pos, lab->s_spos);
2882 /* label %s unused in function %s */
2883 warning(192, lab->s_name, funcsym->s_name);
2884 } else if (!lab->s_set) {
2885 STRUCT_ASSIGN(curr_pos, lab->s_upos);
2886 /* undefined label %s */
2887 warning(23, lab->s_name);
2895 if (!incompl(sym->s_type))
2898 /* complain alwasy about incomplet tags declared inside blocks */
2899 if (!zflag || dcs->d_ctx != EXTERN)
2902 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2903 switch (sym->s_type->t_tspec) {
2905 /* struct %s never defined */
2906 warning(233, sym->s_name);
2909 /* union %s never defined */
2910 warning(234, sym->s_name);
2913 /* enum %s never defined */
2914 warning(235, sym->s_name);
2917 lerror("chktusg() 1");
2922 * Called after the entire translation unit has been parsed.
2923 * Changes tentative definitions in definitions.
2924 * Performs some tests on global Symbols. Detected Problems are:
2925 * - defined variables of incomplete type
2926 * - constant variables which are not initialized
2927 * - static symbols which are never used
2935 if (blklev != 0 || dcs->d_nxt != NULL)
2938 STRUCT_ASSIGN(cpos, curr_pos);
2940 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
2941 if (sym->s_blklev == -1)
2943 if (sym->s_kind == FVFT) {
2945 } else if (sym->s_kind == FTAG) {
2948 if (sym->s_kind != FMOS)
2949 lerror("chkglsyms() 1");
2953 STRUCT_ASSIGN(curr_pos, cpos);
2957 chkglvar(sym_t *sym)
2960 if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON)
2963 if (sym->s_scl != EXTERN && sym->s_scl != STATIC)
2964 lerror("chkglvar() 1");
2968 if (sym->s_scl == STATIC) {
2969 if (sym->s_type->t_tspec == FUNC) {
2970 if (sym->s_used && sym->s_def != DEF) {
2971 STRUCT_ASSIGN(curr_pos, sym->s_upos);
2972 /* static func. called but not def.. */
2973 error(225, sym->s_name);
2977 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2978 if (sym->s_type->t_tspec == FUNC) {
2979 if (sym->s_def == DEF) {
2981 /* static function %s unused */
2982 warning(236, sym->s_name);
2984 /* static function %s decl. but ... */
2985 warning(290, sym->s_name);
2987 } else if (!sym->s_set) {
2988 /* static variable %s unused */
2989 warning(226, sym->s_name);
2991 /* static variable %s set but not used */
2992 warning(307, sym->s_name);
2995 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
2996 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
2997 /* const object %s should have initializer */
2998 warning(227, sym->s_name);
3007 if (sym->s_def == TDEF) {
3008 if (sym->s_type->t_tspec == FUNC)
3010 * this can happen if a syntax error occurred
3011 * after a function declaration
3014 STRUCT_ASSIGN(curr_pos, sym->s_dpos);
3015 if (length(sym->s_type, sym->s_name) == 0 &&
3016 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3017 /* empty array declaration: %s */
3018 if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3019 warning(190, sym->s_name);
3021 error(190, sym->s_name);
3028 * Prints information about location of previous definition/declaration.
3031 prevdecl(int msg, sym_t *psym)
3038 STRUCT_ASSIGN(cpos, curr_pos);
3039 STRUCT_ASSIGN(curr_pos, psym->s_dpos);
3041 message(msg, psym->s_name);
3042 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3043 /* previous definition of %s */
3044 message(261, psym->s_name);
3046 /* previous declaration of %s */
3047 message(260, psym->s_name);
3049 STRUCT_ASSIGN(curr_pos, cpos);