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