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