Home | History | Annotate | Line # | Download | only in lint1
init.c revision 1.236
      1 /*	$NetBSD: init.c,v 1.236 2022/06/22 19:23:17 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995 Jochen Pohl
      5  * Copyright (c) 2021 Roland Illig
      6  * All Rights Reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Jochen Pohl for
     19  *	The NetBSD Project.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #if HAVE_NBTOOL_CONFIG_H
     36 #include "nbtool_config.h"
     37 #endif
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(__RCSID)
     41 __RCSID("$NetBSD: init.c,v 1.236 2022/06/22 19:23:17 rillig Exp $");
     42 #endif
     43 
     44 #include <stdlib.h>
     45 #include <string.h>
     46 
     47 #include "lint1.h"
     48 
     49 
     50 /*
     51  * Initialization of global or local objects, like in:
     52  *
     53  *	int number = 12345;
     54  *	int number_with_braces = { 12345 };
     55  *	int array_of_unknown_size[] = { 111, 222, 333 };
     56  *	struct { int x, y; } point = { .y = 4, .x = 3 };
     57  *
     58  * During an initialization, the grammar parser calls these functions:
     59  *
     60  *	begin_initialization
     61  *		init_lbrace			for each '{'
     62  *		add_designator_member		for each '.member' before '='
     63  *		add_designator_subscript	for each '[123]' before '='
     64  *		init_expr			for each expression
     65  *		init_rbrace			for each '}'
     66  *	end_initialization
     67  *
     68  * Each '{' begins a new brace level, each '}' ends the current brace level.
     69  * Each brace level has an associated "current object", which is the starting
     70  * point for resolving the optional designations such as '.member[3]'.
     71  *
     72  * See also:
     73  *	C99 6.7.8 "Initialization"
     74  *	C11 6.7.9 "Initialization"
     75  *	d_c99_init.c for more examples
     76  */
     77 
     78 
     79 typedef enum designator_kind {
     80 	DK_STRUCT,		/* .member */
     81 	DK_UNION,		/* .member */
     82 	DK_ARRAY,		/* [subscript] */
     83 	/* TODO: actually necessary? */
     84 	DK_SCALAR		/* no textual representation, not generated
     85 				 * by the parser */
     86 } designator_kind;
     87 
     88 /*
     89  * A single component on the path from the "current object" of a brace level
     90  * to the sub-object that is initialized by an expression.
     91  *
     92  * C99 6.7.8p6, 6.7.8p7
     93  */
     94 typedef struct designator {
     95 	designator_kind	dr_kind;
     96 	const sym_t	*dr_member;	/* for DK_STRUCT and DK_UNION */
     97 	size_t		dr_subscript;	/* for DK_ARRAY */
     98 	bool		dr_done;
     99 } designator;
    100 
    101 /*
    102  * The path from the "current object" of a brace level to the sub-object that
    103  * is initialized by an expression.  Examples for designations are '.member'
    104  * or '.member[123].member.member[1][1]'.
    105  *
    106  * C99 6.7.8p6, 6.7.8p7
    107  */
    108 typedef struct designation {
    109 	designator	*dn_items;
    110 	size_t		dn_len;
    111 	size_t		dn_cap;
    112 } designation;
    113 
    114 /*
    115  * Everything that happens between a '{' and the corresponding '}', as part
    116  * of an initialization.
    117  *
    118  * Each brace level has a "current object".   For the outermost brace level,
    119  * it is the same as the object to be initialized.  Each nested '{' begins a
    120  * nested brace level, for the sub-object pointed to by the designator of the
    121  * outer brace level.
    122  *
    123  * C99 6.7.8p17
    124  */
    125 typedef struct brace_level {
    126 	/* The type of the "current object". */
    127 	const type_t	*bl_type;
    128 
    129 	/*
    130 	 * The path from the "current object" to the sub-object that is
    131 	 * initialized by the next expression.
    132 	 *
    133 	 * Initially, the designation is empty.  Before handling an
    134 	 * expression, the designation is updated to point to the
    135 	 * corresponding sub-object to be initialized.  After handling an
    136 	 * expression, the designation is marked as done.  It is later
    137 	 * advanced as necessary.
    138 	 */
    139 	designation	bl_designation;
    140 
    141 	struct brace_level *bl_enclosing;
    142 } brace_level;
    143 
    144 /*
    145  * An ongoing initialization.
    146  *
    147  * In most cases there is only ever a single initialization at a time.  See
    148  * pointer_to_compound_literal in msg_171.c for a real-life counterexample.
    149  */
    150 typedef struct initialization {
    151 	/* The symbol that is to be initialized. */
    152 	sym_t		*in_sym;
    153 
    154 	/* The innermost brace level. */
    155 	brace_level	*in_brace_level;
    156 
    157 	/*
    158 	 * The maximum subscript that has ever been seen for an array of
    159 	 * unknown size, which can only occur at the outermost brace level.
    160 	 */
    161 	size_t		in_max_subscript;
    162 
    163 	/*
    164 	 * Is set when a structural error occurred in the initialization.
    165 	 * If set, the rest of the initialization is still parsed, but the
    166 	 * initialization assignments are not checked.
    167 	 */
    168 	bool		in_err;
    169 
    170 	struct initialization *in_enclosing;
    171 } initialization;
    172 
    173 
    174 static void *
    175 unconst_cast(const void *p)
    176 {
    177 	void *r;
    178 
    179 	memcpy(&r, &p, sizeof(r));
    180 	return r;
    181 }
    182 
    183 static bool
    184 has_automatic_storage_duration(const sym_t *sym)
    185 {
    186 
    187 	return sym->s_scl == AUTO || sym->s_scl == REG;
    188 }
    189 
    190 /*
    191  * Test whether rn is a string literal that can initialize ltp.
    192  *
    193  * See also:
    194  *	C99 6.7.8p14		for plain character strings
    195  *	C99 6.7.8p15		for wide character strings
    196  */
    197 static bool
    198 can_init_character_array(const type_t *ltp, const tnode_t *rn)
    199 {
    200 	tspec_t lst, rst;
    201 
    202 	if (!(ltp != NULL && ltp->t_tspec == ARRAY && rn->tn_op == STRING))
    203 		return false;
    204 
    205 	lst = ltp->t_subt->t_tspec;
    206 	rst = rn->tn_type->t_subt->t_tspec;
    207 
    208 	return rst == CHAR
    209 	    ? lst == CHAR || lst == UCHAR || lst == SCHAR
    210 	    : lst == WCHAR;
    211 }
    212 
    213 /* C99 6.7.8p9 */
    214 static const sym_t *
    215 skip_unnamed(const sym_t *m)
    216 {
    217 
    218 	while (m != NULL && m->s_name == unnamed)
    219 		m = m->s_next;
    220 	return m;
    221 }
    222 
    223 static const sym_t *
    224 first_named_member(const type_t *tp)
    225 {
    226 
    227 	lint_assert(is_struct_or_union(tp->t_tspec));
    228 	return skip_unnamed(tp->t_str->sou_first_member);
    229 }
    230 
    231 static const sym_t *
    232 look_up_member(const type_t *tp, const char *name)
    233 {
    234 	const sym_t *m;
    235 
    236 	lint_assert(is_struct_or_union(tp->t_tspec));
    237 	for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next)
    238 		if (strcmp(m->s_name, name) == 0)
    239 			return m;
    240 	return NULL;
    241 }
    242 
    243 /*
    244  * C99 6.7.8p22 says that the type of an array of unknown size becomes known
    245  * at the end of its initializer list.
    246  */
    247 static void
    248 update_type_of_array_of_unknown_size(sym_t *sym, size_t size)
    249 {
    250 	type_t *tp;
    251 
    252 	tp = block_dup_type(sym->s_type);
    253 	tp->t_dim = (int)size;
    254 	tp->t_incomplete_array = false;
    255 	sym->s_type = tp;
    256 	debug_step("completed array type is '%s'", type_name(sym->s_type));
    257 }
    258 
    259 
    260 /* In traditional C, bit-fields can be initialized only by integer constants. */
    261 static void
    262 check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
    263 {
    264 
    265 	if (!allow_c90 &&
    266 	    is_integer(lt) &&
    267 	    ln->tn_type->t_bitfield &&
    268 	    !is_integer(rt)) {
    269 		/* bit-field initialization is illegal in traditional C */
    270 		warning(186);
    271 	}
    272 }
    273 
    274 static void
    275 check_non_constant_initializer(const tnode_t *tn, const sym_t *sym)
    276 {
    277 	const sym_t *unused_sym;
    278 	ptrdiff_t unused_offs;
    279 
    280 	if (tn == NULL || tn->tn_op == CON)
    281 		return;
    282 
    283 	if (constant_addr(tn, &unused_sym, &unused_offs))
    284 		return;
    285 
    286 	if (has_automatic_storage_duration(sym)) {
    287 		/* non-constant initializer */
    288 		c99ism(177);
    289 	} else {
    290 		/* non-constant initializer */
    291 		error(177);
    292 	}
    293 }
    294 
    295 static void
    296 check_trad_no_auto_aggregate(const sym_t *sym)
    297 {
    298 
    299 	if (has_automatic_storage_duration(sym) &&
    300 	    !is_scalar(sym->s_type->t_tspec)) {
    301 		/* no automatic aggregate initialization in traditional C */
    302 		warning(188);
    303 	}
    304 }
    305 
    306 static void
    307 check_init_expr(const type_t *ltp, sym_t *lsym, tnode_t *rn)
    308 {
    309 	tnode_t *ln;
    310 	type_t *lutp;
    311 	tspec_t lt, rt;
    312 	struct memory_block *tmem;
    313 
    314 	lutp = expr_unqualified_type(ltp);
    315 
    316 	/* Create a temporary node for the left side. */
    317 	ln = expr_zero_alloc(sizeof(*ln));
    318 	ln->tn_op = NAME;
    319 	ln->tn_type = lutp;
    320 	ln->tn_lvalue = true;
    321 	ln->tn_sym = lsym;
    322 
    323 	rn = cconv(rn);
    324 
    325 	lt = ln->tn_type->t_tspec;
    326 	rt = rn->tn_type->t_tspec;
    327 
    328 	debug_step("typeok '%s', '%s'",
    329 	    type_name(ln->tn_type), type_name(rn->tn_type));
    330 	if (!typeok(INIT, 0, ln, rn))
    331 		return;
    332 
    333 	/*
    334 	 * Preserve the tree memory. This is necessary because otherwise
    335 	 * expr() would free it.
    336 	 */
    337 	tmem = expr_save_memory();
    338 	expr(rn, true, false, true, false);
    339 	expr_restore_memory(tmem);
    340 
    341 	check_bit_field_init(ln, lt, rt);
    342 
    343 	/*
    344 	 * XXX: Is it correct to do this conversion _after_ the typeok above?
    345 	 */
    346 	if (lt != rt || (ltp->t_bitfield && rn->tn_op == CON))
    347 		rn = convert(INIT, 0, unconst_cast(ltp), rn);
    348 
    349 	check_non_constant_initializer(rn, lsym);
    350 }
    351 
    352 
    353 static const type_t *
    354 designator_type(const designator *dr, const type_t *tp)
    355 {
    356 	switch (tp->t_tspec) {
    357 	case STRUCT:
    358 	case UNION:
    359 		if (dr->dr_kind != DK_STRUCT && dr->dr_kind != DK_UNION) {
    360 			const sym_t *fmem = first_named_member(tp);
    361 			/* syntax error '%s' */
    362 			error(249, "designator '[...]' is only for arrays");
    363 			return fmem != NULL ? fmem->s_type : NULL;
    364 		}
    365 
    366 		lint_assert(dr->dr_member != NULL);
    367 		return dr->dr_member->s_type;
    368 	case ARRAY:
    369 		if (dr->dr_kind != DK_ARRAY) {
    370 			/* syntax error '%s' */
    371 			error(249,
    372 			    "designator '.member' is only for struct/union");
    373 		}
    374 		if (!tp->t_incomplete_array)
    375 			lint_assert(dr->dr_subscript < (size_t)tp->t_dim);
    376 		return tp->t_subt;
    377 	default:
    378 		if (dr->dr_kind != DK_SCALAR) {
    379 			/* syntax error '%s' */
    380 			error(249, "scalar type cannot use designator");
    381 		}
    382 		return tp;
    383 	}
    384 }
    385 
    386 
    387 #ifdef DEBUG
    388 static void
    389 designator_debug(const designator *dr)
    390 {
    391 
    392 	if (dr->dr_kind == DK_STRUCT || dr->dr_kind == DK_UNION) {
    393 		lint_assert(dr->dr_subscript == 0);
    394 		debug_printf(".%s",
    395 		    dr->dr_member != NULL
    396 			? dr->dr_member->s_name
    397 			: "<end>");
    398 	} else if (dr->dr_kind == DK_ARRAY) {
    399 		lint_assert(dr->dr_member == NULL);
    400 		debug_printf("[%zu]", dr->dr_subscript);
    401 	} else {
    402 		lint_assert(dr->dr_member == NULL);
    403 		lint_assert(dr->dr_subscript == 0);
    404 		debug_printf("<scalar>");
    405 	}
    406 
    407 	if (dr->dr_done)
    408 		debug_printf(" (done)");
    409 }
    410 
    411 static void
    412 designation_debug(const designation *dn)
    413 {
    414 	size_t i;
    415 
    416 	if (dn->dn_len == 0) {
    417 		debug_step("designation: (empty)");
    418 		return;
    419 	}
    420 
    421 	debug_print_indent();
    422 	debug_printf("designation: ");
    423 	for (i = 0; i < dn->dn_len; i++)
    424 		designator_debug(dn->dn_items + i);
    425 	debug_printf("\n");
    426 }
    427 #else
    428 #define designation_debug(dn) do { } while (false)
    429 #endif
    430 
    431 static designator *
    432 designation_last(designation *dn)
    433 {
    434 
    435 	lint_assert(dn->dn_len > 0);
    436 	return &dn->dn_items[dn->dn_len - 1];
    437 }
    438 
    439 static void
    440 designation_push(designation *dn, designator_kind kind,
    441 		 const sym_t *member, size_t subscript)
    442 {
    443 	designator *dr;
    444 
    445 	if (dn->dn_len == dn->dn_cap) {
    446 		dn->dn_cap += 4;
    447 		dn->dn_items = xrealloc(dn->dn_items,
    448 		    dn->dn_cap * sizeof(dn->dn_items[0]));
    449 	}
    450 
    451 	dr = &dn->dn_items[dn->dn_len++];
    452 	dr->dr_kind = kind;
    453 	dr->dr_member = member;
    454 	dr->dr_subscript = subscript;
    455 	dr->dr_done = false;
    456 	designation_debug(dn);
    457 }
    458 
    459 /*
    460  * Extend the designation as appropriate for the given type.
    461  *
    462  * C11 6.7.9p17
    463  */
    464 static bool
    465 designation_descend(designation *dn, const type_t *tp)
    466 {
    467 
    468 	if (is_struct_or_union(tp->t_tspec)) {
    469 		const sym_t *member = first_named_member(tp);
    470 		if (member == NULL)
    471 			return false;
    472 		designation_push(dn,
    473 		    tp->t_tspec == STRUCT ? DK_STRUCT : DK_UNION, member, 0);
    474 	} else if (tp->t_tspec == ARRAY)
    475 		designation_push(dn, DK_ARRAY, NULL, 0);
    476 	else
    477 		designation_push(dn, DK_SCALAR, NULL, 0);
    478 	return true;
    479 }
    480 
    481 /*
    482  * Starting at the type of the current object, resolve the type of the
    483  * sub-object by following each designator in the list.
    484  *
    485  * C99 6.7.8p18
    486  */
    487 static const type_t *
    488 designation_type(const designation *dn, const type_t *tp)
    489 {
    490 	size_t i;
    491 
    492 	for (i = 0; i < dn->dn_len && tp != NULL; i++)
    493 		tp = designator_type(dn->dn_items + i, tp);
    494 	return tp;
    495 }
    496 
    497 static const type_t *
    498 designation_parent_type(const designation *dn, const type_t *tp)
    499 {
    500 	size_t i;
    501 
    502 	for (i = 0; i + 1 < dn->dn_len && tp != NULL; i++)
    503 		tp = designator_type(dn->dn_items + i, tp);
    504 	return tp;
    505 }
    506 
    507 
    508 static brace_level *
    509 brace_level_new(const type_t *tp, brace_level *enclosing)
    510 {
    511 	brace_level *bl;
    512 
    513 	bl = xcalloc(1, sizeof(*bl));
    514 	bl->bl_type = tp;
    515 	bl->bl_enclosing = enclosing;
    516 
    517 	return bl;
    518 }
    519 
    520 static void
    521 brace_level_free(brace_level *bl)
    522 {
    523 
    524 	free(bl->bl_designation.dn_items);
    525 	free(bl);
    526 }
    527 
    528 #ifdef DEBUG
    529 static void
    530 brace_level_debug(const brace_level *bl)
    531 {
    532 
    533 	lint_assert(bl->bl_type != NULL);
    534 
    535 	debug_printf("type '%s'\n", type_name(bl->bl_type));
    536 	debug_indent_inc();
    537 	designation_debug(&bl->bl_designation);
    538 	debug_indent_dec();
    539 }
    540 #else
    541 #define brace_level_debug(level) do { } while (false)
    542 #endif
    543 
    544 /* Return the type of the sub-object that is currently being initialized. */
    545 static const type_t *
    546 brace_level_sub_type(const brace_level *bl)
    547 {
    548 
    549 	return designation_type(&bl->bl_designation, bl->bl_type);
    550 }
    551 
    552 /*
    553  * After initializing a sub-object, advance the designation to point after
    554  * the sub-object that has just been initialized.
    555  *
    556  * C99 6.7.8p17
    557  * C11 6.7.9p17
    558  */
    559 static void
    560 brace_level_advance(brace_level *bl, size_t *max_subscript)
    561 {
    562 	const type_t *tp;
    563 	designation *dn;
    564 	designator *dr;
    565 
    566 	debug_enter();
    567 	dn = &bl->bl_designation;
    568 	tp = designation_parent_type(dn, bl->bl_type);
    569 
    570 	if (bl->bl_designation.dn_len == 0)
    571 		(void)designation_descend(dn, bl->bl_type);
    572 	dr = designation_last(dn);
    573 	/* TODO: try to switch on dr->dr_kind instead */
    574 	switch (tp->t_tspec) {
    575 	case STRUCT:
    576 		lint_assert(dr->dr_member != NULL);
    577 		dr->dr_member = skip_unnamed(dr->dr_member->s_next);
    578 		if (dr->dr_member == NULL)
    579 			dr->dr_done = true;
    580 		break;
    581 	case UNION:
    582 		dr->dr_member = NULL;
    583 		dr->dr_done = true;
    584 		break;
    585 	case ARRAY:
    586 		dr->dr_subscript++;
    587 		if (tp->t_incomplete_array &&
    588 		    dr->dr_subscript > *max_subscript)
    589 			*max_subscript = dr->dr_subscript;
    590 		if (!tp->t_incomplete_array &&
    591 		    dr->dr_subscript >= (size_t)tp->t_dim)
    592 			dr->dr_done = true;
    593 		break;
    594 	default:
    595 		dr->dr_done = true;
    596 		break;
    597 	}
    598 	designation_debug(dn);
    599 	debug_leave();
    600 }
    601 
    602 static void
    603 warn_too_many_initializers(designator_kind kind, const type_t *tp)
    604 {
    605 
    606 	if (kind == DK_STRUCT || kind == DK_UNION) {
    607 		/* too many struct/union initializers */
    608 		error(172);
    609 	} else if (kind == DK_ARRAY) {
    610 		lint_assert(tp->t_tspec == ARRAY);
    611 		lint_assert(!tp->t_incomplete_array);
    612 		/* too many array initializers, expected %d */
    613 		error(173, tp->t_dim);
    614 	} else {
    615 		/* too many initializers */
    616 		error(174);
    617 	}
    618 
    619 }
    620 
    621 static bool
    622 brace_level_pop_done(brace_level *bl, size_t *max_subscript)
    623 {
    624 	designation *dn = &bl->bl_designation;
    625 	designator_kind dr_kind = designation_last(dn)->dr_kind;
    626 	const type_t *sub_type = designation_parent_type(dn, bl->bl_type);
    627 
    628 	while (designation_last(dn)->dr_done) {
    629 		dn->dn_len--;
    630 		designation_debug(dn);
    631 		if (dn->dn_len == 0) {
    632 			warn_too_many_initializers(dr_kind, sub_type);
    633 			return false;
    634 		}
    635 		brace_level_advance(bl, max_subscript);
    636 	}
    637 	return true;
    638 }
    639 
    640 static void
    641 brace_level_pop_final(brace_level *bl, size_t *max_subscript)
    642 {
    643 	designation *dn = &bl->bl_designation;
    644 
    645 	while (dn->dn_len > 0 && designation_last(dn)->dr_done) {
    646 		dn->dn_len--;
    647 		designation_debug(dn);
    648 		if (dn->dn_len == 0)
    649 			return;
    650 		brace_level_advance(bl, max_subscript);
    651 	}
    652 }
    653 
    654 /*
    655  * Make the designation point to the sub-object to be initialized next.
    656  * Initially or after a previous expression, the designation is not advanced
    657  * yet since the place to stop depends on the next expression, especially for
    658  * string literals.
    659  */
    660 static bool
    661 brace_level_goto(brace_level *bl, const tnode_t *rn, size_t *max_subscript)
    662 {
    663 	const type_t *ltp;
    664 	designation *dn;
    665 
    666 	dn = &bl->bl_designation;
    667 	if (dn->dn_len == 0 && can_init_character_array(bl->bl_type, rn))
    668 		return true;
    669 	if (dn->dn_len == 0 && !designation_descend(dn, bl->bl_type))
    670 		return false;
    671 
    672 again:
    673 	if (!brace_level_pop_done(bl, max_subscript))
    674 		return false;
    675 
    676 	ltp = brace_level_sub_type(bl);
    677 	if (eqtype(ltp, rn->tn_type, true, false, NULL))
    678 		return true;
    679 
    680 	if (is_struct_or_union(ltp->t_tspec) || ltp->t_tspec == ARRAY) {
    681 		if (can_init_character_array(ltp, rn))
    682 			return true;
    683 		if (!designation_descend(dn, ltp))
    684 			return false;
    685 		goto again;
    686 	}
    687 
    688 	return true;
    689 }
    690 
    691 
    692 static initialization *
    693 initialization_new(sym_t *sym, initialization *enclosing)
    694 {
    695 	initialization *in;
    696 
    697 	in = xcalloc(1, sizeof(*in));
    698 	in->in_sym = sym;
    699 	in->in_enclosing = enclosing;
    700 
    701 	return in;
    702 }
    703 
    704 static void
    705 initialization_free(initialization *in)
    706 {
    707 	brace_level *bl, *next;
    708 
    709 	/* TODO: lint_assert(in->in_brace_level == NULL) */
    710 	for (bl = in->in_brace_level; bl != NULL; bl = next) {
    711 		next = bl->bl_enclosing;
    712 		brace_level_free(bl);
    713 	}
    714 
    715 	free(in);
    716 }
    717 
    718 #ifdef DEBUG
    719 static void
    720 initialization_debug(const initialization *in)
    721 {
    722 	size_t i;
    723 	const brace_level *bl;
    724 
    725 	if (in->in_err)
    726 		debug_step("initialization error");
    727 	if (in->in_brace_level == NULL) {
    728 		debug_step("no brace level");
    729 		return;
    730 	}
    731 
    732 	i = 0;
    733 	for (bl = in->in_brace_level; bl != NULL; bl = bl->bl_enclosing) {
    734 		debug_print_indent();
    735 		debug_printf("brace level %zu: ", i);
    736 		brace_level_debug(bl);
    737 		i++;
    738 	}
    739 }
    740 #else
    741 #define initialization_debug(in) do { } while (false)
    742 #endif
    743 
    744 /*
    745  * Return the type of the object or sub-object that is currently being
    746  * initialized.
    747  */
    748 static const type_t *
    749 initialization_sub_type(initialization *in)
    750 {
    751 	const type_t *tp;
    752 
    753 	if (in->in_brace_level == NULL)
    754 		return in->in_sym->s_type;
    755 
    756 	tp = brace_level_sub_type(in->in_brace_level);
    757 	if (tp == NULL)
    758 		in->in_err = true;
    759 	return tp;
    760 }
    761 
    762 static void
    763 initialization_lbrace(initialization *in)
    764 {
    765 	const type_t *tp;
    766 	brace_level *outer_bl;
    767 
    768 	if (in->in_err)
    769 		return;
    770 
    771 	debug_enter();
    772 
    773 	tp = initialization_sub_type(in);
    774 	if (tp == NULL)
    775 		goto done;
    776 
    777 	outer_bl = in->in_brace_level;
    778 	if (!allow_c90 && outer_bl == NULL)
    779 		check_trad_no_auto_aggregate(in->in_sym);
    780 
    781 	if (!allow_c90 && tp->t_tspec == UNION) {
    782 		/* initialization of union is illegal in traditional C */
    783 		warning(238);
    784 	}
    785 
    786 	if (is_struct_or_union(tp->t_tspec) && tp->t_str->sou_incomplete) {
    787 		/* initialization of incomplete type '%s' */
    788 		error(175, type_name(tp));
    789 		in->in_err = true;
    790 		goto done;
    791 	}
    792 
    793 	if (outer_bl != NULL && outer_bl->bl_designation.dn_len == 0) {
    794 		designation *dn = &outer_bl->bl_designation;
    795 		(void)designation_descend(dn, outer_bl->bl_type);
    796 		tp = designation_type(dn, outer_bl->bl_type);
    797 	}
    798 
    799 	in->in_brace_level = brace_level_new(tp, outer_bl);
    800 	if (is_struct_or_union(tp->t_tspec) &&
    801 	    first_named_member(tp) == NULL) {
    802 		/* cannot initialize struct/union with no named member */
    803 		error(179);
    804 		in->in_err = true;
    805 	}
    806 
    807 done:
    808 	initialization_debug(in);
    809 	debug_leave();
    810 }
    811 
    812 static void
    813 initialization_rbrace(initialization *in)
    814 {
    815 	brace_level *inner_bl, *outer_bl;
    816 
    817 	debug_enter();
    818 
    819 	if (in->in_brace_level != NULL)
    820 		brace_level_pop_final(in->in_brace_level,
    821 		    &in->in_max_subscript);
    822 
    823 	/* C99 6.7.8p22 */
    824 	if (in->in_sym->s_type->t_incomplete_array &&
    825 	    in->in_brace_level->bl_enclosing == NULL) {
    826 
    827 		/* prevent "empty array declaration for '%s' [190]" */
    828 		size_t dim = in->in_max_subscript;
    829 		if (dim == 0 && in->in_err)
    830 			dim = 1;
    831 
    832 		update_type_of_array_of_unknown_size(in->in_sym, dim);
    833 	}
    834 
    835 	if (in->in_err)
    836 		goto done;
    837 
    838 	inner_bl = in->in_brace_level;
    839 	outer_bl = inner_bl->bl_enclosing;
    840 	in->in_brace_level = outer_bl;
    841 	brace_level_free(inner_bl);
    842 
    843 	if (outer_bl != NULL)
    844 		brace_level_advance(outer_bl, &in->in_max_subscript);
    845 
    846 done:
    847 	initialization_debug(in);
    848 	debug_leave();
    849 }
    850 
    851 static void
    852 initialization_add_designator_member(initialization *in, const char *name)
    853 {
    854 	brace_level *bl;
    855 	const type_t *tp;
    856 	const sym_t *member;
    857 
    858 	if (in->in_err)
    859 		return;
    860 
    861 	bl = in->in_brace_level;
    862 	lint_assert(bl != NULL);
    863 
    864 	tp = brace_level_sub_type(bl);
    865 	if (is_struct_or_union(tp->t_tspec))
    866 		goto proceed;
    867 	else if (tp->t_tspec == ARRAY) {
    868 		/* syntax error '%s' */
    869 		error(249, "designator '.member' is only for struct/union");
    870 		in->in_err = true;
    871 		return;
    872 	} else {
    873 		/* syntax error '%s' */
    874 		error(249, "scalar type cannot use designator");
    875 		in->in_err = true;
    876 		return;
    877 	}
    878 
    879 proceed:
    880 	member = look_up_member(tp, name);
    881 	if (member == NULL) {
    882 		/* type '%s' does not have member '%s' */
    883 		error(101, type_name(tp), name);
    884 		in->in_err = true;
    885 		return;
    886 	}
    887 
    888 	designation_push(&bl->bl_designation,
    889 	    tp->t_tspec == STRUCT ? DK_STRUCT : DK_UNION, member, 0);
    890 }
    891 
    892 static void
    893 initialization_add_designator_subscript(initialization *in, size_t subscript)
    894 {
    895 	brace_level *bl;
    896 	const type_t *tp;
    897 
    898 	if (in->in_err)
    899 		return;
    900 
    901 	bl = in->in_brace_level;
    902 	lint_assert(bl != NULL);
    903 
    904 	tp = brace_level_sub_type(bl);
    905 	if (tp->t_tspec != ARRAY) {
    906 		/* syntax error '%s' */
    907 		error(249, "designator '[...]' is only for arrays");
    908 		in->in_err = true;
    909 		return;
    910 	}
    911 
    912 	if (!tp->t_incomplete_array && subscript >= (size_t)tp->t_dim) {
    913 		/* array subscript cannot be > %d: %ld */
    914 		error(168, tp->t_dim - 1, (long)subscript);
    915 		subscript = 0;	/* suppress further errors */
    916 	}
    917 
    918 	if (tp->t_incomplete_array && subscript > in->in_max_subscript)
    919 		in->in_max_subscript = subscript;
    920 
    921 	designation_push(&bl->bl_designation, DK_ARRAY, NULL, subscript);
    922 }
    923 
    924 /*
    925  * Initialize an object with automatic storage duration that has an
    926  * initializer expression without braces.
    927  */
    928 static bool
    929 initialization_expr_using_op(initialization *in, tnode_t *rn)
    930 {
    931 	tnode_t *ln, *tn;
    932 
    933 	if (!has_automatic_storage_duration(in->in_sym))
    934 		return false;
    935 	if (in->in_brace_level != NULL)
    936 		return false;
    937 	if (in->in_sym->s_type->t_tspec == ARRAY)
    938 		return false;
    939 
    940 	debug_step("handing over to INIT");
    941 
    942 	ln = build_name(in->in_sym, false);
    943 	ln->tn_type = expr_unqualified_type(ln->tn_type);
    944 
    945 	tn = build_binary(ln, INIT, false /* XXX */, rn);
    946 	expr(tn, false, false, false, false);
    947 
    948 	return true;
    949 }
    950 
    951 /* Initialize a character array or wchar_t array with a string literal. */
    952 static bool
    953 initialization_init_array_from_string(initialization *in, tnode_t *tn)
    954 {
    955 	brace_level *bl;
    956 	const type_t *tp;
    957 	size_t len;
    958 
    959 	if (tn->tn_op != STRING)
    960 		return false;
    961 
    962 	tp = initialization_sub_type(in);
    963 
    964 	if (!can_init_character_array(tp, tn))
    965 		return false;
    966 
    967 	len = tn->tn_string->st_len;
    968 	if (!tp->t_incomplete_array && (size_t)tp->t_dim < len) {
    969 		/* string literal too long (%lu) for target array (%lu) */
    970 		warning(187, (unsigned long)len, (unsigned long)tp->t_dim);
    971 	}
    972 
    973 	bl = in->in_brace_level;
    974 	if (bl != NULL && bl->bl_designation.dn_len == 0)
    975 		(void)designation_descend(&bl->bl_designation, bl->bl_type);
    976 	if (bl != NULL)
    977 		brace_level_advance(bl, &in->in_max_subscript);
    978 
    979 	if (tp->t_incomplete_array)
    980 		update_type_of_array_of_unknown_size(in->in_sym, len + 1);
    981 
    982 	return true;
    983 }
    984 
    985 /*
    986  * Initialize a single sub-object as part of the currently ongoing
    987  * initialization.
    988  */
    989 static void
    990 initialization_expr(initialization *in, tnode_t *tn)
    991 {
    992 	brace_level *bl;
    993 	const type_t *tp;
    994 
    995 	if (in->in_err || tn == NULL)
    996 		return;
    997 
    998 	debug_enter();
    999 
   1000 	bl = in->in_brace_level;
   1001 	if (bl != NULL && !brace_level_goto(bl, tn, &in->in_max_subscript)) {
   1002 		in->in_err = true;
   1003 		goto done;
   1004 	}
   1005 	if (initialization_expr_using_op(in, tn))
   1006 		goto done;
   1007 	if (initialization_init_array_from_string(in, tn))
   1008 		goto done;
   1009 	if (in->in_err)
   1010 		goto done;
   1011 
   1012 	tp = initialization_sub_type(in);
   1013 	if (tp == NULL)
   1014 		goto done;
   1015 
   1016 	if (bl == NULL && !is_scalar(tp->t_tspec)) {
   1017 		/* {}-enclosed initializer required */
   1018 		error(181);
   1019 		goto done;
   1020 	}
   1021 
   1022 	debug_step("expecting '%s', expression has '%s'",
   1023 	    type_name(tp), type_name(tn->tn_type));
   1024 	check_init_expr(tp, in->in_sym, tn);
   1025 	if (bl != NULL)
   1026 		brace_level_advance(bl, &in->in_max_subscript);
   1027 
   1028 done:
   1029 	initialization_debug(in);
   1030 	debug_leave();
   1031 }
   1032 
   1033 
   1034 static initialization *init;
   1035 
   1036 
   1037 static initialization *
   1038 current_init(void)
   1039 {
   1040 
   1041 	lint_assert(init != NULL);
   1042 	return init;
   1043 }
   1044 
   1045 sym_t **
   1046 current_initsym(void)
   1047 {
   1048 
   1049 	return &current_init()->in_sym;
   1050 }
   1051 
   1052 void
   1053 begin_initialization(sym_t *sym)
   1054 {
   1055 
   1056 	debug_step("begin initialization of '%s'", type_name(sym->s_type));
   1057 	debug_indent_inc();
   1058 
   1059 	init = initialization_new(sym, init);
   1060 }
   1061 
   1062 void
   1063 end_initialization(void)
   1064 {
   1065 	initialization *in;
   1066 
   1067 	in = init;
   1068 	init = in->in_enclosing;
   1069 	initialization_free(in);
   1070 
   1071 	debug_indent_dec();
   1072 	debug_step("end initialization");
   1073 }
   1074 
   1075 void
   1076 begin_designation(void)
   1077 {
   1078 	initialization *in;
   1079 	brace_level *bl;
   1080 
   1081 	in = current_init();
   1082 	if (in->in_err)
   1083 		return;
   1084 
   1085 	bl = in->in_brace_level;
   1086 	lint_assert(bl != NULL);
   1087 	bl->bl_designation.dn_len = 0;
   1088 	designation_debug(&bl->bl_designation);
   1089 }
   1090 
   1091 void
   1092 add_designator_member(sbuf_t *sb)
   1093 {
   1094 
   1095 	initialization_add_designator_member(current_init(), sb->sb_name);
   1096 }
   1097 
   1098 void
   1099 add_designator_subscript(range_t range)
   1100 {
   1101 
   1102 	initialization_add_designator_subscript(current_init(), range.hi);
   1103 }
   1104 
   1105 void
   1106 init_lbrace(void)
   1107 {
   1108 
   1109 	initialization_lbrace(current_init());
   1110 }
   1111 
   1112 void
   1113 init_expr(tnode_t *tn)
   1114 {
   1115 
   1116 	initialization_expr(current_init(), tn);
   1117 }
   1118 
   1119 void
   1120 init_rbrace(void)
   1121 {
   1122 
   1123 	initialization_rbrace(current_init());
   1124 }
   1125