Oops. Fix the preprocessor handling of "..." arguments.
[smatch.git] / evaluate.c
blobe1dd340c7e341ed433b065a76b691ff44e6c364d
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 *evaluate_symbol_expression(struct expression *expr)
33 struct symbol *sym = expr->symbol;
34 struct symbol *base_type;
36 if (!sym) {
37 if (preprocessing) {
38 expr->ctype = &int_ctype;
39 return &int_ctype;
41 warn(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
42 return NULL;
45 examine_symbol_type(sym);
46 if ((sym->ctype.context ^ current_context) & (sym->ctype.contextmask & current_contextmask))
47 warn(expr->pos, "Using symbol '%s' in wrong context", show_ident(expr->symbol_name));
49 base_type = sym->ctype.base_type;
50 if (!base_type) {
51 warn(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
52 return NULL;
55 /* The type of a symbol is the symbol itself! */
56 expr->ctype = sym;
58 /* enum's can be turned into plain values */
59 if (sym->type != SYM_ENUM) {
60 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
61 addr->symbol = sym;
62 addr->symbol_name = expr->symbol_name;
63 addr->ctype = &ptr_ctype;
64 expr->type = EXPR_PREOP;
65 expr->op = '*';
66 expr->unop = addr;
67 return sym;
69 expr->type = EXPR_VALUE;
70 expr->value = sym->value;
71 expr->ctype = base_type;
72 return sym;
75 static struct symbol *evaluate_string(struct expression *expr)
77 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
78 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
79 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
80 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
81 unsigned int length = expr->string->length;
83 sym->array_size = alloc_const_expression(expr->pos, length);
84 sym->bit_size = BITS_IN_CHAR * length;
85 sym->ctype.alignment = 1;
86 sym->ctype.modifiers = MOD_STATIC;
87 sym->ctype.base_type = array;
88 sym->initializer = initstr;
90 initstr->ctype = sym;
91 initstr->string = expr->string;
93 array->array_size = sym->array_size;
94 array->bit_size = BITS_IN_CHAR * length;
95 array->ctype.alignment = 1;
96 array->ctype.modifiers = MOD_STATIC;
97 array->ctype.base_type = &char_ctype;
99 addr->symbol = sym;
100 addr->ctype = &ptr_ctype;
102 expr->type = EXPR_PREOP;
103 expr->op = '*';
104 expr->unop = addr;
105 expr->ctype = sym;
106 return sym;
109 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
111 unsigned long lmod, rmod, mod;
113 if (left == right)
114 return left;
116 if (left->bit_size > right->bit_size)
117 return left;
119 if (right->bit_size > left->bit_size)
120 return right;
122 /* Same size integers - promote to unsigned, promote to long */
123 lmod = left->ctype.modifiers;
124 rmod = right->ctype.modifiers;
125 mod = lmod | rmod;
126 if (mod == lmod)
127 return left;
128 if (mod == rmod)
129 return right;
130 return ctype_integer(mod);
133 static struct expression * cast_to(struct expression *old, struct symbol *type)
135 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
136 expr->ctype = type;
137 expr->cast_type = type;
138 expr->cast_expression = old;
139 return expr;
142 static int is_type_type(struct symbol *type)
144 return (type->ctype.modifiers & MOD_TYPE) != 0;
147 static int is_ptr_type(struct symbol *type)
149 if (type->type == SYM_NODE)
150 type = type->ctype.base_type;
151 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
154 static int is_int_type(struct symbol *type)
156 if (type->type == SYM_NODE)
157 type = type->ctype.base_type;
158 return type->ctype.base_type == &int_type;
161 static struct symbol *bad_expr_type(struct expression *expr)
163 warn(expr->pos, "incompatible types for operation");
164 return NULL;
167 static struct symbol * compatible_integer_binop(struct expression *expr, struct expression **lp, struct expression **rp)
169 struct expression *left = *lp, *right = *rp;
170 struct symbol *ltype = left->ctype, *rtype = right->ctype;
172 if (ltype->type == SYM_NODE)
173 ltype = ltype->ctype.base_type;
174 if (rtype->type == SYM_NODE)
175 rtype = rtype->ctype.base_type;
176 /* Integer promotion? */
177 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
178 ltype = &int_ctype;
179 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
180 rtype = &int_ctype;
181 if (is_int_type(ltype) && is_int_type(rtype)) {
182 struct symbol *ctype = bigger_int_type(ltype, rtype);
184 /* Don't bother promoting same-size entities, it only adds clutter */
185 if (ltype->bit_size != ctype->bit_size)
186 *lp = cast_to(left, ctype);
187 if (rtype->bit_size != ctype->bit_size)
188 *rp = cast_to(right, ctype);
189 return ctype;
191 return NULL;
194 static struct symbol *evaluate_int_binop(struct expression *expr)
196 struct symbol *ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
197 if (ctype) {
198 expr->ctype = ctype;
199 return ctype;
201 return bad_expr_type(expr);
204 static inline int lvalue_expression(struct expression *expr)
206 while (expr->type == EXPR_CAST)
207 expr = expr->cast_expression;
208 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
211 /* Arrays degenerate into pointers on pointer arithmetic */
212 static struct symbol *degenerate(struct expression *expr, struct symbol *ctype, struct expression **ptr_p)
214 struct symbol *base = ctype;
216 if (ctype->type == SYM_NODE)
217 base = ctype->ctype.base_type;
218 if (base->type == SYM_ARRAY || base->type == SYM_FN) {
219 struct symbol *sym = alloc_symbol(expr->pos, SYM_PTR);
220 struct expression *n = alloc_expression(expr->pos, 0);
221 struct expression *ptr;
223 merge_type(sym, ctype);
224 if (base->type == SYM_FN)
225 base = ctype;
226 merge_type(sym, base);
227 sym->bit_size = BITS_IN_POINTER;
228 ctype = sym;
230 ptr = *ptr_p;
231 *n = *ptr->unop;
232 n->ctype = ctype;
233 *ptr_p = n;
236 * This all really assumes that we got the degenerate
237 * array as an lvalue (ie a dereference). If that
238 * is not the case, then holler - because we've screwed
239 * up.
241 if (!lvalue_expression(ptr))
242 warn(ptr->pos, "internal error: strange degenerate array case");
244 return ctype;
247 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
249 struct symbol *ctype;
250 struct symbol *ptr_type = ptr->ctype;
251 struct symbol *i_type = i->ctype;
252 int bit_size;
254 if (i_type->type == SYM_NODE)
255 i_type = i_type->ctype.base_type;
256 if (ptr_type->type == SYM_NODE)
257 ptr_type = ptr_type->ctype.base_type;
259 if (i_type->type == SYM_ENUM)
260 i_type = &int_ctype;
261 if (!is_int_type(i_type))
262 return bad_expr_type(expr);
264 ctype = ptr->ctype;
265 examine_symbol_type(ctype);
267 ctype = degenerate(expr, ctype, &ptr);
268 bit_size = ctype->ctype.base_type->bit_size;
270 /* Special case: adding zero commonly happens as a result of 'array[0]' */
271 if (i->type == EXPR_VALUE && !i->value) {
272 *expr = *ptr;
273 } else if (bit_size > BITS_IN_CHAR) {
274 struct expression *add = expr;
275 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
276 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
278 val->ctype = size_t_ctype;
279 val->value = bit_size >> 3;
281 mul->op = '*';
282 mul->ctype = size_t_ctype;
283 mul->left = i;
284 mul->right = val;
286 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
287 add->left = ptr;
288 add->right = mul;
291 expr->ctype = ctype;
292 return ctype;
295 static struct symbol *evaluate_add(struct expression *expr)
297 struct expression *left = expr->left, *right = expr->right;
298 struct symbol *ltype = left->ctype, *rtype = right->ctype;
300 if (is_ptr_type(ltype))
301 return evaluate_ptr_add(expr, left, right);
303 if (is_ptr_type(rtype))
304 return evaluate_ptr_add(expr, right, left);
306 // FIXME! FP promotion
307 return evaluate_int_binop(expr);
310 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
311 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED)
313 const char * type_difference(struct symbol *target, struct symbol *source,
314 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
316 for (;;) {
317 unsigned long mod1, mod2, diff;
318 unsigned long as1, as2;
319 int type1, type2;
320 struct symbol *base1, *base2;
322 if (target == source)
323 break;
324 if (!target || !source)
325 return "different types";
327 * Peel of per-node information.
328 * FIXME! Check alignment, address space, and context too here!
330 if (target->type == SYM_NODE)
331 target = target->ctype.base_type;
332 if (source->type == SYM_NODE)
333 source = source->ctype.base_type;
335 if (target == source)
336 break;
337 if (!target || !source)
338 return "different types";
340 mod1 = target->ctype.modifiers;
341 as1 = target->ctype.as;
342 mod2 = source->ctype.modifiers;
343 as2 = source->ctype.as;
345 type1 = target->type;
346 base1 = target->ctype.base_type;
348 type2 = source->type;
349 base2 = source->ctype.base_type;
352 * Pointers to arrays degenerate to pointers to the array entry.
353 * Pointers to functions compare as the function itself
355 if (type1 == SYM_PTR && base1) {
356 switch (base1->type) {
357 case SYM_FN:
358 type1 = SYM_FN;
359 target = base1;
360 /* fallthrough */
361 case SYM_ARRAY:
362 base1 = base1->ctype.base_type;
363 default:
364 /* nothing */;
367 if (type2 == SYM_PTR && base2) {
368 switch (base2->type) {
369 case SYM_FN:
370 type2 = SYM_FN;
371 source = base2;
372 /* fallthrough */
373 case SYM_ARRAY:
374 base2 = base2->ctype.base_type;
375 default:
376 /* nothing */;
380 /* Arrays degenerate to pointers for type comparisons */
381 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
382 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
384 if (type1 != type2)
385 return "different base types";
387 /* Must be same address space to be comparable */
388 if (as1 != as2)
389 return "different address spaces";
391 /* Ignore differences in storage types, sign, or addressability */
392 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
393 if (diff) {
394 mod1 &= diff & ~target_mod_ignore;
395 mod2 &= diff & ~source_mod_ignore;
396 if (mod1 | mod2) {
397 if ((mod1 | mod2) & MOD_SIZE)
398 return "different type sizes";
399 return "different modifiers";
403 if (type1 == SYM_FN) {
404 int i;
405 struct symbol *arg1, *arg2;
406 if (base1->variadic != base2->variadic)
407 return "incompatible variadic arguments";
408 PREPARE_PTR_LIST(target->arguments, arg1);
409 PREPARE_PTR_LIST(source->arguments, arg2);
410 i = 1;
411 for (;;) {
412 const char *diff;
413 diff = type_difference(arg1, arg2, 0, 0);
414 if (diff) {
415 static char argdiff[80];
416 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
417 return argdiff;
419 if (!arg1)
420 break;
421 NEXT_PTR_LIST(arg1);
422 NEXT_PTR_LIST(arg2);
423 i++;
425 FINISH_PTR_LIST(arg2);
426 FINISH_PTR_LIST(arg1);
429 target = base1;
430 source = base2;
432 return NULL;
435 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
437 /* NULL expression? Just return the type of the "other side" */
438 if (r->type == EXPR_VALUE && !r->value)
439 return l->ctype;
440 if (l->type == EXPR_VALUE && !l->value)
441 return r->ctype;
442 return NULL;
446 * Ignore differences in "volatile" and "const"ness when
447 * subtracting pointers
449 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
451 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
453 const char *typediff;
454 struct symbol *ctype;
455 struct symbol *ltype = l->ctype, *rtype = r->ctype;
458 * If it is an integer subtract: the ptr add case will do the
459 * right thing.
461 if (!is_ptr_type(rtype))
462 return evaluate_ptr_add(expr, l, r);
464 ctype = ltype;
465 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
466 if (typediff) {
467 ctype = common_ptr_type(l, r);
468 if (!ctype) {
469 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
470 return NULL;
473 examine_symbol_type(ctype);
475 /* Figure out the base type we point to */
476 if (ctype->type == SYM_NODE)
477 ctype = ctype->ctype.base_type;
478 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
479 warn(expr->pos, "subtraction of functions? Share your drugs");
480 return NULL;
482 ctype = ctype->ctype.base_type;
484 expr->ctype = ssize_t_ctype;
485 if (ctype->bit_size > BITS_IN_CHAR) {
486 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
487 struct expression *div = expr;
488 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
490 val->ctype = size_t_ctype;
491 val->value = ctype->bit_size >> 3;
493 sub->op = '-';
494 sub->ctype = ssize_t_ctype;
495 sub->left = l;
496 sub->right = r;
498 div->op = '/';
499 div->left = sub;
500 div->right = val;
503 return ssize_t_ctype;
506 static struct symbol *evaluate_sub(struct expression *expr)
508 struct expression *left = expr->left, *right = expr->right;
509 struct symbol *ltype = left->ctype;
511 if (is_ptr_type(ltype))
512 return evaluate_ptr_sub(expr, left, right);
514 // FIXME! FP promotion
515 return evaluate_int_binop(expr);
518 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
520 static struct symbol *evaluate_conditional(struct expression *expr)
522 struct symbol *ctype;
524 if (!expr)
525 return NULL;
527 if (expr->type == EXPR_ASSIGNMENT)
528 warn(expr->pos, "assignment expression in conditional");
530 ctype = evaluate_expression(expr);
531 if (ctype && is_safe_type(ctype))
532 warn(expr->pos, "testing a 'safe expression'");
534 return ctype;
537 static struct symbol *evaluate_logical(struct expression *expr)
539 if (!evaluate_conditional(expr->left))
540 return NULL;
541 if (!evaluate_conditional(expr->right))
542 return NULL;
544 expr->ctype = &bool_ctype;
545 return &bool_ctype;
548 static struct symbol *evaluate_arithmetic(struct expression *expr)
550 // FIXME! Floating-point promotion!
551 return evaluate_int_binop(expr);
554 static struct symbol *evaluate_binop(struct expression *expr)
556 switch (expr->op) {
557 // addition can take ptr+int, fp and int
558 case '+':
559 return evaluate_add(expr);
561 // subtraction can take ptr-ptr, fp and int
562 case '-':
563 return evaluate_sub(expr);
565 // Arithmetic operations can take fp and int
566 case '*': case '/': case '%':
567 return evaluate_arithmetic(expr);
569 // The rest are integer operations (bitops)
570 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
571 // '&', '^', '|'
572 default:
573 return evaluate_int_binop(expr);
577 static struct symbol *evaluate_comma(struct expression *expr)
579 expr->ctype = expr->right->ctype;
580 return expr->ctype;
583 static struct symbol *evaluate_compare(struct expression *expr)
585 struct expression *left = expr->left, *right = expr->right;
586 struct symbol *ltype = left->ctype, *rtype = right->ctype;
587 struct symbol *ctype;
589 /* Type types? */
590 if (is_type_type(ltype) && is_type_type(rtype)) {
591 expr->ctype = &bool_ctype;
592 return &bool_ctype;
595 if (is_safe_type(ltype) || is_safe_type(rtype))
596 warn(expr->pos, "testing a 'safe expression'");
598 /* Pointer types? */
599 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
600 expr->ctype = &bool_ctype;
601 // FIXME! Check the types for compatibility
602 return &bool_ctype;
605 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
606 if (ctype) {
607 expr->ctype = &bool_ctype;
608 return &bool_ctype;
611 return bad_expr_type(expr);
614 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
616 /* Integer promotion? */
617 if (ltype->type == SYM_NODE)
618 ltype = ltype->ctype.base_type;
619 if (rtype->type == SYM_NODE)
620 rtype = rtype->ctype.base_type;
621 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
622 ltype = &int_ctype;
623 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
624 rtype = &int_ctype;
625 return (is_int_type(ltype) && is_int_type(rtype));
628 static int is_null_ptr(struct expression *expr)
630 return (expr->type == EXPR_VALUE &&
631 expr->value == 0);
635 * FIXME!! This should do casts, array degeneration etc..
637 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
639 struct symbol *ltype = left->ctype, *rtype = right->ctype;
641 if (ltype->type == SYM_NODE)
642 ltype = ltype->ctype.base_type;
644 if (ltype->type == SYM_PTR) {
645 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
646 return ltype;
649 if (rtype->type == SYM_NODE)
650 rtype = rtype->ctype.base_type;
652 if (rtype->type == SYM_PTR) {
653 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
654 return rtype;
656 return NULL;
659 static struct symbol *do_degenerate(struct expression **ep)
661 struct expression *expr = *ep;
662 return degenerate(expr, expr->ctype, ep);
665 static struct symbol * evaluate_conditional_expression(struct expression *expr)
667 struct expression *cond, *true, *false;
668 struct symbol *ctype, *ltype, *rtype;
669 const char * typediff;
671 ctype = do_degenerate(&expr->conditional);
672 cond = expr->conditional;
674 ltype = ctype;
675 true = cond;
676 if (expr->cond_true) {
677 ltype = do_degenerate(&expr->cond_true);
678 true = expr->cond_true;
681 rtype = do_degenerate(&expr->cond_false);
682 false = expr->cond_false;
684 ctype = ltype;
685 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
686 if (typediff) {
687 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
688 if (!ctype) {
689 ctype = compatible_ptr_type(true, expr->cond_false);
690 if (!ctype) {
691 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
692 return NULL;
697 expr->ctype = ctype;
698 return ctype;
701 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
702 struct expression **rp, struct symbol *source, const char *where)
704 const char *typediff;
705 struct symbol *t;
706 int target_as;
708 /* It's ok if the target is more volatile or const than the source */
709 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
710 if (!typediff)
711 return 1;
713 if (compatible_integer_types(target, source)) {
714 if (target->bit_size != source->bit_size)
715 *rp = cast_to(*rp, target);
716 return 1;
719 /* Pointer destination? */
720 t = target;
721 target_as = t->ctype.as;
722 if (t->type == SYM_NODE) {
723 t = t->ctype.base_type;
724 target_as |= t->ctype.as;
726 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
727 struct expression *right = *rp;
728 struct symbol *s = source;
729 int source_as;
731 // NULL pointer is always ok
732 if (right->type == EXPR_VALUE && !right->value)
733 return 1;
735 /* "void *" matches anything as long as the address space is ok */
736 source_as = s->ctype.as;
737 if (s->type == SYM_NODE) {
738 s = s->ctype.base_type;
739 source_as |= s->ctype.as;
741 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
742 s = s->ctype.base_type;
743 t = t->ctype.base_type;
744 if (s == &void_ctype || t == &void_ctype)
745 return 1;
749 // FIXME!! Cast it?
750 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
751 warn(expr->pos, " expected %s", show_typename(target));
752 warn(expr->pos, " got %s", show_typename(source));
753 return 0;
757 * FIXME!! This is wrong from a double evaluation standpoint. We can't
758 * just expand the expression twice, that would make any side effects
759 * happen twice too.
761 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
763 int op = expr->op;
764 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
765 static const int op_trans[] = {
766 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
767 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
768 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
769 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
770 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
771 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
772 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
773 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
774 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
775 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
778 subexpr->left = left;
779 subexpr->right = right;
780 subexpr->op = op_trans[op - SPECIAL_BASE];
781 expr->op = '=';
782 expr->right = subexpr;
783 return evaluate_binop(subexpr);
786 static struct symbol *evaluate_assignment(struct expression *expr)
788 struct expression *left = expr->left, *right = expr->right;
789 struct symbol *ltype, *rtype;
791 ltype = left->ctype;
792 rtype = right->ctype;
793 if (expr->op != '=') {
794 rtype = evaluate_binop_assignment(expr, left, right);
795 if (!rtype)
796 return 0;
797 right = expr->right;
800 if (!lvalue_expression(left)) {
801 warn(expr->pos, "not an lvalue");
802 return NULL;
805 rtype = degenerate(right, rtype, &expr->right);
807 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
808 return 0;
810 if (ltype->type == SYM_NODE)
811 ltype->ctype.modifiers |= MOD_ASSIGNED;
813 expr->ctype = ltype;
814 return ltype;
817 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym)
819 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
821 ptr->ctype.base_type = sym;
822 ptr->bit_size = BITS_IN_POINTER;
824 sym->ctype.modifiers |= MOD_ADDRESSABLE;
825 if (sym->ctype.modifiers & MOD_REGISTER) {
826 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
827 sym->ctype.modifiers &= ~MOD_REGISTER;
829 if (sym->type == SYM_NODE)
830 sym = sym->ctype.base_type;
831 if (sym->type == SYM_ARRAY)
832 sym = sym->ctype.base_type;
833 ptr->ctype.base_type = sym;
835 return ptr;
838 static struct symbol *evaluate_addressof(struct expression *expr)
840 struct expression *op = expr->unop;
841 struct symbol *ctype;
843 if (op->op != '*' || op->type != EXPR_PREOP) {
844 warn(expr->pos, "not addressable");
845 return NULL;
848 ctype = create_pointer(expr, op->ctype);
850 *expr = *op->unop;
852 expr->ctype = ctype;
853 return ctype;
857 static struct symbol *evaluate_dereference(struct expression *expr)
859 struct expression *op = expr->unop;
860 struct symbol *ctype = op->ctype, *sym;
862 sym = alloc_symbol(expr->pos, SYM_NODE);
863 sym->ctype = ctype->ctype;
864 if (ctype->type == SYM_NODE) {
865 ctype = ctype->ctype.base_type;
866 merge_type(sym, ctype);
868 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
869 warn(expr->pos, "cannot derefence this type");
870 return 0;
873 ctype = ctype->ctype.base_type;
874 examine_symbol_type(ctype);
875 if (!ctype) {
876 warn(expr->pos, "undefined type");
877 return NULL;
880 sym->bit_size = ctype->bit_size;
881 sym->array_size = ctype->array_size;
883 /* Simplify: *&(expr) => (expr) */
884 if (op->type == EXPR_PREOP && op->op == '&') {
885 *expr = *op->unop;
888 expr->ctype = sym;
889 return sym;
893 * Unary post-ops: x++ and x--
895 static struct symbol *evaluate_postop(struct expression *expr)
897 struct expression *op = expr->unop;
898 struct symbol *ctype = op->ctype;
900 if (!lvalue_expression(expr->unop)) {
901 warn(expr->pos, "need lvalue expression for ++/--");
902 return NULL;
905 if (ctype->type == SYM_NODE)
906 ctype->ctype.modifiers |= MOD_ASSIGNED;
908 expr->ctype = ctype;
909 return ctype;
912 static struct symbol *evaluate_preop(struct expression *expr)
914 struct symbol *ctype = expr->unop->ctype;
916 switch (expr->op) {
917 case '(':
918 case '+':
919 *expr = *expr->unop;
920 return ctype;
922 case '*':
923 return evaluate_dereference(expr);
925 case '&':
926 return evaluate_addressof(expr);
928 case SPECIAL_INCREMENT:
929 case SPECIAL_DECREMENT:
931 * From a type evaluation standpoint the pre-ops are
932 * the same as the postops
934 return evaluate_postop(expr);
936 case '!':
937 if (is_safe_type(ctype))
938 warn(expr->pos, "testing a 'safe expression'");
939 ctype = &bool_ctype;
940 break;
942 default:
943 break;
945 expr->ctype = ctype;
946 return &bool_ctype;
949 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
951 struct ptr_list *head = (struct ptr_list *)_list;
952 struct ptr_list *list = head;
954 if (!head)
955 return NULL;
956 do {
957 int i;
958 for (i = 0; i < list->nr; i++) {
959 struct symbol *sym = (struct symbol *) list->list[i];
960 if (sym->ident) {
961 if (sym->ident != ident)
962 continue;
963 *offset = sym->offset;
964 return sym;
965 } else {
966 struct symbol *ctype = sym->ctype.base_type;
967 struct symbol *sub;
968 if (!ctype)
969 continue;
970 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
971 continue;
972 sub = find_identifier(ident, ctype->symbol_list, offset);
973 if (!sub)
974 continue;
975 *offset += sym->offset;
976 return sub;
979 } while ((list = list->next) != head);
980 return NULL;
983 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset, struct symbol *ctype)
985 struct expression *add;
987 add = expr;
988 if (offset) {
989 /* Create a new add-expression */
990 add = alloc_expression(expr->pos, EXPR_BINOP);
991 add->op = '+';
992 add->left = expr;
993 add->right = alloc_expression(expr->pos, EXPR_VALUE);
994 add->right->ctype = &int_ctype;
995 add->right->value = offset;
998 add->ctype = ctype;
999 return add;
1002 /* structure/union dereference */
1003 static struct symbol *evaluate_member_dereference(struct expression *expr)
1005 int offset;
1006 struct symbol *ctype, *member;
1007 struct expression *deref = expr->deref, *add;
1008 struct ident *ident = expr->member;
1009 unsigned int mod;
1010 int address_space;
1012 if (!evaluate_expression(deref))
1013 return NULL;
1014 if (!ident) {
1015 warn(expr->pos, "bad member name");
1016 return NULL;
1019 ctype = deref->ctype;
1020 address_space = ctype->ctype.as;
1021 mod = ctype->ctype.modifiers;
1022 if (ctype->type == SYM_NODE) {
1023 ctype = ctype->ctype.base_type;
1024 address_space |= ctype->ctype.as;
1025 mod |= ctype->ctype.modifiers;
1027 if (expr->op == SPECIAL_DEREFERENCE) {
1028 /* Arrays will degenerate into pointers for '->' */
1029 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
1030 warn(expr->pos, "expected a pointer to a struct/union");
1031 return NULL;
1033 mod = ctype->ctype.modifiers;
1034 address_space = ctype->ctype.as;
1035 ctype = ctype->ctype.base_type;
1036 if (ctype->type == SYM_NODE) {
1037 mod |= ctype->ctype.modifiers;
1038 address_space |= ctype->ctype.as;
1039 ctype = ctype->ctype.base_type;
1041 } else {
1042 if (!lvalue_expression(deref)) {
1043 warn(deref->pos, "expected lvalue for member dereference");
1044 return NULL;
1046 deref = deref->unop;
1047 expr->deref = deref;
1049 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1050 warn(expr->pos, "expected structure or union");
1051 return NULL;
1053 offset = 0;
1054 member = find_identifier(ident, ctype->symbol_list, &offset);
1055 if (!member) {
1056 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1057 const char *name = "<unnamed>";
1058 int namelen = 9;
1059 if (ctype->ident) {
1060 name = ctype->ident->name;
1061 namelen = ctype->ident->len;
1063 warn(expr->pos, "no member '%s' in %s %.*s",
1064 show_ident(ident), type, namelen, name);
1065 return NULL;
1068 ctype = create_pointer(deref, member);
1069 ctype->ctype.modifiers = mod;
1070 ctype->ctype.as = address_space;
1071 add = evaluate_offset(deref, offset, ctype);
1073 ctype = member->ctype.base_type;
1074 if (ctype->type == SYM_BITFIELD) {
1075 ctype = ctype->ctype.base_type;
1076 expr->type = EXPR_BITFIELD;
1077 expr->bitpos = member->bit_offset;
1078 expr->nrbits = member->fieldwidth;
1079 expr->address = add;
1080 } else {
1081 expr->type = EXPR_PREOP;
1082 expr->op = '*';
1083 expr->unop = add;
1086 expr->ctype = member;
1087 return member;
1090 static struct symbol *evaluate_sizeof(struct expression *expr)
1092 int size;
1094 if (expr->cast_type) {
1095 examine_symbol_type(expr->cast_type);
1096 size = expr->cast_type->bit_size;
1097 } else {
1098 if (!evaluate_expression(expr->cast_expression))
1099 return 0;
1100 size = expr->cast_expression->ctype->bit_size;
1102 if (size & 7) {
1103 warn(expr->pos, "cannot size expression");
1104 return 0;
1106 expr->type = EXPR_VALUE;
1107 expr->value = size >> 3;
1108 expr->ctype = size_t_ctype;
1109 return size_t_ctype;
1112 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1114 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1115 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1116 return clash != 0;
1119 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1121 struct expression *expr;
1122 struct symbol_list *argument_types = fn->arguments;
1123 struct symbol *argtype;
1124 int i = 1;
1126 PREPARE_PTR_LIST(argument_types, argtype);
1127 FOR_EACH_PTR (head, expr) {
1128 struct expression **p = THIS_ADDRESS(expr);
1129 struct symbol *ctype, *target;
1130 ctype = evaluate_expression(expr);
1132 if (!ctype)
1133 return 0;
1135 if (context_clash(f, ctype))
1136 warn(expr->pos, "argument %d used in wrong context", i);
1138 ctype = degenerate(expr, ctype, p);
1140 target = argtype;
1141 if (!target && ctype->bit_size < BITS_IN_INT)
1142 target = &int_ctype;
1143 if (target) {
1144 static char where[30];
1145 examine_symbol_type(target);
1146 sprintf(where, "argument %d", i);
1147 compatible_assignment_types(expr, target, p, ctype, where);
1150 i++;
1151 NEXT_PTR_LIST(argtype);
1152 } END_FOR_EACH_PTR;
1153 FINISH_PTR_LIST(argtype);
1154 return 1;
1157 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1158 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1160 struct expression *entry;
1161 int current = 0;
1162 int max = 0;
1164 FOR_EACH_PTR(expr->expr_list, entry) {
1165 struct expression **p = THIS_ADDRESS(entry);
1167 if (entry->type == EXPR_INDEX) {
1168 current = entry->idx_to;
1169 continue;
1171 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1172 current++;
1173 if (current > max)
1174 max = current;
1175 } END_FOR_EACH_PTR;
1176 return max;
1179 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1181 struct expression *entry;
1182 struct symbol *sym;
1184 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1185 FOR_EACH_PTR(expr->expr_list, entry) {
1186 struct expression **p = THIS_ADDRESS(entry);
1188 if (entry->type == EXPR_IDENTIFIER) {
1189 struct ident *ident = entry->expr_ident;
1190 /* We special-case the "already right place" case */
1191 if (sym && sym->ident == ident)
1192 continue;
1193 RESET_PTR_LIST(sym);
1194 for (;;) {
1195 if (!sym) {
1196 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1197 return 0;
1199 if (sym->ident == ident)
1200 break;
1201 NEXT_PTR_LIST(sym);
1203 continue;
1206 if (!sym) {
1207 warn(expr->pos, "too many initializers for struct/union");
1208 return 0;
1211 evaluate_initializer(sym, p, offset + sym->offset);
1213 NEXT_PTR_LIST(sym);
1214 } END_FOR_EACH_PTR;
1215 FINISH_PTR_LIST(sym);
1217 return 0;
1221 * Initializers are kind of like assignments. Except
1222 * they can be a hell of a lot more complex.
1224 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1226 struct expression *expr = *ep;
1229 * Simple non-structure/array initializers are the simple
1230 * case, and look (and parse) largely like assignments.
1232 if (expr->type != EXPR_INITIALIZER) {
1233 int size = 0;
1234 struct symbol *rtype = evaluate_expression(expr);
1235 if (rtype) {
1236 struct expression *pos;
1238 // FIXME! char array[] = "string" special case
1239 // should _not_ degenerate.
1240 rtype = degenerate(expr, rtype, ep);
1241 expr = *ep;
1242 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1243 /* strings are special: char arrays */
1244 if (rtype->type == SYM_ARRAY)
1245 size = get_expression_value(rtype->array_size);
1247 * Don't bother creating a position expression for
1248 * the simple initializer cases that don't need it.
1250 * We need a position if the initializer has a byte
1251 * offset, _or_ if we're initializing a bitfield.
1253 if (offset || ctype->fieldwidth) {
1254 pos = alloc_expression(expr->pos, EXPR_POS);
1255 pos->init_offset = offset;
1256 pos->init_sym = ctype;
1257 pos->init_expr = *ep;
1258 pos->ctype = expr->ctype;
1259 *ep = pos;
1262 return size;
1265 expr->ctype = ctype;
1266 if (ctype->type == SYM_NODE)
1267 ctype = ctype->ctype.base_type;
1269 switch (ctype->type) {
1270 case SYM_ARRAY:
1271 case SYM_PTR:
1272 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1273 case SYM_UNION:
1274 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1275 case SYM_STRUCT:
1276 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1277 default:
1278 break;
1280 warn(expr->pos, "unexpected compound initializer");
1281 return 0;
1284 static struct symbol *evaluate_cast(struct expression *expr)
1286 struct expression *target = expr->cast_expression;
1287 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1289 expr->ctype = ctype;
1290 expr->cast_type = ctype;
1293 * Special case: a cast can be followed by an
1294 * initializer, in which case we need to pass
1295 * the type value down to that initializer rather
1296 * than trying to evaluate it as an expression
1298 * A more complex case is when the initializer is
1299 * dereferenced as part of a post-fix expression.
1300 * We need to produce an expression that can be dereferenced.
1302 if (target->type == EXPR_INITIALIZER) {
1303 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
1304 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1306 sym->ctype.base_type = ctype;
1307 sym->initializer = expr->cast_expression;
1308 evaluate_symbol(sym);
1310 addr->ctype = &ptr_ctype;
1311 addr->symbol = sym;
1313 expr->type = EXPR_PREOP;
1314 expr->op = '*';
1315 expr->unop = addr;
1316 expr->ctype = ctype;
1317 return ctype;
1320 evaluate_expression(target);
1323 * Casts of constant values are special: they
1324 * can be NULL, and thus need to be simplified
1325 * early.
1327 if (target->type == EXPR_VALUE)
1328 cast_value(expr, ctype, target, target->ctype);
1330 return ctype;
1334 * Evaluate a call expression with a symbol. This
1335 * should expand inline functions, and evaluate
1336 * builtins.
1338 static int evaluate_symbol_call(struct expression *expr)
1340 struct expression *fn = expr->fn;
1341 struct symbol *ctype = fn->ctype;
1343 if (fn->type != EXPR_PREOP)
1344 return 0;
1346 if (ctype->op && ctype->op->evaluate)
1347 return ctype->op->evaluate(expr);
1349 if (ctype->ctype.modifiers & MOD_INLINE) {
1350 int ret;
1351 struct symbol *curr = current_fn;
1352 unsigned long context = current_context;
1353 unsigned long mask = current_contextmask;
1355 current_context |= ctype->ctype.context;
1356 current_contextmask |= ctype->ctype.contextmask;
1357 current_fn = ctype->ctype.base_type;
1358 ret = inline_function(expr, ctype);
1360 /* restore the old function context */
1361 current_fn = curr;
1362 current_context = context;
1363 current_contextmask = mask;
1364 return ret;
1367 return 0;
1370 static struct symbol *evaluate_call(struct expression *expr)
1372 int args, fnargs;
1373 struct symbol *ctype, *sym;
1374 struct expression *fn = expr->fn;
1375 struct expression_list *arglist = expr->args;
1377 if (!evaluate_expression(fn))
1378 return NULL;
1379 sym = ctype = fn->ctype;
1380 if (ctype->type == SYM_NODE)
1381 ctype = ctype->ctype.base_type;
1382 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1383 ctype = ctype->ctype.base_type;
1384 if (!evaluate_arguments(sym, ctype, arglist))
1385 return NULL;
1386 if (sym->type == SYM_NODE) {
1387 if (evaluate_symbol_call(expr))
1388 return expr->ctype;
1390 if (sym->type == SYM_NODE) {
1391 if (evaluate_symbol_call(expr))
1392 return expr->ctype;
1394 if (ctype->type != SYM_FN) {
1395 warn(expr->pos, "not a function %.*s",
1396 sym->ident->len, sym->ident->name);
1397 return NULL;
1399 args = expression_list_size(expr->args);
1400 fnargs = symbol_list_size(ctype->arguments);
1401 if (args < fnargs)
1402 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1403 if (args > fnargs && !ctype->variadic)
1404 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1405 expr->ctype = ctype->ctype.base_type;
1406 return expr->ctype;
1409 struct symbol *evaluate_expression(struct expression *expr)
1411 if (!expr)
1412 return NULL;
1413 if (expr->ctype)
1414 return expr->ctype;
1416 switch (expr->type) {
1417 case EXPR_VALUE:
1418 warn(expr->pos, "value expression without a type");
1419 return NULL;
1420 case EXPR_STRING:
1421 return evaluate_string(expr);
1422 case EXPR_SYMBOL:
1423 return evaluate_symbol_expression(expr);
1424 case EXPR_BINOP:
1425 if (!evaluate_expression(expr->left))
1426 return NULL;
1427 if (!evaluate_expression(expr->right))
1428 return NULL;
1429 return evaluate_binop(expr);
1430 case EXPR_LOGICAL:
1431 return evaluate_logical(expr);
1432 case EXPR_COMMA:
1433 if (!evaluate_expression(expr->left))
1434 return NULL;
1435 if (!evaluate_expression(expr->right))
1436 return NULL;
1437 return evaluate_comma(expr);
1438 case EXPR_COMPARE:
1439 if (!evaluate_expression(expr->left))
1440 return NULL;
1441 if (!evaluate_expression(expr->right))
1442 return NULL;
1443 return evaluate_compare(expr);
1444 case EXPR_ASSIGNMENT:
1445 if (!evaluate_expression(expr->left))
1446 return NULL;
1447 if (!evaluate_expression(expr->right))
1448 return NULL;
1449 return evaluate_assignment(expr);
1450 case EXPR_PREOP:
1451 if (!evaluate_expression(expr->unop))
1452 return NULL;
1453 return evaluate_preop(expr);
1454 case EXPR_POSTOP:
1455 if (!evaluate_expression(expr->unop))
1456 return NULL;
1457 return evaluate_postop(expr);
1458 case EXPR_CAST:
1459 return evaluate_cast(expr);
1460 case EXPR_SIZEOF:
1461 return evaluate_sizeof(expr);
1462 case EXPR_DEREF:
1463 return evaluate_member_dereference(expr);
1464 case EXPR_CALL:
1465 return evaluate_call(expr);
1466 case EXPR_BITFIELD:
1467 warn(expr->pos, "bitfield generated by parser");
1468 return NULL;
1469 case EXPR_CONDITIONAL:
1470 if (!evaluate_conditional(expr->conditional))
1471 return NULL;
1472 if (!evaluate_expression(expr->cond_false))
1473 return NULL;
1474 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1475 return NULL;
1476 return evaluate_conditional_expression(expr);
1477 case EXPR_STATEMENT:
1478 expr->ctype = evaluate_statement(expr->statement);
1479 return expr->ctype;
1481 case EXPR_LABEL:
1482 expr->ctype = &ptr_ctype;
1483 return &ptr_ctype;
1485 case EXPR_TYPE:
1486 /* Evaluate the type of the symbol .. */
1487 evaluate_symbol(expr->symbol);
1488 /* .. but the type of the _expression_ is a "type" */
1489 expr->ctype = &type_ctype;
1490 return &type_ctype;
1492 /* These can not exist as stand-alone expressions */
1493 case EXPR_INITIALIZER:
1494 case EXPR_IDENTIFIER:
1495 case EXPR_INDEX:
1496 case EXPR_POS:
1497 warn(expr->pos, "internal front-end error: initializer in expression");
1498 return NULL;
1500 return NULL;
1503 void check_duplicates(struct symbol *sym)
1505 struct symbol *next = sym;
1507 while ((next = next->same_symbol) != NULL) {
1508 const char *typediff;
1509 evaluate_symbol(next);
1510 typediff = type_difference(sym, next, 0, 0);
1511 if (typediff) {
1512 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1513 show_ident(sym->ident),
1514 input_streams[next->pos.stream].name, next->pos.line, typediff);
1515 return;
1520 struct symbol *evaluate_symbol(struct symbol *sym)
1522 struct symbol *base_type;
1524 if (!sym)
1525 return sym;
1527 sym = examine_symbol_type(sym);
1528 base_type = sym->ctype.base_type;
1529 if (!base_type)
1530 return NULL;
1532 /* Evaluate the initializers */
1533 if (sym->initializer) {
1534 int count = evaluate_initializer(sym, &sym->initializer, 0);
1535 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
1536 int bit_size = count * base_type->ctype.base_type->bit_size;
1537 base_type->array_size = alloc_const_expression(sym->pos, count);
1538 base_type->bit_size = bit_size;
1539 sym->array_size = base_type->array_size;
1540 sym->bit_size = bit_size;
1544 /* And finally, evaluate the body of the symbol too */
1545 if (base_type->type == SYM_FN) {
1546 struct symbol *s;
1548 FOR_EACH_PTR(base_type->arguments, s) {
1549 evaluate_symbol(s);
1550 } END_FOR_EACH_PTR;
1552 if (base_type->stmt) {
1553 current_fn = base_type;
1554 current_contextmask = sym->ctype.contextmask;
1555 current_context = sym->ctype.context;
1556 evaluate_statement(base_type->stmt);
1560 return base_type;
1563 struct symbol *evaluate_return_expression(struct statement *stmt)
1565 struct expression *expr = stmt->expression;
1566 struct symbol *ctype, *fntype;
1568 fntype = current_fn->ctype.base_type;
1569 if (!fntype || fntype == &void_ctype) {
1570 if (expr)
1571 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
1572 return NULL;
1575 if (!expr) {
1576 warn(stmt->pos, "return with no return value");
1577 return NULL;
1579 ctype = evaluate_expression(expr);
1580 if (!ctype)
1581 return NULL;
1582 ctype = degenerate(expr, ctype, &expr);
1583 expr->ctype = ctype;
1584 compatible_assignment_types(expr, fntype, &expr, ctype, "return expression");
1585 stmt->expression = expr;
1586 return NULL;
1589 static void evaluate_if_statement(struct statement *stmt)
1591 struct expression *expr = stmt->if_conditional;
1592 struct symbol *ctype;
1594 if (!expr)
1595 return;
1597 ctype = evaluate_conditional(expr);
1598 if (!ctype)
1599 return;
1601 evaluate_statement(stmt->if_true);
1602 evaluate_statement(stmt->if_false);
1605 struct symbol *evaluate_statement(struct statement *stmt)
1607 if (!stmt)
1608 return NULL;
1610 switch (stmt->type) {
1611 case STMT_RETURN:
1612 return evaluate_return_expression(stmt);
1614 case STMT_EXPRESSION:
1615 return evaluate_expression(stmt->expression);
1617 case STMT_COMPOUND: {
1618 struct statement *s;
1619 struct symbol *type = NULL;
1620 struct symbol *sym;
1622 /* Evaluate each symbol in the compound statement */
1623 FOR_EACH_PTR(stmt->syms, sym) {
1624 evaluate_symbol(sym);
1625 } END_FOR_EACH_PTR;
1626 evaluate_symbol(stmt->ret);
1629 * Then, evaluate each statement, making the type of the
1630 * compound statement be the type of the last statement
1632 type = NULL;
1633 FOR_EACH_PTR(stmt->stmts, s) {
1634 type = evaluate_statement(s);
1635 } END_FOR_EACH_PTR;
1636 return type;
1638 case STMT_IF:
1639 evaluate_if_statement(stmt);
1640 return NULL;
1641 case STMT_ITERATOR:
1642 evaluate_conditional(stmt->iterator_pre_condition);
1643 evaluate_conditional(stmt->iterator_post_condition);
1644 evaluate_statement(stmt->iterator_pre_statement);
1645 evaluate_statement(stmt->iterator_statement);
1646 evaluate_statement(stmt->iterator_post_statement);
1647 return NULL;
1648 case STMT_SWITCH:
1649 evaluate_expression(stmt->switch_expression);
1650 evaluate_statement(stmt->switch_statement);
1651 return NULL;
1652 case STMT_CASE:
1653 evaluate_expression(stmt->case_expression);
1654 evaluate_expression(stmt->case_to);
1655 evaluate_statement(stmt->case_statement);
1656 return NULL;
1657 case STMT_LABEL:
1658 evaluate_statement(stmt->label_statement);
1659 return NULL;
1660 case STMT_GOTO:
1661 evaluate_expression(stmt->goto_expression);
1662 return NULL;
1663 case STMT_NONE:
1664 break;
1665 case STMT_ASM:
1666 /* FIXME! Do the asm parameter evaluation! */
1667 break;
1669 return NULL;