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