init.c revision 1.254 1 /* $NetBSD: init.c,v 1.254 2024/01/09 23:46:54 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * Copyright (c) 2021 Roland Illig
6 * All Rights Reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Jochen Pohl for
19 * The NetBSD Project.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
38
39 #include <sys/cdefs.h>
40 #if defined(__RCSID)
41 __RCSID("$NetBSD: init.c,v 1.254 2024/01/09 23:46:54 rillig Exp $");
42 #endif
43
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include "lint1.h"
48
49
50 /*
51 * Initialization of global or local objects, like in:
52 *
53 * int number = 12345;
54 * int number_with_braces = { 12345 };
55 * int array_of_unknown_size[] = { 111, 222, 333 };
56 * struct { int x, y; } point = { .y = 4, .x = 3 };
57 *
58 * During an initialization, the grammar parser calls these functions:
59 *
60 * begin_initialization
61 * init_lbrace for each '{'
62 * add_designator_member for each '.member' before '='
63 * add_designator_subscript for each '[123]' before '='
64 * init_expr for each expression
65 * init_rbrace for each '}'
66 * end_initialization
67 *
68 * Each '{' begins a new brace level, each '}' ends the current brace level.
69 * Each brace level has an associated "current object", which is the starting
70 * point for resolving the optional designations such as '.member[3]'.
71 *
72 * See also:
73 * C99 6.7.8 "Initialization"
74 * C11 6.7.9 "Initialization"
75 * d_c99_init.c for more examples
76 */
77
78
79 /*
80 * Everything that happens between a '{' and the corresponding '}', as part
81 * of an initialization.
82 *
83 * Each brace level has a "current object". For the outermost brace level,
84 * it is the same as the object to be initialized. Each nested '{' begins a
85 * nested brace level, for the sub-object pointed to by the designator of the
86 * outer brace level.
87 *
88 * C99 6.7.8p17
89 */
90 typedef struct brace_level {
91 /* The type of the "current object". */
92 const type_t *bl_type;
93
94 /*
95 * The path from the "current object" to the sub-object that is
96 * initialized by the next expression.
97 *
98 * Initially, the designation is empty. Before handling an expression,
99 * the designation is updated to point to the corresponding sub-object
100 * to be initialized. After handling an expression, the designation is
101 * marked as done. It is later advanced as necessary.
102 */
103 designation bl_designation;
104
105 struct brace_level *bl_enclosing;
106 } brace_level;
107
108 /*
109 * An ongoing initialization.
110 *
111 * In most cases there is only ever a single initialization at a time. See
112 * pointer_to_compound_literal in msg_171.c for a real-life counterexample.
113 */
114 typedef struct initialization {
115 /* The symbol that is to be initialized. */
116 sym_t *in_sym;
117
118 /* The innermost brace level. */
119 brace_level *in_brace_level;
120
121 /*
122 * The maximum subscript that has ever been seen for an array of
123 * unknown size, which can only occur at the outermost brace level.
124 */
125 size_t in_max_subscript;
126
127 /*
128 * Is set when a structural error occurred in the initialization. If
129 * set, the rest of the initialization is still parsed, but the
130 * initialization assignments are not checked.
131 */
132 bool in_err;
133
134 struct initialization *in_enclosing;
135 } initialization;
136
137
138 static void *
139 unconst_cast(const void *p)
140 {
141 void *r;
142
143 memcpy(&r, &p, sizeof(r));
144 return r;
145 }
146
147 static bool
148 has_automatic_storage_duration(const sym_t *sym)
149 {
150
151 return sym->s_scl == AUTO || sym->s_scl == REG;
152 }
153
154 /*
155 * Test whether rn is a string literal that can initialize ltp.
156 *
157 * See also:
158 * C99 6.7.8p14 for plain character strings
159 * C99 6.7.8p15 for wide character strings
160 */
161 static bool
162 can_init_character_array(const type_t *ltp, const tnode_t *rn)
163 {
164
165 if (!(ltp != NULL && ltp->t_tspec == ARRAY && rn->tn_op == STRING))
166 return false;
167
168 tspec_t lst = ltp->t_subt->t_tspec;
169 tspec_t rst = rn->tn_type->t_subt->t_tspec;
170
171 return rst == CHAR
172 ? lst == CHAR || lst == UCHAR || lst == SCHAR
173 : lst == WCHAR_TSPEC;
174 }
175
176 /*
177 * C11 6.7.9p9 seems to say that all unnamed members are skipped. C11 6.7.2.1p8
178 * suggests an exception to that rule, and together with C11 6.7.2.1p13, it
179 * says that the members from an anonymous struct/union member are "considered
180 * to be members of the containing structure or union", thereby preventing that
181 * the containing structure or union has only unnamed members.
182 */
183 static const sym_t *
184 skip_unnamed(const sym_t *m)
185 {
186
187 while (m != NULL && m->s_name == unnamed
188 && !is_struct_or_union(m->s_type->t_tspec))
189 m = m->s_next;
190 return m;
191 }
192
193 static const sym_t *
194 first_named_member(const type_t *tp)
195 {
196
197 lint_assert(is_struct_or_union(tp->t_tspec));
198 return skip_unnamed(tp->t_sou->sou_first_member);
199 }
200
201 /*
202 * C99 6.7.8p22 says that the type of an array of unknown size becomes known
203 * at the end of its initializer list.
204 */
205 static void
206 update_type_of_array_of_unknown_size(sym_t *sym, size_t size)
207 {
208
209 type_t *tp = block_dup_type(sym->s_type);
210 tp->t_dim = (int)size;
211 tp->t_incomplete_array = false;
212 sym->s_type = tp;
213 debug_step("completed array type is '%s'", type_name(sym->s_type));
214 outsym(sym, sym->s_scl, sym->s_def);
215 }
216
217
218 /* In traditional C, bit-fields can be initialized only by integer constants. */
219 static void
220 check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
221 {
222
223 if (!allow_c90 &&
224 is_integer(lt) &&
225 ln->tn_type->t_bitfield &&
226 !is_integer(rt)) {
227 /* bit-field initialization is illegal in traditional C */
228 warning(186);
229 }
230 }
231
232 static void
233 check_non_constant_initializer(const tnode_t *tn, const sym_t *sym)
234 {
235
236 if (tn == NULL || tn->tn_op == CON)
237 return;
238
239 const sym_t *unused_sym;
240 ptrdiff_t unused_offs;
241 if (constant_addr(tn, &unused_sym, &unused_offs))
242 return;
243
244 if (has_automatic_storage_duration(sym)) {
245 /* non-constant initializer */
246 c99ism(177);
247 } else {
248 /* non-constant initializer */
249 error(177);
250 }
251 }
252
253 static void
254 check_trad_no_auto_aggregate(const sym_t *sym)
255 {
256
257 if (has_automatic_storage_duration(sym) &&
258 !is_scalar(sym->s_type->t_tspec)) {
259 /* no automatic aggregate initialization in traditional C */
260 warning(188);
261 }
262 }
263
264 static void
265 check_init_expr(const type_t *ltp, sym_t *lsym, tnode_t *rn)
266 {
267
268 type_t *lutp = expr_unqualified_type(ltp);
269
270 /* Create a temporary node for the left side. */
271 tnode_t *ln = expr_zero_alloc(sizeof(*ln), "tnode");
272 ln->tn_op = NAME;
273 ln->tn_type = lutp;
274 ln->tn_lvalue = true;
275 ln->tn_sym = lsym;
276
277 rn = cconv(rn);
278
279 tspec_t lt = ln->tn_type->t_tspec;
280 tspec_t rt = rn->tn_type->t_tspec;
281
282 debug_step("typeok '%s', '%s'",
283 type_name(ln->tn_type), type_name(rn->tn_type));
284 if (!typeok(INIT, 0, ln, rn))
285 return;
286
287 /*
288 * Preserve the tree memory. This is necessary because otherwise expr()
289 * would free it.
290 */
291 memory_pool saved_mem = expr_save_memory();
292 expr(rn, true, false, true, false);
293 expr_restore_memory(saved_mem);
294
295 check_bit_field_init(ln, lt, rt);
296
297 /*
298 * XXX: Is it correct to do this conversion _after_ the typeok above?
299 */
300 if (lt != rt || (ltp->t_bitfield && rn->tn_op == CON))
301 rn = convert(INIT, 0, unconst_cast(ltp), rn);
302
303 check_non_constant_initializer(rn, lsym);
304 }
305
306
307 static const type_t *
308 designator_type(const designator *dr, const type_t *tp)
309 {
310
311 switch (tp->t_tspec) {
312 case STRUCT:
313 case UNION:
314 if (dr->dr_kind != DK_STRUCT && dr->dr_kind != DK_UNION) {
315 const sym_t *fmem = first_named_member(tp);
316 /* syntax error '%s' */
317 error(249, "designator '[...]' is only for arrays");
318 return fmem != NULL ? fmem->s_type : NULL;
319 }
320
321 lint_assert(dr->dr_member != NULL);
322 return dr->dr_member->s_type;
323 case ARRAY:
324 if (dr->dr_kind != DK_ARRAY) {
325 /* syntax error '%s' */
326 error(249,
327 "designator '.member' is only for struct/union");
328 }
329 if (!tp->t_incomplete_array)
330 lint_assert(dr->dr_subscript < (size_t)tp->t_dim);
331 return tp->t_subt;
332 default:
333 if (dr->dr_kind != DK_SCALAR) {
334 /* syntax error '%s' */
335 error(249, "scalar type cannot use designator");
336 }
337 return tp;
338 }
339 }
340
341
342 #ifdef DEBUG
343 static void
344 designator_debug(const designator *dr)
345 {
346
347 if (dr->dr_kind == DK_STRUCT || dr->dr_kind == DK_UNION) {
348 lint_assert(dr->dr_subscript == 0);
349 debug_printf(".%s",
350 dr->dr_member != NULL
351 ? dr->dr_member->s_name
352 : "<end>");
353 } else if (dr->dr_kind == DK_ARRAY) {
354 lint_assert(dr->dr_member == NULL);
355 debug_printf("[%zu]", dr->dr_subscript);
356 } else {
357 lint_assert(dr->dr_member == NULL);
358 lint_assert(dr->dr_subscript == 0);
359 debug_printf("<scalar>");
360 }
361
362 if (dr->dr_done)
363 debug_printf(" (done)");
364 }
365
366 static void
367 designation_debug(const designation *dn)
368 {
369
370 if (dn->dn_len == 0) {
371 debug_step("designation: (empty)");
372 return;
373 }
374
375 debug_printf("designation: ");
376 for (size_t i = 0; i < dn->dn_len; i++)
377 designator_debug(dn->dn_items + i);
378 debug_printf("\n");
379 }
380 #else
381 #define designation_debug(dn) do { } while (false)
382 #endif
383
384 static designator *
385 designation_last(designation *dn)
386 {
387
388 lint_assert(dn->dn_len > 0);
389 return &dn->dn_items[dn->dn_len - 1];
390 }
391
392 void
393 designation_push(designation *dn, designator_kind kind,
394 const sym_t *member, size_t subscript)
395 {
396
397 if (dn->dn_len == dn->dn_cap) {
398 dn->dn_cap += 4;
399 dn->dn_items = xrealloc(dn->dn_items,
400 dn->dn_cap * sizeof(dn->dn_items[0]));
401 }
402
403 designator *dr = &dn->dn_items[dn->dn_len++];
404 dr->dr_kind = kind;
405 dr->dr_member = member;
406 dr->dr_subscript = subscript;
407 dr->dr_done = false;
408 designation_debug(dn);
409 }
410
411 /*
412 * Extend the designation as appropriate for the given type.
413 *
414 * C11 6.7.9p17
415 */
416 static bool
417 designation_descend(designation *dn, const type_t *tp)
418 {
419
420 if (is_struct_or_union(tp->t_tspec)) {
421 const sym_t *member = first_named_member(tp);
422 if (member == NULL)
423 return false;
424 designation_push(dn,
425 tp->t_tspec == STRUCT ? DK_STRUCT : DK_UNION, member, 0);
426 } else if (tp->t_tspec == ARRAY)
427 designation_push(dn, DK_ARRAY, NULL, 0);
428 else
429 designation_push(dn, DK_SCALAR, NULL, 0);
430 return true;
431 }
432
433 /*
434 * Starting at the type of the current object, resolve the type of the
435 * sub-object by following each designator in the list.
436 *
437 * C99 6.7.8p18
438 */
439 static const type_t *
440 designation_type(const designation *dn, const type_t *tp)
441 {
442
443 for (size_t i = 0; i < dn->dn_len && tp != NULL; i++)
444 tp = designator_type(dn->dn_items + i, tp);
445 return tp;
446 }
447
448 static const type_t *
449 designation_parent_type(const designation *dn, const type_t *tp)
450 {
451
452 for (size_t i = 0; i + 1 < dn->dn_len && tp != NULL; i++)
453 tp = designator_type(dn->dn_items + i, tp);
454 return tp;
455 }
456
457
458 static brace_level *
459 brace_level_new(const type_t *tp, brace_level *enclosing)
460 {
461
462 brace_level *bl = xcalloc(1, sizeof(*bl));
463 bl->bl_type = tp;
464 bl->bl_enclosing = enclosing;
465
466 return bl;
467 }
468
469 static void
470 brace_level_free(brace_level *bl)
471 {
472
473 free(bl->bl_designation.dn_items);
474 free(bl);
475 }
476
477 #ifdef DEBUG
478 static void
479 brace_level_debug(const brace_level *bl)
480 {
481
482 lint_assert(bl->bl_type != NULL);
483
484 debug_printf("type '%s'\n", type_name(bl->bl_type));
485 debug_indent_inc();
486 designation_debug(&bl->bl_designation);
487 debug_indent_dec();
488 }
489 #else
490 #define brace_level_debug(level) do { } while (false)
491 #endif
492
493 /* Return the type of the sub-object that is currently being initialized. */
494 static const type_t *
495 brace_level_sub_type(const brace_level *bl)
496 {
497
498 return designation_type(&bl->bl_designation, bl->bl_type);
499 }
500
501 /*
502 * After initializing a sub-object, advance the designation to point after
503 * the sub-object that has just been initialized.
504 *
505 * C99 6.7.8p17
506 * C11 6.7.9p17
507 */
508 static void
509 brace_level_advance(brace_level *bl, size_t *max_subscript)
510 {
511
512 debug_enter();
513 designation *dn = &bl->bl_designation;
514 const type_t *tp = designation_parent_type(dn, bl->bl_type);
515
516 if (bl->bl_designation.dn_len == 0)
517 (void)designation_descend(dn, bl->bl_type);
518
519 designator *dr = designation_last(dn);
520 /* TODO: try to switch on dr->dr_kind instead */
521 switch (tp->t_tspec) {
522 case STRUCT:
523 lint_assert(dr->dr_member != NULL);
524 dr->dr_member = skip_unnamed(dr->dr_member->s_next);
525 if (dr->dr_member == NULL)
526 dr->dr_done = true;
527 break;
528 case UNION:
529 dr->dr_member = NULL;
530 dr->dr_done = true;
531 break;
532 case ARRAY:
533 dr->dr_subscript++;
534 if (tp->t_incomplete_array &&
535 dr->dr_subscript > *max_subscript)
536 *max_subscript = dr->dr_subscript;
537 if (!tp->t_incomplete_array &&
538 dr->dr_subscript >= (size_t)tp->t_dim)
539 dr->dr_done = true;
540 break;
541 default:
542 dr->dr_done = true;
543 break;
544 }
545 designation_debug(dn);
546 debug_leave();
547 }
548
549 static void
550 warn_too_many_initializers(designator_kind kind, const type_t *tp)
551 {
552
553 if (kind == DK_STRUCT || kind == DK_UNION) {
554 /* too many struct/union initializers */
555 error(172);
556 } else if (kind == DK_ARRAY) {
557 lint_assert(tp->t_tspec == ARRAY);
558 lint_assert(!tp->t_incomplete_array);
559 /* too many array initializers, expected %d */
560 error(173, tp->t_dim);
561 } else {
562 /* too many initializers */
563 error(174);
564 }
565 }
566
567 static bool
568 brace_level_pop_done(brace_level *bl, size_t *max_subscript)
569 {
570 designation *dn = &bl->bl_designation;
571 designator_kind dr_kind = designation_last(dn)->dr_kind;
572 const type_t *sub_type = designation_parent_type(dn, bl->bl_type);
573
574 while (designation_last(dn)->dr_done) {
575 dn->dn_len--;
576 designation_debug(dn);
577 if (dn->dn_len == 0) {
578 warn_too_many_initializers(dr_kind, sub_type);
579 return false;
580 }
581 brace_level_advance(bl, max_subscript);
582 }
583 return true;
584 }
585
586 static void
587 brace_level_pop_final(brace_level *bl, size_t *max_subscript)
588 {
589 designation *dn = &bl->bl_designation;
590
591 while (dn->dn_len > 0 && designation_last(dn)->dr_done) {
592 dn->dn_len--;
593 designation_debug(dn);
594 if (dn->dn_len == 0)
595 return;
596 brace_level_advance(bl, max_subscript);
597 }
598 }
599
600 /*
601 * Make the designation point to the sub-object to be initialized next.
602 * Initially or after a previous expression, the designation is not advanced
603 * yet since the place to stop depends on the next expression, especially for
604 * string literals.
605 */
606 static bool
607 brace_level_goto(brace_level *bl, const tnode_t *rn, size_t *max_subscript)
608 {
609
610 designation *dn = &bl->bl_designation;
611 if (dn->dn_len == 0 && can_init_character_array(bl->bl_type, rn))
612 return true;
613 if (dn->dn_len == 0 && !designation_descend(dn, bl->bl_type))
614 return false;
615
616 again:
617 if (!brace_level_pop_done(bl, max_subscript))
618 return false;
619
620 const type_t *ltp = brace_level_sub_type(bl);
621 if (types_compatible(ltp, rn->tn_type, true, false, NULL))
622 return true;
623
624 if (is_struct_or_union(ltp->t_tspec) || ltp->t_tspec == ARRAY) {
625 if (can_init_character_array(ltp, rn))
626 return true;
627 if (!designation_descend(dn, ltp))
628 return false;
629 goto again;
630 }
631
632 return true;
633 }
634
635
636 static initialization *
637 initialization_new(sym_t *sym, initialization *enclosing)
638 {
639
640 initialization *in = xcalloc(1, sizeof(*in));
641 in->in_sym = sym;
642 in->in_enclosing = enclosing;
643
644 return in;
645 }
646
647 static void
648 initialization_free(initialization *in)
649 {
650 brace_level *bl, *next;
651
652 /* TODO: lint_assert(in->in_brace_level == NULL) */
653 for (bl = in->in_brace_level; bl != NULL; bl = next) {
654 next = bl->bl_enclosing;
655 brace_level_free(bl);
656 }
657
658 free(in);
659 }
660
661 #ifdef DEBUG
662 static void
663 initialization_debug(const initialization *in)
664 {
665
666 if (in->in_err)
667 debug_step("initialization error");
668 if (in->in_brace_level == NULL) {
669 debug_step("no brace level");
670 return;
671 }
672
673 const brace_level *bl;
674 size_t i = 0;
675 for (bl = in->in_brace_level; bl != NULL; bl = bl->bl_enclosing) {
676 debug_printf("brace level %zu: ", i);
677 brace_level_debug(bl);
678 i++;
679 }
680 }
681 #else
682 #define initialization_debug(in) do { } while (false)
683 #endif
684
685 /*
686 * Return the type of the object or sub-object that is currently being
687 * initialized.
688 */
689 static const type_t *
690 initialization_sub_type(initialization *in)
691 {
692
693 if (in->in_brace_level == NULL)
694 return in->in_sym->s_type;
695
696 const type_t *tp = brace_level_sub_type(in->in_brace_level);
697 if (tp == NULL)
698 in->in_err = true;
699 return tp;
700 }
701
702 static void
703 initialization_lbrace(initialization *in)
704 {
705
706 if (in->in_err)
707 return;
708
709 debug_enter();
710
711 const type_t *tp = initialization_sub_type(in);
712 if (tp == NULL)
713 goto done;
714
715 brace_level *outer_bl = in->in_brace_level;
716 if (!allow_c90 && outer_bl == NULL)
717 check_trad_no_auto_aggregate(in->in_sym);
718
719 if (!allow_c90 && tp->t_tspec == UNION) {
720 /* initialization of union is illegal in traditional C */
721 warning(238);
722 }
723
724 if (is_struct_or_union(tp->t_tspec) && tp->t_sou->sou_incomplete) {
725 /* initialization of incomplete type '%s' */
726 error(175, type_name(tp));
727 in->in_err = true;
728 goto done;
729 }
730
731 if (outer_bl != NULL && outer_bl->bl_designation.dn_len == 0) {
732 designation *dn = &outer_bl->bl_designation;
733 (void)designation_descend(dn, outer_bl->bl_type);
734 tp = designation_type(dn, outer_bl->bl_type);
735 }
736
737 in->in_brace_level = brace_level_new(tp, outer_bl);
738 if (is_struct_or_union(tp->t_tspec) &&
739 first_named_member(tp) == NULL) {
740 /* cannot initialize struct/union with no named member */
741 error(179);
742 in->in_err = true;
743 }
744
745 done:
746 initialization_debug(in);
747 debug_leave();
748 }
749
750 static void
751 initialization_rbrace(initialization *in)
752 {
753
754 debug_enter();
755
756 if (in->in_brace_level != NULL)
757 brace_level_pop_final(in->in_brace_level,
758 &in->in_max_subscript);
759
760 /* C99 6.7.8p22 */
761 if (in->in_sym->s_type->t_incomplete_array &&
762 in->in_brace_level->bl_enclosing == NULL) {
763
764 /* prevent "empty array declaration for '%s' [190]" */
765 size_t dim = in->in_max_subscript;
766 if (dim == 0 && in->in_err)
767 dim = 1;
768
769 update_type_of_array_of_unknown_size(in->in_sym, dim);
770 }
771
772 if (in->in_err)
773 goto done;
774
775 brace_level *inner_bl = in->in_brace_level;
776 brace_level *outer_bl = inner_bl->bl_enclosing;
777 in->in_brace_level = outer_bl;
778 brace_level_free(inner_bl);
779
780 if (outer_bl != NULL)
781 brace_level_advance(outer_bl, &in->in_max_subscript);
782
783 done:
784 initialization_debug(in);
785 debug_leave();
786 }
787
788 static void
789 initialization_add_designator_member(initialization *in, const char *name)
790 {
791
792 if (in->in_err)
793 return;
794
795 brace_level *bl = in->in_brace_level;
796 lint_assert(bl != NULL);
797
798 const type_t *tp = brace_level_sub_type(bl);
799 if (is_struct_or_union(tp->t_tspec))
800 goto proceed;
801 else if (tp->t_tspec == ARRAY)
802 /* syntax error '%s' */
803 error(249, "designator '.member' is only for struct/union");
804 else
805 /* syntax error '%s' */
806 error(249, "scalar type cannot use designator");
807 in->in_err = true;
808 return;
809
810 proceed:;
811 const sym_t *member = find_member(tp->t_sou, name);
812 if (member == NULL) {
813 /* type '%s' does not have member '%s' */
814 error(101, type_name(tp), name);
815 in->in_err = true;
816 return;
817 }
818
819 designation_push(&bl->bl_designation,
820 tp->t_tspec == STRUCT ? DK_STRUCT : DK_UNION, member, 0);
821 }
822
823 static void
824 initialization_add_designator_subscript(initialization *in, size_t subscript)
825 {
826
827 if (in->in_err)
828 return;
829
830 brace_level *bl = in->in_brace_level;
831 lint_assert(bl != NULL);
832
833 const type_t *tp = brace_level_sub_type(bl);
834 if (tp->t_tspec != ARRAY) {
835 /* syntax error '%s' */
836 error(249, "designator '[...]' is only for arrays");
837 in->in_err = true;
838 return;
839 }
840
841 if (!tp->t_incomplete_array && subscript >= (size_t)tp->t_dim) {
842 /* array subscript cannot be > %d: %ld */
843 error(168, tp->t_dim - 1, (long)subscript);
844 subscript = 0; /* suppress further errors */
845 }
846
847 if (tp->t_incomplete_array && subscript > in->in_max_subscript)
848 in->in_max_subscript = subscript;
849
850 designation_push(&bl->bl_designation, DK_ARRAY, NULL, subscript);
851 }
852
853 /*
854 * Initialize an object with automatic storage duration that has an
855 * initializer expression without braces.
856 */
857 static bool
858 initialization_expr_using_op(initialization *in, tnode_t *rn)
859 {
860
861 if (!has_automatic_storage_duration(in->in_sym))
862 return false;
863 if (in->in_brace_level != NULL)
864 return false;
865 if (in->in_sym->s_type->t_tspec == ARRAY)
866 return false;
867
868 debug_step("handing over to INIT");
869
870 tnode_t *ln = build_name(in->in_sym, false);
871 ln->tn_type = expr_unqualified_type(ln->tn_type);
872
873 tnode_t *tn = build_binary(ln, INIT, false /* XXX */, rn);
874 expr(tn, false, false, false, false);
875
876 return true;
877 }
878
879 /* Initialize a character array or wchar_t array with a string literal. */
880 static bool
881 initialization_init_array_from_string(initialization *in, tnode_t *tn)
882 {
883
884 if (tn->tn_op != STRING)
885 return false;
886
887 const type_t *tp = initialization_sub_type(in);
888
889 if (!can_init_character_array(tp, tn))
890 return false;
891
892 size_t len = tn->tn_string->st_len;
893 if (!tp->t_incomplete_array && (size_t)tp->t_dim < len) {
894 /* string literal too long (%lu) for target array (%lu) */
895 warning(187, (unsigned long)len, (unsigned long)tp->t_dim);
896 }
897
898 brace_level *bl = in->in_brace_level;
899 if (bl != NULL && bl->bl_designation.dn_len == 0)
900 (void)designation_descend(&bl->bl_designation, bl->bl_type);
901 if (bl != NULL)
902 brace_level_advance(bl, &in->in_max_subscript);
903
904 if (tp->t_incomplete_array)
905 update_type_of_array_of_unknown_size(in->in_sym, len + 1);
906
907 return true;
908 }
909
910 /*
911 * Initialize a single sub-object as part of the currently ongoing
912 * initialization.
913 */
914 static void
915 initialization_expr(initialization *in, tnode_t *tn)
916 {
917
918 if (in->in_err || tn == NULL)
919 return;
920
921 debug_enter();
922
923 brace_level *bl = in->in_brace_level;
924 if (bl != NULL && !brace_level_goto(bl, tn, &in->in_max_subscript)) {
925 in->in_err = true;
926 goto done;
927 }
928 if (initialization_expr_using_op(in, tn))
929 goto done;
930 if (initialization_init_array_from_string(in, tn))
931 goto done;
932 if (in->in_err)
933 goto done;
934
935 const type_t *tp = initialization_sub_type(in);
936 if (tp == NULL)
937 goto done;
938
939 if (bl == NULL && !is_scalar(tp->t_tspec)) {
940 /* {}-enclosed or constant initializer of type '%s' required */
941 error(181, type_name(in->in_sym->s_type));
942 goto done;
943 }
944
945 debug_step("expecting '%s', expression has '%s'",
946 type_name(tp), type_name(tn->tn_type));
947 check_init_expr(tp, in->in_sym, tn);
948 if (bl != NULL)
949 brace_level_advance(bl, &in->in_max_subscript);
950
951 done:
952 initialization_debug(in);
953 debug_leave();
954 }
955
956
957 static initialization *init;
958
959
960 sym_t *
961 current_initsym(void)
962 {
963
964 return init->in_sym;
965 }
966
967 void
968 begin_initialization(sym_t *sym)
969 {
970
971 debug_step("begin initialization of '%s'", type_name(sym->s_type));
972 debug_indent_inc();
973
974 init = initialization_new(sym, init);
975 }
976
977 void
978 end_initialization(void)
979 {
980
981 initialization *in = init;
982 init = in->in_enclosing;
983 initialization_free(in);
984
985 debug_indent_dec();
986 debug_step("end initialization");
987 }
988
989 void
990 begin_designation(void)
991 {
992
993 initialization *in = init;
994 if (in->in_err)
995 return;
996
997 brace_level *bl = in->in_brace_level;
998 lint_assert(bl != NULL);
999 bl->bl_designation.dn_len = 0;
1000 designation_debug(&bl->bl_designation);
1001 }
1002
1003 void
1004 add_designator_member(sbuf_t *sb)
1005 {
1006
1007 initialization_add_designator_member(init, sb->sb_name);
1008 }
1009
1010 void
1011 add_designator_subscript(range_t range)
1012 {
1013
1014 initialization_add_designator_subscript(init, range.hi);
1015 }
1016
1017 void
1018 init_lbrace(void)
1019 {
1020
1021 initialization_lbrace(init);
1022 }
1023
1024 void
1025 init_expr(tnode_t *tn)
1026 {
1027
1028 initialization_expr(init, tn);
1029 }
1030
1031 void
1032 init_rbrace(void)
1033 {
1034
1035 initialization_rbrace(init);
1036 }
1037