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