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