[PATCH] more op-codes
[smatch.git] / evaluate.c
blob1ca366a2b7c3aa67c43538dae581e58df8721a57
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 *evaluate_addressof(struct expression *expr)
774 struct symbol *ctype, *symbol;
775 struct expression *op = expr->unop;
777 if (op->op != '*' || op->type != EXPR_PREOP) {
778 warn(expr->pos, "not addressable");
779 return NULL;
782 symbol = alloc_symbol(expr->pos, SYM_PTR);
783 symbol->ctype.alignment = POINTER_ALIGNMENT;
784 symbol->bit_size = BITS_IN_POINTER;
786 ctype = op->ctype;
787 if (ctype->type == SYM_NODE) {
788 ctype->ctype.modifiers |= MOD_ADDRESSABLE;
789 if (ctype->ctype.modifiers & MOD_REGISTER) {
790 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(ctype->ident));
791 ctype->ctype.modifiers &= ~MOD_REGISTER;
793 symbol->ctype.modifiers = ctype->ctype.modifiers;
794 symbol->ctype.as = ctype->ctype.as;
795 symbol->ctype.context = ctype->ctype.context;
796 symbol->ctype.contextmask = ctype->ctype.contextmask;
797 ctype = ctype->ctype.base_type;
800 symbol->ctype.base_type = ctype;
801 *expr = *op->unop;
802 expr->ctype = symbol;
803 return symbol;
807 static struct symbol *evaluate_dereference(struct expression *expr)
809 struct expression *op = expr->unop;
810 struct symbol *ctype = op->ctype, *sym;
812 sym = alloc_symbol(expr->pos, SYM_NODE);
813 sym->ctype = ctype->ctype;
814 if (ctype->type == SYM_NODE) {
815 ctype = ctype->ctype.base_type;
816 merge_type(sym, ctype);
818 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
819 warn(expr->pos, "cannot derefence this type");
820 return 0;
823 ctype = ctype->ctype.base_type;
824 examine_symbol_type(ctype);
825 if (!ctype) {
826 warn(expr->pos, "undefined type");
827 return NULL;
830 sym->bit_size = ctype->bit_size;
831 sym->array_size = ctype->array_size;
833 /* Simplify: *&(expr) => (expr) */
834 if (op->type == EXPR_PREOP && op->op == '&') {
835 *expr = *op->unop;
838 expr->ctype = sym;
839 return sym;
843 * Unary post-ops: x++ and x--
845 static struct symbol *evaluate_postop(struct expression *expr)
847 struct expression *op = expr->unop;
848 struct symbol *ctype = op->ctype;
850 if (!lvalue_expression(expr->unop)) {
851 warn(expr->pos, "need lvalue expression for ++/--");
852 return NULL;
854 expr->ctype = ctype;
855 return ctype;
858 static struct symbol *evaluate_preop(struct expression *expr)
860 struct symbol *ctype = expr->unop->ctype;
862 switch (expr->op) {
863 case '(':
864 case '+':
865 *expr = *expr->unop;
866 return ctype;
868 case '*':
869 return evaluate_dereference(expr);
871 case '&':
872 return evaluate_addressof(expr);
874 case SPECIAL_INCREMENT:
875 case SPECIAL_DECREMENT:
877 * From a type evaluation standpoint the pre-ops are
878 * the same as the postops
880 return evaluate_postop(expr);
882 case '!':
883 ctype = &bool_ctype;
884 break;
886 default:
887 break;
889 expr->ctype = ctype;
890 return &bool_ctype;
893 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
895 struct ptr_list *head = (struct ptr_list *)_list;
896 struct ptr_list *list = head;
898 if (!head)
899 return NULL;
900 do {
901 int i;
902 for (i = 0; i < list->nr; i++) {
903 struct symbol *sym = (struct symbol *) list->list[i];
904 if (sym->ident) {
905 if (sym->ident != ident)
906 continue;
907 *offset = sym->offset;
908 return sym;
909 } else {
910 struct symbol *ctype = sym->ctype.base_type;
911 struct symbol *sub;
912 if (!ctype)
913 continue;
914 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
915 continue;
916 sub = find_identifier(ident, ctype->symbol_list, offset);
917 if (!sub)
918 continue;
919 *offset += sym->offset;
920 return sub;
923 } while ((list = list->next) != head);
924 return NULL;
927 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
929 struct expression *add;
931 if (!offset)
932 return expr;
934 /* Create a new add-expression */
935 add = alloc_expression(expr->pos, EXPR_BINOP);
936 add->op = '+';
937 add->ctype = &ptr_ctype;
938 add->left = expr;
939 add->right = alloc_expression(expr->pos, EXPR_VALUE);
940 add->right->ctype = &int_ctype;
941 add->right->value = offset;
943 return add;
946 /* structure/union dereference */
947 static struct symbol *evaluate_member_dereference(struct expression *expr)
949 int offset;
950 struct symbol *ctype, *member, *sym;
951 struct expression *deref = expr->deref, *add;
952 struct ident *ident = expr->member;
953 unsigned int mod;
954 int address_space;
956 if (!evaluate_expression(deref))
957 return NULL;
958 if (!ident) {
959 warn(expr->pos, "bad member name");
960 return NULL;
963 ctype = deref->ctype;
964 address_space = ctype->ctype.as;
965 mod = ctype->ctype.modifiers;
966 if (ctype->type == SYM_NODE) {
967 ctype = ctype->ctype.base_type;
968 address_space |= ctype->ctype.as;
969 mod |= ctype->ctype.modifiers;
971 if (expr->op == SPECIAL_DEREFERENCE) {
972 /* Arrays will degenerate into pointers for '->' */
973 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
974 warn(expr->pos, "expected a pointer to a struct/union");
975 return NULL;
977 mod = ctype->ctype.modifiers;
978 address_space = ctype->ctype.as;
979 ctype = ctype->ctype.base_type;
980 if (ctype->type == SYM_NODE) {
981 mod |= ctype->ctype.modifiers;
982 address_space |= ctype->ctype.as;
983 ctype = ctype->ctype.base_type;
985 } else {
986 if (!lvalue_expression(deref)) {
987 warn(deref->pos, "expected lvalue for member dereference");
988 return NULL;
990 deref = deref->unop;
991 expr->deref = deref;
993 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
994 warn(expr->pos, "expected structure or union");
995 return NULL;
997 offset = 0;
998 member = find_identifier(ident, ctype->symbol_list, &offset);
999 if (!member) {
1000 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1001 const char *name = "<unnamed>";
1002 int namelen = 9;
1003 if (ctype->ident) {
1004 name = ctype->ident->name;
1005 namelen = ctype->ident->len;
1007 warn(expr->pos, "no member '%s' in %s %.*s",
1008 show_ident(ident), type, namelen, name);
1009 return NULL;
1012 add = evaluate_offset(deref, offset);
1014 sym = alloc_symbol(expr->pos, SYM_NODE);
1015 sym->bit_size = member->bit_size;
1016 sym->array_size = member->array_size;
1017 sym->ctype = member->ctype;
1018 sym->ctype.modifiers = mod;
1019 sym->ctype.as = address_space;
1020 ctype = member->ctype.base_type;
1021 if (ctype->type == SYM_BITFIELD) {
1022 ctype = ctype->ctype.base_type;
1023 expr->type = EXPR_BITFIELD;
1024 expr->bitpos = member->bit_offset;
1025 expr->nrbits = member->fieldwidth;
1026 expr->address = add;
1027 } else {
1028 expr->type = EXPR_PREOP;
1029 expr->op = '*';
1030 expr->unop = add;
1033 expr->ctype = sym;
1034 return sym;
1037 static struct symbol *evaluate_sizeof(struct expression *expr)
1039 int size;
1041 if (expr->cast_type) {
1042 examine_symbol_type(expr->cast_type);
1043 size = expr->cast_type->bit_size;
1044 } else {
1045 if (!evaluate_expression(expr->cast_expression))
1046 return 0;
1047 size = expr->cast_expression->ctype->bit_size;
1049 if (size & 7) {
1050 warn(expr->pos, "cannot size expression");
1051 return 0;
1053 expr->type = EXPR_VALUE;
1054 expr->value = size >> 3;
1055 expr->ctype = size_t_ctype;
1056 return size_t_ctype;
1059 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1061 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1062 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1063 return clash != 0;
1066 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1068 struct expression *expr;
1069 struct symbol_list *argument_types = fn->arguments;
1070 struct symbol *argtype;
1071 int i = 1;
1073 PREPARE_PTR_LIST(argument_types, argtype);
1074 FOR_EACH_PTR (head, expr) {
1075 struct expression **p = THIS_ADDRESS(expr);
1076 struct symbol *ctype, *target;
1077 ctype = evaluate_expression(expr);
1079 if (!ctype)
1080 return 0;
1082 if (context_clash(f, ctype))
1083 warn(expr->pos, "argument %d used in wrong context", i);
1085 ctype = degenerate(expr, ctype, p);
1087 target = argtype;
1088 if (!target && ctype->bit_size < BITS_IN_INT)
1089 target = &int_ctype;
1090 if (target) {
1091 static char where[30];
1092 examine_symbol_type(target);
1093 sprintf(where, "argument %d", i);
1094 compatible_assignment_types(expr, target, p, ctype, where);
1097 i++;
1098 NEXT_PTR_LIST(argtype);
1099 } END_FOR_EACH_PTR;
1100 FINISH_PTR_LIST(argtype);
1101 return 1;
1104 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1105 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1107 struct expression *entry;
1108 int current = 0;
1109 int max = 0;
1111 FOR_EACH_PTR(expr->expr_list, entry) {
1112 struct expression **p = THIS_ADDRESS(entry);
1114 if (entry->type == EXPR_INDEX) {
1115 current = entry->idx_to;
1116 continue;
1118 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1119 current++;
1120 if (current > max)
1121 max = current;
1122 } END_FOR_EACH_PTR;
1123 return max;
1126 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1128 struct expression *entry;
1129 struct symbol *sym;
1131 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1132 FOR_EACH_PTR(expr->expr_list, entry) {
1133 struct expression **p = THIS_ADDRESS(entry);
1135 if (entry->type == EXPR_IDENTIFIER) {
1136 struct ident *ident = entry->expr_ident;
1137 /* We special-case the "already right place" case */
1138 if (sym && sym->ident == ident)
1139 continue;
1140 RESET_PTR_LIST(sym);
1141 for (;;) {
1142 if (!sym) {
1143 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1144 return 0;
1146 if (sym->ident == ident)
1147 break;
1148 NEXT_PTR_LIST(sym);
1150 continue;
1153 if (!sym) {
1154 warn(expr->pos, "too many initializers for struct/union");
1155 return 0;
1158 evaluate_initializer(sym, p, offset + sym->offset);
1160 NEXT_PTR_LIST(sym);
1161 } END_FOR_EACH_PTR;
1162 FINISH_PTR_LIST(sym);
1164 return 0;
1168 * Initializers are kind of like assignments. Except
1169 * they can be a hell of a lot more complex.
1171 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1173 struct expression *expr = *ep;
1176 * Simple non-structure/array initializers are the simple
1177 * case, and look (and parse) largely like assignments.
1179 if (expr->type != EXPR_INITIALIZER) {
1180 int size = 0;
1181 struct symbol *rtype = evaluate_expression(expr);
1182 if (rtype) {
1183 struct expression *pos;
1185 // FIXME! char array[] = "string" special case
1186 // should _not_ degenerate.
1187 rtype = degenerate(expr, rtype, ep);
1188 expr = *ep;
1189 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1190 /* strings are special: char arrays */
1191 if (rtype->type == SYM_ARRAY)
1192 size = get_expression_value(rtype->array_size);
1194 * Don't bother creating a position expression for
1195 * the simple initializer cases that don't need it.
1197 * We need a position if the initializer has a byte
1198 * offset, _or_ if we're initializing a bitfield.
1200 if (offset || ctype->fieldwidth) {
1201 pos = alloc_expression(expr->pos, EXPR_POS);
1202 pos->init_offset = offset;
1203 pos->init_sym = ctype;
1204 pos->init_expr = *ep;
1205 pos->ctype = expr->ctype;
1206 *ep = pos;
1209 return size;
1212 expr->ctype = ctype;
1213 if (ctype->type == SYM_NODE)
1214 ctype = ctype->ctype.base_type;
1216 switch (ctype->type) {
1217 case SYM_ARRAY:
1218 case SYM_PTR:
1219 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1220 case SYM_UNION:
1221 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1222 case SYM_STRUCT:
1223 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1224 default:
1225 break;
1227 warn(expr->pos, "unexpected compound initializer");
1228 return 0;
1231 static struct symbol *evaluate_cast(struct expression *expr)
1233 struct expression *target = expr->cast_expression;
1234 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1236 expr->ctype = ctype;
1237 expr->cast_type = ctype;
1240 * Special case: a cast can be followed by an
1241 * initializer, in which case we need to pass
1242 * the type value down to that initializer rather
1243 * than trying to evaluate it as an expression
1245 * A more complex case is when the initializer is
1246 * dereferenced as part of a post-fix expression.
1247 * We need to produce an expression that can be dereferenced.
1249 if (target->type == EXPR_INITIALIZER) {
1250 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
1251 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1253 sym->ctype.base_type = ctype;
1254 sym->initializer = expr->cast_expression;
1255 evaluate_symbol(sym);
1257 addr->ctype = &ptr_ctype;
1258 addr->symbol = sym;
1260 expr->type = EXPR_PREOP;
1261 expr->op = '*';
1262 expr->unop = addr;
1263 expr->ctype = ctype;
1264 return ctype;
1267 evaluate_expression(target);
1270 * Casts of constant values are special: they
1271 * can be NULL, and thus need to be simplified
1272 * early.
1274 if (target->type == EXPR_VALUE)
1275 cast_value(expr, ctype, target, target->ctype);
1277 return ctype;
1281 * Evaluate a call expression with a symbol. This
1282 * should expand inline functions, and evaluate
1283 * builtins.
1285 static int evaluate_symbol_call(struct expression *expr)
1287 struct expression *fn = expr->fn;
1288 struct symbol *ctype = fn->ctype;
1290 if (fn->type != EXPR_PREOP)
1291 return 0;
1293 if (ctype->op && ctype->op->evaluate)
1294 return ctype->op->evaluate(expr);
1296 if (ctype->ctype.modifiers & MOD_INLINE) {
1297 int ret;
1298 struct symbol *curr = current_fn;
1299 unsigned long context = current_context;
1300 unsigned long mask = current_contextmask;
1302 current_context |= ctype->ctype.context;
1303 current_contextmask |= ctype->ctype.contextmask;
1304 current_fn = ctype->ctype.base_type;
1305 ret = inline_function(expr, ctype);
1307 /* restore the old function context */
1308 current_fn = curr;
1309 current_context = context;
1310 current_contextmask = mask;
1311 return ret;
1314 return 0;
1317 static struct symbol *evaluate_call(struct expression *expr)
1319 int args, fnargs;
1320 struct symbol *ctype, *sym;
1321 struct expression *fn = expr->fn;
1322 struct expression_list *arglist = expr->args;
1324 if (!evaluate_expression(fn))
1325 return NULL;
1326 sym = ctype = fn->ctype;
1327 if (ctype->type == SYM_NODE)
1328 ctype = ctype->ctype.base_type;
1329 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1330 ctype = ctype->ctype.base_type;
1331 if (!evaluate_arguments(sym, ctype, arglist))
1332 return NULL;
1333 if (sym->type == SYM_NODE) {
1334 if (evaluate_symbol_call(expr))
1335 return expr->ctype;
1337 if (sym->type == SYM_NODE) {
1338 if (evaluate_symbol_call(expr))
1339 return expr->ctype;
1341 if (ctype->type != SYM_FN) {
1342 warn(expr->pos, "not a function %.*s",
1343 sym->ident->len, sym->ident->name);
1344 return NULL;
1346 args = expression_list_size(expr->args);
1347 fnargs = symbol_list_size(ctype->arguments);
1348 if (args < fnargs)
1349 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1350 if (args > fnargs && !ctype->variadic)
1351 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1352 expr->ctype = ctype->ctype.base_type;
1353 return expr->ctype;
1356 struct symbol *evaluate_expression(struct expression *expr)
1358 if (!expr)
1359 return NULL;
1360 if (expr->ctype)
1361 return expr->ctype;
1363 switch (expr->type) {
1364 case EXPR_VALUE:
1365 warn(expr->pos, "value expression without a type");
1366 return NULL;
1367 case EXPR_STRING:
1368 return evaluate_string(expr);
1369 case EXPR_SYMBOL:
1370 return evaluate_symbol_expression(expr);
1371 case EXPR_BINOP:
1372 if (!evaluate_expression(expr->left))
1373 return NULL;
1374 if (!evaluate_expression(expr->right))
1375 return NULL;
1376 return evaluate_binop(expr);
1377 case EXPR_LOGICAL:
1378 return evaluate_logical(expr);
1379 case EXPR_COMMA:
1380 if (!evaluate_expression(expr->left))
1381 return NULL;
1382 if (!evaluate_expression(expr->right))
1383 return NULL;
1384 return evaluate_comma(expr);
1385 case EXPR_COMPARE:
1386 if (!evaluate_expression(expr->left))
1387 return NULL;
1388 if (!evaluate_expression(expr->right))
1389 return NULL;
1390 return evaluate_compare(expr);
1391 case EXPR_ASSIGNMENT:
1392 if (!evaluate_expression(expr->left))
1393 return NULL;
1394 if (!evaluate_expression(expr->right))
1395 return NULL;
1396 return evaluate_assignment(expr);
1397 case EXPR_PREOP:
1398 if (!evaluate_expression(expr->unop))
1399 return NULL;
1400 return evaluate_preop(expr);
1401 case EXPR_POSTOP:
1402 if (!evaluate_expression(expr->unop))
1403 return NULL;
1404 return evaluate_postop(expr);
1405 case EXPR_CAST:
1406 return evaluate_cast(expr);
1407 case EXPR_SIZEOF:
1408 return evaluate_sizeof(expr);
1409 case EXPR_DEREF:
1410 return evaluate_member_dereference(expr);
1411 case EXPR_CALL:
1412 return evaluate_call(expr);
1413 case EXPR_BITFIELD:
1414 warn(expr->pos, "bitfield generated by parser");
1415 return NULL;
1416 case EXPR_CONDITIONAL:
1417 if (!evaluate_expression(expr->conditional))
1418 return NULL;
1419 if (!evaluate_expression(expr->cond_false))
1420 return NULL;
1421 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1422 return NULL;
1423 return evaluate_conditional(expr);
1424 case EXPR_STATEMENT:
1425 expr->ctype = evaluate_statement(expr->statement);
1426 return expr->ctype;
1428 case EXPR_LABEL:
1429 expr->ctype = &ptr_ctype;
1430 return &ptr_ctype;
1432 case EXPR_TYPE:
1433 /* Evaluate the type of the symbol .. */
1434 evaluate_symbol(expr->symbol);
1435 /* .. but the type of the _expression_ is a "type" */
1436 expr->ctype = &type_ctype;
1437 return &type_ctype;
1439 /* These can not exist as stand-alone expressions */
1440 case EXPR_INITIALIZER:
1441 case EXPR_IDENTIFIER:
1442 case EXPR_INDEX:
1443 case EXPR_POS:
1444 warn(expr->pos, "internal front-end error: initializer in expression");
1445 return NULL;
1447 return NULL;
1450 void check_duplicates(struct symbol *sym)
1452 struct symbol *next = sym;
1454 while ((next = next->same_symbol) != NULL) {
1455 const char *typediff;
1456 evaluate_symbol(next);
1457 typediff = type_difference(sym, next, 0, 0);
1458 if (typediff) {
1459 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1460 show_ident(sym->ident),
1461 input_streams[next->pos.stream].name, next->pos.line, typediff);
1462 return;
1467 struct symbol *evaluate_symbol(struct symbol *sym)
1469 struct symbol *base_type;
1471 if (!sym)
1472 return sym;
1474 sym = examine_symbol_type(sym);
1475 base_type = sym->ctype.base_type;
1476 if (!base_type)
1477 return NULL;
1479 /* Evaluate the initializers */
1480 if (sym->initializer) {
1481 int count = evaluate_initializer(sym, &sym->initializer, 0);
1482 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
1483 int bit_size = count * base_type->ctype.base_type->bit_size;
1484 base_type->array_size = alloc_const_expression(sym->pos, count);
1485 base_type->bit_size = bit_size;
1486 sym->array_size = base_type->array_size;
1487 sym->bit_size = bit_size;
1491 /* And finally, evaluate the body of the symbol too */
1492 if (base_type->type == SYM_FN) {
1493 struct symbol *s;
1495 FOR_EACH_PTR(base_type->arguments, s) {
1496 evaluate_symbol(s);
1497 } END_FOR_EACH_PTR;
1499 if (base_type->stmt) {
1500 current_fn = base_type;
1501 current_contextmask = sym->ctype.contextmask;
1502 current_context = sym->ctype.context;
1503 evaluate_statement(base_type->stmt);
1507 return base_type;
1510 struct symbol *evaluate_return_expression(struct statement *stmt)
1512 struct expression *expr = stmt->expression;
1513 struct symbol *ctype, *fntype;
1515 fntype = current_fn->ctype.base_type;
1516 if (!fntype || fntype == &void_ctype) {
1517 if (expr)
1518 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
1519 return NULL;
1522 if (!expr) {
1523 warn(stmt->pos, "return with no return value");
1524 return NULL;
1526 ctype = evaluate_expression(expr);
1527 if (!ctype)
1528 return NULL;
1529 ctype = degenerate(expr, ctype, &expr);
1530 expr->ctype = ctype;
1531 compatible_assignment_types(expr, fntype, &expr, ctype, "return expression");
1532 stmt->expression = expr;
1533 return NULL;
1536 static void evaluate_if_statement(struct statement *stmt)
1538 struct expression *expr = stmt->if_conditional;
1539 struct symbol *ctype;
1541 if (!expr)
1542 return;
1543 if (expr->type == EXPR_ASSIGNMENT)
1544 warn(expr->pos, "assignment expression in conditional");
1546 ctype = evaluate_expression(expr);
1547 if (!ctype)
1548 return;
1550 evaluate_statement(stmt->if_true);
1551 evaluate_statement(stmt->if_false);
1554 struct symbol *evaluate_statement(struct statement *stmt)
1556 if (!stmt)
1557 return NULL;
1559 switch (stmt->type) {
1560 case STMT_RETURN:
1561 return evaluate_return_expression(stmt);
1563 case STMT_EXPRESSION:
1564 return evaluate_expression(stmt->expression);
1566 case STMT_COMPOUND: {
1567 struct statement *s;
1568 struct symbol *type = NULL;
1569 struct symbol *sym;
1571 /* Evaluate each symbol in the compound statement */
1572 FOR_EACH_PTR(stmt->syms, sym) {
1573 evaluate_symbol(sym);
1574 } END_FOR_EACH_PTR;
1575 evaluate_symbol(stmt->ret);
1578 * Then, evaluate each statement, making the type of the
1579 * compound statement be the type of the last statement
1581 type = NULL;
1582 FOR_EACH_PTR(stmt->stmts, s) {
1583 type = evaluate_statement(s);
1584 } END_FOR_EACH_PTR;
1585 return type;
1587 case STMT_IF:
1588 evaluate_if_statement(stmt);
1589 return NULL;
1590 case STMT_ITERATOR:
1591 evaluate_expression(stmt->iterator_pre_condition);
1592 evaluate_expression(stmt->iterator_post_condition);
1593 evaluate_statement(stmt->iterator_pre_statement);
1594 evaluate_statement(stmt->iterator_statement);
1595 evaluate_statement(stmt->iterator_post_statement);
1596 return NULL;
1597 case STMT_SWITCH:
1598 evaluate_expression(stmt->switch_expression);
1599 evaluate_statement(stmt->switch_statement);
1600 return NULL;
1601 case STMT_CASE:
1602 evaluate_expression(stmt->case_expression);
1603 evaluate_expression(stmt->case_to);
1604 evaluate_statement(stmt->case_statement);
1605 return NULL;
1606 case STMT_LABEL:
1607 evaluate_statement(stmt->label_statement);
1608 return NULL;
1609 case STMT_GOTO:
1610 evaluate_expression(stmt->goto_expression);
1611 return NULL;
1612 case STMT_NONE:
1613 break;
1614 case STMT_ASM:
1615 /* FIXME! Do the asm parameter evaluation! */
1616 break;
1618 return NULL;