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