init.c revision 1.238 1 /* $NetBSD: init.c,v 1.238 2023/01/13 19:41:50 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.238 2023/01/13 19:41:50 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 typedef enum designator_kind {
80 DK_STRUCT, /* .member */
81 DK_UNION, /* .member */
82 DK_ARRAY, /* [subscript] */
83 /* TODO: actually necessary? */
84 DK_SCALAR /* no textual representation, not generated
85 * by the parser */
86 } designator_kind;
87
88 /*
89 * A single component on the path from the "current object" of a brace level
90 * to the sub-object that is initialized by an expression.
91 *
92 * C99 6.7.8p6, 6.7.8p7
93 */
94 typedef struct designator {
95 designator_kind dr_kind;
96 const sym_t *dr_member; /* for DK_STRUCT and DK_UNION */
97 size_t dr_subscript; /* for DK_ARRAY */
98 bool dr_done;
99 } designator;
100
101 /*
102 * The path from the "current object" of a brace level to the sub-object that
103 * is initialized by an expression. Examples for designations are '.member'
104 * or '.member[123].member.member[1][1]'.
105 *
106 * C99 6.7.8p6, 6.7.8p7
107 */
108 typedef struct designation {
109 designator *dn_items;
110 size_t dn_len;
111 size_t dn_cap;
112 } designation;
113
114 /*
115 * Everything that happens between a '{' and the corresponding '}', as part
116 * of an initialization.
117 *
118 * Each brace level has a "current object". For the outermost brace level,
119 * it is the same as the object to be initialized. Each nested '{' begins a
120 * nested brace level, for the sub-object pointed to by the designator of the
121 * outer brace level.
122 *
123 * C99 6.7.8p17
124 */
125 typedef struct brace_level {
126 /* The type of the "current object". */
127 const type_t *bl_type;
128
129 /*
130 * The path from the "current object" to the sub-object that is
131 * initialized by the next expression.
132 *
133 * Initially, the designation is empty. Before handling an
134 * expression, the designation is updated to point to the
135 * corresponding sub-object to be initialized. After handling an
136 * expression, the designation is marked as done. It is later
137 * advanced as necessary.
138 */
139 designation bl_designation;
140
141 struct brace_level *bl_enclosing;
142 } brace_level;
143
144 /*
145 * An ongoing initialization.
146 *
147 * In most cases there is only ever a single initialization at a time. See
148 * pointer_to_compound_literal in msg_171.c for a real-life counterexample.
149 */
150 typedef struct initialization {
151 /* The symbol that is to be initialized. */
152 sym_t *in_sym;
153
154 /* The innermost brace level. */
155 brace_level *in_brace_level;
156
157 /*
158 * The maximum subscript that has ever been seen for an array of
159 * unknown size, which can only occur at the outermost brace level.
160 */
161 size_t in_max_subscript;
162
163 /*
164 * Is set when a structural error occurred in the initialization.
165 * If set, the rest of the initialization is still parsed, but the
166 * initialization assignments are not checked.
167 */
168 bool in_err;
169
170 struct initialization *in_enclosing;
171 } initialization;
172
173
174 static void *
175 unconst_cast(const void *p)
176 {
177 void *r;
178
179 memcpy(&r, &p, sizeof(r));
180 return r;
181 }
182
183 static bool
184 has_automatic_storage_duration(const sym_t *sym)
185 {
186
187 return sym->s_scl == AUTO || sym->s_scl == REG;
188 }
189
190 /*
191 * Test whether rn is a string literal that can initialize ltp.
192 *
193 * See also:
194 * C99 6.7.8p14 for plain character strings
195 * C99 6.7.8p15 for wide character strings
196 */
197 static bool
198 can_init_character_array(const type_t *ltp, const tnode_t *rn)
199 {
200 tspec_t lst, rst;
201
202 if (!(ltp != NULL && ltp->t_tspec == ARRAY && rn->tn_op == STRING))
203 return false;
204
205 lst = ltp->t_subt->t_tspec;
206 rst = rn->tn_type->t_subt->t_tspec;
207
208 return rst == CHAR
209 ? lst == CHAR || lst == UCHAR || lst == SCHAR
210 : lst == WCHAR;
211 }
212
213 /* C99 6.7.8p9 */
214 static const sym_t *
215 skip_unnamed(const sym_t *m)
216 {
217
218 while (m != NULL && m->s_name == unnamed)
219 m = m->s_next;
220 return m;
221 }
222
223 static const sym_t *
224 first_named_member(const type_t *tp)
225 {
226
227 lint_assert(is_struct_or_union(tp->t_tspec));
228 return skip_unnamed(tp->t_str->sou_first_member);
229 }
230
231 static const sym_t *
232 look_up_member(const type_t *tp, const char *name)
233 {
234 const sym_t *m;
235
236 lint_assert(is_struct_or_union(tp->t_tspec));
237 for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next)
238 if (strcmp(m->s_name, name) == 0)
239 return m;
240 return NULL;
241 }
242
243 /*
244 * C99 6.7.8p22 says that the type of an array of unknown size becomes known
245 * at the end of its initializer list.
246 */
247 static void
248 update_type_of_array_of_unknown_size(sym_t *sym, size_t size)
249 {
250 type_t *tp;
251
252 tp = block_dup_type(sym->s_type);
253 tp->t_dim = (int)size;
254 tp->t_incomplete_array = false;
255 sym->s_type = tp;
256 debug_step("completed array type is '%s'", type_name(sym->s_type));
257 }
258
259
260 /* In traditional C, bit-fields can be initialized only by integer constants. */
261 static void
262 check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
263 {
264
265 if (!allow_c90 &&
266 is_integer(lt) &&
267 ln->tn_type->t_bitfield &&
268 !is_integer(rt)) {
269 /* bit-field initialization is illegal in traditional C */
270 warning(186);
271 }
272 }
273
274 static void
275 check_non_constant_initializer(const tnode_t *tn, const sym_t *sym)
276 {
277 const sym_t *unused_sym;
278 ptrdiff_t unused_offs;
279
280 if (tn == NULL || tn->tn_op == CON)
281 return;
282
283 if (constant_addr(tn, &unused_sym, &unused_offs))
284 return;
285
286 if (has_automatic_storage_duration(sym)) {
287 /* non-constant initializer */
288 c99ism(177);
289 } else {
290 /* non-constant initializer */
291 error(177);
292 }
293 }
294
295 static void
296 check_trad_no_auto_aggregate(const sym_t *sym)
297 {
298
299 if (has_automatic_storage_duration(sym) &&
300 !is_scalar(sym->s_type->t_tspec)) {
301 /* no automatic aggregate initialization in traditional C */
302 warning(188);
303 }
304 }
305
306 static void
307 check_init_expr(const type_t *ltp, sym_t *lsym, tnode_t *rn)
308 {
309 tnode_t *ln;
310 type_t *lutp;
311 tspec_t lt, rt;
312
313 lutp = expr_unqualified_type(ltp);
314
315 /* Create a temporary node for the left side. */
316 ln = expr_zero_alloc(sizeof(*ln));
317 ln->tn_op = NAME;
318 ln->tn_type = lutp;
319 ln->tn_lvalue = true;
320 ln->tn_sym = lsym;
321
322 rn = cconv(rn);
323
324 lt = ln->tn_type->t_tspec;
325 rt = rn->tn_type->t_tspec;
326
327 debug_step("typeok '%s', '%s'",
328 type_name(ln->tn_type), type_name(rn->tn_type));
329 if (!typeok(INIT, 0, ln, rn))
330 return;
331
332 /*
333 * Preserve the tree memory. This is necessary because otherwise
334 * expr() would free it.
335 */
336 memory_pool saved_mem = expr_save_memory();
337 expr(rn, true, false, true, false);
338 expr_restore_memory(saved_mem);
339
340 check_bit_field_init(ln, lt, rt);
341
342 /*
343 * XXX: Is it correct to do this conversion _after_ the typeok above?
344 */
345 if (lt != rt || (ltp->t_bitfield && rn->tn_op == CON))
346 rn = convert(INIT, 0, unconst_cast(ltp), rn);
347
348 check_non_constant_initializer(rn, lsym);
349 }
350
351
352 static const type_t *
353 designator_type(const designator *dr, const type_t *tp)
354 {
355 switch (tp->t_tspec) {
356 case STRUCT:
357 case UNION:
358 if (dr->dr_kind != DK_STRUCT && dr->dr_kind != DK_UNION) {
359 const sym_t *fmem = first_named_member(tp);
360 /* syntax error '%s' */
361 error(249, "designator '[...]' is only for arrays");
362 return fmem != NULL ? fmem->s_type : NULL;
363 }
364
365 lint_assert(dr->dr_member != NULL);
366 return dr->dr_member->s_type;
367 case ARRAY:
368 if (dr->dr_kind != DK_ARRAY) {
369 /* syntax error '%s' */
370 error(249,
371 "designator '.member' is only for struct/union");
372 }
373 if (!tp->t_incomplete_array)
374 lint_assert(dr->dr_subscript < (size_t)tp->t_dim);
375 return tp->t_subt;
376 default:
377 if (dr->dr_kind != DK_SCALAR) {
378 /* syntax error '%s' */
379 error(249, "scalar type cannot use designator");
380 }
381 return tp;
382 }
383 }
384
385
386 #ifdef DEBUG
387 static void
388 designator_debug(const designator *dr)
389 {
390
391 if (dr->dr_kind == DK_STRUCT || dr->dr_kind == DK_UNION) {
392 lint_assert(dr->dr_subscript == 0);
393 debug_printf(".%s",
394 dr->dr_member != NULL
395 ? dr->dr_member->s_name
396 : "<end>");
397 } else if (dr->dr_kind == DK_ARRAY) {
398 lint_assert(dr->dr_member == NULL);
399 debug_printf("[%zu]", dr->dr_subscript);
400 } else {
401 lint_assert(dr->dr_member == NULL);
402 lint_assert(dr->dr_subscript == 0);
403 debug_printf("<scalar>");
404 }
405
406 if (dr->dr_done)
407 debug_printf(" (done)");
408 }
409
410 static void
411 designation_debug(const designation *dn)
412 {
413 size_t i;
414
415 if (dn->dn_len == 0) {
416 debug_step("designation: (empty)");
417 return;
418 }
419
420 debug_print_indent();
421 debug_printf("designation: ");
422 for (i = 0; i < dn->dn_len; i++)
423 designator_debug(dn->dn_items + i);
424 debug_printf("\n");
425 }
426 #else
427 #define designation_debug(dn) do { } while (false)
428 #endif
429
430 static designator *
431 designation_last(designation *dn)
432 {
433
434 lint_assert(dn->dn_len > 0);
435 return &dn->dn_items[dn->dn_len - 1];
436 }
437
438 static void
439 designation_push(designation *dn, designator_kind kind,
440 const sym_t *member, size_t subscript)
441 {
442 designator *dr;
443
444 if (dn->dn_len == dn->dn_cap) {
445 dn->dn_cap += 4;
446 dn->dn_items = xrealloc(dn->dn_items,
447 dn->dn_cap * sizeof(dn->dn_items[0]));
448 }
449
450 dr = &dn->dn_items[dn->dn_len++];
451 dr->dr_kind = kind;
452 dr->dr_member = member;
453 dr->dr_subscript = subscript;
454 dr->dr_done = false;
455 designation_debug(dn);
456 }
457
458 /*
459 * Extend the designation as appropriate for the given type.
460 *
461 * C11 6.7.9p17
462 */
463 static bool
464 designation_descend(designation *dn, const type_t *tp)
465 {
466
467 if (is_struct_or_union(tp->t_tspec)) {
468 const sym_t *member = first_named_member(tp);
469 if (member == NULL)
470 return false;
471 designation_push(dn,
472 tp->t_tspec == STRUCT ? DK_STRUCT : DK_UNION, member, 0);
473 } else if (tp->t_tspec == ARRAY)
474 designation_push(dn, DK_ARRAY, NULL, 0);
475 else
476 designation_push(dn, DK_SCALAR, NULL, 0);
477 return true;
478 }
479
480 /*
481 * Starting at the type of the current object, resolve the type of the
482 * sub-object by following each designator in the list.
483 *
484 * C99 6.7.8p18
485 */
486 static const type_t *
487 designation_type(const designation *dn, const type_t *tp)
488 {
489 size_t i;
490
491 for (i = 0; i < dn->dn_len && tp != NULL; i++)
492 tp = designator_type(dn->dn_items + i, tp);
493 return tp;
494 }
495
496 static const type_t *
497 designation_parent_type(const designation *dn, const type_t *tp)
498 {
499 size_t i;
500
501 for (i = 0; i + 1 < dn->dn_len && tp != NULL; i++)
502 tp = designator_type(dn->dn_items + i, tp);
503 return tp;
504 }
505
506
507 static brace_level *
508 brace_level_new(const type_t *tp, brace_level *enclosing)
509 {
510 brace_level *bl;
511
512 bl = xcalloc(1, sizeof(*bl));
513 bl->bl_type = tp;
514 bl->bl_enclosing = enclosing;
515
516 return bl;
517 }
518
519 static void
520 brace_level_free(brace_level *bl)
521 {
522
523 free(bl->bl_designation.dn_items);
524 free(bl);
525 }
526
527 #ifdef DEBUG
528 static void
529 brace_level_debug(const brace_level *bl)
530 {
531
532 lint_assert(bl->bl_type != NULL);
533
534 debug_printf("type '%s'\n", type_name(bl->bl_type));
535 debug_indent_inc();
536 designation_debug(&bl->bl_designation);
537 debug_indent_dec();
538 }
539 #else
540 #define brace_level_debug(level) do { } while (false)
541 #endif
542
543 /* Return the type of the sub-object that is currently being initialized. */
544 static const type_t *
545 brace_level_sub_type(const brace_level *bl)
546 {
547
548 return designation_type(&bl->bl_designation, bl->bl_type);
549 }
550
551 /*
552 * After initializing a sub-object, advance the designation to point after
553 * the sub-object that has just been initialized.
554 *
555 * C99 6.7.8p17
556 * C11 6.7.9p17
557 */
558 static void
559 brace_level_advance(brace_level *bl, size_t *max_subscript)
560 {
561 const type_t *tp;
562 designation *dn;
563 designator *dr;
564
565 debug_enter();
566 dn = &bl->bl_designation;
567 tp = designation_parent_type(dn, bl->bl_type);
568
569 if (bl->bl_designation.dn_len == 0)
570 (void)designation_descend(dn, bl->bl_type);
571 dr = designation_last(dn);
572 /* TODO: try to switch on dr->dr_kind instead */
573 switch (tp->t_tspec) {
574 case STRUCT:
575 lint_assert(dr->dr_member != NULL);
576 dr->dr_member = skip_unnamed(dr->dr_member->s_next);
577 if (dr->dr_member == NULL)
578 dr->dr_done = true;
579 break;
580 case UNION:
581 dr->dr_member = NULL;
582 dr->dr_done = true;
583 break;
584 case ARRAY:
585 dr->dr_subscript++;
586 if (tp->t_incomplete_array &&
587 dr->dr_subscript > *max_subscript)
588 *max_subscript = dr->dr_subscript;
589 if (!tp->t_incomplete_array &&
590 dr->dr_subscript >= (size_t)tp->t_dim)
591 dr->dr_done = true;
592 break;
593 default:
594 dr->dr_done = true;
595 break;
596 }
597 designation_debug(dn);
598 debug_leave();
599 }
600
601 static void
602 warn_too_many_initializers(designator_kind kind, const type_t *tp)
603 {
604
605 if (kind == DK_STRUCT || kind == DK_UNION) {
606 /* too many struct/union initializers */
607 error(172);
608 } else if (kind == DK_ARRAY) {
609 lint_assert(tp->t_tspec == ARRAY);
610 lint_assert(!tp->t_incomplete_array);
611 /* too many array initializers, expected %d */
612 error(173, tp->t_dim);
613 } else {
614 /* too many initializers */
615 error(174);
616 }
617
618 }
619
620 static bool
621 brace_level_pop_done(brace_level *bl, size_t *max_subscript)
622 {
623 designation *dn = &bl->bl_designation;
624 designator_kind dr_kind = designation_last(dn)->dr_kind;
625 const type_t *sub_type = designation_parent_type(dn, bl->bl_type);
626
627 while (designation_last(dn)->dr_done) {
628 dn->dn_len--;
629 designation_debug(dn);
630 if (dn->dn_len == 0) {
631 warn_too_many_initializers(dr_kind, sub_type);
632 return false;
633 }
634 brace_level_advance(bl, max_subscript);
635 }
636 return true;
637 }
638
639 static void
640 brace_level_pop_final(brace_level *bl, size_t *max_subscript)
641 {
642 designation *dn = &bl->bl_designation;
643
644 while (dn->dn_len > 0 && designation_last(dn)->dr_done) {
645 dn->dn_len--;
646 designation_debug(dn);
647 if (dn->dn_len == 0)
648 return;
649 brace_level_advance(bl, max_subscript);
650 }
651 }
652
653 /*
654 * Make the designation point to the sub-object to be initialized next.
655 * Initially or after a previous expression, the designation is not advanced
656 * yet since the place to stop depends on the next expression, especially for
657 * string literals.
658 */
659 static bool
660 brace_level_goto(brace_level *bl, const tnode_t *rn, size_t *max_subscript)
661 {
662 const type_t *ltp;
663 designation *dn;
664
665 dn = &bl->bl_designation;
666 if (dn->dn_len == 0 && can_init_character_array(bl->bl_type, rn))
667 return true;
668 if (dn->dn_len == 0 && !designation_descend(dn, bl->bl_type))
669 return false;
670
671 again:
672 if (!brace_level_pop_done(bl, max_subscript))
673 return false;
674
675 ltp = brace_level_sub_type(bl);
676 if (types_compatible(ltp, rn->tn_type, true, false, NULL))
677 return true;
678
679 if (is_struct_or_union(ltp->t_tspec) || ltp->t_tspec == ARRAY) {
680 if (can_init_character_array(ltp, rn))
681 return true;
682 if (!designation_descend(dn, ltp))
683 return false;
684 goto again;
685 }
686
687 return true;
688 }
689
690
691 static initialization *
692 initialization_new(sym_t *sym, initialization *enclosing)
693 {
694 initialization *in;
695
696 in = xcalloc(1, sizeof(*in));
697 in->in_sym = sym;
698 in->in_enclosing = enclosing;
699
700 return in;
701 }
702
703 static void
704 initialization_free(initialization *in)
705 {
706 brace_level *bl, *next;
707
708 /* TODO: lint_assert(in->in_brace_level == NULL) */
709 for (bl = in->in_brace_level; bl != NULL; bl = next) {
710 next = bl->bl_enclosing;
711 brace_level_free(bl);
712 }
713
714 free(in);
715 }
716
717 #ifdef DEBUG
718 static void
719 initialization_debug(const initialization *in)
720 {
721 size_t i;
722 const brace_level *bl;
723
724 if (in->in_err)
725 debug_step("initialization error");
726 if (in->in_brace_level == NULL) {
727 debug_step("no brace level");
728 return;
729 }
730
731 i = 0;
732 for (bl = in->in_brace_level; bl != NULL; bl = bl->bl_enclosing) {
733 debug_print_indent();
734 debug_printf("brace level %zu: ", i);
735 brace_level_debug(bl);
736 i++;
737 }
738 }
739 #else
740 #define initialization_debug(in) do { } while (false)
741 #endif
742
743 /*
744 * Return the type of the object or sub-object that is currently being
745 * initialized.
746 */
747 static const type_t *
748 initialization_sub_type(initialization *in)
749 {
750 const type_t *tp;
751
752 if (in->in_brace_level == NULL)
753 return in->in_sym->s_type;
754
755 tp = brace_level_sub_type(in->in_brace_level);
756 if (tp == NULL)
757 in->in_err = true;
758 return tp;
759 }
760
761 static void
762 initialization_lbrace(initialization *in)
763 {
764 const type_t *tp;
765 brace_level *outer_bl;
766
767 if (in->in_err)
768 return;
769
770 debug_enter();
771
772 tp = initialization_sub_type(in);
773 if (tp == NULL)
774 goto done;
775
776 outer_bl = in->in_brace_level;
777 if (!allow_c90 && outer_bl == NULL)
778 check_trad_no_auto_aggregate(in->in_sym);
779
780 if (!allow_c90 && tp->t_tspec == UNION) {
781 /* initialization of union is illegal in traditional C */
782 warning(238);
783 }
784
785 if (is_struct_or_union(tp->t_tspec) && tp->t_str->sou_incomplete) {
786 /* initialization of incomplete type '%s' */
787 error(175, type_name(tp));
788 in->in_err = true;
789 goto done;
790 }
791
792 if (outer_bl != NULL && outer_bl->bl_designation.dn_len == 0) {
793 designation *dn = &outer_bl->bl_designation;
794 (void)designation_descend(dn, outer_bl->bl_type);
795 tp = designation_type(dn, outer_bl->bl_type);
796 }
797
798 in->in_brace_level = brace_level_new(tp, outer_bl);
799 if (is_struct_or_union(tp->t_tspec) &&
800 first_named_member(tp) == NULL) {
801 /* cannot initialize struct/union with no named member */
802 error(179);
803 in->in_err = true;
804 }
805
806 done:
807 initialization_debug(in);
808 debug_leave();
809 }
810
811 static void
812 initialization_rbrace(initialization *in)
813 {
814 brace_level *inner_bl, *outer_bl;
815
816 debug_enter();
817
818 if (in->in_brace_level != NULL)
819 brace_level_pop_final(in->in_brace_level,
820 &in->in_max_subscript);
821
822 /* C99 6.7.8p22 */
823 if (in->in_sym->s_type->t_incomplete_array &&
824 in->in_brace_level->bl_enclosing == NULL) {
825
826 /* prevent "empty array declaration for '%s' [190]" */
827 size_t dim = in->in_max_subscript;
828 if (dim == 0 && in->in_err)
829 dim = 1;
830
831 update_type_of_array_of_unknown_size(in->in_sym, dim);
832 }
833
834 if (in->in_err)
835 goto done;
836
837 inner_bl = in->in_brace_level;
838 outer_bl = inner_bl->bl_enclosing;
839 in->in_brace_level = outer_bl;
840 brace_level_free(inner_bl);
841
842 if (outer_bl != NULL)
843 brace_level_advance(outer_bl, &in->in_max_subscript);
844
845 done:
846 initialization_debug(in);
847 debug_leave();
848 }
849
850 static void
851 initialization_add_designator_member(initialization *in, const char *name)
852 {
853 brace_level *bl;
854 const type_t *tp;
855 const sym_t *member;
856
857 if (in->in_err)
858 return;
859
860 bl = in->in_brace_level;
861 lint_assert(bl != NULL);
862
863 tp = brace_level_sub_type(bl);
864 if (is_struct_or_union(tp->t_tspec))
865 goto proceed;
866 else if (tp->t_tspec == ARRAY) {
867 /* syntax error '%s' */
868 error(249, "designator '.member' is only for struct/union");
869 in->in_err = true;
870 return;
871 } else {
872 /* syntax error '%s' */
873 error(249, "scalar type cannot use designator");
874 in->in_err = true;
875 return;
876 }
877
878 proceed:
879 member = look_up_member(tp, name);
880 if (member == NULL) {
881 /* type '%s' does not have member '%s' */
882 error(101, type_name(tp), name);
883 in->in_err = true;
884 return;
885 }
886
887 designation_push(&bl->bl_designation,
888 tp->t_tspec == STRUCT ? DK_STRUCT : DK_UNION, member, 0);
889 }
890
891 static void
892 initialization_add_designator_subscript(initialization *in, size_t subscript)
893 {
894 brace_level *bl;
895 const type_t *tp;
896
897 if (in->in_err)
898 return;
899
900 bl = in->in_brace_level;
901 lint_assert(bl != NULL);
902
903 tp = brace_level_sub_type(bl);
904 if (tp->t_tspec != ARRAY) {
905 /* syntax error '%s' */
906 error(249, "designator '[...]' is only for arrays");
907 in->in_err = true;
908 return;
909 }
910
911 if (!tp->t_incomplete_array && subscript >= (size_t)tp->t_dim) {
912 /* array subscript cannot be > %d: %ld */
913 error(168, tp->t_dim - 1, (long)subscript);
914 subscript = 0; /* suppress further errors */
915 }
916
917 if (tp->t_incomplete_array && subscript > in->in_max_subscript)
918 in->in_max_subscript = subscript;
919
920 designation_push(&bl->bl_designation, DK_ARRAY, NULL, subscript);
921 }
922
923 /*
924 * Initialize an object with automatic storage duration that has an
925 * initializer expression without braces.
926 */
927 static bool
928 initialization_expr_using_op(initialization *in, tnode_t *rn)
929 {
930 tnode_t *ln, *tn;
931
932 if (!has_automatic_storage_duration(in->in_sym))
933 return false;
934 if (in->in_brace_level != NULL)
935 return false;
936 if (in->in_sym->s_type->t_tspec == ARRAY)
937 return false;
938
939 debug_step("handing over to INIT");
940
941 ln = build_name(in->in_sym, false);
942 ln->tn_type = expr_unqualified_type(ln->tn_type);
943
944 tn = build_binary(ln, INIT, false /* XXX */, rn);
945 expr(tn, false, false, false, false);
946
947 return true;
948 }
949
950 /* Initialize a character array or wchar_t array with a string literal. */
951 static bool
952 initialization_init_array_from_string(initialization *in, tnode_t *tn)
953 {
954 brace_level *bl;
955 const type_t *tp;
956 size_t len;
957
958 if (tn->tn_op != STRING)
959 return false;
960
961 tp = initialization_sub_type(in);
962
963 if (!can_init_character_array(tp, tn))
964 return false;
965
966 len = tn->tn_string->st_len;
967 if (!tp->t_incomplete_array && (size_t)tp->t_dim < len) {
968 /* string literal too long (%lu) for target array (%lu) */
969 warning(187, (unsigned long)len, (unsigned long)tp->t_dim);
970 }
971
972 bl = in->in_brace_level;
973 if (bl != NULL && bl->bl_designation.dn_len == 0)
974 (void)designation_descend(&bl->bl_designation, bl->bl_type);
975 if (bl != NULL)
976 brace_level_advance(bl, &in->in_max_subscript);
977
978 if (tp->t_incomplete_array)
979 update_type_of_array_of_unknown_size(in->in_sym, len + 1);
980
981 return true;
982 }
983
984 /*
985 * Initialize a single sub-object as part of the currently ongoing
986 * initialization.
987 */
988 static void
989 initialization_expr(initialization *in, tnode_t *tn)
990 {
991 brace_level *bl;
992 const type_t *tp;
993
994 if (in->in_err || tn == NULL)
995 return;
996
997 debug_enter();
998
999 bl = in->in_brace_level;
1000 if (bl != NULL && !brace_level_goto(bl, tn, &in->in_max_subscript)) {
1001 in->in_err = true;
1002 goto done;
1003 }
1004 if (initialization_expr_using_op(in, tn))
1005 goto done;
1006 if (initialization_init_array_from_string(in, tn))
1007 goto done;
1008 if (in->in_err)
1009 goto done;
1010
1011 tp = initialization_sub_type(in);
1012 if (tp == NULL)
1013 goto done;
1014
1015 if (bl == NULL && !is_scalar(tp->t_tspec)) {
1016 /* {}-enclosed initializer required */
1017 error(181);
1018 goto done;
1019 }
1020
1021 debug_step("expecting '%s', expression has '%s'",
1022 type_name(tp), type_name(tn->tn_type));
1023 check_init_expr(tp, in->in_sym, tn);
1024 if (bl != NULL)
1025 brace_level_advance(bl, &in->in_max_subscript);
1026
1027 done:
1028 initialization_debug(in);
1029 debug_leave();
1030 }
1031
1032
1033 static initialization *init;
1034
1035
1036 static initialization *
1037 current_init(void)
1038 {
1039
1040 lint_assert(init != NULL);
1041 return init;
1042 }
1043
1044 sym_t **
1045 current_initsym(void)
1046 {
1047
1048 return ¤t_init()->in_sym;
1049 }
1050
1051 void
1052 begin_initialization(sym_t *sym)
1053 {
1054
1055 debug_step("begin initialization of '%s'", type_name(sym->s_type));
1056 debug_indent_inc();
1057
1058 init = initialization_new(sym, init);
1059 }
1060
1061 void
1062 end_initialization(void)
1063 {
1064 initialization *in;
1065
1066 in = init;
1067 init = in->in_enclosing;
1068 initialization_free(in);
1069
1070 debug_indent_dec();
1071 debug_step("end initialization");
1072 }
1073
1074 void
1075 begin_designation(void)
1076 {
1077 initialization *in;
1078 brace_level *bl;
1079
1080 in = current_init();
1081 if (in->in_err)
1082 return;
1083
1084 bl = in->in_brace_level;
1085 lint_assert(bl != NULL);
1086 bl->bl_designation.dn_len = 0;
1087 designation_debug(&bl->bl_designation);
1088 }
1089
1090 void
1091 add_designator_member(sbuf_t *sb)
1092 {
1093
1094 initialization_add_designator_member(current_init(), sb->sb_name);
1095 }
1096
1097 void
1098 add_designator_subscript(range_t range)
1099 {
1100
1101 initialization_add_designator_subscript(current_init(), range.hi);
1102 }
1103
1104 void
1105 init_lbrace(void)
1106 {
1107
1108 initialization_lbrace(current_init());
1109 }
1110
1111 void
1112 init_expr(tnode_t *tn)
1113 {
1114
1115 initialization_expr(current_init(), tn);
1116 }
1117
1118 void
1119 init_rbrace(void)
1120 {
1121
1122 initialization_rbrace(current_init());
1123 }
1124