| @@ -1,3201 +1,3183 @@ | | | @@ -1,3201 +1,3183 @@ |
1 | /* $NetBSD: decl.c,v 1.363 2023/07/28 21:50:03 rillig Exp $ */ | | 1 | /* $NetBSD: decl.c,v 1.364 2023/07/29 07:03:19 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) | | 40 | #if defined(__RCSID) |
41 | __RCSID("$NetBSD: decl.c,v 1.363 2023/07/28 21:50:03 rillig Exp $"); | | 41 | __RCSID("$NetBSD: decl.c,v 1.364 2023/07/29 07:03:19 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 arithmetic types and void */ | | 53 | /* shared type structures for arithmetic types and void */ |
54 | static type_t typetab[NTSPEC]; | | 54 | static type_t typetab[NTSPEC]; |
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 | * Points to the innermost element of a stack that contains information about | | 60 | * Points to the innermost element of a stack that contains information about |
61 | * nested declarations, such as struct declarations, function prototypes, | | 61 | * nested declarations, such as struct declarations, function prototypes, |
62 | * local variables. | | 62 | * local variables. |
63 | */ | | 63 | */ |
64 | decl_level *dcs; | | 64 | decl_level *dcs; |
65 | | | 65 | |
66 | static type_t *typedef_error(type_t *, tspec_t); | | | |
67 | static void set_first_typedef(type_t *, sym_t *); | | | |
68 | static void dcs_align(unsigned int, unsigned int); | | | |
69 | static sym_t *new_tag(sym_t *, scl_t, bool, bool); | | | |
70 | static bool prototypes_compatible(const type_t *, const type_t *, bool *); | | | |
71 | static bool matches_no_arg_function(const type_t *, bool *); | | | |
72 | static bool check_old_style_definition(sym_t *, sym_t *); | | | |
73 | static bool check_prototype_declaration(sym_t *, sym_t *); | | | |
74 | static void check_prototype_parameters(sym_t *); | | | |
75 | static void old_style_function(sym_t *, sym_t *); | | | |
76 | static void declare_external_in_block(sym_t *); | | | |
77 | static bool check_init(sym_t *); | | | |
78 | static void check_argument_usage(bool, sym_t *); | | | |
79 | static void check_variable_usage(bool, sym_t *); | | | |
80 | static void check_label_usage(sym_t *); | | | |
81 | static void check_tag_usage(sym_t *); | | | |
82 | static void check_global_variable(const sym_t *); | | | |
83 | static void check_global_variable_size(const sym_t *); | | | |
84 | | | 66 | |
85 | /* | | 67 | /* |
86 | * initializes all global vars used in declarations | | 68 | * initializes all global vars used in declarations |
87 | */ | | 69 | */ |
88 | void | | 70 | void |
89 | initdecl(void) | | 71 | initdecl(void) |
90 | { | | 72 | { |
91 | | | 73 | |
92 | /* declaration stack */ | | 74 | /* declaration stack */ |
93 | dcs = xcalloc(1, sizeof(*dcs)); | | 75 | dcs = xcalloc(1, sizeof(*dcs)); |
94 | dcs->d_kind = DLK_EXTERN; | | 76 | dcs->d_kind = DLK_EXTERN; |
95 | dcs->d_last_dlsym = &dcs->d_first_dlsym; | | 77 | dcs->d_last_dlsym = &dcs->d_first_dlsym; |
96 | | | 78 | |
97 | if (!pflag) { | | 79 | if (!pflag) { |
98 | for (size_t i = 0; i < NTSPEC; i++) { | | 80 | for (size_t i = 0; i < NTSPEC; i++) { |
99 | if (ttab[i].tt_rank_kind != RK_NONE) | | 81 | if (ttab[i].tt_rank_kind != RK_NONE) |
100 | ttab[i].tt_rank_value = | | 82 | ttab[i].tt_rank_value = |
101 | ttab[i].tt_size_in_bits; | | 83 | ttab[i].tt_size_in_bits; |
102 | } | | 84 | } |
103 | ttab[BOOL].tt_rank_value = 1; | | 85 | ttab[BOOL].tt_rank_value = 1; |
104 | } | | 86 | } |
105 | | | 87 | |
106 | if (Tflag) { | | 88 | if (Tflag) { |
107 | ttab[BOOL].tt_is_integer = false; | | 89 | ttab[BOOL].tt_is_integer = false; |
108 | ttab[BOOL].tt_is_uinteger = false; | | 90 | ttab[BOOL].tt_is_uinteger = false; |
109 | ttab[BOOL].tt_is_arithmetic = false; | | 91 | ttab[BOOL].tt_is_arithmetic = false; |
110 | } | | 92 | } |
111 | | | 93 | |
112 | /* struct, union, enum, ptr, array and func are not shared. */ | | 94 | /* struct, union, enum, ptr, array and func are not shared. */ |
113 | for (int i = (int)SIGNED; i < (int)STRUCT; i++) | | 95 | for (int i = (int)SIGNED; i < (int)STRUCT; i++) |
114 | typetab[i].t_tspec = (tspec_t)i; | | 96 | typetab[i].t_tspec = (tspec_t)i; |
115 | } | | 97 | } |
116 | | | 98 | |
117 | /* | | 99 | /* |
118 | * Returns a shared type structure for arithmetic types and void. | | 100 | * Returns a shared type structure for arithmetic types and void. |
119 | * | | 101 | * |
120 | * It's important to duplicate this structure using block_dup_type or | | 102 | * It's important to duplicate this structure using block_dup_type or |
121 | * expr_dup_type if it is to be modified (adding qualifiers or anything | | 103 | * expr_dup_type if it is to be modified (adding qualifiers or anything |
122 | * else). | | 104 | * else). |
123 | */ | | 105 | */ |
124 | type_t * | | 106 | type_t * |
125 | gettyp(tspec_t t) | | 107 | gettyp(tspec_t t) |
126 | { | | 108 | { |
127 | | | 109 | |
128 | lint_assert((int)t < (int)STRUCT); | | 110 | lint_assert((int)t < (int)STRUCT); |
129 | /* TODO: make the return type 'const' */ | | 111 | /* TODO: make the return type 'const' */ |
130 | return &typetab[t]; | | 112 | return &typetab[t]; |
131 | } | | 113 | } |
132 | | | 114 | |
133 | type_t * | | 115 | type_t * |
134 | block_dup_type(const type_t *tp) | | 116 | block_dup_type(const type_t *tp) |
135 | { | | 117 | { |
136 | | | 118 | |
137 | type_t *ntp = block_zero_alloc(sizeof(*ntp), "type"); | | 119 | type_t *ntp = block_zero_alloc(sizeof(*ntp), "type"); |
138 | *ntp = *tp; | | 120 | *ntp = *tp; |
139 | return ntp; | | 121 | return ntp; |
140 | } | | 122 | } |
141 | | | 123 | |
142 | /* Duplicate a type, free the allocated memory after the expression. */ | | 124 | /* Duplicate a type, free the allocated memory after the expression. */ |
143 | type_t * | | 125 | type_t * |
144 | expr_dup_type(const type_t *tp) | | 126 | expr_dup_type(const type_t *tp) |
145 | { | | 127 | { |
146 | | | 128 | |
147 | type_t *ntp = expr_zero_alloc(sizeof(*ntp), "type"); | | 129 | type_t *ntp = expr_zero_alloc(sizeof(*ntp), "type"); |
148 | *ntp = *tp; | | 130 | *ntp = *tp; |
149 | return ntp; | | 131 | return ntp; |
150 | } | | 132 | } |
151 | | | 133 | |
152 | /* | | 134 | /* |
153 | * Return the unqualified version of the type. The returned type is freed at | | 135 | * Return the unqualified version of the type. The returned type is freed at |
154 | * the end of the current expression. | | 136 | * the end of the current expression. |
155 | * | | 137 | * |
156 | * See C99 6.2.5p25. | | 138 | * See C99 6.2.5p25. |
157 | */ | | 139 | */ |
158 | type_t * | | 140 | type_t * |
159 | expr_unqualified_type(const type_t *tp) | | 141 | expr_unqualified_type(const type_t *tp) |
160 | { | | 142 | { |
161 | | | 143 | |
162 | type_t *ntp = expr_zero_alloc(sizeof(*ntp), "type"); | | 144 | type_t *ntp = expr_zero_alloc(sizeof(*ntp), "type"); |
163 | *ntp = *tp; | | 145 | *ntp = *tp; |
164 | ntp->t_const = false; | | 146 | ntp->t_const = false; |
165 | ntp->t_volatile = false; | | 147 | ntp->t_volatile = false; |
166 | | | 148 | |
167 | /* | | 149 | /* |
168 | * In case of a struct or union type, the members should lose their | | 150 | * In case of a struct or union type, the members should lose their |
169 | * qualifiers as well, but that would require a deep copy of the | | 151 | * qualifiers as well, but that would require a deep copy of the |
170 | * struct or union type. This in turn would defeat the type | | 152 | * struct or union type. This in turn would defeat the type |
171 | * comparison in types_compatible, which simply tests whether | | 153 | * comparison in types_compatible, which simply tests whether |
172 | * tp1->t_sou == tp2->t_sou. | | 154 | * tp1->t_sou == tp2->t_sou. |
173 | */ | | 155 | */ |
174 | | | 156 | |
175 | return ntp; | | 157 | return ntp; |
176 | } | | 158 | } |
177 | | | 159 | |
178 | /* | | 160 | /* |
179 | * Returns whether the argument is void or an incomplete array, struct, union | | 161 | * Returns whether the argument is void or an incomplete array, struct, union |
180 | * or enum type. | | 162 | * or enum type. |
181 | */ | | 163 | */ |
182 | bool | | 164 | bool |
183 | is_incomplete(const type_t *tp) | | 165 | is_incomplete(const type_t *tp) |
184 | { | | 166 | { |
185 | tspec_t t = tp->t_tspec; | | 167 | tspec_t t = tp->t_tspec; |
186 | | | 168 | |
187 | if (t == VOID) | | 169 | if (t == VOID) |
188 | return true; | | 170 | return true; |
189 | if (t == ARRAY) | | 171 | if (t == ARRAY) |
190 | return tp->t_incomplete_array; | | 172 | return tp->t_incomplete_array; |
191 | if (is_struct_or_union(t)) | | 173 | if (is_struct_or_union(t)) |
192 | return tp->t_sou->sou_incomplete; | | 174 | return tp->t_sou->sou_incomplete; |
193 | if (t == ENUM) | | 175 | if (t == ENUM) |
194 | return tp->t_enum->en_incomplete; | | 176 | return tp->t_enum->en_incomplete; |
195 | return false; | | 177 | return false; |
196 | } | | 178 | } |
197 | | | 179 | |
198 | void | | 180 | void |
199 | dcs_add_function_specifier(function_specifier fs) | | 181 | dcs_add_function_specifier(function_specifier fs) |
200 | { | | 182 | { |
201 | debug_step("%s: %s", __func__, function_specifier_name(fs)); | | 183 | debug_step("%s: %s", __func__, function_specifier_name(fs)); |
202 | if (fs == FS_INLINE) { | | 184 | if (fs == FS_INLINE) { |
203 | if (dcs->d_inline) | | 185 | if (dcs->d_inline) |
204 | /* duplicate '%s' */ | | 186 | /* duplicate '%s' */ |
205 | warning(10, "inline"); | | 187 | warning(10, "inline"); |
206 | dcs->d_inline = true; | | 188 | dcs->d_inline = true; |
207 | } | | 189 | } |
208 | } | | 190 | } |
209 | | | 191 | |
210 | /* | | 192 | /* |
211 | * Remember the storage class of the current declaration and detect multiple | | 193 | * Remember the storage class of the current declaration and detect multiple |
212 | * storage classes. | | 194 | * storage classes. |
213 | */ | | 195 | */ |
214 | void | | 196 | void |
215 | dcs_add_storage_class(scl_t sc) | | 197 | dcs_add_storage_class(scl_t sc) |
216 | { | | 198 | { |
217 | | | 199 | |
218 | if (dcs->d_type != NULL || dcs->d_abstract_type != NO_TSPEC || | | 200 | if (dcs->d_type != NULL || dcs->d_abstract_type != NO_TSPEC || |
219 | dcs->d_sign_mod != NO_TSPEC || dcs->d_rank_mod != NO_TSPEC) { | | 201 | dcs->d_sign_mod != NO_TSPEC || dcs->d_rank_mod != NO_TSPEC) { |
220 | /* storage class after type is obsolescent */ | | 202 | /* storage class after type is obsolescent */ |
221 | warning(83); | | 203 | warning(83); |
222 | } | | 204 | } |
223 | | | 205 | |
224 | if (dcs->d_scl == NOSCL) | | 206 | if (dcs->d_scl == NOSCL) |
225 | dcs->d_scl = sc; | | 207 | dcs->d_scl = sc; |
226 | else if ((dcs->d_scl == EXTERN && sc == THREAD_LOCAL) | | 208 | else if ((dcs->d_scl == EXTERN && sc == THREAD_LOCAL) |
227 | || (dcs->d_scl == THREAD_LOCAL && sc == EXTERN)) | | 209 | || (dcs->d_scl == THREAD_LOCAL && sc == EXTERN)) |
228 | dcs->d_scl = EXTERN; /* ignore thread_local */ | | 210 | dcs->d_scl = EXTERN; /* ignore thread_local */ |
229 | else if ((dcs->d_scl == STATIC && sc == THREAD_LOCAL) | | 211 | else if ((dcs->d_scl == STATIC && sc == THREAD_LOCAL) |
230 | || (dcs->d_scl == THREAD_LOCAL && sc == STATIC)) | | 212 | || (dcs->d_scl == THREAD_LOCAL && sc == STATIC)) |
231 | dcs->d_scl = STATIC; /* ignore thread_local */ | | 213 | dcs->d_scl = STATIC; /* ignore thread_local */ |
232 | else | | 214 | else |
233 | dcs->d_multiple_storage_classes = true; | | 215 | dcs->d_multiple_storage_classes = true; |
234 | } | | 216 | } |
235 | | | 217 | |
| | | 218 | /* Merge the signedness into the abstract type. */ |
| | | 219 | static tspec_t |
| | | 220 | merge_signedness(tspec_t t, tspec_t s) |
| | | 221 | { |
| | | 222 | |
| | | 223 | if (s == SIGNED) |
| | | 224 | return t == CHAR ? SCHAR : t; |
| | | 225 | if (s != UNSIGN) |
| | | 226 | return t; |
| | | 227 | return t == CHAR ? UCHAR |
| | | 228 | : t == SHORT ? USHORT |
| | | 229 | : t == INT ? UINT |
| | | 230 | : t == LONG ? ULONG |
| | | 231 | : t == LLONG ? ULLONG |
| | | 232 | : t; |
| | | 233 | } |
| | | 234 | |
| | | 235 | /* |
| | | 236 | * Called if a list of declaration specifiers contains a typedef name |
| | | 237 | * and other specifiers (except struct, union, enum, typedef name). |
| | | 238 | */ |
| | | 239 | static type_t * |
| | | 240 | typedef_error(type_t *td, tspec_t t) |
| | | 241 | { |
| | | 242 | |
| | | 243 | tspec_t t2 = td->t_tspec; |
| | | 244 | |
| | | 245 | if ((t == SIGNED || t == UNSIGN) && |
| | | 246 | (t2 == CHAR || t2 == SHORT || t2 == INT || |
| | | 247 | t2 == LONG || t2 == LLONG)) { |
| | | 248 | if (allow_c90) |
| | | 249 | /* modifying typedef with '%s'; only qualifiers... */ |
| | | 250 | warning(5, tspec_name(t)); |
| | | 251 | td = block_dup_type(gettyp(merge_signedness(t2, t))); |
| | | 252 | td->t_typedef = true; |
| | | 253 | return td; |
| | | 254 | } |
| | | 255 | |
| | | 256 | if (t == SHORT && (t2 == INT || t2 == UINT)) { |
| | | 257 | /* modifying typedef with '%s'; only qualifiers allowed */ |
| | | 258 | warning(5, "short"); |
| | | 259 | td = block_dup_type(gettyp(t2 == INT ? SHORT : USHORT)); |
| | | 260 | td->t_typedef = true; |
| | | 261 | return td; |
| | | 262 | } |
| | | 263 | |
| | | 264 | if (t != LONG) |
| | | 265 | goto invalid; |
| | | 266 | |
| | | 267 | if (t2 == INT) |
| | | 268 | td = gettyp(LONG); |
| | | 269 | else if (t2 == UINT) |
| | | 270 | td = gettyp(ULONG); |
| | | 271 | else if (t2 == LONG) |
| | | 272 | td = gettyp(LLONG); |
| | | 273 | else if (t2 == ULONG) |
| | | 274 | td = gettyp(ULLONG); |
| | | 275 | else if (t2 == FLOAT) |
| | | 276 | td = gettyp(DOUBLE); |
| | | 277 | else if (t2 == DOUBLE) |
| | | 278 | td = gettyp(LDOUBLE); |
| | | 279 | else if (t2 == DCOMPLEX) |
| | | 280 | td = gettyp(LCOMPLEX); |
| | | 281 | else |
| | | 282 | goto invalid; |
| | | 283 | |
| | | 284 | /* modifying typedef with '%s'; only qualifiers allowed */ |
| | | 285 | warning(5, "long"); |
| | | 286 | td = block_dup_type(td); |
| | | 287 | td->t_typedef = true; |
| | | 288 | return td; |
| | | 289 | |
| | | 290 | invalid: |
| | | 291 | /* Anything else is not accepted. */ |
| | | 292 | dcs->d_invalid_type_combination = true; |
| | | 293 | return td; |
| | | 294 | } |
| | | 295 | |
236 | /* | | 296 | /* |
237 | * Remember the type, modifier or typedef name returned by the parser in the | | 297 | * Remember the type, modifier or typedef name returned by the parser in the |
238 | * top element of the declaration stack. This information is used in | | 298 | * top element of the declaration stack. This information is used in |
239 | * dcs_end_type to build the type used for all declarators in this declaration. | | 299 | * dcs_end_type to build the type used for all declarators in this declaration. |
240 | * | | 300 | * |
241 | * If tp->t_typedef is true, the type comes from a previously defined typename. | | 301 | * If tp->t_typedef is true, the type comes from a previously defined typename. |
242 | * Otherwise, it comes from a type specifier (int, long, ...) or a | | 302 | * Otherwise, it comes from a type specifier (int, long, ...) or a |
243 | * struct/union/enum tag. | | 303 | * struct/union/enum tag. |
244 | */ | | 304 | */ |
245 | void | | 305 | void |
246 | dcs_add_type(type_t *tp) | | 306 | dcs_add_type(type_t *tp) |
247 | { | | 307 | { |
248 | | | 308 | |
249 | debug_step("%s: %s", __func__, type_name(tp)); | | 309 | debug_step("%s: %s", __func__, type_name(tp)); |
250 | if (tp->t_typedef) { | | 310 | if (tp->t_typedef) { |
251 | /* | | 311 | /* |
252 | * something like "typedef int a; int a b;" | | 312 | * something like "typedef int a; int a b;" |
253 | * This should not happen with current grammar. | | 313 | * This should not happen with current grammar. |
254 | */ | | 314 | */ |
255 | lint_assert(dcs->d_type == NULL); | | 315 | lint_assert(dcs->d_type == NULL); |
256 | lint_assert(dcs->d_abstract_type == NO_TSPEC); | | 316 | lint_assert(dcs->d_abstract_type == NO_TSPEC); |
257 | lint_assert(dcs->d_sign_mod == NO_TSPEC); | | 317 | lint_assert(dcs->d_sign_mod == NO_TSPEC); |
258 | lint_assert(dcs->d_rank_mod == NO_TSPEC); | | 318 | lint_assert(dcs->d_rank_mod == NO_TSPEC); |
259 | | | 319 | |
260 | dcs->d_type = tp; | | 320 | dcs->d_type = tp; |
261 | return; | | 321 | return; |
262 | } | | 322 | } |
263 | | | 323 | |
264 | tspec_t t = tp->t_tspec; | | 324 | tspec_t t = tp->t_tspec; |
265 | if (is_struct_or_union(t) || t == ENUM) { | | 325 | if (is_struct_or_union(t) || t == ENUM) { |
266 | /* | | 326 | /* |
267 | * something like "int struct a ..." | | 327 | * something like "int struct a ..." |
268 | * struct/union/enum with anything else is not allowed | | 328 | * struct/union/enum with anything else is not allowed |
269 | */ | | 329 | */ |
270 | if (dcs->d_type != NULL || dcs->d_abstract_type != NO_TSPEC || | | 330 | if (dcs->d_type != NULL || dcs->d_abstract_type != NO_TSPEC || |
271 | dcs->d_rank_mod != NO_TSPEC || dcs->d_sign_mod != NO_TSPEC) { | | 331 | dcs->d_rank_mod != NO_TSPEC || dcs->d_sign_mod != NO_TSPEC) { |
272 | dcs->d_invalid_type_combination = true; | | 332 | dcs->d_invalid_type_combination = true; |
273 | dcs->d_abstract_type = NO_TSPEC; | | 333 | dcs->d_abstract_type = NO_TSPEC; |
274 | dcs->d_sign_mod = NO_TSPEC; | | 334 | dcs->d_sign_mod = NO_TSPEC; |
275 | dcs->d_rank_mod = NO_TSPEC; | | 335 | dcs->d_rank_mod = NO_TSPEC; |
276 | } | | 336 | } |
277 | dcs->d_type = tp; | | 337 | dcs->d_type = tp; |
278 | return; | | 338 | return; |
279 | } | | 339 | } |
280 | | | 340 | |
281 | if (dcs->d_type != NULL && !dcs->d_type->t_typedef) { | | 341 | if (dcs->d_type != NULL && !dcs->d_type->t_typedef) { |
282 | /* | | 342 | /* |
283 | * something like "struct a int" | | 343 | * something like "struct a int" |
284 | * struct/union/enum with anything else is not allowed | | 344 | * struct/union/enum with anything else is not allowed |
285 | */ | | 345 | */ |
286 | dcs->d_invalid_type_combination = true; | | 346 | dcs->d_invalid_type_combination = true; |
287 | return; | | 347 | return; |
288 | } | | 348 | } |
289 | | | 349 | |
290 | if (t == COMPLEX) { | | 350 | if (t == COMPLEX) { |
291 | if (dcs->d_complex_mod == FLOAT) | | 351 | if (dcs->d_complex_mod == FLOAT) |
292 | t = FCOMPLEX; | | 352 | t = FCOMPLEX; |
293 | else if (dcs->d_complex_mod == DOUBLE) | | 353 | else if (dcs->d_complex_mod == DOUBLE) |
294 | t = DCOMPLEX; | | 354 | t = DCOMPLEX; |
295 | else { | | 355 | else { |
296 | /* invalid type for _Complex */ | | 356 | /* invalid type for _Complex */ |
297 | error(308); | | 357 | error(308); |
298 | t = DCOMPLEX; /* just as a fallback */ | | 358 | t = DCOMPLEX; /* just as a fallback */ |
299 | } | | 359 | } |
300 | dcs->d_complex_mod = NO_TSPEC; | | 360 | dcs->d_complex_mod = NO_TSPEC; |
301 | } | | 361 | } |
302 | | | 362 | |
303 | if (t == LONG && dcs->d_rank_mod == LONG) { | | 363 | if (t == LONG && dcs->d_rank_mod == LONG) { |
304 | /* "long long" or "long ... long" */ | | 364 | /* "long long" or "long ... long" */ |
305 | t = LLONG; | | 365 | t = LLONG; |
306 | dcs->d_rank_mod = NO_TSPEC; | | 366 | dcs->d_rank_mod = NO_TSPEC; |
307 | if (!suppress_longlong) | | 367 | if (!suppress_longlong) |
308 | /* %s does not support 'long long' */ | | 368 | /* %s does not support 'long long' */ |
309 | c99ism(265, allow_c90 ? "C90" : "traditional C"); | | 369 | c99ism(265, allow_c90 ? "C90" : "traditional C"); |
310 | } | | 370 | } |
311 | | | 371 | |
312 | if (dcs->d_type != NULL && dcs->d_type->t_typedef) { | | 372 | if (dcs->d_type != NULL && dcs->d_type->t_typedef) { |
313 | /* something like "typedef int a; a long ..." */ | | 373 | /* something like "typedef int a; a long ..." */ |
314 | dcs->d_type = typedef_error(dcs->d_type, t); | | 374 | dcs->d_type = typedef_error(dcs->d_type, t); |
315 | return; | | 375 | return; |
316 | } | | 376 | } |
317 | | | 377 | |
318 | /* now it can be only a combination of arithmetic types and void */ | | 378 | /* now it can be only a combination of arithmetic types and void */ |
319 | if (t == SIGNED || t == UNSIGN) { | | 379 | if (t == SIGNED || t == UNSIGN) { |
320 | if (dcs->d_sign_mod != NO_TSPEC) | | 380 | if (dcs->d_sign_mod != NO_TSPEC) |
321 | dcs->d_invalid_type_combination = true; | | 381 | dcs->d_invalid_type_combination = true; |
322 | dcs->d_sign_mod = t; | | 382 | dcs->d_sign_mod = t; |
323 | } else if (t == SHORT || t == LONG || t == LLONG) { | | 383 | } else if (t == SHORT || t == LONG || t == LLONG) { |
324 | if (dcs->d_rank_mod != NO_TSPEC) | | 384 | if (dcs->d_rank_mod != NO_TSPEC) |
325 | dcs->d_invalid_type_combination = true; | | 385 | dcs->d_invalid_type_combination = true; |
326 | dcs->d_rank_mod = t; | | 386 | dcs->d_rank_mod = t; |
327 | } else if (t == FLOAT || t == DOUBLE) { | | 387 | } else if (t == FLOAT || t == DOUBLE) { |
328 | if (dcs->d_rank_mod == NO_TSPEC || dcs->d_rank_mod == LONG) { | | 388 | if (dcs->d_rank_mod == NO_TSPEC || dcs->d_rank_mod == LONG) { |
329 | if (dcs->d_complex_mod != NO_TSPEC | | 389 | if (dcs->d_complex_mod != NO_TSPEC |
330 | || (t == FLOAT && dcs->d_rank_mod == LONG)) | | 390 | || (t == FLOAT && dcs->d_rank_mod == LONG)) |
331 | dcs->d_invalid_type_combination = true; | | 391 | dcs->d_invalid_type_combination = true; |
332 | dcs->d_complex_mod = t; | | 392 | dcs->d_complex_mod = t; |
333 | } else { | | 393 | } else { |
334 | if (dcs->d_abstract_type != NO_TSPEC) | | 394 | if (dcs->d_abstract_type != NO_TSPEC) |
335 | dcs->d_invalid_type_combination = true; | | 395 | dcs->d_invalid_type_combination = true; |
336 | dcs->d_abstract_type = t; | | 396 | dcs->d_abstract_type = t; |
337 | } | | 397 | } |
338 | } else if (t == PTR) { | | 398 | } else if (t == PTR) { |
339 | dcs->d_type = tp; | | 399 | dcs->d_type = tp; |
340 | } else { | | 400 | } else { |
341 | if (dcs->d_abstract_type != NO_TSPEC) | | 401 | if (dcs->d_abstract_type != NO_TSPEC) |
342 | dcs->d_invalid_type_combination = true; | | 402 | dcs->d_invalid_type_combination = true; |
343 | dcs->d_abstract_type = t; | | 403 | dcs->d_abstract_type = t; |
344 | } | | 404 | } |
345 | } | | 405 | } |
346 | | | 406 | |
347 | /* Merge the signedness into the abstract type. */ | | | |
348 | static tspec_t | | | |
349 | merge_signedness(tspec_t t, tspec_t s) | | | |
350 | { | | | |
351 | | | | |
352 | if (s == SIGNED) | | | |
353 | return t == CHAR ? SCHAR : t; | | | |
354 | if (s != UNSIGN) | | | |
355 | return t; | | | |
356 | return t == CHAR ? UCHAR | | | |
357 | : t == SHORT ? USHORT | | | |
358 | : t == INT ? UINT | | | |
359 | : t == LONG ? ULONG | | | |
360 | : t == LLONG ? ULLONG | | | |
361 | : t; | | | |
362 | } | | | |
363 | | | | |
364 | /* | | | |
365 | * Called if a list of declaration specifiers contains a typedef name | | | |
366 | * and other specifiers (except struct, union, enum, typedef name). | | | |
367 | */ | | | |
368 | static type_t * | | | |
369 | typedef_error(type_t *td, tspec_t t) | | | |
370 | { | | | |
371 | | | | |
372 | tspec_t t2 = td->t_tspec; | | | |
373 | | | | |
374 | if ((t == SIGNED || t == UNSIGN) && | | | |
375 | (t2 == CHAR || t2 == SHORT || t2 == INT || | | | |
376 | t2 == LONG || t2 == LLONG)) { | | | |
377 | if (allow_c90) | | | |
378 | /* modifying typedef with '%s'; only qualifiers... */ | | | |
379 | warning(5, tspec_name(t)); | | | |
380 | td = block_dup_type(gettyp(merge_signedness(t2, t))); | | | |
381 | td->t_typedef = true; | | | |
382 | return td; | | | |
383 | } | | | |
384 | | | | |
385 | if (t == SHORT && (t2 == INT || t2 == UINT)) { | | | |
386 | /* modifying typedef with '%s'; only qualifiers allowed */ | | | |
387 | warning(5, "short"); | | | |
388 | td = block_dup_type(gettyp(t2 == INT ? SHORT : USHORT)); | | | |
389 | td->t_typedef = true; | | | |
390 | return td; | | | |
391 | } | | | |
392 | | | | |
393 | if (t != LONG) | | | |
394 | goto invalid; | | | |
395 | | | | |
396 | if (t2 == INT) | | | |
397 | td = gettyp(LONG); | | | |
398 | else if (t2 == UINT) | | | |
399 | td = gettyp(ULONG); | | | |
400 | else if (t2 == LONG) | | | |
401 | td = gettyp(LLONG); | | | |
402 | else if (t2 == ULONG) | | | |
403 | td = gettyp(ULLONG); | | | |
404 | else if (t2 == FLOAT) | | | |
405 | td = gettyp(DOUBLE); | | | |
406 | else if (t2 == DOUBLE) | | | |
407 | td = gettyp(LDOUBLE); | | | |
408 | else if (t2 == DCOMPLEX) | | | |
409 | td = gettyp(LCOMPLEX); | | | |
410 | else | | | |
411 | goto invalid; | | | |
412 | | | | |
413 | /* modifying typedef with '%s'; only qualifiers allowed */ | | | |
414 | warning(5, "long"); | | | |
415 | td = block_dup_type(td); | | | |
416 | td->t_typedef = true; | | | |
417 | return td; | | | |
418 | | | | |
419 | invalid: | | | |
420 | /* Anything else is not accepted. */ | | | |
421 | dcs->d_invalid_type_combination = true; | | | |
422 | return td; | | | |
423 | } | | | |
424 | | | | |
425 | static void | | 407 | static void |
426 | set_first_typedef(type_t *tp, sym_t *sym) | | 408 | set_first_typedef(type_t *tp, sym_t *sym) |
427 | { | | 409 | { |
428 | | | 410 | |
429 | tspec_t t = tp->t_tspec; | | 411 | tspec_t t = tp->t_tspec; |
430 | if (is_struct_or_union(t) && tp->t_sou->sou_first_typedef == NULL) | | 412 | if (is_struct_or_union(t) && tp->t_sou->sou_first_typedef == NULL) |
431 | tp->t_sou->sou_first_typedef = sym; | | 413 | tp->t_sou->sou_first_typedef = sym; |
432 | if (t == ENUM && tp->t_enum->en_first_typedef == NULL) | | 414 | if (t == ENUM && tp->t_enum->en_first_typedef == NULL) |
433 | tp->t_enum->en_first_typedef = sym; | | 415 | tp->t_enum->en_first_typedef = sym; |
434 | } | | 416 | } |
435 | | | 417 | |
436 | static unsigned int | | 418 | static unsigned int |
437 | bit_fields_width(const sym_t **mem, bool *named) | | 419 | bit_fields_width(const sym_t **mem, bool *named) |
438 | { | | 420 | { |
439 | unsigned int width = 0; | | 421 | unsigned int width = 0; |
440 | unsigned int align = 0; | | 422 | unsigned int align = 0; |
441 | while (*mem != NULL && (*mem)->s_type->t_bitfield) { | | 423 | while (*mem != NULL && (*mem)->s_type->t_bitfield) { |
442 | if ((*mem)->s_name != unnamed) | | 424 | if ((*mem)->s_name != unnamed) |
443 | *named = true; | | 425 | *named = true; |
444 | width += (*mem)->s_type->t_bit_field_width; | | 426 | width += (*mem)->s_type->t_bit_field_width; |
445 | unsigned int mem_align = alignment_in_bits((*mem)->s_type); | | 427 | unsigned int mem_align = alignment_in_bits((*mem)->s_type); |
446 | if (mem_align > align) | | 428 | if (mem_align > align) |
447 | align = mem_align; | | 429 | align = mem_align; |
448 | *mem = (*mem)->s_next; | | 430 | *mem = (*mem)->s_next; |
449 | } | | 431 | } |
450 | return (width + align - 1) & -align; | | 432 | return (width + align - 1) & -align; |
451 | } | | 433 | } |
452 | | | 434 | |
453 | static void | | 435 | static void |
454 | pack_struct_or_union(type_t *tp) | | 436 | pack_struct_or_union(type_t *tp) |
455 | { | | 437 | { |
456 | | | 438 | |
457 | if (!is_struct_or_union(tp->t_tspec)) { | | 439 | if (!is_struct_or_union(tp->t_tspec)) { |
458 | /* attribute '%s' ignored for '%s' */ | | 440 | /* attribute '%s' ignored for '%s' */ |
459 | warning(326, "packed", type_name(tp)); | | 441 | warning(326, "packed", type_name(tp)); |
460 | return; | | 442 | return; |
461 | } | | 443 | } |
462 | | | 444 | |
463 | unsigned int bits = 0; | | 445 | unsigned int bits = 0; |
464 | bool named = false; | | 446 | bool named = false; |
465 | for (const sym_t *mem = tp->t_sou->sou_first_member; | | 447 | for (const sym_t *mem = tp->t_sou->sou_first_member; |
466 | mem != NULL; mem = mem->s_next) { | | 448 | mem != NULL; mem = mem->s_next) { |
467 | // TODO: Maybe update mem->u.s_member.sm_offset_in_bits. | | 449 | // TODO: Maybe update mem->u.s_member.sm_offset_in_bits. |
468 | if (mem->s_type->t_bitfield) { | | 450 | if (mem->s_type->t_bitfield) { |
469 | bits += bit_fields_width(&mem, &named); | | 451 | bits += bit_fields_width(&mem, &named); |
470 | if (mem == NULL) | | 452 | if (mem == NULL) |
471 | break; | | 453 | break; |
472 | } | | 454 | } |
473 | unsigned int mem_bits = type_size_in_bits(mem->s_type); | | 455 | unsigned int mem_bits = type_size_in_bits(mem->s_type); |
474 | if (tp->t_tspec == STRUCT) | | 456 | if (tp->t_tspec == STRUCT) |
475 | bits += mem_bits; | | 457 | bits += mem_bits; |
476 | else if (mem_bits > bits) | | 458 | else if (mem_bits > bits) |
477 | bits = mem_bits; | | 459 | bits = mem_bits; |
478 | } | | 460 | } |
479 | tp->t_sou->sou_size_in_bits = bits; | | 461 | tp->t_sou->sou_size_in_bits = bits; |
480 | } | | 462 | } |
481 | | | 463 | |
482 | void | | 464 | void |
483 | dcs_add_packed(void) | | 465 | dcs_add_packed(void) |
484 | { | | 466 | { |
485 | if (dcs->d_type == NULL) | | 467 | if (dcs->d_type == NULL) |
486 | dcs->d_packed = true; | | 468 | dcs->d_packed = true; |
487 | else | | 469 | else |
488 | pack_struct_or_union(dcs->d_type); | | 470 | pack_struct_or_union(dcs->d_type); |
489 | } | | 471 | } |
490 | | | 472 | |
491 | void | | 473 | void |
492 | dcs_set_used(void) | | 474 | dcs_set_used(void) |
493 | { | | 475 | { |
494 | dcs->d_used = true; | | 476 | dcs->d_used = true; |
495 | } | | 477 | } |
496 | | | 478 | |
497 | /* | | 479 | /* |
498 | * Remember a qualifier that is part of the declaration specifiers (and not the | | 480 | * Remember a qualifier that is part of the declaration specifiers (and not the |
499 | * declarator). The remembered qualifier is used by dcs_end_type for all | | 481 | * declarator). The remembered qualifier is used by dcs_end_type for all |
500 | * declarators. | | 482 | * declarators. |
501 | */ | | 483 | */ |
502 | void | | 484 | void |
503 | dcs_add_qualifiers(type_qualifiers qs) | | 485 | dcs_add_qualifiers(type_qualifiers qs) |
504 | { | | 486 | { |
505 | add_type_qualifiers(&dcs->d_qual, qs); | | 487 | add_type_qualifiers(&dcs->d_qual, qs); |
506 | } | | 488 | } |
507 | | | 489 | |
508 | void | | 490 | void |
509 | begin_declaration_level(decl_level_kind kind) | | 491 | begin_declaration_level(decl_level_kind kind) |
510 | { | | 492 | { |
511 | | | 493 | |
512 | decl_level *dl = xcalloc(1, sizeof(*dl)); | | 494 | decl_level *dl = xcalloc(1, sizeof(*dl)); |
513 | dl->d_enclosing = dcs; | | 495 | dl->d_enclosing = dcs; |
514 | dl->d_kind = kind; | | 496 | dl->d_kind = kind; |
515 | dl->d_last_dlsym = &dl->d_first_dlsym; | | 497 | dl->d_last_dlsym = &dl->d_first_dlsym; |
516 | dcs = dl; | | 498 | dcs = dl; |
517 | debug_enter(); | | 499 | debug_enter(); |
518 | debug_dcs(true); | | 500 | debug_dcs(true); |
519 | } | | 501 | } |
520 | | | 502 | |
521 | void | | 503 | void |
522 | end_declaration_level(void) | | 504 | end_declaration_level(void) |
523 | { | | 505 | { |
524 | | | 506 | |
525 | debug_dcs(true); | | 507 | debug_dcs(true); |
526 | | | 508 | |
527 | decl_level *dl = dcs; | | 509 | decl_level *dl = dcs; |
528 | dcs = dl->d_enclosing; | | 510 | dcs = dl->d_enclosing; |
529 | lint_assert(dcs != NULL); | | 511 | lint_assert(dcs != NULL); |
530 | | | 512 | |
531 | switch (dl->d_kind) { | | 513 | switch (dl->d_kind) { |
532 | case DLK_STRUCT: | | 514 | case DLK_STRUCT: |
533 | case DLK_UNION: | | 515 | case DLK_UNION: |
534 | case DLK_ENUM: | | 516 | case DLK_ENUM: |
535 | /* | | 517 | /* |
536 | * Symbols declared in (nested) structs or enums are part of | | 518 | * Symbols declared in (nested) structs or enums are part of |
537 | * the next level (they are removed from the symbol table if | | 519 | * the next level (they are removed from the symbol table if |
538 | * the symbols of the outer level are removed). | | 520 | * the symbols of the outer level are removed). |
539 | */ | | 521 | */ |
540 | if ((*dcs->d_last_dlsym = dl->d_first_dlsym) != NULL) | | 522 | if ((*dcs->d_last_dlsym = dl->d_first_dlsym) != NULL) |
541 | dcs->d_last_dlsym = dl->d_last_dlsym; | | 523 | dcs->d_last_dlsym = dl->d_last_dlsym; |
542 | break; | | 524 | break; |
543 | case DLK_OLD_STYLE_ARGS: | | 525 | case DLK_OLD_STYLE_ARGS: |
544 | /* | | 526 | /* |
545 | * All symbols in dcs->d_first_dlsym are introduced in | | 527 | * All symbols in dcs->d_first_dlsym are introduced in |
546 | * old-style argument declarations (it's not clean, but | | 528 | * old-style argument declarations (it's not clean, but |
547 | * possible). They are appended to the list of symbols declared | | 529 | * possible). They are appended to the list of symbols declared |
548 | * in an old-style argument identifier list or a new-style | | 530 | * in an old-style argument identifier list or a new-style |
549 | * parameter type list. | | 531 | * parameter type list. |
550 | */ | | 532 | */ |
551 | if (dl->d_first_dlsym != NULL) { | | 533 | if (dl->d_first_dlsym != NULL) { |
552 | *dl->d_last_dlsym = dcs->d_func_proto_syms; | | 534 | *dl->d_last_dlsym = dcs->d_func_proto_syms; |
553 | dcs->d_func_proto_syms = dl->d_first_dlsym; | | 535 | dcs->d_func_proto_syms = dl->d_first_dlsym; |
554 | } | | 536 | } |
555 | break; | | 537 | break; |
556 | case DLK_ABSTRACT: | | 538 | case DLK_ABSTRACT: |
557 | /* | | 539 | /* |
558 | * Append all symbols declared in the abstract declaration to | | 540 | * Append all symbols declared in the abstract declaration to |
559 | * the list of symbols declared in the surrounding declaration | | 541 | * the list of symbols declared in the surrounding declaration |
560 | * or block. | | 542 | * or block. |
561 | * | | 543 | * |
562 | * XXX I'm not sure whether they should be removed from the | | 544 | * XXX I'm not sure whether they should be removed from the |
563 | * symbol table now or later. | | 545 | * symbol table now or later. |
564 | */ | | 546 | */ |
565 | if ((*dcs->d_last_dlsym = dl->d_first_dlsym) != NULL) | | 547 | if ((*dcs->d_last_dlsym = dl->d_first_dlsym) != NULL) |
566 | dcs->d_last_dlsym = dl->d_last_dlsym; | | 548 | dcs->d_last_dlsym = dl->d_last_dlsym; |
567 | break; | | 549 | break; |
568 | case DLK_AUTO: | | 550 | case DLK_AUTO: |
569 | check_usage(dl); | | 551 | check_usage(dl); |
570 | /* FALLTHROUGH */ | | 552 | /* FALLTHROUGH */ |
571 | case DLK_PROTO_PARAMS: | | 553 | case DLK_PROTO_PARAMS: |
572 | /* usage of arguments will be checked by end_function() */ | | 554 | /* usage of arguments will be checked by end_function() */ |
573 | symtab_remove_level(dl->d_first_dlsym); | | 555 | symtab_remove_level(dl->d_first_dlsym); |
574 | break; | | 556 | break; |
575 | case DLK_EXTERN: | | 557 | case DLK_EXTERN: |
576 | /* there is nothing around an external declaration */ | | 558 | /* there is nothing around an external declaration */ |
577 | /* FALLTHROUGH */ | | 559 | /* FALLTHROUGH */ |
578 | default: | | 560 | default: |
579 | lint_assert(/*CONSTCOND*/false); | | 561 | lint_assert(/*CONSTCOND*/false); |
580 | } | | 562 | } |
581 | free(dl); | | 563 | free(dl); |
582 | debug_leave(); | | 564 | debug_leave(); |
583 | } | | 565 | } |
584 | | | 566 | |
585 | /* | | 567 | /* |
586 | * Set flag d_asm in all declaration stack elements up to the outermost one. | | 568 | * Set flag d_asm in all declaration stack elements up to the outermost one. |
587 | * | | 569 | * |
588 | * This is used to mark compound statements which have, possibly in nested | | 570 | * This is used to mark compound statements which have, possibly in nested |
589 | * compound statements, asm statements. For these compound statements, no | | 571 | * compound statements, asm statements. For these compound statements, no |
590 | * warnings about unused or uninitialized variables are printed. | | 572 | * warnings about unused or uninitialized variables are printed. |
591 | * | | 573 | * |
592 | * There is no need to clear d_asm in decl_level structs with context AUTO, as | | 574 | * There is no need to clear d_asm in decl_level structs with context AUTO, as |
593 | * these structs are freed at the end of the compound statement. But it must be | | 575 | * these structs are freed at the end of the compound statement. But it must be |
594 | * cleared in the outermost decl_level struct, which has context EXTERN. This | | 576 | * cleared in the outermost decl_level struct, which has context EXTERN. This |
595 | * could be done in dcs_begin_type and would work for C90, but not for C99 or | | 577 | * could be done in dcs_begin_type and would work for C90, but not for C99 or |
596 | * C++ (due to mixed statements and declarations). Thus, we clear it in | | 578 | * C++ (due to mixed statements and declarations). Thus, we clear it in |
597 | * global_clean_up_decl. | | 579 | * global_clean_up_decl. |
598 | */ | | 580 | */ |
599 | void | | 581 | void |
600 | dcs_set_asm(void) | | 582 | dcs_set_asm(void) |
601 | { | | 583 | { |
602 | | | 584 | |
603 | for (decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing) | | 585 | for (decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing) |
604 | dl->d_asm = true; | | 586 | dl->d_asm = true; |
605 | } | | 587 | } |
606 | | | 588 | |
607 | void | | 589 | void |
608 | dcs_begin_type(void) | | 590 | dcs_begin_type(void) |
609 | { | | 591 | { |
610 | | | 592 | |
611 | debug_enter(); | | 593 | debug_enter(); |
612 | dcs->d_abstract_type = NO_TSPEC; | | 594 | dcs->d_abstract_type = NO_TSPEC; |
613 | dcs->d_complex_mod = NO_TSPEC; | | 595 | dcs->d_complex_mod = NO_TSPEC; |
614 | dcs->d_sign_mod = NO_TSPEC; | | 596 | dcs->d_sign_mod = NO_TSPEC; |
615 | dcs->d_rank_mod = NO_TSPEC; | | 597 | dcs->d_rank_mod = NO_TSPEC; |
616 | dcs->d_scl = NOSCL; | | 598 | dcs->d_scl = NOSCL; |
617 | dcs->d_type = NULL; | | 599 | dcs->d_type = NULL; |
618 | dcs->d_qual = (type_qualifiers) { .tq_const = false }; | | 600 | dcs->d_qual = (type_qualifiers) { .tq_const = false }; |
619 | dcs->d_inline = false; | | 601 | dcs->d_inline = false; |
620 | dcs->d_multiple_storage_classes = false; | | 602 | dcs->d_multiple_storage_classes = false; |
621 | dcs->d_invalid_type_combination = false; | | 603 | dcs->d_invalid_type_combination = false; |
622 | dcs->d_nonempty_decl = false; | | 604 | dcs->d_nonempty_decl = false; |
623 | dcs->d_no_type_specifier = false; | | 605 | dcs->d_no_type_specifier = false; |
624 | } | | 606 | } |
625 | | | 607 | |
626 | static void | | 608 | static void |
627 | dcs_adjust_storage_class(void) | | 609 | dcs_adjust_storage_class(void) |
628 | { | | 610 | { |
629 | if (dcs->d_kind == DLK_EXTERN) { | | 611 | if (dcs->d_kind == DLK_EXTERN) { |
630 | if (dcs->d_scl == REG || dcs->d_scl == AUTO) { | | 612 | if (dcs->d_scl == REG || dcs->d_scl == AUTO) { |
631 | /* illegal storage class */ | | 613 | /* illegal storage class */ |
632 | error(8); | | 614 | error(8); |
633 | dcs->d_scl = NOSCL; | | 615 | dcs->d_scl = NOSCL; |
634 | } | | 616 | } |
635 | } else if (dcs->d_kind == DLK_OLD_STYLE_ARGS || | | 617 | } else if (dcs->d_kind == DLK_OLD_STYLE_ARGS || |
636 | dcs->d_kind == DLK_PROTO_PARAMS) { | | 618 | dcs->d_kind == DLK_PROTO_PARAMS) { |
637 | if (dcs->d_scl != NOSCL && dcs->d_scl != REG) { | | 619 | if (dcs->d_scl != NOSCL && dcs->d_scl != REG) { |
638 | /* only 'register' is valid as storage class ... */ | | 620 | /* only 'register' is valid as storage class ... */ |
639 | error(9); | | 621 | error(9); |
640 | dcs->d_scl = NOSCL; | | 622 | dcs->d_scl = NOSCL; |
641 | } | | 623 | } |
642 | } | | 624 | } |
643 | } | | 625 | } |
644 | | | 626 | |
645 | /* | | 627 | /* |
646 | * Merge the declaration specifiers from dcs into dcs->d_type. | | 628 | * Merge the declaration specifiers from dcs into dcs->d_type. |
647 | * | | 629 | * |
648 | * See C99 6.7.2 "Type specifiers". | | 630 | * See C99 6.7.2 "Type specifiers". |
649 | */ | | 631 | */ |
650 | static void | | 632 | static void |
651 | dcs_merge_declaration_specifiers(void) | | 633 | dcs_merge_declaration_specifiers(void) |
652 | { | | 634 | { |
653 | tspec_t t = dcs->d_abstract_type; | | 635 | tspec_t t = dcs->d_abstract_type; |
654 | tspec_t c = dcs->d_complex_mod; | | 636 | tspec_t c = dcs->d_complex_mod; |
655 | tspec_t s = dcs->d_sign_mod; | | 637 | tspec_t s = dcs->d_sign_mod; |
656 | tspec_t l = dcs->d_rank_mod; | | 638 | tspec_t l = dcs->d_rank_mod; |
657 | type_t *tp = dcs->d_type; | | 639 | type_t *tp = dcs->d_type; |
658 | | | 640 | |
659 | if (tp != NULL) { | | 641 | if (tp != NULL) { |
660 | lint_assert(t == NO_TSPEC); | | 642 | lint_assert(t == NO_TSPEC); |
661 | lint_assert(s == NO_TSPEC); | | 643 | lint_assert(s == NO_TSPEC); |
662 | lint_assert(l == NO_TSPEC); | | 644 | lint_assert(l == NO_TSPEC); |
663 | return; | | 645 | return; |
664 | } | | 646 | } |
665 | | | 647 | |
666 | debug_step("%s: %s", __func__, type_name(tp)); | | 648 | debug_step("%s: %s", __func__, type_name(tp)); |
667 | | | 649 | |
668 | if (t == NO_TSPEC && s == NO_TSPEC && l == NO_TSPEC && c == NO_TSPEC) | | 650 | if (t == NO_TSPEC && s == NO_TSPEC && l == NO_TSPEC && c == NO_TSPEC) |
669 | dcs->d_no_type_specifier = true; | | 651 | dcs->d_no_type_specifier = true; |
670 | if (t == NO_TSPEC && s == NO_TSPEC && (l == NO_TSPEC || l == LONG)) | | 652 | if (t == NO_TSPEC && s == NO_TSPEC && (l == NO_TSPEC || l == LONG)) |
671 | t = c; | | 653 | t = c; |
672 | | | 654 | |
673 | if (t == NO_TSPEC) | | 655 | if (t == NO_TSPEC) |
674 | t = INT; | | 656 | t = INT; |
675 | if (s == NO_TSPEC && t == INT) | | 657 | if (s == NO_TSPEC && t == INT) |
676 | s = SIGNED; | | 658 | s = SIGNED; |
677 | if (l != NO_TSPEC && t == CHAR) { | | 659 | if (l != NO_TSPEC && t == CHAR) { |
678 | dcs->d_invalid_type_combination = true; | | 660 | dcs->d_invalid_type_combination = true; |
679 | l = NO_TSPEC; | | 661 | l = NO_TSPEC; |
680 | } | | 662 | } |
681 | if (l == LONG && t == FLOAT) { | | 663 | if (l == LONG && t == FLOAT) { |
682 | l = NO_TSPEC; | | 664 | l = NO_TSPEC; |
683 | t = DOUBLE; | | 665 | t = DOUBLE; |
684 | if (allow_c90) | | 666 | if (allow_c90) |
685 | /* use 'double' instead of 'long float' */ | | 667 | /* use 'double' instead of 'long float' */ |
686 | warning(6); | | 668 | warning(6); |
687 | } | | 669 | } |
688 | if ((l == LONG && t == DOUBLE) || t == LDOUBLE) { | | 670 | if ((l == LONG && t == DOUBLE) || t == LDOUBLE) { |
689 | l = NO_TSPEC; | | 671 | l = NO_TSPEC; |
690 | t = LDOUBLE; | | 672 | t = LDOUBLE; |
691 | } | | 673 | } |
692 | if (t == LDOUBLE && !allow_c90) { | | 674 | if (t == LDOUBLE && !allow_c90) { |
693 | /* 'long double' is illegal in traditional C */ | | 675 | /* 'long double' is illegal in traditional C */ |
694 | warning(266); | | 676 | warning(266); |
695 | } | | 677 | } |
696 | if (l == LONG && t == DCOMPLEX) { | | 678 | if (l == LONG && t == DCOMPLEX) { |
697 | l = NO_TSPEC; | | 679 | l = NO_TSPEC; |
698 | t = LCOMPLEX; | | 680 | t = LCOMPLEX; |
699 | } | | 681 | } |
700 | | | 682 | |
701 | if (t != INT && t != CHAR && (s != NO_TSPEC || l != NO_TSPEC)) { | | 683 | if (t != INT && t != CHAR && (s != NO_TSPEC || l != NO_TSPEC)) { |
702 | dcs->d_invalid_type_combination = true; | | 684 | dcs->d_invalid_type_combination = true; |
703 | l = s = NO_TSPEC; | | 685 | l = s = NO_TSPEC; |
704 | } | | 686 | } |
705 | if (l != NO_TSPEC) | | 687 | if (l != NO_TSPEC) |
706 | t = l; | | 688 | t = l; |
707 | dcs->d_type = gettyp(merge_signedness(t, s)); | | 689 | dcs->d_type = gettyp(merge_signedness(t, s)); |
708 | } | | 690 | } |
709 | | | 691 | |
710 | /* Create a type in 'dcs->d_type' from the information gathered in 'dcs'. */ | | 692 | /* Create a type in 'dcs->d_type' from the information gathered in 'dcs'. */ |
711 | void | | 693 | void |
712 | dcs_end_type(void) | | 694 | dcs_end_type(void) |
713 | { | | 695 | { |
714 | | | 696 | |
715 | dcs_merge_declaration_specifiers(); | | 697 | dcs_merge_declaration_specifiers(); |
716 | | | 698 | |
717 | if (dcs->d_multiple_storage_classes) { | | 699 | if (dcs->d_multiple_storage_classes) { |
718 | /* only one storage class allowed */ | | 700 | /* only one storage class allowed */ |
719 | error(7); | | 701 | error(7); |
720 | } | | 702 | } |
721 | if (dcs->d_invalid_type_combination) { | | 703 | if (dcs->d_invalid_type_combination) { |
722 | /* illegal type combination */ | | 704 | /* illegal type combination */ |
723 | error(4); | | 705 | error(4); |
724 | } | | 706 | } |
725 | | | 707 | |
726 | dcs_adjust_storage_class(); | | 708 | dcs_adjust_storage_class(); |
727 | | | 709 | |
728 | if (dcs->d_qual.tq_const && dcs->d_type->t_const | | 710 | if (dcs->d_qual.tq_const && dcs->d_type->t_const |
729 | && !dcs->d_type->t_typeof) { | | 711 | && !dcs->d_type->t_typeof) { |
730 | lint_assert(dcs->d_type->t_typedef); | | 712 | lint_assert(dcs->d_type->t_typedef); |
731 | /* typedef already qualified with '%s' */ | | 713 | /* typedef already qualified with '%s' */ |
732 | warning(68, "const"); | | 714 | warning(68, "const"); |
733 | } | | 715 | } |
734 | if (dcs->d_qual.tq_volatile && dcs->d_type->t_volatile && | | 716 | if (dcs->d_qual.tq_volatile && dcs->d_type->t_volatile && |
735 | !dcs->d_type->t_typeof) { | | 717 | !dcs->d_type->t_typeof) { |
736 | lint_assert(dcs->d_type->t_typedef); | | 718 | lint_assert(dcs->d_type->t_typedef); |
737 | /* typedef already qualified with '%s' */ | | 719 | /* typedef already qualified with '%s' */ |
738 | warning(68, "volatile"); | | 720 | warning(68, "volatile"); |
739 | } | | 721 | } |
740 | | | 722 | |
741 | if (dcs->d_qual.tq_const || dcs->d_qual.tq_volatile) { | | 723 | if (dcs->d_qual.tq_const || dcs->d_qual.tq_volatile) { |
742 | dcs->d_type = block_dup_type(dcs->d_type); | | 724 | dcs->d_type = block_dup_type(dcs->d_type); |
743 | dcs->d_type->t_const |= dcs->d_qual.tq_const; | | 725 | dcs->d_type->t_const |= dcs->d_qual.tq_const; |
744 | dcs->d_type->t_volatile |= dcs->d_qual.tq_volatile; | | 726 | dcs->d_type->t_volatile |= dcs->d_qual.tq_volatile; |
745 | } | | 727 | } |
746 | | | 728 | |
747 | debug_leave(); | | 729 | debug_leave(); |
748 | } | | 730 | } |
749 | | | 731 | |
750 | /* | | 732 | /* |
751 | * Return the length of a type in bits. For bit-fields, return the length of | | 733 | * Return the length of a type in bits. For bit-fields, return the length of |
752 | * the underlying storage type. | | 734 | * the underlying storage type. |
753 | * | | 735 | * |
754 | * Printing a message if the outermost dimension of an array is 0 must | | 736 | * Printing a message if the outermost dimension of an array is 0 must |
755 | * be done by the caller. All other problems are reported by this function | | 737 | * be done by the caller. All other problems are reported by this function |
756 | * if name is not NULL. | | 738 | * if name is not NULL. |
757 | */ | | 739 | */ |
758 | int | | 740 | int |
759 | length_in_bits(const type_t *tp, const char *name) | | 741 | length_in_bits(const type_t *tp, const char *name) |
760 | { | | 742 | { |
761 | | | 743 | |
762 | if (tp == NULL) | | 744 | if (tp == NULL) |
763 | return -1; | | 745 | return -1; |
764 | | | 746 | |
765 | unsigned int elem = 1; | | 747 | unsigned int elem = 1; |
766 | while (tp->t_tspec == ARRAY) { | | 748 | while (tp->t_tspec == ARRAY) { |
767 | elem *= tp->t_dim; | | 749 | elem *= tp->t_dim; |
768 | tp = tp->t_subt; | | 750 | tp = tp->t_subt; |
769 | } | | 751 | } |
770 | | | 752 | |
771 | if (is_struct_or_union(tp->t_tspec)) { | | 753 | if (is_struct_or_union(tp->t_tspec)) { |
772 | if (is_incomplete(tp) && name != NULL) { | | 754 | if (is_incomplete(tp) && name != NULL) { |
773 | /* '%s' has incomplete type '%s' */ | | 755 | /* '%s' has incomplete type '%s' */ |
774 | error(31, name, type_name(tp)); | | 756 | error(31, name, type_name(tp)); |
775 | } | | 757 | } |
776 | return (int)(elem * tp->t_sou->sou_size_in_bits); | | 758 | return (int)(elem * tp->t_sou->sou_size_in_bits); |
777 | } | | 759 | } |
778 | | | 760 | |
779 | if (tp->t_tspec == ENUM && is_incomplete(tp) && name != NULL) | | 761 | if (tp->t_tspec == ENUM && is_incomplete(tp) && name != NULL) |
780 | /* incomplete enum type '%s' */ | | 762 | /* incomplete enum type '%s' */ |
781 | warning(13, name); | | 763 | warning(13, name); |
782 | | | 764 | |
783 | lint_assert(tp->t_tspec != FUNC); | | 765 | lint_assert(tp->t_tspec != FUNC); |
784 | | | 766 | |
785 | unsigned int elsz = size_in_bits(tp->t_tspec); | | 767 | unsigned int elsz = size_in_bits(tp->t_tspec); |
786 | /* | | 768 | /* |
787 | * Workaround until the type parser (see add_function, add_array, | | 769 | * Workaround until the type parser (see add_function, add_array, |
788 | * add_pointer) does not construct the invalid intermediate declaration | | 770 | * add_pointer) does not construct the invalid intermediate declaration |
789 | * 'void b[4]' for the legitimate declaration 'void *b[4]'. | | 771 | * 'void b[4]' for the legitimate declaration 'void *b[4]'. |
790 | */ | | 772 | */ |
791 | if (sytxerr > 0 && elsz == 0) | | 773 | if (sytxerr > 0 && elsz == 0) |
792 | elsz = CHAR_SIZE; | | 774 | elsz = CHAR_SIZE; |
793 | lint_assert(elsz > 0); | | 775 | lint_assert(elsz > 0); |
794 | return (int)(elem * elsz); | | 776 | return (int)(elem * elsz); |
795 | } | | 777 | } |
796 | | | 778 | |
797 | unsigned int | | 779 | unsigned int |
798 | alignment_in_bits(const type_t *tp) | | 780 | alignment_in_bits(const type_t *tp) |
799 | { | | 781 | { |
800 | | | 782 | |
801 | /* Super conservative so that it works for most systems. */ | | 783 | /* Super conservative so that it works for most systems. */ |
802 | unsigned int worst_align_in_bits = 2 * LONG_SIZE; | | 784 | unsigned int worst_align_in_bits = 2 * LONG_SIZE; |
803 | | | 785 | |
804 | while (tp->t_tspec == ARRAY) | | 786 | while (tp->t_tspec == ARRAY) |
805 | tp = tp->t_subt; | | 787 | tp = tp->t_subt; |
806 | | | 788 | |
807 | tspec_t t = tp->t_tspec; | | 789 | tspec_t t = tp->t_tspec; |
808 | unsigned int a; | | 790 | unsigned int a; |
809 | if (is_struct_or_union(t)) | | 791 | if (is_struct_or_union(t)) |
810 | a = tp->t_sou->sou_align_in_bits; | | 792 | a = tp->t_sou->sou_align_in_bits; |
811 | else { | | 793 | else { |
812 | lint_assert(t != FUNC); | | 794 | lint_assert(t != FUNC); |
813 | if ((a = size_in_bits(t)) == 0) | | 795 | if ((a = size_in_bits(t)) == 0) |
814 | a = CHAR_SIZE; | | 796 | a = CHAR_SIZE; |
815 | else if (a > worst_align_in_bits) | | 797 | else if (a > worst_align_in_bits) |
816 | a = worst_align_in_bits; | | 798 | a = worst_align_in_bits; |
817 | } | | 799 | } |
818 | lint_assert(a >= CHAR_SIZE); | | 800 | lint_assert(a >= CHAR_SIZE); |
819 | lint_assert(a <= worst_align_in_bits); | | 801 | lint_assert(a <= worst_align_in_bits); |
820 | return a; | | 802 | return a; |
821 | } | | 803 | } |
822 | | | 804 | |
823 | /* | | 805 | /* |
824 | * Concatenate two lists of symbols by s_next. Used by declarations of | | 806 | * Concatenate two lists of symbols by s_next. Used by declarations of |
825 | * struct/union/enum elements and parameters. | | 807 | * struct/union/enum elements and parameters. |
826 | */ | | 808 | */ |
827 | sym_t * | | 809 | sym_t * |
828 | concat_symbols(sym_t *l1, sym_t *l2) | | 810 | concat_symbols(sym_t *l1, sym_t *l2) |
829 | { | | 811 | { |
830 | | | 812 | |
831 | if (l1 == NULL) | | 813 | if (l1 == NULL) |
832 | return l2; | | 814 | return l2; |
833 | sym_t *l = l1; | | 815 | sym_t *l = l1; |
834 | while (l->s_next != NULL) | | 816 | while (l->s_next != NULL) |
835 | l = l->s_next; | | 817 | l = l->s_next; |
836 | l->s_next = l2; | | 818 | l->s_next = l2; |
837 | return l1; | | 819 | return l1; |
838 | } | | 820 | } |
839 | | | 821 | |
840 | /* | | 822 | /* |
841 | * Check if the type of the given symbol is valid. | | 823 | * Check if the type of the given symbol is valid. |
842 | * | | 824 | * |
843 | * Invalid types are: | | 825 | * Invalid types are: |
844 | * - arrays of incomplete types or functions | | 826 | * - arrays of incomplete types or functions |
845 | * - functions returning arrays or functions | | 827 | * - functions returning arrays or functions |
846 | * - void types other than type of function or pointer | | 828 | * - void types other than type of function or pointer |
847 | */ | | 829 | */ |
848 | void | | 830 | void |
849 | check_type(sym_t *sym) | | 831 | check_type(sym_t *sym) |
850 | { | | 832 | { |
851 | | | 833 | |
852 | type_t **tpp = &sym->s_type; | | 834 | type_t **tpp = &sym->s_type; |
853 | tspec_t to = NO_TSPEC; | | 835 | tspec_t to = NO_TSPEC; |
854 | while (*tpp != NULL) { | | 836 | while (*tpp != NULL) { |
855 | type_t *tp = *tpp; | | 837 | type_t *tp = *tpp; |
856 | tspec_t t = tp->t_tspec; | | 838 | tspec_t t = tp->t_tspec; |
857 | /* | | 839 | /* |
858 | * If this is the type of an old-style function definition, | | 840 | * If this is the type of an old-style function definition, |
859 | * a better warning is printed in begin_function(). | | 841 | * a better warning is printed in begin_function(). |
860 | */ | | 842 | */ |
861 | if (t == FUNC && !tp->t_proto && | | 843 | if (t == FUNC && !tp->t_proto && |
862 | !(to == NO_TSPEC && sym->s_osdef)) { | | 844 | !(to == NO_TSPEC && sym->s_osdef)) { |
863 | /* TODO: Make this an error in C99 mode as well. */ | | 845 | /* TODO: Make this an error in C99 mode as well. */ |
864 | if ((!allow_trad && !allow_c99) && hflag) | | 846 | if ((!allow_trad && !allow_c99) && hflag) |
865 | /* function declaration is not a prototype */ | | 847 | /* function declaration is not a prototype */ |
866 | warning(287); | | 848 | warning(287); |
867 | } | | 849 | } |
868 | if (to == FUNC) { | | 850 | if (to == FUNC) { |
869 | if (t == FUNC || t == ARRAY) { | | 851 | if (t == FUNC || t == ARRAY) { |
870 | /* function returns illegal type '%s' */ | | 852 | /* function returns illegal type '%s' */ |
871 | error(15, type_name(tp)); | | 853 | error(15, type_name(tp)); |
872 | *tpp = block_derive_type( | | 854 | *tpp = block_derive_type( |
873 | t == FUNC ? *tpp : (*tpp)->t_subt, PTR); | | 855 | t == FUNC ? *tpp : (*tpp)->t_subt, PTR); |
874 | return; | | 856 | return; |
875 | } | | 857 | } |
876 | if (tp->t_const || tp->t_volatile) { | | 858 | if (tp->t_const || tp->t_volatile) { |
877 | /* TODO: Make this a warning in C99 mode as well. */ | | 859 | /* TODO: Make this a warning in C99 mode as well. */ |
878 | if (!allow_trad && !allow_c99) { /* XXX or better allow_c90? */ | | 860 | if (!allow_trad && !allow_c99) { /* XXX or better allow_c90? */ |
879 | /* function cannot return const... */ | | 861 | /* function cannot return const... */ |
880 | warning(228); | | 862 | warning(228); |
881 | } | | 863 | } |
882 | } | | 864 | } |
883 | } else if (to == ARRAY) { | | 865 | } else if (to == ARRAY) { |
884 | if (t == FUNC) { | | 866 | if (t == FUNC) { |
885 | /* array of function is illegal */ | | 867 | /* array of function is illegal */ |
886 | error(16); | | 868 | error(16); |
887 | *tpp = gettyp(INT); | | 869 | *tpp = gettyp(INT); |
888 | return; | | 870 | return; |
889 | } | | 871 | } |
890 | if (t == ARRAY && tp->t_dim == 0) { | | 872 | if (t == ARRAY && tp->t_dim == 0) { |
891 | /* null dimension */ | | 873 | /* null dimension */ |
892 | error(17); | | 874 | error(17); |
893 | return; | | 875 | return; |
894 | } | | 876 | } |
895 | if (t == VOID) { | | 877 | if (t == VOID) { |
896 | /* illegal use of 'void' */ | | 878 | /* illegal use of 'void' */ |
897 | error(18); | | 879 | error(18); |
898 | *tpp = gettyp(INT); | | 880 | *tpp = gettyp(INT); |
899 | } | | 881 | } |
900 | /* | | 882 | /* |
901 | * No need to check for incomplete types here as | | 883 | * No need to check for incomplete types here as |
902 | * length_in_bits already does this. | | 884 | * length_in_bits already does this. |
903 | */ | | 885 | */ |
904 | } else if (to == NO_TSPEC && t == VOID) { | | 886 | } else if (to == NO_TSPEC && t == VOID) { |
905 | if (dcs->d_kind == DLK_PROTO_PARAMS) { | | 887 | if (dcs->d_kind == DLK_PROTO_PARAMS) { |
906 | if (sym->s_scl != ABSTRACT) { | | 888 | if (sym->s_scl != ABSTRACT) { |
907 | lint_assert(sym->s_name != unnamed); | | 889 | lint_assert(sym->s_name != unnamed); |
908 | /* void parameter '%s' cannot ... */ | | 890 | /* void parameter '%s' cannot ... */ |
909 | error(61, sym->s_name); | | 891 | error(61, sym->s_name); |
910 | *tpp = gettyp(INT); | | 892 | *tpp = gettyp(INT); |
911 | } | | 893 | } |
912 | } else if (dcs->d_kind == DLK_ABSTRACT) { | | 894 | } else if (dcs->d_kind == DLK_ABSTRACT) { |
913 | /* ok */ | | 895 | /* ok */ |
914 | } else if (sym->s_scl != TYPEDEF) { | | 896 | } else if (sym->s_scl != TYPEDEF) { |
915 | /* void type for '%s' */ | | 897 | /* void type for '%s' */ |
916 | error(19, sym->s_name); | | 898 | error(19, sym->s_name); |
917 | *tpp = gettyp(INT); | | 899 | *tpp = gettyp(INT); |
918 | } | | 900 | } |
919 | } | | 901 | } |
920 | if (t == VOID && to != PTR) { | | 902 | if (t == VOID && to != PTR) { |
921 | if (tp->t_const || tp->t_volatile) { | | 903 | if (tp->t_const || tp->t_volatile) { |
922 | /* inappropriate qualifiers with 'void' */ | | 904 | /* inappropriate qualifiers with 'void' */ |
923 | warning(69); | | 905 | warning(69); |
924 | tp->t_const = tp->t_volatile = false; | | 906 | tp->t_const = tp->t_volatile = false; |
925 | } | | 907 | } |
926 | } | | 908 | } |
927 | tpp = &tp->t_subt; | | 909 | tpp = &tp->t_subt; |
928 | to = t; | | 910 | to = t; |
929 | } | | 911 | } |
930 | } | | 912 | } |
931 | | | 913 | |
932 | /* | | 914 | /* |
933 | * In traditional C, the only portable type for bit-fields is unsigned int. | | 915 | * In traditional C, the only portable type for bit-fields is unsigned int. |
934 | * | | 916 | * |
935 | * In C90, the only allowed types for bit-fields are int, signed int and | | 917 | * In C90, the only allowed types for bit-fields are int, signed int and |
936 | * unsigned int (3.5.2.1). There is no mention of implementation-defined | | 918 | * unsigned int (3.5.2.1). There is no mention of implementation-defined |
937 | * types. | | 919 | * types. |
938 | * | | 920 | * |
939 | * In C99, the only portable types for bit-fields are _Bool, signed int and | | 921 | * In C99, the only portable types for bit-fields are _Bool, signed int and |
940 | * unsigned int (6.7.2.1p4). In addition, C99 allows "or some other | | 922 | * unsigned int (6.7.2.1p4). In addition, C99 allows "or some other |
941 | * implementation-defined type". | | 923 | * implementation-defined type". |
942 | */ | | 924 | */ |
943 | static void | | 925 | static void |
944 | check_bit_field_type(sym_t *dsym, type_t **const inout_tp, tspec_t *inout_t) | | 926 | check_bit_field_type(sym_t *dsym, type_t **const inout_tp, tspec_t *inout_t) |
945 | { | | 927 | { |
946 | type_t *tp = *inout_tp; | | 928 | type_t *tp = *inout_tp; |
947 | tspec_t t = *inout_t; | | 929 | tspec_t t = *inout_t; |
948 | | | 930 | |
949 | if (t == CHAR || t == UCHAR || t == SCHAR || | | 931 | if (t == CHAR || t == UCHAR || t == SCHAR || |
950 | t == SHORT || t == USHORT || t == ENUM) { | | 932 | t == SHORT || t == USHORT || t == ENUM) { |
951 | if (!suppress_bitfieldtype) { | | 933 | if (!suppress_bitfieldtype) { |
952 | /* TODO: Make this an error in C99 mode as well. */ | | 934 | /* TODO: Make this an error in C99 mode as well. */ |
953 | if (!allow_trad && !allow_c99) { | | 935 | if (!allow_trad && !allow_c99) { |
954 | type_t *btp = block_dup_type(tp); | | 936 | type_t *btp = block_dup_type(tp); |
955 | btp->t_bitfield = false; | | 937 | btp->t_bitfield = false; |
956 | /* bit-field type '%s' invalid in ANSI C */ | | 938 | /* bit-field type '%s' invalid in ANSI C */ |
957 | warning(273, type_name(btp)); | | 939 | warning(273, type_name(btp)); |
958 | } else if (pflag) { | | 940 | } else if (pflag) { |
959 | type_t *btp = block_dup_type(tp); | | 941 | type_t *btp = block_dup_type(tp); |
960 | btp->t_bitfield = false; | | 942 | btp->t_bitfield = false; |
961 | /* nonportable bit-field type '%s' */ | | 943 | /* nonportable bit-field type '%s' */ |
962 | warning(34, type_name(btp)); | | 944 | warning(34, type_name(btp)); |
963 | } | | 945 | } |
964 | } | | 946 | } |
965 | } else if (t == INT && dcs->d_sign_mod == NO_TSPEC) { | | 947 | } else if (t == INT && dcs->d_sign_mod == NO_TSPEC) { |
966 | if (pflag && !suppress_bitfieldtype) { | | 948 | if (pflag && !suppress_bitfieldtype) { |
967 | /* bit-field of type plain 'int' has ... */ | | 949 | /* bit-field of type plain 'int' has ... */ |
968 | warning(344); | | 950 | warning(344); |
969 | } | | 951 | } |
970 | } else if (!(t == INT || t == UINT || t == BOOL | | 952 | } else if (!(t == INT || t == UINT || t == BOOL |
971 | || (is_integer(t) && (suppress_bitfieldtype || allow_gcc)))) { | | 953 | || (is_integer(t) && (suppress_bitfieldtype || allow_gcc)))) { |
972 | | | 954 | |
973 | type_t *btp = block_dup_type(tp); | | 955 | type_t *btp = block_dup_type(tp); |
974 | btp->t_bitfield = false; | | 956 | btp->t_bitfield = false; |
975 | /* illegal bit-field type '%s' */ | | 957 | /* illegal bit-field type '%s' */ |
976 | warning(35, type_name(btp)); | | 958 | warning(35, type_name(btp)); |
977 | | | 959 | |
978 | unsigned int width = tp->t_bit_field_width; | | 960 | unsigned int width = tp->t_bit_field_width; |
979 | dsym->s_type = tp = block_dup_type(gettyp(t = INT)); | | 961 | dsym->s_type = tp = block_dup_type(gettyp(t = INT)); |
980 | if ((tp->t_bit_field_width = width) > size_in_bits(t)) | | 962 | if ((tp->t_bit_field_width = width) > size_in_bits(t)) |
981 | tp->t_bit_field_width = size_in_bits(t); | | 963 | tp->t_bit_field_width = size_in_bits(t); |
982 | *inout_t = t; | | 964 | *inout_t = t; |
983 | *inout_tp = tp; | | 965 | *inout_tp = tp; |
984 | } | | 966 | } |
985 | } | | 967 | } |
986 | | | 968 | |
987 | static void | | 969 | static void |
988 | check_bit_field(sym_t *dsym, tspec_t *inout_t, type_t **const inout_tp) | | 970 | check_bit_field(sym_t *dsym, tspec_t *inout_t, type_t **const inout_tp) |
989 | { | | 971 | { |
990 | | | 972 | |
991 | check_bit_field_type(dsym, inout_tp, inout_t); | | 973 | check_bit_field_type(dsym, inout_tp, inout_t); |
992 | | | 974 | |
993 | type_t *tp = *inout_tp; | | 975 | type_t *tp = *inout_tp; |
994 | tspec_t t = *inout_t; | | 976 | tspec_t t = *inout_t; |
995 | unsigned int t_width = size_in_bits(t); | | 977 | unsigned int t_width = size_in_bits(t); |
996 | if (tp->t_bit_field_width > t_width) { | | 978 | if (tp->t_bit_field_width > t_width) { |
997 | /* illegal bit-field size: %d */ | | 979 | /* illegal bit-field size: %d */ |
998 | error(36, (int)tp->t_bit_field_width); | | 980 | error(36, (int)tp->t_bit_field_width); |
999 | tp->t_bit_field_width = t_width; | | 981 | tp->t_bit_field_width = t_width; |
1000 | } else if (tp->t_bit_field_width == 0 && dsym->s_name != unnamed) { | | 982 | } else if (tp->t_bit_field_width == 0 && dsym->s_name != unnamed) { |
1001 | /* zero size bit-field */ | | 983 | /* zero size bit-field */ |
1002 | error(37); | | 984 | error(37); |
1003 | tp->t_bit_field_width = t_width; | | 985 | tp->t_bit_field_width = t_width; |
1004 | } | | 986 | } |
1005 | if (dsym->s_scl == UNION_MEMBER) { | | 987 | if (dsym->s_scl == UNION_MEMBER) { |
1006 | /* bit-field in union is very unusual */ | | 988 | /* bit-field in union is very unusual */ |
1007 | warning(41); | | 989 | warning(41); |
1008 | dsym->s_type->t_bitfield = false; | | 990 | dsym->s_type->t_bitfield = false; |
1009 | dsym->s_bitfield = false; | | 991 | dsym->s_bitfield = false; |
1010 | } | | 992 | } |
1011 | } | | 993 | } |
1012 | | | 994 | |
| | | 995 | /* Aligns the next structure element as required. */ |
| | | 996 | static void |
| | | 997 | dcs_align(unsigned int member_alignment, unsigned int bit_field_width) |
| | | 998 | { |
| | | 999 | |
| | | 1000 | if (member_alignment > dcs->d_sou_align_in_bits) |
| | | 1001 | dcs->d_sou_align_in_bits = member_alignment; |
| | | 1002 | |
| | | 1003 | unsigned int offset = (dcs->d_sou_size_in_bits + member_alignment - 1) |
| | | 1004 | & ~(member_alignment - 1); |
| | | 1005 | if (bit_field_width == 0 |
| | | 1006 | || dcs->d_sou_size_in_bits + bit_field_width > offset) |
| | | 1007 | dcs->d_sou_size_in_bits = offset; |
| | | 1008 | } |
| | | 1009 | |
1013 | /* Add a member to the struct or union type that is being built in 'dcs'. */ | | 1010 | /* Add a member to the struct or union type that is being built in 'dcs'. */ |
1014 | static void | | 1011 | static void |
1015 | dcs_add_member(sym_t *mem) | | 1012 | dcs_add_member(sym_t *mem) |
1016 | { | | 1013 | { |
1017 | type_t *tp = mem->s_type; | | 1014 | type_t *tp = mem->s_type; |
1018 | | | 1015 | |
1019 | unsigned int union_size = 0; | | 1016 | unsigned int union_size = 0; |
1020 | if (dcs->d_kind == DLK_UNION) { | | 1017 | if (dcs->d_kind == DLK_UNION) { |
1021 | union_size = dcs->d_sou_size_in_bits; | | 1018 | union_size = dcs->d_sou_size_in_bits; |
1022 | dcs->d_sou_size_in_bits = 0; | | 1019 | dcs->d_sou_size_in_bits = 0; |
1023 | } | | 1020 | } |
1024 | | | 1021 | |
1025 | if (mem->s_bitfield) { | | 1022 | if (mem->s_bitfield) { |
1026 | dcs_align(alignment_in_bits(tp), tp->t_bit_field_width); | | 1023 | dcs_align(alignment_in_bits(tp), tp->t_bit_field_width); |
1027 | // XXX: Why round down? | | 1024 | // XXX: Why round down? |
1028 | mem->u.s_member.sm_offset_in_bits = dcs->d_sou_size_in_bits | | 1025 | mem->u.s_member.sm_offset_in_bits = dcs->d_sou_size_in_bits |
1029 | - dcs->d_sou_size_in_bits % size_in_bits(tp->t_tspec); | | 1026 | - dcs->d_sou_size_in_bits % size_in_bits(tp->t_tspec); |
1030 | tp->t_bit_field_offset = dcs->d_sou_size_in_bits | | 1027 | tp->t_bit_field_offset = dcs->d_sou_size_in_bits |
1031 | - mem->u.s_member.sm_offset_in_bits; | | 1028 | - mem->u.s_member.sm_offset_in_bits; |
1032 | dcs->d_sou_size_in_bits += tp->t_bit_field_width; | | 1029 | dcs->d_sou_size_in_bits += tp->t_bit_field_width; |
1033 | } else { | | 1030 | } else { |
1034 | dcs_align(alignment_in_bits(tp), 0); | | 1031 | dcs_align(alignment_in_bits(tp), 0); |
1035 | mem->u.s_member.sm_offset_in_bits = dcs->d_sou_size_in_bits; | | 1032 | mem->u.s_member.sm_offset_in_bits = dcs->d_sou_size_in_bits; |
1036 | dcs->d_sou_size_in_bits += type_size_in_bits(tp); | | 1033 | dcs->d_sou_size_in_bits += type_size_in_bits(tp); |
1037 | } | | 1034 | } |
1038 | | | 1035 | |
1039 | if (union_size > dcs->d_sou_size_in_bits) | | 1036 | if (union_size > dcs->d_sou_size_in_bits) |
1040 | dcs->d_sou_size_in_bits = union_size; | | 1037 | dcs->d_sou_size_in_bits = union_size; |
1041 | } | | 1038 | } |
1042 | | | 1039 | |
1043 | sym_t * | | 1040 | sym_t * |
1044 | declare_unnamed_member(void) | | 1041 | declare_unnamed_member(void) |
1045 | { | | 1042 | { |
1046 | | | 1043 | |
1047 | sym_t *mem = block_zero_alloc(sizeof(*mem), "sym"); | | 1044 | sym_t *mem = block_zero_alloc(sizeof(*mem), "sym"); |
1048 | mem->s_name = unnamed; | | 1045 | mem->s_name = unnamed; |
1049 | mem->s_kind = FMEMBER; | | 1046 | mem->s_kind = FMEMBER; |
1050 | mem->s_scl = dcs->d_kind == DLK_STRUCT ? STRUCT_MEMBER : UNION_MEMBER; | | 1047 | mem->s_scl = dcs->d_kind == DLK_STRUCT ? STRUCT_MEMBER : UNION_MEMBER; |
1051 | mem->s_block_level = -1; | | 1048 | mem->s_block_level = -1; |
1052 | mem->s_type = dcs->d_type; | | 1049 | mem->s_type = dcs->d_type; |
1053 | mem->u.s_member.sm_containing_type = dcs->d_tag_type->t_sou; | | 1050 | mem->u.s_member.sm_containing_type = dcs->d_tag_type->t_sou; |
1054 | | | 1051 | |
1055 | dcs_add_member(mem); | | 1052 | dcs_add_member(mem); |
1056 | suppress_bitfieldtype = false; | | 1053 | suppress_bitfieldtype = false; |
1057 | return mem; | | 1054 | return mem; |
1058 | } | | 1055 | } |
1059 | | | 1056 | |
1060 | sym_t * | | 1057 | sym_t * |
1061 | declare_member(sym_t *dsym) | | 1058 | declare_member(sym_t *dsym) |
1062 | { | | 1059 | { |
1063 | | | 1060 | |
1064 | lint_assert(is_member(dsym)); | | 1061 | lint_assert(is_member(dsym)); |
1065 | | | 1062 | |
1066 | if (dcs->d_redeclared_symbol != NULL) { | | 1063 | if (dcs->d_redeclared_symbol != NULL) { |
1067 | lint_assert(is_member(dcs->d_redeclared_symbol)); | | 1064 | lint_assert(is_member(dcs->d_redeclared_symbol)); |
1068 | | | 1065 | |
1069 | if (dsym->u.s_member.sm_containing_type == | | 1066 | if (dsym->u.s_member.sm_containing_type == |
1070 | dcs->d_redeclared_symbol->u.s_member.sm_containing_type) { | | 1067 | dcs->d_redeclared_symbol->u.s_member.sm_containing_type) { |
1071 | /* duplicate member name '%s' */ | | 1068 | /* duplicate member name '%s' */ |
1072 | error(33, dsym->s_name); | | 1069 | error(33, dsym->s_name); |
1073 | rmsym(dcs->d_redeclared_symbol); | | 1070 | rmsym(dcs->d_redeclared_symbol); |
1074 | } | | 1071 | } |
1075 | } | | 1072 | } |
1076 | | | 1073 | |
1077 | check_type(dsym); | | 1074 | check_type(dsym); |
1078 | | | 1075 | |
1079 | type_t *tp = dsym->s_type; | | 1076 | type_t *tp = dsym->s_type; |
1080 | tspec_t t = tp->t_tspec; | | 1077 | tspec_t t = tp->t_tspec; |
1081 | if (dsym->s_bitfield) | | 1078 | if (dsym->s_bitfield) |
1082 | check_bit_field(dsym, &t, &tp); | | 1079 | check_bit_field(dsym, &t, &tp); |
1083 | else if (t == FUNC) { | | 1080 | else if (t == FUNC) { |
1084 | /* function illegal in structure or union */ | | 1081 | /* function illegal in structure or union */ |
1085 | error(38); | | 1082 | error(38); |
1086 | dsym->s_type = tp = block_derive_type(tp, t = PTR); | | 1083 | dsym->s_type = tp = block_derive_type(tp, t = PTR); |
1087 | } | | 1084 | } |
1088 | | | 1085 | |
1089 | /* | | 1086 | /* |
1090 | * bit-fields of length 0 are not warned about because length_in_bits | | 1087 | * bit-fields of length 0 are not warned about because length_in_bits |
1091 | * does not return the length of the bit-field but the length | | 1088 | * does not return the length of the bit-field but the length |
1092 | * of the type the bit-field is packed in (it's ok) | | 1089 | * of the type the bit-field is packed in (it's ok) |
1093 | */ | | 1090 | */ |
1094 | int sz = length_in_bits(dsym->s_type, dsym->s_name); | | 1091 | int sz = length_in_bits(dsym->s_type, dsym->s_name); |
1095 | if (sz == 0 && t == ARRAY && dsym->s_type->t_dim == 0) { | | 1092 | if (sz == 0 && t == ARRAY && dsym->s_type->t_dim == 0) { |
1096 | /* zero-sized array '%s' in struct is a C99 extension */ | | 1093 | /* zero-sized array '%s' in struct is a C99 extension */ |
1097 | c99ism(39, dsym->s_name); | | 1094 | c99ism(39, dsym->s_name); |
1098 | } | | 1095 | } |
1099 | | | 1096 | |
1100 | dcs_add_member(dsym); | | 1097 | dcs_add_member(dsym); |
1101 | | | 1098 | |
1102 | check_function_definition(dsym, false); | | 1099 | check_function_definition(dsym, false); |
1103 | | | 1100 | |
1104 | suppress_bitfieldtype = false; | | 1101 | suppress_bitfieldtype = false; |
1105 | | | 1102 | |
1106 | return dsym; | | 1103 | return dsym; |
1107 | } | | 1104 | } |
1108 | | | 1105 | |
1109 | /* Aligns the next structure element as required. */ | | | |
1110 | static void | | | |
1111 | dcs_align(unsigned int member_alignment, unsigned int bit_field_width) | | | |
1112 | { | | | |
1113 | | | | |
1114 | if (member_alignment > dcs->d_sou_align_in_bits) | | | |
1115 | dcs->d_sou_align_in_bits = member_alignment; | | | |
1116 | | | | |
1117 | unsigned int offset = (dcs->d_sou_size_in_bits + member_alignment - 1) | | | |
1118 | & ~(member_alignment - 1); | | | |
1119 | if (bit_field_width == 0 | | | |
1120 | || dcs->d_sou_size_in_bits + bit_field_width > offset) | | | |
1121 | dcs->d_sou_size_in_bits = offset; | | | |
1122 | } | | | |
1123 | | | | |
1124 | sym_t * | | 1106 | sym_t * |
1125 | set_bit_field_width(sym_t *dsym, int bit_field_width) | | 1107 | set_bit_field_width(sym_t *dsym, int bit_field_width) |
1126 | { | | 1108 | { |
1127 | | | 1109 | |
1128 | if (dsym == NULL) { | | 1110 | if (dsym == NULL) { |
1129 | dsym = block_zero_alloc(sizeof(*dsym), "sym"); | | 1111 | dsym = block_zero_alloc(sizeof(*dsym), "sym"); |
1130 | dsym->s_name = unnamed; | | 1112 | dsym->s_name = unnamed; |
1131 | dsym->s_kind = FMEMBER; | | 1113 | dsym->s_kind = FMEMBER; |
1132 | dsym->s_scl = STRUCT_MEMBER; | | 1114 | dsym->s_scl = STRUCT_MEMBER; |
1133 | dsym->s_type = gettyp(UINT); | | 1115 | dsym->s_type = gettyp(UINT); |
1134 | dsym->s_block_level = -1; | | 1116 | dsym->s_block_level = -1; |
1135 | } | | 1117 | } |
1136 | dsym->s_type = block_dup_type(dsym->s_type); | | 1118 | dsym->s_type = block_dup_type(dsym->s_type); |
1137 | dsym->s_type->t_bitfield = true; | | 1119 | dsym->s_type->t_bitfield = true; |
1138 | dsym->s_type->t_bit_field_width = bit_field_width; | | 1120 | dsym->s_type->t_bit_field_width = bit_field_width; |
1139 | dsym->s_bitfield = true; | | 1121 | dsym->s_bitfield = true; |
1140 | return dsym; | | 1122 | return dsym; |
1141 | } | | 1123 | } |
1142 | | | 1124 | |
1143 | void | | 1125 | void |
1144 | add_type_qualifiers(type_qualifiers *dst, type_qualifiers src) | | 1126 | add_type_qualifiers(type_qualifiers *dst, type_qualifiers src) |
1145 | { | | 1127 | { |
1146 | | | 1128 | |
1147 | if (src.tq_const && dst->tq_const) | | 1129 | if (src.tq_const && dst->tq_const) |
1148 | /* duplicate '%s' */ | | 1130 | /* duplicate '%s' */ |
1149 | warning(10, "const"); | | 1131 | warning(10, "const"); |
1150 | if (src.tq_volatile && dst->tq_volatile) | | 1132 | if (src.tq_volatile && dst->tq_volatile) |
1151 | /* duplicate '%s' */ | | 1133 | /* duplicate '%s' */ |
1152 | warning(10, "volatile"); | | 1134 | warning(10, "volatile"); |
1153 | | | 1135 | |
1154 | dst->tq_const = dst->tq_const | src.tq_const; | | 1136 | dst->tq_const = dst->tq_const | src.tq_const; |
1155 | dst->tq_restrict = dst->tq_restrict | src.tq_restrict; | | 1137 | dst->tq_restrict = dst->tq_restrict | src.tq_restrict; |
1156 | dst->tq_volatile = dst->tq_volatile | src.tq_volatile; | | 1138 | dst->tq_volatile = dst->tq_volatile | src.tq_volatile; |
1157 | dst->tq_atomic = dst->tq_atomic | src.tq_atomic; | | 1139 | dst->tq_atomic = dst->tq_atomic | src.tq_atomic; |
1158 | } | | 1140 | } |
1159 | | | 1141 | |
1160 | qual_ptr * | | 1142 | qual_ptr * |
1161 | append_qualified_pointer(qual_ptr *p1, qual_ptr *p2) | | 1143 | append_qualified_pointer(qual_ptr *p1, qual_ptr *p2) |
1162 | { | | 1144 | { |
1163 | | | 1145 | |
1164 | qual_ptr *tail = p2; | | 1146 | qual_ptr *tail = p2; |
1165 | while (tail->p_next != NULL) | | 1147 | while (tail->p_next != NULL) |
1166 | tail = tail->p_next; | | 1148 | tail = tail->p_next; |
1167 | tail->p_next = p1; | | 1149 | tail->p_next = p1; |
1168 | return p2; | | 1150 | return p2; |
1169 | } | | 1151 | } |
1170 | | | 1152 | |
1171 | static type_t * | | 1153 | static type_t * |
1172 | block_derive_pointer(type_t *stp, bool is_const, bool is_volatile) | | 1154 | block_derive_pointer(type_t *stp, bool is_const, bool is_volatile) |
1173 | { | | 1155 | { |
1174 | | | 1156 | |
1175 | type_t *tp = block_derive_type(stp, PTR); | | 1157 | type_t *tp = block_derive_type(stp, PTR); |
1176 | tp->t_const = is_const; | | 1158 | tp->t_const = is_const; |
1177 | tp->t_volatile = is_volatile; | | 1159 | tp->t_volatile = is_volatile; |
1178 | return tp; | | 1160 | return tp; |
1179 | } | | 1161 | } |
1180 | | | 1162 | |
1181 | /* | | 1163 | /* |
1182 | * The following 3 functions extend the type of a declarator with | | 1164 | * The following 3 functions extend the type of a declarator with |
1183 | * pointer, function and array types. | | 1165 | * pointer, function and array types. |
1184 | * | | 1166 | * |
1185 | * The current type is the type built by dcs_end_type (dcs->d_type) and | | 1167 | * The current type is the type built by dcs_end_type (dcs->d_type) and |
1186 | * pointer, function and array types already added for this | | 1168 | * pointer, function and array types already added for this |
1187 | * declarator. The new type extension is inserted between both. | | 1169 | * declarator. The new type extension is inserted between both. |
1188 | */ | | 1170 | */ |
1189 | sym_t * | | 1171 | sym_t * |
1190 | add_pointer(sym_t *decl, qual_ptr *p) | | 1172 | add_pointer(sym_t *decl, qual_ptr *p) |
1191 | { | | 1173 | { |
1192 | | | 1174 | |
1193 | debug_dcs(false); | | 1175 | debug_dcs(false); |
1194 | | | 1176 | |
1195 | type_t **tpp = &decl->s_type; | | 1177 | type_t **tpp = &decl->s_type; |
1196 | while (*tpp != NULL && *tpp != dcs->d_type) | | 1178 | while (*tpp != NULL && *tpp != dcs->d_type) |
1197 | tpp = &(*tpp)->t_subt; | | 1179 | tpp = &(*tpp)->t_subt; |
1198 | if (*tpp == NULL) { | | 1180 | if (*tpp == NULL) { |
1199 | debug_step("add_pointer: unchanged '%s'", | | 1181 | debug_step("add_pointer: unchanged '%s'", |
1200 | type_name(decl->s_type)); | | 1182 | type_name(decl->s_type)); |
1201 | return decl; | | 1183 | return decl; |
1202 | } | | 1184 | } |
1203 | | | 1185 | |
1204 | while (p != NULL) { | | 1186 | while (p != NULL) { |
1205 | *tpp = block_derive_pointer(dcs->d_type, | | 1187 | *tpp = block_derive_pointer(dcs->d_type, |
1206 | p->qualifiers.tq_const, p->qualifiers.tq_volatile); | | 1188 | p->qualifiers.tq_const, p->qualifiers.tq_volatile); |
1207 | | | 1189 | |
1208 | tpp = &(*tpp)->t_subt; | | 1190 | tpp = &(*tpp)->t_subt; |
1209 | | | 1191 | |
1210 | qual_ptr *next = p->p_next; | | 1192 | qual_ptr *next = p->p_next; |
1211 | free(p); | | 1193 | free(p); |
1212 | p = next; | | 1194 | p = next; |
1213 | } | | 1195 | } |
1214 | debug_step("add_pointer: '%s'", type_name(decl->s_type)); | | 1196 | debug_step("add_pointer: '%s'", type_name(decl->s_type)); |
1215 | return decl; | | 1197 | return decl; |
1216 | } | | 1198 | } |
1217 | | | 1199 | |
1218 | static type_t * | | 1200 | static type_t * |
1219 | block_derive_array(type_t *stp, bool dim, int len) | | 1201 | block_derive_array(type_t *stp, bool dim, int len) |
1220 | { | | 1202 | { |
1221 | | | 1203 | |
1222 | type_t *tp = block_derive_type(stp, ARRAY); | | 1204 | type_t *tp = block_derive_type(stp, ARRAY); |
1223 | tp->t_dim = len; | | 1205 | tp->t_dim = len; |
1224 | | | 1206 | |
1225 | #if 0 | | 1207 | #if 0 |
1226 | /* | | 1208 | /* |
1227 | * As of 2022-04-03, the implementation of the type parser (see | | 1209 | * As of 2022-04-03, the implementation of the type parser (see |
1228 | * add_function, add_array, add_pointer) is strange. When it sees | | 1210 | * add_function, add_array, add_pointer) is strange. When it sees |
1229 | * the type 'void *b[4]', it first creates 'void b[4]' and only later | | 1211 | * the type 'void *b[4]', it first creates 'void b[4]' and only later |
1230 | * inserts the '*' in the middle of the type. Late modifications like | | 1212 | * inserts the '*' in the middle of the type. Late modifications like |
1231 | * these should not be done at all, instead the parser should be fixed | | 1213 | * these should not be done at all, instead the parser should be fixed |
1232 | * to process the type names in the proper syntactical order. | | 1214 | * to process the type names in the proper syntactical order. |
1233 | * | | 1215 | * |
1234 | * Since the intermediate type would be an array of void, but the | | 1216 | * Since the intermediate type would be an array of void, but the |
1235 | * final type is valid, this check cannot be enabled yet. | | 1217 | * final type is valid, this check cannot be enabled yet. |
1236 | */ | | 1218 | */ |
1237 | if (stp->t_tspec == VOID) { | | 1219 | if (stp->t_tspec == VOID) { |
1238 | /* array of incomplete type */ | | 1220 | /* array of incomplete type */ |
1239 | error(301); | | 1221 | error(301); |
1240 | tp->t_subt = gettyp(CHAR); | | 1222 | tp->t_subt = gettyp(CHAR); |
1241 | } | | 1223 | } |
1242 | #endif | | 1224 | #endif |
1243 | if (len < 0) { | | 1225 | if (len < 0) { |
1244 | /* negative array dimension (%d) */ | | 1226 | /* negative array dimension (%d) */ |
1245 | error(20, len); | | 1227 | error(20, len); |
1246 | } else if (len == 0 && dim) { | | 1228 | } else if (len == 0 && dim) { |
1247 | /* zero sized array is a C99 extension */ | | 1229 | /* zero sized array is a C99 extension */ |
1248 | c99ism(322); | | 1230 | c99ism(322); |
1249 | } else if (len == 0 && !dim) | | 1231 | } else if (len == 0 && !dim) |
1250 | tp->t_incomplete_array = true; | | 1232 | tp->t_incomplete_array = true; |
1251 | | | 1233 | |
1252 | return tp; | | 1234 | return tp; |
1253 | } | | 1235 | } |
1254 | | | 1236 | |
1255 | /* | | 1237 | /* |
1256 | * If a dimension was specified, dim is true, otherwise false | | 1238 | * If a dimension was specified, dim is true, otherwise false |
1257 | * n is the specified dimension | | 1239 | * n is the specified dimension |
1258 | */ | | 1240 | */ |
1259 | sym_t * | | 1241 | sym_t * |
1260 | add_array(sym_t *decl, bool dim, int n) | | 1242 | add_array(sym_t *decl, bool dim, int n) |
1261 | { | | 1243 | { |
1262 | | | 1244 | |
1263 | debug_dcs(false); | | 1245 | debug_dcs(false); |
1264 | | | 1246 | |
1265 | type_t **tpp = &decl->s_type; | | 1247 | type_t **tpp = &decl->s_type; |
1266 | while (*tpp != NULL && *tpp != dcs->d_type) | | 1248 | while (*tpp != NULL && *tpp != dcs->d_type) |
1267 | tpp = &(*tpp)->t_subt; | | 1249 | tpp = &(*tpp)->t_subt; |
1268 | if (*tpp == NULL) { | | 1250 | if (*tpp == NULL) { |
1269 | debug_step("add_array: unchanged '%s'", | | 1251 | debug_step("add_array: unchanged '%s'", |
1270 | type_name(decl->s_type)); | | 1252 | type_name(decl->s_type)); |
1271 | return decl; | | 1253 | return decl; |
1272 | } | | 1254 | } |
1273 | | | 1255 | |
1274 | *tpp = block_derive_array(dcs->d_type, dim, n); | | 1256 | *tpp = block_derive_array(dcs->d_type, dim, n); |
1275 | | | 1257 | |
1276 | debug_step("add_array: '%s'", type_name(decl->s_type)); | | 1258 | debug_step("add_array: '%s'", type_name(decl->s_type)); |
1277 | return decl; | | 1259 | return decl; |
1278 | } | | 1260 | } |
1279 | | | 1261 | |
1280 | static type_t * | | 1262 | static type_t * |
1281 | block_derive_function(type_t *ret, bool proto, sym_t *args, bool vararg) | | 1263 | block_derive_function(type_t *ret, bool proto, sym_t *args, bool vararg) |
1282 | { | | 1264 | { |
1283 | | | 1265 | |
1284 | type_t *tp = block_derive_type(ret, FUNC); | | 1266 | type_t *tp = block_derive_type(ret, FUNC); |
1285 | tp->t_proto = proto; | | 1267 | tp->t_proto = proto; |
1286 | if (proto) | | 1268 | if (proto) |
1287 | tp->t_args = args; | | 1269 | tp->t_args = args; |
1288 | tp->t_vararg = vararg; | | 1270 | tp->t_vararg = vararg; |
1289 | return tp; | | 1271 | return tp; |
1290 | } | | 1272 | } |
1291 | | | 1273 | |
| | | 1274 | static void |
| | | 1275 | check_prototype_parameters(sym_t *args) |
| | | 1276 | { |
| | | 1277 | |
| | | 1278 | for (sym_t *sym = dcs->d_first_dlsym; |
| | | 1279 | sym != NULL; sym = sym->s_level_next) { |
| | | 1280 | scl_t sc = sym->s_scl; |
| | | 1281 | if (sc == STRUCT_TAG || sc == UNION_TAG || sc == ENUM_TAG) { |
| | | 1282 | /* dubious tag declaration '%s %s' */ |
| | | 1283 | warning(85, storage_class_name(sc), sym->s_name); |
| | | 1284 | } |
| | | 1285 | } |
| | | 1286 | |
| | | 1287 | for (sym_t *arg = args; arg != NULL; arg = arg->s_next) { |
| | | 1288 | if (arg->s_type->t_tspec == VOID && |
| | | 1289 | !(arg == args && arg->s_next == NULL)) { |
| | | 1290 | /* void must be sole parameter */ |
| | | 1291 | error(60); |
| | | 1292 | arg->s_type = gettyp(INT); |
| | | 1293 | } |
| | | 1294 | } |
| | | 1295 | } |
| | | 1296 | |
| | | 1297 | static void |
| | | 1298 | old_style_function(sym_t *decl, sym_t *args) |
| | | 1299 | { |
| | | 1300 | |
| | | 1301 | /* |
| | | 1302 | * Remember the list of parameters only if this really seems to be a |
| | | 1303 | * function definition. |
| | | 1304 | */ |
| | | 1305 | if (dcs->d_enclosing->d_kind == DLK_EXTERN && |
| | | 1306 | decl->s_type == dcs->d_enclosing->d_type) { |
| | | 1307 | /* |
| | | 1308 | * Assume that this becomes a function definition. If not, it |
| | | 1309 | * will be corrected in check_function_definition. |
| | | 1310 | */ |
| | | 1311 | if (args != NULL) { |
| | | 1312 | decl->s_osdef = true; |
| | | 1313 | decl->u.s_old_style_args = args; |
| | | 1314 | } |
| | | 1315 | } else { |
| | | 1316 | if (args != NULL) |
| | | 1317 | /* function prototype parameters must have types */ |
| | | 1318 | warning(62); |
| | | 1319 | } |
| | | 1320 | } |
| | | 1321 | |
1292 | sym_t * | | 1322 | sym_t * |
1293 | add_function(sym_t *decl, struct parameter_list params) | | 1323 | add_function(sym_t *decl, struct parameter_list params) |
1294 | { | | 1324 | { |
1295 | | | 1325 | |
1296 | debug_enter(); | | 1326 | debug_enter(); |
1297 | debug_dcs(true); | | 1327 | debug_dcs(true); |
1298 | debug_sym("decl: ", decl, "\n"); | | 1328 | debug_sym("decl: ", decl, "\n"); |
1299 | #ifdef DEBUG | | 1329 | #ifdef DEBUG |
1300 | for (const sym_t *arg = params.first; arg != NULL; arg = arg->s_next) | | 1330 | for (const sym_t *arg = params.first; arg != NULL; arg = arg->s_next) |
1301 | debug_sym("arg: ", arg, "\n"); | | 1331 | debug_sym("arg: ", arg, "\n"); |
1302 | #endif | | 1332 | #endif |
1303 | | | 1333 | |
1304 | if (params.prototype) { | | 1334 | if (params.prototype) { |
1305 | if (!allow_c90) | | 1335 | if (!allow_c90) |
1306 | /* function prototypes are illegal in traditional C */ | | 1336 | /* function prototypes are illegal in traditional C */ |
1307 | warning(270); | | 1337 | warning(270); |
1308 | check_prototype_parameters(params.first); | | 1338 | check_prototype_parameters(params.first); |
1309 | if (params.first != NULL | | 1339 | if (params.first != NULL |
1310 | && params.first->s_type->t_tspec == VOID) | | 1340 | && params.first->s_type->t_tspec == VOID) |
1311 | params.first = NULL; | | 1341 | params.first = NULL; |
1312 | } else | | 1342 | } else |
1313 | old_style_function(decl, params.first); | | 1343 | old_style_function(decl, params.first); |
1314 | | | 1344 | |
1315 | /* | | 1345 | /* |
1316 | * The symbols are removed from the symbol table by | | 1346 | * The symbols are removed from the symbol table by |
1317 | * end_declaration_level after add_function. To be able to restore | | 1347 | * end_declaration_level after add_function. To be able to restore |
1318 | * them if this is a function definition, a pointer to the list of | | 1348 | * them if this is a function definition, a pointer to the list of |
1319 | * all symbols is stored in dcs->d_enclosing->d_func_proto_syms. Also, | | 1349 | * all symbols is stored in dcs->d_enclosing->d_func_proto_syms. Also, |
1320 | * a list of the arguments (concatenated by s_next) is stored in | | 1350 | * a list of the arguments (concatenated by s_next) is stored in |
1321 | * dcs->d_enclosing->d_func_args. (dcs->d_enclosing must be used | | 1351 | * dcs->d_enclosing->d_func_args. (dcs->d_enclosing must be used |
1322 | * because *dcs is the declaration stack element created for the list | | 1352 | * because *dcs is the declaration stack element created for the list |
1323 | * of params and is removed after add_function.) | | 1353 | * of params and is removed after add_function.) |
1324 | */ | | 1354 | */ |
1325 | if (dcs->d_enclosing->d_kind == DLK_EXTERN && | | 1355 | if (dcs->d_enclosing->d_kind == DLK_EXTERN && |
1326 | decl->s_type == dcs->d_enclosing->d_type) { | | 1356 | decl->s_type == dcs->d_enclosing->d_type) { |
1327 | dcs->d_enclosing->d_func_proto_syms = dcs->d_first_dlsym; | | 1357 | dcs->d_enclosing->d_func_proto_syms = dcs->d_first_dlsym; |
1328 | dcs->d_enclosing->d_func_args = params.first; | | 1358 | dcs->d_enclosing->d_func_args = params.first; |
1329 | } | | 1359 | } |
1330 | | | 1360 | |
1331 | /* | | 1361 | /* |
1332 | * XXX: What is this code doing on a semantic level, and why? | | 1362 | * XXX: What is this code doing on a semantic level, and why? |
1333 | * Returning decl leads to the wrong function types in msg_347. | | 1363 | * Returning decl leads to the wrong function types in msg_347. |
1334 | */ | | 1364 | */ |
1335 | type_t **tpp = &decl->s_type; | | 1365 | type_t **tpp = &decl->s_type; |
1336 | if (*tpp == NULL) | | 1366 | if (*tpp == NULL) |
1337 | decl->s_type = dcs->d_enclosing->d_type; | | 1367 | decl->s_type = dcs->d_enclosing->d_type; |
1338 | while (*tpp != NULL && *tpp != dcs->d_enclosing->d_type) | | 1368 | while (*tpp != NULL && *tpp != dcs->d_enclosing->d_type) |
1339 | /* | | 1369 | /* |
1340 | * XXX: accessing INT->t_subt feels strange, even though it | | 1370 | * XXX: accessing INT->t_subt feels strange, even though it |
1341 | * may even be guaranteed to be NULL. | | 1371 | * may even be guaranteed to be NULL. |
1342 | */ | | 1372 | */ |
1343 | tpp = &(*tpp)->t_subt; | | 1373 | tpp = &(*tpp)->t_subt; |
1344 | if (*tpp == NULL) { | | 1374 | if (*tpp == NULL) { |
1345 | debug_step("add_function: unchanged '%s'", | | 1375 | debug_step("add_function: unchanged '%s'", |
1346 | type_name(decl->s_type)); | | 1376 | type_name(decl->s_type)); |
1347 | debug_leave(); | | 1377 | debug_leave(); |
1348 | return decl; /* see msg_347 */ | | 1378 | return decl; /* see msg_347 */ |
1349 | } | | 1379 | } |
1350 | | | 1380 | |
1351 | *tpp = block_derive_function(dcs->d_enclosing->d_type, | | 1381 | *tpp = block_derive_function(dcs->d_enclosing->d_type, |
1352 | params.prototype, params.first, params.vararg); | | 1382 | params.prototype, params.first, params.vararg); |
1353 | | | 1383 | |
1354 | debug_step("add_function: '%s'", type_name(decl->s_type)); | | 1384 | debug_step("add_function: '%s'", type_name(decl->s_type)); |
1355 | debug_dcs(true); | | 1385 | debug_dcs(true); |
1356 | debug_leave(); | | 1386 | debug_leave(); |
1357 | return decl; | | 1387 | return decl; |
1358 | } | | 1388 | } |
1359 | | | 1389 | |
1360 | static void | | 1390 | /* |
1361 | check_prototype_parameters(sym_t *args) | | 1391 | * In a function declaration, a list of identifiers (as opposed to a list of |
| | | 1392 | * types) is allowed only if it's also a function definition. |
| | | 1393 | */ |
| | | 1394 | void |
| | | 1395 | check_function_definition(sym_t *sym, bool msg) |
1362 | { | | 1396 | { |
1363 | | | 1397 | |
1364 | for (sym_t *sym = dcs->d_first_dlsym; | | 1398 | if (sym->s_osdef) { |
1365 | sym != NULL; sym = sym->s_level_next) { | | 1399 | if (msg) { |
1366 | scl_t sc = sym->s_scl; | | 1400 | /* incomplete or misplaced function definition */ |
1367 | if (sc == STRUCT_TAG || sc == UNION_TAG || sc == ENUM_TAG) { | | 1401 | error(22); |
1368 | /* dubious tag declaration '%s %s' */ | | | |
1369 | warning(85, storage_class_name(sc), sym->s_name); | | | |
1370 | } | | 1402 | } |
| | | 1403 | sym->s_osdef = false; |
| | | 1404 | sym->u.s_old_style_args = NULL; |
1371 | } | | 1405 | } |
1372 | | | 1406 | } |
1373 | for (sym_t *arg = args; arg != NULL; arg = arg->s_next) { | | | |
1374 | if (arg->s_type->t_tspec == VOID && | | | |
1375 | !(arg == args && arg->s_next == NULL)) { | | | |
1376 | /* void must be sole parameter */ | | | |
1377 | error(60); | | | |
1378 | arg->s_type = gettyp(INT); | | | |
1379 | } | | | |
1380 | } | | | |
1381 | } | | | |
1382 | | | | |
1383 | static void | | | |
1384 | old_style_function(sym_t *decl, sym_t *args) | | | |
1385 | { | | | |
1386 | | | | |
1387 | /* | | | |
1388 | * Remember the list of parameters only if this really seems to be a | | | |
1389 | * function definition. | | | |
1390 | */ | | | |
1391 | if (dcs->d_enclosing->d_kind == DLK_EXTERN && | | | |
1392 | decl->s_type == dcs->d_enclosing->d_type) { | | | |
1393 | /* | | | |
1394 | * Assume that this becomes a function definition. If not, it | | | |
1395 | * will be corrected in check_function_definition. | | | |
1396 | */ | | | |
1397 | if (args != NULL) { | | | |
1398 | decl->s_osdef = true; | | | |
1399 | decl->u.s_old_style_args = args; | | | |
1400 | } | | | |
1401 | } else { | | | |
1402 | if (args != NULL) | | | |
1403 | /* function prototype parameters must have types */ | | | |
1404 | warning(62); | | | |
1405 | } | | | |
1406 | } | | | |
1407 | | | | |
1408 | /* | | | |
1409 | * In a function declaration, a list of identifiers (as opposed to a list of | | | |
1410 | * types) is allowed only if it's also a function definition. | | | |
1411 | */ | | | |
1412 | void | | | |
1413 | check_function_definition(sym_t *sym, bool msg) | | | |
1414 | { | | | |
1415 | | | | |
1416 | if (sym->s_osdef) { | | | |
1417 | if (msg) { | | | |
1418 | /* incomplete or misplaced function definition */ | | | |
1419 | error(22); | | | |
1420 | } | | | |
1421 | sym->s_osdef = false; | | | |
1422 | sym->u.s_old_style_args = NULL; | | | |
1423 | } | | | |
1424 | } | | | |
1425 | | | 1407 | |
1426 | /* The symbol gets a storage class and a definedness. */ | | 1408 | /* The symbol gets a storage class and a definedness. */ |
1427 | sym_t * | | 1409 | sym_t * |
1428 | declarator_name(sym_t *sym) | | 1410 | declarator_name(sym_t *sym) |
1429 | { | | 1411 | { |
1430 | scl_t sc = NOSCL; | | 1412 | scl_t sc = NOSCL; |
1431 | | | 1413 | |
1432 | if (sym->s_scl == NOSCL) | | 1414 | if (sym->s_scl == NOSCL) |
1433 | dcs->d_redeclared_symbol = NULL; | | 1415 | dcs->d_redeclared_symbol = NULL; |
1434 | else if (sym->s_defarg) { | | 1416 | else if (sym->s_defarg) { |
1435 | sym->s_defarg = false; | | 1417 | sym->s_defarg = false; |
1436 | dcs->d_redeclared_symbol = NULL; | | 1418 | dcs->d_redeclared_symbol = NULL; |
1437 | } else { | | 1419 | } else { |
1438 | dcs->d_redeclared_symbol = sym; | | 1420 | dcs->d_redeclared_symbol = sym; |
1439 | sym = pushdown(sym); | | 1421 | sym = pushdown(sym); |
1440 | } | | 1422 | } |
1441 | | | 1423 | |
1442 | switch (dcs->d_kind) { | | 1424 | switch (dcs->d_kind) { |
1443 | case DLK_STRUCT: | | 1425 | case DLK_STRUCT: |
1444 | case DLK_UNION: | | 1426 | case DLK_UNION: |
1445 | sym->u.s_member.sm_containing_type = dcs->d_tag_type->t_sou; | | 1427 | sym->u.s_member.sm_containing_type = dcs->d_tag_type->t_sou; |
1446 | sym->s_def = DEF; | | 1428 | sym->s_def = DEF; |
1447 | sc = dcs->d_kind == DLK_STRUCT ? STRUCT_MEMBER : UNION_MEMBER; | | 1429 | sc = dcs->d_kind == DLK_STRUCT ? STRUCT_MEMBER : UNION_MEMBER; |
1448 | break; | | 1430 | break; |
1449 | case DLK_EXTERN: | | 1431 | case DLK_EXTERN: |
1450 | /* | | 1432 | /* |
1451 | * Symbols that are 'static' or without any storage class are | | 1433 | * Symbols that are 'static' or without any storage class are |
1452 | * tentatively defined. Symbols that are tentatively defined or | | 1434 | * tentatively defined. Symbols that are tentatively defined or |
1453 | * declared may later become defined if an initializer is seen | | 1435 | * declared may later become defined if an initializer is seen |
1454 | * or this is a function definition. | | 1436 | * or this is a function definition. |
1455 | */ | | 1437 | */ |
1456 | sc = dcs->d_scl; | | 1438 | sc = dcs->d_scl; |
1457 | if (sc == NOSCL || sc == THREAD_LOCAL) { | | 1439 | if (sc == NOSCL || sc == THREAD_LOCAL) { |
1458 | sc = EXTERN; | | 1440 | sc = EXTERN; |
1459 | sym->s_def = TDEF; | | 1441 | sym->s_def = TDEF; |
1460 | } else if (sc == STATIC) | | 1442 | } else if (sc == STATIC) |
1461 | sym->s_def = TDEF; | | 1443 | sym->s_def = TDEF; |
1462 | else if (sc == TYPEDEF) | | 1444 | else if (sc == TYPEDEF) |
1463 | sym->s_def = DEF; | | 1445 | sym->s_def = DEF; |
1464 | else { | | 1446 | else { |
1465 | lint_assert(sc == EXTERN); | | 1447 | lint_assert(sc == EXTERN); |
1466 | sym->s_def = DECL; | | 1448 | sym->s_def = DECL; |
1467 | } | | 1449 | } |
1468 | break; | | 1450 | break; |
1469 | case DLK_PROTO_PARAMS: | | 1451 | case DLK_PROTO_PARAMS: |
1470 | sym->s_arg = true; | | 1452 | sym->s_arg = true; |
1471 | /* FALLTHROUGH */ | | 1453 | /* FALLTHROUGH */ |
1472 | case DLK_OLD_STYLE_ARGS: | | 1454 | case DLK_OLD_STYLE_ARGS: |
1473 | if ((sc = dcs->d_scl) == NOSCL) | | 1455 | if ((sc = dcs->d_scl) == NOSCL) |
1474 | sc = AUTO; | | 1456 | sc = AUTO; |
1475 | else { | | 1457 | else { |
1476 | lint_assert(sc == REG); | | 1458 | lint_assert(sc == REG); |
1477 | sym->s_register = true; | | 1459 | sym->s_register = true; |
1478 | sc = AUTO; | | 1460 | sc = AUTO; |
1479 | } | | 1461 | } |
1480 | sym->s_def = DEF; | | 1462 | sym->s_def = DEF; |
1481 | break; | | 1463 | break; |
1482 | case DLK_AUTO: | | 1464 | case DLK_AUTO: |
1483 | if ((sc = dcs->d_scl) == NOSCL) { | | 1465 | if ((sc = dcs->d_scl) == NOSCL) { |
1484 | /* | | 1466 | /* |
1485 | * XXX somewhat ugly because we don't know whether this | | 1467 | * XXX somewhat ugly because we don't know whether this |
1486 | * is AUTO or EXTERN (functions). If we are wrong, it | | 1468 | * is AUTO or EXTERN (functions). If we are wrong, it |
1487 | * must be corrected in declare_local, when the | | 1469 | * must be corrected in declare_local, when the |
1488 | * necessary type information is available. | | 1470 | * necessary type information is available. |
1489 | */ | | 1471 | */ |
1490 | sc = AUTO; | | 1472 | sc = AUTO; |
1491 | sym->s_def = DEF; | | 1473 | sym->s_def = DEF; |
1492 | } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF | | 1474 | } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF |
1493 | || sc == THREAD_LOCAL) | | 1475 | || sc == THREAD_LOCAL) |
1494 | sym->s_def = DEF; | | 1476 | sym->s_def = DEF; |
1495 | else if (sc == REG) { | | 1477 | else if (sc == REG) { |
1496 | sym->s_register = true; | | 1478 | sym->s_register = true; |
1497 | sc = AUTO; | | 1479 | sc = AUTO; |
1498 | sym->s_def = DEF; | | 1480 | sym->s_def = DEF; |
1499 | } else { | | 1481 | } else { |
1500 | lint_assert(sc == EXTERN); | | 1482 | lint_assert(sc == EXTERN); |
1501 | sym->s_def = DECL; | | 1483 | sym->s_def = DECL; |
1502 | } | | 1484 | } |
1503 | break; | | 1485 | break; |
1504 | default: | | 1486 | default: |
1505 | lint_assert(dcs->d_kind == DLK_ABSTRACT); | | 1487 | lint_assert(dcs->d_kind == DLK_ABSTRACT); |
1506 | /* try to continue after syntax errors */ | | 1488 | /* try to continue after syntax errors */ |
1507 | sc = NOSCL; | | 1489 | sc = NOSCL; |
1508 | } | | 1490 | } |
1509 | sym->s_scl = sc; | | 1491 | sym->s_scl = sc; |
1510 | | | 1492 | |
1511 | sym->s_type = dcs->d_type; | | 1493 | sym->s_type = dcs->d_type; |
1512 | | | 1494 | |
1513 | dcs->d_func_proto_syms = NULL; | | 1495 | dcs->d_func_proto_syms = NULL; |
1514 | | | 1496 | |
1515 | return sym; | | 1497 | return sym; |
1516 | } | | 1498 | } |
1517 | | | 1499 | |
1518 | sym_t * | | 1500 | sym_t * |
1519 | old_style_function_parameter_name(sym_t *sym) | | 1501 | old_style_function_parameter_name(sym_t *sym) |
1520 | { | | 1502 | { |
1521 | | | 1503 | |
1522 | if (sym->s_scl != NOSCL) { | | 1504 | if (sym->s_scl != NOSCL) { |
1523 | if (block_level == sym->s_block_level) { | | 1505 | if (block_level == sym->s_block_level) { |
1524 | /* redeclaration of formal parameter '%s' */ | | 1506 | /* redeclaration of formal parameter '%s' */ |
1525 | error(21, sym->s_name); | | 1507 | error(21, sym->s_name); |
1526 | lint_assert(sym->s_defarg); | | 1508 | lint_assert(sym->s_defarg); |
1527 | } | | 1509 | } |
1528 | sym = pushdown(sym); | | 1510 | sym = pushdown(sym); |
1529 | } | | 1511 | } |
1530 | sym->s_type = gettyp(INT); | | 1512 | sym->s_type = gettyp(INT); |
1531 | sym->s_scl = AUTO; | | 1513 | sym->s_scl = AUTO; |
1532 | sym->s_def = DEF; | | 1514 | sym->s_def = DEF; |
1533 | sym->s_defarg = sym->s_arg = true; | | 1515 | sym->s_defarg = sym->s_arg = true; |
1534 | return sym; | | 1516 | return sym; |
1535 | } | | 1517 | } |
1536 | | | 1518 | |
1537 | /*- | | 1519 | /*- |
| | | 1520 | * Checks all possible cases of tag redeclarations. |
| | | 1521 | * |
| | | 1522 | * decl whether T_LBRACE follows |
| | | 1523 | * semi whether T_SEMI follows |
| | | 1524 | */ |
| | | 1525 | static sym_t * |
| | | 1526 | new_tag(sym_t *tag, scl_t scl, bool decl, bool semi) |
| | | 1527 | { |
| | | 1528 | |
| | | 1529 | if (tag->s_block_level < block_level) { |
| | | 1530 | if (semi) { |
| | | 1531 | /* "struct a;" */ |
| | | 1532 | if (allow_c90) { |
| | | 1533 | /* XXX: Why is this warning suppressed in C90 mode? */ |
| | | 1534 | if (allow_trad || allow_c99) |
| | | 1535 | /* declaration of '%s %s' intro... */ |
| | | 1536 | warning(44, storage_class_name(scl), |
| | | 1537 | tag->s_name); |
| | | 1538 | tag = pushdown(tag); |
| | | 1539 | } else if (tag->s_scl != scl) { |
| | | 1540 | /* base type is really '%s %s' */ |
| | | 1541 | warning(45, storage_class_name(tag->s_scl), |
| | | 1542 | tag->s_name); |
| | | 1543 | } |
| | | 1544 | dcs->d_enclosing->d_nonempty_decl = true; |
| | | 1545 | } else if (decl) { |
| | | 1546 | /* "struct a { ... } " */ |
| | | 1547 | if (hflag) |
| | | 1548 | /* redefinition of '%s' hides earlier one */ |
| | | 1549 | warning(43, tag->s_name); |
| | | 1550 | tag = pushdown(tag); |
| | | 1551 | dcs->d_enclosing->d_nonempty_decl = true; |
| | | 1552 | } else if (tag->s_scl != scl) { |
| | | 1553 | /* base type is really '%s %s' */ |
| | | 1554 | warning(45, storage_class_name(tag->s_scl), |
| | | 1555 | tag->s_name); |
| | | 1556 | /* XXX: Why is this warning suppressed in C90 mode? */ |
| | | 1557 | if (allow_trad || allow_c99) { |
| | | 1558 | /* declaration of '%s %s' introduces ... */ |
| | | 1559 | warning(44, storage_class_name(scl), |
| | | 1560 | tag->s_name); |
| | | 1561 | } |
| | | 1562 | tag = pushdown(tag); |
| | | 1563 | dcs->d_enclosing->d_nonempty_decl = true; |
| | | 1564 | } |
| | | 1565 | } else { |
| | | 1566 | if (tag->s_scl != scl || |
| | | 1567 | (decl && !is_incomplete(tag->s_type))) { |
| | | 1568 | /* %s tag '%s' redeclared as %s */ |
| | | 1569 | error(46, storage_class_name(tag->s_scl), |
| | | 1570 | tag->s_name, storage_class_name(scl)); |
| | | 1571 | print_previous_declaration(tag); |
| | | 1572 | tag = pushdown(tag); |
| | | 1573 | dcs->d_enclosing->d_nonempty_decl = true; |
| | | 1574 | } else if (semi || decl) |
| | | 1575 | dcs->d_enclosing->d_nonempty_decl = true; |
| | | 1576 | } |
| | | 1577 | return tag; |
| | | 1578 | } |
| | | 1579 | |
| | | 1580 | /*- |
1538 | * tag the symbol table entry of the tag | | 1581 | * tag the symbol table entry of the tag |
1539 | * kind the kind of the tag (STRUCT/UNION/ENUM) | | 1582 | * kind the kind of the tag (STRUCT/UNION/ENUM) |
1540 | * decl whether the tag type will be completed in this declaration | | 1583 | * decl whether the tag type will be completed in this declaration |
1541 | * (when the following token is T_LBRACE) | | 1584 | * (when the following token is T_LBRACE) |
1542 | * semi whether the following token is T_SEMI | | 1585 | * semi whether the following token is T_SEMI |
1543 | */ | | 1586 | */ |
1544 | type_t * | | 1587 | type_t * |
1545 | make_tag_type(sym_t *tag, tspec_t kind, bool decl, bool semi) | | 1588 | make_tag_type(sym_t *tag, tspec_t kind, bool decl, bool semi) |
1546 | { | | 1589 | { |
1547 | scl_t scl; | | 1590 | scl_t scl; |
1548 | type_t *tp; | | 1591 | type_t *tp; |
1549 | | | 1592 | |
1550 | if (kind == STRUCT) | | 1593 | if (kind == STRUCT) |
1551 | scl = STRUCT_TAG; | | 1594 | scl = STRUCT_TAG; |
1552 | else if (kind == UNION) | | 1595 | else if (kind == UNION) |
1553 | scl = UNION_TAG; | | 1596 | scl = UNION_TAG; |
1554 | else { | | 1597 | else { |
1555 | lint_assert(kind == ENUM); | | 1598 | lint_assert(kind == ENUM); |
1556 | scl = ENUM_TAG; | | 1599 | scl = ENUM_TAG; |
1557 | } | | 1600 | } |
1558 | | | 1601 | |
1559 | if (tag != NULL) { | | 1602 | if (tag != NULL) { |
1560 | if (tag->s_scl != NOSCL) | | 1603 | if (tag->s_scl != NOSCL) |
1561 | tag = new_tag(tag, scl, decl, semi); | | 1604 | tag = new_tag(tag, scl, decl, semi); |
1562 | else { | | 1605 | else { |
1563 | /* a new tag, no empty declaration */ | | 1606 | /* a new tag, no empty declaration */ |
1564 | dcs->d_enclosing->d_nonempty_decl = true; | | 1607 | dcs->d_enclosing->d_nonempty_decl = true; |
1565 | if (scl == ENUM_TAG && !decl) { | | 1608 | if (scl == ENUM_TAG && !decl) { |
1566 | /* TODO: Make this an error in C99 mode as well. */ | | 1609 | /* TODO: Make this an error in C99 mode as well. */ |
1567 | if (allow_c90 && | | 1610 | if (allow_c90 && |
1568 | ((!allow_trad && !allow_c99) || pflag)) | | 1611 | ((!allow_trad && !allow_c99) || pflag)) |
1569 | /* forward reference to enum type */ | | 1612 | /* forward reference to enum type */ |
1570 | warning(42); | | 1613 | warning(42); |
1571 | } | | 1614 | } |
1572 | } | | 1615 | } |
1573 | if (tag->s_scl == NOSCL) { | | 1616 | if (tag->s_scl == NOSCL) { |
1574 | tag->s_scl = scl; | | 1617 | tag->s_scl = scl; |
1575 | tag->s_type = tp = | | 1618 | tag->s_type = tp = |
1576 | block_zero_alloc(sizeof(*tp), "type"); | | 1619 | block_zero_alloc(sizeof(*tp), "type"); |
1577 | tp->t_packed = dcs->d_packed; | | 1620 | tp->t_packed = dcs->d_packed; |
1578 | } else | | 1621 | } else |
1579 | tp = tag->s_type; | | 1622 | tp = tag->s_type; |
1580 | | | 1623 | |
1581 | } else { | | 1624 | } else { |
1582 | tag = block_zero_alloc(sizeof(*tag), "sym"); | | 1625 | tag = block_zero_alloc(sizeof(*tag), "sym"); |
1583 | tag->s_name = unnamed; | | 1626 | tag->s_name = unnamed; |
1584 | tag->s_def_pos = unique_curr_pos(); | | 1627 | tag->s_def_pos = unique_curr_pos(); |
1585 | tag->s_kind = FTAG; | | 1628 | tag->s_kind = FTAG; |
1586 | tag->s_scl = scl; | | 1629 | tag->s_scl = scl; |
1587 | tag->s_block_level = -1; | | 1630 | tag->s_block_level = -1; |
1588 | tag->s_type = tp = block_zero_alloc(sizeof(*tp), "type"); | | 1631 | tag->s_type = tp = block_zero_alloc(sizeof(*tp), "type"); |
1589 | tp->t_packed = dcs->d_packed; | | 1632 | tp->t_packed = dcs->d_packed; |
1590 | dcs->d_enclosing->d_nonempty_decl = true; | | 1633 | dcs->d_enclosing->d_nonempty_decl = true; |
1591 | } | | 1634 | } |
1592 | | | 1635 | |
1593 | if (tp->t_tspec == NO_TSPEC) { | | 1636 | if (tp->t_tspec == NO_TSPEC) { |
1594 | tp->t_tspec = kind; | | 1637 | tp->t_tspec = kind; |
1595 | if (kind != ENUM) { | | 1638 | if (kind != ENUM) { |
1596 | tp->t_sou = block_zero_alloc(sizeof(*tp->t_sou), | | 1639 | tp->t_sou = block_zero_alloc(sizeof(*tp->t_sou), |
1597 | "struct_or_union"); | | 1640 | "struct_or_union"); |
1598 | tp->t_sou->sou_align_in_bits = CHAR_SIZE; | | 1641 | tp->t_sou->sou_align_in_bits = CHAR_SIZE; |
1599 | tp->t_sou->sou_tag = tag; | | 1642 | tp->t_sou->sou_tag = tag; |
1600 | tp->t_sou->sou_incomplete = true; | | 1643 | tp->t_sou->sou_incomplete = true; |
1601 | } else { | | 1644 | } else { |
1602 | tp->t_is_enum = true; | | 1645 | tp->t_is_enum = true; |
1603 | tp->t_enum = block_zero_alloc(sizeof(*tp->t_enum), | | 1646 | tp->t_enum = block_zero_alloc(sizeof(*tp->t_enum), |
1604 | "enumeration"); | | 1647 | "enumeration"); |
1605 | tp->t_enum->en_tag = tag; | | 1648 | tp->t_enum->en_tag = tag; |
1606 | tp->t_enum->en_incomplete = true; | | 1649 | tp->t_enum->en_incomplete = true; |
1607 | } | | 1650 | } |
1608 | } | | 1651 | } |
1609 | return tp; | | 1652 | return tp; |
1610 | } | | 1653 | } |
1611 | | | 1654 | |
1612 | /*- | | | |
1613 | * Checks all possible cases of tag redeclarations. | | | |
1614 | * | | | |
1615 | * decl whether T_LBRACE follows | | | |
1616 | * semi whether T_SEMI follows | | | |
1617 | */ | | | |
1618 | static sym_t * | | | |
1619 | new_tag(sym_t *tag, scl_t scl, bool decl, bool semi) | | | |
1620 | { | | | |
1621 | | | | |
1622 | if (tag->s_block_level < block_level) { | | | |
1623 | if (semi) { | | | |
1624 | /* "struct a;" */ | | | |
1625 | if (allow_c90) { | | | |
1626 | /* XXX: Why is this warning suppressed in C90 mode? */ | | | |
1627 | if (allow_trad || allow_c99) | | | |
1628 | /* declaration of '%s %s' intro... */ | | | |
1629 | warning(44, storage_class_name(scl), | | | |
1630 | tag->s_name); | | | |
1631 | tag = pushdown(tag); | | | |
1632 | } else if (tag->s_scl != scl) { | | | |
1633 | /* base type is really '%s %s' */ | | | |
1634 | warning(45, storage_class_name(tag->s_scl), | | | |
1635 | tag->s_name); | | | |
1636 | } | | | |
1637 | dcs->d_enclosing->d_nonempty_decl = true; | | | |
1638 | } else if (decl) { | | | |
1639 | /* "struct a { ... } " */ | | | |
1640 | if (hflag) | | | |
1641 | /* redefinition of '%s' hides earlier one */ | | | |
1642 | warning(43, tag->s_name); | | | |
1643 | tag = pushdown(tag); | | | |
1644 | dcs->d_enclosing->d_nonempty_decl = true; | | | |
1645 | } else if (tag->s_scl != scl) { | | | |
1646 | /* base type is really '%s %s' */ | | | |
1647 | warning(45, storage_class_name(tag->s_scl), | | | |
1648 | tag->s_name); | | | |
1649 | /* XXX: Why is this warning suppressed in C90 mode? */ | | | |
1650 | if (allow_trad || allow_c99) { | | | |
1651 | /* declaration of '%s %s' introduces ... */ | | | |
1652 | warning(44, storage_class_name(scl), | | | |
1653 | tag->s_name); | | | |
1654 | } | | | |
1655 | tag = pushdown(tag); | | | |
1656 | dcs->d_enclosing->d_nonempty_decl = true; | | | |
1657 | } | | | |
1658 | } else { | | | |
1659 | if (tag->s_scl != scl || | | | |
1660 | (decl && !is_incomplete(tag->s_type))) { | | | |
1661 | /* %s tag '%s' redeclared as %s */ | | | |
1662 | error(46, storage_class_name(tag->s_scl), | | | |
1663 | tag->s_name, storage_class_name(scl)); | | | |
1664 | print_previous_declaration(tag); | | | |
1665 | tag = pushdown(tag); | | | |
1666 | dcs->d_enclosing->d_nonempty_decl = true; | | | |
1667 | } else if (semi || decl) | | | |
1668 | dcs->d_enclosing->d_nonempty_decl = true; | | | |
1669 | } | | | |
1670 | return tag; | | | |
1671 | } | | | |
1672 | | | | |
1673 | const char * | | 1655 | const char * |
1674 | storage_class_name(scl_t sc) | | 1656 | storage_class_name(scl_t sc) |
1675 | { | | 1657 | { |
1676 | switch (sc) { | | 1658 | switch (sc) { |
1677 | case EXTERN: return "extern"; | | 1659 | case EXTERN: return "extern"; |
1678 | case STATIC: return "static"; | | 1660 | case STATIC: return "static"; |
1679 | case AUTO: return "auto"; | | 1661 | case AUTO: return "auto"; |
1680 | case REG: return "register"; | | 1662 | case REG: return "register"; |
1681 | case TYPEDEF: return "typedef"; | | 1663 | case TYPEDEF: return "typedef"; |
1682 | case STRUCT_TAG:return "struct"; | | 1664 | case STRUCT_TAG:return "struct"; |
1683 | case UNION_TAG: return "union"; | | 1665 | case UNION_TAG: return "union"; |
1684 | case ENUM_TAG: return "enum"; | | 1666 | case ENUM_TAG: return "enum"; |
1685 | default: lint_assert(/*CONSTCOND*/false); | | 1667 | default: lint_assert(/*CONSTCOND*/false); |
1686 | } | | 1668 | } |
1687 | /* NOTREACHED */ | | 1669 | /* NOTREACHED */ |
1688 | } | | 1670 | } |
1689 | | | 1671 | |
1690 | static bool | | 1672 | static bool |
1691 | has_named_member(const type_t *tp) | | 1673 | has_named_member(const type_t *tp) |
1692 | { | | 1674 | { |
1693 | for (const sym_t *mem = tp->t_sou->sou_first_member; | | 1675 | for (const sym_t *mem = tp->t_sou->sou_first_member; |
1694 | mem != NULL; mem = mem->s_next) { | | 1676 | mem != NULL; mem = mem->s_next) { |
1695 | if (mem->s_name != unnamed) | | 1677 | if (mem->s_name != unnamed) |
1696 | return true; | | 1678 | return true; |
1697 | if (is_struct_or_union(mem->s_type->t_tspec) | | 1679 | if (is_struct_or_union(mem->s_type->t_tspec) |
1698 | && has_named_member(mem->s_type)) | | 1680 | && has_named_member(mem->s_type)) |
1699 | return true; | | 1681 | return true; |
1700 | } | | 1682 | } |
1701 | return false; | | 1683 | return false; |
1702 | } | | 1684 | } |
1703 | | | 1685 | |
1704 | type_t * | | 1686 | type_t * |
1705 | complete_struct_or_union(sym_t *first_member) | | 1687 | complete_struct_or_union(sym_t *first_member) |
1706 | { | | 1688 | { |
1707 | | | 1689 | |
1708 | type_t *tp = dcs->d_tag_type; | | 1690 | type_t *tp = dcs->d_tag_type; |
1709 | if (tp == NULL) /* in case of syntax errors */ | | 1691 | if (tp == NULL) /* in case of syntax errors */ |
1710 | return gettyp(INT); | | 1692 | return gettyp(INT); |
1711 | | | 1693 | |
1712 | dcs_align(dcs->d_sou_align_in_bits, 0); | | 1694 | dcs_align(dcs->d_sou_align_in_bits, 0); |
1713 | | | 1695 | |
1714 | struct_or_union *sou = tp->t_sou; | | 1696 | struct_or_union *sou = tp->t_sou; |
1715 | sou->sou_align_in_bits = dcs->d_sou_align_in_bits; | | 1697 | sou->sou_align_in_bits = dcs->d_sou_align_in_bits; |
1716 | sou->sou_incomplete = false; | | 1698 | sou->sou_incomplete = false; |
1717 | sou->sou_first_member = first_member; | | 1699 | sou->sou_first_member = first_member; |
1718 | if (tp->t_packed) | | 1700 | if (tp->t_packed) |
1719 | pack_struct_or_union(tp); | | 1701 | pack_struct_or_union(tp); |
1720 | else | | 1702 | else |
1721 | sou->sou_size_in_bits = dcs->d_sou_size_in_bits; | | 1703 | sou->sou_size_in_bits = dcs->d_sou_size_in_bits; |
1722 | | | 1704 | |
1723 | if (sou->sou_size_in_bits == 0) { | | 1705 | if (sou->sou_size_in_bits == 0) { |
1724 | /* zero sized %s is a C99 feature */ | | 1706 | /* zero sized %s is a C99 feature */ |
1725 | c99ism(47, tspec_name(tp->t_tspec)); | | 1707 | c99ism(47, tspec_name(tp->t_tspec)); |
1726 | } else if (!has_named_member(tp)) { | | 1708 | } else if (!has_named_member(tp)) { |
1727 | /* '%s' has no named members */ | | 1709 | /* '%s' has no named members */ |
1728 | warning(65, type_name(tp)); | | 1710 | warning(65, type_name(tp)); |
1729 | } | | 1711 | } |
1730 | return tp; | | 1712 | return tp; |
1731 | } | | 1713 | } |
1732 | | | 1714 | |
1733 | type_t * | | 1715 | type_t * |
1734 | complete_enum(sym_t *first_enumerator) | | 1716 | complete_enum(sym_t *first_enumerator) |
1735 | { | | 1717 | { |
1736 | | | 1718 | |
1737 | type_t *tp = dcs->d_tag_type; | | 1719 | type_t *tp = dcs->d_tag_type; |
1738 | tp->t_enum->en_incomplete = false; | | 1720 | tp->t_enum->en_incomplete = false; |
1739 | tp->t_enum->en_first_enumerator = first_enumerator; | | 1721 | tp->t_enum->en_first_enumerator = first_enumerator; |
1740 | return tp; | | 1722 | return tp; |
1741 | } | | 1723 | } |
1742 | | | 1724 | |
1743 | /* | | 1725 | /* |
1744 | * Processes the name of an enumerator in an enum declaration. | | 1726 | * Processes the name of an enumerator in an enum declaration. |
1745 | * | | 1727 | * |
1746 | * sym points to the enumerator | | 1728 | * sym points to the enumerator |
1747 | * val is the value of the enumerator | | 1729 | * val is the value of the enumerator |
1748 | * impl is true if the value of the enumerator was not explicitly specified. | | 1730 | * impl is true if the value of the enumerator was not explicitly specified. |
1749 | */ | | 1731 | */ |
1750 | sym_t * | | 1732 | sym_t * |
1751 | enumeration_constant(sym_t *sym, int val, bool impl) | | 1733 | enumeration_constant(sym_t *sym, int val, bool impl) |
1752 | { | | 1734 | { |
1753 | | | 1735 | |
1754 | if (sym->s_scl != NOSCL) { | | 1736 | if (sym->s_scl != NOSCL) { |
1755 | if (sym->s_block_level == block_level) { | | 1737 | if (sym->s_block_level == block_level) { |
1756 | /* no hflag, because this is illegal */ | | 1738 | /* no hflag, because this is illegal */ |
1757 | if (sym->s_arg) { | | 1739 | if (sym->s_arg) { |
1758 | /* enumeration constant '%s' hides parameter */ | | 1740 | /* enumeration constant '%s' hides parameter */ |
1759 | warning(57, sym->s_name); | | 1741 | warning(57, sym->s_name); |
1760 | } else { | | 1742 | } else { |
1761 | /* redeclaration of '%s' */ | | 1743 | /* redeclaration of '%s' */ |
1762 | error(27, sym->s_name); | | 1744 | error(27, sym->s_name); |
1763 | /* | | 1745 | /* |
1764 | * Inside blocks, it should not be too | | 1746 | * Inside blocks, it should not be too |
1765 | * complicated to find the position of the | | 1747 | * complicated to find the position of the |
1766 | * previous declaration | | 1748 | * previous declaration |
1767 | */ | | 1749 | */ |
1768 | if (block_level == 0) | | 1750 | if (block_level == 0) |
1769 | print_previous_declaration(sym); | | 1751 | print_previous_declaration(sym); |
1770 | } | | 1752 | } |
1771 | } else { | | 1753 | } else { |
1772 | if (hflag) | | 1754 | if (hflag) |
1773 | /* redefinition of '%s' hides earlier one */ | | 1755 | /* redefinition of '%s' hides earlier one */ |
1774 | warning(43, sym->s_name); | | 1756 | warning(43, sym->s_name); |
1775 | } | | 1757 | } |
1776 | sym = pushdown(sym); | | 1758 | sym = pushdown(sym); |
1777 | } | | 1759 | } |
1778 | | | 1760 | |
1779 | sym->s_scl = ENUM_CONST; | | 1761 | sym->s_scl = ENUM_CONST; |
1780 | sym->s_type = dcs->d_tag_type; | | 1762 | sym->s_type = dcs->d_tag_type; |
1781 | sym->u.s_enum_constant = val; | | 1763 | sym->u.s_enum_constant = val; |
1782 | | | 1764 | |
1783 | if (impl && val == TARG_INT_MIN) { | | 1765 | if (impl && val == TARG_INT_MIN) { |
1784 | /* enumeration value '%s' overflows */ | | 1766 | /* enumeration value '%s' overflows */ |
1785 | warning(48, sym->s_name); | | 1767 | warning(48, sym->s_name); |
1786 | } | | 1768 | } |
1787 | | | 1769 | |
1788 | enumval = val == TARG_INT_MAX ? TARG_INT_MIN : val + 1; | | 1770 | enumval = val == TARG_INT_MAX ? TARG_INT_MIN : val + 1; |
1789 | return sym; | | 1771 | return sym; |
1790 | } | | 1772 | } |
1791 | | | 1773 | |
1792 | static bool | | 1774 | static bool |
1793 | ends_with(const char *s, const char *suffix) | | 1775 | ends_with(const char *s, const char *suffix) |
1794 | { | | 1776 | { |
1795 | size_t s_len = strlen(s); | | 1777 | size_t s_len = strlen(s); |
1796 | size_t suffix_len = strlen(suffix); | | 1778 | size_t suffix_len = strlen(suffix); |
1797 | return s_len >= suffix_len && | | 1779 | return s_len >= suffix_len && |
1798 | memcmp(s + s_len - suffix_len, suffix, suffix_len) == 0; | | 1780 | memcmp(s + s_len - suffix_len, suffix, suffix_len) == 0; |
1799 | } | | 1781 | } |
1800 | | | 1782 | |
1801 | void | | 1783 | void |
1802 | check_extern_declaration(const sym_t *sym) | | 1784 | check_extern_declaration(const sym_t *sym) |
1803 | { | | 1785 | { |
1804 | | | 1786 | |
1805 | if (sym->s_scl == EXTERN && | | 1787 | if (sym->s_scl == EXTERN && |
1806 | dcs->d_redeclared_symbol == NULL && | | 1788 | dcs->d_redeclared_symbol == NULL && |
1807 | ends_with(curr_pos.p_file, ".c") && | | 1789 | ends_with(curr_pos.p_file, ".c") && |
1808 | allow_c90 && | | 1790 | allow_c90 && |
1809 | !ch_isdigit(sym->s_name[0]) && /* see mktempsym */ | | 1791 | !ch_isdigit(sym->s_name[0]) && /* see mktempsym */ |
1810 | strcmp(sym->s_name, "main") != 0) { | | 1792 | strcmp(sym->s_name, "main") != 0) { |
1811 | /* missing%s header declaration for '%s' */ | | 1793 | /* missing%s header declaration for '%s' */ |
1812 | warning(351, sym->s_type->t_tspec == FUNC ? "" : " 'extern'", | | 1794 | warning(351, sym->s_type->t_tspec == FUNC ? "" : " 'extern'", |
1813 | sym->s_name); | | 1795 | sym->s_name); |
1814 | } | | 1796 | } |
1815 | if (any_query_enabled && | | 1797 | if (any_query_enabled && |
1816 | sym->s_type->t_tspec == FUNC && | | 1798 | sym->s_type->t_tspec == FUNC && |
1817 | sym->s_scl == EXTERN && | | 1799 | sym->s_scl == EXTERN && |
1818 | sym->s_def == DECL && | | 1800 | sym->s_def == DECL && |
1819 | !in_system_header) { | | 1801 | !in_system_header) { |
1820 | /* redundant 'extern' in function declaration of '%s' */ | | 1802 | /* redundant 'extern' in function declaration of '%s' */ |
1821 | query_message(13, sym->s_name); | | 1803 | query_message(13, sym->s_name); |
1822 | } | | 1804 | } |
1823 | } | | 1805 | } |
1824 | | | 1806 | |
| | | 1807 | /* |
| | | 1808 | * Check whether the symbol cannot be initialized due to type/storage class. |
| | | 1809 | * Return whether an error has been detected. |
| | | 1810 | */ |
| | | 1811 | static bool |
| | | 1812 | check_init(sym_t *sym) |
| | | 1813 | { |
| | | 1814 | |
| | | 1815 | if (sym->s_type->t_tspec == FUNC) { |
| | | 1816 | /* cannot initialize function '%s' */ |
| | | 1817 | error(24, sym->s_name); |
| | | 1818 | return true; |
| | | 1819 | } |
| | | 1820 | if (sym->s_scl == TYPEDEF) { |
| | | 1821 | /* cannot initialize typedef '%s' */ |
| | | 1822 | error(25, sym->s_name); |
| | | 1823 | return true; |
| | | 1824 | } |
| | | 1825 | if (sym->s_scl == EXTERN && sym->s_def == DECL) { |
| | | 1826 | if (dcs->d_kind == DLK_EXTERN) { |
| | | 1827 | /* cannot initialize extern declaration '%s' */ |
| | | 1828 | warning(26, sym->s_name); |
| | | 1829 | } else { |
| | | 1830 | /* cannot initialize extern declaration '%s' */ |
| | | 1831 | error(26, sym->s_name); |
| | | 1832 | return true; |
| | | 1833 | } |
| | | 1834 | } |
| | | 1835 | |
| | | 1836 | return false; |
| | | 1837 | } |
| | | 1838 | |
| | | 1839 | /* |
| | | 1840 | * Compares a prototype declaration with the remembered arguments of a previous |
| | | 1841 | * old-style function definition. |
| | | 1842 | */ |
| | | 1843 | static bool |
| | | 1844 | check_old_style_definition(sym_t *rdsym, sym_t *dsym) |
| | | 1845 | { |
| | | 1846 | |
| | | 1847 | sym_t *args = rdsym->u.s_old_style_args; |
| | | 1848 | sym_t *pargs = dsym->s_type->t_args; |
| | | 1849 | |
| | | 1850 | bool msg = false; |
| | | 1851 | |
| | | 1852 | int narg = 0; |
| | | 1853 | for (sym_t *arg = args; arg != NULL; arg = arg->s_next) |
| | | 1854 | narg++; |
| | | 1855 | int nparg = 0; |
| | | 1856 | for (sym_t *parg = pargs; parg != NULL; parg = parg->s_next) |
| | | 1857 | nparg++; |
| | | 1858 | if (narg != nparg) { |
| | | 1859 | /* prototype does not match old-style definition */ |
| | | 1860 | error(63); |
| | | 1861 | msg = true; |
| | | 1862 | goto end; |
| | | 1863 | } |
| | | 1864 | |
| | | 1865 | sym_t *arg = args; |
| | | 1866 | sym_t *parg = pargs; |
| | | 1867 | int n = 1; |
| | | 1868 | while (narg-- > 0) { |
| | | 1869 | bool dowarn = false; |
| | | 1870 | /* |
| | | 1871 | * If it does not match due to promotion and lint runs in |
| | | 1872 | * "traditional to C90" migration mode, print only a warning. |
| | | 1873 | * |
| | | 1874 | * XXX: Where is this "only a warning"? |
| | | 1875 | */ |
| | | 1876 | if (!types_compatible(arg->s_type, parg->s_type, |
| | | 1877 | true, true, &dowarn) || |
| | | 1878 | dowarn) { |
| | | 1879 | /* prototype does not match old-style ... */ |
| | | 1880 | error(299, n); |
| | | 1881 | msg = true; |
| | | 1882 | } |
| | | 1883 | arg = arg->s_next; |
| | | 1884 | parg = parg->s_next; |
| | | 1885 | n++; |
| | | 1886 | } |
| | | 1887 | |
| | | 1888 | end: |
| | | 1889 | if (msg && rflag) { |
| | | 1890 | /* old-style definition */ |
| | | 1891 | message_at(300, &rdsym->s_def_pos); |
| | | 1892 | } |
| | | 1893 | |
| | | 1894 | return msg; |
| | | 1895 | } |
| | | 1896 | |
1825 | /* Process a single external or 'static' declarator. */ | | 1897 | /* Process a single external or 'static' declarator. */ |
1826 | static void | | 1898 | static void |
1827 | declare_extern(sym_t *dsym, bool has_initializer, sbuf_t *renaming) | | 1899 | declare_extern(sym_t *dsym, bool has_initializer, sbuf_t *renaming) |
1828 | { | | 1900 | { |
1829 | | | 1901 | |
1830 | if (renaming != NULL) { | | 1902 | if (renaming != NULL) { |
1831 | lint_assert(dsym->s_rename == NULL); | | 1903 | lint_assert(dsym->s_rename == NULL); |
1832 | | | 1904 | |
1833 | char *s = level_zero_alloc(1, renaming->sb_len + 1, "string"); | | 1905 | char *s = level_zero_alloc(1, renaming->sb_len + 1, "string"); |
1834 | (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1); | | 1906 | (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1); |
1835 | dsym->s_rename = s; | | 1907 | dsym->s_rename = s; |
1836 | } | | 1908 | } |
1837 | | | 1909 | |
1838 | check_extern_declaration(dsym); | | 1910 | check_extern_declaration(dsym); |
1839 | | | 1911 | |
1840 | check_function_definition(dsym, true); | | 1912 | check_function_definition(dsym, true); |
1841 | | | 1913 | |
1842 | check_type(dsym); | | 1914 | check_type(dsym); |
1843 | | | 1915 | |
1844 | if (has_initializer && !check_init(dsym)) | | 1916 | if (has_initializer && !check_init(dsym)) |
1845 | dsym->s_def = DEF; | | 1917 | dsym->s_def = DEF; |
1846 | | | 1918 | |
1847 | /* | | 1919 | /* |
1848 | * Declarations of functions are marked as "tentative" in | | 1920 | * Declarations of functions are marked as "tentative" in |
1849 | * declarator_name(). This is wrong because there are no | | 1921 | * declarator_name(). This is wrong because there are no |
1850 | * tentative function definitions. | | 1922 | * tentative function definitions. |
1851 | */ | | 1923 | */ |
1852 | if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF) | | 1924 | if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF) |
1853 | dsym->s_def = DECL; | | 1925 | dsym->s_def = DECL; |
1854 | | | 1926 | |
1855 | if (dcs->d_inline) { | | 1927 | if (dcs->d_inline) { |
1856 | if (dsym->s_type->t_tspec == FUNC) { | | 1928 | if (dsym->s_type->t_tspec == FUNC) { |
1857 | dsym->s_inline = true; | | 1929 | dsym->s_inline = true; |
1858 | } else { | | 1930 | } else { |
1859 | /* variable '%s' declared inline */ | | 1931 | /* variable '%s' declared inline */ |
1860 | warning(268, dsym->s_name); | | 1932 | warning(268, dsym->s_name); |
1861 | } | | 1933 | } |
1862 | } | | 1934 | } |
1863 | | | 1935 | |
1864 | /* Write the declaration into the output file */ | | 1936 | /* Write the declaration into the output file */ |
1865 | if (plibflg && llibflg && | | 1937 | if (plibflg && llibflg && |
1866 | dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) { | | 1938 | dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) { |
1867 | /* | | 1939 | /* |
1868 | * With both LINTLIBRARY and PROTOLIB the prototype is | | 1940 | * With both LINTLIBRARY and PROTOLIB the prototype is |
1869 | * written as a function definition to the output file. | | 1941 | * written as a function definition to the output file. |
1870 | */ | | 1942 | */ |
1871 | bool rval = dsym->s_type->t_subt->t_tspec != VOID; | | 1943 | bool rval = dsym->s_type->t_subt->t_tspec != VOID; |
1872 | outfdef(dsym, &dsym->s_def_pos, rval, false, NULL); | | 1944 | outfdef(dsym, &dsym->s_def_pos, rval, false, NULL); |
1873 | } else if (!is_compiler_builtin(dsym->s_name) | | 1945 | } else if (!is_compiler_builtin(dsym->s_name) |
1874 | && !(has_initializer && dsym->s_type->t_incomplete_array)) { | | 1946 | && !(has_initializer && dsym->s_type->t_incomplete_array)) { |
1875 | outsym(dsym, dsym->s_scl, dsym->s_def); | | 1947 | outsym(dsym, dsym->s_scl, dsym->s_def); |
1876 | } | | 1948 | } |
1877 | | | 1949 | |
1878 | sym_t *rdsym = dcs->d_redeclared_symbol; | | 1950 | sym_t *rdsym = dcs->d_redeclared_symbol; |
1879 | if (rdsym != NULL) { | | 1951 | if (rdsym != NULL) { |
1880 | | | 1952 | |
1881 | /* | | 1953 | /* |
1882 | * If the old symbol stems from an old-style function | | 1954 | * If the old symbol stems from an old-style function |
1883 | * definition, we have remembered the params in | | 1955 | * definition, we have remembered the params in |
1884 | * rdsym->s_old_style_args and compare them with the params | | 1956 | * rdsym->s_old_style_args and compare them with the params |
1885 | * of the prototype. | | 1957 | * of the prototype. |
1886 | */ | | 1958 | */ |
1887 | bool redec = rdsym->s_osdef && dsym->s_type->t_proto && | | 1959 | bool redec = rdsym->s_osdef && dsym->s_type->t_proto && |
1888 | check_old_style_definition(rdsym, dsym); | | 1960 | check_old_style_definition(rdsym, dsym); |
1889 | | | 1961 | |
1890 | bool dowarn = false; | | 1962 | bool dowarn = false; |
1891 | if (!redec && !check_redeclaration(dsym, &dowarn)) { | | 1963 | if (!redec && !check_redeclaration(dsym, &dowarn)) { |
1892 | if (dowarn) { | | 1964 | if (dowarn) { |
1893 | /* TODO: Make this an error in C99 mode as well. */ | | 1965 | /* TODO: Make this an error in C99 mode as well. */ |
1894 | if (!allow_trad && !allow_c99) | | 1966 | if (!allow_trad && !allow_c99) |
1895 | /* redeclaration of '%s' */ | | 1967 | /* redeclaration of '%s' */ |
1896 | error(27, dsym->s_name); | | 1968 | error(27, dsym->s_name); |
1897 | else | | 1969 | else |
1898 | /* redeclaration of '%s' */ | | 1970 | /* redeclaration of '%s' */ |
1899 | warning(27, dsym->s_name); | | 1971 | warning(27, dsym->s_name); |
1900 | print_previous_declaration(rdsym); | | 1972 | print_previous_declaration(rdsym); |
1901 | } | | 1973 | } |
1902 | | | 1974 | |
1903 | /* | | 1975 | /* |
1904 | * Take over the remembered params if the new symbol | | 1976 | * Take over the remembered params if the new symbol |
1905 | * is not a prototype. | | 1977 | * is not a prototype. |
1906 | */ | | 1978 | */ |
1907 | if (rdsym->s_osdef && !dsym->s_type->t_proto) { | | 1979 | if (rdsym->s_osdef && !dsym->s_type->t_proto) { |
1908 | dsym->s_osdef = rdsym->s_osdef; | | 1980 | dsym->s_osdef = rdsym->s_osdef; |
1909 | dsym->u.s_old_style_args = | | 1981 | dsym->u.s_old_style_args = |
1910 | rdsym->u.s_old_style_args; | | 1982 | rdsym->u.s_old_style_args; |
1911 | dsym->s_def_pos = rdsym->s_def_pos; | | 1983 | dsym->s_def_pos = rdsym->s_def_pos; |
1912 | } | | 1984 | } |
1913 | | | 1985 | |
1914 | if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) | | 1986 | if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) |
1915 | dsym->s_def_pos = rdsym->s_def_pos; | | 1987 | dsym->s_def_pos = rdsym->s_def_pos; |
1916 | else if (rdsym->s_def == DEF && dsym->s_def != DEF) | | 1988 | else if (rdsym->s_def == DEF && dsym->s_def != DEF) |
1917 | dsym->s_def_pos = rdsym->s_def_pos; | | 1989 | dsym->s_def_pos = rdsym->s_def_pos; |
1918 | | | 1990 | |
1919 | copy_usage_info(dsym, rdsym); | | 1991 | copy_usage_info(dsym, rdsym); |
1920 | | | 1992 | |
1921 | /* Once a name is defined, it remains defined. */ | | 1993 | /* Once a name is defined, it remains defined. */ |
1922 | if (rdsym->s_def == DEF) | | 1994 | if (rdsym->s_def == DEF) |
1923 | dsym->s_def = DEF; | | 1995 | dsym->s_def = DEF; |
1924 | | | 1996 | |
1925 | /* once a function is inline, it remains inline */ | | 1997 | /* once a function is inline, it remains inline */ |
1926 | if (rdsym->s_inline) | | 1998 | if (rdsym->s_inline) |
1927 | dsym->s_inline = true; | | 1999 | dsym->s_inline = true; |
1928 | | | 2000 | |
1929 | complete_type(dsym, rdsym); | | 2001 | complete_type(dsym, rdsym); |
1930 | } | | 2002 | } |
1931 | | | 2003 | |
1932 | rmsym(rdsym); | | 2004 | rmsym(rdsym); |
1933 | } | | 2005 | } |
1934 | | | 2006 | |
1935 | if (dsym->s_scl == TYPEDEF) { | | 2007 | if (dsym->s_scl == TYPEDEF) { |
1936 | dsym->s_type = block_dup_type(dsym->s_type); | | 2008 | dsym->s_type = block_dup_type(dsym->s_type); |
1937 | dsym->s_type->t_typedef = true; | | 2009 | dsym->s_type->t_typedef = true; |
1938 | set_first_typedef(dsym->s_type, dsym); | | 2010 | set_first_typedef(dsym->s_type, dsym); |
1939 | } | | 2011 | } |
1940 | } | | 2012 | } |
1941 | | | 2013 | |
1942 | void | | 2014 | void |
1943 | declare(sym_t *decl, bool has_initializer, sbuf_t *renaming) | | 2015 | declare(sym_t *decl, bool has_initializer, sbuf_t *renaming) |
1944 | { | | 2016 | { |
1945 | | | 2017 | |
1946 | if (dcs->d_kind == DLK_EXTERN) | | 2018 | if (dcs->d_kind == DLK_EXTERN) |
1947 | declare_extern(decl, has_initializer, renaming); | | 2019 | declare_extern(decl, has_initializer, renaming); |
1948 | else if (dcs->d_kind == DLK_OLD_STYLE_ARGS || | | 2020 | else if (dcs->d_kind == DLK_OLD_STYLE_ARGS || |
1949 | dcs->d_kind == DLK_PROTO_PARAMS) { | | 2021 | dcs->d_kind == DLK_PROTO_PARAMS) { |
1950 | if (renaming != NULL) { | | 2022 | if (renaming != NULL) { |
1951 | /* symbol renaming can't be used on function arguments */ | | 2023 | /* symbol renaming can't be used on function arguments */ |
1952 | error(310); | | 2024 | error(310); |
1953 | } else | | 2025 | } else |
1954 | (void)declare_argument(decl, has_initializer); | | 2026 | (void)declare_argument(decl, has_initializer); |
1955 | } else { | | 2027 | } else { |
1956 | lint_assert(dcs->d_kind == DLK_AUTO); | | 2028 | lint_assert(dcs->d_kind == DLK_AUTO); |
1957 | if (renaming != NULL) { | | 2029 | if (renaming != NULL) { |
1958 | /* symbol renaming can't be used on automatic variables */ | | 2030 | /* symbol renaming can't be used on automatic variables */ |
1959 | error(311); | | 2031 | error(311); |
1960 | } else | | 2032 | } else |
1961 | declare_local(decl, has_initializer); | | 2033 | declare_local(decl, has_initializer); |
1962 | } | | 2034 | } |
1963 | } | | 2035 | } |
1964 | | | 2036 | |
1965 | /* | | 2037 | /* |
1966 | * Copies information about usage into a new symbol table entry of | | 2038 | * Copies information about usage into a new symbol table entry of |
1967 | * the same symbol. | | 2039 | * the same symbol. |
1968 | */ | | 2040 | */ |
1969 | void | | 2041 | void |
1970 | copy_usage_info(sym_t *sym, sym_t *rdsym) | | 2042 | copy_usage_info(sym_t *sym, sym_t *rdsym) |
1971 | { | | 2043 | { |
1972 | | | 2044 | |
1973 | sym->s_set_pos = rdsym->s_set_pos; | | 2045 | sym->s_set_pos = rdsym->s_set_pos; |
1974 | sym->s_use_pos = rdsym->s_use_pos; | | 2046 | sym->s_use_pos = rdsym->s_use_pos; |
1975 | sym->s_set = rdsym->s_set; | | 2047 | sym->s_set = rdsym->s_set; |
1976 | sym->s_used = rdsym->s_used; | | 2048 | sym->s_used = rdsym->s_used; |
1977 | } | | 2049 | } |
1978 | | | 2050 | |
1979 | /* | | 2051 | /* |
1980 | * Prints an error and returns true if a symbol is redeclared/redefined. | | 2052 | * Prints an error and returns true if a symbol is redeclared/redefined. |
1981 | * Otherwise, returns false and, in some cases of minor problems, prints | | 2053 | * Otherwise, returns false and, in some cases of minor problems, prints |
1982 | * a warning. | | 2054 | * a warning. |
1983 | */ | | 2055 | */ |
1984 | bool | | 2056 | bool |
1985 | check_redeclaration(sym_t *dsym, bool *dowarn) | | 2057 | check_redeclaration(sym_t *dsym, bool *dowarn) |
1986 | { | | 2058 | { |
1987 | | | 2059 | |
1988 | sym_t *rdsym = dcs->d_redeclared_symbol; | | 2060 | sym_t *rdsym = dcs->d_redeclared_symbol; |
1989 | if (rdsym->s_scl == ENUM_CONST) { | | 2061 | if (rdsym->s_scl == ENUM_CONST) { |
1990 | /* redeclaration of '%s' */ | | 2062 | /* redeclaration of '%s' */ |
1991 | error(27, dsym->s_name); | | 2063 | error(27, dsym->s_name); |
1992 | print_previous_declaration(rdsym); | | 2064 | print_previous_declaration(rdsym); |
1993 | return true; | | 2065 | return true; |
1994 | } | | 2066 | } |
1995 | if (rdsym->s_scl == TYPEDEF) { | | 2067 | if (rdsym->s_scl == TYPEDEF) { |
1996 | /* typedef '%s' redeclared */ | | 2068 | /* typedef '%s' redeclared */ |
1997 | error(89, dsym->s_name); | | 2069 | error(89, dsym->s_name); |
1998 | print_previous_declaration(rdsym); | | 2070 | print_previous_declaration(rdsym); |
1999 | return true; | | 2071 | return true; |
2000 | } | | 2072 | } |
2001 | if (dsym->s_scl == TYPEDEF) { | | 2073 | if (dsym->s_scl == TYPEDEF) { |
2002 | /* redeclaration of '%s' */ | | 2074 | /* redeclaration of '%s' */ |
2003 | error(27, dsym->s_name); | | 2075 | error(27, dsym->s_name); |
2004 | print_previous_declaration(rdsym); | | 2076 | print_previous_declaration(rdsym); |
2005 | return true; | | 2077 | return true; |
2006 | } | | 2078 | } |
2007 | if (rdsym->s_def == DEF && dsym->s_def == DEF) { | | 2079 | if (rdsym->s_def == DEF && dsym->s_def == DEF) { |
2008 | /* redefinition of '%s' */ | | 2080 | /* redefinition of '%s' */ |
2009 | error(28, dsym->s_name); | | 2081 | error(28, dsym->s_name); |
2010 | print_previous_declaration(rdsym); | | 2082 | print_previous_declaration(rdsym); |
2011 | return true; | | 2083 | return true; |
2012 | } | | 2084 | } |
2013 | if (!types_compatible(rdsym->s_type, dsym->s_type, | | 2085 | if (!types_compatible(rdsym->s_type, dsym->s_type, |
2014 | false, false, dowarn)) { | | 2086 | false, false, dowarn)) { |
2015 | /* redeclaration of '%s' with type '%s', expected '%s' */ | | 2087 | /* redeclaration of '%s' with type '%s', expected '%s' */ |
2016 | error(347, dsym->s_name, | | 2088 | error(347, dsym->s_name, |
2017 | type_name(dsym->s_type), type_name(rdsym->s_type)); | | 2089 | type_name(dsym->s_type), type_name(rdsym->s_type)); |
2018 | print_previous_declaration(rdsym); | | 2090 | print_previous_declaration(rdsym); |
2019 | return true; | | 2091 | return true; |
2020 | } | | 2092 | } |
2021 | if (rdsym->s_scl == EXTERN && dsym->s_scl == EXTERN) | | 2093 | if (rdsym->s_scl == EXTERN && dsym->s_scl == EXTERN) |
2022 | return false; | | 2094 | return false; |
2023 | if (rdsym->s_scl == STATIC && dsym->s_scl == STATIC) | | 2095 | if (rdsym->s_scl == STATIC && dsym->s_scl == STATIC) |
2024 | return false; | | 2096 | return false; |
2025 | if (rdsym->s_scl == STATIC && dsym->s_def == DECL) | | 2097 | if (rdsym->s_scl == STATIC && dsym->s_def == DECL) |
2026 | return false; | | 2098 | return false; |
2027 | if (rdsym->s_scl == EXTERN && rdsym->s_def == DEF) { | | 2099 | if (rdsym->s_scl == EXTERN && rdsym->s_def == DEF) { |
2028 | /* | | 2100 | /* |
2029 | * All cases except "int a = 1; static int a;" are caught | | 2101 | * All cases except "int a = 1; static int a;" are caught |
2030 | * above with or without a warning | | 2102 | * above with or without a warning |
2031 | */ | | 2103 | */ |
2032 | /* redeclaration of '%s' */ | | 2104 | /* redeclaration of '%s' */ |
2033 | error(27, dsym->s_name); | | 2105 | error(27, dsym->s_name); |
2034 | print_previous_declaration(rdsym); | | 2106 | print_previous_declaration(rdsym); |
2035 | return true; | | 2107 | return true; |
2036 | } | | 2108 | } |
2037 | if (rdsym->s_scl == EXTERN) { | | 2109 | if (rdsym->s_scl == EXTERN) { |
2038 | /* '%s' was previously declared extern, becomes static */ | | 2110 | /* '%s' was previously declared extern, becomes static */ |
2039 | warning(29, dsym->s_name); | | 2111 | warning(29, dsym->s_name); |
2040 | print_previous_declaration(rdsym); | | 2112 | print_previous_declaration(rdsym); |
2041 | return false; | | 2113 | return false; |
2042 | } | | 2114 | } |
2043 | /* | | 2115 | /* |
2044 | * Now it's one of: | | 2116 | * Now it's one of: |
2045 | * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;" | | 2117 | * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;" |
2046 | */ | | 2118 | */ |
2047 | /* TODO: Make this an error in C99 mode as well. */ | | 2119 | /* TODO: Make this an error in C99 mode as well. */ |
2048 | if (!allow_trad && !allow_c99) { | | 2120 | if (!allow_trad && !allow_c99) { |
2049 | /* redeclaration of '%s'; ANSI C requires static */ | | 2121 | /* redeclaration of '%s'; ANSI C requires static */ |
2050 | warning(30, dsym->s_name); | | 2122 | warning(30, dsym->s_name); |
2051 | print_previous_declaration(rdsym); | | 2123 | print_previous_declaration(rdsym); |
2052 | } | | 2124 | } |
2053 | dsym->s_scl = STATIC; | | 2125 | dsym->s_scl = STATIC; |
2054 | return false; | | 2126 | return false; |
2055 | } | | 2127 | } |
2056 | | | 2128 | |
2057 | static bool | | 2129 | static bool |
2058 | qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual) | | 2130 | qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual) |
2059 | { | | 2131 | { |
2060 | | | 2132 | |
2061 | if (tp1->t_const != tp2->t_const && !ignqual && allow_c90) | | 2133 | if (tp1->t_const != tp2->t_const && !ignqual && allow_c90) |
2062 | return false; | | 2134 | return false; |
2063 | if (tp1->t_volatile != tp2->t_volatile && !ignqual && allow_c90) | | 2135 | if (tp1->t_volatile != tp2->t_volatile && !ignqual && allow_c90) |
2064 | return false; | | 2136 | return false; |
2065 | return true; | | 2137 | return true; |
2066 | } | | 2138 | } |
2067 | | | 2139 | |
2068 | bool | | 2140 | bool |
2069 | pointer_types_are_compatible(const type_t *tp1, const type_t *tp2, bool ignqual) | | 2141 | pointer_types_are_compatible(const type_t *tp1, const type_t *tp2, bool ignqual) |
2070 | { | | 2142 | { |
2071 | | | 2143 | |
2072 | return tp1->t_tspec == VOID || tp2->t_tspec == VOID || | | 2144 | return tp1->t_tspec == VOID || tp2->t_tspec == VOID || |
2073 | qualifiers_correspond(tp1, tp2, ignqual); | | 2145 | qualifiers_correspond(tp1, tp2, ignqual); |
2074 | } | | 2146 | } |
2075 | | | 2147 | |
| | | 2148 | static bool |
| | | 2149 | prototypes_compatible(const type_t *tp1, const type_t *tp2, bool *dowarn) |
| | | 2150 | { |
| | | 2151 | |
| | | 2152 | if (tp1->t_vararg != tp2->t_vararg) |
| | | 2153 | return false; |
| | | 2154 | |
| | | 2155 | sym_t *a1 = tp1->t_args; |
| | | 2156 | sym_t *a2 = tp2->t_args; |
| | | 2157 | |
| | | 2158 | for (; a1 != NULL && a2 != NULL; a1 = a1->s_next, a2 = a2->s_next) { |
| | | 2159 | if (!types_compatible(a1->s_type, a2->s_type, |
| | | 2160 | true, false, dowarn)) |
| | | 2161 | return false; |
| | | 2162 | } |
| | | 2163 | return a1 == a2; |
| | | 2164 | } |
| | | 2165 | |
| | | 2166 | /* |
| | | 2167 | * Returns whether all parameters of a prototype are compatible with an |
| | | 2168 | * old-style function declaration. |
| | | 2169 | * |
| | | 2170 | * This is the case if the following conditions are met: |
| | | 2171 | * 1. the prototype has a fixed number of parameters |
| | | 2172 | * 2. no parameter is of type float |
| | | 2173 | * 3. no parameter is converted to another type if integer promotion |
| | | 2174 | * is applied on it |
| | | 2175 | */ |
| | | 2176 | static bool |
| | | 2177 | matches_no_arg_function(const type_t *tp, bool *dowarn) |
| | | 2178 | { |
| | | 2179 | |
| | | 2180 | if (tp->t_vararg && dowarn != NULL) |
| | | 2181 | *dowarn = true; |
| | | 2182 | for (sym_t *arg = tp->t_args; arg != NULL; arg = arg->s_next) { |
| | | 2183 | tspec_t t = arg->s_type->t_tspec; |
| | | 2184 | if (t == FLOAT || |
| | | 2185 | t == CHAR || t == SCHAR || t == UCHAR || |
| | | 2186 | t == SHORT || t == USHORT) { |
| | | 2187 | if (dowarn != NULL) |
| | | 2188 | *dowarn = true; |
| | | 2189 | } |
| | | 2190 | } |
| | | 2191 | /* FIXME: Always returning true cannot be correct. */ |
| | | 2192 | return true; |
| | | 2193 | } |
| | | 2194 | |
2076 | /*- | | 2195 | /*- |
2077 | * ignqual ignore type qualifiers; used for function parameters | | 2196 | * ignqual ignore type qualifiers; used for function parameters |
2078 | * promot promote the left type; used for comparison of parameters of | | 2197 | * promot promote the left type; used for comparison of parameters of |
2079 | * old-style function definitions with parameters of prototypes. | | 2198 | * old-style function definitions with parameters of prototypes. |
2080 | * *dowarn is set to true if an old-style function declaration is not | | 2199 | * *dowarn is set to true if an old-style function declaration is not |
2081 | * compatible with a prototype | | 2200 | * compatible with a prototype |
2082 | */ | | 2201 | */ |
2083 | bool | | 2202 | bool |
2084 | types_compatible(const type_t *tp1, const type_t *tp2, | | 2203 | types_compatible(const type_t *tp1, const type_t *tp2, |
2085 | bool ignqual, bool promot, bool *dowarn) | | 2204 | bool ignqual, bool promot, bool *dowarn) |
2086 | { | | 2205 | { |
2087 | | | 2206 | |
2088 | while (tp1 != NULL && tp2 != NULL) { | | 2207 | while (tp1 != NULL && tp2 != NULL) { |
2089 | tspec_t t = tp1->t_tspec; | | 2208 | tspec_t t = tp1->t_tspec; |
2090 | if (promot) { | | 2209 | if (promot) { |
2091 | if (t == FLOAT) | | 2210 | if (t == FLOAT) |
2092 | t = DOUBLE; | | 2211 | t = DOUBLE; |
2093 | else if (t == CHAR || t == SCHAR) | | 2212 | else if (t == CHAR || t == SCHAR) |
2094 | t = INT; | | 2213 | t = INT; |
2095 | else if (t == UCHAR) | | 2214 | else if (t == UCHAR) |
2096 | t = allow_c90 ? INT : UINT; | | 2215 | t = allow_c90 ? INT : UINT; |
2097 | else if (t == SHORT) | | 2216 | else if (t == SHORT) |
2098 | t = INT; | | 2217 | t = INT; |
2099 | else if (t == USHORT) { | | 2218 | else if (t == USHORT) { |
2100 | /* CONSTCOND */ | | 2219 | /* CONSTCOND */ |
2101 | t = TARG_INT_MAX < TARG_USHRT_MAX || !allow_c90 | | 2220 | t = TARG_INT_MAX < TARG_USHRT_MAX || !allow_c90 |
2102 | ? UINT : INT; | | 2221 | ? UINT : INT; |
2103 | } | | 2222 | } |
2104 | } | | 2223 | } |
2105 | | | 2224 | |
2106 | if (t != tp2->t_tspec) | | 2225 | if (t != tp2->t_tspec) |
2107 | return false; | | 2226 | return false; |
2108 | | | 2227 | |
2109 | if (!qualifiers_correspond(tp1, tp2, ignqual)) | | 2228 | if (!qualifiers_correspond(tp1, tp2, ignqual)) |
2110 | return false; | | 2229 | return false; |
2111 | | | 2230 | |
2112 | if (is_struct_or_union(t)) | | 2231 | if (is_struct_or_union(t)) |
2113 | return tp1->t_sou == tp2->t_sou; | | 2232 | return tp1->t_sou == tp2->t_sou; |
2114 | | | 2233 | |
2115 | if (t == ENUM && eflag) | | 2234 | if (t == ENUM && eflag) |
2116 | return tp1->t_enum == tp2->t_enum; | | 2235 | return tp1->t_enum == tp2->t_enum; |
2117 | | | 2236 | |
2118 | if (t == ARRAY && tp1->t_dim != tp2->t_dim) { | | 2237 | if (t == ARRAY && tp1->t_dim != tp2->t_dim) { |
2119 | if (tp1->t_dim != 0 && tp2->t_dim != 0) | | 2238 | if (tp1->t_dim != 0 && tp2->t_dim != 0) |
2120 | return false; | | 2239 | return false; |
2121 | } | | 2240 | } |
2122 | | | 2241 | |
2123 | /* don't check prototypes for traditional */ | | 2242 | /* don't check prototypes for traditional */ |
2124 | if (t == FUNC && allow_c90) { | | 2243 | if (t == FUNC && allow_c90) { |
2125 | if (tp1->t_proto && tp2->t_proto) { | | 2244 | if (tp1->t_proto && tp2->t_proto) { |
2126 | if (!prototypes_compatible(tp1, tp2, dowarn)) | | 2245 | if (!prototypes_compatible(tp1, tp2, dowarn)) |
2127 | return false; | | 2246 | return false; |
2128 | } else if (tp1->t_proto) { | | 2247 | } else if (tp1->t_proto) { |
2129 | if (!matches_no_arg_function(tp1, dowarn)) | | 2248 | if (!matches_no_arg_function(tp1, dowarn)) |
2130 | return false; | | 2249 | return false; |
2131 | } else if (tp2->t_proto) { | | 2250 | } else if (tp2->t_proto) { |
2132 | if (!matches_no_arg_function(tp2, dowarn)) | | 2251 | if (!matches_no_arg_function(tp2, dowarn)) |
2133 | return false; | | 2252 | return false; |
2134 | } | | 2253 | } |
2135 | } | | 2254 | } |
2136 | | | 2255 | |
2137 | tp1 = tp1->t_subt; | | 2256 | tp1 = tp1->t_subt; |
2138 | tp2 = tp2->t_subt; | | 2257 | tp2 = tp2->t_subt; |
2139 | ignqual = promot = false; | | 2258 | ignqual = promot = false; |
2140 | } | | 2259 | } |
2141 | | | 2260 | |
2142 | return tp1 == tp2; | | 2261 | return tp1 == tp2; |
2143 | } | | 2262 | } |
2144 | | | 2263 | |
2145 | static bool | | | |
2146 | prototypes_compatible(const type_t *tp1, const type_t *tp2, bool *dowarn) | | | |
2147 | { | | | |
2148 | | | | |
2149 | if (tp1->t_vararg != tp2->t_vararg) | | | |
2150 | return false; | | | |
2151 | | | | |
2152 | sym_t *a1 = tp1->t_args; | | | |
2153 | sym_t *a2 = tp2->t_args; | | | |
2154 | | | | |
2155 | for (; a1 != NULL && a2 != NULL; a1 = a1->s_next, a2 = a2->s_next) { | | | |
2156 | if (!types_compatible(a1->s_type, a2->s_type, | | | |
2157 | true, false, dowarn)) | | | |
2158 | return false; | | | |
2159 | } | | | |
2160 | return a1 == a2; | | | |
2161 | } | | | |
2162 | | | | |
2163 | /* | | | |
2164 | * Returns whether all parameters of a prototype are compatible with an | | | |
2165 | * old-style function declaration. | | | |
2166 | * | | | |
2167 | * This is the case if the following conditions are met: | | | |
2168 | * 1. the prototype has a fixed number of parameters | | | |
2169 | * 2. no parameter is of type float | | | |
2170 | * 3. no parameter is converted to another type if integer promotion | | | |
2171 | * is applied on it | | | |
2172 | */ | | | |
2173 | static bool | | | |
2174 | matches_no_arg_function(const type_t *tp, bool *dowarn) | | | |
2175 | { | | | |
2176 | | | | |
2177 | if (tp->t_vararg && dowarn != NULL) | | | |
2178 | *dowarn = true; | | | |
2179 | for (sym_t *arg = tp->t_args; arg != NULL; arg = arg->s_next) { | | | |
2180 | tspec_t t = arg->s_type->t_tspec; | | | |
2181 | if (t == FLOAT || | | | |
2182 | t == CHAR || t == SCHAR || t == UCHAR || | | | |
2183 | t == SHORT || t == USHORT) { | | | |
2184 | if (dowarn != NULL) | | | |
2185 | *dowarn = true; | | | |
2186 | } | | | |
2187 | } | | | |
2188 | /* FIXME: Always returning true cannot be correct. */ | | | |
2189 | return true; | | | |
2190 | } | | | |
2191 | | | | |
2192 | /* | | | |
2193 | * Compares a prototype declaration with the remembered arguments of a previous | | | |
2194 | * old-style function definition. | | | |
2195 | */ | | | |
2196 | static bool | | | |
2197 | check_old_style_definition(sym_t *rdsym, sym_t *dsym) | | | |
2198 | { | | | |
2199 | | | | |
2200 | sym_t *args = rdsym->u.s_old_style_args; | | | |
2201 | sym_t *pargs = dsym->s_type->t_args; | | | |
2202 | | | | |
2203 | bool msg = false; | | | |
2204 | | | | |
2205 | int narg = 0; | | | |
2206 | for (sym_t *arg = args; arg != NULL; arg = arg->s_next) | | | |
2207 | narg++; | | | |
2208 | int nparg = 0; | | | |
2209 | for (sym_t *parg = pargs; parg != NULL; parg = parg->s_next) | | | |
2210 | nparg++; | | | |
2211 | if (narg != nparg) { | | | |
2212 | /* prototype does not match old-style definition */ | | | |
2213 | error(63); | | | |
2214 | msg = true; | | | |
2215 | goto end; | | | |
2216 | } | | | |
2217 | | | | |
2218 | sym_t *arg = args; | | | |
2219 | sym_t *parg = pargs; | | | |
2220 | int n = 1; | | | |
2221 | while (narg-- > 0) { | | | |
2222 | bool dowarn = false; | | | |
2223 | /* | | | |
2224 | * If it does not match due to promotion and lint runs in | | | |
2225 | * "traditional to C90" migration mode, print only a warning. | | | |
2226 | * | | | |
2227 | * XXX: Where is this "only a warning"? | | | |
2228 | */ | | | |
2229 | if (!types_compatible(arg->s_type, parg->s_type, | | | |
2230 | true, true, &dowarn) || | | | |
2231 | dowarn) { | | | |
2232 | /* prototype does not match old-style ... */ | | | |
2233 | error(299, n); | | | |
2234 | msg = true; | | | |
2235 | } | | | |
2236 | arg = arg->s_next; | | | |
2237 | parg = parg->s_next; | | | |
2238 | n++; | | | |
2239 | } | | | |
2240 | | | | |
2241 | end: | | | |
2242 | if (msg && rflag) { | | | |
2243 | /* old-style definition */ | | | |
2244 | message_at(300, &rdsym->s_def_pos); | | | |
2245 | } | | | |
2246 | | | | |
2247 | return msg; | | | |
2248 | } | | | |
2249 | | | | |
2250 | /* | | 2264 | /* |
2251 | * Completes a type by copying the dimension and prototype information from a | | 2265 | * Completes a type by copying the dimension and prototype information from a |
2252 | * second compatible type. | | 2266 | * second compatible type. |
2253 | * | | 2267 | * |
2254 | * Following lines are legal: | | 2268 | * Following lines are legal: |
2255 | * "typedef a[]; a b; a b[10]; a c; a c[20];" | | 2269 | * "typedef a[]; a b; a b[10]; a c; a c[20];" |
2256 | * "typedef ft(); ft f; f(int); ft g; g(long);" | | 2270 | * "typedef ft(); ft f; f(int); ft g; g(long);" |
2257 | * This means that, if a type is completed, the type structure must be | | 2271 | * This means that, if a type is completed, the type structure must be |
2258 | * duplicated. | | 2272 | * duplicated. |
2259 | */ | | 2273 | */ |
2260 | void | | 2274 | void |
2261 | complete_type(sym_t *dsym, sym_t *ssym) | | 2275 | complete_type(sym_t *dsym, sym_t *ssym) |
2262 | { | | 2276 | { |
2263 | type_t **dstp = &dsym->s_type; | | 2277 | type_t **dstp = &dsym->s_type; |
2264 | type_t *src = ssym->s_type; | | 2278 | type_t *src = ssym->s_type; |
2265 | | | 2279 | |
2266 | while (*dstp != NULL) { | | 2280 | while (*dstp != NULL) { |
2267 | type_t *dst = *dstp; | | 2281 | type_t *dst = *dstp; |
2268 | lint_assert(src != NULL); | | 2282 | lint_assert(src != NULL); |
2269 | lint_assert(dst->t_tspec == src->t_tspec); | | 2283 | lint_assert(dst->t_tspec == src->t_tspec); |
2270 | if (dst->t_tspec == ARRAY) { | | 2284 | if (dst->t_tspec == ARRAY) { |
2271 | if (dst->t_dim == 0 && src->t_dim != 0) { | | 2285 | if (dst->t_dim == 0 && src->t_dim != 0) { |
2272 | *dstp = dst = block_dup_type(dst); | | 2286 | *dstp = dst = block_dup_type(dst); |
2273 | dst->t_dim = src->t_dim; | | 2287 | dst->t_dim = src->t_dim; |
2274 | dst->t_incomplete_array = false; | | 2288 | dst->t_incomplete_array = false; |
2275 | } | | 2289 | } |
2276 | } else if (dst->t_tspec == FUNC) { | | 2290 | } else if (dst->t_tspec == FUNC) { |
2277 | if (!dst->t_proto && src->t_proto) { | | 2291 | if (!dst->t_proto && src->t_proto) { |
2278 | *dstp = dst = block_dup_type(dst); | | 2292 | *dstp = dst = block_dup_type(dst); |
2279 | dst->t_proto = true; | | 2293 | dst->t_proto = true; |
2280 | dst->t_args = src->t_args; | | 2294 | dst->t_args = src->t_args; |
2281 | } | | 2295 | } |
2282 | } | | 2296 | } |
2283 | dstp = &dst->t_subt; | | 2297 | dstp = &dst->t_subt; |
2284 | src = src->t_subt; | | 2298 | src = src->t_subt; |
2285 | } | | 2299 | } |
2286 | } | | 2300 | } |
2287 | | | 2301 | |
2288 | /* | | 2302 | /* |
2289 | * Completes the declaration of a single argument. | | 2303 | * Completes the declaration of a single argument. |
2290 | */ | | 2304 | */ |
2291 | sym_t * | | 2305 | sym_t * |
2292 | declare_argument(sym_t *sym, bool has_initializer) | | 2306 | declare_argument(sym_t *sym, bool has_initializer) |
2293 | { | | 2307 | { |
2294 | | | 2308 | |
2295 | check_function_definition(sym, true); | | 2309 | check_function_definition(sym, true); |
2296 | | | 2310 | |
2297 | check_type(sym); | | 2311 | check_type(sym); |
2298 | | | 2312 | |
2299 | if (dcs->d_redeclared_symbol != NULL && | | 2313 | if (dcs->d_redeclared_symbol != NULL && |
2300 | dcs->d_redeclared_symbol->s_block_level == block_level) { | | 2314 | dcs->d_redeclared_symbol->s_block_level == block_level) { |
2301 | /* redeclaration of formal parameter '%s' */ | | 2315 | /* redeclaration of formal parameter '%s' */ |
2302 | error(237, sym->s_name); | | 2316 | error(237, sym->s_name); |
2303 | rmsym(dcs->d_redeclared_symbol); | | 2317 | rmsym(dcs->d_redeclared_symbol); |
2304 | sym->s_arg = true; | | 2318 | sym->s_arg = true; |
2305 | } | | 2319 | } |
2306 | | | 2320 | |
2307 | if (!sym->s_arg) { | | 2321 | if (!sym->s_arg) { |
2308 | /* declared argument '%s' is missing */ | | 2322 | /* declared argument '%s' is missing */ |
2309 | error(53, sym->s_name); | | 2323 | error(53, sym->s_name); |
2310 | sym->s_arg = true; | | 2324 | sym->s_arg = true; |
2311 | } | | 2325 | } |
2312 | | | 2326 | |
2313 | if (has_initializer) { | | 2327 | if (has_initializer) { |
2314 | /* cannot initialize parameter '%s' */ | | 2328 | /* cannot initialize parameter '%s' */ |
2315 | error(52, sym->s_name); | | 2329 | error(52, sym->s_name); |
2316 | } | | 2330 | } |
2317 | | | 2331 | |
2318 | if (sym->s_type == NULL) /* for c(void()) */ | | 2332 | if (sym->s_type == NULL) /* for c(void()) */ |
2319 | sym->s_type = gettyp(VOID); | | 2333 | sym->s_type = gettyp(VOID); |
2320 | | | 2334 | |
2321 | tspec_t t = sym->s_type->t_tspec; | | 2335 | tspec_t t = sym->s_type->t_tspec; |
2322 | if (t == ARRAY) | | 2336 | if (t == ARRAY) |
2323 | sym->s_type = block_derive_type(sym->s_type->t_subt, PTR); | | 2337 | sym->s_type = block_derive_type(sym->s_type->t_subt, PTR); |
2324 | if (t == FUNC) { | | 2338 | if (t == FUNC) { |
2325 | if (!allow_c90) | | 2339 | if (!allow_c90) |
2326 | /* parameter '%s' has function type, should be ... */ | | 2340 | /* parameter '%s' has function type, should be ... */ |
2327 | warning(50, sym->s_name); | | 2341 | warning(50, sym->s_name); |
2328 | sym->s_type = block_derive_type(sym->s_type, PTR); | | 2342 | sym->s_type = block_derive_type(sym->s_type, PTR); |
2329 | } | | 2343 | } |
2330 | if (t == FLOAT && !allow_c90) | | 2344 | if (t == FLOAT && !allow_c90) |
2331 | sym->s_type = gettyp(DOUBLE); | | 2345 | sym->s_type = gettyp(DOUBLE); |
2332 | | | 2346 | |
2333 | if (dcs->d_inline) | | 2347 | if (dcs->d_inline) |
2334 | /* parameter '%s' declared inline */ | | 2348 | /* parameter '%s' declared inline */ |
2335 | warning(269, sym->s_name); | | 2349 | warning(269, sym->s_name); |
2336 | | | 2350 | |
2337 | /* | | 2351 | /* |
2338 | * Arguments must have complete types. length_in_bits prints the | | 2352 | * Arguments must have complete types. length_in_bits prints the |
2339 | * needed error messages (null dimension is impossible because arrays | | 2353 | * needed error messages (null dimension is impossible because arrays |
2340 | * are converted to pointers). | | 2354 | * are converted to pointers). |
2341 | */ | | 2355 | */ |
2342 | if (sym->s_type->t_tspec != VOID) | | 2356 | if (sym->s_type->t_tspec != VOID) |
2343 | (void)length_in_bits(sym->s_type, sym->s_name); | | 2357 | (void)length_in_bits(sym->s_type, sym->s_name); |
2344 | | | 2358 | |
2345 | sym->s_used = dcs->d_used; | | 2359 | sym->s_used = dcs->d_used; |
2346 | mark_as_set(sym); | | 2360 | mark_as_set(sym); |
2347 | | | 2361 | |
2348 | return sym; | | 2362 | return sym; |
2349 | } | | 2363 | } |
2350 | | | 2364 | |
2351 | static bool | | 2365 | static bool |
2352 | is_character_pointer(const type_t *tp) | | 2366 | is_character_pointer(const type_t *tp) |
2353 | { | | 2367 | { |
2354 | tspec_t st; | | 2368 | tspec_t st; |
2355 | | | 2369 | |
2356 | return tp->t_tspec == PTR && | | 2370 | return tp->t_tspec == PTR && |
2357 | (st = tp->t_subt->t_tspec, | | 2371 | (st = tp->t_subt->t_tspec, |
2358 | st == CHAR || st == SCHAR || st == UCHAR); | | 2372 | st == CHAR || st == SCHAR || st == UCHAR); |
2359 | } | | 2373 | } |
2360 | | | 2374 | |
2361 | void | | 2375 | void |
2362 | check_func_lint_directives(void) | | 2376 | check_func_lint_directives(void) |
2363 | { | | 2377 | { |
2364 | | | 2378 | |
2365 | /* check for illegal combinations of lint directives */ | | 2379 | /* check for illegal combinations of lint directives */ |
2366 | if (printflike_argnum != -1 && scanflike_argnum != -1) { | | 2380 | if (printflike_argnum != -1 && scanflike_argnum != -1) { |
2367 | /* ** PRINTFLIKE ** and ** SCANFLIKE ** cannot be combined */ | | 2381 | /* ** PRINTFLIKE ** and ** SCANFLIKE ** cannot be combined */ |
2368 | warning(289); | | 2382 | warning(289); |
2369 | printflike_argnum = scanflike_argnum = -1; | | 2383 | printflike_argnum = scanflike_argnum = -1; |
2370 | } | | 2384 | } |
2371 | if (nvararg != -1 && | | 2385 | if (nvararg != -1 && |
2372 | (printflike_argnum != -1 || scanflike_argnum != -1)) { | | 2386 | (printflike_argnum != -1 || scanflike_argnum != -1)) { |
2373 | /* dubious use of ** VARARGS ** with ** %s ** */ | | 2387 | /* dubious use of ** VARARGS ** with ** %s ** */ |
2374 | warning(288, | | 2388 | warning(288, |
2375 | printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE"); | | 2389 | printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE"); |
2376 | nvararg = -1; | | 2390 | nvararg = -1; |
2377 | } | | 2391 | } |
2378 | | | 2392 | |
2379 | /* | | 2393 | /* |
2380 | * check if the argument of a lint directive is compatible with the | | 2394 | * check if the argument of a lint directive is compatible with the |
2381 | * number of arguments. | | 2395 | * number of arguments. |
2382 | */ | | 2396 | */ |
2383 | int narg = 0; | | 2397 | int narg = 0; |
2384 | for (sym_t *arg = dcs->d_func_args; arg != NULL; arg = arg->s_next) | | 2398 | for (sym_t *arg = dcs->d_func_args; arg != NULL; arg = arg->s_next) |
2385 | narg++; | | 2399 | narg++; |
2386 | if (nargusg > narg) { | | 2400 | if (nargusg > narg) { |
2387 | /* argument number mismatch in comment ** %s ** */ | | 2401 | /* argument number mismatch in comment ** %s ** */ |
2388 | warning(283, "ARGSUSED"); | | 2402 | warning(283, "ARGSUSED"); |
2389 | nargusg = 0; | | 2403 | nargusg = 0; |
2390 | } | | 2404 | } |
2391 | if (nvararg > narg) { | | 2405 | if (nvararg > narg) { |
2392 | /* argument number mismatch in comment ** %s ** */ | | 2406 | /* argument number mismatch in comment ** %s ** */ |
2393 | warning(283, "VARARGS"); | | 2407 | warning(283, "VARARGS"); |
2394 | nvararg = 0; | | 2408 | nvararg = 0; |
2395 | } | | 2409 | } |
2396 | if (printflike_argnum > narg) { | | 2410 | if (printflike_argnum > narg) { |
2397 | /* argument number mismatch in comment ** %s ** */ | | 2411 | /* argument number mismatch in comment ** %s ** */ |
2398 | warning(283, "PRINTFLIKE"); | | 2412 | warning(283, "PRINTFLIKE"); |
2399 | printflike_argnum = -1; | | 2413 | printflike_argnum = -1; |
2400 | } else if (printflike_argnum == 0) { | | 2414 | } else if (printflike_argnum == 0) { |
2401 | printflike_argnum = -1; | | 2415 | printflike_argnum = -1; |
2402 | } | | 2416 | } |
2403 | if (scanflike_argnum > narg) { | | 2417 | if (scanflike_argnum > narg) { |
2404 | /* argument number mismatch in comment ** %s ** */ | | 2418 | /* argument number mismatch in comment ** %s ** */ |
2405 | warning(283, "SCANFLIKE"); | | 2419 | warning(283, "SCANFLIKE"); |
2406 | scanflike_argnum = -1; | | 2420 | scanflike_argnum = -1; |
2407 | } else if (scanflike_argnum == 0) { | | 2421 | } else if (scanflike_argnum == 0) { |
2408 | scanflike_argnum = -1; | | 2422 | scanflike_argnum = -1; |
2409 | } | | 2423 | } |
2410 | if (printflike_argnum != -1 || scanflike_argnum != -1) { | | 2424 | if (printflike_argnum != -1 || scanflike_argnum != -1) { |
2411 | narg = printflike_argnum != -1 | | 2425 | narg = printflike_argnum != -1 |
2412 | ? printflike_argnum : scanflike_argnum; | | 2426 | ? printflike_argnum : scanflike_argnum; |
2413 | sym_t *arg = dcs->d_func_args; | | 2427 | sym_t *arg = dcs->d_func_args; |
2414 | for (int n = 1; n < narg; n++) | | 2428 | for (int n = 1; n < narg; n++) |
2415 | arg = arg->s_next; | | 2429 | arg = arg->s_next; |
2416 | if (!is_character_pointer(arg->s_type)) { | | 2430 | if (!is_character_pointer(arg->s_type)) { |
2417 | /* argument %d must be 'char *' for PRINTFLIKE/... */ | | 2431 | /* argument %d must be 'char *' for PRINTFLIKE/... */ |
2418 | warning(293, narg); | | 2432 | warning(293, narg); |
2419 | printflike_argnum = scanflike_argnum = -1; | | 2433 | printflike_argnum = scanflike_argnum = -1; |
2420 | } | | 2434 | } |
2421 | } | | 2435 | } |
2422 | } | | 2436 | } |
2423 | | | 2437 | |
2424 | /* | | 2438 | /* |
| | | 2439 | * Checks compatibility of an old-style function definition with a previous |
| | | 2440 | * prototype declaration. |
| | | 2441 | * Returns true if the position of the previous declaration should be reported. |
| | | 2442 | */ |
| | | 2443 | static bool |
| | | 2444 | check_prototype_declaration(sym_t *arg, sym_t *parg) |
| | | 2445 | { |
| | | 2446 | type_t *tp = arg->s_type; |
| | | 2447 | type_t *ptp = parg->s_type; |
| | | 2448 | bool dowarn = false; |
| | | 2449 | |
| | | 2450 | if (!types_compatible(tp, ptp, true, true, &dowarn)) { |
| | | 2451 | if (types_compatible(tp, ptp, true, false, &dowarn)) { |
| | | 2452 | /* type of '%s' does not match prototype */ |
| | | 2453 | return gnuism(58, arg->s_name); |
| | | 2454 | } else { |
| | | 2455 | /* type of '%s' does not match prototype */ |
| | | 2456 | error(58, arg->s_name); |
| | | 2457 | return true; |
| | | 2458 | } |
| | | 2459 | } |
| | | 2460 | if (dowarn) { |
| | | 2461 | /* TODO: Make this an error in C99 mode as well. */ |
| | | 2462 | if (!allow_trad && !allow_c99) |
| | | 2463 | /* type of '%s' does not match prototype */ |
| | | 2464 | error(58, arg->s_name); |
| | | 2465 | else |
| | | 2466 | /* type of '%s' does not match prototype */ |
| | | 2467 | warning(58, arg->s_name); |
| | | 2468 | return true; |
| | | 2469 | } |
| | | 2470 | |
| | | 2471 | return false; |
| | | 2472 | } |
| | | 2473 | |
| | | 2474 | /* |
2425 | * Warn about arguments in old-style function definitions that default to int. | | 2475 | * Warn about arguments in old-style function definitions that default to int. |
2426 | * Check that an old-style function definition is compatible to a previous | | 2476 | * Check that an old-style function definition is compatible to a previous |
2427 | * prototype. | | 2477 | * prototype. |
2428 | */ | | 2478 | */ |
2429 | void | | 2479 | void |
2430 | check_func_old_style_arguments(void) | | 2480 | check_func_old_style_arguments(void) |
2431 | { | | 2481 | { |
2432 | int narg; | | 2482 | int narg; |
2433 | int nparg; | | 2483 | int nparg; |
2434 | bool msg; | | 2484 | bool msg; |
2435 | | | 2485 | |
2436 | sym_t *args = funcsym->u.s_old_style_args; | | 2486 | sym_t *args = funcsym->u.s_old_style_args; |
2437 | sym_t *pargs = funcsym->s_type->t_args; | | 2487 | sym_t *pargs = funcsym->s_type->t_args; |
2438 | | | 2488 | |
2439 | /* | | 2489 | /* |
2440 | * print a warning for each argument of an old-style function | | 2490 | * print a warning for each argument of an old-style function |
2441 | * definition which defaults to int | | 2491 | * definition which defaults to int |
2442 | */ | | 2492 | */ |
2443 | for (sym_t *arg = args; arg != NULL; arg = arg->s_next) { | | 2493 | for (sym_t *arg = args; arg != NULL; arg = arg->s_next) { |
2444 | if (arg->s_defarg) { | | 2494 | if (arg->s_defarg) { |
2445 | /* type of argument '%s' defaults to 'int' */ | | 2495 | /* type of argument '%s' defaults to 'int' */ |
2446 | warning(32, arg->s_name); | | 2496 | warning(32, arg->s_name); |
2447 | arg->s_defarg = false; | | 2497 | arg->s_defarg = false; |
2448 | mark_as_set(arg); | | 2498 | mark_as_set(arg); |
2449 | } | | 2499 | } |
2450 | } | | 2500 | } |
2451 | | | 2501 | |
2452 | /* | | 2502 | /* |
2453 | * If this is an old-style function definition and a prototype | | 2503 | * If this is an old-style function definition and a prototype |
2454 | * exists, compare the types of arguments. | | 2504 | * exists, compare the types of arguments. |
2455 | */ | | 2505 | */ |
2456 | if (funcsym->s_osdef && funcsym->s_type->t_proto) { | | 2506 | if (funcsym->s_osdef && funcsym->s_type->t_proto) { |
2457 | /* | | 2507 | /* |
2458 | * If the number of arguments does not match, we need not | | 2508 | * If the number of arguments does not match, we need not |
2459 | * continue. | | 2509 | * continue. |
2460 | */ | | 2510 | */ |
2461 | narg = nparg = 0; | | 2511 | narg = nparg = 0; |
2462 | msg = false; | | 2512 | msg = false; |
2463 | for (sym_t *parg = pargs; parg != NULL; parg = parg->s_next) | | 2513 | for (sym_t *parg = pargs; parg != NULL; parg = parg->s_next) |
2464 | nparg++; | | 2514 | nparg++; |
2465 | for (sym_t *arg = args; arg != NULL; arg = arg->s_next) | | 2515 | for (sym_t *arg = args; arg != NULL; arg = arg->s_next) |
2466 | narg++; | | 2516 | narg++; |
2467 | if (narg != nparg) { | | 2517 | if (narg != nparg) { |
2468 | /* parameter mismatch: %d declared, %d defined */ | | 2518 | /* parameter mismatch: %d declared, %d defined */ |
2469 | error(51, nparg, narg); | | 2519 | error(51, nparg, narg); |
2470 | msg = true; | | 2520 | msg = true; |
2471 | } else { | | 2521 | } else { |
2472 | sym_t *parg = pargs; | | 2522 | sym_t *parg = pargs; |
2473 | sym_t *arg = args; | | 2523 | sym_t *arg = args; |
2474 | while (narg-- > 0) { | | 2524 | while (narg-- > 0) { |
2475 | msg |= check_prototype_declaration(arg, parg); | | 2525 | msg |= check_prototype_declaration(arg, parg); |
2476 | parg = parg->s_next; | | 2526 | parg = parg->s_next; |
2477 | arg = arg->s_next; | | 2527 | arg = arg->s_next; |
2478 | } | | 2528 | } |
2479 | } | | 2529 | } |
2480 | if (msg && rflag) { | | 2530 | if (msg && rflag) { |
2481 | /* prototype declaration */ | | 2531 | /* prototype declaration */ |
2482 | message_at(285, &dcs->d_redeclared_symbol->s_def_pos); | | 2532 | message_at(285, &dcs->d_redeclared_symbol->s_def_pos); |
2483 | } | | 2533 | } |
2484 | | | 2534 | |
2485 | /* from now on the prototype is valid */ | | 2535 | /* from now on the prototype is valid */ |
2486 | funcsym->s_osdef = false; | | 2536 | funcsym->s_osdef = false; |
2487 | funcsym->u.s_old_style_args = NULL; | | 2537 | funcsym->u.s_old_style_args = NULL; |
2488 | } | | 2538 | } |
2489 | } | | 2539 | } |
2490 | | | 2540 | |
2491 | /* | | | |
2492 | * Checks compatibility of an old-style function definition with a previous | | | |
2493 | * prototype declaration. | | | |
2494 | * Returns true if the position of the previous declaration should be reported. | | | |
2495 | */ | | | |
2496 | static bool | | | |
2497 | check_prototype_declaration(sym_t *arg, sym_t *parg) | | | |
2498 | { | | | |
2499 | type_t *tp = arg->s_type; | | | |
2500 | type_t *ptp = parg->s_type; | | | |
2501 | bool dowarn = false; | | | |
2502 | | | | |
2503 | if (!types_compatible(tp, ptp, true, true, &dowarn)) { | | | |
2504 | if (types_compatible(tp, ptp, true, false, &dowarn)) { | | | |
2505 | /* type of '%s' does not match prototype */ | | | |
2506 | return gnuism(58, arg->s_name); | | | |
2507 | } else { | | | |
2508 | /* type of '%s' does not match prototype */ | | | |
2509 | error(58, arg->s_name); | | | |
2510 | return true; | | | |
2511 | } | | | |
2512 | } | | | |
2513 | if (dowarn) { | | | |
2514 | /* TODO: Make this an error in C99 mode as well. */ | | | |
2515 | if (!allow_trad && !allow_c99) | | | |
2516 | /* type of '%s' does not match prototype */ | | | |
2517 | error(58, arg->s_name); | | | |
2518 | else | | | |
2519 | /* type of '%s' does not match prototype */ | | | |
2520 | warning(58, arg->s_name); | | | |
2521 | return true; | | | |
2522 | } | | | |
2523 | | | | |
2524 | return false; | | | |
2525 | } | | | |
2526 | | | | |
2527 | static void | | 2541 | static void |
2528 | check_local_hiding(const sym_t *dsym) | | 2542 | check_local_hiding(const sym_t *dsym) |
2529 | { | | 2543 | { |
2530 | switch (dsym->s_scl) { | | 2544 | switch (dsym->s_scl) { |
2531 | case AUTO: | | 2545 | case AUTO: |
2532 | /* automatic '%s' hides external declaration */ | | 2546 | /* automatic '%s' hides external declaration */ |
2533 | warning(86, dsym->s_name); | | 2547 | warning(86, dsym->s_name); |
2534 | break; | | 2548 | break; |
2535 | case STATIC: | | 2549 | case STATIC: |
2536 | /* static '%s' hides external declaration */ | | 2550 | /* static '%s' hides external declaration */ |
2537 | warning(87, dsym->s_name); | | 2551 | warning(87, dsym->s_name); |
2538 | break; | | 2552 | break; |
2539 | case TYPEDEF: | | 2553 | case TYPEDEF: |
2540 | /* typedef '%s' hides external declaration */ | | 2554 | /* typedef '%s' hides external declaration */ |
2541 | warning(88, dsym->s_name); | | 2555 | warning(88, dsym->s_name); |
2542 | break; | | 2556 | break; |
2543 | case EXTERN: | | 2557 | case EXTERN: |
2544 | /* Already checked in declare_external_in_block. */ | | 2558 | /* Already checked in declare_external_in_block. */ |
2545 | break; | | 2559 | break; |
2546 | default: | | 2560 | default: |
2547 | lint_assert(/*CONSTCOND*/false); | | 2561 | lint_assert(/*CONSTCOND*/false); |
2548 | } | | 2562 | } |
2549 | } | | 2563 | } |
2550 | | | 2564 | |
2551 | static void | | 2565 | static void |
2552 | check_local_redeclaration(const sym_t *dsym, sym_t *rdsym) | | 2566 | check_local_redeclaration(const sym_t *dsym, sym_t *rdsym) |
2553 | { | | 2567 | { |
2554 | if (rdsym->s_block_level == 0) { | | 2568 | if (rdsym->s_block_level == 0) { |
2555 | if (hflag) | | 2569 | if (hflag) |
2556 | check_local_hiding(dsym); | | 2570 | check_local_hiding(dsym); |
2557 | | | 2571 | |
2558 | } else if (rdsym->s_block_level == block_level) { | | 2572 | } else if (rdsym->s_block_level == block_level) { |
2559 | | | 2573 | |
2560 | /* no hflag, because it's illegal! */ | | 2574 | /* no hflag, because it's illegal! */ |
2561 | if (rdsym->s_arg) { | | 2575 | if (rdsym->s_arg) { |
2562 | /* | | 2576 | /* |
2563 | * if allow_c90, a "redeclaration of '%s'" error | | 2577 | * if allow_c90, a "redeclaration of '%s'" error |
2564 | * is produced below | | 2578 | * is produced below |
2565 | */ | | 2579 | */ |
2566 | if (!allow_c90) { | | 2580 | if (!allow_c90) { |
2567 | if (hflag) { | | 2581 | if (hflag) { |
2568 | /* declaration of '%s' hides ... */ | | 2582 | /* declaration of '%s' hides ... */ |
2569 | warning(91, dsym->s_name); | | 2583 | warning(91, dsym->s_name); |
2570 | } | | 2584 | } |
2571 | rmsym(rdsym); | | 2585 | rmsym(rdsym); |
2572 | } | | 2586 | } |
2573 | } | | 2587 | } |
2574 | | | 2588 | |
2575 | } else if (rdsym->s_block_level < block_level) { | | 2589 | } else if (rdsym->s_block_level < block_level) { |
2576 | if (hflag) { | | 2590 | if (hflag) { |
2577 | /* declaration of '%s' hides earlier one */ | | 2591 | /* declaration of '%s' hides earlier one */ |
2578 | warning(95, dsym->s_name); | | 2592 | warning(95, dsym->s_name); |
| | | 2593 | } |
| | | 2594 | } |
| | | 2595 | |
| | | 2596 | if (rdsym->s_block_level == block_level) { |
| | | 2597 | /* redeclaration of '%s' */ |
| | | 2598 | error(27, dsym->s_name); |
| | | 2599 | rmsym(rdsym); |
| | | 2600 | } |
| | | 2601 | } |
| | | 2602 | |
| | | 2603 | /* Processes (re)declarations of external symbols inside blocks. */ |
| | | 2604 | static void |
| | | 2605 | declare_external_in_block(sym_t *dsym) |
| | | 2606 | { |
| | | 2607 | |
| | | 2608 | /* look for a symbol with the same name */ |
| | | 2609 | sym_t *esym = dcs->d_redeclared_symbol; |
| | | 2610 | while (esym != NULL && esym->s_block_level != 0) { |
| | | 2611 | while ((esym = esym->s_symtab_next) != NULL) { |
| | | 2612 | if (esym->s_kind != FVFT) |
| | | 2613 | continue; |
| | | 2614 | if (strcmp(dsym->s_name, esym->s_name) == 0) |
| | | 2615 | break; |
| | | 2616 | } |
| | | 2617 | } |
| | | 2618 | if (esym == NULL) |
| | | 2619 | return; |
| | | 2620 | if (esym->s_scl != EXTERN && esym->s_scl != STATIC) { |
| | | 2621 | /* gcc accepts this without a warning, pcc prints an error. */ |
| | | 2622 | /* redeclaration of '%s' */ |
| | | 2623 | warning(27, dsym->s_name); |
| | | 2624 | print_previous_declaration(esym); |
| | | 2625 | return; |
| | | 2626 | } |
| | | 2627 | |
| | | 2628 | bool dowarn = false; |
| | | 2629 | bool compatible = types_compatible(esym->s_type, dsym->s_type, |
| | | 2630 | false, false, &dowarn); |
| | | 2631 | |
| | | 2632 | if (!compatible || dowarn) { |
| | | 2633 | if (esym->s_scl == EXTERN) { |
| | | 2634 | /* inconsistent redeclaration of extern '%s' */ |
| | | 2635 | warning(90, dsym->s_name); |
| | | 2636 | print_previous_declaration(esym); |
| | | 2637 | } else { |
| | | 2638 | /* inconsistent redeclaration of static '%s' */ |
| | | 2639 | warning(92, dsym->s_name); |
| | | 2640 | print_previous_declaration(esym); |
2579 | } | | 2641 | } |
2580 | } | | 2642 | } |
2581 | | | 2643 | |
2582 | if (rdsym->s_block_level == block_level) { | | 2644 | if (compatible) { |
2583 | /* redeclaration of '%s' */ | | 2645 | /* |
2584 | error(27, dsym->s_name); | | 2646 | * Remember the external symbol, so we can update usage |
2585 | rmsym(rdsym); | | 2647 | * information at the end of the block. |
| | | 2648 | */ |
| | | 2649 | dsym->s_ext_sym = esym; |
2586 | } | | 2650 | } |
2587 | } | | 2651 | } |
2588 | | | 2652 | |
2589 | /* | | 2653 | /* |
2590 | * Completes a single local declaration/definition. | | 2654 | * Completes a single local declaration/definition. |
2591 | */ | | 2655 | */ |
2592 | void | | 2656 | void |
2593 | declare_local(sym_t *dsym, bool has_initializer) | | 2657 | declare_local(sym_t *dsym, bool has_initializer) |
2594 | { | | 2658 | { |
2595 | | | 2659 | |
2596 | /* Correct a mistake done in declarator_name(). */ | | 2660 | /* Correct a mistake done in declarator_name(). */ |
2597 | if (dsym->s_type->t_tspec == FUNC) { | | 2661 | if (dsym->s_type->t_tspec == FUNC) { |
2598 | dsym->s_def = DECL; | | 2662 | dsym->s_def = DECL; |
2599 | if (dcs->d_scl == NOSCL) | | 2663 | if (dcs->d_scl == NOSCL) |
2600 | dsym->s_scl = EXTERN; | | 2664 | dsym->s_scl = EXTERN; |
2601 | } | | 2665 | } |
2602 | | | 2666 | |
2603 | if (dsym->s_scl == EXTERN) { | | 2667 | if (dsym->s_scl == EXTERN) { |
2604 | /* nested 'extern' declaration of '%s' */ | | 2668 | /* nested 'extern' declaration of '%s' */ |
2605 | warning(352, dsym->s_name); | | 2669 | warning(352, dsym->s_name); |
2606 | } | | 2670 | } |
2607 | | | 2671 | |
2608 | if (dsym->s_type->t_tspec == FUNC) { | | 2672 | if (dsym->s_type->t_tspec == FUNC) { |
2609 | if (dsym->s_scl == STATIC) { | | 2673 | if (dsym->s_scl == STATIC) { |
2610 | /* dubious static function '%s' at block level */ | | 2674 | /* dubious static function '%s' at block level */ |
2611 | warning(93, dsym->s_name); | | 2675 | warning(93, dsym->s_name); |
2612 | dsym->s_scl = EXTERN; | | 2676 | dsym->s_scl = EXTERN; |
2613 | } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) { | | 2677 | } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) { |
2614 | /* function '%s' has illegal storage class */ | | 2678 | /* function '%s' has illegal storage class */ |
2615 | error(94, dsym->s_name); | | 2679 | error(94, dsym->s_name); |
2616 | dsym->s_scl = EXTERN; | | 2680 | dsym->s_scl = EXTERN; |
2617 | } | | 2681 | } |
2618 | } | | 2682 | } |
2619 | | | 2683 | |
2620 | /* | | 2684 | /* |
2621 | * functions may be declared inline at local scope, although | | 2685 | * functions may be declared inline at local scope, although |
2622 | * this has no effect for a later definition of the same | | 2686 | * this has no effect for a later definition of the same |
2623 | * function. | | 2687 | * function. |
2624 | * XXX it should have an effect if !allow_c90 is set. this would | | 2688 | * XXX it should have an effect if !allow_c90 is set. this would |
2625 | * also be the way gcc behaves. | | 2689 | * also be the way gcc behaves. |
2626 | */ | | 2690 | */ |
2627 | if (dcs->d_inline) { | | 2691 | if (dcs->d_inline) { |
2628 | if (dsym->s_type->t_tspec == FUNC) | | 2692 | if (dsym->s_type->t_tspec == FUNC) |
2629 | dsym->s_inline = true; | | 2693 | dsym->s_inline = true; |
2630 | else { | | 2694 | else { |
2631 | /* variable '%s' declared inline */ | | 2695 | /* variable '%s' declared inline */ |
2632 | warning(268, dsym->s_name); | | 2696 | warning(268, dsym->s_name); |
2633 | } | | 2697 | } |
2634 | } | | 2698 | } |
2635 | | | 2699 | |
2636 | check_function_definition(dsym, true); | | 2700 | check_function_definition(dsym, true); |
2637 | | | 2701 | |
2638 | check_type(dsym); | | 2702 | check_type(dsym); |
2639 | | | 2703 | |
2640 | if (dcs->d_redeclared_symbol != NULL && dsym->s_scl == EXTERN) | | 2704 | if (dcs->d_redeclared_symbol != NULL && dsym->s_scl == EXTERN) |
2641 | declare_external_in_block(dsym); | | 2705 | declare_external_in_block(dsym); |
2642 | | | 2706 | |
2643 | if (dsym->s_scl == EXTERN) { | | 2707 | if (dsym->s_scl == EXTERN) { |
2644 | /* | | 2708 | /* |
2645 | * XXX if the static variable at level 0 is only defined | | 2709 | * XXX if the static variable at level 0 is only defined |
2646 | * later, checking will be possible. | | 2710 | * later, checking will be possible. |
2647 | */ | | 2711 | */ |
2648 | if (dsym->s_ext_sym == NULL) | | 2712 | if (dsym->s_ext_sym == NULL) |
2649 | outsym(dsym, EXTERN, dsym->s_def); | | 2713 | outsym(dsym, EXTERN, dsym->s_def); |
2650 | else | | 2714 | else |
2651 | outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def); | | 2715 | outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def); |
2652 | } | | 2716 | } |
2653 | | | 2717 | |
2654 | if (dcs->d_redeclared_symbol != NULL) | | 2718 | if (dcs->d_redeclared_symbol != NULL) |
2655 | check_local_redeclaration(dsym, dcs->d_redeclared_symbol); | | 2719 | check_local_redeclaration(dsym, dcs->d_redeclared_symbol); |
2656 | | | 2720 | |
2657 | if (has_initializer && !check_init(dsym)) { | | 2721 | if (has_initializer && !check_init(dsym)) { |
2658 | dsym->s_def = DEF; | | 2722 | dsym->s_def = DEF; |
2659 | mark_as_set(dsym); | | 2723 | mark_as_set(dsym); |
2660 | } | | 2724 | } |
2661 | | | 2725 | |
2662 | if (dsym->s_scl == TYPEDEF) { | | 2726 | if (dsym->s_scl == TYPEDEF) { |
2663 | dsym->s_type = block_dup_type(dsym->s_type); | | 2727 | dsym->s_type = block_dup_type(dsym->s_type); |
2664 | dsym->s_type->t_typedef = true; | | 2728 | dsym->s_type->t_typedef = true; |
2665 | set_first_typedef(dsym->s_type, dsym); | | 2729 | set_first_typedef(dsym->s_type, dsym); |
2666 | } | | 2730 | } |
2667 | | | 2731 | |
2668 | if (dsym->s_scl == STATIC && any_query_enabled) { | | 2732 | if (dsym->s_scl == STATIC && any_query_enabled) { |
2669 | /* static variable '%s' in function */ | | 2733 | /* static variable '%s' in function */ |
2670 | query_message(11, dsym->s_name); | | 2734 | query_message(11, dsym->s_name); |
2671 | } | | 2735 | } |
2672 | } | | 2736 | } |
2673 | | | 2737 | |
2674 | /* Processes (re)declarations of external symbols inside blocks. */ | | | |
2675 | static void | | | |
2676 | declare_external_in_block(sym_t *dsym) | | | |
2677 | { | | | |
2678 | | | | |
2679 | /* look for a symbol with the same name */ | | | |
2680 | sym_t *esym = dcs->d_redeclared_symbol; | | | |
2681 | while (esym != NULL && esym->s_block_level != 0) { | | | |
2682 | while ((esym = esym->s_symtab_next) != NULL) { | | | |
2683 | if (esym->s_kind != FVFT) | | | |
2684 | continue; | | | |
2685 | if (strcmp(dsym->s_name, esym->s_name) == 0) | | | |
2686 | break; | | | |
2687 | } | | | |
2688 | } | | | |
2689 | if (esym == NULL) | | | |
2690 | return; | | | |
2691 | if (esym->s_scl != EXTERN && esym->s_scl != STATIC) { | | | |
2692 | /* gcc accepts this without a warning, pcc prints an error. */ | | | |
2693 | /* redeclaration of '%s' */ | | | |
2694 | warning(27, dsym->s_name); | | | |
2695 | print_previous_declaration(esym); | | | |
2696 | return; | | | |
2697 | } | | | |
2698 | | | | |
2699 | bool dowarn = false; | | | |
2700 | bool compatible = types_compatible(esym->s_type, dsym->s_type, | | | |
2701 | false, false, &dowarn); | | | |
2702 | | | | |
2703 | if (!compatible || dowarn) { | | | |
2704 | if (esym->s_scl == EXTERN) { | | | |
2705 | /* inconsistent redeclaration of extern '%s' */ | | | |
2706 | warning(90, dsym->s_name); | | | |
2707 | print_previous_declaration(esym); | | | |
2708 | } else { | | | |
2709 | /* inconsistent redeclaration of static '%s' */ | | | |
2710 | warning(92, dsym->s_name); | | | |
2711 | print_previous_declaration(esym); | | | |
2712 | } | | | |
2713 | } | | | |
2714 | | | | |
2715 | if (compatible) { | | | |
2716 | /* | | | |
2717 | * Remember the external symbol, so we can update usage | | | |
2718 | * information at the end of the block. | | | |
2719 | */ | | | |
2720 | dsym->s_ext_sym = esym; | | | |
2721 | } | | | |
2722 | } | | | |
2723 | | | | |
2724 | /* | | | |
2725 | * Check whether the symbol cannot be initialized due to type/storage class. | | | |
2726 | * Return whether an error has been detected. | | | |
2727 | */ | | | |
2728 | static bool | | | |
2729 | check_init(sym_t *sym) | | | |
2730 | { | | | |
2731 | | | | |
2732 | if (sym->s_type->t_tspec == FUNC) { | | | |
2733 | /* cannot initialize function '%s' */ | | | |
2734 | error(24, sym->s_name); | | | |
2735 | return true; | | | |
2736 | } | | | |
2737 | if (sym->s_scl == TYPEDEF) { | | | |
2738 | /* cannot initialize typedef '%s' */ | | | |
2739 | error(25, sym->s_name); | | | |
2740 | return true; | | | |
2741 | } | | | |
2742 | if (sym->s_scl == EXTERN && sym->s_def == DECL) { | | | |
2743 | if (dcs->d_kind == DLK_EXTERN) { | | | |
2744 | /* cannot initialize extern declaration '%s' */ | | | |
2745 | warning(26, sym->s_name); | | | |
2746 | } else { | | | |
2747 | /* cannot initialize extern declaration '%s' */ | | | |
2748 | error(26, sym->s_name); | | | |
2749 | return true; | | | |
2750 | } | | | |
2751 | } | | | |
2752 | | | | |
2753 | return false; | | | |
2754 | } | | | |
2755 | | | | |
2756 | /* Create a symbol for an abstract declaration. */ | | 2738 | /* Create a symbol for an abstract declaration. */ |
2757 | sym_t * | | 2739 | sym_t * |
2758 | abstract_name(void) | | 2740 | abstract_name(void) |
2759 | { | | 2741 | { |
2760 | | | 2742 | |
2761 | lint_assert(dcs->d_kind == DLK_ABSTRACT | | 2743 | lint_assert(dcs->d_kind == DLK_ABSTRACT |
2762 | || dcs->d_kind == DLK_PROTO_PARAMS); | | 2744 | || dcs->d_kind == DLK_PROTO_PARAMS); |
2763 | | | 2745 | |
2764 | sym_t *sym = block_zero_alloc(sizeof(*sym), "sym"); | | 2746 | sym_t *sym = block_zero_alloc(sizeof(*sym), "sym"); |
2765 | sym->s_name = unnamed; | | 2747 | sym->s_name = unnamed; |
2766 | sym->s_def = DEF; | | 2748 | sym->s_def = DEF; |
2767 | sym->s_scl = ABSTRACT; | | 2749 | sym->s_scl = ABSTRACT; |
2768 | sym->s_block_level = -1; | | 2750 | sym->s_block_level = -1; |
2769 | sym->s_arg = dcs->d_kind == DLK_PROTO_PARAMS; | | 2751 | sym->s_arg = dcs->d_kind == DLK_PROTO_PARAMS; |
2770 | | | 2752 | |
2771 | /* | | 2753 | /* |
2772 | * At this point, dcs->d_type contains only the basic type. That | | 2754 | * At this point, dcs->d_type contains only the basic type. That |
2773 | * type will be updated later, adding pointers, arrays and functions | | 2755 | * type will be updated later, adding pointers, arrays and functions |
2774 | * as necessary. | | 2756 | * as necessary. |
2775 | */ | | 2757 | */ |
2776 | /* | | 2758 | /* |
2777 | * XXX: This is not the correct type. For example in msg_347, it is | | 2759 | * XXX: This is not the correct type. For example in msg_347, it is |
2778 | * the type of the last prototype parameter, but it should rather be | | 2760 | * the type of the last prototype parameter, but it should rather be |
2779 | * the return type of the function. | | 2761 | * the return type of the function. |
2780 | */ | | 2762 | */ |
2781 | sym->s_type = dcs->d_type; | | 2763 | sym->s_type = dcs->d_type; |
2782 | dcs->d_redeclared_symbol = NULL; | | 2764 | dcs->d_redeclared_symbol = NULL; |
2783 | | | 2765 | |
2784 | return sym; | | 2766 | return sym; |
2785 | } | | 2767 | } |
2786 | | | 2768 | |
2787 | /* Removes anything which has nothing to do on global level. */ | | 2769 | /* Removes anything which has nothing to do on global level. */ |
2788 | void | | 2770 | void |
2789 | global_clean_up(void) | | 2771 | global_clean_up(void) |
2790 | { | | 2772 | { |
2791 | | | 2773 | |
2792 | while (dcs->d_enclosing != NULL) | | 2774 | while (dcs->d_enclosing != NULL) |
2793 | end_declaration_level(); | | 2775 | end_declaration_level(); |
2794 | | | 2776 | |
2795 | clean_up_after_error(); | | 2777 | clean_up_after_error(); |
2796 | block_level = 0; | | 2778 | block_level = 0; |
2797 | mem_block_level = 0; | | 2779 | mem_block_level = 0; |
2798 | debug_step("%s: mem_block_level = %zu", __func__, mem_block_level); | | 2780 | debug_step("%s: mem_block_level = %zu", __func__, mem_block_level); |
2799 | global_clean_up_decl(true); | | 2781 | global_clean_up_decl(true); |
2800 | } | | 2782 | } |
2801 | | | 2783 | |
2802 | sym_t * | | 2784 | sym_t * |
2803 | declare_abstract_type(sym_t *sym) | | 2785 | declare_abstract_type(sym_t *sym) |
2804 | { | | 2786 | { |
2805 | | | 2787 | |
2806 | check_function_definition(sym, true); | | 2788 | check_function_definition(sym, true); |
2807 | check_type(sym); | | 2789 | check_type(sym); |
2808 | return sym; | | 2790 | return sym; |
2809 | } | | 2791 | } |
2810 | | | 2792 | |
2811 | /* Checks size after declarations of variables and their initialization. */ | | 2793 | /* Checks size after declarations of variables and their initialization. */ |
2812 | void | | 2794 | void |
2813 | check_size(sym_t *dsym) | | 2795 | check_size(sym_t *dsym) |
2814 | { | | 2796 | { |
2815 | | | 2797 | |
2816 | if (dsym->s_def == DEF && | | 2798 | if (dsym->s_def == DEF && |
2817 | dsym->s_scl != TYPEDEF && | | 2799 | dsym->s_scl != TYPEDEF && |
2818 | dsym->s_type->t_tspec != FUNC && | | 2800 | dsym->s_type->t_tspec != FUNC && |
2819 | length_in_bits(dsym->s_type, dsym->s_name) == 0 && | | 2801 | length_in_bits(dsym->s_type, dsym->s_name) == 0 && |
2820 | dsym->s_type->t_tspec == ARRAY && | | 2802 | dsym->s_type->t_tspec == ARRAY && |
2821 | dsym->s_type->t_dim == 0) { | | 2803 | dsym->s_type->t_dim == 0) { |
2822 | if (!allow_c90) | | 2804 | if (!allow_c90) |
2823 | /* empty array declaration for '%s' */ | | 2805 | /* empty array declaration for '%s' */ |
2824 | warning(190, dsym->s_name); | | 2806 | warning(190, dsym->s_name); |
2825 | else | | 2807 | else |
2826 | /* empty array declaration for '%s' */ | | 2808 | /* empty array declaration for '%s' */ |
2827 | error(190, dsym->s_name); | | 2809 | error(190, dsym->s_name); |
2828 | } | | 2810 | } |
2829 | } | | 2811 | } |
2830 | | | 2812 | |
2831 | /* Mark an object as set if it is not already. */ | | 2813 | /* Mark an object as set if it is not already. */ |
2832 | void | | 2814 | void |
2833 | mark_as_set(sym_t *sym) | | 2815 | mark_as_set(sym_t *sym) |
2834 | { | | 2816 | { |
2835 | | | 2817 | |
2836 | if (!sym->s_set) { | | 2818 | if (!sym->s_set) { |
2837 | sym->s_set = true; | | 2819 | sym->s_set = true; |
2838 | sym->s_set_pos = unique_curr_pos(); | | 2820 | sym->s_set_pos = unique_curr_pos(); |
2839 | } | | 2821 | } |
2840 | } | | 2822 | } |
2841 | | | 2823 | |
2842 | /* Mark an object as used if it is not already. */ | | 2824 | /* Mark an object as used if it is not already. */ |
2843 | void | | 2825 | void |
2844 | mark_as_used(sym_t *sym, bool fcall, bool szof) | | 2826 | mark_as_used(sym_t *sym, bool fcall, bool szof) |
2845 | { | | 2827 | { |
2846 | | | 2828 | |
2847 | if (!sym->s_used) { | | 2829 | if (!sym->s_used) { |
2848 | sym->s_used = true; | | 2830 | sym->s_used = true; |
2849 | sym->s_use_pos = unique_curr_pos(); | | 2831 | sym->s_use_pos = unique_curr_pos(); |
2850 | } | | 2832 | } |
2851 | /* | | 2833 | /* |
2852 | * For function calls, another record is written. | | 2834 | * For function calls, another record is written. |
2853 | * | | 2835 | * |
2854 | * XXX: Should symbols used in sizeof() be treated as used or not? | | 2836 | * XXX: Should symbols used in sizeof() be treated as used or not? |
2855 | * Probably not, because there is no point in declaring an external | | 2837 | * Probably not, because there is no point in declaring an external |
2856 | * variable only to get its size. | | 2838 | * variable only to get its size. |
2857 | */ | | 2839 | */ |
2858 | if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN) | | 2840 | if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN) |
2859 | outusg(sym); | | 2841 | outusg(sym); |
2860 | } | | 2842 | } |
2861 | | | 2843 | |
2862 | /* Warns about variables and labels that are not used or only set. */ | | 2844 | /* Warns about variables and labels that are not used or only set. */ |
2863 | void | | 2845 | void |
2864 | check_usage(decl_level *dl) | | 2846 | check_usage(decl_level *dl) |
2865 | { | | 2847 | { |
2866 | /* for this warning LINTED has no effect */ | | 2848 | /* for this warning LINTED has no effect */ |
2867 | int saved_lwarn = lwarn; | | 2849 | int saved_lwarn = lwarn; |
2868 | lwarn = LWARN_ALL; | | 2850 | lwarn = LWARN_ALL; |
2869 | | | 2851 | |
2870 | debug_step("begin lwarn %d", lwarn); | | 2852 | debug_step("begin lwarn %d", lwarn); |
2871 | for (sym_t *sym = dl->d_first_dlsym; | | 2853 | for (sym_t *sym = dl->d_first_dlsym; |
2872 | sym != NULL; sym = sym->s_level_next) | | 2854 | sym != NULL; sym = sym->s_level_next) |
2873 | check_usage_sym(dl->d_asm, sym); | | 2855 | check_usage_sym(dl->d_asm, sym); |
2874 | lwarn = saved_lwarn; | | 2856 | lwarn = saved_lwarn; |
2875 | debug_step("end lwarn %d", lwarn); | | 2857 | debug_step("end lwarn %d", lwarn); |
2876 | } | | 2858 | } |
2877 | | | 2859 | |
2878 | /* Warns about a variable or a label that is not used or only set. */ | | | |
2879 | void | | | |
2880 | check_usage_sym(bool novar, sym_t *sym) | | | |
2881 | { | | | |
2882 | | | | |
2883 | if (sym->s_block_level == -1) | | | |
2884 | return; | | | |
2885 | | | | |
2886 | if (sym->s_kind == FVFT && sym->s_arg) | | | |
2887 | check_argument_usage(novar, sym); | | | |
2888 | else if (sym->s_kind == FVFT) | | | |
2889 | check_variable_usage(novar, sym); | | | |
2890 | else if (sym->s_kind == FLABEL) | | | |
2891 | check_label_usage(sym); | | | |
2892 | else if (sym->s_kind == FTAG) | | | |
2893 | check_tag_usage(sym); | | | |
2894 | } | | | |
2895 | | | | |
2896 | static void | | 2860 | static void |
2897 | check_argument_usage(bool novar, sym_t *arg) | | 2861 | check_argument_usage(bool novar, sym_t *arg) |
2898 | { | | 2862 | { |
2899 | | | 2863 | |
2900 | lint_assert(arg->s_set); | | 2864 | lint_assert(arg->s_set); |
2901 | | | 2865 | |
2902 | if (novar) | | 2866 | if (novar) |
2903 | return; | | 2867 | return; |
2904 | | | 2868 | |
2905 | if (!arg->s_used && !vflag) { | | 2869 | if (!arg->s_used && !vflag) { |
2906 | /* parameter '%s' unused in function '%s' */ | | 2870 | /* parameter '%s' unused in function '%s' */ |
2907 | warning_at(231, &arg->s_def_pos, arg->s_name, funcsym->s_name); | | 2871 | warning_at(231, &arg->s_def_pos, arg->s_name, funcsym->s_name); |
2908 | } | | 2872 | } |
2909 | } | | 2873 | } |
2910 | | | 2874 | |
2911 | static void | | 2875 | static void |
2912 | check_variable_usage(bool novar, sym_t *sym) | | 2876 | check_variable_usage(bool novar, sym_t *sym) |
2913 | { | | 2877 | { |
2914 | | | 2878 | |
2915 | lint_assert(block_level != 0); | | 2879 | lint_assert(block_level != 0); |
2916 | | | 2880 | |
2917 | /* example at file scope: int c = ({ return 3; }); */ | | 2881 | /* example at file scope: int c = ({ return 3; }); */ |
2918 | if (sym->s_block_level == 0 && ch_isdigit(sym->s_name[0])) | | 2882 | if (sym->s_block_level == 0 && ch_isdigit(sym->s_name[0])) |
2919 | return; | | 2883 | return; |
2920 | | | 2884 | |
2921 | /* errors in expressions easily cause lots of these warnings */ | | 2885 | /* errors in expressions easily cause lots of these warnings */ |
2922 | if (seen_error) | | 2886 | if (seen_error) |
2923 | return; | | 2887 | return; |
2924 | | | 2888 | |
2925 | /* | | 2889 | /* |
2926 | * XXX Only variables are checked, although types should | | 2890 | * XXX Only variables are checked, although types should |
2927 | * probably also be checked | | 2891 | * probably also be checked |
2928 | */ | | 2892 | */ |
2929 | scl_t sc = sym->s_scl; | | 2893 | scl_t sc = sym->s_scl; |
2930 | if (sc != EXTERN && sc != STATIC && sc != AUTO && sc != REG) | | 2894 | if (sc != EXTERN && sc != STATIC && sc != AUTO && sc != REG) |
2931 | return; | | 2895 | return; |
2932 | | | 2896 | |
2933 | if (novar) | | 2897 | if (novar) |
2934 | return; | | 2898 | return; |
2935 | | | 2899 | |
2936 | if (sc == EXTERN) { | | 2900 | if (sc == EXTERN) { |
2937 | if (!sym->s_used && !sym->s_set) { | | 2901 | if (!sym->s_used && !sym->s_set) { |
2938 | /* '%s' unused in function '%s' */ | | 2902 | /* '%s' unused in function '%s' */ |
2939 | warning_at(192, &sym->s_def_pos, | | 2903 | warning_at(192, &sym->s_def_pos, |
2940 | sym->s_name, funcsym->s_name); | | 2904 | sym->s_name, funcsym->s_name); |
2941 | } | | 2905 | } |
2942 | } else { | | 2906 | } else { |
2943 | if (sym->s_set && !sym->s_used) { | | 2907 | if (sym->s_set && !sym->s_used) { |
2944 | /* '%s' set but not used in function '%s' */ | | 2908 | /* '%s' set but not used in function '%s' */ |
2945 | warning_at(191, &sym->s_set_pos, | | 2909 | warning_at(191, &sym->s_set_pos, |
2946 | sym->s_name, funcsym->s_name); | | 2910 | sym->s_name, funcsym->s_name); |
2947 | } else if (!sym->s_used) { | | 2911 | } else if (!sym->s_used) { |
2948 | /* '%s' unused in function '%s' */ | | 2912 | /* '%s' unused in function '%s' */ |
2949 | warning_at(192, &sym->s_def_pos, | | 2913 | warning_at(192, &sym->s_def_pos, |
2950 | sym->s_name, funcsym->s_name); | | 2914 | sym->s_name, funcsym->s_name); |
2951 | } | | 2915 | } |
2952 | } | | 2916 | } |
2953 | | | 2917 | |
2954 | if (sc == EXTERN) { | | 2918 | if (sc == EXTERN) { |
2955 | /* | | 2919 | /* |
2956 | * information about usage is taken over into the symbol | | 2920 | * information about usage is taken over into the symbol |
2957 | * table entry at level 0 if the symbol was locally declared | | 2921 | * table entry at level 0 if the symbol was locally declared |
2958 | * as an external symbol. | | 2922 | * as an external symbol. |
2959 | * | | 2923 | * |
2960 | * XXX This is wrong for symbols declared static at level 0 | | 2924 | * XXX This is wrong for symbols declared static at level 0 |
2961 | * if the usage information stems from sizeof(). This is | | 2925 | * if the usage information stems from sizeof(). This is |
2962 | * because symbols at level 0 only used in sizeof() are | | 2926 | * because symbols at level 0 only used in sizeof() are |
2963 | * considered to not be used. | | 2927 | * considered to not be used. |
2964 | */ | | 2928 | */ |
2965 | sym_t *xsym = sym->s_ext_sym; | | 2929 | sym_t *xsym = sym->s_ext_sym; |
2966 | if (xsym != NULL) { | | 2930 | if (xsym != NULL) { |
2967 | if (sym->s_used && !xsym->s_used) { | | 2931 | if (sym->s_used && !xsym->s_used) { |
2968 | xsym->s_used = true; | | 2932 | xsym->s_used = true; |
2969 | xsym->s_use_pos = sym->s_use_pos; | | 2933 | xsym->s_use_pos = sym->s_use_pos; |
2970 | } | | 2934 | } |
2971 | if (sym->s_set && !xsym->s_set) { | | 2935 | if (sym->s_set && !xsym->s_set) { |
2972 | xsym->s_set = true; | | 2936 | xsym->s_set = true; |
2973 | xsym->s_set_pos = sym->s_set_pos; | | 2937 | xsym->s_set_pos = sym->s_set_pos; |
2974 | } | | 2938 | } |
2975 | } | | 2939 | } |
2976 | } | | 2940 | } |
2977 | } | | 2941 | } |
2978 | | | 2942 | |
2979 | static void | | 2943 | static void |
2980 | check_label_usage(sym_t *lab) | | 2944 | check_label_usage(sym_t *lab) |
2981 | { | | 2945 | { |
2982 | | | 2946 | |
2983 | lint_assert(block_level == 1); | | 2947 | lint_assert(block_level == 1); |
2984 | lint_assert(lab->s_block_level == 1); | | 2948 | lint_assert(lab->s_block_level == 1); |
2985 | | | 2949 | |
2986 | if (funcsym == NULL) | | 2950 | if (funcsym == NULL) |
2987 | /* syntax error '%s' */ | | 2951 | /* syntax error '%s' */ |
2988 | error(249, "labels are only valid inside a function"); | | 2952 | error(249, "labels are only valid inside a function"); |
2989 | else if (lab->s_set && !lab->s_used) | | 2953 | else if (lab->s_set && !lab->s_used) |
2990 | /* label '%s' unused in function '%s' */ | | 2954 | /* label '%s' unused in function '%s' */ |
2991 | warning_at(232, &lab->s_set_pos, lab->s_name, funcsym->s_name); | | 2955 | warning_at(232, &lab->s_set_pos, lab->s_name, funcsym->s_name); |
2992 | else if (!lab->s_set) | | 2956 | else if (!lab->s_set) |
2993 | /* undefined label '%s' */ | | 2957 | /* undefined label '%s' */ |
2994 | warning_at(23, &lab->s_use_pos, lab->s_name); | | 2958 | warning_at(23, &lab->s_use_pos, lab->s_name); |
2995 | } | | 2959 | } |
2996 | | | 2960 | |
2997 | static void | | 2961 | static void |
2998 | check_tag_usage(sym_t *sym) | | 2962 | check_tag_usage(sym_t *sym) |
2999 | { | | 2963 | { |
3000 | | | 2964 | |
3001 | if (!is_incomplete(sym->s_type)) | | 2965 | if (!is_incomplete(sym->s_type)) |
3002 | return; | | 2966 | return; |
3003 | | | 2967 | |
3004 | /* always complain about incomplete tags declared inside blocks */ | | 2968 | /* always complain about incomplete tags declared inside blocks */ |
3005 | if (zflag || dcs->d_kind != DLK_EXTERN) | | 2969 | if (zflag || dcs->d_kind != DLK_EXTERN) |
3006 | return; | | 2970 | return; |
3007 | | | 2971 | |
3008 | switch (sym->s_type->t_tspec) { | | 2972 | switch (sym->s_type->t_tspec) { |
3009 | case STRUCT: | | 2973 | case STRUCT: |
3010 | /* struct '%s' never defined */ | | 2974 | /* struct '%s' never defined */ |
3011 | warning_at(233, &sym->s_def_pos, sym->s_name); | | 2975 | warning_at(233, &sym->s_def_pos, sym->s_name); |
3012 | break; | | 2976 | break; |
3013 | case UNION: | | 2977 | case UNION: |
3014 | /* union '%s' never defined */ | | 2978 | /* union '%s' never defined */ |
3015 | warning_at(234, &sym->s_def_pos, sym->s_name); | | 2979 | warning_at(234, &sym->s_def_pos, sym->s_name); |
3016 | break; | | 2980 | break; |
3017 | default: | | 2981 | default: |
3018 | lint_assert(sym->s_type->t_tspec == ENUM); | | 2982 | lint_assert(sym->s_type->t_tspec == ENUM); |
3019 | /* enum '%s' never defined */ | | 2983 | /* enum '%s' never defined */ |
3020 | warning_at(235, &sym->s_def_pos, sym->s_name); | | 2984 | warning_at(235, &sym->s_def_pos, sym->s_name); |
3021 | break; | | 2985 | break; |
3022 | } | | 2986 | } |
3023 | } | | 2987 | } |
3024 | | | 2988 | |
3025 | /* | | 2989 | /* Warns about a variable or a label that is not used or only set. */ |
3026 | * Called after the entire translation unit has been parsed. | | | |
3027 | * Changes tentative definitions into definitions. | | | |
3028 | * Performs some tests on global symbols. Detected problems are: | | | |
3029 | * - defined variables of incomplete type | | | |
3030 | * - constant variables which are not initialized | | | |
3031 | * - static symbols which are never used | | | |
3032 | */ | | | |
3033 | void | | 2990 | void |
3034 | check_global_symbols(void) | | 2991 | check_usage_sym(bool novar, sym_t *sym) |
3035 | { | | 2992 | { |
3036 | sym_t *sym; | | | |
3037 | | | 2993 | |
3038 | if (block_level != 0 || dcs->d_enclosing != NULL) | | 2994 | if (sym->s_block_level == -1) |
3039 | norecover(); | | 2995 | return; |
3040 | | | 2996 | |
3041 | for (sym = dcs->d_first_dlsym; sym != NULL; sym = sym->s_level_next) { | | 2997 | if (sym->s_kind == FVFT && sym->s_arg) |
3042 | if (sym->s_block_level == -1) | | 2998 | check_argument_usage(novar, sym); |
3043 | continue; | | 2999 | else if (sym->s_kind == FVFT) |
3044 | if (sym->s_kind == FVFT) | | 3000 | check_variable_usage(novar, sym); |
3045 | check_global_variable(sym); | | 3001 | else if (sym->s_kind == FLABEL) |
3046 | else if (sym->s_kind == FTAG) | | 3002 | check_label_usage(sym); |
3047 | check_tag_usage(sym); | | 3003 | else if (sym->s_kind == FTAG) |
3048 | else | | 3004 | check_tag_usage(sym); |
3049 | lint_assert(sym->s_kind == FMEMBER); | | 3005 | } |
| | | 3006 | |
| | | 3007 | static void |
| | | 3008 | check_global_variable_size(const sym_t *sym) |
| | | 3009 | { |
| | | 3010 | |
| | | 3011 | if (sym->s_def != TDEF) |
| | | 3012 | return; |
| | | 3013 | if (sym->s_type->t_tspec == FUNC) { |
| | | 3014 | /* Maybe a syntax error after a function declaration. */ |
| | | 3015 | return; |
| | | 3016 | } |
| | | 3017 | if (sym->s_def == TDEF && sym->s_type->t_tspec == VOID) { |
| | | 3018 | /* Prevent an internal error in length_in_bits below. */ |
| | | 3019 | return; |
| | | 3020 | } |
| | | 3021 | |
| | | 3022 | pos_t cpos = curr_pos; |
| | | 3023 | curr_pos = sym->s_def_pos; |
| | | 3024 | int len_in_bits = length_in_bits(sym->s_type, sym->s_name); |
| | | 3025 | curr_pos = cpos; |
| | | 3026 | |
| | | 3027 | if (len_in_bits == 0 && |
| | | 3028 | sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) { |
| | | 3029 | /* TODO: C99 6.7.5.2p1 defines this as an error as well. */ |
| | | 3030 | if (!allow_c90 || |
| | | 3031 | (sym->s_scl == EXTERN && (allow_trad || allow_c99))) { |
| | | 3032 | /* empty array declaration for '%s' */ |
| | | 3033 | warning_at(190, &sym->s_def_pos, sym->s_name); |
| | | 3034 | } else { |
| | | 3035 | /* empty array declaration for '%s' */ |
| | | 3036 | error_at(190, &sym->s_def_pos, sym->s_name); |
| | | 3037 | } |
3050 | } | | 3038 | } |
3051 | } | | 3039 | } |
3052 | | | 3040 | |
3053 | static void | | 3041 | static void |
3054 | check_unused_static_global_variable(const sym_t *sym) | | 3042 | check_unused_static_global_variable(const sym_t *sym) |
3055 | { | | 3043 | { |
3056 | if (sym->s_type->t_tspec == FUNC) { | | 3044 | if (sym->s_type->t_tspec == FUNC) { |
3057 | if (sym->s_def == DEF) { | | 3045 | if (sym->s_def == DEF) { |
3058 | if (!sym->s_inline) | | 3046 | if (!sym->s_inline) |
3059 | /* static function '%s' unused */ | | 3047 | /* static function '%s' unused */ |
3060 | warning_at(236, &sym->s_def_pos, sym->s_name); | | 3048 | warning_at(236, &sym->s_def_pos, sym->s_name); |
3061 | } else { | | 3049 | } else { |
3062 | /* static function '%s' declared but not defined */ | | 3050 | /* static function '%s' declared but not defined */ |
3063 | warning_at(290, &sym->s_def_pos, sym->s_name); | | 3051 | warning_at(290, &sym->s_def_pos, sym->s_name); |
3064 | } | | 3052 | } |
3065 | } else if (!sym->s_set) { | | 3053 | } else if (!sym->s_set) { |
3066 | /* static variable '%s' unused */ | | 3054 | /* static variable '%s' unused */ |
3067 | warning_at(226, &sym->s_def_pos, sym->s_name); | | 3055 | warning_at(226, &sym->s_def_pos, sym->s_name); |
3068 | } else { | | 3056 | } else { |
3069 | /* static variable '%s' set but not used */ | | 3057 | /* static variable '%s' set but not used */ |
3070 | warning_at(307, &sym->s_def_pos, sym->s_name); | | 3058 | warning_at(307, &sym->s_def_pos, sym->s_name); |
3071 | } | | 3059 | } |
3072 | } | | 3060 | } |
3073 | | | 3061 | |
3074 | static void | | 3062 | static void |
3075 | check_static_global_variable(const sym_t *sym) | | 3063 | check_static_global_variable(const sym_t *sym) |
3076 | { | | 3064 | { |
3077 | if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) { | | 3065 | if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) { |
3078 | /* static function '%s' called but not defined */ | | 3066 | /* static function '%s' called but not defined */ |
3079 | error_at(225, &sym->s_use_pos, sym->s_name); | | 3067 | error_at(225, &sym->s_use_pos, sym->s_name); |
3080 | } | | 3068 | } |
3081 | | | 3069 | |
3082 | if (!sym->s_used) | | 3070 | if (!sym->s_used) |
3083 | check_unused_static_global_variable(sym); | | 3071 | check_unused_static_global_variable(sym); |
3084 | | | 3072 | |
3085 | if (allow_c90 && sym->s_def == TDEF && sym->s_type->t_const) { | | 3073 | if (allow_c90 && sym->s_def == TDEF && sym->s_type->t_const) { |
3086 | /* const object '%s' should have initializer */ | | 3074 | /* const object '%s' should have initializer */ |
3087 | warning_at(227, &sym->s_def_pos, sym->s_name); | | 3075 | warning_at(227, &sym->s_def_pos, sym->s_name); |
3088 | } | | 3076 | } |
3089 | } | | 3077 | } |
3090 | | | 3078 | |
3091 | static void | | 3079 | static void |
3092 | check_global_variable(const sym_t *sym) | | 3080 | check_global_variable(const sym_t *sym) |
3093 | { | | 3081 | { |
3094 | scl_t scl = sym->s_scl; | | 3082 | scl_t scl = sym->s_scl; |
3095 | | | 3083 | |
3096 | if (scl == TYPEDEF || scl == BOOL_CONST || scl == ENUM_CONST) | | 3084 | if (scl == TYPEDEF || scl == BOOL_CONST || scl == ENUM_CONST) |
3097 | return; | | 3085 | return; |
3098 | | | 3086 | |
3099 | if (scl == NOSCL) | | 3087 | if (scl == NOSCL) |
3100 | return; /* May be caused by a syntax error. */ | | 3088 | return; /* May be caused by a syntax error. */ |
3101 | | | 3089 | |
3102 | lint_assert(scl == EXTERN || scl == STATIC); | | 3090 | lint_assert(scl == EXTERN || scl == STATIC); |
3103 | | | 3091 | |
3104 | check_global_variable_size(sym); | | 3092 | check_global_variable_size(sym); |
3105 | | | 3093 | |
3106 | if (scl == STATIC) | | 3094 | if (scl == STATIC) |
3107 | check_static_global_variable(sym); | | 3095 | check_static_global_variable(sym); |
3108 | } | | 3096 | } |
3109 | | | 3097 | |
3110 | static void | | 3098 | /* |
3111 | check_global_variable_size(const sym_t *sym) | | 3099 | * Called after the entire translation unit has been parsed. |
| | | 3100 | * Changes tentative definitions into definitions. |
| | | 3101 | * Performs some tests on global symbols. Detected problems are: |
| | | 3102 | * - defined variables of incomplete type |
| | | 3103 | * - constant variables which are not initialized |
| | | 3104 | * - static symbols which are never used |
| | | 3105 | */ |
| | | 3106 | void |
| | | 3107 | check_global_symbols(void) |
3112 | { | | 3108 | { |
| | | 3109 | sym_t *sym; |
3113 | | | 3110 | |
3114 | if (sym->s_def != TDEF) | | 3111 | if (block_level != 0 || dcs->d_enclosing != NULL) |
3115 | return; | | 3112 | norecover(); |
3116 | if (sym->s_type->t_tspec == FUNC) { | | | |
3117 | /* Maybe a syntax error after a function declaration. */ | | | |
3118 | return; | | | |
3119 | } | | | |
3120 | if (sym->s_def == TDEF && sym->s_type->t_tspec == VOID) { | | | |
3121 | /* Prevent an internal error in length_in_bits below. */ | | | |
3122 | return; | | | |
3123 | } | | | |
3124 | | | | |
3125 | pos_t cpos = curr_pos; | | | |
3126 | curr_pos = sym->s_def_pos; | | | |
3127 | int len_in_bits = length_in_bits(sym->s_type, sym->s_name); | | | |
3128 | curr_pos = cpos; | | | |
3129 | | | 3113 | |
3130 | if (len_in_bits == 0 && | | 3114 | for (sym = dcs->d_first_dlsym; sym != NULL; sym = sym->s_level_next) { |
3131 | sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) { | | 3115 | if (sym->s_block_level == -1) |
3132 | /* TODO: C99 6.7.5.2p1 defines this as an error as well. */ | | 3116 | continue; |
3133 | if (!allow_c90 || | | 3117 | if (sym->s_kind == FVFT) |
3134 | (sym->s_scl == EXTERN && (allow_trad || allow_c99))) { | | 3118 | check_global_variable(sym); |
3135 | /* empty array declaration for '%s' */ | | 3119 | else if (sym->s_kind == FTAG) |
3136 | warning_at(190, &sym->s_def_pos, sym->s_name); | | 3120 | check_tag_usage(sym); |
3137 | } else { | | 3121 | else |
3138 | /* empty array declaration for '%s' */ | | 3122 | lint_assert(sym->s_kind == FMEMBER); |
3139 | error_at(190, &sym->s_def_pos, sym->s_name); | | | |
3140 | } | | | |
3141 | } | | 3123 | } |
3142 | } | | 3124 | } |
3143 | | | 3125 | |
3144 | /* | | 3126 | /* |
3145 | * Prints information about location of previous definition/declaration. | | 3127 | * Prints information about location of previous definition/declaration. |
3146 | */ | | 3128 | */ |
3147 | void | | 3129 | void |
3148 | print_previous_declaration(const sym_t *psym) | | 3130 | print_previous_declaration(const sym_t *psym) |
3149 | { | | 3131 | { |
3150 | | | 3132 | |
3151 | if (!rflag) | | 3133 | if (!rflag) |
3152 | return; | | 3134 | return; |
3153 | | | 3135 | |
3154 | if (psym->s_def == DEF || psym->s_def == TDEF) { | | 3136 | if (psym->s_def == DEF || psym->s_def == TDEF) { |
3155 | /* previous definition of '%s' */ | | 3137 | /* previous definition of '%s' */ |
3156 | message_at(261, &psym->s_def_pos, psym->s_name); | | 3138 | message_at(261, &psym->s_def_pos, psym->s_name); |
3157 | } else { | | 3139 | } else { |
3158 | /* previous declaration of '%s' */ | | 3140 | /* previous declaration of '%s' */ |
3159 | message_at(260, &psym->s_def_pos, psym->s_name); | | 3141 | message_at(260, &psym->s_def_pos, psym->s_name); |
3160 | } | | 3142 | } |
3161 | } | | 3143 | } |
3162 | | | 3144 | |
3163 | /* | | 3145 | /* |
3164 | * Gets a node for a constant and returns the value of this constant | | 3146 | * Gets a node for a constant and returns the value of this constant |
3165 | * as integer. | | 3147 | * as integer. |
3166 | * | | 3148 | * |
3167 | * If the node is not constant or too large for int or of type float, | | 3149 | * If the node is not constant or too large for int or of type float, |
3168 | * a warning will be printed. | | 3150 | * a warning will be printed. |
3169 | * | | 3151 | * |
3170 | * to_int_constant() should be used only inside declarations. If it is used in | | 3152 | * to_int_constant() should be used only inside declarations. If it is used in |
3171 | * expressions, it frees the memory used for the expression. | | 3153 | * expressions, it frees the memory used for the expression. |
3172 | */ | | 3154 | */ |
3173 | int | | 3155 | int |
3174 | to_int_constant(tnode_t *tn, bool required) | | 3156 | to_int_constant(tnode_t *tn, bool required) |
3175 | { | | 3157 | { |
3176 | | | 3158 | |
3177 | if (tn == NULL) | | 3159 | if (tn == NULL) |
3178 | return 1; | | 3160 | return 1; |
3179 | | | 3161 | |
3180 | val_t *v = integer_constant(tn, required); | | 3162 | val_t *v = integer_constant(tn, required); |
3181 | bool is_unsigned = is_uinteger(v->v_tspec); | | 3163 | bool is_unsigned = is_uinteger(v->v_tspec); |
3182 | int64_t val = v->u.integer; | | 3164 | int64_t val = v->u.integer; |
3183 | free(v); | | 3165 | free(v); |
3184 | | | 3166 | |
3185 | /* | | 3167 | /* |
3186 | * Abstract declarations are used inside expression. To free | | 3168 | * Abstract declarations are used inside expression. To free |
3187 | * the memory would be a fatal error. | | 3169 | * the memory would be a fatal error. |
3188 | * We don't free blocks that are inside casts because these | | 3170 | * We don't free blocks that are inside casts because these |
3189 | * will be used later to match types. | | 3171 | * will be used later to match types. |
3190 | */ | | 3172 | */ |
3191 | if (tn->tn_op != CON && dcs->d_kind != DLK_ABSTRACT) | | 3173 | if (tn->tn_op != CON && dcs->d_kind != DLK_ABSTRACT) |
3192 | expr_free_all(); | | 3174 | expr_free_all(); |
3193 | | | 3175 | |
3194 | bool out_of_bounds = is_unsigned | | 3176 | bool out_of_bounds = is_unsigned |
3195 | ? (uint64_t)val > (uint64_t)TARG_INT_MAX | | 3177 | ? (uint64_t)val > (uint64_t)TARG_INT_MAX |
3196 | : val > (int64_t)TARG_INT_MAX || val < (int64_t)TARG_INT_MIN; | | 3178 | : val > (int64_t)TARG_INT_MAX || val < (int64_t)TARG_INT_MIN; |
3197 | if (out_of_bounds) | | 3179 | if (out_of_bounds) |
3198 | /* integral constant too large */ | | 3180 | /* integral constant too large */ |
3199 | warning(56); | | 3181 | warning(56); |
3200 | return (int)val; | | 3182 | return (int)val; |
3201 | } | | 3183 | } |