Home | History | Annotate | Line # | Download | only in lint1
      1 /*	$NetBSD: init.c,v 1.275 2025/07/11 19:03:01 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.275 2025/07/11 19:03:01 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)
    413 {
    414 
    415 	for (size_t i = 0; i < dn->dn_len && tp != NULL; i++)
    416 		tp = designator_type(dn->dn_items + i, tp);
    417 	return tp;
    418 }
    419 
    420 static const type_t *
    421 designation_parent_type(const designation *dn, const type_t *tp)
    422 {
    423 
    424 	for (size_t i = 0; i + 1 < dn->dn_len && tp != NULL; i++)
    425 		tp = designator_type(dn->dn_items + i, tp);
    426 	return tp;
    427 }
    428 
    429 
    430 static brace_level *
    431 brace_level_new(const type_t *tp, brace_level *enclosing)
    432 {
    433 
    434 	brace_level *bl = xcalloc(1, sizeof(*bl));
    435 	bl->bl_type = tp;
    436 	bl->bl_enclosing = enclosing;
    437 
    438 	return bl;
    439 }
    440 
    441 static void
    442 brace_level_free(brace_level *bl)
    443 {
    444 
    445 	free(bl->bl_designation.dn_items);
    446 	free(bl);
    447 }
    448 
    449 #ifdef DEBUG
    450 static void
    451 brace_level_debug(const brace_level *bl)
    452 {
    453 
    454 	lint_assert(bl->bl_type != NULL);
    455 
    456 	debug_printf("type '%s'\n", type_name(bl->bl_type));
    457 	debug_indent_inc();
    458 	designation_debug(&bl->bl_designation);
    459 	debug_indent_dec();
    460 }
    461 #else
    462 #define brace_level_debug(level) do { } while (false)
    463 #endif
    464 
    465 /* Return the type of the sub-object that is currently being initialized. */
    466 static const type_t *
    467 brace_level_sub_type(const brace_level *bl)
    468 {
    469 
    470 	return designation_type(&bl->bl_designation, bl->bl_type);
    471 }
    472 
    473 /*
    474  * After initializing a sub-object, advance the designation to point after
    475  * the sub-object that has just been initialized.
    476  *
    477  * C99 6.7.8p17
    478  * C11 6.7.9p17
    479  */
    480 static void
    481 brace_level_advance(brace_level *bl, size_t *max_subscript)
    482 {
    483 
    484 	debug_enter();
    485 	designation *dn = &bl->bl_designation;
    486 	const type_t *tp = designation_parent_type(dn, bl->bl_type);
    487 
    488 	if (bl->bl_designation.dn_len == 0)
    489 		(void)designation_descend(dn, bl->bl_type);
    490 
    491 	designator *dr = designation_last(dn);
    492 	switch (tp->t_tspec) {
    493 	case STRUCT:
    494 		lint_assert(dr->dr_member != NULL);
    495 		dr->dr_member = skip_unnamed(dr->dr_member->s_next);
    496 		if (dr->dr_member == NULL)
    497 			dr->dr_done = true;
    498 		break;
    499 	case UNION:
    500 		dr->dr_member = NULL;
    501 		dr->dr_done = true;
    502 		break;
    503 	case ARRAY:
    504 		dr->dr_subscript++;
    505 		if (tp->t_incomplete_array &&
    506 		    dr->dr_subscript > *max_subscript)
    507 			*max_subscript = dr->dr_subscript;
    508 		if (!tp->t_incomplete_array &&
    509 		    dr->dr_subscript >= (size_t)tp->u.dimension)
    510 			dr->dr_done = true;
    511 		break;
    512 	default:
    513 		dr->dr_done = true;
    514 		break;
    515 	}
    516 	designation_debug(dn);
    517 	debug_leave();
    518 }
    519 
    520 static void
    521 warn_too_many_initializers(designator_kind kind, const type_t *tp)
    522 {
    523 
    524 	if (kind == DK_MEMBER)
    525 		/* too many struct/union initializers */
    526 		error(172);
    527 	else if (kind == DK_SUBSCRIPT) {
    528 		lint_assert(tp->t_tspec == ARRAY);
    529 		lint_assert(!tp->t_incomplete_array);
    530 		/* too many array initializers, expected %d */
    531 		error(173, tp->u.dimension);
    532 	} else
    533 		/* too many initializers for '%s' */
    534 		error(174, type_name(tp));
    535 }
    536 
    537 static bool
    538 brace_level_pop_done(brace_level *bl, size_t *max_subscript)
    539 {
    540 	designation *dn = &bl->bl_designation;
    541 	designator_kind dr_kind = designation_last(dn)->dr_kind;
    542 	const type_t *sub_type = designation_parent_type(dn, bl->bl_type);
    543 
    544 	while (designation_last(dn)->dr_done) {
    545 		dn->dn_len--;
    546 		designation_debug(dn);
    547 		if (dn->dn_len == 0) {
    548 			warn_too_many_initializers(dr_kind, sub_type);
    549 			return false;
    550 		}
    551 		brace_level_advance(bl, max_subscript);
    552 	}
    553 	return true;
    554 }
    555 
    556 static void
    557 brace_level_pop_final(brace_level *bl, size_t *max_subscript)
    558 {
    559 	designation *dn = &bl->bl_designation;
    560 
    561 	while (dn->dn_len > 0 && designation_last(dn)->dr_done) {
    562 		dn->dn_len--;
    563 		designation_debug(dn);
    564 		if (dn->dn_len == 0)
    565 			return;
    566 		brace_level_advance(bl, max_subscript);
    567 	}
    568 }
    569 
    570 /* Make the designation point to the sub-object to be initialized next. */
    571 static bool
    572 brace_level_goto(brace_level *bl, const tnode_t *rn, size_t *max_subscript)
    573 {
    574 
    575 	designation *dn = &bl->bl_designation;
    576 	if (dn->dn_len == 0 && can_init_character_array(bl->bl_type, rn))
    577 		return true;
    578 	if (dn->dn_len == 0 && !designation_descend(dn, bl->bl_type))
    579 		return false;
    580 
    581 again:
    582 	if (!brace_level_pop_done(bl, max_subscript))
    583 		return false;
    584 
    585 	const type_t *ltp = brace_level_sub_type(bl);
    586 	if (types_compatible(ltp, rn->tn_type, true, false, NULL))
    587 		return true;
    588 
    589 	if (is_struct_or_union(ltp->t_tspec) || ltp->t_tspec == ARRAY) {
    590 		if (can_init_character_array(ltp, rn))
    591 			return true;
    592 		if (!designation_descend(dn, ltp))
    593 			return false;
    594 		goto again;
    595 	}
    596 
    597 	return true;
    598 }
    599 
    600 
    601 static initialization *
    602 initialization_new(sym_t *sym, initialization *enclosing)
    603 {
    604 
    605 	initialization *in = xcalloc(1, sizeof(*in));
    606 	in->in_sym = sym;
    607 	in->in_enclosing = enclosing;
    608 
    609 	return in;
    610 }
    611 
    612 static void
    613 initialization_free(initialization *in)
    614 {
    615 	brace_level *bl, *next;
    616 
    617 	for (bl = in->in_brace_level; bl != NULL; bl = next) {
    618 		next = bl->bl_enclosing;
    619 		brace_level_free(bl);
    620 	}
    621 
    622 	free(in);
    623 }
    624 
    625 #ifdef DEBUG
    626 static void
    627 initialization_debug(const initialization *in)
    628 {
    629 
    630 	if (in->in_err)
    631 		debug_step("initialization error");
    632 	if (in->in_brace_level == NULL) {
    633 		debug_step("no brace level");
    634 		return;
    635 	}
    636 
    637 	const brace_level *bl;
    638 	size_t i = 0;
    639 	for (bl = in->in_brace_level; bl != NULL; bl = bl->bl_enclosing) {
    640 		debug_printf("brace level %zu: ", i);
    641 		brace_level_debug(bl);
    642 		i++;
    643 	}
    644 }
    645 #else
    646 #define initialization_debug(in) do { } while (false)
    647 #endif
    648 
    649 /*
    650  * Return the type of the object or sub-object that is currently being
    651  * initialized.
    652  */
    653 static const type_t *
    654 initialization_sub_type(initialization *in)
    655 {
    656 
    657 	if (in->in_brace_level == NULL)
    658 		return in->in_sym->s_type;
    659 
    660 	const type_t *tp = brace_level_sub_type(in->in_brace_level);
    661 	if (tp == NULL)
    662 		in->in_err = true;
    663 	return tp;
    664 }
    665 
    666 static void
    667 initialization_lbrace(initialization *in)
    668 {
    669 
    670 	if (in->in_err)
    671 		return;
    672 
    673 	debug_enter();
    674 
    675 	const type_t *tp = initialization_sub_type(in);
    676 	if (tp == NULL)
    677 		goto done;
    678 
    679 	brace_level *outer_bl = in->in_brace_level;
    680 	if (!allow_c90 && outer_bl == NULL)
    681 		check_trad_no_auto_aggregate(in->in_sym);
    682 
    683 	if (!allow_c90 && tp->t_tspec == UNION)
    684 		/* initialization of union requires C90 or later */
    685 		warning(238);
    686 
    687 	if (is_struct_or_union(tp->t_tspec) && tp->u.sou->sou_incomplete) {
    688 		/* initialization of incomplete type '%s' */
    689 		error(175, type_name(tp));
    690 		in->in_err = true;
    691 		goto done;
    692 	}
    693 
    694 	if (outer_bl != NULL && outer_bl->bl_designation.dn_len == 0) {
    695 		designation *dn = &outer_bl->bl_designation;
    696 		(void)designation_descend(dn, outer_bl->bl_type);
    697 		tp = designation_type(dn, outer_bl->bl_type);
    698 	}
    699 
    700 	in->in_brace_level = brace_level_new(tp, outer_bl);
    701 	if (is_struct_or_union(tp->t_tspec) &&
    702 	    first_named_member(tp) == NULL) {
    703 		/* cannot initialize struct/union with no named member */
    704 		error(179);
    705 		in->in_err = true;
    706 	}
    707 
    708 done:
    709 	initialization_debug(in);
    710 	debug_leave();
    711 }
    712 
    713 static void
    714 initialization_rbrace(initialization *in)
    715 {
    716 
    717 	debug_enter();
    718 
    719 	if (in->in_brace_level != NULL)
    720 		brace_level_pop_final(in->in_brace_level,
    721 		    &in->in_max_subscript);
    722 
    723 	/* C99 6.7.8p22 */
    724 	if (in->in_sym->s_type->t_incomplete_array &&
    725 	    in->in_brace_level->bl_enclosing == NULL) {
    726 
    727 		/* prevent "empty array declaration for '%s' [190]" */
    728 		size_t dim = in->in_max_subscript;
    729 		if (dim == 0 && in->in_err)
    730 			dim = 1;
    731 
    732 		update_type_of_array_of_unknown_size(in->in_sym, dim);
    733 	}
    734 
    735 	if (in->in_err)
    736 		goto done;
    737 
    738 	brace_level *inner_bl = in->in_brace_level;
    739 	brace_level *outer_bl = inner_bl->bl_enclosing;
    740 	in->in_brace_level = outer_bl;
    741 	brace_level_free(inner_bl);
    742 
    743 	if (outer_bl != NULL)
    744 		brace_level_advance(outer_bl, &in->in_max_subscript);
    745 
    746 done:
    747 	initialization_debug(in);
    748 	debug_leave();
    749 }
    750 
    751 static void
    752 initialization_add_designator_member(initialization *in, const char *name)
    753 {
    754 
    755 	if (in->in_err)
    756 		return;
    757 
    758 	brace_level *bl = in->in_brace_level;
    759 	lint_assert(bl != NULL);
    760 
    761 	const type_t *tp = brace_level_sub_type(bl);
    762 	if (is_struct_or_union(tp->t_tspec))
    763 		goto proceed;
    764 	else if (tp->t_tspec == ARRAY)
    765 		/* syntax error '%s' */
    766 		error(249, "designator '.member' is only for struct/union");
    767 	else
    768 		/* syntax error '%s' */
    769 		error(249, "scalar type cannot use designator");
    770 	in->in_err = true;
    771 	return;
    772 
    773 proceed:;
    774 	const sym_t *member = find_member(tp->u.sou, name);
    775 	if (member == NULL) {
    776 		/* type '%s' does not have member '%s' */
    777 		error(101, type_name(tp), name);
    778 		in->in_err = true;
    779 		return;
    780 	}
    781 
    782 	designation_push(&bl->bl_designation, DK_MEMBER, member, 0);
    783 }
    784 
    785 static void
    786 initialization_add_designator_subscript(initialization *in, size_t subscript)
    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 (tp->t_tspec != ARRAY) {
    797 		/* syntax error '%s' */
    798 		error(249, "designator '[...]' is only for arrays");
    799 		in->in_err = true;
    800 		return;
    801 	}
    802 
    803 	if (!tp->t_incomplete_array && subscript >= (size_t)tp->u.dimension) {
    804 		/* array subscript %ju cannot be > %d */
    805 		error(168, (uintmax_t)subscript, tp->u.dimension - 1);
    806 		subscript = 0;	/* suppress further errors */
    807 	}
    808 
    809 	if (tp->t_incomplete_array && subscript > in->in_max_subscript)
    810 		in->in_max_subscript = subscript;
    811 
    812 	designation_push(&bl->bl_designation, DK_SUBSCRIPT, NULL, subscript);
    813 }
    814 
    815 /*
    816  * Initialize an object with automatic storage duration that has an
    817  * initializer expression without braces.
    818  */
    819 static bool
    820 initialization_expr_using_op(initialization *in, tnode_t *rn)
    821 {
    822 
    823 	if (!has_automatic_storage_duration(in->in_sym))
    824 		return false;
    825 	if (in->in_brace_level != NULL)
    826 		return false;
    827 	if (in->in_sym->s_type->t_tspec == ARRAY)
    828 		return false;
    829 
    830 	debug_step("handing over to INIT");
    831 
    832 	tnode_t *ln = build_name(in->in_sym, false);
    833 	ln->tn_type = expr_unqualified_type(ln->tn_type);
    834 
    835 	tnode_t *tn = build_binary(ln, INIT, false /* XXX */, rn);
    836 	expr(tn, false, false, false, false, "init");
    837 
    838 	return true;
    839 }
    840 
    841 /* Initialize a character array or wchar_t array with a string literal. */
    842 static bool
    843 initialization_init_array_from_string(initialization *in, tnode_t *tn)
    844 {
    845 
    846 	if (tn->tn_op != STRING)
    847 		return false;
    848 
    849 	const type_t *tp = initialization_sub_type(in);
    850 
    851 	if (!can_init_character_array(tp, tn))
    852 		return false;
    853 
    854 	size_t len = tn->u.str_literals->len;
    855 	if (tn->u.str_literals->data != NULL) {
    856 		quoted_iterator it = { .end = 0 };
    857 		for (len = 0; quoted_next(tn->u.str_literals, &it); len++)
    858 			continue;
    859 	}
    860 
    861 	if (!tp->t_incomplete_array && (size_t)tp->u.dimension < len)
    862 		/* string literal too long (%ju) for target array (%ju) */
    863 		warning(187, (uintmax_t)len, (uintmax_t)tp->u.dimension);
    864 
    865 	brace_level *bl = in->in_brace_level;
    866 	if (bl != NULL && bl->bl_designation.dn_len == 0)
    867 		(void)designation_descend(&bl->bl_designation, bl->bl_type);
    868 	if (bl != NULL)
    869 		brace_level_advance(bl, &in->in_max_subscript);
    870 
    871 	if (tp->t_incomplete_array) {
    872 		update_type_of_array_of_unknown_size(in->in_sym, len + 1);
    873 		in->in_sym->u.s_array_nonnull_dimension = len;
    874 	}
    875 
    876 	return true;
    877 }
    878 
    879 /*
    880  * Initialize a single sub-object as part of the currently ongoing
    881  * initialization.
    882  */
    883 static void
    884 initialization_expr(initialization *in, tnode_t *tn)
    885 {
    886 
    887 	if (in->in_err || tn == NULL)
    888 		return;
    889 
    890 	debug_enter();
    891 
    892 	brace_level *bl = in->in_brace_level;
    893 	if (bl != NULL && !brace_level_goto(bl, tn, &in->in_max_subscript)) {
    894 		in->in_err = true;
    895 		goto done;
    896 	}
    897 	if (in->in_sym->s_type->t_tspec == AUTO_TYPE)
    898 		in->in_sym->s_type = block_dup_type(tn->tn_type);
    899 	if (initialization_expr_using_op(in, tn))
    900 		goto done;
    901 	if (initialization_init_array_from_string(in, tn))
    902 		goto done;
    903 	if (in->in_err)
    904 		goto done;
    905 
    906 	const type_t *tp = initialization_sub_type(in);
    907 	if (tp == NULL)
    908 		goto done;
    909 
    910 	if (bl == NULL && !is_scalar(tp->t_tspec)) {
    911 		/* {}-enclosed or constant initializer of type '%s' required */
    912 		error(181, type_name(in->in_sym->s_type));
    913 		goto done;
    914 	}
    915 
    916 	debug_step("expecting '%s', expression has '%s'",
    917 	    type_name(tp), type_name(tn->tn_type));
    918 	check_init_expr(tp, in->in_sym, tn);
    919 	if (bl != NULL)
    920 		brace_level_advance(bl, &in->in_max_subscript);
    921 
    922 done:
    923 	initialization_debug(in);
    924 	debug_leave();
    925 }
    926 
    927 
    928 static initialization *init;
    929 
    930 
    931 sym_t *
    932 current_initsym(void)
    933 {
    934 
    935 	return init->in_sym;
    936 }
    937 
    938 void
    939 begin_initialization(sym_t *sym)
    940 {
    941 
    942 	debug_step("begin initialization of '%s'", type_name(sym->s_type));
    943 	debug_indent_inc();
    944 
    945 	init = initialization_new(sym, init);
    946 }
    947 
    948 void
    949 end_initialization(void)
    950 {
    951 
    952 	initialization *in = init;
    953 	init = in->in_enclosing;
    954 	initialization_free(in);
    955 
    956 	debug_indent_dec();
    957 	debug_step("end initialization");
    958 }
    959 
    960 void
    961 begin_designation(void)
    962 {
    963 
    964 	initialization *in = init;
    965 	if (in->in_err)
    966 		return;
    967 
    968 	designation *dn = &in->in_brace_level->bl_designation;
    969 	dn->dn_len = 0;
    970 	designation_debug(dn);
    971 }
    972 
    973 void
    974 add_designator_member(sbuf_t *sb)
    975 {
    976 
    977 	initialization_add_designator_member(init, sb->sb_name);
    978 }
    979 
    980 void
    981 add_designator_subscript(range_t range)
    982 {
    983 
    984 	initialization_add_designator_subscript(init, range.hi);
    985 }
    986 
    987 void
    988 init_lbrace(void)
    989 {
    990 
    991 	initialization_lbrace(init);
    992 }
    993 
    994 void
    995 init_expr(tnode_t *tn)
    996 {
    997 
    998 	initialization_expr(init, tn);
    999 }
   1000 
   1001 void
   1002 init_rbrace(void)
   1003 {
   1004 
   1005 	initialization_rbrace(init);
   1006 }
   1007