Fix pointer addition
[smatch.git] / evaluate.c
blob7f2cbd31c4896d473ed9fec7cf78e0ddc467960b
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "parse.h"
23 #include "token.h"
24 #include "symbol.h"
25 #include "target.h"
26 #include "expression.h"
28 static struct symbol *current_fn;
29 static int current_context, current_contextmask;
31 static struct symbol *evaluate_symbol_expression(struct expression *expr)
33 struct symbol *sym = expr->symbol;
34 struct symbol *base_type;
36 if (!sym) {
37 if (preprocessing) {
38 expr->ctype = &int_ctype;
39 return &int_ctype;
41 warn(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
42 return NULL;
45 examine_symbol_type(sym);
46 if ((sym->ctype.context ^ current_context) & (sym->ctype.contextmask & current_contextmask))
47 warn(expr->pos, "Using symbol '%s' in wrong context", show_ident(expr->symbol_name));
49 base_type = sym->ctype.base_type;
50 if (!base_type) {
51 warn(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
52 return NULL;
55 /* The type of a symbol is the symbol itself! */
56 expr->ctype = sym;
58 /* enum's can be turned into plain values */
59 if (sym->type != SYM_ENUM) {
60 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
61 addr->symbol = sym;
62 addr->symbol_name = expr->symbol_name;
63 addr->ctype = &ptr_ctype;
64 expr->type = EXPR_PREOP;
65 expr->op = '*';
66 expr->unop = addr;
67 return sym;
69 expr->type = EXPR_VALUE;
70 expr->value = sym->value;
71 expr->ctype = base_type;
72 return sym;
75 static struct symbol *evaluate_string(struct expression *expr)
77 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
78 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
79 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
80 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
81 unsigned int length = expr->string->length;
83 sym->array_size = alloc_const_expression(expr->pos, length);
84 sym->bit_size = BITS_IN_CHAR * length;
85 sym->ctype.alignment = 1;
86 sym->ctype.modifiers = MOD_STATIC;
87 sym->ctype.base_type = array;
88 sym->initializer = initstr;
90 initstr->ctype = sym;
91 initstr->string = expr->string;
93 array->array_size = sym->array_size;
94 array->bit_size = BITS_IN_CHAR * length;
95 array->ctype.alignment = 1;
96 array->ctype.modifiers = MOD_STATIC;
97 array->ctype.base_type = &char_ctype;
99 addr->symbol = sym;
100 addr->ctype = &ptr_ctype;
102 expr->type = EXPR_PREOP;
103 expr->op = '*';
104 expr->unop = addr;
105 expr->ctype = sym;
106 return sym;
109 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
111 unsigned long lmod, rmod, mod;
113 if (left == right)
114 return left;
116 if (left->bit_size > right->bit_size)
117 return left;
119 if (right->bit_size > left->bit_size)
120 return right;
122 /* Same size integers - promote to unsigned, promote to long */
123 lmod = left->ctype.modifiers;
124 rmod = right->ctype.modifiers;
125 mod = lmod | rmod;
126 if (mod == lmod)
127 return left;
128 if (mod == rmod)
129 return right;
130 return ctype_integer(mod);
133 static struct expression * cast_to(struct expression *old, struct symbol *type)
135 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
136 expr->ctype = type;
137 expr->cast_type = type;
138 expr->cast_expression = old;
139 return expr;
142 static int is_type_type(struct symbol *type)
144 return (type->ctype.modifiers & MOD_TYPE) != 0;
147 static int is_ptr_type(struct symbol *type)
149 if (type->type == SYM_NODE)
150 type = type->ctype.base_type;
151 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
154 static int is_int_type(struct symbol *type)
156 if (type->type == SYM_NODE)
157 type = type->ctype.base_type;
158 return type->ctype.base_type == &int_type;
161 static struct symbol *bad_expr_type(struct expression *expr)
163 warn(expr->pos, "incompatible types for operation");
164 return NULL;
167 static struct symbol * compatible_integer_binop(struct expression *expr, struct expression **lp, struct expression **rp)
169 struct expression *left = *lp, *right = *rp;
170 struct symbol *ltype = left->ctype, *rtype = right->ctype;
172 if (ltype->type == SYM_NODE)
173 ltype = ltype->ctype.base_type;
174 if (rtype->type == SYM_NODE)
175 rtype = rtype->ctype.base_type;
176 /* Integer promotion? */
177 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
178 ltype = &int_ctype;
179 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
180 rtype = &int_ctype;
181 if (is_int_type(ltype) && is_int_type(rtype)) {
182 struct symbol *ctype = bigger_int_type(ltype, rtype);
184 /* Don't bother promoting same-size entities, it only adds clutter */
185 if (ltype->bit_size != ctype->bit_size)
186 *lp = cast_to(left, ctype);
187 if (rtype->bit_size != ctype->bit_size)
188 *rp = cast_to(right, ctype);
189 return ctype;
191 return NULL;
194 static struct symbol *evaluate_int_binop(struct expression *expr)
196 struct symbol *ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
197 if (ctype) {
198 expr->ctype = ctype;
199 return ctype;
201 return bad_expr_type(expr);
204 static inline int lvalue_expression(struct expression *expr)
206 while (expr->type == EXPR_CAST)
207 expr = expr->cast_expression;
208 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
211 /* Arrays degenerate into pointers on pointer arithmetic */
212 static struct symbol *degenerate(struct expression *expr, struct symbol *ctype, struct expression **ptr_p)
214 struct symbol *base = ctype;
216 if (ctype->type == SYM_NODE)
217 base = ctype->ctype.base_type;
218 if (base->type == SYM_ARRAY || base->type == SYM_FN) {
219 struct symbol *sym = alloc_symbol(expr->pos, SYM_PTR);
220 struct expression *n = alloc_expression(expr->pos, 0);
221 struct expression *ptr;
223 merge_type(sym, ctype);
224 if (base->type == SYM_FN)
225 base = ctype;
226 merge_type(sym, base);
227 sym->bit_size = BITS_IN_POINTER;
228 ctype = sym;
230 ptr = *ptr_p;
231 *n = *ptr->unop;
232 n->ctype = ctype;
233 *ptr_p = n;
236 * This all really assumes that we got the degenerate
237 * array as an lvalue (ie a dereference). If that
238 * is not the case, then holler - because we've screwed
239 * up.
241 if (!lvalue_expression(ptr))
242 warn(ptr->pos, "internal error: strange degenerate array case");
244 return ctype;
247 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
249 struct symbol *ctype;
250 struct symbol *ptr_type = ptr->ctype;
251 struct symbol *i_type = i->ctype;
252 int bit_size;
254 if (i_type->type == SYM_NODE)
255 i_type = i_type->ctype.base_type;
256 if (ptr_type->type == SYM_NODE)
257 ptr_type = ptr_type->ctype.base_type;
259 if (i_type->type == SYM_ENUM)
260 i_type = &int_ctype;
261 if (!is_int_type(i_type))
262 return bad_expr_type(expr);
264 ctype = ptr->ctype;
265 examine_symbol_type(ctype);
267 ctype = degenerate(expr, ctype, &ptr);
268 bit_size = ctype->ctype.base_type->bit_size;
270 /* Special case: adding zero commonly happens as a result of 'array[0]' */
271 if (i->type == EXPR_VALUE && !i->value) {
272 *expr = *ptr;
273 } else if (bit_size > BITS_IN_CHAR) {
274 struct expression *add = expr;
275 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
276 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
278 val->ctype = size_t_ctype;
279 val->value = bit_size >> 3;
281 mul->op = '*';
282 mul->ctype = size_t_ctype;
283 mul->left = i;
284 mul->right = val;
286 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
287 add->left = ptr;
288 add->right = mul;
291 expr->ctype = ctype;
292 return ctype;
295 static struct symbol *evaluate_add(struct expression *expr)
297 struct expression *left = expr->left, *right = expr->right;
298 struct symbol *ltype = left->ctype, *rtype = right->ctype;
300 if (is_ptr_type(ltype))
301 return evaluate_ptr_add(expr, left, right);
303 if (is_ptr_type(rtype))
304 return evaluate_ptr_add(expr, right, left);
306 // FIXME! FP promotion
307 return evaluate_int_binop(expr);
310 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
311 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED)
313 const char * type_difference(struct symbol *target, struct symbol *source,
314 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
316 for (;;) {
317 unsigned long mod1, mod2, diff;
318 unsigned long as1, as2;
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 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
498 static struct symbol *evaluate_conditional(struct expression *expr)
500 struct symbol *ctype;
502 if (!expr)
503 return NULL;
505 if (expr->type == EXPR_ASSIGNMENT)
506 warn(expr->pos, "assignment expression in conditional");
508 ctype = evaluate_expression(expr);
509 if (ctype && is_safe_type(ctype))
510 warn(expr->pos, "testing a 'safe expression'");
512 return ctype;
515 static struct symbol *evaluate_logical(struct expression *expr)
517 if (!evaluate_conditional(expr->left))
518 return NULL;
519 if (!evaluate_conditional(expr->right))
520 return NULL;
522 expr->ctype = &bool_ctype;
523 return &bool_ctype;
526 static struct symbol *evaluate_arithmetic(struct expression *expr)
528 // FIXME! Floating-point promotion!
529 return evaluate_int_binop(expr);
532 static struct symbol *evaluate_binop(struct expression *expr)
534 switch (expr->op) {
535 // addition can take ptr+int, fp and int
536 case '+':
537 return evaluate_add(expr);
539 // subtraction can take ptr-ptr, fp and int
540 case '-':
541 return evaluate_sub(expr);
543 // Arithmetic operations can take fp and int
544 case '*': case '/': case '%':
545 return evaluate_arithmetic(expr);
547 // The rest are integer operations (bitops)
548 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
549 // '&', '^', '|'
550 default:
551 return evaluate_int_binop(expr);
555 static struct symbol *evaluate_comma(struct expression *expr)
557 expr->ctype = expr->right->ctype;
558 return expr->ctype;
561 static struct symbol *evaluate_compare(struct expression *expr)
563 struct expression *left = expr->left, *right = expr->right;
564 struct symbol *ltype = left->ctype, *rtype = right->ctype;
565 struct symbol *ctype;
567 /* Type types? */
568 if (is_type_type(ltype) && is_type_type(rtype)) {
569 expr->ctype = &bool_ctype;
570 return &bool_ctype;
573 if (is_safe_type(ltype) || is_safe_type(rtype))
574 warn(expr->pos, "testing a 'safe expression'");
576 /* Pointer types? */
577 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
578 expr->ctype = &bool_ctype;
579 // FIXME! Check the types for compatibility
580 return &bool_ctype;
583 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
584 if (ctype) {
585 expr->ctype = &bool_ctype;
586 return &bool_ctype;
589 return bad_expr_type(expr);
592 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
594 /* Integer promotion? */
595 if (ltype->type == SYM_NODE)
596 ltype = ltype->ctype.base_type;
597 if (rtype->type == SYM_NODE)
598 rtype = rtype->ctype.base_type;
599 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
600 ltype = &int_ctype;
601 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
602 rtype = &int_ctype;
603 return (is_int_type(ltype) && is_int_type(rtype));
606 static int is_null_ptr(struct expression *expr)
608 return (expr->type == EXPR_VALUE &&
609 expr->value == 0);
613 * FIXME!! This should do casts, array degeneration etc..
615 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
617 struct symbol *ltype = left->ctype, *rtype = right->ctype;
619 if (ltype->type == SYM_NODE)
620 ltype = ltype->ctype.base_type;
622 if (ltype->type == SYM_PTR) {
623 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
624 return ltype;
627 if (rtype->type == SYM_NODE)
628 rtype = rtype->ctype.base_type;
630 if (rtype->type == SYM_PTR) {
631 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
632 return rtype;
634 return NULL;
637 static struct symbol *do_degenerate(struct expression **ep)
639 struct expression *expr = *ep;
640 return degenerate(expr, expr->ctype, ep);
643 static struct symbol * evaluate_conditional_expression(struct expression *expr)
645 struct expression *cond, *true, *false;
646 struct symbol *ctype, *ltype, *rtype;
647 const char * typediff;
649 ctype = do_degenerate(&expr->conditional);
650 cond = expr->conditional;
652 ltype = ctype;
653 true = cond;
654 if (expr->cond_true) {
655 ltype = do_degenerate(&expr->cond_true);
656 true = expr->cond_true;
659 rtype = do_degenerate(&expr->cond_false);
660 false = expr->cond_false;
662 ctype = ltype;
663 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
664 if (typediff) {
665 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
666 if (!ctype) {
667 ctype = compatible_ptr_type(true, expr->cond_false);
668 if (!ctype) {
669 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
670 return NULL;
675 expr->ctype = ctype;
676 return ctype;
679 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
680 struct expression **rp, struct symbol *source, const char *where)
682 const char *typediff;
683 struct symbol *t;
684 int target_as;
686 /* It's ok if the target is more volatile or const than the source */
687 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
688 if (!typediff)
689 return 1;
691 if (compatible_integer_types(target, source)) {
692 if (target->bit_size != source->bit_size)
693 *rp = cast_to(*rp, target);
694 return 1;
697 /* Pointer destination? */
698 t = target;
699 target_as = t->ctype.as;
700 if (t->type == SYM_NODE) {
701 t = t->ctype.base_type;
702 target_as |= t->ctype.as;
704 if (t->type == SYM_PTR || t->type == SYM_FN) {
705 struct expression *right = *rp;
706 struct symbol *s = source;
707 int source_as;
709 // NULL pointer is always ok
710 if (right->type == EXPR_VALUE && !right->value)
711 return 1;
713 /* "void *" matches anything as long as the address space is ok */
714 source_as = s->ctype.as;
715 if (s->type == SYM_NODE) {
716 s = s->ctype.base_type;
717 source_as |= s->ctype.as;
719 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
720 s = s->ctype.base_type;
721 t = t->ctype.base_type;
722 if (s == &void_ctype || t == &void_ctype)
723 return 1;
727 // FIXME!! Cast it?
728 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
729 warn(expr->pos, " expected %s", show_typename(target));
730 warn(expr->pos, " got %s", show_typename(source));
731 return 0;
735 * FIXME!! This is wrong from a double evaluation standpoint. We can't
736 * just expand the expression twice, that would make any side effects
737 * happen twice too.
739 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
741 int op = expr->op;
742 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
743 static const int op_trans[] = {
744 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
745 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
746 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
747 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
748 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
749 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
750 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
751 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
752 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
753 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
756 subexpr->left = left;
757 subexpr->right = right;
758 subexpr->op = op_trans[op - SPECIAL_BASE];
759 expr->op = '=';
760 expr->right = subexpr;
761 return evaluate_binop(subexpr);
764 static struct symbol *evaluate_assignment(struct expression *expr)
766 struct expression *left = expr->left, *right = expr->right;
767 struct symbol *ltype, *rtype;
769 ltype = left->ctype;
770 rtype = right->ctype;
771 if (expr->op != '=') {
772 rtype = evaluate_binop_assignment(expr, left, right);
773 if (!rtype)
774 return 0;
775 right = expr->right;
778 if (!lvalue_expression(left)) {
779 warn(expr->pos, "not an lvalue");
780 return NULL;
783 rtype = degenerate(right, rtype, &expr->right);
785 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
786 return 0;
788 if (ltype->type == SYM_NODE)
789 ltype->ctype.modifiers |= MOD_ASSIGNED;
791 expr->ctype = ltype;
792 return ltype;
795 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym)
797 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
799 ptr->ctype.base_type = sym;
800 ptr->bit_size = BITS_IN_POINTER;
802 sym->ctype.modifiers |= MOD_ADDRESSABLE;
803 if (sym->ctype.modifiers & MOD_REGISTER) {
804 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
805 sym->ctype.modifiers &= ~MOD_REGISTER;
807 if (sym->type == SYM_NODE)
808 sym = sym->ctype.base_type;
809 if (sym->type == SYM_ARRAY)
810 sym = sym->ctype.base_type;
811 ptr->ctype.base_type = sym;
813 return ptr;
816 static struct symbol *evaluate_addressof(struct expression *expr)
818 struct expression *op = expr->unop;
819 struct symbol *ctype;
821 if (op->op != '*' || op->type != EXPR_PREOP) {
822 warn(expr->pos, "not addressable");
823 return NULL;
826 ctype = create_pointer(expr, op->ctype);
828 *expr = *op->unop;
830 expr->ctype = ctype;
831 return ctype;
835 static struct symbol *evaluate_dereference(struct expression *expr)
837 struct expression *op = expr->unop;
838 struct symbol *ctype = op->ctype, *sym;
840 sym = alloc_symbol(expr->pos, SYM_NODE);
841 sym->ctype = ctype->ctype;
842 if (ctype->type == SYM_NODE) {
843 ctype = ctype->ctype.base_type;
844 merge_type(sym, ctype);
846 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
847 warn(expr->pos, "cannot derefence this type");
848 return 0;
851 ctype = ctype->ctype.base_type;
852 examine_symbol_type(ctype);
853 if (!ctype) {
854 warn(expr->pos, "undefined type");
855 return NULL;
858 sym->bit_size = ctype->bit_size;
859 sym->array_size = ctype->array_size;
861 /* Simplify: *&(expr) => (expr) */
862 if (op->type == EXPR_PREOP && op->op == '&') {
863 *expr = *op->unop;
866 expr->ctype = sym;
867 return sym;
871 * Unary post-ops: x++ and x--
873 static struct symbol *evaluate_postop(struct expression *expr)
875 struct expression *op = expr->unop;
876 struct symbol *ctype = op->ctype;
878 if (!lvalue_expression(expr->unop)) {
879 warn(expr->pos, "need lvalue expression for ++/--");
880 return NULL;
883 if (ctype->type == SYM_NODE)
884 ctype->ctype.modifiers |= MOD_ASSIGNED;
886 expr->ctype = ctype;
887 return ctype;
890 static struct symbol *evaluate_preop(struct expression *expr)
892 struct symbol *ctype = expr->unop->ctype;
894 switch (expr->op) {
895 case '(':
896 case '+':
897 *expr = *expr->unop;
898 return ctype;
900 case '*':
901 return evaluate_dereference(expr);
903 case '&':
904 return evaluate_addressof(expr);
906 case SPECIAL_INCREMENT:
907 case SPECIAL_DECREMENT:
909 * From a type evaluation standpoint the pre-ops are
910 * the same as the postops
912 return evaluate_postop(expr);
914 case '!':
915 if (is_safe_type(ctype))
916 warn(expr->pos, "testing a 'safe expression'");
917 ctype = &bool_ctype;
918 break;
920 default:
921 break;
923 expr->ctype = ctype;
924 return &bool_ctype;
927 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
929 struct ptr_list *head = (struct ptr_list *)_list;
930 struct ptr_list *list = head;
932 if (!head)
933 return NULL;
934 do {
935 int i;
936 for (i = 0; i < list->nr; i++) {
937 struct symbol *sym = (struct symbol *) list->list[i];
938 if (sym->ident) {
939 if (sym->ident != ident)
940 continue;
941 *offset = sym->offset;
942 return sym;
943 } else {
944 struct symbol *ctype = sym->ctype.base_type;
945 struct symbol *sub;
946 if (!ctype)
947 continue;
948 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
949 continue;
950 sub = find_identifier(ident, ctype->symbol_list, offset);
951 if (!sub)
952 continue;
953 *offset += sym->offset;
954 return sub;
957 } while ((list = list->next) != head);
958 return NULL;
961 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset, struct symbol *ctype)
963 struct expression *add;
965 add = expr;
966 if (offset) {
967 /* Create a new add-expression */
968 add = alloc_expression(expr->pos, EXPR_BINOP);
969 add->op = '+';
970 add->left = expr;
971 add->right = alloc_expression(expr->pos, EXPR_VALUE);
972 add->right->ctype = &int_ctype;
973 add->right->value = offset;
976 add->ctype = ctype;
977 return add;
980 /* structure/union dereference */
981 static struct symbol *evaluate_member_dereference(struct expression *expr)
983 int offset;
984 struct symbol *ctype, *member;
985 struct expression *deref = expr->deref, *add;
986 struct ident *ident = expr->member;
987 unsigned int mod;
988 int address_space;
990 if (!evaluate_expression(deref))
991 return NULL;
992 if (!ident) {
993 warn(expr->pos, "bad member name");
994 return NULL;
997 ctype = deref->ctype;
998 address_space = ctype->ctype.as;
999 mod = ctype->ctype.modifiers;
1000 if (ctype->type == SYM_NODE) {
1001 ctype = ctype->ctype.base_type;
1002 address_space |= ctype->ctype.as;
1003 mod |= ctype->ctype.modifiers;
1005 if (expr->op == SPECIAL_DEREFERENCE) {
1006 /* Arrays will degenerate into pointers for '->' */
1007 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
1008 warn(expr->pos, "expected a pointer to a struct/union");
1009 return NULL;
1011 mod = ctype->ctype.modifiers;
1012 address_space = ctype->ctype.as;
1013 ctype = ctype->ctype.base_type;
1014 if (ctype->type == SYM_NODE) {
1015 mod |= ctype->ctype.modifiers;
1016 address_space |= ctype->ctype.as;
1017 ctype = ctype->ctype.base_type;
1019 } else {
1020 if (!lvalue_expression(deref)) {
1021 warn(deref->pos, "expected lvalue for member dereference");
1022 return NULL;
1024 deref = deref->unop;
1025 expr->deref = deref;
1027 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1028 warn(expr->pos, "expected structure or union");
1029 return NULL;
1031 offset = 0;
1032 member = find_identifier(ident, ctype->symbol_list, &offset);
1033 if (!member) {
1034 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1035 const char *name = "<unnamed>";
1036 int namelen = 9;
1037 if (ctype->ident) {
1038 name = ctype->ident->name;
1039 namelen = ctype->ident->len;
1041 warn(expr->pos, "no member '%s' in %s %.*s",
1042 show_ident(ident), type, namelen, name);
1043 return NULL;
1046 ctype = create_pointer(deref, member);
1047 ctype->ctype.modifiers = mod;
1048 ctype->ctype.as = address_space;
1049 add = evaluate_offset(deref, offset, ctype);
1051 ctype = member->ctype.base_type;
1052 if (ctype->type == SYM_BITFIELD) {
1053 ctype = ctype->ctype.base_type;
1054 expr->type = EXPR_BITFIELD;
1055 expr->bitpos = member->bit_offset;
1056 expr->nrbits = member->fieldwidth;
1057 expr->address = add;
1058 } else {
1059 expr->type = EXPR_PREOP;
1060 expr->op = '*';
1061 expr->unop = add;
1064 expr->ctype = member;
1065 return member;
1068 static struct symbol *evaluate_sizeof(struct expression *expr)
1070 int size;
1072 if (expr->cast_type) {
1073 examine_symbol_type(expr->cast_type);
1074 size = expr->cast_type->bit_size;
1075 } else {
1076 if (!evaluate_expression(expr->cast_expression))
1077 return 0;
1078 size = expr->cast_expression->ctype->bit_size;
1080 if (size & 7) {
1081 warn(expr->pos, "cannot size expression");
1082 return 0;
1084 expr->type = EXPR_VALUE;
1085 expr->value = size >> 3;
1086 expr->ctype = size_t_ctype;
1087 return size_t_ctype;
1090 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1092 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1093 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1094 return clash != 0;
1097 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1099 struct expression *expr;
1100 struct symbol_list *argument_types = fn->arguments;
1101 struct symbol *argtype;
1102 int i = 1;
1104 PREPARE_PTR_LIST(argument_types, argtype);
1105 FOR_EACH_PTR (head, expr) {
1106 struct expression **p = THIS_ADDRESS(expr);
1107 struct symbol *ctype, *target;
1108 ctype = evaluate_expression(expr);
1110 if (!ctype)
1111 return 0;
1113 if (context_clash(f, ctype))
1114 warn(expr->pos, "argument %d used in wrong context", i);
1116 ctype = degenerate(expr, ctype, p);
1118 target = argtype;
1119 if (!target && ctype->bit_size < BITS_IN_INT)
1120 target = &int_ctype;
1121 if (target) {
1122 static char where[30];
1123 examine_symbol_type(target);
1124 sprintf(where, "argument %d", i);
1125 compatible_assignment_types(expr, target, p, ctype, where);
1128 i++;
1129 NEXT_PTR_LIST(argtype);
1130 } END_FOR_EACH_PTR;
1131 FINISH_PTR_LIST(argtype);
1132 return 1;
1135 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1136 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1138 struct expression *entry;
1139 int current = 0;
1140 int max = 0;
1142 FOR_EACH_PTR(expr->expr_list, entry) {
1143 struct expression **p = THIS_ADDRESS(entry);
1145 if (entry->type == EXPR_INDEX) {
1146 current = entry->idx_to;
1147 continue;
1149 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1150 current++;
1151 if (current > max)
1152 max = current;
1153 } END_FOR_EACH_PTR;
1154 return max;
1157 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1159 struct expression *entry;
1160 struct symbol *sym;
1162 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1163 FOR_EACH_PTR(expr->expr_list, entry) {
1164 struct expression **p = THIS_ADDRESS(entry);
1166 if (entry->type == EXPR_IDENTIFIER) {
1167 struct ident *ident = entry->expr_ident;
1168 /* We special-case the "already right place" case */
1169 if (sym && sym->ident == ident)
1170 continue;
1171 RESET_PTR_LIST(sym);
1172 for (;;) {
1173 if (!sym) {
1174 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1175 return 0;
1177 if (sym->ident == ident)
1178 break;
1179 NEXT_PTR_LIST(sym);
1181 continue;
1184 if (!sym) {
1185 warn(expr->pos, "too many initializers for struct/union");
1186 return 0;
1189 evaluate_initializer(sym, p, offset + sym->offset);
1191 NEXT_PTR_LIST(sym);
1192 } END_FOR_EACH_PTR;
1193 FINISH_PTR_LIST(sym);
1195 return 0;
1199 * Initializers are kind of like assignments. Except
1200 * they can be a hell of a lot more complex.
1202 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1204 struct expression *expr = *ep;
1207 * Simple non-structure/array initializers are the simple
1208 * case, and look (and parse) largely like assignments.
1210 if (expr->type != EXPR_INITIALIZER) {
1211 int size = 0;
1212 struct symbol *rtype = evaluate_expression(expr);
1213 if (rtype) {
1214 struct expression *pos;
1216 // FIXME! char array[] = "string" special case
1217 // should _not_ degenerate.
1218 rtype = degenerate(expr, rtype, ep);
1219 expr = *ep;
1220 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1221 /* strings are special: char arrays */
1222 if (rtype->type == SYM_ARRAY)
1223 size = get_expression_value(rtype->array_size);
1225 * Don't bother creating a position expression for
1226 * the simple initializer cases that don't need it.
1228 * We need a position if the initializer has a byte
1229 * offset, _or_ if we're initializing a bitfield.
1231 if (offset || ctype->fieldwidth) {
1232 pos = alloc_expression(expr->pos, EXPR_POS);
1233 pos->init_offset = offset;
1234 pos->init_sym = ctype;
1235 pos->init_expr = *ep;
1236 pos->ctype = expr->ctype;
1237 *ep = pos;
1240 return size;
1243 expr->ctype = ctype;
1244 if (ctype->type == SYM_NODE)
1245 ctype = ctype->ctype.base_type;
1247 switch (ctype->type) {
1248 case SYM_ARRAY:
1249 case SYM_PTR:
1250 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1251 case SYM_UNION:
1252 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1253 case SYM_STRUCT:
1254 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1255 default:
1256 break;
1258 warn(expr->pos, "unexpected compound initializer");
1259 return 0;
1262 static struct symbol *evaluate_cast(struct expression *expr)
1264 struct expression *target = expr->cast_expression;
1265 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1267 expr->ctype = ctype;
1268 expr->cast_type = ctype;
1271 * Special case: a cast can be followed by an
1272 * initializer, in which case we need to pass
1273 * the type value down to that initializer rather
1274 * than trying to evaluate it as an expression
1276 * A more complex case is when the initializer is
1277 * dereferenced as part of a post-fix expression.
1278 * We need to produce an expression that can be dereferenced.
1280 if (target->type == EXPR_INITIALIZER) {
1281 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
1282 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1284 sym->ctype.base_type = ctype;
1285 sym->initializer = expr->cast_expression;
1286 evaluate_symbol(sym);
1288 addr->ctype = &ptr_ctype;
1289 addr->symbol = sym;
1291 expr->type = EXPR_PREOP;
1292 expr->op = '*';
1293 expr->unop = addr;
1294 expr->ctype = ctype;
1295 return ctype;
1298 evaluate_expression(target);
1301 * Casts of constant values are special: they
1302 * can be NULL, and thus need to be simplified
1303 * early.
1305 if (target->type == EXPR_VALUE)
1306 cast_value(expr, ctype, target, target->ctype);
1308 return ctype;
1312 * Evaluate a call expression with a symbol. This
1313 * should expand inline functions, and evaluate
1314 * builtins.
1316 static int evaluate_symbol_call(struct expression *expr)
1318 struct expression *fn = expr->fn;
1319 struct symbol *ctype = fn->ctype;
1321 if (fn->type != EXPR_PREOP)
1322 return 0;
1324 if (ctype->op && ctype->op->evaluate)
1325 return ctype->op->evaluate(expr);
1327 if (ctype->ctype.modifiers & MOD_INLINE) {
1328 int ret;
1329 struct symbol *curr = current_fn;
1330 unsigned long context = current_context;
1331 unsigned long mask = current_contextmask;
1333 current_context |= ctype->ctype.context;
1334 current_contextmask |= ctype->ctype.contextmask;
1335 current_fn = ctype->ctype.base_type;
1336 ret = inline_function(expr, ctype);
1338 /* restore the old function context */
1339 current_fn = curr;
1340 current_context = context;
1341 current_contextmask = mask;
1342 return ret;
1345 return 0;
1348 static struct symbol *evaluate_call(struct expression *expr)
1350 int args, fnargs;
1351 struct symbol *ctype, *sym;
1352 struct expression *fn = expr->fn;
1353 struct expression_list *arglist = expr->args;
1355 if (!evaluate_expression(fn))
1356 return NULL;
1357 sym = ctype = fn->ctype;
1358 if (ctype->type == SYM_NODE)
1359 ctype = ctype->ctype.base_type;
1360 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1361 ctype = ctype->ctype.base_type;
1362 if (!evaluate_arguments(sym, ctype, arglist))
1363 return NULL;
1364 if (sym->type == SYM_NODE) {
1365 if (evaluate_symbol_call(expr))
1366 return expr->ctype;
1368 if (sym->type == SYM_NODE) {
1369 if (evaluate_symbol_call(expr))
1370 return expr->ctype;
1372 if (ctype->type != SYM_FN) {
1373 warn(expr->pos, "not a function %.*s",
1374 sym->ident->len, sym->ident->name);
1375 return NULL;
1377 args = expression_list_size(expr->args);
1378 fnargs = symbol_list_size(ctype->arguments);
1379 if (args < fnargs)
1380 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1381 if (args > fnargs && !ctype->variadic)
1382 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1383 expr->ctype = ctype->ctype.base_type;
1384 return expr->ctype;
1387 struct symbol *evaluate_expression(struct expression *expr)
1389 if (!expr)
1390 return NULL;
1391 if (expr->ctype)
1392 return expr->ctype;
1394 switch (expr->type) {
1395 case EXPR_VALUE:
1396 warn(expr->pos, "value expression without a type");
1397 return NULL;
1398 case EXPR_STRING:
1399 return evaluate_string(expr);
1400 case EXPR_SYMBOL:
1401 return evaluate_symbol_expression(expr);
1402 case EXPR_BINOP:
1403 if (!evaluate_expression(expr->left))
1404 return NULL;
1405 if (!evaluate_expression(expr->right))
1406 return NULL;
1407 return evaluate_binop(expr);
1408 case EXPR_LOGICAL:
1409 return evaluate_logical(expr);
1410 case EXPR_COMMA:
1411 if (!evaluate_expression(expr->left))
1412 return NULL;
1413 if (!evaluate_expression(expr->right))
1414 return NULL;
1415 return evaluate_comma(expr);
1416 case EXPR_COMPARE:
1417 if (!evaluate_expression(expr->left))
1418 return NULL;
1419 if (!evaluate_expression(expr->right))
1420 return NULL;
1421 return evaluate_compare(expr);
1422 case EXPR_ASSIGNMENT:
1423 if (!evaluate_expression(expr->left))
1424 return NULL;
1425 if (!evaluate_expression(expr->right))
1426 return NULL;
1427 return evaluate_assignment(expr);
1428 case EXPR_PREOP:
1429 if (!evaluate_expression(expr->unop))
1430 return NULL;
1431 return evaluate_preop(expr);
1432 case EXPR_POSTOP:
1433 if (!evaluate_expression(expr->unop))
1434 return NULL;
1435 return evaluate_postop(expr);
1436 case EXPR_CAST:
1437 return evaluate_cast(expr);
1438 case EXPR_SIZEOF:
1439 return evaluate_sizeof(expr);
1440 case EXPR_DEREF:
1441 return evaluate_member_dereference(expr);
1442 case EXPR_CALL:
1443 return evaluate_call(expr);
1444 case EXPR_BITFIELD:
1445 warn(expr->pos, "bitfield generated by parser");
1446 return NULL;
1447 case EXPR_CONDITIONAL:
1448 if (!evaluate_conditional(expr->conditional))
1449 return NULL;
1450 if (!evaluate_expression(expr->cond_false))
1451 return NULL;
1452 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1453 return NULL;
1454 return evaluate_conditional_expression(expr);
1455 case EXPR_STATEMENT:
1456 expr->ctype = evaluate_statement(expr->statement);
1457 return expr->ctype;
1459 case EXPR_LABEL:
1460 expr->ctype = &ptr_ctype;
1461 return &ptr_ctype;
1463 case EXPR_TYPE:
1464 /* Evaluate the type of the symbol .. */
1465 evaluate_symbol(expr->symbol);
1466 /* .. but the type of the _expression_ is a "type" */
1467 expr->ctype = &type_ctype;
1468 return &type_ctype;
1470 /* These can not exist as stand-alone expressions */
1471 case EXPR_INITIALIZER:
1472 case EXPR_IDENTIFIER:
1473 case EXPR_INDEX:
1474 case EXPR_POS:
1475 warn(expr->pos, "internal front-end error: initializer in expression");
1476 return NULL;
1478 return NULL;
1481 void check_duplicates(struct symbol *sym)
1483 struct symbol *next = sym;
1485 while ((next = next->same_symbol) != NULL) {
1486 const char *typediff;
1487 evaluate_symbol(next);
1488 typediff = type_difference(sym, next, 0, 0);
1489 if (typediff) {
1490 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1491 show_ident(sym->ident),
1492 input_streams[next->pos.stream].name, next->pos.line, typediff);
1493 return;
1498 struct symbol *evaluate_symbol(struct symbol *sym)
1500 struct symbol *base_type;
1502 if (!sym)
1503 return sym;
1505 sym = examine_symbol_type(sym);
1506 base_type = sym->ctype.base_type;
1507 if (!base_type)
1508 return NULL;
1510 /* Evaluate the initializers */
1511 if (sym->initializer) {
1512 int count = evaluate_initializer(sym, &sym->initializer, 0);
1513 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
1514 int bit_size = count * base_type->ctype.base_type->bit_size;
1515 base_type->array_size = alloc_const_expression(sym->pos, count);
1516 base_type->bit_size = bit_size;
1517 sym->array_size = base_type->array_size;
1518 sym->bit_size = bit_size;
1522 /* And finally, evaluate the body of the symbol too */
1523 if (base_type->type == SYM_FN) {
1524 struct symbol *s;
1526 FOR_EACH_PTR(base_type->arguments, s) {
1527 evaluate_symbol(s);
1528 } END_FOR_EACH_PTR;
1530 if (base_type->stmt) {
1531 current_fn = base_type;
1532 current_contextmask = sym->ctype.contextmask;
1533 current_context = sym->ctype.context;
1534 evaluate_statement(base_type->stmt);
1538 return base_type;
1541 struct symbol *evaluate_return_expression(struct statement *stmt)
1543 struct expression *expr = stmt->expression;
1544 struct symbol *ctype, *fntype;
1546 fntype = current_fn->ctype.base_type;
1547 if (!fntype || fntype == &void_ctype) {
1548 if (expr)
1549 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
1550 return NULL;
1553 if (!expr) {
1554 warn(stmt->pos, "return with no return value");
1555 return NULL;
1557 ctype = evaluate_expression(expr);
1558 if (!ctype)
1559 return NULL;
1560 ctype = degenerate(expr, ctype, &expr);
1561 expr->ctype = ctype;
1562 compatible_assignment_types(expr, fntype, &expr, ctype, "return expression");
1563 stmt->expression = expr;
1564 return NULL;
1567 static void evaluate_if_statement(struct statement *stmt)
1569 struct expression *expr = stmt->if_conditional;
1570 struct symbol *ctype;
1572 if (!expr)
1573 return;
1575 ctype = evaluate_conditional(expr);
1576 if (!ctype)
1577 return;
1579 evaluate_statement(stmt->if_true);
1580 evaluate_statement(stmt->if_false);
1583 struct symbol *evaluate_statement(struct statement *stmt)
1585 if (!stmt)
1586 return NULL;
1588 switch (stmt->type) {
1589 case STMT_RETURN:
1590 return evaluate_return_expression(stmt);
1592 case STMT_EXPRESSION:
1593 return evaluate_expression(stmt->expression);
1595 case STMT_COMPOUND: {
1596 struct statement *s;
1597 struct symbol *type = NULL;
1598 struct symbol *sym;
1600 /* Evaluate each symbol in the compound statement */
1601 FOR_EACH_PTR(stmt->syms, sym) {
1602 evaluate_symbol(sym);
1603 } END_FOR_EACH_PTR;
1604 evaluate_symbol(stmt->ret);
1607 * Then, evaluate each statement, making the type of the
1608 * compound statement be the type of the last statement
1610 type = NULL;
1611 FOR_EACH_PTR(stmt->stmts, s) {
1612 type = evaluate_statement(s);
1613 } END_FOR_EACH_PTR;
1614 return type;
1616 case STMT_IF:
1617 evaluate_if_statement(stmt);
1618 return NULL;
1619 case STMT_ITERATOR:
1620 evaluate_conditional(stmt->iterator_pre_condition);
1621 evaluate_conditional(stmt->iterator_post_condition);
1622 evaluate_statement(stmt->iterator_pre_statement);
1623 evaluate_statement(stmt->iterator_statement);
1624 evaluate_statement(stmt->iterator_post_statement);
1625 return NULL;
1626 case STMT_SWITCH:
1627 evaluate_expression(stmt->switch_expression);
1628 evaluate_statement(stmt->switch_statement);
1629 return NULL;
1630 case STMT_CASE:
1631 evaluate_expression(stmt->case_expression);
1632 evaluate_expression(stmt->case_to);
1633 evaluate_statement(stmt->case_statement);
1634 return NULL;
1635 case STMT_LABEL:
1636 evaluate_statement(stmt->label_statement);
1637 return NULL;
1638 case STMT_GOTO:
1639 evaluate_expression(stmt->goto_expression);
1640 return NULL;
1641 case STMT_NONE:
1642 break;
1643 case STMT_ASM:
1644 /* FIXME! Do the asm parameter evaluation! */
1645 break;
1647 return NULL;