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