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