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