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