| @@ -1,2120 +1,2120 @@ | | | @@ -1,2120 +1,2120 @@ |
1 | /* $NetBSD: decl.c,v 1.98 2021/01/02 03:49:25 rillig Exp $ */ | | 1 | /* $NetBSD: decl.c,v 1.99 2021/01/02 16:33:39 rillig Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. | | 4 | * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. |
5 | * Copyright (c) 1994, 1995 Jochen Pohl | | 5 | * Copyright (c) 1994, 1995 Jochen Pohl |
6 | * All Rights Reserved. | | 6 | * All Rights Reserved. |
7 | * | | 7 | * |
8 | * Redistribution and use in source and binary forms, with or without | | 8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions | | 9 | * modification, are permitted provided that the following conditions |
10 | * are met: | | 10 | * are met: |
11 | * 1. Redistributions of source code must retain the above copyright | | 11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. | | 12 | * notice, this list of conditions and the following disclaimer. |
13 | * 2. Redistributions in binary form must reproduce the above copyright | | 13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the | | 14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. | | 15 | * documentation and/or other materials provided with the distribution. |
16 | * 3. All advertising materials mentioning features or use of this software | | 16 | * 3. All advertising materials mentioning features or use of this software |
17 | * must display the following acknowledgement: | | 17 | * must display the following acknowledgement: |
18 | * This product includes software developed by Jochen Pohl for | | 18 | * This product includes software developed by Jochen Pohl for |
19 | * The NetBSD Project. | | 19 | * The NetBSD Project. |
20 | * 4. The name of the author may not be used to endorse or promote products | | 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. | | 21 | * derived from this software without specific prior written permission. |
22 | * | | 22 | * |
23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | | 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 | | 24 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
25 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | | 25 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
26 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | | 26 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
27 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | | 27 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
28 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | | 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 | | 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | | 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 | | 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. | | 32 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
33 | */ | | 33 | */ |
34 | | | 34 | |
35 | #if HAVE_NBTOOL_CONFIG_H | | 35 | #if HAVE_NBTOOL_CONFIG_H |
36 | #include "nbtool_config.h" | | 36 | #include "nbtool_config.h" |
37 | #endif | | 37 | #endif |
38 | | | 38 | |
39 | #include <sys/cdefs.h> | | 39 | #include <sys/cdefs.h> |
40 | #if defined(__RCSID) && !defined(lint) | | 40 | #if defined(__RCSID) && !defined(lint) |
41 | __RCSID("$NetBSD: decl.c,v 1.98 2021/01/02 03:49:25 rillig Exp $"); | | 41 | __RCSID("$NetBSD: decl.c,v 1.99 2021/01/02 16:33:39 rillig Exp $"); |
42 | #endif | | 42 | #endif |
43 | | | 43 | |
44 | #include <sys/param.h> | | 44 | #include <sys/param.h> |
45 | #include <limits.h> | | 45 | #include <limits.h> |
46 | #include <stdlib.h> | | 46 | #include <stdlib.h> |
47 | #include <string.h> | | 47 | #include <string.h> |
48 | | | 48 | |
49 | #include "lint1.h" | | 49 | #include "lint1.h" |
50 | | | 50 | |
51 | const char *unnamed = "<unnamed>"; | | 51 | const char *unnamed = "<unnamed>"; |
52 | | | 52 | |
53 | /* shared type structures for arithmtic types and void */ | | 53 | /* shared type structures for arithmtic types and void */ |
54 | static type_t *typetab; | | 54 | static type_t *typetab; |
55 | | | 55 | |
56 | /* value of next enumerator during declaration of enum types */ | | 56 | /* value of next enumerator during declaration of enum types */ |
57 | int enumval; | | 57 | int enumval; |
58 | | | 58 | |
59 | /* | | 59 | /* |
60 | * pointer to top element of a stack which contains information local | | 60 | * pointer to top element of a stack which contains information local |
61 | * to nested declarations | | 61 | * to nested declarations |
62 | */ | | 62 | */ |
63 | dinfo_t *dcs; | | 63 | dinfo_t *dcs; |
64 | | | 64 | |
65 | static type_t *tdeferr(type_t *, tspec_t); | | 65 | static type_t *tdeferr(type_t *, tspec_t); |
66 | static void settdsym(type_t *, sym_t *); | | 66 | static void settdsym(type_t *, sym_t *); |
67 | static tspec_t merge_type_specifiers(tspec_t, tspec_t); | | 67 | static tspec_t merge_type_specifiers(tspec_t, tspec_t); |
68 | static void align(int, int); | | 68 | static void align(int, int); |
69 | static sym_t *newtag(sym_t *, scl_t, int, int); | | 69 | static sym_t *newtag(sym_t *, scl_t, int, int); |
70 | static int eqargs(type_t *, type_t *, int *); | | 70 | static int eqargs(type_t *, type_t *, int *); |
71 | static int mnoarg(type_t *, int *); | | 71 | static int mnoarg(type_t *, int *); |
72 | static int check_old_style_definition(sym_t *, sym_t *); | | 72 | static int check_old_style_definition(sym_t *, sym_t *); |
73 | static int check_prototype_declaration(sym_t *, sym_t *); | | 73 | static int check_prototype_declaration(sym_t *, sym_t *); |
74 | static sym_t *new_style_function(sym_t *, sym_t *); | | 74 | static sym_t *new_style_function(sym_t *, sym_t *); |
75 | static void old_style_function(sym_t *, sym_t *); | | 75 | static void old_style_function(sym_t *, sym_t *); |
76 | static void ledecl(sym_t *); | | 76 | static void ledecl(sym_t *); |
77 | static int check_init(sym_t *); | | 77 | static int check_init(sym_t *); |
78 | static void check_argument_usage(int, sym_t *); | | 78 | static void check_argument_usage(int, sym_t *); |
79 | static void check_variable_usage(int, sym_t *); | | 79 | static void check_variable_usage(int, sym_t *); |
80 | static void check_label_usage(sym_t *); | | 80 | static void check_label_usage(sym_t *); |
81 | static void check_tag_usage(sym_t *); | | 81 | static void check_tag_usage(sym_t *); |
82 | static void check_global_variable(sym_t *); | | 82 | static void check_global_variable(sym_t *); |
83 | static void check_global_variable_size(sym_t *); | | 83 | static void check_global_variable_size(sym_t *); |
84 | | | 84 | |
85 | /* | | 85 | /* |
86 | * initializes all global vars used in declarations | | 86 | * initializes all global vars used in declarations |
87 | */ | | 87 | */ |
88 | void | | 88 | void |
89 | initdecl(void) | | 89 | initdecl(void) |
90 | { | | 90 | { |
91 | int i; | | 91 | int i; |
92 | | | 92 | |
93 | /* declaration stack */ | | 93 | /* declaration stack */ |
94 | dcs = xcalloc(1, sizeof (dinfo_t)); | | 94 | dcs = xcalloc(1, sizeof (dinfo_t)); |
95 | dcs->d_ctx = EXTERN; | | 95 | dcs->d_ctx = EXTERN; |
96 | dcs->d_ldlsym = &dcs->d_dlsyms; | | 96 | dcs->d_ldlsym = &dcs->d_dlsyms; |
97 | | | 97 | |
98 | /* type information and classification */ | | 98 | /* type information and classification */ |
99 | inittyp(); | | 99 | inittyp(); |
100 | | | 100 | |
101 | /* shared type structures */ | | 101 | /* shared type structures */ |
102 | typetab = xcalloc(NTSPEC, sizeof (type_t)); | | 102 | typetab = xcalloc(NTSPEC, sizeof (type_t)); |
103 | for (i = 0; i < NTSPEC; i++) | | 103 | for (i = 0; i < NTSPEC; i++) |
104 | typetab[i].t_tspec = NOTSPEC; | | 104 | typetab[i].t_tspec = NOTSPEC; |
105 | typetab[BOOL].t_tspec = BOOL; | | 105 | typetab[BOOL].t_tspec = BOOL; |
106 | typetab[CHAR].t_tspec = CHAR; | | 106 | typetab[CHAR].t_tspec = CHAR; |
107 | typetab[SCHAR].t_tspec = SCHAR; | | 107 | typetab[SCHAR].t_tspec = SCHAR; |
108 | typetab[UCHAR].t_tspec = UCHAR; | | 108 | typetab[UCHAR].t_tspec = UCHAR; |
109 | typetab[SHORT].t_tspec = SHORT; | | 109 | typetab[SHORT].t_tspec = SHORT; |
110 | typetab[USHORT].t_tspec = USHORT; | | 110 | typetab[USHORT].t_tspec = USHORT; |
111 | typetab[INT].t_tspec = INT; | | 111 | typetab[INT].t_tspec = INT; |
112 | typetab[UINT].t_tspec = UINT; | | 112 | typetab[UINT].t_tspec = UINT; |
113 | typetab[LONG].t_tspec = LONG; | | 113 | typetab[LONG].t_tspec = LONG; |
114 | typetab[ULONG].t_tspec = ULONG; | | 114 | typetab[ULONG].t_tspec = ULONG; |
115 | typetab[QUAD].t_tspec = QUAD; | | 115 | typetab[QUAD].t_tspec = QUAD; |
116 | typetab[UQUAD].t_tspec = UQUAD; | | 116 | typetab[UQUAD].t_tspec = UQUAD; |
117 | typetab[FLOAT].t_tspec = FLOAT; | | 117 | typetab[FLOAT].t_tspec = FLOAT; |
118 | typetab[DOUBLE].t_tspec = DOUBLE; | | 118 | typetab[DOUBLE].t_tspec = DOUBLE; |
119 | typetab[LDOUBLE].t_tspec = LDOUBLE; | | 119 | typetab[LDOUBLE].t_tspec = LDOUBLE; |
120 | typetab[FCOMPLEX].t_tspec = FCOMPLEX; | | 120 | typetab[FCOMPLEX].t_tspec = FCOMPLEX; |
121 | typetab[DCOMPLEX].t_tspec = DCOMPLEX; | | 121 | typetab[DCOMPLEX].t_tspec = DCOMPLEX; |
122 | typetab[LCOMPLEX].t_tspec = LCOMPLEX; | | 122 | typetab[LCOMPLEX].t_tspec = LCOMPLEX; |
123 | typetab[COMPLEX].t_tspec = COMPLEX; | | 123 | typetab[COMPLEX].t_tspec = COMPLEX; |
124 | typetab[VOID].t_tspec = VOID; | | 124 | typetab[VOID].t_tspec = VOID; |
125 | /* | | 125 | /* |
126 | * Next two are not real types. They are only used by the parser | | 126 | * Next two are not real types. They are only used by the parser |
127 | * to return keywords "signed" and "unsigned" | | 127 | * to return keywords "signed" and "unsigned" |
128 | */ | | 128 | */ |
129 | typetab[SIGNED].t_tspec = SIGNED; | | 129 | typetab[SIGNED].t_tspec = SIGNED; |
130 | typetab[UNSIGN].t_tspec = UNSIGN; | | 130 | typetab[UNSIGN].t_tspec = UNSIGN; |
131 | } | | 131 | } |
132 | | | 132 | |
133 | /* | | 133 | /* |
134 | * Returns a shared type structure for arithmetic types and void. | | 134 | * Returns a shared type structure for arithmetic types and void. |
135 | * | | 135 | * |
136 | * It's important to duplicate this structure (using duptyp() or tduptyp()) | | 136 | * It's important to duplicate this structure (using duptyp() or tduptyp()) |
137 | * if it is to be modified (adding qualifiers or anything else). | | 137 | * if it is to be modified (adding qualifiers or anything else). |
138 | */ | | 138 | */ |
139 | type_t * | | 139 | type_t * |
140 | gettyp(tspec_t t) | | 140 | gettyp(tspec_t t) |
141 | { | | 141 | { |
142 | | | 142 | |
143 | return &typetab[t]; | | 143 | return &typetab[t]; |
144 | } | | 144 | } |
145 | | | 145 | |
146 | type_t * | | 146 | type_t * |
147 | duptyp(const type_t *tp) | | 147 | duptyp(const type_t *tp) |
148 | { | | 148 | { |
149 | type_t *ntp; | | 149 | type_t *ntp; |
150 | | | 150 | |
151 | ntp = getblk(sizeof (type_t)); | | 151 | ntp = getblk(sizeof (type_t)); |
152 | *ntp = *tp; | | 152 | *ntp = *tp; |
153 | return ntp; | | 153 | return ntp; |
154 | } | | 154 | } |
155 | | | 155 | |
156 | /* | | 156 | /* |
157 | * Use tduptyp() instead of duptyp() inside expressions (if the | | 157 | * Use tduptyp() instead of duptyp() inside expressions (if the |
158 | * allocated memory should be freed after the expr). | | 158 | * allocated memory should be freed after the expr). |
159 | */ | | 159 | */ |
160 | type_t * | | 160 | type_t * |
161 | tduptyp(const type_t *tp) | | 161 | tduptyp(const type_t *tp) |
162 | { | | 162 | { |
163 | type_t *ntp; | | 163 | type_t *ntp; |
164 | | | 164 | |
165 | ntp = tgetblk(sizeof (type_t)); | | 165 | ntp = tgetblk(sizeof (type_t)); |
166 | *ntp = *tp; | | 166 | *ntp = *tp; |
167 | return ntp; | | 167 | return ntp; |
168 | } | | 168 | } |
169 | | | 169 | |
170 | /* | | 170 | /* |
171 | * Returns 1 if the argument is void or an incomplete array, | | 171 | * Returns 1 if the argument is void or an incomplete array, |
172 | * struct, union or enum type. | | 172 | * struct, union or enum type. |
173 | */ | | 173 | */ |
174 | int | | 174 | int |
175 | incompl(type_t *tp) | | 175 | incompl(type_t *tp) |
176 | { | | 176 | { |
177 | tspec_t t; | | 177 | tspec_t t; |
178 | | | 178 | |
179 | if ((t = tp->t_tspec) == VOID) { | | 179 | if ((t = tp->t_tspec) == VOID) { |
180 | return 1; | | 180 | return 1; |
181 | } else if (t == ARRAY) { | | 181 | } else if (t == ARRAY) { |
182 | return tp->t_aincompl; | | 182 | return tp->t_aincompl; |
183 | } else if (t == STRUCT || t == UNION) { | | 183 | } else if (t == STRUCT || t == UNION) { |
184 | return tp->t_str->sincompl; | | 184 | return tp->t_str->sincompl; |
185 | } else if (t == ENUM) { | | 185 | } else if (t == ENUM) { |
186 | return tp->t_enum->eincompl; | | 186 | return tp->t_enum->eincompl; |
187 | } | | 187 | } |
188 | return 0; | | 188 | return 0; |
189 | } | | 189 | } |
190 | | | 190 | |
191 | /* | | 191 | /* |
192 | * Mark an array, struct, union or enum type as complete or incomplete. | | 192 | * Mark an array, struct, union or enum type as complete or incomplete. |
193 | */ | | 193 | */ |
194 | void | | 194 | void |
195 | setcomplete(type_t *tp, int complete) | | 195 | setcomplete(type_t *tp, int complete) |
196 | { | | 196 | { |
197 | tspec_t t; | | 197 | tspec_t t; |
198 | | | 198 | |
199 | if ((t = tp->t_tspec) == ARRAY) { | | 199 | if ((t = tp->t_tspec) == ARRAY) { |
200 | tp->t_aincompl = !complete; | | 200 | tp->t_aincompl = !complete; |
201 | } else if (t == STRUCT || t == UNION) { | | 201 | } else if (t == STRUCT || t == UNION) { |
202 | tp->t_str->sincompl = !complete; | | 202 | tp->t_str->sincompl = !complete; |
203 | } else { | | 203 | } else { |
204 | lint_assert(t == ENUM); | | 204 | lint_assert(t == ENUM); |
205 | tp->t_enum->eincompl = !complete; | | 205 | tp->t_enum->eincompl = !complete; |
206 | } | | 206 | } |
207 | } | | 207 | } |
208 | | | 208 | |
209 | /* | | 209 | /* |
210 | * Remember the storage class of the current declaration in dcs->d_scl | | 210 | * Remember the storage class of the current declaration in dcs->d_scl |
211 | * (the top element of the declaration stack) and detect multiple | | 211 | * (the top element of the declaration stack) and detect multiple |
212 | * storage classes. | | 212 | * storage classes. |
213 | */ | | 213 | */ |
214 | void | | 214 | void |
215 | add_storage_class(scl_t sc) | | 215 | add_storage_class(scl_t sc) |
216 | { | | 216 | { |
217 | | | 217 | |
218 | if (sc == INLINE) { | | 218 | if (sc == INLINE) { |
219 | if (dcs->d_inline) | | 219 | if (dcs->d_inline) |
220 | /* duplicate '%s' */ | | 220 | /* duplicate '%s' */ |
221 | warning(10, "inline"); | | 221 | warning(10, "inline"); |
222 | dcs->d_inline = 1; | | 222 | dcs->d_inline = 1; |
223 | return; | | 223 | return; |
224 | } | | 224 | } |
225 | if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC || | | 225 | if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC || |
226 | dcs->d_smod != NOTSPEC || dcs->d_lmod != NOTSPEC) { | | 226 | dcs->d_smod != NOTSPEC || dcs->d_lmod != NOTSPEC) { |
227 | /* storage class after type is obsolescent */ | | 227 | /* storage class after type is obsolescent */ |
228 | warning(83); | | 228 | warning(83); |
229 | } | | 229 | } |
230 | if (dcs->d_scl == NOSCL) { | | 230 | if (dcs->d_scl == NOSCL) { |
231 | dcs->d_scl = sc; | | 231 | dcs->d_scl = sc; |
232 | } else { | | 232 | } else { |
233 | /* | | 233 | /* |
234 | * multiple storage classes. An error will be reported in | | 234 | * multiple storage classes. An error will be reported in |
235 | * deftyp(). | | 235 | * deftyp(). |
236 | */ | | 236 | */ |
237 | dcs->d_mscl = 1; | | 237 | dcs->d_mscl = 1; |
238 | } | | 238 | } |
239 | } | | 239 | } |
240 | | | 240 | |
241 | /* | | 241 | /* |
242 | * Remember the type, modifier or typedef name returned by the parser | | 242 | * Remember the type, modifier or typedef name returned by the parser |
243 | * in *dcs (top element of decl stack). This information is used in | | 243 | * in *dcs (top element of decl stack). This information is used in |
244 | * deftyp() to build the type used for all declarators in this | | 244 | * deftyp() to build the type used for all declarators in this |
245 | * declaration. | | 245 | * declaration. |
246 | * | | 246 | * |
247 | * If tp->t_typedef is 1, the type comes from a previously defined typename. | | 247 | * If tp->t_typedef is 1, the type comes from a previously defined typename. |
248 | * Otherwise it comes from a type specifier (int, long, ...) or a | | 248 | * Otherwise it comes from a type specifier (int, long, ...) or a |
249 | * struct/union/enum tag. | | 249 | * struct/union/enum tag. |
250 | */ | | 250 | */ |
251 | void | | 251 | void |
252 | add_type(type_t *tp) | | 252 | add_type(type_t *tp) |
253 | { | | 253 | { |
254 | tspec_t t; | | 254 | tspec_t t; |
255 | #ifdef DEBUG | | 255 | #ifdef DEBUG |
256 | printf("%s: %s\n", __func__, type_name(tp)); | | 256 | printf("%s: %s\n", __func__, type_name(tp)); |
257 | #endif | | 257 | #endif |
258 | if (tp->t_typedef) { | | 258 | if (tp->t_typedef) { |
259 | /* | | 259 | /* |
260 | * something like "typedef int a; int a b;" | | 260 | * something like "typedef int a; int a b;" |
261 | * This should not happen with current grammar. | | 261 | * This should not happen with current grammar. |
262 | */ | | 262 | */ |
263 | lint_assert(dcs->d_type == NULL); | | 263 | lint_assert(dcs->d_type == NULL); |
264 | lint_assert(dcs->d_atyp == NOTSPEC); | | 264 | lint_assert(dcs->d_atyp == NOTSPEC); |
265 | lint_assert(dcs->d_lmod == NOTSPEC); | | 265 | lint_assert(dcs->d_lmod == NOTSPEC); |
266 | lint_assert(dcs->d_smod == NOTSPEC); | | 266 | lint_assert(dcs->d_smod == NOTSPEC); |
267 | | | 267 | |
268 | dcs->d_type = tp; | | 268 | dcs->d_type = tp; |
269 | return; | | 269 | return; |
270 | } | | 270 | } |
271 | | | 271 | |
272 | t = tp->t_tspec; | | 272 | t = tp->t_tspec; |
273 | | | 273 | |
274 | if (t == STRUCT || t == UNION || t == ENUM) { | | 274 | if (t == STRUCT || t == UNION || t == ENUM) { |
275 | /* | | 275 | /* |
276 | * something like "int struct a ..." | | 276 | * something like "int struct a ..." |
277 | * struct/union/enum with anything else is not allowed | | 277 | * struct/union/enum with anything else is not allowed |
278 | */ | | 278 | */ |
279 | if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC || | | 279 | if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC || |
280 | dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) { | | 280 | dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) { |
281 | /* | | 281 | /* |
282 | * remember that an error must be reported in | | 282 | * remember that an error must be reported in |
283 | * deftyp(). | | 283 | * deftyp(). |
284 | */ | | 284 | */ |
285 | dcs->d_terr = 1; | | 285 | dcs->d_terr = 1; |
286 | dcs->d_atyp = dcs->d_lmod = dcs->d_smod = NOTSPEC; | | 286 | dcs->d_atyp = dcs->d_lmod = dcs->d_smod = NOTSPEC; |
287 | } | | 287 | } |
288 | dcs->d_type = tp; | | 288 | dcs->d_type = tp; |
289 | return; | | 289 | return; |
290 | } | | 290 | } |
291 | | | 291 | |
292 | if (dcs->d_type != NULL && !dcs->d_type->t_typedef) { | | 292 | if (dcs->d_type != NULL && !dcs->d_type->t_typedef) { |
293 | /* | | 293 | /* |
294 | * something like "struct a int" | | 294 | * something like "struct a int" |
295 | * struct/union/enum with anything else is not allowed | | 295 | * struct/union/enum with anything else is not allowed |
296 | */ | | 296 | */ |
297 | dcs->d_terr = 1; | | 297 | dcs->d_terr = 1; |
298 | return; | | 298 | return; |
299 | } | | 299 | } |
300 | | | 300 | |
301 | if (t == COMPLEX) { | | 301 | if (t == COMPLEX) { |
302 | if (dcs->d_cmod == FLOAT) | | 302 | if (dcs->d_cmod == FLOAT) |
303 | t = FCOMPLEX; | | 303 | t = FCOMPLEX; |
304 | else if (dcs->d_cmod == DOUBLE) | | 304 | else if (dcs->d_cmod == DOUBLE) |
305 | t = DCOMPLEX; | | 305 | t = DCOMPLEX; |
306 | else | | 306 | else |
307 | /* Invalid type %s for _Complex */ | | 307 | /* Invalid type %s for _Complex */ |
308 | error(308, tspec_name(dcs->d_cmod)); | | 308 | error(308, tspec_name(dcs->d_cmod)); |
309 | dcs->d_cmod = NOTSPEC; | | 309 | dcs->d_cmod = NOTSPEC; |
310 | } | | 310 | } |
311 | | | 311 | |
312 | if (t == LONG && dcs->d_lmod == LONG) { | | 312 | if (t == LONG && dcs->d_lmod == LONG) { |
313 | /* "long long" or "long ... long" */ | | 313 | /* "long long" or "long ... long" */ |
314 | t = QUAD; | | 314 | t = QUAD; |
315 | dcs->d_lmod = NOTSPEC; | | 315 | dcs->d_lmod = NOTSPEC; |
316 | if (!quadflg) | | 316 | if (!quadflg) |
317 | /* %s C does not support 'long long' */ | | 317 | /* %s C does not support 'long long' */ |
318 | (void)c99ism(265, tflag ? "traditional" : "c89"); | | 318 | (void)c99ism(265, tflag ? "traditional" : "c89"); |
319 | } | | 319 | } |
320 | | | 320 | |
321 | if (dcs->d_type != NULL && dcs->d_type->t_typedef) { | | 321 | if (dcs->d_type != NULL && dcs->d_type->t_typedef) { |
322 | /* something like "typedef int a; a long ..." */ | | 322 | /* something like "typedef int a; a long ..." */ |
323 | dcs->d_type = tdeferr(dcs->d_type, t); | | 323 | dcs->d_type = tdeferr(dcs->d_type, t); |
324 | return; | | 324 | return; |
325 | } | | 325 | } |
326 | | | 326 | |
327 | /* now it can be only a combination of arithmetic types and void */ | | 327 | /* now it can be only a combination of arithmetic types and void */ |
328 | if (t == SIGNED || t == UNSIGN) { | | 328 | if (t == SIGNED || t == UNSIGN) { |
329 | /* remember specifiers "signed" & "unsigned" in dcs->d_smod */ | | 329 | /* remember specifiers "signed" & "unsigned" in dcs->d_smod */ |
330 | if (dcs->d_smod != NOTSPEC) | | 330 | if (dcs->d_smod != NOTSPEC) |
331 | /* | | 331 | /* |
332 | * more than one "signed" and/or "unsigned"; print | | 332 | * more than one "signed" and/or "unsigned"; print |
333 | * an error in deftyp() | | 333 | * an error in deftyp() |
334 | */ | | 334 | */ |
335 | dcs->d_terr = 1; | | 335 | dcs->d_terr = 1; |
336 | dcs->d_smod = t; | | 336 | dcs->d_smod = t; |
337 | } else if (t == SHORT || t == LONG || t == QUAD) { | | 337 | } else if (t == SHORT || t == LONG || t == QUAD) { |
338 | /* | | 338 | /* |
339 | * remember specifiers "short", "long" and "long long" in | | 339 | * remember specifiers "short", "long" and "long long" in |
340 | * dcs->d_lmod | | 340 | * dcs->d_lmod |
341 | */ | | 341 | */ |
342 | if (dcs->d_lmod != NOTSPEC) | | 342 | if (dcs->d_lmod != NOTSPEC) |
343 | /* more than one, print error in deftyp() */ | | 343 | /* more than one, print error in deftyp() */ |
344 | dcs->d_terr = 1; | | 344 | dcs->d_terr = 1; |
345 | dcs->d_lmod = t; | | 345 | dcs->d_lmod = t; |
346 | } else if (t == FLOAT || t == DOUBLE) { | | 346 | } else if (t == FLOAT || t == DOUBLE) { |
347 | if (dcs->d_lmod == NOTSPEC || dcs->d_lmod == LONG) { | | 347 | if (dcs->d_lmod == NOTSPEC || dcs->d_lmod == LONG) { |
348 | if (dcs->d_cmod != NOTSPEC | | 348 | if (dcs->d_cmod != NOTSPEC |
349 | || (t == FLOAT && dcs->d_lmod == LONG)) | | 349 | || (t == FLOAT && dcs->d_lmod == LONG)) |
350 | dcs->d_terr = 1; | | 350 | dcs->d_terr = 1; |
351 | dcs->d_cmod = t; | | 351 | dcs->d_cmod = t; |
352 | } else { | | 352 | } else { |
353 | if (dcs->d_atyp != NOTSPEC) | | 353 | if (dcs->d_atyp != NOTSPEC) |
354 | dcs->d_terr = 1; | | 354 | dcs->d_terr = 1; |
355 | dcs->d_atyp = t; | | 355 | dcs->d_atyp = t; |
356 | } | | 356 | } |
357 | } else if (t == PTR) { | | 357 | } else if (t == PTR) { |
358 | dcs->d_type = tp; | | 358 | dcs->d_type = tp; |
359 | } else { | | 359 | } else { |
360 | /* | | 360 | /* |
361 | * remember specifiers "void", "char", "int", | | 361 | * remember specifiers "void", "char", "int", |
362 | * or "_Complex" int dcs->d_atyp | | 362 | * or "_Complex" int dcs->d_atyp |
363 | */ | | 363 | */ |
364 | if (dcs->d_atyp != NOTSPEC) | | 364 | if (dcs->d_atyp != NOTSPEC) |
365 | /* more than one, print error in deftyp() */ | | 365 | /* more than one, print error in deftyp() */ |
366 | dcs->d_terr = 1; | | 366 | dcs->d_terr = 1; |
367 | dcs->d_atyp = t; | | 367 | dcs->d_atyp = t; |
368 | } | | 368 | } |
369 | } | | 369 | } |
370 | | | 370 | |
371 | /* | | 371 | /* |
372 | * called if a list of declaration specifiers contains a typedef name | | 372 | * called if a list of declaration specifiers contains a typedef name |
373 | * and other specifiers (except struct, union, enum, typedef name) | | 373 | * and other specifiers (except struct, union, enum, typedef name) |
374 | */ | | 374 | */ |
375 | static type_t * | | 375 | static type_t * |
376 | tdeferr(type_t *td, tspec_t t) | | 376 | tdeferr(type_t *td, tspec_t t) |
377 | { | | 377 | { |
378 | tspec_t t2; | | 378 | tspec_t t2; |
379 | | | 379 | |
380 | t2 = td->t_tspec; | | 380 | t2 = td->t_tspec; |
381 | | | 381 | |
382 | switch (t) { | | 382 | switch (t) { |
383 | case SIGNED: | | 383 | case SIGNED: |
384 | case UNSIGN: | | 384 | case UNSIGN: |
385 | if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG || | | 385 | if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG || |
386 | t2 == QUAD) { | | 386 | t2 == QUAD) { |
387 | if (!tflag) | | 387 | if (!tflag) |
388 | /* modifying typedef with ... */ | | 388 | /* modifying typedef with ... */ |
389 | warning(5, ttab[t].tt_name); | | 389 | warning(5, ttab[t].tt_name); |
390 | td = duptyp(gettyp(merge_type_specifiers(t2, t))); | | 390 | td = duptyp(gettyp(merge_type_specifiers(t2, t))); |
391 | td->t_typedef = 1; | | 391 | td->t_typedef = 1; |
392 | return td; | | 392 | return td; |
393 | } | | 393 | } |
394 | break; | | 394 | break; |
395 | case SHORT: | | 395 | case SHORT: |
396 | if (t2 == INT || t2 == UINT) { | | 396 | if (t2 == INT || t2 == UINT) { |
397 | /* modifying typedef with ... */ | | 397 | /* modifying typedef with ... */ |
398 | warning(5, "short"); | | 398 | warning(5, "short"); |
399 | td = duptyp(gettyp(t2 == INT ? SHORT : USHORT)); | | 399 | td = duptyp(gettyp(t2 == INT ? SHORT : USHORT)); |
400 | td->t_typedef = 1; | | 400 | td->t_typedef = 1; |
401 | return td; | | 401 | return td; |
402 | } | | 402 | } |
403 | break; | | 403 | break; |
404 | case LONG: | | 404 | case LONG: |
405 | if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG || | | 405 | if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG || |
406 | t2 == FLOAT || t2 == DOUBLE || t2 == DCOMPLEX) { | | 406 | t2 == FLOAT || t2 == DOUBLE || t2 == DCOMPLEX) { |
407 | /* modifying typedef with ... */ | | 407 | /* modifying typedef with ... */ |
408 | warning(5, "long"); | | 408 | warning(5, "long"); |
409 | if (t2 == INT) { | | 409 | if (t2 == INT) { |
410 | td = gettyp(LONG); | | 410 | td = gettyp(LONG); |
411 | } else if (t2 == UINT) { | | 411 | } else if (t2 == UINT) { |
412 | td = gettyp(ULONG); | | 412 | td = gettyp(ULONG); |
413 | } else if (t2 == LONG) { | | 413 | } else if (t2 == LONG) { |
414 | td = gettyp(QUAD); | | 414 | td = gettyp(QUAD); |
415 | } else if (t2 == ULONG) { | | 415 | } else if (t2 == ULONG) { |
416 | td = gettyp(UQUAD); | | 416 | td = gettyp(UQUAD); |
417 | } else if (t2 == FLOAT) { | | 417 | } else if (t2 == FLOAT) { |
418 | td = gettyp(DOUBLE); | | 418 | td = gettyp(DOUBLE); |
419 | } else if (t2 == DOUBLE) { | | 419 | } else if (t2 == DOUBLE) { |
420 | td = gettyp(LDOUBLE); | | 420 | td = gettyp(LDOUBLE); |
421 | } else if (t2 == DCOMPLEX) { | | 421 | } else if (t2 == DCOMPLEX) { |
422 | td = gettyp(LCOMPLEX); | | 422 | td = gettyp(LCOMPLEX); |
423 | } | | 423 | } |
424 | td = duptyp(td); | | 424 | td = duptyp(td); |
425 | td->t_typedef = 1; | | 425 | td->t_typedef = 1; |
426 | return td; | | 426 | return td; |
427 | } | | 427 | } |
428 | break; | | 428 | break; |
429 | /* LINTED206: (enumeration values not handled in switch) */ | | 429 | /* LINTED206: (enumeration values not handled in switch) */ |
430 | case NOTSPEC: | | 430 | case NOTSPEC: |
431 | case USHORT: | | 431 | case USHORT: |
432 | case UCHAR: | | 432 | case UCHAR: |
433 | case SCHAR: | | 433 | case SCHAR: |
434 | case CHAR: | | 434 | case CHAR: |
435 | case BOOL: | | 435 | case BOOL: |
436 | case FUNC: | | 436 | case FUNC: |
437 | case ARRAY: | | 437 | case ARRAY: |
438 | case PTR: | | 438 | case PTR: |
439 | case ENUM: | | 439 | case ENUM: |
440 | case UNION: | | 440 | case UNION: |
441 | case STRUCT: | | 441 | case STRUCT: |
442 | case VOID: | | 442 | case VOID: |
443 | case LDOUBLE: | | 443 | case LDOUBLE: |
444 | case FLOAT: | | 444 | case FLOAT: |
445 | case DOUBLE: | | 445 | case DOUBLE: |
446 | case UQUAD: | | 446 | case UQUAD: |
447 | case QUAD: | | 447 | case QUAD: |
448 | #ifdef INT128_SIZE | | 448 | #ifdef INT128_SIZE |
449 | case UINT128: | | 449 | case UINT128: |
450 | case INT128: | | 450 | case INT128: |
451 | #endif | | 451 | #endif |
452 | case ULONG: | | 452 | case ULONG: |
453 | case UINT: | | 453 | case UINT: |
454 | case INT: | | 454 | case INT: |
455 | case FCOMPLEX: | | 455 | case FCOMPLEX: |
456 | case DCOMPLEX: | | 456 | case DCOMPLEX: |
457 | case LCOMPLEX: | | 457 | case LCOMPLEX: |
458 | case COMPLEX: | | 458 | case COMPLEX: |
459 | break; | | 459 | break; |
460 | } | | 460 | } |
461 | | | 461 | |
462 | /* Anything other is not accepted. */ | | 462 | /* Anything other is not accepted. */ |
463 | | | 463 | |
464 | dcs->d_terr = 1; | | 464 | dcs->d_terr = 1; |
465 | return td; | | 465 | return td; |
466 | } | | 466 | } |
467 | | | 467 | |
468 | /* | | 468 | /* |
469 | * Remember the symbol of a typedef name (2nd arg) in a struct, union | | 469 | * Remember the symbol of a typedef name (2nd arg) in a struct, union |
470 | * or enum tag if the typedef name is the first defined for this tag. | | 470 | * or enum tag if the typedef name is the first defined for this tag. |
471 | * | | 471 | * |
472 | * If the tag is unnamed, the typdef name is used for identification | | 472 | * If the tag is unnamed, the typdef name is used for identification |
473 | * of this tag in lint2. Although it's possible that more than one typedef | | 473 | * of this tag in lint2. Although it's possible that more than one typedef |
474 | * name is defined for one tag, the first name defined should be unique | | 474 | * name is defined for one tag, the first name defined should be unique |
475 | * if the tag is unnamed. | | 475 | * if the tag is unnamed. |
476 | */ | | 476 | */ |
477 | static void | | 477 | static void |
478 | settdsym(type_t *tp, sym_t *sym) | | 478 | settdsym(type_t *tp, sym_t *sym) |
479 | { | | 479 | { |
480 | tspec_t t; | | 480 | tspec_t t; |
481 | | | 481 | |
482 | if ((t = tp->t_tspec) == STRUCT || t == UNION) { | | 482 | if ((t = tp->t_tspec) == STRUCT || t == UNION) { |
483 | if (tp->t_str->stdef == NULL) | | 483 | if (tp->t_str->stdef == NULL) |
484 | tp->t_str->stdef = sym; | | 484 | tp->t_str->stdef = sym; |
485 | } else if (t == ENUM) { | | 485 | } else if (t == ENUM) { |
486 | if (tp->t_enum->etdef == NULL) | | 486 | if (tp->t_enum->etdef == NULL) |
487 | tp->t_enum->etdef = sym; | | 487 | tp->t_enum->etdef = sym; |
488 | } | | 488 | } |
489 | } | | 489 | } |
490 | | | 490 | |
491 | static size_t | | 491 | static size_t |
492 | bitfieldsize(sym_t **mem) | | 492 | bitfieldsize(sym_t **mem) |
493 | { | | 493 | { |
494 | size_t len = (*mem)->s_type->t_flen; | | 494 | size_t len = (*mem)->s_type->t_flen; |
495 | while (*mem && (*mem)->s_type->t_isfield) { | | 495 | while (*mem && (*mem)->s_type->t_isfield) { |
496 | len += (*mem)->s_type->t_flen; | | 496 | len += (*mem)->s_type->t_flen; |
497 | *mem = (*mem)->s_next; | | 497 | *mem = (*mem)->s_next; |
498 | } | | 498 | } |
499 | return ((len + INT_SIZE - 1) / INT_SIZE) * INT_SIZE; | | 499 | return ((len + INT_SIZE - 1) / INT_SIZE) * INT_SIZE; |
500 | } | | 500 | } |
501 | | | 501 | |
502 | static void | | 502 | static void |
503 | setpackedsize(type_t *tp) | | 503 | setpackedsize(type_t *tp) |
504 | { | | 504 | { |
505 | str_t *sp; | | 505 | str_t *sp; |
506 | sym_t *mem; | | 506 | sym_t *mem; |
507 | | | 507 | |
508 | switch (tp->t_tspec) { | | 508 | switch (tp->t_tspec) { |
509 | case STRUCT: | | 509 | case STRUCT: |
510 | case UNION: | | 510 | case UNION: |
511 | sp = tp->t_str; | | 511 | sp = tp->t_str; |
512 | sp->size = 0; | | 512 | sp->size = 0; |
513 | for (mem = sp->memb; mem != NULL; mem = mem->s_next) { | | 513 | for (mem = sp->memb; mem != NULL; mem = mem->s_next) { |
514 | if (mem->s_type->t_isfield) { | | 514 | if (mem->s_type->t_isfield) { |
515 | sp->size += bitfieldsize(&mem); | | 515 | sp->size += bitfieldsize(&mem); |
516 | if (mem == NULL) | | 516 | if (mem == NULL) |
517 | break; | | 517 | break; |
518 | } | | 518 | } |
519 | size_t x = (size_t)tsize(mem->s_type); | | 519 | size_t x = (size_t)tsize(mem->s_type); |
520 | if (tp->t_tspec == STRUCT) | | 520 | if (tp->t_tspec == STRUCT) |
521 | sp->size += x; | | 521 | sp->size += x; |
522 | else if (x > sp->size) | | 522 | else if (x > sp->size) |
523 | sp->size = x; | | 523 | sp->size = x; |
524 | } | | 524 | } |
525 | break; | | 525 | break; |
526 | default: | | 526 | default: |
527 | /* %s attribute ignored for %s */ | | 527 | /* %s attribute ignored for %s */ |
528 | warning(326, "packed", type_name(tp)); | | 528 | warning(326, "packed", type_name(tp)); |
529 | break; | | 529 | break; |
530 | } | | 530 | } |
531 | } | | 531 | } |
532 | | | 532 | |
533 | void | | 533 | void |
534 | addpacked(void) | | 534 | addpacked(void) |
535 | { | | 535 | { |
536 | if (dcs->d_type == NULL) | | 536 | if (dcs->d_type == NULL) |
537 | dcs->d_ispacked = 1; | | 537 | dcs->d_ispacked = 1; |
538 | else | | 538 | else |
539 | setpackedsize(dcs->d_type); | | 539 | setpackedsize(dcs->d_type); |
540 | } | | 540 | } |
541 | | | 541 | |
542 | void | | 542 | void |
543 | add_attr_used(void) | | 543 | add_attr_used(void) |
544 | { | | 544 | { |
545 | dcs->d_used = 1; | | 545 | dcs->d_used = 1; |
546 | } | | 546 | } |
547 | | | 547 | |
548 | /* | | 548 | /* |
549 | * Remember a qualifier which is part of the declaration specifiers | | 549 | * Remember a qualifier which is part of the declaration specifiers |
550 | * (and not the declarator) in the top element of the declaration stack. | | 550 | * (and not the declarator) in the top element of the declaration stack. |
551 | * Also detect multiple qualifiers of the same kind. | | 551 | * Also detect multiple qualifiers of the same kind. |
552 | | | 552 | |
553 | * The remembered qualifier is used by deftyp() to construct the type | | 553 | * The remembered qualifier is used by deftyp() to construct the type |
554 | * for all declarators. | | 554 | * for all declarators. |
555 | */ | | 555 | */ |
556 | void | | 556 | void |
557 | add_qualifier(tqual_t q) | | 557 | add_qualifier(tqual_t q) |
558 | { | | 558 | { |
559 | | | 559 | |
560 | if (q == CONST) { | | 560 | if (q == CONST) { |
561 | if (dcs->d_const) { | | 561 | if (dcs->d_const) { |
562 | /* duplicate '%s' */ | | 562 | /* duplicate '%s' */ |
563 | warning(10, "const"); | | 563 | warning(10, "const"); |
564 | } | | 564 | } |
565 | dcs->d_const = 1; | | 565 | dcs->d_const = 1; |
566 | } else { | | 566 | } else { |
567 | if (q == THREAD) | | 567 | if (q == THREAD) |
568 | return; | | 568 | return; |
569 | lint_assert(q == VOLATILE); | | 569 | lint_assert(q == VOLATILE); |
570 | if (dcs->d_volatile) { | | 570 | if (dcs->d_volatile) { |
571 | /* duplicate '%s' */ | | 571 | /* duplicate '%s' */ |
572 | warning(10, "volatile"); | | 572 | warning(10, "volatile"); |
573 | } | | 573 | } |
574 | dcs->d_volatile = 1; | | 574 | dcs->d_volatile = 1; |
575 | } | | 575 | } |
576 | } | | 576 | } |
577 | | | 577 | |
578 | /* | | 578 | /* |
579 | * Go to the next declaration level (structs, nested structs, blocks, | | 579 | * Go to the next declaration level (structs, nested structs, blocks, |
580 | * argument declaration lists ...) | | 580 | * argument declaration lists ...) |
581 | */ | | 581 | */ |
582 | void | | 582 | void |
583 | pushdecl(scl_t sc) | | 583 | pushdecl(scl_t sc) |
584 | { | | 584 | { |
585 | dinfo_t *di; | | 585 | dinfo_t *di; |
586 | | | 586 | |
587 | /* put a new element on the declaration stack */ | | 587 | /* put a new element on the declaration stack */ |
588 | di = xcalloc(1, sizeof (dinfo_t)); | | 588 | di = xcalloc(1, sizeof (dinfo_t)); |
589 | di->d_next = dcs; | | 589 | di->d_next = dcs; |
590 | dcs = di; | | 590 | dcs = di; |
591 | di->d_ctx = sc; | | 591 | di->d_ctx = sc; |
592 | di->d_ldlsym = &di->d_dlsyms; | | 592 | di->d_ldlsym = &di->d_dlsyms; |
593 | if (dflag) | | 593 | if (dflag) |
594 | (void)printf("pushdecl(%p %d)\n", dcs, (int)sc); | | 594 | (void)printf("pushdecl(%p %d)\n", dcs, (int)sc); |
595 | | | 595 | |
596 | } | | 596 | } |
597 | | | 597 | |
598 | /* | | 598 | /* |
599 | * Go back to previous declaration level | | 599 | * Go back to previous declaration level |
600 | */ | | 600 | */ |
601 | void | | 601 | void |
602 | popdecl(void) | | 602 | popdecl(void) |
603 | { | | 603 | { |
604 | dinfo_t *di; | | 604 | dinfo_t *di; |
605 | | | 605 | |
606 | if (dflag) | | 606 | if (dflag) |
607 | (void)printf("popdecl(%p %d)\n", dcs, (int)dcs->d_ctx); | | 607 | (void)printf("popdecl(%p %d)\n", dcs, (int)dcs->d_ctx); |
608 | | | 608 | |
609 | lint_assert(dcs->d_next != NULL); | | 609 | lint_assert(dcs->d_next != NULL); |
610 | di = dcs; | | 610 | di = dcs; |
611 | dcs = di->d_next; | | 611 | dcs = di->d_next; |
612 | switch (di->d_ctx) { | | 612 | switch (di->d_ctx) { |
613 | case MOS: | | 613 | case MOS: |
614 | case MOU: | | 614 | case MOU: |
615 | case ENUMCON: | | 615 | case ENUMCON: |
616 | /* | | 616 | /* |
617 | * Symbols declared in (nested) structs or enums are | | 617 | * Symbols declared in (nested) structs or enums are |
618 | * part of the next level (they are removed from the | | 618 | * part of the next level (they are removed from the |
619 | * symbol table if the symbols of the outher level are | | 619 | * symbol table if the symbols of the outher level are |
620 | * removed). | | 620 | * removed). |
621 | */ | | 621 | */ |
622 | if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL) | | 622 | if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL) |
623 | dcs->d_ldlsym = di->d_ldlsym; | | 623 | dcs->d_ldlsym = di->d_ldlsym; |
624 | break; | | 624 | break; |
625 | case ARG: | | 625 | case ARG: |
626 | /* | | 626 | /* |
627 | * All symbols in dcs->d_dlsyms are introduced in old style | | 627 | * All symbols in dcs->d_dlsyms are introduced in old style |
628 | * argument declarations (it's not clean, but possible). | | 628 | * argument declarations (it's not clean, but possible). |
629 | * They are appended to the list of symbols declared in | | 629 | * They are appended to the list of symbols declared in |
630 | * an old style argument identifier list or a new style | | 630 | * an old style argument identifier list or a new style |
631 | * parameter type list. | | 631 | * parameter type list. |
632 | */ | | 632 | */ |
633 | if (di->d_dlsyms != NULL) { | | 633 | if (di->d_dlsyms != NULL) { |
634 | *di->d_ldlsym = dcs->d_fpsyms; | | 634 | *di->d_ldlsym = dcs->d_fpsyms; |
635 | dcs->d_fpsyms = di->d_dlsyms; | | 635 | dcs->d_fpsyms = di->d_dlsyms; |
636 | } | | 636 | } |
637 | break; | | 637 | break; |
638 | case ABSTRACT: | | 638 | case ABSTRACT: |
639 | /* | | 639 | /* |
640 | * casts and sizeof | | 640 | * casts and sizeof |
641 | * Append all symbols declared in the abstract declaration | | 641 | * Append all symbols declared in the abstract declaration |
642 | * to the list of symbols declared in the surrounding | | 642 | * to the list of symbols declared in the surrounding |
643 | * declaration or block. | | 643 | * declaration or block. |
644 | * XXX I'm not sure whether they should be removed from the | | 644 | * XXX I'm not sure whether they should be removed from the |
645 | * symbol table now or later. | | 645 | * symbol table now or later. |
646 | */ | | 646 | */ |
647 | if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL) | | 647 | if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL) |
648 | dcs->d_ldlsym = di->d_ldlsym; | | 648 | dcs->d_ldlsym = di->d_ldlsym; |
649 | break; | | 649 | break; |
650 | case AUTO: | | 650 | case AUTO: |
651 | /* check usage of local vars */ | | 651 | /* check usage of local vars */ |
652 | check_usage(di); | | 652 | check_usage(di); |
653 | /* FALLTHROUGH */ | | 653 | /* FALLTHROUGH */ |
654 | case PARG: | | 654 | case PARG: |
655 | /* usage of arguments will be checked by funcend() */ | | 655 | /* usage of arguments will be checked by funcend() */ |
656 | rmsyms(di->d_dlsyms); | | 656 | rmsyms(di->d_dlsyms); |
657 | break; | | 657 | break; |
658 | case EXTERN: | | 658 | case EXTERN: |
659 | /* there is nothing after external declarations */ | | 659 | /* there is nothing after external declarations */ |
660 | /* FALLTHROUGH */ | | 660 | /* FALLTHROUGH */ |
661 | default: | | 661 | default: |
662 | lint_assert(0); | | 662 | lint_assert(0); |
663 | } | | 663 | } |
664 | free(di); | | 664 | free(di); |
665 | } | | 665 | } |
666 | | | 666 | |
667 | /* | | 667 | /* |
668 | * Set flag d_asm in all declaration stack elements up to the | | 668 | * Set flag d_asm in all declaration stack elements up to the |
669 | * outermost one. | | 669 | * outermost one. |
670 | * | | 670 | * |
671 | * This is used to mark compound statements which have, possibly in | | 671 | * This is used to mark compound statements which have, possibly in |
672 | * nested compound statements, asm statements. For these compound | | 672 | * nested compound statements, asm statements. For these compound |
673 | * statements no warnings about unused or unitialized variables are | | 673 | * statements no warnings about unused or unitialized variables are |
674 | * printed. | | 674 | * printed. |
675 | * | | 675 | * |
676 | * There is no need to clear d_asm in dinfo structs with context AUTO, | | 676 | * There is no need to clear d_asm in dinfo structs with context AUTO, |
677 | * because these structs are freed at the end of the compound statement. | | 677 | * because these structs are freed at the end of the compound statement. |
678 | * But it must be cleared in the outermost dinfo struct, which has | | 678 | * But it must be cleared in the outermost dinfo struct, which has |
679 | * context EXTERN. This could be done in clrtyp() and would work for | | 679 | * context EXTERN. This could be done in clrtyp() and would work for |
680 | * C, but not for C++ (due to mixed statements and declarations). Thus | | 680 | * C, but not for C++ (due to mixed statements and declarations). Thus |
681 | * we clear it in global_clean_up_decl(), which is used to do some cleanup | | 681 | * we clear it in global_clean_up_decl(), which is used to do some cleanup |
682 | * after global declarations/definitions. | | 682 | * after global declarations/definitions. |
683 | */ | | 683 | */ |
684 | void | | 684 | void |
685 | setasm(void) | | 685 | setasm(void) |
686 | { | | 686 | { |
687 | dinfo_t *di; | | 687 | dinfo_t *di; |
688 | | | 688 | |
689 | for (di = dcs; di != NULL; di = di->d_next) | | 689 | for (di = dcs; di != NULL; di = di->d_next) |
690 | di->d_asm = 1; | | 690 | di->d_asm = 1; |
691 | } | | 691 | } |
692 | | | 692 | |
693 | /* | | 693 | /* |
694 | * Clean all elements of the top element of declaration stack which | | 694 | * Clean all elements of the top element of declaration stack which |
695 | * will be used by the next declaration | | 695 | * will be used by the next declaration |
696 | */ | | 696 | */ |
697 | void | | 697 | void |
698 | clrtyp(void) | | 698 | clrtyp(void) |
699 | { | | 699 | { |
700 | | | 700 | |
701 | dcs->d_atyp = dcs->d_cmod = dcs->d_smod = dcs->d_lmod = NOTSPEC; | | 701 | dcs->d_atyp = dcs->d_cmod = dcs->d_smod = dcs->d_lmod = NOTSPEC; |
702 | dcs->d_scl = NOSCL; | | 702 | dcs->d_scl = NOSCL; |
703 | dcs->d_type = NULL; | | 703 | dcs->d_type = NULL; |
704 | dcs->d_const = dcs->d_volatile = 0; | | 704 | dcs->d_const = dcs->d_volatile = 0; |
705 | dcs->d_inline = 0; | | 705 | dcs->d_inline = 0; |
706 | dcs->d_mscl = dcs->d_terr = 0; | | 706 | dcs->d_mscl = dcs->d_terr = 0; |
707 | dcs->d_nedecl = 0; | | 707 | dcs->d_nedecl = 0; |
708 | dcs->d_notyp = 0; | | 708 | dcs->d_notyp = 0; |
709 | } | | 709 | } |
710 | | | 710 | |
711 | /* | | 711 | /* |
712 | * Create a type structure from the information gathered in | | 712 | * Create a type structure from the information gathered in |
713 | * the declaration stack. | | 713 | * the declaration stack. |
714 | * Complain about storage classes which are not possible in current | | 714 | * Complain about storage classes which are not possible in current |
715 | * context. | | 715 | * context. |
716 | */ | | 716 | */ |
717 | void | | 717 | void |
718 | deftyp(void) | | 718 | deftyp(void) |
719 | { | | 719 | { |
720 | tspec_t t, s, l, c; | | 720 | tspec_t t, s, l, c; |
721 | type_t *tp; | | 721 | type_t *tp; |
722 | scl_t scl; | | 722 | scl_t scl; |
723 | | | 723 | |
724 | t = dcs->d_atyp; /* BOOL, CHAR, INT, COMPLEX, VOID */ | | 724 | t = dcs->d_atyp; /* BOOL, CHAR, INT, COMPLEX, VOID */ |
725 | s = dcs->d_smod; /* SIGNED, UNSIGNED */ | | 725 | s = dcs->d_smod; /* SIGNED, UNSIGNED */ |
726 | l = dcs->d_lmod; /* SHORT, LONG, QUAD */ | | 726 | l = dcs->d_lmod; /* SHORT, LONG, QUAD */ |
727 | c = dcs->d_cmod; /* FLOAT, DOUBLE */ | | 727 | c = dcs->d_cmod; /* FLOAT, DOUBLE */ |
728 | tp = dcs->d_type; | | 728 | tp = dcs->d_type; |
729 | scl = dcs->d_scl; | | 729 | scl = dcs->d_scl; |
730 | | | 730 | |
731 | #ifdef DEBUG | | 731 | #ifdef DEBUG |
732 | printf("%s: %s\n", __func__, type_name(tp)); | | 732 | printf("%s: %s\n", __func__, type_name(tp)); |
733 | #endif | | 733 | #endif |
734 | if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC && | | 734 | if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC && |
735 | tp == NULL) | | 735 | tp == NULL) |
736 | dcs->d_notyp = 1; | | 736 | dcs->d_notyp = 1; |
737 | if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) && | | 737 | if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) && |
738 | tp == NULL) | | 738 | tp == NULL) |
739 | t = c; | | 739 | t = c; |
740 | | | 740 | |
741 | if (tp != NULL) { | | 741 | if (tp != NULL) { |
742 | lint_assert(t == NOTSPEC); | | 742 | lint_assert(t == NOTSPEC); |
743 | lint_assert(s == NOTSPEC); | | 743 | lint_assert(s == NOTSPEC); |
744 | lint_assert(l == NOTSPEC); | | 744 | lint_assert(l == NOTSPEC); |
745 | } | | 745 | } |
746 | | | 746 | |
747 | if (tp == NULL) { | | 747 | if (tp == NULL) { |
748 | switch (t) { | | 748 | switch (t) { |
749 | case BOOL: | | 749 | case BOOL: |
750 | break; | | 750 | break; |
751 | case NOTSPEC: | | 751 | case NOTSPEC: |
752 | t = INT; | | 752 | t = INT; |
753 | /* FALLTHROUGH */ | | 753 | /* FALLTHROUGH */ |
754 | case INT: | | 754 | case INT: |
755 | if (s == NOTSPEC) | | 755 | if (s == NOTSPEC) |
756 | s = SIGNED; | | 756 | s = SIGNED; |
757 | break; | | 757 | break; |
758 | case CHAR: | | 758 | case CHAR: |
759 | if (l != NOTSPEC) { | | 759 | if (l != NOTSPEC) { |
760 | dcs->d_terr = 1; | | 760 | dcs->d_terr = 1; |
761 | l = NOTSPEC; | | 761 | l = NOTSPEC; |
762 | } | | 762 | } |
763 | break; | | 763 | break; |
764 | case FLOAT: | | 764 | case FLOAT: |
765 | if (l == LONG) { | | 765 | if (l == LONG) { |
766 | l = NOTSPEC; | | 766 | l = NOTSPEC; |
767 | t = DOUBLE; | | 767 | t = DOUBLE; |
768 | if (!tflag) | | 768 | if (!tflag) |
769 | /* use 'double' instead of 'long ... */ | | 769 | /* use 'double' instead of 'long ... */ |
770 | warning(6); | | 770 | warning(6); |
771 | } | | 771 | } |
772 | break; | | 772 | break; |
773 | case DOUBLE: | | 773 | case DOUBLE: |
774 | if (l == LONG) { | | 774 | if (l == LONG) { |
775 | case LDOUBLE: | | 775 | case LDOUBLE: |
776 | l = NOTSPEC; | | 776 | l = NOTSPEC; |
777 | t = LDOUBLE; | | 777 | t = LDOUBLE; |
778 | if (tflag) | | 778 | if (tflag) |
779 | /* 'long double' is illegal in ... */ | | 779 | /* 'long double' is illegal in ... */ |
780 | warning(266); | | 780 | warning(266); |
781 | } | | 781 | } |
782 | break; | | 782 | break; |
783 | case DCOMPLEX: | | 783 | case DCOMPLEX: |
784 | if (l == LONG) { | | 784 | if (l == LONG) { |
785 | l = NOTSPEC; | | 785 | l = NOTSPEC; |
786 | t = LCOMPLEX; | | 786 | t = LCOMPLEX; |
787 | if (tflag) | | 787 | if (tflag) |
788 | /* 'long double' is illegal in ... */ | | 788 | /* 'long double' is illegal in ... */ |
789 | warning(266); | | 789 | warning(266); |
790 | } | | 790 | } |
791 | break; | | 791 | break; |
792 | case VOID: | | 792 | case VOID: |
793 | case FCOMPLEX: | | 793 | case FCOMPLEX: |
794 | case LCOMPLEX: | | 794 | case LCOMPLEX: |
795 | break; | | 795 | break; |
796 | default: | | 796 | default: |
797 | LERROR("deftyp(%s)", tspec_name(t)); | | 797 | LERROR("deftyp(%s)", tspec_name(t)); |
798 | } | | 798 | } |
799 | if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) { | | 799 | if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) { |
800 | dcs->d_terr = 1; | | 800 | dcs->d_terr = 1; |
801 | l = s = NOTSPEC; | | 801 | l = s = NOTSPEC; |
802 | } | | 802 | } |
803 | if (l != NOTSPEC) | | 803 | if (l != NOTSPEC) |
804 | t = l; | | 804 | t = l; |
805 | dcs->d_type = gettyp(merge_type_specifiers(t, s)); | | 805 | dcs->d_type = gettyp(merge_type_specifiers(t, s)); |
806 | } | | 806 | } |
807 | | | 807 | |
808 | if (dcs->d_mscl) { | | 808 | if (dcs->d_mscl) { |
809 | /* only one storage class allowed */ | | 809 | /* only one storage class allowed */ |
810 | error(7); | | 810 | error(7); |
811 | } | | 811 | } |
812 | if (dcs->d_terr) { | | 812 | if (dcs->d_terr) { |
813 | /* illegal type combination */ | | 813 | /* illegal type combination */ |
814 | error(4); | | 814 | error(4); |
815 | } | | 815 | } |
816 | | | 816 | |
817 | if (dcs->d_ctx == EXTERN) { | | 817 | if (dcs->d_ctx == EXTERN) { |
818 | if (scl == REG || scl == AUTO) { | | 818 | if (scl == REG || scl == AUTO) { |
819 | /* illegal storage class */ | | 819 | /* illegal storage class */ |
820 | error(8); | | 820 | error(8); |
821 | scl = NOSCL; | | 821 | scl = NOSCL; |
822 | } | | 822 | } |
823 | } else if (dcs->d_ctx == ARG || dcs->d_ctx == PARG) { | | 823 | } else if (dcs->d_ctx == ARG || dcs->d_ctx == PARG) { |
824 | if (scl != NOSCL && scl != REG) { | | 824 | if (scl != NOSCL && scl != REG) { |
825 | /* only register valid ... */ | | 825 | /* only register valid ... */ |
826 | error(9); | | 826 | error(9); |
827 | scl = NOSCL; | | 827 | scl = NOSCL; |
828 | } | | 828 | } |
829 | } | | 829 | } |
830 | | | 830 | |
831 | dcs->d_scl = scl; | | 831 | dcs->d_scl = scl; |
832 | | | 832 | |
833 | if (dcs->d_const && dcs->d_type->t_const) { | | 833 | if (dcs->d_const && dcs->d_type->t_const) { |
834 | lint_assert(dcs->d_type->t_typedef); | | 834 | lint_assert(dcs->d_type->t_typedef); |
835 | /* typedef already qualified with '%s' */ | | 835 | /* typedef already qualified with '%s' */ |
836 | warning(68, "const"); | | 836 | warning(68, "const"); |
837 | } | | 837 | } |
838 | if (dcs->d_volatile && dcs->d_type->t_volatile) { | | 838 | if (dcs->d_volatile && dcs->d_type->t_volatile) { |
839 | lint_assert(dcs->d_type->t_typedef); | | 839 | lint_assert(dcs->d_type->t_typedef); |
840 | /* typedef already qualified with '%s' */ | | 840 | /* typedef already qualified with '%s' */ |
841 | warning(68, "volatile"); | | 841 | warning(68, "volatile"); |
842 | } | | 842 | } |
843 | | | 843 | |
844 | if (dcs->d_const || dcs->d_volatile) { | | 844 | if (dcs->d_const || dcs->d_volatile) { |
845 | dcs->d_type = duptyp(dcs->d_type); | | 845 | dcs->d_type = duptyp(dcs->d_type); |
846 | dcs->d_type->t_const |= dcs->d_const; | | 846 | dcs->d_type->t_const |= dcs->d_const; |
847 | dcs->d_type->t_volatile |= dcs->d_volatile; | | 847 | dcs->d_type->t_volatile |= dcs->d_volatile; |
848 | } | | 848 | } |
849 | } | | 849 | } |
850 | | | 850 | |
851 | /* | | 851 | /* |
852 | * Merge type specifiers (char, ..., long long, signed, unsigned). | | 852 | * Merge type specifiers (char, ..., long long, signed, unsigned). |
853 | */ | | 853 | */ |
854 | static tspec_t | | 854 | static tspec_t |
855 | merge_type_specifiers(tspec_t t, tspec_t s) | | 855 | merge_type_specifiers(tspec_t t, tspec_t s) |
856 | { | | 856 | { |
857 | | | 857 | |
858 | if (s == SIGNED || s == UNSIGN) { | | 858 | if (s == SIGNED || s == UNSIGN) { |
859 | if (t == CHAR) { | | 859 | if (t == CHAR) { |
860 | t = s == SIGNED ? SCHAR : UCHAR; | | 860 | t = s == SIGNED ? SCHAR : UCHAR; |
861 | } else if (t == SHORT) { | | 861 | } else if (t == SHORT) { |
862 | t = s == SIGNED ? SHORT : USHORT; | | 862 | t = s == SIGNED ? SHORT : USHORT; |
863 | } else if (t == INT) { | | 863 | } else if (t == INT) { |
864 | t = s == SIGNED ? INT : UINT; | | 864 | t = s == SIGNED ? INT : UINT; |
865 | } else if (t == LONG) { | | 865 | } else if (t == LONG) { |
866 | t = s == SIGNED ? LONG : ULONG; | | 866 | t = s == SIGNED ? LONG : ULONG; |
867 | } else if (t == QUAD) { | | 867 | } else if (t == QUAD) { |
868 | t = s == SIGNED ? QUAD : UQUAD; | | 868 | t = s == SIGNED ? QUAD : UQUAD; |
869 | } | | 869 | } |
870 | } | | 870 | } |
871 | | | 871 | |
872 | return t; | | 872 | return t; |
873 | } | | 873 | } |
874 | | | 874 | |
875 | /* | | 875 | /* |
876 | * Return the length of a type in bits. | | 876 | * Return the length of a type in bits. |
877 | * | | 877 | * |
878 | * Printing a message if the outhermost dimension of an array is 0 must | | 878 | * Printing a message if the outhermost dimension of an array is 0 must |
879 | * be done by the caller. All other problems are reported by length() | | 879 | * be done by the caller. All other problems are reported by length() |
880 | * if name is not NULL. | | 880 | * if name is not NULL. |
881 | */ | | 881 | */ |
882 | int | | 882 | int |
883 | length(type_t *tp, const char *name) | | 883 | length(type_t *tp, const char *name) |
884 | { | | 884 | { |
885 | int elem, elsz; | | 885 | int elem, elsz; |
886 | | | 886 | |
887 | elem = 1; | | 887 | elem = 1; |
888 | while (tp && tp->t_tspec == ARRAY) { | | 888 | while (tp && tp->t_tspec == ARRAY) { |
889 | elem *= tp->t_dim; | | 889 | elem *= tp->t_dim; |
890 | tp = tp->t_subt; | | 890 | tp = tp->t_subt; |
891 | } | | 891 | } |
892 | if (tp == NULL) | | 892 | if (tp == NULL) |
893 | return -1; | | 893 | return -1; |
894 | | | 894 | |
895 | switch (tp->t_tspec) { | | 895 | switch (tp->t_tspec) { |
896 | case FUNC: | | 896 | case FUNC: |
897 | /* compiler takes size of function */ | | 897 | /* compiler takes size of function */ |
898 | LERROR("%s", msgs[12]); | | 898 | LERROR("%s", msgs[12]); |
899 | /* NOTREACHED */ | | 899 | /* NOTREACHED */ |
900 | case STRUCT: | | 900 | case STRUCT: |
901 | case UNION: | | 901 | case UNION: |
902 | if (incompl(tp) && name != NULL) { | | 902 | if (incompl(tp) && name != NULL) { |
903 | /* incomplete structure or union %s: %s */ | | 903 | /* incomplete structure or union %s: %s */ |
904 | error(31, tp->t_str->stag->s_name, name); | | 904 | error(31, tp->t_str->stag->s_name, name); |
905 | } | | 905 | } |
906 | elsz = tp->t_str->size; | | 906 | elsz = tp->t_str->size; |
907 | break; | | 907 | break; |
908 | case ENUM: | | 908 | case ENUM: |
909 | if (incompl(tp) && name != NULL) { | | 909 | if (incompl(tp) && name != NULL) { |
910 | /* incomplete enum type: %s */ | | 910 | /* incomplete enum type: %s */ |
911 | warning(13, name); | | 911 | warning(13, name); |
912 | } | | 912 | } |
913 | /* FALLTHROUGH */ | | 913 | /* FALLTHROUGH */ |
914 | default: | | 914 | default: |
915 | elsz = size(tp->t_tspec); | | 915 | elsz = size(tp->t_tspec); |
916 | if (elsz <= 0) | | 916 | if (elsz <= 0) |
917 | LERROR("length(%d)", elsz); | | 917 | LERROR("length(%d)", elsz); |
918 | break; | | 918 | break; |
919 | } | | 919 | } |
920 | return elem * elsz; | | 920 | return elem * elsz; |
921 | } | | 921 | } |
922 | | | 922 | |
923 | /* | | 923 | /* |
924 | * Get the alignment of the given Type in bits. | | 924 | * Get the alignment of the given Type in bits. |
925 | */ | | 925 | */ |
926 | int | | 926 | int |
927 | getbound(type_t *tp) | | 927 | getbound(type_t *tp) |
928 | { | | 928 | { |
929 | size_t a; | | 929 | size_t a; |
930 | tspec_t t; | | 930 | tspec_t t; |
931 | | | 931 | |
932 | while (tp && tp->t_tspec == ARRAY) | | 932 | while (tp && tp->t_tspec == ARRAY) |
933 | tp = tp->t_subt; | | 933 | tp = tp->t_subt; |
934 | | | 934 | |
935 | if (tp == NULL) | | 935 | if (tp == NULL) |
936 | return -1; | | 936 | return -1; |
937 | | | 937 | |
938 | if ((t = tp->t_tspec) == STRUCT || t == UNION) { | | 938 | if ((t = tp->t_tspec) == STRUCT || t == UNION) { |
939 | a = tp->t_str->align; | | 939 | a = tp->t_str->align; |
940 | } else if (t == FUNC) { | | 940 | } else if (t == FUNC) { |
941 | /* compiler takes alignment of function */ | | 941 | /* compiler takes alignment of function */ |
942 | error(14); | | 942 | error(14); |
943 | a = WORST_ALIGN(1) * CHAR_BIT; | | 943 | a = WORST_ALIGN(1) * CHAR_BIT; |
944 | } else { | | 944 | } else { |
945 | if ((a = size(t)) == 0) { | | 945 | if ((a = size(t)) == 0) { |
946 | a = CHAR_BIT; | | 946 | a = CHAR_BIT; |
947 | } else if (a > WORST_ALIGN(1) * CHAR_BIT) { | | 947 | } else if (a > WORST_ALIGN(1) * CHAR_BIT) { |
948 | a = WORST_ALIGN(1) * CHAR_BIT; | | 948 | a = WORST_ALIGN(1) * CHAR_BIT; |
949 | } | | 949 | } |
950 | } | | 950 | } |
951 | lint_assert(a >= CHAR_BIT); | | 951 | lint_assert(a >= CHAR_BIT); |
952 | lint_assert(a <= WORST_ALIGN(1) * CHAR_BIT); | | 952 | lint_assert(a <= WORST_ALIGN(1) * CHAR_BIT); |
953 | return a; | | 953 | return a; |
954 | } | | 954 | } |
955 | | | 955 | |
956 | /* | | 956 | /* |
957 | * Concatenate two lists of symbols by s_next. Used by declarations of | | 957 | * Concatenate two lists of symbols by s_next. Used by declarations of |
958 | * struct/union/enum elements and parameters. | | 958 | * struct/union/enum elements and parameters. |
959 | */ | | 959 | */ |
960 | sym_t * | | 960 | sym_t * |
961 | lnklst(sym_t *l1, sym_t *l2) | | 961 | lnklst(sym_t *l1, sym_t *l2) |
962 | { | | 962 | { |
963 | sym_t *l; | | 963 | sym_t *l; |
964 | | | 964 | |
965 | if ((l = l1) == NULL) | | 965 | if ((l = l1) == NULL) |
966 | return l2; | | 966 | return l2; |
967 | while (l1->s_next != NULL) | | 967 | while (l1->s_next != NULL) |
968 | l1 = l1->s_next; | | 968 | l1 = l1->s_next; |
969 | l1->s_next = l2; | | 969 | l1->s_next = l2; |
970 | return l; | | 970 | return l; |
971 | } | | 971 | } |
972 | | | 972 | |
973 | /* | | 973 | /* |
974 | * Check if the type of the given symbol is valid and print an error | | 974 | * Check if the type of the given symbol is valid and print an error |
975 | * message if it is not. | | 975 | * message if it is not. |
976 | * | | 976 | * |
977 | * Invalid types are: | | 977 | * Invalid types are: |
978 | * - arrays of incomlete types or functions | | 978 | * - arrays of incomlete types or functions |
979 | * - functions returning arrays or functions | | 979 | * - functions returning arrays or functions |
980 | * - void types other than type of function or pointer | | 980 | * - void types other than type of function or pointer |
981 | */ | | 981 | */ |
982 | void | | 982 | void |
983 | check_type(sym_t *sym) | | 983 | check_type(sym_t *sym) |
984 | { | | 984 | { |
985 | tspec_t to, t; | | 985 | tspec_t to, t; |
986 | type_t **tpp, *tp; | | 986 | type_t **tpp, *tp; |
987 | | | 987 | |
988 | tpp = &sym->s_type; | | 988 | tpp = &sym->s_type; |
989 | to = NOTSPEC; | | 989 | to = NOTSPEC; |
990 | while ((tp = *tpp) != NULL) { | | 990 | while ((tp = *tpp) != NULL) { |
991 | t = tp->t_tspec; | | 991 | t = tp->t_tspec; |
992 | /* | | 992 | /* |
993 | * If this is the type of an old style function definition, | | 993 | * If this is the type of an old style function definition, |
994 | * a better warning is printed in funcdef(). | | 994 | * a better warning is printed in funcdef(). |
995 | */ | | 995 | */ |
996 | if (t == FUNC && !tp->t_proto && | | 996 | if (t == FUNC && !tp->t_proto && |
997 | !(to == NOTSPEC && sym->s_osdef)) { | | 997 | !(to == NOTSPEC && sym->s_osdef)) { |
998 | if (sflag && hflag) | | 998 | if (sflag && hflag) |
999 | /* function declaration is not a prototype */ | | 999 | /* function declaration is not a prototype */ |
1000 | warning(287); | | 1000 | warning(287); |
1001 | } | | 1001 | } |
1002 | if (to == FUNC) { | | 1002 | if (to == FUNC) { |
1003 | if (t == FUNC || t == ARRAY) { | | 1003 | if (t == FUNC || t == ARRAY) { |
1004 | /* function returns illegal type */ | | 1004 | /* function returns illegal type */ |
1005 | error(15); | | 1005 | error(15); |
1006 | if (t == FUNC) { | | 1006 | if (t == FUNC) { |
1007 | *tpp = incref(*tpp, PTR); | | 1007 | *tpp = incref(*tpp, PTR); |
1008 | } else { | | 1008 | } else { |
1009 | *tpp = incref((*tpp)->t_subt, PTR); | | 1009 | *tpp = incref((*tpp)->t_subt, PTR); |
1010 | } | | 1010 | } |
1011 | return; | | 1011 | return; |
1012 | } else if (tp->t_const || tp->t_volatile) { | | 1012 | } else if (tp->t_const || tp->t_volatile) { |
1013 | if (sflag) { /* XXX oder better !tflag ? */ | | 1013 | if (sflag) { /* XXX oder better !tflag ? */ |
1014 | /* function cannot return const... */ | | 1014 | /* function cannot return const... */ |
1015 | warning(228); | | 1015 | warning(228); |
1016 | } | | 1016 | } |
1017 | } | | 1017 | } |
1018 | } if (to == ARRAY) { | | 1018 | } if (to == ARRAY) { |
1019 | if (t == FUNC) { | | 1019 | if (t == FUNC) { |
1020 | /* array of function is illegal */ | | 1020 | /* array of function is illegal */ |
1021 | error(16); | | 1021 | error(16); |
1022 | *tpp = gettyp(INT); | | 1022 | *tpp = gettyp(INT); |
1023 | return; | | 1023 | return; |
1024 | } else if (t == ARRAY && tp->t_dim == 0) { | | 1024 | } else if (t == ARRAY && tp->t_dim == 0) { |
1025 | /* null dimension */ | | 1025 | /* null dimension */ |
1026 | error(17); | | 1026 | error(17); |
1027 | return; | | 1027 | return; |
1028 | } else if (t == VOID) { | | 1028 | } else if (t == VOID) { |
1029 | /* illegal use of 'void' */ | | 1029 | /* illegal use of 'void' */ |
1030 | error(18); | | 1030 | error(18); |
1031 | *tpp = gettyp(INT); | | 1031 | *tpp = gettyp(INT); |
1032 | #if 0 /* errors are produced by length() */ | | 1032 | #if 0 /* errors are produced by length() */ |
1033 | } else if (incompl(tp)) { | | 1033 | } else if (incompl(tp)) { |
1034 | /* array of incomplete type */ | | 1034 | /* array of incomplete type */ |
1035 | if (sflag) { | | 1035 | if (sflag) { |
1036 | /* array of incomplete type */ | | 1036 | /* array of incomplete type */ |
1037 | error(301); | | 1037 | error(301); |
1038 | } else { | | 1038 | } else { |
1039 | /* array of incomplete type */ | | 1039 | /* array of incomplete type */ |
1040 | warning(301); | | 1040 | warning(301); |
1041 | } | | 1041 | } |
1042 | #endif | | 1042 | #endif |
1043 | } | | 1043 | } |
1044 | } else if (to == NOTSPEC && t == VOID) { | | 1044 | } else if (to == NOTSPEC && t == VOID) { |
1045 | if (dcs->d_ctx == PARG) { | | 1045 | if (dcs->d_ctx == PARG) { |
1046 | if (sym->s_scl != ABSTRACT) { | | 1046 | if (sym->s_scl != ABSTRACT) { |
1047 | lint_assert(sym->s_name != unnamed); | | 1047 | lint_assert(sym->s_name != unnamed); |
1048 | /* void param. cannot have name: %s */ | | 1048 | /* void param. cannot have name: %s */ |
1049 | error(61, sym->s_name); | | 1049 | error(61, sym->s_name); |
1050 | *tpp = gettyp(INT); | | 1050 | *tpp = gettyp(INT); |
1051 | } | | 1051 | } |
1052 | } else if (dcs->d_ctx == ABSTRACT) { | | 1052 | } else if (dcs->d_ctx == ABSTRACT) { |
1053 | /* ok */ | | 1053 | /* ok */ |
1054 | } else if (sym->s_scl != TYPEDEF) { | | 1054 | } else if (sym->s_scl != TYPEDEF) { |
1055 | /* void type for %s */ | | 1055 | /* void type for %s */ |
1056 | error(19, sym->s_name); | | 1056 | error(19, sym->s_name); |
1057 | *tpp = gettyp(INT); | | 1057 | *tpp = gettyp(INT); |
1058 | } | | 1058 | } |
1059 | } | | 1059 | } |
1060 | if (t == VOID && to != PTR) { | | 1060 | if (t == VOID && to != PTR) { |
1061 | if (tp->t_const || tp->t_volatile) { | | 1061 | if (tp->t_const || tp->t_volatile) { |
1062 | /* inappropriate qualifiers with 'void' */ | | 1062 | /* inappropriate qualifiers with 'void' */ |
1063 | warning(69); | | 1063 | warning(69); |
1064 | tp->t_const = tp->t_volatile = 0; | | 1064 | tp->t_const = tp->t_volatile = 0; |
1065 | } | | 1065 | } |
1066 | } | | 1066 | } |
1067 | tpp = &tp->t_subt; | | 1067 | tpp = &tp->t_subt; |
1068 | to = t; | | 1068 | to = t; |
1069 | } | | 1069 | } |
1070 | } | | 1070 | } |
1071 | | | 1071 | |
1072 | /* | | 1072 | /* |
1073 | * Process the declarator of a struct/union element. | | 1073 | * Process the declarator of a struct/union element. |
1074 | */ | | 1074 | */ |
1075 | sym_t * | | 1075 | sym_t * |
1076 | declarator_1_struct_union(sym_t *dsym) | | 1076 | declarator_1_struct_union(sym_t *dsym) |
1077 | { | | 1077 | { |
1078 | type_t *tp; | | 1078 | type_t *tp; |
1079 | tspec_t t; | | 1079 | tspec_t t; |
1080 | int sz, len; | | 1080 | int sz, len; |
1081 | int o = 0; /* Appease gcc */ | | 1081 | int o = 0; /* Appease gcc */ |
1082 | scl_t sc; | | 1082 | scl_t sc; |
1083 | | | 1083 | |
1084 | lint_assert((sc = dsym->s_scl) == MOS || sc == MOU); | | 1084 | lint_assert((sc = dsym->s_scl) == MOS || sc == MOU); |
1085 | | | 1085 | |
1086 | if (dcs->d_rdcsym != NULL) { | | 1086 | if (dcs->d_rdcsym != NULL) { |
1087 | /* should be ensured by storesym() */ | | 1087 | /* should be ensured by storesym() */ |
1088 | lint_assert((sc = dcs->d_rdcsym->s_scl) == MOS || sc == MOU); | | 1088 | lint_assert((sc = dcs->d_rdcsym->s_scl) == MOS || sc == MOU); |
1089 | if (dsym->s_styp == dcs->d_rdcsym->s_styp) { | | 1089 | if (dsym->s_styp == dcs->d_rdcsym->s_styp) { |
1090 | /* duplicate member name: %s */ | | 1090 | /* duplicate member name: %s */ |
1091 | error(33, dsym->s_name); | | 1091 | error(33, dsym->s_name); |
1092 | rmsym(dcs->d_rdcsym); | | 1092 | rmsym(dcs->d_rdcsym); |
1093 | } | | 1093 | } |
1094 | } | | 1094 | } |
1095 | | | 1095 | |
1096 | check_type(dsym); | | 1096 | check_type(dsym); |
1097 | | | 1097 | |
1098 | t = (tp = dsym->s_type)->t_tspec; | | 1098 | t = (tp = dsym->s_type)->t_tspec; |
1099 | | | 1099 | |
1100 | if (dsym->s_bitfield) { | | 1100 | if (dsym->s_bitfield) { |
1101 | /* | | 1101 | /* |
1102 | * only unsigned and signed int are portable bit-field types | | 1102 | * only unsigned and signed int are portable bit-field types |
1103 | *(at least in ANSI C, in traditional C only unsigned int) | | 1103 | *(at least in ANSI C, in traditional C only unsigned int) |
1104 | */ | | 1104 | */ |
1105 | if (t == CHAR || t == UCHAR || t == SCHAR || | | 1105 | if (t == CHAR || t == UCHAR || t == SCHAR || |
1106 | t == SHORT || t == USHORT || t == ENUM) { | | 1106 | t == SHORT || t == USHORT || t == ENUM) { |
1107 | if (bitfieldtype_ok == 0) { | | 1107 | if (bitfieldtype_ok == 0) { |
1108 | if (sflag) { | | 1108 | if (sflag) { |
1109 | /* bit-field type '%s' invalid ... */ | | 1109 | /* bit-field type '%s' invalid ... */ |
1110 | warning(273, type_name(tp)); | | 1110 | warning(273, type_name(tp)); |
1111 | } else if (pflag) { | | 1111 | } else if (pflag) { |
1112 | /* nonportable bit-field type */ | | 1112 | /* nonportable bit-field type */ |
1113 | warning(34); | | 1113 | warning(34); |
1114 | } | | 1114 | } |
1115 | } | | 1115 | } |
1116 | } else if (t == INT && dcs->d_smod == NOTSPEC) { | | 1116 | } else if (t == INT && dcs->d_smod == NOTSPEC) { |
1117 | if (pflag && bitfieldtype_ok == 0) { | | 1117 | if (pflag && bitfieldtype_ok == 0) { |
1118 | /* nonportable bit-field type */ | | 1118 | /* nonportable bit-field type */ |
1119 | warning(34); | | 1119 | warning(34); |
1120 | } | | 1120 | } |
1121 | } else if (t != INT && t != UINT) { | | 1121 | } else if (t != INT && t != UINT && t != BOOL) { |
1122 | /* | | 1122 | /* |
1123 | * Non-integer types are always illegal for | | 1123 | * Non-integer types are always illegal for |
1124 | * bitfields, regardless of BITFIELDTYPE. | | 1124 | * bitfields, regardless of BITFIELDTYPE. |
1125 | * Integer types not dealt with above are | | 1125 | * Integer types not dealt with above are |
1126 | * okay only if BITFIELDTYPE is in effect. | | 1126 | * okay only if BITFIELDTYPE is in effect. |
1127 | */ | | 1127 | */ |
1128 | if (bitfieldtype_ok == 0 || tspec_is_int(t) == 0) { | | 1128 | if (bitfieldtype_ok == 0 || tspec_is_int(t) == 0) { |
1129 | /* illegal bit-field type */ | | 1129 | /* illegal bit-field type */ |
1130 | warning(35); | | 1130 | warning(35); |
1131 | sz = tp->t_flen; | | 1131 | sz = tp->t_flen; |
1132 | dsym->s_type = tp = duptyp(gettyp(t = INT)); | | 1132 | dsym->s_type = tp = duptyp(gettyp(t = INT)); |
1133 | if ((tp->t_flen = sz) > size(t)) | | 1133 | if ((tp->t_flen = sz) > size(t)) |
1134 | tp->t_flen = size(t); | | 1134 | tp->t_flen = size(t); |
1135 | } | | 1135 | } |
1136 | } | | 1136 | } |
1137 | if ((len = tp->t_flen) < 0 || len > (ssize_t)size(t)) { | | 1137 | if ((len = tp->t_flen) < 0 || len > (ssize_t)size(t)) { |
1138 | /* illegal bit-field size: %d */ | | 1138 | /* illegal bit-field size: %d */ |
1139 | error(36, len); | | 1139 | error(36, len); |
1140 | tp->t_flen = size(t); | | 1140 | tp->t_flen = size(t); |
1141 | } else if (len == 0 && dsym->s_name != unnamed) { | | 1141 | } else if (len == 0 && dsym->s_name != unnamed) { |
1142 | /* zero size bit-field */ | | 1142 | /* zero size bit-field */ |
1143 | error(37); | | 1143 | error(37); |
1144 | tp->t_flen = size(t); | | 1144 | tp->t_flen = size(t); |
1145 | } | | 1145 | } |
1146 | if (dsym->s_scl == MOU) { | | 1146 | if (dsym->s_scl == MOU) { |
1147 | /* illegal use of bit-field */ | | 1147 | /* illegal use of bit-field */ |
1148 | error(41); | | 1148 | error(41); |
1149 | dsym->s_type->t_isfield = 0; | | 1149 | dsym->s_type->t_isfield = 0; |
1150 | dsym->s_bitfield = 0; | | 1150 | dsym->s_bitfield = 0; |
1151 | } | | 1151 | } |
1152 | } else if (t == FUNC) { | | 1152 | } else if (t == FUNC) { |
1153 | /* function illegal in structure or union */ | | 1153 | /* function illegal in structure or union */ |
1154 | error(38); | | 1154 | error(38); |
1155 | dsym->s_type = tp = incref(tp, t = PTR); | | 1155 | dsym->s_type = tp = incref(tp, t = PTR); |
1156 | } | | 1156 | } |
1157 | | | 1157 | |
1158 | /* | | 1158 | /* |
1159 | * bit-fields of length 0 are not warned about because length() | | 1159 | * bit-fields of length 0 are not warned about because length() |
1160 | * does not return the length of the bit-field but the length | | 1160 | * does not return the length of the bit-field but the length |
1161 | * of the type the bit-field is packed in (it's ok) | | 1161 | * of the type the bit-field is packed in (it's ok) |
1162 | */ | | 1162 | */ |
1163 | if ((sz = length(dsym->s_type, dsym->s_name)) == 0) { | | 1163 | if ((sz = length(dsym->s_type, dsym->s_name)) == 0) { |
1164 | if (t == ARRAY && dsym->s_type->t_dim == 0) { | | 1164 | if (t == ARRAY && dsym->s_type->t_dim == 0) { |
1165 | /* zero sized array in struct is a C99 extension: %s */ | | 1165 | /* zero sized array in struct is a C99 extension: %s */ |
1166 | c99ism(39, dsym->s_name); | | 1166 | c99ism(39, dsym->s_name); |
1167 | } | | 1167 | } |
1168 | } | | 1168 | } |
1169 | | | 1169 | |
1170 | if (dcs->d_ctx == MOU) { | | 1170 | if (dcs->d_ctx == MOU) { |
1171 | o = dcs->d_offset; | | 1171 | o = dcs->d_offset; |
1172 | dcs->d_offset = 0; | | 1172 | dcs->d_offset = 0; |
1173 | } | | 1173 | } |
1174 | if (dsym->s_bitfield) { | | 1174 | if (dsym->s_bitfield) { |
1175 | align(getbound(tp), tp->t_flen); | | 1175 | align(getbound(tp), tp->t_flen); |
1176 | dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t); | | 1176 | dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t); |
1177 | tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad; | | 1177 | tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad; |
1178 | dcs->d_offset += tp->t_flen; | | 1178 | dcs->d_offset += tp->t_flen; |
1179 | } else { | | 1179 | } else { |
1180 | align(getbound(tp), 0); | | 1180 | align(getbound(tp), 0); |
1181 | dsym->s_value.v_quad = dcs->d_offset; | | 1181 | dsym->s_value.v_quad = dcs->d_offset; |
1182 | dcs->d_offset += sz; | | 1182 | dcs->d_offset += sz; |
1183 | } | | 1183 | } |
1184 | if (dcs->d_ctx == MOU) { | | 1184 | if (dcs->d_ctx == MOU) { |
1185 | if (o > dcs->d_offset) | | 1185 | if (o > dcs->d_offset) |
1186 | dcs->d_offset = o; | | 1186 | dcs->d_offset = o; |
1187 | } | | 1187 | } |
1188 | | | 1188 | |
1189 | check_function_definition(dsym, 0); | | 1189 | check_function_definition(dsym, 0); |
1190 | | | 1190 | |
1191 | /* | | 1191 | /* |
1192 | * Clear the BITFIELDTYPE indicator after processing each | | 1192 | * Clear the BITFIELDTYPE indicator after processing each |
1193 | * structure element. | | 1193 | * structure element. |
1194 | */ | | 1194 | */ |
1195 | bitfieldtype_ok = 0; | | 1195 | bitfieldtype_ok = 0; |
1196 | | | 1196 | |
1197 | return dsym; | | 1197 | return dsym; |
1198 | } | | 1198 | } |
1199 | | | 1199 | |
1200 | /* | | 1200 | /* |
1201 | * Aligns next structure element as required. | | 1201 | * Aligns next structure element as required. |
1202 | * | | 1202 | * |
1203 | * al contains the required alignment, len the length of a bit-field. | | 1203 | * al contains the required alignment, len the length of a bit-field. |
1204 | */ | | 1204 | */ |
1205 | static void | | 1205 | static void |
1206 | align(int al, int len) | | 1206 | align(int al, int len) |
1207 | { | | 1207 | { |
1208 | int no; | | 1208 | int no; |
1209 | | | 1209 | |
1210 | /* | | 1210 | /* |
1211 | * The alignment of the current element becomes the alignment of | | 1211 | * The alignment of the current element becomes the alignment of |
1212 | * the struct/union if it is larger than the current alignment | | 1212 | * the struct/union if it is larger than the current alignment |
1213 | * of the struct/union. | | 1213 | * of the struct/union. |
1214 | */ | | 1214 | */ |
1215 | if (al > dcs->d_stralign) | | 1215 | if (al > dcs->d_stralign) |
1216 | dcs->d_stralign = al; | | 1216 | dcs->d_stralign = al; |
1217 | | | 1217 | |
1218 | no = (dcs->d_offset + (al - 1)) & ~(al - 1); | | 1218 | no = (dcs->d_offset + (al - 1)) & ~(al - 1); |
1219 | if (len == 0 || dcs->d_offset + len > no) | | 1219 | if (len == 0 || dcs->d_offset + len > no) |
1220 | dcs->d_offset = no; | | 1220 | dcs->d_offset = no; |
1221 | } | | 1221 | } |
1222 | | | 1222 | |
1223 | /* | | 1223 | /* |
1224 | * Remember the width of the field in its type structure. | | 1224 | * Remember the width of the field in its type structure. |
1225 | */ | | 1225 | */ |
1226 | sym_t * | | 1226 | sym_t * |
1227 | bitfield(sym_t *dsym, int len) | | 1227 | bitfield(sym_t *dsym, int len) |
1228 | { | | 1228 | { |
1229 | | | 1229 | |
1230 | if (dsym == NULL) { | | 1230 | if (dsym == NULL) { |
1231 | dsym = getblk(sizeof (sym_t)); | | 1231 | dsym = getblk(sizeof (sym_t)); |
1232 | dsym->s_name = unnamed; | | 1232 | dsym->s_name = unnamed; |
1233 | dsym->s_kind = FMEMBER; | | 1233 | dsym->s_kind = FMEMBER; |
1234 | dsym->s_scl = MOS; | | 1234 | dsym->s_scl = MOS; |
1235 | dsym->s_type = gettyp(UINT); | | 1235 | dsym->s_type = gettyp(UINT); |
1236 | dsym->s_blklev = -1; | | 1236 | dsym->s_blklev = -1; |
1237 | } | | 1237 | } |
1238 | dsym->s_type = duptyp(dsym->s_type); | | 1238 | dsym->s_type = duptyp(dsym->s_type); |
1239 | dsym->s_type->t_isfield = 1; | | 1239 | dsym->s_type->t_isfield = 1; |
1240 | dsym->s_type->t_flen = len; | | 1240 | dsym->s_type->t_flen = len; |
1241 | dsym->s_bitfield = 1; | | 1241 | dsym->s_bitfield = 1; |
1242 | return dsym; | | 1242 | return dsym; |
1243 | } | | 1243 | } |
1244 | | | 1244 | |
1245 | /* | | 1245 | /* |
1246 | * Collect information about a sequence of asterisks and qualifiers in a | | 1246 | * Collect information about a sequence of asterisks and qualifiers in a |
1247 | * list of type pqinf_t. | | 1247 | * list of type pqinf_t. |
1248 | * Qualifiers always refer to the left asterisk. | | 1248 | * Qualifiers always refer to the left asterisk. |
1249 | * The rightmost asterisk will be at the top of the list. | | 1249 | * The rightmost asterisk will be at the top of the list. |
1250 | */ | | 1250 | */ |
1251 | pqinf_t * | | 1251 | pqinf_t * |
1252 | merge_pointers_and_qualifiers(pqinf_t *p1, pqinf_t *p2) | | 1252 | merge_pointers_and_qualifiers(pqinf_t *p1, pqinf_t *p2) |
1253 | { | | 1253 | { |
1254 | pqinf_t *p; | | 1254 | pqinf_t *p; |
1255 | | | 1255 | |
1256 | if (p2->p_pcnt != 0) { | | 1256 | if (p2->p_pcnt != 0) { |
1257 | /* left '*' at the end of the list */ | | 1257 | /* left '*' at the end of the list */ |
1258 | for (p = p2; p->p_next != NULL; p = p->p_next) | | 1258 | for (p = p2; p->p_next != NULL; p = p->p_next) |
1259 | continue; | | 1259 | continue; |
1260 | p->p_next = p1; | | 1260 | p->p_next = p1; |
1261 | return p2; | | 1261 | return p2; |
1262 | } else { | | 1262 | } else { |
1263 | if (p2->p_const) { | | 1263 | if (p2->p_const) { |
1264 | if (p1->p_const) { | | 1264 | if (p1->p_const) { |
1265 | /* duplicate '%s' */ | | 1265 | /* duplicate '%s' */ |
1266 | warning(10, "const"); | | 1266 | warning(10, "const"); |
1267 | } | | 1267 | } |
1268 | p1->p_const = 1; | | 1268 | p1->p_const = 1; |
1269 | } | | 1269 | } |
1270 | if (p2->p_volatile) { | | 1270 | if (p2->p_volatile) { |
1271 | if (p1->p_volatile) { | | 1271 | if (p1->p_volatile) { |
1272 | /* duplicate '%s' */ | | 1272 | /* duplicate '%s' */ |
1273 | warning(10, "volatile"); | | 1273 | warning(10, "volatile"); |
1274 | } | | 1274 | } |
1275 | p1->p_volatile = 1; | | 1275 | p1->p_volatile = 1; |
1276 | } | | 1276 | } |
1277 | free(p2); | | 1277 | free(p2); |
1278 | return p1; | | 1278 | return p1; |
1279 | } | | 1279 | } |
1280 | } | | 1280 | } |
1281 | | | 1281 | |
1282 | /* | | 1282 | /* |
1283 | * The following 3 functions extend the type of a declarator with | | 1283 | * The following 3 functions extend the type of a declarator with |
1284 | * pointer, function and array types. | | 1284 | * pointer, function and array types. |
1285 | * | | 1285 | * |
1286 | * The current type is the Type built by deftyp() (dcs->d_type) and | | 1286 | * The current type is the Type built by deftyp() (dcs->d_type) and |
1287 | * pointer, function and array types already added for this | | 1287 | * pointer, function and array types already added for this |
1288 | * declarator. The new type extension is inserted between both. | | 1288 | * declarator. The new type extension is inserted between both. |
1289 | */ | | 1289 | */ |
1290 | sym_t * | | 1290 | sym_t * |
1291 | add_pointer(sym_t *decl, pqinf_t *pi) | | 1291 | add_pointer(sym_t *decl, pqinf_t *pi) |
1292 | { | | 1292 | { |
1293 | type_t **tpp, *tp; | | 1293 | type_t **tpp, *tp; |
1294 | pqinf_t *npi; | | 1294 | pqinf_t *npi; |
1295 | | | 1295 | |
1296 | tpp = &decl->s_type; | | 1296 | tpp = &decl->s_type; |
1297 | while (*tpp && *tpp != dcs->d_type) | | 1297 | while (*tpp && *tpp != dcs->d_type) |
1298 | tpp = &(*tpp)->t_subt; | | 1298 | tpp = &(*tpp)->t_subt; |
1299 | if (*tpp == NULL) | | 1299 | if (*tpp == NULL) |
1300 | return decl; | | 1300 | return decl; |
1301 | | | 1301 | |
1302 | while (pi != NULL) { | | 1302 | while (pi != NULL) { |
1303 | *tpp = tp = getblk(sizeof (type_t)); | | 1303 | *tpp = tp = getblk(sizeof (type_t)); |
1304 | tp->t_tspec = PTR; | | 1304 | tp->t_tspec = PTR; |
1305 | tp->t_const = pi->p_const; | | 1305 | tp->t_const = pi->p_const; |
1306 | tp->t_volatile = pi->p_volatile; | | 1306 | tp->t_volatile = pi->p_volatile; |
1307 | *(tpp = &tp->t_subt) = dcs->d_type; | | 1307 | *(tpp = &tp->t_subt) = dcs->d_type; |
1308 | npi = pi->p_next; | | 1308 | npi = pi->p_next; |
1309 | free(pi); | | 1309 | free(pi); |
1310 | pi = npi; | | 1310 | pi = npi; |
1311 | } | | 1311 | } |
1312 | return decl; | | 1312 | return decl; |
1313 | } | | 1313 | } |
1314 | | | 1314 | |
1315 | /* | | 1315 | /* |
1316 | * If a dimension was specified, dim is 1, otherwise 0 | | 1316 | * If a dimension was specified, dim is 1, otherwise 0 |
1317 | * n is the specified dimension | | 1317 | * n is the specified dimension |
1318 | */ | | 1318 | */ |
1319 | sym_t * | | 1319 | sym_t * |
1320 | add_array(sym_t *decl, int dim, int n) | | 1320 | add_array(sym_t *decl, int dim, int n) |
1321 | { | | 1321 | { |
1322 | type_t **tpp, *tp; | | 1322 | type_t **tpp, *tp; |
1323 | | | 1323 | |
1324 | tpp = &decl->s_type; | | 1324 | tpp = &decl->s_type; |
1325 | while (*tpp && *tpp != dcs->d_type) | | 1325 | while (*tpp && *tpp != dcs->d_type) |
1326 | tpp = &(*tpp)->t_subt; | | 1326 | tpp = &(*tpp)->t_subt; |
1327 | if (*tpp == NULL) | | 1327 | if (*tpp == NULL) |
1328 | return decl; | | 1328 | return decl; |
1329 | | | 1329 | |
1330 | *tpp = tp = getblk(sizeof (type_t)); | | 1330 | *tpp = tp = getblk(sizeof (type_t)); |
1331 | tp->t_tspec = ARRAY; | | 1331 | tp->t_tspec = ARRAY; |
1332 | tp->t_subt = dcs->d_type; | | 1332 | tp->t_subt = dcs->d_type; |
1333 | tp->t_dim = n; | | 1333 | tp->t_dim = n; |
1334 | | | 1334 | |
1335 | if (n < 0) { | | 1335 | if (n < 0) { |
1336 | /* negative array dimension (%d) */ | | 1336 | /* negative array dimension (%d) */ |
1337 | error(20, n); | | 1337 | error(20, n); |
1338 | n = 0; | | 1338 | n = 0; |
1339 | } else if (n == 0 && dim) { | | 1339 | } else if (n == 0 && dim) { |
1340 | /* zero sized array is a C99 extension */ | | 1340 | /* zero sized array is a C99 extension */ |
1341 | c99ism(322); | | 1341 | c99ism(322); |
1342 | } else if (n == 0 && !dim) { | | 1342 | } else if (n == 0 && !dim) { |
1343 | setcomplete(tp, 0); | | 1343 | setcomplete(tp, 0); |
1344 | } | | 1344 | } |
1345 | | | 1345 | |
1346 | return decl; | | 1346 | return decl; |
1347 | } | | 1347 | } |
1348 | | | 1348 | |
1349 | sym_t * | | 1349 | sym_t * |
1350 | add_function(sym_t *decl, sym_t *args) | | 1350 | add_function(sym_t *decl, sym_t *args) |
1351 | { | | 1351 | { |
1352 | type_t **tpp, *tp; | | 1352 | type_t **tpp, *tp; |
1353 | | | 1353 | |
1354 | if (dcs->d_proto) { | | 1354 | if (dcs->d_proto) { |
1355 | if (tflag) | | 1355 | if (tflag) |
1356 | /* function prototypes are illegal in traditional C */ | | 1356 | /* function prototypes are illegal in traditional C */ |
1357 | warning(270); | | 1357 | warning(270); |
1358 | args = new_style_function(decl, args); | | 1358 | args = new_style_function(decl, args); |
1359 | } else { | | 1359 | } else { |
1360 | old_style_function(decl, args); | | 1360 | old_style_function(decl, args); |
1361 | } | | 1361 | } |
1362 | | | 1362 | |
1363 | /* | | 1363 | /* |
1364 | * The symbols are removed from the symbol table by popdecl() after | | 1364 | * The symbols are removed from the symbol table by popdecl() after |
1365 | * add_function(). To be able to restore them if this is a function | | 1365 | * add_function(). To be able to restore them if this is a function |
1366 | * definition, a pointer to the list of all symbols is stored in | | 1366 | * definition, a pointer to the list of all symbols is stored in |
1367 | * dcs->d_next->d_fpsyms. Also a list of the arguments (concatenated | | 1367 | * dcs->d_next->d_fpsyms. Also a list of the arguments (concatenated |
1368 | * by s_next) is stored in dcs->d_next->d_fargs. | | 1368 | * by s_next) is stored in dcs->d_next->d_fargs. |
1369 | * (dcs->d_next must be used because *dcs is the declaration stack | | 1369 | * (dcs->d_next must be used because *dcs is the declaration stack |
1370 | * element created for the list of params and is removed after | | 1370 | * element created for the list of params and is removed after |
1371 | * add_function()) | | 1371 | * add_function()) |
1372 | */ | | 1372 | */ |
1373 | if (dcs->d_next->d_ctx == EXTERN && | | 1373 | if (dcs->d_next->d_ctx == EXTERN && |
1374 | decl->s_type == dcs->d_next->d_type) { | | 1374 | decl->s_type == dcs->d_next->d_type) { |
1375 | dcs->d_next->d_fpsyms = dcs->d_dlsyms; | | 1375 | dcs->d_next->d_fpsyms = dcs->d_dlsyms; |
1376 | dcs->d_next->d_fargs = args; | | 1376 | dcs->d_next->d_fargs = args; |
1377 | } | | 1377 | } |
1378 | | | 1378 | |
1379 | tpp = &decl->s_type; | | 1379 | tpp = &decl->s_type; |
1380 | while (*tpp && *tpp != dcs->d_next->d_type) | | 1380 | while (*tpp && *tpp != dcs->d_next->d_type) |
1381 | tpp = &(*tpp)->t_subt; | | 1381 | tpp = &(*tpp)->t_subt; |
1382 | if (*tpp == NULL) | | 1382 | if (*tpp == NULL) |
1383 | return decl; | | 1383 | return decl; |
1384 | | | 1384 | |
1385 | *tpp = tp = getblk(sizeof (type_t)); | | 1385 | *tpp = tp = getblk(sizeof (type_t)); |
1386 | tp->t_tspec = FUNC; | | 1386 | tp->t_tspec = FUNC; |
1387 | tp->t_subt = dcs->d_next->d_type; | | 1387 | tp->t_subt = dcs->d_next->d_type; |
1388 | if ((tp->t_proto = dcs->d_proto) != 0) | | 1388 | if ((tp->t_proto = dcs->d_proto) != 0) |
1389 | tp->t_args = args; | | 1389 | tp->t_args = args; |
1390 | tp->t_vararg = dcs->d_vararg; | | 1390 | tp->t_vararg = dcs->d_vararg; |
1391 | | | 1391 | |
1392 | return decl; | | 1392 | return decl; |
1393 | } | | 1393 | } |
1394 | | | 1394 | |
1395 | /* | | 1395 | /* |
1396 | * Called for new style function declarations. | | 1396 | * Called for new style function declarations. |
1397 | */ | | 1397 | */ |
1398 | /* ARGSUSED */ | | 1398 | /* ARGSUSED */ |
1399 | static sym_t * | | 1399 | static sym_t * |
1400 | new_style_function(sym_t *decl, sym_t *args) | | 1400 | new_style_function(sym_t *decl, sym_t *args) |
1401 | { | | 1401 | { |
1402 | sym_t *arg, *sym; | | 1402 | sym_t *arg, *sym; |
1403 | scl_t sc; | | 1403 | scl_t sc; |
1404 | int n; | | 1404 | int n; |
1405 | | | 1405 | |
1406 | /* | | 1406 | /* |
1407 | * Declarations of structs/unions/enums in param lists are legal, | | 1407 | * Declarations of structs/unions/enums in param lists are legal, |
1408 | * but senseless. | | 1408 | * but senseless. |
1409 | */ | | 1409 | */ |
1410 | for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) { | | 1410 | for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) { |
1411 | sc = sym->s_scl; | | 1411 | sc = sym->s_scl; |
1412 | if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) { | | 1412 | if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) { |
1413 | /* dubious tag declaration: %s %s */ | | 1413 | /* dubious tag declaration: %s %s */ |
1414 | warning(85, storage_class_name(sc), sym->s_name); | | 1414 | warning(85, storage_class_name(sc), sym->s_name); |
1415 | } | | 1415 | } |
1416 | } | | 1416 | } |
1417 | | | 1417 | |
1418 | n = 1; | | 1418 | n = 1; |
1419 | for (arg = args; arg != NULL; arg = arg->s_next) { | | 1419 | for (arg = args; arg != NULL; arg = arg->s_next) { |
1420 | if (arg->s_type->t_tspec == VOID) { | | 1420 | if (arg->s_type->t_tspec == VOID) { |
1421 | if (n > 1 || arg->s_next != NULL) { | | 1421 | if (n > 1 || arg->s_next != NULL) { |
1422 | /* void must be sole parameter */ | | 1422 | /* void must be sole parameter */ |
1423 | error(60); | | 1423 | error(60); |
1424 | arg->s_type = gettyp(INT); | | 1424 | arg->s_type = gettyp(INT); |
1425 | } | | 1425 | } |
1426 | } | | 1426 | } |
1427 | n++; | | 1427 | n++; |
1428 | } | | 1428 | } |
1429 | | | 1429 | |
1430 | /* return NULL if first param is VOID */ | | 1430 | /* return NULL if first param is VOID */ |
1431 | return args != NULL && args->s_type->t_tspec != VOID ? args : NULL; | | 1431 | return args != NULL && args->s_type->t_tspec != VOID ? args : NULL; |
1432 | } | | 1432 | } |
1433 | | | 1433 | |
1434 | /* | | 1434 | /* |
1435 | * Called for old style function declarations. | | 1435 | * Called for old style function declarations. |
1436 | */ | | 1436 | */ |
1437 | static void | | 1437 | static void |
1438 | old_style_function(sym_t *decl, sym_t *args) | | 1438 | old_style_function(sym_t *decl, sym_t *args) |
1439 | { | | 1439 | { |
1440 | | | 1440 | |
1441 | /* | | 1441 | /* |
1442 | * Remember list of params only if this is really seams to be | | 1442 | * Remember list of params only if this is really seams to be |
1443 | * a function definition. | | 1443 | * a function definition. |
1444 | */ | | 1444 | */ |
1445 | if (dcs->d_next->d_ctx == EXTERN && | | 1445 | if (dcs->d_next->d_ctx == EXTERN && |
1446 | decl->s_type == dcs->d_next->d_type) { | | 1446 | decl->s_type == dcs->d_next->d_type) { |
1447 | /* | | 1447 | /* |
1448 | * We assume that this becomes a function definition. If | | 1448 | * We assume that this becomes a function definition. If |
1449 | * we are wrong, it's corrected in check_function_definition(). | | 1449 | * we are wrong, it's corrected in check_function_definition(). |
1450 | */ | | 1450 | */ |
1451 | if (args != NULL) { | | 1451 | if (args != NULL) { |
1452 | decl->s_osdef = 1; | | 1452 | decl->s_osdef = 1; |
1453 | decl->s_args = args; | | 1453 | decl->s_args = args; |
1454 | } | | 1454 | } |
1455 | } else { | | 1455 | } else { |
1456 | if (args != NULL) | | 1456 | if (args != NULL) |
1457 | /* function prototype parameters must have types */ | | 1457 | /* function prototype parameters must have types */ |
1458 | warning(62); | | 1458 | warning(62); |
1459 | } | | 1459 | } |
1460 | } | | 1460 | } |
1461 | | | 1461 | |
1462 | /* | | 1462 | /* |
1463 | * Lists of Identifiers in functions declarations are allowed only if | | 1463 | * Lists of Identifiers in functions declarations are allowed only if |
1464 | * it's also a function definition. If this is not the case, print a | | 1464 | * it's also a function definition. If this is not the case, print a |
1465 | * error message. | | 1465 | * error message. |
1466 | */ | | 1466 | */ |
1467 | void | | 1467 | void |
1468 | check_function_definition(sym_t *sym, int msg) | | 1468 | check_function_definition(sym_t *sym, int msg) |
1469 | { | | 1469 | { |
1470 | | | 1470 | |
1471 | if (sym->s_osdef) { | | 1471 | if (sym->s_osdef) { |
1472 | if (msg) { | | 1472 | if (msg) { |
1473 | /* incomplete or misplaced function definition */ | | 1473 | /* incomplete or misplaced function definition */ |
1474 | error(22); | | 1474 | error(22); |
1475 | } | | 1475 | } |
1476 | sym->s_osdef = 0; | | 1476 | sym->s_osdef = 0; |
1477 | sym->s_args = NULL; | | 1477 | sym->s_args = NULL; |
1478 | } | | 1478 | } |
1479 | } | | 1479 | } |
1480 | | | 1480 | |
1481 | /* | | 1481 | /* |
1482 | * Process the name in a declarator. | | 1482 | * Process the name in a declarator. |
1483 | * The symbol gets one of the storage classes EXTERN, STATIC, AUTO or | | 1483 | * The symbol gets one of the storage classes EXTERN, STATIC, AUTO or |
1484 | * TYPEDEF. | | 1484 | * TYPEDEF. |
1485 | * s_def and s_reg are valid after declarator_name(). | | 1485 | * s_def and s_reg are valid after declarator_name(). |
1486 | */ | | 1486 | */ |
1487 | sym_t * | | 1487 | sym_t * |
1488 | declarator_name(sym_t *sym) | | 1488 | declarator_name(sym_t *sym) |
1489 | { | | 1489 | { |
1490 | scl_t sc = NOSCL; | | 1490 | scl_t sc = NOSCL; |
1491 | | | 1491 | |
1492 | if (sym->s_scl == NOSCL) { | | 1492 | if (sym->s_scl == NOSCL) { |
1493 | dcs->d_rdcsym = NULL; | | 1493 | dcs->d_rdcsym = NULL; |
1494 | } else if (sym->s_defarg) { | | 1494 | } else if (sym->s_defarg) { |
1495 | sym->s_defarg = 0; | | 1495 | sym->s_defarg = 0; |
1496 | dcs->d_rdcsym = NULL; | | 1496 | dcs->d_rdcsym = NULL; |
1497 | } else { | | 1497 | } else { |
1498 | dcs->d_rdcsym = sym; | | 1498 | dcs->d_rdcsym = sym; |
1499 | sym = pushdown(sym); | | 1499 | sym = pushdown(sym); |
1500 | } | | 1500 | } |
1501 | | | 1501 | |
1502 | switch (dcs->d_ctx) { | | 1502 | switch (dcs->d_ctx) { |
1503 | case MOS: | | 1503 | case MOS: |
1504 | case MOU: | | 1504 | case MOU: |
1505 | /* Set parent */ | | 1505 | /* Set parent */ |
1506 | sym->s_styp = dcs->d_tagtyp->t_str; | | 1506 | sym->s_styp = dcs->d_tagtyp->t_str; |
1507 | sym->s_def = DEF; | | 1507 | sym->s_def = DEF; |
1508 | sym->s_value.v_tspec = INT; | | 1508 | sym->s_value.v_tspec = INT; |
1509 | sc = dcs->d_ctx; | | 1509 | sc = dcs->d_ctx; |
1510 | break; | | 1510 | break; |
1511 | case EXTERN: | | 1511 | case EXTERN: |
1512 | /* | | 1512 | /* |
1513 | * static and external symbols without "extern" are | | 1513 | * static and external symbols without "extern" are |
1514 | * considered to be tentative defined, external | | 1514 | * considered to be tentative defined, external |
1515 | * symbols with "extern" are declared, and typedef names | | 1515 | * symbols with "extern" are declared, and typedef names |
1516 | * are defined. Tentative defined and declared symbols | | 1516 | * are defined. Tentative defined and declared symbols |
1517 | * may become defined if an initializer is present or | | 1517 | * may become defined if an initializer is present or |
1518 | * this is a function definition. | | 1518 | * this is a function definition. |
1519 | */ | | 1519 | */ |
1520 | if ((sc = dcs->d_scl) == NOSCL) { | | 1520 | if ((sc = dcs->d_scl) == NOSCL) { |
1521 | sc = EXTERN; | | 1521 | sc = EXTERN; |
1522 | sym->s_def = TDEF; | | 1522 | sym->s_def = TDEF; |
1523 | } else if (sc == STATIC) { | | 1523 | } else if (sc == STATIC) { |
1524 | sym->s_def = TDEF; | | 1524 | sym->s_def = TDEF; |
1525 | } else if (sc == TYPEDEF) { | | 1525 | } else if (sc == TYPEDEF) { |
1526 | sym->s_def = DEF; | | 1526 | sym->s_def = DEF; |
1527 | } else { | | 1527 | } else { |
1528 | lint_assert(sc == EXTERN); | | 1528 | lint_assert(sc == EXTERN); |
1529 | sym->s_def = DECL; | | 1529 | sym->s_def = DECL; |
1530 | } | | 1530 | } |
1531 | break; | | 1531 | break; |
1532 | case PARG: | | 1532 | case PARG: |
1533 | sym->s_arg = 1; | | 1533 | sym->s_arg = 1; |
1534 | /* FALLTHROUGH */ | | 1534 | /* FALLTHROUGH */ |
1535 | case ARG: | | 1535 | case ARG: |
1536 | if ((sc = dcs->d_scl) == NOSCL) { | | 1536 | if ((sc = dcs->d_scl) == NOSCL) { |
1537 | sc = AUTO; | | 1537 | sc = AUTO; |
1538 | } else { | | 1538 | } else { |
1539 | lint_assert(sc == REG); | | 1539 | lint_assert(sc == REG); |
1540 | sym->s_reg = 1; | | 1540 | sym->s_reg = 1; |
1541 | sc = AUTO; | | 1541 | sc = AUTO; |
1542 | } | | 1542 | } |
1543 | sym->s_def = DEF; | | 1543 | sym->s_def = DEF; |
1544 | break; | | 1544 | break; |
1545 | case AUTO: | | 1545 | case AUTO: |
1546 | if ((sc = dcs->d_scl) == NOSCL) { | | 1546 | if ((sc = dcs->d_scl) == NOSCL) { |
1547 | /* | | 1547 | /* |
1548 | * XXX somewhat ugly because we dont know whether | | 1548 | * XXX somewhat ugly because we dont know whether |
1549 | * this is AUTO or EXTERN (functions). If we are | | 1549 | * this is AUTO or EXTERN (functions). If we are |
1550 | * wrong it must be corrected in decl1loc(), where | | 1550 | * wrong it must be corrected in decl1loc(), where |
1551 | * we have the necessary type information. | | 1551 | * we have the necessary type information. |
1552 | */ | | 1552 | */ |
1553 | sc = AUTO; | | 1553 | sc = AUTO; |
1554 | sym->s_def = DEF; | | 1554 | sym->s_def = DEF; |
1555 | } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) { | | 1555 | } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) { |
1556 | sym->s_def = DEF; | | 1556 | sym->s_def = DEF; |
1557 | } else if (sc == REG) { | | 1557 | } else if (sc == REG) { |
1558 | sym->s_reg = 1; | | 1558 | sym->s_reg = 1; |
1559 | sc = AUTO; | | 1559 | sc = AUTO; |
1560 | sym->s_def = DEF; | | 1560 | sym->s_def = DEF; |
1561 | } else { | | 1561 | } else { |
1562 | lint_assert(sc == EXTERN); | | 1562 | lint_assert(sc == EXTERN); |
1563 | sym->s_def = DECL; | | 1563 | sym->s_def = DECL; |
1564 | } | | 1564 | } |
1565 | break; | | 1565 | break; |
1566 | default: | | 1566 | default: |
1567 | lint_assert(0); | | 1567 | lint_assert(0); |
1568 | } | | 1568 | } |
1569 | sym->s_scl = sc; | | 1569 | sym->s_scl = sc; |
1570 | | | 1570 | |
1571 | sym->s_type = dcs->d_type; | | 1571 | sym->s_type = dcs->d_type; |
1572 | | | 1572 | |
1573 | dcs->d_fpsyms = NULL; | | 1573 | dcs->d_fpsyms = NULL; |
1574 | | | 1574 | |
1575 | return sym; | | 1575 | return sym; |
1576 | } | | 1576 | } |
1577 | | | 1577 | |
1578 | /* | | 1578 | /* |
1579 | * Process a name in the list of formal params in an old style function | | 1579 | * Process a name in the list of formal params in an old style function |
1580 | * definition. | | 1580 | * definition. |
1581 | */ | | 1581 | */ |
1582 | sym_t * | | 1582 | sym_t * |
1583 | old_style_function_name(sym_t *sym) | | 1583 | old_style_function_name(sym_t *sym) |
1584 | { | | 1584 | { |
1585 | | | 1585 | |
1586 | if (sym->s_scl != NOSCL) { | | 1586 | if (sym->s_scl != NOSCL) { |
1587 | if (blklev == sym->s_blklev) { | | 1587 | if (blklev == sym->s_blklev) { |
1588 | /* redeclaration of formal parameter %s */ | | 1588 | /* redeclaration of formal parameter %s */ |
1589 | error(21, sym->s_name); | | 1589 | error(21, sym->s_name); |
1590 | lint_assert(sym->s_defarg); | | 1590 | lint_assert(sym->s_defarg); |
1591 | } | | 1591 | } |
1592 | sym = pushdown(sym); | | 1592 | sym = pushdown(sym); |
1593 | } | | 1593 | } |
1594 | sym->s_type = gettyp(INT); | | 1594 | sym->s_type = gettyp(INT); |
1595 | sym->s_scl = AUTO; | | 1595 | sym->s_scl = AUTO; |
1596 | sym->s_def = DEF; | | 1596 | sym->s_def = DEF; |
1597 | sym->s_defarg = sym->s_arg = 1; | | 1597 | sym->s_defarg = sym->s_arg = 1; |
1598 | return sym; | | 1598 | return sym; |
1599 | } | | 1599 | } |
1600 | | | 1600 | |
1601 | /* | | 1601 | /* |
1602 | * Create the type of a tag. | | 1602 | * Create the type of a tag. |
1603 | * | | 1603 | * |
1604 | * tag points to the symbol table entry of the tag | | 1604 | * tag points to the symbol table entry of the tag |
1605 | * kind is the kind of the tag (STRUCT/UNION/ENUM) | | 1605 | * kind is the kind of the tag (STRUCT/UNION/ENUM) |
1606 | * decl is 1 if the type of the tag will be completed in this declaration | | 1606 | * decl is 1 if the type of the tag will be completed in this declaration |
1607 | * (the following token is T_LBRACE) | | 1607 | * (the following token is T_LBRACE) |
1608 | * semi is 1 if the following token is T_SEMI | | 1608 | * semi is 1 if the following token is T_SEMI |
1609 | */ | | 1609 | */ |
1610 | type_t * | | 1610 | type_t * |
1611 | mktag(sym_t *tag, tspec_t kind, int decl, int semi) | | 1611 | mktag(sym_t *tag, tspec_t kind, int decl, int semi) |
1612 | { | | 1612 | { |
1613 | scl_t scl = NOSCL; | | 1613 | scl_t scl = NOSCL; |
1614 | type_t *tp; | | 1614 | type_t *tp; |
1615 | | | 1615 | |
1616 | if (kind == STRUCT) { | | 1616 | if (kind == STRUCT) { |
1617 | scl = STRTAG; | | 1617 | scl = STRTAG; |
1618 | } else if (kind == UNION) { | | 1618 | } else if (kind == UNION) { |
1619 | scl = UNIONTAG; | | 1619 | scl = UNIONTAG; |
1620 | } else { | | 1620 | } else { |
1621 | lint_assert(kind == ENUM); | | 1621 | lint_assert(kind == ENUM); |
1622 | scl = ENUMTAG; | | 1622 | scl = ENUMTAG; |
1623 | } | | 1623 | } |
1624 | | | 1624 | |
1625 | if (tag != NULL) { | | 1625 | if (tag != NULL) { |
1626 | if (tag->s_scl != NOSCL) { | | 1626 | if (tag->s_scl != NOSCL) { |
1627 | tag = newtag(tag, scl, decl, semi); | | 1627 | tag = newtag(tag, scl, decl, semi); |
1628 | } else { | | 1628 | } else { |
1629 | /* a new tag, no empty declaration */ | | 1629 | /* a new tag, no empty declaration */ |
1630 | dcs->d_next->d_nedecl = 1; | | 1630 | dcs->d_next->d_nedecl = 1; |
1631 | if (scl == ENUMTAG && !decl) { | | 1631 | if (scl == ENUMTAG && !decl) { |
1632 | if (!tflag && (sflag || pflag)) | | 1632 | if (!tflag && (sflag || pflag)) |
1633 | /* forward reference to enum type */ | | 1633 | /* forward reference to enum type */ |
1634 | warning(42); | | 1634 | warning(42); |
1635 | } | | 1635 | } |
1636 | } | | 1636 | } |
1637 | if (tag->s_scl == NOSCL) { | | 1637 | if (tag->s_scl == NOSCL) { |
1638 | tag->s_scl = scl; | | 1638 | tag->s_scl = scl; |
1639 | tag->s_type = tp = getblk(sizeof (type_t)); | | 1639 | tag->s_type = tp = getblk(sizeof (type_t)); |
1640 | tp->t_ispacked = dcs->d_ispacked; | | 1640 | tp->t_ispacked = dcs->d_ispacked; |
1641 | } else { | | 1641 | } else { |
1642 | tp = tag->s_type; | | 1642 | tp = tag->s_type; |
1643 | } | | 1643 | } |
1644 | } else { | | 1644 | } else { |
1645 | tag = getblk(sizeof (sym_t)); | | 1645 | tag = getblk(sizeof (sym_t)); |
1646 | tag->s_name = unnamed; | | 1646 | tag->s_name = unnamed; |
1647 | UNIQUE_CURR_POS(tag->s_def_pos); | | 1647 | UNIQUE_CURR_POS(tag->s_def_pos); |
1648 | tag->s_kind = FTAG; | | 1648 | tag->s_kind = FTAG; |
1649 | tag->s_scl = scl; | | 1649 | tag->s_scl = scl; |
1650 | tag->s_blklev = -1; | | 1650 | tag->s_blklev = -1; |
1651 | tag->s_type = tp = getblk(sizeof (type_t)); | | 1651 | tag->s_type = tp = getblk(sizeof (type_t)); |
1652 | tp->t_ispacked = dcs->d_ispacked; | | 1652 | tp->t_ispacked = dcs->d_ispacked; |
1653 | dcs->d_next->d_nedecl = 1; | | 1653 | dcs->d_next->d_nedecl = 1; |
1654 | } | | 1654 | } |
1655 | | | 1655 | |
1656 | if (tp->t_tspec == NOTSPEC) { | | 1656 | if (tp->t_tspec == NOTSPEC) { |
1657 | tp->t_tspec = kind; | | 1657 | tp->t_tspec = kind; |
1658 | if (kind != ENUM) { | | 1658 | if (kind != ENUM) { |
1659 | tp->t_str = getblk(sizeof (str_t)); | | 1659 | tp->t_str = getblk(sizeof (str_t)); |
1660 | tp->t_str->align = CHAR_BIT; | | 1660 | tp->t_str->align = CHAR_BIT; |
1661 | tp->t_str->stag = tag; | | 1661 | tp->t_str->stag = tag; |
1662 | } else { | | 1662 | } else { |
1663 | tp->t_isenum = 1; | | 1663 | tp->t_isenum = 1; |
1664 | tp->t_enum = getblk(sizeof(*tp->t_enum)); | | 1664 | tp->t_enum = getblk(sizeof(*tp->t_enum)); |
1665 | tp->t_enum->etag = tag; | | 1665 | tp->t_enum->etag = tag; |
1666 | } | | 1666 | } |
1667 | setcomplete(tp, 0); | | 1667 | setcomplete(tp, 0); |
1668 | } | | 1668 | } |
1669 | return tp; | | 1669 | return tp; |
1670 | } | | 1670 | } |
1671 | | | 1671 | |
1672 | /* | | 1672 | /* |
1673 | * Checks all possible cases of tag redeclarations. | | 1673 | * Checks all possible cases of tag redeclarations. |
1674 | * decl is 1 if T_LBRACE follows | | 1674 | * decl is 1 if T_LBRACE follows |
1675 | * semi is 1 if T_SEMI follows | | 1675 | * semi is 1 if T_SEMI follows |
1676 | */ | | 1676 | */ |
1677 | static sym_t * | | 1677 | static sym_t * |
1678 | newtag(sym_t *tag, scl_t scl, int decl, int semi) | | 1678 | newtag(sym_t *tag, scl_t scl, int decl, int semi) |
1679 | { | | 1679 | { |
1680 | | | 1680 | |
1681 | if (tag->s_blklev < blklev) { | | 1681 | if (tag->s_blklev < blklev) { |
1682 | if (semi) { | | 1682 | if (semi) { |
1683 | /* "struct a;" */ | | 1683 | /* "struct a;" */ |
1684 | if (!tflag) { | | 1684 | if (!tflag) { |
1685 | if (!sflag) | | 1685 | if (!sflag) |
1686 | /* decl. introduces new type ... */ | | 1686 | /* decl. introduces new type ... */ |
1687 | warning(44, storage_class_name(scl), | | 1687 | warning(44, storage_class_name(scl), |
1688 | tag->s_name); | | 1688 | tag->s_name); |
1689 | tag = pushdown(tag); | | 1689 | tag = pushdown(tag); |
1690 | } else if (tag->s_scl != scl) { | | 1690 | } else if (tag->s_scl != scl) { |
1691 | /* base type is really '%s %s' */ | | 1691 | /* base type is really '%s %s' */ |
1692 | warning(45, storage_class_name(tag->s_scl), | | 1692 | warning(45, storage_class_name(tag->s_scl), |
1693 | tag->s_name); | | 1693 | tag->s_name); |
1694 | } | | 1694 | } |
1695 | dcs->d_next->d_nedecl = 1; | | 1695 | dcs->d_next->d_nedecl = 1; |
1696 | } else if (decl) { | | 1696 | } else if (decl) { |
1697 | /* "struct a { ... } " */ | | 1697 | /* "struct a { ... } " */ |
1698 | if (hflag) | | 1698 | if (hflag) |
1699 | /* redefinition hides earlier one: %s */ | | 1699 | /* redefinition hides earlier one: %s */ |
1700 | warning(43, tag->s_name); | | 1700 | warning(43, tag->s_name); |
1701 | tag = pushdown(tag); | | 1701 | tag = pushdown(tag); |
1702 | dcs->d_next->d_nedecl = 1; | | 1702 | dcs->d_next->d_nedecl = 1; |
1703 | } else if (tag->s_scl != scl) { | | 1703 | } else if (tag->s_scl != scl) { |
1704 | /* base type is really '%s %s' */ | | 1704 | /* base type is really '%s %s' */ |
1705 | warning(45, storage_class_name(tag->s_scl), | | 1705 | warning(45, storage_class_name(tag->s_scl), |
1706 | tag->s_name); | | 1706 | tag->s_name); |
1707 | /* declaration introduces new type in ANSI C: %s %s */ | | 1707 | /* declaration introduces new type in ANSI C: %s %s */ |
1708 | if (!sflag) { | | 1708 | if (!sflag) { |
1709 | /* decl. introduces new type in ANSI C: %s %s */ | | 1709 | /* decl. introduces new type in ANSI C: %s %s */ |
1710 | warning(44, storage_class_name(scl), | | 1710 | warning(44, storage_class_name(scl), |
1711 | tag->s_name); | | 1711 | tag->s_name); |
1712 | } | | 1712 | } |
1713 | tag = pushdown(tag); | | 1713 | tag = pushdown(tag); |
1714 | dcs->d_next->d_nedecl = 1; | | 1714 | dcs->d_next->d_nedecl = 1; |
1715 | } | | 1715 | } |
1716 | } else { | | 1716 | } else { |
1717 | if (tag->s_scl != scl) { | | 1717 | if (tag->s_scl != scl) { |
1718 | /* (%s) tag redeclared */ | | 1718 | /* (%s) tag redeclared */ |
1719 | error(46, storage_class_name(tag->s_scl)); | | 1719 | error(46, storage_class_name(tag->s_scl)); |
1720 | print_previous_declaration(-1, tag); | | 1720 | print_previous_declaration(-1, tag); |
1721 | tag = pushdown(tag); | | 1721 | tag = pushdown(tag); |
1722 | dcs->d_next->d_nedecl = 1; | | 1722 | dcs->d_next->d_nedecl = 1; |
1723 | } else if (decl && !incompl(tag->s_type)) { | | 1723 | } else if (decl && !incompl(tag->s_type)) { |
1724 | /* (%s) tag redeclared */ | | 1724 | /* (%s) tag redeclared */ |
1725 | error(46, storage_class_name(tag->s_scl)); | | 1725 | error(46, storage_class_name(tag->s_scl)); |
1726 | print_previous_declaration(-1, tag); | | 1726 | print_previous_declaration(-1, tag); |
1727 | tag = pushdown(tag); | | 1727 | tag = pushdown(tag); |
1728 | dcs->d_next->d_nedecl = 1; | | 1728 | dcs->d_next->d_nedecl = 1; |
1729 | } else if (semi || decl) { | | 1729 | } else if (semi || decl) { |
1730 | dcs->d_next->d_nedecl = 1; | | 1730 | dcs->d_next->d_nedecl = 1; |
1731 | } | | 1731 | } |
1732 | } | | 1732 | } |
1733 | return tag; | | 1733 | return tag; |
1734 | } | | 1734 | } |
1735 | | | 1735 | |
1736 | const char * | | 1736 | const char * |
1737 | storage_class_name(scl_t sc) | | 1737 | storage_class_name(scl_t sc) |
1738 | { | | 1738 | { |
1739 | const char *s; | | 1739 | const char *s; |
1740 | | | 1740 | |
1741 | switch (sc) { | | 1741 | switch (sc) { |
1742 | case EXTERN: s = "extern"; break; | | 1742 | case EXTERN: s = "extern"; break; |
1743 | case STATIC: s = "static"; break; | | 1743 | case STATIC: s = "static"; break; |
1744 | case AUTO: s = "auto"; break; | | 1744 | case AUTO: s = "auto"; break; |
1745 | case REG: s = "register"; break; | | 1745 | case REG: s = "register"; break; |
1746 | case TYPEDEF: s = "typedef"; break; | | 1746 | case TYPEDEF: s = "typedef"; break; |
1747 | case STRTAG: s = "struct"; break; | | 1747 | case STRTAG: s = "struct"; break; |
1748 | case UNIONTAG: s = "union"; break; | | 1748 | case UNIONTAG: s = "union"; break; |
1749 | case ENUMTAG: s = "enum"; break; | | 1749 | case ENUMTAG: s = "enum"; break; |
1750 | default: lint_assert(0); | | 1750 | default: lint_assert(0); |
1751 | } | | 1751 | } |
1752 | return s; | | 1752 | return s; |
1753 | } | | 1753 | } |
1754 | | | 1754 | |
1755 | /* | | 1755 | /* |
1756 | * tp points to the type of the tag, fmem to the list of members. | | 1756 | * tp points to the type of the tag, fmem to the list of members. |
1757 | */ | | 1757 | */ |
1758 | type_t * | | 1758 | type_t * |
1759 | complete_tag_struct_or_union(type_t *tp, sym_t *fmem) | | 1759 | complete_tag_struct_or_union(type_t *tp, sym_t *fmem) |
1760 | { | | 1760 | { |
1761 | tspec_t t; | | 1761 | tspec_t t; |
1762 | str_t *sp; | | 1762 | str_t *sp; |
1763 | int n; | | 1763 | int n; |
1764 | sym_t *mem; | | 1764 | sym_t *mem; |
1765 | | | 1765 | |
1766 | setcomplete(tp, 1); | | 1766 | setcomplete(tp, 1); |
1767 | | | 1767 | |
1768 | t = tp->t_tspec; | | 1768 | t = tp->t_tspec; |
1769 | align(dcs->d_stralign, 0); | | 1769 | align(dcs->d_stralign, 0); |
1770 | sp = tp->t_str; | | 1770 | sp = tp->t_str; |
1771 | sp->align = dcs->d_stralign; | | 1771 | sp->align = dcs->d_stralign; |
1772 | sp->memb = fmem; | | 1772 | sp->memb = fmem; |
1773 | if (tp->t_ispacked) | | 1773 | if (tp->t_ispacked) |
1774 | setpackedsize(tp); | | 1774 | setpackedsize(tp); |
1775 | else | | 1775 | else |
1776 | sp->size = dcs->d_offset; | | 1776 | sp->size = dcs->d_offset; |
1777 | | | 1777 | |
1778 | if (sp->size == 0) { | | 1778 | if (sp->size == 0) { |
1779 | /* zero sized %s */ | | 1779 | /* zero sized %s */ |
1780 | (void)c99ism(47, ttab[t].tt_name); | | 1780 | (void)c99ism(47, ttab[t].tt_name); |
1781 | } | | 1781 | } |
1782 | | | 1782 | |
1783 | n = 0; | | 1783 | n = 0; |
1784 | for (mem = fmem; mem != NULL; mem = mem->s_next) { | | 1784 | for (mem = fmem; mem != NULL; mem = mem->s_next) { |
1785 | /* bind anonymous members to the structure */ | | 1785 | /* bind anonymous members to the structure */ |
1786 | if (mem->s_styp == NULL) { | | 1786 | if (mem->s_styp == NULL) { |
1787 | mem->s_styp = sp; | | 1787 | mem->s_styp = sp; |
1788 | if (mem->s_type->t_isfield) { | | 1788 | if (mem->s_type->t_isfield) { |
1789 | sp->size += bitfieldsize(&mem); | | 1789 | sp->size += bitfieldsize(&mem); |
1790 | if (mem == NULL) | | 1790 | if (mem == NULL) |
1791 | break; | | 1791 | break; |
1792 | } | | 1792 | } |
1793 | sp->size += tsize(mem->s_type); | | 1793 | sp->size += tsize(mem->s_type); |
1794 | } | | 1794 | } |
1795 | if (mem->s_name != unnamed) | | 1795 | if (mem->s_name != unnamed) |
1796 | n++; | | 1796 | n++; |
1797 | } | | 1797 | } |
1798 | | | 1798 | |
1799 | if (n == 0 && sp->size != 0) { | | 1799 | if (n == 0 && sp->size != 0) { |
1800 | /* %s has no named members */ | | 1800 | /* %s has no named members */ |
1801 | warning(65, t == STRUCT ? "structure" : "union"); | | 1801 | warning(65, t == STRUCT ? "structure" : "union"); |
1802 | } | | 1802 | } |
1803 | return tp; | | 1803 | return tp; |
1804 | } | | 1804 | } |
1805 | | | 1805 | |
1806 | type_t * | | 1806 | type_t * |
1807 | complete_tag_enum(type_t *tp, sym_t *fmem) | | 1807 | complete_tag_enum(type_t *tp, sym_t *fmem) |
1808 | { | | 1808 | { |
1809 | | | 1809 | |
1810 | setcomplete(tp, 1); | | 1810 | setcomplete(tp, 1); |
1811 | tp->t_enum->elem = fmem; | | 1811 | tp->t_enum->elem = fmem; |
1812 | return tp; | | 1812 | return tp; |
1813 | } | | 1813 | } |
1814 | | | 1814 | |
1815 | /* | | 1815 | /* |
1816 | * Processes the name of an enumerator in an enum declaration. | | 1816 | * Processes the name of an enumerator in an enum declaration. |
1817 | * | | 1817 | * |
1818 | * sym points to the enumerator | | 1818 | * sym points to the enumerator |
1819 | * val is the value of the enumerator | | 1819 | * val is the value of the enumerator |
1820 | * impl is 1 if the value of the enumerator was not explicitly specified. | | 1820 | * impl is 1 if the value of the enumerator was not explicitly specified. |
1821 | */ | | 1821 | */ |
1822 | sym_t * | | 1822 | sym_t * |
1823 | ename(sym_t *sym, int val, int impl) | | 1823 | ename(sym_t *sym, int val, int impl) |
1824 | { | | 1824 | { |
1825 | | | 1825 | |
1826 | if (sym->s_scl) { | | 1826 | if (sym->s_scl) { |
1827 | if (sym->s_blklev == blklev) { | | 1827 | if (sym->s_blklev == blklev) { |
1828 | /* no hflag, because this is illegal!!! */ | | 1828 | /* no hflag, because this is illegal!!! */ |
1829 | if (sym->s_arg) { | | 1829 | if (sym->s_arg) { |
1830 | /* enumeration constant hides parameter: %s */ | | 1830 | /* enumeration constant hides parameter: %s */ |
1831 | warning(57, sym->s_name); | | 1831 | warning(57, sym->s_name); |
1832 | } else { | | 1832 | } else { |
1833 | /* redeclaration of %s */ | | 1833 | /* redeclaration of %s */ |
1834 | error(27, sym->s_name); | | 1834 | error(27, sym->s_name); |
1835 | /* | | 1835 | /* |
1836 | * inside blocks it should not be too | | 1836 | * inside blocks it should not be too |
1837 | * complicated to find the position of the | | 1837 | * complicated to find the position of the |
1838 | * previous declaration | | 1838 | * previous declaration |
1839 | */ | | 1839 | */ |
1840 | if (blklev == 0) | | 1840 | if (blklev == 0) |
1841 | print_previous_declaration(-1, sym); | | 1841 | print_previous_declaration(-1, sym); |
1842 | } | | 1842 | } |
1843 | } else { | | 1843 | } else { |
1844 | if (hflag) | | 1844 | if (hflag) |
1845 | /* redefinition hides earlier one: %s */ | | 1845 | /* redefinition hides earlier one: %s */ |
1846 | warning(43, sym->s_name); | | 1846 | warning(43, sym->s_name); |
1847 | } | | 1847 | } |
1848 | sym = pushdown(sym); | | 1848 | sym = pushdown(sym); |
1849 | } | | 1849 | } |
1850 | sym->s_scl = ENUMCON; | | 1850 | sym->s_scl = ENUMCON; |
1851 | sym->s_type = dcs->d_tagtyp; | | 1851 | sym->s_type = dcs->d_tagtyp; |
1852 | sym->s_value.v_tspec = INT; | | 1852 | sym->s_value.v_tspec = INT; |
1853 | sym->s_value.v_quad = val; | | 1853 | sym->s_value.v_quad = val; |
1854 | if (impl && val - 1 == TARG_INT_MAX) { | | 1854 | if (impl && val - 1 == TARG_INT_MAX) { |
1855 | /* overflow in enumeration values: %s */ | | 1855 | /* overflow in enumeration values: %s */ |
1856 | warning(48, sym->s_name); | | 1856 | warning(48, sym->s_name); |
1857 | } | | 1857 | } |
1858 | enumval = val + 1; | | 1858 | enumval = val + 1; |
1859 | return sym; | | 1859 | return sym; |
1860 | } | | 1860 | } |
1861 | | | 1861 | |
1862 | /* | | 1862 | /* |
1863 | * Process a single external declarator. | | 1863 | * Process a single external declarator. |
1864 | */ | | 1864 | */ |
1865 | void | | 1865 | void |
1866 | decl1ext(sym_t *dsym, int initflg) | | 1866 | decl1ext(sym_t *dsym, int initflg) |
1867 | { | | 1867 | { |
1868 | int dowarn, rval, redec; | | 1868 | int dowarn, rval, redec; |
1869 | sym_t *rdsym; | | 1869 | sym_t *rdsym; |
1870 | | | 1870 | |
1871 | check_function_definition(dsym, 1); | | 1871 | check_function_definition(dsym, 1); |
1872 | | | 1872 | |
1873 | check_type(dsym); | | 1873 | check_type(dsym); |
1874 | | | 1874 | |
1875 | if (initflg && !(initerr = check_init(dsym))) | | 1875 | if (initflg && !(initerr = check_init(dsym))) |
1876 | dsym->s_def = DEF; | | 1876 | dsym->s_def = DEF; |
1877 | | | 1877 | |
1878 | /* | | 1878 | /* |
1879 | * Declarations of functions are marked as "tentative" in | | 1879 | * Declarations of functions are marked as "tentative" in |
1880 | * declarator_name(). This is wrong because there are no | | 1880 | * declarator_name(). This is wrong because there are no |
1881 | * tentative function definitions. | | 1881 | * tentative function definitions. |
1882 | */ | | 1882 | */ |
1883 | if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF) | | 1883 | if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF) |
1884 | dsym->s_def = DECL; | | 1884 | dsym->s_def = DECL; |
1885 | | | 1885 | |
1886 | if (dcs->d_inline) { | | 1886 | if (dcs->d_inline) { |
1887 | if (dsym->s_type->t_tspec == FUNC) { | | 1887 | if (dsym->s_type->t_tspec == FUNC) { |
1888 | dsym->s_inline = 1; | | 1888 | dsym->s_inline = 1; |
1889 | } else { | | 1889 | } else { |
1890 | /* variable declared inline: %s */ | | 1890 | /* variable declared inline: %s */ |
1891 | warning(268, dsym->s_name); | | 1891 | warning(268, dsym->s_name); |
1892 | } | | 1892 | } |
1893 | } | | 1893 | } |
1894 | | | 1894 | |
1895 | /* Write the declaration into the output file */ | | 1895 | /* Write the declaration into the output file */ |
1896 | if (plibflg && llibflg && | | 1896 | if (plibflg && llibflg && |
1897 | dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) { | | 1897 | dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) { |
1898 | /* | | 1898 | /* |
1899 | * With both LINTLIBRARY and PROTOLIB the prototyp is | | 1899 | * With both LINTLIBRARY and PROTOLIB the prototyp is |
1900 | * written as a function definition to the output file. | | 1900 | * written as a function definition to the output file. |
1901 | */ | | 1901 | */ |
1902 | rval = dsym->s_type->t_subt->t_tspec != VOID; | | 1902 | rval = dsym->s_type->t_subt->t_tspec != VOID; |
1903 | outfdef(dsym, &dsym->s_def_pos, rval, 0, NULL); | | 1903 | outfdef(dsym, &dsym->s_def_pos, rval, 0, NULL); |
1904 | } else { | | 1904 | } else { |
1905 | outsym(dsym, dsym->s_scl, dsym->s_def); | | 1905 | outsym(dsym, dsym->s_scl, dsym->s_def); |
1906 | } | | 1906 | } |
1907 | | | 1907 | |
1908 | if ((rdsym = dcs->d_rdcsym) != NULL) { | | 1908 | if ((rdsym = dcs->d_rdcsym) != NULL) { |
1909 | | | 1909 | |
1910 | /* | | 1910 | /* |
1911 | * If the old symbol stems from an old style function | | 1911 | * If the old symbol stems from an old style function |
1912 | * definition, we have remembered the params in rdsmy->s_args | | 1912 | * definition, we have remembered the params in rdsmy->s_args |
1913 | * and compare them with the params of the prototype. | | 1913 | * and compare them with the params of the prototype. |
1914 | */ | | 1914 | */ |
1915 | if (rdsym->s_osdef && dsym->s_type->t_proto) { | | 1915 | if (rdsym->s_osdef && dsym->s_type->t_proto) { |
1916 | redec = check_old_style_definition(rdsym, dsym); | | 1916 | redec = check_old_style_definition(rdsym, dsym); |
1917 | } else { | | 1917 | } else { |
1918 | redec = 0; | | 1918 | redec = 0; |
1919 | } | | 1919 | } |
1920 | | | 1920 | |
1921 | if (!redec && !check_redeclaration(dsym, (dowarn = 0, &dowarn))) { | | 1921 | if (!redec && !check_redeclaration(dsym, (dowarn = 0, &dowarn))) { |
1922 | | | 1922 | |
1923 | if (dowarn) { | | 1923 | if (dowarn) { |
1924 | /* redeclaration of %s */ | | 1924 | /* redeclaration of %s */ |
1925 | (*(sflag ? error : warning))(27, dsym->s_name); | | 1925 | (*(sflag ? error : warning))(27, dsym->s_name); |
1926 | print_previous_declaration(-1, rdsym); | | 1926 | print_previous_declaration(-1, rdsym); |
1927 | } | | 1927 | } |
1928 | | | 1928 | |
1929 | /* | | 1929 | /* |
1930 | * Take over the remembered params if the new symbol | | 1930 | * Take over the remembered params if the new symbol |
1931 | * is not a prototype. | | 1931 | * is not a prototype. |
1932 | */ | | 1932 | */ |
1933 | if (rdsym->s_osdef && !dsym->s_type->t_proto) { | | 1933 | if (rdsym->s_osdef && !dsym->s_type->t_proto) { |
1934 | dsym->s_osdef = rdsym->s_osdef; | | 1934 | dsym->s_osdef = rdsym->s_osdef; |
1935 | dsym->s_args = rdsym->s_args; | | 1935 | dsym->s_args = rdsym->s_args; |
1936 | dsym->s_def_pos = rdsym->s_def_pos; | | 1936 | dsym->s_def_pos = rdsym->s_def_pos; |
1937 | } | | 1937 | } |
1938 | | | 1938 | |
1939 | /* | | 1939 | /* |
1940 | * Remember the position of the declaration if the | | 1940 | * Remember the position of the declaration if the |
1941 | * old symbol was a prototype and the new is not. | | 1941 | * old symbol was a prototype and the new is not. |
1942 | * Also remember the position if the old symbol | | 1942 | * Also remember the position if the old symbol |
1943 | * was defined and the new is not. | | 1943 | * was defined and the new is not. |
1944 | */ | | 1944 | */ |
1945 | if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) { | | 1945 | if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) { |
1946 | dsym->s_def_pos = rdsym->s_def_pos; | | 1946 | dsym->s_def_pos = rdsym->s_def_pos; |
1947 | } else if (rdsym->s_def == DEF && dsym->s_def != DEF) { | | 1947 | } else if (rdsym->s_def == DEF && dsym->s_def != DEF) { |
1948 | dsym->s_def_pos = rdsym->s_def_pos; | | 1948 | dsym->s_def_pos = rdsym->s_def_pos; |
1949 | } | | 1949 | } |
1950 | | | 1950 | |
1951 | /* | | 1951 | /* |
1952 | * Copy usage information of the name into the new | | 1952 | * Copy usage information of the name into the new |
1953 | * symbol. | | 1953 | * symbol. |
1954 | */ | | 1954 | */ |
1955 | copy_usage_info(dsym, rdsym); | | 1955 | copy_usage_info(dsym, rdsym); |
1956 | | | 1956 | |
1957 | /* Once a name is defined, it remains defined. */ | | 1957 | /* Once a name is defined, it remains defined. */ |
1958 | if (rdsym->s_def == DEF) | | 1958 | if (rdsym->s_def == DEF) |
1959 | dsym->s_def = DEF; | | 1959 | dsym->s_def = DEF; |
1960 | | | 1960 | |
1961 | /* once a function is inline, it remains inline */ | | 1961 | /* once a function is inline, it remains inline */ |
1962 | if (rdsym->s_inline) | | 1962 | if (rdsym->s_inline) |
1963 | dsym->s_inline = 1; | | 1963 | dsym->s_inline = 1; |
1964 | | | 1964 | |
1965 | complete_type(dsym, rdsym); | | 1965 | complete_type(dsym, rdsym); |
1966 | | | 1966 | |
1967 | } | | 1967 | } |
1968 | | | 1968 | |
1969 | rmsym(rdsym); | | 1969 | rmsym(rdsym); |
1970 | } | | 1970 | } |
1971 | | | 1971 | |
1972 | if (dsym->s_scl == TYPEDEF) { | | 1972 | if (dsym->s_scl == TYPEDEF) { |
1973 | dsym->s_type = duptyp(dsym->s_type); | | 1973 | dsym->s_type = duptyp(dsym->s_type); |
1974 | dsym->s_type->t_typedef = 1; | | 1974 | dsym->s_type->t_typedef = 1; |
1975 | settdsym(dsym->s_type, dsym); | | 1975 | settdsym(dsym->s_type, dsym); |
1976 | } | | 1976 | } |
1977 | | | 1977 | |
1978 | } | | 1978 | } |
1979 | | | 1979 | |
1980 | /* | | 1980 | /* |
1981 | * Copies information about usage into a new symbol table entry of | | 1981 | * Copies information about usage into a new symbol table entry of |
1982 | * the same symbol. | | 1982 | * the same symbol. |
1983 | */ | | 1983 | */ |
1984 | void | | 1984 | void |
1985 | copy_usage_info(sym_t *sym, sym_t *rdsym) | | 1985 | copy_usage_info(sym_t *sym, sym_t *rdsym) |
1986 | { | | 1986 | { |
1987 | | | 1987 | |
1988 | sym->s_set_pos = rdsym->s_set_pos; | | 1988 | sym->s_set_pos = rdsym->s_set_pos; |
1989 | sym->s_use_pos = rdsym->s_use_pos; | | 1989 | sym->s_use_pos = rdsym->s_use_pos; |
1990 | sym->s_set = rdsym->s_set; | | 1990 | sym->s_set = rdsym->s_set; |
1991 | sym->s_used = rdsym->s_used; | | 1991 | sym->s_used = rdsym->s_used; |
1992 | } | | 1992 | } |
1993 | | | 1993 | |
1994 | /* | | 1994 | /* |
1995 | * Prints an error and returns 1 if a symbol is redeclared/redefined. | | 1995 | * Prints an error and returns 1 if a symbol is redeclared/redefined. |
1996 | * Otherwise returns 0 and, in some cases of minor problems, prints | | 1996 | * Otherwise returns 0 and, in some cases of minor problems, prints |
1997 | * a warning. | | 1997 | * a warning. |
1998 | */ | | 1998 | */ |
1999 | int | | 1999 | int |
2000 | check_redeclaration(sym_t *dsym, int *dowarn) | | 2000 | check_redeclaration(sym_t *dsym, int *dowarn) |
2001 | { | | 2001 | { |
2002 | sym_t *rsym; | | 2002 | sym_t *rsym; |
2003 | | | 2003 | |
2004 | if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) { | | 2004 | if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) { |
2005 | /* redeclaration of %s */ | | 2005 | /* redeclaration of %s */ |
2006 | error(27, dsym->s_name); | | 2006 | error(27, dsym->s_name); |
2007 | print_previous_declaration(-1, rsym); | | 2007 | print_previous_declaration(-1, rsym); |
2008 | return 1; | | 2008 | return 1; |
2009 | } | | 2009 | } |
2010 | if (rsym->s_scl == TYPEDEF) { | | 2010 | if (rsym->s_scl == TYPEDEF) { |
2011 | /* typedef redeclared: %s */ | | 2011 | /* typedef redeclared: %s */ |
2012 | error(89, dsym->s_name); | | 2012 | error(89, dsym->s_name); |
2013 | print_previous_declaration(-1, rsym); | | 2013 | print_previous_declaration(-1, rsym); |
2014 | return 1; | | 2014 | return 1; |
2015 | } | | 2015 | } |
2016 | if (dsym->s_scl == TYPEDEF) { | | 2016 | if (dsym->s_scl == TYPEDEF) { |
2017 | /* redeclaration of %s */ | | 2017 | /* redeclaration of %s */ |
2018 | error(27, dsym->s_name); | | 2018 | error(27, dsym->s_name); |
2019 | print_previous_declaration(-1, rsym); | | 2019 | print_previous_declaration(-1, rsym); |
2020 | return 1; | | 2020 | return 1; |
2021 | } | | 2021 | } |
2022 | if (rsym->s_def == DEF && dsym->s_def == DEF) { | | 2022 | if (rsym->s_def == DEF && dsym->s_def == DEF) { |
2023 | /* redefinition of %s */ | | 2023 | /* redefinition of %s */ |
2024 | error(28, dsym->s_name); | | 2024 | error(28, dsym->s_name); |
2025 | print_previous_declaration(-1, rsym); | | 2025 | print_previous_declaration(-1, rsym); |
2026 | return(1); | | 2026 | return(1); |
2027 | } | | 2027 | } |
2028 | if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, dowarn)) { | | 2028 | if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, dowarn)) { |
2029 | /* redeclaration of %s */ | | 2029 | /* redeclaration of %s */ |
2030 | error(27, dsym->s_name); | | 2030 | error(27, dsym->s_name); |
2031 | print_previous_declaration(-1, rsym); | | 2031 | print_previous_declaration(-1, rsym); |
2032 | return(1); | | 2032 | return(1); |
2033 | } | | 2033 | } |
2034 | if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN) | | 2034 | if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN) |
2035 | return(0); | | 2035 | return(0); |
2036 | if (rsym->s_scl == STATIC && dsym->s_scl == STATIC) | | 2036 | if (rsym->s_scl == STATIC && dsym->s_scl == STATIC) |
2037 | return(0); | | 2037 | return(0); |
2038 | if (rsym->s_scl == STATIC && dsym->s_def == DECL) | | 2038 | if (rsym->s_scl == STATIC && dsym->s_def == DECL) |
2039 | return(0); | | 2039 | return(0); |
2040 | if (rsym->s_scl == EXTERN && rsym->s_def == DEF) { | | 2040 | if (rsym->s_scl == EXTERN && rsym->s_def == DEF) { |
2041 | /* | | 2041 | /* |
2042 | * All cases except "int a = 1; static int a;" are caught | | 2042 | * All cases except "int a = 1; static int a;" are caught |
2043 | * above with or without a warning | | 2043 | * above with or without a warning |
2044 | */ | | 2044 | */ |
2045 | /* redeclaration of %s */ | | 2045 | /* redeclaration of %s */ |
2046 | error(27, dsym->s_name); | | 2046 | error(27, dsym->s_name); |
2047 | print_previous_declaration(-1, rsym); | | 2047 | print_previous_declaration(-1, rsym); |
2048 | return(1); | | 2048 | return(1); |
2049 | } | | 2049 | } |
2050 | if (rsym->s_scl == EXTERN) { | | 2050 | if (rsym->s_scl == EXTERN) { |
2051 | /* previously declared extern, becomes static: %s */ | | 2051 | /* previously declared extern, becomes static: %s */ |
2052 | warning(29, dsym->s_name); | | 2052 | warning(29, dsym->s_name); |
2053 | print_previous_declaration(-1, rsym); | | 2053 | print_previous_declaration(-1, rsym); |
2054 | return(0); | | 2054 | return(0); |
2055 | } | | 2055 | } |
2056 | /* | | 2056 | /* |
2057 | * Now it's one of: | | 2057 | * Now it's one of: |
2058 | * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;" | | 2058 | * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;" |
2059 | */ | | 2059 | */ |
2060 | /* redeclaration of %s; ANSI C requires "static" */ | | 2060 | /* redeclaration of %s; ANSI C requires "static" */ |
2061 | if (sflag) { | | 2061 | if (sflag) { |
2062 | /* redeclaration of %s; ANSI C requires static */ | | 2062 | /* redeclaration of %s; ANSI C requires static */ |
2063 | warning(30, dsym->s_name); | | 2063 | warning(30, dsym->s_name); |
2064 | print_previous_declaration(-1, rsym); | | 2064 | print_previous_declaration(-1, rsym); |
2065 | } | | 2065 | } |
2066 | dsym->s_scl = STATIC; | | 2066 | dsym->s_scl = STATIC; |
2067 | return 0; | | 2067 | return 0; |
2068 | } | | 2068 | } |
2069 | | | 2069 | |
2070 | static int | | 2070 | static int |
2071 | chkqual(type_t *tp1, type_t *tp2, int ignqual) | | 2071 | chkqual(type_t *tp1, type_t *tp2, int ignqual) |
2072 | { | | 2072 | { |
2073 | if (tp1->t_const != tp2->t_const && !ignqual && !tflag) | | 2073 | if (tp1->t_const != tp2->t_const && !ignqual && !tflag) |
2074 | return 0; | | 2074 | return 0; |
2075 | | | 2075 | |
2076 | if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag) | | 2076 | if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag) |
2077 | return 0; | | 2077 | return 0; |
2078 | | | 2078 | |
2079 | return 1; | | 2079 | return 1; |
2080 | } | | 2080 | } |
2081 | | | 2081 | |
2082 | int | | 2082 | int |
2083 | eqptrtype(type_t *tp1, type_t *tp2, int ignqual) | | 2083 | eqptrtype(type_t *tp1, type_t *tp2, int ignqual) |
2084 | { | | 2084 | { |
2085 | if (tp1->t_tspec != VOID && tp2->t_tspec != VOID) | | 2085 | if (tp1->t_tspec != VOID && tp2->t_tspec != VOID) |
2086 | return 0; | | 2086 | return 0; |
2087 | | | 2087 | |
2088 | if (!chkqual(tp1, tp2, ignqual)) | | 2088 | if (!chkqual(tp1, tp2, ignqual)) |
2089 | return 0; | | 2089 | return 0; |
2090 | | | 2090 | |
2091 | return 1; | | 2091 | return 1; |
2092 | } | | 2092 | } |
2093 | | | 2093 | |
2094 | | | 2094 | |
2095 | /* | | 2095 | /* |
2096 | * Checks if two types are compatible. Returns 0 if not, otherwise 1. | | 2096 | * Checks if two types are compatible. Returns 0 if not, otherwise 1. |
2097 | * | | 2097 | * |
2098 | * ignqual ignore qualifiers of type; used for function params | | 2098 | * ignqual ignore qualifiers of type; used for function params |
2099 | * promot promote left type; used for comparison of params of | | 2099 | * promot promote left type; used for comparison of params of |
2100 | * old style function definitions with params of prototypes. | | 2100 | * old style function definitions with params of prototypes. |
2101 | * *dowarn set to 1 if an old style function declaration is not | | 2101 | * *dowarn set to 1 if an old style function declaration is not |
2102 | * compatible with a prototype | | 2102 | * compatible with a prototype |
2103 | */ | | 2103 | */ |
2104 | int | | 2104 | int |
2105 | eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *dowarn) | | 2105 | eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *dowarn) |
2106 | { | | 2106 | { |
2107 | tspec_t t; | | 2107 | tspec_t t; |
2108 | | | 2108 | |
2109 | while (tp1 != NULL && tp2 != NULL) { | | 2109 | while (tp1 != NULL && tp2 != NULL) { |
2110 | | | 2110 | |
2111 | t = tp1->t_tspec; | | 2111 | t = tp1->t_tspec; |
2112 | if (promot) { | | 2112 | if (promot) { |
2113 | if (t == FLOAT) { | | 2113 | if (t == FLOAT) { |
2114 | t = DOUBLE; | | 2114 | t = DOUBLE; |
2115 | } else if (t == CHAR || t == SCHAR) { | | 2115 | } else if (t == CHAR || t == SCHAR) { |
2116 | t = INT; | | 2116 | t = INT; |
2117 | } else if (t == UCHAR) { | | 2117 | } else if (t == UCHAR) { |
2118 | t = tflag ? UINT : INT; | | 2118 | t = tflag ? UINT : INT; |
2119 | } else if (t == SHORT) { | | 2119 | } else if (t == SHORT) { |
2120 | t = INT; | | 2120 | t = INT; |