init.c revision 1.86 1 1.86 rillig /* $NetBSD: init.c,v 1.86 2021/02/21 14:57:25 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.86 rillig __RCSID("$NetBSD: init.c,v 1.86 2021/02/21 14:57:25 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.1 cgd /*
470 1.1 cgd * Inside of other aggregate types must not be an incomplete
471 1.1 cgd * type.
472 1.1 cgd */
473 1.77 rillig lint_assert(istk->i_enclosing->i_enclosing == NULL);
474 1.85 rillig lint_assert(istk->i_type->t_tspec == ARRAY);
475 1.85 rillig debug_step("extending array of unknown size '%s'",
476 1.85 rillig type_name(istk->i_type));
477 1.43 rillig istk->i_remaining = 1;
478 1.1 cgd istk->i_type->t_dim++;
479 1.63 rillig setcomplete(istk->i_type, true);
480 1.85 rillig debug_step("extended type is '%s'", type_name(istk->i_type));
481 1.1 cgd }
482 1.1 cgd
483 1.44 rillig lint_assert(istk->i_remaining > 0);
484 1.61 rillig lint_assert(istk->i_type == NULL || !is_scalar(istk->i_type->t_tspec));
485 1.1 cgd
486 1.79 rillig initstk = xcalloc(1, sizeof (initstack_element));
487 1.77 rillig initstk->i_enclosing = istk;
488 1.1 cgd initstk->i_type = istk->i_subt;
489 1.44 rillig lint_assert(initstk->i_type->t_tspec != FUNC);
490 1.1 cgd
491 1.12 christos again:
492 1.1 cgd istk = initstk;
493 1.1 cgd
494 1.68 rillig debug_step("typename %s", type_name(istk->i_type));
495 1.1 cgd switch (istk->i_type->t_tspec) {
496 1.1 cgd case ARRAY:
497 1.62 rillig if (namedmem != NULL) {
498 1.68 rillig debug_step("ARRAY %s brace=%d",
499 1.68 rillig namedmem->n_name, istk->i_brace);
500 1.19 christos goto pop;
501 1.77 rillig } else if (istk->i_enclosing->i_seen_named_member) {
502 1.62 rillig istk->i_brace = true;
503 1.68 rillig debug_step("ARRAY brace=%d, namedmem=%d",
504 1.77 rillig istk->i_brace,
505 1.77 rillig istk->i_enclosing->i_seen_named_member);
506 1.20 christos }
507 1.20 christos
508 1.65 rillig if (is_incomplete(istk->i_type) &&
509 1.77 rillig istk->i_enclosing->i_enclosing != NULL) {
510 1.1 cgd /* initialisation of an incomplete type */
511 1.1 cgd error(175);
512 1.62 rillig initerr = true;
513 1.73 rillig debug_initstack();
514 1.68 rillig debug_leave();
515 1.1 cgd return;
516 1.1 cgd }
517 1.1 cgd istk->i_subt = istk->i_type->t_subt;
518 1.77 rillig istk->i_array_of_unknown_size = is_incomplete(istk->i_type);
519 1.43 rillig istk->i_remaining = istk->i_type->t_dim;
520 1.68 rillig debug_step("elements array %s[%d] %s",
521 1.57 rillig type_name(istk->i_subt), istk->i_remaining,
522 1.74 rillig namedmem != NULL ? namedmem->n_name : "*none*");
523 1.1 cgd break;
524 1.1 cgd case UNION:
525 1.1 cgd if (tflag)
526 1.1 cgd /* initialisation of union is illegal in trad. C */
527 1.1 cgd warning(238);
528 1.1 cgd /* FALLTHROUGH */
529 1.1 cgd case STRUCT:
530 1.65 rillig if (is_incomplete(istk->i_type)) {
531 1.1 cgd /* initialisation of an incomplete type */
532 1.1 cgd error(175);
533 1.62 rillig initerr = true;
534 1.73 rillig debug_initstack();
535 1.68 rillig debug_leave();
536 1.1 cgd return;
537 1.1 cgd }
538 1.1 cgd cnt = 0;
539 1.68 rillig debug_step("lookup type=%s, name=%s named=%d",
540 1.57 rillig type_name(istk->i_type),
541 1.74 rillig namedmem != NULL ? namedmem->n_name : "*none*",
542 1.77 rillig istk->i_seen_named_member);
543 1.67 rillig for (m = istk->i_type->t_str->sou_first_member;
544 1.67 rillig m != NULL; m = m->s_next) {
545 1.48 rillig if (m->s_bitfield && m->s_name == unnamed)
546 1.1 cgd continue;
547 1.12 christos if (namedmem != NULL) {
548 1.68 rillig debug_step("named lhs.member=%s, rhs.member=%s",
549 1.68 rillig m->s_name, namedmem->n_name);
550 1.12 christos if (strcmp(m->s_name, namedmem->n_name) == 0) {
551 1.12 christos cnt++;
552 1.12 christos break;
553 1.12 christos } else
554 1.12 christos continue;
555 1.12 christos }
556 1.1 cgd if (++cnt == 1) {
557 1.77 rillig istk->i_current_object = m;
558 1.1 cgd istk->i_subt = m->s_type;
559 1.1 cgd }
560 1.1 cgd }
561 1.12 christos if (namedmem != NULL) {
562 1.12 christos if (m == NULL) {
563 1.68 rillig debug_step("pop struct");
564 1.19 christos goto pop;
565 1.28 rillig }
566 1.77 rillig istk->i_current_object = m;
567 1.26 christos istk->i_subt = m->s_type;
568 1.77 rillig istk->i_seen_named_member = true;
569 1.68 rillig debug_step("named name=%s", namedmem->n_name);
570 1.34 rillig pop_member();
571 1.12 christos cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
572 1.12 christos }
573 1.62 rillig istk->i_brace = true;
574 1.68 rillig debug_step("unnamed type=%s, brace=%d",
575 1.74 rillig type_name(
576 1.74 rillig istk->i_type != NULL ? istk->i_type : istk->i_subt),
577 1.68 rillig istk->i_brace);
578 1.1 cgd if (cnt == 0) {
579 1.1 cgd /* cannot init. struct/union with no named member */
580 1.1 cgd error(179);
581 1.62 rillig initerr = true;
582 1.73 rillig debug_initstack();
583 1.68 rillig debug_leave();
584 1.1 cgd return;
585 1.1 cgd }
586 1.43 rillig istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
587 1.1 cgd break;
588 1.1 cgd default:
589 1.62 rillig if (namedmem != NULL) {
590 1.68 rillig debug_step("pop");
591 1.19 christos pop:
592 1.77 rillig inxt = initstk->i_enclosing;
593 1.12 christos free(istk);
594 1.22 ad initstk = inxt;
595 1.12 christos goto again;
596 1.12 christos }
597 1.43 rillig istk->i_remaining = 1;
598 1.1 cgd break;
599 1.1 cgd }
600 1.68 rillig
601 1.73 rillig debug_initstack();
602 1.68 rillig debug_leave();
603 1.1 cgd }
604 1.1 cgd
605 1.1 cgd static void
606 1.84 rillig check_too_many_initializers(void)
607 1.1 cgd {
608 1.1 cgd
609 1.84 rillig const initstack_element *istk = initstk;
610 1.84 rillig if (istk->i_remaining > 0)
611 1.84 rillig return;
612 1.84 rillig if (istk->i_array_of_unknown_size || istk->i_seen_named_member)
613 1.84 rillig return;
614 1.1 cgd
615 1.84 rillig tspec_t t = istk->i_type->t_tspec;
616 1.84 rillig if (t == ARRAY) {
617 1.84 rillig /* too many array initializers, expected %d */
618 1.84 rillig error(173, istk->i_type->t_dim);
619 1.84 rillig } else if (t == STRUCT || t == UNION) {
620 1.84 rillig /* too many struct/union initializers */
621 1.84 rillig error(172);
622 1.84 rillig } else {
623 1.84 rillig /* too many initializers */
624 1.84 rillig error(174);
625 1.1 cgd }
626 1.84 rillig initerr = true;
627 1.1 cgd }
628 1.1 cgd
629 1.1 cgd static void
630 1.37 rillig initstack_next_brace(void)
631 1.1 cgd {
632 1.7 lukem
633 1.68 rillig debug_enter();
634 1.73 rillig debug_initstack();
635 1.68 rillig
636 1.61 rillig if (initstk->i_type != NULL && is_scalar(initstk->i_type->t_tspec)) {
637 1.49 rillig /* invalid initializer type %s */
638 1.57 rillig error(176, type_name(initstk->i_type));
639 1.62 rillig initerr = true;
640 1.37 rillig }
641 1.37 rillig if (!initerr)
642 1.84 rillig check_too_many_initializers();
643 1.37 rillig if (!initerr)
644 1.37 rillig initstack_push();
645 1.37 rillig if (!initerr) {
646 1.62 rillig initstk->i_brace = true;
647 1.68 rillig debug_step("%p %s", namedmem, type_name(
648 1.74 rillig initstk->i_type != NULL ? initstk->i_type
649 1.74 rillig : initstk->i_subt));
650 1.37 rillig }
651 1.68 rillig
652 1.73 rillig debug_initstack();
653 1.68 rillig debug_leave();
654 1.37 rillig }
655 1.37 rillig
656 1.37 rillig static void
657 1.37 rillig initstack_next_nobrace(void)
658 1.37 rillig {
659 1.68 rillig debug_enter();
660 1.73 rillig debug_initstack();
661 1.37 rillig
662 1.61 rillig if (initstk->i_type == NULL && !is_scalar(initstk->i_subt->t_tspec)) {
663 1.37 rillig /* {}-enclosed initializer required */
664 1.37 rillig error(181);
665 1.37 rillig }
666 1.37 rillig
667 1.84 rillig if (!initerr)
668 1.84 rillig check_too_many_initializers();
669 1.84 rillig
670 1.71 rillig /*
671 1.71 rillig * Make sure an entry with a scalar type is at the top of the stack.
672 1.71 rillig *
673 1.71 rillig * FIXME: Since C99 an initializer for an object with automatic
674 1.71 rillig * storage need not be a constant expression anymore. It is
675 1.71 rillig * perfectly fine to initialize a struct with a struct expression,
676 1.71 rillig * see d_struct_init_nested.c for a demonstration.
677 1.71 rillig */
678 1.42 rillig while (!initerr) {
679 1.42 rillig if ((initstk->i_type != NULL &&
680 1.61 rillig is_scalar(initstk->i_type->t_tspec)))
681 1.42 rillig break;
682 1.42 rillig initstack_push();
683 1.1 cgd }
684 1.68 rillig
685 1.73 rillig debug_initstack();
686 1.68 rillig debug_leave();
687 1.1 cgd }
688 1.1 cgd
689 1.1 cgd void
690 1.34 rillig init_lbrace(void)
691 1.1 cgd {
692 1.1 cgd if (initerr)
693 1.1 cgd return;
694 1.1 cgd
695 1.68 rillig debug_enter();
696 1.73 rillig debug_initstack();
697 1.68 rillig
698 1.1 cgd if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
699 1.77 rillig initstk->i_enclosing == NULL) {
700 1.61 rillig if (tflag && !is_scalar(initstk->i_subt->t_tspec))
701 1.50 rillig /* no automatic aggregate initialization in trad. C */
702 1.1 cgd warning(188);
703 1.1 cgd }
704 1.1 cgd
705 1.1 cgd /*
706 1.1 cgd * Remove all entries which cannot be used for further initializers
707 1.1 cgd * and do not expect a closing brace.
708 1.1 cgd */
709 1.36 rillig initstack_pop_nobrace();
710 1.1 cgd
711 1.37 rillig initstack_next_brace();
712 1.68 rillig
713 1.73 rillig debug_initstack();
714 1.68 rillig debug_leave();
715 1.1 cgd }
716 1.1 cgd
717 1.1 cgd void
718 1.34 rillig init_rbrace(void)
719 1.1 cgd {
720 1.1 cgd if (initerr)
721 1.1 cgd return;
722 1.1 cgd
723 1.68 rillig debug_enter();
724 1.73 rillig debug_initstack();
725 1.68 rillig
726 1.36 rillig initstack_pop_brace();
727 1.68 rillig
728 1.73 rillig debug_initstack();
729 1.68 rillig debug_leave();
730 1.1 cgd }
731 1.1 cgd
732 1.86 rillig /* In traditional C, bit-fields can be initialized only by integer constants. */
733 1.86 rillig static void
734 1.86 rillig check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
735 1.86 rillig {
736 1.86 rillig if (tflag &&
737 1.86 rillig is_integer(lt) &&
738 1.86 rillig ln->tn_type->t_bitfield &&
739 1.86 rillig !is_integer(rt)) {
740 1.86 rillig /* bit-field initialisation is illegal in traditional C */
741 1.86 rillig warning(186);
742 1.86 rillig }
743 1.86 rillig }
744 1.86 rillig
745 1.1 cgd void
746 1.69 rillig init_using_expr(tnode_t *tn)
747 1.1 cgd {
748 1.1 cgd tspec_t lt, rt;
749 1.1 cgd tnode_t *ln;
750 1.1 cgd struct mbl *tmem;
751 1.81 rillig scl_t sclass;
752 1.1 cgd
753 1.68 rillig debug_enter();
754 1.68 rillig debug_named_member();
755 1.76 rillig debug_node(tn, debug_ind);
756 1.73 rillig debug_initstack();
757 1.55 rillig
758 1.68 rillig if (initerr || tn == NULL) {
759 1.68 rillig debug_leave();
760 1.25 christos return;
761 1.68 rillig }
762 1.1 cgd
763 1.81 rillig sclass = initsym->s_scl;
764 1.1 cgd
765 1.1 cgd /*
766 1.13 christos * Do not test for automatic aggregate initialisation. If the
767 1.9 wiz * initializer starts with a brace we have the warning already.
768 1.1 cgd * If not, an error will be printed that the initializer must
769 1.1 cgd * be enclosed by braces.
770 1.1 cgd */
771 1.1 cgd
772 1.1 cgd /*
773 1.1 cgd * Local initialisation of non-array-types with only one expression
774 1.1 cgd * without braces is done by ASSIGN
775 1.1 cgd */
776 1.81 rillig if ((sclass == AUTO || sclass == REG) &&
777 1.77 rillig initsym->s_type->t_tspec != ARRAY && initstk->i_enclosing == NULL) {
778 1.82 rillig debug_step("handing over to ASSIGN");
779 1.60 rillig ln = new_name_node(initsym, 0);
780 1.1 cgd ln->tn_type = tduptyp(ln->tn_type);
781 1.62 rillig ln->tn_type->t_const = false;
782 1.1 cgd tn = build(ASSIGN, ln, tn);
783 1.66 rillig expr(tn, false, false, false, false);
784 1.82 rillig /* XXX: why not clean up the initstack here already? */
785 1.68 rillig debug_leave();
786 1.25 christos return;
787 1.1 cgd }
788 1.1 cgd
789 1.36 rillig initstack_pop_nobrace();
790 1.1 cgd
791 1.82 rillig if (init_array_using_string(tn)) {
792 1.82 rillig debug_step("after initializing the string:");
793 1.82 rillig /* XXX: why not clean up the initstack here already? */
794 1.73 rillig debug_initstack();
795 1.68 rillig debug_leave();
796 1.25 christos return;
797 1.68 rillig }
798 1.1 cgd
799 1.37 rillig initstack_next_nobrace();
800 1.68 rillig if (initerr || tn == NULL) {
801 1.73 rillig debug_initstack();
802 1.68 rillig debug_leave();
803 1.25 christos return;
804 1.68 rillig }
805 1.1 cgd
806 1.43 rillig initstk->i_remaining--;
807 1.83 rillig debug_step("%d elements remaining", initstk->i_remaining);
808 1.83 rillig
809 1.1 cgd /* Create a temporary node for the left side. */
810 1.1 cgd ln = tgetblk(sizeof (tnode_t));
811 1.1 cgd ln->tn_op = NAME;
812 1.1 cgd ln->tn_type = tduptyp(initstk->i_type);
813 1.62 rillig ln->tn_type->t_const = false;
814 1.62 rillig ln->tn_lvalue = true;
815 1.1 cgd ln->tn_sym = initsym; /* better than nothing */
816 1.1 cgd
817 1.1 cgd tn = cconv(tn);
818 1.1 cgd
819 1.1 cgd lt = ln->tn_type->t_tspec;
820 1.1 cgd rt = tn->tn_type->t_tspec;
821 1.1 cgd
822 1.61 rillig lint_assert(is_scalar(lt));
823 1.1 cgd
824 1.68 rillig if (!typeok(INIT, 0, ln, tn)) {
825 1.73 rillig debug_initstack();
826 1.68 rillig debug_leave();
827 1.25 christos return;
828 1.68 rillig }
829 1.1 cgd
830 1.1 cgd /*
831 1.38 rillig * Store the tree memory. This is necessary because otherwise
832 1.1 cgd * expr() would free it.
833 1.1 cgd */
834 1.1 cgd tmem = tsave();
835 1.66 rillig expr(tn, true, false, true, false);
836 1.1 cgd trestor(tmem);
837 1.7 lukem
838 1.86 rillig check_bit_field_init(ln, lt, rt);
839 1.1 cgd
840 1.59 rillig if (lt != rt || (initstk->i_type->t_bitfield && tn->tn_op == CON))
841 1.1 cgd tn = convert(INIT, 0, initstk->i_type, tn);
842 1.1 cgd
843 1.1 cgd if (tn != NULL && tn->tn_op != CON) {
844 1.81 rillig sym_t *sym;
845 1.81 rillig ptrdiff_t offs;
846 1.64 rillig if (!constant_addr(tn, &sym, &offs)) {
847 1.81 rillig if (sclass == AUTO || sclass == REG) {
848 1.1 cgd /* non-constant initializer */
849 1.58 rillig c99ism(177);
850 1.1 cgd } else {
851 1.1 cgd /* non-constant initializer */
852 1.1 cgd error(177);
853 1.1 cgd }
854 1.1 cgd }
855 1.1 cgd }
856 1.68 rillig
857 1.73 rillig debug_initstack();
858 1.68 rillig debug_leave();
859 1.1 cgd }
860 1.1 cgd
861 1.1 cgd
862 1.82 rillig /* Initialize a character array or wchar_t array with a string literal. */
863 1.62 rillig static bool
864 1.82 rillig init_array_using_string(tnode_t *tn)
865 1.1 cgd {
866 1.1 cgd tspec_t t;
867 1.79 rillig initstack_element *istk;
868 1.1 cgd int len;
869 1.1 cgd strg_t *strg;
870 1.1 cgd
871 1.1 cgd if (tn->tn_op != STRING)
872 1.63 rillig return false;
873 1.1 cgd
874 1.68 rillig debug_enter();
875 1.73 rillig debug_initstack();
876 1.68 rillig
877 1.1 cgd istk = initstk;
878 1.47 rillig strg = tn->tn_string;
879 1.1 cgd
880 1.1 cgd /*
881 1.1 cgd * Check if we have an array type which can be initialized by
882 1.1 cgd * the string.
883 1.1 cgd */
884 1.12 christos if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
885 1.68 rillig debug_step("subt array");
886 1.1 cgd t = istk->i_subt->t_subt->t_tspec;
887 1.1 cgd if (!((strg->st_tspec == CHAR &&
888 1.1 cgd (t == CHAR || t == UCHAR || t == SCHAR)) ||
889 1.1 cgd (strg->st_tspec == WCHAR && t == WCHAR))) {
890 1.68 rillig debug_leave();
891 1.63 rillig return false;
892 1.1 cgd }
893 1.82 rillig /* XXX: duplicate code, see below */
894 1.1 cgd /* Put the array at top of stack */
895 1.35 rillig initstack_push();
896 1.1 cgd istk = initstk;
897 1.1 cgd } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
898 1.68 rillig debug_step("type array");
899 1.1 cgd t = istk->i_type->t_subt->t_tspec;
900 1.1 cgd if (!((strg->st_tspec == CHAR &&
901 1.1 cgd (t == CHAR || t == UCHAR || t == SCHAR)) ||
902 1.1 cgd (strg->st_tspec == WCHAR && t == WCHAR))) {
903 1.68 rillig debug_leave();
904 1.63 rillig return false;
905 1.1 cgd }
906 1.82 rillig /* XXX: duplicate code, see above */
907 1.1 cgd /*
908 1.1 cgd * If the array is already partly initialized, we are
909 1.1 cgd * wrong here.
910 1.1 cgd */
911 1.43 rillig if (istk->i_remaining != istk->i_type->t_dim)
912 1.68 rillig debug_leave();
913 1.63 rillig return false;
914 1.1 cgd } else {
915 1.68 rillig debug_leave();
916 1.63 rillig return false;
917 1.1 cgd }
918 1.1 cgd
919 1.1 cgd /* Get length without trailing NUL character. */
920 1.1 cgd len = strg->st_len;
921 1.1 cgd
922 1.77 rillig if (istk->i_array_of_unknown_size) {
923 1.77 rillig istk->i_array_of_unknown_size = false;
924 1.1 cgd istk->i_type->t_dim = len + 1;
925 1.63 rillig setcomplete(istk->i_type, true);
926 1.1 cgd } else {
927 1.1 cgd if (istk->i_type->t_dim < len) {
928 1.1 cgd /* non-null byte ignored in string initializer */
929 1.1 cgd warning(187);
930 1.1 cgd }
931 1.1 cgd }
932 1.1 cgd
933 1.1 cgd /* In every case the array is initialized completely. */
934 1.43 rillig istk->i_remaining = 0;
935 1.1 cgd
936 1.73 rillig debug_initstack();
937 1.68 rillig debug_leave();
938 1.63 rillig return true;
939 1.1 cgd }
940