[PATCH] two-arguments ?:
[smatch.git] / evaluate.c
blob4de33b90e85a5cdf529648b05a5e527833a476d4
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "parse.h"
23 #include "token.h"
24 #include "symbol.h"
25 #include "target.h"
26 #include "expression.h"
28 static struct symbol *current_fn;
29 static int current_context, current_contextmask;
31 static struct symbol *degenerate(struct expression *expr);
33 static struct symbol *evaluate_symbol_expression(struct expression *expr)
35 struct symbol *sym = expr->symbol;
36 struct symbol *base_type;
38 if (!sym) {
39 warning(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
40 return NULL;
43 examine_symbol_type(sym);
44 if ((sym->ctype.context ^ current_context) & (sym->ctype.contextmask & current_contextmask))
45 warning(expr->pos, "Using symbol '%s' in wrong context", show_ident(expr->symbol_name));
47 base_type = sym->ctype.base_type;
48 if (!base_type) {
49 warning(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
50 return NULL;
53 /* The type of a symbol is the symbol itself! */
54 expr->ctype = sym;
56 /* enums can be turned into plain values */
57 if (sym->type != SYM_ENUM) {
58 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
59 addr->symbol = sym;
60 addr->symbol_name = expr->symbol_name;
61 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
62 expr->type = EXPR_PREOP;
63 expr->op = '*';
64 expr->unop = addr;
65 return sym;
66 } else if (base_type->bit_size < bits_in_int) {
67 /* ugly - we need to force sizeof for these guys */
68 struct expression *e = alloc_expression(expr->pos, EXPR_VALUE);
69 e->value = sym->value;
70 e->ctype = base_type;
71 expr->type = EXPR_PREOP;
72 expr->op = '+';
73 expr->unop = e;
74 } else {
75 expr->type = EXPR_VALUE;
76 expr->value = sym->value;
78 expr->ctype = base_type;
79 return base_type;
82 static struct symbol *evaluate_string(struct expression *expr)
84 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
85 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
86 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
87 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
88 unsigned int length = expr->string->length;
90 sym->array_size = alloc_const_expression(expr->pos, length);
91 sym->bit_size = bits_in_char * length;
92 sym->ctype.alignment = 1;
93 sym->ctype.modifiers = MOD_STATIC;
94 sym->ctype.base_type = array;
95 sym->initializer = initstr;
97 initstr->ctype = sym;
98 initstr->string = expr->string;
100 array->array_size = sym->array_size;
101 array->bit_size = bits_in_char * length;
102 array->ctype.alignment = 1;
103 array->ctype.modifiers = MOD_STATIC;
104 array->ctype.base_type = &char_ctype;
106 addr->symbol = sym;
107 addr->ctype = &lazy_ptr_ctype;
109 expr->type = EXPR_PREOP;
110 expr->op = '*';
111 expr->unop = addr;
112 expr->ctype = sym;
113 return sym;
116 static inline struct symbol *integer_promotion(struct symbol *type)
118 unsigned long mod = type->ctype.modifiers;
119 int width;
121 if (type->type == SYM_ENUM) {
122 type = type->ctype.base_type;
123 mod = type->ctype.modifiers;
125 if (type->type == SYM_BITFIELD) {
126 mod = type->ctype.base_type->ctype.modifiers;
127 width = type->fieldwidth;
128 } else if (mod & (MOD_CHAR | MOD_SHORT))
129 width = type->bit_size;
130 else
131 return type;
132 if (mod & MOD_UNSIGNED && width == bits_in_int)
133 return &uint_ctype;
134 return &int_ctype;
138 * integer part of usual arithmetic conversions:
139 * integer promotions are applied
140 * if left and right are identical, we are done
141 * if signedness is the same, convert one with lower rank
142 * unless unsigned argument has rank lower than signed one, convert the
143 * signed one.
144 * if signed argument is bigger than unsigned one, convert the unsigned.
145 * otherwise, convert signed.
147 * Leaving aside the integer promotions, that is equivalent to
148 * if identical, don't convert
149 * if left is bigger than right, convert right
150 * if right is bigger than left, convert right
151 * otherwise, if signedness is the same, convert one with lower rank
152 * otherwise convert the signed one.
154 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
156 unsigned long lmod, rmod;
158 left = integer_promotion(left);
159 right = integer_promotion(right);
161 if (left == right)
162 goto left;
164 if (left->bit_size > right->bit_size)
165 goto left;
167 if (right->bit_size > left->bit_size)
168 goto right;
170 lmod = left->ctype.modifiers;
171 rmod = right->ctype.modifiers;
172 if ((lmod ^ rmod) & MOD_UNSIGNED) {
173 if (lmod & MOD_UNSIGNED)
174 goto left;
175 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
176 goto left;
177 right:
178 left = right;
179 left:
180 return left;
183 static struct expression * cast_to(struct expression *old, struct symbol *type)
185 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
186 expr->ctype = type;
187 expr->cast_type = type;
188 expr->cast_expression = old;
189 return expr;
192 static int is_type_type(struct symbol *type)
194 return (type->ctype.modifiers & MOD_TYPE) != 0;
197 static int is_ptr_type(struct symbol *type)
199 if (type->type == SYM_NODE)
200 type = type->ctype.base_type;
201 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
204 static inline int is_float_type(struct symbol *type)
206 if (type->type == SYM_NODE)
207 type = type->ctype.base_type;
208 return type->ctype.base_type == &fp_type;
211 static inline int is_byte_type(struct symbol *type)
213 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
216 static inline int is_string_type(struct symbol *type)
218 if (type->type == SYM_NODE)
219 type = type->ctype.base_type;
220 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
223 static struct symbol *bad_expr_type(struct expression *expr)
225 warning(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
226 switch (expr->type) {
227 case EXPR_BINOP:
228 case EXPR_COMPARE:
229 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
230 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
231 break;
232 case EXPR_PREOP:
233 case EXPR_POSTOP:
234 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
235 break;
236 default:
237 break;
240 return NULL;
243 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
245 struct expression *left = *lp, *right = *rp;
246 struct symbol *ltype = left->ctype, *rtype = right->ctype;
248 if (ltype->type == SYM_NODE)
249 ltype = ltype->ctype.base_type;
250 if (rtype->type == SYM_NODE)
251 rtype = rtype->ctype.base_type;
252 if (is_float_type(ltype)) {
253 if (is_int_type(rtype))
254 goto Left;
255 if (is_float_type(rtype)) {
256 unsigned long lmod = ltype->ctype.modifiers;
257 unsigned long rmod = rtype->ctype.modifiers;
258 lmod &= MOD_LONG | MOD_LONGLONG;
259 rmod &= MOD_LONG | MOD_LONGLONG;
260 if (lmod == rmod)
261 return ltype;
262 if (lmod & ~rmod)
263 goto Left;
264 else
265 goto Right;
267 return NULL;
269 if (!is_float_type(rtype) || !is_int_type(ltype))
270 return NULL;
271 Right:
272 *lp = cast_to(left, rtype);
273 return rtype;
274 Left:
275 *rp = cast_to(right, ltype);
276 return ltype;
279 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
281 struct expression *left = *lp, *right = *rp;
282 struct symbol *ltype = left->ctype, *rtype = right->ctype;
284 if (ltype->type == SYM_NODE)
285 ltype = ltype->ctype.base_type;
286 if (rtype->type == SYM_NODE)
287 rtype = rtype->ctype.base_type;
288 if (is_int_type(ltype) && is_int_type(rtype)) {
289 struct symbol *ctype = bigger_int_type(ltype, rtype);
291 /* Don't bother promoting same-size entities, it only adds clutter */
292 if (ltype->bit_size != ctype->bit_size)
293 *lp = cast_to(left, ctype);
294 if (rtype->bit_size != ctype->bit_size)
295 *rp = cast_to(right, ctype);
296 return ctype;
298 return NULL;
301 static int restricted_value(struct expression *v, struct symbol *type)
303 if (v->type != EXPR_VALUE)
304 return 1;
305 if (v->value != 0)
306 return 1;
307 return 0;
310 static int restricted_binop(int op, struct symbol *type)
312 switch (op) {
313 case '&':
314 case '|':
315 case '^':
316 case '?':
317 case SPECIAL_EQUAL:
318 case SPECIAL_NOTEQUAL:
319 return 0;
320 default:
321 return 1;
325 static int restricted_unop(int op, struct symbol *type)
327 if (op == '~' && type->bit_size >= bits_in_int)
328 return 0;
329 if (op == '+')
330 return 0;
331 return 1;
334 static struct symbol *compatible_restricted_binop(int op, struct expression **lp, struct expression **rp)
336 struct expression *left = *lp, *right = *rp;
337 struct symbol *ltype = left->ctype, *rtype = right->ctype;
338 struct symbol *type = NULL;
340 if (ltype->type == SYM_NODE)
341 ltype = ltype->ctype.base_type;
342 if (ltype->type == SYM_ENUM)
343 ltype = ltype->ctype.base_type;
344 if (rtype->type == SYM_NODE)
345 rtype = rtype->ctype.base_type;
346 if (rtype->type == SYM_ENUM)
347 rtype = rtype->ctype.base_type;
348 if (is_restricted_type(ltype)) {
349 if (is_restricted_type(rtype)) {
350 if (ltype == rtype)
351 type = ltype;
352 } else {
353 if (!restricted_value(right, ltype))
354 type = ltype;
356 } else if (is_restricted_type(rtype)) {
357 if (!restricted_value(left, rtype))
358 type = rtype;
360 if (!type)
361 return NULL;
362 if (restricted_binop(op, type))
363 return NULL;
364 return type;
367 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
369 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
370 if (!ctype && float_ok)
371 ctype = compatible_float_binop(&expr->left, &expr->right);
372 if (!ctype)
373 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
374 if (ctype) {
375 expr->ctype = ctype;
376 return ctype;
378 return bad_expr_type(expr);
381 static inline int lvalue_expression(struct expression *expr)
383 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
386 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
388 struct symbol *ctype;
389 struct symbol *ptr_type = ptr->ctype;
390 int bit_size;
392 if (ptr_type->type == SYM_NODE)
393 ptr_type = ptr_type->ctype.base_type;
395 if (!is_int_type(i->ctype))
396 return bad_expr_type(expr);
398 ctype = ptr->ctype;
399 examine_symbol_type(ctype);
401 ctype = degenerate(ptr);
402 if (!ctype->ctype.base_type) {
403 warning(expr->pos, "missing type information");
404 return NULL;
407 /* Get the size of whatever the pointer points to */
408 ptr_type = ctype;
409 if (ptr_type->type == SYM_NODE)
410 ptr_type = ptr_type->ctype.base_type;
411 if (ptr_type->type == SYM_PTR)
412 ptr_type = ptr_type->ctype.base_type;
413 bit_size = ptr_type->bit_size;
415 /* Special case: adding zero commonly happens as a result of 'array[0]' */
416 if (i->type == EXPR_VALUE && !i->value) {
417 *expr = *ptr;
418 } else if (bit_size > bits_in_char) {
419 struct expression *add = expr;
420 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
421 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
423 val->ctype = size_t_ctype;
424 val->value = bit_size >> 3;
426 mul->op = '*';
427 mul->ctype = size_t_ctype;
428 mul->left = i;
429 mul->right = val;
431 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
432 add->left = ptr;
433 add->right = mul;
436 expr->ctype = ctype;
437 return ctype;
440 static struct symbol *evaluate_add(struct expression *expr)
442 struct expression *left = expr->left, *right = expr->right;
443 struct symbol *ltype = left->ctype, *rtype = right->ctype;
445 if (is_ptr_type(ltype))
446 return evaluate_ptr_add(expr, left, right);
448 if (is_ptr_type(rtype))
449 return evaluate_ptr_add(expr, right, left);
451 return evaluate_arith(expr, 1);
454 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
455 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
456 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
457 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED)
459 const char * type_difference(struct symbol *target, struct symbol *source,
460 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
462 for (;;) {
463 unsigned long mod1, mod2, diff;
464 unsigned long as1, as2;
465 int type1, type2;
466 struct symbol *base1, *base2;
468 if (target == source)
469 break;
470 if (!target || !source)
471 return "different types";
473 * Peel of per-node information.
474 * FIXME! Check alignment and context too here!
476 mod1 = target->ctype.modifiers;
477 as1 = target->ctype.as;
478 mod2 = source->ctype.modifiers;
479 as2 = source->ctype.as;
480 if (target->type == SYM_NODE) {
481 target = target->ctype.base_type;
482 if (!target)
483 return "bad types";
484 if (target->type == SYM_PTR) {
485 mod1 = 0;
486 as1 = 0;
488 mod1 |= target->ctype.modifiers;
489 as1 |= target->ctype.as;
491 if (source->type == SYM_NODE) {
492 source = source->ctype.base_type;
493 if (!source)
494 return "bad types";
495 if (source->type == SYM_PTR) {
496 mod2 = 0;
497 as2 = 0;
499 mod2 |= source->ctype.modifiers;
500 as2 |= source->ctype.as;
502 if (target->type == SYM_ENUM) {
503 target = target->ctype.base_type;
504 if (!target)
505 return "bad types";
507 if (source->type == SYM_ENUM) {
508 source = source->ctype.base_type;
509 if (!source)
510 return "bad types";
513 if (target == source)
514 break;
515 if (!target || !source)
516 return "different types";
518 type1 = target->type;
519 base1 = target->ctype.base_type;
521 type2 = source->type;
522 base2 = source->ctype.base_type;
525 * Pointers to functions compare as the function itself
527 if (type1 == SYM_PTR && base1) {
528 switch (base1->type) {
529 case SYM_FN:
530 type1 = SYM_FN;
531 target = base1;
532 base1 = base1->ctype.base_type;
533 default:
534 /* nothing */;
537 if (type2 == SYM_PTR && base2) {
538 switch (base2->type) {
539 case SYM_FN:
540 type2 = SYM_FN;
541 source = base2;
542 base2 = base2->ctype.base_type;
543 default:
544 /* nothing */;
548 /* Arrays degenerate to pointers for type comparisons */
549 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
550 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
552 if (type1 != type2 || type1 == SYM_RESTRICT)
553 return "different base types";
555 /* Must be same address space to be comparable */
556 if (as1 != as2)
557 return "different address spaces";
559 /* Ignore differences in storage types or addressability */
560 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
561 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
562 if (diff) {
563 if (diff & MOD_SIZE)
564 return "different type sizes";
565 if (diff & ~MOD_SIGNEDNESS)
566 return "different modifiers";
568 /* Differs in signedness only.. */
569 if (Wtypesign) {
571 * Warn if both are explicitly signed ("unsigned" is obvously
572 * always explicit, and since we know one of them has to be
573 * unsigned, we check if the signed one was explicit).
575 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
576 return "different explicit signedness";
579 * "char" matches both "unsigned char" and "signed char",
580 * so if the explicit test didn't trigger, then we should
581 * not warn about a char.
583 if (!(mod1 & MOD_CHAR))
584 return "different signedness";
588 if (type1 == SYM_FN) {
589 int i;
590 struct symbol *arg1, *arg2;
591 if (base1->variadic != base2->variadic)
592 return "incompatible variadic arguments";
593 PREPARE_PTR_LIST(target->arguments, arg1);
594 PREPARE_PTR_LIST(source->arguments, arg2);
595 i = 1;
596 for (;;) {
597 const char *diff;
598 diff = type_difference(arg1, arg2, 0, 0);
599 if (diff) {
600 static char argdiff[80];
601 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
602 return argdiff;
604 if (!arg1)
605 break;
606 NEXT_PTR_LIST(arg1);
607 NEXT_PTR_LIST(arg2);
608 i++;
610 FINISH_PTR_LIST(arg2);
611 FINISH_PTR_LIST(arg1);
614 target = base1;
615 source = base2;
617 return NULL;
620 static int is_null_ptr(struct expression *expr)
622 if (expr->type != EXPR_VALUE || expr->value)
623 return 0;
624 if (!is_ptr_type(expr->ctype))
625 warning(expr->pos, "Using plain integer as NULL pointer");
626 return 1;
629 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
631 /* NULL expression? Just return the type of the "other side" */
632 if (is_null_ptr(r))
633 return l->ctype;
634 if (is_null_ptr(l))
635 return r->ctype;
636 return NULL;
640 * Ignore differences in "volatile" and "const"ness when
641 * subtracting pointers
643 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
645 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
647 const char *typediff;
648 struct symbol *ctype;
649 struct symbol *ltype, *rtype;
651 ltype = degenerate(l);
652 rtype = degenerate(r);
655 * If it is an integer subtract: the ptr add case will do the
656 * right thing.
658 if (!is_ptr_type(rtype))
659 return evaluate_ptr_add(expr, l, r);
661 ctype = ltype;
662 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
663 if (typediff) {
664 ctype = common_ptr_type(l, r);
665 if (!ctype) {
666 warning(expr->pos, "subtraction of different types can't work (%s)", typediff);
667 return NULL;
670 examine_symbol_type(ctype);
672 /* Figure out the base type we point to */
673 if (ctype->type == SYM_NODE)
674 ctype = ctype->ctype.base_type;
675 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
676 warning(expr->pos, "subtraction of functions? Share your drugs");
677 return NULL;
679 ctype = ctype->ctype.base_type;
681 expr->ctype = ssize_t_ctype;
682 if (ctype->bit_size > bits_in_char) {
683 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
684 struct expression *div = expr;
685 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
687 val->ctype = size_t_ctype;
688 val->value = ctype->bit_size >> 3;
690 sub->op = '-';
691 sub->ctype = ssize_t_ctype;
692 sub->left = l;
693 sub->right = r;
695 div->op = '/';
696 div->left = sub;
697 div->right = val;
700 return ssize_t_ctype;
703 static struct symbol *evaluate_sub(struct expression *expr)
705 struct expression *left = expr->left, *right = expr->right;
706 struct symbol *ltype = left->ctype;
708 if (is_ptr_type(ltype))
709 return evaluate_ptr_sub(expr, left, right);
711 return evaluate_arith(expr, 1);
714 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
716 static struct symbol *evaluate_conditional(struct expression **p)
718 struct symbol *ctype;
719 struct expression *expr = *p;
721 if (!expr)
722 return NULL;
724 if (expr->type == EXPR_ASSIGNMENT)
725 warning(expr->pos, "assignment expression in conditional");
727 ctype = evaluate_expression(expr);
728 if (ctype) {
729 if (is_safe_type(ctype))
730 warning(expr->pos, "testing a 'safe expression'");
731 if (is_float_type(ctype)) {
732 struct expression *comp;
734 * It's easier to handle here, rather than deal with
735 * FP all over the place. Floating point in boolean
736 * context is rare enough (and very often wrong),
737 * so price of explicit comparison with appropriate
738 * FP zero is not too high. And it simplifies things
739 * elsewhere.
741 comp = alloc_expression(expr->pos, EXPR_BINOP);
742 comp->op = SPECIAL_NOTEQUAL;
743 comp->left = expr;
744 comp->right = alloc_expression(expr->pos, EXPR_FVALUE);
745 comp->right->ctype = comp->left->ctype;
746 comp->right->fvalue = 0;
747 ctype = comp->ctype = &bool_ctype;
748 *p = comp;
752 return ctype;
755 static struct symbol *evaluate_logical(struct expression *expr)
757 if (!evaluate_conditional(&expr->left))
758 return NULL;
759 if (!evaluate_conditional(&expr->right))
760 return NULL;
762 expr->ctype = &bool_ctype;
763 return &bool_ctype;
766 static struct symbol *evaluate_shift(struct expression *expr)
768 struct expression *left = expr->left, *right = expr->right;
769 struct symbol *ltype = left->ctype, *rtype = right->ctype;
771 if (ltype->type == SYM_NODE)
772 ltype = ltype->ctype.base_type;
773 if (rtype->type == SYM_NODE)
774 rtype = rtype->ctype.base_type;
775 if (is_int_type(ltype) && is_int_type(rtype)) {
776 struct symbol *ctype = integer_promotion(ltype);
777 if (ltype->bit_size != ctype->bit_size)
778 expr->left = cast_to(expr->left, ctype);
779 expr->ctype = ctype;
780 ctype = integer_promotion(rtype);
781 if (rtype->bit_size != ctype->bit_size)
782 expr->right = cast_to(expr->right, ctype);
783 return expr->ctype;
785 return bad_expr_type(expr);
788 static struct symbol *evaluate_binop(struct expression *expr)
790 switch (expr->op) {
791 // addition can take ptr+int, fp and int
792 case '+':
793 return evaluate_add(expr);
795 // subtraction can take ptr-ptr, fp and int
796 case '-':
797 return evaluate_sub(expr);
799 // Arithmetic operations can take fp and int
800 case '*': case '/':
801 return evaluate_arith(expr, 1);
803 // shifts do integer promotions, but that's it.
804 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
805 return evaluate_shift(expr);
807 // The rest are integer operations
808 // '%', '&', '^', '|'
809 default:
810 return evaluate_arith(expr, 0);
814 static struct symbol *evaluate_comma(struct expression *expr)
816 expr->ctype = expr->right->ctype;
817 return expr->ctype;
820 static int modify_for_unsigned(int op)
822 if (op == '<')
823 op = SPECIAL_UNSIGNED_LT;
824 else if (op == '>')
825 op = SPECIAL_UNSIGNED_GT;
826 else if (op == SPECIAL_LTE)
827 op = SPECIAL_UNSIGNED_LTE;
828 else if (op == SPECIAL_GTE)
829 op = SPECIAL_UNSIGNED_GTE;
830 return op;
833 static struct symbol *evaluate_compare(struct expression *expr)
835 struct expression *left = expr->left, *right = expr->right;
836 struct symbol *ltype = left->ctype, *rtype = right->ctype;
837 struct symbol *ctype;
839 /* Type types? */
840 if (is_type_type(ltype) && is_type_type(rtype))
841 goto OK;
843 if (is_safe_type(ltype) || is_safe_type(rtype))
844 warning(expr->pos, "testing a 'safe expression'");
846 /* Pointer types? */
847 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
848 // FIXME! Check the types for compatibility
849 goto OK;
852 ctype = compatible_integer_binop(&expr->left, &expr->right);
853 if (ctype) {
854 if (ctype->ctype.modifiers & MOD_UNSIGNED)
855 expr->op = modify_for_unsigned(expr->op);
856 goto OK;
859 ctype = compatible_float_binop(&expr->left, &expr->right);
860 if (ctype)
861 goto OK;
863 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
864 if (ctype)
865 goto OK;
867 bad_expr_type(expr);
870 expr->ctype = &bool_ctype;
871 return &bool_ctype;
875 * FIXME!! This should do casts, array degeneration etc..
877 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
879 struct symbol *ltype = left->ctype, *rtype = right->ctype;
881 if (ltype->type == SYM_NODE)
882 ltype = ltype->ctype.base_type;
884 if (rtype->type == SYM_NODE)
885 rtype = rtype->ctype.base_type;
887 if (ltype->type == SYM_PTR) {
888 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
889 return ltype;
892 if (rtype->type == SYM_PTR) {
893 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
894 return rtype;
896 return NULL;
899 static struct symbol *evaluate_conditional_expression(struct expression *expr)
901 struct symbol *ctype, *ltype, *rtype;
902 const char * typediff;
904 if (!evaluate_conditional(&expr->conditional))
905 return NULL;
906 if (!evaluate_expression(expr->cond_false))
907 return NULL;
908 if (!evaluate_expression(expr->cond_true))
909 return NULL;
911 ctype = degenerate(expr->conditional);
913 ltype = degenerate(expr->cond_true);
914 rtype = degenerate(expr->cond_false);
916 ctype = ltype;
917 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
918 if (!typediff)
919 goto out;
921 ctype = compatible_integer_binop(&expr->cond_true, &expr->cond_false);
922 if (ctype)
923 goto out;
924 ctype = compatible_ptr_type(expr->cond_true, expr->cond_false);
925 if (ctype)
926 goto out;
927 ctype = compatible_float_binop(&expr->cond_true, &expr->cond_false);
928 if (ctype)
929 goto out;
930 ctype = compatible_restricted_binop('?', &expr->cond_true, &expr->cond_false);
931 if (ctype)
932 goto out;
933 warning(expr->pos, "incompatible types in conditional expression (%s)", typediff);
934 return NULL;
936 out:
937 expr->ctype = ctype;
938 return ctype;
941 static struct symbol *evaluate_short_conditional(struct expression *expr)
943 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
944 struct expression *e0, *e1, *e2, *e3;
945 struct symbol *ctype;
947 if (!evaluate_expression(expr->conditional))
948 return NULL;
950 ctype = degenerate(expr->conditional);
952 a->ctype.base_type = ctype;
953 a->bit_size = ctype->bit_size;
954 a->array_size = ctype->array_size;
956 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
957 e0->symbol = a;
958 e0->ctype = &lazy_ptr_ctype;
960 e1 = alloc_expression(expr->pos, EXPR_PREOP);
961 e1->unop = e0;
962 e1->op = '*';
963 e1->ctype = ctype;
965 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
966 e2->left = e1;
967 e2->right = expr->conditional;
968 e2->op = '=';
969 e2->ctype = ctype;
971 e3 = alloc_expression(expr->pos, EXPR_CONDITIONAL);
972 e3->conditional = e1;
973 e3->cond_true = e1;
974 e3->cond_false = expr->cond_false;
975 e3->ctype = evaluate_conditional_expression(e3);
977 expr->type = EXPR_COMMA;
978 expr->left = e2;
979 expr->right = e3;
980 return expr->ctype = e3->ctype;
983 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
984 struct expression **rp, struct symbol *source, const char *where)
986 const char *typediff;
987 struct symbol *t;
988 int target_as;
990 /* It's ok if the target is more volatile or const than the source */
991 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
992 if (!typediff)
993 return 1;
995 if (is_int_type(target)) {
996 if (is_int_type(source)) {
997 if (target->bit_size != source->bit_size)
998 goto Cast;
999 return 1;
1001 if (is_float_type(source))
1002 goto Cast;
1003 } else if (is_float_type(target)) {
1004 if (is_int_type(source))
1005 goto Cast;
1006 if (is_float_type(source)) {
1007 if (target->bit_size != source->bit_size)
1008 goto Cast;
1009 return 1;
1013 if (is_restricted_type(target) && !restricted_value(*rp, target))
1014 return 1;
1016 /* Pointer destination? */
1017 t = target;
1018 target_as = t->ctype.as;
1019 if (t->type == SYM_NODE) {
1020 t = t->ctype.base_type;
1021 target_as |= t->ctype.as;
1023 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
1024 struct expression *right = *rp;
1025 struct symbol *s = source;
1026 int source_as;
1028 // NULL pointer is always ok
1029 if (is_null_ptr(right))
1030 return 1;
1032 /* "void *" matches anything as long as the address space is ok */
1033 source_as = s->ctype.as;
1034 if (s->type == SYM_NODE) {
1035 s = s->ctype.base_type;
1036 source_as |= s->ctype.as;
1038 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1039 s = s->ctype.base_type;
1040 t = t->ctype.base_type;
1041 if (s == &void_ctype || t == &void_ctype)
1042 return 1;
1046 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1047 info(expr->pos, " expected %s", show_typename(target));
1048 info(expr->pos, " got %s", show_typename(source));
1049 *rp = cast_to(*rp, target);
1050 return 0;
1051 Cast:
1052 *rp = cast_to(*rp, target);
1053 return 1;
1057 * FIXME!! This is wrong from a double evaluation standpoint. We can't
1058 * just expand the expression twice, that would make any side effects
1059 * happen twice too.
1061 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
1063 int op = expr->op;
1064 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
1065 static const int op_trans[] = {
1066 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
1067 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
1068 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
1069 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
1070 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
1071 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
1072 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
1073 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
1074 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
1075 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
1077 struct expression *e0, *e1, *e2, *e3, *e4, *e5;
1078 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1079 struct symbol *ltype = left->ctype;
1080 struct expression *addr;
1081 struct symbol *lptype;
1083 if (left->type == EXPR_BITFIELD)
1084 addr = left->address;
1085 else
1086 addr = left->unop;
1088 lptype = addr->ctype;
1090 a->ctype.base_type = lptype;
1091 a->bit_size = lptype->bit_size;
1092 a->array_size = lptype->array_size;
1094 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1095 e0->symbol = a;
1096 e0->ctype = &lazy_ptr_ctype;
1098 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1099 e1->unop = e0;
1100 e1->op = '*';
1101 e1->ctype = lptype;
1103 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1104 e2->left = e1;
1105 e2->right = addr;
1106 e2->op = '=';
1107 e2->ctype = lptype;
1109 /* we can't cannibalize left, unfortunately */
1110 e3 = alloc_expression(expr->pos, left->type);
1111 *e3 = *left;
1112 if (e3->type == EXPR_BITFIELD)
1113 e3->address = e1;
1114 else
1115 e3->unop = e1;
1117 e4 = alloc_expression(expr->pos, EXPR_BINOP);
1118 e4->op = subexpr->op = op_trans[op - SPECIAL_BASE];
1119 e4->left = e3;
1120 e4->right = right;
1121 /* will calculate type later */
1123 e5 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1124 e5->left = e3; /* we can share that one */
1125 e5->right = e4;
1126 e5->op = '=';
1127 e5->ctype = ltype;
1129 expr->type = EXPR_COMMA;
1130 expr->left = e2;
1131 expr->right = e5;
1132 expr->ctype = ltype;
1134 return evaluate_binop(e4);
1137 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1139 if (type->ctype.modifiers & MOD_CONST)
1140 warning(left->pos, "assignment to const expression");
1141 if (type->type == SYM_NODE)
1142 type->ctype.modifiers |= MOD_ASSIGNED;
1145 static struct symbol *evaluate_assignment(struct expression *expr)
1147 struct expression *left = expr->left, *right = expr->right;
1148 struct expression *where = expr;
1149 struct symbol *ltype, *rtype;
1151 if (!lvalue_expression(left)) {
1152 warning(expr->pos, "not an lvalue");
1153 return NULL;
1156 ltype = left->ctype;
1158 if (expr->op != '=') {
1159 if (!evaluate_binop_assignment(expr, left, right))
1160 return NULL;
1161 where = expr->right; /* expr is EXPR_COMMA now */
1162 left = where->left;
1163 right = where->right;
1166 rtype = degenerate(right);
1168 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment"))
1169 return NULL;
1171 evaluate_assign_to(left, ltype);
1173 expr->ctype = ltype;
1174 return ltype;
1177 static void examine_fn_arguments(struct symbol *fn)
1179 struct symbol *s;
1181 FOR_EACH_PTR(fn->arguments, s) {
1182 struct symbol *arg = evaluate_symbol(s);
1183 /* Array/function arguments silently degenerate into pointers */
1184 if (arg) {
1185 struct symbol *ptr;
1186 switch(arg->type) {
1187 case SYM_ARRAY:
1188 case SYM_FN:
1189 ptr = alloc_symbol(s->pos, SYM_PTR);
1190 if (arg->type == SYM_ARRAY)
1191 ptr->ctype = arg->ctype;
1192 else
1193 ptr->ctype.base_type = arg;
1194 ptr->ctype.as |= s->ctype.as;
1195 ptr->ctype.modifiers |= s->ctype.modifiers;
1197 s->ctype.base_type = ptr;
1198 s->ctype.as = 0;
1199 s->ctype.modifiers = 0;
1200 examine_symbol_type(s);
1201 break;
1202 default:
1203 /* nothing */
1204 break;
1207 } END_FOR_EACH_PTR(s);
1210 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1212 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1213 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1214 *newsym = *sym;
1215 newsym->ctype.as = as;
1216 newsym->ctype.modifiers = mod;
1217 sym = newsym;
1219 return sym;
1222 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1224 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1225 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1227 node->ctype.base_type = ptr;
1228 ptr->bit_size = bits_in_pointer;
1229 ptr->ctype.alignment = pointer_alignment;
1231 node->bit_size = bits_in_pointer;
1232 node->ctype.alignment = pointer_alignment;
1234 access_symbol(sym);
1235 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1236 if (sym->ctype.modifiers & MOD_REGISTER) {
1237 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1238 sym->ctype.modifiers &= ~MOD_REGISTER;
1240 if (sym->type == SYM_NODE) {
1241 ptr->ctype.as |= sym->ctype.as;
1242 ptr->ctype.modifiers |= sym->ctype.modifiers;
1243 sym = sym->ctype.base_type;
1245 if (degenerate && sym->type == SYM_ARRAY) {
1246 ptr->ctype.as |= sym->ctype.as;
1247 ptr->ctype.modifiers |= sym->ctype.modifiers;
1248 sym = sym->ctype.base_type;
1250 ptr->ctype.base_type = sym;
1252 return node;
1255 /* Arrays degenerate into pointers on pointer arithmetic */
1256 static struct symbol *degenerate(struct expression *expr)
1258 struct symbol *ctype, *base;
1260 if (!expr)
1261 return NULL;
1262 ctype = expr->ctype;
1263 if (!ctype)
1264 return NULL;
1265 base = ctype;
1266 if (ctype->type == SYM_NODE)
1267 base = ctype->ctype.base_type;
1269 * Arrays degenerate into pointers to the entries, while
1270 * functions degenerate into pointers to themselves.
1271 * If array was part of non-lvalue compound, we create a copy
1272 * of that compound first and then act as if we were dealing with
1273 * the corresponding field in there.
1275 switch (base->type) {
1276 case SYM_ARRAY:
1277 if (expr->type == EXPR_SLICE) {
1278 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1279 struct expression *e0, *e1, *e2, *e3, *e4;
1281 a->ctype.base_type = expr->base->ctype;
1282 a->bit_size = expr->base->ctype->bit_size;
1283 a->array_size = expr->base->ctype->array_size;
1285 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1286 e0->symbol = a;
1287 e0->ctype = &lazy_ptr_ctype;
1289 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1290 e1->unop = e0;
1291 e1->op = '*';
1292 e1->ctype = expr->base->ctype; /* XXX */
1294 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1295 e2->left = e1;
1296 e2->right = expr->base;
1297 e2->op = '=';
1298 e2->ctype = expr->base->ctype;
1300 if (expr->r_bitpos) {
1301 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1302 e3->op = '+';
1303 e3->left = e0;
1304 e3->right = alloc_const_expression(expr->pos,
1305 expr->r_bitpos >> 3);
1306 e3->ctype = &lazy_ptr_ctype;
1307 } else {
1308 e3 = e0;
1311 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1312 e4->left = e2;
1313 e4->right = e3;
1314 e4->ctype = &lazy_ptr_ctype;
1316 expr->unop = e4;
1317 expr->type = EXPR_PREOP;
1318 expr->op = '*';
1320 case SYM_FN:
1321 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1322 warning(expr->pos, "strange non-value function or array");
1323 return NULL;
1325 *expr = *expr->unop;
1326 ctype = create_pointer(expr, ctype, 1);
1327 expr->ctype = ctype;
1328 default:
1329 /* nothing */;
1331 return ctype;
1334 static struct symbol *evaluate_addressof(struct expression *expr)
1336 struct expression *op = expr->unop;
1337 struct symbol *ctype;
1339 if (op->op != '*' || op->type != EXPR_PREOP) {
1340 warning(expr->pos, "not addressable");
1341 return NULL;
1343 ctype = op->ctype;
1344 *expr = *op->unop;
1347 * symbol expression evaluation is lazy about the type
1348 * of the sub-expression, so we may have to generate
1349 * the type here if so..
1351 if (expr->ctype == &lazy_ptr_ctype) {
1352 ctype = create_pointer(expr, ctype, 0);
1353 expr->ctype = ctype;
1355 return expr->ctype;
1359 static struct symbol *evaluate_dereference(struct expression *expr)
1361 struct expression *op = expr->unop;
1362 struct symbol *ctype = op->ctype, *node, *target;
1364 /* Simplify: *&(expr) => (expr) */
1365 if (op->type == EXPR_PREOP && op->op == '&') {
1366 *expr = *op->unop;
1367 return expr->ctype;
1370 /* Dereferencing a node drops all the node information. */
1371 if (ctype->type == SYM_NODE)
1372 ctype = ctype->ctype.base_type;
1374 node = alloc_symbol(expr->pos, SYM_NODE);
1375 target = ctype->ctype.base_type;
1377 switch (ctype->type) {
1378 default:
1379 warning(expr->pos, "cannot derefence this type");
1380 return NULL;
1381 case SYM_PTR:
1382 merge_type(node, ctype);
1383 if (ctype->type != SYM_ARRAY)
1384 break;
1386 * Dereferencing a pointer to an array results in a
1387 * degenerate dereference: the expression becomes
1388 * just a pointer to the entry, and the derefence
1389 * goes away.
1391 *expr = *op;
1393 target = alloc_symbol(expr->pos, SYM_PTR);
1394 target->bit_size = bits_in_pointer;
1395 target->ctype.alignment = pointer_alignment;
1396 merge_type(target, ctype->ctype.base_type);
1397 break;
1399 case SYM_ARRAY:
1400 if (!lvalue_expression(op)) {
1401 warning(op->pos, "non-lvalue array??");
1402 return NULL;
1405 /* Do the implied "addressof" on the array */
1406 *op = *op->unop;
1409 * When an array is dereferenced, we need to pick
1410 * up the attributes of the original node too..
1412 merge_type(node, op->ctype);
1413 merge_type(node, ctype);
1414 break;
1417 node->bit_size = target->bit_size;
1418 node->array_size = target->array_size;
1420 expr->ctype = node;
1421 return node;
1425 * Unary post-ops: x++ and x--
1427 static struct symbol *evaluate_postop(struct expression *expr)
1429 struct expression *op = expr->unop;
1430 struct symbol *ctype = op->ctype;
1432 if (!lvalue_expression(expr->unop)) {
1433 warning(expr->pos, "need lvalue expression for ++/--");
1434 return NULL;
1436 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1437 warning(expr->pos, "bad operation on restricted");
1438 return NULL;
1441 evaluate_assign_to(op, ctype);
1443 expr->ctype = ctype;
1444 return ctype;
1447 static struct symbol *evaluate_sign(struct expression *expr)
1449 struct symbol *ctype = expr->unop->ctype;
1450 if (is_int_type(ctype)) {
1451 struct symbol *rtype = rtype = integer_promotion(ctype);
1452 if (rtype->bit_size != ctype->bit_size)
1453 expr->unop = cast_to(expr->unop, rtype);
1454 ctype = rtype;
1455 } else if (is_float_type(ctype) && expr->op != '~') {
1456 /* no conversions needed */
1457 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1458 /* no conversions needed */
1459 } else {
1460 return bad_expr_type(expr);
1462 if (expr->op == '+')
1463 *expr = *expr->unop;
1464 expr->ctype = ctype;
1465 return ctype;
1468 static struct symbol *evaluate_preop(struct expression *expr)
1470 struct symbol *ctype = expr->unop->ctype;
1472 switch (expr->op) {
1473 case '(':
1474 *expr = *expr->unop;
1475 return ctype;
1477 case '+':
1478 case '-':
1479 case '~':
1480 return evaluate_sign(expr);
1482 case '*':
1483 return evaluate_dereference(expr);
1485 case '&':
1486 return evaluate_addressof(expr);
1488 case SPECIAL_INCREMENT:
1489 case SPECIAL_DECREMENT:
1491 * From a type evaluation standpoint the pre-ops are
1492 * the same as the postops
1494 return evaluate_postop(expr);
1496 case '!':
1497 if (is_safe_type(ctype))
1498 warning(expr->pos, "testing a 'safe expression'");
1499 if (is_float_type(ctype)) {
1500 struct expression *arg = expr->unop;
1501 expr->type = EXPR_BINOP;
1502 expr->op = SPECIAL_EQUAL;
1503 expr->left = arg;
1504 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1505 expr->right->ctype = ctype;
1506 expr->right->fvalue = 0;
1508 ctype = &bool_ctype;
1509 break;
1511 default:
1512 break;
1514 expr->ctype = ctype;
1515 return &bool_ctype;
1518 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1520 struct ptr_list *head = (struct ptr_list *)_list;
1521 struct ptr_list *list = head;
1523 if (!head)
1524 return NULL;
1525 do {
1526 int i;
1527 for (i = 0; i < list->nr; i++) {
1528 struct symbol *sym = (struct symbol *) list->list[i];
1529 if (sym->ident) {
1530 if (sym->ident != ident)
1531 continue;
1532 *offset = sym->offset;
1533 return sym;
1534 } else {
1535 struct symbol *ctype = sym->ctype.base_type;
1536 struct symbol *sub;
1537 if (!ctype)
1538 continue;
1539 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1540 continue;
1541 sub = find_identifier(ident, ctype->symbol_list, offset);
1542 if (!sub)
1543 continue;
1544 *offset += sym->offset;
1545 return sub;
1548 } while ((list = list->next) != head);
1549 return NULL;
1552 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1554 struct expression *add;
1557 * Create a new add-expression
1559 * NOTE! Even if we just add zero, we need a new node
1560 * for the member pointer, since it has a different
1561 * type than the original pointer. We could make that
1562 * be just a cast, but the fact is, a node is a node,
1563 * so we might as well just do the "add zero" here.
1565 add = alloc_expression(expr->pos, EXPR_BINOP);
1566 add->op = '+';
1567 add->left = expr;
1568 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1569 add->right->ctype = &int_ctype;
1570 add->right->value = offset;
1573 * The ctype of the pointer will be lazily evaluated if
1574 * we ever take the address of this member dereference..
1576 add->ctype = &lazy_ptr_ctype;
1577 return add;
1580 /* structure/union dereference */
1581 static struct symbol *evaluate_member_dereference(struct expression *expr)
1583 int offset;
1584 struct symbol *ctype, *member;
1585 struct expression *deref = expr->deref, *add;
1586 struct ident *ident = expr->member;
1587 unsigned int mod;
1588 int address_space;
1590 if (!evaluate_expression(deref))
1591 return NULL;
1592 if (!ident) {
1593 warning(expr->pos, "bad member name");
1594 return NULL;
1597 ctype = deref->ctype;
1598 address_space = ctype->ctype.as;
1599 mod = ctype->ctype.modifiers;
1600 if (ctype->type == SYM_NODE) {
1601 ctype = ctype->ctype.base_type;
1602 address_space |= ctype->ctype.as;
1603 mod |= ctype->ctype.modifiers;
1605 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1606 warning(expr->pos, "expected structure or union");
1607 return NULL;
1609 offset = 0;
1610 member = find_identifier(ident, ctype->symbol_list, &offset);
1611 if (!member) {
1612 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1613 const char *name = "<unnamed>";
1614 int namelen = 9;
1615 if (ctype->ident) {
1616 name = ctype->ident->name;
1617 namelen = ctype->ident->len;
1619 warning(expr->pos, "no member '%s' in %s %.*s",
1620 show_ident(ident), type, namelen, name);
1621 return NULL;
1625 * The member needs to take on the address space and modifiers of
1626 * the "parent" type.
1628 member = convert_to_as_mod(member, address_space, mod);
1629 ctype = member->ctype.base_type;
1631 if (!lvalue_expression(deref)) {
1632 if (deref->type != EXPR_SLICE) {
1633 expr->base = deref;
1634 expr->r_bitpos = 0;
1635 } else {
1636 expr->base = deref->base;
1637 expr->r_bitpos = deref->r_bitpos;
1639 expr->r_bitpos += offset << 3;
1640 expr->type = EXPR_SLICE;
1641 if (ctype->type == SYM_BITFIELD) {
1642 expr->r_bitpos += member->bit_offset;
1643 expr->r_nrbits = member->fieldwidth;
1644 } else {
1645 expr->r_nrbits = member->bit_size;
1647 expr->ctype = member;
1648 return member;
1651 deref = deref->unop;
1652 expr->deref = deref;
1654 add = evaluate_offset(deref, offset);
1655 if (ctype->type == SYM_BITFIELD) {
1656 expr->type = EXPR_BITFIELD;
1657 expr->bitpos = member->bit_offset;
1658 expr->nrbits = member->fieldwidth;
1659 expr->address = add;
1660 } else {
1661 expr->type = EXPR_PREOP;
1662 expr->op = '*';
1663 expr->unop = add;
1666 expr->ctype = member;
1667 return member;
1670 static int is_promoted(struct expression *expr)
1672 while (1) {
1673 switch (expr->type) {
1674 case EXPR_BINOP:
1675 case EXPR_SELECT:
1676 case EXPR_CONDITIONAL:
1677 return 1;
1678 case EXPR_COMMA:
1679 expr = expr->right;
1680 continue;
1681 case EXPR_PREOP:
1682 switch (expr->op) {
1683 case '(':
1684 expr = expr->unop;
1685 continue;
1686 case '+':
1687 case '-':
1688 case '~':
1689 return 1;
1690 default:
1691 return 0;
1693 default:
1694 return 0;
1700 static struct symbol *evaluate_cast(struct expression *);
1702 static struct symbol *evaluate_sizeof(struct expression *expr)
1704 struct expression *what = expr->cast_expression;
1705 int size;
1707 if (expr->cast_type) {
1708 if (what) {
1709 struct symbol *sym = evaluate_cast(expr);
1710 size = sym->bit_size;
1711 } else {
1712 examine_symbol_type(expr->cast_type);
1713 size = expr->cast_type->bit_size;
1715 } else {
1716 if (!evaluate_expression(what))
1717 return NULL;
1718 size = what->ctype->bit_size;
1719 if (is_restricted_type(what->ctype)) {
1720 if (size < bits_in_int && is_promoted(what))
1721 size = bits_in_int;
1723 if (is_bitfield_type(what->ctype))
1724 warning(expr->pos, "sizeof applied to bitfield type");
1726 if (size & 7)
1727 warning(expr->pos, "cannot size expression");
1728 expr->type = EXPR_VALUE;
1729 expr->value = size >> 3;
1730 expr->ctype = size_t_ctype;
1731 return size_t_ctype;
1734 static struct symbol *evaluate_alignof(struct expression *expr)
1736 struct symbol *type = expr->cast_type;
1738 if (!type) {
1739 type = evaluate_expression(expr->cast_expression);
1740 if (!type)
1741 return NULL;
1743 if (is_bitfield_type(type))
1744 warning(expr->pos, "alignof applied to bitfield type");
1745 examine_symbol_type(type);
1746 expr->type = EXPR_VALUE;
1747 expr->value = type->ctype.alignment;
1748 expr->ctype = size_t_ctype;
1749 return size_t_ctype;
1752 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1754 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1755 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1756 return clash != 0;
1759 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1761 struct expression *expr;
1762 struct symbol_list *argument_types = fn->arguments;
1763 struct symbol *argtype;
1764 int i = 1;
1766 PREPARE_PTR_LIST(argument_types, argtype);
1767 FOR_EACH_PTR (head, expr) {
1768 struct expression **p = THIS_ADDRESS(expr);
1769 struct symbol *ctype, *target;
1770 ctype = evaluate_expression(expr);
1772 if (!ctype)
1773 return 0;
1775 if (context_clash(f, ctype))
1776 warning(expr->pos, "argument %d used in wrong context", i);
1778 ctype = degenerate(expr);
1780 target = argtype;
1781 if (!target && ctype->bit_size < bits_in_int)
1782 target = &int_ctype;
1783 if (target) {
1784 static char where[30];
1785 examine_symbol_type(target);
1786 sprintf(where, "argument %d", i);
1787 compatible_assignment_types(expr, target, p, ctype, where);
1790 i++;
1791 NEXT_PTR_LIST(argtype);
1792 } END_FOR_EACH_PTR(expr);
1793 FINISH_PTR_LIST(argtype);
1794 return 1;
1797 static int evaluate_initializer(struct symbol *ctype, struct expression **ep);
1799 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1801 struct expression *entry = *ep;
1802 struct expression **parent, *reuse = NULL;
1803 unsigned long offset;
1804 struct symbol *sym;
1805 unsigned long from, to;
1806 int accept_string = is_byte_type(ctype);
1808 from = current;
1809 to = from+1;
1810 parent = ep;
1811 if (entry->type == EXPR_INDEX) {
1812 from = entry->idx_from;
1813 to = entry->idx_to+1;
1814 parent = &entry->idx_expression;
1815 reuse = entry;
1816 entry = entry->idx_expression;
1819 offset = from * (ctype->bit_size>>3);
1820 if (offset) {
1821 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1822 reuse->type = EXPR_POS;
1823 reuse->ctype = ctype;
1824 reuse->init_offset = offset;
1825 reuse->init_nr = to - from;
1826 reuse->init_expr = entry;
1827 parent = &reuse->init_expr;
1828 entry = reuse;
1830 *ep = entry;
1832 if (accept_string && entry->type == EXPR_STRING) {
1833 sym = evaluate_expression(entry);
1834 to = from + get_expression_value(sym->array_size);
1835 } else {
1836 evaluate_initializer(ctype, parent);
1838 return to;
1841 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1843 struct expression *entry;
1844 int current = 0;
1845 int max = 0;
1847 FOR_EACH_PTR(expr->expr_list, entry) {
1848 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1849 if (current > max)
1850 max = current;
1851 } END_FOR_EACH_PTR(entry);
1852 return max;
1855 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1856 static int evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1858 if (expression_list_size(expr->expr_list) != 1) {
1859 warning(expr->pos, "unexpected compound initializer");
1860 return 0;
1862 return evaluate_array_initializer(ctype, expr);
1865 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1867 struct symbol *sym;
1869 FOR_EACH_PTR(ctype->symbol_list, sym) {
1870 if (sym->ident == ident)
1871 return sym;
1872 } END_FOR_EACH_PTR(sym);
1873 return NULL;
1876 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1878 struct expression *entry = *ep;
1879 struct expression **parent;
1880 struct expression *reuse = NULL;
1881 unsigned long offset;
1883 if (!sym) {
1884 error(entry->pos, "unknown named initializer");
1885 return -1;
1888 if (entry->type == EXPR_IDENTIFIER) {
1889 reuse = entry;
1890 entry = entry->ident_expression;
1893 parent = ep;
1894 offset = sym->offset;
1895 if (offset) {
1896 if (!reuse)
1897 reuse = alloc_expression(entry->pos, EXPR_POS);
1898 reuse->type = EXPR_POS;
1899 reuse->ctype = ctype;
1900 reuse->init_offset = offset;
1901 reuse->init_nr = 1;
1902 reuse->init_expr = entry;
1903 parent = &reuse->init_expr;
1904 entry = reuse;
1906 *ep = entry;
1907 evaluate_initializer(sym, parent);
1908 return 0;
1911 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1913 struct expression *entry;
1914 struct symbol *sym;
1916 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1917 FOR_EACH_PTR(expr->expr_list, entry) {
1918 if (entry->type == EXPR_IDENTIFIER) {
1919 struct ident *ident = entry->expr_ident;
1920 /* We special-case the "already right place" case */
1921 if (!sym || sym->ident != ident) {
1922 RESET_PTR_LIST(sym);
1923 for (;;) {
1924 if (!sym)
1925 break;
1926 if (sym->ident == ident)
1927 break;
1928 NEXT_PTR_LIST(sym);
1932 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1933 return 0;
1934 NEXT_PTR_LIST(sym);
1935 } END_FOR_EACH_PTR(entry);
1936 FINISH_PTR_LIST(sym);
1938 return 0;
1942 * Initializers are kind of like assignments. Except
1943 * they can be a hell of a lot more complex.
1945 static int evaluate_initializer(struct symbol *ctype, struct expression **ep)
1947 struct expression *expr = *ep;
1950 * Simple non-structure/array initializers are the simple
1951 * case, and look (and parse) largely like assignments.
1953 switch (expr->type) {
1954 default: {
1955 int size = 0, is_string = expr->type == EXPR_STRING;
1956 struct symbol *rtype = evaluate_expression(expr);
1957 if (rtype) {
1959 * Special case:
1960 * char array[] = "string"
1961 * should _not_ degenerate.
1963 if (is_string && is_string_type(ctype)) {
1964 struct expression *array_size = ctype->array_size;
1965 if (!array_size)
1966 array_size = ctype->array_size = rtype->array_size;
1967 size = get_expression_value(array_size);
1968 } else {
1969 rtype = degenerate(expr);
1970 size = 1;
1972 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1974 return size;
1977 case EXPR_INITIALIZER:
1978 expr->ctype = ctype;
1979 if (ctype->type == SYM_NODE)
1980 ctype = ctype->ctype.base_type;
1982 switch (ctype->type) {
1983 case SYM_ARRAY:
1984 case SYM_PTR:
1985 return evaluate_array_initializer(ctype->ctype.base_type, expr);
1986 case SYM_UNION:
1987 return evaluate_struct_or_union_initializer(ctype, expr, 0);
1988 case SYM_STRUCT:
1989 return evaluate_struct_or_union_initializer(ctype, expr, 1);
1990 default:
1991 return evaluate_scalar_initializer(ctype, expr);
1994 case EXPR_IDENTIFIER:
1995 if (ctype->type == SYM_NODE)
1996 ctype = ctype->ctype.base_type;
1997 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
1998 error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
1999 show_symbol(ctype);
2000 return 0;
2002 return evaluate_one_struct_initializer(ctype, ep,
2003 find_struct_ident(ctype, expr->expr_ident));
2005 case EXPR_INDEX:
2006 if (ctype->type == SYM_NODE)
2007 ctype = ctype->ctype.base_type;
2008 if (ctype->type != SYM_ARRAY) {
2009 error(expr->pos, "expected array");
2010 return 0;
2012 return evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
2014 case EXPR_POS:
2016 * An EXPR_POS expression has already been evaluated, and we don't
2017 * need to do anything more
2019 return 0;
2023 static int get_as(struct symbol *sym)
2025 int as;
2026 unsigned long mod;
2028 if (!sym)
2029 return 0;
2030 as = sym->ctype.as;
2031 mod = sym->ctype.modifiers;
2032 if (sym->type == SYM_NODE) {
2033 sym = sym->ctype.base_type;
2034 as |= sym->ctype.as;
2035 mod |= sym->ctype.modifiers;
2039 * At least for now, allow casting to a "unsigned long".
2040 * That's how we do things like pointer arithmetic and
2041 * store pointers to registers.
2043 if (sym == &ulong_ctype)
2044 return -1;
2046 if (sym && sym->type == SYM_PTR) {
2047 sym = sym->ctype.base_type;
2048 as |= sym->ctype.as;
2049 mod |= sym->ctype.modifiers;
2051 if (mod & MOD_FORCE)
2052 return -1;
2053 return as;
2056 static struct symbol *evaluate_cast(struct expression *expr)
2058 struct expression *target = expr->cast_expression;
2059 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2060 enum type type;
2062 if (!target)
2063 return NULL;
2065 expr->ctype = ctype;
2066 expr->cast_type = ctype;
2069 * Special case: a cast can be followed by an
2070 * initializer, in which case we need to pass
2071 * the type value down to that initializer rather
2072 * than trying to evaluate it as an expression
2074 * A more complex case is when the initializer is
2075 * dereferenced as part of a post-fix expression.
2076 * We need to produce an expression that can be dereferenced.
2078 if (target->type == EXPR_INITIALIZER) {
2079 struct symbol *sym = expr->cast_type;
2080 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2082 sym->initializer = expr->cast_expression;
2083 evaluate_symbol(sym);
2085 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2086 addr->symbol = sym;
2088 expr->type = EXPR_PREOP;
2089 expr->op = '*';
2090 expr->unop = addr;
2091 expr->ctype = sym;
2093 return sym;
2096 evaluate_expression(target);
2097 degenerate(target);
2100 * You can always throw a value away by casting to
2101 * "void" - that's an implicit "force". Note that
2102 * the same is _not_ true of "void *".
2104 if (ctype == &void_ctype)
2105 goto out;
2107 type = ctype->type;
2108 if (type == SYM_NODE) {
2109 type = ctype->ctype.base_type->type;
2110 if (ctype->ctype.base_type == &void_ctype)
2111 goto out;
2113 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2114 warning(expr->pos, "cast to non-scalar");
2116 if (!target->ctype) {
2117 warning(expr->pos, "cast from unknown type");
2118 goto out;
2121 type = target->ctype->type;
2122 if (type == SYM_NODE)
2123 type = target->ctype->ctype.base_type->type;
2124 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2125 warning(expr->pos, "cast from non-scalar");
2127 if (!get_as(ctype) && get_as(target->ctype) > 0)
2128 warning(expr->pos, "cast removes address space of expression");
2130 if (!(ctype->ctype.modifiers & MOD_FORCE)) {
2131 struct symbol *t1 = ctype, *t2 = target->ctype;
2132 if (t1->type == SYM_NODE)
2133 t1 = t1->ctype.base_type;
2134 if (t2->type == SYM_NODE)
2135 t2 = t2->ctype.base_type;
2136 if (t1 != t2) {
2137 if (t1->type == SYM_RESTRICT)
2138 warning(expr->pos, "cast to restricted type");
2139 if (t2->type == SYM_RESTRICT)
2140 warning(expr->pos, "cast from restricted type");
2145 * Casts of constant values are special: they
2146 * can be NULL, and thus need to be simplified
2147 * early.
2149 if (target->type == EXPR_VALUE)
2150 cast_value(expr, ctype, target, target->ctype);
2152 out:
2153 return ctype;
2157 * Evaluate a call expression with a symbol. This
2158 * should expand inline functions, and evaluate
2159 * builtins.
2161 static int evaluate_symbol_call(struct expression *expr)
2163 struct expression *fn = expr->fn;
2164 struct symbol *ctype = fn->ctype;
2166 if (fn->type != EXPR_PREOP)
2167 return 0;
2169 if (ctype->op && ctype->op->evaluate)
2170 return ctype->op->evaluate(expr);
2172 if (ctype->ctype.modifiers & MOD_INLINE) {
2173 int ret;
2174 struct symbol *curr = current_fn;
2175 unsigned long context = current_context;
2176 unsigned long mask = current_contextmask;
2178 current_context |= ctype->ctype.context;
2179 current_contextmask |= ctype->ctype.contextmask;
2180 current_fn = ctype->ctype.base_type;
2181 examine_fn_arguments(current_fn);
2183 ret = inline_function(expr, ctype);
2185 /* restore the old function context */
2186 current_fn = curr;
2187 current_context = context;
2188 current_contextmask = mask;
2189 return ret;
2192 return 0;
2195 static struct symbol *evaluate_call(struct expression *expr)
2197 int args, fnargs;
2198 struct symbol *ctype, *sym;
2199 struct expression *fn = expr->fn;
2200 struct expression_list *arglist = expr->args;
2202 if (!evaluate_expression(fn))
2203 return NULL;
2204 sym = ctype = fn->ctype;
2205 if (ctype->type == SYM_NODE)
2206 ctype = ctype->ctype.base_type;
2207 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2208 ctype = ctype->ctype.base_type;
2209 if (!evaluate_arguments(sym, ctype, arglist))
2210 return NULL;
2211 if (ctype->type != SYM_FN) {
2212 warning(expr->pos, "not a function %s", show_ident(sym->ident));
2213 return NULL;
2215 args = expression_list_size(expr->args);
2216 fnargs = symbol_list_size(ctype->arguments);
2217 if (args < fnargs)
2218 warning(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2219 if (args > fnargs && !ctype->variadic)
2220 warning(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2221 if (sym->type == SYM_NODE) {
2222 if (evaluate_symbol_call(expr))
2223 return expr->ctype;
2225 expr->ctype = ctype->ctype.base_type;
2226 return expr->ctype;
2229 struct symbol *evaluate_expression(struct expression *expr)
2231 if (!expr)
2232 return NULL;
2233 if (expr->ctype)
2234 return expr->ctype;
2236 switch (expr->type) {
2237 case EXPR_VALUE:
2238 case EXPR_FVALUE:
2239 warning(expr->pos, "value expression without a type");
2240 return NULL;
2241 case EXPR_STRING:
2242 return evaluate_string(expr);
2243 case EXPR_SYMBOL:
2244 return evaluate_symbol_expression(expr);
2245 case EXPR_BINOP:
2246 if (!evaluate_expression(expr->left))
2247 return NULL;
2248 if (!evaluate_expression(expr->right))
2249 return NULL;
2250 return evaluate_binop(expr);
2251 case EXPR_LOGICAL:
2252 return evaluate_logical(expr);
2253 case EXPR_COMMA:
2254 evaluate_expression(expr->left);
2255 if (!evaluate_expression(expr->right))
2256 return NULL;
2257 return evaluate_comma(expr);
2258 case EXPR_COMPARE:
2259 if (!evaluate_expression(expr->left))
2260 return NULL;
2261 if (!evaluate_expression(expr->right))
2262 return NULL;
2263 return evaluate_compare(expr);
2264 case EXPR_ASSIGNMENT:
2265 if (!evaluate_expression(expr->left))
2266 return NULL;
2267 if (!evaluate_expression(expr->right))
2268 return NULL;
2269 return evaluate_assignment(expr);
2270 case EXPR_PREOP:
2271 if (!evaluate_expression(expr->unop))
2272 return NULL;
2273 return evaluate_preop(expr);
2274 case EXPR_POSTOP:
2275 if (!evaluate_expression(expr->unop))
2276 return NULL;
2277 return evaluate_postop(expr);
2278 case EXPR_CAST:
2279 return evaluate_cast(expr);
2280 case EXPR_SIZEOF:
2281 return evaluate_sizeof(expr);
2282 case EXPR_ALIGNOF:
2283 return evaluate_alignof(expr);
2284 case EXPR_DEREF:
2285 return evaluate_member_dereference(expr);
2286 case EXPR_CALL:
2287 return evaluate_call(expr);
2288 case EXPR_BITFIELD:
2289 warning(expr->pos, "bitfield generated by parser");
2290 return NULL;
2291 case EXPR_SELECT:
2292 case EXPR_CONDITIONAL:
2293 if (expr->cond_true)
2294 return evaluate_conditional_expression(expr);
2295 else
2296 return evaluate_short_conditional(expr);
2297 case EXPR_STATEMENT:
2298 expr->ctype = evaluate_statement(expr->statement);
2299 return expr->ctype;
2301 case EXPR_LABEL:
2302 expr->ctype = &ptr_ctype;
2303 return &ptr_ctype;
2305 case EXPR_TYPE:
2306 /* Evaluate the type of the symbol .. */
2307 evaluate_symbol(expr->symbol);
2308 /* .. but the type of the _expression_ is a "type" */
2309 expr->ctype = &type_ctype;
2310 return &type_ctype;
2312 /* These can not exist as stand-alone expressions */
2313 case EXPR_INITIALIZER:
2314 case EXPR_IDENTIFIER:
2315 case EXPR_INDEX:
2316 case EXPR_POS:
2317 warning(expr->pos, "internal front-end error: initializer in expression");
2318 return NULL;
2319 case EXPR_SLICE:
2320 warning(expr->pos, "internal front-end error: SLICE re-evaluated");
2321 return NULL;
2323 return NULL;
2326 void check_duplicates(struct symbol *sym)
2328 struct symbol *next = sym;
2330 while ((next = next->same_symbol) != NULL) {
2331 const char *typediff;
2332 evaluate_symbol(next);
2333 typediff = type_difference(sym, next, 0, 0);
2334 if (typediff) {
2335 warning(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2336 show_ident(sym->ident),
2337 input_streams[next->pos.stream].name, next->pos.line, typediff);
2338 return;
2343 struct symbol *evaluate_symbol(struct symbol *sym)
2345 struct symbol *base_type;
2347 if (!sym)
2348 return sym;
2350 sym = examine_symbol_type(sym);
2351 base_type = sym->ctype.base_type;
2352 if (!base_type)
2353 return NULL;
2355 /* Evaluate the initializers */
2356 if (sym->initializer) {
2357 int count = evaluate_initializer(sym, &sym->initializer);
2358 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
2359 int bit_size = count * base_type->ctype.base_type->bit_size;
2360 base_type->array_size = alloc_const_expression(sym->pos, count);
2361 base_type->bit_size = bit_size;
2362 sym->array_size = base_type->array_size;
2363 sym->bit_size = bit_size;
2367 /* And finally, evaluate the body of the symbol too */
2368 if (base_type->type == SYM_FN) {
2369 struct symbol *curr = current_fn;
2370 unsigned long context = current_context;
2371 unsigned long mask = current_contextmask;
2373 current_fn = base_type;
2374 current_contextmask = sym->ctype.contextmask;
2375 current_context = sym->ctype.context;
2377 examine_fn_arguments(base_type);
2378 if (!base_type->stmt && base_type->inline_stmt)
2379 uninline(sym);
2380 if (base_type->stmt)
2381 evaluate_statement(base_type->stmt);
2383 current_fn = curr;
2384 current_contextmask = mask;
2385 current_context = context;
2388 return base_type;
2391 struct symbol *evaluate_return_expression(struct statement *stmt)
2393 struct expression *expr = stmt->expression;
2394 struct symbol *ctype, *fntype;
2396 evaluate_expression(expr);
2397 ctype = degenerate(expr);
2398 fntype = current_fn->ctype.base_type;
2399 if (!fntype || fntype == &void_ctype) {
2400 if (expr && ctype != &void_ctype)
2401 warning(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2402 return NULL;
2405 if (!expr) {
2406 warning(stmt->pos, "return with no return value");
2407 return NULL;
2409 if (!ctype)
2410 return NULL;
2411 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
2412 return NULL;
2415 static void evaluate_if_statement(struct statement *stmt)
2417 if (!stmt->if_conditional)
2418 return;
2420 evaluate_conditional(&stmt->if_conditional);
2421 evaluate_statement(stmt->if_true);
2422 evaluate_statement(stmt->if_false);
2425 static void evaluate_iterator(struct statement *stmt)
2427 struct expression **pre = &stmt->iterator_pre_condition;
2428 struct expression **post = &stmt->iterator_post_condition;
2429 if (*pre == *post) {
2430 evaluate_conditional(pre);
2431 *post = *pre;
2432 } else {
2433 evaluate_conditional(pre);
2434 evaluate_conditional(post);
2436 evaluate_statement(stmt->iterator_pre_statement);
2437 evaluate_statement(stmt->iterator_statement);
2438 evaluate_statement(stmt->iterator_post_statement);
2441 struct symbol *evaluate_statement(struct statement *stmt)
2443 if (!stmt)
2444 return NULL;
2446 switch (stmt->type) {
2447 case STMT_RETURN:
2448 return evaluate_return_expression(stmt);
2450 case STMT_EXPRESSION:
2451 if (!evaluate_expression(stmt->expression))
2452 return NULL;
2453 return degenerate(stmt->expression);
2455 case STMT_COMPOUND: {
2456 struct statement *s;
2457 struct symbol *type = NULL;
2458 struct symbol *sym;
2460 /* Evaluate each symbol in the compound statement */
2461 FOR_EACH_PTR(stmt->syms, sym) {
2462 evaluate_symbol(sym);
2463 } END_FOR_EACH_PTR(sym);
2464 evaluate_symbol(stmt->ret);
2467 * Then, evaluate each statement, making the type of the
2468 * compound statement be the type of the last statement
2470 type = NULL;
2471 FOR_EACH_PTR(stmt->stmts, s) {
2472 type = evaluate_statement(s);
2473 } END_FOR_EACH_PTR(s);
2474 if (!type)
2475 type = &void_ctype;
2476 return type;
2478 case STMT_IF:
2479 evaluate_if_statement(stmt);
2480 return NULL;
2481 case STMT_ITERATOR:
2482 evaluate_iterator(stmt);
2483 return NULL;
2484 case STMT_SWITCH:
2485 evaluate_expression(stmt->switch_expression);
2486 evaluate_statement(stmt->switch_statement);
2487 return NULL;
2488 case STMT_CASE:
2489 evaluate_expression(stmt->case_expression);
2490 evaluate_expression(stmt->case_to);
2491 evaluate_statement(stmt->case_statement);
2492 return NULL;
2493 case STMT_LABEL:
2494 return evaluate_statement(stmt->label_statement);
2495 case STMT_GOTO:
2496 evaluate_expression(stmt->goto_expression);
2497 return NULL;
2498 case STMT_NONE:
2499 break;
2500 case STMT_ASM:
2501 /* FIXME! Do the asm parameter evaluation! */
2502 break;
2504 return NULL;