init.c revision 1.8 1 1.8 wiz /* $NetBSD: init.c,v 1.8 2001/09/16 16:34:44 wiz 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.5 christos #include <sys/cdefs.h>
35 1.1 cgd #ifndef lint
36 1.8 wiz __RCSID("$NetBSD: init.c,v 1.8 2001/09/16 16:34:44 wiz Exp $");
37 1.1 cgd #endif
38 1.1 cgd
39 1.1 cgd #include <stdlib.h>
40 1.1 cgd
41 1.1 cgd #include "lint1.h"
42 1.1 cgd
43 1.1 cgd /*
44 1.8 wiz * initerr is set as soon as a fatal error occurred in an initialisation.
45 1.1 cgd * The effect is that the rest of the initialisation is ignored (parsed
46 1.1 cgd * by yacc, expression trees built, but no initialisation takes place).
47 1.1 cgd */
48 1.1 cgd int initerr;
49 1.1 cgd
50 1.1 cgd /* Pointer to the symbol which is to be initialized. */
51 1.1 cgd sym_t *initsym;
52 1.1 cgd
53 1.1 cgd /* Points to the top element of the initialisation stack. */
54 1.1 cgd istk_t *initstk;
55 1.1 cgd
56 1.1 cgd
57 1.7 lukem static void popi2(void);
58 1.7 lukem static void popinit(int);
59 1.7 lukem static void pushinit(void);
60 1.7 lukem static void testinit(void);
61 1.7 lukem static void nextinit(int);
62 1.7 lukem static int strginit(tnode_t *);
63 1.1 cgd
64 1.1 cgd
65 1.1 cgd /*
66 1.1 cgd * Initialize the initialisation stack by putting an entry for the variable
67 1.1 cgd * which is to be initialized on it.
68 1.1 cgd */
69 1.1 cgd void
70 1.7 lukem prepinit(void)
71 1.1 cgd {
72 1.1 cgd istk_t *istk;
73 1.1 cgd
74 1.1 cgd if (initerr)
75 1.1 cgd return;
76 1.1 cgd
77 1.1 cgd /* free memory used in last initialisation */
78 1.1 cgd while ((istk = initstk) != NULL) {
79 1.1 cgd initstk = istk->i_nxt;
80 1.1 cgd free(istk);
81 1.1 cgd }
82 1.1 cgd
83 1.1 cgd /*
84 1.1 cgd * If the type which is to be initialized is an incomplete type,
85 1.1 cgd * it must be duplicated.
86 1.1 cgd */
87 1.1 cgd if (initsym->s_type->t_tspec == ARRAY && incompl(initsym->s_type))
88 1.1 cgd initsym->s_type = duptyp(initsym->s_type);
89 1.1 cgd
90 1.1 cgd istk = initstk = xcalloc(1, sizeof (istk_t));
91 1.1 cgd istk->i_subt = initsym->s_type;
92 1.1 cgd istk->i_cnt = 1;
93 1.1 cgd
94 1.1 cgd }
95 1.1 cgd
96 1.1 cgd static void
97 1.7 lukem popi2(void)
98 1.1 cgd {
99 1.1 cgd istk_t *istk;
100 1.1 cgd sym_t *m;
101 1.1 cgd
102 1.1 cgd initstk = (istk = initstk)->i_nxt;
103 1.1 cgd if (initstk == NULL)
104 1.1 cgd lerror("popi2() 1");
105 1.1 cgd free(istk);
106 1.1 cgd
107 1.1 cgd istk = initstk;
108 1.1 cgd
109 1.1 cgd istk->i_cnt--;
110 1.1 cgd if (istk->i_cnt < 0)
111 1.1 cgd lerror("popi2() 3");
112 1.1 cgd
113 1.1 cgd /*
114 1.1 cgd * If the removed element was a structure member, we must go
115 1.1 cgd * to the next structure member.
116 1.1 cgd */
117 1.1 cgd if (istk->i_cnt > 0 && istk->i_type->t_tspec == STRUCT) {
118 1.1 cgd do {
119 1.1 cgd m = istk->i_mem = istk->i_mem->s_nxt;
120 1.1 cgd if (m == NULL)
121 1.1 cgd lerror("popi2() 2");
122 1.1 cgd } while (m->s_field && m->s_name == unnamed);
123 1.1 cgd istk->i_subt = m->s_type;
124 1.1 cgd }
125 1.1 cgd }
126 1.1 cgd
127 1.1 cgd static void
128 1.7 lukem popinit(int brace)
129 1.1 cgd {
130 1.7 lukem
131 1.1 cgd if (brace) {
132 1.1 cgd /*
133 1.1 cgd * Take all entries, including the first which requires
134 1.1 cgd * a closing brace, from the stack.
135 1.1 cgd */
136 1.1 cgd do {
137 1.1 cgd brace = initstk->i_brace;
138 1.1 cgd popi2();
139 1.1 cgd } while (!brace);
140 1.1 cgd } else {
141 1.1 cgd /*
142 1.1 cgd * Take all entries which cannot be used for further
143 1.1 cgd * initializers from the stack, but do this only if
144 1.1 cgd * they do not require a closing brace.
145 1.1 cgd */
146 1.1 cgd while (!initstk->i_brace &&
147 1.1 cgd initstk->i_cnt == 0 && !initstk->i_nolimit) {
148 1.1 cgd popi2();
149 1.1 cgd }
150 1.1 cgd }
151 1.1 cgd }
152 1.1 cgd
153 1.1 cgd static void
154 1.7 lukem pushinit(void)
155 1.1 cgd {
156 1.1 cgd istk_t *istk;
157 1.1 cgd int cnt;
158 1.1 cgd sym_t *m;
159 1.1 cgd
160 1.1 cgd istk = initstk;
161 1.1 cgd
162 1.1 cgd /* Extend an incomplete array type by one element */
163 1.1 cgd if (istk->i_cnt == 0) {
164 1.1 cgd /*
165 1.1 cgd * Inside of other aggregate types must not be an incomplete
166 1.1 cgd * type.
167 1.1 cgd */
168 1.1 cgd if (istk->i_nxt->i_nxt != NULL)
169 1.1 cgd lerror("pushinit() 1");
170 1.1 cgd istk->i_cnt = 1;
171 1.1 cgd if (istk->i_type->t_tspec != ARRAY)
172 1.1 cgd lerror("pushinit() 2");
173 1.1 cgd istk->i_type->t_dim++;
174 1.1 cgd /* from now its an complete type */
175 1.1 cgd setcompl(istk->i_type, 0);
176 1.1 cgd }
177 1.1 cgd
178 1.1 cgd if (istk->i_cnt <= 0)
179 1.1 cgd lerror("pushinit() 3");
180 1.1 cgd if (istk->i_type != NULL && issclt(istk->i_type->t_tspec))
181 1.1 cgd lerror("pushinit() 4");
182 1.1 cgd
183 1.1 cgd initstk = xcalloc(1, sizeof (istk_t));
184 1.1 cgd initstk->i_nxt = istk;
185 1.1 cgd initstk->i_type = istk->i_subt;
186 1.1 cgd if (initstk->i_type->t_tspec == FUNC)
187 1.1 cgd lerror("pushinit() 5");
188 1.1 cgd
189 1.1 cgd istk = initstk;
190 1.1 cgd
191 1.1 cgd switch (istk->i_type->t_tspec) {
192 1.1 cgd case ARRAY:
193 1.1 cgd if (incompl(istk->i_type) && istk->i_nxt->i_nxt != NULL) {
194 1.1 cgd /* initialisation of an incomplete type */
195 1.1 cgd error(175);
196 1.1 cgd initerr = 1;
197 1.1 cgd return;
198 1.1 cgd }
199 1.1 cgd istk->i_subt = istk->i_type->t_subt;
200 1.1 cgd istk->i_nolimit = incompl(istk->i_type);
201 1.1 cgd istk->i_cnt = istk->i_type->t_dim;
202 1.1 cgd break;
203 1.1 cgd case UNION:
204 1.1 cgd if (tflag)
205 1.1 cgd /* initialisation of union is illegal in trad. C */
206 1.1 cgd warning(238);
207 1.1 cgd /* FALLTHROUGH */
208 1.1 cgd case STRUCT:
209 1.1 cgd if (incompl(istk->i_type)) {
210 1.1 cgd /* initialisation of an incomplete type */
211 1.1 cgd error(175);
212 1.1 cgd initerr = 1;
213 1.1 cgd return;
214 1.1 cgd }
215 1.1 cgd cnt = 0;
216 1.1 cgd for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
217 1.1 cgd if (m->s_field && m->s_name == unnamed)
218 1.1 cgd continue;
219 1.1 cgd if (++cnt == 1) {
220 1.1 cgd istk->i_mem = m;
221 1.1 cgd istk->i_subt = m->s_type;
222 1.1 cgd }
223 1.1 cgd }
224 1.1 cgd if (cnt == 0) {
225 1.1 cgd /* cannot init. struct/union with no named member */
226 1.1 cgd error(179);
227 1.1 cgd initerr = 1;
228 1.1 cgd return;
229 1.1 cgd }
230 1.1 cgd istk->i_cnt = istk->i_type->t_tspec == STRUCT ? cnt : 1;
231 1.1 cgd break;
232 1.1 cgd default:
233 1.1 cgd istk->i_cnt = 1;
234 1.1 cgd break;
235 1.1 cgd }
236 1.1 cgd }
237 1.1 cgd
238 1.1 cgd static void
239 1.7 lukem testinit(void)
240 1.1 cgd {
241 1.1 cgd istk_t *istk;
242 1.1 cgd
243 1.1 cgd istk = initstk;
244 1.1 cgd
245 1.1 cgd /*
246 1.1 cgd * If a closing brace is expected we have at least one initializer
247 1.1 cgd * too much.
248 1.1 cgd */
249 1.1 cgd if (istk->i_cnt == 0 && !istk->i_nolimit) {
250 1.1 cgd switch (istk->i_type->t_tspec) {
251 1.1 cgd case ARRAY:
252 1.1 cgd /* too many array initializers */
253 1.1 cgd error(173);
254 1.1 cgd break;
255 1.1 cgd case STRUCT:
256 1.1 cgd case UNION:
257 1.1 cgd /* too many struct/union initializers */
258 1.1 cgd error(172);
259 1.1 cgd break;
260 1.1 cgd default:
261 1.1 cgd /* too many initializers */
262 1.1 cgd error(174);
263 1.1 cgd break;
264 1.1 cgd }
265 1.1 cgd initerr = 1;
266 1.1 cgd }
267 1.1 cgd }
268 1.1 cgd
269 1.1 cgd static void
270 1.7 lukem nextinit(int brace)
271 1.1 cgd {
272 1.7 lukem
273 1.1 cgd if (!brace) {
274 1.1 cgd if (initstk->i_type == NULL &&
275 1.1 cgd !issclt(initstk->i_subt->t_tspec)) {
276 1.1 cgd /* {}-enclosed initializer required */
277 1.1 cgd error(181);
278 1.1 cgd }
279 1.1 cgd /*
280 1.1 cgd * Make sure an entry with a scalar type is at the top
281 1.1 cgd * of the stack.
282 1.1 cgd */
283 1.1 cgd if (!initerr)
284 1.1 cgd testinit();
285 1.1 cgd while (!initerr && (initstk->i_type == NULL ||
286 1.1 cgd !issclt(initstk->i_type->t_tspec))) {
287 1.1 cgd if (!initerr)
288 1.1 cgd pushinit();
289 1.1 cgd }
290 1.1 cgd } else {
291 1.1 cgd if (initstk->i_type != NULL &&
292 1.1 cgd issclt(initstk->i_type->t_tspec)) {
293 1.1 cgd /* invalid initializer */
294 1.1 cgd error(176);
295 1.1 cgd initerr = 1;
296 1.1 cgd }
297 1.1 cgd if (!initerr)
298 1.1 cgd testinit();
299 1.1 cgd if (!initerr)
300 1.1 cgd pushinit();
301 1.1 cgd if (!initerr)
302 1.1 cgd initstk->i_brace = 1;
303 1.1 cgd }
304 1.1 cgd }
305 1.1 cgd
306 1.1 cgd void
307 1.7 lukem initlbr(void)
308 1.1 cgd {
309 1.7 lukem
310 1.1 cgd if (initerr)
311 1.1 cgd return;
312 1.1 cgd
313 1.1 cgd if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
314 1.1 cgd initstk->i_nxt == NULL) {
315 1.1 cgd if (tflag && !issclt(initstk->i_subt->t_tspec))
316 1.1 cgd /* no automatic aggregate initialization in trad. C*/
317 1.1 cgd warning(188);
318 1.1 cgd }
319 1.1 cgd
320 1.1 cgd /*
321 1.1 cgd * Remove all entries which cannot be used for further initializers
322 1.1 cgd * and do not expect a closing brace.
323 1.1 cgd */
324 1.1 cgd popinit(0);
325 1.1 cgd
326 1.1 cgd nextinit(1);
327 1.1 cgd }
328 1.1 cgd
329 1.1 cgd void
330 1.7 lukem initrbr(void)
331 1.1 cgd {
332 1.7 lukem
333 1.1 cgd if (initerr)
334 1.1 cgd return;
335 1.1 cgd
336 1.1 cgd popinit(1);
337 1.1 cgd }
338 1.1 cgd
339 1.1 cgd void
340 1.7 lukem mkinit(tnode_t *tn)
341 1.1 cgd {
342 1.1 cgd ptrdiff_t offs;
343 1.1 cgd sym_t *sym;
344 1.1 cgd tspec_t lt, rt;
345 1.1 cgd tnode_t *ln;
346 1.1 cgd struct mbl *tmem;
347 1.1 cgd scl_t sc;
348 1.1 cgd
349 1.1 cgd if (initerr || tn == NULL)
350 1.1 cgd goto end;
351 1.1 cgd
352 1.1 cgd sc = initsym->s_scl;
353 1.1 cgd
354 1.1 cgd /*
355 1.1 cgd * Do not test for automatic aggregat initialisation. If the
356 1.1 cgd * initalizer starts with a brace we have the warning already.
357 1.1 cgd * If not, an error will be printed that the initializer must
358 1.1 cgd * be enclosed by braces.
359 1.1 cgd */
360 1.1 cgd
361 1.1 cgd /*
362 1.1 cgd * Local initialisation of non-array-types with only one expression
363 1.1 cgd * without braces is done by ASSIGN
364 1.1 cgd */
365 1.1 cgd if ((sc == AUTO || sc == REG) &&
366 1.1 cgd initsym->s_type->t_tspec != ARRAY && initstk->i_nxt == NULL) {
367 1.1 cgd ln = getnnode(initsym, 0);
368 1.1 cgd ln->tn_type = tduptyp(ln->tn_type);
369 1.1 cgd ln->tn_type->t_const = 0;
370 1.1 cgd tn = build(ASSIGN, ln, tn);
371 1.1 cgd expr(tn, 0, 0);
372 1.1 cgd goto end;
373 1.1 cgd }
374 1.1 cgd
375 1.1 cgd /*
376 1.1 cgd * Remove all entries which cannot be used for further initializers
377 1.1 cgd * and do not require a closing brace.
378 1.1 cgd */
379 1.1 cgd popinit(0);
380 1.1 cgd
381 1.1 cgd /* Initialisations by strings are done in strginit(). */
382 1.1 cgd if (strginit(tn))
383 1.1 cgd goto end;
384 1.1 cgd
385 1.1 cgd nextinit(0);
386 1.1 cgd if (initerr || tn == NULL)
387 1.1 cgd goto end;
388 1.1 cgd
389 1.1 cgd initstk->i_cnt--;
390 1.1 cgd
391 1.1 cgd /* Create a temporary node for the left side. */
392 1.1 cgd ln = tgetblk(sizeof (tnode_t));
393 1.1 cgd ln->tn_op = NAME;
394 1.1 cgd ln->tn_type = tduptyp(initstk->i_type);
395 1.1 cgd ln->tn_type->t_const = 0;
396 1.1 cgd ln->tn_lvalue = 1;
397 1.1 cgd ln->tn_sym = initsym; /* better than nothing */
398 1.1 cgd
399 1.1 cgd tn = cconv(tn);
400 1.1 cgd
401 1.1 cgd lt = ln->tn_type->t_tspec;
402 1.1 cgd rt = tn->tn_type->t_tspec;
403 1.1 cgd
404 1.1 cgd if (!issclt(lt))
405 1.1 cgd lerror("mkinit() 1");
406 1.1 cgd
407 1.1 cgd if (!typeok(INIT, 0, ln, tn))
408 1.1 cgd goto end;
409 1.1 cgd
410 1.1 cgd /*
411 1.1 cgd * Store the tree memory. This is nessesary because otherwise
412 1.1 cgd * expr() would free it.
413 1.1 cgd */
414 1.1 cgd tmem = tsave();
415 1.1 cgd expr(tn, 1, 0);
416 1.1 cgd trestor(tmem);
417 1.7 lukem
418 1.1 cgd if (isityp(lt) && ln->tn_type->t_isfield && !isityp(rt)) {
419 1.1 cgd /*
420 1.1 cgd * Bit-fields can be initialized in trad. C only by integer
421 1.1 cgd * constants.
422 1.1 cgd */
423 1.1 cgd if (tflag)
424 1.1 cgd /* bit-field initialisation is illegal in trad. C */
425 1.1 cgd warning(186);
426 1.1 cgd }
427 1.1 cgd
428 1.1 cgd if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON))
429 1.1 cgd tn = convert(INIT, 0, initstk->i_type, tn);
430 1.1 cgd
431 1.1 cgd if (tn != NULL && tn->tn_op != CON) {
432 1.1 cgd sym = NULL;
433 1.1 cgd offs = 0;
434 1.1 cgd if (conaddr(tn, &sym, &offs) == -1) {
435 1.3 jpo if (sc == AUTO || sc == REG) {
436 1.1 cgd /* non-constant initializer */
437 1.4 jpo (void)gnuism(177);
438 1.1 cgd } else {
439 1.1 cgd /* non-constant initializer */
440 1.1 cgd error(177);
441 1.1 cgd }
442 1.1 cgd }
443 1.1 cgd }
444 1.1 cgd
445 1.1 cgd end:
446 1.1 cgd tfreeblk();
447 1.1 cgd }
448 1.1 cgd
449 1.1 cgd
450 1.1 cgd static int
451 1.7 lukem strginit(tnode_t *tn)
452 1.1 cgd {
453 1.1 cgd tspec_t t;
454 1.1 cgd istk_t *istk;
455 1.1 cgd int len;
456 1.1 cgd strg_t *strg;
457 1.1 cgd
458 1.1 cgd if (tn->tn_op != STRING)
459 1.1 cgd return (0);
460 1.1 cgd
461 1.1 cgd istk = initstk;
462 1.1 cgd strg = tn->tn_strg;
463 1.1 cgd
464 1.1 cgd /*
465 1.1 cgd * Check if we have an array type which can be initialized by
466 1.1 cgd * the string.
467 1.1 cgd */
468 1.1 cgd if (istk->i_subt->t_tspec == ARRAY) {
469 1.1 cgd t = istk->i_subt->t_subt->t_tspec;
470 1.1 cgd if (!((strg->st_tspec == CHAR &&
471 1.1 cgd (t == CHAR || t == UCHAR || t == SCHAR)) ||
472 1.1 cgd (strg->st_tspec == WCHAR && t == WCHAR))) {
473 1.1 cgd return (0);
474 1.1 cgd }
475 1.1 cgd /* Put the array at top of stack */
476 1.1 cgd pushinit();
477 1.1 cgd istk = initstk;
478 1.1 cgd } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
479 1.1 cgd t = istk->i_type->t_subt->t_tspec;
480 1.1 cgd if (!((strg->st_tspec == CHAR &&
481 1.1 cgd (t == CHAR || t == UCHAR || t == SCHAR)) ||
482 1.1 cgd (strg->st_tspec == WCHAR && t == WCHAR))) {
483 1.1 cgd return (0);
484 1.1 cgd }
485 1.1 cgd /*
486 1.1 cgd * If the array is already partly initialized, we are
487 1.1 cgd * wrong here.
488 1.1 cgd */
489 1.1 cgd if (istk->i_cnt != istk->i_type->t_dim)
490 1.1 cgd return (0);
491 1.1 cgd } else {
492 1.1 cgd return (0);
493 1.1 cgd }
494 1.1 cgd
495 1.1 cgd /* Get length without trailing NUL character. */
496 1.1 cgd len = strg->st_len;
497 1.1 cgd
498 1.1 cgd if (istk->i_nolimit) {
499 1.1 cgd istk->i_nolimit = 0;
500 1.1 cgd istk->i_type->t_dim = len + 1;
501 1.1 cgd /* from now complete type */
502 1.1 cgd setcompl(istk->i_type, 0);
503 1.1 cgd } else {
504 1.1 cgd if (istk->i_type->t_dim < len) {
505 1.1 cgd /* non-null byte ignored in string initializer */
506 1.1 cgd warning(187);
507 1.1 cgd }
508 1.1 cgd }
509 1.1 cgd
510 1.1 cgd /* In every case the array is initialized completely. */
511 1.1 cgd istk->i_cnt = 0;
512 1.1 cgd
513 1.1 cgd return (1);
514 1.1 cgd }
515