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