init.c revision 1.200 1 /* $NetBSD: init.c,v 1.200 2021/06/29 21:05:32 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) && !defined(lint)
41 __RCSID("$NetBSD: init.c,v 1.200 2021/06/29 21:05:32 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 *
56 * int array_of_unknown_size[] = { 111, 222, 333 };
57 * int array_flat[2][2] = { 11, 12, 21, 22 };
58 * int array_nested[2][2] = { { 11, 12 }, { 21, 22 } };
59 *
60 * struct { int x, y; } point = { 3, 4 };
61 * struct { int x, y; } point = { .y = 4, .x = 3 };
62 *
63 * Any scalar expression in the initializer may be surrounded by arbitrarily
64 * many extra pairs of braces, like in the example 'number_with_braces' (C99
65 * 6.7.8p11).
66 *
67 * For multi-dimensional arrays, the inner braces may be omitted like in
68 * array_flat or spelled out like in array_nested. This is unusual in
69 * practice and therefore only supported very basically.
70 *
71 * During an initialization, the grammar parser calls these functions:
72 *
73 * begin_initialization
74 * init_lbrace for each '{'
75 * add_designator_member for each '.member' before '='
76 * add_designator_subscript for each '[123]' before '='
77 * init_expr for each expression
78 * init_rbrace for each '}'
79 * end_initialization
80 *
81 * Each '{' begins a new brace level, each '}' ends the current brace level.
82 * Each brace level has an associated "current object", which is the starting
83 * point for resolving the optional designations such as '.member[3]'.
84 *
85 * See also:
86 * C99 6.7.8 "Initialization"
87 * d_c99_init.c for more examples
88 */
89
90 /*
91 * A single component on the path to the sub-object that is initialized by an
92 * initializer expression. Either a struct or union member, or an array
93 * subscript.
94 *
95 * C99 6.7.8p6, 6.7.8p7
96 */
97 struct designator {
98 const char *dr_name; /* for struct and union */
99 size_t dr_subscript; /* for array */
100 struct designator *dr_next;
101 };
102
103 /*
104 * The optional designation for an initializer, saying which sub-object to
105 * initialize. Examples for designations are '.member' or
106 * '.member[123].member.member[1][1]'.
107 *
108 * C99 6.7.8p6, 6.7.8p7
109 */
110 struct designation {
111 struct designator *dn_head;
112 struct designator *dn_tail;
113 };
114
115 /*
116 * Describes a single brace level of an ongoing initialization.
117 *
118 * See C99 6.7.8p17.
119 */
120 struct brace_level {
121 const type_t *bl_type; /* The type of the current object that
122 * is initialized at this brace
123 * level. */
124
125 struct designation bl_designation; /* .member[123].member */
126
127 const sym_t *bl_member; /* for structs and unions */
128 size_t bl_subscript; /* for arrays */
129 bool bl_scalar_done: 1; /* for scalars */
130 bool bl_confused: 1; /* skip further checks */
131
132 struct brace_level *bl_enclosing;
133 };
134
135 /*
136 * An ongoing initialization.
137 *
138 * In most cases there is only ever a single initialization going on. See
139 * pointer_to_compound_literal in msg_171.c for an exception.
140 */
141 struct initialization {
142 /* The symbol that is to be initialized. */
143 sym_t *in_sym;
144
145 /* The innermost brace level. */
146 struct brace_level *in_brace_level;
147
148 /*
149 * Is set as soon as a fatal error occurred in the initialization.
150 * The effect is that the rest of the initialization is ignored
151 * (parsed by yacc, expression trees built, but no initialization
152 * takes place).
153 */
154 bool in_err;
155
156 struct initialization *in_enclosing;
157 };
158
159
160 #ifdef DEBUG
161 static int debug_indentation = 0;
162 #endif
163
164
165 #ifdef DEBUG
166
167 static void __printflike(1, 2)
168 debug_printf(const char *fmt, ...)
169 {
170 va_list va;
171
172 va_start(va, fmt);
173 vfprintf(stdout, fmt, va);
174 va_end(va);
175 }
176
177 static void
178 debug_indent(void)
179 {
180
181 debug_printf("%*s", 2 * debug_indentation, "");
182 }
183
184 static void
185 debug_enter(const char *func)
186 {
187
188 printf("%*s+ %s\n", 2 * debug_indentation++, "", func);
189 }
190
191 static void __printflike(1, 2)
192 debug_step(const char *fmt, ...)
193 {
194 va_list va;
195
196 debug_indent();
197 va_start(va, fmt);
198 vfprintf(stdout, fmt, va);
199 va_end(va);
200 printf("\n");
201 }
202 #define debug_step0 debug_step
203 #define debug_step1 debug_step
204 #define debug_step2 debug_step
205
206 static void
207 debug_leave(const char *func)
208 {
209
210 printf("%*s- %s\n", 2 * --debug_indentation, "", func);
211 }
212
213 #define debug_enter() (debug_enter)(__func__)
214 #define debug_leave() (debug_leave)(__func__)
215
216 #else
217
218 #define debug_indent() do { } while (false)
219 #define debug_enter() do { } while (false)
220 #define debug_step0(fmt) do { } while (false)
221 #define debug_step1(fmt, arg0) do { } while (false)
222 #define debug_step2(fmt, arg1, arg2) do { } while (false)
223 #define debug_leave() do { } while (false)
224
225 #endif
226
227
228 static void *
229 unconst_cast(const void *p)
230 {
231 void *r;
232
233 memcpy(&r, &p, sizeof(r));
234 return r;
235 }
236
237 static bool
238 has_automatic_storage_duration(const sym_t *sym)
239 {
240
241 return sym->s_scl == AUTO || sym->s_scl == REG;
242 }
243
244 /* C99 6.7.8p14, 6.7.8p15 */
245 static bool
246 is_string_array(const type_t *tp, tspec_t t)
247 {
248 tspec_t st;
249
250 if (tp == NULL || tp->t_tspec != ARRAY)
251 return false;
252
253 st = tp->t_subt->t_tspec;
254 return t == CHAR
255 ? st == CHAR || st == UCHAR || st == SCHAR
256 : st == WCHAR;
257 }
258
259 /* C99 6.7.8p9 */
260 static bool
261 is_unnamed(const sym_t *m)
262 {
263
264 return m->s_bitfield && m->s_name == unnamed;
265 }
266
267 /* C99 6.7.8p9 */
268 static const sym_t *
269 skip_unnamed(const sym_t *m)
270 {
271
272 while (m != NULL && is_unnamed(m))
273 m = m->s_next;
274 return m;
275 }
276
277 static const sym_t *
278 first_named_member(const type_t *tp)
279 {
280
281 lint_assert(is_struct_or_union(tp->t_tspec));
282 return skip_unnamed(tp->t_str->sou_first_member);
283 }
284
285 static const sym_t *
286 look_up_member(const type_t *tp, const char *name)
287 {
288 const sym_t *m;
289
290 lint_assert(is_struct_or_union(tp->t_tspec));
291 for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next)
292 if (!is_unnamed(m) && strcmp(m->s_name, name) == 0)
293 return m;
294 return NULL;
295 }
296
297 static const type_t *
298 sym_type(const sym_t *sym)
299 {
300
301 return sym != NULL ? sym->s_type : NULL;
302 }
303
304 static const type_t *
305 look_up_member_type(const type_t *tp, const char *name)
306 {
307 const sym_t *member;
308
309 member = look_up_member(tp, name);
310 if (member == NULL) {
311 /* type '%s' does not have member '%s' */
312 error(101, type_name(tp), name);
313 }
314
315 return sym_type(member);
316 }
317
318 /*
319 * C99 6.7.8p22 says that the type of an array of unknown size becomes known
320 * at the end of its initializer list.
321 */
322 static void
323 update_type_of_array_of_unknown_size(sym_t *sym, size_t size)
324 {
325 type_t *tp;
326
327 tp = dup_type(sym->s_type);
328 tp->t_dim = (int)size;
329 tp->t_incomplete_array = false;
330 sym->s_type = tp;
331 }
332
333
334 /* In traditional C, bit-fields can be initialized only by integer constants. */
335 static void
336 check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
337 {
338
339 if (tflag &&
340 is_integer(lt) &&
341 ln->tn_type->t_bitfield &&
342 !is_integer(rt)) {
343 /* bit-field initialization is illegal in traditional C */
344 warning(186);
345 }
346 }
347
348 static void
349 check_non_constant_initializer(const tnode_t *tn, const sym_t *sym)
350 {
351 const sym_t *unused_sym;
352 ptrdiff_t unused_offs;
353
354 if (tn == NULL || tn->tn_op == CON)
355 return;
356
357 if (constant_addr(tn, &unused_sym, &unused_offs))
358 return;
359
360 if (has_automatic_storage_duration(sym)) {
361 /* non-constant initializer */
362 c99ism(177);
363 } else {
364 /* non-constant initializer */
365 error(177);
366 }
367 }
368
369 static void
370 check_trad_no_auto_aggregate(const sym_t *sym)
371 {
372
373 if (has_automatic_storage_duration(sym) &&
374 !is_scalar(sym->s_type->t_tspec)) {
375 /* no automatic aggregate initialization in trad. C */
376 warning(188);
377 }
378 }
379
380 static void
381 check_init_expr(const type_t *tp, sym_t *sym, tnode_t *tn)
382 {
383 tnode_t *ln;
384 type_t *ltp;
385 tspec_t lt, rt;
386 struct memory_block *tmem;
387
388 ltp = expr_dup_type(tp);
389 ltp->t_const = false;
390
391 /* Create a temporary node for the left side. */
392 ln = expr_zalloc(sizeof(*ln));
393 ln->tn_op = NAME;
394 ln->tn_type = ltp;
395 ln->tn_lvalue = true;
396 ln->tn_sym = sym;
397
398 tn = cconv(tn);
399
400 lt = ln->tn_type->t_tspec;
401 rt = tn->tn_type->t_tspec;
402
403 debug_step2("typeok '%s', '%s'",
404 type_name(ln->tn_type), type_name(tn->tn_type));
405 if (!typeok(INIT, 0, ln, tn))
406 return;
407
408 /*
409 * Preserve the tree memory. This is necessary because otherwise
410 * expr() would free it.
411 */
412 tmem = expr_save_memory();
413 expr(tn, true, false, true, false);
414 expr_restore_memory(tmem);
415
416 check_bit_field_init(ln, lt, rt);
417
418 /*
419 * XXX: Is it correct to do this conversion _after_ the typeok above?
420 */
421 if (lt != rt || (tp->t_bitfield && tn->tn_op == CON))
422 tn = convert(INIT, 0, unconst_cast(tp), tn);
423
424 check_non_constant_initializer(tn, sym);
425 }
426
427
428 static struct designator *
429 designator_new(const char *name, size_t subscript)
430 {
431 struct designator *dr;
432
433 dr = xcalloc(1, sizeof(*dr));
434 dr->dr_name = name;
435 dr->dr_subscript = subscript;
436 return dr;
437 }
438
439 static void
440 designator_free(struct designator *dr)
441 {
442
443 free(dr);
444 }
445
446
447 static const type_t *
448 designator_look_up(const struct designator *dr, const type_t *tp)
449 {
450 switch (tp->t_tspec) {
451 case STRUCT:
452 case UNION:
453 if (dr->dr_name == NULL) {
454 /* syntax error '%s' */
455 error(249, "designator '[...]' is only for arrays");
456 return sym_type(first_named_member(tp));
457 }
458
459 return look_up_member_type(tp, dr->dr_name);
460 case ARRAY:
461 if (dr->dr_name != NULL) {
462 /* syntax error '%s' */
463 error(249,
464 "designator '.member' is only for struct/union");
465 }
466 if (!tp->t_incomplete_array &&
467 dr->dr_subscript >= (size_t)tp->t_dim) {
468 /* array subscript cannot be > %d: %ld */
469 error(168, tp->t_dim - 1, (long)dr->dr_subscript);
470 }
471 return tp->t_subt;
472 default:
473 /* syntax error '%s' */
474 error(249, "scalar type cannot use designator");
475 return tp;
476 }
477 }
478
479
480 #ifdef DEBUG
481 static void
482 designation_debug(const struct designation *dn)
483 {
484 const struct designator *dr;
485
486 if (dn->dn_head == NULL)
487 return;
488
489 debug_indent();
490 debug_printf("designation: ");
491 for (dr = dn->dn_head; dr != NULL; dr = dr->dr_next) {
492 if (dr->dr_name != NULL) {
493 debug_printf(".%s", dr->dr_name);
494 lint_assert(dr->dr_subscript == 0);
495 } else
496 debug_printf("[%zu]", dr->dr_subscript);
497 }
498 debug_printf("\n");
499 }
500 #else
501 #define designation_debug(dn) do { } while (false)
502 #endif
503
504 static void
505 designation_add(struct designation *dn, const char *name, size_t subscript)
506 {
507 struct designator *dr;
508
509 dr = designator_new(name, subscript);
510
511 if (dn->dn_head != NULL) {
512 dn->dn_tail->dr_next = dr;
513 dn->dn_tail = dr;
514 } else {
515 dn->dn_head = dr;
516 dn->dn_tail = dr;
517 }
518 }
519
520 /*
521 * Starting at the type of the current object, resolve the type of the
522 * sub-object by following each designator in the list.
523 *
524 * C99 6.7.8p18
525 */
526 static const type_t *
527 designation_look_up(const struct designation *dn, const type_t *tp)
528 {
529 const struct designator *dr;
530
531 for (dr = dn->dn_head; dr != NULL && tp != NULL; dr = dr->dr_next)
532 tp = designator_look_up(dr, tp);
533 return tp;
534 }
535
536 static void
537 designation_reset(struct designation *dn)
538 {
539 struct designator *dr, *next;
540
541 for (dr = dn->dn_head; dr != NULL; dr = next) {
542 next = dr->dr_next;
543 designator_free(dr);
544 }
545
546 dn->dn_head = NULL;
547 dn->dn_tail = NULL;
548 }
549
550
551 static struct brace_level *
552 brace_level_new(const type_t *tp, struct brace_level *enclosing)
553 {
554 struct brace_level *bl;
555
556 bl = xcalloc(1, sizeof(*bl));
557 bl->bl_type = tp;
558 bl->bl_enclosing = enclosing;
559 if (is_struct_or_union(tp->t_tspec))
560 bl->bl_member = first_named_member(tp);
561
562 return bl;
563 }
564
565 static void
566 brace_level_free(struct brace_level *bl)
567 {
568
569 designation_reset(&bl->bl_designation);
570 free(bl);
571 }
572
573 #ifdef DEBUG
574 static void
575 brace_level_debug(const struct brace_level *bl)
576 {
577
578 lint_assert(bl->bl_type != NULL);
579 lint_assert(bl->bl_member == NULL || !is_unnamed(bl->bl_member));
580
581 debug_printf("type '%s'", type_name(bl->bl_type));
582
583 if (is_struct_or_union(bl->bl_type->t_tspec) && bl->bl_member != NULL)
584 debug_printf(", member '%s'", bl->bl_member->s_name);
585 if (bl->bl_type->t_tspec == ARRAY)
586 debug_printf(", subscript %zu", bl->bl_subscript);
587
588 debug_printf("\n");
589 }
590 #else
591 #define brace_level_debug(level) do { } while (false)
592 #endif
593
594 static const type_t *
595 brace_level_sub_type_struct_or_union(const struct brace_level *bl)
596 {
597
598 if (bl->bl_member == NULL) {
599 /* too many struct/union initializers */
600 error(172);
601 return NULL;
602 }
603
604 lint_assert(!is_unnamed(bl->bl_member));
605 return sym_type(bl->bl_member);
606 }
607
608 static const type_t *
609 brace_level_sub_type_array(const struct brace_level *bl, bool is_string)
610 {
611
612 if (!bl->bl_confused && !bl->bl_type->t_incomplete_array &&
613 bl->bl_subscript >= (size_t)bl->bl_type->t_dim) {
614 /* too many array initializers, expected %d */
615 error(173, bl->bl_type->t_dim);
616 }
617
618 if (is_string && bl->bl_subscript == 0 &&
619 bl->bl_type->t_subt->t_tspec != ARRAY)
620 return bl->bl_type;
621
622 return bl->bl_type->t_subt;
623 }
624
625 static const type_t *
626 brace_level_sub_type_scalar(const struct brace_level *bl)
627 {
628
629 if (bl->bl_scalar_done) {
630 /* too many initializers */
631 error(174);
632 }
633
634 return bl->bl_type;
635 }
636
637 /* Return the type of the sub-object that is currently being initialized. */
638 static const type_t *
639 brace_level_sub_type(const struct brace_level *bl, bool is_string)
640 {
641
642 if (bl->bl_designation.dn_head != NULL)
643 return designation_look_up(&bl->bl_designation, bl->bl_type);
644
645 switch (bl->bl_type->t_tspec) {
646 case STRUCT:
647 case UNION:
648 return brace_level_sub_type_struct_or_union(bl);
649 case ARRAY:
650 return brace_level_sub_type_array(bl, is_string);
651 default:
652 return brace_level_sub_type_scalar(bl);
653 }
654 }
655
656 /* C99 6.7.8p17 */
657 static void
658 brace_level_apply_designation(struct brace_level *bl)
659 {
660 const struct designator *dr = bl->bl_designation.dn_head;
661
662 if (dr == NULL)
663 return;
664
665 designation_debug(&bl->bl_designation);
666
667 switch (bl->bl_type->t_tspec) {
668 case STRUCT:
669 case UNION:
670 if (dr->dr_name == NULL)
671 return; /* error, silently ignored */
672 bl->bl_member = look_up_member(bl->bl_type, dr->dr_name);
673 break;
674 case ARRAY:
675 if (dr->dr_name != NULL)
676 return; /* error, silently ignored */
677 bl->bl_subscript = dr->dr_subscript;
678 break;
679 default:
680 break; /* error, silently ignored */
681 }
682 }
683
684 /*
685 * After initializing a sub-object, advance to the next sub-object.
686 *
687 * C99 6.7.8p17
688 */
689 static void
690 brace_level_advance(struct brace_level *bl)
691 {
692
693 switch (bl->bl_type->t_tspec) {
694 case STRUCT:
695 lint_assert(bl->bl_member != NULL);
696 bl->bl_member = skip_unnamed(bl->bl_member->s_next);
697 break;
698 case UNION:
699 bl->bl_member = NULL;
700 break;
701 case ARRAY:
702 bl->bl_subscript++;
703 break;
704 default:
705 bl->bl_scalar_done = true;
706 break;
707 }
708 }
709
710
711 static struct initialization *
712 initialization_new(sym_t *sym)
713 {
714 struct initialization *in;
715
716 in = xcalloc(1, sizeof(*in));
717 in->in_sym = sym;
718
719 return in;
720 }
721
722 static void
723 initialization_free(struct initialization *in)
724 {
725 struct brace_level *bl, *next;
726
727 for (bl = in->in_brace_level; bl != NULL; bl = next) {
728 next = bl->bl_enclosing;
729 brace_level_free(bl);
730 }
731
732 free(in);
733 }
734
735 #ifdef DEBUG
736 static void
737 initialization_debug(const struct initialization *in)
738 {
739 size_t i;
740 const struct brace_level *bl;
741
742 if (in->in_brace_level == NULL) {
743 debug_step("no brace level");
744 return;
745 }
746
747 i = 0;
748 for (bl = in->in_brace_level; bl != NULL; bl = bl->bl_enclosing) {
749 debug_indent();
750 debug_printf("brace level %zu: ", i);
751 brace_level_debug(bl);
752 i++;
753 }
754 }
755 #else
756 #define initialization_debug(in) do { } while (false)
757 #endif
758
759 /*
760 * Return the type of the object or sub-object that is currently being
761 * initialized.
762 */
763 static const type_t *
764 initialization_sub_type(struct initialization *in, bool is_string)
765 {
766 const type_t *tp;
767
768 tp = in->in_brace_level != NULL
769 ? brace_level_sub_type(in->in_brace_level, is_string)
770 : in->in_sym->s_type;
771 if (tp == NULL)
772 in->in_err = true;
773 return tp;
774 }
775
776 static void
777 initialization_begin_brace_level(struct initialization *in)
778 {
779 const type_t *tp;
780
781 if (in->in_err)
782 return;
783
784 debug_enter();
785
786 tp = initialization_sub_type(in, false);
787 if (tp == NULL) {
788 in->in_err = true;
789 goto done;
790 }
791
792 if (tflag && in->in_brace_level == NULL)
793 check_trad_no_auto_aggregate(in->in_sym);
794
795 if (tflag && tp->t_tspec == UNION) {
796 /* initialization of union is illegal in traditional C */
797 warning(238);
798 }
799
800 if (tp->t_tspec == STRUCT && tp->t_str->sou_incomplete) {
801 /* initialization of incomplete type '%s' */
802 error(175, type_name(tp));
803 in->in_err = true;
804 goto done;
805 }
806
807 if (in->in_brace_level != NULL)
808 brace_level_apply_designation(in->in_brace_level);
809
810 in->in_brace_level = brace_level_new(tp, in->in_brace_level);
811
812 done:
813 initialization_debug(in);
814 debug_leave();
815 }
816
817 /* C99 6.7.8p22 */
818 static void
819 initialization_set_size_of_unknown_array(struct initialization *in)
820 {
821
822 if (in->in_sym->s_type->t_incomplete_array &&
823 in->in_brace_level->bl_enclosing == NULL)
824 update_type_of_array_of_unknown_size(in->in_sym,
825 in->in_brace_level->bl_subscript);
826 /*
827 * XXX: bl_subscript is not entirely correct.
828 * It should rather be max(actually used subscript) + 1.
829 * int arr[] = { [100] = 100, [0] = 0 };
830 */
831 }
832
833 static void
834 initialization_end_brace_level(struct initialization *in)
835 {
836 struct brace_level *bl;
837
838 if (in->in_err)
839 return;
840
841 debug_enter();
842
843 initialization_set_size_of_unknown_array(in);
844
845 bl = in->in_brace_level;
846 in->in_brace_level = bl->bl_enclosing;
847 brace_level_free(bl);
848 bl = in->in_brace_level;
849
850 if (bl != NULL)
851 brace_level_advance(bl);
852 if (bl != NULL)
853 designation_reset(&bl->bl_designation);
854
855 initialization_debug(in);
856 debug_leave();
857 }
858
859 static void
860 initialization_add_designator(struct initialization *in,
861 const char *name, size_t subscript)
862 {
863
864 if (in->in_err)
865 return;
866
867 lint_assert(in->in_brace_level != NULL);
868 designation_add(&in->in_brace_level->bl_designation, name, subscript);
869 }
870
871 /*
872 * An object with automatic storage duration that has a single initializer
873 * expression without braces and is not an array is initialized by delegating
874 * to the ASSIGN operator.
875 */
876 static bool
877 initialization_expr_using_assign(struct initialization *in, tnode_t *rn)
878 {
879 tnode_t *ln, *tn;
880
881 if (!has_automatic_storage_duration(in->in_sym))
882 return false;
883 if (in->in_brace_level != NULL)
884 return false;
885 if (in->in_sym->s_type->t_tspec == ARRAY)
886 return false;
887
888 debug_step0("handing over to ASSIGN");
889
890 ln = new_name_node(in->in_sym, 0);
891 ln->tn_type = expr_dup_type(ln->tn_type);
892 ln->tn_type->t_const = false;
893
894 tn = build(ASSIGN, ln, rn);
895 expr(tn, false, false, false, false);
896
897 return true;
898 }
899
900 /* Initialize a character array or wchar_t array with a string literal. */
901 static bool
902 initialization_init_array_using_string(struct initialization *in, tnode_t *tn)
903 {
904 struct brace_level *bl;
905 const type_t *tp;
906 strg_t *strg;
907
908 if (tn->tn_op != STRING)
909 return false;
910
911 bl = in->in_brace_level;
912 tp = initialization_sub_type(in, true);
913 strg = tn->tn_string;
914
915 if (!is_string_array(tp, strg->st_tspec))
916 return false;
917 if (bl != NULL && tp->t_tspec != ARRAY && bl->bl_subscript != 0)
918 return false;
919
920 if (!tp->t_incomplete_array && tp->t_dim < (int)strg->st_len) {
921 /* non-null byte ignored in string initializer */
922 warning(187);
923 }
924
925 if (tp == in->in_sym->s_type && tp->t_incomplete_array) {
926 if (bl != NULL) {
927 bl->bl_subscript = strg->st_len;
928 /* see brace_level_advance for the +1 */
929 /* see initialization_set_size_of_unknown_array */
930 } else
931 update_type_of_array_of_unknown_size(in->in_sym,
932 strg->st_len + 1);
933 }
934
935 return true;
936 }
937
938 /*
939 * Initialize a single sub-object as part of the currently ongoing
940 * initialization.
941 */
942 static void
943 initialization_expr(struct initialization *in, tnode_t *tn)
944 {
945 struct brace_level *bl;
946 const type_t *tp;
947
948 if (in->in_err)
949 return;
950
951 bl = in->in_brace_level;
952 if (bl != NULL && bl->bl_confused)
953 return;
954
955 debug_enter();
956
957 if (tn == NULL)
958 goto advance;
959 if (initialization_expr_using_assign(in, tn))
960 goto done;
961 if (initialization_init_array_using_string(in, tn))
962 goto advance;
963
964 if (bl != NULL)
965 brace_level_apply_designation(bl);
966 tp = initialization_sub_type(in, false);
967 if (tp == NULL)
968 goto done;
969
970 if (bl == NULL && !is_scalar(tp->t_tspec)) {
971 /* {}-enclosed initializer required */
972 error(181);
973 goto done;
974 }
975
976 /*
977 * Hack to accept initializations with omitted braces, see
978 * c99_6_7_8_p28_example5 in test d_c99_init.c. Since both GCC and
979 * Clang already warn about this at level -Wall, there is no point
980 * in repeating the same check in lint. If needed, support for these
981 * edge cases could be added, but that would increase the complexity.
982 */
983 if (is_scalar(tn->tn_type->t_tspec) &&
984 (tp->t_tspec == ARRAY || is_struct_or_union(tp->t_tspec)) &&
985 bl != NULL) {
986 bl->bl_confused = true;
987 goto done;
988 }
989
990 debug_step2("expecting '%s', expression has '%s'",
991 type_name(tp), type_name(tn->tn_type));
992 check_init_expr(tp, in->in_sym, tn);
993
994 advance:
995 if (bl != NULL)
996 brace_level_advance(bl);
997 done:
998 if (bl != NULL)
999 designation_reset(&bl->bl_designation);
1000
1001 initialization_debug(in);
1002 debug_leave();
1003 }
1004
1005
1006 static struct initialization *init;
1007
1008
1009 static struct initialization *
1010 current_init(void)
1011 {
1012
1013 lint_assert(init != NULL);
1014 return init;
1015 }
1016
1017 sym_t **
1018 current_initsym(void)
1019 {
1020
1021 return ¤t_init()->in_sym;
1022 }
1023
1024 void
1025 begin_initialization(sym_t *sym)
1026 {
1027 struct initialization *in;
1028
1029 debug_step1("begin initialization of '%s'", type_name(sym->s_type));
1030 #ifdef DEBUG
1031 debug_indentation++;
1032 #endif
1033
1034 in = initialization_new(sym);
1035 in->in_enclosing = init;
1036 init = in;
1037 }
1038
1039 void
1040 end_initialization(void)
1041 {
1042 struct initialization *in;
1043
1044 in = init;
1045 init = in->in_enclosing;
1046 initialization_free(in);
1047
1048 #ifdef DEBUG
1049 debug_indentation--;
1050 #endif
1051 debug_step0("end initialization");
1052 }
1053
1054 void
1055 add_designator_member(sbuf_t *sb)
1056 {
1057
1058 initialization_add_designator(current_init(), sb->sb_name, 0);
1059 }
1060
1061 void
1062 add_designator_subscript(range_t range)
1063 {
1064
1065 initialization_add_designator(current_init(), NULL, range.hi);
1066 }
1067
1068 void
1069 init_lbrace(void)
1070 {
1071
1072 initialization_begin_brace_level(current_init());
1073 }
1074
1075 void
1076 init_expr(tnode_t *tn)
1077 {
1078
1079 initialization_expr(current_init(), tn);
1080 }
1081
1082 void
1083 init_rbrace(void)
1084 {
1085
1086 initialization_end_brace_level(current_init());
1087 }
1088