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