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