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