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