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