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