init.c revision 1.84 1 1.84 rillig /* $NetBSD: init.c,v 1.84 2021/02/21 14:02:36 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.84 rillig __RCSID("$NetBSD: init.c,v 1.84 2021/02/21 14:02:36 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.53 rillig * Type of stack which is used for initialisation of aggregate types.
51 1.54 rillig *
52 1.54 rillig * XXX: Since C99, a stack is an inappropriate data structure for modelling
53 1.54 rillig * an initialization, since the designators don't have to be listed in a
54 1.54 rillig * particular order and can designate parts of sub-objects. The member names
55 1.54 rillig * of non-leaf structs may thus appear repeatedly, as demonstrated in
56 1.54 rillig * d_init_pop_member.c.
57 1.54 rillig *
58 1.54 rillig * XXX: During initialization, there may be members of the top-level struct
59 1.54 rillig * that are partially initialized. The simple i_remaining cannot model this
60 1.54 rillig * appropriately.
61 1.54 rillig *
62 1.54 rillig * See C99 6.7.8, which spans 6 pages full of tricky details and carefully
63 1.54 rillig * selected examples.
64 1.53 rillig */
65 1.79 rillig typedef struct initstack_element {
66 1.77 rillig
67 1.77 rillig /* XXX: Why is i_type often null? */
68 1.53 rillig type_t *i_type; /* type of initialisation */
69 1.53 rillig type_t *i_subt; /* type of next level */
70 1.77 rillig
71 1.82 rillig /*
72 1.82 rillig * This level of the initializer requires a '}' to be completed.
73 1.82 rillig *
74 1.82 rillig * Multidimensional arrays do not need a closing brace to complete
75 1.82 rillig * an inner array; for example, { 1, 2, 3, 4 } is a valid initializer
76 1.82 rillig * for int arr[2][2].
77 1.82 rillig *
78 1.82 rillig * TODO: Do structs containing structs need a closing brace?
79 1.82 rillig * TODO: Do arrays of structs need a closing brace after each struct?
80 1.82 rillig */
81 1.77 rillig bool i_brace: 1;
82 1.82 rillig
83 1.77 rillig bool i_array_of_unknown_size: 1;
84 1.77 rillig bool i_seen_named_member: 1;
85 1.77 rillig
86 1.77 rillig /*
87 1.78 rillig * For structs, the next member to be initialized by an initializer
88 1.78 rillig * without an optional designator.
89 1.77 rillig */
90 1.77 rillig sym_t *i_current_object;
91 1.77 rillig
92 1.77 rillig /*
93 1.77 rillig * The number of remaining elements.
94 1.77 rillig *
95 1.77 rillig * XXX: for scalars?
96 1.77 rillig * XXX: for structs?
97 1.77 rillig * XXX: for unions?
98 1.77 rillig * XXX: for arrays?
99 1.77 rillig */
100 1.77 rillig int i_remaining;
101 1.77 rillig
102 1.77 rillig /*
103 1.77 rillig * The initialization state of the enclosing data structure
104 1.77 rillig * (struct, union, array).
105 1.77 rillig */
106 1.79 rillig struct initstack_element *i_enclosing;
107 1.79 rillig } initstack_element;
108 1.53 rillig
109 1.55 rillig /*
110 1.55 rillig * The names for a nested C99 initialization designator, in a circular list.
111 1.55 rillig *
112 1.55 rillig * Example:
113 1.55 rillig * struct stat st = {
114 1.55 rillig * .st_size = 123,
115 1.55 rillig * .st_mtim.tv_sec = 45,
116 1.55 rillig * .st_mtim.tv_nsec
117 1.55 rillig * };
118 1.55 rillig *
119 1.55 rillig * During initialization, this list first contains ["st_size"], then
120 1.55 rillig * ["st_mtim", "tv_sec"], then ["st_mtim", "tv_nsec"].
121 1.55 rillig */
122 1.53 rillig typedef struct namlist {
123 1.53 rillig const char *n_name;
124 1.53 rillig struct namlist *n_prev;
125 1.53 rillig struct namlist *n_next;
126 1.53 rillig } namlist_t;
127 1.53 rillig
128 1.53 rillig
129 1.1 cgd /*
130 1.8 wiz * initerr is set as soon as a fatal error occurred in an initialisation.
131 1.1 cgd * The effect is that the rest of the initialisation is ignored (parsed
132 1.1 cgd * by yacc, expression trees built, but no initialisation takes place).
133 1.1 cgd */
134 1.62 rillig bool initerr;
135 1.1 cgd
136 1.1 cgd /* Pointer to the symbol which is to be initialized. */
137 1.1 cgd sym_t *initsym;
138 1.1 cgd
139 1.1 cgd /* Points to the top element of the initialisation stack. */
140 1.79 rillig initstack_element *initstk;
141 1.1 cgd
142 1.12 christos /* Points to a c9x named member; */
143 1.12 christos namlist_t *namedmem = NULL;
144 1.12 christos
145 1.1 cgd
146 1.82 rillig static bool init_array_using_string(tnode_t *);
147 1.12 christos
148 1.12 christos #ifndef DEBUG
149 1.75 rillig #define debug_printf(fmt, ...) do { } while (false)
150 1.75 rillig #define debug_indent() do { } while (false)
151 1.75 rillig #define debug_enter(a) do { } while (false)
152 1.75 rillig #define debug_step(fmt, ...) do { } while (false)
153 1.75 rillig #define debug_leave(a) do { } while (false)
154 1.12 christos #else
155 1.68 rillig static int debug_ind = 0;
156 1.68 rillig
157 1.68 rillig static void __printflike(1, 2)
158 1.68 rillig debug_printf(const char *fmt, ...)
159 1.68 rillig {
160 1.68 rillig va_list va;
161 1.68 rillig
162 1.68 rillig va_start(va, fmt);
163 1.68 rillig vfprintf(stdout, fmt, va);
164 1.68 rillig va_end(va);
165 1.68 rillig }
166 1.68 rillig
167 1.68 rillig static void
168 1.68 rillig debug_indent(void)
169 1.68 rillig {
170 1.68 rillig debug_printf("%*s", 2 * debug_ind, "");
171 1.68 rillig }
172 1.68 rillig
173 1.68 rillig static void
174 1.68 rillig debug_enter(const char *func)
175 1.68 rillig {
176 1.68 rillig printf("%*s+ %s\n", 2 * debug_ind++, "", func);
177 1.68 rillig }
178 1.68 rillig
179 1.68 rillig static void __printflike(1, 2)
180 1.68 rillig debug_step(const char *fmt, ...)
181 1.68 rillig {
182 1.68 rillig va_list va;
183 1.68 rillig
184 1.68 rillig printf("%*s", 2 * debug_ind, "");
185 1.68 rillig va_start(va, fmt);
186 1.68 rillig vfprintf(stdout, fmt, va);
187 1.68 rillig va_end(va);
188 1.68 rillig printf("\n");
189 1.68 rillig }
190 1.68 rillig
191 1.68 rillig static void
192 1.68 rillig debug_leave(const char *func)
193 1.68 rillig {
194 1.68 rillig printf("%*s- %s\n", 2 * --debug_ind, "", func);
195 1.68 rillig }
196 1.68 rillig
197 1.68 rillig #define debug_enter() debug_enter(__func__)
198 1.68 rillig #define debug_leave() debug_leave(__func__)
199 1.68 rillig
200 1.12 christos #endif
201 1.12 christos
202 1.12 christos void
203 1.51 rillig push_member(sbuf_t *sb)
204 1.12 christos {
205 1.28 rillig namlist_t *nam = xcalloc(1, sizeof (namlist_t));
206 1.12 christos nam->n_name = sb->sb_name;
207 1.70 rillig
208 1.71 rillig debug_step("%s: '%s' %p", __func__, nam->n_name, nam);
209 1.70 rillig
210 1.12 christos if (namedmem == NULL) {
211 1.51 rillig /*
212 1.51 rillig * XXX: Why is this a circular list?
213 1.51 rillig * XXX: Why is this a doubly-linked list?
214 1.51 rillig * A simple stack should suffice.
215 1.51 rillig */
216 1.12 christos nam->n_prev = nam->n_next = nam;
217 1.12 christos namedmem = nam;
218 1.12 christos } else {
219 1.12 christos namedmem->n_prev->n_next = nam;
220 1.12 christos nam->n_prev = namedmem->n_prev;
221 1.12 christos nam->n_next = namedmem;
222 1.12 christos namedmem->n_prev = nam;
223 1.12 christos }
224 1.12 christos }
225 1.12 christos
226 1.12 christos static void
227 1.34 rillig pop_member(void)
228 1.12 christos {
229 1.68 rillig debug_step("%s: %s %p", __func__, namedmem->n_name, namedmem);
230 1.12 christos if (namedmem->n_next == namedmem) {
231 1.12 christos free(namedmem);
232 1.12 christos namedmem = NULL;
233 1.12 christos } else {
234 1.12 christos namlist_t *nam = namedmem;
235 1.12 christos namedmem = namedmem->n_next;
236 1.52 rillig nam->n_prev->n_next = nam->n_next;
237 1.52 rillig nam->n_next->n_prev = nam->n_prev;
238 1.12 christos free(nam);
239 1.12 christos }
240 1.12 christos }
241 1.1 cgd
242 1.72 rillig #ifdef DEBUG
243 1.55 rillig static void
244 1.68 rillig debug_named_member(void)
245 1.55 rillig {
246 1.55 rillig namlist_t *name;
247 1.55 rillig
248 1.55 rillig if (namedmem == NULL)
249 1.55 rillig return;
250 1.55 rillig name = namedmem;
251 1.68 rillig debug_indent();
252 1.68 rillig debug_printf("named member:");
253 1.55 rillig do {
254 1.68 rillig debug_printf(" %s", name->n_name);
255 1.55 rillig name = name->n_next;
256 1.55 rillig } while (name != namedmem);
257 1.68 rillig debug_printf("\n");
258 1.55 rillig }
259 1.72 rillig #else
260 1.75 rillig #define debug_named_member() do { } while (false)
261 1.72 rillig #endif
262 1.1 cgd
263 1.73 rillig #ifdef DEBUG
264 1.78 rillig static void
265 1.79 rillig debug_initstack_element(const initstack_element *elem)
266 1.74 rillig {
267 1.78 rillig if (elem->i_type != NULL)
268 1.78 rillig debug_step(" i_type = %s", type_name(elem->i_type));
269 1.78 rillig if (elem->i_subt != NULL)
270 1.78 rillig debug_step(" i_subt = %s", type_name(elem->i_subt));
271 1.78 rillig
272 1.78 rillig if (elem->i_brace)
273 1.78 rillig debug_step(" i_brace");
274 1.78 rillig if (elem->i_array_of_unknown_size)
275 1.78 rillig debug_step(" i_array_of_unknown_size");
276 1.78 rillig if (elem->i_seen_named_member)
277 1.78 rillig debug_step(" i_seen_named_member");
278 1.78 rillig
279 1.78 rillig const type_t *eff_type = elem->i_type != NULL
280 1.78 rillig ? elem->i_type : elem->i_subt;
281 1.78 rillig if (eff_type->t_tspec == STRUCT && elem->i_current_object != NULL)
282 1.78 rillig debug_step(" i_current_object = %s",
283 1.78 rillig elem->i_current_object->s_name);
284 1.78 rillig
285 1.78 rillig debug_step(" i_remaining = %d", elem->i_remaining);
286 1.74 rillig }
287 1.74 rillig
288 1.73 rillig static void
289 1.73 rillig debug_initstack(void)
290 1.73 rillig {
291 1.73 rillig if (initstk == NULL) {
292 1.73 rillig debug_step("initstk is empty");
293 1.73 rillig return;
294 1.73 rillig }
295 1.73 rillig
296 1.73 rillig size_t i = 0;
297 1.79 rillig for (const initstack_element *elem = initstk;
298 1.77 rillig elem != NULL; elem = elem->i_enclosing) {
299 1.73 rillig debug_step("initstk[%zu]:", i);
300 1.78 rillig debug_initstack_element(elem);
301 1.73 rillig i++;
302 1.73 rillig }
303 1.73 rillig }
304 1.73 rillig #else
305 1.80 rillig #define debug_initstack_element(elem) do { } while (false)
306 1.75 rillig #define debug_initstack() do { } while (false)
307 1.73 rillig #endif
308 1.73 rillig
309 1.1 cgd /*
310 1.77 rillig * Initialize the initialisation stack by putting an entry for the object
311 1.1 cgd * which is to be initialized on it.
312 1.1 cgd */
313 1.1 cgd void
314 1.35 rillig initstack_init(void)
315 1.1 cgd {
316 1.79 rillig initstack_element *istk;
317 1.1 cgd
318 1.1 cgd if (initerr)
319 1.1 cgd return;
320 1.1 cgd
321 1.1 cgd /* free memory used in last initialisation */
322 1.1 cgd while ((istk = initstk) != NULL) {
323 1.77 rillig initstk = istk->i_enclosing;
324 1.1 cgd free(istk);
325 1.1 cgd }
326 1.1 cgd
327 1.70 rillig debug_enter();
328 1.38 rillig
329 1.1 cgd /*
330 1.77 rillig * If the type which is to be initialized is an incomplete array,
331 1.1 cgd * it must be duplicated.
332 1.1 cgd */
333 1.65 rillig if (initsym->s_type->t_tspec == ARRAY && is_incomplete(initsym->s_type))
334 1.1 cgd initsym->s_type = duptyp(initsym->s_type);
335 1.1 cgd
336 1.79 rillig istk = initstk = xcalloc(1, sizeof (initstack_element));
337 1.1 cgd istk->i_subt = initsym->s_type;
338 1.43 rillig istk->i_remaining = 1;
339 1.70 rillig
340 1.73 rillig debug_initstack();
341 1.70 rillig debug_leave();
342 1.1 cgd }
343 1.1 cgd
344 1.1 cgd static void
345 1.35 rillig initstack_pop_item(void)
346 1.1 cgd {
347 1.79 rillig initstack_element *istk;
348 1.1 cgd sym_t *m;
349 1.1 cgd
350 1.68 rillig debug_enter();
351 1.68 rillig
352 1.41 rillig istk = initstk;
353 1.80 rillig debug_step("popping:");
354 1.80 rillig debug_initstack_element(istk);
355 1.40 rillig
356 1.77 rillig initstk = istk->i_enclosing;
357 1.1 cgd free(istk);
358 1.1 cgd istk = initstk;
359 1.44 rillig lint_assert(istk != NULL);
360 1.26 christos
361 1.43 rillig istk->i_remaining--;
362 1.44 rillig lint_assert(istk->i_remaining >= 0);
363 1.1 cgd
364 1.80 rillig debug_step("new top element with updated remaining:");
365 1.80 rillig debug_initstack_element(istk);
366 1.40 rillig
367 1.80 rillig if (namedmem != NULL) {
368 1.80 rillig debug_step("initializing named member '%s'", namedmem->n_name);
369 1.38 rillig
370 1.80 rillig /* XXX: undefined behavior if this is reached with an array? */
371 1.67 rillig for (m = istk->i_type->t_str->sou_first_member;
372 1.67 rillig m != NULL; m = m->s_next) {
373 1.80 rillig
374 1.48 rillig if (m->s_bitfield && m->s_name == unnamed)
375 1.12 christos continue;
376 1.80 rillig
377 1.12 christos if (strcmp(m->s_name, namedmem->n_name) == 0) {
378 1.80 rillig debug_step("found matching member");
379 1.12 christos istk->i_subt = m->s_type;
380 1.80 rillig /* XXX: why ++? */
381 1.43 rillig istk->i_remaining++;
382 1.80 rillig /* XXX: why is i_seen_named_member not set? */
383 1.34 rillig pop_member();
384 1.73 rillig debug_initstack();
385 1.68 rillig debug_leave();
386 1.12 christos return;
387 1.12 christos }
388 1.12 christos }
389 1.80 rillig
390 1.50 rillig /* undefined struct/union member: %s */
391 1.12 christos error(101, namedmem->n_name);
392 1.80 rillig
393 1.34 rillig pop_member();
394 1.77 rillig istk->i_seen_named_member = true;
395 1.73 rillig debug_initstack();
396 1.68 rillig debug_leave();
397 1.12 christos return;
398 1.12 christos }
399 1.80 rillig
400 1.1 cgd /*
401 1.1 cgd * If the removed element was a structure member, we must go
402 1.1 cgd * to the next structure member.
403 1.1 cgd */
404 1.43 rillig if (istk->i_remaining > 0 && istk->i_type->t_tspec == STRUCT &&
405 1.77 rillig !istk->i_seen_named_member) {
406 1.1 cgd do {
407 1.77 rillig m = istk->i_current_object =
408 1.77 rillig istk->i_current_object->s_next;
409 1.80 rillig /* XXX: can this assertion be made to fail? */
410 1.44 rillig lint_assert(m != NULL);
411 1.68 rillig debug_step("pop %s", m->s_name);
412 1.48 rillig } while (m->s_bitfield && m->s_name == unnamed);
413 1.80 rillig /* XXX: duplicate code for skipping unnamed bit-fields */
414 1.1 cgd istk->i_subt = m->s_type;
415 1.1 cgd }
416 1.73 rillig debug_initstack();
417 1.68 rillig debug_leave();
418 1.1 cgd }
419 1.1 cgd
420 1.36 rillig /*
421 1.36 rillig * Take all entries, including the first which requires a closing brace,
422 1.36 rillig * from the stack.
423 1.36 rillig */
424 1.36 rillig static void
425 1.36 rillig initstack_pop_brace(void)
426 1.36 rillig {
427 1.62 rillig bool brace;
428 1.36 rillig
429 1.68 rillig debug_enter();
430 1.73 rillig debug_initstack();
431 1.36 rillig do {
432 1.36 rillig brace = initstk->i_brace;
433 1.68 rillig debug_step("loop brace=%d", brace);
434 1.36 rillig initstack_pop_item();
435 1.36 rillig } while (!brace);
436 1.73 rillig debug_initstack();
437 1.68 rillig debug_leave();
438 1.36 rillig }
439 1.36 rillig
440 1.36 rillig /*
441 1.36 rillig * Take all entries which cannot be used for further initializers from the
442 1.36 rillig * stack, but do this only if they do not require a closing brace.
443 1.36 rillig */
444 1.1 cgd static void
445 1.36 rillig initstack_pop_nobrace(void)
446 1.1 cgd {
447 1.7 lukem
448 1.68 rillig debug_enter();
449 1.43 rillig while (!initstk->i_brace && initstk->i_remaining == 0 &&
450 1.77 rillig !initstk->i_array_of_unknown_size)
451 1.36 rillig initstack_pop_item();
452 1.68 rillig debug_leave();
453 1.1 cgd }
454 1.1 cgd
455 1.1 cgd static void
456 1.35 rillig initstack_push(void)
457 1.1 cgd {
458 1.79 rillig initstack_element *istk, *inxt;
459 1.1 cgd int cnt;
460 1.1 cgd sym_t *m;
461 1.1 cgd
462 1.68 rillig debug_enter();
463 1.73 rillig debug_initstack();
464 1.68 rillig
465 1.1 cgd istk = initstk;
466 1.1 cgd
467 1.1 cgd /* Extend an incomplete array type by one element */
468 1.43 rillig if (istk->i_remaining == 0) {
469 1.68 rillig debug_step("(extend) %s", type_name(istk->i_type));
470 1.1 cgd /*
471 1.1 cgd * Inside of other aggregate types must not be an incomplete
472 1.1 cgd * type.
473 1.1 cgd */
474 1.77 rillig lint_assert(istk->i_enclosing->i_enclosing == NULL);
475 1.43 rillig istk->i_remaining = 1;
476 1.44 rillig lint_assert(istk->i_type->t_tspec == ARRAY);
477 1.1 cgd istk->i_type->t_dim++;
478 1.63 rillig setcomplete(istk->i_type, true);
479 1.1 cgd }
480 1.1 cgd
481 1.44 rillig lint_assert(istk->i_remaining > 0);
482 1.61 rillig lint_assert(istk->i_type == NULL || !is_scalar(istk->i_type->t_tspec));
483 1.1 cgd
484 1.79 rillig initstk = xcalloc(1, sizeof (initstack_element));
485 1.77 rillig initstk->i_enclosing = istk;
486 1.1 cgd initstk->i_type = istk->i_subt;
487 1.44 rillig lint_assert(initstk->i_type->t_tspec != FUNC);
488 1.1 cgd
489 1.12 christos again:
490 1.1 cgd istk = initstk;
491 1.1 cgd
492 1.68 rillig debug_step("typename %s", type_name(istk->i_type));
493 1.1 cgd switch (istk->i_type->t_tspec) {
494 1.1 cgd case ARRAY:
495 1.62 rillig if (namedmem != NULL) {
496 1.68 rillig debug_step("ARRAY %s brace=%d",
497 1.68 rillig namedmem->n_name, istk->i_brace);
498 1.19 christos goto pop;
499 1.77 rillig } else if (istk->i_enclosing->i_seen_named_member) {
500 1.62 rillig istk->i_brace = true;
501 1.68 rillig debug_step("ARRAY brace=%d, namedmem=%d",
502 1.77 rillig istk->i_brace,
503 1.77 rillig istk->i_enclosing->i_seen_named_member);
504 1.20 christos }
505 1.20 christos
506 1.65 rillig if (is_incomplete(istk->i_type) &&
507 1.77 rillig istk->i_enclosing->i_enclosing != NULL) {
508 1.1 cgd /* initialisation of an incomplete type */
509 1.1 cgd error(175);
510 1.62 rillig initerr = true;
511 1.73 rillig debug_initstack();
512 1.68 rillig debug_leave();
513 1.1 cgd return;
514 1.1 cgd }
515 1.1 cgd istk->i_subt = istk->i_type->t_subt;
516 1.77 rillig istk->i_array_of_unknown_size = is_incomplete(istk->i_type);
517 1.43 rillig istk->i_remaining = istk->i_type->t_dim;
518 1.68 rillig debug_step("elements array %s[%d] %s",
519 1.57 rillig type_name(istk->i_subt), istk->i_remaining,
520 1.74 rillig namedmem != NULL ? namedmem->n_name : "*none*");
521 1.1 cgd break;
522 1.1 cgd case UNION:
523 1.1 cgd if (tflag)
524 1.1 cgd /* initialisation of union is illegal in trad. C */
525 1.1 cgd warning(238);
526 1.1 cgd /* FALLTHROUGH */
527 1.1 cgd case STRUCT:
528 1.65 rillig if (is_incomplete(istk->i_type)) {
529 1.1 cgd /* initialisation of an incomplete type */
530 1.1 cgd error(175);
531 1.62 rillig initerr = true;
532 1.73 rillig debug_initstack();
533 1.68 rillig debug_leave();
534 1.1 cgd return;
535 1.1 cgd }
536 1.1 cgd cnt = 0;
537 1.68 rillig debug_step("lookup type=%s, name=%s named=%d",
538 1.57 rillig type_name(istk->i_type),
539 1.74 rillig namedmem != NULL ? namedmem->n_name : "*none*",
540 1.77 rillig istk->i_seen_named_member);
541 1.67 rillig for (m = istk->i_type->t_str->sou_first_member;
542 1.67 rillig m != NULL; m = m->s_next) {
543 1.48 rillig if (m->s_bitfield && m->s_name == unnamed)
544 1.1 cgd continue;
545 1.12 christos if (namedmem != NULL) {
546 1.68 rillig debug_step("named lhs.member=%s, rhs.member=%s",
547 1.68 rillig m->s_name, namedmem->n_name);
548 1.12 christos if (strcmp(m->s_name, namedmem->n_name) == 0) {
549 1.12 christos cnt++;
550 1.12 christos break;
551 1.12 christos } else
552 1.12 christos continue;
553 1.12 christos }
554 1.1 cgd if (++cnt == 1) {
555 1.77 rillig istk->i_current_object = m;
556 1.1 cgd istk->i_subt = m->s_type;
557 1.1 cgd }
558 1.1 cgd }
559 1.12 christos if (namedmem != NULL) {
560 1.12 christos if (m == NULL) {
561 1.68 rillig debug_step("pop struct");
562 1.19 christos goto pop;
563 1.28 rillig }
564 1.77 rillig istk->i_current_object = m;
565 1.26 christos istk->i_subt = m->s_type;
566 1.77 rillig istk->i_seen_named_member = true;
567 1.68 rillig debug_step("named name=%s", namedmem->n_name);
568 1.34 rillig pop_member();
569 1.12 christos cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
570 1.12 christos }
571 1.62 rillig istk->i_brace = true;
572 1.68 rillig debug_step("unnamed type=%s, brace=%d",
573 1.74 rillig type_name(
574 1.74 rillig istk->i_type != NULL ? istk->i_type : istk->i_subt),
575 1.68 rillig istk->i_brace);
576 1.1 cgd if (cnt == 0) {
577 1.1 cgd /* cannot init. struct/union with no named member */
578 1.1 cgd error(179);
579 1.62 rillig initerr = true;
580 1.73 rillig debug_initstack();
581 1.68 rillig debug_leave();
582 1.1 cgd return;
583 1.1 cgd }
584 1.43 rillig istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
585 1.1 cgd break;
586 1.1 cgd default:
587 1.62 rillig if (namedmem != NULL) {
588 1.68 rillig debug_step("pop");
589 1.19 christos pop:
590 1.77 rillig inxt = initstk->i_enclosing;
591 1.12 christos free(istk);
592 1.22 ad initstk = inxt;
593 1.12 christos goto again;
594 1.12 christos }
595 1.43 rillig istk->i_remaining = 1;
596 1.1 cgd break;
597 1.1 cgd }
598 1.68 rillig
599 1.73 rillig debug_initstack();
600 1.68 rillig debug_leave();
601 1.1 cgd }
602 1.1 cgd
603 1.1 cgd static void
604 1.84 rillig check_too_many_initializers(void)
605 1.1 cgd {
606 1.1 cgd
607 1.84 rillig const initstack_element *istk = initstk;
608 1.84 rillig if (istk->i_remaining > 0)
609 1.84 rillig return;
610 1.84 rillig if (istk->i_array_of_unknown_size || istk->i_seen_named_member)
611 1.84 rillig return;
612 1.1 cgd
613 1.84 rillig tspec_t t = istk->i_type->t_tspec;
614 1.84 rillig if (t == ARRAY) {
615 1.84 rillig /* too many array initializers, expected %d */
616 1.84 rillig error(173, istk->i_type->t_dim);
617 1.84 rillig } else if (t == STRUCT || t == UNION) {
618 1.84 rillig /* too many struct/union initializers */
619 1.84 rillig error(172);
620 1.84 rillig } else {
621 1.84 rillig /* too many initializers */
622 1.84 rillig error(174);
623 1.1 cgd }
624 1.84 rillig initerr = true;
625 1.1 cgd }
626 1.1 cgd
627 1.1 cgd static void
628 1.37 rillig initstack_next_brace(void)
629 1.1 cgd {
630 1.7 lukem
631 1.68 rillig debug_enter();
632 1.73 rillig debug_initstack();
633 1.68 rillig
634 1.61 rillig if (initstk->i_type != NULL && is_scalar(initstk->i_type->t_tspec)) {
635 1.49 rillig /* invalid initializer type %s */
636 1.57 rillig error(176, type_name(initstk->i_type));
637 1.62 rillig initerr = true;
638 1.37 rillig }
639 1.37 rillig if (!initerr)
640 1.84 rillig check_too_many_initializers();
641 1.37 rillig if (!initerr)
642 1.37 rillig initstack_push();
643 1.37 rillig if (!initerr) {
644 1.62 rillig initstk->i_brace = true;
645 1.68 rillig debug_step("%p %s", namedmem, type_name(
646 1.74 rillig initstk->i_type != NULL ? initstk->i_type
647 1.74 rillig : initstk->i_subt));
648 1.37 rillig }
649 1.68 rillig
650 1.73 rillig debug_initstack();
651 1.68 rillig debug_leave();
652 1.37 rillig }
653 1.37 rillig
654 1.37 rillig static void
655 1.37 rillig initstack_next_nobrace(void)
656 1.37 rillig {
657 1.68 rillig debug_enter();
658 1.73 rillig debug_initstack();
659 1.37 rillig
660 1.61 rillig if (initstk->i_type == NULL && !is_scalar(initstk->i_subt->t_tspec)) {
661 1.37 rillig /* {}-enclosed initializer required */
662 1.37 rillig error(181);
663 1.37 rillig }
664 1.37 rillig
665 1.84 rillig if (!initerr)
666 1.84 rillig check_too_many_initializers();
667 1.84 rillig
668 1.71 rillig /*
669 1.71 rillig * Make sure an entry with a scalar type is at the top of the stack.
670 1.71 rillig *
671 1.71 rillig * FIXME: Since C99 an initializer for an object with automatic
672 1.71 rillig * storage need not be a constant expression anymore. It is
673 1.71 rillig * perfectly fine to initialize a struct with a struct expression,
674 1.71 rillig * see d_struct_init_nested.c for a demonstration.
675 1.71 rillig */
676 1.42 rillig while (!initerr) {
677 1.42 rillig if ((initstk->i_type != NULL &&
678 1.61 rillig is_scalar(initstk->i_type->t_tspec)))
679 1.42 rillig break;
680 1.42 rillig initstack_push();
681 1.1 cgd }
682 1.68 rillig
683 1.73 rillig debug_initstack();
684 1.68 rillig debug_leave();
685 1.1 cgd }
686 1.1 cgd
687 1.1 cgd void
688 1.34 rillig init_lbrace(void)
689 1.1 cgd {
690 1.1 cgd if (initerr)
691 1.1 cgd return;
692 1.1 cgd
693 1.68 rillig debug_enter();
694 1.73 rillig debug_initstack();
695 1.68 rillig
696 1.1 cgd if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
697 1.77 rillig initstk->i_enclosing == NULL) {
698 1.61 rillig if (tflag && !is_scalar(initstk->i_subt->t_tspec))
699 1.50 rillig /* no automatic aggregate initialization in trad. C */
700 1.1 cgd warning(188);
701 1.1 cgd }
702 1.1 cgd
703 1.1 cgd /*
704 1.1 cgd * Remove all entries which cannot be used for further initializers
705 1.1 cgd * and do not expect a closing brace.
706 1.1 cgd */
707 1.36 rillig initstack_pop_nobrace();
708 1.1 cgd
709 1.37 rillig initstack_next_brace();
710 1.68 rillig
711 1.73 rillig debug_initstack();
712 1.68 rillig debug_leave();
713 1.1 cgd }
714 1.1 cgd
715 1.1 cgd void
716 1.34 rillig init_rbrace(void)
717 1.1 cgd {
718 1.1 cgd if (initerr)
719 1.1 cgd return;
720 1.1 cgd
721 1.68 rillig debug_enter();
722 1.73 rillig debug_initstack();
723 1.68 rillig
724 1.36 rillig initstack_pop_brace();
725 1.68 rillig
726 1.73 rillig debug_initstack();
727 1.68 rillig debug_leave();
728 1.1 cgd }
729 1.1 cgd
730 1.1 cgd void
731 1.69 rillig init_using_expr(tnode_t *tn)
732 1.1 cgd {
733 1.1 cgd tspec_t lt, rt;
734 1.1 cgd tnode_t *ln;
735 1.1 cgd struct mbl *tmem;
736 1.81 rillig scl_t sclass;
737 1.1 cgd
738 1.68 rillig debug_enter();
739 1.68 rillig debug_named_member();
740 1.76 rillig debug_node(tn, debug_ind);
741 1.73 rillig debug_initstack();
742 1.55 rillig
743 1.68 rillig if (initerr || tn == NULL) {
744 1.68 rillig debug_leave();
745 1.25 christos return;
746 1.68 rillig }
747 1.1 cgd
748 1.81 rillig sclass = initsym->s_scl;
749 1.1 cgd
750 1.1 cgd /*
751 1.13 christos * Do not test for automatic aggregate initialisation. If the
752 1.9 wiz * initializer starts with a brace we have the warning already.
753 1.1 cgd * If not, an error will be printed that the initializer must
754 1.1 cgd * be enclosed by braces.
755 1.1 cgd */
756 1.1 cgd
757 1.1 cgd /*
758 1.1 cgd * Local initialisation of non-array-types with only one expression
759 1.1 cgd * without braces is done by ASSIGN
760 1.1 cgd */
761 1.81 rillig if ((sclass == AUTO || sclass == REG) &&
762 1.77 rillig initsym->s_type->t_tspec != ARRAY && initstk->i_enclosing == NULL) {
763 1.82 rillig debug_step("handing over to ASSIGN");
764 1.60 rillig ln = new_name_node(initsym, 0);
765 1.1 cgd ln->tn_type = tduptyp(ln->tn_type);
766 1.62 rillig ln->tn_type->t_const = false;
767 1.1 cgd tn = build(ASSIGN, ln, tn);
768 1.66 rillig expr(tn, false, false, false, false);
769 1.82 rillig /* XXX: why not clean up the initstack here already? */
770 1.68 rillig debug_leave();
771 1.25 christos return;
772 1.1 cgd }
773 1.1 cgd
774 1.36 rillig initstack_pop_nobrace();
775 1.1 cgd
776 1.82 rillig if (init_array_using_string(tn)) {
777 1.82 rillig debug_step("after initializing the string:");
778 1.82 rillig /* XXX: why not clean up the initstack here already? */
779 1.73 rillig debug_initstack();
780 1.68 rillig debug_leave();
781 1.25 christos return;
782 1.68 rillig }
783 1.1 cgd
784 1.37 rillig initstack_next_nobrace();
785 1.68 rillig if (initerr || tn == NULL) {
786 1.73 rillig debug_initstack();
787 1.68 rillig debug_leave();
788 1.25 christos return;
789 1.68 rillig }
790 1.1 cgd
791 1.43 rillig initstk->i_remaining--;
792 1.83 rillig debug_step("%d elements remaining", initstk->i_remaining);
793 1.83 rillig
794 1.1 cgd /* Create a temporary node for the left side. */
795 1.1 cgd ln = tgetblk(sizeof (tnode_t));
796 1.1 cgd ln->tn_op = NAME;
797 1.1 cgd ln->tn_type = tduptyp(initstk->i_type);
798 1.62 rillig ln->tn_type->t_const = false;
799 1.62 rillig ln->tn_lvalue = true;
800 1.1 cgd ln->tn_sym = initsym; /* better than nothing */
801 1.1 cgd
802 1.1 cgd tn = cconv(tn);
803 1.1 cgd
804 1.1 cgd lt = ln->tn_type->t_tspec;
805 1.1 cgd rt = tn->tn_type->t_tspec;
806 1.1 cgd
807 1.61 rillig lint_assert(is_scalar(lt));
808 1.1 cgd
809 1.68 rillig if (!typeok(INIT, 0, ln, tn)) {
810 1.73 rillig debug_initstack();
811 1.68 rillig debug_leave();
812 1.25 christos return;
813 1.68 rillig }
814 1.1 cgd
815 1.1 cgd /*
816 1.38 rillig * Store the tree memory. This is necessary because otherwise
817 1.1 cgd * expr() would free it.
818 1.1 cgd */
819 1.1 cgd tmem = tsave();
820 1.66 rillig expr(tn, true, false, true, false);
821 1.1 cgd trestor(tmem);
822 1.7 lukem
823 1.61 rillig if (is_integer(lt) && ln->tn_type->t_bitfield && !is_integer(rt)) {
824 1.1 cgd /*
825 1.1 cgd * Bit-fields can be initialized in trad. C only by integer
826 1.1 cgd * constants.
827 1.1 cgd */
828 1.1 cgd if (tflag)
829 1.1 cgd /* bit-field initialisation is illegal in trad. C */
830 1.1 cgd warning(186);
831 1.1 cgd }
832 1.1 cgd
833 1.59 rillig if (lt != rt || (initstk->i_type->t_bitfield && tn->tn_op == CON))
834 1.1 cgd tn = convert(INIT, 0, initstk->i_type, tn);
835 1.1 cgd
836 1.1 cgd if (tn != NULL && tn->tn_op != CON) {
837 1.81 rillig sym_t *sym;
838 1.81 rillig ptrdiff_t offs;
839 1.64 rillig if (!constant_addr(tn, &sym, &offs)) {
840 1.81 rillig if (sclass == AUTO || sclass == REG) {
841 1.1 cgd /* non-constant initializer */
842 1.58 rillig c99ism(177);
843 1.1 cgd } else {
844 1.1 cgd /* non-constant initializer */
845 1.1 cgd error(177);
846 1.1 cgd }
847 1.1 cgd }
848 1.1 cgd }
849 1.68 rillig
850 1.73 rillig debug_initstack();
851 1.68 rillig debug_leave();
852 1.1 cgd }
853 1.1 cgd
854 1.1 cgd
855 1.82 rillig /* Initialize a character array or wchar_t array with a string literal. */
856 1.62 rillig static bool
857 1.82 rillig init_array_using_string(tnode_t *tn)
858 1.1 cgd {
859 1.1 cgd tspec_t t;
860 1.79 rillig initstack_element *istk;
861 1.1 cgd int len;
862 1.1 cgd strg_t *strg;
863 1.1 cgd
864 1.1 cgd if (tn->tn_op != STRING)
865 1.63 rillig return false;
866 1.1 cgd
867 1.68 rillig debug_enter();
868 1.73 rillig debug_initstack();
869 1.68 rillig
870 1.1 cgd istk = initstk;
871 1.47 rillig strg = tn->tn_string;
872 1.1 cgd
873 1.1 cgd /*
874 1.1 cgd * Check if we have an array type which can be initialized by
875 1.1 cgd * the string.
876 1.1 cgd */
877 1.12 christos if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
878 1.68 rillig debug_step("subt array");
879 1.1 cgd t = istk->i_subt->t_subt->t_tspec;
880 1.1 cgd if (!((strg->st_tspec == CHAR &&
881 1.1 cgd (t == CHAR || t == UCHAR || t == SCHAR)) ||
882 1.1 cgd (strg->st_tspec == WCHAR && t == WCHAR))) {
883 1.68 rillig debug_leave();
884 1.63 rillig return false;
885 1.1 cgd }
886 1.82 rillig /* XXX: duplicate code, see below */
887 1.1 cgd /* Put the array at top of stack */
888 1.35 rillig initstack_push();
889 1.1 cgd istk = initstk;
890 1.1 cgd } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
891 1.68 rillig debug_step("type array");
892 1.1 cgd t = istk->i_type->t_subt->t_tspec;
893 1.1 cgd if (!((strg->st_tspec == CHAR &&
894 1.1 cgd (t == CHAR || t == UCHAR || t == SCHAR)) ||
895 1.1 cgd (strg->st_tspec == WCHAR && t == WCHAR))) {
896 1.68 rillig debug_leave();
897 1.63 rillig return false;
898 1.1 cgd }
899 1.82 rillig /* XXX: duplicate code, see above */
900 1.1 cgd /*
901 1.1 cgd * If the array is already partly initialized, we are
902 1.1 cgd * wrong here.
903 1.1 cgd */
904 1.43 rillig if (istk->i_remaining != istk->i_type->t_dim)
905 1.68 rillig debug_leave();
906 1.63 rillig return false;
907 1.1 cgd } else {
908 1.68 rillig debug_leave();
909 1.63 rillig return false;
910 1.1 cgd }
911 1.1 cgd
912 1.1 cgd /* Get length without trailing NUL character. */
913 1.1 cgd len = strg->st_len;
914 1.1 cgd
915 1.77 rillig if (istk->i_array_of_unknown_size) {
916 1.77 rillig istk->i_array_of_unknown_size = false;
917 1.1 cgd istk->i_type->t_dim = len + 1;
918 1.63 rillig setcomplete(istk->i_type, true);
919 1.1 cgd } else {
920 1.1 cgd if (istk->i_type->t_dim < len) {
921 1.1 cgd /* non-null byte ignored in string initializer */
922 1.1 cgd warning(187);
923 1.1 cgd }
924 1.1 cgd }
925 1.1 cgd
926 1.1 cgd /* In every case the array is initialized completely. */
927 1.43 rillig istk->i_remaining = 0;
928 1.1 cgd
929 1.73 rillig debug_initstack();
930 1.68 rillig debug_leave();
931 1.63 rillig return true;
932 1.1 cgd }
933