init.c revision 1.33 1 /* $NetBSD: init.c,v 1.33 2020/12/29 11:35:11 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.33 2020/12/29 11:35:11 rillig Exp $");
41 #endif
42
43 #include <ctype.h>
44 #include <stdlib.h>
45 #include <string.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 setcomplete(istk->i_type, 1);
279 }
280
281 if (istk->i_cnt <= 0)
282 LERROR("pushinit()");
283 if (istk->i_type != NULL && tspec_is_scalar(istk->i_type->t_tspec))
284 LERROR("pushinit()");
285
286 initstk = xcalloc(1, sizeof (istk_t));
287 initstk->i_nxt = istk;
288 initstk->i_type = istk->i_subt;
289 if (initstk->i_type->t_tspec == FUNC)
290 LERROR("pushinit()");
291
292 again:
293 istk = initstk;
294
295 DPRINTF(("%s(%s)\n", __func__, tyname(buf, sizeof(buf), istk->i_type)));
296 switch (istk->i_type->t_tspec) {
297 case ARRAY:
298 if (namedmem) {
299 DPRINTF(("%s: ARRAY %s brace=%d\n", __func__,
300 namedmem->n_name, istk->i_brace));
301 goto pop;
302 } else if (istk->i_nxt->i_namedmem) {
303 istk->i_brace = 1;
304 DPRINTF(("%s ARRAY brace=%d, namedmem=%d\n", __func__,
305 istk->i_brace, istk->i_nxt->i_namedmem));
306 }
307
308 if (incompl(istk->i_type) && istk->i_nxt->i_nxt != NULL) {
309 /* initialisation of an incomplete type */
310 error(175);
311 initerr = 1;
312 return;
313 }
314 istk->i_subt = istk->i_type->t_subt;
315 istk->i_nolimit = incompl(istk->i_type);
316 istk->i_cnt = istk->i_type->t_dim;
317 DPRINTF(("%s: elements array %s[%d] %s\n", __func__,
318 tyname(buf, sizeof(buf), istk->i_subt), istk->i_cnt,
319 namedmem ? namedmem->n_name : "*none*"));
320 break;
321 case UNION:
322 if (tflag)
323 /* initialisation of union is illegal in trad. C */
324 warning(238);
325 /* FALLTHROUGH */
326 case STRUCT:
327 if (incompl(istk->i_type)) {
328 /* initialisation of an incomplete type */
329 error(175);
330 initerr = 1;
331 return;
332 }
333 cnt = 0;
334 DPRINTF(("%s: 2. member lookup %s %s i_namedmem=%d\n", __func__,
335 tyname(buf, sizeof(buf), istk->i_type),
336 namedmem ? namedmem->n_name : "*none*", istk->i_namedmem));
337 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
338 if (m->s_field && m->s_name == unnamed)
339 continue;
340 if (namedmem != NULL) {
341 DPRINTF(("%s():[member:%s, looking:%s]\n",
342 __func__, m->s_name, namedmem->n_name));
343 if (strcmp(m->s_name, namedmem->n_name) == 0) {
344 cnt++;
345 break;
346 } else
347 continue;
348 }
349 if (++cnt == 1) {
350 istk->i_mem = m;
351 istk->i_subt = m->s_type;
352 }
353 }
354 if (namedmem != NULL) {
355 if (m == NULL) {
356 DPRINTF(("%s(): struct pop\n", __func__));
357 goto pop;
358 }
359 istk->i_mem = m;
360 istk->i_subt = m->s_type;
361 istk->i_namedmem = 1;
362 DPRINTF(("%s(): namedmem %s\n", __func__,
363 namedmem->n_name));
364 memberpop();
365 cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
366 }
367 istk->i_brace = 1;
368 DPRINTF(("%s(): %s brace=%d\n", __func__,
369 tyname(buf, sizeof(buf),
370 istk->i_type ? istk->i_type : istk->i_subt),
371 istk->i_brace));
372 if (cnt == 0) {
373 /* cannot init. struct/union with no named member */
374 error(179);
375 initerr = 1;
376 return;
377 }
378 istk->i_cnt = istk->i_type->t_tspec == STRUCT ? cnt : 1;
379 break;
380 default:
381 if (namedmem) {
382 DPRINTF(("%s(): pop\n", __func__));
383 pop:
384 inxt = initstk->i_nxt;
385 free(istk);
386 initstk = inxt;
387 goto again;
388 }
389 istk->i_cnt = 1;
390 break;
391 }
392 }
393
394 static void
395 testinit(void)
396 {
397 istk_t *istk;
398
399 istk = initstk;
400
401 /*
402 * If a closing brace is expected we have at least one initializer
403 * too much.
404 */
405 if (istk->i_cnt == 0 && !istk->i_nolimit && !istk->i_namedmem) {
406 switch (istk->i_type->t_tspec) {
407 case ARRAY:
408 /* too many array initializers */
409 error(173, istk->i_type->t_dim);
410 break;
411 case STRUCT:
412 case UNION:
413 /* too many struct/union initializers */
414 error(172);
415 break;
416 default:
417 /* too many initializers */
418 error(174);
419 break;
420 }
421 initerr = 1;
422 }
423 }
424
425 static void
426 nextinit(int brace)
427 {
428 char buf[64];
429
430 DPRINTF(("%s(%d)\n", __func__, brace));
431 if (!brace) {
432 if (initstk->i_type == NULL &&
433 !tspec_is_scalar(initstk->i_subt->t_tspec)) {
434 /* {}-enclosed initializer required */
435 error(181);
436 }
437 /*
438 * Make sure an entry with a scalar type is at the top
439 * of the stack.
440 */
441 if (!initerr)
442 testinit();
443 while (!initerr && (initstk->i_type == NULL ||
444 !tspec_is_scalar(
445 initstk->i_type->t_tspec))) {
446 if (!initerr)
447 pushinit();
448 }
449 } else {
450 if (initstk->i_type != NULL &&
451 tspec_is_scalar(initstk->i_type->t_tspec)) {
452 /* invalid initializer */
453 error(176, tyname(buf, sizeof(buf), initstk->i_type));
454 initerr = 1;
455 }
456 if (!initerr)
457 testinit();
458 if (!initerr)
459 pushinit();
460 if (!initerr) {
461 initstk->i_brace = 1;
462 DPRINTF(("%s(): %p %s brace=%d\n", __func__,
463 namedmem,
464 tyname(buf, sizeof(buf),
465 initstk->i_type ? initstk->i_type
466 : initstk->i_subt),
467 initstk->i_brace));
468 }
469 }
470 }
471
472 void
473 initlbr(void)
474 {
475 DPRINTF(("%s\n", __func__));
476
477 if (initerr)
478 return;
479
480 if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
481 initstk->i_nxt == NULL) {
482 if (tflag && !tspec_is_scalar(initstk->i_subt->t_tspec))
483 /* no automatic aggregate initialization in trad. C*/
484 warning(188);
485 }
486
487 /*
488 * Remove all entries which cannot be used for further initializers
489 * and do not expect a closing brace.
490 */
491 popinit(0);
492
493 nextinit(1);
494 }
495
496 void
497 initrbr(void)
498 {
499 DPRINTF(("%s\n", __func__));
500
501 if (initerr)
502 return;
503
504 popinit(1);
505 }
506
507 void
508 mkinit(tnode_t *tn)
509 {
510 ptrdiff_t offs;
511 sym_t *sym;
512 tspec_t lt, rt;
513 tnode_t *ln;
514 struct mbl *tmem;
515 scl_t sc;
516 #ifdef DEBUG
517 char buf[64], sbuf[64];
518 #endif
519
520 DPRINTF(("%s(%s %s)\n", __func__, tyname(buf, sizeof(buf), tn->tn_type),
521 prtnode(sbuf, sizeof(sbuf), tn)));
522 if (initerr || tn == NULL)
523 return;
524
525 sc = initsym->s_scl;
526
527 /*
528 * Do not test for automatic aggregate initialisation. If the
529 * initializer starts with a brace we have the warning already.
530 * If not, an error will be printed that the initializer must
531 * be enclosed by braces.
532 */
533
534 /*
535 * Local initialisation of non-array-types with only one expression
536 * without braces is done by ASSIGN
537 */
538 if ((sc == AUTO || sc == REG) &&
539 initsym->s_type->t_tspec != ARRAY && initstk->i_nxt == NULL) {
540 ln = getnnode(initsym, 0);
541 ln->tn_type = tduptyp(ln->tn_type);
542 ln->tn_type->t_const = 0;
543 tn = build(ASSIGN, ln, tn);
544 expr(tn, 0, 0, 0);
545 return;
546 }
547
548 /*
549 * Remove all entries which cannot be used for further initializers
550 * and do not require a closing brace.
551 */
552 popinit(0);
553
554 /* Initialisations by strings are done in strginit(). */
555 if (strginit(tn))
556 return;
557
558 nextinit(0);
559 if (initerr || tn == NULL)
560 return;
561
562 initstk->i_cnt--;
563 DPRINTF(("%s() cnt=%d tn=%p\n", __func__, initstk->i_cnt, tn));
564 /* Create a temporary node for the left side. */
565 ln = tgetblk(sizeof (tnode_t));
566 ln->tn_op = NAME;
567 ln->tn_type = tduptyp(initstk->i_type);
568 ln->tn_type->t_const = 0;
569 ln->tn_lvalue = 1;
570 ln->tn_sym = initsym; /* better than nothing */
571
572 tn = cconv(tn);
573
574 lt = ln->tn_type->t_tspec;
575 rt = tn->tn_type->t_tspec;
576
577 if (!tspec_is_scalar(lt))
578 LERROR("mkinit()");
579
580 if (!typeok(INIT, 0, ln, tn))
581 return;
582
583 /*
584 * Store the tree memory. This is nessesary because otherwise
585 * expr() would free it.
586 */
587 tmem = tsave();
588 expr(tn, 1, 0, 1);
589 trestor(tmem);
590
591 if (tspec_is_int(lt) && ln->tn_type->t_isfield && !tspec_is_int(rt)) {
592 /*
593 * Bit-fields can be initialized in trad. C only by integer
594 * constants.
595 */
596 if (tflag)
597 /* bit-field initialisation is illegal in trad. C */
598 warning(186);
599 }
600
601 if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON))
602 tn = convert(INIT, 0, initstk->i_type, tn);
603
604 if (tn != NULL && tn->tn_op != CON) {
605 sym = NULL;
606 offs = 0;
607 if (conaddr(tn, &sym, &offs) == -1) {
608 if (sc == AUTO || sc == REG) {
609 /* non-constant initializer */
610 (void)c99ism(177);
611 } else {
612 /* non-constant initializer */
613 error(177);
614 }
615 }
616 }
617 }
618
619
620 static int
621 strginit(tnode_t *tn)
622 {
623 tspec_t t;
624 istk_t *istk;
625 int len;
626 strg_t *strg;
627
628 if (tn->tn_op != STRING)
629 return 0;
630
631 istk = initstk;
632 strg = tn->tn_strg;
633
634 /*
635 * Check if we have an array type which can be initialized by
636 * the string.
637 */
638 if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
639 DPRINTF(("%s: subt array\n", __func__));
640 t = istk->i_subt->t_subt->t_tspec;
641 if (!((strg->st_tspec == CHAR &&
642 (t == CHAR || t == UCHAR || t == SCHAR)) ||
643 (strg->st_tspec == WCHAR && t == WCHAR))) {
644 return 0;
645 }
646 /* Put the array at top of stack */
647 pushinit();
648 istk = initstk;
649 } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
650 DPRINTF(("%s: type array\n", __func__));
651 t = istk->i_type->t_subt->t_tspec;
652 if (!((strg->st_tspec == CHAR &&
653 (t == CHAR || t == UCHAR || t == SCHAR)) ||
654 (strg->st_tspec == WCHAR && t == WCHAR))) {
655 return 0;
656 }
657 /*
658 * If the array is already partly initialized, we are
659 * wrong here.
660 */
661 if (istk->i_cnt != istk->i_type->t_dim)
662 return 0;
663 } else {
664 return 0;
665 }
666
667 /* Get length without trailing NUL character. */
668 len = strg->st_len;
669
670 if (istk->i_nolimit) {
671 istk->i_nolimit = 0;
672 istk->i_type->t_dim = len + 1;
673 setcomplete(istk->i_type, 1);
674 } else {
675 if (istk->i_type->t_dim < len) {
676 /* non-null byte ignored in string initializer */
677 warning(187);
678 }
679 }
680
681 /* In every case the array is initialized completely. */
682 istk->i_cnt = 0;
683
684 return 1;
685 }
686