Split "STMT_GOTO_BB" into "STMT_CONDTRUE" and "STMT_CONDFALSE".
[smatch.git] / evaluate.c
blob353fe94b994717e317864851e8d183168f251ac3
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_ptr_type(struct symbol *type)
144 if (type->type == SYM_NODE)
145 type = type->ctype.base_type;
146 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
149 static int is_int_type(struct symbol *type)
151 if (type->type == SYM_NODE)
152 type = type->ctype.base_type;
153 return type->ctype.base_type == &int_type;
156 static struct symbol *bad_expr_type(struct expression *expr)
158 warn(expr->pos, "incompatible types for operation");
159 return NULL;
162 static struct symbol * compatible_integer_binop(struct expression *expr, struct expression **lp, struct expression **rp)
164 struct expression *left = *lp, *right = *rp;
165 struct symbol *ltype = left->ctype, *rtype = right->ctype;
167 if (ltype->type == SYM_NODE)
168 ltype = ltype->ctype.base_type;
169 if (rtype->type == SYM_NODE)
170 rtype = rtype->ctype.base_type;
171 /* Integer promotion? */
172 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
173 ltype = &int_ctype;
174 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
175 rtype = &int_ctype;
176 if (is_int_type(ltype) && is_int_type(rtype)) {
177 struct symbol *ctype = bigger_int_type(ltype, rtype);
179 /* Don't bother promoting same-size entities, it only adds clutter */
180 if (ltype->bit_size != ctype->bit_size)
181 *lp = cast_to(left, ctype);
182 if (rtype->bit_size != ctype->bit_size)
183 *rp = cast_to(right, ctype);
184 return ctype;
186 return NULL;
189 static struct symbol *evaluate_int_binop(struct expression *expr)
191 struct symbol *ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
192 if (ctype) {
193 expr->ctype = ctype;
194 return ctype;
196 return bad_expr_type(expr);
199 static inline int lvalue_expression(struct expression *expr)
201 while (expr->type == EXPR_CAST)
202 expr = expr->cast_expression;
203 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
206 /* Arrays degenerate into pointers on pointer arithmetic */
207 static struct symbol *degenerate(struct expression *expr, struct symbol *ctype, struct expression **ptr_p)
209 struct symbol *base = ctype;
211 if (ctype->type == SYM_NODE)
212 base = ctype->ctype.base_type;
213 if (base->type == SYM_ARRAY || base->type == SYM_FN) {
214 struct symbol *sym = alloc_symbol(expr->pos, SYM_PTR);
215 struct expression *n = alloc_expression(expr->pos, 0);
216 struct expression *ptr;
218 merge_type(sym, ctype);
219 if (base->type == SYM_FN)
220 base = ctype;
221 merge_type(sym, base);
222 sym->bit_size = BITS_IN_POINTER;
223 ctype = sym;
225 ptr = *ptr_p;
226 *n = *ptr->unop;
227 n->ctype = ctype;
228 *ptr_p = n;
231 * This all really assumes that we got the degenerate
232 * array as an lvalue (ie a dereference). If that
233 * is not the case, then holler - because we've screwed
234 * up.
236 if (!lvalue_expression(ptr))
237 warn(ptr->pos, "internal error: strange degenerate array case");
239 return ctype;
242 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
244 struct symbol *ctype;
245 struct symbol *ptr_type = ptr->ctype;
246 struct symbol *i_type = i->ctype;
247 int bit_size;
249 if (i_type->type == SYM_NODE)
250 i_type = i_type->ctype.base_type;
251 if (ptr_type->type == SYM_NODE)
252 ptr_type = ptr_type->ctype.base_type;
254 if (i_type->type == SYM_ENUM)
255 i_type = &int_ctype;
256 if (!is_int_type(i_type))
257 return bad_expr_type(expr);
259 ctype = ptr->ctype;
260 examine_symbol_type(ctype);
262 ctype = degenerate(expr, ctype, &ptr);
263 bit_size = ctype->bit_size;
265 /* Special case: adding zero commonly happens as a result of 'array[0]' */
266 if (i->type == EXPR_VALUE && !i->value) {
267 *expr = *ptr;
268 } else if (bit_size > BITS_IN_CHAR) {
269 struct expression *add = expr;
270 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
271 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
273 val->ctype = size_t_ctype;
274 val->value = bit_size >> 3;
276 mul->op = '*';
277 mul->ctype = size_t_ctype;
278 mul->left = i;
279 mul->right = val;
281 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
282 add->left = ptr;
283 add->right = mul;
286 expr->ctype = ctype;
287 return ctype;
290 static struct symbol *evaluate_add(struct expression *expr)
292 struct expression *left = expr->left, *right = expr->right;
293 struct symbol *ltype = left->ctype, *rtype = right->ctype;
295 if (is_ptr_type(ltype))
296 return evaluate_ptr_add(expr, left, right);
298 if (is_ptr_type(rtype))
299 return evaluate_ptr_add(expr, right, left);
301 // FIXME! FP promotion
302 return evaluate_int_binop(expr);
305 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
306 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED)
308 static const char * type_difference(struct symbol *target, struct symbol *source,
309 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
311 for (;;) {
312 unsigned long mod1, mod2, diff;
313 unsigned long as1, as2;
315 if (target == source)
316 break;
317 if (!target || !source)
318 return "different types";
320 * Peel of per-node information.
321 * FIXME! Check alignment, address space, and context too here!
323 if (target->type == SYM_NODE)
324 target = target->ctype.base_type;
325 if (source->type == SYM_NODE)
326 source = source->ctype.base_type;
328 if (target == source)
329 break;
330 if (!target || !source)
331 return "different types";
333 mod1 = target->ctype.modifiers;
334 as1 = target->ctype.as;
335 mod2 = source->ctype.modifiers;
336 as2 = source->ctype.as;
338 if (target->type != source->type) {
339 int type1 = target->type;
340 int type2 = source->type;
342 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
343 type1 = type1 == SYM_ARRAY ? SYM_PTR : type1;
344 type2 = type2 == SYM_ARRAY ? SYM_PTR : type2;
346 if ((type1 == SYM_PTR) && (target->ctype.base_type->type == SYM_FN)) {
347 target = target->ctype.base_type;
348 type1 = SYM_FN;
351 if ((type2 == SYM_PTR) && (source->ctype.base_type->type == SYM_FN)) {
352 source = source->ctype.base_type;
353 type2 = SYM_FN;
356 if (type1 != type2)
357 return "different base types";
360 /* Must be same address space to be comparable */
361 if (as1 != as2)
362 return "different address spaces";
364 /* Ignore differences in storage types, sign, or addressability */
365 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
366 if (diff) {
367 mod1 &= diff & ~target_mod_ignore;
368 mod2 &= diff & ~source_mod_ignore;
369 if (mod1 | mod2) {
370 if ((mod1 | mod2) & MOD_SIZE)
371 return "different type sizes";
372 return "different modifiers";
376 if (target->type == SYM_FN) {
377 int i;
378 struct symbol *arg1, *arg2;
379 if (target->variadic != source->variadic)
380 return "incompatible variadic arguments";
381 PREPARE_PTR_LIST(target->arguments, arg1);
382 PREPARE_PTR_LIST(source->arguments, arg2);
383 i = 1;
384 for (;;) {
385 const char *diff;
386 diff = type_difference(arg1, arg2, 0, 0);
387 if (diff) {
388 static char argdiff[80];
389 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
390 return argdiff;
392 if (!arg1)
393 break;
394 NEXT_PTR_LIST(arg1);
395 NEXT_PTR_LIST(arg2);
396 i++;
398 FINISH_PTR_LIST(arg2);
399 FINISH_PTR_LIST(arg1);
402 target = target->ctype.base_type;
403 source = source->ctype.base_type;
405 return NULL;
408 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
410 /* NULL expression? Just return the type of the "other side" */
411 if (r->type == EXPR_VALUE && !r->value)
412 return l->ctype;
413 if (l->type == EXPR_VALUE && !l->value)
414 return r->ctype;
415 return NULL;
419 * Ignore differences in "volatile" and "const"ness when
420 * subtracting pointers
422 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
424 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
426 const char *typediff;
427 struct symbol *ctype;
428 struct symbol *ltype = l->ctype, *rtype = r->ctype;
431 * If it is an integer subtract: the ptr add case will do the
432 * right thing.
434 if (!is_ptr_type(rtype))
435 return evaluate_ptr_add(expr, l, r);
437 ctype = ltype;
438 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
439 if (typediff) {
440 ctype = common_ptr_type(l, r);
441 if (!ctype) {
442 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
443 return NULL;
446 examine_symbol_type(ctype);
448 /* Figure out the base type we point to */
449 if (ctype->type == SYM_NODE)
450 ctype = ctype->ctype.base_type;
451 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
452 warn(expr->pos, "subtraction of functions? Share your drugs");
453 return NULL;
455 ctype = ctype->ctype.base_type;
457 expr->ctype = ssize_t_ctype;
458 if (ctype->bit_size > BITS_IN_CHAR) {
459 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
460 struct expression *div = expr;
461 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
463 val->ctype = size_t_ctype;
464 val->value = ctype->bit_size >> 3;
466 sub->op = '-';
467 sub->ctype = ssize_t_ctype;
468 sub->left = l;
469 sub->right = r;
471 div->op = '/';
472 div->left = sub;
473 div->right = val;
476 return ssize_t_ctype;
479 static struct symbol *evaluate_sub(struct expression *expr)
481 struct expression *left = expr->left, *right = expr->right;
482 struct symbol *ltype = left->ctype;
484 if (is_ptr_type(ltype))
485 return evaluate_ptr_sub(expr, left, right);
487 // FIXME! FP promotion
488 return evaluate_int_binop(expr);
491 static struct symbol *evaluate_logical(struct expression *expr)
493 if (!evaluate_expression(expr->left))
494 return NULL;
495 if (!evaluate_expression(expr->right))
496 return NULL;
497 expr->ctype = &bool_ctype;
498 return &bool_ctype;
501 static struct symbol *evaluate_arithmetic(struct expression *expr)
503 // FIXME! Floating-point promotion!
504 return evaluate_int_binop(expr);
507 static struct symbol *evaluate_binop(struct expression *expr)
509 switch (expr->op) {
510 // addition can take ptr+int, fp and int
511 case '+':
512 return evaluate_add(expr);
514 // subtraction can take ptr-ptr, fp and int
515 case '-':
516 return evaluate_sub(expr);
518 // Arithmetic operations can take fp and int
519 case '*': case '/': case '%':
520 return evaluate_arithmetic(expr);
522 // The rest are integer operations (bitops)
523 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
524 // '&', '^', '|'
525 default:
526 return evaluate_int_binop(expr);
530 static struct symbol *evaluate_comma(struct expression *expr)
532 expr->ctype = expr->right->ctype;
533 return expr->ctype;
536 static struct symbol *evaluate_compare(struct expression *expr)
538 struct expression *left = expr->left, *right = expr->right;
539 struct symbol *ltype = left->ctype, *rtype = right->ctype;
540 struct symbol *ctype;
542 /* Pointer types? */
543 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
544 expr->ctype = &bool_ctype;
545 // FIXME! Check the types for compatibility
546 return &bool_ctype;
549 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
550 if (ctype) {
551 expr->ctype = &bool_ctype;
552 return &bool_ctype;
555 return bad_expr_type(expr);
558 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
560 /* Integer promotion? */
561 if (ltype->type == SYM_NODE)
562 ltype = ltype->ctype.base_type;
563 if (rtype->type == SYM_NODE)
564 rtype = rtype->ctype.base_type;
565 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
566 ltype = &int_ctype;
567 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
568 rtype = &int_ctype;
569 return (is_int_type(ltype) && is_int_type(rtype));
572 static int is_null_ptr(struct expression *expr)
574 return (expr->type == EXPR_VALUE &&
575 expr->value == 0);
579 * FIXME!! This should do casts, array degeneration etc..
581 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
583 struct symbol *ltype = left->ctype, *rtype = right->ctype;
585 if (ltype->type == SYM_NODE)
586 ltype = ltype->ctype.base_type;
588 if (ltype->type == SYM_PTR) {
589 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
590 return ltype;
593 if (rtype->type == SYM_NODE)
594 rtype = rtype->ctype.base_type;
596 if (rtype->type == SYM_PTR) {
597 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
598 return rtype;
600 return NULL;
603 static struct symbol *do_degenerate(struct expression **ep)
605 struct expression *expr = *ep;
606 return degenerate(expr, expr->ctype, ep);
609 static struct symbol * evaluate_conditional(struct expression *expr)
611 struct expression *cond, *true, *false;
612 struct symbol *ctype, *ltype, *rtype;
613 const char * typediff;
615 ctype = do_degenerate(&expr->conditional);
616 cond = expr->conditional;
618 ltype = ctype;
619 true = cond;
620 if (expr->cond_true) {
621 ltype = do_degenerate(&expr->cond_true);
622 true = expr->cond_true;
625 rtype = do_degenerate(&expr->cond_false);
626 false = expr->cond_false;
628 ctype = ltype;
629 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
630 if (typediff) {
631 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
632 if (!ctype) {
633 ctype = compatible_ptr_type(true, expr->cond_false);
634 if (!ctype) {
635 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
636 return NULL;
641 expr->ctype = ctype;
642 return ctype;
645 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
646 struct expression **rp, struct symbol *source, const char *where)
648 const char *typediff;
649 struct symbol *t;
650 int target_as;
652 /* It's ok if the target is more volatile or const than the source */
653 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
654 if (!typediff)
655 return 1;
657 if (compatible_integer_types(target, source)) {
658 if (target->bit_size != source->bit_size)
659 *rp = cast_to(*rp, target);
660 return 1;
663 /* Pointer destination? */
664 t = target;
665 target_as = t->ctype.as;
666 if (t->type == SYM_NODE) {
667 t = t->ctype.base_type;
668 target_as |= t->ctype.as;
670 if (t->type == SYM_PTR || t->type == SYM_FN) {
671 struct expression *right = *rp;
672 struct symbol *s = source;
673 int source_as;
675 // NULL pointer is always ok
676 if (right->type == EXPR_VALUE && !right->value)
677 return 1;
679 /* "void *" matches anything as long as the address space is ok */
680 source_as = s->ctype.as;
681 if (s->type == SYM_NODE) {
682 s = s->ctype.base_type;
683 source_as |= s->ctype.as;
685 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
686 s = s->ctype.base_type;
687 t = t->ctype.base_type;
688 if (s == &void_ctype || t == &void_ctype)
689 return 1;
693 // FIXME!! Cast it?
694 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
695 warn(expr->pos, " expected %s", show_typename(target));
696 warn(expr->pos, " got %s", show_typename(source));
697 return 0;
701 * FIXME!! This is wrong from a double evaluation standpoint. We can't
702 * just expand the expression twice, that would make any side effects
703 * happen twice too.
705 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
707 int op = expr->op;
708 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
709 static const int op_trans[] = {
710 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
711 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
712 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
713 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
714 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
715 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
716 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
717 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
718 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
719 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
722 subexpr->left = left;
723 subexpr->right = right;
724 subexpr->op = op_trans[op - SPECIAL_BASE];
725 expr->op = '=';
726 expr->right = subexpr;
727 return evaluate_binop(subexpr);
730 static struct symbol *evaluate_assignment(struct expression *expr)
732 struct expression *left = expr->left, *right = expr->right;
733 struct symbol *ltype, *rtype;
735 ltype = left->ctype;
736 rtype = right->ctype;
737 if (expr->op != '=') {
738 rtype = evaluate_binop_assignment(expr, left, right);
739 if (!rtype)
740 return 0;
741 right = expr->right;
744 if (!lvalue_expression(left)) {
745 warn(expr->pos, "not an lvalue");
746 return NULL;
749 rtype = degenerate(right, rtype, &expr->right);
751 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
752 return 0;
754 if (ltype->type == SYM_NODE)
755 ltype->ctype.modifiers |= MOD_ASSIGNED;
757 expr->ctype = ltype;
758 return ltype;
761 static struct symbol *evaluate_addressof(struct expression *expr)
763 struct symbol *ctype, *symbol;
764 struct expression *op = expr->unop;
766 if (op->op != '*' || op->type != EXPR_PREOP) {
767 warn(expr->pos, "not addressable");
768 return NULL;
771 symbol = alloc_symbol(expr->pos, SYM_PTR);
772 symbol->ctype.alignment = POINTER_ALIGNMENT;
773 symbol->bit_size = BITS_IN_POINTER;
775 ctype = op->ctype;
776 if (ctype->type == SYM_NODE) {
777 ctype->ctype.modifiers |= MOD_ADDRESSABLE;
778 if (ctype->ctype.modifiers & MOD_REGISTER) {
779 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(ctype->ident));
780 ctype->ctype.modifiers &= ~MOD_REGISTER;
782 symbol->ctype.modifiers = ctype->ctype.modifiers;
783 symbol->ctype.as = ctype->ctype.as;
784 symbol->ctype.context = ctype->ctype.context;
785 symbol->ctype.contextmask = ctype->ctype.contextmask;
786 ctype = ctype->ctype.base_type;
789 symbol->ctype.base_type = ctype;
790 *expr = *op->unop;
791 expr->ctype = symbol;
792 return symbol;
796 static struct symbol *evaluate_dereference(struct expression *expr)
798 struct expression *op = expr->unop;
799 struct symbol *ctype = op->ctype, *sym;
801 sym = alloc_symbol(expr->pos, SYM_NODE);
802 sym->ctype = ctype->ctype;
803 if (ctype->type == SYM_NODE) {
804 ctype = ctype->ctype.base_type;
805 merge_type(sym, ctype);
807 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
808 warn(expr->pos, "cannot derefence this type");
809 return 0;
812 ctype = ctype->ctype.base_type;
813 examine_symbol_type(ctype);
814 if (!ctype) {
815 warn(expr->pos, "undefined type");
816 return NULL;
819 sym->bit_size = ctype->bit_size;
820 sym->array_size = ctype->array_size;
822 /* Simplify: *&(expr) => (expr) */
823 if (op->type == EXPR_PREOP && op->op == '&') {
824 *expr = *op->unop;
827 expr->ctype = sym;
828 return sym;
832 * Unary post-ops: x++ and x--
834 static struct symbol *evaluate_postop(struct expression *expr)
836 struct expression *op = expr->unop;
837 struct symbol *ctype = op->ctype;
839 if (!lvalue_expression(expr->unop)) {
840 warn(expr->pos, "need lvalue expression for ++/--");
841 return NULL;
843 expr->ctype = ctype;
844 return ctype;
847 static struct symbol *evaluate_preop(struct expression *expr)
849 struct symbol *ctype = expr->unop->ctype;
851 switch (expr->op) {
852 case '(':
853 case '+':
854 *expr = *expr->unop;
855 return ctype;
857 case '*':
858 return evaluate_dereference(expr);
860 case '&':
861 return evaluate_addressof(expr);
863 case SPECIAL_INCREMENT:
864 case SPECIAL_DECREMENT:
866 * From a type evaluation standpoint the pre-ops are
867 * the same as the postops
869 return evaluate_postop(expr);
871 case '!':
872 ctype = &bool_ctype;
873 break;
875 default:
876 break;
878 expr->ctype = ctype;
879 return &bool_ctype;
882 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
884 struct ptr_list *head = (struct ptr_list *)_list;
885 struct ptr_list *list = head;
887 if (!head)
888 return NULL;
889 do {
890 int i;
891 for (i = 0; i < list->nr; i++) {
892 struct symbol *sym = (struct symbol *) list->list[i];
893 if (sym->ident) {
894 if (sym->ident != ident)
895 continue;
896 *offset = sym->offset;
897 return sym;
898 } else {
899 struct symbol *ctype = sym->ctype.base_type;
900 struct symbol *sub;
901 if (!ctype)
902 continue;
903 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
904 continue;
905 sub = find_identifier(ident, ctype->symbol_list, offset);
906 if (!sub)
907 continue;
908 *offset += sym->offset;
909 return sub;
912 } while ((list = list->next) != head);
913 return NULL;
916 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
918 struct expression *add;
920 if (!offset)
921 return expr;
923 /* Create a new add-expression */
924 add = alloc_expression(expr->pos, EXPR_BINOP);
925 add->op = '+';
926 add->ctype = &ptr_ctype;
927 add->left = expr;
928 add->right = alloc_expression(expr->pos, EXPR_VALUE);
929 add->right->ctype = &int_ctype;
930 add->right->value = offset;
932 return add;
935 /* structure/union dereference */
936 static struct symbol *evaluate_member_dereference(struct expression *expr)
938 int offset;
939 struct symbol *ctype, *member, *sym;
940 struct expression *deref = expr->deref, *add;
941 struct ident *ident = expr->member;
942 unsigned int mod;
943 int address_space;
945 if (!evaluate_expression(deref))
946 return NULL;
947 if (!ident) {
948 warn(expr->pos, "bad member name");
949 return NULL;
952 ctype = deref->ctype;
953 address_space = ctype->ctype.as;
954 mod = ctype->ctype.modifiers;
955 if (ctype->type == SYM_NODE) {
956 ctype = ctype->ctype.base_type;
957 address_space |= ctype->ctype.as;
958 mod |= ctype->ctype.modifiers;
960 if (expr->op == SPECIAL_DEREFERENCE) {
961 /* Arrays will degenerate into pointers for '->' */
962 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
963 warn(expr->pos, "expected a pointer to a struct/union");
964 return NULL;
966 mod = ctype->ctype.modifiers;
967 address_space = ctype->ctype.as;
968 ctype = ctype->ctype.base_type;
969 if (ctype->type == SYM_NODE) {
970 mod |= ctype->ctype.modifiers;
971 address_space |= ctype->ctype.as;
972 ctype = ctype->ctype.base_type;
974 } else {
975 if (!lvalue_expression(deref)) {
976 warn(deref->pos, "expected lvalue for member dereference");
977 return NULL;
979 deref = deref->unop;
980 expr->deref = deref;
982 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
983 warn(expr->pos, "expected structure or union");
984 return NULL;
986 offset = 0;
987 member = find_identifier(ident, ctype->symbol_list, &offset);
988 if (!member) {
989 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
990 const char *name = "<unnamed>";
991 int namelen = 9;
992 if (ctype->ident) {
993 name = ctype->ident->name;
994 namelen = ctype->ident->len;
996 warn(expr->pos, "no member '%s' in %s %.*s",
997 show_ident(ident), type, namelen, name);
998 return NULL;
1001 add = evaluate_offset(deref, offset);
1003 sym = alloc_symbol(expr->pos, SYM_NODE);
1004 sym->bit_size = member->bit_size;
1005 sym->array_size = member->array_size;
1006 sym->ctype = member->ctype;
1007 sym->ctype.modifiers = mod;
1008 sym->ctype.as = address_space;
1009 ctype = member->ctype.base_type;
1010 if (ctype->type == SYM_BITFIELD) {
1011 ctype = ctype->ctype.base_type;
1012 expr->type = EXPR_BITFIELD;
1013 expr->bitpos = member->bit_offset;
1014 expr->nrbits = member->fieldwidth;
1015 expr->address = add;
1016 } else {
1017 expr->type = EXPR_PREOP;
1018 expr->op = '*';
1019 expr->unop = add;
1022 expr->ctype = sym;
1023 return sym;
1026 static struct symbol *evaluate_sizeof(struct expression *expr)
1028 int size;
1030 if (expr->cast_type) {
1031 examine_symbol_type(expr->cast_type);
1032 size = expr->cast_type->bit_size;
1033 } else {
1034 if (!evaluate_expression(expr->cast_expression))
1035 return 0;
1036 size = expr->cast_expression->ctype->bit_size;
1038 if (size & 7) {
1039 warn(expr->pos, "cannot size expression");
1040 return 0;
1042 expr->type = EXPR_VALUE;
1043 expr->value = size >> 3;
1044 expr->ctype = size_t_ctype;
1045 return size_t_ctype;
1048 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1050 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1051 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1052 return clash != 0;
1055 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1057 struct expression *expr;
1058 struct symbol_list *argument_types = fn->arguments;
1059 struct symbol *argtype;
1060 int i = 1;
1062 PREPARE_PTR_LIST(argument_types, argtype);
1063 FOR_EACH_PTR (head, expr) {
1064 struct expression **p = THIS_ADDRESS(expr);
1065 struct symbol *ctype, *target;
1066 ctype = evaluate_expression(expr);
1068 if (!ctype)
1069 return 0;
1071 if (context_clash(f, ctype))
1072 warn(expr->pos, "argument %d used in wrong context", i);
1074 ctype = degenerate(expr, ctype, p);
1076 target = argtype;
1077 if (!target && ctype->bit_size < BITS_IN_INT)
1078 target = &int_ctype;
1079 if (target) {
1080 static char where[30];
1081 examine_symbol_type(target);
1082 sprintf(where, "argument %d", i);
1083 compatible_assignment_types(expr, target, p, ctype, where);
1086 i++;
1087 NEXT_PTR_LIST(argtype);
1088 } END_FOR_EACH_PTR;
1089 FINISH_PTR_LIST(argtype);
1090 return 1;
1093 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1094 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1096 struct expression *entry;
1097 int current = 0;
1098 int max = 0;
1100 FOR_EACH_PTR(expr->expr_list, entry) {
1101 struct expression **p = THIS_ADDRESS(entry);
1103 if (entry->type == EXPR_INDEX) {
1104 current = entry->idx_to;
1105 continue;
1107 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1108 current++;
1109 if (current > max)
1110 max = current;
1111 } END_FOR_EACH_PTR;
1112 return max;
1115 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1117 struct expression *entry;
1118 struct symbol *sym;
1120 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1121 FOR_EACH_PTR(expr->expr_list, entry) {
1122 struct expression **p = THIS_ADDRESS(entry);
1124 if (entry->type == EXPR_IDENTIFIER) {
1125 struct ident *ident = entry->expr_ident;
1126 /* We special-case the "already right place" case */
1127 if (sym && sym->ident == ident)
1128 continue;
1129 RESET_PTR_LIST(sym);
1130 for (;;) {
1131 if (!sym) {
1132 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1133 return 0;
1135 if (sym->ident == ident)
1136 break;
1137 NEXT_PTR_LIST(sym);
1139 continue;
1142 if (!sym) {
1143 warn(expr->pos, "too many initializers for struct/union");
1144 return 0;
1147 evaluate_initializer(sym, p, offset + sym->offset);
1149 NEXT_PTR_LIST(sym);
1150 } END_FOR_EACH_PTR;
1151 FINISH_PTR_LIST(sym);
1153 return 0;
1157 * Initializers are kind of like assignments. Except
1158 * they can be a hell of a lot more complex.
1160 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1162 struct expression *expr = *ep;
1165 * Simple non-structure/array initializers are the simple
1166 * case, and look (and parse) largely like assignments.
1168 if (expr->type != EXPR_INITIALIZER) {
1169 int size = 0;
1170 struct symbol *rtype = evaluate_expression(expr);
1171 if (rtype) {
1172 struct expression *pos;
1174 // FIXME! char array[] = "string" special case
1175 // should _not_ degenerate.
1176 rtype = degenerate(expr, rtype, ep);
1177 expr = *ep;
1178 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1179 /* strings are special: char arrays */
1180 if (rtype->type == SYM_ARRAY)
1181 size = get_expression_value(rtype->array_size);
1183 * Don't bother creating a position expression for
1184 * the simple initializer cases that don't need it.
1186 * We need a position if the initializer has a byte
1187 * offset, _or_ if we're initializing a bitfield.
1189 if (offset || ctype->fieldwidth) {
1190 pos = alloc_expression(expr->pos, EXPR_POS);
1191 pos->init_offset = offset;
1192 pos->init_sym = ctype;
1193 pos->init_expr = *ep;
1194 pos->ctype = expr->ctype;
1195 *ep = pos;
1198 return size;
1201 expr->ctype = ctype;
1202 if (ctype->type == SYM_NODE)
1203 ctype = ctype->ctype.base_type;
1205 switch (ctype->type) {
1206 case SYM_ARRAY:
1207 case SYM_PTR:
1208 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1209 case SYM_UNION:
1210 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1211 case SYM_STRUCT:
1212 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1213 default:
1214 break;
1216 warn(expr->pos, "unexpected compound initializer");
1217 return 0;
1220 static struct symbol *evaluate_cast(struct expression *expr)
1222 struct expression *target = expr->cast_expression;
1223 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1225 expr->ctype = ctype;
1226 expr->cast_type = ctype;
1229 * Special case: a cast can be followed by an
1230 * initializer, in which case we need to pass
1231 * the type value down to that initializer rather
1232 * than trying to evaluate it as an expression
1234 * A more complex case is when the initializer is
1235 * dereferenced as part of a post-fix expression.
1236 * We need to produce an expression that can be dereferenced.
1238 if (target->type == EXPR_INITIALIZER) {
1239 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
1240 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1242 sym->ctype.base_type = ctype;
1243 sym->initializer = expr->cast_expression;
1244 evaluate_symbol(sym);
1246 addr->ctype = &ptr_ctype;
1247 addr->symbol = sym;
1249 expr->type = EXPR_PREOP;
1250 expr->op = '*';
1251 expr->unop = addr;
1252 expr->ctype = ctype;
1253 return ctype;
1256 evaluate_expression(target);
1259 * Casts of constant values are special: they
1260 * can be NULL, and thus need to be simplified
1261 * early.
1263 if (target->type == EXPR_VALUE)
1264 cast_value(expr, ctype, target, target->ctype);
1266 return ctype;
1270 * Evaluate a call expression with a symbol. This
1271 * should expand inline functions, and evaluate
1272 * builtins.
1274 static int evaluate_symbol_call(struct expression *expr)
1276 struct expression *fn = expr->fn;
1277 struct symbol *ctype = fn->ctype;
1279 if (fn->type != EXPR_PREOP)
1280 return 0;
1282 if (ctype->op && ctype->op->evaluate)
1283 return ctype->op->evaluate(expr);
1285 if (ctype->ctype.modifiers & MOD_INLINE) {
1286 int ret;
1287 struct symbol *curr = current_fn;
1288 unsigned long context = current_context;
1289 unsigned long mask = current_contextmask;
1291 current_context |= ctype->ctype.context;
1292 current_contextmask |= ctype->ctype.contextmask;
1293 current_fn = ctype->ctype.base_type;
1294 ret = inline_function(expr, ctype);
1296 /* restore the old function context */
1297 current_fn = curr;
1298 current_context = context;
1299 current_contextmask = mask;
1300 return ret;
1303 return 0;
1306 static struct symbol *evaluate_call(struct expression *expr)
1308 int args, fnargs;
1309 struct symbol *ctype, *sym;
1310 struct expression *fn = expr->fn;
1311 struct expression_list *arglist = expr->args;
1313 if (!evaluate_expression(fn))
1314 return NULL;
1315 sym = ctype = fn->ctype;
1316 if (ctype->type == SYM_NODE)
1317 ctype = ctype->ctype.base_type;
1318 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1319 ctype = ctype->ctype.base_type;
1320 if (!evaluate_arguments(sym, ctype, arglist))
1321 return NULL;
1322 if (sym->type == SYM_NODE) {
1323 if (evaluate_symbol_call(expr))
1324 return expr->ctype;
1326 if (sym->type == SYM_NODE) {
1327 if (evaluate_symbol_call(expr))
1328 return expr->ctype;
1330 if (ctype->type != SYM_FN) {
1331 warn(expr->pos, "not a function %.*s",
1332 sym->ident->len, sym->ident->name);
1333 return NULL;
1335 args = expression_list_size(expr->args);
1336 fnargs = symbol_list_size(ctype->arguments);
1337 if (args < fnargs)
1338 warn(expr->pos, "not enough arguments for function %.*s",
1339 sym->ident->len, sym->ident->name);
1340 if (args > fnargs && !ctype->variadic)
1341 warn(expr->pos, "too many arguments for function %.*s",
1342 sym->ident->len, sym->ident->name);
1343 expr->ctype = ctype->ctype.base_type;
1344 return expr->ctype;
1347 struct symbol *evaluate_expression(struct expression *expr)
1349 if (!expr)
1350 return NULL;
1351 if (expr->ctype)
1352 return expr->ctype;
1354 switch (expr->type) {
1355 case EXPR_VALUE:
1356 warn(expr->pos, "value expression without a type");
1357 return NULL;
1358 case EXPR_STRING:
1359 return evaluate_string(expr);
1360 case EXPR_SYMBOL:
1361 return evaluate_symbol_expression(expr);
1362 case EXPR_BINOP:
1363 if (!evaluate_expression(expr->left))
1364 return NULL;
1365 if (!evaluate_expression(expr->right))
1366 return NULL;
1367 return evaluate_binop(expr);
1368 case EXPR_LOGICAL:
1369 return evaluate_logical(expr);
1370 case EXPR_COMMA:
1371 if (!evaluate_expression(expr->left))
1372 return NULL;
1373 if (!evaluate_expression(expr->right))
1374 return NULL;
1375 return evaluate_comma(expr);
1376 case EXPR_COMPARE:
1377 if (!evaluate_expression(expr->left))
1378 return NULL;
1379 if (!evaluate_expression(expr->right))
1380 return NULL;
1381 return evaluate_compare(expr);
1382 case EXPR_ASSIGNMENT:
1383 if (!evaluate_expression(expr->left))
1384 return NULL;
1385 if (!evaluate_expression(expr->right))
1386 return NULL;
1387 return evaluate_assignment(expr);
1388 case EXPR_PREOP:
1389 if (!evaluate_expression(expr->unop))
1390 return NULL;
1391 return evaluate_preop(expr);
1392 case EXPR_POSTOP:
1393 if (!evaluate_expression(expr->unop))
1394 return NULL;
1395 return evaluate_postop(expr);
1396 case EXPR_CAST:
1397 return evaluate_cast(expr);
1398 case EXPR_SIZEOF:
1399 return evaluate_sizeof(expr);
1400 case EXPR_DEREF:
1401 return evaluate_member_dereference(expr);
1402 case EXPR_CALL:
1403 return evaluate_call(expr);
1404 case EXPR_BITFIELD:
1405 warn(expr->pos, "bitfield generated by parser");
1406 return NULL;
1407 case EXPR_CONDITIONAL:
1408 if (!evaluate_expression(expr->conditional))
1409 return NULL;
1410 if (!evaluate_expression(expr->cond_false))
1411 return NULL;
1412 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1413 return NULL;
1414 return evaluate_conditional(expr);
1415 case EXPR_STATEMENT:
1416 expr->ctype = evaluate_statement(expr->statement);
1417 return expr->ctype;
1419 case EXPR_LABEL:
1420 expr->ctype = &ptr_ctype;
1421 return &ptr_ctype;
1423 /* These can not exist as stand-alone expressions */
1424 case EXPR_INITIALIZER:
1425 case EXPR_IDENTIFIER:
1426 case EXPR_INDEX:
1427 case EXPR_POS:
1428 warn(expr->pos, "internal front-end error: initializer in expression");
1429 return NULL;
1431 return NULL;
1434 void check_duplicates(struct symbol *sym)
1436 struct symbol *next = sym;
1438 while ((next = next->same_symbol) != NULL) {
1439 const char *typediff;
1440 evaluate_symbol(next);
1441 typediff = type_difference(sym, next, 0, 0);
1442 if (typediff) {
1443 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1444 show_ident(sym->ident),
1445 input_streams[next->pos.stream].name, next->pos.line, typediff);
1446 return;
1451 struct symbol *evaluate_symbol(struct symbol *sym)
1453 struct symbol *base_type;
1455 if (!sym)
1456 return sym;
1458 sym = examine_symbol_type(sym);
1459 base_type = sym->ctype.base_type;
1460 if (!base_type)
1461 return NULL;
1463 /* Evaluate the initializers */
1464 if (sym->initializer) {
1465 int count = evaluate_initializer(sym, &sym->initializer, 0);
1466 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
1467 int bit_size = count * base_type->ctype.base_type->bit_size;
1468 base_type->array_size = alloc_const_expression(sym->pos, count);
1469 base_type->bit_size = bit_size;
1470 sym->array_size = base_type->array_size;
1471 sym->bit_size = bit_size;
1475 /* And finally, evaluate the body of the symbol too */
1476 if (base_type->type == SYM_FN) {
1477 struct symbol *s;
1479 FOR_EACH_PTR(base_type->arguments, s) {
1480 evaluate_symbol(s);
1481 } END_FOR_EACH_PTR;
1483 if (base_type->stmt) {
1484 current_fn = base_type;
1485 current_contextmask = sym->ctype.contextmask;
1486 current_context = sym->ctype.context;
1487 evaluate_statement(base_type->stmt);
1491 return base_type;
1494 struct symbol *evaluate_return_expression(struct statement *stmt)
1496 struct expression *expr = stmt->expression;
1497 struct symbol *ctype, *fntype;
1499 fntype = current_fn->ctype.base_type;
1500 if (!fntype || fntype == &void_ctype) {
1501 if (expr)
1502 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
1503 return NULL;
1506 if (!expr) {
1507 warn(stmt->pos, "return with no return value");
1508 return NULL;
1510 ctype = evaluate_expression(expr);
1511 if (!ctype)
1512 return NULL;
1513 ctype = degenerate(expr, ctype, &expr);
1514 expr->ctype = ctype;
1515 compatible_assignment_types(expr, fntype, &expr, ctype, "return expression");
1516 stmt->expression = expr;
1517 return NULL;
1520 static void evaluate_if_statement(struct statement *stmt)
1522 struct expression *expr = stmt->if_conditional;
1523 struct symbol *ctype;
1525 if (!expr)
1526 return;
1527 if (expr->type == EXPR_ASSIGNMENT)
1528 warn(expr->pos, "assignment expression in conditional");
1530 ctype = evaluate_expression(expr);
1531 if (!ctype)
1532 return;
1534 evaluate_statement(stmt->if_true);
1535 evaluate_statement(stmt->if_false);
1538 struct symbol *evaluate_statement(struct statement *stmt)
1540 if (!stmt)
1541 return NULL;
1543 switch (stmt->type) {
1544 case STMT_RETURN:
1545 return evaluate_return_expression(stmt);
1547 case STMT_EXPRESSION:
1548 return evaluate_expression(stmt->expression);
1550 case STMT_COMPOUND: {
1551 struct statement *s;
1552 struct symbol *type = NULL;
1553 struct symbol *sym;
1555 /* Evaluate each symbol in the compound statement */
1556 FOR_EACH_PTR(stmt->syms, sym) {
1557 evaluate_symbol(sym);
1558 } END_FOR_EACH_PTR;
1559 evaluate_symbol(stmt->ret);
1562 * Then, evaluate each statement, making the type of the
1563 * compound statement be the type of the last statement
1565 type = NULL;
1566 FOR_EACH_PTR(stmt->stmts, s) {
1567 type = evaluate_statement(s);
1568 } END_FOR_EACH_PTR;
1569 return type;
1571 case STMT_IF:
1572 evaluate_if_statement(stmt);
1573 return NULL;
1574 case STMT_ITERATOR:
1575 evaluate_expression(stmt->iterator_pre_condition);
1576 evaluate_expression(stmt->iterator_post_condition);
1577 evaluate_statement(stmt->iterator_pre_statement);
1578 evaluate_statement(stmt->iterator_statement);
1579 evaluate_statement(stmt->iterator_post_statement);
1580 return NULL;
1581 case STMT_SWITCH:
1582 evaluate_expression(stmt->switch_expression);
1583 evaluate_statement(stmt->switch_statement);
1584 return NULL;
1585 case STMT_CASE:
1586 evaluate_expression(stmt->case_expression);
1587 evaluate_expression(stmt->case_to);
1588 evaluate_statement(stmt->case_statement);
1589 return NULL;
1590 case STMT_LABEL:
1591 evaluate_statement(stmt->label_statement);
1592 return NULL;
1593 case STMT_GOTO:
1594 evaluate_expression(stmt->goto_expression);
1595 return NULL;
1596 case STMT_NONE:
1597 break;
1598 case STMT_ASM:
1599 /* FIXME! Do the asm parameter evaluation! */
1600 break;
1602 /* These should not exist at evaluation time */
1603 case STMT_CONDTRUE:
1604 case STMT_CONDFALSE:
1605 break;
1607 return NULL;