Home | History | Annotate | Line # | Download | only in lint1
init.c revision 1.265
      1 /*	$NetBSD: init.c,v 1.265 2024/03/29 07:35:45 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.265 2024/03/29 07:35:45 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 /*
     80  * Everything that happens between a '{' and the corresponding '}', as part
     81  * of an initialization.
     82  *
     83  * Each brace level has a "current object".  For the outermost brace level,
     84  * it is the same as the object to be initialized.  Each nested '{' begins a
     85  * nested brace level, for the sub-object pointed to by the designator of the
     86  * outer brace level.
     87  *
     88  * C99 6.7.8p17
     89  */
     90 typedef struct brace_level {
     91 	/* The type of the "current object". */
     92 	const type_t	*bl_type;
     93 
     94 	/*
     95 	 * The path from the "current object" to the sub-object that is
     96 	 * initialized by the next expression.
     97 	 *
     98 	 * Initially, the designation is empty.  Before handling an expression,
     99 	 * the designation is updated to point to the corresponding sub-object
    100 	 * to be initialized.  After handling an expression, the designation is
    101 	 * marked as done.  It is later advanced as necessary.
    102 	 */
    103 	designation	bl_designation;
    104 
    105 	struct brace_level *bl_enclosing;
    106 } brace_level;
    107 
    108 /*
    109  * An ongoing initialization.
    110  *
    111  * In most cases there is only ever a single initialization at a time.  See
    112  * pointer_to_compound_literal in msg_171.c for a real-life counterexample.
    113  */
    114 typedef struct initialization {
    115 	/* The symbol that is to be initialized. */
    116 	sym_t		*in_sym;
    117 
    118 	/* The innermost brace level. */
    119 	brace_level	*in_brace_level;
    120 
    121 	/*
    122 	 * The maximum subscript that has ever been seen for an array of
    123 	 * unknown size, which can only occur at the outermost brace level.
    124 	 */
    125 	size_t		in_max_subscript;
    126 
    127 	/*
    128 	 * Is set when a structural error occurred in the initialization. If
    129 	 * set, the rest of the initialization is still parsed, but the
    130 	 * initialization assignments are not checked.
    131 	 */
    132 	bool		in_err;
    133 
    134 	struct initialization *in_enclosing;
    135 } initialization;
    136 
    137 
    138 static void *
    139 unconst_cast(const void *p)
    140 {
    141 	void *r;
    142 
    143 	memcpy(&r, &p, sizeof(r));
    144 	return r;
    145 }
    146 
    147 static bool
    148 has_automatic_storage_duration(const sym_t *sym)
    149 {
    150 
    151 	return sym->s_scl == AUTO || sym->s_scl == REG;
    152 }
    153 
    154 /*
    155  * Test whether rn is a string literal that can initialize ltp.
    156  *
    157  * See also:
    158  *	C99 6.7.8p14		for plain character strings
    159  *	C99 6.7.8p15		for wide character strings
    160  */
    161 static bool
    162 can_init_character_array(const type_t *ltp, const tnode_t *rn)
    163 {
    164 
    165 	if (!(ltp != NULL && ltp->t_tspec == ARRAY && rn->tn_op == STRING))
    166 		return false;
    167 
    168 	tspec_t lst = ltp->t_subt->t_tspec;
    169 	tspec_t rst = rn->tn_type->t_subt->t_tspec;
    170 
    171 	return rst == CHAR
    172 	    ? lst == CHAR || lst == UCHAR || lst == SCHAR
    173 	    : lst == WCHAR_TSPEC;
    174 }
    175 
    176 /*
    177  * C11 6.7.9p9 seems to say that all unnamed members are skipped. C11 6.7.2.1p8
    178  * suggests an exception to that rule, and together with C11 6.7.2.1p13, it
    179  * says that the members from an anonymous struct/union member are "considered
    180  * to be members of the containing structure or union", thereby preventing that
    181  * the containing structure or union has only unnamed members.
    182  */
    183 static const sym_t *
    184 skip_unnamed(const sym_t *m)
    185 {
    186 
    187 	while (m != NULL && m->s_name == unnamed
    188 	    && !is_struct_or_union(m->s_type->t_tspec))
    189 		m = m->s_next;
    190 	return m;
    191 }
    192 
    193 static const sym_t *
    194 first_named_member(const type_t *tp)
    195 {
    196 
    197 	lint_assert(is_struct_or_union(tp->t_tspec));
    198 	return skip_unnamed(tp->u.sou->sou_first_member);
    199 }
    200 
    201 /*
    202  * C99 6.7.8p22 says that the type of an array of unknown size becomes known
    203  * at the end of its initializer list.
    204  */
    205 static void
    206 update_type_of_array_of_unknown_size(sym_t *sym, size_t size)
    207 {
    208 
    209 	type_t *tp = block_dup_type(sym->s_type);
    210 	tp->u.dimension = (int)size;
    211 	tp->t_incomplete_array = false;
    212 	sym->s_type = tp;
    213 	debug_step("completed array type is '%s'", type_name(sym->s_type));
    214 	outsym(sym, sym->s_scl, sym->s_def);
    215 }
    216 
    217 
    218 /* In traditional C, bit-fields can be initialized only by integer constants. */
    219 static void
    220 check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
    221 {
    222 
    223 	if (!allow_c90 &&
    224 	    is_integer(lt) &&
    225 	    ln->tn_type->t_bitfield &&
    226 	    !is_integer(rt))
    227 		/* bit-field initializer must be an integer in ... */
    228 		warning(186);
    229 }
    230 
    231 static void
    232 check_non_constant_initializer(const tnode_t *tn, const sym_t *sym)
    233 {
    234 
    235 	if (tn == NULL || tn->tn_op == CON)
    236 		return;
    237 
    238 	const sym_t *unused_sym;
    239 	ptrdiff_t unused_offs;
    240 	if (constant_addr(tn, &unused_sym, &unused_offs))
    241 		return;
    242 
    243 	if (has_automatic_storage_duration(sym)) {
    244 		/* non-constant initializer */
    245 		c99ism(177);
    246 	} else {
    247 		/* non-constant initializer */
    248 		error(177);
    249 	}
    250 }
    251 
    252 static void
    253 check_trad_no_auto_aggregate(const sym_t *sym)
    254 {
    255 
    256 	if (has_automatic_storage_duration(sym) &&
    257 	    !is_scalar(sym->s_type->t_tspec)) {
    258 		/* no automatic aggregate initialization in traditional C */
    259 		warning(188);
    260 	}
    261 }
    262 
    263 static void
    264 check_init_expr(const type_t *ltp, sym_t *lsym, tnode_t *rn)
    265 {
    266 
    267 	type_t *lutp = expr_unqualified_type(ltp);
    268 
    269 	/* Create a temporary node for the left side. */
    270 	tnode_t *ln = expr_zero_alloc(sizeof(*ln), "tnode");
    271 	ln->tn_op = NAME;
    272 	ln->tn_type = lutp;
    273 	ln->tn_lvalue = true;
    274 	ln->u.sym = lsym;
    275 
    276 	rn = cconv(rn);
    277 
    278 	tspec_t lt = ln->tn_type->t_tspec;
    279 	tspec_t rt = rn->tn_type->t_tspec;
    280 
    281 	debug_step("typeok '%s', '%s'",
    282 	    type_name(ln->tn_type), type_name(rn->tn_type));
    283 	if (!typeok(INIT, 0, ln, rn))
    284 		return;
    285 
    286 	/*
    287 	 * Preserve the tree memory. This is necessary because otherwise expr()
    288 	 * would free it.
    289 	 */
    290 	memory_pool saved_mem = expr_save_memory();
    291 	expr(rn, true, false, true, false);
    292 	expr_restore_memory(saved_mem);
    293 
    294 	check_bit_field_init(ln, lt, rt);
    295 
    296 	/*
    297 	 * XXX: Is it correct to do this conversion _after_ the typeok above?
    298 	 */
    299 	if (lt != rt || (ltp->t_bitfield && rn->tn_op == CON))
    300 		rn = convert(INIT, 0, unconst_cast(ltp), rn);
    301 
    302 	check_non_constant_initializer(rn, lsym);
    303 }
    304 
    305 
    306 static const type_t *
    307 designator_type(const designator *dr, const type_t *tp)
    308 {
    309 
    310 	switch (tp->t_tspec) {
    311 	case STRUCT:
    312 	case UNION:
    313 		if (dr->dr_kind != DK_MEMBER) {
    314 			const sym_t *fmem = first_named_member(tp);
    315 			/* syntax error '%s' */
    316 			error(249, "designator '[...]' is only for arrays");
    317 			return fmem != NULL ? fmem->s_type : NULL;
    318 		}
    319 
    320 		lint_assert(dr->dr_member != NULL);
    321 		return dr->dr_member->s_type;
    322 	case ARRAY:
    323 		if (dr->dr_kind != DK_SUBSCRIPT) {
    324 			/* syntax error '%s' */
    325 			error(249,
    326 			    "designator '.member' is only for struct/union");
    327 		}
    328 		if (!tp->t_incomplete_array)
    329 			lint_assert(
    330 			    dr->dr_subscript < (size_t)tp->u.dimension);
    331 		return tp->t_subt;
    332 	default:
    333 		if (dr->dr_kind != DK_SCALAR)
    334 			/* syntax error '%s' */
    335 			error(249, "scalar type cannot use designator");
    336 		return tp;
    337 	}
    338 }
    339 
    340 
    341 #ifdef DEBUG
    342 static void
    343 designator_debug(const designator *dr)
    344 {
    345 
    346 	if (dr->dr_kind == DK_MEMBER) {
    347 		lint_assert(dr->dr_subscript == 0);
    348 		debug_printf(".%s",
    349 		    dr->dr_member != NULL
    350 			? dr->dr_member->s_name
    351 			: "<end>");
    352 	} else if (dr->dr_kind == DK_SUBSCRIPT) {
    353 		lint_assert(dr->dr_member == NULL);
    354 		debug_printf("[%zu]", dr->dr_subscript);
    355 	} else {
    356 		lint_assert(dr->dr_member == NULL);
    357 		lint_assert(dr->dr_subscript == 0);
    358 		debug_printf("<scalar>");
    359 	}
    360 
    361 	if (dr->dr_done)
    362 		debug_printf(" (done)");
    363 }
    364 
    365 static void
    366 designation_debug(const designation *dn)
    367 {
    368 
    369 	if (dn->dn_len == 0) {
    370 		debug_step("designation: (empty)");
    371 		return;
    372 	}
    373 
    374 	debug_printf("designation: ");
    375 	for (size_t i = 0; i < dn->dn_len; i++)
    376 		designator_debug(dn->dn_items + i);
    377 	debug_printf("\n");
    378 }
    379 #else
    380 #define designation_debug(dn) do { } while (false)
    381 #endif
    382 
    383 static designator *
    384 designation_last(designation *dn)
    385 {
    386 
    387 	lint_assert(dn->dn_len > 0);
    388 	return &dn->dn_items[dn->dn_len - 1];
    389 }
    390 
    391 void
    392 designation_push(designation *dn, designator_kind kind,
    393 		 const sym_t *member, size_t subscript)
    394 {
    395 
    396 	if (dn->dn_len == dn->dn_cap) {
    397 		dn->dn_cap += 4;
    398 		dn->dn_items = xrealloc(dn->dn_items,
    399 		    dn->dn_cap * sizeof(dn->dn_items[0]));
    400 	}
    401 
    402 	designator *dr = &dn->dn_items[dn->dn_len++];
    403 	dr->dr_kind = kind;
    404 	dr->dr_member = member;
    405 	dr->dr_subscript = subscript;
    406 	dr->dr_done = false;
    407 	designation_debug(dn);
    408 }
    409 
    410 /*
    411  * Extend the designation as appropriate for the given type.
    412  *
    413  * C11 6.7.9p17
    414  */
    415 static bool
    416 designation_descend(designation *dn, const type_t *tp)
    417 {
    418 
    419 	if (is_struct_or_union(tp->t_tspec)) {
    420 		const sym_t *member = first_named_member(tp);
    421 		if (member == NULL)
    422 			return false;
    423 		designation_push(dn, DK_MEMBER, member, 0);
    424 	} else if (tp->t_tspec == ARRAY)
    425 		designation_push(dn, DK_SUBSCRIPT, NULL, 0);
    426 	else
    427 		designation_push(dn, DK_SCALAR, NULL, 0);
    428 	return true;
    429 }
    430 
    431 /*
    432  * Starting at the type of the current object, resolve the type of the
    433  * sub-object by following each designator in the list.
    434  *
    435  * C99 6.7.8p18
    436  */
    437 static const type_t *
    438 designation_type(const designation *dn, const type_t *tp)
    439 {
    440 
    441 	for (size_t i = 0; i < dn->dn_len && tp != NULL; i++)
    442 		tp = designator_type(dn->dn_items + i, tp);
    443 	return tp;
    444 }
    445 
    446 static const type_t *
    447 designation_parent_type(const designation *dn, const type_t *tp)
    448 {
    449 
    450 	for (size_t i = 0; i + 1 < dn->dn_len && tp != NULL; i++)
    451 		tp = designator_type(dn->dn_items + i, tp);
    452 	return tp;
    453 }
    454 
    455 
    456 static brace_level *
    457 brace_level_new(const type_t *tp, brace_level *enclosing)
    458 {
    459 
    460 	brace_level *bl = xcalloc(1, sizeof(*bl));
    461 	bl->bl_type = tp;
    462 	bl->bl_enclosing = enclosing;
    463 
    464 	return bl;
    465 }
    466 
    467 static void
    468 brace_level_free(brace_level *bl)
    469 {
    470 
    471 	free(bl->bl_designation.dn_items);
    472 	free(bl);
    473 }
    474 
    475 #ifdef DEBUG
    476 static void
    477 brace_level_debug(const brace_level *bl)
    478 {
    479 
    480 	lint_assert(bl->bl_type != NULL);
    481 
    482 	debug_printf("type '%s'\n", type_name(bl->bl_type));
    483 	debug_indent_inc();
    484 	designation_debug(&bl->bl_designation);
    485 	debug_indent_dec();
    486 }
    487 #else
    488 #define brace_level_debug(level) do { } while (false)
    489 #endif
    490 
    491 /* Return the type of the sub-object that is currently being initialized. */
    492 static const type_t *
    493 brace_level_sub_type(const brace_level *bl)
    494 {
    495 
    496 	return designation_type(&bl->bl_designation, bl->bl_type);
    497 }
    498 
    499 /*
    500  * After initializing a sub-object, advance the designation to point after
    501  * the sub-object that has just been initialized.
    502  *
    503  * C99 6.7.8p17
    504  * C11 6.7.9p17
    505  */
    506 static void
    507 brace_level_advance(brace_level *bl, size_t *max_subscript)
    508 {
    509 
    510 	debug_enter();
    511 	designation *dn = &bl->bl_designation;
    512 	const type_t *tp = designation_parent_type(dn, bl->bl_type);
    513 
    514 	if (bl->bl_designation.dn_len == 0)
    515 		(void)designation_descend(dn, bl->bl_type);
    516 
    517 	designator *dr = designation_last(dn);
    518 	/* TODO: try to switch on dr->dr_kind instead */
    519 	switch (tp->t_tspec) {
    520 	case STRUCT:
    521 		lint_assert(dr->dr_member != NULL);
    522 		dr->dr_member = skip_unnamed(dr->dr_member->s_next);
    523 		if (dr->dr_member == NULL)
    524 			dr->dr_done = true;
    525 		break;
    526 	case UNION:
    527 		dr->dr_member = NULL;
    528 		dr->dr_done = true;
    529 		break;
    530 	case ARRAY:
    531 		dr->dr_subscript++;
    532 		if (tp->t_incomplete_array &&
    533 		    dr->dr_subscript > *max_subscript)
    534 			*max_subscript = dr->dr_subscript;
    535 		if (!tp->t_incomplete_array &&
    536 		    dr->dr_subscript >= (size_t)tp->u.dimension)
    537 			dr->dr_done = true;
    538 		break;
    539 	default:
    540 		dr->dr_done = true;
    541 		break;
    542 	}
    543 	designation_debug(dn);
    544 	debug_leave();
    545 }
    546 
    547 static void
    548 warn_too_many_initializers(designator_kind kind, const type_t *tp)
    549 {
    550 
    551 	if (kind == DK_MEMBER)
    552 		/* too many struct/union initializers */
    553 		error(172);
    554 	else if (kind == DK_SUBSCRIPT) {
    555 		lint_assert(tp->t_tspec == ARRAY);
    556 		lint_assert(!tp->t_incomplete_array);
    557 		/* too many array initializers, expected %d */
    558 		error(173, tp->u.dimension);
    559 	} else
    560 		/* too many initializers */
    561 		error(174);
    562 }
    563 
    564 static bool
    565 brace_level_pop_done(brace_level *bl, size_t *max_subscript)
    566 {
    567 	designation *dn = &bl->bl_designation;
    568 	designator_kind dr_kind = designation_last(dn)->dr_kind;
    569 	const type_t *sub_type = designation_parent_type(dn, bl->bl_type);
    570 
    571 	while (designation_last(dn)->dr_done) {
    572 		dn->dn_len--;
    573 		designation_debug(dn);
    574 		if (dn->dn_len == 0) {
    575 			warn_too_many_initializers(dr_kind, sub_type);
    576 			return false;
    577 		}
    578 		brace_level_advance(bl, max_subscript);
    579 	}
    580 	return true;
    581 }
    582 
    583 static void
    584 brace_level_pop_final(brace_level *bl, size_t *max_subscript)
    585 {
    586 	designation *dn = &bl->bl_designation;
    587 
    588 	while (dn->dn_len > 0 && designation_last(dn)->dr_done) {
    589 		dn->dn_len--;
    590 		designation_debug(dn);
    591 		if (dn->dn_len == 0)
    592 			return;
    593 		brace_level_advance(bl, max_subscript);
    594 	}
    595 }
    596 
    597 /*
    598  * Make the designation point to the sub-object to be initialized next.
    599  * Initially or after a previous expression, the designation is not advanced
    600  * yet since the place to stop depends on the next expression, especially for
    601  * string literals.
    602  */
    603 static bool
    604 brace_level_goto(brace_level *bl, const tnode_t *rn, size_t *max_subscript)
    605 {
    606 
    607 	designation *dn = &bl->bl_designation;
    608 	if (dn->dn_len == 0 && can_init_character_array(bl->bl_type, rn))
    609 		return true;
    610 	if (dn->dn_len == 0 && !designation_descend(dn, bl->bl_type))
    611 		return false;
    612 
    613 again:
    614 	if (!brace_level_pop_done(bl, max_subscript))
    615 		return false;
    616 
    617 	const type_t *ltp = brace_level_sub_type(bl);
    618 	if (types_compatible(ltp, rn->tn_type, true, false, NULL))
    619 		return true;
    620 
    621 	if (is_struct_or_union(ltp->t_tspec) || ltp->t_tspec == ARRAY) {
    622 		if (can_init_character_array(ltp, rn))
    623 			return true;
    624 		if (!designation_descend(dn, ltp))
    625 			return false;
    626 		goto again;
    627 	}
    628 
    629 	return true;
    630 }
    631 
    632 
    633 static initialization *
    634 initialization_new(sym_t *sym, initialization *enclosing)
    635 {
    636 
    637 	initialization *in = xcalloc(1, sizeof(*in));
    638 	in->in_sym = sym;
    639 	in->in_enclosing = enclosing;
    640 
    641 	return in;
    642 }
    643 
    644 static void
    645 initialization_free(initialization *in)
    646 {
    647 	brace_level *bl, *next;
    648 
    649 	/* TODO: lint_assert(in->in_brace_level == NULL) */
    650 	for (bl = in->in_brace_level; bl != NULL; bl = next) {
    651 		next = bl->bl_enclosing;
    652 		brace_level_free(bl);
    653 	}
    654 
    655 	free(in);
    656 }
    657 
    658 #ifdef DEBUG
    659 static void
    660 initialization_debug(const initialization *in)
    661 {
    662 
    663 	if (in->in_err)
    664 		debug_step("initialization error");
    665 	if (in->in_brace_level == NULL) {
    666 		debug_step("no brace level");
    667 		return;
    668 	}
    669 
    670 	const brace_level *bl;
    671 	size_t i = 0;
    672 	for (bl = in->in_brace_level; bl != NULL; bl = bl->bl_enclosing) {
    673 		debug_printf("brace level %zu: ", i);
    674 		brace_level_debug(bl);
    675 		i++;
    676 	}
    677 }
    678 #else
    679 #define initialization_debug(in) do { } while (false)
    680 #endif
    681 
    682 /*
    683  * Return the type of the object or sub-object that is currently being
    684  * initialized.
    685  */
    686 static const type_t *
    687 initialization_sub_type(initialization *in)
    688 {
    689 
    690 	if (in->in_brace_level == NULL)
    691 		return in->in_sym->s_type;
    692 
    693 	const type_t *tp = brace_level_sub_type(in->in_brace_level);
    694 	if (tp == NULL)
    695 		in->in_err = true;
    696 	return tp;
    697 }
    698 
    699 static void
    700 initialization_lbrace(initialization *in)
    701 {
    702 
    703 	if (in->in_err)
    704 		return;
    705 
    706 	debug_enter();
    707 
    708 	const type_t *tp = initialization_sub_type(in);
    709 	if (tp == NULL)
    710 		goto done;
    711 
    712 	brace_level *outer_bl = in->in_brace_level;
    713 	if (!allow_c90 && outer_bl == NULL)
    714 		check_trad_no_auto_aggregate(in->in_sym);
    715 
    716 	if (!allow_c90 && tp->t_tspec == UNION)
    717 		/* initialization of union is illegal in traditional C */
    718 		warning(238);
    719 
    720 	if (is_struct_or_union(tp->t_tspec) && tp->u.sou->sou_incomplete) {
    721 		/* initialization of incomplete type '%s' */
    722 		error(175, type_name(tp));
    723 		in->in_err = true;
    724 		goto done;
    725 	}
    726 
    727 	if (outer_bl != NULL && outer_bl->bl_designation.dn_len == 0) {
    728 		designation *dn = &outer_bl->bl_designation;
    729 		(void)designation_descend(dn, outer_bl->bl_type);
    730 		tp = designation_type(dn, outer_bl->bl_type);
    731 	}
    732 
    733 	in->in_brace_level = brace_level_new(tp, outer_bl);
    734 	if (is_struct_or_union(tp->t_tspec) &&
    735 	    first_named_member(tp) == NULL) {
    736 		/* cannot initialize struct/union with no named member */
    737 		error(179);
    738 		in->in_err = true;
    739 	}
    740 
    741 done:
    742 	initialization_debug(in);
    743 	debug_leave();
    744 }
    745 
    746 static void
    747 initialization_rbrace(initialization *in)
    748 {
    749 
    750 	debug_enter();
    751 
    752 	if (in->in_brace_level != NULL)
    753 		brace_level_pop_final(in->in_brace_level,
    754 		    &in->in_max_subscript);
    755 
    756 	/* C99 6.7.8p22 */
    757 	if (in->in_sym->s_type->t_incomplete_array &&
    758 	    in->in_brace_level->bl_enclosing == NULL) {
    759 
    760 		/* prevent "empty array declaration for '%s' [190]" */
    761 		size_t dim = in->in_max_subscript;
    762 		if (dim == 0 && in->in_err)
    763 			dim = 1;
    764 
    765 		update_type_of_array_of_unknown_size(in->in_sym, dim);
    766 	}
    767 
    768 	if (in->in_err)
    769 		goto done;
    770 
    771 	brace_level *inner_bl = in->in_brace_level;
    772 	brace_level *outer_bl = inner_bl->bl_enclosing;
    773 	in->in_brace_level = outer_bl;
    774 	brace_level_free(inner_bl);
    775 
    776 	if (outer_bl != NULL)
    777 		brace_level_advance(outer_bl, &in->in_max_subscript);
    778 
    779 done:
    780 	initialization_debug(in);
    781 	debug_leave();
    782 }
    783 
    784 static void
    785 initialization_add_designator_member(initialization *in, const char *name)
    786 {
    787 
    788 	if (in->in_err)
    789 		return;
    790 
    791 	brace_level *bl = in->in_brace_level;
    792 	lint_assert(bl != NULL);
    793 
    794 	const type_t *tp = brace_level_sub_type(bl);
    795 	if (is_struct_or_union(tp->t_tspec))
    796 		goto proceed;
    797 	else if (tp->t_tspec == ARRAY)
    798 		/* syntax error '%s' */
    799 		error(249, "designator '.member' is only for struct/union");
    800 	else
    801 		/* syntax error '%s' */
    802 		error(249, "scalar type cannot use designator");
    803 	in->in_err = true;
    804 	return;
    805 
    806 proceed:;
    807 	const sym_t *member = find_member(tp->u.sou, name);
    808 	if (member == NULL) {
    809 		/* type '%s' does not have member '%s' */
    810 		error(101, type_name(tp), name);
    811 		in->in_err = true;
    812 		return;
    813 	}
    814 
    815 	designation_push(&bl->bl_designation, DK_MEMBER, member, 0);
    816 }
    817 
    818 static void
    819 initialization_add_designator_subscript(initialization *in, size_t subscript)
    820 {
    821 
    822 	if (in->in_err)
    823 		return;
    824 
    825 	brace_level *bl = in->in_brace_level;
    826 	lint_assert(bl != NULL);
    827 
    828 	const type_t *tp = brace_level_sub_type(bl);
    829 	if (tp->t_tspec != ARRAY) {
    830 		/* syntax error '%s' */
    831 		error(249, "designator '[...]' is only for arrays");
    832 		in->in_err = true;
    833 		return;
    834 	}
    835 
    836 	if (!tp->t_incomplete_array && subscript >= (size_t)tp->u.dimension) {
    837 		/* array subscript cannot be > %d: %jd */
    838 		error(168, tp->u.dimension - 1, (intmax_t)subscript);
    839 		subscript = 0;	/* suppress further errors */
    840 	}
    841 
    842 	if (tp->t_incomplete_array && subscript > in->in_max_subscript)
    843 		in->in_max_subscript = subscript;
    844 
    845 	designation_push(&bl->bl_designation, DK_SUBSCRIPT, NULL, subscript);
    846 }
    847 
    848 /*
    849  * Initialize an object with automatic storage duration that has an
    850  * initializer expression without braces.
    851  */
    852 static bool
    853 initialization_expr_using_op(initialization *in, tnode_t *rn)
    854 {
    855 
    856 	if (!has_automatic_storage_duration(in->in_sym))
    857 		return false;
    858 	if (in->in_brace_level != NULL)
    859 		return false;
    860 	if (in->in_sym->s_type->t_tspec == ARRAY)
    861 		return false;
    862 
    863 	debug_step("handing over to INIT");
    864 
    865 	tnode_t *ln = build_name(in->in_sym, false);
    866 	ln->tn_type = expr_unqualified_type(ln->tn_type);
    867 
    868 	tnode_t *tn = build_binary(ln, INIT, false /* XXX */, rn);
    869 	expr(tn, false, false, false, false);
    870 
    871 	return true;
    872 }
    873 
    874 /* Initialize a character array or wchar_t array with a string literal. */
    875 static bool
    876 initialization_init_array_from_string(initialization *in, tnode_t *tn)
    877 {
    878 
    879 	if (tn->tn_op != STRING)
    880 		return false;
    881 
    882 	const type_t *tp = initialization_sub_type(in);
    883 
    884 	if (!can_init_character_array(tp, tn))
    885 		return false;
    886 
    887 	size_t len = tn->u.str_literals->len;
    888 	if (tn->u.str_literals->data != NULL) {
    889 		quoted_iterator it = { .end = 0 };
    890 		for (len = 0; quoted_next(tn->u.str_literals, &it); len++)
    891 			continue;
    892 	}
    893 
    894 	if (!tp->t_incomplete_array && (size_t)tp->u.dimension < len)
    895 		/* string literal too long (%ju) for target array (%ju) */
    896 		warning(187, (uintmax_t)len, (uintmax_t)tp->u.dimension);
    897 
    898 	brace_level *bl = in->in_brace_level;
    899 	if (bl != NULL && bl->bl_designation.dn_len == 0)
    900 		(void)designation_descend(&bl->bl_designation, bl->bl_type);
    901 	if (bl != NULL)
    902 		brace_level_advance(bl, &in->in_max_subscript);
    903 
    904 	if (tp->t_incomplete_array)
    905 		update_type_of_array_of_unknown_size(in->in_sym, len + 1);
    906 
    907 	return true;
    908 }
    909 
    910 /*
    911  * Initialize a single sub-object as part of the currently ongoing
    912  * initialization.
    913  */
    914 static void
    915 initialization_expr(initialization *in, tnode_t *tn)
    916 {
    917 
    918 	if (in->in_err || tn == NULL)
    919 		return;
    920 
    921 	debug_enter();
    922 
    923 	brace_level *bl = in->in_brace_level;
    924 	if (bl != NULL && !brace_level_goto(bl, tn, &in->in_max_subscript)) {
    925 		in->in_err = true;
    926 		goto done;
    927 	}
    928 	if (initialization_expr_using_op(in, tn))
    929 		goto done;
    930 	if (initialization_init_array_from_string(in, tn))
    931 		goto done;
    932 	if (in->in_err)
    933 		goto done;
    934 
    935 	const type_t *tp = initialization_sub_type(in);
    936 	if (tp == NULL)
    937 		goto done;
    938 
    939 	if (bl == NULL && !is_scalar(tp->t_tspec)) {
    940 		/* {}-enclosed or constant initializer of type '%s' required */
    941 		error(181, type_name(in->in_sym->s_type));
    942 		goto done;
    943 	}
    944 
    945 	debug_step("expecting '%s', expression has '%s'",
    946 	    type_name(tp), type_name(tn->tn_type));
    947 	check_init_expr(tp, in->in_sym, tn);
    948 	if (bl != NULL)
    949 		brace_level_advance(bl, &in->in_max_subscript);
    950 
    951 done:
    952 	initialization_debug(in);
    953 	debug_leave();
    954 }
    955 
    956 
    957 static initialization *init;
    958 
    959 
    960 sym_t *
    961 current_initsym(void)
    962 {
    963 
    964 	return init->in_sym;
    965 }
    966 
    967 void
    968 begin_initialization(sym_t *sym)
    969 {
    970 
    971 	debug_step("begin initialization of '%s'", type_name(sym->s_type));
    972 	debug_indent_inc();
    973 
    974 	init = initialization_new(sym, init);
    975 }
    976 
    977 void
    978 end_initialization(void)
    979 {
    980 
    981 	initialization *in = init;
    982 	init = in->in_enclosing;
    983 	initialization_free(in);
    984 
    985 	debug_indent_dec();
    986 	debug_step("end initialization");
    987 }
    988 
    989 void
    990 begin_designation(void)
    991 {
    992 
    993 	initialization *in = init;
    994 	if (in->in_err)
    995 		return;
    996 
    997 	designation *dn = &in->in_brace_level->bl_designation;
    998 	dn->dn_len = 0;
    999 	designation_debug(dn);
   1000 }
   1001 
   1002 void
   1003 add_designator_member(sbuf_t *sb)
   1004 {
   1005 
   1006 	initialization_add_designator_member(init, sb->sb_name);
   1007 }
   1008 
   1009 void
   1010 add_designator_subscript(range_t range)
   1011 {
   1012 
   1013 	initialization_add_designator_subscript(init, range.hi);
   1014 }
   1015 
   1016 void
   1017 init_lbrace(void)
   1018 {
   1019 
   1020 	initialization_lbrace(init);
   1021 }
   1022 
   1023 void
   1024 init_expr(tnode_t *tn)
   1025 {
   1026 
   1027 	initialization_expr(init, tn);
   1028 }
   1029 
   1030 void
   1031 init_rbrace(void)
   1032 {
   1033 
   1034 	initialization_rbrace(init);
   1035 }
   1036