init.c revision 1.93 1 1.93 rillig /* $NetBSD: init.c,v 1.93 2021/03/18 20:22:50 rillig Exp $ */
2 1.2 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1994, 1995 Jochen Pohl
5 1.1 cgd * All Rights Reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by Jochen Pohl for
18 1.1 cgd * The NetBSD Project.
19 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
20 1.1 cgd * derived from this software without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.18 jmc #if HAVE_NBTOOL_CONFIG_H
35 1.18 jmc #include "nbtool_config.h"
36 1.18 jmc #endif
37 1.18 jmc
38 1.5 christos #include <sys/cdefs.h>
39 1.10 tv #if defined(__RCSID) && !defined(lint)
40 1.93 rillig __RCSID("$NetBSD: init.c,v 1.93 2021/03/18 20:22:50 rillig Exp $");
41 1.1 cgd #endif
42 1.1 cgd
43 1.1 cgd #include <stdlib.h>
44 1.17 thorpej #include <string.h>
45 1.1 cgd
46 1.1 cgd #include "lint1.h"
47 1.1 cgd
48 1.53 rillig
49 1.53 rillig /*
50 1.93 rillig * Initialization
51 1.93 rillig *
52 1.93 rillig * Handles initializations of global or local objects, like in:
53 1.93 rillig *
54 1.93 rillig * int number = 12345;
55 1.93 rillig * int number_with_braces = { 12345 };
56 1.93 rillig *
57 1.93 rillig * int array_of_unknown_size[] = { 111, 222, 333 };
58 1.93 rillig * int array_flat[2][2] = { 11, 12, 21, 22 };
59 1.93 rillig * int array_nested[2][2] = { { 11, 12 }, { 21, 22 } };
60 1.93 rillig *
61 1.93 rillig * struct { int x, y; } point = { 3, 4 };
62 1.93 rillig * struct { int x, y; } point = { .y = 3, .x = 4 };
63 1.93 rillig *
64 1.93 rillig * An initializer may be surrounded by an extra pair of braces, like in the
65 1.93 rillig * example 'number_with_braces'. For multi-dimensional arrays, the inner
66 1.93 rillig * braces may be omitted like in array_flat or spelled out like in
67 1.93 rillig * array_nested.
68 1.93 rillig *
69 1.93 rillig * For the initializer, the grammar parser calls these functions:
70 1.93 rillig *
71 1.93 rillig * init_lbrace for a '{'
72 1.93 rillig * init_using_expr for a value
73 1.93 rillig * init_rbrace for a '}'
74 1.93 rillig *
75 1.93 rillig * The state of the current initialization is stored in initstk, a stack of
76 1.93 rillig * initstack_element, one element per level of braces.
77 1.93 rillig * (TODO: It might be more complicated for multi-dimensional arrays.)
78 1.93 rillig *
79 1.93 rillig * In initstk, when initializing an array, there is an additional level where
80 1.93 rillig * the number of remaining elements toggles between 1 and 0.
81 1.93 rillig * (TODO: Why is this extra level actually needed? It seems redundant.)
82 1.93 rillig *
83 1.93 rillig * See also:
84 1.93 rillig * C99 6.7.8 "Initialization"
85 1.93 rillig * d_c99_init.c for more examples
86 1.93 rillig */
87 1.93 rillig
88 1.93 rillig
89 1.93 rillig /*
90 1.89 rillig * Type of stack which is used for initialization of aggregate types.
91 1.54 rillig *
92 1.54 rillig * XXX: Since C99, a stack is an inappropriate data structure for modelling
93 1.54 rillig * an initialization, since the designators don't have to be listed in a
94 1.54 rillig * particular order and can designate parts of sub-objects. The member names
95 1.54 rillig * of non-leaf structs may thus appear repeatedly, as demonstrated in
96 1.54 rillig * d_init_pop_member.c.
97 1.54 rillig *
98 1.54 rillig * XXX: During initialization, there may be members of the top-level struct
99 1.54 rillig * that are partially initialized. The simple i_remaining cannot model this
100 1.54 rillig * appropriately.
101 1.54 rillig *
102 1.54 rillig * See C99 6.7.8, which spans 6 pages full of tricky details and carefully
103 1.54 rillig * selected examples.
104 1.53 rillig */
105 1.79 rillig typedef struct initstack_element {
106 1.77 rillig
107 1.92 rillig /*
108 1.92 rillig * The type to be initialized at this level.
109 1.92 rillig */
110 1.92 rillig type_t *i_type;
111 1.92 rillig /*
112 1.92 rillig * The type that is initialized inside a further level of
113 1.92 rillig * braces. It is completely independent from i_type->t_subt.
114 1.92 rillig *
115 1.92 rillig * For example, in 'int var = { init }', initially there is an
116 1.92 rillig * initstack_element with i_subt == int. When the '{' is processed,
117 1.92 rillig * an element with i_type == int is pushed to the stack. When the
118 1.92 rillig * corresponding '}' is processed, the inner element is popped again.
119 1.92 rillig *
120 1.92 rillig * During initialization, only the top 2 elements of the stack are
121 1.92 rillig * looked at.
122 1.92 rillig */
123 1.92 rillig type_t *i_subt;
124 1.77 rillig
125 1.82 rillig /*
126 1.82 rillig * This level of the initializer requires a '}' to be completed.
127 1.82 rillig *
128 1.82 rillig * Multidimensional arrays do not need a closing brace to complete
129 1.82 rillig * an inner array; for example, { 1, 2, 3, 4 } is a valid initializer
130 1.82 rillig * for int arr[2][2].
131 1.82 rillig *
132 1.82 rillig * TODO: Do structs containing structs need a closing brace?
133 1.82 rillig * TODO: Do arrays of structs need a closing brace after each struct?
134 1.82 rillig */
135 1.77 rillig bool i_brace: 1;
136 1.82 rillig
137 1.93 rillig /* Whether i_type is an array of unknown size. */
138 1.77 rillig bool i_array_of_unknown_size: 1;
139 1.77 rillig bool i_seen_named_member: 1;
140 1.77 rillig
141 1.77 rillig /*
142 1.78 rillig * For structs, the next member to be initialized by an initializer
143 1.78 rillig * without an optional designator.
144 1.77 rillig */
145 1.77 rillig sym_t *i_current_object;
146 1.77 rillig
147 1.77 rillig /*
148 1.77 rillig * The number of remaining elements.
149 1.77 rillig *
150 1.93 rillig * For an array of unknown size, this is always 0 and thus irrelevant.
151 1.93 rillig *
152 1.77 rillig * XXX: for scalars?
153 1.77 rillig * XXX: for structs?
154 1.77 rillig * XXX: for unions?
155 1.77 rillig * XXX: for arrays?
156 1.77 rillig */
157 1.77 rillig int i_remaining;
158 1.77 rillig
159 1.77 rillig /*
160 1.77 rillig * The initialization state of the enclosing data structure
161 1.77 rillig * (struct, union, array).
162 1.77 rillig */
163 1.79 rillig struct initstack_element *i_enclosing;
164 1.79 rillig } initstack_element;
165 1.53 rillig
166 1.55 rillig /*
167 1.55 rillig * The names for a nested C99 initialization designator, in a circular list.
168 1.55 rillig *
169 1.55 rillig * Example:
170 1.55 rillig * struct stat st = {
171 1.55 rillig * .st_size = 123,
172 1.55 rillig * .st_mtim.tv_sec = 45,
173 1.55 rillig * .st_mtim.tv_nsec
174 1.55 rillig * };
175 1.55 rillig *
176 1.55 rillig * During initialization, this list first contains ["st_size"], then
177 1.55 rillig * ["st_mtim", "tv_sec"], then ["st_mtim", "tv_nsec"].
178 1.55 rillig */
179 1.53 rillig typedef struct namlist {
180 1.53 rillig const char *n_name;
181 1.53 rillig struct namlist *n_prev;
182 1.53 rillig struct namlist *n_next;
183 1.53 rillig } namlist_t;
184 1.53 rillig
185 1.53 rillig
186 1.1 cgd /*
187 1.89 rillig * initerr is set as soon as a fatal error occurred in an initialization.
188 1.89 rillig * The effect is that the rest of the initialization is ignored (parsed
189 1.89 rillig * by yacc, expression trees built, but no initialization takes place).
190 1.1 cgd */
191 1.62 rillig bool initerr;
192 1.1 cgd
193 1.1 cgd /* Pointer to the symbol which is to be initialized. */
194 1.1 cgd sym_t *initsym;
195 1.1 cgd
196 1.89 rillig /* Points to the top element of the initialization stack. */
197 1.79 rillig initstack_element *initstk;
198 1.1 cgd
199 1.12 christos /* Points to a c9x named member; */
200 1.12 christos namlist_t *namedmem = NULL;
201 1.12 christos
202 1.1 cgd
203 1.82 rillig static bool init_array_using_string(tnode_t *);
204 1.12 christos
205 1.12 christos #ifndef DEBUG
206 1.90 rillig
207 1.75 rillig #define debug_printf(fmt, ...) do { } while (false)
208 1.75 rillig #define debug_indent() do { } while (false)
209 1.75 rillig #define debug_enter(a) do { } while (false)
210 1.75 rillig #define debug_step(fmt, ...) do { } while (false)
211 1.75 rillig #define debug_leave(a) do { } while (false)
212 1.90 rillig #define debug_named_member() do { } while (false)
213 1.90 rillig #define debug_initstack_element(elem) do { } while (false)
214 1.90 rillig #define debug_initstack() do { } while (false)
215 1.90 rillig
216 1.12 christos #else
217 1.90 rillig
218 1.68 rillig static int debug_ind = 0;
219 1.68 rillig
220 1.68 rillig static void __printflike(1, 2)
221 1.68 rillig debug_printf(const char *fmt, ...)
222 1.68 rillig {
223 1.68 rillig va_list va;
224 1.68 rillig
225 1.68 rillig va_start(va, fmt);
226 1.68 rillig vfprintf(stdout, fmt, va);
227 1.68 rillig va_end(va);
228 1.68 rillig }
229 1.68 rillig
230 1.68 rillig static void
231 1.68 rillig debug_indent(void)
232 1.68 rillig {
233 1.68 rillig debug_printf("%*s", 2 * debug_ind, "");
234 1.68 rillig }
235 1.68 rillig
236 1.68 rillig static void
237 1.68 rillig debug_enter(const char *func)
238 1.68 rillig {
239 1.68 rillig printf("%*s+ %s\n", 2 * debug_ind++, "", func);
240 1.68 rillig }
241 1.68 rillig
242 1.68 rillig static void __printflike(1, 2)
243 1.68 rillig debug_step(const char *fmt, ...)
244 1.68 rillig {
245 1.68 rillig va_list va;
246 1.68 rillig
247 1.68 rillig printf("%*s", 2 * debug_ind, "");
248 1.68 rillig va_start(va, fmt);
249 1.68 rillig vfprintf(stdout, fmt, va);
250 1.68 rillig va_end(va);
251 1.68 rillig printf("\n");
252 1.68 rillig }
253 1.68 rillig
254 1.68 rillig static void
255 1.68 rillig debug_leave(const char *func)
256 1.68 rillig {
257 1.68 rillig printf("%*s- %s\n", 2 * --debug_ind, "", func);
258 1.68 rillig }
259 1.68 rillig
260 1.55 rillig static void
261 1.68 rillig debug_named_member(void)
262 1.55 rillig {
263 1.55 rillig namlist_t *name;
264 1.55 rillig
265 1.55 rillig if (namedmem == NULL)
266 1.55 rillig return;
267 1.55 rillig name = namedmem;
268 1.68 rillig debug_indent();
269 1.68 rillig debug_printf("named member:");
270 1.55 rillig do {
271 1.68 rillig debug_printf(" %s", name->n_name);
272 1.55 rillig name = name->n_next;
273 1.55 rillig } while (name != namedmem);
274 1.68 rillig debug_printf("\n");
275 1.55 rillig }
276 1.1 cgd
277 1.78 rillig static void
278 1.79 rillig debug_initstack_element(const initstack_element *elem)
279 1.74 rillig {
280 1.78 rillig if (elem->i_type != NULL)
281 1.78 rillig debug_step(" i_type = %s", type_name(elem->i_type));
282 1.78 rillig if (elem->i_subt != NULL)
283 1.78 rillig debug_step(" i_subt = %s", type_name(elem->i_subt));
284 1.78 rillig
285 1.78 rillig if (elem->i_brace)
286 1.78 rillig debug_step(" i_brace");
287 1.78 rillig if (elem->i_array_of_unknown_size)
288 1.78 rillig debug_step(" i_array_of_unknown_size");
289 1.78 rillig if (elem->i_seen_named_member)
290 1.78 rillig debug_step(" i_seen_named_member");
291 1.78 rillig
292 1.78 rillig const type_t *eff_type = elem->i_type != NULL
293 1.78 rillig ? elem->i_type : elem->i_subt;
294 1.78 rillig if (eff_type->t_tspec == STRUCT && elem->i_current_object != NULL)
295 1.78 rillig debug_step(" i_current_object = %s",
296 1.78 rillig elem->i_current_object->s_name);
297 1.78 rillig
298 1.78 rillig debug_step(" i_remaining = %d", elem->i_remaining);
299 1.74 rillig }
300 1.74 rillig
301 1.73 rillig static void
302 1.73 rillig debug_initstack(void)
303 1.73 rillig {
304 1.73 rillig if (initstk == NULL) {
305 1.73 rillig debug_step("initstk is empty");
306 1.73 rillig return;
307 1.73 rillig }
308 1.73 rillig
309 1.73 rillig size_t i = 0;
310 1.79 rillig for (const initstack_element *elem = initstk;
311 1.77 rillig elem != NULL; elem = elem->i_enclosing) {
312 1.73 rillig debug_step("initstk[%zu]:", i);
313 1.78 rillig debug_initstack_element(elem);
314 1.73 rillig i++;
315 1.73 rillig }
316 1.73 rillig }
317 1.90 rillig
318 1.90 rillig #define debug_enter() debug_enter(__func__)
319 1.90 rillig #define debug_leave() debug_leave(__func__)
320 1.90 rillig
321 1.73 rillig #endif
322 1.73 rillig
323 1.90 rillig void
324 1.90 rillig push_member(sbuf_t *sb)
325 1.90 rillig {
326 1.90 rillig namlist_t *nam = xcalloc(1, sizeof (namlist_t));
327 1.90 rillig nam->n_name = sb->sb_name;
328 1.90 rillig
329 1.90 rillig debug_step("%s: '%s' %p", __func__, nam->n_name, nam);
330 1.90 rillig
331 1.90 rillig if (namedmem == NULL) {
332 1.90 rillig /*
333 1.90 rillig * XXX: Why is this a circular list?
334 1.90 rillig * XXX: Why is this a doubly-linked list?
335 1.90 rillig * A simple stack should suffice.
336 1.90 rillig */
337 1.90 rillig nam->n_prev = nam->n_next = nam;
338 1.90 rillig namedmem = nam;
339 1.90 rillig } else {
340 1.90 rillig namedmem->n_prev->n_next = nam;
341 1.90 rillig nam->n_prev = namedmem->n_prev;
342 1.90 rillig nam->n_next = namedmem;
343 1.90 rillig namedmem->n_prev = nam;
344 1.90 rillig }
345 1.90 rillig }
346 1.90 rillig
347 1.91 rillig /*
348 1.91 rillig * A struct member that has array type is initialized using a designator.
349 1.91 rillig *
350 1.91 rillig * C99 example: struct { int member[4]; } var = { [2] = 12345 };
351 1.91 rillig *
352 1.91 rillig * GNU example: struct { int member[4]; } var = { [1 ... 3] = 12345 };
353 1.91 rillig */
354 1.91 rillig void
355 1.91 rillig designator_push_subscript(range_t range)
356 1.91 rillig {
357 1.91 rillig debug_enter();
358 1.91 rillig debug_step("subscript range is %zu ... %zu", range.lo, range.hi);
359 1.91 rillig debug_initstack();
360 1.91 rillig debug_leave();
361 1.91 rillig }
362 1.91 rillig
363 1.90 rillig static void
364 1.90 rillig pop_member(void)
365 1.90 rillig {
366 1.90 rillig debug_step("%s: %s %p", __func__, namedmem->n_name, namedmem);
367 1.90 rillig if (namedmem->n_next == namedmem) {
368 1.90 rillig free(namedmem);
369 1.90 rillig namedmem = NULL;
370 1.90 rillig } else {
371 1.90 rillig namlist_t *nam = namedmem;
372 1.90 rillig namedmem = namedmem->n_next;
373 1.90 rillig nam->n_prev->n_next = nam->n_next;
374 1.90 rillig nam->n_next->n_prev = nam->n_prev;
375 1.90 rillig free(nam);
376 1.90 rillig }
377 1.90 rillig }
378 1.90 rillig
379 1.1 cgd /*
380 1.89 rillig * Initialize the initialization stack by putting an entry for the object
381 1.1 cgd * which is to be initialized on it.
382 1.1 cgd */
383 1.1 cgd void
384 1.35 rillig initstack_init(void)
385 1.1 cgd {
386 1.79 rillig initstack_element *istk;
387 1.1 cgd
388 1.1 cgd if (initerr)
389 1.1 cgd return;
390 1.1 cgd
391 1.89 rillig /* free memory used in last initialization */
392 1.1 cgd while ((istk = initstk) != NULL) {
393 1.77 rillig initstk = istk->i_enclosing;
394 1.1 cgd free(istk);
395 1.1 cgd }
396 1.1 cgd
397 1.70 rillig debug_enter();
398 1.38 rillig
399 1.1 cgd /*
400 1.77 rillig * If the type which is to be initialized is an incomplete array,
401 1.1 cgd * it must be duplicated.
402 1.1 cgd */
403 1.65 rillig if (initsym->s_type->t_tspec == ARRAY && is_incomplete(initsym->s_type))
404 1.1 cgd initsym->s_type = duptyp(initsym->s_type);
405 1.1 cgd
406 1.79 rillig istk = initstk = xcalloc(1, sizeof (initstack_element));
407 1.1 cgd istk->i_subt = initsym->s_type;
408 1.43 rillig istk->i_remaining = 1;
409 1.70 rillig
410 1.73 rillig debug_initstack();
411 1.70 rillig debug_leave();
412 1.1 cgd }
413 1.1 cgd
414 1.1 cgd static void
415 1.35 rillig initstack_pop_item(void)
416 1.1 cgd {
417 1.79 rillig initstack_element *istk;
418 1.1 cgd sym_t *m;
419 1.1 cgd
420 1.68 rillig debug_enter();
421 1.68 rillig
422 1.41 rillig istk = initstk;
423 1.80 rillig debug_step("popping:");
424 1.80 rillig debug_initstack_element(istk);
425 1.40 rillig
426 1.77 rillig initstk = istk->i_enclosing;
427 1.1 cgd free(istk);
428 1.1 cgd istk = initstk;
429 1.44 rillig lint_assert(istk != NULL);
430 1.26 christos
431 1.43 rillig istk->i_remaining--;
432 1.44 rillig lint_assert(istk->i_remaining >= 0);
433 1.1 cgd
434 1.80 rillig debug_step("new top element with updated remaining:");
435 1.80 rillig debug_initstack_element(istk);
436 1.40 rillig
437 1.80 rillig if (namedmem != NULL) {
438 1.80 rillig debug_step("initializing named member '%s'", namedmem->n_name);
439 1.38 rillig
440 1.80 rillig /* XXX: undefined behavior if this is reached with an array? */
441 1.67 rillig for (m = istk->i_type->t_str->sou_first_member;
442 1.67 rillig m != NULL; m = m->s_next) {
443 1.80 rillig
444 1.48 rillig if (m->s_bitfield && m->s_name == unnamed)
445 1.12 christos continue;
446 1.80 rillig
447 1.12 christos if (strcmp(m->s_name, namedmem->n_name) == 0) {
448 1.80 rillig debug_step("found matching member");
449 1.12 christos istk->i_subt = m->s_type;
450 1.80 rillig /* XXX: why ++? */
451 1.43 rillig istk->i_remaining++;
452 1.80 rillig /* XXX: why is i_seen_named_member not set? */
453 1.34 rillig pop_member();
454 1.73 rillig debug_initstack();
455 1.68 rillig debug_leave();
456 1.12 christos return;
457 1.12 christos }
458 1.12 christos }
459 1.80 rillig
460 1.50 rillig /* undefined struct/union member: %s */
461 1.12 christos error(101, namedmem->n_name);
462 1.80 rillig
463 1.34 rillig pop_member();
464 1.77 rillig istk->i_seen_named_member = true;
465 1.73 rillig debug_initstack();
466 1.68 rillig debug_leave();
467 1.12 christos return;
468 1.12 christos }
469 1.80 rillig
470 1.1 cgd /*
471 1.1 cgd * If the removed element was a structure member, we must go
472 1.1 cgd * to the next structure member.
473 1.1 cgd */
474 1.43 rillig if (istk->i_remaining > 0 && istk->i_type->t_tspec == STRUCT &&
475 1.77 rillig !istk->i_seen_named_member) {
476 1.1 cgd do {
477 1.77 rillig m = istk->i_current_object =
478 1.77 rillig istk->i_current_object->s_next;
479 1.80 rillig /* XXX: can this assertion be made to fail? */
480 1.44 rillig lint_assert(m != NULL);
481 1.68 rillig debug_step("pop %s", m->s_name);
482 1.48 rillig } while (m->s_bitfield && m->s_name == unnamed);
483 1.80 rillig /* XXX: duplicate code for skipping unnamed bit-fields */
484 1.1 cgd istk->i_subt = m->s_type;
485 1.1 cgd }
486 1.73 rillig debug_initstack();
487 1.68 rillig debug_leave();
488 1.1 cgd }
489 1.1 cgd
490 1.36 rillig /*
491 1.36 rillig * Take all entries, including the first which requires a closing brace,
492 1.36 rillig * from the stack.
493 1.36 rillig */
494 1.36 rillig static void
495 1.36 rillig initstack_pop_brace(void)
496 1.36 rillig {
497 1.62 rillig bool brace;
498 1.36 rillig
499 1.68 rillig debug_enter();
500 1.73 rillig debug_initstack();
501 1.36 rillig do {
502 1.36 rillig brace = initstk->i_brace;
503 1.68 rillig debug_step("loop brace=%d", brace);
504 1.36 rillig initstack_pop_item();
505 1.36 rillig } while (!brace);
506 1.73 rillig debug_initstack();
507 1.68 rillig debug_leave();
508 1.36 rillig }
509 1.36 rillig
510 1.36 rillig /*
511 1.36 rillig * Take all entries which cannot be used for further initializers from the
512 1.36 rillig * stack, but do this only if they do not require a closing brace.
513 1.36 rillig */
514 1.1 cgd static void
515 1.36 rillig initstack_pop_nobrace(void)
516 1.1 cgd {
517 1.7 lukem
518 1.68 rillig debug_enter();
519 1.43 rillig while (!initstk->i_brace && initstk->i_remaining == 0 &&
520 1.77 rillig !initstk->i_array_of_unknown_size)
521 1.36 rillig initstack_pop_item();
522 1.68 rillig debug_leave();
523 1.1 cgd }
524 1.1 cgd
525 1.1 cgd static void
526 1.35 rillig initstack_push(void)
527 1.1 cgd {
528 1.79 rillig initstack_element *istk, *inxt;
529 1.1 cgd int cnt;
530 1.1 cgd sym_t *m;
531 1.1 cgd
532 1.68 rillig debug_enter();
533 1.73 rillig debug_initstack();
534 1.68 rillig
535 1.1 cgd istk = initstk;
536 1.1 cgd
537 1.1 cgd /* Extend an incomplete array type by one element */
538 1.43 rillig if (istk->i_remaining == 0) {
539 1.1 cgd /*
540 1.1 cgd * Inside of other aggregate types must not be an incomplete
541 1.1 cgd * type.
542 1.1 cgd */
543 1.77 rillig lint_assert(istk->i_enclosing->i_enclosing == NULL);
544 1.85 rillig lint_assert(istk->i_type->t_tspec == ARRAY);
545 1.93 rillig
546 1.85 rillig debug_step("extending array of unknown size '%s'",
547 1.85 rillig type_name(istk->i_type));
548 1.43 rillig istk->i_remaining = 1;
549 1.1 cgd istk->i_type->t_dim++;
550 1.63 rillig setcomplete(istk->i_type, true);
551 1.93 rillig
552 1.85 rillig debug_step("extended type is '%s'", type_name(istk->i_type));
553 1.1 cgd }
554 1.1 cgd
555 1.44 rillig lint_assert(istk->i_remaining > 0);
556 1.61 rillig lint_assert(istk->i_type == NULL || !is_scalar(istk->i_type->t_tspec));
557 1.1 cgd
558 1.79 rillig initstk = xcalloc(1, sizeof (initstack_element));
559 1.77 rillig initstk->i_enclosing = istk;
560 1.1 cgd initstk->i_type = istk->i_subt;
561 1.44 rillig lint_assert(initstk->i_type->t_tspec != FUNC);
562 1.1 cgd
563 1.12 christos again:
564 1.1 cgd istk = initstk;
565 1.1 cgd
566 1.88 rillig debug_step("expecting type '%s'", type_name(istk->i_type));
567 1.1 cgd switch (istk->i_type->t_tspec) {
568 1.1 cgd case ARRAY:
569 1.62 rillig if (namedmem != NULL) {
570 1.68 rillig debug_step("ARRAY %s brace=%d",
571 1.68 rillig namedmem->n_name, istk->i_brace);
572 1.19 christos goto pop;
573 1.77 rillig } else if (istk->i_enclosing->i_seen_named_member) {
574 1.62 rillig istk->i_brace = true;
575 1.68 rillig debug_step("ARRAY brace=%d, namedmem=%d",
576 1.77 rillig istk->i_brace,
577 1.77 rillig istk->i_enclosing->i_seen_named_member);
578 1.20 christos }
579 1.20 christos
580 1.65 rillig if (is_incomplete(istk->i_type) &&
581 1.77 rillig istk->i_enclosing->i_enclosing != NULL) {
582 1.89 rillig /* initialization of an incomplete type */
583 1.1 cgd error(175);
584 1.62 rillig initerr = true;
585 1.73 rillig debug_initstack();
586 1.68 rillig debug_leave();
587 1.1 cgd return;
588 1.1 cgd }
589 1.1 cgd istk->i_subt = istk->i_type->t_subt;
590 1.77 rillig istk->i_array_of_unknown_size = is_incomplete(istk->i_type);
591 1.43 rillig istk->i_remaining = istk->i_type->t_dim;
592 1.93 rillig debug_named_member();
593 1.93 rillig debug_step("type '%s' remaining %d",
594 1.93 rillig type_name(istk->i_type), istk->i_remaining);
595 1.1 cgd break;
596 1.1 cgd case UNION:
597 1.1 cgd if (tflag)
598 1.89 rillig /* initialization of union is illegal in trad. C */
599 1.1 cgd warning(238);
600 1.1 cgd /* FALLTHROUGH */
601 1.1 cgd case STRUCT:
602 1.65 rillig if (is_incomplete(istk->i_type)) {
603 1.89 rillig /* initialization of an incomplete type */
604 1.1 cgd error(175);
605 1.62 rillig initerr = true;
606 1.73 rillig debug_initstack();
607 1.68 rillig debug_leave();
608 1.1 cgd return;
609 1.1 cgd }
610 1.1 cgd cnt = 0;
611 1.93 rillig debug_named_member();
612 1.93 rillig debug_step("lookup for '%s'%s",
613 1.57 rillig type_name(istk->i_type),
614 1.93 rillig istk->i_seen_named_member ? ", seen named member" : "");
615 1.67 rillig for (m = istk->i_type->t_str->sou_first_member;
616 1.67 rillig m != NULL; m = m->s_next) {
617 1.48 rillig if (m->s_bitfield && m->s_name == unnamed)
618 1.1 cgd continue;
619 1.12 christos if (namedmem != NULL) {
620 1.68 rillig debug_step("named lhs.member=%s, rhs.member=%s",
621 1.68 rillig m->s_name, namedmem->n_name);
622 1.12 christos if (strcmp(m->s_name, namedmem->n_name) == 0) {
623 1.12 christos cnt++;
624 1.12 christos break;
625 1.12 christos } else
626 1.12 christos continue;
627 1.12 christos }
628 1.1 cgd if (++cnt == 1) {
629 1.77 rillig istk->i_current_object = m;
630 1.1 cgd istk->i_subt = m->s_type;
631 1.1 cgd }
632 1.1 cgd }
633 1.12 christos if (namedmem != NULL) {
634 1.12 christos if (m == NULL) {
635 1.68 rillig debug_step("pop struct");
636 1.19 christos goto pop;
637 1.28 rillig }
638 1.77 rillig istk->i_current_object = m;
639 1.26 christos istk->i_subt = m->s_type;
640 1.77 rillig istk->i_seen_named_member = true;
641 1.68 rillig debug_step("named name=%s", namedmem->n_name);
642 1.34 rillig pop_member();
643 1.12 christos cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
644 1.12 christos }
645 1.62 rillig istk->i_brace = true;
646 1.68 rillig debug_step("unnamed type=%s, brace=%d",
647 1.74 rillig type_name(
648 1.74 rillig istk->i_type != NULL ? istk->i_type : istk->i_subt),
649 1.68 rillig istk->i_brace);
650 1.1 cgd if (cnt == 0) {
651 1.1 cgd /* cannot init. struct/union with no named member */
652 1.1 cgd error(179);
653 1.62 rillig initerr = true;
654 1.73 rillig debug_initstack();
655 1.68 rillig debug_leave();
656 1.1 cgd return;
657 1.1 cgd }
658 1.43 rillig istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
659 1.1 cgd break;
660 1.1 cgd default:
661 1.62 rillig if (namedmem != NULL) {
662 1.68 rillig debug_step("pop");
663 1.19 christos pop:
664 1.77 rillig inxt = initstk->i_enclosing;
665 1.12 christos free(istk);
666 1.22 ad initstk = inxt;
667 1.12 christos goto again;
668 1.12 christos }
669 1.92 rillig /* XXX: Why is this set to 1 unconditionally? */
670 1.43 rillig istk->i_remaining = 1;
671 1.1 cgd break;
672 1.1 cgd }
673 1.68 rillig
674 1.73 rillig debug_initstack();
675 1.68 rillig debug_leave();
676 1.1 cgd }
677 1.1 cgd
678 1.1 cgd static void
679 1.84 rillig check_too_many_initializers(void)
680 1.1 cgd {
681 1.1 cgd
682 1.84 rillig const initstack_element *istk = initstk;
683 1.84 rillig if (istk->i_remaining > 0)
684 1.84 rillig return;
685 1.84 rillig if (istk->i_array_of_unknown_size || istk->i_seen_named_member)
686 1.84 rillig return;
687 1.1 cgd
688 1.84 rillig tspec_t t = istk->i_type->t_tspec;
689 1.84 rillig if (t == ARRAY) {
690 1.84 rillig /* too many array initializers, expected %d */
691 1.84 rillig error(173, istk->i_type->t_dim);
692 1.84 rillig } else if (t == STRUCT || t == UNION) {
693 1.84 rillig /* too many struct/union initializers */
694 1.84 rillig error(172);
695 1.84 rillig } else {
696 1.84 rillig /* too many initializers */
697 1.84 rillig error(174);
698 1.1 cgd }
699 1.84 rillig initerr = true;
700 1.1 cgd }
701 1.1 cgd
702 1.92 rillig /*
703 1.92 rillig * Process a '{' in an initializer by starting the initialization of the
704 1.92 rillig * nested data structure, with i_type being the i_subt of the outer
705 1.92 rillig * initialization level.
706 1.92 rillig */
707 1.1 cgd static void
708 1.37 rillig initstack_next_brace(void)
709 1.1 cgd {
710 1.7 lukem
711 1.68 rillig debug_enter();
712 1.73 rillig debug_initstack();
713 1.68 rillig
714 1.61 rillig if (initstk->i_type != NULL && is_scalar(initstk->i_type->t_tspec)) {
715 1.49 rillig /* invalid initializer type %s */
716 1.57 rillig error(176, type_name(initstk->i_type));
717 1.62 rillig initerr = true;
718 1.37 rillig }
719 1.37 rillig if (!initerr)
720 1.84 rillig check_too_many_initializers();
721 1.37 rillig if (!initerr)
722 1.37 rillig initstack_push();
723 1.37 rillig if (!initerr) {
724 1.62 rillig initstk->i_brace = true;
725 1.92 rillig debug_named_member();
726 1.92 rillig debug_step("expecting type '%s'",
727 1.92 rillig type_name(initstk->i_type != NULL ? initstk->i_type
728 1.74 rillig : initstk->i_subt));
729 1.37 rillig }
730 1.68 rillig
731 1.73 rillig debug_initstack();
732 1.68 rillig debug_leave();
733 1.37 rillig }
734 1.37 rillig
735 1.37 rillig static void
736 1.37 rillig initstack_next_nobrace(void)
737 1.37 rillig {
738 1.68 rillig debug_enter();
739 1.73 rillig debug_initstack();
740 1.37 rillig
741 1.61 rillig if (initstk->i_type == NULL && !is_scalar(initstk->i_subt->t_tspec)) {
742 1.37 rillig /* {}-enclosed initializer required */
743 1.37 rillig error(181);
744 1.92 rillig /* XXX: maybe set initerr here */
745 1.37 rillig }
746 1.37 rillig
747 1.84 rillig if (!initerr)
748 1.84 rillig check_too_many_initializers();
749 1.84 rillig
750 1.71 rillig /*
751 1.71 rillig * Make sure an entry with a scalar type is at the top of the stack.
752 1.71 rillig *
753 1.92 rillig * FIXME: Since C99, an initializer for an object with automatic
754 1.71 rillig * storage need not be a constant expression anymore. It is
755 1.71 rillig * perfectly fine to initialize a struct with a struct expression,
756 1.71 rillig * see d_struct_init_nested.c for a demonstration.
757 1.71 rillig */
758 1.42 rillig while (!initerr) {
759 1.42 rillig if ((initstk->i_type != NULL &&
760 1.61 rillig is_scalar(initstk->i_type->t_tspec)))
761 1.42 rillig break;
762 1.42 rillig initstack_push();
763 1.1 cgd }
764 1.68 rillig
765 1.73 rillig debug_initstack();
766 1.68 rillig debug_leave();
767 1.1 cgd }
768 1.1 cgd
769 1.1 cgd void
770 1.34 rillig init_lbrace(void)
771 1.1 cgd {
772 1.1 cgd if (initerr)
773 1.1 cgd return;
774 1.1 cgd
775 1.68 rillig debug_enter();
776 1.73 rillig debug_initstack();
777 1.68 rillig
778 1.1 cgd if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
779 1.77 rillig initstk->i_enclosing == NULL) {
780 1.61 rillig if (tflag && !is_scalar(initstk->i_subt->t_tspec))
781 1.50 rillig /* no automatic aggregate initialization in trad. C */
782 1.1 cgd warning(188);
783 1.1 cgd }
784 1.1 cgd
785 1.1 cgd /*
786 1.1 cgd * Remove all entries which cannot be used for further initializers
787 1.1 cgd * and do not expect a closing brace.
788 1.1 cgd */
789 1.36 rillig initstack_pop_nobrace();
790 1.1 cgd
791 1.37 rillig initstack_next_brace();
792 1.68 rillig
793 1.73 rillig debug_initstack();
794 1.68 rillig debug_leave();
795 1.1 cgd }
796 1.1 cgd
797 1.92 rillig /*
798 1.92 rillig * Process a '}' in an initializer by finishing the current level of the
799 1.92 rillig * initialization stack.
800 1.92 rillig */
801 1.1 cgd void
802 1.34 rillig init_rbrace(void)
803 1.1 cgd {
804 1.1 cgd if (initerr)
805 1.1 cgd return;
806 1.1 cgd
807 1.68 rillig debug_enter();
808 1.36 rillig initstack_pop_brace();
809 1.68 rillig debug_leave();
810 1.1 cgd }
811 1.1 cgd
812 1.86 rillig /* In traditional C, bit-fields can be initialized only by integer constants. */
813 1.86 rillig static void
814 1.86 rillig check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
815 1.86 rillig {
816 1.86 rillig if (tflag &&
817 1.86 rillig is_integer(lt) &&
818 1.86 rillig ln->tn_type->t_bitfield &&
819 1.86 rillig !is_integer(rt)) {
820 1.89 rillig /* bit-field initialization is illegal in traditional C */
821 1.86 rillig warning(186);
822 1.86 rillig }
823 1.86 rillig }
824 1.86 rillig
825 1.87 rillig static void
826 1.87 rillig check_non_constant_initializer(const tnode_t *tn, scl_t sclass)
827 1.87 rillig {
828 1.87 rillig if (tn == NULL || tn->tn_op == CON)
829 1.87 rillig return;
830 1.87 rillig
831 1.87 rillig sym_t *sym;
832 1.87 rillig ptrdiff_t offs;
833 1.87 rillig if (constant_addr(tn, &sym, &offs))
834 1.87 rillig return;
835 1.87 rillig
836 1.87 rillig if (sclass == AUTO || sclass == REG) {
837 1.87 rillig /* non-constant initializer */
838 1.87 rillig c99ism(177);
839 1.87 rillig } else {
840 1.87 rillig /* non-constant initializer */
841 1.87 rillig error(177);
842 1.87 rillig }
843 1.87 rillig }
844 1.87 rillig
845 1.1 cgd void
846 1.69 rillig init_using_expr(tnode_t *tn)
847 1.1 cgd {
848 1.1 cgd tspec_t lt, rt;
849 1.1 cgd tnode_t *ln;
850 1.1 cgd struct mbl *tmem;
851 1.81 rillig scl_t sclass;
852 1.1 cgd
853 1.68 rillig debug_enter();
854 1.92 rillig debug_initstack();
855 1.68 rillig debug_named_member();
856 1.76 rillig debug_node(tn, debug_ind);
857 1.55 rillig
858 1.68 rillig if (initerr || tn == NULL) {
859 1.68 rillig debug_leave();
860 1.25 christos return;
861 1.68 rillig }
862 1.1 cgd
863 1.81 rillig sclass = initsym->s_scl;
864 1.1 cgd
865 1.1 cgd /*
866 1.89 rillig * Do not test for automatic aggregate initialization. If the
867 1.9 wiz * initializer starts with a brace we have the warning already.
868 1.1 cgd * If not, an error will be printed that the initializer must
869 1.1 cgd * be enclosed by braces.
870 1.1 cgd */
871 1.1 cgd
872 1.1 cgd /*
873 1.89 rillig * Local initialization of non-array-types with only one expression
874 1.1 cgd * without braces is done by ASSIGN
875 1.1 cgd */
876 1.81 rillig if ((sclass == AUTO || sclass == REG) &&
877 1.77 rillig initsym->s_type->t_tspec != ARRAY && initstk->i_enclosing == NULL) {
878 1.82 rillig debug_step("handing over to ASSIGN");
879 1.60 rillig ln = new_name_node(initsym, 0);
880 1.1 cgd ln->tn_type = tduptyp(ln->tn_type);
881 1.62 rillig ln->tn_type->t_const = false;
882 1.1 cgd tn = build(ASSIGN, ln, tn);
883 1.66 rillig expr(tn, false, false, false, false);
884 1.82 rillig /* XXX: why not clean up the initstack here already? */
885 1.68 rillig debug_leave();
886 1.25 christos return;
887 1.1 cgd }
888 1.1 cgd
889 1.36 rillig initstack_pop_nobrace();
890 1.1 cgd
891 1.82 rillig if (init_array_using_string(tn)) {
892 1.82 rillig debug_step("after initializing the string:");
893 1.82 rillig /* XXX: why not clean up the initstack here already? */
894 1.73 rillig debug_initstack();
895 1.68 rillig debug_leave();
896 1.25 christos return;
897 1.68 rillig }
898 1.1 cgd
899 1.37 rillig initstack_next_nobrace();
900 1.68 rillig if (initerr || tn == NULL) {
901 1.73 rillig debug_initstack();
902 1.68 rillig debug_leave();
903 1.25 christos return;
904 1.68 rillig }
905 1.1 cgd
906 1.43 rillig initstk->i_remaining--;
907 1.83 rillig debug_step("%d elements remaining", initstk->i_remaining);
908 1.83 rillig
909 1.1 cgd /* Create a temporary node for the left side. */
910 1.1 cgd ln = tgetblk(sizeof (tnode_t));
911 1.1 cgd ln->tn_op = NAME;
912 1.1 cgd ln->tn_type = tduptyp(initstk->i_type);
913 1.62 rillig ln->tn_type->t_const = false;
914 1.62 rillig ln->tn_lvalue = true;
915 1.1 cgd ln->tn_sym = initsym; /* better than nothing */
916 1.1 cgd
917 1.1 cgd tn = cconv(tn);
918 1.1 cgd
919 1.1 cgd lt = ln->tn_type->t_tspec;
920 1.1 cgd rt = tn->tn_type->t_tspec;
921 1.1 cgd
922 1.92 rillig lint_assert(is_scalar(lt)); /* at least before C99 */
923 1.1 cgd
924 1.93 rillig debug_step("typeok '%s', '%s'",
925 1.93 rillig type_name(ln->tn_type), type_name(tn->tn_type));
926 1.68 rillig if (!typeok(INIT, 0, ln, tn)) {
927 1.73 rillig debug_initstack();
928 1.68 rillig debug_leave();
929 1.25 christos return;
930 1.68 rillig }
931 1.1 cgd
932 1.1 cgd /*
933 1.38 rillig * Store the tree memory. This is necessary because otherwise
934 1.1 cgd * expr() would free it.
935 1.1 cgd */
936 1.1 cgd tmem = tsave();
937 1.66 rillig expr(tn, true, false, true, false);
938 1.1 cgd trestor(tmem);
939 1.7 lukem
940 1.86 rillig check_bit_field_init(ln, lt, rt);
941 1.1 cgd
942 1.93 rillig /*
943 1.93 rillig * XXX: Is it correct to do this conversion _after_ the typeok above?
944 1.93 rillig */
945 1.59 rillig if (lt != rt || (initstk->i_type->t_bitfield && tn->tn_op == CON))
946 1.1 cgd tn = convert(INIT, 0, initstk->i_type, tn);
947 1.1 cgd
948 1.87 rillig check_non_constant_initializer(tn, sclass);
949 1.68 rillig
950 1.73 rillig debug_initstack();
951 1.68 rillig debug_leave();
952 1.1 cgd }
953 1.1 cgd
954 1.1 cgd
955 1.82 rillig /* Initialize a character array or wchar_t array with a string literal. */
956 1.62 rillig static bool
957 1.82 rillig init_array_using_string(tnode_t *tn)
958 1.1 cgd {
959 1.1 cgd tspec_t t;
960 1.79 rillig initstack_element *istk;
961 1.1 cgd int len;
962 1.1 cgd strg_t *strg;
963 1.1 cgd
964 1.1 cgd if (tn->tn_op != STRING)
965 1.63 rillig return false;
966 1.1 cgd
967 1.68 rillig debug_enter();
968 1.73 rillig debug_initstack();
969 1.68 rillig
970 1.1 cgd istk = initstk;
971 1.47 rillig strg = tn->tn_string;
972 1.1 cgd
973 1.1 cgd /*
974 1.1 cgd * Check if we have an array type which can be initialized by
975 1.1 cgd * the string.
976 1.1 cgd */
977 1.12 christos if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
978 1.68 rillig debug_step("subt array");
979 1.1 cgd t = istk->i_subt->t_subt->t_tspec;
980 1.1 cgd if (!((strg->st_tspec == CHAR &&
981 1.1 cgd (t == CHAR || t == UCHAR || t == SCHAR)) ||
982 1.1 cgd (strg->st_tspec == WCHAR && t == WCHAR))) {
983 1.68 rillig debug_leave();
984 1.63 rillig return false;
985 1.1 cgd }
986 1.82 rillig /* XXX: duplicate code, see below */
987 1.1 cgd /* Put the array at top of stack */
988 1.35 rillig initstack_push();
989 1.1 cgd istk = initstk;
990 1.1 cgd } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
991 1.68 rillig debug_step("type array");
992 1.1 cgd t = istk->i_type->t_subt->t_tspec;
993 1.1 cgd if (!((strg->st_tspec == CHAR &&
994 1.1 cgd (t == CHAR || t == UCHAR || t == SCHAR)) ||
995 1.1 cgd (strg->st_tspec == WCHAR && t == WCHAR))) {
996 1.68 rillig debug_leave();
997 1.63 rillig return false;
998 1.1 cgd }
999 1.82 rillig /* XXX: duplicate code, see above */
1000 1.1 cgd /*
1001 1.1 cgd * If the array is already partly initialized, we are
1002 1.1 cgd * wrong here.
1003 1.1 cgd */
1004 1.43 rillig if (istk->i_remaining != istk->i_type->t_dim)
1005 1.68 rillig debug_leave();
1006 1.63 rillig return false;
1007 1.1 cgd } else {
1008 1.68 rillig debug_leave();
1009 1.63 rillig return false;
1010 1.1 cgd }
1011 1.1 cgd
1012 1.1 cgd /* Get length without trailing NUL character. */
1013 1.1 cgd len = strg->st_len;
1014 1.1 cgd
1015 1.77 rillig if (istk->i_array_of_unknown_size) {
1016 1.77 rillig istk->i_array_of_unknown_size = false;
1017 1.1 cgd istk->i_type->t_dim = len + 1;
1018 1.63 rillig setcomplete(istk->i_type, true);
1019 1.1 cgd } else {
1020 1.1 cgd if (istk->i_type->t_dim < len) {
1021 1.1 cgd /* non-null byte ignored in string initializer */
1022 1.1 cgd warning(187);
1023 1.1 cgd }
1024 1.1 cgd }
1025 1.1 cgd
1026 1.1 cgd /* In every case the array is initialized completely. */
1027 1.43 rillig istk->i_remaining = 0;
1028 1.1 cgd
1029 1.73 rillig debug_initstack();
1030 1.68 rillig debug_leave();
1031 1.63 rillig return true;
1032 1.1 cgd }
1033