Home | History | Annotate | Line # | Download | only in lint1
init.c revision 1.219
      1 /*	$NetBSD: init.c,v 1.219 2021/12/18 13:06:33 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) && !defined(lint)
     41 __RCSID("$NetBSD: init.c,v 1.219 2021/12/18 13:06:33 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  *
     56  *	int array_of_unknown_size[] = { 111, 222, 333 };
     57  *	int array_flat[2][2] = { 11, 12, 21, 22 };
     58  *	int array_nested[2][2] = { { 11, 12 }, { 21, 22 } };
     59  *
     60  *	struct { int x, y; } point = { 3, 4 };
     61  *	struct { int x, y; } point = { .y = 4, .x = 3 };
     62  *
     63  * Any scalar expression in the initializer may be surrounded by arbitrarily
     64  * many extra pairs of braces, like in the example 'number_with_braces' (C99
     65  * 6.7.8p11).
     66  *
     67  * For multi-dimensional arrays, the inner braces may be omitted like in
     68  * array_flat or spelled out like in array_nested.  This is unusual in
     69  * practice and therefore only supported very basically.
     70  *
     71  * During an initialization, the grammar parser calls these functions:
     72  *
     73  *	begin_initialization
     74  *		init_lbrace			for each '{'
     75  *		add_designator_member		for each '.member' before '='
     76  *		add_designator_subscript	for each '[123]' before '='
     77  *		init_expr			for each expression
     78  *		init_rbrace			for each '}'
     79  *	end_initialization
     80  *
     81  * Each '{' begins a new brace level, each '}' ends the current brace level.
     82  * Each brace level has an associated "current object", which is the starting
     83  * point for resolving the optional designations such as '.member[3]'.
     84  *
     85  * See also:
     86  *	C99 6.7.8 "Initialization"
     87  *	d_c99_init.c for more examples
     88  */
     89 
     90 /*
     91  * A single component on the path to the sub-object that is initialized by an
     92  * initializer expression.  Either a struct or union member, or an array
     93  * subscript.
     94  *
     95  * C99 6.7.8p6, 6.7.8p7
     96  */
     97 struct designator {
     98 	const char	*dr_name;	/* for struct and union */
     99 	size_t		dr_subscript;	/* for array */
    100 };
    101 
    102 /*
    103  * The optional designation for an initializer, saying which sub-object to
    104  * initialize.  Examples for designations are '.member' or
    105  * '.member[123].member.member[1][1]'.
    106  *
    107  * C99 6.7.8p6, 6.7.8p7
    108  */
    109 struct designation {
    110 	struct designator *dn_items;
    111 	size_t dn_len;
    112 	size_t dn_cap;
    113 };
    114 
    115 /*
    116  * Describes a single brace level of an ongoing initialization.
    117  *
    118  * See C99 6.7.8p17.
    119  */
    120 struct brace_level {
    121 	/*
    122 	 * The type of the current object that is initialized at this brace
    123 	 * level.
    124 	 */
    125 	const type_t	*bl_type;
    126 
    127 	struct designation bl_designation;	/* .member[123].member */
    128 
    129 	/*
    130 	 * The next member of the struct or union that is to be initialized,
    131 	 * unless a specific member is selected by a designator.
    132 	 */
    133 	const sym_t	*bl_member;
    134 	/*
    135 	 * The subscript of the next array element that is to be initialized,
    136 	 * unless a specific subscript is selected by a designator.
    137 	 */
    138 	size_t		bl_subscript;
    139 	bool		bl_scalar_done: 1;	/* for scalars */
    140 	bool		bl_confused: 1;		/* skip further checks */
    141 
    142 	struct brace_level *bl_enclosing;
    143 };
    144 
    145 /*
    146  * An ongoing initialization.
    147  *
    148  * In most cases there is only ever a single initialization going on.  See
    149  * pointer_to_compound_literal in msg_171.c for an exception.
    150  */
    151 struct initialization {
    152 	/* The symbol that is to be initialized. */
    153 	sym_t		*in_sym;
    154 
    155 	/* The innermost brace level. */
    156 	struct brace_level *in_brace_level;
    157 
    158 	/*
    159 	 * The maximum subscript that has ever be seen for an array of
    160 	 * unknown size, which can only occur at the outermost brace level.
    161 	 */
    162 	size_t		in_max_subscript;
    163 
    164 	/*
    165 	 * Is set when a structural error occurred in the initialization.
    166 	 * The effect is that the rest of the initialization is ignored
    167 	 * (parsed by yacc, expression trees built, but no initialization
    168 	 * takes place).
    169 	 */
    170 	bool		in_err;
    171 
    172 	struct initialization *in_enclosing;
    173 };
    174 
    175 
    176 static void *
    177 unconst_cast(const void *p)
    178 {
    179 	void *r;
    180 
    181 	memcpy(&r, &p, sizeof(r));
    182 	return r;
    183 }
    184 
    185 static bool
    186 has_automatic_storage_duration(const sym_t *sym)
    187 {
    188 
    189 	return sym->s_scl == AUTO || sym->s_scl == REG;
    190 }
    191 
    192 /* C99 6.7.8p14, 6.7.8p15 */
    193 static bool
    194 is_character_array(const type_t *tp, tspec_t t)
    195 {
    196 	tspec_t st;
    197 
    198 	if (tp == NULL || tp->t_tspec != ARRAY)
    199 		return false;
    200 
    201 	st = tp->t_subt->t_tspec;
    202 	return t == CHAR
    203 	    ? st == CHAR || st == UCHAR || st == SCHAR
    204 	    : st == WCHAR;
    205 }
    206 
    207 /* C99 6.7.8p9 */
    208 static bool
    209 is_unnamed(const sym_t *m)
    210 {
    211 
    212 	return m->s_bitfield && m->s_name == unnamed;
    213 }
    214 
    215 /* C99 6.7.8p9 */
    216 static const sym_t *
    217 skip_unnamed(const sym_t *m)
    218 {
    219 
    220 	while (m != NULL && is_unnamed(m))
    221 		m = m->s_next;
    222 	return m;
    223 }
    224 
    225 static const sym_t *
    226 first_named_member(const type_t *tp)
    227 {
    228 
    229 	lint_assert(is_struct_or_union(tp->t_tspec));
    230 	return skip_unnamed(tp->t_str->sou_first_member);
    231 }
    232 
    233 static const sym_t *
    234 look_up_member(const type_t *tp, const char *name)
    235 {
    236 	const sym_t *m;
    237 
    238 	lint_assert(is_struct_or_union(tp->t_tspec));
    239 	for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next)
    240 		if (!is_unnamed(m) && strcmp(m->s_name, name) == 0)
    241 			return m;
    242 	return NULL;
    243 }
    244 
    245 static const type_t *
    246 sym_type(const sym_t *sym)
    247 {
    248 
    249 	return sym != NULL ? sym->s_type : NULL;
    250 }
    251 
    252 static const type_t *
    253 look_up_member_type(const type_t *tp, const char *name)
    254 {
    255 	const sym_t *member;
    256 
    257 	member = look_up_member(tp, name);
    258 	if (member == NULL) {
    259 		/* type '%s' does not have member '%s' */
    260 		error(101, type_name(tp), name);
    261 	}
    262 
    263 	return sym_type(member);
    264 }
    265 
    266 /*
    267  * C99 6.7.8p22 says that the type of an array of unknown size becomes known
    268  * at the end of its initializer list.
    269  */
    270 static void
    271 update_type_of_array_of_unknown_size(sym_t *sym, size_t size)
    272 {
    273 	type_t *tp;
    274 
    275 	tp = dup_type(sym->s_type);
    276 	tp->t_dim = (int)size;
    277 	tp->t_incomplete_array = false;
    278 	sym->s_type = tp;
    279 }
    280 
    281 
    282 /* In traditional C, bit-fields can be initialized only by integer constants. */
    283 static void
    284 check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
    285 {
    286 
    287 	if (tflag &&
    288 	    is_integer(lt) &&
    289 	    ln->tn_type->t_bitfield &&
    290 	    !is_integer(rt)) {
    291 		/* bit-field initialization is illegal in traditional C */
    292 		warning(186);
    293 	}
    294 }
    295 
    296 static void
    297 check_non_constant_initializer(const tnode_t *tn, const sym_t *sym)
    298 {
    299 	const sym_t *unused_sym;
    300 	ptrdiff_t unused_offs;
    301 
    302 	if (tn == NULL || tn->tn_op == CON)
    303 		return;
    304 
    305 	if (constant_addr(tn, &unused_sym, &unused_offs))
    306 		return;
    307 
    308 	if (has_automatic_storage_duration(sym)) {
    309 		/* non-constant initializer */
    310 		c99ism(177);
    311 	} else {
    312 		/* non-constant initializer */
    313 		error(177);
    314 	}
    315 }
    316 
    317 static void
    318 check_trad_no_auto_aggregate(const sym_t *sym)
    319 {
    320 
    321 	if (has_automatic_storage_duration(sym) &&
    322 	    !is_scalar(sym->s_type->t_tspec)) {
    323 		/* no automatic aggregate initialization in traditional C */
    324 		warning(188);
    325 	}
    326 }
    327 
    328 static void
    329 check_init_expr(const type_t *tp, sym_t *sym, tnode_t *tn)
    330 {
    331 	tnode_t *ln;
    332 	type_t *ltp;
    333 	tspec_t lt, rt;
    334 	struct memory_block *tmem;
    335 
    336 	ltp = expr_unqualified_type(tp);
    337 
    338 	/* Create a temporary node for the left side. */
    339 	ln = expr_zalloc(sizeof(*ln));
    340 	ln->tn_op = NAME;
    341 	ln->tn_type = ltp;
    342 	ln->tn_lvalue = true;
    343 	ln->tn_sym = sym;
    344 
    345 	tn = cconv(tn);
    346 
    347 	lt = ln->tn_type->t_tspec;
    348 	rt = tn->tn_type->t_tspec;
    349 
    350 	debug_step("typeok '%s', '%s'",
    351 	    type_name(ln->tn_type), type_name(tn->tn_type));
    352 	if (!typeok(INIT, 0, ln, tn))
    353 		return;
    354 
    355 	/*
    356 	 * Preserve the tree memory. This is necessary because otherwise
    357 	 * expr() would free it.
    358 	 */
    359 	tmem = expr_save_memory();
    360 	expr(tn, true, false, true, false);
    361 	expr_restore_memory(tmem);
    362 
    363 	check_bit_field_init(ln, lt, rt);
    364 
    365 	/*
    366 	 * XXX: Is it correct to do this conversion _after_ the typeok above?
    367 	 */
    368 	if (lt != rt || (tp->t_bitfield && tn->tn_op == CON))
    369 		tn = convert(INIT, 0, unconst_cast(tp), tn);
    370 
    371 	check_non_constant_initializer(tn, sym);
    372 }
    373 
    374 
    375 static const type_t *
    376 designator_look_up(const struct designator *dr, const type_t *tp)
    377 {
    378 	switch (tp->t_tspec) {
    379 	case STRUCT:
    380 	case UNION:
    381 		if (dr->dr_name == NULL) {
    382 			/* syntax error '%s' */
    383 			error(249, "designator '[...]' is only for arrays");
    384 			return sym_type(first_named_member(tp));
    385 		}
    386 
    387 		return look_up_member_type(tp, dr->dr_name);
    388 	case ARRAY:
    389 		if (dr->dr_name != NULL) {
    390 			/* syntax error '%s' */
    391 			error(249,
    392 			    "designator '.member' is only for struct/union");
    393 		}
    394 		if (!tp->t_incomplete_array &&
    395 		    dr->dr_subscript >= (size_t)tp->t_dim) {
    396 			/* array subscript cannot be > %d: %ld */
    397 			error(168, tp->t_dim - 1, (long)dr->dr_subscript);
    398 		}
    399 		return tp->t_subt;
    400 	default:
    401 		/* syntax error '%s' */
    402 		error(249, "scalar type cannot use designator");
    403 		return tp;
    404 	}
    405 }
    406 
    407 
    408 #ifdef DEBUG
    409 static void
    410 designation_debug(const struct designation *dn)
    411 {
    412 	size_t i;
    413 
    414 	if (dn->dn_len == 0)
    415 		return;
    416 
    417 	debug_indent();
    418 	debug_printf("designation: ");
    419 	for (i = 0; i < dn->dn_len; i++) {
    420 		const struct designator *dr = dn->dn_items + i;
    421 		if (dr->dr_name != NULL) {
    422 			debug_printf(".%s", dr->dr_name);
    423 			lint_assert(dr->dr_subscript == 0);
    424 		} else
    425 			debug_printf("[%zu]", dr->dr_subscript);
    426 	}
    427 	debug_printf("\n");
    428 }
    429 #else
    430 #define designation_debug(dn) do { } while (false)
    431 #endif
    432 
    433 static void
    434 designation_add(struct designation *dn, const char *name, size_t subscript)
    435 {
    436 
    437 	if (dn->dn_len == dn->dn_cap) {
    438 		dn->dn_cap += 4;
    439 		dn->dn_items = xrealloc(dn->dn_items,
    440 		    dn->dn_cap * sizeof(dn->dn_items[0]));
    441 	}
    442 
    443 	dn->dn_items[dn->dn_len].dr_name = name;
    444 	dn->dn_items[dn->dn_len].dr_subscript = subscript;
    445 	dn->dn_len++;
    446 }
    447 
    448 /*
    449  * Starting at the type of the current object, resolve the type of the
    450  * sub-object by following each designator in the list.
    451  *
    452  * C99 6.7.8p18
    453  */
    454 static const type_t *
    455 designation_look_up(const struct designation *dn, const type_t *tp)
    456 {
    457 	size_t i;
    458 
    459 	for (i = 0; i < dn->dn_len && tp != NULL; i++)
    460 		tp = designator_look_up(dn->dn_items + i, tp);
    461 	return tp;
    462 }
    463 
    464 static void
    465 designation_reset(struct designation *dn)
    466 {
    467 
    468 	dn->dn_len = 0;
    469 }
    470 
    471 static void
    472 designation_free(struct designation *dn)
    473 {
    474 
    475 	free(dn->dn_items);
    476 }
    477 
    478 
    479 static struct brace_level *
    480 brace_level_new(const type_t *tp, struct brace_level *enclosing)
    481 {
    482 	struct brace_level *bl;
    483 
    484 	bl = xcalloc(1, sizeof(*bl));
    485 	bl->bl_type = tp;
    486 	bl->bl_enclosing = enclosing;
    487 	if (is_struct_or_union(tp->t_tspec))
    488 		bl->bl_member = first_named_member(tp);
    489 
    490 	return bl;
    491 }
    492 
    493 static void
    494 brace_level_free(struct brace_level *bl)
    495 {
    496 
    497 	designation_free(&bl->bl_designation);
    498 	free(bl);
    499 }
    500 
    501 #ifdef DEBUG
    502 static void
    503 brace_level_debug(const struct brace_level *bl)
    504 {
    505 
    506 	lint_assert(bl->bl_type != NULL);
    507 	lint_assert(bl->bl_member == NULL || !is_unnamed(bl->bl_member));
    508 
    509 	debug_printf("type '%s'", type_name(bl->bl_type));
    510 
    511 	if (is_struct_or_union(bl->bl_type->t_tspec) && bl->bl_member != NULL)
    512 		debug_printf(", member '%s'", bl->bl_member->s_name);
    513 	if (bl->bl_type->t_tspec == ARRAY)
    514 		debug_printf(", subscript %zu", bl->bl_subscript);
    515 
    516 	debug_printf("\n");
    517 }
    518 #else
    519 #define brace_level_debug(level) do { } while (false)
    520 #endif
    521 
    522 /* Return the type of the sub-object that is currently being initialized. */
    523 static const type_t *
    524 brace_level_sub_type(const struct brace_level *bl, bool is_string)
    525 {
    526 
    527 	if (bl->bl_designation.dn_len > 0)
    528 		return designation_look_up(&bl->bl_designation, bl->bl_type);
    529 
    530 	switch (bl->bl_type->t_tspec) {
    531 	case STRUCT:
    532 	case UNION:
    533 		if (bl->bl_member == NULL) {
    534 			/* too many struct/union initializers */
    535 			error(172);
    536 			return NULL;
    537 		}
    538 
    539 		lint_assert(!is_unnamed(bl->bl_member));
    540 		return sym_type(bl->bl_member);
    541 
    542 	case ARRAY:
    543 		if (!bl->bl_confused && !bl->bl_type->t_incomplete_array &&
    544 		    bl->bl_subscript >= (size_t)bl->bl_type->t_dim) {
    545 			/* too many array initializers, expected %d */
    546 			error(173, bl->bl_type->t_dim);
    547 		}
    548 
    549 		if (is_string && bl->bl_subscript == 0 &&
    550 		    bl->bl_type->t_subt->t_tspec != ARRAY)
    551 			return bl->bl_type;
    552 		return bl->bl_type->t_subt;
    553 
    554 	default:
    555 		if (bl->bl_scalar_done) {
    556 			/* too many initializers */
    557 			error(174);
    558 		}
    559 
    560 		return bl->bl_type;
    561 	}
    562 }
    563 
    564 /* C99 6.7.8p17 */
    565 static void
    566 brace_level_apply_designation(struct brace_level *bl)
    567 {
    568 	const struct designator *dr;
    569 
    570 	if (bl->bl_designation.dn_len == 0)
    571 		return;
    572 	dr = &bl->bl_designation.dn_items[0];
    573 
    574 	designation_debug(&bl->bl_designation);
    575 
    576 	switch (bl->bl_type->t_tspec) {
    577 	case STRUCT:
    578 	case UNION:
    579 		if (dr->dr_name == NULL)
    580 			return;	/* error, silently ignored */
    581 		bl->bl_member = look_up_member(bl->bl_type, dr->dr_name);
    582 		break;
    583 	case ARRAY:
    584 		if (dr->dr_name != NULL)
    585 			return;	/* error, silently ignored */
    586 		bl->bl_subscript = dr->dr_subscript;
    587 		break;
    588 	default:
    589 		break;		/* error, silently ignored */
    590 	}
    591 }
    592 
    593 /*
    594  * After initializing a sub-object, advance to the next sub-object.
    595  *
    596  * C99 6.7.8p17
    597  */
    598 static void
    599 brace_level_advance(struct brace_level *bl, size_t *max_subscript)
    600 {
    601 
    602 	switch (bl->bl_type->t_tspec) {
    603 	case STRUCT:
    604 		lint_assert(bl->bl_member != NULL);
    605 		bl->bl_member = skip_unnamed(bl->bl_member->s_next);
    606 		break;
    607 	case UNION:
    608 		bl->bl_member = NULL;
    609 		break;
    610 	case ARRAY:
    611 		bl->bl_subscript++;
    612 		if (bl->bl_subscript > *max_subscript)
    613 			*max_subscript = bl->bl_subscript;
    614 		break;
    615 	default:
    616 		bl->bl_scalar_done = true;
    617 		break;
    618 	}
    619 }
    620 
    621 
    622 static struct initialization *
    623 initialization_new(sym_t *sym, struct initialization *enclosing)
    624 {
    625 	struct initialization *in;
    626 
    627 	in = xcalloc(1, sizeof(*in));
    628 	in->in_sym = sym;
    629 	in->in_enclosing = enclosing;
    630 
    631 	return in;
    632 }
    633 
    634 static void
    635 initialization_free(struct initialization *in)
    636 {
    637 	struct brace_level *bl, *next;
    638 
    639 	for (bl = in->in_brace_level; bl != NULL; bl = next) {
    640 		next = bl->bl_enclosing;
    641 		brace_level_free(bl);
    642 	}
    643 
    644 	free(in);
    645 }
    646 
    647 #ifdef DEBUG
    648 static void
    649 initialization_debug(const struct initialization *in)
    650 {
    651 	size_t i;
    652 	const struct brace_level *bl;
    653 
    654 	if (in->in_brace_level == NULL) {
    655 		debug_step("no brace level");
    656 		return;
    657 	}
    658 
    659 	i = 0;
    660 	for (bl = in->in_brace_level; bl != NULL; bl = bl->bl_enclosing) {
    661 		debug_indent();
    662 		debug_printf("brace level %zu: ", i);
    663 		brace_level_debug(bl);
    664 		i++;
    665 	}
    666 }
    667 #else
    668 #define initialization_debug(in) do { } while (false)
    669 #endif
    670 
    671 /*
    672  * Return the type of the object or sub-object that is currently being
    673  * initialized.
    674  */
    675 static const type_t *
    676 initialization_sub_type(struct initialization *in, bool is_string)
    677 {
    678 	const type_t *tp;
    679 
    680 	tp = in->in_brace_level != NULL
    681 	    ? brace_level_sub_type(in->in_brace_level, is_string)
    682 	    : in->in_sym->s_type;
    683 	if (tp == NULL)
    684 		in->in_err = true;
    685 	return tp;
    686 }
    687 
    688 static void
    689 initialization_begin_brace_level(struct initialization *in)
    690 {
    691 	const type_t *tp;
    692 
    693 	if (in->in_err)
    694 		return;
    695 
    696 	debug_enter();
    697 
    698 	tp = initialization_sub_type(in, false);
    699 	if (tp == NULL) {
    700 		in->in_err = true;
    701 		goto done;
    702 	}
    703 
    704 	if (tflag && in->in_brace_level == NULL)
    705 		check_trad_no_auto_aggregate(in->in_sym);
    706 
    707 	if (tflag && tp->t_tspec == UNION) {
    708 		/* initialization of union is illegal in traditional C */
    709 		warning(238);
    710 	}
    711 
    712 	if (tp->t_tspec == STRUCT && tp->t_str->sou_incomplete) {
    713 		/* initialization of incomplete type '%s' */
    714 		error(175, type_name(tp));
    715 		in->in_err = true;
    716 		goto done;
    717 	}
    718 
    719 	if (in->in_brace_level != NULL)
    720 		brace_level_apply_designation(in->in_brace_level);
    721 
    722 	in->in_brace_level = brace_level_new(tp, in->in_brace_level);
    723 
    724 done:
    725 	initialization_debug(in);
    726 	debug_leave();
    727 }
    728 
    729 /* C99 6.7.8p22 */
    730 static void
    731 initialization_set_size_of_unknown_array(struct initialization *in)
    732 {
    733 	size_t dim;
    734 
    735 	if (!(in->in_sym->s_type->t_incomplete_array &&
    736 	      in->in_brace_level->bl_enclosing == NULL))
    737 		return;
    738 
    739 	dim = in->in_max_subscript;
    740 	if (dim == 0 && (in->in_err || in->in_brace_level->bl_confused))
    741 		dim = 1;	/* prevent "empty array declaration: %s" */
    742 
    743 	update_type_of_array_of_unknown_size(in->in_sym, dim);
    744 }
    745 
    746 static void
    747 initialization_end_brace_level(struct initialization *in)
    748 {
    749 	struct brace_level *bl;
    750 
    751 	debug_enter();
    752 
    753 	initialization_set_size_of_unknown_array(in);
    754 	if (in->in_err)
    755 		goto done;
    756 
    757 	bl = in->in_brace_level;
    758 	in->in_brace_level = bl->bl_enclosing;
    759 	brace_level_free(bl);
    760 	bl = in->in_brace_level;
    761 
    762 	if (bl != NULL)
    763 		brace_level_advance(bl, &in->in_max_subscript);
    764 	if (bl != NULL)
    765 		designation_reset(&bl->bl_designation);
    766 
    767 done:
    768 	initialization_debug(in);
    769 	debug_leave();
    770 }
    771 
    772 static void
    773 initialization_add_designator(struct initialization *in,
    774 			      const char *name, size_t subscript)
    775 {
    776 
    777 	if (in->in_err)
    778 		return;
    779 
    780 	lint_assert(in->in_brace_level != NULL);
    781 	designation_add(&in->in_brace_level->bl_designation, name, subscript);
    782 }
    783 
    784 /*
    785  * Initialize an object with automatic storage duration that has an
    786  * initializer expression without braces.
    787  */
    788 static bool
    789 initialization_expr_using_op(struct initialization *in, tnode_t *rn)
    790 {
    791 	tnode_t *ln, *tn;
    792 
    793 	if (!has_automatic_storage_duration(in->in_sym))
    794 		return false;
    795 	if (in->in_brace_level != NULL)
    796 		return false;
    797 	if (in->in_sym->s_type->t_tspec == ARRAY)
    798 		return false;
    799 
    800 	debug_step("handing over to INIT");
    801 
    802 	ln = build_name(in->in_sym, false);
    803 	ln->tn_type = expr_unqualified_type(ln->tn_type);
    804 
    805 	tn = build_binary(ln, INIT, false /* XXX */, rn);
    806 	expr(tn, false, false, false, false);
    807 
    808 	return true;
    809 }
    810 
    811 /* Initialize a character array or wchar_t array with a string literal. */
    812 static bool
    813 initialization_init_array_using_string(struct initialization *in, tnode_t *tn)
    814 {
    815 	struct brace_level *bl;
    816 	const type_t *tp;
    817 	strg_t	*strg;
    818 
    819 	if (tn->tn_op != STRING)
    820 		return false;
    821 
    822 	bl = in->in_brace_level;
    823 	tp = initialization_sub_type(in, true);
    824 	strg = tn->tn_string;
    825 
    826 	if (!is_character_array(tp, strg->st_tspec)) {
    827 		/* TODO: recursively try first member or [0] */
    828 		if (is_struct_or_union(tp->t_tspec) ||
    829 		    (tp->t_tspec == ARRAY &&
    830 		     is_struct_or_union(tp->t_subt->t_tspec)))
    831 			in->in_err = true;
    832 		return false;
    833 	}
    834 
    835 	if (bl != NULL && tp->t_tspec != ARRAY && bl->bl_subscript != 0)
    836 		return false;
    837 
    838 	if (!tp->t_incomplete_array && tp->t_dim < (int)strg->st_len) {
    839 		/* non-null byte ignored in string initializer */
    840 		warning(187);
    841 	}
    842 
    843 	if (tp == in->in_sym->s_type && tp->t_incomplete_array) {
    844 		if (bl != NULL) {
    845 			bl->bl_subscript = strg->st_len;
    846 			/* see brace_level_advance for the +1 */
    847 			/* see initialization_set_size_of_unknown_array */
    848 		} else
    849 			update_type_of_array_of_unknown_size(in->in_sym,
    850 			    strg->st_len + 1);
    851 	}
    852 
    853 	return true;
    854 }
    855 
    856 /*
    857  * Initialize a single sub-object as part of the currently ongoing
    858  * initialization.
    859  */
    860 static void
    861 initialization_expr(struct initialization *in, tnode_t *tn)
    862 {
    863 	struct brace_level *bl;
    864 	const type_t *tp;
    865 
    866 	if (in->in_err)
    867 		return;
    868 
    869 	bl = in->in_brace_level;
    870 	if (bl != NULL && bl->bl_confused)
    871 		return;
    872 
    873 	debug_enter();
    874 
    875 	if (tn == NULL)
    876 		goto advance;
    877 	if (initialization_expr_using_op(in, tn))
    878 		goto done;
    879 	if (initialization_init_array_using_string(in, tn))
    880 		goto advance;
    881 	if (in->in_err)
    882 		goto done;
    883 
    884 	if (bl != NULL)
    885 		brace_level_apply_designation(bl);
    886 	tp = initialization_sub_type(in, false);
    887 	if (tp == NULL)
    888 		goto done;
    889 
    890 	if (bl == NULL && !is_scalar(tp->t_tspec)) {
    891 		/* {}-enclosed initializer required */
    892 		error(181);
    893 		goto done;
    894 	}
    895 
    896 	/*
    897 	 * Hack to accept initializations with omitted braces, see
    898 	 * c99_6_7_8_p28_example5 in test d_c99_init.c.  Since both GCC and
    899 	 * Clang already warn about this at level -Wall, there is no point
    900 	 * in repeating the same check in lint.  If needed, support for these
    901 	 * edge cases could be added, but that would increase the complexity.
    902 	 */
    903 	if ((is_scalar(tn->tn_type->t_tspec) ||
    904 	     tn->tn_type->t_tspec == FUNC) &&
    905 	    (tp->t_tspec == ARRAY || is_struct_or_union(tp->t_tspec)) &&
    906 	    bl != NULL) {
    907 		bl->bl_confused = true;
    908 		goto done;
    909 	}
    910 
    911 	debug_step("expecting '%s', expression has '%s'",
    912 	    type_name(tp), type_name(tn->tn_type));
    913 	check_init_expr(tp, in->in_sym, tn);
    914 
    915 advance:
    916 	if (bl != NULL)
    917 		brace_level_advance(bl, &in->in_max_subscript);
    918 done:
    919 	if (bl != NULL)
    920 		designation_reset(&bl->bl_designation);
    921 
    922 	initialization_debug(in);
    923 	debug_leave();
    924 }
    925 
    926 
    927 static struct initialization *init;
    928 
    929 
    930 static struct initialization *
    931 current_init(void)
    932 {
    933 
    934 	lint_assert(init != NULL);
    935 	return init;
    936 }
    937 
    938 sym_t **
    939 current_initsym(void)
    940 {
    941 
    942 	return &current_init()->in_sym;
    943 }
    944 
    945 void
    946 begin_initialization(sym_t *sym)
    947 {
    948 
    949 	debug_step("begin initialization of '%s'", type_name(sym->s_type));
    950 	debug_indent_inc();
    951 
    952 	init = initialization_new(sym, init);
    953 }
    954 
    955 void
    956 end_initialization(void)
    957 {
    958 	struct initialization *in;
    959 
    960 	in = init;
    961 	init = in->in_enclosing;
    962 	initialization_free(in);
    963 
    964 	debug_indent_dec();
    965 	debug_step("end initialization");
    966 }
    967 
    968 void
    969 add_designator_member(sbuf_t *sb)
    970 {
    971 
    972 	initialization_add_designator(current_init(), sb->sb_name, 0);
    973 }
    974 
    975 void
    976 add_designator_subscript(range_t range)
    977 {
    978 
    979 	initialization_add_designator(current_init(), NULL, range.hi);
    980 }
    981 
    982 void
    983 init_lbrace(void)
    984 {
    985 
    986 	initialization_begin_brace_level(current_init());
    987 }
    988 
    989 void
    990 init_expr(tnode_t *tn)
    991 {
    992 
    993 	initialization_expr(current_init(), tn);
    994 }
    995 
    996 void
    997 init_rbrace(void)
    998 {
    999 
   1000 	initialization_end_brace_level(current_init());
   1001 }
   1002