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