init.c revision 1.36 1 1.36 rillig /* $NetBSD: init.c,v 1.36 2020/12/29 16:53:36 rillig Exp $ */
2 1.2 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1994, 1995 Jochen Pohl
5 1.1 cgd * All Rights Reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by Jochen Pohl for
18 1.1 cgd * The NetBSD Project.
19 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
20 1.1 cgd * derived from this software without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.18 jmc #if HAVE_NBTOOL_CONFIG_H
35 1.18 jmc #include "nbtool_config.h"
36 1.18 jmc #endif
37 1.18 jmc
38 1.5 christos #include <sys/cdefs.h>
39 1.10 tv #if defined(__RCSID) && !defined(lint)
40 1.36 rillig __RCSID("$NetBSD: init.c,v 1.36 2020/12/29 16:53:36 rillig Exp $");
41 1.1 cgd #endif
42 1.1 cgd
43 1.31 rillig #include <ctype.h>
44 1.1 cgd #include <stdlib.h>
45 1.17 thorpej #include <string.h>
46 1.1 cgd
47 1.1 cgd #include "lint1.h"
48 1.1 cgd
49 1.1 cgd /*
50 1.8 wiz * initerr is set as soon as a fatal error occurred in an initialisation.
51 1.1 cgd * The effect is that the rest of the initialisation is ignored (parsed
52 1.1 cgd * by yacc, expression trees built, but no initialisation takes place).
53 1.1 cgd */
54 1.1 cgd int initerr;
55 1.1 cgd
56 1.1 cgd /* Pointer to the symbol which is to be initialized. */
57 1.1 cgd sym_t *initsym;
58 1.1 cgd
59 1.1 cgd /* Points to the top element of the initialisation stack. */
60 1.1 cgd istk_t *initstk;
61 1.1 cgd
62 1.12 christos typedef struct namlist {
63 1.12 christos const char *n_name;
64 1.12 christos struct namlist *n_prev;
65 1.12 christos struct namlist *n_next;
66 1.12 christos } namlist_t;
67 1.12 christos
68 1.12 christos /* Points to a c9x named member; */
69 1.12 christos namlist_t *namedmem = NULL;
70 1.12 christos
71 1.1 cgd
72 1.35 rillig static void initstack_push(void);
73 1.35 rillig static void initstack_next(int);
74 1.35 rillig static void initstack_check_too_many(void);
75 1.36 rillig static void initstack_pop_brace(void);
76 1.36 rillig static void initstack_pop_nobrace(void);
77 1.35 rillig static void initstack_pop_item(void);
78 1.35 rillig static int initstack_string(tnode_t *);
79 1.34 rillig static void pop_member(void);
80 1.12 christos
81 1.12 christos #ifndef DEBUG
82 1.12 christos #define DPRINTF(a)
83 1.12 christos #else
84 1.12 christos #define DPRINTF(a) printf a
85 1.12 christos #endif
86 1.12 christos
87 1.12 christos void
88 1.34 rillig push_member(sb)
89 1.12 christos sbuf_t *sb;
90 1.12 christos {
91 1.28 rillig namlist_t *nam = xcalloc(1, sizeof (namlist_t));
92 1.12 christos nam->n_name = sb->sb_name;
93 1.27 christos DPRINTF(("%s: %s %p\n", __func__, nam->n_name, nam));
94 1.12 christos if (namedmem == NULL) {
95 1.12 christos nam->n_prev = nam->n_next = nam;
96 1.12 christos namedmem = nam;
97 1.12 christos } else {
98 1.12 christos namedmem->n_prev->n_next = nam;
99 1.12 christos nam->n_prev = namedmem->n_prev;
100 1.12 christos nam->n_next = namedmem;
101 1.12 christos namedmem->n_prev = nam;
102 1.12 christos }
103 1.12 christos }
104 1.12 christos
105 1.12 christos static void
106 1.34 rillig pop_member(void)
107 1.12 christos {
108 1.27 christos DPRINTF(("%s: %s %p\n", __func__, namedmem->n_name, namedmem));
109 1.12 christos if (namedmem->n_next == namedmem) {
110 1.12 christos free(namedmem);
111 1.12 christos namedmem = NULL;
112 1.12 christos } else {
113 1.12 christos namlist_t *nam = namedmem;
114 1.12 christos namedmem = namedmem->n_next;
115 1.27 christos namedmem->n_next = nam->n_next;
116 1.27 christos namedmem->n_prev = nam->n_prev;
117 1.12 christos free(nam);
118 1.12 christos }
119 1.12 christos }
120 1.1 cgd
121 1.1 cgd
122 1.1 cgd /*
123 1.1 cgd * Initialize the initialisation stack by putting an entry for the variable
124 1.1 cgd * which is to be initialized on it.
125 1.1 cgd */
126 1.1 cgd void
127 1.35 rillig initstack_init(void)
128 1.1 cgd {
129 1.1 cgd istk_t *istk;
130 1.1 cgd
131 1.1 cgd if (initerr)
132 1.1 cgd return;
133 1.1 cgd
134 1.1 cgd /* free memory used in last initialisation */
135 1.1 cgd while ((istk = initstk) != NULL) {
136 1.1 cgd initstk = istk->i_nxt;
137 1.1 cgd free(istk);
138 1.1 cgd }
139 1.1 cgd
140 1.1 cgd /*
141 1.1 cgd * If the type which is to be initialized is an incomplete type,
142 1.1 cgd * it must be duplicated.
143 1.1 cgd */
144 1.1 cgd if (initsym->s_type->t_tspec == ARRAY && incompl(initsym->s_type))
145 1.1 cgd initsym->s_type = duptyp(initsym->s_type);
146 1.1 cgd
147 1.1 cgd istk = initstk = xcalloc(1, sizeof (istk_t));
148 1.1 cgd istk->i_subt = initsym->s_type;
149 1.1 cgd istk->i_cnt = 1;
150 1.1 cgd
151 1.1 cgd }
152 1.1 cgd
153 1.1 cgd static void
154 1.35 rillig initstack_pop_item(void)
155 1.1 cgd {
156 1.12 christos #ifdef DEBUG
157 1.12 christos char buf[64];
158 1.12 christos #endif
159 1.1 cgd istk_t *istk;
160 1.1 cgd sym_t *m;
161 1.1 cgd
162 1.26 christos DPRINTF(("%s+(%s): brace=%d count=%d namedmem %d\n", __func__,
163 1.20 christos tyname(buf, sizeof(buf),
164 1.30 rillig initstk->i_type ? initstk->i_type : initstk->i_subt),
165 1.20 christos initstk->i_brace, initstk->i_cnt, initstk->i_namedmem));
166 1.1 cgd initstk = (istk = initstk)->i_nxt;
167 1.1 cgd free(istk);
168 1.1 cgd
169 1.1 cgd istk = initstk;
170 1.26 christos if (istk == NULL)
171 1.35 rillig LERROR("initstack_pop_item()");
172 1.26 christos
173 1.26 christos DPRINTF(("%s-(%s): brace=%d count=%d namedmem %d\n", __func__,
174 1.26 christos tyname(buf, sizeof(buf),
175 1.30 rillig initstk->i_type ? initstk->i_type : initstk->i_subt),
176 1.26 christos initstk->i_brace, initstk->i_cnt, initstk->i_namedmem));
177 1.1 cgd
178 1.1 cgd istk->i_cnt--;
179 1.1 cgd if (istk->i_cnt < 0)
180 1.35 rillig LERROR("initstack_pop_item()");
181 1.1 cgd
182 1.26 christos DPRINTF(("%s(): %d %s\n", __func__, istk->i_cnt,
183 1.16 christos namedmem ? namedmem->n_name : "*null*"));
184 1.12 christos if (istk->i_cnt >= 0 && namedmem != NULL) {
185 1.26 christos DPRINTF(("%s(): %d %s %s\n", __func__, istk->i_cnt,
186 1.12 christos tyname(buf, sizeof(buf), istk->i_type), namedmem->n_name));
187 1.12 christos for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
188 1.26 christos DPRINTF(("%s(): pop [%s %s]\n", __func__,
189 1.26 christos namedmem->n_name, m->s_name));
190 1.12 christos if (m->s_field && m->s_name == unnamed)
191 1.12 christos continue;
192 1.12 christos if (strcmp(m->s_name, namedmem->n_name) == 0) {
193 1.12 christos istk->i_subt = m->s_type;
194 1.12 christos istk->i_cnt++;
195 1.34 rillig pop_member();
196 1.12 christos return;
197 1.12 christos }
198 1.12 christos }
199 1.12 christos error(101, namedmem->n_name);
200 1.26 christos DPRINTF(("%s(): namedmem %s\n", __func__, namedmem->n_name));
201 1.34 rillig pop_member();
202 1.12 christos istk->i_namedmem = 1;
203 1.12 christos return;
204 1.12 christos }
205 1.1 cgd /*
206 1.1 cgd * If the removed element was a structure member, we must go
207 1.1 cgd * to the next structure member.
208 1.1 cgd */
209 1.12 christos if (istk->i_cnt > 0 && istk->i_type->t_tspec == STRUCT &&
210 1.12 christos !istk->i_namedmem) {
211 1.1 cgd do {
212 1.1 cgd m = istk->i_mem = istk->i_mem->s_nxt;
213 1.1 cgd if (m == NULL)
214 1.35 rillig LERROR("initstack_pop_item()");
215 1.26 christos DPRINTF(("%s(): pop %s\n", __func__, m->s_name));
216 1.1 cgd } while (m->s_field && m->s_name == unnamed);
217 1.1 cgd istk->i_subt = m->s_type;
218 1.1 cgd }
219 1.1 cgd }
220 1.1 cgd
221 1.36 rillig /*
222 1.36 rillig * Take all entries, including the first which requires a closing brace,
223 1.36 rillig * from the stack.
224 1.36 rillig */
225 1.36 rillig static void
226 1.36 rillig initstack_pop_brace(void)
227 1.36 rillig {
228 1.36 rillig int brace;
229 1.36 rillig
230 1.36 rillig DPRINTF(("%s\n", __func__));
231 1.36 rillig do {
232 1.36 rillig brace = initstk->i_brace;
233 1.36 rillig DPRINTF(("%s: loop %d\n", __func__, brace));
234 1.36 rillig initstack_pop_item();
235 1.36 rillig } while (!brace);
236 1.36 rillig DPRINTF(("%s: done\n", __func__));
237 1.36 rillig }
238 1.36 rillig
239 1.36 rillig /*
240 1.36 rillig * Take all entries which cannot be used for further initializers from the
241 1.36 rillig * stack, but do this only if they do not require a closing brace.
242 1.36 rillig */
243 1.1 cgd static void
244 1.36 rillig initstack_pop_nobrace(void)
245 1.1 cgd {
246 1.7 lukem
247 1.36 rillig DPRINTF(("%s\n", __func__));
248 1.36 rillig while (!initstk->i_brace && initstk->i_cnt == 0 && !initstk->i_nolimit)
249 1.36 rillig initstack_pop_item();
250 1.36 rillig DPRINTF(("%s: done\n", __func__));
251 1.1 cgd }
252 1.1 cgd
253 1.1 cgd static void
254 1.35 rillig initstack_push(void)
255 1.1 cgd {
256 1.12 christos #ifdef DEBUG
257 1.12 christos char buf[64];
258 1.12 christos #endif
259 1.22 ad istk_t *istk, *inxt;
260 1.1 cgd int cnt;
261 1.1 cgd sym_t *m;
262 1.1 cgd
263 1.1 cgd istk = initstk;
264 1.1 cgd
265 1.1 cgd /* Extend an incomplete array type by one element */
266 1.1 cgd if (istk->i_cnt == 0) {
267 1.30 rillig DPRINTF(("%s(extend) %s\n", __func__,
268 1.30 rillig tyname(buf, sizeof(buf), istk->i_type)));
269 1.1 cgd /*
270 1.1 cgd * Inside of other aggregate types must not be an incomplete
271 1.1 cgd * type.
272 1.1 cgd */
273 1.1 cgd if (istk->i_nxt->i_nxt != NULL)
274 1.35 rillig LERROR("initstack_push()");
275 1.1 cgd istk->i_cnt = 1;
276 1.1 cgd if (istk->i_type->t_tspec != ARRAY)
277 1.35 rillig LERROR("initstack_push()");
278 1.1 cgd istk->i_type->t_dim++;
279 1.32 rillig setcomplete(istk->i_type, 1);
280 1.1 cgd }
281 1.1 cgd
282 1.1 cgd if (istk->i_cnt <= 0)
283 1.35 rillig LERROR("initstack_push()");
284 1.29 rillig if (istk->i_type != NULL && tspec_is_scalar(istk->i_type->t_tspec))
285 1.35 rillig LERROR("initstack_push()");
286 1.1 cgd
287 1.1 cgd initstk = xcalloc(1, sizeof (istk_t));
288 1.1 cgd initstk->i_nxt = istk;
289 1.1 cgd initstk->i_type = istk->i_subt;
290 1.1 cgd if (initstk->i_type->t_tspec == FUNC)
291 1.35 rillig LERROR("initstack_push()");
292 1.1 cgd
293 1.12 christos again:
294 1.1 cgd istk = initstk;
295 1.1 cgd
296 1.26 christos DPRINTF(("%s(%s)\n", __func__, tyname(buf, sizeof(buf), istk->i_type)));
297 1.1 cgd switch (istk->i_type->t_tspec) {
298 1.1 cgd case ARRAY:
299 1.16 christos if (namedmem) {
300 1.26 christos DPRINTF(("%s: ARRAY %s brace=%d\n", __func__,
301 1.20 christos namedmem->n_name, istk->i_brace));
302 1.19 christos goto pop;
303 1.20 christos } else if (istk->i_nxt->i_namedmem) {
304 1.26 christos istk->i_brace = 1;
305 1.26 christos DPRINTF(("%s ARRAY brace=%d, namedmem=%d\n", __func__,
306 1.20 christos istk->i_brace, istk->i_nxt->i_namedmem));
307 1.20 christos }
308 1.20 christos
309 1.1 cgd if (incompl(istk->i_type) && istk->i_nxt->i_nxt != NULL) {
310 1.1 cgd /* initialisation of an incomplete type */
311 1.1 cgd error(175);
312 1.1 cgd initerr = 1;
313 1.1 cgd return;
314 1.1 cgd }
315 1.1 cgd istk->i_subt = istk->i_type->t_subt;
316 1.1 cgd istk->i_nolimit = incompl(istk->i_type);
317 1.1 cgd istk->i_cnt = istk->i_type->t_dim;
318 1.26 christos DPRINTF(("%s: elements array %s[%d] %s\n", __func__,
319 1.16 christos tyname(buf, sizeof(buf), istk->i_subt), istk->i_cnt,
320 1.16 christos namedmem ? namedmem->n_name : "*none*"));
321 1.1 cgd break;
322 1.1 cgd case UNION:
323 1.1 cgd if (tflag)
324 1.1 cgd /* initialisation of union is illegal in trad. C */
325 1.1 cgd warning(238);
326 1.1 cgd /* FALLTHROUGH */
327 1.1 cgd case STRUCT:
328 1.1 cgd if (incompl(istk->i_type)) {
329 1.1 cgd /* initialisation of an incomplete type */
330 1.1 cgd error(175);
331 1.1 cgd initerr = 1;
332 1.1 cgd return;
333 1.1 cgd }
334 1.1 cgd cnt = 0;
335 1.26 christos DPRINTF(("%s: 2. member lookup %s %s i_namedmem=%d\n", __func__,
336 1.16 christos tyname(buf, sizeof(buf), istk->i_type),
337 1.26 christos namedmem ? namedmem->n_name : "*none*", istk->i_namedmem));
338 1.1 cgd for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
339 1.1 cgd if (m->s_field && m->s_name == unnamed)
340 1.1 cgd continue;
341 1.12 christos if (namedmem != NULL) {
342 1.26 christos DPRINTF(("%s():[member:%s, looking:%s]\n",
343 1.26 christos __func__, m->s_name, namedmem->n_name));
344 1.12 christos if (strcmp(m->s_name, namedmem->n_name) == 0) {
345 1.12 christos cnt++;
346 1.12 christos break;
347 1.12 christos } else
348 1.12 christos continue;
349 1.12 christos }
350 1.1 cgd if (++cnt == 1) {
351 1.1 cgd istk->i_mem = m;
352 1.1 cgd istk->i_subt = m->s_type;
353 1.1 cgd }
354 1.1 cgd }
355 1.12 christos if (namedmem != NULL) {
356 1.12 christos if (m == NULL) {
357 1.26 christos DPRINTF(("%s(): struct pop\n", __func__));
358 1.19 christos goto pop;
359 1.28 rillig }
360 1.26 christos istk->i_mem = m;
361 1.26 christos istk->i_subt = m->s_type;
362 1.19 christos istk->i_namedmem = 1;
363 1.26 christos DPRINTF(("%s(): namedmem %s\n", __func__,
364 1.26 christos namedmem->n_name));
365 1.34 rillig pop_member();
366 1.12 christos cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
367 1.12 christos }
368 1.26 christos istk->i_brace = 1;
369 1.26 christos DPRINTF(("%s(): %s brace=%d\n", __func__,
370 1.26 christos tyname(buf, sizeof(buf),
371 1.30 rillig istk->i_type ? istk->i_type : istk->i_subt),
372 1.26 christos istk->i_brace));
373 1.1 cgd if (cnt == 0) {
374 1.1 cgd /* cannot init. struct/union with no named member */
375 1.1 cgd error(179);
376 1.1 cgd initerr = 1;
377 1.1 cgd return;
378 1.1 cgd }
379 1.1 cgd istk->i_cnt = istk->i_type->t_tspec == STRUCT ? cnt : 1;
380 1.1 cgd break;
381 1.1 cgd default:
382 1.12 christos if (namedmem) {
383 1.26 christos DPRINTF(("%s(): pop\n", __func__));
384 1.19 christos pop:
385 1.22 ad inxt = initstk->i_nxt;
386 1.12 christos free(istk);
387 1.22 ad initstk = inxt;
388 1.12 christos goto again;
389 1.12 christos }
390 1.1 cgd istk->i_cnt = 1;
391 1.1 cgd break;
392 1.1 cgd }
393 1.1 cgd }
394 1.1 cgd
395 1.1 cgd static void
396 1.35 rillig initstack_check_too_many(void)
397 1.1 cgd {
398 1.1 cgd istk_t *istk;
399 1.1 cgd
400 1.1 cgd istk = initstk;
401 1.1 cgd
402 1.1 cgd /*
403 1.1 cgd * If a closing brace is expected we have at least one initializer
404 1.1 cgd * too much.
405 1.1 cgd */
406 1.12 christos if (istk->i_cnt == 0 && !istk->i_nolimit && !istk->i_namedmem) {
407 1.1 cgd switch (istk->i_type->t_tspec) {
408 1.1 cgd case ARRAY:
409 1.1 cgd /* too many array initializers */
410 1.23 christos error(173, istk->i_type->t_dim);
411 1.1 cgd break;
412 1.1 cgd case STRUCT:
413 1.1 cgd case UNION:
414 1.1 cgd /* too many struct/union initializers */
415 1.1 cgd error(172);
416 1.1 cgd break;
417 1.1 cgd default:
418 1.1 cgd /* too many initializers */
419 1.1 cgd error(174);
420 1.1 cgd break;
421 1.1 cgd }
422 1.1 cgd initerr = 1;
423 1.1 cgd }
424 1.1 cgd }
425 1.1 cgd
426 1.1 cgd static void
427 1.35 rillig initstack_next(int brace)
428 1.1 cgd {
429 1.12 christos char buf[64];
430 1.7 lukem
431 1.26 christos DPRINTF(("%s(%d)\n", __func__, brace));
432 1.1 cgd if (!brace) {
433 1.1 cgd if (initstk->i_type == NULL &&
434 1.29 rillig !tspec_is_scalar(initstk->i_subt->t_tspec)) {
435 1.1 cgd /* {}-enclosed initializer required */
436 1.1 cgd error(181);
437 1.1 cgd }
438 1.1 cgd /*
439 1.1 cgd * Make sure an entry with a scalar type is at the top
440 1.1 cgd * of the stack.
441 1.1 cgd */
442 1.1 cgd if (!initerr)
443 1.35 rillig initstack_check_too_many();
444 1.1 cgd while (!initerr && (initstk->i_type == NULL ||
445 1.29 rillig !tspec_is_scalar(
446 1.29 rillig initstk->i_type->t_tspec))) {
447 1.1 cgd if (!initerr)
448 1.35 rillig initstack_push();
449 1.1 cgd }
450 1.1 cgd } else {
451 1.1 cgd if (initstk->i_type != NULL &&
452 1.29 rillig tspec_is_scalar(initstk->i_type->t_tspec)) {
453 1.1 cgd /* invalid initializer */
454 1.12 christos error(176, tyname(buf, sizeof(buf), initstk->i_type));
455 1.1 cgd initerr = 1;
456 1.1 cgd }
457 1.1 cgd if (!initerr)
458 1.35 rillig initstack_check_too_many();
459 1.1 cgd if (!initerr)
460 1.35 rillig initstack_push();
461 1.26 christos if (!initerr) {
462 1.1 cgd initstk->i_brace = 1;
463 1.26 christos DPRINTF(("%s(): %p %s brace=%d\n", __func__,
464 1.30 rillig namedmem,
465 1.30 rillig tyname(buf, sizeof(buf),
466 1.30 rillig initstk->i_type ? initstk->i_type
467 1.30 rillig : initstk->i_subt),
468 1.30 rillig initstk->i_brace));
469 1.26 christos }
470 1.1 cgd }
471 1.1 cgd }
472 1.1 cgd
473 1.1 cgd void
474 1.34 rillig init_lbrace(void)
475 1.1 cgd {
476 1.26 christos DPRINTF(("%s\n", __func__));
477 1.7 lukem
478 1.1 cgd if (initerr)
479 1.1 cgd return;
480 1.1 cgd
481 1.1 cgd if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
482 1.1 cgd initstk->i_nxt == NULL) {
483 1.29 rillig if (tflag && !tspec_is_scalar(initstk->i_subt->t_tspec))
484 1.1 cgd /* no automatic aggregate initialization in trad. C*/
485 1.1 cgd warning(188);
486 1.1 cgd }
487 1.1 cgd
488 1.1 cgd /*
489 1.1 cgd * Remove all entries which cannot be used for further initializers
490 1.1 cgd * and do not expect a closing brace.
491 1.1 cgd */
492 1.36 rillig initstack_pop_nobrace();
493 1.1 cgd
494 1.35 rillig initstack_next(1);
495 1.1 cgd }
496 1.1 cgd
497 1.1 cgd void
498 1.34 rillig init_rbrace(void)
499 1.1 cgd {
500 1.26 christos DPRINTF(("%s\n", __func__));
501 1.7 lukem
502 1.1 cgd if (initerr)
503 1.1 cgd return;
504 1.1 cgd
505 1.36 rillig initstack_pop_brace();
506 1.1 cgd }
507 1.1 cgd
508 1.1 cgd void
509 1.7 lukem mkinit(tnode_t *tn)
510 1.1 cgd {
511 1.1 cgd ptrdiff_t offs;
512 1.1 cgd sym_t *sym;
513 1.1 cgd tspec_t lt, rt;
514 1.1 cgd tnode_t *ln;
515 1.1 cgd struct mbl *tmem;
516 1.1 cgd scl_t sc;
517 1.12 christos #ifdef DEBUG
518 1.19 christos char buf[64], sbuf[64];
519 1.12 christos #endif
520 1.1 cgd
521 1.34 rillig DPRINTF(("%s(%s %s)\n", __func__,
522 1.34 rillig tyname(buf, sizeof(buf), tn->tn_type),
523 1.34 rillig print_tnode(sbuf, sizeof(sbuf), tn)));
524 1.1 cgd if (initerr || tn == NULL)
525 1.25 christos return;
526 1.1 cgd
527 1.1 cgd sc = initsym->s_scl;
528 1.1 cgd
529 1.1 cgd /*
530 1.13 christos * Do not test for automatic aggregate initialisation. If the
531 1.9 wiz * initializer starts with a brace we have the warning already.
532 1.1 cgd * If not, an error will be printed that the initializer must
533 1.1 cgd * be enclosed by braces.
534 1.1 cgd */
535 1.1 cgd
536 1.1 cgd /*
537 1.1 cgd * Local initialisation of non-array-types with only one expression
538 1.1 cgd * without braces is done by ASSIGN
539 1.1 cgd */
540 1.1 cgd if ((sc == AUTO || sc == REG) &&
541 1.1 cgd initsym->s_type->t_tspec != ARRAY && initstk->i_nxt == NULL) {
542 1.1 cgd ln = getnnode(initsym, 0);
543 1.1 cgd ln->tn_type = tduptyp(ln->tn_type);
544 1.1 cgd ln->tn_type->t_const = 0;
545 1.1 cgd tn = build(ASSIGN, ln, tn);
546 1.25 christos expr(tn, 0, 0, 0);
547 1.25 christos return;
548 1.1 cgd }
549 1.1 cgd
550 1.1 cgd /*
551 1.1 cgd * Remove all entries which cannot be used for further initializers
552 1.1 cgd * and do not require a closing brace.
553 1.1 cgd */
554 1.36 rillig initstack_pop_nobrace();
555 1.1 cgd
556 1.35 rillig /* Initialisations by strings are done in initstack_string(). */
557 1.35 rillig if (initstack_string(tn))
558 1.25 christos return;
559 1.1 cgd
560 1.35 rillig initstack_next(0);
561 1.1 cgd if (initerr || tn == NULL)
562 1.25 christos return;
563 1.1 cgd
564 1.1 cgd initstk->i_cnt--;
565 1.26 christos DPRINTF(("%s() cnt=%d tn=%p\n", __func__, initstk->i_cnt, tn));
566 1.1 cgd /* Create a temporary node for the left side. */
567 1.1 cgd ln = tgetblk(sizeof (tnode_t));
568 1.1 cgd ln->tn_op = NAME;
569 1.1 cgd ln->tn_type = tduptyp(initstk->i_type);
570 1.1 cgd ln->tn_type->t_const = 0;
571 1.1 cgd ln->tn_lvalue = 1;
572 1.1 cgd ln->tn_sym = initsym; /* better than nothing */
573 1.1 cgd
574 1.1 cgd tn = cconv(tn);
575 1.1 cgd
576 1.1 cgd lt = ln->tn_type->t_tspec;
577 1.1 cgd rt = tn->tn_type->t_tspec;
578 1.1 cgd
579 1.29 rillig if (!tspec_is_scalar(lt))
580 1.11 christos LERROR("mkinit()");
581 1.1 cgd
582 1.1 cgd if (!typeok(INIT, 0, ln, tn))
583 1.25 christos return;
584 1.1 cgd
585 1.1 cgd /*
586 1.1 cgd * Store the tree memory. This is nessesary because otherwise
587 1.1 cgd * expr() would free it.
588 1.1 cgd */
589 1.1 cgd tmem = tsave();
590 1.15 christos expr(tn, 1, 0, 1);
591 1.1 cgd trestor(tmem);
592 1.7 lukem
593 1.29 rillig if (tspec_is_int(lt) && ln->tn_type->t_isfield && !tspec_is_int(rt)) {
594 1.1 cgd /*
595 1.1 cgd * Bit-fields can be initialized in trad. C only by integer
596 1.1 cgd * constants.
597 1.1 cgd */
598 1.1 cgd if (tflag)
599 1.1 cgd /* bit-field initialisation is illegal in trad. C */
600 1.1 cgd warning(186);
601 1.1 cgd }
602 1.1 cgd
603 1.1 cgd if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON))
604 1.1 cgd tn = convert(INIT, 0, initstk->i_type, tn);
605 1.1 cgd
606 1.1 cgd if (tn != NULL && tn->tn_op != CON) {
607 1.1 cgd sym = NULL;
608 1.1 cgd offs = 0;
609 1.1 cgd if (conaddr(tn, &sym, &offs) == -1) {
610 1.3 jpo if (sc == AUTO || sc == REG) {
611 1.1 cgd /* non-constant initializer */
612 1.24 christos (void)c99ism(177);
613 1.1 cgd } else {
614 1.1 cgd /* non-constant initializer */
615 1.1 cgd error(177);
616 1.1 cgd }
617 1.1 cgd }
618 1.1 cgd }
619 1.1 cgd }
620 1.1 cgd
621 1.1 cgd
622 1.1 cgd static int
623 1.35 rillig initstack_string(tnode_t *tn)
624 1.1 cgd {
625 1.1 cgd tspec_t t;
626 1.1 cgd istk_t *istk;
627 1.1 cgd int len;
628 1.1 cgd strg_t *strg;
629 1.1 cgd
630 1.1 cgd if (tn->tn_op != STRING)
631 1.33 rillig return 0;
632 1.1 cgd
633 1.1 cgd istk = initstk;
634 1.1 cgd strg = tn->tn_strg;
635 1.1 cgd
636 1.1 cgd /*
637 1.1 cgd * Check if we have an array type which can be initialized by
638 1.1 cgd * the string.
639 1.1 cgd */
640 1.12 christos if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
641 1.26 christos DPRINTF(("%s: subt array\n", __func__));
642 1.1 cgd t = istk->i_subt->t_subt->t_tspec;
643 1.1 cgd if (!((strg->st_tspec == CHAR &&
644 1.1 cgd (t == CHAR || t == UCHAR || t == SCHAR)) ||
645 1.1 cgd (strg->st_tspec == WCHAR && t == WCHAR))) {
646 1.33 rillig return 0;
647 1.1 cgd }
648 1.1 cgd /* Put the array at top of stack */
649 1.35 rillig initstack_push();
650 1.1 cgd istk = initstk;
651 1.1 cgd } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
652 1.26 christos DPRINTF(("%s: type array\n", __func__));
653 1.1 cgd t = istk->i_type->t_subt->t_tspec;
654 1.1 cgd if (!((strg->st_tspec == CHAR &&
655 1.1 cgd (t == CHAR || t == UCHAR || t == SCHAR)) ||
656 1.1 cgd (strg->st_tspec == WCHAR && t == WCHAR))) {
657 1.33 rillig return 0;
658 1.1 cgd }
659 1.1 cgd /*
660 1.1 cgd * If the array is already partly initialized, we are
661 1.1 cgd * wrong here.
662 1.1 cgd */
663 1.1 cgd if (istk->i_cnt != istk->i_type->t_dim)
664 1.33 rillig return 0;
665 1.1 cgd } else {
666 1.33 rillig return 0;
667 1.1 cgd }
668 1.1 cgd
669 1.1 cgd /* Get length without trailing NUL character. */
670 1.1 cgd len = strg->st_len;
671 1.1 cgd
672 1.1 cgd if (istk->i_nolimit) {
673 1.1 cgd istk->i_nolimit = 0;
674 1.1 cgd istk->i_type->t_dim = len + 1;
675 1.32 rillig setcomplete(istk->i_type, 1);
676 1.1 cgd } else {
677 1.1 cgd if (istk->i_type->t_dim < len) {
678 1.1 cgd /* non-null byte ignored in string initializer */
679 1.1 cgd warning(187);
680 1.1 cgd }
681 1.1 cgd }
682 1.1 cgd
683 1.1 cgd /* In every case the array is initialized completely. */
684 1.1 cgd istk->i_cnt = 0;
685 1.1 cgd
686 1.33 rillig return 1;
687 1.1 cgd }
688