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