init.c revision 1.30 1 /* $NetBSD: init.c,v 1.30 2020/12/28 19:02:16 rillig 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 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
37
38 #include <sys/cdefs.h>
39 #if defined(__RCSID) && !defined(lint)
40 __RCSID("$NetBSD: init.c,v 1.30 2020/12/28 19:02:16 rillig Exp $");
41 #endif
42
43 #include <stdlib.h>
44 #include <string.h>
45 #include <ctype.h>
46
47 #include "lint1.h"
48
49 /*
50 * initerr is set as soon as a fatal error occurred in an initialisation.
51 * The effect is that the rest of the initialisation is ignored (parsed
52 * by yacc, expression trees built, but no initialisation takes place).
53 */
54 int initerr;
55
56 /* Pointer to the symbol which is to be initialized. */
57 sym_t *initsym;
58
59 /* Points to the top element of the initialisation stack. */
60 istk_t *initstk;
61
62 typedef struct namlist {
63 const char *n_name;
64 struct namlist *n_prev;
65 struct namlist *n_next;
66 } namlist_t;
67
68 /* Points to a c9x named member; */
69 namlist_t *namedmem = NULL;
70
71
72 static void popi2(void);
73 static void popinit(int);
74 static void pushinit(void);
75 static void testinit(void);
76 static void nextinit(int);
77 static int strginit(tnode_t *);
78 static void memberpop(void);
79
80 #ifndef DEBUG
81 #define DPRINTF(a)
82 #else
83 #define DPRINTF(a) printf a
84 #endif
85
86 void
87 memberpush(sb)
88 sbuf_t *sb;
89 {
90 namlist_t *nam = xcalloc(1, sizeof (namlist_t));
91 nam->n_name = sb->sb_name;
92 DPRINTF(("%s: %s %p\n", __func__, nam->n_name, nam));
93 if (namedmem == NULL) {
94 nam->n_prev = nam->n_next = nam;
95 namedmem = nam;
96 } else {
97 namedmem->n_prev->n_next = nam;
98 nam->n_prev = namedmem->n_prev;
99 nam->n_next = namedmem;
100 namedmem->n_prev = nam;
101 }
102 }
103
104 static void
105 memberpop(void)
106 {
107 DPRINTF(("%s: %s %p\n", __func__, namedmem->n_name, namedmem));
108 if (namedmem->n_next == namedmem) {
109 free(namedmem);
110 namedmem = NULL;
111 } else {
112 namlist_t *nam = namedmem;
113 namedmem = namedmem->n_next;
114 namedmem->n_next = nam->n_next;
115 namedmem->n_prev = nam->n_prev;
116 free(nam);
117 }
118 }
119
120
121 /*
122 * Initialize the initialisation stack by putting an entry for the variable
123 * which is to be initialized on it.
124 */
125 void
126 prepinit(void)
127 {
128 istk_t *istk;
129
130 if (initerr)
131 return;
132
133 /* free memory used in last initialisation */
134 while ((istk = initstk) != NULL) {
135 initstk = istk->i_nxt;
136 free(istk);
137 }
138
139 /*
140 * If the type which is to be initialized is an incomplete type,
141 * it must be duplicated.
142 */
143 if (initsym->s_type->t_tspec == ARRAY && incompl(initsym->s_type))
144 initsym->s_type = duptyp(initsym->s_type);
145
146 istk = initstk = xcalloc(1, sizeof (istk_t));
147 istk->i_subt = initsym->s_type;
148 istk->i_cnt = 1;
149
150 }
151
152 static void
153 popi2(void)
154 {
155 #ifdef DEBUG
156 char buf[64];
157 #endif
158 istk_t *istk;
159 sym_t *m;
160
161 DPRINTF(("%s+(%s): brace=%d count=%d namedmem %d\n", __func__,
162 tyname(buf, sizeof(buf),
163 initstk->i_type ? initstk->i_type : initstk->i_subt),
164 initstk->i_brace, initstk->i_cnt, initstk->i_namedmem));
165 initstk = (istk = initstk)->i_nxt;
166 free(istk);
167
168 istk = initstk;
169 if (istk == NULL)
170 LERROR("popi2()");
171
172 DPRINTF(("%s-(%s): brace=%d count=%d namedmem %d\n", __func__,
173 tyname(buf, sizeof(buf),
174 initstk->i_type ? initstk->i_type : initstk->i_subt),
175 initstk->i_brace, initstk->i_cnt, initstk->i_namedmem));
176
177 istk->i_cnt--;
178 if (istk->i_cnt < 0)
179 LERROR("popi2()");
180
181 DPRINTF(("%s(): %d %s\n", __func__, istk->i_cnt,
182 namedmem ? namedmem->n_name : "*null*"));
183 if (istk->i_cnt >= 0 && namedmem != NULL) {
184 DPRINTF(("%s(): %d %s %s\n", __func__, istk->i_cnt,
185 tyname(buf, sizeof(buf), istk->i_type), namedmem->n_name));
186 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
187 DPRINTF(("%s(): pop [%s %s]\n", __func__,
188 namedmem->n_name, m->s_name));
189 if (m->s_field && m->s_name == unnamed)
190 continue;
191 if (strcmp(m->s_name, namedmem->n_name) == 0) {
192 istk->i_subt = m->s_type;
193 istk->i_cnt++;
194 memberpop();
195 return;
196 }
197 }
198 error(101, namedmem->n_name);
199 DPRINTF(("%s(): namedmem %s\n", __func__, namedmem->n_name));
200 memberpop();
201 istk->i_namedmem = 1;
202 return;
203 }
204 /*
205 * If the removed element was a structure member, we must go
206 * to the next structure member.
207 */
208 if (istk->i_cnt > 0 && istk->i_type->t_tspec == STRUCT &&
209 !istk->i_namedmem) {
210 do {
211 m = istk->i_mem = istk->i_mem->s_nxt;
212 if (m == NULL)
213 LERROR("popi2()");
214 DPRINTF(("%s(): pop %s\n", __func__, m->s_name));
215 } while (m->s_field && m->s_name == unnamed);
216 istk->i_subt = m->s_type;
217 }
218 }
219
220 static void
221 popinit(int brace)
222 {
223 DPRINTF(("%s(%d)\n", __func__, brace));
224
225 if (brace) {
226 /*
227 * Take all entries, including the first which requires
228 * a closing brace, from the stack.
229 */
230 DPRINTF(("%s: brace\n", __func__));
231 do {
232 brace = initstk->i_brace;
233 DPRINTF(("%s: loop brace %d\n", __func__, brace));
234 popi2();
235 } while (!brace);
236 DPRINTF(("%s: brace done\n", __func__));
237 } else {
238 /*
239 * Take all entries which cannot be used for further
240 * initializers from the stack, but do this only if
241 * they do not require a closing brace.
242 */
243 DPRINTF(("%s: no brace\n", __func__));
244 while (!initstk->i_brace &&
245 initstk->i_cnt == 0 && !initstk->i_nolimit) {
246 popi2();
247 }
248 DPRINTF(("%s: no brace done\n", __func__));
249 }
250 }
251
252 static void
253 pushinit(void)
254 {
255 #ifdef DEBUG
256 char buf[64];
257 #endif
258 istk_t *istk, *inxt;
259 int cnt;
260 sym_t *m;
261
262 istk = initstk;
263
264 /* Extend an incomplete array type by one element */
265 if (istk->i_cnt == 0) {
266 DPRINTF(("%s(extend) %s\n", __func__,
267 tyname(buf, sizeof(buf), istk->i_type)));
268 /*
269 * Inside of other aggregate types must not be an incomplete
270 * type.
271 */
272 if (istk->i_nxt->i_nxt != NULL)
273 LERROR("pushinit()");
274 istk->i_cnt = 1;
275 if (istk->i_type->t_tspec != ARRAY)
276 LERROR("pushinit()");
277 istk->i_type->t_dim++;
278 /* from now its an complete type */
279 setcompl(istk->i_type, 0);
280 }
281
282 if (istk->i_cnt <= 0)
283 LERROR("pushinit()");
284 if (istk->i_type != NULL && tspec_is_scalar(istk->i_type->t_tspec))
285 LERROR("pushinit()");
286
287 initstk = xcalloc(1, sizeof (istk_t));
288 initstk->i_nxt = istk;
289 initstk->i_type = istk->i_subt;
290 if (initstk->i_type->t_tspec == FUNC)
291 LERROR("pushinit()");
292
293 again:
294 istk = initstk;
295
296 DPRINTF(("%s(%s)\n", __func__, tyname(buf, sizeof(buf), istk->i_type)));
297 switch (istk->i_type->t_tspec) {
298 case ARRAY:
299 if (namedmem) {
300 DPRINTF(("%s: ARRAY %s brace=%d\n", __func__,
301 namedmem->n_name, istk->i_brace));
302 goto pop;
303 } else if (istk->i_nxt->i_namedmem) {
304 istk->i_brace = 1;
305 DPRINTF(("%s ARRAY brace=%d, namedmem=%d\n", __func__,
306 istk->i_brace, istk->i_nxt->i_namedmem));
307 }
308
309 if (incompl(istk->i_type) && istk->i_nxt->i_nxt != NULL) {
310 /* initialisation of an incomplete type */
311 error(175);
312 initerr = 1;
313 return;
314 }
315 istk->i_subt = istk->i_type->t_subt;
316 istk->i_nolimit = incompl(istk->i_type);
317 istk->i_cnt = istk->i_type->t_dim;
318 DPRINTF(("%s: elements array %s[%d] %s\n", __func__,
319 tyname(buf, sizeof(buf), istk->i_subt), istk->i_cnt,
320 namedmem ? namedmem->n_name : "*none*"));
321 break;
322 case UNION:
323 if (tflag)
324 /* initialisation of union is illegal in trad. C */
325 warning(238);
326 /* FALLTHROUGH */
327 case STRUCT:
328 if (incompl(istk->i_type)) {
329 /* initialisation of an incomplete type */
330 error(175);
331 initerr = 1;
332 return;
333 }
334 cnt = 0;
335 DPRINTF(("%s: 2. member lookup %s %s i_namedmem=%d\n", __func__,
336 tyname(buf, sizeof(buf), istk->i_type),
337 namedmem ? namedmem->n_name : "*none*", istk->i_namedmem));
338 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
339 if (m->s_field && m->s_name == unnamed)
340 continue;
341 if (namedmem != NULL) {
342 DPRINTF(("%s():[member:%s, looking:%s]\n",
343 __func__, m->s_name, namedmem->n_name));
344 if (strcmp(m->s_name, namedmem->n_name) == 0) {
345 cnt++;
346 break;
347 } else
348 continue;
349 }
350 if (++cnt == 1) {
351 istk->i_mem = m;
352 istk->i_subt = m->s_type;
353 }
354 }
355 if (namedmem != NULL) {
356 if (m == NULL) {
357 DPRINTF(("%s(): struct pop\n", __func__));
358 goto pop;
359 }
360 istk->i_mem = m;
361 istk->i_subt = m->s_type;
362 istk->i_namedmem = 1;
363 DPRINTF(("%s(): namedmem %s\n", __func__,
364 namedmem->n_name));
365 memberpop();
366 cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
367 }
368 istk->i_brace = 1;
369 DPRINTF(("%s(): %s brace=%d\n", __func__,
370 tyname(buf, sizeof(buf),
371 istk->i_type ? istk->i_type : istk->i_subt),
372 istk->i_brace));
373 if (cnt == 0) {
374 /* cannot init. struct/union with no named member */
375 error(179);
376 initerr = 1;
377 return;
378 }
379 istk->i_cnt = istk->i_type->t_tspec == STRUCT ? cnt : 1;
380 break;
381 default:
382 if (namedmem) {
383 DPRINTF(("%s(): pop\n", __func__));
384 pop:
385 inxt = initstk->i_nxt;
386 free(istk);
387 initstk = inxt;
388 goto again;
389 }
390 istk->i_cnt = 1;
391 break;
392 }
393 }
394
395 static void
396 testinit(void)
397 {
398 istk_t *istk;
399
400 istk = initstk;
401
402 /*
403 * If a closing brace is expected we have at least one initializer
404 * too much.
405 */
406 if (istk->i_cnt == 0 && !istk->i_nolimit && !istk->i_namedmem) {
407 switch (istk->i_type->t_tspec) {
408 case ARRAY:
409 /* too many array initializers */
410 error(173, istk->i_type->t_dim);
411 break;
412 case STRUCT:
413 case UNION:
414 /* too many struct/union initializers */
415 error(172);
416 break;
417 default:
418 /* too many initializers */
419 error(174);
420 break;
421 }
422 initerr = 1;
423 }
424 }
425
426 static void
427 nextinit(int brace)
428 {
429 char buf[64];
430
431 DPRINTF(("%s(%d)\n", __func__, brace));
432 if (!brace) {
433 if (initstk->i_type == NULL &&
434 !tspec_is_scalar(initstk->i_subt->t_tspec)) {
435 /* {}-enclosed initializer required */
436 error(181);
437 }
438 /*
439 * Make sure an entry with a scalar type is at the top
440 * of the stack.
441 */
442 if (!initerr)
443 testinit();
444 while (!initerr && (initstk->i_type == NULL ||
445 !tspec_is_scalar(
446 initstk->i_type->t_tspec))) {
447 if (!initerr)
448 pushinit();
449 }
450 } else {
451 if (initstk->i_type != NULL &&
452 tspec_is_scalar(initstk->i_type->t_tspec)) {
453 /* invalid initializer */
454 error(176, tyname(buf, sizeof(buf), initstk->i_type));
455 initerr = 1;
456 }
457 if (!initerr)
458 testinit();
459 if (!initerr)
460 pushinit();
461 if (!initerr) {
462 initstk->i_brace = 1;
463 DPRINTF(("%s(): %p %s brace=%d\n", __func__,
464 namedmem,
465 tyname(buf, sizeof(buf),
466 initstk->i_type ? initstk->i_type
467 : initstk->i_subt),
468 initstk->i_brace));
469 }
470 }
471 }
472
473 void
474 initlbr(void)
475 {
476 DPRINTF(("%s\n", __func__));
477
478 if (initerr)
479 return;
480
481 if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
482 initstk->i_nxt == NULL) {
483 if (tflag && !tspec_is_scalar(initstk->i_subt->t_tspec))
484 /* no automatic aggregate initialization in trad. C*/
485 warning(188);
486 }
487
488 /*
489 * Remove all entries which cannot be used for further initializers
490 * and do not expect a closing brace.
491 */
492 popinit(0);
493
494 nextinit(1);
495 }
496
497 void
498 initrbr(void)
499 {
500 DPRINTF(("%s\n", __func__));
501
502 if (initerr)
503 return;
504
505 popinit(1);
506 }
507
508 void
509 mkinit(tnode_t *tn)
510 {
511 ptrdiff_t offs;
512 sym_t *sym;
513 tspec_t lt, rt;
514 tnode_t *ln;
515 struct mbl *tmem;
516 scl_t sc;
517 #ifdef DEBUG
518 char buf[64], sbuf[64];
519 #endif
520
521 DPRINTF(("%s(%s %s)\n", __func__, tyname(buf, sizeof(buf), tn->tn_type),
522 prtnode(sbuf, sizeof(sbuf), tn)));
523 if (initerr || tn == NULL)
524 return;
525
526 sc = initsym->s_scl;
527
528 /*
529 * Do not test for automatic aggregate initialisation. If the
530 * initializer starts with a brace we have the warning already.
531 * If not, an error will be printed that the initializer must
532 * be enclosed by braces.
533 */
534
535 /*
536 * Local initialisation of non-array-types with only one expression
537 * without braces is done by ASSIGN
538 */
539 if ((sc == AUTO || sc == REG) &&
540 initsym->s_type->t_tspec != ARRAY && initstk->i_nxt == NULL) {
541 ln = getnnode(initsym, 0);
542 ln->tn_type = tduptyp(ln->tn_type);
543 ln->tn_type->t_const = 0;
544 tn = build(ASSIGN, ln, tn);
545 expr(tn, 0, 0, 0);
546 return;
547 }
548
549 /*
550 * Remove all entries which cannot be used for further initializers
551 * and do not require a closing brace.
552 */
553 popinit(0);
554
555 /* Initialisations by strings are done in strginit(). */
556 if (strginit(tn))
557 return;
558
559 nextinit(0);
560 if (initerr || tn == NULL)
561 return;
562
563 initstk->i_cnt--;
564 DPRINTF(("%s() cnt=%d tn=%p\n", __func__, initstk->i_cnt, tn));
565 /* Create a temporary node for the left side. */
566 ln = tgetblk(sizeof (tnode_t));
567 ln->tn_op = NAME;
568 ln->tn_type = tduptyp(initstk->i_type);
569 ln->tn_type->t_const = 0;
570 ln->tn_lvalue = 1;
571 ln->tn_sym = initsym; /* better than nothing */
572
573 tn = cconv(tn);
574
575 lt = ln->tn_type->t_tspec;
576 rt = tn->tn_type->t_tspec;
577
578 if (!tspec_is_scalar(lt))
579 LERROR("mkinit()");
580
581 if (!typeok(INIT, 0, ln, tn))
582 return;
583
584 /*
585 * Store the tree memory. This is nessesary because otherwise
586 * expr() would free it.
587 */
588 tmem = tsave();
589 expr(tn, 1, 0, 1);
590 trestor(tmem);
591
592 if (tspec_is_int(lt) && ln->tn_type->t_isfield && !tspec_is_int(rt)) {
593 /*
594 * Bit-fields can be initialized in trad. C only by integer
595 * constants.
596 */
597 if (tflag)
598 /* bit-field initialisation is illegal in trad. C */
599 warning(186);
600 }
601
602 if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON))
603 tn = convert(INIT, 0, initstk->i_type, tn);
604
605 if (tn != NULL && tn->tn_op != CON) {
606 sym = NULL;
607 offs = 0;
608 if (conaddr(tn, &sym, &offs) == -1) {
609 if (sc == AUTO || sc == REG) {
610 /* non-constant initializer */
611 (void)c99ism(177);
612 } else {
613 /* non-constant initializer */
614 error(177);
615 }
616 }
617 }
618 }
619
620
621 static int
622 strginit(tnode_t *tn)
623 {
624 tspec_t t;
625 istk_t *istk;
626 int len;
627 strg_t *strg;
628
629 if (tn->tn_op != STRING)
630 return (0);
631
632 istk = initstk;
633 strg = tn->tn_strg;
634
635 /*
636 * Check if we have an array type which can be initialized by
637 * the string.
638 */
639 if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
640 DPRINTF(("%s: subt array\n", __func__));
641 t = istk->i_subt->t_subt->t_tspec;
642 if (!((strg->st_tspec == CHAR &&
643 (t == CHAR || t == UCHAR || t == SCHAR)) ||
644 (strg->st_tspec == WCHAR && t == WCHAR))) {
645 return (0);
646 }
647 /* Put the array at top of stack */
648 pushinit();
649 istk = initstk;
650 } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
651 DPRINTF(("%s: type array\n", __func__));
652 t = istk->i_type->t_subt->t_tspec;
653 if (!((strg->st_tspec == CHAR &&
654 (t == CHAR || t == UCHAR || t == SCHAR)) ||
655 (strg->st_tspec == WCHAR && t == WCHAR))) {
656 return (0);
657 }
658 /*
659 * If the array is already partly initialized, we are
660 * wrong here.
661 */
662 if (istk->i_cnt != istk->i_type->t_dim)
663 return (0);
664 } else {
665 return (0);
666 }
667
668 /* Get length without trailing NUL character. */
669 len = strg->st_len;
670
671 if (istk->i_nolimit) {
672 istk->i_nolimit = 0;
673 istk->i_type->t_dim = len + 1;
674 /* from now complete type */
675 setcompl(istk->i_type, 0);
676 } else {
677 if (istk->i_type->t_dim < len) {
678 /* non-null byte ignored in string initializer */
679 warning(187);
680 }
681 }
682
683 /* In every case the array is initialized completely. */
684 istk->i_cnt = 0;
685
686 return (1);
687 }
688