1 /* $NetBSD: init.c,v 1.9 2001/09/18 18:15:54 wiz Exp $ */
4 * Copyright (c) 1994, 1995 Jochen Pohl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #if defined(__RCSID) && !defined(lint)
36 __RCSID("$NetBSD: init.c,v 1.9 2001/09/18 18:15:54 wiz Exp $");
38 __FBSDID("$FreeBSD$");
45 * initerr is set as soon as a fatal error occurred in an initialisation.
46 * The effect is that the rest of the initialisation is ignored (parsed
47 * by yacc, expression trees built, but no initialisation takes place).
51 /* Pointer to the symbol which is to be initialized. */
54 /* Points to the top element of the initialisation stack. */
58 static void popi2(void);
59 static void popinit(int);
60 static void pushinit(void);
61 static void testinit(void);
62 static void nextinit(int);
63 static int strginit(tnode_t *);
67 * Initialize the initialisation stack by putting an entry for the variable
68 * which is to be initialized on it.
78 /* free memory used in last initialisation */
79 while ((istk = initstk) != NULL) {
80 initstk = istk->i_nxt;
85 * If the type which is to be initialized is an incomplete type,
86 * it must be duplicated.
88 if (initsym->s_type->t_tspec == ARRAY && incompl(initsym->s_type))
89 initsym->s_type = duptyp(initsym->s_type);
91 istk = initstk = xcalloc(1, sizeof (istk_t));
92 istk->i_subt = initsym->s_type;
103 initstk = (istk = initstk)->i_nxt;
115 * If the removed element was a structure member, we must go
116 * to the next structure member.
118 if (istk->i_cnt > 0 && istk->i_type->t_tspec == STRUCT) {
120 m = istk->i_mem = istk->i_mem->s_nxt;
123 } while (m->s_field && m->s_name == unnamed);
124 istk->i_subt = m->s_type;
134 * Take all entries, including the first which requires
135 * a closing brace, from the stack.
138 brace = initstk->i_brace;
143 * Take all entries which cannot be used for further
144 * initializers from the stack, but do this only if
145 * they do not require a closing brace.
147 while (!initstk->i_brace &&
148 initstk->i_cnt == 0 && !initstk->i_nolimit) {
163 /* Extend an incomplete array type by one element */
164 if (istk->i_cnt == 0) {
166 * Inside of other aggregate types must not be an incomplete
169 if (istk->i_nxt->i_nxt != NULL)
170 lerror("pushinit() 1");
172 if (istk->i_type->t_tspec != ARRAY)
173 lerror("pushinit() 2");
174 istk->i_type->t_dim++;
175 /* from now its a complete type */
176 setcompl(istk->i_type, 0);
179 if (istk->i_cnt <= 0)
180 lerror("pushinit() 3");
181 if (istk->i_type != NULL && issclt(istk->i_type->t_tspec))
182 lerror("pushinit() 4");
184 initstk = xcalloc(1, sizeof (istk_t));
185 initstk->i_nxt = istk;
186 initstk->i_type = istk->i_subt;
187 if (initstk->i_type->t_tspec == FUNC)
188 lerror("pushinit() 5");
192 switch (istk->i_type->t_tspec) {
194 if (incompl(istk->i_type) && istk->i_nxt->i_nxt != NULL) {
195 /* initialisation of an incomplete type */
200 istk->i_subt = istk->i_type->t_subt;
201 istk->i_nolimit = incompl(istk->i_type);
202 istk->i_cnt = istk->i_type->t_dim;
206 /* initialisation of union is illegal in trad. C */
210 if (incompl(istk->i_type)) {
211 /* initialisation of an incomplete type */
217 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
218 if (m->s_field && m->s_name == unnamed)
222 istk->i_subt = m->s_type;
226 /* cannot init. struct/union with no named member */
231 istk->i_cnt = istk->i_type->t_tspec == STRUCT ? cnt : 1;
247 * If a closing brace is expected we have at least one initializer
250 if (istk->i_cnt == 0 && !istk->i_nolimit) {
251 switch (istk->i_type->t_tspec) {
253 /* too many array initializers */
258 /* too many struct/union initializers */
262 /* too many initializers */
275 if (initstk->i_type == NULL &&
276 !issclt(initstk->i_subt->t_tspec)) {
277 /* {}-enclosed initializer required */
281 * Make sure an entry with a scalar type is at the top
286 while (!initerr && (initstk->i_type == NULL ||
287 !issclt(initstk->i_type->t_tspec))) {
292 if (initstk->i_type != NULL &&
293 issclt(initstk->i_type->t_tspec)) {
294 /* invalid initializer */
303 initstk->i_brace = 1;
314 if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
315 initstk->i_nxt == NULL) {
316 if (tflag && !issclt(initstk->i_subt->t_tspec))
317 /* no automatic aggregate initialization in trad. C*/
322 * Remove all entries which cannot be used for further initializers
323 * and do not expect a closing brace.
350 if (initerr || tn == NULL)
356 * Do not test for automatic aggregat initialisation. If the
357 * initializer starts with a brace we have the warning already.
358 * If not, an error will be printed that the initializer must
359 * be enclosed by braces.
363 * Local initialisation of non-array-types with only one expression
364 * without braces is done by ASSIGN
366 if ((sc == AUTO || sc == REG) &&
367 initsym->s_type->t_tspec != ARRAY && initstk->i_nxt == NULL) {
368 ln = getnnode(initsym, 0);
369 ln->tn_type = tduptyp(ln->tn_type);
370 ln->tn_type->t_const = 0;
371 tn = build(ASSIGN, ln, tn);
377 * Remove all entries which cannot be used for further initializers
378 * and do not require a closing brace.
382 /* Initialisations by strings are done in strginit(). */
387 if (initerr || tn == NULL)
392 /* Create a temporary node for the left side. */
393 ln = tgetblk(sizeof (tnode_t));
395 ln->tn_type = tduptyp(initstk->i_type);
396 ln->tn_type->t_const = 0;
398 ln->tn_sym = initsym; /* better than nothing */
402 lt = ln->tn_type->t_tspec;
403 rt = tn->tn_type->t_tspec;
406 lerror("mkinit() 1");
408 if (!typeok(INIT, 0, ln, tn))
412 * Store the tree memory. This is nessesary because otherwise
413 * expr() would free it.
419 if (isityp(lt) && ln->tn_type->t_isfield && !isityp(rt)) {
421 * Bit-fields can be initialized in trad. C only by integer
425 /* bit-field initialisation is illegal in trad. C */
429 if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON))
430 tn = convert(INIT, 0, initstk->i_type, tn);
432 if (tn != NULL && tn->tn_op != CON) {
435 if (conaddr(tn, &sym, &offs) == -1) {
436 if (sc == AUTO || sc == REG) {
437 /* non-constant initializer */
440 /* non-constant initializer */
452 strginit(tnode_t *tn)
459 if (tn->tn_op != STRING)
466 * Check if we have an array type which can be initialized by
469 if (istk->i_subt->t_tspec == ARRAY) {
470 t = istk->i_subt->t_subt->t_tspec;
471 if (!((strg->st_tspec == CHAR &&
472 (t == CHAR || t == UCHAR || t == SCHAR)) ||
473 (strg->st_tspec == WCHAR && t == WCHAR))) {
476 /* Put the array at top of stack */
479 } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
480 t = istk->i_type->t_subt->t_tspec;
481 if (!((strg->st_tspec == CHAR &&
482 (t == CHAR || t == UCHAR || t == SCHAR)) ||
483 (strg->st_tspec == WCHAR && t == WCHAR))) {
487 * If the array is already partly initialized, we are
490 if (istk->i_cnt != istk->i_type->t_dim)
496 /* Get length without trailing NUL character. */
499 if (istk->i_nolimit) {
501 istk->i_type->t_dim = len + 1;
502 /* from now complete type */
503 setcompl(istk->i_type, 0);
505 if (istk->i_type->t_dim < len) {
506 /* non-null byte ignored in string initializer */
511 /* In every case the array is initialized completely. */