init.c revision 1.78 1 1.78 rillig /* $NetBSD: init.c,v 1.78 2021/02/21 08:27:41 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.78 rillig __RCSID("$NetBSD: init.c,v 1.78 2021/02/21 08:27:41 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.53 rillig typedef struct istk {
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.77 rillig struct istk *i_enclosing;
97 1.53 rillig } istk_t;
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.1 cgd istk_t *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.78 rillig debug_initstack_element(const istk_t *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.77 rillig for (const istk_t *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.75 rillig #define debug_initstack() do { } while (false)
296 1.73 rillig #endif
297 1.73 rillig
298 1.1 cgd /*
299 1.77 rillig * Initialize the initialisation stack by putting an entry for the object
300 1.1 cgd * which is to be initialized on it.
301 1.1 cgd */
302 1.1 cgd void
303 1.35 rillig initstack_init(void)
304 1.1 cgd {
305 1.1 cgd istk_t *istk;
306 1.1 cgd
307 1.1 cgd if (initerr)
308 1.1 cgd return;
309 1.1 cgd
310 1.1 cgd /* free memory used in last initialisation */
311 1.1 cgd while ((istk = initstk) != NULL) {
312 1.77 rillig initstk = istk->i_enclosing;
313 1.1 cgd free(istk);
314 1.1 cgd }
315 1.1 cgd
316 1.70 rillig debug_enter();
317 1.38 rillig
318 1.1 cgd /*
319 1.77 rillig * If the type which is to be initialized is an incomplete array,
320 1.1 cgd * it must be duplicated.
321 1.1 cgd */
322 1.65 rillig if (initsym->s_type->t_tspec == ARRAY && is_incomplete(initsym->s_type))
323 1.1 cgd initsym->s_type = duptyp(initsym->s_type);
324 1.1 cgd
325 1.1 cgd istk = initstk = xcalloc(1, sizeof (istk_t));
326 1.1 cgd istk->i_subt = initsym->s_type;
327 1.43 rillig istk->i_remaining = 1;
328 1.70 rillig
329 1.73 rillig debug_initstack();
330 1.70 rillig debug_leave();
331 1.1 cgd }
332 1.1 cgd
333 1.1 cgd static void
334 1.35 rillig initstack_pop_item(void)
335 1.1 cgd {
336 1.1 cgd istk_t *istk;
337 1.1 cgd sym_t *m;
338 1.1 cgd
339 1.68 rillig debug_enter();
340 1.68 rillig
341 1.41 rillig istk = initstk;
342 1.68 rillig debug_step("pop type=%s, brace=%d remaining=%d named=%d",
343 1.74 rillig type_name(istk->i_type != NULL ? istk->i_type : istk->i_subt),
344 1.77 rillig istk->i_brace, istk->i_remaining, istk->i_seen_named_member);
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.68 rillig debug_step("top type=%s, brace=%d remaining=%d named=%d",
352 1.74 rillig type_name(istk->i_type != NULL ? istk->i_type : istk->i_subt),
353 1.77 rillig istk->i_brace, istk->i_remaining, istk->i_seen_named_member);
354 1.1 cgd
355 1.43 rillig istk->i_remaining--;
356 1.44 rillig lint_assert(istk->i_remaining >= 0);
357 1.1 cgd
358 1.68 rillig debug_step("top remaining=%d rhs.name=%s",
359 1.74 rillig istk->i_remaining, namedmem != NULL ? namedmem->n_name : "*null*");
360 1.40 rillig
361 1.43 rillig if (istk->i_remaining >= 0 && namedmem != NULL) {
362 1.40 rillig
363 1.68 rillig debug_step("named remaining=%d type=%s, rhs.name=%s",
364 1.68 rillig istk->i_remaining, type_name(istk->i_type),
365 1.68 rillig namedmem->n_name);
366 1.38 rillig
367 1.67 rillig for (m = istk->i_type->t_str->sou_first_member;
368 1.67 rillig m != NULL; m = m->s_next) {
369 1.68 rillig debug_step("pop lhs.name=%s rhs.name=%s",
370 1.68 rillig m->s_name, namedmem->n_name);
371 1.48 rillig if (m->s_bitfield && m->s_name == unnamed)
372 1.12 christos continue;
373 1.12 christos if (strcmp(m->s_name, namedmem->n_name) == 0) {
374 1.12 christos istk->i_subt = m->s_type;
375 1.43 rillig istk->i_remaining++;
376 1.34 rillig pop_member();
377 1.73 rillig debug_initstack();
378 1.68 rillig debug_leave();
379 1.12 christos return;
380 1.12 christos }
381 1.12 christos }
382 1.50 rillig /* undefined struct/union member: %s */
383 1.12 christos error(101, namedmem->n_name);
384 1.68 rillig debug_step("end rhs.name=%s", namedmem->n_name);
385 1.34 rillig pop_member();
386 1.77 rillig istk->i_seen_named_member = true;
387 1.73 rillig debug_initstack();
388 1.68 rillig debug_leave();
389 1.12 christos return;
390 1.12 christos }
391 1.1 cgd /*
392 1.1 cgd * If the removed element was a structure member, we must go
393 1.1 cgd * to the next structure member.
394 1.1 cgd */
395 1.43 rillig if (istk->i_remaining > 0 && istk->i_type->t_tspec == STRUCT &&
396 1.77 rillig !istk->i_seen_named_member) {
397 1.1 cgd do {
398 1.77 rillig m = istk->i_current_object =
399 1.77 rillig istk->i_current_object->s_next;
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.1 cgd istk->i_subt = m->s_type;
404 1.1 cgd }
405 1.73 rillig debug_initstack();
406 1.68 rillig debug_leave();
407 1.1 cgd }
408 1.1 cgd
409 1.36 rillig /*
410 1.36 rillig * Take all entries, including the first which requires a closing brace,
411 1.36 rillig * from the stack.
412 1.36 rillig */
413 1.36 rillig static void
414 1.36 rillig initstack_pop_brace(void)
415 1.36 rillig {
416 1.62 rillig bool brace;
417 1.36 rillig
418 1.68 rillig debug_enter();
419 1.73 rillig debug_initstack();
420 1.36 rillig do {
421 1.36 rillig brace = initstk->i_brace;
422 1.68 rillig debug_step("loop brace=%d", brace);
423 1.36 rillig initstack_pop_item();
424 1.36 rillig } while (!brace);
425 1.73 rillig debug_initstack();
426 1.68 rillig debug_leave();
427 1.36 rillig }
428 1.36 rillig
429 1.36 rillig /*
430 1.36 rillig * Take all entries which cannot be used for further initializers from the
431 1.36 rillig * stack, but do this only if they do not require a closing brace.
432 1.36 rillig */
433 1.1 cgd static void
434 1.36 rillig initstack_pop_nobrace(void)
435 1.1 cgd {
436 1.7 lukem
437 1.68 rillig debug_enter();
438 1.73 rillig debug_initstack();
439 1.43 rillig while (!initstk->i_brace && initstk->i_remaining == 0 &&
440 1.77 rillig !initstk->i_array_of_unknown_size)
441 1.36 rillig initstack_pop_item();
442 1.73 rillig debug_initstack();
443 1.68 rillig debug_leave();
444 1.1 cgd }
445 1.1 cgd
446 1.1 cgd static void
447 1.35 rillig initstack_push(void)
448 1.1 cgd {
449 1.22 ad istk_t *istk, *inxt;
450 1.1 cgd int cnt;
451 1.1 cgd sym_t *m;
452 1.1 cgd
453 1.68 rillig debug_enter();
454 1.73 rillig debug_initstack();
455 1.68 rillig
456 1.1 cgd istk = initstk;
457 1.1 cgd
458 1.1 cgd /* Extend an incomplete array type by one element */
459 1.43 rillig if (istk->i_remaining == 0) {
460 1.68 rillig debug_step("(extend) %s", type_name(istk->i_type));
461 1.1 cgd /*
462 1.1 cgd * Inside of other aggregate types must not be an incomplete
463 1.1 cgd * type.
464 1.1 cgd */
465 1.77 rillig lint_assert(istk->i_enclosing->i_enclosing == NULL);
466 1.43 rillig istk->i_remaining = 1;
467 1.44 rillig lint_assert(istk->i_type->t_tspec == ARRAY);
468 1.1 cgd istk->i_type->t_dim++;
469 1.63 rillig setcomplete(istk->i_type, true);
470 1.1 cgd }
471 1.1 cgd
472 1.44 rillig lint_assert(istk->i_remaining > 0);
473 1.61 rillig lint_assert(istk->i_type == NULL || !is_scalar(istk->i_type->t_tspec));
474 1.1 cgd
475 1.1 cgd initstk = xcalloc(1, sizeof (istk_t));
476 1.77 rillig initstk->i_enclosing = istk;
477 1.1 cgd initstk->i_type = istk->i_subt;
478 1.44 rillig lint_assert(initstk->i_type->t_tspec != FUNC);
479 1.1 cgd
480 1.12 christos again:
481 1.1 cgd istk = initstk;
482 1.1 cgd
483 1.68 rillig debug_step("typename %s", type_name(istk->i_type));
484 1.1 cgd switch (istk->i_type->t_tspec) {
485 1.1 cgd case ARRAY:
486 1.62 rillig if (namedmem != NULL) {
487 1.68 rillig debug_step("ARRAY %s brace=%d",
488 1.68 rillig namedmem->n_name, istk->i_brace);
489 1.19 christos goto pop;
490 1.77 rillig } else if (istk->i_enclosing->i_seen_named_member) {
491 1.62 rillig istk->i_brace = true;
492 1.68 rillig debug_step("ARRAY brace=%d, namedmem=%d",
493 1.77 rillig istk->i_brace,
494 1.77 rillig istk->i_enclosing->i_seen_named_member);
495 1.20 christos }
496 1.20 christos
497 1.65 rillig if (is_incomplete(istk->i_type) &&
498 1.77 rillig istk->i_enclosing->i_enclosing != NULL) {
499 1.1 cgd /* initialisation of an incomplete type */
500 1.1 cgd error(175);
501 1.62 rillig initerr = true;
502 1.73 rillig debug_initstack();
503 1.68 rillig debug_leave();
504 1.1 cgd return;
505 1.1 cgd }
506 1.1 cgd istk->i_subt = istk->i_type->t_subt;
507 1.77 rillig istk->i_array_of_unknown_size = is_incomplete(istk->i_type);
508 1.43 rillig istk->i_remaining = istk->i_type->t_dim;
509 1.68 rillig debug_step("elements array %s[%d] %s",
510 1.57 rillig type_name(istk->i_subt), istk->i_remaining,
511 1.74 rillig namedmem != NULL ? namedmem->n_name : "*none*");
512 1.1 cgd break;
513 1.1 cgd case UNION:
514 1.1 cgd if (tflag)
515 1.1 cgd /* initialisation of union is illegal in trad. C */
516 1.1 cgd warning(238);
517 1.1 cgd /* FALLTHROUGH */
518 1.1 cgd case STRUCT:
519 1.65 rillig if (is_incomplete(istk->i_type)) {
520 1.1 cgd /* initialisation of an incomplete type */
521 1.1 cgd error(175);
522 1.62 rillig initerr = true;
523 1.73 rillig debug_initstack();
524 1.68 rillig debug_leave();
525 1.1 cgd return;
526 1.1 cgd }
527 1.1 cgd cnt = 0;
528 1.68 rillig debug_step("lookup type=%s, name=%s named=%d",
529 1.57 rillig type_name(istk->i_type),
530 1.74 rillig namedmem != NULL ? namedmem->n_name : "*none*",
531 1.77 rillig istk->i_seen_named_member);
532 1.67 rillig for (m = istk->i_type->t_str->sou_first_member;
533 1.67 rillig m != NULL; m = m->s_next) {
534 1.48 rillig if (m->s_bitfield && m->s_name == unnamed)
535 1.1 cgd continue;
536 1.12 christos if (namedmem != NULL) {
537 1.68 rillig debug_step("named lhs.member=%s, rhs.member=%s",
538 1.68 rillig m->s_name, namedmem->n_name);
539 1.12 christos if (strcmp(m->s_name, namedmem->n_name) == 0) {
540 1.12 christos cnt++;
541 1.12 christos break;
542 1.12 christos } else
543 1.12 christos continue;
544 1.12 christos }
545 1.1 cgd if (++cnt == 1) {
546 1.77 rillig istk->i_current_object = m;
547 1.1 cgd istk->i_subt = m->s_type;
548 1.1 cgd }
549 1.1 cgd }
550 1.12 christos if (namedmem != NULL) {
551 1.12 christos if (m == NULL) {
552 1.68 rillig debug_step("pop struct");
553 1.19 christos goto pop;
554 1.28 rillig }
555 1.77 rillig istk->i_current_object = m;
556 1.26 christos istk->i_subt = m->s_type;
557 1.77 rillig istk->i_seen_named_member = true;
558 1.68 rillig debug_step("named name=%s", namedmem->n_name);
559 1.34 rillig pop_member();
560 1.12 christos cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
561 1.12 christos }
562 1.62 rillig istk->i_brace = true;
563 1.68 rillig debug_step("unnamed type=%s, brace=%d",
564 1.74 rillig type_name(
565 1.74 rillig istk->i_type != NULL ? istk->i_type : istk->i_subt),
566 1.68 rillig istk->i_brace);
567 1.1 cgd if (cnt == 0) {
568 1.1 cgd /* cannot init. struct/union with no named member */
569 1.1 cgd error(179);
570 1.62 rillig initerr = true;
571 1.73 rillig debug_initstack();
572 1.68 rillig debug_leave();
573 1.1 cgd return;
574 1.1 cgd }
575 1.43 rillig istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
576 1.1 cgd break;
577 1.1 cgd default:
578 1.62 rillig if (namedmem != NULL) {
579 1.68 rillig debug_step("pop");
580 1.19 christos pop:
581 1.77 rillig inxt = initstk->i_enclosing;
582 1.12 christos free(istk);
583 1.22 ad initstk = inxt;
584 1.12 christos goto again;
585 1.12 christos }
586 1.43 rillig istk->i_remaining = 1;
587 1.1 cgd break;
588 1.1 cgd }
589 1.68 rillig
590 1.73 rillig debug_initstack();
591 1.68 rillig debug_leave();
592 1.1 cgd }
593 1.1 cgd
594 1.1 cgd static void
595 1.35 rillig initstack_check_too_many(void)
596 1.1 cgd {
597 1.1 cgd istk_t *istk;
598 1.1 cgd
599 1.1 cgd istk = initstk;
600 1.1 cgd
601 1.1 cgd /*
602 1.1 cgd * If a closing brace is expected we have at least one initializer
603 1.1 cgd * too much.
604 1.1 cgd */
605 1.77 rillig if (istk->i_remaining == 0 && !istk->i_array_of_unknown_size &&
606 1.77 rillig !istk->i_seen_named_member) {
607 1.1 cgd switch (istk->i_type->t_tspec) {
608 1.1 cgd case ARRAY:
609 1.49 rillig /* too many array initializers, expected %d */
610 1.23 christos error(173, istk->i_type->t_dim);
611 1.1 cgd break;
612 1.1 cgd case STRUCT:
613 1.1 cgd case UNION:
614 1.1 cgd /* too many struct/union initializers */
615 1.1 cgd error(172);
616 1.1 cgd break;
617 1.1 cgd default:
618 1.1 cgd /* too many initializers */
619 1.1 cgd error(174);
620 1.1 cgd break;
621 1.1 cgd }
622 1.62 rillig initerr = true;
623 1.1 cgd }
624 1.1 cgd }
625 1.1 cgd
626 1.1 cgd static void
627 1.37 rillig initstack_next_brace(void)
628 1.1 cgd {
629 1.7 lukem
630 1.68 rillig debug_enter();
631 1.73 rillig debug_initstack();
632 1.68 rillig
633 1.61 rillig if (initstk->i_type != NULL && is_scalar(initstk->i_type->t_tspec)) {
634 1.49 rillig /* invalid initializer type %s */
635 1.57 rillig error(176, type_name(initstk->i_type));
636 1.62 rillig initerr = true;
637 1.37 rillig }
638 1.37 rillig if (!initerr)
639 1.37 rillig initstack_check_too_many();
640 1.37 rillig if (!initerr)
641 1.37 rillig initstack_push();
642 1.37 rillig if (!initerr) {
643 1.62 rillig initstk->i_brace = true;
644 1.68 rillig debug_step("%p %s", namedmem, type_name(
645 1.74 rillig initstk->i_type != NULL ? initstk->i_type
646 1.74 rillig : initstk->i_subt));
647 1.37 rillig }
648 1.68 rillig
649 1.73 rillig debug_initstack();
650 1.68 rillig debug_leave();
651 1.37 rillig }
652 1.37 rillig
653 1.37 rillig static void
654 1.37 rillig initstack_next_nobrace(void)
655 1.37 rillig {
656 1.68 rillig debug_enter();
657 1.73 rillig debug_initstack();
658 1.37 rillig
659 1.61 rillig if (initstk->i_type == NULL && !is_scalar(initstk->i_subt->t_tspec)) {
660 1.37 rillig /* {}-enclosed initializer required */
661 1.37 rillig error(181);
662 1.37 rillig }
663 1.37 rillig
664 1.71 rillig /*
665 1.71 rillig * Make sure an entry with a scalar type is at the top of the stack.
666 1.71 rillig *
667 1.71 rillig * FIXME: Since C99 an initializer for an object with automatic
668 1.71 rillig * storage need not be a constant expression anymore. It is
669 1.71 rillig * perfectly fine to initialize a struct with a struct expression,
670 1.71 rillig * see d_struct_init_nested.c for a demonstration.
671 1.71 rillig */
672 1.37 rillig if (!initerr)
673 1.37 rillig initstack_check_too_many();
674 1.42 rillig while (!initerr) {
675 1.42 rillig if ((initstk->i_type != NULL &&
676 1.61 rillig is_scalar(initstk->i_type->t_tspec)))
677 1.42 rillig break;
678 1.42 rillig initstack_push();
679 1.1 cgd }
680 1.68 rillig
681 1.73 rillig debug_initstack();
682 1.68 rillig debug_leave();
683 1.1 cgd }
684 1.1 cgd
685 1.1 cgd void
686 1.34 rillig init_lbrace(void)
687 1.1 cgd {
688 1.1 cgd if (initerr)
689 1.1 cgd return;
690 1.1 cgd
691 1.68 rillig debug_enter();
692 1.73 rillig debug_initstack();
693 1.68 rillig
694 1.1 cgd if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
695 1.77 rillig initstk->i_enclosing == NULL) {
696 1.61 rillig if (tflag && !is_scalar(initstk->i_subt->t_tspec))
697 1.50 rillig /* no automatic aggregate initialization in trad. C */
698 1.1 cgd warning(188);
699 1.1 cgd }
700 1.1 cgd
701 1.1 cgd /*
702 1.1 cgd * Remove all entries which cannot be used for further initializers
703 1.1 cgd * and do not expect a closing brace.
704 1.1 cgd */
705 1.36 rillig initstack_pop_nobrace();
706 1.1 cgd
707 1.37 rillig initstack_next_brace();
708 1.68 rillig
709 1.73 rillig debug_initstack();
710 1.68 rillig debug_leave();
711 1.1 cgd }
712 1.1 cgd
713 1.1 cgd void
714 1.34 rillig init_rbrace(void)
715 1.1 cgd {
716 1.1 cgd if (initerr)
717 1.1 cgd return;
718 1.1 cgd
719 1.68 rillig debug_enter();
720 1.73 rillig debug_initstack();
721 1.68 rillig
722 1.36 rillig initstack_pop_brace();
723 1.68 rillig
724 1.73 rillig debug_initstack();
725 1.68 rillig debug_leave();
726 1.1 cgd }
727 1.1 cgd
728 1.1 cgd void
729 1.69 rillig init_using_expr(tnode_t *tn)
730 1.1 cgd {
731 1.1 cgd ptrdiff_t offs;
732 1.1 cgd sym_t *sym;
733 1.1 cgd tspec_t lt, rt;
734 1.1 cgd tnode_t *ln;
735 1.1 cgd struct mbl *tmem;
736 1.1 cgd scl_t sc;
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.1 cgd sc = 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.1 cgd if ((sc == AUTO || sc == REG) &&
762 1.77 rillig initsym->s_type->t_tspec != ARRAY && initstk->i_enclosing == NULL) {
763 1.60 rillig ln = new_name_node(initsym, 0);
764 1.1 cgd ln->tn_type = tduptyp(ln->tn_type);
765 1.62 rillig ln->tn_type->t_const = false;
766 1.1 cgd tn = build(ASSIGN, ln, tn);
767 1.66 rillig expr(tn, false, false, false, false);
768 1.73 rillig debug_initstack();
769 1.68 rillig debug_leave();
770 1.25 christos return;
771 1.1 cgd }
772 1.1 cgd
773 1.1 cgd /*
774 1.1 cgd * Remove all entries which cannot be used for further initializers
775 1.1 cgd * and do not require a closing brace.
776 1.1 cgd */
777 1.36 rillig initstack_pop_nobrace();
778 1.1 cgd
779 1.35 rillig /* Initialisations by strings are done in initstack_string(). */
780 1.68 rillig if (initstack_string(tn)) {
781 1.73 rillig debug_initstack();
782 1.68 rillig debug_leave();
783 1.25 christos return;
784 1.68 rillig }
785 1.1 cgd
786 1.37 rillig initstack_next_nobrace();
787 1.68 rillig if (initerr || tn == NULL) {
788 1.73 rillig debug_initstack();
789 1.68 rillig debug_leave();
790 1.25 christos return;
791 1.68 rillig }
792 1.1 cgd
793 1.43 rillig initstk->i_remaining--;
794 1.68 rillig debug_step("remaining=%d tn=%p", initstk->i_remaining, tn);
795 1.1 cgd /* Create a temporary node for the left side. */
796 1.1 cgd ln = tgetblk(sizeof (tnode_t));
797 1.1 cgd ln->tn_op = NAME;
798 1.1 cgd ln->tn_type = tduptyp(initstk->i_type);
799 1.62 rillig ln->tn_type->t_const = false;
800 1.62 rillig ln->tn_lvalue = true;
801 1.1 cgd ln->tn_sym = initsym; /* better than nothing */
802 1.1 cgd
803 1.1 cgd tn = cconv(tn);
804 1.1 cgd
805 1.1 cgd lt = ln->tn_type->t_tspec;
806 1.1 cgd rt = tn->tn_type->t_tspec;
807 1.1 cgd
808 1.61 rillig lint_assert(is_scalar(lt));
809 1.1 cgd
810 1.68 rillig if (!typeok(INIT, 0, ln, tn)) {
811 1.73 rillig debug_initstack();
812 1.68 rillig debug_leave();
813 1.25 christos return;
814 1.68 rillig }
815 1.1 cgd
816 1.1 cgd /*
817 1.38 rillig * Store the tree memory. This is necessary because otherwise
818 1.1 cgd * expr() would free it.
819 1.1 cgd */
820 1.1 cgd tmem = tsave();
821 1.66 rillig expr(tn, true, false, true, false);
822 1.1 cgd trestor(tmem);
823 1.7 lukem
824 1.61 rillig if (is_integer(lt) && ln->tn_type->t_bitfield && !is_integer(rt)) {
825 1.1 cgd /*
826 1.1 cgd * Bit-fields can be initialized in trad. C only by integer
827 1.1 cgd * constants.
828 1.1 cgd */
829 1.1 cgd if (tflag)
830 1.1 cgd /* bit-field initialisation is illegal in trad. C */
831 1.1 cgd warning(186);
832 1.1 cgd }
833 1.1 cgd
834 1.59 rillig if (lt != rt || (initstk->i_type->t_bitfield && tn->tn_op == CON))
835 1.1 cgd tn = convert(INIT, 0, initstk->i_type, tn);
836 1.1 cgd
837 1.1 cgd if (tn != NULL && tn->tn_op != CON) {
838 1.1 cgd sym = NULL;
839 1.1 cgd offs = 0;
840 1.64 rillig if (!constant_addr(tn, &sym, &offs)) {
841 1.3 jpo if (sc == AUTO || sc == REG) {
842 1.1 cgd /* non-constant initializer */
843 1.58 rillig c99ism(177);
844 1.1 cgd } else {
845 1.1 cgd /* non-constant initializer */
846 1.1 cgd error(177);
847 1.1 cgd }
848 1.1 cgd }
849 1.1 cgd }
850 1.68 rillig
851 1.73 rillig debug_initstack();
852 1.68 rillig debug_leave();
853 1.1 cgd }
854 1.1 cgd
855 1.1 cgd
856 1.62 rillig static bool
857 1.35 rillig initstack_string(tnode_t *tn)
858 1.1 cgd {
859 1.1 cgd tspec_t t;
860 1.1 cgd istk_t *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.1 cgd /* Put the array at top of stack */
887 1.35 rillig initstack_push();
888 1.1 cgd istk = initstk;
889 1.1 cgd } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
890 1.68 rillig debug_step("type array");
891 1.1 cgd t = istk->i_type->t_subt->t_tspec;
892 1.1 cgd if (!((strg->st_tspec == CHAR &&
893 1.1 cgd (t == CHAR || t == UCHAR || t == SCHAR)) ||
894 1.1 cgd (strg->st_tspec == WCHAR && t == WCHAR))) {
895 1.68 rillig debug_leave();
896 1.63 rillig return false;
897 1.1 cgd }
898 1.1 cgd /*
899 1.1 cgd * If the array is already partly initialized, we are
900 1.1 cgd * wrong here.
901 1.1 cgd */
902 1.43 rillig if (istk->i_remaining != istk->i_type->t_dim)
903 1.68 rillig debug_leave();
904 1.63 rillig return false;
905 1.1 cgd } else {
906 1.68 rillig debug_leave();
907 1.63 rillig return false;
908 1.1 cgd }
909 1.1 cgd
910 1.1 cgd /* Get length without trailing NUL character. */
911 1.1 cgd len = strg->st_len;
912 1.1 cgd
913 1.77 rillig if (istk->i_array_of_unknown_size) {
914 1.77 rillig istk->i_array_of_unknown_size = false;
915 1.1 cgd istk->i_type->t_dim = len + 1;
916 1.63 rillig setcomplete(istk->i_type, true);
917 1.1 cgd } else {
918 1.1 cgd if (istk->i_type->t_dim < len) {
919 1.1 cgd /* non-null byte ignored in string initializer */
920 1.1 cgd warning(187);
921 1.1 cgd }
922 1.1 cgd }
923 1.1 cgd
924 1.1 cgd /* In every case the array is initialized completely. */
925 1.43 rillig istk->i_remaining = 0;
926 1.1 cgd
927 1.73 rillig debug_initstack();
928 1.68 rillig debug_leave();
929 1.63 rillig return true;
930 1.1 cgd }
931