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