Home | History | Annotate | Line # | Download | only in lint1
init.c revision 1.54
      1  1.54    rillig /*	$NetBSD: init.c,v 1.54 2021/01/01 19:28:51 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.54    rillig __RCSID("$NetBSD: init.c,v 1.54 2021/01/01 19:28:51 rillig Exp $");
     41   1.1       cgd #endif
     42   1.1       cgd 
     43  1.31    rillig #include <ctype.h>
     44   1.1       cgd #include <stdlib.h>
     45  1.17   thorpej #include <string.h>
     46   1.1       cgd 
     47   1.1       cgd #include "lint1.h"
     48   1.1       cgd 
     49  1.53    rillig 
     50  1.53    rillig /*
     51  1.53    rillig  * Type of stack which is used for initialisation of aggregate types.
     52  1.54    rillig  *
     53  1.54    rillig  * XXX: Since C99, a stack is an inappropriate data structure for modelling
     54  1.54    rillig  * an initialization, since the designators don't have to be listed in a
     55  1.54    rillig  * particular order and can designate parts of sub-objects.  The member names
     56  1.54    rillig  * of non-leaf structs may thus appear repeatedly, as demonstrated in
     57  1.54    rillig  * d_init_pop_member.c.
     58  1.54    rillig  *
     59  1.54    rillig  * XXX: During initialization, there may be members of the top-level struct
     60  1.54    rillig  * that are partially initialized.  The simple i_remaining cannot model this
     61  1.54    rillig  * appropriately.
     62  1.54    rillig  *
     63  1.54    rillig  * See C99 6.7.8, which spans 6 pages full of tricky details and carefully
     64  1.54    rillig  * selected examples.
     65  1.53    rillig  */
     66  1.53    rillig typedef	struct istk {
     67  1.53    rillig 	type_t	*i_type;		/* type of initialisation */
     68  1.53    rillig 	type_t	*i_subt;		/* type of next level */
     69  1.53    rillig 	u_int	i_brace : 1;		/* need } for pop */
     70  1.53    rillig 	u_int	i_nolimit : 1;		/* incomplete array type */
     71  1.53    rillig 	u_int	i_namedmem : 1;		/* has c9x named members */
     72  1.53    rillig 	sym_t	*i_mem;			/* next structure member */
     73  1.53    rillig 	int	i_remaining;		/* # of remaining elements */
     74  1.53    rillig 	struct	istk *i_next;		/* previous level */
     75  1.53    rillig } istk_t;
     76  1.53    rillig 
     77  1.53    rillig typedef struct namlist {
     78  1.53    rillig 	const char *n_name;
     79  1.53    rillig 	struct namlist *n_prev;
     80  1.53    rillig 	struct namlist *n_next;
     81  1.53    rillig } namlist_t;
     82  1.53    rillig 
     83  1.53    rillig 
     84   1.1       cgd /*
     85   1.8       wiz  * initerr is set as soon as a fatal error occurred in an initialisation.
     86   1.1       cgd  * The effect is that the rest of the initialisation is ignored (parsed
     87   1.1       cgd  * by yacc, expression trees built, but no initialisation takes place).
     88   1.1       cgd  */
     89   1.1       cgd int	initerr;
     90   1.1       cgd 
     91   1.1       cgd /* Pointer to the symbol which is to be initialized. */
     92   1.1       cgd sym_t	*initsym;
     93   1.1       cgd 
     94   1.1       cgd /* Points to the top element of the initialisation stack. */
     95   1.1       cgd istk_t	*initstk;
     96   1.1       cgd 
     97  1.12  christos /* Points to a c9x named member; */
     98  1.12  christos namlist_t	*namedmem = NULL;
     99  1.12  christos 
    100   1.1       cgd 
    101  1.35    rillig static	int	initstack_string(tnode_t *);
    102  1.12  christos 
    103  1.12  christos #ifndef DEBUG
    104  1.12  christos #define DPRINTF(a)
    105  1.12  christos #else
    106  1.12  christos #define DPRINTF(a) printf a
    107  1.12  christos #endif
    108  1.12  christos 
    109  1.12  christos void
    110  1.51    rillig push_member(sbuf_t *sb)
    111  1.12  christos {
    112  1.28    rillig 	namlist_t *nam = xcalloc(1, sizeof (namlist_t));
    113  1.12  christos 	nam->n_name = sb->sb_name;
    114  1.27  christos 	DPRINTF(("%s: %s %p\n", __func__, nam->n_name, nam));
    115  1.12  christos 	if (namedmem == NULL) {
    116  1.51    rillig 		/*
    117  1.51    rillig 		 * XXX: Why is this a circular list?
    118  1.51    rillig 		 * XXX: Why is this a doubly-linked list?
    119  1.51    rillig 		 * A simple stack should suffice.
    120  1.51    rillig 		 */
    121  1.12  christos 		nam->n_prev = nam->n_next = nam;
    122  1.12  christos 		namedmem = nam;
    123  1.12  christos 	} else {
    124  1.12  christos 		namedmem->n_prev->n_next = nam;
    125  1.12  christos 		nam->n_prev = namedmem->n_prev;
    126  1.12  christos 		nam->n_next = namedmem;
    127  1.12  christos 		namedmem->n_prev = nam;
    128  1.12  christos 	}
    129  1.12  christos }
    130  1.12  christos 
    131  1.12  christos static void
    132  1.34    rillig pop_member(void)
    133  1.12  christos {
    134  1.27  christos 	DPRINTF(("%s: %s %p\n", __func__, namedmem->n_name, namedmem));
    135  1.12  christos 	if (namedmem->n_next == namedmem) {
    136  1.12  christos 		free(namedmem);
    137  1.12  christos 		namedmem = NULL;
    138  1.12  christos 	} else {
    139  1.12  christos 		namlist_t *nam = namedmem;
    140  1.12  christos 		namedmem = namedmem->n_next;
    141  1.52    rillig 		nam->n_prev->n_next = nam->n_next;
    142  1.52    rillig 		nam->n_next->n_prev = nam->n_prev;
    143  1.12  christos 		free(nam);
    144  1.12  christos 	}
    145  1.12  christos }
    146   1.1       cgd 
    147   1.1       cgd 
    148   1.1       cgd /*
    149   1.1       cgd  * Initialize the initialisation stack by putting an entry for the variable
    150   1.1       cgd  * which is to be initialized on it.
    151   1.1       cgd  */
    152   1.1       cgd void
    153  1.35    rillig initstack_init(void)
    154   1.1       cgd {
    155   1.1       cgd 	istk_t	*istk;
    156   1.1       cgd 
    157   1.1       cgd 	if (initerr)
    158   1.1       cgd 		return;
    159   1.1       cgd 
    160   1.1       cgd 	/* free memory used in last initialisation */
    161   1.1       cgd 	while ((istk = initstk) != NULL) {
    162  1.46    rillig 		initstk = istk->i_next;
    163   1.1       cgd 		free(istk);
    164   1.1       cgd 	}
    165   1.1       cgd 
    166  1.38    rillig 	DPRINTF(("%s\n", __func__));
    167  1.38    rillig 
    168   1.1       cgd 	/*
    169   1.1       cgd 	 * If the type which is to be initialized is an incomplete type,
    170   1.1       cgd 	 * it must be duplicated.
    171   1.1       cgd 	 */
    172   1.1       cgd 	if (initsym->s_type->t_tspec == ARRAY && incompl(initsym->s_type))
    173   1.1       cgd 		initsym->s_type = duptyp(initsym->s_type);
    174   1.1       cgd 
    175   1.1       cgd 	istk = initstk = xcalloc(1, sizeof (istk_t));
    176   1.1       cgd 	istk->i_subt = initsym->s_type;
    177  1.43    rillig 	istk->i_remaining = 1;
    178   1.1       cgd }
    179   1.1       cgd 
    180   1.1       cgd static void
    181  1.35    rillig initstack_pop_item(void)
    182   1.1       cgd {
    183  1.12  christos #ifdef DEBUG
    184  1.12  christos 	char	buf[64];
    185  1.12  christos #endif
    186   1.1       cgd 	istk_t	*istk;
    187   1.1       cgd 	sym_t	*m;
    188   1.1       cgd 
    189  1.41    rillig 	istk = initstk;
    190  1.40    rillig 	DPRINTF(("%s: pop type=%s, brace=%d remaining=%d named=%d\n", __func__,
    191  1.41    rillig 	    tyname(buf, sizeof buf, istk->i_type ? istk->i_type : istk->i_subt),
    192  1.43    rillig 	    istk->i_brace, istk->i_remaining, istk->i_namedmem));
    193  1.40    rillig 
    194  1.46    rillig 	initstk = istk->i_next;
    195   1.1       cgd 	free(istk);
    196   1.1       cgd 	istk = initstk;
    197  1.44    rillig 	lint_assert(istk != NULL);
    198  1.26  christos 
    199  1.40    rillig 	DPRINTF(("%s: top type=%s, brace=%d remaining=%d named=%d\n", __func__,
    200  1.41    rillig 	    tyname(buf, sizeof buf, istk->i_type ? istk->i_type : istk->i_subt),
    201  1.43    rillig 	    istk->i_brace, istk->i_remaining, istk->i_namedmem));
    202   1.1       cgd 
    203  1.43    rillig 	istk->i_remaining--;
    204  1.44    rillig 	lint_assert(istk->i_remaining >= 0);
    205   1.1       cgd 
    206  1.40    rillig 	DPRINTF(("%s: top remaining=%d rhs.name=%s\n", __func__,
    207  1.43    rillig 	    istk->i_remaining, namedmem ? namedmem->n_name : "*null*"));
    208  1.40    rillig 
    209  1.43    rillig 	if (istk->i_remaining >= 0 && namedmem != NULL) {
    210  1.40    rillig 
    211  1.40    rillig 		DPRINTF(("%s: named remaining=%d type=%s, rhs.name=%s\n",
    212  1.43    rillig 		    __func__, istk->i_remaining,
    213  1.40    rillig 		    tyname(buf, sizeof(buf), istk->i_type), namedmem->n_name));
    214  1.38    rillig 
    215  1.45    rillig 		for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_next) {
    216  1.40    rillig 			DPRINTF(("%s: pop lhs.name=%s rhs.name=%s\n", __func__,
    217  1.40    rillig 			    m->s_name, namedmem->n_name));
    218  1.48    rillig 			if (m->s_bitfield && m->s_name == unnamed)
    219  1.12  christos 				continue;
    220  1.12  christos 			if (strcmp(m->s_name, namedmem->n_name) == 0) {
    221  1.12  christos 				istk->i_subt = m->s_type;
    222  1.43    rillig 				istk->i_remaining++;
    223  1.34    rillig 				pop_member();
    224  1.12  christos 				return;
    225  1.12  christos 			}
    226  1.12  christos 		}
    227  1.50    rillig 		/* undefined struct/union member: %s */
    228  1.12  christos 		error(101, namedmem->n_name);
    229  1.40    rillig 		DPRINTF(("%s: end rhs.name=%s\n", __func__, namedmem->n_name));
    230  1.34    rillig 		pop_member();
    231  1.12  christos 		istk->i_namedmem = 1;
    232  1.12  christos 		return;
    233  1.12  christos 	}
    234   1.1       cgd 	/*
    235   1.1       cgd 	 * If the removed element was a structure member, we must go
    236   1.1       cgd 	 * to the next structure member.
    237   1.1       cgd 	 */
    238  1.43    rillig 	if (istk->i_remaining > 0 && istk->i_type->t_tspec == STRUCT &&
    239  1.12  christos 	    !istk->i_namedmem) {
    240   1.1       cgd 		do {
    241  1.45    rillig 			m = istk->i_mem = istk->i_mem->s_next;
    242  1.44    rillig 			lint_assert(m != NULL);
    243  1.40    rillig 			DPRINTF(("%s: pop %s\n", __func__, m->s_name));
    244  1.48    rillig 		} while (m->s_bitfield && m->s_name == unnamed);
    245   1.1       cgd 		istk->i_subt = m->s_type;
    246   1.1       cgd 	}
    247   1.1       cgd }
    248   1.1       cgd 
    249  1.36    rillig /*
    250  1.36    rillig  * Take all entries, including the first which requires a closing brace,
    251  1.36    rillig  * from the stack.
    252  1.36    rillig  */
    253  1.36    rillig static void
    254  1.36    rillig initstack_pop_brace(void)
    255  1.36    rillig {
    256  1.36    rillig 	int brace;
    257  1.36    rillig 
    258  1.36    rillig 	DPRINTF(("%s\n", __func__));
    259  1.36    rillig 	do {
    260  1.36    rillig 		brace = initstk->i_brace;
    261  1.38    rillig 		DPRINTF(("%s: loop brace=%d\n", __func__, brace));
    262  1.36    rillig 		initstack_pop_item();
    263  1.36    rillig 	} while (!brace);
    264  1.36    rillig 	DPRINTF(("%s: done\n", __func__));
    265  1.36    rillig }
    266  1.36    rillig 
    267  1.36    rillig /*
    268  1.36    rillig  * Take all entries which cannot be used for further initializers from the
    269  1.36    rillig  * stack, but do this only if they do not require a closing brace.
    270  1.36    rillig  */
    271   1.1       cgd static void
    272  1.36    rillig initstack_pop_nobrace(void)
    273   1.1       cgd {
    274   1.7     lukem 
    275  1.36    rillig 	DPRINTF(("%s\n", __func__));
    276  1.43    rillig 	while (!initstk->i_brace && initstk->i_remaining == 0 &&
    277  1.43    rillig 	       !initstk->i_nolimit)
    278  1.36    rillig 		initstack_pop_item();
    279  1.36    rillig 	DPRINTF(("%s: done\n", __func__));
    280   1.1       cgd }
    281   1.1       cgd 
    282   1.1       cgd static void
    283  1.35    rillig initstack_push(void)
    284   1.1       cgd {
    285  1.12  christos #ifdef DEBUG
    286  1.12  christos 	char	buf[64];
    287  1.12  christos #endif
    288  1.22        ad 	istk_t	*istk, *inxt;
    289   1.1       cgd 	int	cnt;
    290   1.1       cgd 	sym_t	*m;
    291   1.1       cgd 
    292   1.1       cgd 	istk = initstk;
    293   1.1       cgd 
    294   1.1       cgd 	/* Extend an incomplete array type by one element */
    295  1.43    rillig 	if (istk->i_remaining == 0) {
    296  1.30    rillig 		DPRINTF(("%s(extend) %s\n", __func__,
    297  1.30    rillig 		    tyname(buf, sizeof(buf), istk->i_type)));
    298   1.1       cgd 		/*
    299   1.1       cgd 		 * Inside of other aggregate types must not be an incomplete
    300   1.1       cgd 		 * type.
    301   1.1       cgd 		 */
    302  1.46    rillig 		lint_assert(istk->i_next->i_next == NULL);
    303  1.43    rillig 		istk->i_remaining = 1;
    304  1.44    rillig 		lint_assert(istk->i_type->t_tspec == ARRAY);
    305   1.1       cgd 		istk->i_type->t_dim++;
    306  1.32    rillig 		setcomplete(istk->i_type, 1);
    307   1.1       cgd 	}
    308   1.1       cgd 
    309  1.44    rillig 	lint_assert(istk->i_remaining > 0);
    310  1.44    rillig 	lint_assert(istk->i_type == NULL ||
    311  1.44    rillig 	    !tspec_is_scalar(istk->i_type->t_tspec));
    312   1.1       cgd 
    313   1.1       cgd 	initstk = xcalloc(1, sizeof (istk_t));
    314  1.46    rillig 	initstk->i_next = istk;
    315   1.1       cgd 	initstk->i_type = istk->i_subt;
    316  1.44    rillig 	lint_assert(initstk->i_type->t_tspec != FUNC);
    317   1.1       cgd 
    318  1.12  christos again:
    319   1.1       cgd 	istk = initstk;
    320   1.1       cgd 
    321  1.26  christos 	DPRINTF(("%s(%s)\n", __func__, tyname(buf, sizeof(buf), istk->i_type)));
    322   1.1       cgd 	switch (istk->i_type->t_tspec) {
    323   1.1       cgd 	case ARRAY:
    324  1.16  christos 		if (namedmem) {
    325  1.26  christos 			DPRINTF(("%s: ARRAY %s brace=%d\n", __func__,
    326  1.20  christos 			    namedmem->n_name, istk->i_brace));
    327  1.19  christos 			goto pop;
    328  1.46    rillig 		} else if (istk->i_next->i_namedmem) {
    329  1.26  christos 			istk->i_brace = 1;
    330  1.26  christos 			DPRINTF(("%s ARRAY brace=%d, namedmem=%d\n", __func__,
    331  1.46    rillig 			    istk->i_brace, istk->i_next->i_namedmem));
    332  1.20  christos 		}
    333  1.20  christos 
    334  1.46    rillig 		if (incompl(istk->i_type) && istk->i_next->i_next != NULL) {
    335   1.1       cgd 			/* initialisation of an incomplete type */
    336   1.1       cgd 			error(175);
    337   1.1       cgd 			initerr = 1;
    338   1.1       cgd 			return;
    339   1.1       cgd 		}
    340   1.1       cgd 		istk->i_subt = istk->i_type->t_subt;
    341   1.1       cgd 		istk->i_nolimit = incompl(istk->i_type);
    342  1.43    rillig 		istk->i_remaining = istk->i_type->t_dim;
    343  1.26  christos 		DPRINTF(("%s: elements array %s[%d] %s\n", __func__,
    344  1.43    rillig 		    tyname(buf, sizeof(buf), istk->i_subt), istk->i_remaining,
    345  1.16  christos 		    namedmem ? namedmem->n_name : "*none*"));
    346   1.1       cgd 		break;
    347   1.1       cgd 	case UNION:
    348   1.1       cgd 		if (tflag)
    349   1.1       cgd 			/* initialisation of union is illegal in trad. C */
    350   1.1       cgd 			warning(238);
    351   1.1       cgd 		/* FALLTHROUGH */
    352   1.1       cgd 	case STRUCT:
    353   1.1       cgd 		if (incompl(istk->i_type)) {
    354   1.1       cgd 			/* initialisation of an incomplete type */
    355   1.1       cgd 			error(175);
    356   1.1       cgd 			initerr = 1;
    357   1.1       cgd 			return;
    358   1.1       cgd 		}
    359   1.1       cgd 		cnt = 0;
    360  1.40    rillig 		DPRINTF(("%s: lookup type=%s, name=%s named=%d\n", __func__,
    361  1.16  christos 		    tyname(buf, sizeof(buf), istk->i_type),
    362  1.26  christos 		    namedmem ? namedmem->n_name : "*none*", istk->i_namedmem));
    363  1.45    rillig 		for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_next) {
    364  1.48    rillig 			if (m->s_bitfield && m->s_name == unnamed)
    365   1.1       cgd 				continue;
    366  1.12  christos 			if (namedmem != NULL) {
    367  1.40    rillig 				DPRINTF(("%s: named lhs.member=%s, rhs.member=%s\n",
    368  1.26  christos 				    __func__, m->s_name, namedmem->n_name));
    369  1.12  christos 				if (strcmp(m->s_name, namedmem->n_name) == 0) {
    370  1.12  christos 					cnt++;
    371  1.12  christos 					break;
    372  1.12  christos 				} else
    373  1.12  christos 					continue;
    374  1.12  christos 			}
    375   1.1       cgd 			if (++cnt == 1) {
    376   1.1       cgd 				istk->i_mem = m;
    377   1.1       cgd 				istk->i_subt = m->s_type;
    378   1.1       cgd 			}
    379   1.1       cgd 		}
    380  1.12  christos 		if (namedmem != NULL) {
    381  1.12  christos 			if (m == NULL) {
    382  1.40    rillig 				DPRINTF(("%s: pop struct\n", __func__));
    383  1.19  christos 				goto pop;
    384  1.28    rillig 			}
    385  1.26  christos 			istk->i_mem = m;
    386  1.26  christos 			istk->i_subt = m->s_type;
    387  1.19  christos 			istk->i_namedmem = 1;
    388  1.40    rillig 			DPRINTF(("%s: named name=%s\n", __func__,
    389  1.26  christos 			    namedmem->n_name));
    390  1.34    rillig 			pop_member();
    391  1.12  christos 			cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
    392  1.12  christos 		}
    393  1.26  christos 		istk->i_brace = 1;
    394  1.40    rillig 		DPRINTF(("%s: unnamed type=%s, brace=%d\n", __func__,
    395  1.26  christos 		    tyname(buf, sizeof(buf),
    396  1.30    rillig 			istk->i_type ? istk->i_type : istk->i_subt),
    397  1.26  christos 		    istk->i_brace));
    398   1.1       cgd 		if (cnt == 0) {
    399   1.1       cgd 			/* cannot init. struct/union with no named member */
    400   1.1       cgd 			error(179);
    401   1.1       cgd 			initerr = 1;
    402   1.1       cgd 			return;
    403   1.1       cgd 		}
    404  1.43    rillig 		istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
    405   1.1       cgd 		break;
    406   1.1       cgd 	default:
    407  1.12  christos 		if (namedmem) {
    408  1.40    rillig 			DPRINTF(("%s: pop\n", __func__));
    409  1.19  christos 	pop:
    410  1.46    rillig 			inxt = initstk->i_next;
    411  1.12  christos 			free(istk);
    412  1.22        ad 			initstk = inxt;
    413  1.12  christos 			goto again;
    414  1.12  christos 		}
    415  1.43    rillig 		istk->i_remaining = 1;
    416   1.1       cgd 		break;
    417   1.1       cgd 	}
    418   1.1       cgd }
    419   1.1       cgd 
    420   1.1       cgd static void
    421  1.35    rillig initstack_check_too_many(void)
    422   1.1       cgd {
    423   1.1       cgd 	istk_t	*istk;
    424   1.1       cgd 
    425   1.1       cgd 	istk = initstk;
    426   1.1       cgd 
    427   1.1       cgd 	/*
    428   1.1       cgd 	 * If a closing brace is expected we have at least one initializer
    429   1.1       cgd 	 * too much.
    430   1.1       cgd 	 */
    431  1.43    rillig 	if (istk->i_remaining == 0 && !istk->i_nolimit && !istk->i_namedmem) {
    432   1.1       cgd 		switch (istk->i_type->t_tspec) {
    433   1.1       cgd 		case ARRAY:
    434  1.49    rillig 			/* too many array initializers, expected %d */
    435  1.23  christos 			error(173, istk->i_type->t_dim);
    436   1.1       cgd 			break;
    437   1.1       cgd 		case STRUCT:
    438   1.1       cgd 		case UNION:
    439   1.1       cgd 			/* too many struct/union initializers */
    440   1.1       cgd 			error(172);
    441   1.1       cgd 			break;
    442   1.1       cgd 		default:
    443   1.1       cgd 			/* too many initializers */
    444   1.1       cgd 			error(174);
    445   1.1       cgd 			break;
    446   1.1       cgd 		}
    447   1.1       cgd 		initerr = 1;
    448   1.1       cgd 	}
    449   1.1       cgd }
    450   1.1       cgd 
    451   1.1       cgd static void
    452  1.37    rillig initstack_next_brace(void)
    453   1.1       cgd {
    454  1.12  christos 	char buf[64];
    455   1.7     lukem 
    456  1.37    rillig 	DPRINTF(("%s\n", __func__));
    457  1.37    rillig 	if (initstk->i_type != NULL &&
    458  1.37    rillig 	    tspec_is_scalar(initstk->i_type->t_tspec)) {
    459  1.49    rillig 		/* invalid initializer type %s */
    460  1.37    rillig 		error(176, tyname(buf, sizeof(buf), initstk->i_type));
    461  1.37    rillig 		initerr = 1;
    462  1.37    rillig 	}
    463  1.37    rillig 	if (!initerr)
    464  1.37    rillig 		initstack_check_too_many();
    465  1.37    rillig 	if (!initerr)
    466  1.37    rillig 		initstack_push();
    467  1.37    rillig 	if (!initerr) {
    468  1.37    rillig 		initstk->i_brace = 1;
    469  1.40    rillig 		DPRINTF(("%s: %p %s\n", __func__,
    470  1.37    rillig 		    namedmem,
    471  1.37    rillig 		    tyname(buf, sizeof(buf),
    472  1.40    rillig 			initstk->i_type ? initstk->i_type : initstk->i_subt)));
    473  1.37    rillig 	}
    474  1.37    rillig }
    475  1.37    rillig 
    476  1.37    rillig static void
    477  1.37    rillig initstack_next_nobrace(void)
    478  1.37    rillig {
    479  1.37    rillig 
    480  1.37    rillig 	DPRINTF(("%s\n", __func__));
    481  1.37    rillig 	if (initstk->i_type == NULL &&
    482  1.37    rillig 	    !tspec_is_scalar(initstk->i_subt->t_tspec)) {
    483  1.37    rillig 		/* {}-enclosed initializer required */
    484  1.37    rillig 		error(181);
    485  1.37    rillig 	}
    486  1.37    rillig 
    487  1.37    rillig 	/* Make sure an entry with a scalar type is at the top of the stack. */
    488  1.37    rillig 	if (!initerr)
    489  1.37    rillig 		initstack_check_too_many();
    490  1.42    rillig 	while (!initerr) {
    491  1.42    rillig 		if ((initstk->i_type != NULL &&
    492  1.42    rillig 		     tspec_is_scalar(initstk->i_type->t_tspec)))
    493  1.42    rillig 			break;
    494  1.42    rillig 		initstack_push();
    495   1.1       cgd 	}
    496   1.1       cgd }
    497   1.1       cgd 
    498   1.1       cgd void
    499  1.34    rillig init_lbrace(void)
    500   1.1       cgd {
    501  1.26  christos 	DPRINTF(("%s\n", __func__));
    502   1.7     lukem 
    503   1.1       cgd 	if (initerr)
    504   1.1       cgd 		return;
    505   1.1       cgd 
    506   1.1       cgd 	if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
    507  1.46    rillig 	    initstk->i_next == NULL) {
    508  1.29    rillig 		if (tflag && !tspec_is_scalar(initstk->i_subt->t_tspec))
    509  1.50    rillig 			/* no automatic aggregate initialization in trad. C */
    510   1.1       cgd 			warning(188);
    511   1.1       cgd 	}
    512   1.1       cgd 
    513   1.1       cgd 	/*
    514   1.1       cgd 	 * Remove all entries which cannot be used for further initializers
    515   1.1       cgd 	 * and do not expect a closing brace.
    516   1.1       cgd 	 */
    517  1.36    rillig 	initstack_pop_nobrace();
    518   1.1       cgd 
    519  1.37    rillig 	initstack_next_brace();
    520   1.1       cgd }
    521   1.1       cgd 
    522   1.1       cgd void
    523  1.34    rillig init_rbrace(void)
    524   1.1       cgd {
    525  1.26  christos 	DPRINTF(("%s\n", __func__));
    526   1.7     lukem 
    527   1.1       cgd 	if (initerr)
    528   1.1       cgd 		return;
    529   1.1       cgd 
    530  1.36    rillig 	initstack_pop_brace();
    531   1.1       cgd }
    532   1.1       cgd 
    533   1.1       cgd void
    534   1.7     lukem mkinit(tnode_t *tn)
    535   1.1       cgd {
    536   1.1       cgd 	ptrdiff_t offs;
    537   1.1       cgd 	sym_t	*sym;
    538   1.1       cgd 	tspec_t	lt, rt;
    539   1.1       cgd 	tnode_t	*ln;
    540   1.1       cgd 	struct	mbl *tmem;
    541   1.1       cgd 	scl_t	sc;
    542  1.12  christos #ifdef DEBUG
    543  1.19  christos 	char	buf[64], sbuf[64];
    544  1.12  christos #endif
    545   1.1       cgd 
    546  1.40    rillig 	DPRINTF(("%s: type=%s, value=%s\n", __func__,
    547  1.34    rillig 	    tyname(buf, sizeof(buf), tn->tn_type),
    548  1.34    rillig 	    print_tnode(sbuf, sizeof(sbuf), tn)));
    549   1.1       cgd 	if (initerr || tn == NULL)
    550  1.25  christos 		return;
    551   1.1       cgd 
    552   1.1       cgd 	sc = initsym->s_scl;
    553   1.1       cgd 
    554   1.1       cgd 	/*
    555  1.13  christos 	 * Do not test for automatic aggregate initialisation. If the
    556   1.9       wiz 	 * initializer starts with a brace we have the warning already.
    557   1.1       cgd 	 * If not, an error will be printed that the initializer must
    558   1.1       cgd 	 * be enclosed by braces.
    559   1.1       cgd 	 */
    560   1.1       cgd 
    561   1.1       cgd 	/*
    562   1.1       cgd 	 * Local initialisation of non-array-types with only one expression
    563   1.1       cgd 	 * without braces is done by ASSIGN
    564   1.1       cgd 	 */
    565   1.1       cgd 	if ((sc == AUTO || sc == REG) &&
    566  1.46    rillig 	    initsym->s_type->t_tspec != ARRAY && initstk->i_next == NULL) {
    567   1.1       cgd 		ln = getnnode(initsym, 0);
    568   1.1       cgd 		ln->tn_type = tduptyp(ln->tn_type);
    569   1.1       cgd 		ln->tn_type->t_const = 0;
    570   1.1       cgd 		tn = build(ASSIGN, ln, tn);
    571  1.25  christos 		expr(tn, 0, 0, 0);
    572  1.25  christos 		return;
    573   1.1       cgd 	}
    574   1.1       cgd 
    575   1.1       cgd 	/*
    576   1.1       cgd 	 * Remove all entries which cannot be used for further initializers
    577   1.1       cgd 	 * and do not require a closing brace.
    578   1.1       cgd 	 */
    579  1.36    rillig 	initstack_pop_nobrace();
    580   1.1       cgd 
    581  1.35    rillig 	/* Initialisations by strings are done in initstack_string(). */
    582  1.35    rillig 	if (initstack_string(tn))
    583  1.25  christos 		return;
    584   1.1       cgd 
    585  1.37    rillig 	initstack_next_nobrace();
    586   1.1       cgd 	if (initerr || tn == NULL)
    587  1.25  christos 		return;
    588   1.1       cgd 
    589  1.43    rillig 	initstk->i_remaining--;
    590  1.43    rillig 	DPRINTF(("%s: remaining=%d tn=%p\n", __func__,
    591  1.43    rillig 	    initstk->i_remaining, tn));
    592   1.1       cgd 	/* Create a temporary node for the left side. */
    593   1.1       cgd 	ln = tgetblk(sizeof (tnode_t));
    594   1.1       cgd 	ln->tn_op = NAME;
    595   1.1       cgd 	ln->tn_type = tduptyp(initstk->i_type);
    596   1.1       cgd 	ln->tn_type->t_const = 0;
    597   1.1       cgd 	ln->tn_lvalue = 1;
    598   1.1       cgd 	ln->tn_sym = initsym;		/* better than nothing */
    599   1.1       cgd 
    600   1.1       cgd 	tn = cconv(tn);
    601   1.1       cgd 
    602   1.1       cgd 	lt = ln->tn_type->t_tspec;
    603   1.1       cgd 	rt = tn->tn_type->t_tspec;
    604   1.1       cgd 
    605  1.44    rillig 	lint_assert(tspec_is_scalar(lt));
    606   1.1       cgd 
    607   1.1       cgd 	if (!typeok(INIT, 0, ln, tn))
    608  1.25  christos 		return;
    609   1.1       cgd 
    610   1.1       cgd 	/*
    611  1.38    rillig 	 * Store the tree memory. This is necessary because otherwise
    612   1.1       cgd 	 * expr() would free it.
    613   1.1       cgd 	 */
    614   1.1       cgd 	tmem = tsave();
    615  1.15  christos 	expr(tn, 1, 0, 1);
    616   1.1       cgd 	trestor(tmem);
    617   1.7     lukem 
    618  1.29    rillig 	if (tspec_is_int(lt) && ln->tn_type->t_isfield && !tspec_is_int(rt)) {
    619   1.1       cgd 		/*
    620   1.1       cgd 		 * Bit-fields can be initialized in trad. C only by integer
    621   1.1       cgd 		 * constants.
    622   1.1       cgd 		 */
    623   1.1       cgd 		if (tflag)
    624   1.1       cgd 			/* bit-field initialisation is illegal in trad. C */
    625   1.1       cgd 			warning(186);
    626   1.1       cgd 	}
    627   1.1       cgd 
    628   1.1       cgd 	if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON))
    629   1.1       cgd 		tn = convert(INIT, 0, initstk->i_type, tn);
    630   1.1       cgd 
    631   1.1       cgd 	if (tn != NULL && tn->tn_op != CON) {
    632   1.1       cgd 		sym = NULL;
    633   1.1       cgd 		offs = 0;
    634   1.1       cgd 		if (conaddr(tn, &sym, &offs) == -1) {
    635   1.3       jpo 			if (sc == AUTO || sc == REG) {
    636   1.1       cgd 				/* non-constant initializer */
    637  1.24  christos 				(void)c99ism(177);
    638   1.1       cgd 			} else {
    639   1.1       cgd 				/* non-constant initializer */
    640   1.1       cgd 				error(177);
    641   1.1       cgd 			}
    642   1.1       cgd 		}
    643   1.1       cgd 	}
    644   1.1       cgd }
    645   1.1       cgd 
    646   1.1       cgd 
    647   1.1       cgd static int
    648  1.35    rillig initstack_string(tnode_t *tn)
    649   1.1       cgd {
    650   1.1       cgd 	tspec_t	t;
    651   1.1       cgd 	istk_t	*istk;
    652   1.1       cgd 	int	len;
    653   1.1       cgd 	strg_t	*strg;
    654   1.1       cgd 
    655   1.1       cgd 	if (tn->tn_op != STRING)
    656  1.33    rillig 		return 0;
    657   1.1       cgd 
    658   1.1       cgd 	istk = initstk;
    659  1.47    rillig 	strg = tn->tn_string;
    660   1.1       cgd 
    661   1.1       cgd 	/*
    662   1.1       cgd 	 * Check if we have an array type which can be initialized by
    663   1.1       cgd 	 * the string.
    664   1.1       cgd 	 */
    665  1.12  christos 	if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
    666  1.26  christos 		DPRINTF(("%s: subt array\n", __func__));
    667   1.1       cgd 		t = istk->i_subt->t_subt->t_tspec;
    668   1.1       cgd 		if (!((strg->st_tspec == CHAR &&
    669   1.1       cgd 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
    670   1.1       cgd 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
    671  1.33    rillig 			return 0;
    672   1.1       cgd 		}
    673   1.1       cgd 		/* Put the array at top of stack */
    674  1.35    rillig 		initstack_push();
    675   1.1       cgd 		istk = initstk;
    676   1.1       cgd 	} else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
    677  1.26  christos 		DPRINTF(("%s: type array\n", __func__));
    678   1.1       cgd 		t = istk->i_type->t_subt->t_tspec;
    679   1.1       cgd 		if (!((strg->st_tspec == CHAR &&
    680   1.1       cgd 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
    681   1.1       cgd 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
    682  1.33    rillig 			return 0;
    683   1.1       cgd 		}
    684   1.1       cgd 		/*
    685   1.1       cgd 		 * If the array is already partly initialized, we are
    686   1.1       cgd 		 * wrong here.
    687   1.1       cgd 		 */
    688  1.43    rillig 		if (istk->i_remaining != istk->i_type->t_dim)
    689  1.33    rillig 			return 0;
    690   1.1       cgd 	} else {
    691  1.33    rillig 		return 0;
    692   1.1       cgd 	}
    693   1.1       cgd 
    694   1.1       cgd 	/* Get length without trailing NUL character. */
    695   1.1       cgd 	len = strg->st_len;
    696   1.1       cgd 
    697   1.1       cgd 	if (istk->i_nolimit) {
    698   1.1       cgd 		istk->i_nolimit = 0;
    699   1.1       cgd 		istk->i_type->t_dim = len + 1;
    700  1.32    rillig 		setcomplete(istk->i_type, 1);
    701   1.1       cgd 	} else {
    702   1.1       cgd 		if (istk->i_type->t_dim < len) {
    703   1.1       cgd 			/* non-null byte ignored in string initializer */
    704   1.1       cgd 			warning(187);
    705   1.1       cgd 		}
    706   1.1       cgd 	}
    707   1.1       cgd 
    708   1.1       cgd 	/* In every case the array is initialized completely. */
    709  1.43    rillig 	istk->i_remaining = 0;
    710   1.1       cgd 
    711  1.33    rillig 	return 1;
    712   1.1       cgd }
    713