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