init.c revision 1.77 1 /* $NetBSD: init.c,v 1.77 2021/02/21 08:01:14 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.77 2021/02/21 08:01:14 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 istk {
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 (XXX: and unions?), the next member to be initialized
78 * by an initializer 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 istk *i_enclosing;
97 } istk_t;
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 istk_t *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 const char *
255 noyes(bool cond)
256 {
257 return cond ? "yes" : "no";
258 }
259
260 static void
261 debug_initstack(void)
262 {
263 if (initstk == NULL) {
264 debug_step("initstk is empty");
265 return;
266 }
267
268 size_t i = 0;
269 for (const istk_t *elem = initstk;
270 elem != NULL; elem = elem->i_enclosing) {
271 debug_step("initstk[%zu]:", i);
272 debug_step(" i_type = %s", type_name(elem->i_type));
273 debug_step(" i_subt = %s", type_name(elem->i_subt));
274 debug_step(" flags =%s%s%s%s",
275 elem->i_brace
276 ? " brace" : "",
277 elem->i_array_of_unknown_size
278 ? "array_of_unknown_size" : "",
279 elem->i_seen_named_member ? "seen_named_member" : "",
280 !(elem->i_brace || elem->i_array_of_unknown_size ||
281 elem->i_seen_named_member)
282 ? " none" : "");
283 debug_step(" i_current_object = %s",
284 elem->i_current_object != NULL
285 ? elem->i_current_object->s_name
286 : "(null)");
287 debug_step(" i_remaining = %d", elem->i_remaining);
288 i++;
289 }
290 }
291 #else
292 #define debug_initstack() do { } while (false)
293 #endif
294
295 /*
296 * Initialize the initialisation stack by putting an entry for the object
297 * which is to be initialized on it.
298 */
299 void
300 initstack_init(void)
301 {
302 istk_t *istk;
303
304 if (initerr)
305 return;
306
307 /* free memory used in last initialisation */
308 while ((istk = initstk) != NULL) {
309 initstk = istk->i_enclosing;
310 free(istk);
311 }
312
313 debug_enter();
314
315 /*
316 * If the type which is to be initialized is an incomplete array,
317 * it must be duplicated.
318 */
319 if (initsym->s_type->t_tspec == ARRAY && is_incomplete(initsym->s_type))
320 initsym->s_type = duptyp(initsym->s_type);
321
322 istk = initstk = xcalloc(1, sizeof (istk_t));
323 istk->i_subt = initsym->s_type;
324 istk->i_remaining = 1;
325
326 debug_initstack();
327 debug_leave();
328 }
329
330 static void
331 initstack_pop_item(void)
332 {
333 istk_t *istk;
334 sym_t *m;
335
336 debug_enter();
337
338 istk = initstk;
339 debug_step("pop type=%s, brace=%d remaining=%d named=%d",
340 type_name(istk->i_type != NULL ? istk->i_type : istk->i_subt),
341 istk->i_brace, istk->i_remaining, istk->i_seen_named_member);
342
343 initstk = istk->i_enclosing;
344 free(istk);
345 istk = initstk;
346 lint_assert(istk != NULL);
347
348 debug_step("top type=%s, brace=%d remaining=%d named=%d",
349 type_name(istk->i_type != NULL ? istk->i_type : istk->i_subt),
350 istk->i_brace, istk->i_remaining, istk->i_seen_named_member);
351
352 istk->i_remaining--;
353 lint_assert(istk->i_remaining >= 0);
354
355 debug_step("top remaining=%d rhs.name=%s",
356 istk->i_remaining, namedmem != NULL ? namedmem->n_name : "*null*");
357
358 if (istk->i_remaining >= 0 && namedmem != NULL) {
359
360 debug_step("named remaining=%d type=%s, rhs.name=%s",
361 istk->i_remaining, type_name(istk->i_type),
362 namedmem->n_name);
363
364 for (m = istk->i_type->t_str->sou_first_member;
365 m != NULL; m = m->s_next) {
366 debug_step("pop lhs.name=%s rhs.name=%s",
367 m->s_name, namedmem->n_name);
368 if (m->s_bitfield && m->s_name == unnamed)
369 continue;
370 if (strcmp(m->s_name, namedmem->n_name) == 0) {
371 istk->i_subt = m->s_type;
372 istk->i_remaining++;
373 pop_member();
374 debug_initstack();
375 debug_leave();
376 return;
377 }
378 }
379 /* undefined struct/union member: %s */
380 error(101, namedmem->n_name);
381 debug_step("end rhs.name=%s", namedmem->n_name);
382 pop_member();
383 istk->i_seen_named_member = true;
384 debug_initstack();
385 debug_leave();
386 return;
387 }
388 /*
389 * If the removed element was a structure member, we must go
390 * to the next structure member.
391 */
392 if (istk->i_remaining > 0 && istk->i_type->t_tspec == STRUCT &&
393 !istk->i_seen_named_member) {
394 do {
395 m = istk->i_current_object =
396 istk->i_current_object->s_next;
397 lint_assert(m != NULL);
398 debug_step("pop %s", m->s_name);
399 } while (m->s_bitfield && m->s_name == unnamed);
400 istk->i_subt = m->s_type;
401 }
402 debug_initstack();
403 debug_leave();
404 }
405
406 /*
407 * Take all entries, including the first which requires a closing brace,
408 * from the stack.
409 */
410 static void
411 initstack_pop_brace(void)
412 {
413 bool brace;
414
415 debug_enter();
416 debug_initstack();
417 do {
418 brace = initstk->i_brace;
419 debug_step("loop brace=%d", brace);
420 initstack_pop_item();
421 } while (!brace);
422 debug_initstack();
423 debug_leave();
424 }
425
426 /*
427 * Take all entries which cannot be used for further initializers from the
428 * stack, but do this only if they do not require a closing brace.
429 */
430 static void
431 initstack_pop_nobrace(void)
432 {
433
434 debug_enter();
435 debug_initstack();
436 while (!initstk->i_brace && initstk->i_remaining == 0 &&
437 !initstk->i_array_of_unknown_size)
438 initstack_pop_item();
439 debug_initstack();
440 debug_leave();
441 }
442
443 static void
444 initstack_push(void)
445 {
446 istk_t *istk, *inxt;
447 int cnt;
448 sym_t *m;
449
450 debug_enter();
451 debug_initstack();
452
453 istk = initstk;
454
455 /* Extend an incomplete array type by one element */
456 if (istk->i_remaining == 0) {
457 debug_step("(extend) %s", type_name(istk->i_type));
458 /*
459 * Inside of other aggregate types must not be an incomplete
460 * type.
461 */
462 lint_assert(istk->i_enclosing->i_enclosing == NULL);
463 istk->i_remaining = 1;
464 lint_assert(istk->i_type->t_tspec == ARRAY);
465 istk->i_type->t_dim++;
466 setcomplete(istk->i_type, true);
467 }
468
469 lint_assert(istk->i_remaining > 0);
470 lint_assert(istk->i_type == NULL || !is_scalar(istk->i_type->t_tspec));
471
472 initstk = xcalloc(1, sizeof (istk_t));
473 initstk->i_enclosing = istk;
474 initstk->i_type = istk->i_subt;
475 lint_assert(initstk->i_type->t_tspec != FUNC);
476
477 again:
478 istk = initstk;
479
480 debug_step("typename %s", type_name(istk->i_type));
481 switch (istk->i_type->t_tspec) {
482 case ARRAY:
483 if (namedmem != NULL) {
484 debug_step("ARRAY %s brace=%d",
485 namedmem->n_name, istk->i_brace);
486 goto pop;
487 } else if (istk->i_enclosing->i_seen_named_member) {
488 istk->i_brace = true;
489 debug_step("ARRAY brace=%d, namedmem=%d",
490 istk->i_brace,
491 istk->i_enclosing->i_seen_named_member);
492 }
493
494 if (is_incomplete(istk->i_type) &&
495 istk->i_enclosing->i_enclosing != NULL) {
496 /* initialisation of an incomplete type */
497 error(175);
498 initerr = true;
499 debug_initstack();
500 debug_leave();
501 return;
502 }
503 istk->i_subt = istk->i_type->t_subt;
504 istk->i_array_of_unknown_size = is_incomplete(istk->i_type);
505 istk->i_remaining = istk->i_type->t_dim;
506 debug_step("elements array %s[%d] %s",
507 type_name(istk->i_subt), istk->i_remaining,
508 namedmem != NULL ? namedmem->n_name : "*none*");
509 break;
510 case UNION:
511 if (tflag)
512 /* initialisation of union is illegal in trad. C */
513 warning(238);
514 /* FALLTHROUGH */
515 case STRUCT:
516 if (is_incomplete(istk->i_type)) {
517 /* initialisation of an incomplete type */
518 error(175);
519 initerr = true;
520 debug_initstack();
521 debug_leave();
522 return;
523 }
524 cnt = 0;
525 debug_step("lookup type=%s, name=%s named=%d",
526 type_name(istk->i_type),
527 namedmem != NULL ? namedmem->n_name : "*none*",
528 istk->i_seen_named_member);
529 for (m = istk->i_type->t_str->sou_first_member;
530 m != NULL; m = m->s_next) {
531 if (m->s_bitfield && m->s_name == unnamed)
532 continue;
533 if (namedmem != NULL) {
534 debug_step("named lhs.member=%s, rhs.member=%s",
535 m->s_name, namedmem->n_name);
536 if (strcmp(m->s_name, namedmem->n_name) == 0) {
537 cnt++;
538 break;
539 } else
540 continue;
541 }
542 if (++cnt == 1) {
543 istk->i_current_object = m;
544 istk->i_subt = m->s_type;
545 }
546 }
547 if (namedmem != NULL) {
548 if (m == NULL) {
549 debug_step("pop struct");
550 goto pop;
551 }
552 istk->i_current_object = m;
553 istk->i_subt = m->s_type;
554 istk->i_seen_named_member = true;
555 debug_step("named name=%s", namedmem->n_name);
556 pop_member();
557 cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
558 }
559 istk->i_brace = true;
560 debug_step("unnamed type=%s, brace=%d",
561 type_name(
562 istk->i_type != NULL ? istk->i_type : istk->i_subt),
563 istk->i_brace);
564 if (cnt == 0) {
565 /* cannot init. struct/union with no named member */
566 error(179);
567 initerr = true;
568 debug_initstack();
569 debug_leave();
570 return;
571 }
572 istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
573 break;
574 default:
575 if (namedmem != NULL) {
576 debug_step("pop");
577 pop:
578 inxt = initstk->i_enclosing;
579 free(istk);
580 initstk = inxt;
581 goto again;
582 }
583 istk->i_remaining = 1;
584 break;
585 }
586
587 debug_initstack();
588 debug_leave();
589 }
590
591 static void
592 initstack_check_too_many(void)
593 {
594 istk_t *istk;
595
596 istk = initstk;
597
598 /*
599 * If a closing brace is expected we have at least one initializer
600 * too much.
601 */
602 if (istk->i_remaining == 0 && !istk->i_array_of_unknown_size &&
603 !istk->i_seen_named_member) {
604 switch (istk->i_type->t_tspec) {
605 case ARRAY:
606 /* too many array initializers, expected %d */
607 error(173, istk->i_type->t_dim);
608 break;
609 case STRUCT:
610 case UNION:
611 /* too many struct/union initializers */
612 error(172);
613 break;
614 default:
615 /* too many initializers */
616 error(174);
617 break;
618 }
619 initerr = true;
620 }
621 }
622
623 static void
624 initstack_next_brace(void)
625 {
626
627 debug_enter();
628 debug_initstack();
629
630 if (initstk->i_type != NULL && is_scalar(initstk->i_type->t_tspec)) {
631 /* invalid initializer type %s */
632 error(176, type_name(initstk->i_type));
633 initerr = true;
634 }
635 if (!initerr)
636 initstack_check_too_many();
637 if (!initerr)
638 initstack_push();
639 if (!initerr) {
640 initstk->i_brace = true;
641 debug_step("%p %s", namedmem, type_name(
642 initstk->i_type != NULL ? initstk->i_type
643 : initstk->i_subt));
644 }
645
646 debug_initstack();
647 debug_leave();
648 }
649
650 static void
651 initstack_next_nobrace(void)
652 {
653 debug_enter();
654 debug_initstack();
655
656 if (initstk->i_type == NULL && !is_scalar(initstk->i_subt->t_tspec)) {
657 /* {}-enclosed initializer required */
658 error(181);
659 }
660
661 /*
662 * Make sure an entry with a scalar type is at the top of the stack.
663 *
664 * FIXME: Since C99 an initializer for an object with automatic
665 * storage need not be a constant expression anymore. It is
666 * perfectly fine to initialize a struct with a struct expression,
667 * see d_struct_init_nested.c for a demonstration.
668 */
669 if (!initerr)
670 initstack_check_too_many();
671 while (!initerr) {
672 if ((initstk->i_type != NULL &&
673 is_scalar(initstk->i_type->t_tspec)))
674 break;
675 initstack_push();
676 }
677
678 debug_initstack();
679 debug_leave();
680 }
681
682 void
683 init_lbrace(void)
684 {
685 if (initerr)
686 return;
687
688 debug_enter();
689 debug_initstack();
690
691 if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
692 initstk->i_enclosing == NULL) {
693 if (tflag && !is_scalar(initstk->i_subt->t_tspec))
694 /* no automatic aggregate initialization in trad. C */
695 warning(188);
696 }
697
698 /*
699 * Remove all entries which cannot be used for further initializers
700 * and do not expect a closing brace.
701 */
702 initstack_pop_nobrace();
703
704 initstack_next_brace();
705
706 debug_initstack();
707 debug_leave();
708 }
709
710 void
711 init_rbrace(void)
712 {
713 if (initerr)
714 return;
715
716 debug_enter();
717 debug_initstack();
718
719 initstack_pop_brace();
720
721 debug_initstack();
722 debug_leave();
723 }
724
725 void
726 init_using_expr(tnode_t *tn)
727 {
728 ptrdiff_t offs;
729 sym_t *sym;
730 tspec_t lt, rt;
731 tnode_t *ln;
732 struct mbl *tmem;
733 scl_t sc;
734
735 debug_enter();
736 debug_named_member();
737 debug_node(tn, debug_ind);
738 debug_initstack();
739
740 if (initerr || tn == NULL) {
741 debug_leave();
742 return;
743 }
744
745 sc = initsym->s_scl;
746
747 /*
748 * Do not test for automatic aggregate initialisation. If the
749 * initializer starts with a brace we have the warning already.
750 * If not, an error will be printed that the initializer must
751 * be enclosed by braces.
752 */
753
754 /*
755 * Local initialisation of non-array-types with only one expression
756 * without braces is done by ASSIGN
757 */
758 if ((sc == AUTO || sc == REG) &&
759 initsym->s_type->t_tspec != ARRAY && initstk->i_enclosing == NULL) {
760 ln = new_name_node(initsym, 0);
761 ln->tn_type = tduptyp(ln->tn_type);
762 ln->tn_type->t_const = false;
763 tn = build(ASSIGN, ln, tn);
764 expr(tn, false, false, false, false);
765 debug_initstack();
766 debug_leave();
767 return;
768 }
769
770 /*
771 * Remove all entries which cannot be used for further initializers
772 * and do not require a closing brace.
773 */
774 initstack_pop_nobrace();
775
776 /* Initialisations by strings are done in initstack_string(). */
777 if (initstack_string(tn)) {
778 debug_initstack();
779 debug_leave();
780 return;
781 }
782
783 initstack_next_nobrace();
784 if (initerr || tn == NULL) {
785 debug_initstack();
786 debug_leave();
787 return;
788 }
789
790 initstk->i_remaining--;
791 debug_step("remaining=%d tn=%p", initstk->i_remaining, tn);
792 /* Create a temporary node for the left side. */
793 ln = tgetblk(sizeof (tnode_t));
794 ln->tn_op = NAME;
795 ln->tn_type = tduptyp(initstk->i_type);
796 ln->tn_type->t_const = false;
797 ln->tn_lvalue = true;
798 ln->tn_sym = initsym; /* better than nothing */
799
800 tn = cconv(tn);
801
802 lt = ln->tn_type->t_tspec;
803 rt = tn->tn_type->t_tspec;
804
805 lint_assert(is_scalar(lt));
806
807 if (!typeok(INIT, 0, ln, tn)) {
808 debug_initstack();
809 debug_leave();
810 return;
811 }
812
813 /*
814 * Store the tree memory. This is necessary because otherwise
815 * expr() would free it.
816 */
817 tmem = tsave();
818 expr(tn, true, false, true, false);
819 trestor(tmem);
820
821 if (is_integer(lt) && ln->tn_type->t_bitfield && !is_integer(rt)) {
822 /*
823 * Bit-fields can be initialized in trad. C only by integer
824 * constants.
825 */
826 if (tflag)
827 /* bit-field initialisation is illegal in trad. C */
828 warning(186);
829 }
830
831 if (lt != rt || (initstk->i_type->t_bitfield && tn->tn_op == CON))
832 tn = convert(INIT, 0, initstk->i_type, tn);
833
834 if (tn != NULL && tn->tn_op != CON) {
835 sym = NULL;
836 offs = 0;
837 if (!constant_addr(tn, &sym, &offs)) {
838 if (sc == AUTO || sc == REG) {
839 /* non-constant initializer */
840 c99ism(177);
841 } else {
842 /* non-constant initializer */
843 error(177);
844 }
845 }
846 }
847
848 debug_initstack();
849 debug_leave();
850 }
851
852
853 static bool
854 initstack_string(tnode_t *tn)
855 {
856 tspec_t t;
857 istk_t *istk;
858 int len;
859 strg_t *strg;
860
861 if (tn->tn_op != STRING)
862 return false;
863
864 debug_enter();
865 debug_initstack();
866
867 istk = initstk;
868 strg = tn->tn_string;
869
870 /*
871 * Check if we have an array type which can be initialized by
872 * the string.
873 */
874 if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
875 debug_step("subt array");
876 t = istk->i_subt->t_subt->t_tspec;
877 if (!((strg->st_tspec == CHAR &&
878 (t == CHAR || t == UCHAR || t == SCHAR)) ||
879 (strg->st_tspec == WCHAR && t == WCHAR))) {
880 debug_leave();
881 return false;
882 }
883 /* Put the array at top of stack */
884 initstack_push();
885 istk = initstk;
886 } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
887 debug_step("type array");
888 t = istk->i_type->t_subt->t_tspec;
889 if (!((strg->st_tspec == CHAR &&
890 (t == CHAR || t == UCHAR || t == SCHAR)) ||
891 (strg->st_tspec == WCHAR && t == WCHAR))) {
892 debug_leave();
893 return false;
894 }
895 /*
896 * If the array is already partly initialized, we are
897 * wrong here.
898 */
899 if (istk->i_remaining != istk->i_type->t_dim)
900 debug_leave();
901 return false;
902 } else {
903 debug_leave();
904 return false;
905 }
906
907 /* Get length without trailing NUL character. */
908 len = strg->st_len;
909
910 if (istk->i_array_of_unknown_size) {
911 istk->i_array_of_unknown_size = false;
912 istk->i_type->t_dim = len + 1;
913 setcomplete(istk->i_type, true);
914 } else {
915 if (istk->i_type->t_dim < len) {
916 /* non-null byte ignored in string initializer */
917 warning(187);
918 }
919 }
920
921 /* In every case the array is initialized completely. */
922 istk->i_remaining = 0;
923
924 debug_initstack();
925 debug_leave();
926 return true;
927 }
928