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