Sun Jul 30 22:38:10 2023 UTC ()
lint: increase debug logging for declarations, constify

Lint doesn't parse declarators correctly, see msg_347.c, and fixing this
part is nontrivial.


(rillig)
diff -r1.368 -r1.369 src/usr.bin/xlint/lint1/decl.c

cvs diff -r1.368 -r1.369 src/usr.bin/xlint/lint1/decl.c (switch to unified diff)

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