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