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