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