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