init.c revision 1.127 1 1.127 rillig /* $NetBSD: init.c,v 1.127 2021/03/25 20:38:16 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.127 rillig __RCSID("$NetBSD: init.c,v 1.127 2021/03/25 20:38:16 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.93 rillig * Initialization
51 1.93 rillig *
52 1.93 rillig * Handles initializations of global or local objects, like in:
53 1.93 rillig *
54 1.93 rillig * int number = 12345;
55 1.93 rillig * int number_with_braces = { 12345 };
56 1.93 rillig *
57 1.93 rillig * int array_of_unknown_size[] = { 111, 222, 333 };
58 1.93 rillig * int array_flat[2][2] = { 11, 12, 21, 22 };
59 1.93 rillig * int array_nested[2][2] = { { 11, 12 }, { 21, 22 } };
60 1.93 rillig *
61 1.93 rillig * struct { int x, y; } point = { 3, 4 };
62 1.119 rillig * struct { int x, y; } point = { .y = 4, .x = 3 };
63 1.93 rillig *
64 1.119 rillig * Any scalar expression in the initializer may be surrounded by arbitrarily
65 1.119 rillig * many extra pairs of braces, like in the example 'number_with_braces' (C99
66 1.119 rillig * 6.7.8p11).
67 1.119 rillig *
68 1.119 rillig * For multi-dimensional arrays, the inner braces may be omitted like in
69 1.112 rillig * array_flat or spelled out like in array_nested.
70 1.93 rillig *
71 1.93 rillig * For the initializer, the grammar parser calls these functions:
72 1.93 rillig *
73 1.119 rillig * begin_initialization
74 1.119 rillig * init_lbrace for each '{'
75 1.119 rillig * designator_push_name for each '.member' before '='
76 1.119 rillig * designator_push_subscript for each '[123]' before '='
77 1.119 rillig * init_using_expr for each expression
78 1.119 rillig * init_rbrace for each '}'
79 1.119 rillig * end_initialization
80 1.93 rillig *
81 1.93 rillig * The state of the current initialization is stored in initstk, a stack of
82 1.94 rillig * initstack_element, one element per type aggregate level.
83 1.119 rillig * XXX: Or is that "one element per brace level"? C99 mandates in 6.7.8p17
84 1.119 rillig * that "each brace-enclosed initializer list has an associated current
85 1.119 rillig * object".
86 1.93 rillig *
87 1.94 rillig * Most of the time, the topmost level of initstk contains a scalar type, and
88 1.94 rillig * its remaining count toggles between 1 and 0.
89 1.93 rillig *
90 1.93 rillig * See also:
91 1.93 rillig * C99 6.7.8 "Initialization"
92 1.93 rillig * d_c99_init.c for more examples
93 1.93 rillig */
94 1.93 rillig
95 1.93 rillig
96 1.93 rillig /*
97 1.89 rillig * Type of stack which is used for initialization of aggregate types.
98 1.54 rillig *
99 1.119 rillig * XXX: Since C99, the initializers can be listed in arbitrary order by using
100 1.119 rillig * designators to specify the sub-object to be initialized. The member names
101 1.54 rillig * of non-leaf structs may thus appear repeatedly, as demonstrated in
102 1.54 rillig * d_init_pop_member.c.
103 1.54 rillig *
104 1.54 rillig * See C99 6.7.8, which spans 6 pages full of tricky details and carefully
105 1.54 rillig * selected examples.
106 1.53 rillig */
107 1.79 rillig typedef struct initstack_element {
108 1.77 rillig
109 1.92 rillig /*
110 1.92 rillig * The type to be initialized at this level.
111 1.102 rillig *
112 1.102 rillig * On the outermost element, this is always NULL since the outermost
113 1.102 rillig * initializer-expression may be enclosed in an optional pair of
114 1.119 rillig * braces, as of the current implementation.
115 1.119 rillig *
116 1.119 rillig * FIXME: This approach is wrong. It's not that the outermost
117 1.119 rillig * initializer may be enclosed in additional braces, it's every scalar
118 1.119 rillig * that may be enclosed in additional braces, as of C99 6.7.8p11.
119 1.102 rillig *
120 1.102 rillig * Everywhere else it is nonnull.
121 1.92 rillig */
122 1.92 rillig type_t *i_type;
123 1.102 rillig
124 1.92 rillig /*
125 1.102 rillig * The type that will be initialized at the next initialization level,
126 1.102 rillig * usually enclosed by another pair of braces.
127 1.92 rillig *
128 1.102 rillig * For an array, it is the element type, but without 'const'.
129 1.102 rillig *
130 1.102 rillig * For a struct or union type, it is one of the member types, but
131 1.102 rillig * without 'const'.
132 1.102 rillig *
133 1.102 rillig * The outermost stack element has no i_type but nevertheless has
134 1.102 rillig * i_subt. For example, in 'int var = { 12345 }', initially there is
135 1.102 rillig * an initstack_element with i_subt 'int'. When the '{' is processed,
136 1.102 rillig * an element with i_type 'int' is pushed to the stack. When the
137 1.92 rillig * corresponding '}' is processed, the inner element is popped again.
138 1.92 rillig *
139 1.92 rillig * During initialization, only the top 2 elements of the stack are
140 1.92 rillig * looked at.
141 1.119 rillig *
142 1.119 rillig * XXX: Having i_subt here is the wrong approach, it should not be
143 1.119 rillig * necessary at all; see i_type.
144 1.92 rillig */
145 1.92 rillig type_t *i_subt;
146 1.77 rillig
147 1.82 rillig /*
148 1.119 rillig * Whether this level of the initializer requires a '}' to be
149 1.119 rillig * completed.
150 1.82 rillig *
151 1.82 rillig * Multidimensional arrays do not need a closing brace to complete
152 1.82 rillig * an inner array; for example, { 1, 2, 3, 4 } is a valid initializer
153 1.119 rillig * for 'int arr[2][2]'.
154 1.82 rillig *
155 1.82 rillig * TODO: Do structs containing structs need a closing brace?
156 1.82 rillig * TODO: Do arrays of structs need a closing brace after each struct?
157 1.119 rillig *
158 1.119 rillig * XXX: Double-check whether this is the correct approach at all; see
159 1.119 rillig * i_type.
160 1.82 rillig */
161 1.77 rillig bool i_brace: 1;
162 1.82 rillig
163 1.93 rillig /* Whether i_type is an array of unknown size. */
164 1.77 rillig bool i_array_of_unknown_size: 1;
165 1.119 rillig
166 1.119 rillig /*
167 1.119 rillig * XXX: This feels wrong. Whether or not there has been a named
168 1.119 rillig * initializer (called 'designation' since C99) should not matter at
169 1.119 rillig * all. Even after an initializer with designation, counting of the
170 1.119 rillig * remaining elements continues, see C99 6.7.8p17.
171 1.119 rillig */
172 1.77 rillig bool i_seen_named_member: 1;
173 1.77 rillig
174 1.77 rillig /*
175 1.125 rillig * For structs, the next member to be initialized by a designator-less
176 1.125 rillig * initializer.
177 1.77 rillig */
178 1.125 rillig sym_t *i_next_member;
179 1.125 rillig
180 1.125 rillig /* TODO: Add i_next_subscript for arrays. */
181 1.125 rillig
182 1.125 rillig /* TODO: Understand C99 6.7.8p17 and footnote 128 for unions. */
183 1.77 rillig
184 1.77 rillig /*
185 1.102 rillig * The number of remaining elements to be used by expressions without
186 1.102 rillig * designator.
187 1.102 rillig *
188 1.102 rillig * This says nothing about which members have been initialized or not
189 1.102 rillig * since starting with C99, members may be initialized in arbitrary
190 1.102 rillig * order by using designators.
191 1.77 rillig *
192 1.93 rillig * For an array of unknown size, this is always 0 and thus irrelevant.
193 1.93 rillig *
194 1.77 rillig * XXX: for scalars?
195 1.77 rillig * XXX: for structs?
196 1.77 rillig * XXX: for unions?
197 1.77 rillig * XXX: for arrays?
198 1.119 rillig *
199 1.119 rillig * XXX: Having the count of remaining objects should not be necessary.
200 1.119 rillig * It is probably clearer to use i_next_member and i_next_subscript
201 1.125 rillig * for this purpose.
202 1.77 rillig */
203 1.77 rillig int i_remaining;
204 1.77 rillig
205 1.77 rillig /*
206 1.77 rillig * The initialization state of the enclosing data structure
207 1.77 rillig * (struct, union, array).
208 1.119 rillig *
209 1.119 rillig * XXX: Or for a scalar, for the top-level element, or for expressions
210 1.119 rillig * in redundant braces such as '{{{{ 0 }}}}' (not yet implemented as
211 1.119 rillig * of 2021-03-25).
212 1.77 rillig */
213 1.79 rillig struct initstack_element *i_enclosing;
214 1.119 rillig
215 1.79 rillig } initstack_element;
216 1.53 rillig
217 1.55 rillig /*
218 1.55 rillig * The names for a nested C99 initialization designator, in a circular list.
219 1.55 rillig *
220 1.55 rillig * Example:
221 1.55 rillig * struct stat st = {
222 1.55 rillig * .st_size = 123,
223 1.55 rillig * .st_mtim.tv_sec = 45,
224 1.55 rillig * .st_mtim.tv_nsec
225 1.55 rillig * };
226 1.55 rillig *
227 1.55 rillig * During initialization, this list first contains ["st_size"], then
228 1.55 rillig * ["st_mtim", "tv_sec"], then ["st_mtim", "tv_nsec"].
229 1.55 rillig */
230 1.53 rillig typedef struct namlist {
231 1.53 rillig const char *n_name;
232 1.53 rillig struct namlist *n_next;
233 1.53 rillig } namlist_t;
234 1.53 rillig
235 1.110 rillig struct initialization {
236 1.110 rillig /*
237 1.111 rillig * is set as soon as a fatal error occurred in the initialization.
238 1.111 rillig * The effect is that the rest of the initialization is ignored
239 1.111 rillig * (parsed by yacc, expression trees built, but no initialization
240 1.111 rillig * takes place).
241 1.110 rillig */
242 1.110 rillig bool initerr;
243 1.110 rillig
244 1.110 rillig /* Pointer to the symbol which is to be initialized. */
245 1.110 rillig sym_t *initsym;
246 1.53 rillig
247 1.110 rillig /* Points to the top element of the initialization stack. */
248 1.110 rillig initstack_element *initstk;
249 1.1 cgd
250 1.124 rillig /*
251 1.124 rillig * The C99 designator, if any, for the current initialization
252 1.124 rillig * expression.
253 1.124 rillig */
254 1.124 rillig namlist_t *designation;
255 1.127 rillig namlist_t *designation_tail;
256 1.1 cgd
257 1.110 rillig struct initialization *next;
258 1.110 rillig };
259 1.1 cgd
260 1.111 rillig static struct initialization *init;
261 1.12 christos
262 1.1 cgd
263 1.119 rillig /* XXX: unnecessary prototype since it is not recursive */
264 1.82 rillig static bool init_array_using_string(tnode_t *);
265 1.12 christos
266 1.119 rillig
267 1.119 rillig /* TODO: replace the following functions with current_init */
268 1.119 rillig
269 1.110 rillig bool *
270 1.110 rillig current_initerr(void)
271 1.110 rillig {
272 1.111 rillig lint_assert(init != NULL);
273 1.111 rillig return &init->initerr;
274 1.110 rillig }
275 1.110 rillig
276 1.110 rillig sym_t **
277 1.110 rillig current_initsym(void)
278 1.110 rillig {
279 1.111 rillig lint_assert(init != NULL);
280 1.111 rillig return &init->initsym;
281 1.110 rillig }
282 1.110 rillig
283 1.110 rillig static namlist_t **
284 1.123 rillig current_designation_mod(void)
285 1.110 rillig {
286 1.111 rillig lint_assert(init != NULL);
287 1.124 rillig return &init->designation;
288 1.110 rillig }
289 1.110 rillig
290 1.123 rillig static const namlist_t *
291 1.123 rillig current_designation(void)
292 1.123 rillig {
293 1.123 rillig return *current_designation_mod();
294 1.123 rillig }
295 1.123 rillig
296 1.110 rillig static initstack_element **
297 1.110 rillig current_initstk(void)
298 1.110 rillig {
299 1.111 rillig lint_assert(init != NULL);
300 1.111 rillig return &init->initstk;
301 1.110 rillig }
302 1.110 rillig
303 1.121 rillig static void
304 1.121 rillig free_initialization(struct initialization *in)
305 1.121 rillig {
306 1.121 rillig initstack_element *el, *next;
307 1.121 rillig
308 1.121 rillig for (el = in->initstk; el != NULL; el = next) {
309 1.121 rillig next = el->i_enclosing;
310 1.121 rillig free(el);
311 1.121 rillig }
312 1.121 rillig
313 1.121 rillig free(in);
314 1.121 rillig }
315 1.121 rillig
316 1.119 rillig #define initerr (*current_initerr())
317 1.119 rillig #define initsym (*current_initsym())
318 1.119 rillig #define initstk (*current_initstk())
319 1.110 rillig
320 1.12 christos #ifndef DEBUG
321 1.90 rillig
322 1.75 rillig #define debug_printf(fmt, ...) do { } while (false)
323 1.75 rillig #define debug_indent() do { } while (false)
324 1.75 rillig #define debug_enter(a) do { } while (false)
325 1.75 rillig #define debug_step(fmt, ...) do { } while (false)
326 1.75 rillig #define debug_leave(a) do { } while (false)
327 1.126 rillig #define debug_designation() do { } while (false)
328 1.90 rillig #define debug_initstack_element(elem) do { } while (false)
329 1.90 rillig #define debug_initstack() do { } while (false)
330 1.90 rillig
331 1.12 christos #else
332 1.90 rillig
333 1.68 rillig static int debug_ind = 0;
334 1.68 rillig
335 1.68 rillig static void __printflike(1, 2)
336 1.68 rillig debug_printf(const char *fmt, ...)
337 1.68 rillig {
338 1.68 rillig va_list va;
339 1.68 rillig
340 1.68 rillig va_start(va, fmt);
341 1.68 rillig vfprintf(stdout, fmt, va);
342 1.68 rillig va_end(va);
343 1.68 rillig }
344 1.68 rillig
345 1.68 rillig static void
346 1.68 rillig debug_indent(void)
347 1.68 rillig {
348 1.68 rillig debug_printf("%*s", 2 * debug_ind, "");
349 1.68 rillig }
350 1.68 rillig
351 1.68 rillig static void
352 1.68 rillig debug_enter(const char *func)
353 1.68 rillig {
354 1.68 rillig printf("%*s+ %s\n", 2 * debug_ind++, "", func);
355 1.68 rillig }
356 1.68 rillig
357 1.68 rillig static void __printflike(1, 2)
358 1.68 rillig debug_step(const char *fmt, ...)
359 1.68 rillig {
360 1.68 rillig va_list va;
361 1.68 rillig
362 1.68 rillig printf("%*s", 2 * debug_ind, "");
363 1.68 rillig va_start(va, fmt);
364 1.68 rillig vfprintf(stdout, fmt, va);
365 1.68 rillig va_end(va);
366 1.68 rillig printf("\n");
367 1.68 rillig }
368 1.68 rillig
369 1.68 rillig static void
370 1.68 rillig debug_leave(const char *func)
371 1.68 rillig {
372 1.68 rillig printf("%*s- %s\n", 2 * --debug_ind, "", func);
373 1.68 rillig }
374 1.68 rillig
375 1.55 rillig static void
376 1.126 rillig debug_designation(void)
377 1.55 rillig {
378 1.123 rillig const namlist_t *head = current_designation(), *name;
379 1.122 rillig if (head == NULL)
380 1.55 rillig return;
381 1.126 rillig
382 1.68 rillig debug_indent();
383 1.126 rillig debug_printf("designation: ");
384 1.122 rillig name = head;
385 1.55 rillig do {
386 1.106 rillig debug_printf(".%s", name->n_name);
387 1.55 rillig name = name->n_next;
388 1.122 rillig } while (name != head);
389 1.68 rillig debug_printf("\n");
390 1.55 rillig }
391 1.1 cgd
392 1.119 rillig /*
393 1.119 rillig * TODO: try whether a single-line output is more readable
394 1.119 rillig *
395 1.119 rillig * TODO: only log the top of the stack after each modifying operation
396 1.119 rillig *
397 1.119 rillig * TODO: wrap all write accesses to initstack_element in setter functions
398 1.119 rillig */
399 1.78 rillig static void
400 1.79 rillig debug_initstack_element(const initstack_element *elem)
401 1.74 rillig {
402 1.78 rillig if (elem->i_type != NULL)
403 1.120 rillig debug_printf("type '%s'", type_name(elem->i_type));
404 1.120 rillig if (elem->i_type != NULL && elem->i_subt != NULL)
405 1.120 rillig debug_printf(", ");
406 1.78 rillig if (elem->i_subt != NULL)
407 1.120 rillig debug_printf("subtype '%s'", type_name(elem->i_subt));
408 1.78 rillig
409 1.78 rillig if (elem->i_brace)
410 1.120 rillig debug_printf(", needs closing brace");
411 1.78 rillig if (elem->i_array_of_unknown_size)
412 1.120 rillig debug_printf(", array of unknown size");
413 1.78 rillig if (elem->i_seen_named_member)
414 1.120 rillig debug_printf(", seen named member");
415 1.78 rillig
416 1.78 rillig const type_t *eff_type = elem->i_type != NULL
417 1.78 rillig ? elem->i_type : elem->i_subt;
418 1.125 rillig if (eff_type->t_tspec == STRUCT && elem->i_next_member != NULL)
419 1.125 rillig debug_printf(", next member '%s'",
420 1.125 rillig elem->i_next_member->s_name);
421 1.78 rillig
422 1.120 rillig debug_printf(", remaining %d\n", elem->i_remaining);
423 1.74 rillig }
424 1.74 rillig
425 1.119 rillig /*
426 1.119 rillig * TODO: only call debug_initstack after each push/pop.
427 1.119 rillig */
428 1.73 rillig static void
429 1.73 rillig debug_initstack(void)
430 1.73 rillig {
431 1.73 rillig if (initstk == NULL) {
432 1.73 rillig debug_step("initstk is empty");
433 1.73 rillig return;
434 1.73 rillig }
435 1.73 rillig
436 1.73 rillig size_t i = 0;
437 1.79 rillig for (const initstack_element *elem = initstk;
438 1.77 rillig elem != NULL; elem = elem->i_enclosing) {
439 1.120 rillig debug_indent();
440 1.120 rillig debug_printf("initstk[%zu]: ", i);
441 1.78 rillig debug_initstack_element(elem);
442 1.73 rillig i++;
443 1.73 rillig }
444 1.73 rillig }
445 1.90 rillig
446 1.90 rillig #define debug_enter() debug_enter(__func__)
447 1.90 rillig #define debug_leave() debug_leave(__func__)
448 1.90 rillig
449 1.73 rillig #endif
450 1.73 rillig
451 1.111 rillig
452 1.111 rillig void
453 1.111 rillig begin_initialization(sym_t *sym)
454 1.111 rillig {
455 1.111 rillig struct initialization *curr_init;
456 1.111 rillig
457 1.118 rillig debug_step("begin initialization of '%s'", type_name(sym->s_type));
458 1.111 rillig curr_init = xcalloc(1, sizeof *curr_init);
459 1.111 rillig curr_init->next = init;
460 1.111 rillig init = curr_init;
461 1.111 rillig initsym = sym;
462 1.111 rillig }
463 1.111 rillig
464 1.111 rillig void
465 1.111 rillig end_initialization(void)
466 1.111 rillig {
467 1.111 rillig struct initialization *curr_init;
468 1.111 rillig
469 1.111 rillig curr_init = init;
470 1.111 rillig init = init->next;
471 1.121 rillig free_initialization(curr_init);
472 1.111 rillig debug_step("end initialization");
473 1.111 rillig }
474 1.111 rillig
475 1.90 rillig void
476 1.103 rillig designator_push_name(sbuf_t *sb)
477 1.90 rillig {
478 1.127 rillig const namlist_t *designation = current_designation();
479 1.127 rillig
480 1.90 rillig namlist_t *nam = xcalloc(1, sizeof (namlist_t));
481 1.90 rillig nam->n_name = sb->sb_name;
482 1.90 rillig
483 1.127 rillig /* TODO: remove direct access to 'init' */
484 1.127 rillig if (designation != NULL) {
485 1.127 rillig init->designation_tail->n_next = nam;
486 1.127 rillig init->designation_tail = nam;
487 1.90 rillig } else {
488 1.127 rillig init->designation = nam;
489 1.127 rillig init->designation_tail = nam;
490 1.90 rillig }
491 1.106 rillig
492 1.126 rillig debug_designation();
493 1.90 rillig }
494 1.90 rillig
495 1.91 rillig /*
496 1.119 rillig * A sub-object of an array is initialized using a designator. This does not
497 1.119 rillig * have to be an array element directly, it can also be used to initialize
498 1.119 rillig * only a sub-object of the array element.
499 1.91 rillig *
500 1.91 rillig * C99 example: struct { int member[4]; } var = { [2] = 12345 };
501 1.91 rillig *
502 1.91 rillig * GNU example: struct { int member[4]; } var = { [1 ... 3] = 12345 };
503 1.119 rillig *
504 1.119 rillig * TODO: test the following initialization with an outer and an inner type:
505 1.119 rillig *
506 1.119 rillig * .deeply[0].nested = {
507 1.119 rillig * .deeply[1].nested = {
508 1.119 rillig * 12345,
509 1.119 rillig * },
510 1.119 rillig * }
511 1.91 rillig */
512 1.91 rillig void
513 1.91 rillig designator_push_subscript(range_t range)
514 1.91 rillig {
515 1.91 rillig debug_enter();
516 1.91 rillig debug_step("subscript range is %zu ... %zu", range.lo, range.hi);
517 1.91 rillig debug_initstack();
518 1.91 rillig debug_leave();
519 1.91 rillig }
520 1.91 rillig
521 1.119 rillig /* TODO: add support for array subscripts, not only named members */
522 1.90 rillig static void
523 1.105 rillig designator_shift_name(void)
524 1.90 rillig {
525 1.127 rillig /* TODO: remove direct access to 'init' */
526 1.127 rillig lint_assert(init != NULL);
527 1.127 rillig if (init->designation == init->designation_tail) {
528 1.127 rillig free(init->designation);
529 1.127 rillig init->designation = NULL;
530 1.127 rillig init->designation_tail = NULL;
531 1.90 rillig } else {
532 1.127 rillig namlist_t *head = init->designation;
533 1.127 rillig init->designation = init->designation->n_next;
534 1.126 rillig free(head);
535 1.90 rillig }
536 1.106 rillig
537 1.126 rillig debug_designation();
538 1.90 rillig }
539 1.90 rillig
540 1.1 cgd /*
541 1.89 rillig * Initialize the initialization stack by putting an entry for the object
542 1.1 cgd * which is to be initialized on it.
543 1.119 rillig *
544 1.119 rillig * TODO: merge into begin_initialization
545 1.1 cgd */
546 1.1 cgd void
547 1.35 rillig initstack_init(void)
548 1.1 cgd {
549 1.79 rillig initstack_element *istk;
550 1.1 cgd
551 1.1 cgd if (initerr)
552 1.1 cgd return;
553 1.1 cgd
554 1.119 rillig /* TODO: merge into init_using_expr */
555 1.123 rillig while (current_designation() != NULL)
556 1.108 rillig designator_shift_name();
557 1.1 cgd
558 1.70 rillig debug_enter();
559 1.38 rillig
560 1.1 cgd /*
561 1.77 rillig * If the type which is to be initialized is an incomplete array,
562 1.1 cgd * it must be duplicated.
563 1.1 cgd */
564 1.65 rillig if (initsym->s_type->t_tspec == ARRAY && is_incomplete(initsym->s_type))
565 1.1 cgd initsym->s_type = duptyp(initsym->s_type);
566 1.119 rillig /* TODO: does 'duptyp' create a memory leak? */
567 1.1 cgd
568 1.79 rillig istk = initstk = xcalloc(1, sizeof (initstack_element));
569 1.1 cgd istk->i_subt = initsym->s_type;
570 1.43 rillig istk->i_remaining = 1;
571 1.70 rillig
572 1.73 rillig debug_initstack();
573 1.70 rillig debug_leave();
574 1.1 cgd }
575 1.1 cgd
576 1.119 rillig /* TODO: document me */
577 1.1 cgd static void
578 1.101 rillig initstack_pop_item_named_member(void)
579 1.1 cgd {
580 1.101 rillig initstack_element *istk = initstk;
581 1.101 rillig sym_t *m;
582 1.1 cgd
583 1.119 rillig /*
584 1.119 rillig * TODO: fix wording of the debug message; this doesn't seem to be
585 1.119 rillig * related to initializing the named member.
586 1.119 rillig */
587 1.122 rillig debug_step("initializing named member '%s'",
588 1.123 rillig current_designation()->n_name);
589 1.68 rillig
590 1.104 rillig if (istk->i_type->t_tspec != STRUCT &&
591 1.104 rillig istk->i_type->t_tspec != UNION) {
592 1.104 rillig /* syntax error '%s' */
593 1.104 rillig error(249, "named member must only be used with struct/union");
594 1.104 rillig initerr = true;
595 1.104 rillig return;
596 1.104 rillig }
597 1.104 rillig
598 1.101 rillig for (m = istk->i_type->t_str->sou_first_member;
599 1.101 rillig m != NULL; m = m->s_next) {
600 1.40 rillig
601 1.101 rillig if (m->s_bitfield && m->s_name == unnamed)
602 1.101 rillig continue;
603 1.26 christos
604 1.123 rillig if (strcmp(m->s_name, current_designation()->n_name) == 0) {
605 1.101 rillig debug_step("found matching member");
606 1.101 rillig istk->i_subt = m->s_type;
607 1.101 rillig /* XXX: why ++? */
608 1.101 rillig istk->i_remaining++;
609 1.101 rillig /* XXX: why is i_seen_named_member not set? */
610 1.105 rillig designator_shift_name();
611 1.101 rillig return;
612 1.101 rillig }
613 1.101 rillig }
614 1.40 rillig
615 1.101 rillig /* undefined struct/union member: %s */
616 1.123 rillig error(101, current_designation()->n_name);
617 1.38 rillig
618 1.105 rillig designator_shift_name();
619 1.101 rillig istk->i_seen_named_member = true;
620 1.101 rillig }
621 1.80 rillig
622 1.119 rillig /* TODO: think of a better name than 'pop' */
623 1.101 rillig static void
624 1.101 rillig initstack_pop_item_unnamed(void)
625 1.101 rillig {
626 1.101 rillig initstack_element *istk = initstk;
627 1.101 rillig sym_t *m;
628 1.80 rillig
629 1.1 cgd /*
630 1.1 cgd * If the removed element was a structure member, we must go
631 1.1 cgd * to the next structure member.
632 1.1 cgd */
633 1.43 rillig if (istk->i_remaining > 0 && istk->i_type->t_tspec == STRUCT &&
634 1.77 rillig !istk->i_seen_named_member) {
635 1.1 cgd do {
636 1.125 rillig m = istk->i_next_member =
637 1.125 rillig istk->i_next_member->s_next;
638 1.80 rillig /* XXX: can this assertion be made to fail? */
639 1.44 rillig lint_assert(m != NULL);
640 1.68 rillig debug_step("pop %s", m->s_name);
641 1.48 rillig } while (m->s_bitfield && m->s_name == unnamed);
642 1.80 rillig /* XXX: duplicate code for skipping unnamed bit-fields */
643 1.1 cgd istk->i_subt = m->s_type;
644 1.1 cgd }
645 1.101 rillig }
646 1.101 rillig
647 1.119 rillig /* TODO: think of a better name than 'pop' */
648 1.101 rillig static void
649 1.101 rillig initstack_pop_item(void)
650 1.101 rillig {
651 1.101 rillig initstack_element *istk;
652 1.101 rillig
653 1.101 rillig debug_enter();
654 1.101 rillig
655 1.101 rillig istk = initstk;
656 1.120 rillig debug_indent();
657 1.120 rillig debug_printf("popping: ");
658 1.101 rillig debug_initstack_element(istk);
659 1.101 rillig
660 1.101 rillig initstk = istk->i_enclosing;
661 1.101 rillig free(istk);
662 1.101 rillig istk = initstk;
663 1.101 rillig lint_assert(istk != NULL);
664 1.101 rillig
665 1.101 rillig istk->i_remaining--;
666 1.101 rillig lint_assert(istk->i_remaining >= 0);
667 1.101 rillig debug_step("%d elements remaining", istk->i_remaining);
668 1.101 rillig
669 1.123 rillig if (current_designation() != NULL)
670 1.101 rillig initstack_pop_item_named_member();
671 1.101 rillig else
672 1.101 rillig initstack_pop_item_unnamed();
673 1.101 rillig
674 1.73 rillig debug_initstack();
675 1.68 rillig debug_leave();
676 1.1 cgd }
677 1.1 cgd
678 1.36 rillig /*
679 1.36 rillig * Take all entries, including the first which requires a closing brace,
680 1.36 rillig * from the stack.
681 1.36 rillig */
682 1.36 rillig static void
683 1.36 rillig initstack_pop_brace(void)
684 1.36 rillig {
685 1.62 rillig bool brace;
686 1.36 rillig
687 1.68 rillig debug_enter();
688 1.73 rillig debug_initstack();
689 1.36 rillig do {
690 1.36 rillig brace = initstk->i_brace;
691 1.119 rillig /* TODO: improve wording of the debug message */
692 1.68 rillig debug_step("loop brace=%d", brace);
693 1.36 rillig initstack_pop_item();
694 1.36 rillig } while (!brace);
695 1.73 rillig debug_initstack();
696 1.68 rillig debug_leave();
697 1.36 rillig }
698 1.36 rillig
699 1.36 rillig /*
700 1.36 rillig * Take all entries which cannot be used for further initializers from the
701 1.36 rillig * stack, but do this only if they do not require a closing brace.
702 1.36 rillig */
703 1.119 rillig /* TODO: think of a better name than 'pop' */
704 1.1 cgd static void
705 1.36 rillig initstack_pop_nobrace(void)
706 1.1 cgd {
707 1.7 lukem
708 1.68 rillig debug_enter();
709 1.43 rillig while (!initstk->i_brace && initstk->i_remaining == 0 &&
710 1.77 rillig !initstk->i_array_of_unknown_size)
711 1.36 rillig initstack_pop_item();
712 1.68 rillig debug_leave();
713 1.1 cgd }
714 1.1 cgd
715 1.97 rillig /* Extend an array of unknown size by one element */
716 1.97 rillig static void
717 1.97 rillig extend_if_array_of_unknown_size(void)
718 1.97 rillig {
719 1.97 rillig initstack_element *istk = initstk;
720 1.97 rillig
721 1.97 rillig if (istk->i_remaining != 0)
722 1.97 rillig return;
723 1.97 rillig
724 1.97 rillig /*
725 1.97 rillig * The only place where an incomplete array may appear is at the
726 1.97 rillig * outermost aggregate level of the object to be initialized.
727 1.97 rillig */
728 1.97 rillig lint_assert(istk->i_enclosing->i_enclosing == NULL);
729 1.97 rillig lint_assert(istk->i_type->t_tspec == ARRAY);
730 1.97 rillig
731 1.97 rillig debug_step("extending array of unknown size '%s'",
732 1.97 rillig type_name(istk->i_type));
733 1.97 rillig istk->i_remaining = 1;
734 1.97 rillig istk->i_type->t_dim++;
735 1.97 rillig setcomplete(istk->i_type, true);
736 1.97 rillig
737 1.97 rillig debug_step("extended type is '%s'", type_name(istk->i_type));
738 1.97 rillig }
739 1.97 rillig
740 1.119 rillig /* TODO: document me */
741 1.119 rillig /* TODO: think of a better name than 'push' */
742 1.1 cgd static void
743 1.99 rillig initstack_push_array(void)
744 1.99 rillig {
745 1.99 rillig initstack_element *const istk = initstk;
746 1.99 rillig
747 1.99 rillig if (istk->i_enclosing->i_seen_named_member) {
748 1.99 rillig istk->i_brace = true;
749 1.124 rillig debug_step("ARRAY%s%s",
750 1.124 rillig istk->i_brace ? ", needs closing brace" : "",
751 1.124 rillig istk->i_enclosing->i_seen_named_member
752 1.124 rillig ? ", seen named member" : "");
753 1.99 rillig }
754 1.99 rillig
755 1.99 rillig if (is_incomplete(istk->i_type) &&
756 1.99 rillig istk->i_enclosing->i_enclosing != NULL) {
757 1.99 rillig /* initialization of an incomplete type */
758 1.99 rillig error(175);
759 1.99 rillig initerr = true;
760 1.99 rillig return;
761 1.99 rillig }
762 1.99 rillig
763 1.99 rillig istk->i_subt = istk->i_type->t_subt;
764 1.99 rillig istk->i_array_of_unknown_size = is_incomplete(istk->i_type);
765 1.99 rillig istk->i_remaining = istk->i_type->t_dim;
766 1.126 rillig debug_designation();
767 1.99 rillig debug_step("type '%s' remaining %d",
768 1.99 rillig type_name(istk->i_type), istk->i_remaining);
769 1.99 rillig }
770 1.99 rillig
771 1.119 rillig /* TODO: document me */
772 1.119 rillig /* TODO: think of a better name than 'push' */
773 1.99 rillig static bool
774 1.99 rillig initstack_push_struct_or_union(void)
775 1.99 rillig {
776 1.119 rillig /*
777 1.119 rillig * TODO: remove unnecessary 'const' for variables in functions that
778 1.119 rillig * fit on a single screen. Keep it for larger functions.
779 1.119 rillig */
780 1.99 rillig initstack_element *const istk = initstk;
781 1.99 rillig int cnt;
782 1.99 rillig sym_t *m;
783 1.99 rillig
784 1.99 rillig if (is_incomplete(istk->i_type)) {
785 1.99 rillig /* initialization of an incomplete type */
786 1.99 rillig error(175);
787 1.99 rillig initerr = true;
788 1.99 rillig return false;
789 1.99 rillig }
790 1.100 rillig
791 1.99 rillig cnt = 0;
792 1.126 rillig debug_designation();
793 1.99 rillig debug_step("lookup for '%s'%s",
794 1.99 rillig type_name(istk->i_type),
795 1.99 rillig istk->i_seen_named_member ? ", seen named member" : "");
796 1.100 rillig
797 1.99 rillig for (m = istk->i_type->t_str->sou_first_member;
798 1.99 rillig m != NULL; m = m->s_next) {
799 1.99 rillig if (m->s_bitfield && m->s_name == unnamed)
800 1.99 rillig continue;
801 1.119 rillig /*
802 1.119 rillig * TODO: split into separate functions:
803 1.119 rillig *
804 1.119 rillig * look_up_array_next
805 1.119 rillig * look_up_array_designator
806 1.119 rillig * look_up_struct_next
807 1.119 rillig * look_up_struct_designator
808 1.119 rillig */
809 1.123 rillig if (current_designation() != NULL) {
810 1.119 rillig /* XXX: this log entry looks unnecessarily verbose */
811 1.100 rillig debug_step("have member '%s', want member '%s'",
812 1.123 rillig m->s_name, current_designation()->n_name);
813 1.123 rillig if (strcmp(m->s_name, current_designation()->n_name) ==
814 1.122 rillig 0) {
815 1.99 rillig cnt++;
816 1.99 rillig break;
817 1.99 rillig } else
818 1.99 rillig continue;
819 1.99 rillig }
820 1.99 rillig if (++cnt == 1) {
821 1.125 rillig istk->i_next_member = m;
822 1.99 rillig istk->i_subt = m->s_type;
823 1.99 rillig }
824 1.99 rillig }
825 1.100 rillig
826 1.123 rillig if (current_designation() != NULL) {
827 1.99 rillig if (m == NULL) {
828 1.99 rillig debug_step("pop struct");
829 1.99 rillig return true;
830 1.99 rillig }
831 1.125 rillig istk->i_next_member = m;
832 1.99 rillig istk->i_subt = m->s_type;
833 1.99 rillig istk->i_seen_named_member = true;
834 1.123 rillig debug_step("named member '%s'", current_designation()->n_name);
835 1.105 rillig designator_shift_name();
836 1.99 rillig cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
837 1.99 rillig }
838 1.99 rillig istk->i_brace = true;
839 1.99 rillig debug_step("unnamed element with type '%s'%s",
840 1.99 rillig type_name(istk->i_type != NULL ? istk->i_type : istk->i_subt),
841 1.99 rillig istk->i_brace ? ", needs closing brace" : "");
842 1.99 rillig if (cnt == 0) {
843 1.99 rillig /* cannot init. struct/union with no named member */
844 1.99 rillig error(179);
845 1.99 rillig initerr = true;
846 1.99 rillig return false;
847 1.99 rillig }
848 1.99 rillig istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
849 1.99 rillig return false;
850 1.99 rillig }
851 1.99 rillig
852 1.119 rillig /* TODO: document me */
853 1.119 rillig /* TODO: think of a better name than 'push' */
854 1.99 rillig static void
855 1.35 rillig initstack_push(void)
856 1.1 cgd {
857 1.79 rillig initstack_element *istk, *inxt;
858 1.1 cgd
859 1.68 rillig debug_enter();
860 1.68 rillig
861 1.97 rillig extend_if_array_of_unknown_size();
862 1.97 rillig
863 1.1 cgd istk = initstk;
864 1.44 rillig lint_assert(istk->i_remaining > 0);
865 1.61 rillig lint_assert(istk->i_type == NULL || !is_scalar(istk->i_type->t_tspec));
866 1.1 cgd
867 1.79 rillig initstk = xcalloc(1, sizeof (initstack_element));
868 1.77 rillig initstk->i_enclosing = istk;
869 1.1 cgd initstk->i_type = istk->i_subt;
870 1.44 rillig lint_assert(initstk->i_type->t_tspec != FUNC);
871 1.1 cgd
872 1.12 christos again:
873 1.1 cgd istk = initstk;
874 1.1 cgd
875 1.88 rillig debug_step("expecting type '%s'", type_name(istk->i_type));
876 1.107 rillig lint_assert(istk->i_type != NULL);
877 1.1 cgd switch (istk->i_type->t_tspec) {
878 1.1 cgd case ARRAY:
879 1.123 rillig if (current_designation() != NULL) {
880 1.124 rillig debug_step("pop array, named member '%s'%s",
881 1.124 rillig current_designation()->n_name,
882 1.124 rillig istk->i_brace ? ", needs closing brace" : "");
883 1.19 christos goto pop;
884 1.98 rillig }
885 1.98 rillig
886 1.99 rillig initstack_push_array();
887 1.99 rillig break;
888 1.20 christos
889 1.1 cgd case UNION:
890 1.1 cgd if (tflag)
891 1.89 rillig /* initialization of union is illegal in trad. C */
892 1.1 cgd warning(238);
893 1.1 cgd /* FALLTHROUGH */
894 1.1 cgd case STRUCT:
895 1.99 rillig if (initstack_push_struct_or_union())
896 1.99 rillig goto pop;
897 1.1 cgd break;
898 1.1 cgd default:
899 1.123 rillig if (current_designation() != NULL) {
900 1.100 rillig debug_step("pop scalar");
901 1.19 christos pop:
902 1.119 rillig /* TODO: extract this into end_initializer_level */
903 1.77 rillig inxt = initstk->i_enclosing;
904 1.12 christos free(istk);
905 1.22 ad initstk = inxt;
906 1.12 christos goto again;
907 1.12 christos }
908 1.100 rillig /* The initialization stack now expects a single scalar. */
909 1.43 rillig istk->i_remaining = 1;
910 1.1 cgd break;
911 1.1 cgd }
912 1.68 rillig
913 1.73 rillig debug_initstack();
914 1.68 rillig debug_leave();
915 1.1 cgd }
916 1.1 cgd
917 1.1 cgd static void
918 1.84 rillig check_too_many_initializers(void)
919 1.1 cgd {
920 1.1 cgd
921 1.84 rillig const initstack_element *istk = initstk;
922 1.84 rillig if (istk->i_remaining > 0)
923 1.84 rillig return;
924 1.119 rillig /*
925 1.119 rillig * FIXME: even with named members, there can be too many initializers
926 1.119 rillig */
927 1.84 rillig if (istk->i_array_of_unknown_size || istk->i_seen_named_member)
928 1.84 rillig return;
929 1.1 cgd
930 1.84 rillig tspec_t t = istk->i_type->t_tspec;
931 1.84 rillig if (t == ARRAY) {
932 1.84 rillig /* too many array initializers, expected %d */
933 1.84 rillig error(173, istk->i_type->t_dim);
934 1.84 rillig } else if (t == STRUCT || t == UNION) {
935 1.84 rillig /* too many struct/union initializers */
936 1.84 rillig error(172);
937 1.84 rillig } else {
938 1.84 rillig /* too many initializers */
939 1.84 rillig error(174);
940 1.1 cgd }
941 1.84 rillig initerr = true;
942 1.1 cgd }
943 1.1 cgd
944 1.92 rillig /*
945 1.92 rillig * Process a '{' in an initializer by starting the initialization of the
946 1.92 rillig * nested data structure, with i_type being the i_subt of the outer
947 1.92 rillig * initialization level.
948 1.92 rillig */
949 1.1 cgd static void
950 1.37 rillig initstack_next_brace(void)
951 1.1 cgd {
952 1.7 lukem
953 1.68 rillig debug_enter();
954 1.73 rillig debug_initstack();
955 1.68 rillig
956 1.61 rillig if (initstk->i_type != NULL && is_scalar(initstk->i_type->t_tspec)) {
957 1.49 rillig /* invalid initializer type %s */
958 1.57 rillig error(176, type_name(initstk->i_type));
959 1.62 rillig initerr = true;
960 1.37 rillig }
961 1.37 rillig if (!initerr)
962 1.84 rillig check_too_many_initializers();
963 1.37 rillig if (!initerr)
964 1.37 rillig initstack_push();
965 1.37 rillig if (!initerr) {
966 1.62 rillig initstk->i_brace = true;
967 1.126 rillig debug_designation();
968 1.92 rillig debug_step("expecting type '%s'",
969 1.92 rillig type_name(initstk->i_type != NULL ? initstk->i_type
970 1.74 rillig : initstk->i_subt));
971 1.37 rillig }
972 1.68 rillig
973 1.73 rillig debug_initstack();
974 1.68 rillig debug_leave();
975 1.37 rillig }
976 1.37 rillig
977 1.119 rillig /* TODO: document me, or think of a better name */
978 1.37 rillig static void
979 1.118 rillig initstack_next_nobrace(tnode_t *tn)
980 1.37 rillig {
981 1.68 rillig debug_enter();
982 1.37 rillig
983 1.61 rillig if (initstk->i_type == NULL && !is_scalar(initstk->i_subt->t_tspec)) {
984 1.37 rillig /* {}-enclosed initializer required */
985 1.37 rillig error(181);
986 1.92 rillig /* XXX: maybe set initerr here */
987 1.37 rillig }
988 1.37 rillig
989 1.84 rillig if (!initerr)
990 1.84 rillig check_too_many_initializers();
991 1.84 rillig
992 1.42 rillig while (!initerr) {
993 1.118 rillig initstack_element *istk = initstk;
994 1.118 rillig
995 1.118 rillig if (tn->tn_type->t_tspec == STRUCT &&
996 1.118 rillig istk->i_type == tn->tn_type &&
997 1.118 rillig istk->i_enclosing != NULL &&
998 1.118 rillig istk->i_enclosing->i_enclosing != NULL) {
999 1.118 rillig istk->i_brace = false;
1000 1.118 rillig istk->i_remaining = 1; /* the struct itself */
1001 1.118 rillig break;
1002 1.118 rillig }
1003 1.118 rillig
1004 1.118 rillig if ((istk->i_type != NULL && is_scalar(istk->i_type->t_tspec)))
1005 1.42 rillig break;
1006 1.42 rillig initstack_push();
1007 1.1 cgd }
1008 1.68 rillig
1009 1.73 rillig debug_initstack();
1010 1.68 rillig debug_leave();
1011 1.1 cgd }
1012 1.1 cgd
1013 1.119 rillig /* TODO: document me */
1014 1.1 cgd void
1015 1.34 rillig init_lbrace(void)
1016 1.1 cgd {
1017 1.1 cgd if (initerr)
1018 1.1 cgd return;
1019 1.1 cgd
1020 1.68 rillig debug_enter();
1021 1.73 rillig debug_initstack();
1022 1.68 rillig
1023 1.1 cgd if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
1024 1.77 rillig initstk->i_enclosing == NULL) {
1025 1.61 rillig if (tflag && !is_scalar(initstk->i_subt->t_tspec))
1026 1.50 rillig /* no automatic aggregate initialization in trad. C */
1027 1.1 cgd warning(188);
1028 1.1 cgd }
1029 1.1 cgd
1030 1.1 cgd /*
1031 1.1 cgd * Remove all entries which cannot be used for further initializers
1032 1.1 cgd * and do not expect a closing brace.
1033 1.1 cgd */
1034 1.36 rillig initstack_pop_nobrace();
1035 1.1 cgd
1036 1.37 rillig initstack_next_brace();
1037 1.68 rillig
1038 1.73 rillig debug_initstack();
1039 1.68 rillig debug_leave();
1040 1.1 cgd }
1041 1.1 cgd
1042 1.92 rillig /*
1043 1.92 rillig * Process a '}' in an initializer by finishing the current level of the
1044 1.92 rillig * initialization stack.
1045 1.92 rillig */
1046 1.1 cgd void
1047 1.34 rillig init_rbrace(void)
1048 1.1 cgd {
1049 1.1 cgd if (initerr)
1050 1.1 cgd return;
1051 1.1 cgd
1052 1.68 rillig debug_enter();
1053 1.36 rillig initstack_pop_brace();
1054 1.68 rillig debug_leave();
1055 1.1 cgd }
1056 1.1 cgd
1057 1.86 rillig /* In traditional C, bit-fields can be initialized only by integer constants. */
1058 1.86 rillig static void
1059 1.86 rillig check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
1060 1.86 rillig {
1061 1.86 rillig if (tflag &&
1062 1.86 rillig is_integer(lt) &&
1063 1.86 rillig ln->tn_type->t_bitfield &&
1064 1.86 rillig !is_integer(rt)) {
1065 1.89 rillig /* bit-field initialization is illegal in traditional C */
1066 1.86 rillig warning(186);
1067 1.86 rillig }
1068 1.86 rillig }
1069 1.86 rillig
1070 1.87 rillig static void
1071 1.87 rillig check_non_constant_initializer(const tnode_t *tn, scl_t sclass)
1072 1.87 rillig {
1073 1.119 rillig /* TODO: rename CON to CONSTANT to avoid ambiguity with CONVERT */
1074 1.87 rillig if (tn == NULL || tn->tn_op == CON)
1075 1.87 rillig return;
1076 1.87 rillig
1077 1.87 rillig sym_t *sym;
1078 1.87 rillig ptrdiff_t offs;
1079 1.87 rillig if (constant_addr(tn, &sym, &offs))
1080 1.87 rillig return;
1081 1.87 rillig
1082 1.87 rillig if (sclass == AUTO || sclass == REG) {
1083 1.87 rillig /* non-constant initializer */
1084 1.87 rillig c99ism(177);
1085 1.87 rillig } else {
1086 1.87 rillig /* non-constant initializer */
1087 1.87 rillig error(177);
1088 1.87 rillig }
1089 1.87 rillig }
1090 1.87 rillig
1091 1.114 rillig /*
1092 1.119 rillig * Initialize a non-array object with automatic storage duration and only a
1093 1.119 rillig * single initializer expression without braces by delegating to ASSIGN.
1094 1.114 rillig */
1095 1.114 rillig static bool
1096 1.114 rillig init_using_assign(tnode_t *rn)
1097 1.114 rillig {
1098 1.114 rillig tnode_t *ln, *tn;
1099 1.114 rillig
1100 1.114 rillig if (initsym->s_type->t_tspec == ARRAY)
1101 1.114 rillig return false;
1102 1.114 rillig if (initstk->i_enclosing != NULL)
1103 1.114 rillig return false;
1104 1.114 rillig
1105 1.114 rillig debug_step("handing over to ASSIGN");
1106 1.119 rillig
1107 1.114 rillig ln = new_name_node(initsym, 0);
1108 1.114 rillig ln->tn_type = tduptyp(ln->tn_type);
1109 1.114 rillig ln->tn_type->t_const = false;
1110 1.119 rillig
1111 1.114 rillig tn = build(ASSIGN, ln, rn);
1112 1.114 rillig expr(tn, false, false, false, false);
1113 1.119 rillig
1114 1.114 rillig /* XXX: why not clean up the initstack here already? */
1115 1.114 rillig return true;
1116 1.114 rillig }
1117 1.114 rillig
1118 1.116 rillig static void
1119 1.116 rillig check_init_expr(tnode_t *tn, scl_t sclass)
1120 1.116 rillig {
1121 1.116 rillig tnode_t *ln;
1122 1.116 rillig tspec_t lt, rt;
1123 1.116 rillig struct mbl *tmem;
1124 1.116 rillig
1125 1.116 rillig /* Create a temporary node for the left side. */
1126 1.116 rillig ln = tgetblk(sizeof (tnode_t));
1127 1.116 rillig ln->tn_op = NAME;
1128 1.116 rillig ln->tn_type = tduptyp(initstk->i_type);
1129 1.116 rillig ln->tn_type->t_const = false;
1130 1.116 rillig ln->tn_lvalue = true;
1131 1.116 rillig ln->tn_sym = initsym; /* better than nothing */
1132 1.116 rillig
1133 1.116 rillig tn = cconv(tn);
1134 1.116 rillig
1135 1.116 rillig lt = ln->tn_type->t_tspec;
1136 1.116 rillig rt = tn->tn_type->t_tspec;
1137 1.116 rillig
1138 1.116 rillig debug_step("typeok '%s', '%s'",
1139 1.116 rillig type_name(ln->tn_type), type_name(tn->tn_type));
1140 1.116 rillig if (!typeok(INIT, 0, ln, tn))
1141 1.116 rillig return;
1142 1.116 rillig
1143 1.116 rillig /*
1144 1.119 rillig * Preserve the tree memory. This is necessary because otherwise
1145 1.116 rillig * expr() would free it.
1146 1.116 rillig */
1147 1.116 rillig tmem = tsave();
1148 1.116 rillig expr(tn, true, false, true, false);
1149 1.116 rillig trestor(tmem);
1150 1.116 rillig
1151 1.116 rillig check_bit_field_init(ln, lt, rt);
1152 1.116 rillig
1153 1.116 rillig /*
1154 1.116 rillig * XXX: Is it correct to do this conversion _after_ the typeok above?
1155 1.116 rillig */
1156 1.116 rillig if (lt != rt || (initstk->i_type->t_bitfield && tn->tn_op == CON))
1157 1.116 rillig tn = convert(INIT, 0, initstk->i_type, tn);
1158 1.116 rillig
1159 1.116 rillig check_non_constant_initializer(tn, sclass);
1160 1.116 rillig }
1161 1.116 rillig
1162 1.1 cgd void
1163 1.69 rillig init_using_expr(tnode_t *tn)
1164 1.1 cgd {
1165 1.81 rillig scl_t sclass;
1166 1.1 cgd
1167 1.68 rillig debug_enter();
1168 1.92 rillig debug_initstack();
1169 1.126 rillig debug_designation();
1170 1.95 rillig debug_step("expr:");
1171 1.95 rillig debug_node(tn, debug_ind + 1);
1172 1.55 rillig
1173 1.113 rillig if (initerr || tn == NULL)
1174 1.113 rillig goto done;
1175 1.1 cgd
1176 1.81 rillig sclass = initsym->s_scl;
1177 1.114 rillig if ((sclass == AUTO || sclass == REG) && init_using_assign(tn))
1178 1.113 rillig goto done;
1179 1.1 cgd
1180 1.36 rillig initstack_pop_nobrace();
1181 1.1 cgd
1182 1.82 rillig if (init_array_using_string(tn)) {
1183 1.82 rillig debug_step("after initializing the string:");
1184 1.82 rillig /* XXX: why not clean up the initstack here already? */
1185 1.113 rillig goto done_initstack;
1186 1.68 rillig }
1187 1.1 cgd
1188 1.118 rillig initstack_next_nobrace(tn);
1189 1.113 rillig if (initerr || tn == NULL)
1190 1.113 rillig goto done_initstack;
1191 1.1 cgd
1192 1.43 rillig initstk->i_remaining--;
1193 1.83 rillig debug_step("%d elements remaining", initstk->i_remaining);
1194 1.83 rillig
1195 1.116 rillig check_init_expr(tn, sclass);
1196 1.68 rillig
1197 1.113 rillig done_initstack:
1198 1.73 rillig debug_initstack();
1199 1.113 rillig done:
1200 1.68 rillig debug_leave();
1201 1.1 cgd }
1202 1.1 cgd
1203 1.1 cgd
1204 1.82 rillig /* Initialize a character array or wchar_t array with a string literal. */
1205 1.62 rillig static bool
1206 1.82 rillig init_array_using_string(tnode_t *tn)
1207 1.1 cgd {
1208 1.1 cgd tspec_t t;
1209 1.79 rillig initstack_element *istk;
1210 1.1 cgd int len;
1211 1.1 cgd strg_t *strg;
1212 1.1 cgd
1213 1.1 cgd if (tn->tn_op != STRING)
1214 1.63 rillig return false;
1215 1.1 cgd
1216 1.68 rillig debug_enter();
1217 1.73 rillig debug_initstack();
1218 1.68 rillig
1219 1.1 cgd istk = initstk;
1220 1.47 rillig strg = tn->tn_string;
1221 1.1 cgd
1222 1.1 cgd /*
1223 1.1 cgd * Check if we have an array type which can be initialized by
1224 1.1 cgd * the string.
1225 1.1 cgd */
1226 1.12 christos if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
1227 1.68 rillig debug_step("subt array");
1228 1.1 cgd t = istk->i_subt->t_subt->t_tspec;
1229 1.1 cgd if (!((strg->st_tspec == CHAR &&
1230 1.1 cgd (t == CHAR || t == UCHAR || t == SCHAR)) ||
1231 1.1 cgd (strg->st_tspec == WCHAR && t == WCHAR))) {
1232 1.68 rillig debug_leave();
1233 1.63 rillig return false;
1234 1.1 cgd }
1235 1.82 rillig /* XXX: duplicate code, see below */
1236 1.119 rillig
1237 1.1 cgd /* Put the array at top of stack */
1238 1.35 rillig initstack_push();
1239 1.1 cgd istk = initstk;
1240 1.119 rillig
1241 1.119 rillig
1242 1.119 rillig /* TODO: what if both i_type and i_subt are ARRAY? */
1243 1.119 rillig
1244 1.1 cgd } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
1245 1.68 rillig debug_step("type array");
1246 1.1 cgd t = istk->i_type->t_subt->t_tspec;
1247 1.1 cgd if (!((strg->st_tspec == CHAR &&
1248 1.1 cgd (t == CHAR || t == UCHAR || t == SCHAR)) ||
1249 1.1 cgd (strg->st_tspec == WCHAR && t == WCHAR))) {
1250 1.68 rillig debug_leave();
1251 1.63 rillig return false;
1252 1.1 cgd }
1253 1.82 rillig /* XXX: duplicate code, see above */
1254 1.119 rillig
1255 1.119 rillig /*
1256 1.119 rillig * TODO: is this really not needed in the branch above this
1257 1.119 rillig * one?
1258 1.119 rillig */
1259 1.1 cgd /*
1260 1.1 cgd * If the array is already partly initialized, we are
1261 1.1 cgd * wrong here.
1262 1.1 cgd */
1263 1.115 rillig if (istk->i_remaining != istk->i_type->t_dim) {
1264 1.68 rillig debug_leave();
1265 1.63 rillig return false;
1266 1.115 rillig }
1267 1.1 cgd } else {
1268 1.68 rillig debug_leave();
1269 1.63 rillig return false;
1270 1.1 cgd }
1271 1.1 cgd
1272 1.1 cgd /* Get length without trailing NUL character. */
1273 1.1 cgd len = strg->st_len;
1274 1.1 cgd
1275 1.77 rillig if (istk->i_array_of_unknown_size) {
1276 1.77 rillig istk->i_array_of_unknown_size = false;
1277 1.1 cgd istk->i_type->t_dim = len + 1;
1278 1.63 rillig setcomplete(istk->i_type, true);
1279 1.1 cgd } else {
1280 1.119 rillig /*
1281 1.119 rillig * TODO: check for buffer overflow in the object to be
1282 1.119 rillig * initialized
1283 1.119 rillig */
1284 1.119 rillig /* XXX: double-check for off-by-one error */
1285 1.1 cgd if (istk->i_type->t_dim < len) {
1286 1.1 cgd /* non-null byte ignored in string initializer */
1287 1.1 cgd warning(187);
1288 1.1 cgd }
1289 1.119 rillig
1290 1.119 rillig /*
1291 1.119 rillig * TODO: C99 6.7.8p14 allows a string literal to be enclosed
1292 1.119 rillig * in optional redundant braces, just like scalars. Add tests
1293 1.119 rillig * for this.
1294 1.119 rillig */
1295 1.1 cgd }
1296 1.1 cgd
1297 1.1 cgd /* In every case the array is initialized completely. */
1298 1.43 rillig istk->i_remaining = 0;
1299 1.1 cgd
1300 1.73 rillig debug_initstack();
1301 1.68 rillig debug_leave();
1302 1.63 rillig return true;
1303 1.1 cgd }
1304