Make sparse sources themselves be sparse-clean.
[smatch.git] / evaluate.c
blob104d95252dc76aeee3a2ea17decd81bcc57e9b6c
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 *degenerate(struct expression *expr);
33 static struct symbol *evaluate_symbol_expression(struct expression *expr)
35 struct symbol *sym = expr->symbol;
36 struct symbol *base_type;
38 if (!sym) {
39 if (preprocessing) {
40 expr->ctype = &int_ctype;
41 return &int_ctype;
43 warn(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
44 return NULL;
47 examine_symbol_type(sym);
48 if ((sym->ctype.context ^ current_context) & (sym->ctype.contextmask & current_contextmask))
49 warn(expr->pos, "Using symbol '%s' in wrong context", show_ident(expr->symbol_name));
51 base_type = sym->ctype.base_type;
52 if (!base_type) {
53 warn(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
54 return NULL;
57 /* The type of a symbol is the symbol itself! */
58 expr->ctype = sym;
60 /* enum's can be turned into plain values */
61 if (sym->type != SYM_ENUM) {
62 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
63 addr->symbol = sym;
64 addr->symbol_name = expr->symbol_name;
65 addr->ctype = NULL; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
66 expr->type = EXPR_PREOP;
67 expr->op = '*';
68 expr->unop = addr;
69 return sym;
71 expr->type = EXPR_VALUE;
72 expr->value = sym->value;
73 expr->ctype = base_type;
74 return sym;
77 static struct symbol *evaluate_string(struct expression *expr)
79 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
80 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
81 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
82 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
83 unsigned int length = expr->string->length;
85 sym->array_size = alloc_const_expression(expr->pos, length);
86 sym->bit_size = bits_in_char * length;
87 sym->ctype.alignment = 1;
88 sym->ctype.modifiers = MOD_STATIC;
89 sym->ctype.base_type = array;
90 sym->initializer = initstr;
92 initstr->ctype = sym;
93 initstr->string = expr->string;
95 array->array_size = sym->array_size;
96 array->bit_size = bits_in_char * length;
97 array->ctype.alignment = 1;
98 array->ctype.modifiers = MOD_STATIC;
99 array->ctype.base_type = &char_ctype;
101 addr->symbol = sym;
102 addr->ctype = NULL;
104 expr->type = EXPR_PREOP;
105 expr->op = '*';
106 expr->unop = addr;
107 expr->ctype = sym;
108 return sym;
111 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
113 unsigned long lmod, rmod, mod;
115 if (left == right)
116 goto left;
118 if (left->bit_size > right->bit_size)
119 goto left;
121 if (right->bit_size > left->bit_size)
122 goto right;
124 /* Same size integers - promote to unsigned, promote to long */
125 lmod = left->ctype.modifiers;
126 rmod = right->ctype.modifiers;
127 mod = lmod | rmod;
128 if (mod == lmod)
129 goto left;
130 if (mod == rmod)
131 goto right;
132 return ctype_integer(mod);
134 right:
135 left = right;
136 left:
137 if (left->bit_size < bits_in_int)
138 left = &int_ctype;
139 return left;
142 static struct expression * cast_to(struct expression *old, struct symbol *type)
144 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
145 expr->ctype = type;
146 expr->cast_type = type;
147 expr->cast_expression = old;
148 return expr;
151 static int is_type_type(struct symbol *type)
153 return (type->ctype.modifiers & MOD_TYPE) != 0;
156 static int is_ptr_type(struct symbol *type)
158 if (type->type == SYM_NODE)
159 type = type->ctype.base_type;
160 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
163 static int is_int_type(struct symbol *type)
165 if (type->type == SYM_NODE)
166 type = type->ctype.base_type;
167 return (type->type == SYM_BITFIELD) || type->ctype.base_type == &int_type;
170 static struct symbol *bad_expr_type(struct expression *expr)
172 warn(expr->pos, "incompatible types for operation");
173 return NULL;
176 static struct symbol * compatible_integer_binop(struct expression *expr, struct expression **lp, struct expression **rp)
178 struct expression *left = *lp, *right = *rp;
179 struct symbol *ltype = left->ctype, *rtype = right->ctype;
181 if (ltype->type == SYM_NODE)
182 ltype = ltype->ctype.base_type;
183 if (rtype->type == SYM_NODE)
184 rtype = rtype->ctype.base_type;
185 /* Integer promotion? */
186 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
187 ltype = &int_ctype;
188 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
189 rtype = &int_ctype;
190 if (is_int_type(ltype) && is_int_type(rtype)) {
191 struct symbol *ctype = bigger_int_type(ltype, rtype);
193 /* Don't bother promoting same-size entities, it only adds clutter */
194 if (ltype->bit_size != ctype->bit_size)
195 *lp = cast_to(left, ctype);
196 if (rtype->bit_size != ctype->bit_size)
197 *rp = cast_to(right, ctype);
198 return ctype;
200 return NULL;
203 static struct symbol *evaluate_int_binop(struct expression *expr)
205 struct symbol *ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
206 if (ctype) {
207 expr->ctype = ctype;
208 return ctype;
210 return bad_expr_type(expr);
213 static inline int lvalue_expression(struct expression *expr)
215 while (expr->type == EXPR_CAST)
216 expr = expr->cast_expression;
217 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
220 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
222 struct symbol *ctype;
223 struct symbol *ptr_type = ptr->ctype;
224 struct symbol *i_type = i->ctype;
225 int bit_size;
227 if (i_type->type == SYM_NODE)
228 i_type = i_type->ctype.base_type;
229 if (ptr_type->type == SYM_NODE)
230 ptr_type = ptr_type->ctype.base_type;
232 if (i_type->type == SYM_ENUM)
233 i_type = &int_ctype;
234 if (!is_int_type(i_type))
235 return bad_expr_type(expr);
237 ctype = ptr->ctype;
238 examine_symbol_type(ctype);
240 ctype = degenerate(ptr);
241 if (!ctype->ctype.base_type) {
242 warn(expr->pos, "missing type information");
243 return NULL;
246 /* Get the size of whatever the pointer points to */
247 ptr_type = ctype;
248 if (ptr_type->type == SYM_NODE)
249 ptr_type = ptr_type->ctype.base_type;
250 if (ptr_type->type == SYM_PTR)
251 ptr_type = ptr_type->ctype.base_type;
252 bit_size = ptr_type->bit_size;
254 /* Special case: adding zero commonly happens as a result of 'array[0]' */
255 if (i->type == EXPR_VALUE && !i->value) {
256 *expr = *ptr;
257 } else if (bit_size > bits_in_char) {
258 struct expression *add = expr;
259 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
260 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
262 val->ctype = size_t_ctype;
263 val->value = bit_size >> 3;
265 mul->op = '*';
266 mul->ctype = size_t_ctype;
267 mul->left = i;
268 mul->right = val;
270 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
271 add->left = ptr;
272 add->right = mul;
275 expr->ctype = ctype;
276 return ctype;
279 static struct symbol *evaluate_add(struct expression *expr)
281 struct expression *left = expr->left, *right = expr->right;
282 struct symbol *ltype = left->ctype, *rtype = right->ctype;
284 if (is_ptr_type(ltype))
285 return evaluate_ptr_add(expr, left, right);
287 if (is_ptr_type(rtype))
288 return evaluate_ptr_add(expr, right, left);
290 // FIXME! FP promotion
291 return evaluate_int_binop(expr);
294 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
295 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE)
297 const char * type_difference(struct symbol *target, struct symbol *source,
298 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
300 for (;;) {
301 unsigned long mod1, mod2, diff;
302 unsigned long as1, as2;
303 int type1, type2;
304 struct symbol *base1, *base2;
306 if (target == source)
307 break;
308 if (!target || !source)
309 return "different types";
311 * Peel of per-node information.
312 * FIXME! Check alignment, address space, and context too here!
314 if (target->type == SYM_NODE)
315 target = target->ctype.base_type;
316 if (source->type == SYM_NODE)
317 source = source->ctype.base_type;
319 if (target == source)
320 break;
321 if (!target || !source)
322 return "different types";
324 mod1 = target->ctype.modifiers;
325 as1 = target->ctype.as;
326 mod2 = source->ctype.modifiers;
327 as2 = source->ctype.as;
329 type1 = target->type;
330 base1 = target->ctype.base_type;
332 type2 = source->type;
333 base2 = source->ctype.base_type;
336 * Pointers to functions compare as the function itself
338 if (type1 == SYM_PTR && base1) {
339 switch (base1->type) {
340 case SYM_FN:
341 type1 = SYM_FN;
342 target = base1;
343 base1 = base1->ctype.base_type;
344 default:
345 /* nothing */;
348 if (type2 == SYM_PTR && base2) {
349 switch (base2->type) {
350 case SYM_FN:
351 type2 = SYM_FN;
352 source = base2;
353 base2 = base2->ctype.base_type;
354 default:
355 /* nothing */;
359 /* Arrays degenerate to pointers for type comparisons */
360 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
361 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
363 if (type1 != type2)
364 return "different base types";
366 /* Must be same address space to be comparable */
367 if (as1 != as2)
368 return "different address spaces";
370 /* Ignore differences in storage types, sign, or addressability */
371 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
372 if (diff) {
373 mod1 &= diff & ~target_mod_ignore;
374 mod2 &= diff & ~source_mod_ignore;
375 if (mod1 | mod2) {
376 if ((mod1 | mod2) & MOD_SIZE)
377 return "different type sizes";
378 return "different modifiers";
382 if (type1 == SYM_FN) {
383 int i;
384 struct symbol *arg1, *arg2;
385 if (base1->variadic != base2->variadic)
386 return "incompatible variadic arguments";
387 PREPARE_PTR_LIST(target->arguments, arg1);
388 PREPARE_PTR_LIST(source->arguments, arg2);
389 i = 1;
390 for (;;) {
391 const char *diff;
392 diff = type_difference(arg1, arg2, 0, 0);
393 if (diff) {
394 static char argdiff[80];
395 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
396 return argdiff;
398 if (!arg1)
399 break;
400 NEXT_PTR_LIST(arg1);
401 NEXT_PTR_LIST(arg2);
402 i++;
404 FINISH_PTR_LIST(arg2);
405 FINISH_PTR_LIST(arg1);
408 target = base1;
409 source = base2;
411 return NULL;
414 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
416 /* NULL expression? Just return the type of the "other side" */
417 if (r->type == EXPR_VALUE && !r->value)
418 return l->ctype;
419 if (l->type == EXPR_VALUE && !l->value)
420 return r->ctype;
421 return NULL;
425 * Ignore differences in "volatile" and "const"ness when
426 * subtracting pointers
428 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
430 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
432 const char *typediff;
433 struct symbol *ctype;
434 struct symbol *ltype, *rtype;
436 ltype = degenerate(l);
437 rtype = degenerate(r);
440 * If it is an integer subtract: the ptr add case will do the
441 * right thing.
443 if (!is_ptr_type(rtype))
444 return evaluate_ptr_add(expr, l, r);
446 ctype = ltype;
447 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
448 if (typediff) {
449 ctype = common_ptr_type(l, r);
450 if (!ctype) {
451 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
452 return NULL;
455 examine_symbol_type(ctype);
457 /* Figure out the base type we point to */
458 if (ctype->type == SYM_NODE)
459 ctype = ctype->ctype.base_type;
460 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
461 warn(expr->pos, "subtraction of functions? Share your drugs");
462 return NULL;
464 ctype = ctype->ctype.base_type;
466 expr->ctype = ssize_t_ctype;
467 if (ctype->bit_size > bits_in_char) {
468 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
469 struct expression *div = expr;
470 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
472 val->ctype = size_t_ctype;
473 val->value = ctype->bit_size >> 3;
475 sub->op = '-';
476 sub->ctype = ssize_t_ctype;
477 sub->left = l;
478 sub->right = r;
480 div->op = '/';
481 div->left = sub;
482 div->right = val;
485 return ssize_t_ctype;
488 static struct symbol *evaluate_sub(struct expression *expr)
490 struct expression *left = expr->left, *right = expr->right;
491 struct symbol *ltype = left->ctype;
493 if (is_ptr_type(ltype))
494 return evaluate_ptr_sub(expr, left, right);
496 // FIXME! FP promotion
497 return evaluate_int_binop(expr);
500 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
502 static struct symbol *evaluate_conditional(struct expression *expr)
504 struct symbol *ctype;
506 if (!expr)
507 return NULL;
509 if (expr->type == EXPR_ASSIGNMENT)
510 warn(expr->pos, "assignment expression in conditional");
512 ctype = evaluate_expression(expr);
513 if (ctype && is_safe_type(ctype))
514 warn(expr->pos, "testing a 'safe expression'");
516 return ctype;
519 static struct symbol *evaluate_logical(struct expression *expr)
521 if (!evaluate_conditional(expr->left))
522 return NULL;
523 if (!evaluate_conditional(expr->right))
524 return NULL;
526 expr->ctype = &bool_ctype;
527 return &bool_ctype;
530 static struct symbol *evaluate_arithmetic(struct expression *expr)
532 // FIXME! Floating-point promotion!
533 return evaluate_int_binop(expr);
536 static struct symbol *evaluate_binop(struct expression *expr)
538 switch (expr->op) {
539 // addition can take ptr+int, fp and int
540 case '+':
541 return evaluate_add(expr);
543 // subtraction can take ptr-ptr, fp and int
544 case '-':
545 return evaluate_sub(expr);
547 // Arithmetic operations can take fp and int
548 case '*': case '/': case '%':
549 return evaluate_arithmetic(expr);
551 // The rest are integer operations (bitops)
552 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
553 // '&', '^', '|'
554 default:
555 return evaluate_int_binop(expr);
559 static struct symbol *evaluate_comma(struct expression *expr)
561 expr->ctype = expr->right->ctype;
562 return expr->ctype;
565 static struct symbol *evaluate_compare(struct expression *expr)
567 struct expression *left = expr->left, *right = expr->right;
568 struct symbol *ltype = left->ctype, *rtype = right->ctype;
569 struct symbol *ctype;
571 /* Type types? */
572 if (is_type_type(ltype) && is_type_type(rtype)) {
573 expr->ctype = &bool_ctype;
574 return &bool_ctype;
577 if (is_safe_type(ltype) || is_safe_type(rtype))
578 warn(expr->pos, "testing a 'safe expression'");
580 /* Pointer types? */
581 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
582 expr->ctype = &bool_ctype;
583 // FIXME! Check the types for compatibility
584 return &bool_ctype;
587 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
588 if (ctype) {
589 expr->ctype = &bool_ctype;
590 return &bool_ctype;
593 return bad_expr_type(expr);
596 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
598 /* Integer promotion? */
599 if (ltype->type == SYM_NODE)
600 ltype = ltype->ctype.base_type;
601 if (rtype->type == SYM_NODE)
602 rtype = rtype->ctype.base_type;
603 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
604 ltype = &int_ctype;
605 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
606 rtype = &int_ctype;
607 return (is_int_type(ltype) && is_int_type(rtype));
610 static int is_null_ptr(struct expression *expr)
612 return (expr->type == EXPR_VALUE &&
613 expr->value == 0);
617 * FIXME!! This should do casts, array degeneration etc..
619 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
621 struct symbol *ltype = left->ctype, *rtype = right->ctype;
623 if (ltype->type == SYM_NODE)
624 ltype = ltype->ctype.base_type;
626 if (ltype->type == SYM_PTR) {
627 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
628 return ltype;
631 if (rtype->type == SYM_NODE)
632 rtype = rtype->ctype.base_type;
634 if (rtype->type == SYM_PTR) {
635 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
636 return rtype;
638 return NULL;
641 static struct symbol * evaluate_conditional_expression(struct expression *expr)
643 struct expression *cond, *true, *false;
644 struct symbol *ctype, *ltype, *rtype;
645 const char * typediff;
647 ctype = degenerate(expr->conditional);
648 cond = expr->conditional;
650 ltype = ctype;
651 true = cond;
652 if (expr->cond_true) {
653 ltype = degenerate(expr->cond_true);
654 true = expr->cond_true;
657 rtype = degenerate(expr->cond_false);
658 false = expr->cond_false;
660 ctype = ltype;
661 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
662 if (typediff) {
663 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
664 if (!ctype) {
665 ctype = compatible_ptr_type(true, expr->cond_false);
666 if (!ctype) {
667 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
668 return NULL;
673 expr->ctype = ctype;
674 return ctype;
677 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
678 struct expression **rp, struct symbol *source, const char *where)
680 const char *typediff;
681 struct symbol *t;
682 int target_as;
684 /* It's ok if the target is more volatile or const than the source */
685 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
686 if (!typediff)
687 return 1;
689 if (compatible_integer_types(target, source)) {
690 if (target->bit_size != source->bit_size)
691 *rp = cast_to(*rp, target);
692 return 1;
695 /* Pointer destination? */
696 t = target;
697 target_as = t->ctype.as;
698 if (t->type == SYM_NODE) {
699 t = t->ctype.base_type;
700 target_as |= t->ctype.as;
702 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
703 struct expression *right = *rp;
704 struct symbol *s = source;
705 int source_as;
707 // NULL pointer is always ok
708 if (right->type == EXPR_VALUE && !right->value)
709 return 1;
711 /* "void *" matches anything as long as the address space is ok */
712 source_as = s->ctype.as;
713 if (s->type == SYM_NODE) {
714 s = s->ctype.base_type;
715 source_as |= s->ctype.as;
717 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
718 s = s->ctype.base_type;
719 t = t->ctype.base_type;
720 if (s == &void_ctype || t == &void_ctype)
721 return 1;
725 // FIXME!! Cast it?
726 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
727 warn(expr->pos, " expected %s", show_typename(target));
728 warn(expr->pos, " got %s", show_typename(source));
729 return 0;
733 * FIXME!! This is wrong from a double evaluation standpoint. We can't
734 * just expand the expression twice, that would make any side effects
735 * happen twice too.
737 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
739 int op = expr->op;
740 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
741 static const int op_trans[] = {
742 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
743 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
744 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
745 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
746 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
747 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
748 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
749 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
750 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
751 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
754 subexpr->left = left;
755 subexpr->right = right;
756 subexpr->op = op_trans[op - SPECIAL_BASE];
757 expr->op = '=';
758 expr->right = subexpr;
759 return evaluate_binop(subexpr);
762 static struct symbol *evaluate_assignment(struct expression *expr)
764 struct expression *left = expr->left, *right = expr->right;
765 struct symbol *ltype, *rtype;
767 ltype = left->ctype;
768 rtype = right->ctype;
769 if (expr->op != '=') {
770 rtype = evaluate_binop_assignment(expr, left, right);
771 if (!rtype)
772 return 0;
773 right = expr->right;
776 if (!lvalue_expression(left)) {
777 warn(expr->pos, "not an lvalue");
778 return NULL;
781 rtype = degenerate(right);
783 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
784 return 0;
786 if (ltype->type == SYM_NODE)
787 ltype->ctype.modifiers |= MOD_ASSIGNED;
789 expr->ctype = ltype;
790 return ltype;
793 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
795 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
796 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
797 *newsym = *sym;
798 newsym->ctype.as = as;
799 newsym->ctype.modifiers = mod;
800 sym = newsym;
802 return sym;
805 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
807 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
808 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
810 node->ctype.base_type = ptr;
811 ptr->bit_size = bits_in_pointer;
812 ptr->ctype.alignment = pointer_alignment;
814 node->bit_size = bits_in_pointer;
815 node->ctype.alignment = pointer_alignment;
817 sym->ctype.modifiers |= MOD_ADDRESSABLE;
818 if (sym->ctype.modifiers & MOD_REGISTER) {
819 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
820 sym->ctype.modifiers &= ~MOD_REGISTER;
822 if (sym->type == SYM_NODE) {
823 ptr->ctype.as |= sym->ctype.as;
824 ptr->ctype.modifiers |= sym->ctype.modifiers;
825 sym = sym->ctype.base_type;
827 if (degenerate && sym->type == SYM_ARRAY) {
828 ptr->ctype.as |= sym->ctype.as;
829 ptr->ctype.modifiers |= sym->ctype.modifiers;
830 sym = sym->ctype.base_type;
832 ptr->ctype.base_type = sym;
834 return node;
837 /* Arrays degenerate into pointers on pointer arithmetic */
838 static struct symbol *degenerate(struct expression *expr)
840 struct symbol *ctype, *base;
842 if (!expr)
843 return NULL;
844 ctype = expr->ctype;
845 if (!ctype)
846 return NULL;
847 base = ctype;
848 if (ctype->type == SYM_NODE)
849 base = ctype->ctype.base_type;
851 * Arrays degenerate into pointers to the entries, while
852 * functions degenerate into pointers to themselves
854 switch (base->type) {
855 case SYM_FN:
856 case SYM_ARRAY:
857 if (expr->op != '*' || expr->type != EXPR_PREOP) {
858 warn(expr->pos, "strange non-value function or array");
859 return NULL;
861 *expr = *expr->unop;
862 ctype = create_pointer(expr, ctype, 1);
863 expr->ctype = ctype;
864 default:
865 /* nothing */;
867 return ctype;
870 static struct symbol *evaluate_addressof(struct expression *expr)
872 struct expression *op = expr->unop;
873 struct symbol *ctype;
875 if (op->op != '*' || op->type != EXPR_PREOP) {
876 warn(expr->pos, "not addressable");
877 return NULL;
879 ctype = op->ctype;
880 *expr = *op->unop;
883 * symbol expression evaluation is lazy about the type
884 * of the sub-expression, so we may have to generate
885 * the type here if so..
887 if (!expr->ctype) {
888 ctype = create_pointer(expr, ctype, 0);
889 expr->ctype = ctype;
891 return expr->ctype;
895 static struct symbol *evaluate_dereference(struct expression *expr)
897 struct expression *op = expr->unop;
898 struct symbol *ctype = op->ctype, *node, *target;
900 /* Simplify: *&(expr) => (expr) */
901 if (op->type == EXPR_PREOP && op->op == '&') {
902 *expr = *op->unop;
903 return expr->ctype;
906 /* Dereferencing a node drops all the node information. */
907 if (ctype->type == SYM_NODE)
908 ctype = ctype->ctype.base_type;
910 node = alloc_symbol(expr->pos, SYM_NODE);
911 target = ctype->ctype.base_type;
913 switch (ctype->type) {
914 default:
915 warn(expr->pos, "cannot derefence this type");
916 return NULL;
917 case SYM_PTR:
918 merge_type(node, ctype);
919 if (ctype->type != SYM_ARRAY)
920 break;
922 * Dereferencing a pointer to an array results in a
923 * degenerate dereference: the expression becomes
924 * just a pointer to the entry, and the derefence
925 * goes away.
927 *expr = *op;
929 target = alloc_symbol(expr->pos, SYM_PTR);
930 target->bit_size = bits_in_pointer;
931 target->ctype.alignment = pointer_alignment;
932 merge_type(target, ctype->ctype.base_type);
933 break;
935 case SYM_ARRAY:
937 * When an array is dereferenced, we need to pick
938 * up the attributes of the original node too..
940 merge_type(node, op->ctype);
941 merge_type(node, ctype);
942 break;
945 node->bit_size = target->bit_size;
946 node->array_size = target->array_size;
948 expr->ctype = node;
949 return node;
953 * Unary post-ops: x++ and x--
955 static struct symbol *evaluate_postop(struct expression *expr)
957 struct expression *op = expr->unop;
958 struct symbol *ctype = op->ctype;
960 if (!lvalue_expression(expr->unop)) {
961 warn(expr->pos, "need lvalue expression for ++/--");
962 return NULL;
965 if (ctype->type == SYM_NODE)
966 ctype->ctype.modifiers |= MOD_ASSIGNED;
968 expr->ctype = ctype;
969 return ctype;
972 static struct symbol *evaluate_preop(struct expression *expr)
974 struct symbol *ctype = expr->unop->ctype;
976 switch (expr->op) {
977 case '(':
978 case '+':
979 *expr = *expr->unop;
980 return ctype;
982 case '*':
983 return evaluate_dereference(expr);
985 case '&':
986 return evaluate_addressof(expr);
988 case SPECIAL_INCREMENT:
989 case SPECIAL_DECREMENT:
991 * From a type evaluation standpoint the pre-ops are
992 * the same as the postops
994 return evaluate_postop(expr);
996 case '!':
997 if (is_safe_type(ctype))
998 warn(expr->pos, "testing a 'safe expression'");
999 ctype = &bool_ctype;
1000 break;
1002 default:
1003 break;
1005 expr->ctype = ctype;
1006 return &bool_ctype;
1009 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1011 struct ptr_list *head = (struct ptr_list *)_list;
1012 struct ptr_list *list = head;
1014 if (!head)
1015 return NULL;
1016 do {
1017 int i;
1018 for (i = 0; i < list->nr; i++) {
1019 struct symbol *sym = (struct symbol *) list->list[i];
1020 if (sym->ident) {
1021 if (sym->ident != ident)
1022 continue;
1023 *offset = sym->offset;
1024 return sym;
1025 } else {
1026 struct symbol *ctype = sym->ctype.base_type;
1027 struct symbol *sub;
1028 if (!ctype)
1029 continue;
1030 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1031 continue;
1032 sub = find_identifier(ident, ctype->symbol_list, offset);
1033 if (!sub)
1034 continue;
1035 *offset += sym->offset;
1036 return sub;
1039 } while ((list = list->next) != head);
1040 return NULL;
1043 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1045 struct expression *add;
1047 add = expr;
1048 if (offset) {
1049 /* Create a new add-expression */
1050 add = alloc_expression(expr->pos, EXPR_BINOP);
1051 add->op = '+';
1052 add->left = expr;
1053 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1054 add->right->ctype = &int_ctype;
1055 add->right->value = offset;
1059 * The ctype of the pointer will be lazily evaluated if
1060 * we ever take the address of this member dereference..
1062 add->ctype = NULL;
1063 return add;
1066 /* structure/union dereference */
1067 static struct symbol *evaluate_member_dereference(struct expression *expr)
1069 int offset;
1070 struct symbol *ctype, *member;
1071 struct expression *deref = expr->deref, *add;
1072 struct ident *ident = expr->member;
1073 unsigned int mod;
1074 int address_space;
1076 if (!evaluate_expression(deref))
1077 return NULL;
1078 if (!ident) {
1079 warn(expr->pos, "bad member name");
1080 return NULL;
1083 ctype = deref->ctype;
1084 address_space = ctype->ctype.as;
1085 mod = ctype->ctype.modifiers;
1086 if (ctype->type == SYM_NODE) {
1087 ctype = ctype->ctype.base_type;
1088 address_space |= ctype->ctype.as;
1089 mod |= ctype->ctype.modifiers;
1091 if (!lvalue_expression(deref)) {
1092 warn(deref->pos, "expected lvalue for member dereference");
1093 return NULL;
1095 deref = deref->unop;
1096 expr->deref = deref;
1097 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1098 warn(expr->pos, "expected structure or union");
1099 return NULL;
1101 offset = 0;
1102 member = find_identifier(ident, ctype->symbol_list, &offset);
1103 if (!member) {
1104 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1105 const char *name = "<unnamed>";
1106 int namelen = 9;
1107 if (ctype->ident) {
1108 name = ctype->ident->name;
1109 namelen = ctype->ident->len;
1111 warn(expr->pos, "no member '%s' in %s %.*s",
1112 show_ident(ident), type, namelen, name);
1113 return NULL;
1117 * The member needs to take on the address space and modifiers of
1118 * the "parent" type.
1120 member = convert_to_as_mod(member, address_space, mod);
1121 add = evaluate_offset(deref, offset);
1123 ctype = member->ctype.base_type;
1124 if (ctype->type == SYM_BITFIELD) {
1125 expr->type = EXPR_BITFIELD;
1126 expr->bitpos = member->bit_offset;
1127 expr->nrbits = member->fieldwidth;
1128 expr->address = add;
1129 } else {
1130 expr->type = EXPR_PREOP;
1131 expr->op = '*';
1132 expr->unop = add;
1135 expr->ctype = member;
1136 return member;
1139 static struct symbol *evaluate_sizeof(struct expression *expr)
1141 int size;
1143 if (expr->cast_type) {
1144 examine_symbol_type(expr->cast_type);
1145 size = expr->cast_type->bit_size;
1146 } else {
1147 if (!evaluate_expression(expr->cast_expression))
1148 return 0;
1149 size = expr->cast_expression->ctype->bit_size;
1151 if (size & 7) {
1152 warn(expr->pos, "cannot size expression");
1153 return 0;
1155 expr->type = EXPR_VALUE;
1156 expr->value = size >> 3;
1157 expr->ctype = size_t_ctype;
1158 return size_t_ctype;
1161 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1163 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1164 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1165 return clash != 0;
1168 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1170 struct expression *expr;
1171 struct symbol_list *argument_types = fn->arguments;
1172 struct symbol *argtype;
1173 int i = 1;
1175 PREPARE_PTR_LIST(argument_types, argtype);
1176 FOR_EACH_PTR (head, expr) {
1177 struct expression **p = THIS_ADDRESS(expr);
1178 struct symbol *ctype, *target;
1179 ctype = evaluate_expression(expr);
1181 if (!ctype)
1182 return 0;
1184 if (context_clash(f, ctype))
1185 warn(expr->pos, "argument %d used in wrong context", i);
1187 ctype = degenerate(expr);
1189 target = argtype;
1190 if (!target && ctype->bit_size < bits_in_int)
1191 target = &int_ctype;
1192 if (target) {
1193 static char where[30];
1194 examine_symbol_type(target);
1195 sprintf(where, "argument %d", i);
1196 compatible_assignment_types(expr, target, p, ctype, where);
1199 i++;
1200 NEXT_PTR_LIST(argtype);
1201 } END_FOR_EACH_PTR;
1202 FINISH_PTR_LIST(argtype);
1203 return 1;
1206 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1207 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1209 struct expression *entry;
1210 int current = 0;
1211 int max = 0;
1213 FOR_EACH_PTR(expr->expr_list, entry) {
1214 struct expression **p = THIS_ADDRESS(entry);
1216 if (entry->type == EXPR_INDEX) {
1217 current = entry->idx_to;
1218 continue;
1220 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1221 current++;
1222 if (current > max)
1223 max = current;
1224 } END_FOR_EACH_PTR;
1225 return max;
1228 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1230 struct expression *entry;
1231 struct symbol *sym;
1233 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1234 FOR_EACH_PTR(expr->expr_list, entry) {
1235 struct expression **p = THIS_ADDRESS(entry);
1237 if (entry->type == EXPR_IDENTIFIER) {
1238 struct ident *ident = entry->expr_ident;
1239 /* We special-case the "already right place" case */
1240 if (sym && sym->ident == ident)
1241 continue;
1242 RESET_PTR_LIST(sym);
1243 for (;;) {
1244 if (!sym) {
1245 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1246 return 0;
1248 if (sym->ident == ident)
1249 break;
1250 NEXT_PTR_LIST(sym);
1252 continue;
1255 if (!sym) {
1256 warn(expr->pos, "too many initializers for struct/union");
1257 return 0;
1260 evaluate_initializer(sym, p, offset + sym->offset);
1262 NEXT_PTR_LIST(sym);
1263 } END_FOR_EACH_PTR;
1264 FINISH_PTR_LIST(sym);
1266 return 0;
1270 * Initializers are kind of like assignments. Except
1271 * they can be a hell of a lot more complex.
1273 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1275 struct expression *expr = *ep;
1278 * Simple non-structure/array initializers are the simple
1279 * case, and look (and parse) largely like assignments.
1281 if (expr->type != EXPR_INITIALIZER) {
1282 int size = 0;
1283 struct symbol *rtype = evaluate_expression(expr);
1284 if (rtype) {
1285 struct expression *pos;
1287 // FIXME! char array[] = "string" special case
1288 // should _not_ degenerate.
1289 rtype = degenerate(expr);
1290 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1291 /* strings are special: char arrays */
1292 if (rtype->type == SYM_ARRAY)
1293 size = get_expression_value(rtype->array_size);
1295 * Don't bother creating a position expression for
1296 * the simple initializer cases that don't need it.
1298 * We need a position if the initializer has a byte
1299 * offset, _or_ if we're initializing a bitfield.
1301 if (offset || ctype->fieldwidth) {
1302 pos = alloc_expression(expr->pos, EXPR_POS);
1303 pos->init_offset = offset;
1304 pos->init_sym = ctype;
1305 pos->init_expr = *ep;
1306 pos->ctype = expr->ctype;
1307 *ep = pos;
1310 return size;
1313 expr->ctype = ctype;
1314 if (ctype->type == SYM_NODE)
1315 ctype = ctype->ctype.base_type;
1317 switch (ctype->type) {
1318 case SYM_ARRAY:
1319 case SYM_PTR:
1320 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1321 case SYM_UNION:
1322 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1323 case SYM_STRUCT:
1324 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1325 default:
1326 break;
1328 warn(expr->pos, "unexpected compound initializer");
1329 return 0;
1332 static struct symbol *evaluate_cast(struct expression *expr)
1334 struct expression *target = expr->cast_expression;
1335 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1337 expr->ctype = ctype;
1338 expr->cast_type = ctype;
1341 * Special case: a cast can be followed by an
1342 * initializer, in which case we need to pass
1343 * the type value down to that initializer rather
1344 * than trying to evaluate it as an expression
1346 * A more complex case is when the initializer is
1347 * dereferenced as part of a post-fix expression.
1348 * We need to produce an expression that can be dereferenced.
1350 if (target->type == EXPR_INITIALIZER) {
1351 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
1352 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1354 sym->ctype.base_type = ctype;
1355 sym->initializer = expr->cast_expression;
1356 evaluate_symbol(sym);
1358 addr->ctype = NULL; /* Lazy eval */
1359 addr->symbol = sym;
1361 expr->type = EXPR_PREOP;
1362 expr->op = '*';
1363 expr->unop = addr;
1364 expr->ctype = ctype;
1365 return ctype;
1368 evaluate_expression(target);
1369 degenerate(target);
1372 * Casts of constant values are special: they
1373 * can be NULL, and thus need to be simplified
1374 * early.
1376 if (target->type == EXPR_VALUE)
1377 cast_value(expr, ctype, target, target->ctype);
1379 return ctype;
1383 * Evaluate a call expression with a symbol. This
1384 * should expand inline functions, and evaluate
1385 * builtins.
1387 static int evaluate_symbol_call(struct expression *expr)
1389 struct expression *fn = expr->fn;
1390 struct symbol *ctype = fn->ctype;
1392 if (fn->type != EXPR_PREOP)
1393 return 0;
1395 if (ctype->op && ctype->op->evaluate)
1396 return ctype->op->evaluate(expr);
1398 if (ctype->ctype.modifiers & MOD_INLINE) {
1399 int ret;
1400 struct symbol *curr = current_fn;
1401 unsigned long context = current_context;
1402 unsigned long mask = current_contextmask;
1404 current_context |= ctype->ctype.context;
1405 current_contextmask |= ctype->ctype.contextmask;
1406 current_fn = ctype->ctype.base_type;
1407 ret = inline_function(expr, ctype);
1409 /* restore the old function context */
1410 current_fn = curr;
1411 current_context = context;
1412 current_contextmask = mask;
1413 return ret;
1416 return 0;
1419 static struct symbol *evaluate_call(struct expression *expr)
1421 int args, fnargs;
1422 struct symbol *ctype, *sym;
1423 struct expression *fn = expr->fn;
1424 struct expression_list *arglist = expr->args;
1426 if (!evaluate_expression(fn))
1427 return NULL;
1428 sym = ctype = fn->ctype;
1429 if (ctype->type == SYM_NODE)
1430 ctype = ctype->ctype.base_type;
1431 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1432 ctype = ctype->ctype.base_type;
1433 if (!evaluate_arguments(sym, ctype, arglist))
1434 return NULL;
1435 if (ctype->type != SYM_FN) {
1436 warn(expr->pos, "not a function %s", show_ident(sym->ident));
1437 return NULL;
1439 args = expression_list_size(expr->args);
1440 fnargs = symbol_list_size(ctype->arguments);
1441 if (args < fnargs)
1442 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1443 if (args > fnargs && !ctype->variadic)
1444 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1445 if (sym->type == SYM_NODE) {
1446 if (evaluate_symbol_call(expr))
1447 return expr->ctype;
1449 expr->ctype = ctype->ctype.base_type;
1450 return expr->ctype;
1453 struct symbol *evaluate_expression(struct expression *expr)
1455 if (!expr)
1456 return NULL;
1457 if (expr->ctype)
1458 return expr->ctype;
1460 switch (expr->type) {
1461 case EXPR_VALUE:
1462 warn(expr->pos, "value expression without a type");
1463 return NULL;
1464 case EXPR_STRING:
1465 return evaluate_string(expr);
1466 case EXPR_SYMBOL:
1467 return evaluate_symbol_expression(expr);
1468 case EXPR_BINOP:
1469 if (!evaluate_expression(expr->left))
1470 return NULL;
1471 if (!evaluate_expression(expr->right))
1472 return NULL;
1473 return evaluate_binop(expr);
1474 case EXPR_LOGICAL:
1475 return evaluate_logical(expr);
1476 case EXPR_COMMA:
1477 if (!evaluate_expression(expr->left))
1478 return NULL;
1479 if (!evaluate_expression(expr->right))
1480 return NULL;
1481 return evaluate_comma(expr);
1482 case EXPR_COMPARE:
1483 if (!evaluate_expression(expr->left))
1484 return NULL;
1485 if (!evaluate_expression(expr->right))
1486 return NULL;
1487 return evaluate_compare(expr);
1488 case EXPR_ASSIGNMENT:
1489 if (!evaluate_expression(expr->left))
1490 return NULL;
1491 if (!evaluate_expression(expr->right))
1492 return NULL;
1493 return evaluate_assignment(expr);
1494 case EXPR_PREOP:
1495 if (!evaluate_expression(expr->unop))
1496 return NULL;
1497 return evaluate_preop(expr);
1498 case EXPR_POSTOP:
1499 if (!evaluate_expression(expr->unop))
1500 return NULL;
1501 return evaluate_postop(expr);
1502 case EXPR_CAST:
1503 return evaluate_cast(expr);
1504 case EXPR_SIZEOF:
1505 return evaluate_sizeof(expr);
1506 case EXPR_DEREF:
1507 return evaluate_member_dereference(expr);
1508 case EXPR_CALL:
1509 return evaluate_call(expr);
1510 case EXPR_BITFIELD:
1511 warn(expr->pos, "bitfield generated by parser");
1512 return NULL;
1513 case EXPR_CONDITIONAL:
1514 if (!evaluate_conditional(expr->conditional))
1515 return NULL;
1516 if (!evaluate_expression(expr->cond_false))
1517 return NULL;
1518 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1519 return NULL;
1520 return evaluate_conditional_expression(expr);
1521 case EXPR_STATEMENT:
1522 expr->ctype = evaluate_statement(expr->statement);
1523 return expr->ctype;
1525 case EXPR_LABEL:
1526 expr->ctype = &ptr_ctype;
1527 return &ptr_ctype;
1529 case EXPR_TYPE:
1530 /* Evaluate the type of the symbol .. */
1531 evaluate_symbol(expr->symbol);
1532 /* .. but the type of the _expression_ is a "type" */
1533 expr->ctype = &type_ctype;
1534 return &type_ctype;
1536 /* These can not exist as stand-alone expressions */
1537 case EXPR_INITIALIZER:
1538 case EXPR_IDENTIFIER:
1539 case EXPR_INDEX:
1540 case EXPR_POS:
1541 warn(expr->pos, "internal front-end error: initializer in expression");
1542 return NULL;
1544 return NULL;
1547 void check_duplicates(struct symbol *sym)
1549 struct symbol *next = sym;
1551 while ((next = next->same_symbol) != NULL) {
1552 const char *typediff;
1553 evaluate_symbol(next);
1554 typediff = type_difference(sym, next, 0, 0);
1555 if (typediff) {
1556 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1557 show_ident(sym->ident),
1558 input_streams[next->pos.stream].name, next->pos.line, typediff);
1559 return;
1564 struct symbol *evaluate_symbol(struct symbol *sym)
1566 struct symbol *base_type;
1568 if (!sym)
1569 return sym;
1571 sym = examine_symbol_type(sym);
1572 base_type = sym->ctype.base_type;
1573 if (!base_type)
1574 return NULL;
1576 /* Evaluate the initializers */
1577 if (sym->initializer) {
1578 int count = evaluate_initializer(sym, &sym->initializer, 0);
1579 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
1580 int bit_size = count * base_type->ctype.base_type->bit_size;
1581 base_type->array_size = alloc_const_expression(sym->pos, count);
1582 base_type->bit_size = bit_size;
1583 sym->array_size = base_type->array_size;
1584 sym->bit_size = bit_size;
1588 /* And finally, evaluate the body of the symbol too */
1589 if (base_type->type == SYM_FN) {
1590 struct symbol *s;
1592 FOR_EACH_PTR(base_type->arguments, s) {
1593 evaluate_symbol(s);
1594 } END_FOR_EACH_PTR;
1596 if (base_type->stmt) {
1597 current_fn = base_type;
1598 current_contextmask = sym->ctype.contextmask;
1599 current_context = sym->ctype.context;
1600 evaluate_statement(base_type->stmt);
1604 return base_type;
1607 struct symbol *evaluate_return_expression(struct statement *stmt)
1609 struct expression *expr = stmt->expression;
1610 struct symbol *ctype, *fntype;
1612 fntype = current_fn->ctype.base_type;
1613 if (!fntype || fntype == &void_ctype) {
1614 if (expr)
1615 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
1616 return NULL;
1619 if (!expr) {
1620 warn(stmt->pos, "return with no return value");
1621 return NULL;
1623 ctype = evaluate_expression(expr);
1624 if (!ctype)
1625 return NULL;
1626 ctype = degenerate(expr);
1627 expr->ctype = ctype;
1628 compatible_assignment_types(expr, fntype, &expr, ctype, "return expression");
1629 stmt->expression = expr;
1630 return NULL;
1633 static void evaluate_if_statement(struct statement *stmt)
1635 struct expression *expr = stmt->if_conditional;
1636 struct symbol *ctype;
1638 if (!expr)
1639 return;
1641 ctype = evaluate_conditional(expr);
1642 if (!ctype)
1643 return;
1645 evaluate_statement(stmt->if_true);
1646 evaluate_statement(stmt->if_false);
1649 struct symbol *evaluate_statement(struct statement *stmt)
1651 if (!stmt)
1652 return NULL;
1654 switch (stmt->type) {
1655 case STMT_RETURN:
1656 return evaluate_return_expression(stmt);
1658 case STMT_EXPRESSION:
1659 evaluate_expression(stmt->expression);
1660 return degenerate(stmt->expression);
1662 case STMT_COMPOUND: {
1663 struct statement *s;
1664 struct symbol *type = NULL;
1665 struct symbol *sym;
1667 /* Evaluate each symbol in the compound statement */
1668 FOR_EACH_PTR(stmt->syms, sym) {
1669 evaluate_symbol(sym);
1670 } END_FOR_EACH_PTR;
1671 evaluate_symbol(stmt->ret);
1674 * Then, evaluate each statement, making the type of the
1675 * compound statement be the type of the last statement
1677 type = NULL;
1678 FOR_EACH_PTR(stmt->stmts, s) {
1679 type = evaluate_statement(s);
1680 } END_FOR_EACH_PTR;
1681 return type;
1683 case STMT_IF:
1684 evaluate_if_statement(stmt);
1685 return NULL;
1686 case STMT_ITERATOR:
1687 evaluate_conditional(stmt->iterator_pre_condition);
1688 evaluate_conditional(stmt->iterator_post_condition);
1689 evaluate_statement(stmt->iterator_pre_statement);
1690 evaluate_statement(stmt->iterator_statement);
1691 evaluate_statement(stmt->iterator_post_statement);
1692 return NULL;
1693 case STMT_SWITCH:
1694 evaluate_expression(stmt->switch_expression);
1695 evaluate_statement(stmt->switch_statement);
1696 return NULL;
1697 case STMT_CASE:
1698 evaluate_expression(stmt->case_expression);
1699 evaluate_expression(stmt->case_to);
1700 evaluate_statement(stmt->case_statement);
1701 return NULL;
1702 case STMT_LABEL:
1703 evaluate_statement(stmt->label_statement);
1704 return NULL;
1705 case STMT_GOTO:
1706 evaluate_expression(stmt->goto_expression);
1707 return NULL;
1708 case STMT_NONE:
1709 break;
1710 case STMT_ASM:
1711 /* FIXME! Do the asm parameter evaluation! */
1712 break;
1714 return NULL;