init.c revision 1.5 1 1.5 christos /* $NetBSD: init.c,v 1.5 1998/02/22 15:40:40 christos 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.5 christos __RCSID("$NetBSD");
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.1 cgd * initerr is set as soon as a fatal error occured 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.1 cgd static void popi2 __P((void));
58 1.1 cgd static void popinit __P((int));
59 1.1 cgd static void pushinit __P((void));
60 1.1 cgd static void testinit __P((void));
61 1.1 cgd static void nextinit __P((int));
62 1.1 cgd static int strginit __P((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.1 cgd prepinit()
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.1 cgd popi2()
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.1 cgd popinit(brace)
129 1.1 cgd int brace;
130 1.1 cgd {
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.1 cgd pushinit()
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.1 cgd testinit()
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.1 cgd nextinit(brace)
271 1.1 cgd int brace;
272 1.1 cgd {
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.1 cgd initlbr()
308 1.1 cgd {
309 1.1 cgd if (initerr)
310 1.1 cgd return;
311 1.1 cgd
312 1.1 cgd if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
313 1.1 cgd initstk->i_nxt == NULL) {
314 1.1 cgd if (tflag && !issclt(initstk->i_subt->t_tspec))
315 1.1 cgd /* no automatic aggregate initialization in trad. C*/
316 1.1 cgd warning(188);
317 1.1 cgd }
318 1.1 cgd
319 1.1 cgd /*
320 1.1 cgd * Remove all entries which cannot be used for further initializers
321 1.1 cgd * and do not expect a closing brace.
322 1.1 cgd */
323 1.1 cgd popinit(0);
324 1.1 cgd
325 1.1 cgd nextinit(1);
326 1.1 cgd }
327 1.1 cgd
328 1.1 cgd void
329 1.1 cgd initrbr()
330 1.1 cgd {
331 1.1 cgd if (initerr)
332 1.1 cgd return;
333 1.1 cgd
334 1.1 cgd popinit(1);
335 1.1 cgd }
336 1.1 cgd
337 1.1 cgd void
338 1.1 cgd mkinit(tn)
339 1.1 cgd tnode_t *tn;
340 1.1 cgd {
341 1.1 cgd ptrdiff_t offs;
342 1.1 cgd sym_t *sym;
343 1.1 cgd tspec_t lt, rt;
344 1.1 cgd tnode_t *ln;
345 1.1 cgd struct mbl *tmem;
346 1.1 cgd scl_t sc;
347 1.1 cgd
348 1.1 cgd if (initerr || tn == NULL)
349 1.1 cgd goto end;
350 1.1 cgd
351 1.1 cgd sc = initsym->s_scl;
352 1.1 cgd
353 1.1 cgd /*
354 1.1 cgd * Do not test for automatic aggregat initialisation. If the
355 1.1 cgd * initalizer starts with a brace we have the warning already.
356 1.1 cgd * If not, an error will be printed that the initializer must
357 1.1 cgd * be enclosed by braces.
358 1.1 cgd */
359 1.1 cgd
360 1.1 cgd /*
361 1.1 cgd * Local initialisation of non-array-types with only one expression
362 1.1 cgd * without braces is done by ASSIGN
363 1.1 cgd */
364 1.1 cgd if ((sc == AUTO || sc == REG) &&
365 1.1 cgd initsym->s_type->t_tspec != ARRAY && initstk->i_nxt == NULL) {
366 1.1 cgd ln = getnnode(initsym, 0);
367 1.1 cgd ln->tn_type = tduptyp(ln->tn_type);
368 1.1 cgd ln->tn_type->t_const = 0;
369 1.1 cgd tn = build(ASSIGN, ln, tn);
370 1.1 cgd expr(tn, 0, 0);
371 1.1 cgd goto end;
372 1.1 cgd }
373 1.1 cgd
374 1.1 cgd /*
375 1.1 cgd * Remove all entries which cannot be used for further initializers
376 1.1 cgd * and do not require a closing brace.
377 1.1 cgd */
378 1.1 cgd popinit(0);
379 1.1 cgd
380 1.1 cgd /* Initialisations by strings are done in strginit(). */
381 1.1 cgd if (strginit(tn))
382 1.1 cgd goto end;
383 1.1 cgd
384 1.1 cgd nextinit(0);
385 1.1 cgd if (initerr || tn == NULL)
386 1.1 cgd goto end;
387 1.1 cgd
388 1.1 cgd initstk->i_cnt--;
389 1.1 cgd
390 1.1 cgd /* Create a temporary node for the left side. */
391 1.1 cgd ln = tgetblk(sizeof (tnode_t));
392 1.1 cgd ln->tn_op = NAME;
393 1.1 cgd ln->tn_type = tduptyp(initstk->i_type);
394 1.1 cgd ln->tn_type->t_const = 0;
395 1.1 cgd ln->tn_lvalue = 1;
396 1.1 cgd ln->tn_sym = initsym; /* better than nothing */
397 1.1 cgd
398 1.1 cgd tn = cconv(tn);
399 1.1 cgd
400 1.1 cgd lt = ln->tn_type->t_tspec;
401 1.1 cgd rt = tn->tn_type->t_tspec;
402 1.1 cgd
403 1.1 cgd if (!issclt(lt))
404 1.1 cgd lerror("mkinit() 1");
405 1.1 cgd
406 1.1 cgd if (!typeok(INIT, 0, ln, tn))
407 1.1 cgd goto end;
408 1.1 cgd
409 1.1 cgd /*
410 1.1 cgd * Store the tree memory. This is nessesary because otherwise
411 1.1 cgd * expr() would free it.
412 1.1 cgd */
413 1.1 cgd tmem = tsave();
414 1.1 cgd expr(tn, 1, 0);
415 1.1 cgd trestor(tmem);
416 1.1 cgd
417 1.1 cgd if (isityp(lt) && ln->tn_type->t_isfield && !isityp(rt)) {
418 1.1 cgd /*
419 1.1 cgd * Bit-fields can be initialized in trad. C only by integer
420 1.1 cgd * constants.
421 1.1 cgd */
422 1.1 cgd if (tflag)
423 1.1 cgd /* bit-field initialisation is illegal in trad. C */
424 1.1 cgd warning(186);
425 1.1 cgd }
426 1.1 cgd
427 1.1 cgd if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON))
428 1.1 cgd tn = convert(INIT, 0, initstk->i_type, tn);
429 1.1 cgd
430 1.1 cgd if (tn != NULL && tn->tn_op != CON) {
431 1.1 cgd sym = NULL;
432 1.1 cgd offs = 0;
433 1.1 cgd if (conaddr(tn, &sym, &offs) == -1) {
434 1.3 jpo if (sc == AUTO || sc == REG) {
435 1.1 cgd /* non-constant initializer */
436 1.4 jpo (void)gnuism(177);
437 1.1 cgd } else {
438 1.1 cgd /* non-constant initializer */
439 1.1 cgd error(177);
440 1.1 cgd }
441 1.1 cgd }
442 1.1 cgd }
443 1.1 cgd
444 1.1 cgd end:
445 1.1 cgd tfreeblk();
446 1.1 cgd }
447 1.1 cgd
448 1.1 cgd
449 1.1 cgd static int
450 1.1 cgd strginit(tn)
451 1.1 cgd 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