init.c revision 1.104 1 /* $NetBSD: init.c,v 1.104 2021/03/19 01:02:52 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for
18 * The NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
37
38 #include <sys/cdefs.h>
39 #if defined(__RCSID) && !defined(lint)
40 __RCSID("$NetBSD: init.c,v 1.104 2021/03/19 01:02:52 rillig Exp $");
41 #endif
42
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "lint1.h"
47
48
49 /*
50 * Initialization
51 *
52 * Handles initializations of global or local objects, like in:
53 *
54 * int number = 12345;
55 * int number_with_braces = { 12345 };
56 *
57 * int array_of_unknown_size[] = { 111, 222, 333 };
58 * int array_flat[2][2] = { 11, 12, 21, 22 };
59 * int array_nested[2][2] = { { 11, 12 }, { 21, 22 } };
60 *
61 * struct { int x, y; } point = { 3, 4 };
62 * struct { int x, y; } point = { .y = 3, .x = 4 };
63 *
64 * The initializer that follows the '=' may be surrounded by an extra pair of
65 * braces, like in the example 'number_with_braces'. For multi-dimensional
66 * arrays, the inner braces may be omitted like in array_flat or spelled out
67 * like in array_nested.
68 *
69 * For the initializer, the grammar parser calls these functions:
70 *
71 * init_lbrace for each '{'
72 * init_using_expr for each value
73 * init_rbrace for each '}'
74 *
75 * The state of the current initialization is stored in initstk, a stack of
76 * initstack_element, one element per type aggregate level.
77 *
78 * Most of the time, the topmost level of initstk contains a scalar type, and
79 * its remaining count toggles between 1 and 0.
80 *
81 * See also:
82 * C99 6.7.8 "Initialization"
83 * d_c99_init.c for more examples
84 */
85
86
87 /*
88 * Type of stack which is used for initialization of aggregate types.
89 *
90 * XXX: Since C99, a stack is an inappropriate data structure for modelling
91 * an initialization, since the designators don't have to be listed in a
92 * particular order and can designate parts of sub-objects. The member names
93 * of non-leaf structs may thus appear repeatedly, as demonstrated in
94 * d_init_pop_member.c.
95 *
96 * XXX: During initialization, there may be members of the top-level struct
97 * that are partially initialized. The simple i_remaining cannot model this
98 * appropriately.
99 *
100 * See C99 6.7.8, which spans 6 pages full of tricky details and carefully
101 * selected examples.
102 */
103 typedef struct initstack_element {
104
105 /*
106 * The type to be initialized at this level.
107 *
108 * On the outermost element, this is always NULL since the outermost
109 * initializer-expression may be enclosed in an optional pair of
110 * braces. This optional pair of braces is handled by the combination
111 * of i_type and i_subt.
112 *
113 * Everywhere else it is nonnull.
114 */
115 type_t *i_type;
116
117 /*
118 * The type that will be initialized at the next initialization level,
119 * usually enclosed by another pair of braces.
120 *
121 * For an array, it is the element type, but without 'const'.
122 *
123 * For a struct or union type, it is one of the member types, but
124 * without 'const'.
125 *
126 * The outermost stack element has no i_type but nevertheless has
127 * i_subt. For example, in 'int var = { 12345 }', initially there is
128 * an initstack_element with i_subt 'int'. When the '{' is processed,
129 * an element with i_type 'int' is pushed to the stack. When the
130 * corresponding '}' is processed, the inner element is popped again.
131 *
132 * During initialization, only the top 2 elements of the stack are
133 * looked at.
134 */
135 type_t *i_subt;
136
137 /*
138 * This level of the initializer requires a '}' to be completed.
139 *
140 * Multidimensional arrays do not need a closing brace to complete
141 * an inner array; for example, { 1, 2, 3, 4 } is a valid initializer
142 * for int arr[2][2].
143 *
144 * TODO: Do structs containing structs need a closing brace?
145 * TODO: Do arrays of structs need a closing brace after each struct?
146 */
147 bool i_brace: 1;
148
149 /* Whether i_type is an array of unknown size. */
150 bool i_array_of_unknown_size: 1;
151 bool i_seen_named_member: 1;
152
153 /*
154 * For structs, the next member to be initialized by an initializer
155 * without an optional designator.
156 */
157 sym_t *i_current_object;
158
159 /*
160 * The number of remaining elements to be used by expressions without
161 * designator.
162 *
163 * This says nothing about which members have been initialized or not
164 * since starting with C99, members may be initialized in arbitrary
165 * order by using designators.
166 *
167 * For an array of unknown size, this is always 0 and thus irrelevant.
168 *
169 * XXX: for scalars?
170 * XXX: for structs?
171 * XXX: for unions?
172 * XXX: for arrays?
173 */
174 int i_remaining;
175
176 /*
177 * The initialization state of the enclosing data structure
178 * (struct, union, array).
179 */
180 struct initstack_element *i_enclosing;
181 } initstack_element;
182
183 /*
184 * The names for a nested C99 initialization designator, in a circular list.
185 *
186 * Example:
187 * struct stat st = {
188 * .st_size = 123,
189 * .st_mtim.tv_sec = 45,
190 * .st_mtim.tv_nsec
191 * };
192 *
193 * During initialization, this list first contains ["st_size"], then
194 * ["st_mtim", "tv_sec"], then ["st_mtim", "tv_nsec"].
195 */
196 typedef struct namlist {
197 const char *n_name;
198 struct namlist *n_prev;
199 struct namlist *n_next;
200 } namlist_t;
201
202
203 /*
204 * initerr is set as soon as a fatal error occurred in an initialization.
205 * The effect is that the rest of the initialization is ignored (parsed
206 * by yacc, expression trees built, but no initialization takes place).
207 */
208 bool initerr;
209
210 /* Pointer to the symbol which is to be initialized. */
211 sym_t *initsym;
212
213 /* Points to the top element of the initialization stack. */
214 initstack_element *initstk;
215
216 /* Points to a c9x named member; */
217 namlist_t *namedmem = NULL;
218
219
220 static bool init_array_using_string(tnode_t *);
221
222 #ifndef DEBUG
223
224 #define debug_printf(fmt, ...) do { } while (false)
225 #define debug_indent() do { } while (false)
226 #define debug_enter(a) do { } while (false)
227 #define debug_step(fmt, ...) do { } while (false)
228 #define debug_leave(a) do { } while (false)
229 #define debug_named_member() do { } while (false)
230 #define debug_initstack_element(elem) do { } while (false)
231 #define debug_initstack() do { } while (false)
232
233 #else
234
235 static int debug_ind = 0;
236
237 static void __printflike(1, 2)
238 debug_printf(const char *fmt, ...)
239 {
240 va_list va;
241
242 va_start(va, fmt);
243 vfprintf(stdout, fmt, va);
244 va_end(va);
245 }
246
247 static void
248 debug_indent(void)
249 {
250 debug_printf("%*s", 2 * debug_ind, "");
251 }
252
253 static void
254 debug_enter(const char *func)
255 {
256 printf("%*s+ %s\n", 2 * debug_ind++, "", func);
257 }
258
259 static void __printflike(1, 2)
260 debug_step(const char *fmt, ...)
261 {
262 va_list va;
263
264 printf("%*s", 2 * debug_ind, "");
265 va_start(va, fmt);
266 vfprintf(stdout, fmt, va);
267 va_end(va);
268 printf("\n");
269 }
270
271 static void
272 debug_leave(const char *func)
273 {
274 printf("%*s- %s\n", 2 * --debug_ind, "", func);
275 }
276
277 static void
278 debug_named_member(void)
279 {
280 namlist_t *name;
281
282 if (namedmem == NULL)
283 return;
284 name = namedmem;
285 debug_indent();
286 debug_printf("named member:");
287 do {
288 debug_printf(" %s", name->n_name);
289 name = name->n_next;
290 } while (name != namedmem);
291 debug_printf("\n");
292 }
293
294 static void
295 debug_initstack_element(const initstack_element *elem)
296 {
297 if (elem->i_type != NULL)
298 debug_step(" i_type = %s", type_name(elem->i_type));
299 if (elem->i_subt != NULL)
300 debug_step(" i_subt = %s", type_name(elem->i_subt));
301
302 if (elem->i_brace)
303 debug_step(" i_brace");
304 if (elem->i_array_of_unknown_size)
305 debug_step(" i_array_of_unknown_size");
306 if (elem->i_seen_named_member)
307 debug_step(" i_seen_named_member");
308
309 const type_t *eff_type = elem->i_type != NULL
310 ? elem->i_type : elem->i_subt;
311 if (eff_type->t_tspec == STRUCT && elem->i_current_object != NULL)
312 debug_step(" i_current_object = %s",
313 elem->i_current_object->s_name);
314
315 debug_step(" i_remaining = %d", elem->i_remaining);
316 }
317
318 static void
319 debug_initstack(void)
320 {
321 if (initstk == NULL) {
322 debug_step("initstk is empty");
323 return;
324 }
325
326 size_t i = 0;
327 for (const initstack_element *elem = initstk;
328 elem != NULL; elem = elem->i_enclosing) {
329 debug_step("initstk[%zu]:", i);
330 debug_initstack_element(elem);
331 i++;
332 }
333 }
334
335 #define debug_enter() debug_enter(__func__)
336 #define debug_leave() debug_leave(__func__)
337
338 #endif
339
340 void
341 designator_push_name(sbuf_t *sb)
342 {
343 namlist_t *nam = xcalloc(1, sizeof (namlist_t));
344 nam->n_name = sb->sb_name;
345
346 debug_step("%s: '%s' %p", __func__, nam->n_name, nam);
347
348 if (namedmem == NULL) {
349 /*
350 * XXX: Why is this a circular list?
351 * XXX: Why is this a doubly-linked list?
352 * A simple stack should suffice.
353 */
354 nam->n_prev = nam->n_next = nam;
355 namedmem = nam;
356 } else {
357 namedmem->n_prev->n_next = nam;
358 nam->n_prev = namedmem->n_prev;
359 nam->n_next = namedmem;
360 namedmem->n_prev = nam;
361 }
362 }
363
364 /*
365 * A struct member that has array type is initialized using a designator.
366 *
367 * C99 example: struct { int member[4]; } var = { [2] = 12345 };
368 *
369 * GNU example: struct { int member[4]; } var = { [1 ... 3] = 12345 };
370 */
371 void
372 designator_push_subscript(range_t range)
373 {
374 debug_enter();
375 debug_step("subscript range is %zu ... %zu", range.lo, range.hi);
376 debug_initstack();
377 debug_leave();
378 }
379
380 static void
381 designator_pop_name(void)
382 {
383 debug_step("%s: %s %p", __func__, namedmem->n_name, namedmem);
384 if (namedmem->n_next == namedmem) {
385 free(namedmem);
386 namedmem = NULL;
387 } else {
388 namlist_t *nam = namedmem;
389 namedmem = namedmem->n_next;
390 nam->n_prev->n_next = nam->n_next;
391 nam->n_next->n_prev = nam->n_prev;
392 free(nam);
393 }
394 }
395
396 /*
397 * Initialize the initialization stack by putting an entry for the object
398 * which is to be initialized on it.
399 */
400 void
401 initstack_init(void)
402 {
403 initstack_element *istk;
404
405 if (initerr)
406 return;
407
408 /* free memory used in last initialization */
409 while ((istk = initstk) != NULL) {
410 initstk = istk->i_enclosing;
411 free(istk);
412 }
413
414 debug_enter();
415
416 /*
417 * If the type which is to be initialized is an incomplete array,
418 * it must be duplicated.
419 */
420 if (initsym->s_type->t_tspec == ARRAY && is_incomplete(initsym->s_type))
421 initsym->s_type = duptyp(initsym->s_type);
422
423 istk = initstk = xcalloc(1, sizeof (initstack_element));
424 istk->i_subt = initsym->s_type;
425 istk->i_remaining = 1;
426
427 debug_initstack();
428 debug_leave();
429 }
430
431 static void
432 initstack_pop_item_named_member(void)
433 {
434 initstack_element *istk = initstk;
435 sym_t *m;
436
437 debug_step("initializing named member '%s'", namedmem->n_name);
438
439 if (istk->i_type->t_tspec != STRUCT &&
440 istk->i_type->t_tspec != UNION) {
441 /* syntax error '%s' */
442 error(249, "named member must only be used with struct/union");
443 initerr = true;
444 return;
445 }
446
447 for (m = istk->i_type->t_str->sou_first_member;
448 m != NULL; m = m->s_next) {
449
450 if (m->s_bitfield && m->s_name == unnamed)
451 continue;
452
453 if (strcmp(m->s_name, namedmem->n_name) == 0) {
454 debug_step("found matching member");
455 istk->i_subt = m->s_type;
456 /* XXX: why ++? */
457 istk->i_remaining++;
458 /* XXX: why is i_seen_named_member not set? */
459 designator_pop_name();
460 return;
461 }
462 }
463
464 /* undefined struct/union member: %s */
465 error(101, namedmem->n_name);
466
467 designator_pop_name();
468 istk->i_seen_named_member = true;
469 }
470
471 static void
472 initstack_pop_item_unnamed(void)
473 {
474 initstack_element *istk = initstk;
475 sym_t *m;
476
477 /*
478 * If the removed element was a structure member, we must go
479 * to the next structure member.
480 */
481 if (istk->i_remaining > 0 && istk->i_type->t_tspec == STRUCT &&
482 !istk->i_seen_named_member) {
483 do {
484 m = istk->i_current_object =
485 istk->i_current_object->s_next;
486 /* XXX: can this assertion be made to fail? */
487 lint_assert(m != NULL);
488 debug_step("pop %s", m->s_name);
489 } while (m->s_bitfield && m->s_name == unnamed);
490 /* XXX: duplicate code for skipping unnamed bit-fields */
491 istk->i_subt = m->s_type;
492 }
493 }
494
495 static void
496 initstack_pop_item(void)
497 {
498 initstack_element *istk;
499
500 debug_enter();
501
502 istk = initstk;
503 debug_step("popping:");
504 debug_initstack_element(istk);
505
506 initstk = istk->i_enclosing;
507 free(istk);
508 istk = initstk;
509 lint_assert(istk != NULL);
510
511 istk->i_remaining--;
512 lint_assert(istk->i_remaining >= 0);
513 debug_step("%d elements remaining", istk->i_remaining);
514
515 if (namedmem != NULL)
516 initstack_pop_item_named_member();
517 else
518 initstack_pop_item_unnamed();
519
520 debug_initstack();
521 debug_leave();
522 }
523
524 /*
525 * Take all entries, including the first which requires a closing brace,
526 * from the stack.
527 */
528 static void
529 initstack_pop_brace(void)
530 {
531 bool brace;
532
533 debug_enter();
534 debug_initstack();
535 do {
536 brace = initstk->i_brace;
537 debug_step("loop brace=%d", brace);
538 initstack_pop_item();
539 } while (!brace);
540 debug_initstack();
541 debug_leave();
542 }
543
544 /*
545 * Take all entries which cannot be used for further initializers from the
546 * stack, but do this only if they do not require a closing brace.
547 */
548 static void
549 initstack_pop_nobrace(void)
550 {
551
552 debug_enter();
553 while (!initstk->i_brace && initstk->i_remaining == 0 &&
554 !initstk->i_array_of_unknown_size)
555 initstack_pop_item();
556 debug_leave();
557 }
558
559 /* Extend an array of unknown size by one element */
560 static void
561 extend_if_array_of_unknown_size(void)
562 {
563 initstack_element *istk = initstk;
564
565 if (istk->i_remaining != 0)
566 return;
567
568 /*
569 * The only place where an incomplete array may appear is at the
570 * outermost aggregate level of the object to be initialized.
571 */
572 lint_assert(istk->i_enclosing->i_enclosing == NULL);
573 lint_assert(istk->i_type->t_tspec == ARRAY);
574
575 debug_step("extending array of unknown size '%s'",
576 type_name(istk->i_type));
577 istk->i_remaining = 1;
578 istk->i_type->t_dim++;
579 setcomplete(istk->i_type, true);
580
581 debug_step("extended type is '%s'", type_name(istk->i_type));
582 }
583
584 static void
585 initstack_push_array(void)
586 {
587 initstack_element *const istk = initstk;
588
589 if (istk->i_enclosing->i_seen_named_member) {
590 istk->i_brace = true;
591 debug_step("ARRAY brace=%d, namedmem=%d",
592 istk->i_brace, istk->i_enclosing->i_seen_named_member);
593 }
594
595 if (is_incomplete(istk->i_type) &&
596 istk->i_enclosing->i_enclosing != NULL) {
597 /* initialization of an incomplete type */
598 error(175);
599 initerr = true;
600 return;
601 }
602
603 istk->i_subt = istk->i_type->t_subt;
604 istk->i_array_of_unknown_size = is_incomplete(istk->i_type);
605 istk->i_remaining = istk->i_type->t_dim;
606 debug_named_member();
607 debug_step("type '%s' remaining %d",
608 type_name(istk->i_type), istk->i_remaining);
609 }
610
611 static bool
612 initstack_push_struct_or_union(void)
613 {
614 initstack_element *const istk = initstk;
615 int cnt;
616 sym_t *m;
617
618 if (is_incomplete(istk->i_type)) {
619 /* initialization of an incomplete type */
620 error(175);
621 initerr = true;
622 return false;
623 }
624
625 cnt = 0;
626 debug_named_member();
627 debug_step("lookup for '%s'%s",
628 type_name(istk->i_type),
629 istk->i_seen_named_member ? ", seen named member" : "");
630
631 for (m = istk->i_type->t_str->sou_first_member;
632 m != NULL; m = m->s_next) {
633 if (m->s_bitfield && m->s_name == unnamed)
634 continue;
635 if (namedmem != NULL) {
636 debug_step("have member '%s', want member '%s'",
637 m->s_name, namedmem->n_name);
638 if (strcmp(m->s_name, namedmem->n_name) == 0) {
639 cnt++;
640 break;
641 } else
642 continue;
643 }
644 if (++cnt == 1) {
645 istk->i_current_object = m;
646 istk->i_subt = m->s_type;
647 }
648 }
649
650 if (namedmem != NULL) {
651 if (m == NULL) {
652 debug_step("pop struct");
653 return true;
654 }
655 istk->i_current_object = m;
656 istk->i_subt = m->s_type;
657 istk->i_seen_named_member = true;
658 debug_step("named member '%s'", namedmem->n_name);
659 designator_pop_name();
660 cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
661 }
662 istk->i_brace = true;
663 debug_step("unnamed element with type '%s'%s",
664 type_name(istk->i_type != NULL ? istk->i_type : istk->i_subt),
665 istk->i_brace ? ", needs closing brace" : "");
666 if (cnt == 0) {
667 /* cannot init. struct/union with no named member */
668 error(179);
669 initerr = true;
670 return false;
671 }
672 istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
673 return false;
674 }
675
676 static void
677 initstack_push(void)
678 {
679 initstack_element *istk, *inxt;
680
681 debug_enter();
682
683 extend_if_array_of_unknown_size();
684
685 istk = initstk;
686 lint_assert(istk->i_remaining > 0);
687 lint_assert(istk->i_type == NULL || !is_scalar(istk->i_type->t_tspec));
688
689 initstk = xcalloc(1, sizeof (initstack_element));
690 initstk->i_enclosing = istk;
691 initstk->i_type = istk->i_subt;
692 lint_assert(initstk->i_type->t_tspec != FUNC);
693
694 again:
695 istk = initstk;
696
697 debug_step("expecting type '%s'", type_name(istk->i_type));
698 switch (istk->i_type->t_tspec) {
699 case ARRAY:
700 if (namedmem != NULL) {
701 debug_step("pop array namedmem=%s brace=%d",
702 namedmem->n_name, istk->i_brace);
703 goto pop;
704 }
705
706 initstack_push_array();
707 break;
708
709 case UNION:
710 if (tflag)
711 /* initialization of union is illegal in trad. C */
712 warning(238);
713 /* FALLTHROUGH */
714 case STRUCT:
715 if (initstack_push_struct_or_union())
716 goto pop;
717 break;
718 default:
719 if (namedmem != NULL) {
720 debug_step("pop scalar");
721 pop:
722 inxt = initstk->i_enclosing;
723 free(istk);
724 initstk = inxt;
725 goto again;
726 }
727 /* The initialization stack now expects a single scalar. */
728 istk->i_remaining = 1;
729 break;
730 }
731
732 debug_initstack();
733 debug_leave();
734 }
735
736 static void
737 check_too_many_initializers(void)
738 {
739
740 const initstack_element *istk = initstk;
741 if (istk->i_remaining > 0)
742 return;
743 if (istk->i_array_of_unknown_size || istk->i_seen_named_member)
744 return;
745
746 tspec_t t = istk->i_type->t_tspec;
747 if (t == ARRAY) {
748 /* too many array initializers, expected %d */
749 error(173, istk->i_type->t_dim);
750 } else if (t == STRUCT || t == UNION) {
751 /* too many struct/union initializers */
752 error(172);
753 } else {
754 /* too many initializers */
755 error(174);
756 }
757 initerr = true;
758 }
759
760 /*
761 * Process a '{' in an initializer by starting the initialization of the
762 * nested data structure, with i_type being the i_subt of the outer
763 * initialization level.
764 */
765 static void
766 initstack_next_brace(void)
767 {
768
769 debug_enter();
770 debug_initstack();
771
772 if (initstk->i_type != NULL && is_scalar(initstk->i_type->t_tspec)) {
773 /* invalid initializer type %s */
774 error(176, type_name(initstk->i_type));
775 initerr = true;
776 }
777 if (!initerr)
778 check_too_many_initializers();
779 if (!initerr)
780 initstack_push();
781 if (!initerr) {
782 initstk->i_brace = true;
783 debug_named_member();
784 debug_step("expecting type '%s'",
785 type_name(initstk->i_type != NULL ? initstk->i_type
786 : initstk->i_subt));
787 }
788
789 debug_initstack();
790 debug_leave();
791 }
792
793 static void
794 initstack_next_nobrace(void)
795 {
796 debug_enter();
797
798 if (initstk->i_type == NULL && !is_scalar(initstk->i_subt->t_tspec)) {
799 /* {}-enclosed initializer required */
800 error(181);
801 /* XXX: maybe set initerr here */
802 }
803
804 if (!initerr)
805 check_too_many_initializers();
806
807 /*
808 * Make sure an entry with a scalar type is at the top of the stack.
809 *
810 * FIXME: Since C99, an initializer for an object with automatic
811 * storage need not be a constant expression anymore. It is
812 * perfectly fine to initialize a struct with a struct expression,
813 * see d_struct_init_nested.c for a demonstration.
814 */
815 while (!initerr) {
816 if ((initstk->i_type != NULL &&
817 is_scalar(initstk->i_type->t_tspec)))
818 break;
819 initstack_push();
820 }
821
822 debug_initstack();
823 debug_leave();
824 }
825
826 void
827 init_lbrace(void)
828 {
829 if (initerr)
830 return;
831
832 debug_enter();
833 debug_initstack();
834
835 if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
836 initstk->i_enclosing == NULL) {
837 if (tflag && !is_scalar(initstk->i_subt->t_tspec))
838 /* no automatic aggregate initialization in trad. C */
839 warning(188);
840 }
841
842 /*
843 * Remove all entries which cannot be used for further initializers
844 * and do not expect a closing brace.
845 */
846 initstack_pop_nobrace();
847
848 initstack_next_brace();
849
850 debug_initstack();
851 debug_leave();
852 }
853
854 /*
855 * Process a '}' in an initializer by finishing the current level of the
856 * initialization stack.
857 */
858 void
859 init_rbrace(void)
860 {
861 if (initerr)
862 return;
863
864 debug_enter();
865 initstack_pop_brace();
866 debug_leave();
867 }
868
869 /* In traditional C, bit-fields can be initialized only by integer constants. */
870 static void
871 check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
872 {
873 if (tflag &&
874 is_integer(lt) &&
875 ln->tn_type->t_bitfield &&
876 !is_integer(rt)) {
877 /* bit-field initialization is illegal in traditional C */
878 warning(186);
879 }
880 }
881
882 static void
883 check_non_constant_initializer(const tnode_t *tn, scl_t sclass)
884 {
885 if (tn == NULL || tn->tn_op == CON)
886 return;
887
888 sym_t *sym;
889 ptrdiff_t offs;
890 if (constant_addr(tn, &sym, &offs))
891 return;
892
893 if (sclass == AUTO || sclass == REG) {
894 /* non-constant initializer */
895 c99ism(177);
896 } else {
897 /* non-constant initializer */
898 error(177);
899 }
900 }
901
902 void
903 init_using_expr(tnode_t *tn)
904 {
905 tspec_t lt, rt;
906 tnode_t *ln;
907 struct mbl *tmem;
908 scl_t sclass;
909
910 debug_enter();
911 debug_initstack();
912 debug_named_member();
913 debug_step("expr:");
914 debug_node(tn, debug_ind + 1);
915
916 if (initerr || tn == NULL) {
917 debug_leave();
918 return;
919 }
920
921 sclass = initsym->s_scl;
922
923 /*
924 * Do not test for automatic aggregate initialization. If the
925 * initializer starts with a brace we have the warning already.
926 * If not, an error will be printed that the initializer must
927 * be enclosed by braces.
928 */
929
930 /*
931 * Local initialization of non-array-types with only one expression
932 * without braces is done by ASSIGN
933 */
934 if ((sclass == AUTO || sclass == REG) &&
935 initsym->s_type->t_tspec != ARRAY && initstk->i_enclosing == NULL) {
936 debug_step("handing over to ASSIGN");
937 ln = new_name_node(initsym, 0);
938 ln->tn_type = tduptyp(ln->tn_type);
939 ln->tn_type->t_const = false;
940 tn = build(ASSIGN, ln, tn);
941 expr(tn, false, false, false, false);
942 /* XXX: why not clean up the initstack here already? */
943 debug_leave();
944 return;
945 }
946
947 initstack_pop_nobrace();
948
949 if (init_array_using_string(tn)) {
950 debug_step("after initializing the string:");
951 /* XXX: why not clean up the initstack here already? */
952 debug_initstack();
953 debug_leave();
954 return;
955 }
956
957 initstack_next_nobrace();
958 if (initerr || tn == NULL) {
959 debug_initstack();
960 debug_leave();
961 return;
962 }
963
964 initstk->i_remaining--;
965 debug_step("%d elements remaining", initstk->i_remaining);
966
967 /* Create a temporary node for the left side. */
968 ln = tgetblk(sizeof (tnode_t));
969 ln->tn_op = NAME;
970 ln->tn_type = tduptyp(initstk->i_type);
971 ln->tn_type->t_const = false;
972 ln->tn_lvalue = true;
973 ln->tn_sym = initsym; /* better than nothing */
974
975 tn = cconv(tn);
976
977 lt = ln->tn_type->t_tspec;
978 rt = tn->tn_type->t_tspec;
979
980 lint_assert(is_scalar(lt)); /* at least before C99 */
981
982 debug_step("typeok '%s', '%s'",
983 type_name(ln->tn_type), type_name(tn->tn_type));
984 if (!typeok(INIT, 0, ln, tn)) {
985 debug_initstack();
986 debug_leave();
987 return;
988 }
989
990 /*
991 * Store the tree memory. This is necessary because otherwise
992 * expr() would free it.
993 */
994 tmem = tsave();
995 expr(tn, true, false, true, false);
996 trestor(tmem);
997
998 check_bit_field_init(ln, lt, rt);
999
1000 /*
1001 * XXX: Is it correct to do this conversion _after_ the typeok above?
1002 */
1003 if (lt != rt || (initstk->i_type->t_bitfield && tn->tn_op == CON))
1004 tn = convert(INIT, 0, initstk->i_type, tn);
1005
1006 check_non_constant_initializer(tn, sclass);
1007
1008 debug_initstack();
1009 debug_leave();
1010 }
1011
1012
1013 /* Initialize a character array or wchar_t array with a string literal. */
1014 static bool
1015 init_array_using_string(tnode_t *tn)
1016 {
1017 tspec_t t;
1018 initstack_element *istk;
1019 int len;
1020 strg_t *strg;
1021
1022 if (tn->tn_op != STRING)
1023 return false;
1024
1025 debug_enter();
1026 debug_initstack();
1027
1028 istk = initstk;
1029 strg = tn->tn_string;
1030
1031 /*
1032 * Check if we have an array type which can be initialized by
1033 * the string.
1034 */
1035 if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
1036 debug_step("subt array");
1037 t = istk->i_subt->t_subt->t_tspec;
1038 if (!((strg->st_tspec == CHAR &&
1039 (t == CHAR || t == UCHAR || t == SCHAR)) ||
1040 (strg->st_tspec == WCHAR && t == WCHAR))) {
1041 debug_leave();
1042 return false;
1043 }
1044 /* XXX: duplicate code, see below */
1045 /* Put the array at top of stack */
1046 initstack_push();
1047 istk = initstk;
1048 } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
1049 debug_step("type array");
1050 t = istk->i_type->t_subt->t_tspec;
1051 if (!((strg->st_tspec == CHAR &&
1052 (t == CHAR || t == UCHAR || t == SCHAR)) ||
1053 (strg->st_tspec == WCHAR && t == WCHAR))) {
1054 debug_leave();
1055 return false;
1056 }
1057 /* XXX: duplicate code, see above */
1058 /*
1059 * If the array is already partly initialized, we are
1060 * wrong here.
1061 */
1062 if (istk->i_remaining != istk->i_type->t_dim)
1063 debug_leave();
1064 return false;
1065 } else {
1066 debug_leave();
1067 return false;
1068 }
1069
1070 /* Get length without trailing NUL character. */
1071 len = strg->st_len;
1072
1073 if (istk->i_array_of_unknown_size) {
1074 istk->i_array_of_unknown_size = false;
1075 istk->i_type->t_dim = len + 1;
1076 setcomplete(istk->i_type, true);
1077 } else {
1078 if (istk->i_type->t_dim < len) {
1079 /* non-null byte ignored in string initializer */
1080 warning(187);
1081 }
1082 }
1083
1084 /* In every case the array is initialized completely. */
1085 istk->i_remaining = 0;
1086
1087 debug_initstack();
1088 debug_leave();
1089 return true;
1090 }
1091