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