init.c revision 1.150 1 /* $NetBSD: init.c,v 1.150 2021/03/28 09:39:04 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.150 2021/03/28 09:39:04 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 = 4, .x = 3 };
63 *
64 * Any scalar expression in the initializer may be surrounded by arbitrarily
65 * many extra pairs of braces, like in the example 'number_with_braces' (C99
66 * 6.7.8p11).
67 *
68 * For multi-dimensional arrays, the inner braces may be omitted like in
69 * array_flat or spelled out like in array_nested.
70 *
71 * For the initializer, the grammar parser calls these functions:
72 *
73 * begin_initialization
74 * init_lbrace for each '{'
75 * designation_add_name for each '.member' before '='
76 * designation_add_subscript for each '[123]' before '='
77 * init_using_expr for each expression
78 * init_rbrace for each '}'
79 * end_initialization
80 *
81 * Each '{' begins a new brace level, each '}' ends the current brace level.
82 * Each brace level has an associated "current object".
83 *
84 * Most of the time, the topmost level of brace_level contains a scalar type,
85 * and its remaining count toggles between 1 and 0.
86 *
87 * See also:
88 * C99 6.7.8 "Initialization"
89 * d_c99_init.c for more examples
90 */
91
92
93 /*
94 * Describes a single brace level of an ongoing initialization.
95 *
96 * XXX: Since C99, the initializers can be listed in arbitrary order by using
97 * designators to specify the sub-object to be initialized. The member names
98 * of non-leaf structs may thus appear repeatedly, as demonstrated in
99 * d_init_pop_member.c.
100 *
101 * See C99 6.7.8, which spans 6 pages full of tricky details and carefully
102 * selected examples.
103 */
104 struct brace_level {
105
106 /*
107 * The type of the current object that is initialized at this brace
108 * level.
109 *
110 * On the outermost element, this is always NULL since the outermost
111 * initializer-expression may be enclosed in an optional pair of
112 * braces, as of the current implementation.
113 *
114 * FIXME: This approach is wrong. It's not that the outermost
115 * initializer may be enclosed in additional braces, it's every scalar
116 * that may be enclosed in additional braces, as of C99 6.7.8p11.
117 *
118 * Everywhere else it is nonnull.
119 */
120 type_t *bl_type;
121
122 /*
123 * The type that will be initialized at the next initialization level,
124 * usually enclosed by another pair of braces.
125 *
126 * For an array, it is the element type, but without 'const'.
127 *
128 * For a struct or union type, it is one of the member types, but
129 * without 'const'.
130 *
131 * The outermost stack element has no bl_type but nevertheless has
132 * bl_subtype. For example, in 'int var = { 12345 }', initially there
133 * is a brace_level with bl_subtype 'int'. When the '{' is processed,
134 * an element with bl_type 'int' is pushed to the stack. When the
135 * corresponding '}' is processed, the inner element is popped again.
136 *
137 * During initialization, only the top 2 elements of the stack are
138 * looked at.
139 *
140 * XXX: Having bl_subtype here is the wrong approach, it should not be
141 * necessary at all; see bl_type.
142 */
143 type_t *bl_subtype;
144
145 /*
146 * Whether this level of the initializer requires a '}' to be
147 * completed.
148 *
149 * Multidimensional arrays do not need a closing brace to complete
150 * an inner array; for example, { 1, 2, 3, 4 } is a valid initializer
151 * for 'int arr[2][2]'.
152 *
153 * XXX: Double-check whether this is the correct approach at all; see
154 * bl_type.
155 */
156 bool bl_brace: 1;
157
158 /* Whether bl_type is an array of unknown size. */
159 bool bl_array_of_unknown_size: 1;
160
161 /*
162 * XXX: This feels wrong. Whether or not there has been a named
163 * initializer (called 'designation' since C99) should not matter at
164 * all. Even after an initializer with designation, counting of the
165 * remaining elements continues, see C99 6.7.8p17.
166 */
167 bool bl_seen_named_member: 1;
168
169 /*
170 * For structs, the next member to be initialized by a designator-less
171 * initializer.
172 */
173 sym_t *bl_next_member;
174
175 /* TODO: Add bl_next_subscript for arrays. */
176
177 /* TODO: Understand C99 6.7.8p17 and footnote 128 for unions. */
178
179 /*
180 * The number of remaining elements to be used by expressions without
181 * designator.
182 *
183 * This says nothing about which members have been initialized or not
184 * since starting with C99, members may be initialized in arbitrary
185 * order by using designators.
186 *
187 * For an array of unknown size, this is always 0 and thus irrelevant.
188 *
189 * XXX: for scalars?
190 * XXX: for structs?
191 * XXX: for unions?
192 * XXX: for arrays?
193 *
194 * XXX: Having the count of remaining objects should not be necessary.
195 * It is probably clearer to use bl_next_member and bl_next_subscript
196 * for this purpose.
197 */
198 int bl_remaining;
199
200 /*
201 * The initialization state of the enclosing data structure
202 * (struct, union, array).
203 *
204 * XXX: Or for a scalar, for the top-level element, or for expressions
205 * in redundant braces such as '{{{{ 0 }}}}' (not yet implemented as
206 * of 2021-03-25).
207 */
208 struct brace_level *bl_enclosing;
209 };
210
211 /*
212 * A single component on the path to the sub-object that is initialized by an
213 * initializer expression. Either a struct or union member, or an array
214 * subscript.
215 *
216 * See also: C99 6.7.8 "Initialization"
217 */
218 struct designator {
219 const char *name; /* for struct and union */
220 /* TODO: add 'subscript' for arrays */
221 struct designator *next;
222 };
223
224 /*
225 * The optional designation for an initializer, saying which sub-object to
226 * initialize. Examples for designations are '.member' or
227 * '.member[123].member.member[1][1]'.
228 *
229 * See also: C99 6.7.8 "Initialization"
230 */
231 struct designation {
232 struct designator *head;
233 struct designator *tail;
234 };
235
236 struct initialization {
237 /*
238 * is set as soon as a fatal error occurred in the initialization.
239 * The effect is that the rest of the initialization is ignored
240 * (parsed by yacc, expression trees built, but no initialization
241 * takes place).
242 */
243 bool initerr;
244
245 /* The symbol that is to be initialized. */
246 sym_t *initsym;
247
248 /* The innermost brace level. */
249 struct brace_level *brace_level;
250
251 /*
252 * The C99 designator, if any, for the current initialization
253 * expression.
254 */
255 struct designation designation;
256
257 struct initialization *next;
258 };
259
260
261 static struct initialization *init;
262
263 #ifdef DEBUG
264 static int debug_ind = 0;
265 #endif
266
267
268 #ifdef DEBUG
269
270 static void __printflike(1, 2)
271 debug_printf(const char *fmt, ...)
272 {
273 va_list va;
274
275 va_start(va, fmt);
276 vfprintf(stdout, fmt, va);
277 va_end(va);
278 }
279
280 static void
281 debug_indent(void)
282 {
283 debug_printf("%*s", 2 * debug_ind, "");
284 }
285
286 static void
287 debug_enter(const char *func)
288 {
289 printf("%*s+ %s\n", 2 * debug_ind++, "", func);
290 }
291
292 static void __printflike(1, 2)
293 debug_step(const char *fmt, ...)
294 {
295 va_list va;
296
297 debug_indent();
298 va_start(va, fmt);
299 vfprintf(stdout, fmt, va);
300 va_end(va);
301 printf("\n");
302 }
303
304 static void
305 debug_leave(const char *func)
306 {
307 printf("%*s- %s\n", 2 * --debug_ind, "", func);
308 }
309
310 #else
311
312 #define debug_printf(fmt, ...) do { } while (false)
313 #define debug_indent() do { } while (false)
314 #define debug_enter(function) do { } while (false)
315 #define debug_step(fmt, ...) do { } while (false)
316 #define debug_leave(function) do { } while (false)
317
318 #endif
319
320
321 static struct designator *
322 designator_new(const char *name)
323 {
324 struct designator *d = xcalloc(1, sizeof *d);
325 d->name = name;
326 return d;
327 }
328
329 static void
330 designator_free(struct designator *d)
331 {
332 free(d);
333 }
334
335
336 #ifdef DEBUG
337 static void
338 designation_debug(const struct designation *dn)
339 {
340 const struct designator *p;
341
342 if (dn->head == NULL)
343 return;
344
345 debug_indent();
346 debug_printf("designation: ");
347 for (p = dn->head; p != NULL; p = p->next)
348 debug_printf(".%s", p->name);
349 debug_printf("\n");
350 }
351 #else
352 #define designation_debug(dn) do { } while (false)
353 #endif
354
355 static void
356 designation_add(struct designation *dn, struct designator *dr)
357 {
358
359 if (dn->head != NULL) {
360 dn->tail->next = dr;
361 dn->tail = dr;
362 } else {
363 dn->head = dr;
364 dn->tail = dr;
365 }
366
367 designation_debug(dn);
368 }
369
370 /* TODO: add support for array subscripts, not only named members */
371 /*
372 * TODO: This function should not be necessary at all. There is no need to
373 * remove the head of the list.
374 */
375 static void
376 designation_shift_level(struct designation *dn)
377 {
378 lint_assert(dn->head != NULL);
379
380 if (dn->head == dn->tail) {
381 designator_free(dn->head);
382 dn->head = NULL;
383 dn->tail = NULL;
384 } else {
385 struct designator *head = dn->head;
386 dn->head = dn->head->next;
387 designator_free(head);
388 }
389
390 designation_debug(dn);
391 }
392
393
394 #ifdef DEBUG
395 /*
396 * TODO: only log the top of the stack after each modifying operation
397 *
398 * TODO: wrap all write accesses to brace_level in setter functions
399 */
400 static void
401 brace_level_debug(const struct brace_level *level)
402 {
403 if (level->bl_type != NULL)
404 debug_printf("type '%s'", type_name(level->bl_type));
405 if (level->bl_type != NULL && level->bl_subtype != NULL)
406 debug_printf(", ");
407 if (level->bl_subtype != NULL)
408 debug_printf("subtype '%s'", type_name(level->bl_subtype));
409
410 if (level->bl_brace)
411 debug_printf(", needs closing brace");
412 if (level->bl_array_of_unknown_size)
413 debug_printf(", array of unknown size");
414 if (level->bl_seen_named_member)
415 debug_printf(", seen named member");
416
417 const type_t *eff_type = level->bl_type != NULL
418 ? level->bl_type : level->bl_subtype;
419 if (eff_type->t_tspec == STRUCT && level->bl_next_member != NULL)
420 debug_printf(", next member '%s'",
421 level->bl_next_member->s_name);
422
423 debug_printf(", remaining %d\n", level->bl_remaining);
424 }
425 #else
426 #define brace_level_debug(level) do { } while (false)
427 #endif
428
429 static const sym_t *
430 brace_level_look_up_member(const struct brace_level *level, const char *name)
431 {
432 const type_t *tp = level->bl_type;
433 const sym_t *m;
434
435 lint_assert(tp->t_tspec == STRUCT || tp->t_tspec == UNION);
436
437 for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next) {
438 if (m->s_bitfield && m->s_name == unnamed)
439 continue;
440 if (strcmp(m->s_name, name) == 0)
441 return m;
442 }
443
444 return NULL;
445 }
446
447 /* TODO: merge duplicate code */
448 static sym_t *
449 brace_level_look_up_member_bloated(struct brace_level *level,
450 const struct designator *dr, int *count)
451 {
452 sym_t *m;
453
454 for (m = level->bl_type->t_str->sou_first_member;
455 m != NULL; m = m->s_next) {
456 if (m->s_bitfield && m->s_name == unnamed)
457 continue;
458 /*
459 * TODO: split into separate functions:
460 *
461 * look_up_array_next
462 * look_up_array_designator
463 * look_up_struct_next
464 * look_up_struct_designator
465 */
466 if (dr != NULL) {
467 /* XXX: this log entry looks unnecessarily verbose */
468 debug_step("have member '%s', want member '%s'",
469 m->s_name, dr->name);
470 if (strcmp(m->s_name, dr->name) == 0) {
471 (*count)++;
472 break;
473 } else
474 continue;
475 }
476
477 /* XXX: What is this code for? */
478 if (++(*count) == 1) {
479 level->bl_next_member = m;
480 level->bl_subtype = m->s_type;
481 }
482 }
483
484 return m;
485 }
486
487
488 static struct initialization *
489 initialization_new(sym_t *sym)
490 {
491 struct initialization *in = xcalloc(1, sizeof(*in));
492
493 in->initsym = sym;
494
495 return in;
496 }
497
498 static void
499 initialization_free(struct initialization *in)
500 {
501 struct brace_level *level, *next;
502
503 for (level = in->brace_level; level != NULL; level = next) {
504 next = level->bl_enclosing;
505 free(level);
506 }
507
508 free(in);
509 }
510
511 #ifdef DEBUG
512 /*
513 * TODO: only call debug_initstack after each push/pop.
514 */
515 static void
516 initialization_debug(const struct initialization *in)
517 {
518 if (in->brace_level == NULL) {
519 debug_step("no brace level in the current initialization");
520 return;
521 }
522
523 size_t i = 0;
524 for (const struct brace_level *level = in->brace_level;
525 level != NULL; level = level->bl_enclosing) {
526 debug_indent();
527 debug_printf("brace level %zu: ", i);
528 brace_level_debug(level);
529 i++;
530 }
531 }
532 #else
533 #define initialization_debug(in) do { } while (false)
534 #endif
535
536 static void
537 initialization_set_error(struct initialization *in)
538 {
539 in->initerr = true;
540 }
541
542
543 /* XXX: unnecessary prototype since it is not recursive */
544 static bool init_array_using_string(struct initialization *, tnode_t *);
545
546
547 static struct initialization *
548 current_init(void)
549 {
550 lint_assert(init != NULL);
551 return init;
552 }
553
554 bool *
555 current_initerr(void)
556 {
557 return ¤t_init()->initerr;
558 }
559
560 sym_t **
561 current_initsym(void)
562 {
563 return ¤t_init()->initsym;
564 }
565
566 #define initsym (*current_initsym())
567
568 #ifndef DEBUG
569
570 #define debug_designation() do { } while (false)
571 #define debug_brace_level(level) do { } while (false)
572 #define debug_initstack() do { } while (false)
573
574 #else
575
576
577 #define debug_enter() debug_enter(__func__)
578 #define debug_leave() debug_leave(__func__)
579
580 #endif
581
582
583 void
584 begin_initialization(sym_t *sym)
585 {
586 struct initialization *curr_init;
587
588 debug_step("begin initialization of '%s'", type_name(sym->s_type));
589 curr_init = initialization_new(sym);
590 curr_init->next = init;
591 init = curr_init;
592 }
593
594 void
595 end_initialization(void)
596 {
597 struct initialization *curr_init;
598
599 curr_init = init;
600 init = init->next;
601 initialization_free(curr_init);
602 debug_step("end initialization");
603 }
604
605
606
607 void
608 designation_add_name(sbuf_t *sb)
609 {
610 designation_add(¤t_init()->designation,
611 designator_new(sb->sb_name));
612 }
613
614 /* TODO: Move the function body up here, to avoid the forward declaration. */
615 static void initstack_pop_nobrace(struct initialization *);
616
617 static struct brace_level *
618 brace_level_new(type_t *type, type_t *subtype, int remaining)
619 {
620 struct brace_level *level = xcalloc(1, sizeof(*level));
621
622 level->bl_type = type;
623 level->bl_subtype = subtype;
624 level->bl_remaining = remaining;
625
626 return level;
627 }
628
629 static void
630 brace_level_set_array_dimension(struct brace_level *level, int dim)
631 {
632 debug_step("setting the array size to %d", dim);
633 level->bl_type->t_dim = dim;
634 debug_indent();
635 brace_level_debug(level);
636 }
637
638 static void
639 brace_level_next_member(struct brace_level *level)
640 {
641 const sym_t *m;
642
643 do {
644 m = level->bl_next_member = level->bl_next_member->s_next;
645 /* XXX: can this assertion be made to fail? */
646 lint_assert(m != NULL);
647 } while (m->s_bitfield && m->s_name == unnamed);
648
649 debug_indent();
650 brace_level_debug(level);
651 }
652
653 /*
654 * A sub-object of an array is initialized using a designator. This does not
655 * have to be an array element directly, it can also be used to initialize
656 * only a sub-object of the array element.
657 *
658 * C99 example: struct { int member[4]; } var = { [2] = 12345 };
659 *
660 * GNU example: struct { int member[4]; } var = { [1 ... 3] = 12345 };
661 *
662 * TODO: test the following initialization with an outer and an inner type:
663 *
664 * .deeply[0].nested = {
665 * .deeply[1].nested = {
666 * 12345,
667 * },
668 * }
669 */
670 void
671 designation_add_subscript(range_t range)
672 {
673 struct initialization *in = current_init();
674 struct brace_level *level;
675
676 debug_enter();
677 if (range.lo == range.hi)
678 debug_step("subscript is %zu", range.hi);
679 else
680 debug_step("subscript range is %zu ... %zu",
681 range.lo, range.hi);
682
683 /* XXX: This call is wrong here, it must be somewhere else. */
684 initstack_pop_nobrace(in);
685
686 level = in->brace_level;
687 if (level->bl_array_of_unknown_size) {
688 /* No +1 here, extend_if_array_of_unknown_size will add it. */
689 int auto_dim = (int)range.hi;
690 if (auto_dim > level->bl_type->t_dim)
691 brace_level_set_array_dimension(level, auto_dim);
692 }
693
694 debug_leave();
695 }
696
697
698 /*
699 * Initialize the initialization stack by putting an entry for the object
700 * which is to be initialized on it.
701 *
702 * TODO: merge into begin_initialization
703 */
704 void
705 initstack_init(void)
706 {
707 struct initialization *in = current_init();
708
709 if (in->initerr)
710 return;
711
712 debug_enter();
713
714 /*
715 * If the type which is to be initialized is an incomplete array,
716 * it must be duplicated.
717 */
718 if (initsym->s_type->t_tspec == ARRAY && is_incomplete(initsym->s_type))
719 initsym->s_type = duptyp(initsym->s_type);
720 /* TODO: does 'duptyp' create a memory leak? */
721
722 current_init()->brace_level = brace_level_new(NULL, initsym->s_type, 1);
723
724 initialization_debug(current_init());
725 debug_leave();
726 }
727
728 /* TODO: document me */
729 static void
730 initstack_pop_item_named_member(const char *name)
731 {
732 struct initialization *in = current_init();
733 struct brace_level *level = in->brace_level;
734 const sym_t *m;
735
736 /*
737 * TODO: fix wording of the debug message; this doesn't seem to be
738 * related to initializing the named member.
739 */
740 debug_step("initializing named member '%s'", name);
741
742 if (level->bl_type->t_tspec != STRUCT &&
743 level->bl_type->t_tspec != UNION) {
744 /* syntax error '%s' */
745 error(249, "named member must only be used with struct/union");
746 initialization_set_error(in);
747 return;
748 }
749
750 m = brace_level_look_up_member(level, name);
751 if (m == NULL) {
752 /* TODO: add type information to the message */
753 /* undefined struct/union member: %s */
754 error(101, name);
755
756 designation_shift_level(&in->designation);
757 level->bl_seen_named_member = true;
758 return;
759 }
760
761 debug_step("found matching member");
762 level->bl_subtype = m->s_type;
763 /* XXX: why ++? */
764 level->bl_remaining++;
765 /* XXX: why is bl_seen_named_member not set? */
766 designation_shift_level(&in->designation);
767 }
768
769 /* TODO: think of a better name than 'pop' */
770 static void
771 initstack_pop_item_unnamed(struct initialization *in)
772 {
773 struct brace_level *level = in->brace_level;
774
775 /*
776 * If the removed element was a structure member, we must go
777 * to the next structure member.
778 */
779 if (level->bl_remaining > 0 && level->bl_type->t_tspec == STRUCT &&
780 !level->bl_seen_named_member) {
781 brace_level_next_member(level);
782 level->bl_subtype = level->bl_next_member->s_type;
783 }
784 }
785
786 /* TODO: think of a better name than 'pop' */
787 static void
788 initstack_pop_item(struct initialization *in)
789 {
790 struct brace_level *level;
791
792 debug_enter();
793
794 level = in->brace_level;
795 debug_indent();
796 debug_printf("popping: ");
797 brace_level_debug(level);
798
799 in->brace_level = level->bl_enclosing;
800 free(level);
801 level = in->brace_level;
802 lint_assert(level != NULL);
803
804 level->bl_remaining--;
805 lint_assert(level->bl_remaining >= 0);
806 debug_step("%d elements remaining", level->bl_remaining);
807
808 if (in->designation.head != NULL && in->designation.head->name != NULL)
809 initstack_pop_item_named_member(in->designation.head->name);
810 else
811 initstack_pop_item_unnamed(in);
812
813 initialization_debug(current_init());
814 debug_leave();
815 }
816
817 /*
818 * Take all entries, including the first which requires a closing brace,
819 * from the stack.
820 */
821 static void
822 initstack_pop_brace(struct initialization *in)
823 {
824 bool brace;
825
826 debug_enter();
827 initialization_debug(in);
828 do {
829 brace = in->brace_level->bl_brace;
830 /* TODO: improve wording of the debug message */
831 debug_step("loop brace=%d", brace);
832 initstack_pop_item(in);
833 } while (!brace);
834 initialization_debug(in);
835 debug_leave();
836 }
837
838 /*
839 * Take all entries which cannot be used for further initializers from the
840 * stack, but do this only if they do not require a closing brace.
841 */
842 /* TODO: think of a better name than 'pop' */
843 static void
844 initstack_pop_nobrace(struct initialization *in)
845 {
846
847 debug_enter();
848 while (!in->brace_level->bl_brace &&
849 in->brace_level->bl_remaining == 0 &&
850 !in->brace_level->bl_array_of_unknown_size)
851 initstack_pop_item(in);
852 debug_leave();
853 }
854
855 /* Extend an array of unknown size by one element */
856 static void
857 extend_if_array_of_unknown_size(struct initialization *in)
858 {
859 struct brace_level *level = in->brace_level;
860
861 if (level->bl_remaining != 0)
862 return;
863 /*
864 * XXX: According to the function name, there should be a 'return' if
865 * bl_array_of_unknown_size is false. There's probably a test missing
866 * for that case.
867 */
868
869 /*
870 * The only place where an incomplete array may appear is at the
871 * outermost aggregate level of the object to be initialized.
872 */
873 lint_assert(level->bl_enclosing->bl_enclosing == NULL);
874 lint_assert(level->bl_type->t_tspec == ARRAY);
875
876 debug_step("extending array of unknown size '%s'",
877 type_name(level->bl_type));
878 level->bl_remaining = 1;
879 level->bl_type->t_dim++;
880 setcomplete(level->bl_type, true);
881
882 debug_step("extended type is '%s'", type_name(level->bl_type));
883 }
884
885 /* TODO: document me */
886 /* TODO: think of a better name than 'push' */
887 static void
888 initstack_push_array(struct initialization *in)
889 {
890 struct brace_level *level = in->brace_level;
891
892 if (level->bl_enclosing->bl_seen_named_member) {
893 level->bl_brace = true;
894 debug_step("ARRAY%s%s",
895 level->bl_brace ? ", needs closing brace" : "",
896 /* TODO: this is redundant, always true */
897 level->bl_enclosing->bl_seen_named_member
898 ? ", seen named member" : "");
899 }
900
901 if (is_incomplete(level->bl_type) &&
902 level->bl_enclosing->bl_enclosing != NULL) {
903 /* initialization of an incomplete type */
904 error(175);
905 initialization_set_error(in);
906 return;
907 }
908
909 level->bl_subtype = level->bl_type->t_subt;
910 level->bl_array_of_unknown_size = is_incomplete(level->bl_type);
911 level->bl_remaining = level->bl_type->t_dim;
912 designation_debug(&in->designation);
913 debug_step("type '%s' remaining %d",
914 type_name(level->bl_type), level->bl_remaining);
915 }
916
917
918 /* TODO: document me */
919 /* TODO: think of a better name than 'push' */
920 static bool
921 initstack_push_struct_or_union(struct initialization *in)
922 {
923 /*
924 * TODO: remove unnecessary 'const' for variables in functions that
925 * fit on a single screen. Keep it for larger functions.
926 */
927 struct brace_level *level = in->brace_level;
928 int cnt;
929 sym_t *m;
930
931 if (is_incomplete(level->bl_type)) {
932 /* initialization of an incomplete type */
933 error(175);
934 initialization_set_error(in);
935 return false;
936 }
937
938 cnt = 0;
939 designation_debug(&in->designation);
940 debug_step("lookup for '%s'%s",
941 type_name(level->bl_type),
942 level->bl_seen_named_member ? ", seen named member" : "");
943
944 m = brace_level_look_up_member_bloated(level,
945 in->designation.head, &cnt);
946
947 if (in->designation.head != NULL) {
948 if (m == NULL) {
949 debug_step("pop struct");
950 return true;
951 }
952 level->bl_next_member = m;
953 level->bl_subtype = m->s_type;
954 level->bl_seen_named_member = true;
955 debug_step("named member '%s'",
956 in->designation.head->name);
957 designation_shift_level(&in->designation);
958 cnt = level->bl_type->t_tspec == STRUCT ? 2 : 1;
959 }
960 level->bl_brace = true;
961 debug_step("unnamed element with type '%s'%s",
962 type_name(
963 level->bl_type != NULL ? level->bl_type : level->bl_subtype),
964 level->bl_brace ? ", needs closing brace" : "");
965 if (cnt == 0) {
966 /* cannot init. struct/union with no named member */
967 error(179);
968 initialization_set_error(in);
969 return false;
970 }
971 level->bl_remaining = level->bl_type->t_tspec == STRUCT ? cnt : 1;
972 return false;
973 }
974
975 /* TODO: document me */
976 /* TODO: think of a better name than 'push' */
977 static void
978 initstack_push(struct initialization *in)
979 {
980 struct brace_level *level, *enclosing;
981
982 debug_enter();
983
984 extend_if_array_of_unknown_size(in);
985
986 level = in->brace_level;
987 lint_assert(level->bl_remaining > 0);
988 lint_assert(level->bl_type == NULL ||
989 !is_scalar(level->bl_type->t_tspec));
990
991 in->brace_level = xcalloc(1, sizeof *in->brace_level);
992 in->brace_level->bl_enclosing = level;
993 in->brace_level->bl_type = level->bl_subtype;
994 lint_assert(in->brace_level->bl_type->t_tspec != FUNC);
995
996 again:
997 level = in->brace_level;
998
999 debug_step("expecting type '%s'", type_name(level->bl_type));
1000 lint_assert(level->bl_type != NULL);
1001 switch (level->bl_type->t_tspec) {
1002 case ARRAY:
1003 if (in->designation.head != NULL) {
1004 debug_step("pop array, named member '%s'%s",
1005 in->designation.head->name,
1006 level->bl_brace ? ", needs closing brace" : "");
1007 goto pop;
1008 }
1009
1010 initstack_push_array(in);
1011 break;
1012
1013 case UNION:
1014 if (tflag)
1015 /* initialization of union is illegal in trad. C */
1016 warning(238);
1017 /* FALLTHROUGH */
1018 case STRUCT:
1019 if (initstack_push_struct_or_union(in))
1020 goto pop;
1021 break;
1022 default:
1023 if (in->designation.head != NULL) {
1024 debug_step("pop scalar");
1025 pop:
1026 /* TODO: extract this into end_initializer_level */
1027 enclosing = in->brace_level->bl_enclosing;
1028 free(level);
1029 in->brace_level = enclosing;
1030 goto again;
1031 }
1032 /* The initialization stack now expects a single scalar. */
1033 level->bl_remaining = 1;
1034 break;
1035 }
1036
1037 initialization_debug(current_init());
1038 debug_leave();
1039 }
1040
1041 static void
1042 check_too_many_initializers(void)
1043 {
1044 struct initialization *in = current_init();
1045 const struct brace_level *level = in->brace_level;
1046
1047 if (level->bl_remaining > 0)
1048 return;
1049 /*
1050 * FIXME: even with named members, there can be too many initializers
1051 */
1052 if (level->bl_array_of_unknown_size || level->bl_seen_named_member)
1053 return;
1054
1055 tspec_t t = level->bl_type->t_tspec;
1056 if (t == ARRAY) {
1057 /* too many array initializers, expected %d */
1058 error(173, level->bl_type->t_dim);
1059 } else if (t == STRUCT || t == UNION) {
1060 /* too many struct/union initializers */
1061 error(172);
1062 } else {
1063 /* too many initializers */
1064 error(174);
1065 }
1066 initialization_set_error(in);
1067 }
1068
1069 /*
1070 * Process a '{' in an initializer by starting the initialization of the
1071 * nested data structure, with bl_type being the bl_subtype of the outer
1072 * initialization level.
1073 */
1074 static void
1075 initstack_next_brace(struct initialization *in)
1076 {
1077
1078 debug_enter();
1079 initialization_debug(in);
1080
1081 if (in->brace_level->bl_type != NULL &&
1082 is_scalar(in->brace_level->bl_type->t_tspec)) {
1083 /* invalid initializer type %s */
1084 error(176, type_name(in->brace_level->bl_type));
1085 initialization_set_error(in);
1086 }
1087 if (!in->initerr)
1088 check_too_many_initializers();
1089 if (!in->initerr)
1090 initstack_push(in);
1091 if (!in->initerr) {
1092 in->brace_level->bl_brace = true;
1093 designation_debug(&in->designation);
1094 debug_step("expecting type '%s'",
1095 type_name(in->brace_level->bl_type != NULL
1096 ? in->brace_level->bl_type
1097 : in->brace_level->bl_subtype));
1098 }
1099
1100 initialization_debug(current_init());
1101 debug_leave();
1102 }
1103
1104 /* TODO: document me, or think of a better name */
1105 static void
1106 initstack_next_nobrace(struct initialization *in, tnode_t *tn)
1107 {
1108 debug_enter();
1109
1110 if (in->brace_level->bl_type == NULL &&
1111 !is_scalar(in->brace_level->bl_subtype->t_tspec)) {
1112 /* {}-enclosed initializer required */
1113 error(181);
1114 /* XXX: maybe set initerr here */
1115 }
1116
1117 if (!in->initerr)
1118 check_too_many_initializers();
1119
1120 while (!in->initerr) {
1121 struct brace_level *level = in->brace_level;
1122
1123 if (tn->tn_type->t_tspec == STRUCT &&
1124 level->bl_type == tn->tn_type &&
1125 level->bl_enclosing != NULL &&
1126 level->bl_enclosing->bl_enclosing != NULL) {
1127 level->bl_brace = false;
1128 level->bl_remaining = 1; /* the struct itself */
1129 break;
1130 }
1131
1132 if (level->bl_type != NULL &&
1133 is_scalar(level->bl_type->t_tspec))
1134 break;
1135 initstack_push(in);
1136 }
1137
1138 initialization_debug(current_init());
1139 debug_leave();
1140 }
1141
1142 /* TODO: document me */
1143 void
1144 init_lbrace(void)
1145 {
1146 struct initialization *in = current_init();
1147
1148 if (in->initerr)
1149 return;
1150
1151 debug_enter();
1152 initialization_debug(in);
1153
1154 if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
1155 in->brace_level->bl_enclosing == NULL) {
1156 if (tflag &&
1157 !is_scalar(in->brace_level->bl_subtype->t_tspec))
1158 /* no automatic aggregate initialization in trad. C */
1159 warning(188);
1160 }
1161
1162 /*
1163 * Remove all entries which cannot be used for further initializers
1164 * and do not expect a closing brace.
1165 */
1166 initstack_pop_nobrace(in);
1167
1168 initstack_next_brace(in);
1169
1170 initialization_debug(in);
1171 debug_leave();
1172 }
1173
1174 /*
1175 * Process a '}' in an initializer by finishing the current level of the
1176 * initialization stack.
1177 */
1178 void
1179 init_rbrace(void)
1180 {
1181 struct initialization *in = current_init();
1182
1183 if (in->initerr)
1184 return;
1185
1186 debug_enter();
1187 initstack_pop_brace(in);
1188 debug_leave();
1189 }
1190
1191 /* In traditional C, bit-fields can be initialized only by integer constants. */
1192 static void
1193 check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
1194 {
1195 if (tflag &&
1196 is_integer(lt) &&
1197 ln->tn_type->t_bitfield &&
1198 !is_integer(rt)) {
1199 /* bit-field initialization is illegal in traditional C */
1200 warning(186);
1201 }
1202 }
1203
1204 static void
1205 check_non_constant_initializer(const tnode_t *tn, scl_t sclass)
1206 {
1207 /* TODO: rename CON to CONSTANT to avoid ambiguity with CONVERT */
1208 if (tn == NULL || tn->tn_op == CON)
1209 return;
1210
1211 sym_t *sym;
1212 ptrdiff_t offs;
1213 if (constant_addr(tn, &sym, &offs))
1214 return;
1215
1216 if (sclass == AUTO || sclass == REG) {
1217 /* non-constant initializer */
1218 c99ism(177);
1219 } else {
1220 /* non-constant initializer */
1221 error(177);
1222 }
1223 }
1224
1225 /*
1226 * Initialize a non-array object with automatic storage duration and only a
1227 * single initializer expression without braces by delegating to ASSIGN.
1228 */
1229 static bool
1230 init_using_assign(tnode_t *rn)
1231 {
1232 tnode_t *ln, *tn;
1233
1234 if (initsym->s_type->t_tspec == ARRAY)
1235 return false;
1236 if (current_init()->brace_level->bl_enclosing != NULL)
1237 return false;
1238
1239 debug_step("handing over to ASSIGN");
1240
1241 ln = new_name_node(initsym, 0);
1242 ln->tn_type = tduptyp(ln->tn_type);
1243 ln->tn_type->t_const = false;
1244
1245 tn = build(ASSIGN, ln, rn);
1246 expr(tn, false, false, false, false);
1247
1248 /* XXX: why not clean up the initstack here already? */
1249 return true;
1250 }
1251
1252 static void
1253 check_init_expr(tnode_t *tn, scl_t sclass)
1254 {
1255 struct initialization *in = current_init();
1256 tnode_t *ln;
1257 tspec_t lt, rt;
1258 struct mbl *tmem;
1259
1260 /* Create a temporary node for the left side. */
1261 ln = tgetblk(sizeof *ln);
1262 ln->tn_op = NAME;
1263 ln->tn_type = tduptyp(in->brace_level->bl_type);
1264 ln->tn_type->t_const = false;
1265 ln->tn_lvalue = true;
1266 ln->tn_sym = initsym; /* better than nothing */
1267
1268 tn = cconv(tn);
1269
1270 lt = ln->tn_type->t_tspec;
1271 rt = tn->tn_type->t_tspec;
1272
1273 debug_step("typeok '%s', '%s'",
1274 type_name(ln->tn_type), type_name(tn->tn_type));
1275 if (!typeok(INIT, 0, ln, tn))
1276 return;
1277
1278 /*
1279 * Preserve the tree memory. This is necessary because otherwise
1280 * expr() would free it.
1281 */
1282 tmem = tsave();
1283 expr(tn, true, false, true, false);
1284 trestor(tmem);
1285
1286 check_bit_field_init(ln, lt, rt);
1287
1288 /*
1289 * XXX: Is it correct to do this conversion _after_ the typeok above?
1290 */
1291 if (lt != rt ||
1292 (in->brace_level->bl_type->t_bitfield && tn->tn_op == CON))
1293 tn = convert(INIT, 0, in->brace_level->bl_type, tn);
1294
1295 check_non_constant_initializer(tn, sclass);
1296 }
1297
1298 void
1299 init_using_expr(tnode_t *tn)
1300 {
1301 struct initialization *in = current_init();
1302 scl_t sclass;
1303
1304 debug_enter();
1305 initialization_debug(current_init());
1306 designation_debug(&in->designation);
1307 debug_step("expr:");
1308 debug_node(tn, debug_ind + 1);
1309
1310 if (in->initerr || tn == NULL)
1311 goto done;
1312
1313 sclass = initsym->s_scl;
1314 if ((sclass == AUTO || sclass == REG) && init_using_assign(tn))
1315 goto done;
1316
1317 initstack_pop_nobrace(in);
1318
1319 if (init_array_using_string(in, tn)) {
1320 debug_step("after initializing the string:");
1321 /* XXX: why not clean up the initstack here already? */
1322 goto done_initstack;
1323 }
1324
1325 initstack_next_nobrace(in, tn);
1326 if (in->initerr || tn == NULL)
1327 goto done_initstack;
1328
1329 in->brace_level->bl_remaining--;
1330 debug_step("%d elements remaining", in->brace_level->bl_remaining);
1331
1332 check_init_expr(tn, sclass);
1333
1334 done_initstack:
1335 initialization_debug(current_init());
1336
1337 done:
1338 while (in->designation.head != NULL)
1339 designation_shift_level(&in->designation);
1340
1341 debug_leave();
1342 }
1343
1344
1345 /* Initialize a character array or wchar_t array with a string literal. */
1346 static bool
1347 init_array_using_string(struct initialization *in, tnode_t *tn)
1348 {
1349 tspec_t t;
1350 struct brace_level *level;
1351 int len;
1352 strg_t *strg;
1353
1354 if (tn->tn_op != STRING)
1355 return false;
1356
1357 debug_enter();
1358 initialization_debug(current_init());
1359
1360 level = in->brace_level;
1361 strg = tn->tn_string;
1362
1363 /*
1364 * Check if we have an array type which can be initialized by
1365 * the string.
1366 */
1367 if (level->bl_subtype != NULL && level->bl_subtype->t_tspec == ARRAY) {
1368 debug_step("subt array");
1369 t = level->bl_subtype->t_subt->t_tspec;
1370 if (!((strg->st_tspec == CHAR &&
1371 (t == CHAR || t == UCHAR || t == SCHAR)) ||
1372 (strg->st_tspec == WCHAR && t == WCHAR))) {
1373 debug_leave();
1374 return false;
1375 }
1376 /* XXX: duplicate code, see below */
1377
1378 /* Put the array at top of stack */
1379 initstack_push(in);
1380 level = in->brace_level;
1381
1382 /* TODO: what if both bl_type and bl_subtype are ARRAY? */
1383
1384 } else if (level->bl_type != NULL && level->bl_type->t_tspec == ARRAY) {
1385 debug_step("type array");
1386 t = level->bl_type->t_subt->t_tspec;
1387 if (!((strg->st_tspec == CHAR &&
1388 (t == CHAR || t == UCHAR || t == SCHAR)) ||
1389 (strg->st_tspec == WCHAR && t == WCHAR))) {
1390 debug_leave();
1391 return false;
1392 }
1393 /* XXX: duplicate code, see above */
1394
1395 /*
1396 * TODO: is this really not needed in the branch above this
1397 * one?
1398 */
1399 /*
1400 * If the array is already partly initialized, we are
1401 * wrong here.
1402 */
1403 if (level->bl_remaining != level->bl_type->t_dim) {
1404 debug_leave();
1405 return false;
1406 }
1407 } else {
1408 debug_leave();
1409 return false;
1410 }
1411
1412 /* Get length without trailing NUL character. */
1413 len = strg->st_len;
1414
1415 if (level->bl_array_of_unknown_size) {
1416 level->bl_array_of_unknown_size = false;
1417 level->bl_type->t_dim = len + 1;
1418 setcomplete(level->bl_type, true);
1419 } else {
1420 /*
1421 * TODO: check for buffer overflow in the object to be
1422 * initialized
1423 */
1424 /* XXX: double-check for off-by-one error */
1425 if (level->bl_type->t_dim < len) {
1426 /* non-null byte ignored in string initializer */
1427 warning(187);
1428 }
1429
1430 /*
1431 * TODO: C99 6.7.8p14 allows a string literal to be enclosed
1432 * in optional redundant braces, just like scalars. Add tests
1433 * for this.
1434 */
1435 }
1436
1437 /* In every case the array is initialized completely. */
1438 level->bl_remaining = 0;
1439
1440 initialization_debug(current_init());
1441 debug_leave();
1442 return true;
1443 }
1444