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