1 /* $NetBSD: init.c,v 1.10 2002/01/31 19:36:54 tv 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.10 2002/01/31 19:36:54 tv Exp $");
38 __FBSDID("$FreeBSD$");
47 * initerr is set as soon as a fatal error occurred in an initialisation.
48 * The effect is that the rest of the initialisation is ignored (parsed
49 * by yacc, expression trees built, but no initialisation takes place).
53 /* Pointer to the symbol which is to be initialized. */
56 /* Points to the top element of the initialisation stack. */
59 typedef struct namlist {
61 struct namlist *n_prev;
62 struct namlist *n_next;
65 /* Points to a c9x named member; */
66 namlist_t *namedmem = NULL;
69 static void popi2(void);
70 static void popinit(int);
71 static void pushinit(void);
72 static void testinit(void);
73 static void nextinit(int);
74 static int strginit(tnode_t *);
75 static void memberpop(void);
80 #define DPRINTF(a) printf a
87 namlist_t *nam = xcalloc(1, sizeof (namlist_t));
88 nam->n_name = sb->sb_name;
89 DPRINTF(("memberpush = %s\n", nam->n_name));
90 if (namedmem == NULL) {
91 nam->n_prev = nam->n_next = nam;
94 namedmem->n_prev->n_next = nam;
95 nam->n_prev = namedmem->n_prev;
96 nam->n_next = namedmem;
97 namedmem->n_prev = nam;
100 nam->n_next = namedmem;
108 DPRINTF(("memberpop = %s\n", namedmem->n_name));
109 if (namedmem->n_next == namedmem) {
113 namlist_t *nam = namedmem;
114 namedmem = namedmem->n_next;
118 namedmem = namedmem->n_next;
125 * Initialize the initialisation stack by putting an entry for the variable
126 * which is to be initialized on it.
136 /* free memory used in last initialisation */
137 while ((istk = initstk) != NULL) {
138 initstk = istk->i_nxt;
143 * If the type which is to be initialized is an incomplete type,
144 * it must be duplicated.
146 if (initsym->s_type->t_tspec == ARRAY && incompl(initsym->s_type))
147 initsym->s_type = duptyp(initsym->s_type);
149 istk = initstk = xcalloc(1, sizeof (istk_t));
150 istk->i_subt = initsym->s_type;
164 initstk = (istk = initstk)->i_nxt;
175 DPRINTF(("popi2(): %d %s\n", istk->i_cnt,
176 namedmem ? namedmem->n_name : "*null*"));
177 if (istk->i_cnt >= 0 && namedmem != NULL) {
178 DPRINTF(("popi2(): %d %s %s\n", istk->i_cnt,
179 tyname(buf, sizeof(buf), istk->i_type), namedmem->n_name));
180 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
181 if (m->s_field && m->s_name == unnamed)
183 if (strcmp(m->s_name, namedmem->n_name) == 0) {
184 istk->i_subt = m->s_type;
190 error(101, namedmem->n_name);
192 istk->i_namedmem = 1;
196 * If the removed element was a structure member, we must go
197 * to the next structure member.
199 if (istk->i_cnt > 0 && istk->i_type->t_tspec == STRUCT &&
202 m = istk->i_mem = istk->i_mem->s_nxt;
205 } while (m->s_field && m->s_name == unnamed);
206 istk->i_subt = m->s_type;
213 DPRINTF(("popinit(%d)\n", brace));
217 * Take all entries, including the first which requires
218 * a closing brace, from the stack.
221 brace = initstk->i_brace;
226 * Take all entries which cannot be used for further
227 * initializers from the stack, but do this only if
228 * they do not require a closing brace.
230 while (!initstk->i_brace &&
231 initstk->i_cnt == 0 && !initstk->i_nolimit) {
249 /* Extend an incomplete array type by one element */
250 if (istk->i_cnt == 0) {
251 DPRINTF(("pushinit(extend) %s\n", tyname(buf, sizeof(buf),
254 * Inside of other aggregate types must not be an incomplete
257 if (istk->i_nxt->i_nxt != NULL)
258 LERROR("pushinit()");
260 if (istk->i_type->t_tspec != ARRAY)
261 LERROR("pushinit()");
262 istk->i_type->t_dim++;
263 /* from now its a complete type */
264 setcompl(istk->i_type, 0);
267 if (istk->i_cnt <= 0)
268 LERROR("pushinit()");
269 if (istk->i_type != NULL && issclt(istk->i_type->t_tspec))
270 LERROR("pushinit() 4");
272 initstk = xcalloc(1, sizeof (istk_t));
273 initstk->i_nxt = istk;
274 initstk->i_type = istk->i_subt;
275 if (initstk->i_type->t_tspec == FUNC)
276 LERROR("pushinit()");
281 DPRINTF(("pushinit(%s)\n", tyname(buf, sizeof(buf), istk->i_type)));
282 switch (istk->i_type->t_tspec) {
285 DPRINTF(("pushinit ARRAY %s\n", namedmem->n_name));
287 initstk = initstk->i_nxt;
290 if (incompl(istk->i_type) && istk->i_nxt->i_nxt != NULL) {
291 /* initialisation of an incomplete type */
296 istk->i_subt = istk->i_type->t_subt;
297 istk->i_nolimit = incompl(istk->i_type);
298 istk->i_cnt = istk->i_type->t_dim;
299 DPRINTF(("elements array %s[%d] %s\n",
300 tyname(buf, sizeof(buf), istk->i_subt), istk->i_cnt,
301 namedmem ? namedmem->n_name : "*none*"));
305 /* initialisation of union is illegal in trad. C */
309 if (incompl(istk->i_type)) {
310 /* initialisation of an incomplete type */
316 DPRINTF(("2. member lookup %s %s\n",
317 tyname(buf, sizeof(buf), istk->i_type),
318 namedmem ? namedmem->n_name : "*none*"));
319 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
320 if (m->s_field && m->s_name == unnamed)
322 if (namedmem != NULL) {
323 DPRINTF(("pushinit():[member:%s, looking:%s]\n",
324 m->s_name, namedmem->n_name));
325 if (strcmp(m->s_name, namedmem->n_name) == 0) {
333 istk->i_subt = m->s_type;
336 if (namedmem != NULL) {
337 istk->i_namedmem = 1;
339 error(101, namedmem->n_name);
343 istk->i_subt = m->s_type;
346 cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
349 /* cannot init. struct/union with no named member */
354 istk->i_cnt = istk->i_type->t_tspec == STRUCT ? cnt : 1;
358 DPRINTF(("pushinit(): pop\n"));
360 initstk = initstk->i_nxt;
376 * If a closing brace is expected we have at least one initializer
379 if (istk->i_cnt == 0 && !istk->i_nolimit && !istk->i_namedmem) {
380 switch (istk->i_type->t_tspec) {
382 /* too many array initializers */
387 /* too many struct/union initializers */
391 /* too many initializers */
404 DPRINTF(("nextinit(%d)\n", brace));
406 if (initstk->i_type == NULL &&
407 !issclt(initstk->i_subt->t_tspec)) {
408 /* {}-enclosed initializer required */
412 * Make sure an entry with a scalar type is at the top
417 while (!initerr && (initstk->i_type == NULL ||
418 !issclt(initstk->i_type->t_tspec))) {
423 if (initstk->i_type != NULL &&
424 issclt(initstk->i_type->t_tspec)) {
425 /* invalid initializer */
434 initstk->i_brace = 1;
445 if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
446 initstk->i_nxt == NULL) {
447 if (tflag && !issclt(initstk->i_subt->t_tspec))
448 /* no automatic aggregate initialization in trad. C*/
453 * Remove all entries which cannot be used for further initializers
454 * and do not expect a closing brace.
484 DPRINTF(("mkinit(%s)\n", tyname(buf, sizeof(buf), tn->tn_type)));
485 if (initerr || tn == NULL)
491 * Do not test for automatic aggregate initialisation. If the
492 * initializer starts with a brace we have the warning already.
493 * If not, an error will be printed that the initializer must
494 * be enclosed by braces.
498 * Local initialisation of non-array-types with only one expression
499 * without braces is done by ASSIGN
501 if ((sc == AUTO || sc == REG) &&
502 initsym->s_type->t_tspec != ARRAY && initstk->i_nxt == NULL) {
503 ln = getnnode(initsym, 0);
504 ln->tn_type = tduptyp(ln->tn_type);
505 ln->tn_type->t_const = 0;
506 tn = build(ASSIGN, ln, tn);
512 * Remove all entries which cannot be used for further initializers
513 * and do not require a closing brace.
517 /* Initialisations by strings are done in strginit(). */
522 if (initerr || tn == NULL)
526 DPRINTF(("mkinit() cnt=%d tn=%p\n", initstk->i_cnt, tn));
527 /* Create a temporary node for the left side. */
528 ln = tgetblk(sizeof (tnode_t));
530 ln->tn_type = tduptyp(initstk->i_type);
531 ln->tn_type->t_const = 0;
533 ln->tn_sym = initsym; /* better than nothing */
537 lt = ln->tn_type->t_tspec;
538 rt = tn->tn_type->t_tspec;
543 if (!typeok(INIT, 0, ln, tn))
547 * Store the tree memory. This is necessary because otherwise
548 * expr() would free it.
554 if (isityp(lt) && ln->tn_type->t_isfield && !isityp(rt)) {
556 * Bit-fields can be initialized in trad. C only by integer
560 /* bit-field initialisation is illegal in trad. C */
564 if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON))
565 tn = convert(INIT, 0, initstk->i_type, tn);
567 if (tn != NULL && tn->tn_op != CON) {
570 if (conaddr(tn, &sym, &offs) == -1) {
571 if (sc == AUTO || sc == REG) {
572 /* non-constant initializer */
575 /* non-constant initializer */
583 * We only free the block, if we are not a compound declaration
584 * We know that the only symbols that start with a digit are the
585 * ones we allocate with mktempsym() for compound declarations
587 if (!isdigit((unsigned char)initsym->s_name[0]))
593 strginit(tnode_t *tn)
600 if (tn->tn_op != STRING)
607 * Check if we have an array type which can be initialized by
610 if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
611 t = istk->i_subt->t_subt->t_tspec;
612 if (!((strg->st_tspec == CHAR &&
613 (t == CHAR || t == UCHAR || t == SCHAR)) ||
614 (strg->st_tspec == WCHAR && t == WCHAR))) {
617 /* Put the array at top of stack */
620 } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
621 t = istk->i_type->t_subt->t_tspec;
622 if (!((strg->st_tspec == CHAR &&
623 (t == CHAR || t == UCHAR || t == SCHAR)) ||
624 (strg->st_tspec == WCHAR && t == WCHAR))) {
628 * If the array is already partly initialized, we are
631 if (istk->i_cnt != istk->i_type->t_dim)
637 /* Get length without trailing NUL character. */
640 if (istk->i_nolimit) {
642 istk->i_type->t_dim = len + 1;
643 /* from now complete type */
644 setcompl(istk->i_type, 0);
646 if (istk->i_type->t_dim < len) {
647 /* non-null byte ignored in string initializer */
652 /* In every case the array is initialized completely. */