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