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