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