init.c revision 1.258 1 /* $NetBSD: init.c,v 1.258 2024/02/03 19:25:16 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.258 2024/02/03 19:25:16 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_MEMBER) {
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_SUBSCRIPT) {
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_MEMBER) {
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_SUBSCRIPT) {
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, DK_MEMBER, member, 0);
425 } else if (tp->t_tspec == ARRAY)
426 designation_push(dn, DK_SUBSCRIPT, NULL, 0);
427 else
428 designation_push(dn, DK_SCALAR, NULL, 0);
429 return true;
430 }
431
432 /*
433 * Starting at the type of the current object, resolve the type of the
434 * sub-object by following each designator in the list.
435 *
436 * C99 6.7.8p18
437 */
438 static const type_t *
439 designation_type(const designation *dn, const type_t *tp)
440 {
441
442 for (size_t i = 0; i < dn->dn_len && tp != NULL; i++)
443 tp = designator_type(dn->dn_items + i, tp);
444 return tp;
445 }
446
447 static const type_t *
448 designation_parent_type(const designation *dn, const type_t *tp)
449 {
450
451 for (size_t i = 0; i + 1 < dn->dn_len && tp != NULL; i++)
452 tp = designator_type(dn->dn_items + i, tp);
453 return tp;
454 }
455
456
457 static brace_level *
458 brace_level_new(const type_t *tp, brace_level *enclosing)
459 {
460
461 brace_level *bl = xcalloc(1, sizeof(*bl));
462 bl->bl_type = tp;
463 bl->bl_enclosing = enclosing;
464
465 return bl;
466 }
467
468 static void
469 brace_level_free(brace_level *bl)
470 {
471
472 free(bl->bl_designation.dn_items);
473 free(bl);
474 }
475
476 #ifdef DEBUG
477 static void
478 brace_level_debug(const brace_level *bl)
479 {
480
481 lint_assert(bl->bl_type != NULL);
482
483 debug_printf("type '%s'\n", type_name(bl->bl_type));
484 debug_indent_inc();
485 designation_debug(&bl->bl_designation);
486 debug_indent_dec();
487 }
488 #else
489 #define brace_level_debug(level) do { } while (false)
490 #endif
491
492 /* Return the type of the sub-object that is currently being initialized. */
493 static const type_t *
494 brace_level_sub_type(const brace_level *bl)
495 {
496
497 return designation_type(&bl->bl_designation, bl->bl_type);
498 }
499
500 /*
501 * After initializing a sub-object, advance the designation to point after
502 * the sub-object that has just been initialized.
503 *
504 * C99 6.7.8p17
505 * C11 6.7.9p17
506 */
507 static void
508 brace_level_advance(brace_level *bl, size_t *max_subscript)
509 {
510
511 debug_enter();
512 designation *dn = &bl->bl_designation;
513 const type_t *tp = designation_parent_type(dn, bl->bl_type);
514
515 if (bl->bl_designation.dn_len == 0)
516 (void)designation_descend(dn, bl->bl_type);
517
518 designator *dr = designation_last(dn);
519 /* TODO: try to switch on dr->dr_kind instead */
520 switch (tp->t_tspec) {
521 case STRUCT:
522 lint_assert(dr->dr_member != NULL);
523 dr->dr_member = skip_unnamed(dr->dr_member->s_next);
524 if (dr->dr_member == NULL)
525 dr->dr_done = true;
526 break;
527 case UNION:
528 dr->dr_member = NULL;
529 dr->dr_done = true;
530 break;
531 case ARRAY:
532 dr->dr_subscript++;
533 if (tp->t_incomplete_array &&
534 dr->dr_subscript > *max_subscript)
535 *max_subscript = dr->dr_subscript;
536 if (!tp->t_incomplete_array &&
537 dr->dr_subscript >= (size_t)tp->t_dim)
538 dr->dr_done = true;
539 break;
540 default:
541 dr->dr_done = true;
542 break;
543 }
544 designation_debug(dn);
545 debug_leave();
546 }
547
548 static void
549 warn_too_many_initializers(designator_kind kind, const type_t *tp)
550 {
551
552 if (kind == DK_MEMBER) {
553 /* too many struct/union initializers */
554 error(172);
555 } else if (kind == DK_SUBSCRIPT) {
556 lint_assert(tp->t_tspec == ARRAY);
557 lint_assert(!tp->t_incomplete_array);
558 /* too many array initializers, expected %d */
559 error(173, tp->t_dim);
560 } else {
561 /* too many initializers */
562 error(174);
563 }
564 }
565
566 static bool
567 brace_level_pop_done(brace_level *bl, size_t *max_subscript)
568 {
569 designation *dn = &bl->bl_designation;
570 designator_kind dr_kind = designation_last(dn)->dr_kind;
571 const type_t *sub_type = designation_parent_type(dn, bl->bl_type);
572
573 while (designation_last(dn)->dr_done) {
574 dn->dn_len--;
575 designation_debug(dn);
576 if (dn->dn_len == 0) {
577 warn_too_many_initializers(dr_kind, sub_type);
578 return false;
579 }
580 brace_level_advance(bl, max_subscript);
581 }
582 return true;
583 }
584
585 static void
586 brace_level_pop_final(brace_level *bl, size_t *max_subscript)
587 {
588 designation *dn = &bl->bl_designation;
589
590 while (dn->dn_len > 0 && designation_last(dn)->dr_done) {
591 dn->dn_len--;
592 designation_debug(dn);
593 if (dn->dn_len == 0)
594 return;
595 brace_level_advance(bl, max_subscript);
596 }
597 }
598
599 /*
600 * Make the designation point to the sub-object to be initialized next.
601 * Initially or after a previous expression, the designation is not advanced
602 * yet since the place to stop depends on the next expression, especially for
603 * string literals.
604 */
605 static bool
606 brace_level_goto(brace_level *bl, const tnode_t *rn, size_t *max_subscript)
607 {
608
609 designation *dn = &bl->bl_designation;
610 if (dn->dn_len == 0 && can_init_character_array(bl->bl_type, rn))
611 return true;
612 if (dn->dn_len == 0 && !designation_descend(dn, bl->bl_type))
613 return false;
614
615 again:
616 if (!brace_level_pop_done(bl, max_subscript))
617 return false;
618
619 const type_t *ltp = brace_level_sub_type(bl);
620 if (types_compatible(ltp, rn->tn_type, true, false, NULL))
621 return true;
622
623 if (is_struct_or_union(ltp->t_tspec) || ltp->t_tspec == ARRAY) {
624 if (can_init_character_array(ltp, rn))
625 return true;
626 if (!designation_descend(dn, ltp))
627 return false;
628 goto again;
629 }
630
631 return true;
632 }
633
634
635 static initialization *
636 initialization_new(sym_t *sym, initialization *enclosing)
637 {
638
639 initialization *in = xcalloc(1, sizeof(*in));
640 in->in_sym = sym;
641 in->in_enclosing = enclosing;
642
643 return in;
644 }
645
646 static void
647 initialization_free(initialization *in)
648 {
649 brace_level *bl, *next;
650
651 /* TODO: lint_assert(in->in_brace_level == NULL) */
652 for (bl = in->in_brace_level; bl != NULL; bl = next) {
653 next = bl->bl_enclosing;
654 brace_level_free(bl);
655 }
656
657 free(in);
658 }
659
660 #ifdef DEBUG
661 static void
662 initialization_debug(const initialization *in)
663 {
664
665 if (in->in_err)
666 debug_step("initialization error");
667 if (in->in_brace_level == NULL) {
668 debug_step("no brace level");
669 return;
670 }
671
672 const brace_level *bl;
673 size_t i = 0;
674 for (bl = in->in_brace_level; bl != NULL; bl = bl->bl_enclosing) {
675 debug_printf("brace level %zu: ", i);
676 brace_level_debug(bl);
677 i++;
678 }
679 }
680 #else
681 #define initialization_debug(in) do { } while (false)
682 #endif
683
684 /*
685 * Return the type of the object or sub-object that is currently being
686 * initialized.
687 */
688 static const type_t *
689 initialization_sub_type(initialization *in)
690 {
691
692 if (in->in_brace_level == NULL)
693 return in->in_sym->s_type;
694
695 const type_t *tp = brace_level_sub_type(in->in_brace_level);
696 if (tp == NULL)
697 in->in_err = true;
698 return tp;
699 }
700
701 static void
702 initialization_lbrace(initialization *in)
703 {
704
705 if (in->in_err)
706 return;
707
708 debug_enter();
709
710 const type_t *tp = initialization_sub_type(in);
711 if (tp == NULL)
712 goto done;
713
714 brace_level *outer_bl = in->in_brace_level;
715 if (!allow_c90 && outer_bl == NULL)
716 check_trad_no_auto_aggregate(in->in_sym);
717
718 if (!allow_c90 && tp->t_tspec == UNION) {
719 /* initialization of union is illegal in traditional C */
720 warning(238);
721 }
722
723 if (is_struct_or_union(tp->t_tspec) && tp->t_sou->sou_incomplete) {
724 /* initialization of incomplete type '%s' */
725 error(175, type_name(tp));
726 in->in_err = true;
727 goto done;
728 }
729
730 if (outer_bl != NULL && outer_bl->bl_designation.dn_len == 0) {
731 designation *dn = &outer_bl->bl_designation;
732 (void)designation_descend(dn, outer_bl->bl_type);
733 tp = designation_type(dn, outer_bl->bl_type);
734 }
735
736 in->in_brace_level = brace_level_new(tp, outer_bl);
737 if (is_struct_or_union(tp->t_tspec) &&
738 first_named_member(tp) == NULL) {
739 /* cannot initialize struct/union with no named member */
740 error(179);
741 in->in_err = true;
742 }
743
744 done:
745 initialization_debug(in);
746 debug_leave();
747 }
748
749 static void
750 initialization_rbrace(initialization *in)
751 {
752
753 debug_enter();
754
755 if (in->in_brace_level != NULL)
756 brace_level_pop_final(in->in_brace_level,
757 &in->in_max_subscript);
758
759 /* C99 6.7.8p22 */
760 if (in->in_sym->s_type->t_incomplete_array &&
761 in->in_brace_level->bl_enclosing == NULL) {
762
763 /* prevent "empty array declaration for '%s' [190]" */
764 size_t dim = in->in_max_subscript;
765 if (dim == 0 && in->in_err)
766 dim = 1;
767
768 update_type_of_array_of_unknown_size(in->in_sym, dim);
769 }
770
771 if (in->in_err)
772 goto done;
773
774 brace_level *inner_bl = in->in_brace_level;
775 brace_level *outer_bl = inner_bl->bl_enclosing;
776 in->in_brace_level = outer_bl;
777 brace_level_free(inner_bl);
778
779 if (outer_bl != NULL)
780 brace_level_advance(outer_bl, &in->in_max_subscript);
781
782 done:
783 initialization_debug(in);
784 debug_leave();
785 }
786
787 static void
788 initialization_add_designator_member(initialization *in, const char *name)
789 {
790
791 if (in->in_err)
792 return;
793
794 brace_level *bl = in->in_brace_level;
795 lint_assert(bl != NULL);
796
797 const type_t *tp = brace_level_sub_type(bl);
798 if (is_struct_or_union(tp->t_tspec))
799 goto proceed;
800 else if (tp->t_tspec == ARRAY)
801 /* syntax error '%s' */
802 error(249, "designator '.member' is only for struct/union");
803 else
804 /* syntax error '%s' */
805 error(249, "scalar type cannot use designator");
806 in->in_err = true;
807 return;
808
809 proceed:;
810 const sym_t *member = find_member(tp->t_sou, name);
811 if (member == NULL) {
812 /* type '%s' does not have member '%s' */
813 error(101, type_name(tp), name);
814 in->in_err = true;
815 return;
816 }
817
818 designation_push(&bl->bl_designation, DK_MEMBER, member, 0);
819 }
820
821 static void
822 initialization_add_designator_subscript(initialization *in, size_t subscript)
823 {
824
825 if (in->in_err)
826 return;
827
828 brace_level *bl = in->in_brace_level;
829 lint_assert(bl != NULL);
830
831 const type_t *tp = brace_level_sub_type(bl);
832 if (tp->t_tspec != ARRAY) {
833 /* syntax error '%s' */
834 error(249, "designator '[...]' is only for arrays");
835 in->in_err = true;
836 return;
837 }
838
839 if (!tp->t_incomplete_array && subscript >= (size_t)tp->t_dim) {
840 /* array subscript cannot be > %d: %ld */
841 error(168, tp->t_dim - 1, (long)subscript);
842 subscript = 0; /* suppress further errors */
843 }
844
845 if (tp->t_incomplete_array && subscript > in->in_max_subscript)
846 in->in_max_subscript = subscript;
847
848 designation_push(&bl->bl_designation, DK_SUBSCRIPT, NULL, subscript);
849 }
850
851 /*
852 * Initialize an object with automatic storage duration that has an
853 * initializer expression without braces.
854 */
855 static bool
856 initialization_expr_using_op(initialization *in, tnode_t *rn)
857 {
858
859 if (!has_automatic_storage_duration(in->in_sym))
860 return false;
861 if (in->in_brace_level != NULL)
862 return false;
863 if (in->in_sym->s_type->t_tspec == ARRAY)
864 return false;
865
866 debug_step("handing over to INIT");
867
868 tnode_t *ln = build_name(in->in_sym, false);
869 ln->tn_type = expr_unqualified_type(ln->tn_type);
870
871 tnode_t *tn = build_binary(ln, INIT, false /* XXX */, rn);
872 expr(tn, false, false, false, false);
873
874 return true;
875 }
876
877 /* Initialize a character array or wchar_t array with a string literal. */
878 static bool
879 initialization_init_array_from_string(initialization *in, tnode_t *tn)
880 {
881
882 if (tn->tn_op != STRING)
883 return false;
884
885 const type_t *tp = initialization_sub_type(in);
886
887 if (!can_init_character_array(tp, tn))
888 return false;
889
890 size_t len = tn->tn_string->len;
891 if (tn->tn_string->data != NULL) {
892 quoted_iterator it = { .start = 0 };
893 for (len = 0; quoted_next(tn->tn_string, &it); len++)
894 continue;
895 }
896
897 if (!tp->t_incomplete_array && (size_t)tp->t_dim < len) {
898 /* string literal too long (%lu) for target array (%lu) */
899 warning(187, (unsigned long)len, (unsigned long)tp->t_dim);
900 }
901
902 brace_level *bl = in->in_brace_level;
903 if (bl != NULL && bl->bl_designation.dn_len == 0)
904 (void)designation_descend(&bl->bl_designation, bl->bl_type);
905 if (bl != NULL)
906 brace_level_advance(bl, &in->in_max_subscript);
907
908 if (tp->t_incomplete_array)
909 update_type_of_array_of_unknown_size(in->in_sym, len + 1);
910
911 return true;
912 }
913
914 /*
915 * Initialize a single sub-object as part of the currently ongoing
916 * initialization.
917 */
918 static void
919 initialization_expr(initialization *in, tnode_t *tn)
920 {
921
922 if (in->in_err || tn == NULL)
923 return;
924
925 debug_enter();
926
927 brace_level *bl = in->in_brace_level;
928 if (bl != NULL && !brace_level_goto(bl, tn, &in->in_max_subscript)) {
929 in->in_err = true;
930 goto done;
931 }
932 if (initialization_expr_using_op(in, tn))
933 goto done;
934 if (initialization_init_array_from_string(in, tn))
935 goto done;
936 if (in->in_err)
937 goto done;
938
939 const type_t *tp = initialization_sub_type(in);
940 if (tp == NULL)
941 goto done;
942
943 if (bl == NULL && !is_scalar(tp->t_tspec)) {
944 /* {}-enclosed or constant initializer of type '%s' required */
945 error(181, type_name(in->in_sym->s_type));
946 goto done;
947 }
948
949 debug_step("expecting '%s', expression has '%s'",
950 type_name(tp), type_name(tn->tn_type));
951 check_init_expr(tp, in->in_sym, tn);
952 if (bl != NULL)
953 brace_level_advance(bl, &in->in_max_subscript);
954
955 done:
956 initialization_debug(in);
957 debug_leave();
958 }
959
960
961 static initialization *init;
962
963
964 sym_t *
965 current_initsym(void)
966 {
967
968 return init->in_sym;
969 }
970
971 void
972 begin_initialization(sym_t *sym)
973 {
974
975 debug_step("begin initialization of '%s'", type_name(sym->s_type));
976 debug_indent_inc();
977
978 init = initialization_new(sym, init);
979 }
980
981 void
982 end_initialization(void)
983 {
984
985 initialization *in = init;
986 init = in->in_enclosing;
987 initialization_free(in);
988
989 debug_indent_dec();
990 debug_step("end initialization");
991 }
992
993 void
994 begin_designation(void)
995 {
996
997 initialization *in = init;
998 if (in->in_err)
999 return;
1000
1001 designation *dn = &in->in_brace_level->bl_designation;
1002 dn->dn_len = 0;
1003 designation_debug(dn);
1004 }
1005
1006 void
1007 add_designator_member(sbuf_t *sb)
1008 {
1009
1010 initialization_add_designator_member(init, sb->sb_name);
1011 }
1012
1013 void
1014 add_designator_subscript(range_t range)
1015 {
1016
1017 initialization_add_designator_subscript(init, range.hi);
1018 }
1019
1020 void
1021 init_lbrace(void)
1022 {
1023
1024 initialization_lbrace(init);
1025 }
1026
1027 void
1028 init_expr(tnode_t *tn)
1029 {
1030
1031 initialization_expr(init, tn);
1032 }
1033
1034 void
1035 init_rbrace(void)
1036 {
1037
1038 initialization_rbrace(init);
1039 }
1040