Be more careful about type generation in dereferences.
[smatch.git] / evaluate.c
blob433eeb6c7bafd860bea723da3bd3b7a0fffe9c04
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->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;
320 if (target == source)
321 break;
322 if (!target || !source)
323 return "different types";
325 * Peel of per-node information.
326 * FIXME! Check alignment, address space, and context too here!
328 if (target->type == SYM_NODE)
329 target = target->ctype.base_type;
330 if (source->type == SYM_NODE)
331 source = source->ctype.base_type;
333 if (target == source)
334 break;
335 if (!target || !source)
336 return "different types";
338 mod1 = target->ctype.modifiers;
339 as1 = target->ctype.as;
340 mod2 = source->ctype.modifiers;
341 as2 = source->ctype.as;
343 if (target->type != source->type) {
344 int type1 = target->type;
345 int type2 = source->type;
347 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
348 type1 = type1 == SYM_ARRAY ? SYM_PTR : type1;
349 type2 = type2 == SYM_ARRAY ? SYM_PTR : type2;
351 if ((type1 == SYM_PTR) && (target->ctype.base_type->type == SYM_FN)) {
352 target = target->ctype.base_type;
353 type1 = SYM_FN;
356 if ((type2 == SYM_PTR) && (source->ctype.base_type->type == SYM_FN)) {
357 source = source->ctype.base_type;
358 type2 = SYM_FN;
361 if (type1 != type2)
362 return "different base types";
365 /* Must be same address space to be comparable */
366 if (as1 != as2)
367 return "different address spaces";
369 /* Ignore differences in storage types, sign, or addressability */
370 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
371 if (diff) {
372 mod1 &= diff & ~target_mod_ignore;
373 mod2 &= diff & ~source_mod_ignore;
374 if (mod1 | mod2) {
375 if ((mod1 | mod2) & MOD_SIZE)
376 return "different type sizes";
377 return "different modifiers";
381 if (target->type == SYM_FN) {
382 int i;
383 struct symbol *arg1, *arg2;
384 if (target->variadic != source->variadic)
385 return "incompatible variadic arguments";
386 PREPARE_PTR_LIST(target->arguments, arg1);
387 PREPARE_PTR_LIST(source->arguments, arg2);
388 i = 1;
389 for (;;) {
390 const char *diff;
391 diff = type_difference(arg1, arg2, 0, 0);
392 if (diff) {
393 static char argdiff[80];
394 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
395 return argdiff;
397 if (!arg1)
398 break;
399 NEXT_PTR_LIST(arg1);
400 NEXT_PTR_LIST(arg2);
401 i++;
403 FINISH_PTR_LIST(arg2);
404 FINISH_PTR_LIST(arg1);
407 target = target->ctype.base_type;
408 source = source->ctype.base_type;
410 return NULL;
413 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
415 /* NULL expression? Just return the type of the "other side" */
416 if (r->type == EXPR_VALUE && !r->value)
417 return l->ctype;
418 if (l->type == EXPR_VALUE && !l->value)
419 return r->ctype;
420 return NULL;
424 * Ignore differences in "volatile" and "const"ness when
425 * subtracting pointers
427 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
429 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
431 const char *typediff;
432 struct symbol *ctype;
433 struct symbol *ltype = l->ctype, *rtype = r->ctype;
436 * If it is an integer subtract: the ptr add case will do the
437 * right thing.
439 if (!is_ptr_type(rtype))
440 return evaluate_ptr_add(expr, l, r);
442 ctype = ltype;
443 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
444 if (typediff) {
445 ctype = common_ptr_type(l, r);
446 if (!ctype) {
447 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
448 return NULL;
451 examine_symbol_type(ctype);
453 /* Figure out the base type we point to */
454 if (ctype->type == SYM_NODE)
455 ctype = ctype->ctype.base_type;
456 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
457 warn(expr->pos, "subtraction of functions? Share your drugs");
458 return NULL;
460 ctype = ctype->ctype.base_type;
462 expr->ctype = ssize_t_ctype;
463 if (ctype->bit_size > BITS_IN_CHAR) {
464 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
465 struct expression *div = expr;
466 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
468 val->ctype = size_t_ctype;
469 val->value = ctype->bit_size >> 3;
471 sub->op = '-';
472 sub->ctype = ssize_t_ctype;
473 sub->left = l;
474 sub->right = r;
476 div->op = '/';
477 div->left = sub;
478 div->right = val;
481 return ssize_t_ctype;
484 static struct symbol *evaluate_sub(struct expression *expr)
486 struct expression *left = expr->left, *right = expr->right;
487 struct symbol *ltype = left->ctype;
489 if (is_ptr_type(ltype))
490 return evaluate_ptr_sub(expr, left, right);
492 // FIXME! FP promotion
493 return evaluate_int_binop(expr);
496 static struct symbol *evaluate_logical(struct expression *expr)
498 if (!evaluate_expression(expr->left))
499 return NULL;
500 if (!evaluate_expression(expr->right))
501 return NULL;
502 expr->ctype = &bool_ctype;
503 return &bool_ctype;
506 static struct symbol *evaluate_arithmetic(struct expression *expr)
508 // FIXME! Floating-point promotion!
509 return evaluate_int_binop(expr);
512 static struct symbol *evaluate_binop(struct expression *expr)
514 switch (expr->op) {
515 // addition can take ptr+int, fp and int
516 case '+':
517 return evaluate_add(expr);
519 // subtraction can take ptr-ptr, fp and int
520 case '-':
521 return evaluate_sub(expr);
523 // Arithmetic operations can take fp and int
524 case '*': case '/': case '%':
525 return evaluate_arithmetic(expr);
527 // The rest are integer operations (bitops)
528 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
529 // '&', '^', '|'
530 default:
531 return evaluate_int_binop(expr);
535 static struct symbol *evaluate_comma(struct expression *expr)
537 expr->ctype = expr->right->ctype;
538 return expr->ctype;
541 static struct symbol *evaluate_compare(struct expression *expr)
543 struct expression *left = expr->left, *right = expr->right;
544 struct symbol *ltype = left->ctype, *rtype = right->ctype;
545 struct symbol *ctype;
547 /* Type types? */
548 if (is_type_type(ltype) && is_type_type(rtype)) {
549 expr->ctype = &bool_ctype;
550 return &bool_ctype;
553 /* Pointer types? */
554 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
555 expr->ctype = &bool_ctype;
556 // FIXME! Check the types for compatibility
557 return &bool_ctype;
560 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
561 if (ctype) {
562 expr->ctype = &bool_ctype;
563 return &bool_ctype;
566 return bad_expr_type(expr);
569 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
571 /* Integer promotion? */
572 if (ltype->type == SYM_NODE)
573 ltype = ltype->ctype.base_type;
574 if (rtype->type == SYM_NODE)
575 rtype = rtype->ctype.base_type;
576 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
577 ltype = &int_ctype;
578 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
579 rtype = &int_ctype;
580 return (is_int_type(ltype) && is_int_type(rtype));
583 static int is_null_ptr(struct expression *expr)
585 return (expr->type == EXPR_VALUE &&
586 expr->value == 0);
590 * FIXME!! This should do casts, array degeneration etc..
592 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
594 struct symbol *ltype = left->ctype, *rtype = right->ctype;
596 if (ltype->type == SYM_NODE)
597 ltype = ltype->ctype.base_type;
599 if (ltype->type == SYM_PTR) {
600 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
601 return ltype;
604 if (rtype->type == SYM_NODE)
605 rtype = rtype->ctype.base_type;
607 if (rtype->type == SYM_PTR) {
608 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
609 return rtype;
611 return NULL;
614 static struct symbol *do_degenerate(struct expression **ep)
616 struct expression *expr = *ep;
617 return degenerate(expr, expr->ctype, ep);
620 static struct symbol * evaluate_conditional(struct expression *expr)
622 struct expression *cond, *true, *false;
623 struct symbol *ctype, *ltype, *rtype;
624 const char * typediff;
626 ctype = do_degenerate(&expr->conditional);
627 cond = expr->conditional;
629 ltype = ctype;
630 true = cond;
631 if (expr->cond_true) {
632 ltype = do_degenerate(&expr->cond_true);
633 true = expr->cond_true;
636 rtype = do_degenerate(&expr->cond_false);
637 false = expr->cond_false;
639 ctype = ltype;
640 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
641 if (typediff) {
642 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
643 if (!ctype) {
644 ctype = compatible_ptr_type(true, expr->cond_false);
645 if (!ctype) {
646 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
647 return NULL;
652 expr->ctype = ctype;
653 return ctype;
656 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
657 struct expression **rp, struct symbol *source, const char *where)
659 const char *typediff;
660 struct symbol *t;
661 int target_as;
663 /* It's ok if the target is more volatile or const than the source */
664 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
665 if (!typediff)
666 return 1;
668 if (compatible_integer_types(target, source)) {
669 if (target->bit_size != source->bit_size)
670 *rp = cast_to(*rp, target);
671 return 1;
674 /* Pointer destination? */
675 t = target;
676 target_as = t->ctype.as;
677 if (t->type == SYM_NODE) {
678 t = t->ctype.base_type;
679 target_as |= t->ctype.as;
681 if (t->type == SYM_PTR || t->type == SYM_FN) {
682 struct expression *right = *rp;
683 struct symbol *s = source;
684 int source_as;
686 // NULL pointer is always ok
687 if (right->type == EXPR_VALUE && !right->value)
688 return 1;
690 /* "void *" matches anything as long as the address space is ok */
691 source_as = s->ctype.as;
692 if (s->type == SYM_NODE) {
693 s = s->ctype.base_type;
694 source_as |= s->ctype.as;
696 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
697 s = s->ctype.base_type;
698 t = t->ctype.base_type;
699 if (s == &void_ctype || t == &void_ctype)
700 return 1;
704 // FIXME!! Cast it?
705 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
706 warn(expr->pos, " expected %s", show_typename(target));
707 warn(expr->pos, " got %s", show_typename(source));
708 return 0;
712 * FIXME!! This is wrong from a double evaluation standpoint. We can't
713 * just expand the expression twice, that would make any side effects
714 * happen twice too.
716 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
718 int op = expr->op;
719 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
720 static const int op_trans[] = {
721 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
722 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
723 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
724 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
725 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
726 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
727 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
728 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
729 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
730 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
733 subexpr->left = left;
734 subexpr->right = right;
735 subexpr->op = op_trans[op - SPECIAL_BASE];
736 expr->op = '=';
737 expr->right = subexpr;
738 return evaluate_binop(subexpr);
741 static struct symbol *evaluate_assignment(struct expression *expr)
743 struct expression *left = expr->left, *right = expr->right;
744 struct symbol *ltype, *rtype;
746 ltype = left->ctype;
747 rtype = right->ctype;
748 if (expr->op != '=') {
749 rtype = evaluate_binop_assignment(expr, left, right);
750 if (!rtype)
751 return 0;
752 right = expr->right;
755 if (!lvalue_expression(left)) {
756 warn(expr->pos, "not an lvalue");
757 return NULL;
760 rtype = degenerate(right, rtype, &expr->right);
762 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
763 return 0;
765 if (ltype->type == SYM_NODE)
766 ltype->ctype.modifiers |= MOD_ASSIGNED;
768 expr->ctype = ltype;
769 return ltype;
772 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym)
774 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
776 ptr->ctype.base_type = sym;
777 sym->ctype.modifiers |= MOD_ADDRESSABLE;
778 if (sym->ctype.modifiers & MOD_REGISTER) {
779 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
780 sym->ctype.modifiers &= ~MOD_REGISTER;
782 return ptr;
785 static struct symbol *evaluate_addressof(struct expression *expr)
787 struct expression *op = expr->unop;
788 struct expression *addr;
790 if (op->op != '*' || op->type != EXPR_PREOP) {
791 warn(expr->pos, "not addressable");
792 return NULL;
795 addr = op->unop;
796 *expr = *addr;
799 * We're lazy generating the ptr symbol type for symbols,
800 * so if we actually want the address-of, we now need to
801 * generate the proper pointer.
803 if (addr->type == EXPR_SYMBOL)
804 expr->ctype = create_pointer(expr, addr->symbol);
806 return expr->ctype;
810 static struct symbol *evaluate_dereference(struct expression *expr)
812 struct expression *op = expr->unop;
813 struct symbol *ctype = op->ctype, *sym;
815 sym = alloc_symbol(expr->pos, SYM_NODE);
816 sym->ctype = ctype->ctype;
817 if (ctype->type == SYM_NODE) {
818 ctype = ctype->ctype.base_type;
819 merge_type(sym, ctype);
821 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
822 warn(expr->pos, "cannot derefence this type");
823 return 0;
826 ctype = ctype->ctype.base_type;
827 examine_symbol_type(ctype);
828 if (!ctype) {
829 warn(expr->pos, "undefined type");
830 return NULL;
833 sym->bit_size = ctype->bit_size;
834 sym->array_size = ctype->array_size;
836 /* Simplify: *&(expr) => (expr) */
837 if (op->type == EXPR_PREOP && op->op == '&') {
838 *expr = *op->unop;
841 expr->ctype = sym;
842 return sym;
846 * Unary post-ops: x++ and x--
848 static struct symbol *evaluate_postop(struct expression *expr)
850 struct expression *op = expr->unop;
851 struct symbol *ctype = op->ctype;
853 if (!lvalue_expression(expr->unop)) {
854 warn(expr->pos, "need lvalue expression for ++/--");
855 return NULL;
857 expr->ctype = ctype;
858 return ctype;
861 static struct symbol *evaluate_preop(struct expression *expr)
863 struct symbol *ctype = expr->unop->ctype;
865 switch (expr->op) {
866 case '(':
867 case '+':
868 *expr = *expr->unop;
869 return ctype;
871 case '*':
872 return evaluate_dereference(expr);
874 case '&':
875 return evaluate_addressof(expr);
877 case SPECIAL_INCREMENT:
878 case SPECIAL_DECREMENT:
880 * From a type evaluation standpoint the pre-ops are
881 * the same as the postops
883 return evaluate_postop(expr);
885 case '!':
886 ctype = &bool_ctype;
887 break;
889 default:
890 break;
892 expr->ctype = ctype;
893 return &bool_ctype;
896 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
898 struct ptr_list *head = (struct ptr_list *)_list;
899 struct ptr_list *list = head;
901 if (!head)
902 return NULL;
903 do {
904 int i;
905 for (i = 0; i < list->nr; i++) {
906 struct symbol *sym = (struct symbol *) list->list[i];
907 if (sym->ident) {
908 if (sym->ident != ident)
909 continue;
910 *offset = sym->offset;
911 return sym;
912 } else {
913 struct symbol *ctype = sym->ctype.base_type;
914 struct symbol *sub;
915 if (!ctype)
916 continue;
917 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
918 continue;
919 sub = find_identifier(ident, ctype->symbol_list, offset);
920 if (!sub)
921 continue;
922 *offset += sym->offset;
923 return sub;
926 } while ((list = list->next) != head);
927 return NULL;
930 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset, struct symbol *ctype)
932 struct expression *add;
934 add = expr;
935 if (offset) {
936 /* Create a new add-expression */
937 add = alloc_expression(expr->pos, EXPR_BINOP);
938 add->op = '+';
939 add->left = expr;
940 add->right = alloc_expression(expr->pos, EXPR_VALUE);
941 add->right->ctype = &int_ctype;
942 add->right->value = offset;
945 add->ctype = ctype;
946 return add;
949 /* structure/union dereference */
950 static struct symbol *evaluate_member_dereference(struct expression *expr)
952 int offset;
953 struct symbol *ctype, *member, *sym;
954 struct expression *deref = expr->deref, *add;
955 struct ident *ident = expr->member;
956 unsigned int mod;
957 int address_space;
959 if (!evaluate_expression(deref))
960 return NULL;
961 if (!ident) {
962 warn(expr->pos, "bad member name");
963 return NULL;
966 ctype = deref->ctype;
967 address_space = ctype->ctype.as;
968 mod = ctype->ctype.modifiers;
969 if (ctype->type == SYM_NODE) {
970 ctype = ctype->ctype.base_type;
971 address_space |= ctype->ctype.as;
972 mod |= ctype->ctype.modifiers;
974 if (expr->op == SPECIAL_DEREFERENCE) {
975 /* Arrays will degenerate into pointers for '->' */
976 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
977 warn(expr->pos, "expected a pointer to a struct/union");
978 return NULL;
980 mod = ctype->ctype.modifiers;
981 address_space = ctype->ctype.as;
982 ctype = ctype->ctype.base_type;
983 if (ctype->type == SYM_NODE) {
984 mod |= ctype->ctype.modifiers;
985 address_space |= ctype->ctype.as;
986 ctype = ctype->ctype.base_type;
988 } else {
989 if (!lvalue_expression(deref)) {
990 warn(deref->pos, "expected lvalue for member dereference");
991 return NULL;
993 deref = deref->unop;
994 expr->deref = deref;
996 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
997 warn(expr->pos, "expected structure or union");
998 return NULL;
1000 offset = 0;
1001 member = find_identifier(ident, ctype->symbol_list, &offset);
1002 if (!member) {
1003 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1004 const char *name = "<unnamed>";
1005 int namelen = 9;
1006 if (ctype->ident) {
1007 name = ctype->ident->name;
1008 namelen = ctype->ident->len;
1010 warn(expr->pos, "no member '%s' in %s %.*s",
1011 show_ident(ident), type, namelen, name);
1012 return NULL;
1015 ctype = create_pointer(deref, member);
1016 ctype->ctype.modifiers |= mod;
1017 add = evaluate_offset(deref, offset, ctype);
1019 sym = alloc_symbol(expr->pos, SYM_NODE);
1020 sym->bit_size = member->bit_size;
1021 sym->array_size = member->array_size;
1022 sym->ctype = member->ctype;
1023 sym->ctype.modifiers = mod;
1024 sym->ctype.as = address_space;
1026 ctype = member->ctype.base_type;
1027 if (ctype->type == SYM_BITFIELD) {
1028 ctype = ctype->ctype.base_type;
1029 expr->type = EXPR_BITFIELD;
1030 expr->bitpos = member->bit_offset;
1031 expr->nrbits = member->fieldwidth;
1032 expr->address = add;
1033 } else {
1034 expr->type = EXPR_PREOP;
1035 expr->op = '*';
1036 expr->unop = add;
1039 expr->ctype = sym;
1040 return sym;
1043 static struct symbol *evaluate_sizeof(struct expression *expr)
1045 int size;
1047 if (expr->cast_type) {
1048 examine_symbol_type(expr->cast_type);
1049 size = expr->cast_type->bit_size;
1050 } else {
1051 if (!evaluate_expression(expr->cast_expression))
1052 return 0;
1053 size = expr->cast_expression->ctype->bit_size;
1055 if (size & 7) {
1056 warn(expr->pos, "cannot size expression");
1057 return 0;
1059 expr->type = EXPR_VALUE;
1060 expr->value = size >> 3;
1061 expr->ctype = size_t_ctype;
1062 return size_t_ctype;
1065 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1067 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1068 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1069 return clash != 0;
1072 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1074 struct expression *expr;
1075 struct symbol_list *argument_types = fn->arguments;
1076 struct symbol *argtype;
1077 int i = 1;
1079 PREPARE_PTR_LIST(argument_types, argtype);
1080 FOR_EACH_PTR (head, expr) {
1081 struct expression **p = THIS_ADDRESS(expr);
1082 struct symbol *ctype, *target;
1083 ctype = evaluate_expression(expr);
1085 if (!ctype)
1086 return 0;
1088 if (context_clash(f, ctype))
1089 warn(expr->pos, "argument %d used in wrong context", i);
1091 ctype = degenerate(expr, ctype, p);
1093 target = argtype;
1094 if (!target && ctype->bit_size < BITS_IN_INT)
1095 target = &int_ctype;
1096 if (target) {
1097 static char where[30];
1098 examine_symbol_type(target);
1099 sprintf(where, "argument %d", i);
1100 compatible_assignment_types(expr, target, p, ctype, where);
1103 i++;
1104 NEXT_PTR_LIST(argtype);
1105 } END_FOR_EACH_PTR;
1106 FINISH_PTR_LIST(argtype);
1107 return 1;
1110 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1111 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1113 struct expression *entry;
1114 int current = 0;
1115 int max = 0;
1117 FOR_EACH_PTR(expr->expr_list, entry) {
1118 struct expression **p = THIS_ADDRESS(entry);
1120 if (entry->type == EXPR_INDEX) {
1121 current = entry->idx_to;
1122 continue;
1124 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1125 current++;
1126 if (current > max)
1127 max = current;
1128 } END_FOR_EACH_PTR;
1129 return max;
1132 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1134 struct expression *entry;
1135 struct symbol *sym;
1137 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1138 FOR_EACH_PTR(expr->expr_list, entry) {
1139 struct expression **p = THIS_ADDRESS(entry);
1141 if (entry->type == EXPR_IDENTIFIER) {
1142 struct ident *ident = entry->expr_ident;
1143 /* We special-case the "already right place" case */
1144 if (sym && sym->ident == ident)
1145 continue;
1146 RESET_PTR_LIST(sym);
1147 for (;;) {
1148 if (!sym) {
1149 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1150 return 0;
1152 if (sym->ident == ident)
1153 break;
1154 NEXT_PTR_LIST(sym);
1156 continue;
1159 if (!sym) {
1160 warn(expr->pos, "too many initializers for struct/union");
1161 return 0;
1164 evaluate_initializer(sym, p, offset + sym->offset);
1166 NEXT_PTR_LIST(sym);
1167 } END_FOR_EACH_PTR;
1168 FINISH_PTR_LIST(sym);
1170 return 0;
1174 * Initializers are kind of like assignments. Except
1175 * they can be a hell of a lot more complex.
1177 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1179 struct expression *expr = *ep;
1182 * Simple non-structure/array initializers are the simple
1183 * case, and look (and parse) largely like assignments.
1185 if (expr->type != EXPR_INITIALIZER) {
1186 int size = 0;
1187 struct symbol *rtype = evaluate_expression(expr);
1188 if (rtype) {
1189 struct expression *pos;
1191 // FIXME! char array[] = "string" special case
1192 // should _not_ degenerate.
1193 rtype = degenerate(expr, rtype, ep);
1194 expr = *ep;
1195 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1196 /* strings are special: char arrays */
1197 if (rtype->type == SYM_ARRAY)
1198 size = get_expression_value(rtype->array_size);
1200 * Don't bother creating a position expression for
1201 * the simple initializer cases that don't need it.
1203 * We need a position if the initializer has a byte
1204 * offset, _or_ if we're initializing a bitfield.
1206 if (offset || ctype->fieldwidth) {
1207 pos = alloc_expression(expr->pos, EXPR_POS);
1208 pos->init_offset = offset;
1209 pos->init_sym = ctype;
1210 pos->init_expr = *ep;
1211 pos->ctype = expr->ctype;
1212 *ep = pos;
1215 return size;
1218 expr->ctype = ctype;
1219 if (ctype->type == SYM_NODE)
1220 ctype = ctype->ctype.base_type;
1222 switch (ctype->type) {
1223 case SYM_ARRAY:
1224 case SYM_PTR:
1225 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1226 case SYM_UNION:
1227 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1228 case SYM_STRUCT:
1229 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1230 default:
1231 break;
1233 warn(expr->pos, "unexpected compound initializer");
1234 return 0;
1237 static struct symbol *evaluate_cast(struct expression *expr)
1239 struct expression *target = expr->cast_expression;
1240 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1242 expr->ctype = ctype;
1243 expr->cast_type = ctype;
1246 * Special case: a cast can be followed by an
1247 * initializer, in which case we need to pass
1248 * the type value down to that initializer rather
1249 * than trying to evaluate it as an expression
1251 * A more complex case is when the initializer is
1252 * dereferenced as part of a post-fix expression.
1253 * We need to produce an expression that can be dereferenced.
1255 if (target->type == EXPR_INITIALIZER) {
1256 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
1257 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1259 sym->ctype.base_type = ctype;
1260 sym->initializer = expr->cast_expression;
1261 evaluate_symbol(sym);
1263 addr->ctype = &ptr_ctype;
1264 addr->symbol = sym;
1266 expr->type = EXPR_PREOP;
1267 expr->op = '*';
1268 expr->unop = addr;
1269 expr->ctype = ctype;
1270 return ctype;
1273 evaluate_expression(target);
1276 * Casts of constant values are special: they
1277 * can be NULL, and thus need to be simplified
1278 * early.
1280 if (target->type == EXPR_VALUE)
1281 cast_value(expr, ctype, target, target->ctype);
1283 return ctype;
1287 * Evaluate a call expression with a symbol. This
1288 * should expand inline functions, and evaluate
1289 * builtins.
1291 static int evaluate_symbol_call(struct expression *expr)
1293 struct expression *fn = expr->fn;
1294 struct symbol *ctype = fn->ctype;
1296 if (fn->type != EXPR_PREOP)
1297 return 0;
1299 if (ctype->op && ctype->op->evaluate)
1300 return ctype->op->evaluate(expr);
1302 if (ctype->ctype.modifiers & MOD_INLINE) {
1303 int ret;
1304 struct symbol *curr = current_fn;
1305 unsigned long context = current_context;
1306 unsigned long mask = current_contextmask;
1308 current_context |= ctype->ctype.context;
1309 current_contextmask |= ctype->ctype.contextmask;
1310 current_fn = ctype->ctype.base_type;
1311 ret = inline_function(expr, ctype);
1313 /* restore the old function context */
1314 current_fn = curr;
1315 current_context = context;
1316 current_contextmask = mask;
1317 return ret;
1320 return 0;
1323 static struct symbol *evaluate_call(struct expression *expr)
1325 int args, fnargs;
1326 struct symbol *ctype, *sym;
1327 struct expression *fn = expr->fn;
1328 struct expression_list *arglist = expr->args;
1330 if (!evaluate_expression(fn))
1331 return NULL;
1332 sym = ctype = fn->ctype;
1333 if (ctype->type == SYM_NODE)
1334 ctype = ctype->ctype.base_type;
1335 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1336 ctype = ctype->ctype.base_type;
1337 if (!evaluate_arguments(sym, ctype, arglist))
1338 return NULL;
1339 if (sym->type == SYM_NODE) {
1340 if (evaluate_symbol_call(expr))
1341 return expr->ctype;
1343 if (sym->type == SYM_NODE) {
1344 if (evaluate_symbol_call(expr))
1345 return expr->ctype;
1347 if (ctype->type != SYM_FN) {
1348 warn(expr->pos, "not a function %.*s",
1349 sym->ident->len, sym->ident->name);
1350 return NULL;
1352 args = expression_list_size(expr->args);
1353 fnargs = symbol_list_size(ctype->arguments);
1354 if (args < fnargs)
1355 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1356 if (args > fnargs && !ctype->variadic)
1357 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1358 expr->ctype = ctype->ctype.base_type;
1359 return expr->ctype;
1362 struct symbol *evaluate_expression(struct expression *expr)
1364 if (!expr)
1365 return NULL;
1366 if (expr->ctype)
1367 return expr->ctype;
1369 switch (expr->type) {
1370 case EXPR_VALUE:
1371 warn(expr->pos, "value expression without a type");
1372 return NULL;
1373 case EXPR_STRING:
1374 return evaluate_string(expr);
1375 case EXPR_SYMBOL:
1376 return evaluate_symbol_expression(expr);
1377 case EXPR_BINOP:
1378 if (!evaluate_expression(expr->left))
1379 return NULL;
1380 if (!evaluate_expression(expr->right))
1381 return NULL;
1382 return evaluate_binop(expr);
1383 case EXPR_LOGICAL:
1384 return evaluate_logical(expr);
1385 case EXPR_COMMA:
1386 if (!evaluate_expression(expr->left))
1387 return NULL;
1388 if (!evaluate_expression(expr->right))
1389 return NULL;
1390 return evaluate_comma(expr);
1391 case EXPR_COMPARE:
1392 if (!evaluate_expression(expr->left))
1393 return NULL;
1394 if (!evaluate_expression(expr->right))
1395 return NULL;
1396 return evaluate_compare(expr);
1397 case EXPR_ASSIGNMENT:
1398 if (!evaluate_expression(expr->left))
1399 return NULL;
1400 if (!evaluate_expression(expr->right))
1401 return NULL;
1402 return evaluate_assignment(expr);
1403 case EXPR_PREOP:
1404 if (!evaluate_expression(expr->unop))
1405 return NULL;
1406 return evaluate_preop(expr);
1407 case EXPR_POSTOP:
1408 if (!evaluate_expression(expr->unop))
1409 return NULL;
1410 return evaluate_postop(expr);
1411 case EXPR_CAST:
1412 return evaluate_cast(expr);
1413 case EXPR_SIZEOF:
1414 return evaluate_sizeof(expr);
1415 case EXPR_DEREF:
1416 return evaluate_member_dereference(expr);
1417 case EXPR_CALL:
1418 return evaluate_call(expr);
1419 case EXPR_BITFIELD:
1420 warn(expr->pos, "bitfield generated by parser");
1421 return NULL;
1422 case EXPR_CONDITIONAL:
1423 if (!evaluate_expression(expr->conditional))
1424 return NULL;
1425 if (!evaluate_expression(expr->cond_false))
1426 return NULL;
1427 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1428 return NULL;
1429 return evaluate_conditional(expr);
1430 case EXPR_STATEMENT:
1431 expr->ctype = evaluate_statement(expr->statement);
1432 return expr->ctype;
1434 case EXPR_LABEL:
1435 expr->ctype = &ptr_ctype;
1436 return &ptr_ctype;
1438 case EXPR_TYPE:
1439 /* Evaluate the type of the symbol .. */
1440 evaluate_symbol(expr->symbol);
1441 /* .. but the type of the _expression_ is a "type" */
1442 expr->ctype = &type_ctype;
1443 return &type_ctype;
1445 /* These can not exist as stand-alone expressions */
1446 case EXPR_INITIALIZER:
1447 case EXPR_IDENTIFIER:
1448 case EXPR_INDEX:
1449 case EXPR_POS:
1450 warn(expr->pos, "internal front-end error: initializer in expression");
1451 return NULL;
1453 return NULL;
1456 void check_duplicates(struct symbol *sym)
1458 struct symbol *next = sym;
1460 while ((next = next->same_symbol) != NULL) {
1461 const char *typediff;
1462 evaluate_symbol(next);
1463 typediff = type_difference(sym, next, 0, 0);
1464 if (typediff) {
1465 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1466 show_ident(sym->ident),
1467 input_streams[next->pos.stream].name, next->pos.line, typediff);
1468 return;
1473 struct symbol *evaluate_symbol(struct symbol *sym)
1475 struct symbol *base_type;
1477 if (!sym)
1478 return sym;
1480 sym = examine_symbol_type(sym);
1481 base_type = sym->ctype.base_type;
1482 if (!base_type)
1483 return NULL;
1485 /* Evaluate the initializers */
1486 if (sym->initializer) {
1487 int count = evaluate_initializer(sym, &sym->initializer, 0);
1488 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
1489 int bit_size = count * base_type->ctype.base_type->bit_size;
1490 base_type->array_size = alloc_const_expression(sym->pos, count);
1491 base_type->bit_size = bit_size;
1492 sym->array_size = base_type->array_size;
1493 sym->bit_size = bit_size;
1497 /* And finally, evaluate the body of the symbol too */
1498 if (base_type->type == SYM_FN) {
1499 struct symbol *s;
1501 FOR_EACH_PTR(base_type->arguments, s) {
1502 evaluate_symbol(s);
1503 } END_FOR_EACH_PTR;
1505 if (base_type->stmt) {
1506 current_fn = base_type;
1507 current_contextmask = sym->ctype.contextmask;
1508 current_context = sym->ctype.context;
1509 evaluate_statement(base_type->stmt);
1513 return base_type;
1516 struct symbol *evaluate_return_expression(struct statement *stmt)
1518 struct expression *expr = stmt->expression;
1519 struct symbol *ctype, *fntype;
1521 fntype = current_fn->ctype.base_type;
1522 if (!fntype || fntype == &void_ctype) {
1523 if (expr)
1524 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
1525 return NULL;
1528 if (!expr) {
1529 warn(stmt->pos, "return with no return value");
1530 return NULL;
1532 ctype = evaluate_expression(expr);
1533 if (!ctype)
1534 return NULL;
1535 ctype = degenerate(expr, ctype, &expr);
1536 expr->ctype = ctype;
1537 compatible_assignment_types(expr, fntype, &expr, ctype, "return expression");
1538 stmt->expression = expr;
1539 return NULL;
1542 static void evaluate_if_statement(struct statement *stmt)
1544 struct expression *expr = stmt->if_conditional;
1545 struct symbol *ctype;
1547 if (!expr)
1548 return;
1549 if (expr->type == EXPR_ASSIGNMENT)
1550 warn(expr->pos, "assignment expression in conditional");
1552 ctype = evaluate_expression(expr);
1553 if (!ctype)
1554 return;
1556 evaluate_statement(stmt->if_true);
1557 evaluate_statement(stmt->if_false);
1560 struct symbol *evaluate_statement(struct statement *stmt)
1562 if (!stmt)
1563 return NULL;
1565 switch (stmt->type) {
1566 case STMT_RETURN:
1567 return evaluate_return_expression(stmt);
1569 case STMT_EXPRESSION:
1570 return evaluate_expression(stmt->expression);
1572 case STMT_COMPOUND: {
1573 struct statement *s;
1574 struct symbol *type = NULL;
1575 struct symbol *sym;
1577 /* Evaluate each symbol in the compound statement */
1578 FOR_EACH_PTR(stmt->syms, sym) {
1579 evaluate_symbol(sym);
1580 } END_FOR_EACH_PTR;
1581 evaluate_symbol(stmt->ret);
1584 * Then, evaluate each statement, making the type of the
1585 * compound statement be the type of the last statement
1587 type = NULL;
1588 FOR_EACH_PTR(stmt->stmts, s) {
1589 type = evaluate_statement(s);
1590 } END_FOR_EACH_PTR;
1591 return type;
1593 case STMT_IF:
1594 evaluate_if_statement(stmt);
1595 return NULL;
1596 case STMT_ITERATOR:
1597 evaluate_expression(stmt->iterator_pre_condition);
1598 evaluate_expression(stmt->iterator_post_condition);
1599 evaluate_statement(stmt->iterator_pre_statement);
1600 evaluate_statement(stmt->iterator_statement);
1601 evaluate_statement(stmt->iterator_post_statement);
1602 return NULL;
1603 case STMT_SWITCH:
1604 evaluate_expression(stmt->switch_expression);
1605 evaluate_statement(stmt->switch_statement);
1606 return NULL;
1607 case STMT_CASE:
1608 evaluate_expression(stmt->case_expression);
1609 evaluate_expression(stmt->case_to);
1610 evaluate_statement(stmt->case_statement);
1611 return NULL;
1612 case STMT_LABEL:
1613 evaluate_statement(stmt->label_statement);
1614 return NULL;
1615 case STMT_GOTO:
1616 evaluate_expression(stmt->goto_expression);
1617 return NULL;
1618 case STMT_NONE:
1619 break;
1620 case STMT_ASM:
1621 /* FIXME! Do the asm parameter evaluation! */
1622 break;
1624 return NULL;