init.c revision 1.107 1 /* $NetBSD: init.c,v 1.107 2021/03/20 08:16:30 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.107 2021/03/20 08:16:30 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 if (namedmem == NULL) {
347 /*
348 * XXX: Why is this a circular list?
349 * XXX: Why is this a doubly-linked list?
350 * A simple stack should suffice.
351 */
352 nam->n_prev = nam->n_next = nam;
353 namedmem = nam;
354 } else {
355 namedmem->n_prev->n_next = nam;
356 nam->n_prev = namedmem->n_prev;
357 nam->n_next = namedmem;
358 namedmem->n_prev = nam;
359 }
360
361 debug_named_member();
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_shift_name(void)
382 {
383 if (namedmem->n_next == namedmem) {
384 free(namedmem);
385 namedmem = NULL;
386 } else {
387 namlist_t *nam = namedmem;
388 namedmem = namedmem->n_next;
389 nam->n_prev->n_next = nam->n_next;
390 nam->n_next->n_prev = nam->n_prev;
391 free(nam);
392 }
393
394 debug_named_member();
395 }
396
397 /*
398 * Initialize the initialization stack by putting an entry for the object
399 * which is to be initialized on it.
400 */
401 void
402 initstack_init(void)
403 {
404 initstack_element *istk;
405
406 if (initerr)
407 return;
408
409 /* free memory used in last initialization */
410 while ((istk = initstk) != NULL) {
411 initstk = istk->i_enclosing;
412 free(istk);
413 }
414
415 debug_enter();
416
417 /*
418 * If the type which is to be initialized is an incomplete array,
419 * it must be duplicated.
420 */
421 if (initsym->s_type->t_tspec == ARRAY && is_incomplete(initsym->s_type))
422 initsym->s_type = duptyp(initsym->s_type);
423
424 istk = initstk = xcalloc(1, sizeof (initstack_element));
425 istk->i_subt = initsym->s_type;
426 istk->i_remaining = 1;
427
428 debug_initstack();
429 debug_leave();
430 }
431
432 static void
433 initstack_pop_item_named_member(void)
434 {
435 initstack_element *istk = initstk;
436 sym_t *m;
437
438 debug_step("initializing named member '%s'", namedmem->n_name);
439
440 if (istk->i_type->t_tspec != STRUCT &&
441 istk->i_type->t_tspec != UNION) {
442 /* syntax error '%s' */
443 error(249, "named member must only be used with struct/union");
444 initerr = true;
445 return;
446 }
447
448 for (m = istk->i_type->t_str->sou_first_member;
449 m != NULL; m = m->s_next) {
450
451 if (m->s_bitfield && m->s_name == unnamed)
452 continue;
453
454 if (strcmp(m->s_name, namedmem->n_name) == 0) {
455 debug_step("found matching member");
456 istk->i_subt = m->s_type;
457 /* XXX: why ++? */
458 istk->i_remaining++;
459 /* XXX: why is i_seen_named_member not set? */
460 designator_shift_name();
461 return;
462 }
463 }
464
465 /* undefined struct/union member: %s */
466 error(101, namedmem->n_name);
467
468 designator_shift_name();
469 istk->i_seen_named_member = true;
470 }
471
472 static void
473 initstack_pop_item_unnamed(void)
474 {
475 initstack_element *istk = initstk;
476 sym_t *m;
477
478 /*
479 * If the removed element was a structure member, we must go
480 * to the next structure member.
481 */
482 if (istk->i_remaining > 0 && istk->i_type->t_tspec == STRUCT &&
483 !istk->i_seen_named_member) {
484 do {
485 m = istk->i_current_object =
486 istk->i_current_object->s_next;
487 /* XXX: can this assertion be made to fail? */
488 lint_assert(m != NULL);
489 debug_step("pop %s", m->s_name);
490 } while (m->s_bitfield && m->s_name == unnamed);
491 /* XXX: duplicate code for skipping unnamed bit-fields */
492 istk->i_subt = m->s_type;
493 }
494 }
495
496 static void
497 initstack_pop_item(void)
498 {
499 initstack_element *istk;
500
501 debug_enter();
502
503 istk = initstk;
504 debug_step("popping:");
505 debug_initstack_element(istk);
506
507 initstk = istk->i_enclosing;
508 free(istk);
509 istk = initstk;
510 lint_assert(istk != NULL);
511
512 istk->i_remaining--;
513 lint_assert(istk->i_remaining >= 0);
514 debug_step("%d elements remaining", istk->i_remaining);
515
516 if (namedmem != NULL)
517 initstack_pop_item_named_member();
518 else
519 initstack_pop_item_unnamed();
520
521 debug_initstack();
522 debug_leave();
523 }
524
525 /*
526 * Take all entries, including the first which requires a closing brace,
527 * from the stack.
528 */
529 static void
530 initstack_pop_brace(void)
531 {
532 bool brace;
533
534 debug_enter();
535 debug_initstack();
536 do {
537 brace = initstk->i_brace;
538 debug_step("loop brace=%d", brace);
539 initstack_pop_item();
540 } while (!brace);
541 debug_initstack();
542 debug_leave();
543 }
544
545 /*
546 * Take all entries which cannot be used for further initializers from the
547 * stack, but do this only if they do not require a closing brace.
548 */
549 static void
550 initstack_pop_nobrace(void)
551 {
552
553 debug_enter();
554 while (!initstk->i_brace && initstk->i_remaining == 0 &&
555 !initstk->i_array_of_unknown_size)
556 initstack_pop_item();
557 debug_leave();
558 }
559
560 /* Extend an array of unknown size by one element */
561 static void
562 extend_if_array_of_unknown_size(void)
563 {
564 initstack_element *istk = initstk;
565
566 if (istk->i_remaining != 0)
567 return;
568
569 /*
570 * The only place where an incomplete array may appear is at the
571 * outermost aggregate level of the object to be initialized.
572 */
573 lint_assert(istk->i_enclosing->i_enclosing == NULL);
574 lint_assert(istk->i_type->t_tspec == ARRAY);
575
576 debug_step("extending array of unknown size '%s'",
577 type_name(istk->i_type));
578 istk->i_remaining = 1;
579 istk->i_type->t_dim++;
580 setcomplete(istk->i_type, true);
581
582 debug_step("extended type is '%s'", type_name(istk->i_type));
583 }
584
585 static void
586 initstack_push_array(void)
587 {
588 initstack_element *const istk = initstk;
589
590 if (istk->i_enclosing->i_seen_named_member) {
591 istk->i_brace = true;
592 debug_step("ARRAY brace=%d, namedmem=%d",
593 istk->i_brace, istk->i_enclosing->i_seen_named_member);
594 }
595
596 if (is_incomplete(istk->i_type) &&
597 istk->i_enclosing->i_enclosing != NULL) {
598 /* initialization of an incomplete type */
599 error(175);
600 initerr = true;
601 return;
602 }
603
604 istk->i_subt = istk->i_type->t_subt;
605 istk->i_array_of_unknown_size = is_incomplete(istk->i_type);
606 istk->i_remaining = istk->i_type->t_dim;
607 debug_named_member();
608 debug_step("type '%s' remaining %d",
609 type_name(istk->i_type), istk->i_remaining);
610 }
611
612 static bool
613 initstack_push_struct_or_union(void)
614 {
615 initstack_element *const istk = initstk;
616 int cnt;
617 sym_t *m;
618
619 if (is_incomplete(istk->i_type)) {
620 /* initialization of an incomplete type */
621 error(175);
622 initerr = true;
623 return false;
624 }
625
626 cnt = 0;
627 debug_named_member();
628 debug_step("lookup for '%s'%s",
629 type_name(istk->i_type),
630 istk->i_seen_named_member ? ", seen named member" : "");
631
632 for (m = istk->i_type->t_str->sou_first_member;
633 m != NULL; m = m->s_next) {
634 if (m->s_bitfield && m->s_name == unnamed)
635 continue;
636 if (namedmem != NULL) {
637 debug_step("have member '%s', want member '%s'",
638 m->s_name, namedmem->n_name);
639 if (strcmp(m->s_name, namedmem->n_name) == 0) {
640 cnt++;
641 break;
642 } else
643 continue;
644 }
645 if (++cnt == 1) {
646 istk->i_current_object = m;
647 istk->i_subt = m->s_type;
648 }
649 }
650
651 if (namedmem != NULL) {
652 if (m == NULL) {
653 debug_step("pop struct");
654 return true;
655 }
656 istk->i_current_object = m;
657 istk->i_subt = m->s_type;
658 istk->i_seen_named_member = true;
659 debug_step("named member '%s'", namedmem->n_name);
660 designator_shift_name();
661 cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
662 }
663 istk->i_brace = true;
664 debug_step("unnamed element with type '%s'%s",
665 type_name(istk->i_type != NULL ? istk->i_type : istk->i_subt),
666 istk->i_brace ? ", needs closing brace" : "");
667 if (cnt == 0) {
668 /* cannot init. struct/union with no named member */
669 error(179);
670 initerr = true;
671 return false;
672 }
673 istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
674 return false;
675 }
676
677 static void
678 initstack_push(void)
679 {
680 initstack_element *istk, *inxt;
681
682 debug_enter();
683
684 extend_if_array_of_unknown_size();
685
686 istk = initstk;
687 lint_assert(istk->i_remaining > 0);
688 lint_assert(istk->i_type == NULL || !is_scalar(istk->i_type->t_tspec));
689
690 initstk = xcalloc(1, sizeof (initstack_element));
691 initstk->i_enclosing = istk;
692 initstk->i_type = istk->i_subt;
693 lint_assert(initstk->i_type->t_tspec != FUNC);
694
695 again:
696 istk = initstk;
697
698 debug_step("expecting type '%s'", type_name(istk->i_type));
699 lint_assert(istk->i_type != NULL);
700 switch (istk->i_type->t_tspec) {
701 case ARRAY:
702 if (namedmem != NULL) {
703 debug_step("pop array namedmem=%s brace=%d",
704 namedmem->n_name, istk->i_brace);
705 goto pop;
706 }
707
708 initstack_push_array();
709 break;
710
711 case UNION:
712 if (tflag)
713 /* initialization of union is illegal in trad. C */
714 warning(238);
715 /* FALLTHROUGH */
716 case STRUCT:
717 if (initstack_push_struct_or_union())
718 goto pop;
719 break;
720 default:
721 if (namedmem != NULL) {
722 debug_step("pop scalar");
723 pop:
724 inxt = initstk->i_enclosing;
725 free(istk);
726 initstk = inxt;
727 goto again;
728 }
729 /* The initialization stack now expects a single scalar. */
730 istk->i_remaining = 1;
731 break;
732 }
733
734 debug_initstack();
735 debug_leave();
736 }
737
738 static void
739 check_too_many_initializers(void)
740 {
741
742 const initstack_element *istk = initstk;
743 if (istk->i_remaining > 0)
744 return;
745 if (istk->i_array_of_unknown_size || istk->i_seen_named_member)
746 return;
747
748 tspec_t t = istk->i_type->t_tspec;
749 if (t == ARRAY) {
750 /* too many array initializers, expected %d */
751 error(173, istk->i_type->t_dim);
752 } else if (t == STRUCT || t == UNION) {
753 /* too many struct/union initializers */
754 error(172);
755 } else {
756 /* too many initializers */
757 error(174);
758 }
759 initerr = true;
760 }
761
762 /*
763 * Process a '{' in an initializer by starting the initialization of the
764 * nested data structure, with i_type being the i_subt of the outer
765 * initialization level.
766 */
767 static void
768 initstack_next_brace(void)
769 {
770
771 debug_enter();
772 debug_initstack();
773
774 if (initstk->i_type != NULL && is_scalar(initstk->i_type->t_tspec)) {
775 /* invalid initializer type %s */
776 error(176, type_name(initstk->i_type));
777 initerr = true;
778 }
779 if (!initerr)
780 check_too_many_initializers();
781 if (!initerr)
782 initstack_push();
783 if (!initerr) {
784 initstk->i_brace = true;
785 debug_named_member();
786 debug_step("expecting type '%s'",
787 type_name(initstk->i_type != NULL ? initstk->i_type
788 : initstk->i_subt));
789 }
790
791 debug_initstack();
792 debug_leave();
793 }
794
795 static void
796 initstack_next_nobrace(void)
797 {
798 debug_enter();
799
800 if (initstk->i_type == NULL && !is_scalar(initstk->i_subt->t_tspec)) {
801 /* {}-enclosed initializer required */
802 error(181);
803 /* XXX: maybe set initerr here */
804 }
805
806 if (!initerr)
807 check_too_many_initializers();
808
809 /*
810 * Make sure an entry with a scalar type is at the top of the stack.
811 *
812 * FIXME: Since C99, an initializer for an object with automatic
813 * storage need not be a constant expression anymore. It is
814 * perfectly fine to initialize a struct with a struct expression,
815 * see d_struct_init_nested.c for a demonstration.
816 */
817 while (!initerr) {
818 if ((initstk->i_type != NULL &&
819 is_scalar(initstk->i_type->t_tspec)))
820 break;
821 initstack_push();
822 }
823
824 debug_initstack();
825 debug_leave();
826 }
827
828 void
829 init_lbrace(void)
830 {
831 if (initerr)
832 return;
833
834 debug_enter();
835 debug_initstack();
836
837 if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
838 initstk->i_enclosing == NULL) {
839 if (tflag && !is_scalar(initstk->i_subt->t_tspec))
840 /* no automatic aggregate initialization in trad. C */
841 warning(188);
842 }
843
844 /*
845 * Remove all entries which cannot be used for further initializers
846 * and do not expect a closing brace.
847 */
848 initstack_pop_nobrace();
849
850 initstack_next_brace();
851
852 debug_initstack();
853 debug_leave();
854 }
855
856 /*
857 * Process a '}' in an initializer by finishing the current level of the
858 * initialization stack.
859 */
860 void
861 init_rbrace(void)
862 {
863 if (initerr)
864 return;
865
866 debug_enter();
867 initstack_pop_brace();
868 debug_leave();
869 }
870
871 /* In traditional C, bit-fields can be initialized only by integer constants. */
872 static void
873 check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
874 {
875 if (tflag &&
876 is_integer(lt) &&
877 ln->tn_type->t_bitfield &&
878 !is_integer(rt)) {
879 /* bit-field initialization is illegal in traditional C */
880 warning(186);
881 }
882 }
883
884 static void
885 check_non_constant_initializer(const tnode_t *tn, scl_t sclass)
886 {
887 if (tn == NULL || tn->tn_op == CON)
888 return;
889
890 sym_t *sym;
891 ptrdiff_t offs;
892 if (constant_addr(tn, &sym, &offs))
893 return;
894
895 if (sclass == AUTO || sclass == REG) {
896 /* non-constant initializer */
897 c99ism(177);
898 } else {
899 /* non-constant initializer */
900 error(177);
901 }
902 }
903
904 void
905 init_using_expr(tnode_t *tn)
906 {
907 tspec_t lt, rt;
908 tnode_t *ln;
909 struct mbl *tmem;
910 scl_t sclass;
911
912 debug_enter();
913 debug_initstack();
914 debug_named_member();
915 debug_step("expr:");
916 debug_node(tn, debug_ind + 1);
917
918 if (initerr || tn == NULL) {
919 debug_leave();
920 return;
921 }
922
923 sclass = initsym->s_scl;
924
925 /*
926 * Do not test for automatic aggregate initialization. If the
927 * initializer starts with a brace we have the warning already.
928 * If not, an error will be printed that the initializer must
929 * be enclosed by braces.
930 */
931
932 /*
933 * Local initialization of non-array-types with only one expression
934 * without braces is done by ASSIGN
935 */
936 if ((sclass == AUTO || sclass == REG) &&
937 initsym->s_type->t_tspec != ARRAY && initstk->i_enclosing == NULL) {
938 debug_step("handing over to ASSIGN");
939 ln = new_name_node(initsym, 0);
940 ln->tn_type = tduptyp(ln->tn_type);
941 ln->tn_type->t_const = false;
942 tn = build(ASSIGN, ln, tn);
943 expr(tn, false, false, false, false);
944 /* XXX: why not clean up the initstack here already? */
945 debug_leave();
946 return;
947 }
948
949 initstack_pop_nobrace();
950
951 if (init_array_using_string(tn)) {
952 debug_step("after initializing the string:");
953 /* XXX: why not clean up the initstack here already? */
954 debug_initstack();
955 debug_leave();
956 return;
957 }
958
959 initstack_next_nobrace();
960 if (initerr || tn == NULL) {
961 debug_initstack();
962 debug_leave();
963 return;
964 }
965
966 initstk->i_remaining--;
967 debug_step("%d elements remaining", initstk->i_remaining);
968
969 /* Create a temporary node for the left side. */
970 ln = tgetblk(sizeof (tnode_t));
971 ln->tn_op = NAME;
972 ln->tn_type = tduptyp(initstk->i_type);
973 ln->tn_type->t_const = false;
974 ln->tn_lvalue = true;
975 ln->tn_sym = initsym; /* better than nothing */
976
977 tn = cconv(tn);
978
979 lt = ln->tn_type->t_tspec;
980 rt = tn->tn_type->t_tspec;
981
982 lint_assert(is_scalar(lt)); /* at least before C99 */
983
984 debug_step("typeok '%s', '%s'",
985 type_name(ln->tn_type), type_name(tn->tn_type));
986 if (!typeok(INIT, 0, ln, tn)) {
987 debug_initstack();
988 debug_leave();
989 return;
990 }
991
992 /*
993 * Store the tree memory. This is necessary because otherwise
994 * expr() would free it.
995 */
996 tmem = tsave();
997 expr(tn, true, false, true, false);
998 trestor(tmem);
999
1000 check_bit_field_init(ln, lt, rt);
1001
1002 /*
1003 * XXX: Is it correct to do this conversion _after_ the typeok above?
1004 */
1005 if (lt != rt || (initstk->i_type->t_bitfield && tn->tn_op == CON))
1006 tn = convert(INIT, 0, initstk->i_type, tn);
1007
1008 check_non_constant_initializer(tn, sclass);
1009
1010 debug_initstack();
1011 debug_leave();
1012 }
1013
1014
1015 /* Initialize a character array or wchar_t array with a string literal. */
1016 static bool
1017 init_array_using_string(tnode_t *tn)
1018 {
1019 tspec_t t;
1020 initstack_element *istk;
1021 int len;
1022 strg_t *strg;
1023
1024 if (tn->tn_op != STRING)
1025 return false;
1026
1027 debug_enter();
1028 debug_initstack();
1029
1030 istk = initstk;
1031 strg = tn->tn_string;
1032
1033 /*
1034 * Check if we have an array type which can be initialized by
1035 * the string.
1036 */
1037 if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
1038 debug_step("subt array");
1039 t = istk->i_subt->t_subt->t_tspec;
1040 if (!((strg->st_tspec == CHAR &&
1041 (t == CHAR || t == UCHAR || t == SCHAR)) ||
1042 (strg->st_tspec == WCHAR && t == WCHAR))) {
1043 debug_leave();
1044 return false;
1045 }
1046 /* XXX: duplicate code, see below */
1047 /* Put the array at top of stack */
1048 initstack_push();
1049 istk = initstk;
1050 } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
1051 debug_step("type array");
1052 t = istk->i_type->t_subt->t_tspec;
1053 if (!((strg->st_tspec == CHAR &&
1054 (t == CHAR || t == UCHAR || t == SCHAR)) ||
1055 (strg->st_tspec == WCHAR && t == WCHAR))) {
1056 debug_leave();
1057 return false;
1058 }
1059 /* XXX: duplicate code, see above */
1060 /*
1061 * If the array is already partly initialized, we are
1062 * wrong here.
1063 */
1064 if (istk->i_remaining != istk->i_type->t_dim)
1065 debug_leave();
1066 return false;
1067 } else {
1068 debug_leave();
1069 return false;
1070 }
1071
1072 /* Get length without trailing NUL character. */
1073 len = strg->st_len;
1074
1075 if (istk->i_array_of_unknown_size) {
1076 istk->i_array_of_unknown_size = false;
1077 istk->i_type->t_dim = len + 1;
1078 setcomplete(istk->i_type, true);
1079 } else {
1080 if (istk->i_type->t_dim < len) {
1081 /* non-null byte ignored in string initializer */
1082 warning(187);
1083 }
1084 }
1085
1086 /* In every case the array is initialized completely. */
1087 istk->i_remaining = 0;
1088
1089 debug_initstack();
1090 debug_leave();
1091 return true;
1092 }
1093