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