1 /*
2 * Copyright (c) 2004 - 2010, Nils R. Weller
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Expression parser
28 */
29
30 #include "expr.h"
31 #include <stdlib.h>
32 #include <string.h>
33 #include <limits.h>
34 #include "token.h"
35 #include "error.h"
36 #include "misc.h"
37 #include "defs.h"
38
39 #ifndef PREPROCESSOR
40 # include "decl.h"
41 # include "scope.h"
42 # include "icode.h"
43 # include "debug.h"
44 # include "cc1_main.h"
45 # include "functions.h"
46 # include "analyze.h"
47 # include "zalloc.h"
48 # include "symlist.h"
49 # include "backend.h"
50 # include "reg.h"
51 #else
52 # include "archdefs.h"
53 # include "../features.h" /* XXX */
54 #endif
55
56 #include "type.h"
57 #include "subexpr.h"
58 #include "typemap.h"
59 #include "evalexpr.h"
60 #include "libnwcc.h"
61 #include "n_libc.h"
62
63 void
append_expr(struct expr ** head,struct expr ** tail,struct expr * e)64 append_expr(struct expr **head, struct expr **tail, struct expr *e) {
65 if (*head == NULL) {
66 *head = *tail = e;
67 } else {
68 (*tail)->next = e;
69 e->prev = *tail;
70 *tail = (*tail)->next;
71 }
72 }
73
74 struct expr *
alloc_expr(void)75 alloc_expr(void) {
76 #if USE_ZONE_ALLOCATOR
77 return zalloc_buf(Z_EXPR);
78 #else
79 struct expr *ret = n_xmalloc(sizeof *ret);
80 static struct expr nullexpr;
81 *ret = nullexpr;
82 return ret;
83 #endif
84 }
85
86 struct expr *
dup_expr(struct expr * ex)87 dup_expr(struct expr *ex) {
88 struct expr *ret = n_xmalloc(sizeof *ret);
89 *ret = *ex;
90 return ret;
91 }
92
93
94 void recover(struct token **tok, int delim, int delim2);
95
96 static int
ambig_to_binary(struct token * t)97 ambig_to_binary(struct token *t) {
98 int op = *(int *)t->data;
99 switch (op) {
100 case TOK_OP_AMB_PLUS:
101 op = TOK_OP_PLUS;
102 break;
103 case TOK_OP_AMB_MINUS:
104 op = TOK_OP_MINUS;
105 break;
106 case TOK_OP_AMB_MULTI:
107 op = TOK_OP_MULTI;
108 break;
109 case TOK_OP_AMB_BAND:
110 op = TOK_OP_BAND;
111 break;
112 case TOK_OP_AMB_COND2:
113 op = TOK_OP_COND2;
114 break;
115 }
116
117 if (!IS_BINARY(op) && op != TOK_OP_COND2) {
118 errorfl(t, "Invalid operator ``%s'' - "
119 "binary or ternary operator expected", t->ascii);
120 return -1;
121 }
122 *(int *)t->data = op; /* XXXXXXXXXXXXXXXXXXXXXXXXXXXX ok? */
123 return op;
124 }
125
126
127 int
expr_ends(struct token * t,int delim,int delim2)128 expr_ends(struct token *t, int delim, int delim2) {
129 int type;
130
131 if (t->type == TOK_OPERATOR) {
132 type = *(int *)t->data;
133 } else {
134 type = t->type;
135 }
136 if (type == delim || type == delim2) {
137 return 1;
138 }
139 return 0;
140 }
141
142 /*
143 * Get lowest precedence operator in an expression. Note that the
144 * conditional operator requires special handling; In
145 * foo? bar: baz;
146 * ... ? and : act like parentheses, i.e. lower precedence operators
147 * such as the comma operator may occur between them.
148 */
149 static struct operator *
get_lowest_prec(struct expr * ex,struct expr ** endp,int want_condop)150 get_lowest_prec(struct expr *ex, struct expr **endp, int want_condop) {
151 int lowest = 100;
152 int cond_expr = 0;
153 struct expr *lptr = NULL;
154
155 *endp = NULL;
156 for (; ex != NULL; ex = ex->next) {
157 struct operator *tmp;
158
159 if (ex->op == 0 || ex->used) {
160 continue;
161 }
162 tmp = &operators[LOOKUP_OP2(ex->op)];
163 /*
164 * 08/22/07: This flag had to be added because otherwise
165 * the search loop for the second part of the conditional
166 * operator didn't work right in
167 *
168 * 1? 0: 1? 1,1 : 2;
169 */
170 if (want_condop
171 && ex->op != TOK_OP_COND
172 && ex->op != TOK_OP_COND2) {
173 continue;
174 }
175 if (!cond_expr) {
176 if (tmp->prec < lowest) {
177 lowest = tmp->prec;
178 lptr = ex;
179 } else if (tmp->prec == lowest) {
180 if (tmp->assoc != OP_ASSOC_RIGHT) {
181 lptr = ex;
182 }
183 }
184 }
185
186 if (ex->op == TOK_OP_COND) {
187 ++cond_expr;
188 } else if (ex->op == TOK_OP_COND2) {
189 if (cond_expr == 0) {
190 if (want_condop) {
191 /*
192 * Added... Otherwise stuff breaks
193 * because ? is selected rather than
194 * :
195 */
196 lptr = ex;
197 break;
198 }
199 }
200 --cond_expr;
201 }
202 }
203 *endp = lptr;
204 return lptr? (void *)&operators[LOOKUP_OP2(lptr->op)]: (void *)NULL;
205 }
206
207
208 #if EVAL_CONST_EXPR_CT
209
210 struct token *
replace_const_subexpr(struct expr * endp,struct token * restok0)211 replace_const_subexpr(struct expr *endp, struct token *restok0) {
212 struct token *restok;
213 struct num *n;
214 struct type *need_cast = NULL;
215
216 if (endp->const_value->str != NULL
217 || endp->const_value->address != NULL) {
218 return NULL;
219 } else if (endp->const_value->type->tlist != NULL) {
220 /*
221 * This means we have a constant value which is used as an
222 * address. This may be something like
223 *
224 * &((struct foo *)0)->bar
225 *
226 * ... i.e. the canonical offsetof implementation. In this
227 * case the sub-expression is an integer, but we cannot
228 * just create a token of integer type since then the type
229 * would not be correct.
230 *
231 * So we use the token with the value and append a cast to
232 * the pointer type to the sub-expression
233 */
234 need_cast = endp->const_value->type;
235 } else if (!is_integral_type(endp->const_value->type)
236 && !is_floating_type(endp->const_value->type)) {
237 /*
238 * 07/11/08: Need this to prevent compound literals (struct
239 * types) from being handled as constants
240 * XXX ... which I guess should be possible though!
241 */
242 return NULL;
243 }
244
245 if (restok0 == NULL) {
246 restok = alloc_token();
247 restok->data = endp->const_value->value;
248 restok->type = endp->const_value->type->code;
249 } else {
250 restok = restok0;
251 }
252
253 #if ZALLOC_USE_FREELIST
254 if (endp->op != 0) {
255 if (endp->left != NULL) {
256 zalloc_free_expr(endp->left);
257 }
258 if (endp->right != NULL) {
259 zalloc_free_expr(endp->right);
260 }
261 }
262 #endif
263 endp->op = 0;
264
265 endp->left = NULL;
266 endp->right = NULL;
267 endp->data = alloc_s_expr();
268 endp->data->meat = restok;
269
270
271 if (need_cast != NULL) {
272 #ifndef PREPROCESSOR
273 endp->data->type = backend->get_size_t();
274 #else
275 endp->data->type = make_basic_type(TY_ULONG); /* XXX */
276 #endif
277 endp->data->meat->type = endp->data->type->code;
278 } else {
279 endp->data->type = endp->const_value->type;
280 }
281
282
283 /*
284 * If this is a floating point or long long
285 * constant, we may have to create a new
286 * symbolic label for it, so it must be
287 * saved in the corresponding constant list
288 */
289 if (IS_FLOATING(restok->type)) {
290 n = n_xmalloc(sizeof *n);
291 n->value = restok->data;
292 n->type = restok->type;
293 restok->data = put_float_const_list(n);
294 } else if ((IS_LLONG(restok->type)
295 #ifndef PREPROCESSOR
296 && backend->arch == ARCH_POWER)
297 #else
298 && target_info->arch_info->arch == ARCH_POWER)
299 #endif
300 /*
301 * 10/17/08: Check for long on PPC64 was missing
302 */
303 #ifndef PREPROCESSOR
304 || (backend->abi == ABI_POWER64
305 #else
306 || (target_info->arch_info->abi == ABI_POWER64
307 #endif
308 && IS_LONG(restok->type))) {
309 n = n_xmalloc(sizeof *n);
310 n->value = restok->data;
311 n->type = restok->type;
312 /*restok->data = n;*/
313 put_ppc_llong(n);
314 restok->data2 = llong_const; /* 10/17/08: Missing! */
315 }
316
317 if (need_cast != NULL) {
318 endp->data->operators[0] = make_cast_token(need_cast);
319 endp->data->operators[1] = NULL;
320 }
321 return restok;
322 }
323
324
325
326
327 static void
eval_const_subexpr(struct expr * endp)328 eval_const_subexpr(struct expr *endp) {
329 struct token *ltok = endp->left->data->meat;
330 int rc;
331 int not_constant;
332
333 if (ltok != NULL
334 && IS_CONSTANT(ltok->type)
335 && ltok->type != TOK_STRING_LITERAL) {
336 struct vreg *first;
337 struct vreg *second;
338 struct type *restype;
339 struct tyval tv1;
340 struct tyval tv2;
341 struct expr *winner;
342 struct vreg *winner_vr;
343
344 rc = eval_const_expr(endp->left,
345 EXPR_OPTCONSTSUBEXPR,
346 ¬_constant);
347 if (rc == -1) {
348 return;
349 }
350 /*
351 * 07/01/08: Condition is constant - we
352 * can already pick an operand
353 */
354
355 first = expr_to_icode(endp->right->left,
356 NULL, NULL, 0, 0, 0);
357 second = expr_to_icode(endp->right->right,
358 NULL, NULL, 0, 0, 0);
359
360 if (first == NULL || second == NULL) {
361 return;
362 }
363
364 /*
365 * Now that we have the types
366 * of both sides, it is possible
367 * to perform usual arithmetic
368 * conversions
369 */
370
371 tv1.value = tv2.value = NULL;
372 tv1.type = first->type;
373 tv2.type = second->type;
374
375 /*
376 * 07/15/08: Warn about ptr vs
377 * non-null integer constant
378 */
379 if (tv1.type->tlist || tv2.type->tlist) {
380 if (tv1.type->tlist == NULL) {
381 if (const_value_is_nonzero(
382 endp->right->left->const_value)) {
383 warningfl(endp->tok,
384 "Result of conditional operator has variable type");
385 }
386 } else if (tv2.type->tlist == NULL) {
387 if (const_value_is_nonzero(
388 endp->right->right->const_value)) {
389 warningfl(endp->tok,
390 "Result of conditional operator has variable type");
391 }
392 } else if (tv2.type->code != tv1.type->code) {
393 if (tv1.type->code == TY_VOID
394 && tv1.type->tlist->next == NULL) {
395 } else if (tv2.type->code == TY_VOID
396 && tv2.type->tlist->next == NULL) {
397 } else {
398 warningfl(endp->tok,
399 "Result of conditional operator has variable type");
400 }
401 }
402 } else if ( (tv1.type->code == TY_STRUCT || tv1.type->code == TY_UNION)
403 || (tv2.type->code == TY_STRUCT || tv2.type->code == TY_UNION) ) {
404 if (tv1.type->code != tv2.type->code
405 || tv1.type->tstruc != tv2.type->tstruc) {
406 errorfl(endp->tok,
407 "Result of conditional operator has variable type");
408 }
409 }
410
411 cross_convert_tyval(&tv1, &tv2,
412 &restype);
413 if (const_value_is_nonzero(
414 endp->left->const_value)) {
415 /* Use first operand */
416 winner = endp->right->left;
417 winner_vr = first;
418 } else {
419 /* Use second operand */
420 winner = endp->right->right;
421 winner_vr = second;
422 }
423 endp->op = 0;
424 endp->left = NULL;
425 endp->right = NULL;
426
427 /*
428 * We store the winning
429 * operand as a parenthesized
430 * sub-expression
431 */
432 endp->data = alloc_s_expr();
433 endp->data->is_expr = winner;
434 endp->data->flags |= SEXPR_FROM_CONST_EXPR;
435 if (restype != NULL
436 && (winner_vr->type->code != restype->code
437 || (winner_vr->type->tlist
438 != restype->tlist))) {
439 /*
440 * XXX that tlist comp.
441 * really what we want?
442 * may yield
443 * unnecessary conv
444 */
445 /*
446 * Prepend a cast op to
447 * the sub-expression,
448 * which is the easiest
449 * way to honor the
450 * usual arithmetic
451 * conversion!
452 */
453 endp->data->operators[0] =
454 make_cast_token(restype);
455 endp->data->operators[1] = NULL;
456 }
457 }
458 }
459
460
461
462
463
464 static void
eval_const_part_expr(struct expr * endp)465 eval_const_part_expr(struct expr *endp) {
466 struct token *ltok = NULL;
467 struct token *rtok = NULL;
468 int rc;
469 int not_constant;
470
471 if (endp->right->op != TOK_OP_COND2) {
472 ltok = endp->left->data->meat;
473 rtok = endp->right->data->meat;
474 }
475
476 if (ltok && rtok
477 && IS_CONSTANT(ltok->type)
478 && IS_CONSTANT(rtok->type)) {
479 /*
480 * A constant sub-expression - evaluate it at
481 * this point already! Note that endp already
482 * has the sub-tree for the two items, so we
483 * can just pass it to the evaluation function
484 */
485 rc = eval_const_expr(endp,
486 EXPR_OPTCONSTSUBEXPR,
487 ¬_constant);
488
489 /*
490 * There is a small possibility that the result
491 * is not constant, e.g. if it did something
492 * like *(int *)1234, so check for that
493 */
494 if (rc != -1) { /* !not_constant */
495 /*
496 * Evaluation succeeded! Replace
497 *
498 * operand op operand
499 *
500 * with a single token containing the
501 * evaluated result
502 */
503 (void) replace_const_subexpr(endp, NULL);
504 }
505 } else if (endp->op == TOK_OP_LAND
506 || endp->op == TOK_OP_LOR) {
507 /*
508 * 07/01/08: We have to optimize variable access in
509 * constant expressions away if it can already be
510 * done at compile time! Examples
511 *
512 * 1? 123: var 123 wins anyway
513 * 0 && var First operand already false anyway
514 * 1 || var First operand already true anyway
515 *
516 * In particular, glibc depends on the conditional
517 * operator version being optimized! It uses this to
518 * flag compile-time errors at link time, i.e. if the
519 * compiler does something wrong (wrong macro settings
520 * or somesuch), the condition becomes false and the
521 * undefined, externally declared variable is accessed.
522 * This yields a linker error indicating the problem.
523 * If the condition is true instead, the variable
524 * access is optimized away and there is no linker
525 * error
526 */
527 int new_value = -1;
528 int not_zero;
529
530 if (ltok && IS_CONSTANT(ltok->type)) {
531 /*
532 * Left side is (probably) constant, but right
533 * isn't
534 */
535
536 rc = eval_const_expr(endp->left,
537 EXPR_OPTCONSTSUBEXPR,
538 ¬_constant);
539
540
541 /*
542 * There is a small possibility that the result
543 * is not constant, e.g. if it did something
544 * like *(int *)1234, so check for that
545 */
546 if (rc != -1) { /* !not_constant */
547 /*
548 * Evaluation succeeded!
549 */
550 not_zero = const_value_is_nonzero(
551 endp->left->const_value);
552 if (endp->op == TOK_OP_LOR) {
553 if (not_zero) {
554 /*
555 * First operand already
556 * nonzero, so we can
557 * ignore the second one
558 */
559 new_value = 1;
560 endp->const_value =
561 endp->left->const_value;
562 }
563 } else {
564 if (!not_zero) {
565 /*
566 * First operand already
567 * zero, so we can
568 * ignore the second one
569 */
570 new_value = 0;
571 endp->const_value =
572 endp->left->const_value;
573 }
574 }
575 }
576 } else if (rtok && IS_CONSTANT(rtok->type)) {
577 /*
578 * Right side is constant, but left isn't
579 */
580 }
581
582 if (new_value != -1) {
583 (void) const_from_value(&new_value, /* XXX 12/07/24: needed? */
584 make_basic_type(TY_INT));
585 replace_const_subexpr(endp, NULL);
586 /* XXX delete old nodes */
587 }
588 }
589 }
590
591 #endif /* #if EVAL_CONST_EXPR_CT (disabled for preprocessor) */
592
593
594 static struct expr *
bind_operators(struct expr * ex)595 bind_operators(struct expr *ex) {
596 struct operator *op;
597 struct expr *endp;
598 struct expr *start = ex;
599 int rc;
600 int not_constant;
601
602
603 if (ex == NULL) {
604 return NULL;
605 }
606 if ((op = get_lowest_prec(ex, &endp, 0)) == NULL) {
607 /* No more operators left */
608 if (!ex->used) {
609 endp = ex;
610 endp->used = 1;
611 #if 0
612 /*
613 * This must be a sub-expression, check whether it is (probably)
614 * constant
615 */
616 if (endp->data == NULL) {
617 warningfl(NULL, "BUG? Expression tree leaf has no data");
618 } else {
619 endp->is_const = check_sexpr_is_const(endp->data);
620 }
621 #endif
622 /*
623 * 07/04/08: This was missing; Properly evaluate constant
624 * sub-expressions. Otherwise things like parenthesized
625 * sub-expressions are not combined properly, since the
626 * parentheses are confusing
627 *
628 * 11/08/08: Missed check for compound literals
629 */
630 if ((endp->data->meat && endp->data->meat->type == TOK_STRING_LITERAL)
631 || (endp->data->meat && endp->data->meat->type == TOK_COMP_LITERAL)
632 || (endp->data->is_expr
633 && endp->data->is_expr->data
634 && endp->data->is_expr->data->meat
635 && endp->data->is_expr->data->meat->type == TOK_STRING_LITERAL)) {
636 rc = -1;
637 } else if (endp->op == 0 && endp->data != NULL) {
638 /*
639 * 07/07/08: Have to check for op->data, since this may e.g. be
640 * stmt_as_expr instead of a normal sub-expression!
641 */
642 rc = eval_const_expr(endp,
643 EXPR_OPTCONSTSUBEXPR,
644 ¬_constant);
645 } else {
646 rc = -1;
647 }
648
649 /*
650 * There is a small possibility that the result
651 * is not constant, e.g. if it did something
652 * like *(int *)1234, so check for that
653 */
654 if (rc != -1) { /* !not_constant */
655 #ifndef PREPROCESSOR /* XXX Should this be EVAL_CONST_EXPR_CT instead? */
656 /*
657 * Evaluation succeeded! Replace
658 *
659 * operand op operand
660 *
661 * with a single token containing the
662 * evaluated result
663 */
664 (void) replace_const_subexpr(endp, NULL);
665 #endif
666 }
667 } else {
668 endp = NULL;
669 }
670
671 return endp;
672 } else if (op->value == TOK_OP_COND) {
673 struct expr *endp2;
674 struct expr *ex2 = endp->next;
675
676 /*
677 * Need to get second part of conditional operator. This
678 * requires skipping intermediate conditional operators.
679 *
680 * 08/22/07: This loop is nonsense since we added the
681 * want_condop flag to get_lowest_prec()
682 */
683 /* for (;;) {*/
684 if (ex2 == NULL
685 || (op = get_lowest_prec(ex2, &endp2, 1))
686 == NULL) {
687 errorfl(ex2? ex2->tok: endp->tok,
688 "Parse error - missing second part"
689 " of conditional operator");
690 return NULL;
691 }
692 #if 0
693 } else if (op->value == TOK_OP_COND) {
694 ++cond1;
695 } else if (op->value == TOK_OP_COND2) {
696 if (cond1 == 0) {
697 /* done! */
698 break;
699 } else {
700 --cond1;
701 }
702 }
703 #endif
704 /* ex2 = endp2->next;*/
705 /* } */
706
707 /*
708 * At this point, endp points to ``?'' and endp2 to ``:''. We
709 * bind them such that:
710 * Given x = ``?''
711 * ... x->left = cond-expr
712 * ... x->right = :
713 * ... x->right->left = expr-nonzero
714 * ... x->right->right = expr-zero
715 */
716 endp->used = endp2->used = 1;
717 ex = endp->next;
718 endp->next = NULL;
719 endp->left = bind_operators(start);
720 endp->right = endp2;
721 ex2 = endp2->next;
722 endp2->next = NULL;
723 endp->right->left = bind_operators(ex);
724 endp->right->right = bind_operators(ex2);
725
726 #if EVAL_CONST_EXPR_CT
727 if (endp->left->op == 0 && endp->left->data != NULL) {
728 eval_const_subexpr(endp);
729 }
730 #endif
731 return endp;
732 }
733
734 endp->used = 1;
735
736 ex = endp->next;
737 endp->next = NULL;
738
739 endp->left = bind_operators(start);
740 endp->right = bind_operators(ex);
741
742 #if EVAL_CONST_EXPR_CT
743 if (endp->left->op == 0 && endp->right->op == 0
744 && endp->left->data != NULL && endp->right->data != NULL) {
745 eval_const_part_expr(endp);
746 }
747 #endif
748
749 return endp;
750 }
751
752
753 #ifndef PREPROCESSOR
754
755 static void
append_init_list(struct initializer ** init,struct initializer ** init_tail,struct initializer * i)756 append_init_list(
757 struct initializer **init,
758 struct initializer **init_tail,
759 struct initializer *i) {
760
761 if (i->type == 0) abort();
762 if (*init == NULL) {
763 *init = *init_tail = i;
764 } else {
765 (*init_tail)->next = i;
766 i->prev = *init_tail;
767 *init_tail = (*init_tail)->next;
768 }
769 }
770
771 struct initializer *
alloc_initializer(void)772 alloc_initializer(void) {
773 #if USE_ZONE_ALLOCATOR
774 return zalloc_buf(Z_INITIALIZER);
775 #else
776 static struct initializer nullinit;
777 struct initializer *ret;
778 ret = n_xmalloc(sizeof *ret);
779 *ret = nullinit;
780 return ret;
781 #endif
782 }
783
784 struct initializer *
dup_initializer(struct initializer * init)785 dup_initializer(struct initializer *init) {
786 struct initializer *ret = alloc_initializer();
787 *ret = *init;
788 return ret;
789 }
790
791 static void
conv_init(struct tyval * tv,struct type * toty,struct token * t)792 conv_init(struct tyval *tv, struct type *toty, struct token *t) {
793 struct type *fromty = tv->type;
794 struct type_node *ltn;
795 struct type_node *rtn;
796
797 if (toty->tlist == NULL) {
798 /* Simple basic type conversion */
799 if (tv->address != NULL) {
800 /* Must be pointer to int cast... Dangerous!!! */
801 return;
802 }
803 if (toty->code != fromty->code) {
804 cross_do_conv(tv, toty->code, 1);
805 }
806 return;
807 }
808
809 if (fromty->tlist == NULL ||
810 (toty->code != fromty->code
811 && (toty->code != TY_VOID && fromty->code != TY_VOID))) {
812 /* this bullshit doesn't work */
813 return;
814
815 #if 0
816 #if 0
817 /* XXX */ && (!IS_CHAR(toty->code) || !IS_CHAR(fromty->code)))) {
818 #endif
819 if (!tv->is_nullptr_const) {
820 errorfl(t, "Initializer of incompatible type");
821 return;
822 } else {
823 /* Explains various mysteries - Don't check tlists */
824 return;
825 }
826 #endif
827 }
828
829
830 /*
831 * Must be assignment to pointer since this function is only
832 * called for scalar types
833 */
834
835 if (tv->is_nullptr_const) {
836 return;
837 }
838 if (toty->code == TY_VOID
839 && toty->tlist->type == TN_POINTER_TO
840 && toty->tlist->next == NULL) {
841 /* Assignment to void pointer */
842 return;
843 }
844
845
846 ltn = toty->tlist;
847 rtn = fromty->tlist;
848
849 if (rtn && rtn->type == TN_FUNCTION) {
850 if (ltn->type == TN_POINTER_TO) {
851 /* Probably pointer to function */
852 ltn = ltn->next;
853 }
854 }
855
856 for (; ltn != NULL && rtn != NULL;
857 ltn = ltn->next, rtn = rtn->next) {
858 if (ltn->type != rtn->type) {
859 if (rtn->type == TN_ARRAY_OF
860 && rtn == fromty->tlist) {
861 /* OK - assign array address to ptr */
862 ;
863 } else {
864 errorfl(t, "Initializer of incompatible type");
865 return;
866 }
867 }
868 }
869
870 if (ltn != rtn) {
871 /* One type list is longer */
872 errorfl(t, "Initializer of incompatible type");
873 return;
874 }
875 }
876
877 struct desig_init_data {
878 struct initializer **init_head;
879 struct initializer **init_tail;
880 struct initializer *cur_init_ptr;
881 struct sym_entry *highest_encountered_member;
882 int highest_encountered_index;
883 int real_items_read;
884 /* 07/18/08: Record type (to distinguish union/struct) */
885 struct type *type;
886 struct sym_entry *last_encountered_member; /* For union null padding */
887 };
888
889 static void
890 replace_cur_init(struct desig_init_data *data, struct initializer *init) {
891 if (data->cur_init_ptr->prev) {
892 data->cur_init_ptr->prev->next = init;
893 } else {
894 *data->init_head = init;
895 }
896 if (data->cur_init_ptr->next) {
897 data->cur_init_ptr->next->prev = init;
898 } else {
899 *data->init_tail = init;
900 }
901 init->next = data->cur_init_ptr->next;
902 init->prev = data->cur_init_ptr->prev;
903 free/*initializer*/(data->cur_init_ptr);
904 data->cur_init_ptr = init->next;
905 }
906
907 /*
908 * Put an initializer into the designated initializer list. As soon as the
909 * first designated initializer is encountered, this function also has to
910 * be called for all subsequent initializers, since it may have trashed the
911 * initializer order.
912 *
913 * There are four cases to handle here:
914 *
915 * - The initializer is itself designated, and it jumps forward. If that
916 * happens we create a null initializer for every skipped member (this element
917 * may have to be replaced later if another designated initializer jumps back
918 * in the order)
919 *
920 * - The initializer is itself designated, and it jumps backward. In that
921 * case we have to jump back and OVERWRITE an existing explicit data or null
922 * initializer. Also, the current ``initializer pointer'' must be moved to
923 * that location
924 *
925 * - The initializer is not designated, but overwrites an existing
926 * initializer because the current initializer pointer points to one
927 *
928 * - The initializer is not designated and just appended at the end of
929 * the list
930 */
931 static void
932 put_desig_init_struct(struct desig_init_data *data,
933 struct sym_entry *desig_se,
934 struct sym_entry *prev_se,
935 struct sym_entry *whole_struct,
936 struct initializer *init,
937 struct token *tok,
938 int *items_read) {
939
940 int skipped = 0;
941
942 if (desig_se != NULL
943 && prev_se != NULL
944 && desig_se->dec->offset == prev_se->dec->offset) {
945 /*
946 * Designating the initializer which would we done now
947 * anyway - ignore designation.
948 *
949 * 07/18/08: Note that this also applies to unions, where
950 * every member has offset 0!
951 */
952 desig_se = NULL;
953 }
954
955
956 if (desig_se == NULL) {
957 if (data->type->code == TY_UNION
958 && data->init_head != NULL) {
959 if (*data->init_head != NULL) {
960 free(*data->init_head);
961 *data->init_head = NULL;
962 }
963 append_init_list(data->init_head,
964 data->init_tail, init);
965 } else {
966 /* Not designated, thank heaven */
967 if (data->cur_init_ptr == NULL) {
968 /* At end too! */
969 append_init_list(data->init_head,
970 data->init_tail, init);
971 } else {
972 /*
973 * We have to trash an existing initializer
974 */
975 if (data->cur_init_ptr->type != INIT_NULL) {
976 warningfl(tok, "Member `%s' already has an "
977 "initializer",
978 prev_se->dec->dtype->name);
979 }
980 replace_cur_init(data, init);
981 }
982 }
983 } else {
984 /*
985 * Designated! But is it forward or backward?
986 */
987 if (prev_se == NULL) {
988 /*
989 * Brute-force search for the target
990 */
991 struct initializer *tmpinit;
992
993 tmpinit = *data->init_head;
994 for (prev_se = whole_struct;
995 prev_se != NULL;
996 prev_se = prev_se->next) {
997 if (prev_se->dec->offset == desig_se->dec->offset) {
998 break;
999 }
1000 if (tmpinit->next == NULL) {
1001 /*
1002 * Fill holes with null initializers
1003 */
1004 struct initializer *nullb;
1005
1006 nullb = backend->make_null_block(NULL,
1007 prev_se->next->dec->dtype,
1008 NULL, 1);
1009 nullb->prev = tmpinit;
1010 tmpinit->next = nullb;
1011 }
1012 tmpinit = tmpinit->next;
1013 ++skipped;
1014 }
1015 if (tmpinit->prev) {
1016 tmpinit->prev->next = init;
1017 } else {
1018 *data->init_head = init;
1019 }
1020 if (tmpinit->next) {
1021 tmpinit->next->prev = init;
1022 } else {
1023 *data->init_tail = init;
1024 }
1025 init->prev = tmpinit->prev;
1026 init->next = tmpinit->next;
1027 data->cur_init_ptr = tmpinit->next;
1028 free/*_initializer XXX */(tmpinit);
1029 *items_read = skipped;
1030 if (skipped == 0) {
1031 *data->init_head = init;
1032 } else if (skipped > *items_read) {
1033 *data->init_tail = init;
1034 }
1035 } else if (desig_se->dec->offset > prev_se->dec->offset) {
1036 int highest_offset =
1037 data->highest_encountered_member?
1038 (int)data->highest_encountered_member->dec->offset:
1039 -1;
1040
1041 /*
1042 * Forward - create a null initializer for every
1043 * skipped member if necessary
1044 */
1045 while (prev_se->dec->offset < desig_se->dec->offset) {
1046 if ((signed long)prev_se->dec->offset
1047 > highest_offset) {
1048 struct initializer *nullb;
1049
1050 nullb = backend->make_null_block(NULL,
1051 prev_se->dec->dtype, NULL, 1);
1052 append_init_list(data->init_head,
1053 data->init_tail, nullb);
1054 }
1055 if (data->cur_init_ptr) {
1056 data->cur_init_ptr =
1057 data->cur_init_ptr->next;
1058 }
1059
1060 ++skipped;
1061 prev_se = prev_se->next;
1062 }
1063 if (data->cur_init_ptr) {
1064 if (data->cur_init_ptr->type != INIT_NULL) {
1065 warningfl(tok, "Member `%s' already "
1066 "has an initializer",
1067 desig_se->dec->dtype->name);
1068 }
1069
1070
1071 replace_cur_init(data, init);
1072 } else {
1073 append_init_list(data->init_head,
1074 data->init_tail,
1075 init);
1076 }
1077 *items_read += skipped;
1078 } else {
1079 /*
1080 * Backward. First search the target initializer.
1081 * We can exploit the fact that every member behind
1082 * us has one initializer already (null if skipped.)
1083 */
1084 struct initializer *tmp;
1085
1086 tmp = *data->init_tail;
1087 prev_se = prev_se->prev;
1088 while (prev_se->dec->offset > desig_se->dec->offset) {
1089 prev_se = prev_se->prev;
1090 tmp = tmp->prev;
1091 ++skipped;
1092 }
1093 *items_read -= skipped;
1094
1095 if (tmp->prev) {
1096 tmp->prev->next = init;
1097 } else {
1098 *data->init_head = init;
1099 }
1100
1101 if (tmp->next) {
1102 tmp->next->prev = init;
1103 } else {
1104 *data->init_tail = init;
1105 }
1106 init->next = tmp->next;
1107 init->prev = tmp->prev;
1108 if (desig_se == whole_struct) {
1109 /* Start */
1110 *data->init_head = init;
1111 }
1112
1113 free/*_initializer XXX */(tmp);
1114 data->cur_init_ptr = init->next;
1115 }
1116 }
1117
1118 if (desig_se != NULL) {
1119 if (data->highest_encountered_member == NULL
1120 || data->highest_encountered_member->dec->offset
1121 < desig_se->dec->offset) {
1122 data->highest_encountered_member = desig_se;
1123 }
1124 data->last_encountered_member = desig_se;
1125 } else {
1126 if (data->cur_init_ptr == NULL) {
1127 /* Appending at end */
1128 data->highest_encountered_member = prev_se;
1129 data->last_encountered_member = prev_se;
1130 }
1131 }
1132 }
1133
1134
1135
1136 static void
1137 put_desig_init_array(struct desig_init_data *data,
1138 int desig_elem,
1139 int *items_read,
1140 struct initializer *init,
1141 struct type *elem_type,
1142 struct token *tok) {
1143
1144 int skipped = 0;
1145 int real_items_read = 0;
1146
1147 /*
1148 * 07/11/09: Wow this kludge, which was added for a designated
1149 * initializer in gcc, totally kills PHP compilation for a file
1150 * with an array that is initialized with many thousands of
1151 * constants. Pity this went into nwcc 0.7.9.
1152 *
1153 * Now we count the number of initializers in the list in a
1154 * separate variable instead. XXX Note that put_desig_init_struct()
1155 * does not do and need this counting yet although it also uses
1156 * a desig_init_data!
1157 */
1158 #if 0
1159 struct initializer *temp;
1160 for (temp = *data->init_head; temp != NULL; temp = temp->next) {
1161 ++real_items_read;
1162 }
1163 #endif
1164 real_items_read = data->real_items_read;
1165
1166
1167 if (desig_elem == *items_read) {
1168 /*
1169 * Designating the initializer which would we done now
1170 * anyway - ignore designation.
1171 */
1172 desig_elem = -1;
1173 }
1174
1175 if (desig_elem == -1) {
1176 /* Not designated, thank heaven */
1177 if (data->cur_init_ptr == NULL) {
1178 /* At end too! */
1179 append_init_list(data->init_head,
1180 data->init_tail, init);
1181 ++data->real_items_read;
1182 } else {
1183 /*
1184 * We have to trash an existing initializer
1185 */
1186 if (data->cur_init_ptr->type != INIT_NULL) {
1187 warningfl(tok, "Element %d already has an "
1188 "initializer",
1189 *items_read);
1190 }
1191
1192 replace_cur_init(data, init);
1193 }
1194 } else {
1195 /*
1196 * Designated! But is it forward or backward?
1197 */
1198 if (desig_elem > *items_read) {
1199 /*
1200 * Forward - create a null initializer for every
1201 * skipped member
1202 */
1203 while (*items_read + skipped < desig_elem) {
1204 if (*items_read + skipped >
1205 data->highest_encountered_index) {
1206 struct initializer *nullb;
1207
1208 nullb = backend->make_null_block(NULL,
1209 elem_type, NULL, 1);
1210 append_init_list(data->init_head,
1211 data->init_tail, nullb);
1212 ++data->real_items_read;
1213 }
1214 if (data->cur_init_ptr) {
1215 data->cur_init_ptr =
1216 data->cur_init_ptr->next;
1217 }
1218
1219 ++skipped;
1220 }
1221 if (data->cur_init_ptr) {
1222 warningfl(tok, "Element %d already has an "
1223 "initializer", desig_elem);
1224 replace_cur_init(data, init);
1225 #if 0
1226 } else if (desig_elem < real_items_read) {
1227 /*
1228 * 06/17/09:
1229 */
1230 #endif
1231 } else {
1232 append_init_list(data->init_head,
1233 data->init_tail, init);
1234 ++data->real_items_read;
1235 }
1236 *items_read += skipped;
1237
1238 if (*items_read == real_items_read) { //at_end_of_list ?) {
1239 data->cur_init_ptr = NULL;
1240 }
1241 } else {
1242 /*
1243 * Backward. First search the target initializer.
1244 * We can exploit the fact that every member behind
1245 * us has one initializer already (null if skipped.)
1246 */
1247 struct initializer *tmp;
1248
1249 if (data->cur_init_ptr != NULL) {
1250 /*
1251 * 06/15/09: We have to start somewhere in
1252 * the list because there was already a
1253 * designated backwards jump
1254 */
1255 tmp = data->cur_init_ptr->prev;
1256 } else {
1257 /* Start at end */
1258 tmp = *data->init_tail;
1259 }
1260 ++skipped;
1261
1262 while (*items_read - skipped > desig_elem) {
1263 tmp = tmp->prev;
1264 ++skipped;
1265 }
1266 *items_read -= skipped;
1267
1268 if (tmp->prev) {
1269 tmp->prev->next = init;
1270 } else {
1271 *data->init_head = init;
1272 }
1273
1274 if (tmp->next) {
1275 tmp->next->prev = init;
1276 } else {
1277 *data->init_tail = init;
1278 }
1279 init->next = tmp->next;
1280 init->prev = tmp->prev;
1281
1282 free/*_initializer XXX */(tmp);
1283 data->cur_init_ptr = init->next;
1284 }
1285 }
1286
1287 if (desig_elem != -1) {
1288 if (desig_elem > data->highest_encountered_index) {
1289 data->highest_encountered_index = desig_elem;
1290 }
1291 } else {
1292 if (data->cur_init_ptr == NULL) {
1293 /* Appending at end */
1294 data->highest_encountered_index = *items_read;
1295 }
1296 }
1297 }
1298
1299
1300
1301 static int
1302 is_designated_initializer(struct token *t) {
1303 if ((t->type == TOK_OPERATOR
1304 && *(int *)t->data == TOK_OP_STRUMEMB)
1305 || (t->type == TOK_IDENTIFIER
1306 && t->next
1307 && t->next->type == TOK_OPERATOR
1308 && *(int *)t->next->data == TOK_OP_AMB_COND2)) {
1309 return 1;
1310 } else {
1311 return 0;
1312 }
1313 }
1314
1315
1316 static void
1317 complete_last_storage_unit(struct initializer *init,
1318 struct decl *storage_unit,
1319 unsigned char *buf) {
1320
1321 struct initializer *head = NULL;
1322 struct initializer *tail = NULL;
1323 int i;
1324 int size = backend->get_sizeof_type(storage_unit->dtype, NULL);
1325
1326
1327 if (cross_get_target_arch_properties()->endianness == ENDIAN_BIG) {
1328 if (size > 1) {
1329 unsigned char *start;
1330 unsigned char *end;
1331
1332 end = buf + size - 1;
1333 start = buf;
1334 do {
1335 unsigned char temp = *start;
1336 *start = *end;
1337 *end = temp;
1338 } while (++start < --end);
1339 }
1340 }
1341
1342 /*
1343 * Replace bitfield initializer with (possibly partial) storage unit
1344 * initializer
1345 */
1346 for (i = 0; i < size; ++i) {
1347 struct initializer *newinit = alloc_initializer();
1348 struct expr *ex = alloc_expr();
1349 struct tyval *tv = n_xmalloc(sizeof *tv);
1350 static struct tyval nulltv;
1351
1352 /*
1353 * 10/08/08: We have to make this permanent because it may be
1354 * used as a static variable initializer, which must live until
1355 * all declarations are written
1356 * XXX free properly
1357 */
1358 ex = dup_expr(ex);
1359
1360 *tv = nulltv;
1361 tv->type = make_basic_type(TY_UCHAR);
1362 tv->value = zalloc_buf(Z_CEXPR_BUF); /*n_xmalloc(16);*/ /* XXX */
1363 *(unsigned char *)tv->value = buf[i];
1364 ex->const_value = tv;
1365
1366 newinit->left_type = make_basic_type(TY_UCHAR);
1367 newinit->data = ex;
1368 newinit->type = INIT_EXPR;
1369 append_init_list(&head, &tail, newinit);
1370 }
1371 init->type = INIT_NESTED;
1372 init->data = head;
1373 }
1374
1375 static void
1376 merge_bitfield_init(struct initializer **ret, struct initializer **ret_tail) {
1377 /*
1378 * 10/04/08: Merge bitfield initializers, if any
1379 */
1380 struct initializer *init;
1381 struct decl *last_storage_unit = NULL;
1382 struct initializer *first_bf_init = NULL;
1383 unsigned char buf[16];
1384 int i;
1385
1386 #if 0
1387 printf("init list BEFORE processing\n");
1388 puts("=====================");
1389 {
1390 struct initializer *init;
1391 for (init = *ret; init != NULL; init = init->next) {
1392 printf(" %d\n", init->type);
1393 if (init->type == 1) {
1394 struct initializer *nest;
1395 for (nest = init->data; nest != NULL; nest = nest->next) {
1396 printf(" %d\n", nest->type);
1397 }
1398 }
1399 }
1400 }
1401 puts("=====================");
1402 #endif
1403
1404 memset(buf, 0, sizeof buf);
1405 for (init = *ret; init != NULL;) {
1406 if (init->type == INIT_BITFIELD
1407 || (init->type == INIT_NULL
1408 && init->left_type != NULL
1409 && init->left_type->tbit != NULL
1410 && init->varinit)) {
1411 struct ty_bit *t = init->left_type->tbit;
1412 struct expr *data;
1413 int byte_idx;
1414 int bit_idx;
1415 long long bfval;
1416 struct initializer *continue_at;
1417
1418 /*
1419 * Unlink bitfield initializer! It is replaced
1420 * with the storage unit initializer later
1421 *
1422 * We cannot do this for the very first bitfield
1423 * because its initializer will be replaced with
1424 * the merged result
1425 */
1426
1427 continue_at = init->next;
1428 if (t->bitfield_storage_unit == last_storage_unit) {
1429 if (init->type == INIT_NULL) {
1430 /*
1431 * This is a variable initializer, i.e.
1432 * an expression which must be computed
1433 * and assigned at runtime! Therefore
1434 * we cannot remove this initializer
1435 */
1436 ;
1437 } else {
1438 /*
1439 * First field has already been passed -
1440 * remove initializer for later one
1441 */
1442 if (init->prev != NULL) {
1443 init->prev->next = init->next;
1444 if (*ret_tail == init) {
1445 /* Removing tail */
1446 *ret_tail = (*ret_tail)->prev;
1447 }
1448 if (init->next != NULL) {
1449 init->next->prev = init->prev;
1450 }
1451 } else {
1452 /* Must be head */
1453 if (init == *ret_tail) {
1454 /* Is also tail */
1455 *ret_tail = (*ret_tail)->prev;
1456 }
1457 *ret = (*ret)->next;
1458 if (*ret != NULL) {
1459 (*ret)->prev = NULL;
1460 }
1461 }
1462 }
1463 } else {
1464 /* Processing new storage unit */
1465 if (last_storage_unit != NULL) {
1466 complete_last_storage_unit(first_bf_init,
1467 last_storage_unit, buf);
1468 /* Reset buffer for next storage unit */
1469 memset(buf, 0, sizeof buf);
1470 }
1471
1472 last_storage_unit = t->bitfield_storage_unit;
1473 first_bf_init = init;
1474
1475 if (init->type == INIT_NULL) {
1476 /*
1477 * Variable initializer - computed and
1478 * assigned at runtime - so we have to
1479 * keep this. Because ``init'' is
1480 * overwritten with the new bitfield
1481 * initializer, copy the old one!
1482 */
1483 struct initializer *temp;
1484
1485 temp = dup_initializer(init);
1486 temp->prev = init;
1487 temp->next = init->next;
1488 if (temp->next) {
1489 temp->next->prev = temp;
1490 }
1491 init->next = temp;
1492 continue_at = temp->next;
1493 }
1494 }
1495
1496 /*
1497 * Store bitfield at corresponding offset
1498 */
1499 #if 0
1500 printf(" %p: abs byte offset = %lu - %lu = %lu\n", last_storage_unit, t->absolute_byte_offset,
1501 last_storage_unit->offset,
1502 t->absolute_byte_offset - last_storage_unit->offset);
1503 #endif
1504 byte_idx = t->absolute_byte_offset - last_storage_unit->offset;
1505 bit_idx = t->bit_offset;
1506
1507 if (byte_idx >= (int)sizeof buf) {
1508 warningfl(NULL, "BUG: Bitfield bug encountered! The bitfield "
1509 "initializer will not work as expected");
1510 byte_idx = sizeof buf - 1;
1511 }
1512
1513
1514
1515
1516
1517 #if 0
1518 printf("doing bf %s at %d,%d\n", init->left_type->name,
1519 byte_idx, bit_idx);
1520 printf(" at byte %d, bit %d\n", byte_idx, bit_idx);
1521 printf(" item is %d bits big\n", t->numbits);
1522 printf(" byte idx is %d - %d\n", t->absolute_byte_offset, last_storage_unit->offset);
1523 #endif
1524 data = init->data;
1525
1526 if (init->type != INIT_NULL) {
1527 bfval = cross_to_host_long_long(data->const_value);
1528
1529 #if 0
1530 printf(" doing val %lld\n", bfval);
1531 #endif
1532 if (1 || cross_get_target_arch_properties()->endianness == ENDIAN_LITTLE) {
1533 for (i = 0; i < t->numbits; ++i) {
1534 buf[byte_idx] |= !!(bfval & (1LL << i)) << bit_idx;
1535 if (bit_idx == 7) { /* XXX char bit count... */
1536 ++byte_idx;
1537 bit_idx = 0;
1538 } else {
1539 ++bit_idx;
1540 }
1541 #if 0
1542 printf(" %02x %02x %02x\n", buf[0], buf[1], buf[2]);
1543 #endif
1544 }
1545 }
1546 }
1547 init = continue_at;
1548 } else {
1549 /* Is not bitfield type */
1550 if (last_storage_unit != NULL) {
1551 complete_last_storage_unit(first_bf_init,
1552 last_storage_unit, buf);
1553 /* Reset buffer for next storage unit */
1554 memset(buf, 0, sizeof buf);
1555 first_bf_init = NULL;
1556 last_storage_unit = NULL;
1557 }
1558 init = init->next;
1559 }
1560 }
1561
1562
1563 #if 0
1564 puts("DATA");
1565 for (i = 0 ;i < sizeof buf; ++i) {
1566 printf("%02x ", buf[i]);
1567 }
1568 putchar('\n');
1569 #endif
1570
1571 if (last_storage_unit != NULL) {
1572 complete_last_storage_unit(first_bf_init, last_storage_unit, buf);
1573 }
1574
1575
1576 #if 0
1577 puts("DATA AFTER REVERSAL");
1578 for (i = 0 ;i < sizeof buf; ++i) {
1579 printf("%02x ", buf[i]);
1580 }
1581 putchar('\n');
1582 #endif
1583
1584 #if 0
1585 printf("init list after processing\n");
1586 puts("=====================");
1587 {
1588 struct initializer *init;
1589 for (init = *ret; init != NULL; init = init->next) {
1590 printf(" %d\n", init->type);
1591 if (init->type == 1) {
1592 struct initializer *nest;
1593 for (nest = init->data; nest != NULL; nest = nest->next) {
1594 printf(" %d\n", nest->type);
1595 }
1596 }
1597 }
1598 }
1599 #endif
1600 }
1601
1602
1603 static struct initializer *
1604 make_unnamed_bitfield_init(struct type *type) {
1605 struct initializer *ret = alloc_initializer();
1606 struct expr *ex = alloc_expr();
1607 struct tyval *tv = n_xmalloc(sizeof *tv);
1608
1609 ret->type = INIT_BITFIELD;
1610 ret->left_type = type;
1611 tv = n_xmemdup(tv, sizeof *tv); /* XXX */
1612
1613 tv->value = zalloc_buf(Z_CEXPR_BUF); /*n_xmalloc(16);*/ /* XXX */
1614 memset(tv->value, 0, 16);
1615 tv->type = type;
1616
1617 ex = dup_expr(ex); /* Permanence! */
1618 ex->const_value = tv;
1619 ret->data = ex;
1620 return ret;
1621 }
1622
1623 struct initializer *
1624 get_init_expr(struct token **tok, struct expr *saved_first_expr0,
1625 int type, struct type *lvalue, int initial,
1626 int complit) {
1627
1628 struct token *t = *tok;
1629 struct expr *ex;
1630 struct initializer *ret = NULL;
1631 struct initializer *rettail = NULL;
1632 struct initializer *init;
1633 struct sym_entry *se = NULL;
1634 struct type *curtype = NULL;
1635 struct type arrtype;
1636 struct desig_init_data init_data;
1637 struct expr *saved_first_expr = NULL;
1638 int items_read = 0;
1639 int items_ok = 0;
1640 int is_aggregate = 0;
1641 int is_array = 0;
1642 int is_char_array = 0;
1643 int is_braced_string = 0;
1644 int is_unnamed_bitfield = 0; /* 10/12/08 */
1645 int warned = 0;
1646 int needbrace = 0;
1647 int have_desig = 0;
1648 int struct_ends = 0;
1649
1650 init_data.init_head = &ret;
1651 init_data.init_tail = &rettail;
1652 init_data.cur_init_ptr = NULL;
1653 init_data.highest_encountered_member = NULL;
1654 init_data.highest_encountered_index = -1;
1655 init_data.type = lvalue;
1656 init_data.real_items_read = 0;
1657
1658 if (lvalue->tlist != NULL
1659 && lvalue->tlist->type == TN_ARRAY_OF) {
1660 is_aggregate = 1;
1661 is_array = 1;
1662 items_ok = lvalue->tlist->arrarg_const;
1663 if (!initial && items_ok == 0) {
1664 errorfl(t, "Array size for nested array dimension "
1665 "unspecified");
1666 return NULL;
1667 }
1668 arrtype = *lvalue;
1669 curtype = &arrtype;
1670 arrtype.tlist = arrtype.tlist->next;
1671 if (IS_CHAR(lvalue->code) && lvalue->tlist->next == NULL) {
1672 is_char_array = 1;
1673 }
1674 } else if ((lvalue->code == TY_STRUCT || lvalue->code == TY_UNION)
1675 && lvalue->tlist == NULL) {
1676
1677 if (lvalue->code == TY_STRUCT) {
1678 is_aggregate = 1;
1679 } else {
1680 items_ok = 1;
1681 }
1682 se = lvalue->tstruc->scope->slist;
1683 curtype = se->dec->dtype;
1684
1685 /* XXX should set items_ok ?!?!? */
1686 /*
1687 * 04/02/08: The code below only handled the case:
1688 * struct s val = init;
1689 * ... i.e. a struct-by-value assignment as initializer.
1690 * Hence this was only done if the ``initial'' flag was
1691 * set.
1692 * However, we also have to do this for nested structs,
1693 * e.g.
1694 *
1695 * struct foo { struct bar { int x; } b; };
1696 * struct bar bs;
1697 * struct fs = { bs };
1698 *
1699 * ... here the old version recursed into the ``struct
1700 * bar'' member list and tried to assign the bs struct
1701 * to the x struct member. This behavior is only correct
1702 * for unbraced nested struct values, like
1703 *
1704 * struct foo { struct bar { int x, y, z; } b; };
1705 * struct foo f = { 1, 2, 3 };
1706 *
1707 * The solution: For unparenthesized structure init
1708 * values, ALWAYS read the first expression and checks
1709 * if its type indicates that it fills the entire struct
1710 * by value. Otherwise, assume it's an unbraceded member
1711 * value, and pass that expression to the function while
1712 * recursing
1713 */
1714 if ( /*initial &&*/ t->type != TOK_COMP_OPEN) {
1715 int initializes_entire_struct = 0;
1716
1717 /*
1718 * 09/07/07: This always used EXPR_INIT instead of
1719 * type!! (breaks for constant initializers like
1720 * compound literals)
1721 */
1722 if (type == EXPR_OPTCONSTINIT && initial) {
1723 type = EXPR_INIT;
1724 }
1725 if (initial) {
1726 ex = parse_expr(&t, TOK_OP_COMMA,
1727 TOK_SEMICOLON,
1728 type, 1);
1729 } else {
1730 ex = parse_expr(&t, TOK_OP_COMMA,
1731 TOK_COMP_CLOSE,
1732 type, 1);
1733 }
1734
1735 if (ex == NULL) {
1736 return NULL;
1737 }
1738
1739 if (!initial) {
1740 /*
1741 * 04/02/08: Check if this is a value
1742 * initializing the entire struct. So
1743 * get the type using expr_to_icode()
1744 * with the eval flag set to 0!
1745 */
1746 struct vreg *vr;
1747
1748 vr = expr_to_icode(ex, NULL, NULL, 0, 0, 0);
1749 if (vr != NULL
1750 && check_types_assign(NULL, lvalue,
1751 vr, 1, 1) == 0) {
1752 initializes_entire_struct = 1;
1753 } else {
1754 saved_first_expr = ex;
1755 }
1756 } else {
1757 initializes_entire_struct = 1;
1758 }
1759
1760 if (initializes_entire_struct) {
1761 if (type == EXPR_OPTCONSTINIT
1762 && !ex->is_const) {
1763 /*
1764 * Nested non-constant struct init
1765 */
1766
1767 /*
1768 * Initialize value to 0 for now
1769 */
1770 init = backend->
1771 make_null_block(NULL, lvalue,
1772 NULL, 1);
1773 init->varinit = ex;
1774 init->left_type = dup_type(lvalue);
1775 } else {
1776 if (ex->const_value
1777 && ex->const_value->static_init) {
1778 init = ex->const_value->static_init;
1779 } else {
1780 init = alloc_initializer();
1781 init->type = INIT_STRUCTEXPR;
1782 init->left_type = dup_type(curtype);
1783 init->data = ex;
1784 }
1785 }
1786 *tok = t;
1787 return init;
1788 }
1789 }
1790 } else {
1791 struct initializer *was_nullblock = NULL;
1792
1793 /* The following are for variable struct/array inits */
1794
1795 if (saved_first_expr0 != NULL) {
1796 ex = saved_first_expr0;
1797 goto skip_expr_reading;
1798 }
1799
1800 /* Only one item */
1801 if (t->type == TOK_COMP_OPEN) {
1802 /*
1803 * Compound initializers are also allowed for
1804 * non-aggregate types;
1805 * int x = { 0 };
1806 */
1807 t = t->next;
1808 ex = parse_expr(&t, TOK_COMP_CLOSE, TOK_OP_COMMA,type,1);
1809
1810 if (t->type == TOK_OP_COMMA
1811 && t->next != NULL
1812 && t->next->type == TOK_COMP_CLOSE) {
1813 /* Trailing comma permitted in compound init */
1814 t = t->next->next;
1815 } else if (t->type != TOK_COMP_CLOSE) {
1816 /*
1817 * There may be a trailing comma here. BitchX
1818 * uses that
1819 */
1820 if (t->type == TOK_OPERATOR
1821 && *(int *)t->data == TOK_OP_COMMA
1822 && t->next
1823 && t->next->type == TOK_COMP_CLOSE) {
1824 t = t->next->next;
1825 } else {
1826 errorfl(t, "Invalid initializer");
1827 return NULL;
1828 }
1829 } else {
1830 t = t->next;
1831 }
1832 } else {
1833 ex = parse_expr(&t, TOK_OP_COMMA,
1834 initial? TOK_SEMICOLON: TOK_COMP_CLOSE, type,1);
1835 }
1836 if (ex == NULL) {
1837 *tok = t;
1838 return NULL;
1839 }
1840
1841 skip_expr_reading:
1842
1843 if (type == EXPR_CONSTINIT || type == EXPR_OPTCONSTINIT) {
1844 if (ex->const_value != NULL) {
1845 if (ex->const_value->static_init != NULL) {
1846 /*
1847 * Initialized with value of other static
1848 * variable
1849 * XXX 02/19/08: This is NONSENSE stuff
1850 * isn't it?
1851 */
1852 struct initializer *tmp;
1853
1854 tmp = ex->const_value->static_init;
1855 if (tmp->type == INIT_EXPR) {
1856 struct expr *tmpex = tmp->data;
1857
1858 ex->const_value = tmpex->const_value;
1859 } else {
1860 /* INIT_NULL */
1861 was_nullblock = tmp->data;
1862 }
1863 }
1864 /*
1865 * Convert constant initializer to destination
1866 * type
1867 */
1868 if (!was_nullblock) {
1869 conv_init(ex->const_value, lvalue, t->prev);
1870 /*
1871 * 08/03/07: To make
1872 * static long nonsense = (long)&addr;
1873 * work
1874 */
1875 if (ex->const_value->address != NULL
1876 && is_integral_type(lvalue)) {
1877 ;
1878 } else {
1879 ex->const_value->type = ex->type
1880 = n_xmemdup(lvalue, sizeof *lvalue);
1881 }
1882
1883 /*
1884 * 08/09/08: Limit bitfield range for
1885 * initializers
1886 */
1887 if (lvalue->tbit != NULL) {
1888 struct token *tok;
1889 static struct tyval dest;
1890 static struct tyval masktv;
1891 static struct tyval temp;
1892 static struct token optok;
1893 static int andop = TOK_OP_BAND;
1894
1895 if (ex->const_value->value == NULL) {
1896 errorfl(t, "Invalid "
1897 "bitfield initializer");
1898 return NULL;
1899 }
1900
1901 optok.type = TOK_OPERATOR;
1902 optok.data = &andop;
1903
1904 /*
1905 * Bitwise AND with mask for
1906 * bitfield size
1907 */
1908 tok = make_bitfield_mask(lvalue, 1, NULL, NULL);
1909 /*
1910 * Copy types. We cannot just set the original type
1911 * because it may get promoted in cross_exec_op(),
1912 * which will change the type!
1913 */
1914 masktv.type = dup_type(lvalue);
1915 masktv.value = tok->data;
1916 dest.type = dup_type(lvalue);
1917 dest.value = ex->const_value->value;
1918 cross_exec_op(&optok, &temp, &dest, &masktv);
1919 /*
1920 * The operation may have caused a promotion
1921 * which has changed the type. So convert to
1922 * the expected type
1923 */
1924 conv_init(&temp, lvalue, NULL);
1925 ex->const_value->value = temp.value;
1926 }
1927 } else {
1928 /* XXX */
1929 unimpl();
1930 }
1931 } else {
1932 if (type == EXPR_OPTCONSTINIT
1933 && !ex->is_const) {
1934 /*
1935 * This is a non-constant expression
1936 * used as initializer for a struct
1937 * or an array. We have to save the
1938 * expression and evaluate it when
1939 * the initializer assignment takes
1940 * place
1941 */
1942 /*
1943 * 07/18/08: The re-reading of this
1944 * expression below was wrong and
1945 * apparently useless; The first pass
1946 * ``trashed'' token type values
1947 * (e.g. TOK_PAREN_OPEN becomes
1948 * TOK_OP_CAST), so a re-evaluation
1949 * is not meaningful. Instead we can
1950 * keep the old expression
1951 */
1952 #if 0
1953 ex = parse_expr(&starttok, delim1,
1954 delim2, 0,1);
1955 t = starttok;
1956 #endif
1957 if (ex == NULL) {
1958 *tok = t;
1959 return NULL;
1960 }
1961
1962 /*
1963 * Initialize value to 0 for now
1964 */
1965 was_nullblock = backend->
1966 make_null_block(NULL, lvalue,
1967 NULL, 1);
1968 was_nullblock->varinit = ex;
1969 } else {
1970 return NULL;
1971 }
1972 }
1973 }
1974
1975 if (was_nullblock) {
1976 ret = init = was_nullblock;
1977 } else {
1978 ret = init = alloc_initializer();
1979 if (lvalue->tbit != NULL) {
1980 /* 10/04/08: Bitfield */
1981 conv_init(ex->const_value, lvalue, NULL);
1982 init->type = INIT_BITFIELD;
1983 } else {
1984 init->type = INIT_EXPR;
1985 }
1986 #if XLATE_IMMEDIATELY
1987 init->data = dup_expr(ex);
1988 #else
1989 init->data = ex;
1990 #endif
1991 }
1992 init->left_type = dup_type(lvalue);
1993
1994 *tok = t;
1995 return init;
1996 }
1997
1998 if (t->type == TOK_COMP_OPEN && is_char_array) {
1999 if (t->next && t->next->type == TOK_STRING_LITERAL) {
2000 /*
2001 * This must be a braces-enclosed string constant;
2002 * char buf[] = { "hello" };
2003 * The GNU people love using unnecessary braces
2004 * (and parentheses) like this
2005 */
2006 t = t->next;
2007 is_braced_string = 1;
2008 }
2009 }
2010
2011 /*
2012 * 08/22/07: Null initializers for strings were completely ignored!
2013 * E.g. in
2014 *
2015 * static char buf[10] = "hehe";
2016 *
2017 * ... only 5 bytes would be allocated for the buffer, since there
2018 * were only 5 bytes of initialized data! Same thing with string
2019 * initializers for nested array initializers. So now we resize the
2020 * string and add any necessary null bytes
2021 */
2022 if (t->type == TOK_STRING_LITERAL) {
2023 struct ty_string *ts = t->data;
2024 size_t newlen;
2025
2026 if (lvalue->tlist != NULL
2027 && lvalue->tlist->type == TN_ARRAY_OF
2028 && (newlen = lvalue->tlist->arrarg_const) > ts->size) {
2029 ts->str = n_xrealloc(ts->str, newlen);
2030 memset(ts->str + ts->size - 1, 0, newlen - ts->size);
2031 ts->size = newlen;
2032 ts->ty->tlist->arrarg_const = newlen;
2033 #if REMOVE_ARRARG
2034 ts->ty->tlist->have_array_size = 1;
2035 #endif
2036 }
2037 }
2038
2039 if (t->type != TOK_COMP_OPEN) {
2040 if (t->type == TOK_STRING_LITERAL && is_array) {
2041 struct token *tmp = t;
2042 struct token *stringtok = t;
2043 struct ty_string *tmpts = stringtok->data;
2044
2045 if ( (lvalue->tlist->next != NULL
2046 || !IS_CHAR(lvalue->code))
2047 && !tmpts->is_wide_char) {
2048 errorfl(t, "Invalid initializer");
2049 return NULL;
2050 } else if (lvalue->sign == TOK_KEY_UNSIGNED) {
2051 #if 0
2052 warningfl(t,
2053 "Assigning string constant to array of `unsigned char'");
2054 #endif
2055 }
2056
2057 /*
2058 * 09/01/07: The extra } check was needed because
2059 * othrwise
2060 *
2061 * char foo[1][2] = { "hello" };
2062 * breaks, since "hello" is an un-braced array
2063 * initializer (the braces belong to the outer
2064 * array), and we ended up looking for a , or ;
2065 *
2066 * XXX MAybe this still isn't fully correct
2067 */
2068 if (is_braced_string
2069 || (t->next && t->next->type == TOK_COMP_CLOSE)) {
2070 /*
2071 * Read up to closing brace
2072 */
2073 ex = parse_expr(&t, TOK_COMP_CLOSE, 0, type, 1);
2074 } else {
2075 /*
2076 * Read up to comma or semicolon
2077 */
2078 ex = parse_expr(&t, TOK_OP_COMMA, TOK_SEMICOLON, type,
2079 1);
2080 }
2081 if (ex == NULL) {
2082 return NULL;
2083 }
2084 if (t != tmp->next) {
2085 tmp = tmp->next? tmp->next: t;
2086 errorfl(tmp, "Parse error at `%s'",
2087 tmp->ascii);
2088 return NULL;
2089 }
2090 if (is_braced_string && t != NULL) {
2091 /* Skip brace */
2092 t = t->next;
2093 }
2094 init = alloc_initializer();
2095 /*
2096 * 05/31/08: Don't duplicate the type here, since
2097 * array size may be determined below, so we can
2098 * only do it afterwards
2099 */
2100 /* init->left_type = dup_type(lvalue);*/
2101 #if XLATE_IMMEDIATELY
2102 init->data = dup_expr(ex);
2103 #else
2104 init->data = ex;
2105 #endif
2106 init->type = INIT_EXPR;
2107 *tok = t;
2108
2109 #if ! REMOVE_ARRARG
2110 if (lvalue->tlist->arrarg->const_value != NULL) {
2111 (void) backend->get_sizeof_type(lvalue, NULL);
2112 }
2113 #endif
2114 if (lvalue->tlist->arrarg_const == 0) {
2115 struct ty_string *ts = stringtok->data;
2116
2117 /*
2118 * 05/27/08: This used strlen(t->prev->ascii),
2119 * thus breaking the size of "\0hello"
2120 */
2121 lvalue->tlist->arrarg_const = ts->size;
2122
2123 #if REMOVE_ARRARG
2124 lvalue->tlist->have_array_size = 1;
2125 #endif
2126 }
2127 /* 05/31/08: Now we can copy the completed type */
2128 init->left_type = dup_type(lvalue);
2129 return init;
2130 } else if (initial) {
2131 /* Initial array initializer requires braces */
2132 errorfl(t, "Parse error at `%s'", t->ascii);
2133 return NULL;
2134 } else if (is_aggregate) {
2135 warningfl(t,
2136 "Nested aggregate initializer not surrounded by braces");
2137 needbrace = 0;
2138 }
2139 } else {
2140 needbrace = 1;
2141 if (next_token(&t) != 0) {
2142 return NULL;
2143 }
2144 }
2145
2146
2147
2148 do {
2149 struct token *starttok = t;
2150 struct sym_entry *desig_se = NULL;
2151 struct sym_entry *prev_se = se;
2152 int desig_elem = -1;
2153 int desig_elem_end = -1;
2154
2155 is_unnamed_bitfield = 0; /* 10/12/08 */
2156
2157 if (se != NULL && se->dec->dtype->tbit != NULL && se->dec->dtype->name == NULL) {
2158 /*
2159 * 10/12/08: Handle unnamed bitfields by skipping them
2160 */
2161 is_unnamed_bitfield = 1;
2162 } else if (is_designated_initializer(t)) {
2163 int is_c99_style = 0;
2164
2165 /*
2166 * Designated initializer -
2167 *
2168 * .membername = expr
2169 *
2170 * ... as per C99 or
2171 *
2172 * membername: expr
2173 *
2174 * ... as per GNU C (deprecated)
2175 */
2176 have_desig = 1;
2177 if (is_array) {
2178 /*
2179 * Only allowed for structs! (Note that we
2180 * can't check ``se'' because it may be
2181 * NULL here;
2182 *
2183 * struct foo { int x, y; }
2184 * f = { .y = 0, .x = 0 };
2185 * ^ NULL here
2186 */
2187 errorfl(t, "Invalid designated initializer");
2188 return NULL;
2189 }
2190 if (t->type != TOK_IDENTIFIER) {
2191 is_c99_style = 1;
2192 /* Skip . */
2193 if (next_token(&t) != 0) {
2194 return NULL;
2195 }
2196 if (t->type != TOK_IDENTIFIER) {
2197 errorfl(t, "Syntax error - "
2198 "identifier expected");
2199 return NULL;
2200 }
2201 } else {
2202 warningfl(t, "GNU C designated initializiers "
2203 "are deprecated, use the C99 way "
2204 "instead (`.member = expr' instead "
2205 "of `member: expr')");
2206 }
2207 desig_se = lookup_symbol_se(
2208 lvalue->tstruc->scope, t->data, 0);
2209 if (desig_se == NULL) {
2210 errorfl(t, "Unknown member `%s'", t->data);
2211 return NULL;
2212 }
2213 if (next_token(&t) != 0) {
2214 return NULL;
2215 }
2216 if (is_c99_style) {
2217 if (t->type != TOK_OPERATOR
2218 || *(int *)t->data != TOK_OP_ASSIGN) {
2219 errorfl(t, "Syntax error at `%s' - "
2220 "`=' expected", t->next->ascii);
2221 return NULL;
2222 }
2223 }
2224
2225 /* Skip : (GNU C) or = (C99) */
2226 if (next_token(&t) != 0) {
2227 return NULL;
2228 }
2229 se = desig_se;
2230 curtype = se->dec->dtype;
2231 } else if (t->type == TOK_ARRAY_OPEN) {
2232 struct expr *startex;
2233 struct expr *endex;
2234 size_t elem;
2235 size_t startelem;
2236 size_t endelem;
2237
2238 have_desig = 1;
2239 if (next_token(&t) != 0) {
2240 return NULL;
2241 }
2242 startex = parse_expr(&t, TOK_ARRAY_CLOSE, TOK_ELLIPSIS, EXPR_CONST, 1);
2243 if (startex == NULL) {
2244 return NULL;
2245 }
2246
2247 /*
2248 * 07/18/08: Allow range syntax;
2249 *
2250 * [expr ... expr]
2251 */
2252 if (t->type == TOK_ELLIPSIS) {
2253 /* Is range! */
2254 warningfl(t, "ISO C does not allow designated "
2255 "array initializer ranges");
2256 if (next_token(&t) != 0) {
2257 return NULL;
2258 }
2259 endex = parse_expr(&t, TOK_ARRAY_CLOSE, 0, EXPR_CONST, 1);;
2260 } else {
2261 endex = NULL;
2262 }
2263
2264 startelem = cross_to_host_size_t(startex->const_value);
2265 if (endex != NULL) {
2266 /* Is range */
2267 endelem = cross_to_host_size_t(endex->const_value);
2268 if (startelem >= endelem) {
2269 errorfl(t, "Invalid range - "
2270 "start is after end");
2271 return NULL;
2272 }
2273 /*
2274 * [0 ... 1] covers 2 elements, so increment endelem
2275 */
2276 ++endelem;
2277 } else {
2278 /* Not range */
2279 endelem = startelem + 1;
2280 }
2281
2282 elem = startelem;
2283
2284
2285 if ((ssize_t)elem < 0) {
2286 errorfl(t, "Negative and very large designated "
2287 "array elements are not allowed");
2288 return NULL;
2289 } else if ((ssize_t)elem > items_ok && items_ok != 0) {
2290 errorfl(t, "Array index %lu out of bounds",
2291 (unsigned long)elem);
2292 return NULL;
2293 } else if (elem > INT_MAX) {
2294 errorfl(t, "Array index %lu too large",
2295 (unsigned long)elem);
2296 return NULL;
2297 }
2298 if (next_token(&t) != 0
2299 || t->type != TOK_OPERATOR
2300 || *(int *)t->data != TOK_OP_ASSIGN) {
2301 if (t != NULL) {
2302 errorfl(t, "Syntax error at `%s' - ",
2303 "`=' expected",
2304 t->ascii);
2305 }
2306 return NULL;
2307 }
2308 if (next_token(&t) != 0) {
2309 return NULL;
2310 }
2311 desig_elem = (int)elem;
2312 desig_elem_end = (int)endelem;
2313 }
2314
2315 if (is_unnamed_bitfield) {
2316 /*
2317 * 10/12/08: Handle unnamed bitfields by creating an
2318 * initializer of INIT_BITFIELD with value 0. That way
2319 * named and unnamed bitfields will properly be combined
2320 * into storage units at the end of this function
2321 *
2322 * The initializer is only read for the next named
2323 * struct member
2324 */
2325 if (se->dec->dtype->tbit->numbits == 0) {
2326 /* Terminates storage unit - ignore */
2327 goto skip_init_handling;
2328 } else {
2329 init = make_unnamed_bitfield_init(se->dec->dtype);
2330 }
2331 } else {
2332 init = get_init_expr(&t, saved_first_expr, type, curtype, 0, 0);
2333 }
2334 saved_first_expr = NULL;
2335
2336 if (init == NULL) {
2337 return NULL;
2338 }
2339
2340 if (is_basic_agg_type(curtype)) {
2341 struct initializer *init2;
2342
2343 init2 = alloc_initializer();
2344 init2->left_type = dup_type(curtype);
2345 init2->type = INIT_NESTED;
2346 init2->data = init;
2347 init = init2;
2348 /*append_init_list(&ret, &rettail, init2);*/
2349 } else {
2350 /*append_init_list(&ret, &rettail, init);*/
2351 }
2352
2353 if (is_array) {
2354 /* append_init_list(&ret, &rettail, init);*/
2355 int i;
2356
2357 /*
2358 * 07/18/08: Loop to allow ranges!
2359 */
2360 if (desig_elem != -1) {
2361 /* Destination is designated */
2362 for (i = desig_elem; i < desig_elem_end; ++i) {
2363 put_desig_init_array(&init_data, i,
2364 &items_read,
2365 init,
2366 curtype,
2367 starttok);
2368 if (i + 1 < desig_elem_end) {
2369 /*
2370 * There will be another one,
2371 * so copy the current item.
2372 * Otherwise the node will
2373 * be appended multiple times,
2374 * which trashes the data
2375 * structures
2376 */
2377 init = dup_initializer(init);
2378 }
2379 }
2380 } else {
2381 /* Destination is implicit */
2382 put_desig_init_array(&init_data, desig_elem,
2383 &items_read,
2384 init,
2385 curtype,
2386 starttok);
2387 }
2388 } else {
2389 put_desig_init_struct(&init_data, desig_se,
2390 prev_se,
2391 lvalue->tstruc->scope->slist,
2392 init,
2393 starttok, &items_read);
2394 }
2395
2396 ++items_read;
2397 if (items_ok != 0) {
2398 /*
2399 * 07/19/08: Don't give errors for multiple union
2400 * initializers, but warn about it
2401 */
2402 if (items_read > 1
2403 && lvalue->code == TY_UNION
2404 && lvalue->tlist == NULL) {
2405 if (!warned) {
2406 warningfl(starttok, "More than one "
2407 "initializer for union");
2408 warned = 1;
2409 }
2410 }
2411
2412 if (items_read > items_ok && !warned) {
2413 if (!is_aggregate) {
2414 errorfl(starttok,
2415 "Invalid aggregate initializer");
2416 } else {
2417 errorfl(starttok,
2418 "Initializer for aggregate type too big");
2419 }
2420 warned = 1;
2421 } else if (!needbrace && items_read == items_ok) {
2422 /* We're done */
2423 t = t->prev;
2424 break;
2425 }
2426 }
2427
2428 skip_init_handling:
2429 if (se != NULL) {
2430 se = se->next;
2431 if (se != NULL) {
2432 /* XXX error msg if se = NULL maybe? */
2433 curtype = se->dec->dtype;
2434 } else {
2435 /*
2436 * 05/08/09: This may be a struct
2437 * initializer without braces -
2438 * Record the fact that all struct
2439 * members are exhausted, so the
2440 * struct may end here
2441 *
2442 * XXX what about subsequent
2443 * designated initializers?
2444 */
2445 if (!needbrace) {
2446 struct_ends = 1;
2447 }
2448 }
2449 }
2450
2451 /*
2452 * 08/05/07: The code below was previously BEFORE the se
2453 * update above! Thus if there was a trailing comma, the
2454 * make_null_block() call at the bottom would wrongly
2455 * create a null initializer for this structure member
2456 * even though it already has an initializer
2457 *
2458 * This broke some vim option structs
2459 *
2460 * 10/12/08: The token stuff does not apply to unnamed
2461 * bitfields, which are just skipped
2462 */
2463 if (!is_unnamed_bitfield) {
2464 if (t->type == TOK_OPERATOR
2465 && *(int *)t->data == TOK_OP_COMMA
2466 && t->next != NULL
2467 && t->next->type == TOK_COMP_CLOSE) {
2468 /* Trailing comma permitted in compound init. */
2469 t = t->next;
2470 break;
2471 }
2472 if (t->type == TOK_SEMICOLON) {
2473 break; /* XXXXXXXX */
2474 }
2475 }
2476 } while (!expr_ends(t, TOK_COMP_CLOSE, 0)
2477 && !struct_ends /* 05/12/09: note: comes before t=t->next! now */
2478 && (is_unnamed_bitfield || (t = t->next) != NULL)); /* 10/12/08: Bitfields */
2479
2480 /*
2481 * 05/12/09: This is only a separator token that can be
2482 * skipped if we didn't break out of the loop because
2483 * a struct array element initializer is complete!
2484 *
2485 * struct foo { int x, y; } f[] = { 1, 2, 3, 4 };
2486 */
2487 if (!struct_ends) {
2488 if (t->type != TOK_SEMICOLON) {/* XXXXXXXXXXX */
2489 t = t->next;
2490 }
2491 }
2492 *tok = t;
2493
2494 if (is_array && items_ok == 0) {
2495 lvalue->tlist->arrarg_const = items_ok = items_read;
2496 #if REMOVE_ARRARG
2497 lvalue->tlist->have_array_size = 1;
2498 #endif
2499 }
2500
2501 if (initial
2502 && t->type != TOK_SEMICOLON
2503 && (t->type != TOK_OPERATOR
2504 || *(int *)t->data != TOK_OP_COMMA)) {
2505 if (!complit) {
2506 errorfl(t, "Parse error at `%s'- expected semicolon or comma",
2507 t->ascii);
2508 return NULL;
2509 }
2510 }
2511
2512 /*
2513 * Remaining fields are initialized to 0. This is very straightforward
2514 * for normal initializers - just initialize the rest of the struct
2515 * or array - and requires brute-force for designated initializers
2516 *
2517 * 07/19/08: This requires a different strategy for unions! A union
2518 * has multiple members, but only a single initializer is allowed.
2519 * Therefore we have to check whether that initializer takes up the
2520 * whole size of the union, and if it doesn't, only pad the rest
2521 */
2522 init = NULL;
2523 if (lvalue->code == TY_UNION && lvalue->tlist == NULL) {
2524 /* Union */
2525 if (*init_data.init_head != NULL) {
2526 int union_size = backend->get_sizeof_type(lvalue, NULL);
2527 int init_size = backend->get_sizeof_type((*init_data.init_head)->left_type, NULL);
2528
2529 if (union_size > init_size) {
2530 int remaining = union_size - init_size;
2531
2532 init = backend->make_null_block(init_data.last_encountered_member, NULL, lvalue,
2533 remaining);
2534 }
2535 }
2536 } else {
2537 /* Struct or array */
2538
2539 #if 1
2540 if (!is_array) {
2541 /*
2542 * 10/12/08: If we've already initialized the first
2543 * couple of bitfields in a bitfield storage unit,
2544 * then we have to complete that unit before we
2545 * create an initializer for the remaining fields
2546 */
2547 struct sym_entry *temp;
2548 struct decl *storage_unit = NULL;
2549
2550 if (have_desig) {
2551 /*
2552 * 10/12/08: Moved here from below: If this
2553 * has designated initializers, it may jump
2554 * anywhere in the declaration list, so we
2555 * first have to locate the last processed
2556 * initializer (up to which point null
2557 * initialization has already been handled
2558 * where needed)
2559 */
2560 struct initializer *tmp;
2561
2562 se = lvalue->tstruc->scope->slist;
2563 tmp = ret;
2564 for (; se != NULL; se = se->next) {
2565 if (tmp->next == NULL) {
2566 break;
2567 }
2568 tmp = tmp->next;
2569 }
2570 se = se->next;
2571 }
2572
2573 /*
2574 * Now check whether there are remaining bitfields in
2575 * current storage unit
2576 */
2577 for (temp = se; temp != NULL; temp = temp->next) {
2578 if (temp->dec->dtype->tbit != NULL) {
2579 if (storage_unit == NULL) {
2580 /*
2581 * See whether there is a
2582 * preceding storage unit
2583 */
2584 struct sym_entry *temp2;
2585
2586 for (temp2 = se->prev;
2587 temp2 != NULL;
2588 temp2 = temp2->prev) {
2589 if (temp2->dec->dtype->tbit != NULL) {
2590 storage_unit =
2591 temp2->dec->dtype->tbit->
2592 bitfield_storage_unit;
2593 break;
2594 }
2595 }
2596 if (storage_unit == NULL) {
2597 /* No preceding bitfield storage unit */
2598 break;
2599 }
2600 }
2601 if (temp->dec->dtype->tbit->bitfield_storage_unit
2602 != storage_unit) {
2603 /* OK this is a new bitfield */
2604 break;
2605 } else {
2606 /*
2607 * This must be covered by the preceding
2608 * initializer since it uses the same storage
2609 * unit - skip it
2610 */
2611 temp->has_initializer = 1;
2612 }
2613 }
2614 }
2615 }
2616 #endif
2617
2618 if (have_desig) {
2619 init = NULL;
2620 if (is_array) {
2621 if (items_ok != 0) {
2622 int remaining =
2623 items_ok -
2624 (init_data.highest_encountered_index+1);
2625
2626 if (remaining < 0) {
2627 puts("BUG: ??????");
2628 abort();
2629 } else if (remaining > 0) {
2630 init = backend->
2631 make_null_block(NULL, curtype,
2632 NULL, remaining);
2633 }
2634 }
2635 } else {
2636 #if 0
2637 struct initializer *tmp;
2638
2639 se = lvalue->tstruc->scope->slist;
2640 tmp = ret;
2641 for (; se != NULL; se = se->next) {
2642 if (tmp->next == NULL) {
2643 break;
2644 }
2645 tmp = tmp->next;
2646 }
2647 se = se->next;
2648 #endif
2649 if (se != NULL) {
2650 init = backend->make_null_block(se, NULL, lvalue, 0);
2651 }
2652 }
2653 } else {
2654 if (items_read < items_ok || se != NULL) {
2655 /* Remaining fields are initialized to 0 */
2656 if (se != NULL) {
2657 /* Structure */
2658 init = backend->
2659 make_null_block(se, NULL, lvalue, 0);
2660 } else {
2661 /* Array */
2662 int remaining = items_ok - items_read;
2663 init = backend->
2664 make_null_block(NULL, curtype,
2665 NULL, remaining);
2666 }
2667 }
2668 }
2669 }
2670 if (init != NULL) {
2671 append_init_list(&ret, &rettail, init);
2672 }
2673 merge_bitfield_init(&ret, &rettail);
2674
2675 return ret;
2676 }
2677
2678 #endif /* #ifndef PREPROCESSOR */
2679
2680 /*
2681 * Parses an expression and returns a pointer to the parse tree.
2682 *
2683 * 09/07/07: Finally the ``nesting'' variable is gone! Instead the caller
2684 * passes an ``initial'' flag, which states whether this is the initial
2685 * call to get a particular expression.
2686 *
2687 * Only if ``initial'' is set, will constant expression results be
2688 * evaluated before returning
2689 */
2690 struct expr *
2691 parse_expr(struct token **tok, int delim, int delim2, int type, int initial) {
2692 struct token *t;
2693 struct token *tokstart = *tok;
2694 struct expr *ret = NULL;
2695 struct expr *rettail = NULL;
2696 struct expr *ex = NULL;
2697 struct s_expr *s_ex;
2698 int condop_nesting = 0;
2699
2700 #ifdef NO_EXPR
2701 /* Don't use expression parser */
2702 recover(tok, delim, delim2);
2703 if (*tok == NULL) {
2704 lexerror("Unterminated expression");
2705 exit(1);
2706 }
2707 return NULL;
2708 #endif
2709
2710 t = *tok;
2711
2712 if (t->type == TOK_SEMICOLON) {
2713 /* Empty expression */
2714 if ((delim != TOK_SEMICOLON && delim2 != TOK_SEMICOLON)
2715 || type == EXPR_INIT
2716 || type == EXPR_CONSTINIT
2717 || type == EXPR_OPTCONSTINIT
2718 || type == EXPR_OPTCONSTARRAYSIZE) {
2719 errorfl(t, "Parse error at `%s'", t->ascii);
2720 return NULL;
2721 }
2722 ret = alloc_expr();
2723 ret->is_const = 1;
2724 return ret;
2725 } else if (t->type == TOK_ARRAY_CLOSE
2726 && (delim == TOK_ARRAY_CLOSE
2727 || delim2 == TOK_ARRAY_CLOSE)) {
2728 ret = alloc_expr();
2729 ret->is_const = 1;
2730 return ret;
2731 } else if (delim == TOK_ARRAY_CLOSE
2732 && type == EXPR_CONST_FUNCARRAYPARAM) {
2733 if ((t->type == TOK_KEY_RESTRICT
2734 || t->type == TOK_KEY_CONST /* XXX honor this */
2735 || t->type == TOK_KEY_VOLATILE)
2736 && t->next != NULL
2737 && t->next->type == TOK_ARRAY_CLOSE) {
2738 /*
2739 * This is a construct of the form
2740 *
2741 * char buf[restrict]
2742 *
2743 * which is equivalent to
2744 *
2745 * char *restrict buf
2746 */
2747 *tok = t->next;
2748 ret = alloc_expr();
2749 ret->is_const = 1;
2750 return ret;
2751 }
2752 type = EXPR_CONST;
2753 #ifndef PREPROCESSOR
2754 } else if (t->type == TOK_COMP_OPEN) {
2755 struct scope *scope;
2756
2757 /* GNU C statement-as-expression */
2758
2759 if (ansiflag) {
2760 warningfl(t,
2761 "Statement-as-expression (`({ ... })') isn't "
2762 "available in ISO C (don't use -ansi!)");
2763 }
2764
2765 scope = new_scope(SCOPE_CODE);
2766
2767 if (analyze(&t) != 0) {
2768 close_scope();
2769 return NULL;
2770 }
2771 close_scope();
2772 scope->code->type = ST_EXPRSTMT;
2773
2774 *tok = t->next;
2775 ret = alloc_expr();
2776 ret->stmt_as_expr = /*curscope->next*/scope;
2777 return ret;
2778 #endif
2779 }
2780
2781
2782 /*
2783 * Strategy: An expression is a number of sub-expressions
2784 * connected through binary and ternary operators, so we
2785 * just always need to read a sub-expression, a connecting
2786 * operator - if any - and then the next sub-expression.
2787 */
2788 while ((s_ex = get_sub_expr(&t, delim, delim2, type)) != NULL) {
2789 int op;
2790
2791 ex = alloc_expr();
2792 ex->op = 0;
2793 ex->data = s_ex;
2794
2795 append_expr(&ret, &rettail, ex);
2796
2797 if (expr_ends(t, delim, delim2)) {
2798 if (t != NULL
2799 && t->type == TOK_OPERATOR
2800 && *(int *)t->data == TOK_OP_COMMA
2801 && condop_nesting > 0) {
2802 /*
2803 * 05/04/09: Address the case
2804 *
2805 * int foo = 0? 0,0: 0;
2806 *
2807 * Here the comma used to be interpreted as
2808 * declaration list delimiter (as in
2809 * ``int foo = 0, bar = 0, baz = 0;'') But
2810 * with conditional operators, ? and : have
2811 * to behave like parentheses. This occurs
2812 * in the ``links'' browser!
2813 * XXX Any other such things? And does this
2814 * stuff work for all cases?
2815 */
2816 ;
2817 } else {
2818 if ((ex->tok = s_ex->meat) == NULL) {
2819 if ((ex->tok = s_ex->is_sizeof) == NULL) {
2820 /* Must be parenthesized expr */
2821 ex->tok = s_ex->is_expr->tok;
2822 }
2823 }
2824 break;
2825 }
2826 } else if (t->type != TOK_OPERATOR) {
2827 errorfl(t, "Parse error at `%s'(#2)", t->ascii);
2828 ex = NULL;
2829 break;
2830 } else if (*(int *)t->data == TOK_OP_COND) {
2831 ++condop_nesting;
2832 } else if (*(int *)t->data == TOK_OP_AMB_COND2) {
2833 /*
2834 * XXX Note we're using TOK_OP_AMB_COND2,
2835 * ambig_to_binary() called below!
2836 */
2837 --condop_nesting;
2838 }
2839
2840 /* Must be binary or ternary operator */
2841 ex = alloc_expr();
2842 ex->data = NULL;
2843 op = *(int *)t->data;
2844 ex->tok = t;
2845 if (op != TOK_OP_COND) {
2846 op = ambig_to_binary(t);
2847 }
2848
2849 if (type == EXPR_CONST || type == EXPR_CONSTINIT) {
2850 int err = 0;
2851 char *is_what =
2852 type == EXPR_CONST? "expression": "initializer";
2853
2854 if (op == TOK_OP_COMMA) {
2855 /* 06/01/08: Allow this. hmm */
2856 warningfl(t,
2857 "The comma operator is not allowed "
2858 "in constant %ss", is_what);
2859 /* err = 1;*/
2860 } else if (IS_ASSIGN_OP(op)) {
2861 errorfl(t,
2862 "Assignment operators are not allowed "
2863 "in constant %ss", is_what);
2864 err = 1;
2865 }
2866 if (err) {
2867 free(ex);
2868 ex = NULL;
2869 break;
2870 }
2871 }
2872 ex->op = op;
2873
2874 append_expr(&ret, &rettail, ex);
2875
2876 if (next_token(&t) != 0) {
2877 return NULL;
2878 }
2879 if (expr_ends(t, delim, delim2)) {
2880 errorfl(ex->tok, "Syntax error at `%s'",
2881 ex->tok->ascii);
2882 break;
2883 }
2884 if (op == TOK_OP_COND) {
2885 /*
2886 * 08/18/07: Allow GNU C's empty first conditional
2887 * operator operand
2888 */
2889 if (t->type == TOK_OPERATOR
2890 && *(int *)t->data == TOK_OP_AMB_COND2) {
2891 warningfl(t, "Conditionals with omitted "
2892 "operands are not ISO C (GNU C only)");
2893 if (next_token(&t) != 0) {
2894 return NULL;
2895 }
2896 if (expr_ends(t, delim, delim2)) {
2897 errorfl(t, "Syntax error at `%s'",
2898 t->ascii);
2899 break;
2900 }
2901 ex = alloc_expr();
2902 ex->tok = t;
2903 ex->op = TOK_OP_COND2;
2904 append_expr(&ret, &rettail, ex);
2905 }
2906 }
2907 }
2908
2909 *tok = t;
2910
2911 if (ex == NULL || !expr_ends(t, delim, delim2)) {
2912 /* Try to recover from parse errors */
2913 if (initial) {
2914 recover(&tokstart, delim, delim2);
2915 *tok = tokstart;
2916 }
2917 return NULL;
2918 }
2919
2920 #ifndef PREPROCESSOR
2921 debug_print_expr(ret);
2922 #endif
2923 fflush(stdout);
2924
2925 if ((ret = bind_operators(ret)) == NULL) {
2926 printf("panic: cannot bind operators (initial = %d)\n", initial);
2927 if (tokstart) {
2928 printf("%s ", tokstart->ascii);
2929 if (tokstart->next) {
2930 printf("%s ", tokstart->next->ascii);
2931 if (tokstart->next->next) {
2932 printf("%s ", tokstart->next->next->ascii);
2933 if (tokstart->next->next->next) {
2934 printf("%s ",
2935 tokstart->next->next->next->ascii);
2936 }
2937 }
2938 }
2939 putchar('\n');
2940 }
2941 exit(EXIT_FAILURE); /* XXX */
2942 }
2943 #ifndef PREPROCESSOR
2944 debug_print_tree(ret);
2945 #endif
2946 ret->extype = type;
2947
2948 if (initial
2949 && (type == EXPR_CONST
2950 || type == EXPR_CONSTINIT
2951 || type == EXPR_OPTCONSTINIT
2952 || type == EXPR_OPTCONSTARRAYSIZE)) {
2953 int not_constant;
2954
2955 if (eval_const_expr(ret, type, ¬_constant) != 0) {
2956 if ((type == EXPR_OPTCONSTINIT
2957 || type == EXPR_OPTCONSTARRAYSIZE)
2958 && not_constant) {
2959 ret->is_const = 0;
2960 } else {
2961 return NULL;
2962 }
2963 } else {
2964 ret->is_const = 1;
2965 }
2966 }
2967
2968 return ret;
2969 }
2970
2971
2972
2973
2974 void
2975 recover(struct token **tok, int delim, int delim2) {
2976 struct token *t;
2977 int parens = 0;
2978 int brackets = 0;
2979 int braces = 0;
2980
2981 if (*tok == NULL) return;
2982
2983 if (delim == TOK_PAREN_CLOSE || delim2 == TOK_PAREN_CLOSE) {
2984 ++parens;
2985 }
2986 if (delim == TOK_ARRAY_CLOSE || delim2 == TOK_ARRAY_CLOSE) {
2987 ++brackets;
2988 }
2989 if (delim == TOK_COMP_CLOSE || delim2 == TOK_COMP_CLOSE) {
2990 ++braces;
2991 }
2992
2993 for (t = *tok; t != NULL; t = t->next) {
2994 int ty = -1; /* 0 compared even with unused delim :( */
2995
2996 #ifdef DEBUG2
2997 printf(" lol %s\n", t->ascii);
2998 #endif
2999 if (t->type == TOK_OPERATOR) {
3000 ty = *(int *)t->data;
3001 } else if (t->type == TOK_PAREN_OPEN) {
3002 ++parens;
3003 continue;
3004 } else if (t->type == TOK_PAREN_CLOSE) {
3005 /*
3006 * The below was --parens < 0, but <=
3007 * seems more permissive for parse
3008 * errors - so use that!
3009 */
3010 if (--parens <= 0
3011 && (delim == TOK_PAREN_CLOSE
3012 || delim2 == TOK_PAREN_CLOSE)) {
3013 *tok = t;
3014 return;
3015 }
3016 continue;
3017 } else if (t->type == TOK_ARRAY_OPEN) {
3018 ++brackets;
3019 continue;
3020 } else if (t->type == TOK_ARRAY_CLOSE) {
3021 if (--brackets <= 0
3022 && (delim == TOK_ARRAY_CLOSE
3023 || delim2 == TOK_ARRAY_CLOSE)) {
3024 *tok = t;
3025 return;
3026 }
3027 continue;
3028 } else if (t->type == TOK_COMP_OPEN) {
3029 ++braces;
3030 continue;
3031 } else if (t->type == TOK_COMP_CLOSE) {
3032 if (--braces <= 0
3033 && (delim == TOK_COMP_CLOSE
3034 || delim2 == TOK_COMP_CLOSE)) {
3035 *tok = t;
3036 return;
3037 }
3038 continue;
3039 } else {
3040 ty = t->type;
3041 }
3042 if (ty == delim || ty == delim2) {
3043 if (ty == TOK_OP_COMMA) {
3044 /*
3045 * We must take into account that someone
3046 * could write
3047 * int foo = bar(x, y, z);
3048 * ... in which case we do not want to exit
3049 * after x!
3050 */
3051 if (parens != 0) {
3052 continue;
3053 }
3054 }
3055 *tok = t;
3056 return;
3057 }
3058 }
3059 *tok = NULL;
3060 }
3061
3062