Home | History | Annotate | Line # | Download | only in lint1
init.c revision 1.134
      1  1.134    rillig /*	$NetBSD: init.c,v 1.134 2021/03/26 20:31:07 rillig Exp $	*/
      2    1.2       cgd 
      3    1.1       cgd /*
      4    1.1       cgd  * Copyright (c) 1994, 1995 Jochen Pohl
      5    1.1       cgd  * All Rights Reserved.
      6    1.1       cgd  *
      7    1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8    1.1       cgd  * modification, are permitted provided that the following conditions
      9    1.1       cgd  * are met:
     10    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15    1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16    1.1       cgd  *    must display the following acknowledgement:
     17    1.1       cgd  *      This product includes software developed by Jochen Pohl for
     18    1.1       cgd  *	The NetBSD Project.
     19    1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     20    1.1       cgd  *    derived from this software without specific prior written permission.
     21    1.1       cgd  *
     22    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23    1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24    1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25    1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26    1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27    1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28    1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29    1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30    1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31    1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32    1.1       cgd  */
     33    1.1       cgd 
     34   1.18       jmc #if HAVE_NBTOOL_CONFIG_H
     35   1.18       jmc #include "nbtool_config.h"
     36   1.18       jmc #endif
     37   1.18       jmc 
     38    1.5  christos #include <sys/cdefs.h>
     39   1.10        tv #if defined(__RCSID) && !defined(lint)
     40  1.134    rillig __RCSID("$NetBSD: init.c,v 1.134 2021/03/26 20:31:07 rillig Exp $");
     41    1.1       cgd #endif
     42    1.1       cgd 
     43    1.1       cgd #include <stdlib.h>
     44   1.17   thorpej #include <string.h>
     45    1.1       cgd 
     46    1.1       cgd #include "lint1.h"
     47    1.1       cgd 
     48   1.53    rillig 
     49   1.53    rillig /*
     50   1.93    rillig  * Initialization
     51   1.93    rillig  *
     52   1.93    rillig  * Handles initializations of global or local objects, like in:
     53   1.93    rillig  *
     54   1.93    rillig  *	int number = 12345;
     55   1.93    rillig  *	int number_with_braces = { 12345 };
     56   1.93    rillig  *
     57   1.93    rillig  *	int array_of_unknown_size[] = { 111, 222, 333 };
     58   1.93    rillig  *	int array_flat[2][2] = { 11, 12, 21, 22 };
     59   1.93    rillig  *	int array_nested[2][2] = { { 11, 12 }, { 21, 22 } };
     60   1.93    rillig  *
     61   1.93    rillig  *	struct { int x, y; } point = { 3, 4 };
     62  1.119    rillig  *	struct { int x, y; } point = { .y = 4, .x = 3 };
     63   1.93    rillig  *
     64  1.119    rillig  * Any scalar expression in the initializer may be surrounded by arbitrarily
     65  1.119    rillig  * many extra pairs of braces, like in the example 'number_with_braces' (C99
     66  1.119    rillig  * 6.7.8p11).
     67  1.119    rillig  *
     68  1.119    rillig  * For multi-dimensional arrays, the inner braces may be omitted like in
     69  1.112    rillig  * array_flat or spelled out like in array_nested.
     70   1.93    rillig  *
     71   1.93    rillig  * For the initializer, the grammar parser calls these functions:
     72   1.93    rillig  *
     73  1.119    rillig  *	begin_initialization
     74  1.119    rillig  *		init_lbrace			for each '{'
     75  1.131    rillig  *		designation_add_name		for each '.member' before '='
     76  1.131    rillig  *		designation_add_subscript	for each '[123]' before '='
     77  1.119    rillig  *		init_using_expr			for each expression
     78  1.119    rillig  *		init_rbrace			for each '}'
     79  1.119    rillig  *	end_initialization
     80   1.93    rillig  *
     81   1.93    rillig  * The state of the current initialization is stored in initstk, a stack of
     82   1.94    rillig  * initstack_element, one element per type aggregate level.
     83  1.119    rillig  * XXX: Or is that "one element per brace level"?  C99 mandates in 6.7.8p17
     84  1.119    rillig  * that "each brace-enclosed initializer list has an associated current
     85  1.119    rillig  * object".
     86   1.93    rillig  *
     87   1.94    rillig  * Most of the time, the topmost level of initstk contains a scalar type, and
     88   1.94    rillig  * its remaining count toggles between 1 and 0.
     89   1.93    rillig  *
     90   1.93    rillig  * See also:
     91   1.93    rillig  *	C99 6.7.8 "Initialization"
     92   1.93    rillig  *	d_c99_init.c for more examples
     93   1.93    rillig  */
     94   1.93    rillig 
     95   1.93    rillig 
     96   1.93    rillig /*
     97   1.89    rillig  * Type of stack which is used for initialization of aggregate types.
     98   1.54    rillig  *
     99  1.119    rillig  * XXX: Since C99, the initializers can be listed in arbitrary order by using
    100  1.119    rillig  * designators to specify the sub-object to be initialized.  The member names
    101   1.54    rillig  * of non-leaf structs may thus appear repeatedly, as demonstrated in
    102   1.54    rillig  * d_init_pop_member.c.
    103   1.54    rillig  *
    104   1.54    rillig  * See C99 6.7.8, which spans 6 pages full of tricky details and carefully
    105   1.54    rillig  * selected examples.
    106   1.53    rillig  */
    107   1.79    rillig typedef	struct initstack_element {
    108   1.77    rillig 
    109   1.92    rillig 	/*
    110   1.92    rillig 	 * The type to be initialized at this level.
    111  1.102    rillig 	 *
    112  1.102    rillig 	 * On the outermost element, this is always NULL since the outermost
    113  1.102    rillig 	 * initializer-expression may be enclosed in an optional pair of
    114  1.119    rillig 	 * braces, as of the current implementation.
    115  1.119    rillig 	 *
    116  1.119    rillig 	 * FIXME: This approach is wrong.  It's not that the outermost
    117  1.119    rillig 	 * initializer may be enclosed in additional braces, it's every scalar
    118  1.119    rillig 	 * that may be enclosed in additional braces, as of C99 6.7.8p11.
    119  1.102    rillig 	 *
    120  1.102    rillig 	 * Everywhere else it is nonnull.
    121   1.92    rillig 	 */
    122   1.92    rillig 	type_t	*i_type;
    123  1.102    rillig 
    124   1.92    rillig 	/*
    125  1.102    rillig 	 * The type that will be initialized at the next initialization level,
    126  1.102    rillig 	 * usually enclosed by another pair of braces.
    127   1.92    rillig 	 *
    128  1.102    rillig 	 * For an array, it is the element type, but without 'const'.
    129  1.102    rillig 	 *
    130  1.102    rillig 	 * For a struct or union type, it is one of the member types, but
    131  1.102    rillig 	 * without 'const'.
    132  1.102    rillig 	 *
    133  1.102    rillig 	 * The outermost stack element has no i_type but nevertheless has
    134  1.102    rillig 	 * i_subt.  For example, in 'int var = { 12345 }', initially there is
    135  1.102    rillig 	 * an initstack_element with i_subt 'int'.  When the '{' is processed,
    136  1.102    rillig 	 * an element with i_type 'int' is pushed to the stack.  When the
    137   1.92    rillig 	 * corresponding '}' is processed, the inner element is popped again.
    138   1.92    rillig 	 *
    139   1.92    rillig 	 * During initialization, only the top 2 elements of the stack are
    140   1.92    rillig 	 * looked at.
    141  1.119    rillig 	 *
    142  1.119    rillig 	 * XXX: Having i_subt here is the wrong approach, it should not be
    143  1.119    rillig 	 * necessary at all; see i_type.
    144   1.92    rillig 	 */
    145   1.92    rillig 	type_t	*i_subt;
    146   1.77    rillig 
    147   1.82    rillig 	/*
    148  1.119    rillig 	 * Whether this level of the initializer requires a '}' to be
    149  1.119    rillig 	 * completed.
    150   1.82    rillig 	 *
    151   1.82    rillig 	 * Multidimensional arrays do not need a closing brace to complete
    152   1.82    rillig 	 * an inner array; for example, { 1, 2, 3, 4 } is a valid initializer
    153  1.119    rillig 	 * for 'int arr[2][2]'.
    154   1.82    rillig 	 *
    155   1.82    rillig 	 * TODO: Do structs containing structs need a closing brace?
    156   1.82    rillig 	 * TODO: Do arrays of structs need a closing brace after each struct?
    157  1.119    rillig 	 *
    158  1.119    rillig 	 * XXX: Double-check whether this is the correct approach at all; see
    159  1.119    rillig 	 * i_type.
    160   1.82    rillig 	 */
    161   1.77    rillig 	bool i_brace: 1;
    162   1.82    rillig 
    163   1.93    rillig 	/* Whether i_type is an array of unknown size. */
    164   1.77    rillig 	bool i_array_of_unknown_size: 1;
    165  1.119    rillig 
    166  1.119    rillig 	/*
    167  1.119    rillig 	 * XXX: This feels wrong.  Whether or not there has been a named
    168  1.119    rillig 	 * initializer (called 'designation' since C99) should not matter at
    169  1.119    rillig 	 * all.  Even after an initializer with designation, counting of the
    170  1.119    rillig 	 * remaining elements continues, see C99 6.7.8p17.
    171  1.119    rillig 	 */
    172   1.77    rillig 	bool i_seen_named_member: 1;
    173   1.77    rillig 
    174   1.77    rillig 	/*
    175  1.125    rillig 	 * For structs, the next member to be initialized by a designator-less
    176  1.125    rillig 	 * initializer.
    177   1.77    rillig 	 */
    178  1.125    rillig 	sym_t *i_next_member;
    179  1.125    rillig 
    180  1.125    rillig 	/* TODO: Add i_next_subscript for arrays. */
    181  1.125    rillig 
    182  1.125    rillig 	/* TODO: Understand C99 6.7.8p17 and footnote 128 for unions. */
    183   1.77    rillig 
    184   1.77    rillig 	/*
    185  1.102    rillig 	 * The number of remaining elements to be used by expressions without
    186  1.102    rillig 	 * designator.
    187  1.102    rillig 	 *
    188  1.102    rillig 	 * This says nothing about which members have been initialized or not
    189  1.102    rillig 	 * since starting with C99, members may be initialized in arbitrary
    190  1.102    rillig 	 * order by using designators.
    191   1.77    rillig 	 *
    192   1.93    rillig 	 * For an array of unknown size, this is always 0 and thus irrelevant.
    193   1.93    rillig 	 *
    194   1.77    rillig 	 * XXX: for scalars?
    195   1.77    rillig 	 * XXX: for structs?
    196   1.77    rillig 	 * XXX: for unions?
    197   1.77    rillig 	 * XXX: for arrays?
    198  1.119    rillig 	 *
    199  1.119    rillig 	 * XXX: Having the count of remaining objects should not be necessary.
    200  1.119    rillig 	 * It is probably clearer to use i_next_member and i_next_subscript
    201  1.125    rillig 	 * for this purpose.
    202   1.77    rillig 	 */
    203   1.77    rillig 	int i_remaining;
    204   1.77    rillig 
    205   1.77    rillig 	/*
    206   1.77    rillig 	 * The initialization state of the enclosing data structure
    207   1.77    rillig 	 * (struct, union, array).
    208  1.119    rillig 	 *
    209  1.119    rillig 	 * XXX: Or for a scalar, for the top-level element, or for expressions
    210  1.119    rillig 	 * in redundant braces such as '{{{{ 0 }}}}' (not yet implemented as
    211  1.119    rillig 	 * of 2021-03-25).
    212   1.77    rillig 	 */
    213   1.79    rillig 	struct initstack_element *i_enclosing;
    214  1.119    rillig 
    215   1.79    rillig } initstack_element;
    216   1.53    rillig 
    217   1.55    rillig /*
    218  1.128    rillig  * Leads the path to the sub-object to initialize using the expression.
    219  1.128    rillig  * Example designators are '.member' or '.member[123].member.member[1][1]'.
    220   1.55    rillig  *
    221  1.128    rillig  * See also: C99 6.7.8 "Initialization"
    222  1.128    rillig  */
    223  1.128    rillig typedef struct designator {
    224  1.128    rillig 	const char *name;		/* for struct and union */
    225  1.128    rillig 	/* TODO: add 'subscript' for arrays */
    226  1.128    rillig 	struct designator *next;
    227  1.128    rillig } designator;
    228  1.128    rillig 
    229  1.128    rillig /*
    230  1.128    rillig  * See also: C99 6.7.8 "Initialization"
    231   1.55    rillig  */
    232  1.128    rillig typedef struct {
    233  1.128    rillig 	designator *head;
    234  1.128    rillig 	designator *tail;
    235  1.128    rillig } designation;
    236   1.53    rillig 
    237  1.110    rillig struct initialization {
    238  1.110    rillig 	/*
    239  1.111    rillig 	 * is set as soon as a fatal error occurred in the initialization.
    240  1.111    rillig 	 * The effect is that the rest of the initialization is ignored
    241  1.111    rillig 	 * (parsed by yacc, expression trees built, but no initialization
    242  1.111    rillig 	 * takes place).
    243  1.110    rillig 	 */
    244  1.110    rillig 	bool	initerr;
    245  1.110    rillig 
    246  1.110    rillig 	/* Pointer to the symbol which is to be initialized. */
    247  1.110    rillig 	sym_t	*initsym;
    248   1.53    rillig 
    249  1.110    rillig 	/* Points to the top element of the initialization stack. */
    250  1.110    rillig 	initstack_element *initstk;
    251    1.1       cgd 
    252  1.124    rillig 	/*
    253  1.124    rillig 	 * The C99 designator, if any, for the current initialization
    254  1.124    rillig 	 * expression.
    255  1.124    rillig 	 */
    256  1.128    rillig 	designation designation;
    257    1.1       cgd 
    258  1.110    rillig 	struct initialization *next;
    259  1.110    rillig };
    260    1.1       cgd 
    261  1.111    rillig static struct initialization *init;
    262   1.12  christos 
    263    1.1       cgd 
    264  1.119    rillig /* XXX: unnecessary prototype since it is not recursive */
    265   1.82    rillig static	bool	init_array_using_string(tnode_t *);
    266   1.12  christos 
    267  1.119    rillig 
    268  1.119    rillig /* TODO: replace the following functions with current_init */
    269  1.119    rillig 
    270  1.110    rillig bool *
    271  1.110    rillig current_initerr(void)
    272  1.110    rillig {
    273  1.111    rillig 	lint_assert(init != NULL);
    274  1.111    rillig 	return &init->initerr;
    275  1.110    rillig }
    276  1.110    rillig 
    277  1.110    rillig sym_t **
    278  1.110    rillig current_initsym(void)
    279  1.110    rillig {
    280  1.111    rillig 	lint_assert(init != NULL);
    281  1.111    rillig 	return &init->initsym;
    282  1.110    rillig }
    283  1.110    rillig 
    284  1.128    rillig static designation *
    285  1.123    rillig current_designation_mod(void)
    286  1.110    rillig {
    287  1.111    rillig 	lint_assert(init != NULL);
    288  1.124    rillig 	return &init->designation;
    289  1.110    rillig }
    290  1.110    rillig 
    291  1.128    rillig static designation
    292  1.123    rillig current_designation(void)
    293  1.123    rillig {
    294  1.123    rillig 	return *current_designation_mod();
    295  1.123    rillig }
    296  1.123    rillig 
    297  1.129    rillig static const initstack_element *
    298  1.129    rillig current_initstk(void)
    299  1.129    rillig {
    300  1.129    rillig 	lint_assert(init != NULL);
    301  1.129    rillig 	return init->initstk;
    302  1.129    rillig }
    303  1.129    rillig 
    304  1.110    rillig static initstack_element **
    305  1.129    rillig current_initstk_lvalue(void)
    306  1.110    rillig {
    307  1.111    rillig 	lint_assert(init != NULL);
    308  1.111    rillig 	return &init->initstk;
    309  1.110    rillig }
    310  1.110    rillig 
    311  1.121    rillig static void
    312  1.121    rillig free_initialization(struct initialization *in)
    313  1.121    rillig {
    314  1.121    rillig 	initstack_element *el, *next;
    315  1.121    rillig 
    316  1.121    rillig 	for (el = in->initstk; el != NULL; el = next) {
    317  1.121    rillig 		next = el->i_enclosing;
    318  1.121    rillig 		free(el);
    319  1.121    rillig 	}
    320  1.121    rillig 
    321  1.121    rillig 	free(in);
    322  1.121    rillig }
    323  1.121    rillig 
    324  1.119    rillig #define initerr		(*current_initerr())
    325  1.119    rillig #define initsym		(*current_initsym())
    326  1.129    rillig #define initstk		(current_initstk())
    327  1.129    rillig #define initstk_lvalue	(*current_initstk_lvalue())
    328  1.110    rillig 
    329   1.12  christos #ifndef DEBUG
    330   1.90    rillig 
    331   1.75    rillig #define debug_printf(fmt, ...)	do { } while (false)
    332   1.75    rillig #define debug_indent()		do { } while (false)
    333   1.75    rillig #define debug_enter(a)		do { } while (false)
    334   1.75    rillig #define debug_step(fmt, ...)	do { } while (false)
    335   1.75    rillig #define debug_leave(a)		do { } while (false)
    336  1.126    rillig #define debug_designation()	do { } while (false)
    337   1.90    rillig #define debug_initstack_element(elem) do { } while (false)
    338   1.90    rillig #define debug_initstack()	do { } while (false)
    339   1.90    rillig 
    340   1.12  christos #else
    341   1.90    rillig 
    342   1.68    rillig static int debug_ind = 0;
    343   1.68    rillig 
    344   1.68    rillig static void __printflike(1, 2)
    345   1.68    rillig debug_printf(const char *fmt, ...)
    346   1.68    rillig {
    347   1.68    rillig 	va_list va;
    348   1.68    rillig 
    349   1.68    rillig 	va_start(va, fmt);
    350   1.68    rillig 	vfprintf(stdout, fmt, va);
    351   1.68    rillig 	va_end(va);
    352   1.68    rillig }
    353   1.68    rillig 
    354   1.68    rillig static void
    355   1.68    rillig debug_indent(void)
    356   1.68    rillig {
    357   1.68    rillig 	debug_printf("%*s", 2 * debug_ind, "");
    358   1.68    rillig }
    359   1.68    rillig 
    360   1.68    rillig static void
    361   1.68    rillig debug_enter(const char *func)
    362   1.68    rillig {
    363   1.68    rillig 	printf("%*s+ %s\n", 2 * debug_ind++, "", func);
    364   1.68    rillig }
    365   1.68    rillig 
    366   1.68    rillig static void __printflike(1, 2)
    367   1.68    rillig debug_step(const char *fmt, ...)
    368   1.68    rillig {
    369   1.68    rillig 	va_list va;
    370   1.68    rillig 
    371   1.68    rillig 	printf("%*s", 2 * debug_ind, "");
    372   1.68    rillig 	va_start(va, fmt);
    373   1.68    rillig 	vfprintf(stdout, fmt, va);
    374   1.68    rillig 	va_end(va);
    375   1.68    rillig 	printf("\n");
    376   1.68    rillig }
    377   1.68    rillig 
    378   1.68    rillig static void
    379   1.68    rillig debug_leave(const char *func)
    380   1.68    rillig {
    381   1.68    rillig 	printf("%*s- %s\n", 2 * --debug_ind, "", func);
    382   1.68    rillig }
    383   1.68    rillig 
    384   1.55    rillig static void
    385  1.126    rillig debug_designation(void)
    386   1.55    rillig {
    387  1.128    rillig 	const designator *head = current_designation().head, *p;
    388  1.122    rillig 	if (head == NULL)
    389   1.55    rillig 		return;
    390  1.126    rillig 
    391   1.68    rillig 	debug_indent();
    392  1.126    rillig 	debug_printf("designation: ");
    393  1.128    rillig 	for (p = head; p != NULL; p = p->next)
    394  1.128    rillig 		debug_printf(".%s", p->name);
    395   1.68    rillig 	debug_printf("\n");
    396   1.55    rillig }
    397    1.1       cgd 
    398  1.119    rillig /*
    399  1.119    rillig  * TODO: try whether a single-line output is more readable
    400  1.119    rillig  *
    401  1.119    rillig  * TODO: only log the top of the stack after each modifying operation
    402  1.119    rillig  *
    403  1.119    rillig  * TODO: wrap all write accesses to initstack_element in setter functions
    404  1.119    rillig  */
    405   1.78    rillig static void
    406   1.79    rillig debug_initstack_element(const initstack_element *elem)
    407   1.74    rillig {
    408   1.78    rillig 	if (elem->i_type != NULL)
    409  1.120    rillig 		debug_printf("type '%s'", type_name(elem->i_type));
    410  1.120    rillig 	if (elem->i_type != NULL && elem->i_subt != NULL)
    411  1.120    rillig 		debug_printf(", ");
    412   1.78    rillig 	if (elem->i_subt != NULL)
    413  1.120    rillig 		debug_printf("subtype '%s'", type_name(elem->i_subt));
    414   1.78    rillig 
    415   1.78    rillig 	if (elem->i_brace)
    416  1.120    rillig 		debug_printf(", needs closing brace");
    417   1.78    rillig 	if (elem->i_array_of_unknown_size)
    418  1.120    rillig 		debug_printf(", array of unknown size");
    419   1.78    rillig 	if (elem->i_seen_named_member)
    420  1.120    rillig 		debug_printf(", seen named member");
    421   1.78    rillig 
    422   1.78    rillig 	const type_t *eff_type = elem->i_type != NULL
    423   1.78    rillig 	    ? elem->i_type : elem->i_subt;
    424  1.125    rillig 	if (eff_type->t_tspec == STRUCT && elem->i_next_member != NULL)
    425  1.125    rillig 		debug_printf(", next member '%s'",
    426  1.125    rillig 		    elem->i_next_member->s_name);
    427   1.78    rillig 
    428  1.120    rillig 	debug_printf(", remaining %d\n", elem->i_remaining);
    429   1.74    rillig }
    430   1.74    rillig 
    431  1.119    rillig /*
    432  1.119    rillig  * TODO: only call debug_initstack after each push/pop.
    433  1.119    rillig  */
    434   1.73    rillig static void
    435   1.73    rillig debug_initstack(void)
    436   1.73    rillig {
    437   1.73    rillig 	if (initstk == NULL) {
    438   1.73    rillig 		debug_step("initstk is empty");
    439   1.73    rillig 		return;
    440   1.73    rillig 	}
    441   1.73    rillig 
    442   1.73    rillig 	size_t i = 0;
    443   1.79    rillig 	for (const initstack_element *elem = initstk;
    444   1.77    rillig 	     elem != NULL; elem = elem->i_enclosing) {
    445  1.120    rillig 		debug_indent();
    446  1.120    rillig 		debug_printf("initstk[%zu]: ", i);
    447   1.78    rillig 		debug_initstack_element(elem);
    448   1.73    rillig 		i++;
    449   1.73    rillig 	}
    450   1.73    rillig }
    451   1.90    rillig 
    452   1.90    rillig #define debug_enter() debug_enter(__func__)
    453   1.90    rillig #define debug_leave() debug_leave(__func__)
    454   1.90    rillig 
    455   1.73    rillig #endif
    456   1.73    rillig 
    457  1.111    rillig 
    458  1.111    rillig void
    459  1.111    rillig begin_initialization(sym_t *sym)
    460  1.111    rillig {
    461  1.111    rillig 	struct initialization *curr_init;
    462  1.111    rillig 
    463  1.118    rillig 	debug_step("begin initialization of '%s'", type_name(sym->s_type));
    464  1.111    rillig 	curr_init = xcalloc(1, sizeof *curr_init);
    465  1.111    rillig 	curr_init->next = init;
    466  1.111    rillig 	init = curr_init;
    467  1.111    rillig 	initsym = sym;
    468  1.111    rillig }
    469  1.111    rillig 
    470  1.111    rillig void
    471  1.111    rillig end_initialization(void)
    472  1.111    rillig {
    473  1.111    rillig 	struct initialization *curr_init;
    474  1.111    rillig 
    475  1.111    rillig 	curr_init = init;
    476  1.111    rillig 	init = init->next;
    477  1.121    rillig 	free_initialization(curr_init);
    478  1.111    rillig 	debug_step("end initialization");
    479  1.111    rillig }
    480  1.111    rillig 
    481   1.90    rillig void
    482  1.131    rillig designation_add_name(sbuf_t *sb)
    483   1.90    rillig {
    484  1.128    rillig 	designation *dd = current_designation_mod();
    485  1.127    rillig 
    486  1.128    rillig 	designator *d = xcalloc(1, sizeof *d);
    487  1.128    rillig 	d->name = sb->sb_name;
    488   1.90    rillig 
    489  1.128    rillig 	if (dd->head != NULL) {
    490  1.128    rillig 		dd->tail->next = d;
    491  1.128    rillig 		dd->tail = d;
    492   1.90    rillig 	} else {
    493  1.128    rillig 		dd->head = d;
    494  1.128    rillig 		dd->tail = d;
    495   1.90    rillig 	}
    496  1.106    rillig 
    497  1.126    rillig 	debug_designation();
    498   1.90    rillig }
    499   1.90    rillig 
    500  1.133    rillig /* TODO: Move the function body up here, to avoid the forward declaration. */
    501  1.133    rillig static void initstack_pop_nobrace(void);
    502  1.133    rillig 
    503   1.91    rillig /*
    504  1.119    rillig  * A sub-object of an array is initialized using a designator.  This does not
    505  1.119    rillig  * have to be an array element directly, it can also be used to initialize
    506  1.119    rillig  * only a sub-object of the array element.
    507   1.91    rillig  *
    508   1.91    rillig  * C99 example: struct { int member[4]; } var = { [2] = 12345 };
    509   1.91    rillig  *
    510   1.91    rillig  * GNU example: struct { int member[4]; } var = { [1 ... 3] = 12345 };
    511  1.119    rillig  *
    512  1.119    rillig  * TODO: test the following initialization with an outer and an inner type:
    513  1.119    rillig  *
    514  1.119    rillig  * .deeply[0].nested = {
    515  1.119    rillig  *	.deeply[1].nested = {
    516  1.119    rillig  *		12345,
    517  1.119    rillig  *	},
    518  1.119    rillig  * }
    519   1.91    rillig  */
    520   1.91    rillig void
    521  1.131    rillig designation_add_subscript(range_t range)
    522   1.91    rillig {
    523  1.132    rillig 	initstack_element *istk;
    524  1.132    rillig 
    525   1.91    rillig 	debug_enter();
    526   1.91    rillig 	debug_step("subscript range is %zu ... %zu", range.lo, range.hi);
    527   1.91    rillig 	debug_initstack();
    528  1.132    rillig 
    529  1.133    rillig 	initstack_pop_nobrace();
    530  1.133    rillig 
    531  1.132    rillig 	istk = initstk_lvalue;
    532  1.132    rillig 	if (istk->i_array_of_unknown_size) {
    533  1.133    rillig 		/* No +1 here, extend_if_array_of_unknown_size will add it. */
    534  1.133    rillig 		int auto_dim = (int)range.hi;
    535  1.132    rillig 		if (auto_dim > istk->i_type->t_dim) {
    536  1.132    rillig 			debug_step("setting the array size to %d", auto_dim);
    537  1.132    rillig 			istk->i_type->t_dim = auto_dim;
    538  1.132    rillig 		}
    539  1.132    rillig 	}
    540  1.132    rillig 
    541   1.91    rillig 	debug_leave();
    542   1.91    rillig }
    543   1.91    rillig 
    544  1.119    rillig /* TODO: add support for array subscripts, not only named members */
    545   1.90    rillig static void
    546  1.131    rillig designation_shift_level(void)
    547   1.90    rillig {
    548  1.127    rillig 	/* TODO: remove direct access to 'init' */
    549  1.127    rillig 	lint_assert(init != NULL);
    550  1.128    rillig 	lint_assert(init->designation.head != NULL);
    551  1.128    rillig 	if (init->designation.head == init->designation.tail) {
    552  1.128    rillig 		free(init->designation.head);
    553  1.128    rillig 		init->designation.head = NULL;
    554  1.128    rillig 		init->designation.tail = NULL;
    555   1.90    rillig 	} else {
    556  1.128    rillig 		designator *head = init->designation.head;
    557  1.128    rillig 		init->designation.head = init->designation.head->next;
    558  1.126    rillig 		free(head);
    559   1.90    rillig 	}
    560  1.106    rillig 
    561  1.126    rillig 	debug_designation();
    562   1.90    rillig }
    563   1.90    rillig 
    564    1.1       cgd /*
    565   1.89    rillig  * Initialize the initialization stack by putting an entry for the object
    566    1.1       cgd  * which is to be initialized on it.
    567  1.119    rillig  *
    568  1.119    rillig  * TODO: merge into begin_initialization
    569    1.1       cgd  */
    570    1.1       cgd void
    571   1.35    rillig initstack_init(void)
    572    1.1       cgd {
    573   1.79    rillig 	initstack_element *istk;
    574    1.1       cgd 
    575    1.1       cgd 	if (initerr)
    576    1.1       cgd 		return;
    577    1.1       cgd 
    578   1.70    rillig 	debug_enter();
    579   1.38    rillig 
    580    1.1       cgd 	/*
    581   1.77    rillig 	 * If the type which is to be initialized is an incomplete array,
    582    1.1       cgd 	 * it must be duplicated.
    583    1.1       cgd 	 */
    584   1.65    rillig 	if (initsym->s_type->t_tspec == ARRAY && is_incomplete(initsym->s_type))
    585    1.1       cgd 		initsym->s_type = duptyp(initsym->s_type);
    586  1.119    rillig 	/* TODO: does 'duptyp' create a memory leak? */
    587    1.1       cgd 
    588  1.134    rillig 	istk = initstk_lvalue = xcalloc(1, sizeof *initstk_lvalue);
    589    1.1       cgd 	istk->i_subt = initsym->s_type;
    590   1.43    rillig 	istk->i_remaining = 1;
    591   1.70    rillig 
    592   1.73    rillig 	debug_initstack();
    593   1.70    rillig 	debug_leave();
    594    1.1       cgd }
    595    1.1       cgd 
    596  1.119    rillig /* TODO: document me */
    597    1.1       cgd static void
    598  1.101    rillig initstack_pop_item_named_member(void)
    599    1.1       cgd {
    600  1.129    rillig 	initstack_element *istk = initstk_lvalue;
    601  1.101    rillig 	sym_t *m;
    602    1.1       cgd 
    603  1.119    rillig 	/*
    604  1.119    rillig 	 * TODO: fix wording of the debug message; this doesn't seem to be
    605  1.119    rillig 	 * related to initializing the named member.
    606  1.119    rillig 	 */
    607  1.122    rillig 	debug_step("initializing named member '%s'",
    608  1.128    rillig 	    current_designation().head->name);
    609   1.68    rillig 
    610  1.104    rillig 	if (istk->i_type->t_tspec != STRUCT &&
    611  1.104    rillig 	    istk->i_type->t_tspec != UNION) {
    612  1.104    rillig 		/* syntax error '%s' */
    613  1.104    rillig 		error(249, "named member must only be used with struct/union");
    614  1.104    rillig 		initerr = true;
    615  1.104    rillig 		return;
    616  1.104    rillig 	}
    617  1.104    rillig 
    618  1.101    rillig 	for (m = istk->i_type->t_str->sou_first_member;
    619  1.101    rillig 	     m != NULL; m = m->s_next) {
    620   1.40    rillig 
    621  1.101    rillig 		if (m->s_bitfield && m->s_name == unnamed)
    622  1.101    rillig 			continue;
    623   1.26  christos 
    624  1.128    rillig 		if (strcmp(m->s_name, current_designation().head->name) == 0) {
    625  1.101    rillig 			debug_step("found matching member");
    626  1.101    rillig 			istk->i_subt = m->s_type;
    627  1.101    rillig 			/* XXX: why ++? */
    628  1.101    rillig 			istk->i_remaining++;
    629  1.101    rillig 			/* XXX: why is i_seen_named_member not set? */
    630  1.131    rillig 			designation_shift_level();
    631  1.101    rillig 			return;
    632  1.101    rillig 		}
    633  1.101    rillig 	}
    634   1.40    rillig 
    635  1.101    rillig 	/* undefined struct/union member: %s */
    636  1.128    rillig 	error(101, current_designation().head->name);
    637   1.38    rillig 
    638  1.131    rillig 	designation_shift_level();
    639  1.101    rillig 	istk->i_seen_named_member = true;
    640  1.101    rillig }
    641   1.80    rillig 
    642  1.119    rillig /* TODO: think of a better name than 'pop' */
    643  1.101    rillig static void
    644  1.101    rillig initstack_pop_item_unnamed(void)
    645  1.101    rillig {
    646  1.129    rillig 	initstack_element *istk = initstk_lvalue;
    647  1.101    rillig 	sym_t *m;
    648   1.80    rillig 
    649    1.1       cgd 	/*
    650    1.1       cgd 	 * If the removed element was a structure member, we must go
    651    1.1       cgd 	 * to the next structure member.
    652    1.1       cgd 	 */
    653   1.43    rillig 	if (istk->i_remaining > 0 && istk->i_type->t_tspec == STRUCT &&
    654   1.77    rillig 	    !istk->i_seen_named_member) {
    655    1.1       cgd 		do {
    656  1.125    rillig 			m = istk->i_next_member =
    657  1.125    rillig 			    istk->i_next_member->s_next;
    658   1.80    rillig 			/* XXX: can this assertion be made to fail? */
    659   1.44    rillig 			lint_assert(m != NULL);
    660   1.68    rillig 			debug_step("pop %s", m->s_name);
    661   1.48    rillig 		} while (m->s_bitfield && m->s_name == unnamed);
    662   1.80    rillig 		/* XXX: duplicate code for skipping unnamed bit-fields */
    663    1.1       cgd 		istk->i_subt = m->s_type;
    664    1.1       cgd 	}
    665  1.101    rillig }
    666  1.101    rillig 
    667  1.119    rillig /* TODO: think of a better name than 'pop' */
    668  1.101    rillig static void
    669  1.101    rillig initstack_pop_item(void)
    670  1.101    rillig {
    671  1.101    rillig 	initstack_element *istk;
    672  1.101    rillig 
    673  1.101    rillig 	debug_enter();
    674  1.101    rillig 
    675  1.129    rillig 	istk = initstk_lvalue;
    676  1.120    rillig 	debug_indent();
    677  1.120    rillig 	debug_printf("popping: ");
    678  1.101    rillig 	debug_initstack_element(istk);
    679  1.101    rillig 
    680  1.129    rillig 	initstk_lvalue = istk->i_enclosing;
    681  1.101    rillig 	free(istk);
    682  1.129    rillig 	istk = initstk_lvalue;
    683  1.101    rillig 	lint_assert(istk != NULL);
    684  1.101    rillig 
    685  1.101    rillig 	istk->i_remaining--;
    686  1.101    rillig 	lint_assert(istk->i_remaining >= 0);
    687  1.101    rillig 	debug_step("%d elements remaining", istk->i_remaining);
    688  1.101    rillig 
    689  1.128    rillig 	if (current_designation().head != NULL)
    690  1.101    rillig 		initstack_pop_item_named_member();
    691  1.101    rillig 	else
    692  1.101    rillig 		initstack_pop_item_unnamed();
    693  1.101    rillig 
    694   1.73    rillig 	debug_initstack();
    695   1.68    rillig 	debug_leave();
    696    1.1       cgd }
    697    1.1       cgd 
    698   1.36    rillig /*
    699   1.36    rillig  * Take all entries, including the first which requires a closing brace,
    700   1.36    rillig  * from the stack.
    701   1.36    rillig  */
    702   1.36    rillig static void
    703   1.36    rillig initstack_pop_brace(void)
    704   1.36    rillig {
    705   1.62    rillig 	bool brace;
    706   1.36    rillig 
    707   1.68    rillig 	debug_enter();
    708   1.73    rillig 	debug_initstack();
    709   1.36    rillig 	do {
    710   1.36    rillig 		brace = initstk->i_brace;
    711  1.119    rillig 		/* TODO: improve wording of the debug message */
    712   1.68    rillig 		debug_step("loop brace=%d", brace);
    713   1.36    rillig 		initstack_pop_item();
    714   1.36    rillig 	} while (!brace);
    715   1.73    rillig 	debug_initstack();
    716   1.68    rillig 	debug_leave();
    717   1.36    rillig }
    718   1.36    rillig 
    719   1.36    rillig /*
    720   1.36    rillig  * Take all entries which cannot be used for further initializers from the
    721   1.36    rillig  * stack, but do this only if they do not require a closing brace.
    722   1.36    rillig  */
    723  1.119    rillig /* TODO: think of a better name than 'pop' */
    724    1.1       cgd static void
    725   1.36    rillig initstack_pop_nobrace(void)
    726    1.1       cgd {
    727    1.7     lukem 
    728   1.68    rillig 	debug_enter();
    729   1.43    rillig 	while (!initstk->i_brace && initstk->i_remaining == 0 &&
    730   1.77    rillig 	       !initstk->i_array_of_unknown_size)
    731   1.36    rillig 		initstack_pop_item();
    732   1.68    rillig 	debug_leave();
    733    1.1       cgd }
    734    1.1       cgd 
    735   1.97    rillig /* Extend an array of unknown size by one element */
    736   1.97    rillig static void
    737   1.97    rillig extend_if_array_of_unknown_size(void)
    738   1.97    rillig {
    739  1.129    rillig 	initstack_element *istk = initstk_lvalue;
    740   1.97    rillig 
    741   1.97    rillig 	if (istk->i_remaining != 0)
    742   1.97    rillig 		return;
    743  1.133    rillig 	/*
    744  1.133    rillig 	 * XXX: According to the function name, there should be a 'return' if
    745  1.133    rillig 	 * i_array_of_unknown_size is false.  There's probably a test missing
    746  1.133    rillig 	 * for that case.
    747  1.133    rillig 	 */
    748   1.97    rillig 
    749   1.97    rillig 	/*
    750   1.97    rillig 	 * The only place where an incomplete array may appear is at the
    751   1.97    rillig 	 * outermost aggregate level of the object to be initialized.
    752   1.97    rillig 	 */
    753   1.97    rillig 	lint_assert(istk->i_enclosing->i_enclosing == NULL);
    754   1.97    rillig 	lint_assert(istk->i_type->t_tspec == ARRAY);
    755   1.97    rillig 
    756   1.97    rillig 	debug_step("extending array of unknown size '%s'",
    757   1.97    rillig 	    type_name(istk->i_type));
    758   1.97    rillig 	istk->i_remaining = 1;
    759   1.97    rillig 	istk->i_type->t_dim++;
    760   1.97    rillig 	setcomplete(istk->i_type, true);
    761   1.97    rillig 
    762   1.97    rillig 	debug_step("extended type is '%s'", type_name(istk->i_type));
    763   1.97    rillig }
    764   1.97    rillig 
    765  1.119    rillig /* TODO: document me */
    766  1.119    rillig /* TODO: think of a better name than 'push' */
    767    1.1       cgd static void
    768   1.99    rillig initstack_push_array(void)
    769   1.99    rillig {
    770  1.129    rillig 	initstack_element *istk = initstk_lvalue;
    771   1.99    rillig 
    772   1.99    rillig 	if (istk->i_enclosing->i_seen_named_member) {
    773   1.99    rillig 		istk->i_brace = true;
    774  1.124    rillig 		debug_step("ARRAY%s%s",
    775  1.124    rillig 		    istk->i_brace ? ", needs closing brace" : "",
    776  1.124    rillig 		    istk->i_enclosing->i_seen_named_member
    777  1.124    rillig 			? ", seen named member" : "");
    778   1.99    rillig 	}
    779   1.99    rillig 
    780   1.99    rillig 	if (is_incomplete(istk->i_type) &&
    781   1.99    rillig 	    istk->i_enclosing->i_enclosing != NULL) {
    782   1.99    rillig 		/* initialization of an incomplete type */
    783   1.99    rillig 		error(175);
    784   1.99    rillig 		initerr = true;
    785   1.99    rillig 		return;
    786   1.99    rillig 	}
    787   1.99    rillig 
    788   1.99    rillig 	istk->i_subt = istk->i_type->t_subt;
    789   1.99    rillig 	istk->i_array_of_unknown_size = is_incomplete(istk->i_type);
    790   1.99    rillig 	istk->i_remaining = istk->i_type->t_dim;
    791  1.126    rillig 	debug_designation();
    792   1.99    rillig 	debug_step("type '%s' remaining %d",
    793   1.99    rillig 	    type_name(istk->i_type), istk->i_remaining);
    794   1.99    rillig }
    795   1.99    rillig 
    796  1.119    rillig /* TODO: document me */
    797  1.119    rillig /* TODO: think of a better name than 'push' */
    798   1.99    rillig static bool
    799   1.99    rillig initstack_push_struct_or_union(void)
    800   1.99    rillig {
    801  1.119    rillig 	/*
    802  1.119    rillig 	 * TODO: remove unnecessary 'const' for variables in functions that
    803  1.119    rillig 	 * fit on a single screen.  Keep it for larger functions.
    804  1.119    rillig 	 */
    805  1.129    rillig 	initstack_element *istk = initstk_lvalue;
    806   1.99    rillig 	int cnt;
    807   1.99    rillig 	sym_t *m;
    808   1.99    rillig 
    809   1.99    rillig 	if (is_incomplete(istk->i_type)) {
    810   1.99    rillig 		/* initialization of an incomplete type */
    811   1.99    rillig 		error(175);
    812   1.99    rillig 		initerr = true;
    813   1.99    rillig 		return false;
    814   1.99    rillig 	}
    815  1.100    rillig 
    816   1.99    rillig 	cnt = 0;
    817  1.126    rillig 	debug_designation();
    818   1.99    rillig 	debug_step("lookup for '%s'%s",
    819   1.99    rillig 	    type_name(istk->i_type),
    820   1.99    rillig 	    istk->i_seen_named_member ? ", seen named member" : "");
    821  1.100    rillig 
    822   1.99    rillig 	for (m = istk->i_type->t_str->sou_first_member;
    823   1.99    rillig 	     m != NULL; m = m->s_next) {
    824   1.99    rillig 		if (m->s_bitfield && m->s_name == unnamed)
    825   1.99    rillig 			continue;
    826  1.119    rillig 		/*
    827  1.119    rillig 		 * TODO: split into separate functions:
    828  1.119    rillig 		 *
    829  1.119    rillig 		 * look_up_array_next
    830  1.119    rillig 		 * look_up_array_designator
    831  1.119    rillig 		 * look_up_struct_next
    832  1.119    rillig 		 * look_up_struct_designator
    833  1.119    rillig 		 */
    834  1.128    rillig 		if (current_designation().head != NULL) {
    835  1.119    rillig 			/* XXX: this log entry looks unnecessarily verbose */
    836  1.100    rillig 			debug_step("have member '%s', want member '%s'",
    837  1.128    rillig 			    m->s_name, current_designation().head->name);
    838  1.128    rillig 			if (strcmp(m->s_name,
    839  1.128    rillig 			    current_designation().head->name) == 0) {
    840   1.99    rillig 				cnt++;
    841   1.99    rillig 				break;
    842   1.99    rillig 			} else
    843   1.99    rillig 				continue;
    844   1.99    rillig 		}
    845   1.99    rillig 		if (++cnt == 1) {
    846  1.125    rillig 			istk->i_next_member = m;
    847   1.99    rillig 			istk->i_subt = m->s_type;
    848   1.99    rillig 		}
    849   1.99    rillig 	}
    850  1.100    rillig 
    851  1.128    rillig 	if (current_designation().head != NULL) {
    852   1.99    rillig 		if (m == NULL) {
    853   1.99    rillig 			debug_step("pop struct");
    854   1.99    rillig 			return true;
    855   1.99    rillig 		}
    856  1.125    rillig 		istk->i_next_member = m;
    857   1.99    rillig 		istk->i_subt = m->s_type;
    858   1.99    rillig 		istk->i_seen_named_member = true;
    859  1.128    rillig 		debug_step("named member '%s'",
    860  1.128    rillig 		    current_designation().head->name);
    861  1.131    rillig 		designation_shift_level();
    862   1.99    rillig 		cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
    863   1.99    rillig 	}
    864   1.99    rillig 	istk->i_brace = true;
    865   1.99    rillig 	debug_step("unnamed element with type '%s'%s",
    866   1.99    rillig 	    type_name(istk->i_type != NULL ? istk->i_type : istk->i_subt),
    867   1.99    rillig 	    istk->i_brace ? ", needs closing brace" : "");
    868   1.99    rillig 	if (cnt == 0) {
    869   1.99    rillig 		/* cannot init. struct/union with no named member */
    870   1.99    rillig 		error(179);
    871   1.99    rillig 		initerr = true;
    872   1.99    rillig 		return false;
    873   1.99    rillig 	}
    874   1.99    rillig 	istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
    875   1.99    rillig 	return false;
    876   1.99    rillig }
    877   1.99    rillig 
    878  1.119    rillig /* TODO: document me */
    879  1.119    rillig /* TODO: think of a better name than 'push' */
    880   1.99    rillig static void
    881   1.35    rillig initstack_push(void)
    882    1.1       cgd {
    883   1.79    rillig 	initstack_element *istk, *inxt;
    884    1.1       cgd 
    885   1.68    rillig 	debug_enter();
    886   1.68    rillig 
    887   1.97    rillig 	extend_if_array_of_unknown_size();
    888   1.97    rillig 
    889  1.129    rillig 	istk = initstk_lvalue;
    890   1.44    rillig 	lint_assert(istk->i_remaining > 0);
    891   1.61    rillig 	lint_assert(istk->i_type == NULL || !is_scalar(istk->i_type->t_tspec));
    892    1.1       cgd 
    893  1.134    rillig 	initstk_lvalue = xcalloc(1, sizeof *initstk_lvalue);
    894  1.129    rillig 	initstk_lvalue->i_enclosing = istk;
    895  1.129    rillig 	initstk_lvalue->i_type = istk->i_subt;
    896  1.129    rillig 	lint_assert(initstk_lvalue->i_type->t_tspec != FUNC);
    897    1.1       cgd 
    898   1.12  christos again:
    899  1.129    rillig 	istk = initstk_lvalue;
    900    1.1       cgd 
    901   1.88    rillig 	debug_step("expecting type '%s'", type_name(istk->i_type));
    902  1.107    rillig 	lint_assert(istk->i_type != NULL);
    903    1.1       cgd 	switch (istk->i_type->t_tspec) {
    904    1.1       cgd 	case ARRAY:
    905  1.128    rillig 		if (current_designation().head != NULL) {
    906  1.124    rillig 			debug_step("pop array, named member '%s'%s",
    907  1.128    rillig 			    current_designation().head->name,
    908  1.124    rillig 			    istk->i_brace ? ", needs closing brace" : "");
    909   1.19  christos 			goto pop;
    910   1.98    rillig 		}
    911   1.98    rillig 
    912   1.99    rillig 		initstack_push_array();
    913   1.99    rillig 		break;
    914   1.20  christos 
    915    1.1       cgd 	case UNION:
    916    1.1       cgd 		if (tflag)
    917   1.89    rillig 			/* initialization of union is illegal in trad. C */
    918    1.1       cgd 			warning(238);
    919    1.1       cgd 		/* FALLTHROUGH */
    920    1.1       cgd 	case STRUCT:
    921   1.99    rillig 		if (initstack_push_struct_or_union())
    922   1.99    rillig 			goto pop;
    923    1.1       cgd 		break;
    924    1.1       cgd 	default:
    925  1.128    rillig 		if (current_designation().head != NULL) {
    926  1.100    rillig 			debug_step("pop scalar");
    927   1.19  christos 	pop:
    928  1.119    rillig 			/* TODO: extract this into end_initializer_level */
    929   1.77    rillig 			inxt = initstk->i_enclosing;
    930   1.12  christos 			free(istk);
    931  1.129    rillig 			initstk_lvalue = inxt;
    932   1.12  christos 			goto again;
    933   1.12  christos 		}
    934  1.100    rillig 		/* The initialization stack now expects a single scalar. */
    935   1.43    rillig 		istk->i_remaining = 1;
    936    1.1       cgd 		break;
    937    1.1       cgd 	}
    938   1.68    rillig 
    939   1.73    rillig 	debug_initstack();
    940   1.68    rillig 	debug_leave();
    941    1.1       cgd }
    942    1.1       cgd 
    943    1.1       cgd static void
    944   1.84    rillig check_too_many_initializers(void)
    945    1.1       cgd {
    946    1.1       cgd 
    947   1.84    rillig 	const initstack_element *istk = initstk;
    948   1.84    rillig 	if (istk->i_remaining > 0)
    949   1.84    rillig 		return;
    950  1.119    rillig 	/*
    951  1.119    rillig 	 * FIXME: even with named members, there can be too many initializers
    952  1.119    rillig 	 */
    953   1.84    rillig 	if (istk->i_array_of_unknown_size || istk->i_seen_named_member)
    954   1.84    rillig 		return;
    955    1.1       cgd 
    956   1.84    rillig 	tspec_t t = istk->i_type->t_tspec;
    957   1.84    rillig 	if (t == ARRAY) {
    958   1.84    rillig 		/* too many array initializers, expected %d */
    959   1.84    rillig 		error(173, istk->i_type->t_dim);
    960   1.84    rillig 	} else if (t == STRUCT || t == UNION) {
    961   1.84    rillig 		/* too many struct/union initializers */
    962   1.84    rillig 		error(172);
    963   1.84    rillig 	} else {
    964   1.84    rillig 		/* too many initializers */
    965   1.84    rillig 		error(174);
    966    1.1       cgd 	}
    967   1.84    rillig 	initerr = true;
    968    1.1       cgd }
    969    1.1       cgd 
    970   1.92    rillig /*
    971   1.92    rillig  * Process a '{' in an initializer by starting the initialization of the
    972   1.92    rillig  * nested data structure, with i_type being the i_subt of the outer
    973   1.92    rillig  * initialization level.
    974   1.92    rillig  */
    975    1.1       cgd static void
    976   1.37    rillig initstack_next_brace(void)
    977    1.1       cgd {
    978    1.7     lukem 
    979   1.68    rillig 	debug_enter();
    980   1.73    rillig 	debug_initstack();
    981   1.68    rillig 
    982   1.61    rillig 	if (initstk->i_type != NULL && is_scalar(initstk->i_type->t_tspec)) {
    983   1.49    rillig 		/* invalid initializer type %s */
    984   1.57    rillig 		error(176, type_name(initstk->i_type));
    985   1.62    rillig 		initerr = true;
    986   1.37    rillig 	}
    987   1.37    rillig 	if (!initerr)
    988   1.84    rillig 		check_too_many_initializers();
    989   1.37    rillig 	if (!initerr)
    990   1.37    rillig 		initstack_push();
    991   1.37    rillig 	if (!initerr) {
    992  1.129    rillig 		initstk_lvalue->i_brace = true;
    993  1.126    rillig 		debug_designation();
    994   1.92    rillig 		debug_step("expecting type '%s'",
    995   1.92    rillig 		    type_name(initstk->i_type != NULL ? initstk->i_type
    996   1.74    rillig 			: initstk->i_subt));
    997   1.37    rillig 	}
    998   1.68    rillig 
    999   1.73    rillig 	debug_initstack();
   1000   1.68    rillig 	debug_leave();
   1001   1.37    rillig }
   1002   1.37    rillig 
   1003  1.119    rillig /* TODO: document me, or think of a better name */
   1004   1.37    rillig static void
   1005  1.118    rillig initstack_next_nobrace(tnode_t *tn)
   1006   1.37    rillig {
   1007   1.68    rillig 	debug_enter();
   1008   1.37    rillig 
   1009   1.61    rillig 	if (initstk->i_type == NULL && !is_scalar(initstk->i_subt->t_tspec)) {
   1010   1.37    rillig 		/* {}-enclosed initializer required */
   1011   1.37    rillig 		error(181);
   1012   1.92    rillig 		/* XXX: maybe set initerr here */
   1013   1.37    rillig 	}
   1014   1.37    rillig 
   1015   1.84    rillig 	if (!initerr)
   1016   1.84    rillig 		check_too_many_initializers();
   1017   1.84    rillig 
   1018   1.42    rillig 	while (!initerr) {
   1019  1.129    rillig 		initstack_element *istk = initstk_lvalue;
   1020  1.118    rillig 
   1021  1.118    rillig 		if (tn->tn_type->t_tspec == STRUCT &&
   1022  1.118    rillig 		    istk->i_type == tn->tn_type &&
   1023  1.118    rillig 		    istk->i_enclosing != NULL &&
   1024  1.118    rillig 		    istk->i_enclosing->i_enclosing != NULL) {
   1025  1.118    rillig 			istk->i_brace = false;
   1026  1.118    rillig 			istk->i_remaining = 1; /* the struct itself */
   1027  1.118    rillig 			break;
   1028  1.118    rillig 		}
   1029  1.118    rillig 
   1030  1.118    rillig 		if ((istk->i_type != NULL && is_scalar(istk->i_type->t_tspec)))
   1031   1.42    rillig 			break;
   1032   1.42    rillig 		initstack_push();
   1033    1.1       cgd 	}
   1034   1.68    rillig 
   1035   1.73    rillig 	debug_initstack();
   1036   1.68    rillig 	debug_leave();
   1037    1.1       cgd }
   1038    1.1       cgd 
   1039  1.119    rillig /* TODO: document me */
   1040    1.1       cgd void
   1041   1.34    rillig init_lbrace(void)
   1042    1.1       cgd {
   1043    1.1       cgd 	if (initerr)
   1044    1.1       cgd 		return;
   1045    1.1       cgd 
   1046   1.68    rillig 	debug_enter();
   1047   1.73    rillig 	debug_initstack();
   1048   1.68    rillig 
   1049    1.1       cgd 	if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
   1050   1.77    rillig 	    initstk->i_enclosing == NULL) {
   1051   1.61    rillig 		if (tflag && !is_scalar(initstk->i_subt->t_tspec))
   1052   1.50    rillig 			/* no automatic aggregate initialization in trad. C */
   1053    1.1       cgd 			warning(188);
   1054    1.1       cgd 	}
   1055    1.1       cgd 
   1056    1.1       cgd 	/*
   1057    1.1       cgd 	 * Remove all entries which cannot be used for further initializers
   1058    1.1       cgd 	 * and do not expect a closing brace.
   1059    1.1       cgd 	 */
   1060   1.36    rillig 	initstack_pop_nobrace();
   1061    1.1       cgd 
   1062   1.37    rillig 	initstack_next_brace();
   1063   1.68    rillig 
   1064   1.73    rillig 	debug_initstack();
   1065   1.68    rillig 	debug_leave();
   1066    1.1       cgd }
   1067    1.1       cgd 
   1068   1.92    rillig /*
   1069   1.92    rillig  * Process a '}' in an initializer by finishing the current level of the
   1070   1.92    rillig  * initialization stack.
   1071   1.92    rillig  */
   1072    1.1       cgd void
   1073   1.34    rillig init_rbrace(void)
   1074    1.1       cgd {
   1075    1.1       cgd 	if (initerr)
   1076    1.1       cgd 		return;
   1077    1.1       cgd 
   1078   1.68    rillig 	debug_enter();
   1079   1.36    rillig 	initstack_pop_brace();
   1080   1.68    rillig 	debug_leave();
   1081    1.1       cgd }
   1082    1.1       cgd 
   1083   1.86    rillig /* In traditional C, bit-fields can be initialized only by integer constants. */
   1084   1.86    rillig static void
   1085   1.86    rillig check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
   1086   1.86    rillig {
   1087   1.86    rillig 	if (tflag &&
   1088   1.86    rillig 	    is_integer(lt) &&
   1089   1.86    rillig 	    ln->tn_type->t_bitfield &&
   1090   1.86    rillig 	    !is_integer(rt)) {
   1091   1.89    rillig 		/* bit-field initialization is illegal in traditional C */
   1092   1.86    rillig 		warning(186);
   1093   1.86    rillig 	}
   1094   1.86    rillig }
   1095   1.86    rillig 
   1096   1.87    rillig static void
   1097   1.87    rillig check_non_constant_initializer(const tnode_t *tn, scl_t sclass)
   1098   1.87    rillig {
   1099  1.119    rillig 	/* TODO: rename CON to CONSTANT to avoid ambiguity with CONVERT */
   1100   1.87    rillig 	if (tn == NULL || tn->tn_op == CON)
   1101   1.87    rillig 		return;
   1102   1.87    rillig 
   1103   1.87    rillig 	sym_t *sym;
   1104   1.87    rillig 	ptrdiff_t offs;
   1105   1.87    rillig 	if (constant_addr(tn, &sym, &offs))
   1106   1.87    rillig 		return;
   1107   1.87    rillig 
   1108   1.87    rillig 	if (sclass == AUTO || sclass == REG) {
   1109   1.87    rillig 		/* non-constant initializer */
   1110   1.87    rillig 		c99ism(177);
   1111   1.87    rillig 	} else {
   1112   1.87    rillig 		/* non-constant initializer */
   1113   1.87    rillig 		error(177);
   1114   1.87    rillig 	}
   1115   1.87    rillig }
   1116   1.87    rillig 
   1117  1.114    rillig /*
   1118  1.119    rillig  * Initialize a non-array object with automatic storage duration and only a
   1119  1.119    rillig  * single initializer expression without braces by delegating to ASSIGN.
   1120  1.114    rillig  */
   1121  1.114    rillig static bool
   1122  1.114    rillig init_using_assign(tnode_t *rn)
   1123  1.114    rillig {
   1124  1.114    rillig 	tnode_t *ln, *tn;
   1125  1.114    rillig 
   1126  1.114    rillig 	if (initsym->s_type->t_tspec == ARRAY)
   1127  1.114    rillig 		return false;
   1128  1.114    rillig 	if (initstk->i_enclosing != NULL)
   1129  1.114    rillig 		return false;
   1130  1.114    rillig 
   1131  1.114    rillig 	debug_step("handing over to ASSIGN");
   1132  1.119    rillig 
   1133  1.114    rillig 	ln = new_name_node(initsym, 0);
   1134  1.114    rillig 	ln->tn_type = tduptyp(ln->tn_type);
   1135  1.114    rillig 	ln->tn_type->t_const = false;
   1136  1.119    rillig 
   1137  1.114    rillig 	tn = build(ASSIGN, ln, rn);
   1138  1.114    rillig 	expr(tn, false, false, false, false);
   1139  1.119    rillig 
   1140  1.114    rillig 	/* XXX: why not clean up the initstack here already? */
   1141  1.114    rillig 	return true;
   1142  1.114    rillig }
   1143  1.114    rillig 
   1144  1.116    rillig static void
   1145  1.116    rillig check_init_expr(tnode_t *tn, scl_t sclass)
   1146  1.116    rillig {
   1147  1.116    rillig 	tnode_t *ln;
   1148  1.116    rillig 	tspec_t lt, rt;
   1149  1.116    rillig 	struct mbl *tmem;
   1150  1.116    rillig 
   1151  1.116    rillig 	/* Create a temporary node for the left side. */
   1152  1.134    rillig 	ln = tgetblk(sizeof *ln);
   1153  1.116    rillig 	ln->tn_op = NAME;
   1154  1.116    rillig 	ln->tn_type = tduptyp(initstk->i_type);
   1155  1.116    rillig 	ln->tn_type->t_const = false;
   1156  1.116    rillig 	ln->tn_lvalue = true;
   1157  1.116    rillig 	ln->tn_sym = initsym;		/* better than nothing */
   1158  1.116    rillig 
   1159  1.116    rillig 	tn = cconv(tn);
   1160  1.116    rillig 
   1161  1.116    rillig 	lt = ln->tn_type->t_tspec;
   1162  1.116    rillig 	rt = tn->tn_type->t_tspec;
   1163  1.116    rillig 
   1164  1.116    rillig 	debug_step("typeok '%s', '%s'",
   1165  1.116    rillig 	    type_name(ln->tn_type), type_name(tn->tn_type));
   1166  1.116    rillig 	if (!typeok(INIT, 0, ln, tn))
   1167  1.116    rillig 		return;
   1168  1.116    rillig 
   1169  1.116    rillig 	/*
   1170  1.119    rillig 	 * Preserve the tree memory. This is necessary because otherwise
   1171  1.116    rillig 	 * expr() would free it.
   1172  1.116    rillig 	 */
   1173  1.116    rillig 	tmem = tsave();
   1174  1.116    rillig 	expr(tn, true, false, true, false);
   1175  1.116    rillig 	trestor(tmem);
   1176  1.116    rillig 
   1177  1.116    rillig 	check_bit_field_init(ln, lt, rt);
   1178  1.116    rillig 
   1179  1.116    rillig 	/*
   1180  1.116    rillig 	 * XXX: Is it correct to do this conversion _after_ the typeok above?
   1181  1.116    rillig 	 */
   1182  1.116    rillig 	if (lt != rt || (initstk->i_type->t_bitfield && tn->tn_op == CON))
   1183  1.116    rillig 		tn = convert(INIT, 0, initstk->i_type, tn);
   1184  1.116    rillig 
   1185  1.116    rillig 	check_non_constant_initializer(tn, sclass);
   1186  1.116    rillig }
   1187  1.116    rillig 
   1188    1.1       cgd void
   1189   1.69    rillig init_using_expr(tnode_t *tn)
   1190    1.1       cgd {
   1191   1.81    rillig 	scl_t	sclass;
   1192    1.1       cgd 
   1193   1.68    rillig 	debug_enter();
   1194   1.92    rillig 	debug_initstack();
   1195  1.126    rillig 	debug_designation();
   1196   1.95    rillig 	debug_step("expr:");
   1197   1.95    rillig 	debug_node(tn, debug_ind + 1);
   1198   1.55    rillig 
   1199  1.113    rillig 	if (initerr || tn == NULL)
   1200  1.113    rillig 		goto done;
   1201    1.1       cgd 
   1202   1.81    rillig 	sclass = initsym->s_scl;
   1203  1.114    rillig 	if ((sclass == AUTO || sclass == REG) && init_using_assign(tn))
   1204  1.113    rillig 		goto done;
   1205    1.1       cgd 
   1206   1.36    rillig 	initstack_pop_nobrace();
   1207    1.1       cgd 
   1208   1.82    rillig 	if (init_array_using_string(tn)) {
   1209   1.82    rillig 		debug_step("after initializing the string:");
   1210   1.82    rillig 		/* XXX: why not clean up the initstack here already? */
   1211  1.113    rillig 		goto done_initstack;
   1212   1.68    rillig 	}
   1213    1.1       cgd 
   1214  1.118    rillig 	initstack_next_nobrace(tn);
   1215  1.113    rillig 	if (initerr || tn == NULL)
   1216  1.113    rillig 		goto done_initstack;
   1217    1.1       cgd 
   1218  1.129    rillig 	initstk_lvalue->i_remaining--;
   1219   1.83    rillig 	debug_step("%d elements remaining", initstk->i_remaining);
   1220   1.83    rillig 
   1221  1.116    rillig 	check_init_expr(tn, sclass);
   1222   1.68    rillig 
   1223  1.113    rillig done_initstack:
   1224   1.73    rillig 	debug_initstack();
   1225  1.130    rillig 
   1226  1.113    rillig done:
   1227  1.130    rillig 	while (current_designation().head != NULL)
   1228  1.131    rillig 		designation_shift_level();
   1229  1.130    rillig 
   1230   1.68    rillig 	debug_leave();
   1231    1.1       cgd }
   1232    1.1       cgd 
   1233    1.1       cgd 
   1234   1.82    rillig /* Initialize a character array or wchar_t array with a string literal. */
   1235   1.62    rillig static bool
   1236   1.82    rillig init_array_using_string(tnode_t *tn)
   1237    1.1       cgd {
   1238    1.1       cgd 	tspec_t	t;
   1239   1.79    rillig 	initstack_element *istk;
   1240    1.1       cgd 	int	len;
   1241    1.1       cgd 	strg_t	*strg;
   1242    1.1       cgd 
   1243    1.1       cgd 	if (tn->tn_op != STRING)
   1244   1.63    rillig 		return false;
   1245    1.1       cgd 
   1246   1.68    rillig 	debug_enter();
   1247   1.73    rillig 	debug_initstack();
   1248   1.68    rillig 
   1249  1.129    rillig 	istk = initstk_lvalue;
   1250   1.47    rillig 	strg = tn->tn_string;
   1251    1.1       cgd 
   1252    1.1       cgd 	/*
   1253    1.1       cgd 	 * Check if we have an array type which can be initialized by
   1254    1.1       cgd 	 * the string.
   1255    1.1       cgd 	 */
   1256   1.12  christos 	if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
   1257   1.68    rillig 		debug_step("subt array");
   1258    1.1       cgd 		t = istk->i_subt->t_subt->t_tspec;
   1259    1.1       cgd 		if (!((strg->st_tspec == CHAR &&
   1260    1.1       cgd 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
   1261    1.1       cgd 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
   1262   1.68    rillig 			debug_leave();
   1263   1.63    rillig 			return false;
   1264    1.1       cgd 		}
   1265   1.82    rillig 		/* XXX: duplicate code, see below */
   1266  1.119    rillig 
   1267    1.1       cgd 		/* Put the array at top of stack */
   1268   1.35    rillig 		initstack_push();
   1269  1.129    rillig 		istk = initstk_lvalue;
   1270  1.119    rillig 
   1271  1.119    rillig 
   1272  1.119    rillig 		/* TODO: what if both i_type and i_subt are ARRAY? */
   1273  1.119    rillig 
   1274    1.1       cgd 	} else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
   1275   1.68    rillig 		debug_step("type array");
   1276    1.1       cgd 		t = istk->i_type->t_subt->t_tspec;
   1277    1.1       cgd 		if (!((strg->st_tspec == CHAR &&
   1278    1.1       cgd 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
   1279    1.1       cgd 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
   1280   1.68    rillig 			debug_leave();
   1281   1.63    rillig 			return false;
   1282    1.1       cgd 		}
   1283   1.82    rillig 		/* XXX: duplicate code, see above */
   1284  1.119    rillig 
   1285  1.119    rillig 		/*
   1286  1.119    rillig 		 * TODO: is this really not needed in the branch above this
   1287  1.119    rillig 		 * one?
   1288  1.119    rillig 		 */
   1289    1.1       cgd 		/*
   1290    1.1       cgd 		 * If the array is already partly initialized, we are
   1291    1.1       cgd 		 * wrong here.
   1292    1.1       cgd 		 */
   1293  1.115    rillig 		if (istk->i_remaining != istk->i_type->t_dim) {
   1294   1.68    rillig 			debug_leave();
   1295   1.63    rillig 			return false;
   1296  1.115    rillig 		}
   1297    1.1       cgd 	} else {
   1298   1.68    rillig 		debug_leave();
   1299   1.63    rillig 		return false;
   1300    1.1       cgd 	}
   1301    1.1       cgd 
   1302    1.1       cgd 	/* Get length without trailing NUL character. */
   1303    1.1       cgd 	len = strg->st_len;
   1304    1.1       cgd 
   1305   1.77    rillig 	if (istk->i_array_of_unknown_size) {
   1306   1.77    rillig 		istk->i_array_of_unknown_size = false;
   1307    1.1       cgd 		istk->i_type->t_dim = len + 1;
   1308   1.63    rillig 		setcomplete(istk->i_type, true);
   1309    1.1       cgd 	} else {
   1310  1.119    rillig 		/*
   1311  1.119    rillig 		 * TODO: check for buffer overflow in the object to be
   1312  1.119    rillig 		 * initialized
   1313  1.119    rillig 		 */
   1314  1.119    rillig 		/* XXX: double-check for off-by-one error */
   1315    1.1       cgd 		if (istk->i_type->t_dim < len) {
   1316    1.1       cgd 			/* non-null byte ignored in string initializer */
   1317    1.1       cgd 			warning(187);
   1318    1.1       cgd 		}
   1319  1.119    rillig 
   1320  1.119    rillig 		/*
   1321  1.119    rillig 		 * TODO: C99 6.7.8p14 allows a string literal to be enclosed
   1322  1.119    rillig 		 * in optional redundant braces, just like scalars.  Add tests
   1323  1.119    rillig 		 * for this.
   1324  1.119    rillig 		 */
   1325    1.1       cgd 	}
   1326    1.1       cgd 
   1327    1.1       cgd 	/* In every case the array is initialized completely. */
   1328   1.43    rillig 	istk->i_remaining = 0;
   1329    1.1       cgd 
   1330   1.73    rillig 	debug_initstack();
   1331   1.68    rillig 	debug_leave();
   1332   1.63    rillig 	return true;
   1333    1.1       cgd }
   1334