init.c revision 1.36 1 /* $NetBSD: init.c,v 1.36 2020/12/29 16:53:36 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.36 2020/12/29 16:53:36 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_next(int);
74 static void initstack_check_too_many(void);
75 static void initstack_pop_brace(void);
76 static void initstack_pop_nobrace(void);
77 static void initstack_pop_item(void);
78 static int initstack_string(tnode_t *);
79 static void pop_member(void);
80
81 #ifndef DEBUG
82 #define DPRINTF(a)
83 #else
84 #define DPRINTF(a) printf a
85 #endif
86
87 void
88 push_member(sb)
89 sbuf_t *sb;
90 {
91 namlist_t *nam = xcalloc(1, sizeof (namlist_t));
92 nam->n_name = sb->sb_name;
93 DPRINTF(("%s: %s %p\n", __func__, nam->n_name, nam));
94 if (namedmem == NULL) {
95 nam->n_prev = nam->n_next = nam;
96 namedmem = nam;
97 } else {
98 namedmem->n_prev->n_next = nam;
99 nam->n_prev = namedmem->n_prev;
100 nam->n_next = namedmem;
101 namedmem->n_prev = nam;
102 }
103 }
104
105 static void
106 pop_member(void)
107 {
108 DPRINTF(("%s: %s %p\n", __func__, namedmem->n_name, namedmem));
109 if (namedmem->n_next == namedmem) {
110 free(namedmem);
111 namedmem = NULL;
112 } else {
113 namlist_t *nam = namedmem;
114 namedmem = namedmem->n_next;
115 namedmem->n_next = nam->n_next;
116 namedmem->n_prev = nam->n_prev;
117 free(nam);
118 }
119 }
120
121
122 /*
123 * Initialize the initialisation stack by putting an entry for the variable
124 * which is to be initialized on it.
125 */
126 void
127 initstack_init(void)
128 {
129 istk_t *istk;
130
131 if (initerr)
132 return;
133
134 /* free memory used in last initialisation */
135 while ((istk = initstk) != NULL) {
136 initstk = istk->i_nxt;
137 free(istk);
138 }
139
140 /*
141 * If the type which is to be initialized is an incomplete type,
142 * it must be duplicated.
143 */
144 if (initsym->s_type->t_tspec == ARRAY && incompl(initsym->s_type))
145 initsym->s_type = duptyp(initsym->s_type);
146
147 istk = initstk = xcalloc(1, sizeof (istk_t));
148 istk->i_subt = initsym->s_type;
149 istk->i_cnt = 1;
150
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 count=%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 count=%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(): %d %s\n", __func__, istk->i_cnt,
183 namedmem ? namedmem->n_name : "*null*"));
184 if (istk->i_cnt >= 0 && namedmem != NULL) {
185 DPRINTF(("%s(): %d %s %s\n", __func__, istk->i_cnt,
186 tyname(buf, sizeof(buf), istk->i_type), namedmem->n_name));
187 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
188 DPRINTF(("%s(): pop [%s %s]\n", __func__,
189 namedmem->n_name, m->s_name));
190 if (m->s_field && m->s_name == unnamed)
191 continue;
192 if (strcmp(m->s_name, namedmem->n_name) == 0) {
193 istk->i_subt = m->s_type;
194 istk->i_cnt++;
195 pop_member();
196 return;
197 }
198 }
199 error(101, namedmem->n_name);
200 DPRINTF(("%s(): namedmem %s\n", __func__, namedmem->n_name));
201 pop_member();
202 istk->i_namedmem = 1;
203 return;
204 }
205 /*
206 * If the removed element was a structure member, we must go
207 * to the next structure member.
208 */
209 if (istk->i_cnt > 0 && istk->i_type->t_tspec == STRUCT &&
210 !istk->i_namedmem) {
211 do {
212 m = istk->i_mem = istk->i_mem->s_nxt;
213 if (m == NULL)
214 LERROR("initstack_pop_item()");
215 DPRINTF(("%s(): pop %s\n", __func__, m->s_name));
216 } while (m->s_field && m->s_name == unnamed);
217 istk->i_subt = m->s_type;
218 }
219 }
220
221 /*
222 * Take all entries, including the first which requires a closing brace,
223 * from the stack.
224 */
225 static void
226 initstack_pop_brace(void)
227 {
228 int brace;
229
230 DPRINTF(("%s\n", __func__));
231 do {
232 brace = initstk->i_brace;
233 DPRINTF(("%s: loop %d\n", __func__, brace));
234 initstack_pop_item();
235 } while (!brace);
236 DPRINTF(("%s: done\n", __func__));
237 }
238
239 /*
240 * Take all entries which cannot be used for further initializers from the
241 * stack, but do this only if they do not require a closing brace.
242 */
243 static void
244 initstack_pop_nobrace(void)
245 {
246
247 DPRINTF(("%s\n", __func__));
248 while (!initstk->i_brace && initstk->i_cnt == 0 && !initstk->i_nolimit)
249 initstack_pop_item();
250 DPRINTF(("%s: done\n", __func__));
251 }
252
253 static void
254 initstack_push(void)
255 {
256 #ifdef DEBUG
257 char buf[64];
258 #endif
259 istk_t *istk, *inxt;
260 int cnt;
261 sym_t *m;
262
263 istk = initstk;
264
265 /* Extend an incomplete array type by one element */
266 if (istk->i_cnt == 0) {
267 DPRINTF(("%s(extend) %s\n", __func__,
268 tyname(buf, sizeof(buf), istk->i_type)));
269 /*
270 * Inside of other aggregate types must not be an incomplete
271 * type.
272 */
273 if (istk->i_nxt->i_nxt != NULL)
274 LERROR("initstack_push()");
275 istk->i_cnt = 1;
276 if (istk->i_type->t_tspec != ARRAY)
277 LERROR("initstack_push()");
278 istk->i_type->t_dim++;
279 setcomplete(istk->i_type, 1);
280 }
281
282 if (istk->i_cnt <= 0)
283 LERROR("initstack_push()");
284 if (istk->i_type != NULL && tspec_is_scalar(istk->i_type->t_tspec))
285 LERROR("initstack_push()");
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("initstack_push()");
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 pop_member();
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 initstack_check_too_many(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 initstack_next(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 initstack_check_too_many();
444 while (!initerr && (initstk->i_type == NULL ||
445 !tspec_is_scalar(
446 initstk->i_type->t_tspec))) {
447 if (!initerr)
448 initstack_push();
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 initstack_check_too_many();
459 if (!initerr)
460 initstack_push();
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 init_lbrace(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 initstack_pop_nobrace();
493
494 initstack_next(1);
495 }
496
497 void
498 init_rbrace(void)
499 {
500 DPRINTF(("%s\n", __func__));
501
502 if (initerr)
503 return;
504
505 initstack_pop_brace();
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__,
522 tyname(buf, sizeof(buf), tn->tn_type),
523 print_tnode(sbuf, sizeof(sbuf), tn)));
524 if (initerr || tn == NULL)
525 return;
526
527 sc = initsym->s_scl;
528
529 /*
530 * Do not test for automatic aggregate initialisation. If the
531 * initializer starts with a brace we have the warning already.
532 * If not, an error will be printed that the initializer must
533 * be enclosed by braces.
534 */
535
536 /*
537 * Local initialisation of non-array-types with only one expression
538 * without braces is done by ASSIGN
539 */
540 if ((sc == AUTO || sc == REG) &&
541 initsym->s_type->t_tspec != ARRAY && initstk->i_nxt == NULL) {
542 ln = getnnode(initsym, 0);
543 ln->tn_type = tduptyp(ln->tn_type);
544 ln->tn_type->t_const = 0;
545 tn = build(ASSIGN, ln, tn);
546 expr(tn, 0, 0, 0);
547 return;
548 }
549
550 /*
551 * Remove all entries which cannot be used for further initializers
552 * and do not require a closing brace.
553 */
554 initstack_pop_nobrace();
555
556 /* Initialisations by strings are done in initstack_string(). */
557 if (initstack_string(tn))
558 return;
559
560 initstack_next(0);
561 if (initerr || tn == NULL)
562 return;
563
564 initstk->i_cnt--;
565 DPRINTF(("%s() cnt=%d tn=%p\n", __func__, initstk->i_cnt, tn));
566 /* Create a temporary node for the left side. */
567 ln = tgetblk(sizeof (tnode_t));
568 ln->tn_op = NAME;
569 ln->tn_type = tduptyp(initstk->i_type);
570 ln->tn_type->t_const = 0;
571 ln->tn_lvalue = 1;
572 ln->tn_sym = initsym; /* better than nothing */
573
574 tn = cconv(tn);
575
576 lt = ln->tn_type->t_tspec;
577 rt = tn->tn_type->t_tspec;
578
579 if (!tspec_is_scalar(lt))
580 LERROR("mkinit()");
581
582 if (!typeok(INIT, 0, ln, tn))
583 return;
584
585 /*
586 * Store the tree memory. This is nessesary because otherwise
587 * expr() would free it.
588 */
589 tmem = tsave();
590 expr(tn, 1, 0, 1);
591 trestor(tmem);
592
593 if (tspec_is_int(lt) && ln->tn_type->t_isfield && !tspec_is_int(rt)) {
594 /*
595 * Bit-fields can be initialized in trad. C only by integer
596 * constants.
597 */
598 if (tflag)
599 /* bit-field initialisation is illegal in trad. C */
600 warning(186);
601 }
602
603 if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON))
604 tn = convert(INIT, 0, initstk->i_type, tn);
605
606 if (tn != NULL && tn->tn_op != CON) {
607 sym = NULL;
608 offs = 0;
609 if (conaddr(tn, &sym, &offs) == -1) {
610 if (sc == AUTO || sc == REG) {
611 /* non-constant initializer */
612 (void)c99ism(177);
613 } else {
614 /* non-constant initializer */
615 error(177);
616 }
617 }
618 }
619 }
620
621
622 static int
623 initstack_string(tnode_t *tn)
624 {
625 tspec_t t;
626 istk_t *istk;
627 int len;
628 strg_t *strg;
629
630 if (tn->tn_op != STRING)
631 return 0;
632
633 istk = initstk;
634 strg = tn->tn_strg;
635
636 /*
637 * Check if we have an array type which can be initialized by
638 * the string.
639 */
640 if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
641 DPRINTF(("%s: subt array\n", __func__));
642 t = istk->i_subt->t_subt->t_tspec;
643 if (!((strg->st_tspec == CHAR &&
644 (t == CHAR || t == UCHAR || t == SCHAR)) ||
645 (strg->st_tspec == WCHAR && t == WCHAR))) {
646 return 0;
647 }
648 /* Put the array at top of stack */
649 initstack_push();
650 istk = initstk;
651 } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
652 DPRINTF(("%s: type array\n", __func__));
653 t = istk->i_type->t_subt->t_tspec;
654 if (!((strg->st_tspec == CHAR &&
655 (t == CHAR || t == UCHAR || t == SCHAR)) ||
656 (strg->st_tspec == WCHAR && t == WCHAR))) {
657 return 0;
658 }
659 /*
660 * If the array is already partly initialized, we are
661 * wrong here.
662 */
663 if (istk->i_cnt != istk->i_type->t_dim)
664 return 0;
665 } else {
666 return 0;
667 }
668
669 /* Get length without trailing NUL character. */
670 len = strg->st_len;
671
672 if (istk->i_nolimit) {
673 istk->i_nolimit = 0;
674 istk->i_type->t_dim = len + 1;
675 setcomplete(istk->i_type, 1);
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