[PATCH] integer promotion fixes
[smatch.git] / evaluate.c
blob9eb879daf8332b61e04e4de45c5cffd7df78d3f5
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 inline struct symbol *integer_promotion(struct symbol *type)
113 unsigned long mod = type->ctype.modifiers;
114 int width;
116 if (type->type == SYM_ENUM)
117 return &int_ctype;
118 else if (type->type == SYM_BITFIELD) {
119 mod = type->ctype.base_type->ctype.modifiers;
120 width = type->fieldwidth;
121 } else if (mod & (MOD_CHAR | MOD_SHORT))
122 width = type->bit_size;
123 else
124 return type;
125 if (mod & MOD_UNSIGNED && width == bits_in_int)
126 return &uint_ctype;
127 return &int_ctype;
130 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
132 unsigned long lmod, rmod, mod;
134 left = integer_promotion(left);
135 right = integer_promotion(right);
137 if (left == right)
138 goto left;
140 if (left->bit_size > right->bit_size)
141 goto left;
143 if (right->bit_size > left->bit_size)
144 goto right;
146 /* Same size integers - promote to unsigned, promote to long */
147 lmod = left->ctype.modifiers;
148 rmod = right->ctype.modifiers;
149 mod = lmod | rmod;
150 if (mod == lmod)
151 goto left;
152 if (mod == rmod)
153 goto right;
154 return ctype_integer(mod);
156 right:
157 left = right;
158 left:
159 return left;
162 static struct expression * cast_to(struct expression *old, struct symbol *type)
164 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
165 expr->ctype = type;
166 expr->cast_type = type;
167 expr->cast_expression = old;
168 return expr;
171 static int is_type_type(struct symbol *type)
173 return (type->ctype.modifiers & MOD_TYPE) != 0;
176 static int is_ptr_type(struct symbol *type)
178 if (type->type == SYM_NODE)
179 type = type->ctype.base_type;
180 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
183 static inline int is_int_type(struct symbol *type)
185 if (type->type == SYM_NODE)
186 type = type->ctype.base_type;
187 return (type->type == SYM_ENUM) ||
188 (type->type == SYM_BITFIELD) ||
189 type->ctype.base_type == &int_type;
192 static struct symbol *bad_expr_type(struct expression *expr)
194 warn(expr->pos, "incompatible types for operation");
195 return NULL;
198 static struct symbol * compatible_integer_binop(struct expression *expr, struct expression **lp, struct expression **rp)
200 struct expression *left = *lp, *right = *rp;
201 struct symbol *ltype = left->ctype, *rtype = right->ctype;
203 if (ltype->type == SYM_NODE)
204 ltype = ltype->ctype.base_type;
205 if (rtype->type == SYM_NODE)
206 rtype = rtype->ctype.base_type;
207 if (is_int_type(ltype) && is_int_type(rtype)) {
208 struct symbol *ctype = bigger_int_type(ltype, rtype);
210 /* Don't bother promoting same-size entities, it only adds clutter */
211 if (ltype->bit_size != ctype->bit_size)
212 *lp = cast_to(left, ctype);
213 if (rtype->bit_size != ctype->bit_size)
214 *rp = cast_to(right, ctype);
215 return ctype;
217 return NULL;
220 static struct symbol *evaluate_int_binop(struct expression *expr)
222 struct symbol *ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
223 if (ctype) {
224 expr->ctype = ctype;
225 return ctype;
227 return bad_expr_type(expr);
230 static inline int lvalue_expression(struct expression *expr)
232 while (expr->type == EXPR_CAST)
233 expr = expr->cast_expression;
234 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
237 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
239 struct symbol *ctype;
240 struct symbol *ptr_type = ptr->ctype;
241 int bit_size;
243 if (ptr_type->type == SYM_NODE)
244 ptr_type = ptr_type->ctype.base_type;
246 if (!is_int_type(i->ctype))
247 return bad_expr_type(expr);
249 ctype = ptr->ctype;
250 examine_symbol_type(ctype);
252 ctype = degenerate(ptr);
253 if (!ctype->ctype.base_type) {
254 warn(expr->pos, "missing type information");
255 return NULL;
258 /* Get the size of whatever the pointer points to */
259 ptr_type = ctype;
260 if (ptr_type->type == SYM_NODE)
261 ptr_type = ptr_type->ctype.base_type;
262 if (ptr_type->type == SYM_PTR)
263 ptr_type = ptr_type->ctype.base_type;
264 bit_size = ptr_type->bit_size;
266 /* Special case: adding zero commonly happens as a result of 'array[0]' */
267 if (i->type == EXPR_VALUE && !i->value) {
268 *expr = *ptr;
269 } else if (bit_size > bits_in_char) {
270 struct expression *add = expr;
271 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
272 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
274 val->ctype = size_t_ctype;
275 val->value = bit_size >> 3;
277 mul->op = '*';
278 mul->ctype = size_t_ctype;
279 mul->left = i;
280 mul->right = val;
282 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
283 add->left = ptr;
284 add->right = mul;
287 expr->ctype = ctype;
288 return ctype;
291 static struct symbol *evaluate_add(struct expression *expr)
293 struct expression *left = expr->left, *right = expr->right;
294 struct symbol *ltype = left->ctype, *rtype = right->ctype;
296 if (is_ptr_type(ltype))
297 return evaluate_ptr_add(expr, left, right);
299 if (is_ptr_type(rtype))
300 return evaluate_ptr_add(expr, right, left);
302 // FIXME! FP promotion
303 return evaluate_int_binop(expr);
306 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
307 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE)
309 const char * type_difference(struct symbol *target, struct symbol *source,
310 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
312 for (;;) {
313 unsigned long mod1, mod2, diff;
314 unsigned long as1, as2;
315 int type1, type2;
316 struct symbol *base1, *base2;
318 if (target == source)
319 break;
320 if (!target || !source)
321 return "different types";
323 * Peel of per-node information.
324 * FIXME! Check alignment and context too here!
326 mod1 = target->ctype.modifiers;
327 as1 = target->ctype.as;
328 mod2 = source->ctype.modifiers;
329 as2 = source->ctype.as;
330 if (target->type == SYM_NODE) {
331 target = target->ctype.base_type;
332 if (!target)
333 return "bad types";
334 if (target->type == SYM_PTR) {
335 mod1 = 0;
336 as1 = 0;
338 mod1 |= target->ctype.modifiers;
339 as1 |= target->ctype.as;
341 if (source->type == SYM_NODE) {
342 source = source->ctype.base_type;
343 if (!source)
344 return "bad types";
345 if (source->type == SYM_PTR) {
346 mod2 = 0;
347 as2 = 0;
349 mod2 |= source->ctype.modifiers;
350 as2 |= source->ctype.as;
353 if (target == source)
354 break;
355 if (!target || !source)
356 return "different types";
358 type1 = target->type;
359 base1 = target->ctype.base_type;
361 type2 = source->type;
362 base2 = source->ctype.base_type;
365 * Pointers to functions compare as the function itself
367 if (type1 == SYM_PTR && base1) {
368 switch (base1->type) {
369 case SYM_FN:
370 type1 = SYM_FN;
371 target = base1;
372 base1 = base1->ctype.base_type;
373 default:
374 /* nothing */;
377 if (type2 == SYM_PTR && base2) {
378 switch (base2->type) {
379 case SYM_FN:
380 type2 = SYM_FN;
381 source = base2;
382 base2 = base2->ctype.base_type;
383 default:
384 /* nothing */;
388 /* Arrays degenerate to pointers for type comparisons */
389 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
390 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
392 if (type1 != type2)
393 return "different base types";
395 /* Must be same address space to be comparable */
396 if (as1 != as2)
397 return "different address spaces";
399 /* Ignore differences in storage types, sign, or addressability */
400 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
401 if (diff) {
402 mod1 &= diff & ~target_mod_ignore;
403 mod2 &= diff & ~source_mod_ignore;
404 if (mod1 | mod2) {
405 if ((mod1 | mod2) & MOD_SIZE)
406 return "different type sizes";
407 return "different modifiers";
411 if (type1 == SYM_FN) {
412 int i;
413 struct symbol *arg1, *arg2;
414 if (base1->variadic != base2->variadic)
415 return "incompatible variadic arguments";
416 PREPARE_PTR_LIST(target->arguments, arg1);
417 PREPARE_PTR_LIST(source->arguments, arg2);
418 i = 1;
419 for (;;) {
420 const char *diff;
421 diff = type_difference(arg1, arg2, 0, 0);
422 if (diff) {
423 static char argdiff[80];
424 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
425 return argdiff;
427 if (!arg1)
428 break;
429 NEXT_PTR_LIST(arg1);
430 NEXT_PTR_LIST(arg2);
431 i++;
433 FINISH_PTR_LIST(arg2);
434 FINISH_PTR_LIST(arg1);
437 target = base1;
438 source = base2;
440 return NULL;
443 static int is_null_ptr(struct expression *expr)
445 if (expr->type != EXPR_VALUE || expr->value)
446 return 0;
447 if (!is_ptr_type(expr->ctype))
448 warn(expr->pos, "Using plain integer as NULL pointer");
449 return 1;
452 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
454 /* NULL expression? Just return the type of the "other side" */
455 if (is_null_ptr(r))
456 return l->ctype;
457 if (is_null_ptr(l))
458 return r->ctype;
459 return NULL;
463 * Ignore differences in "volatile" and "const"ness when
464 * subtracting pointers
466 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
468 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
470 const char *typediff;
471 struct symbol *ctype;
472 struct symbol *ltype, *rtype;
474 ltype = degenerate(l);
475 rtype = degenerate(r);
478 * If it is an integer subtract: the ptr add case will do the
479 * right thing.
481 if (!is_ptr_type(rtype))
482 return evaluate_ptr_add(expr, l, r);
484 ctype = ltype;
485 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
486 if (typediff) {
487 ctype = common_ptr_type(l, r);
488 if (!ctype) {
489 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
490 return NULL;
493 examine_symbol_type(ctype);
495 /* Figure out the base type we point to */
496 if (ctype->type == SYM_NODE)
497 ctype = ctype->ctype.base_type;
498 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
499 warn(expr->pos, "subtraction of functions? Share your drugs");
500 return NULL;
502 ctype = ctype->ctype.base_type;
504 expr->ctype = ssize_t_ctype;
505 if (ctype->bit_size > bits_in_char) {
506 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
507 struct expression *div = expr;
508 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
510 val->ctype = size_t_ctype;
511 val->value = ctype->bit_size >> 3;
513 sub->op = '-';
514 sub->ctype = ssize_t_ctype;
515 sub->left = l;
516 sub->right = r;
518 div->op = '/';
519 div->left = sub;
520 div->right = val;
523 return ssize_t_ctype;
526 static struct symbol *evaluate_sub(struct expression *expr)
528 struct expression *left = expr->left, *right = expr->right;
529 struct symbol *ltype = left->ctype;
531 if (is_ptr_type(ltype))
532 return evaluate_ptr_sub(expr, left, right);
534 // FIXME! FP promotion
535 return evaluate_int_binop(expr);
538 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
540 static struct symbol *evaluate_conditional(struct expression *expr)
542 struct symbol *ctype;
544 if (!expr)
545 return NULL;
547 if (expr->type == EXPR_ASSIGNMENT)
548 warn(expr->pos, "assignment expression in conditional");
550 ctype = evaluate_expression(expr);
551 if (ctype && is_safe_type(ctype))
552 warn(expr->pos, "testing a 'safe expression'");
554 return ctype;
557 static struct symbol *evaluate_logical(struct expression *expr)
559 if (!evaluate_conditional(expr->left))
560 return NULL;
561 if (!evaluate_conditional(expr->right))
562 return NULL;
564 expr->ctype = &bool_ctype;
565 return &bool_ctype;
568 static struct symbol *evaluate_arithmetic(struct expression *expr)
570 // FIXME! Floating-point promotion!
571 return evaluate_int_binop(expr);
574 static struct symbol *evaluate_binop(struct expression *expr)
576 switch (expr->op) {
577 // addition can take ptr+int, fp and int
578 case '+':
579 return evaluate_add(expr);
581 // subtraction can take ptr-ptr, fp and int
582 case '-':
583 return evaluate_sub(expr);
585 // Arithmetic operations can take fp and int
586 case '*': case '/': case '%':
587 return evaluate_arithmetic(expr);
589 // The rest are integer operations (bitops)
590 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
591 // '&', '^', '|'
592 default:
593 return evaluate_int_binop(expr);
597 static struct symbol *evaluate_comma(struct expression *expr)
599 expr->ctype = expr->right->ctype;
600 return expr->ctype;
603 static struct symbol *evaluate_compare(struct expression *expr)
605 struct expression *left = expr->left, *right = expr->right;
606 struct symbol *ltype = left->ctype, *rtype = right->ctype;
607 struct symbol *ctype;
609 /* Type types? */
610 if (is_type_type(ltype) && is_type_type(rtype)) {
611 expr->ctype = &bool_ctype;
612 return &bool_ctype;
615 if (is_safe_type(ltype) || is_safe_type(rtype))
616 warn(expr->pos, "testing a 'safe expression'");
618 /* Pointer types? */
619 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
620 expr->ctype = &bool_ctype;
621 // FIXME! Check the types for compatibility
622 return &bool_ctype;
625 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
626 if (ctype) {
627 expr->ctype = &bool_ctype;
628 return &bool_ctype;
631 return bad_expr_type(expr);
634 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
636 return (is_int_type(ltype) && is_int_type(rtype));
640 * FIXME!! This should do casts, array degeneration etc..
642 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
644 struct symbol *ltype = left->ctype, *rtype = right->ctype;
646 if (ltype->type == SYM_NODE)
647 ltype = ltype->ctype.base_type;
649 if (rtype->type == SYM_NODE)
650 rtype = rtype->ctype.base_type;
652 if (ltype->type == SYM_PTR) {
653 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
654 return ltype;
657 if (rtype->type == SYM_PTR) {
658 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
659 return rtype;
661 return NULL;
664 static struct symbol * evaluate_conditional_expression(struct expression *expr)
666 struct expression *cond, *true, *false;
667 struct symbol *ctype, *ltype, *rtype;
668 const char * typediff;
670 ctype = degenerate(expr->conditional);
671 cond = expr->conditional;
673 ltype = ctype;
674 true = cond;
675 if (expr->cond_true) {
676 ltype = degenerate(expr->cond_true);
677 true = expr->cond_true;
680 rtype = degenerate(expr->cond_false);
681 false = expr->cond_false;
683 ctype = ltype;
684 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
685 if (typediff) {
686 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
687 if (!ctype) {
688 ctype = compatible_ptr_type(true, expr->cond_false);
689 if (!ctype) {
690 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
691 return NULL;
696 expr->ctype = ctype;
697 return ctype;
700 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
701 struct expression **rp, struct symbol *source, const char *where)
703 const char *typediff;
704 struct symbol *t;
705 int target_as;
707 /* It's ok if the target is more volatile or const than the source */
708 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
709 if (!typediff)
710 return 1;
712 if (compatible_integer_types(target, source)) {
713 if (target->bit_size != source->bit_size)
714 *rp = cast_to(*rp, target);
715 return 1;
718 /* Pointer destination? */
719 t = target;
720 target_as = t->ctype.as;
721 if (t->type == SYM_NODE) {
722 t = t->ctype.base_type;
723 target_as |= t->ctype.as;
725 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
726 struct expression *right = *rp;
727 struct symbol *s = source;
728 int source_as;
730 // NULL pointer is always ok
731 if (is_null_ptr(right))
732 return 1;
734 /* "void *" matches anything as long as the address space is ok */
735 source_as = s->ctype.as;
736 if (s->type == SYM_NODE) {
737 s = s->ctype.base_type;
738 source_as |= s->ctype.as;
740 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
741 s = s->ctype.base_type;
742 t = t->ctype.base_type;
743 if (s == &void_ctype || t == &void_ctype)
744 return 1;
748 // FIXME!! Cast it?
749 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
750 info(expr->pos, " expected %s", show_typename(target));
751 info(expr->pos, " got %s", show_typename(source));
752 return 0;
756 * FIXME!! This is wrong from a double evaluation standpoint. We can't
757 * just expand the expression twice, that would make any side effects
758 * happen twice too.
760 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
762 int op = expr->op;
763 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
764 static const int op_trans[] = {
765 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
766 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
767 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
768 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
769 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
770 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
771 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
772 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
773 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
774 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
777 subexpr->left = left;
778 subexpr->right = right;
779 subexpr->op = op_trans[op - SPECIAL_BASE];
780 expr->op = '=';
781 expr->right = subexpr;
782 return evaluate_binop(subexpr);
785 static void evaluate_assign_to(struct expression *left, struct symbol *type)
787 if (type->ctype.modifiers & MOD_CONST)
788 warn(left->pos, "assignment to const expression");
789 if (type->type == SYM_NODE)
790 type->ctype.modifiers |= MOD_ASSIGNED;
793 static struct symbol *evaluate_assignment(struct expression *expr)
795 struct expression *left = expr->left, *right = expr->right;
796 struct symbol *ltype, *rtype;
798 ltype = left->ctype;
799 rtype = right->ctype;
800 if (expr->op != '=') {
801 rtype = evaluate_binop_assignment(expr, left, right);
802 if (!rtype)
803 return NULL;
804 right = expr->right;
807 if (!lvalue_expression(left)) {
808 warn(expr->pos, "not an lvalue");
809 return NULL;
812 rtype = degenerate(right);
814 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
815 return NULL;
817 evaluate_assign_to(left, ltype);
819 expr->ctype = ltype;
820 return ltype;
823 static void examine_fn_arguments(struct symbol *fn)
825 struct symbol *s;
827 FOR_EACH_PTR(fn->arguments, s) {
828 struct symbol *arg = evaluate_symbol(s);
829 /* Array/function arguments silently degenerate into pointers */
830 if (arg) {
831 struct symbol *ptr;
832 switch(arg->type) {
833 case SYM_ARRAY:
834 case SYM_FN:
835 ptr = alloc_symbol(s->pos, SYM_PTR);
836 if (arg->type == SYM_ARRAY)
837 ptr->ctype = arg->ctype;
838 else
839 ptr->ctype.base_type = arg;
840 ptr->ctype.as |= s->ctype.as;
841 ptr->ctype.modifiers |= s->ctype.modifiers;
843 s->ctype.base_type = ptr;
844 s->ctype.as = 0;
845 s->ctype.modifiers = 0;
846 examine_symbol_type(s);
847 break;
848 default:
849 /* nothing */
850 break;
853 } END_FOR_EACH_PTR;
856 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
858 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
859 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
860 *newsym = *sym;
861 newsym->ctype.as = as;
862 newsym->ctype.modifiers = mod;
863 sym = newsym;
865 return sym;
868 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
870 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
871 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
873 node->ctype.base_type = ptr;
874 ptr->bit_size = bits_in_pointer;
875 ptr->ctype.alignment = pointer_alignment;
877 node->bit_size = bits_in_pointer;
878 node->ctype.alignment = pointer_alignment;
880 sym->ctype.modifiers |= MOD_ADDRESSABLE;
881 if (sym->ctype.modifiers & MOD_REGISTER) {
882 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
883 sym->ctype.modifiers &= ~MOD_REGISTER;
885 if (sym->type == SYM_NODE) {
886 ptr->ctype.as |= sym->ctype.as;
887 ptr->ctype.modifiers |= sym->ctype.modifiers;
888 sym = sym->ctype.base_type;
890 if (degenerate && sym->type == SYM_ARRAY) {
891 ptr->ctype.as |= sym->ctype.as;
892 ptr->ctype.modifiers |= sym->ctype.modifiers;
893 sym = sym->ctype.base_type;
895 ptr->ctype.base_type = sym;
897 return node;
900 /* Arrays degenerate into pointers on pointer arithmetic */
901 static struct symbol *degenerate(struct expression *expr)
903 struct symbol *ctype, *base;
905 if (!expr)
906 return NULL;
907 ctype = expr->ctype;
908 if (!ctype)
909 return NULL;
910 base = ctype;
911 if (ctype->type == SYM_NODE)
912 base = ctype->ctype.base_type;
914 * Arrays degenerate into pointers to the entries, while
915 * functions degenerate into pointers to themselves
917 switch (base->type) {
918 case SYM_FN:
919 case SYM_ARRAY:
920 if (expr->op != '*' || expr->type != EXPR_PREOP) {
921 warn(expr->pos, "strange non-value function or array");
922 return NULL;
924 *expr = *expr->unop;
925 ctype = create_pointer(expr, ctype, 1);
926 expr->ctype = ctype;
927 default:
928 /* nothing */;
930 return ctype;
933 static struct symbol *evaluate_addressof(struct expression *expr)
935 struct expression *op = expr->unop;
936 struct symbol *ctype;
938 if (op->op != '*' || op->type != EXPR_PREOP) {
939 warn(expr->pos, "not addressable");
940 return NULL;
942 ctype = op->ctype;
943 *expr = *op->unop;
946 * symbol expression evaluation is lazy about the type
947 * of the sub-expression, so we may have to generate
948 * the type here if so..
950 if (!expr->ctype) {
951 ctype = create_pointer(expr, ctype, 0);
952 expr->ctype = ctype;
954 return expr->ctype;
958 static struct symbol *evaluate_dereference(struct expression *expr)
960 struct expression *op = expr->unop;
961 struct symbol *ctype = op->ctype, *node, *target;
963 /* Simplify: *&(expr) => (expr) */
964 if (op->type == EXPR_PREOP && op->op == '&') {
965 *expr = *op->unop;
966 return expr->ctype;
969 /* Dereferencing a node drops all the node information. */
970 if (ctype->type == SYM_NODE)
971 ctype = ctype->ctype.base_type;
973 node = alloc_symbol(expr->pos, SYM_NODE);
974 target = ctype->ctype.base_type;
976 switch (ctype->type) {
977 default:
978 warn(expr->pos, "cannot derefence this type");
979 return NULL;
980 case SYM_PTR:
981 merge_type(node, ctype);
982 if (ctype->type != SYM_ARRAY)
983 break;
985 * Dereferencing a pointer to an array results in a
986 * degenerate dereference: the expression becomes
987 * just a pointer to the entry, and the derefence
988 * goes away.
990 *expr = *op;
992 target = alloc_symbol(expr->pos, SYM_PTR);
993 target->bit_size = bits_in_pointer;
994 target->ctype.alignment = pointer_alignment;
995 merge_type(target, ctype->ctype.base_type);
996 break;
998 case SYM_ARRAY:
1000 * When an array is dereferenced, we need to pick
1001 * up the attributes of the original node too..
1003 merge_type(node, op->ctype);
1004 merge_type(node, ctype);
1005 break;
1008 node->bit_size = target->bit_size;
1009 node->array_size = target->array_size;
1011 expr->ctype = node;
1012 return node;
1016 * Unary post-ops: x++ and x--
1018 static struct symbol *evaluate_postop(struct expression *expr)
1020 struct expression *op = expr->unop;
1021 struct symbol *ctype = op->ctype;
1023 if (!lvalue_expression(expr->unop)) {
1024 warn(expr->pos, "need lvalue expression for ++/--");
1025 return NULL;
1028 evaluate_assign_to(op, ctype);
1030 expr->ctype = ctype;
1031 return ctype;
1034 static struct symbol *evaluate_preop(struct expression *expr)
1036 struct symbol *ctype = expr->unop->ctype;
1038 switch (expr->op) {
1039 case '(':
1040 case '+':
1041 *expr = *expr->unop;
1042 return ctype;
1044 case '*':
1045 return evaluate_dereference(expr);
1047 case '&':
1048 return evaluate_addressof(expr);
1050 case SPECIAL_INCREMENT:
1051 case SPECIAL_DECREMENT:
1053 * From a type evaluation standpoint the pre-ops are
1054 * the same as the postops
1056 return evaluate_postop(expr);
1058 case '!':
1059 if (is_safe_type(ctype))
1060 warn(expr->pos, "testing a 'safe expression'");
1061 ctype = &bool_ctype;
1062 break;
1064 default:
1065 break;
1067 expr->ctype = ctype;
1068 return &bool_ctype;
1071 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1073 struct ptr_list *head = (struct ptr_list *)_list;
1074 struct ptr_list *list = head;
1076 if (!head)
1077 return NULL;
1078 do {
1079 int i;
1080 for (i = 0; i < list->nr; i++) {
1081 struct symbol *sym = (struct symbol *) list->list[i];
1082 if (sym->ident) {
1083 if (sym->ident != ident)
1084 continue;
1085 *offset = sym->offset;
1086 return sym;
1087 } else {
1088 struct symbol *ctype = sym->ctype.base_type;
1089 struct symbol *sub;
1090 if (!ctype)
1091 continue;
1092 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1093 continue;
1094 sub = find_identifier(ident, ctype->symbol_list, offset);
1095 if (!sub)
1096 continue;
1097 *offset += sym->offset;
1098 return sub;
1101 } while ((list = list->next) != head);
1102 return NULL;
1105 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1107 struct expression *add;
1109 add = expr;
1110 if (offset) {
1111 /* Create a new add-expression */
1112 add = alloc_expression(expr->pos, EXPR_BINOP);
1113 add->op = '+';
1114 add->left = expr;
1115 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1116 add->right->ctype = &int_ctype;
1117 add->right->value = offset;
1121 * The ctype of the pointer will be lazily evaluated if
1122 * we ever take the address of this member dereference..
1124 add->ctype = NULL;
1125 return add;
1128 /* structure/union dereference */
1129 static struct symbol *evaluate_member_dereference(struct expression *expr)
1131 int offset;
1132 struct symbol *ctype, *member;
1133 struct expression *deref = expr->deref, *add;
1134 struct ident *ident = expr->member;
1135 unsigned int mod;
1136 int address_space;
1138 if (!evaluate_expression(deref))
1139 return NULL;
1140 if (!ident) {
1141 warn(expr->pos, "bad member name");
1142 return NULL;
1145 ctype = deref->ctype;
1146 address_space = ctype->ctype.as;
1147 mod = ctype->ctype.modifiers;
1148 if (ctype->type == SYM_NODE) {
1149 ctype = ctype->ctype.base_type;
1150 address_space |= ctype->ctype.as;
1151 mod |= ctype->ctype.modifiers;
1153 if (!lvalue_expression(deref)) {
1154 warn(deref->pos, "expected lvalue for member dereference");
1155 return NULL;
1157 deref = deref->unop;
1158 expr->deref = deref;
1159 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1160 warn(expr->pos, "expected structure or union");
1161 return NULL;
1163 offset = 0;
1164 member = find_identifier(ident, ctype->symbol_list, &offset);
1165 if (!member) {
1166 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1167 const char *name = "<unnamed>";
1168 int namelen = 9;
1169 if (ctype->ident) {
1170 name = ctype->ident->name;
1171 namelen = ctype->ident->len;
1173 warn(expr->pos, "no member '%s' in %s %.*s",
1174 show_ident(ident), type, namelen, name);
1175 return NULL;
1179 * The member needs to take on the address space and modifiers of
1180 * the "parent" type.
1182 member = convert_to_as_mod(member, address_space, mod);
1183 add = evaluate_offset(deref, offset);
1185 ctype = member->ctype.base_type;
1186 if (ctype->type == SYM_BITFIELD) {
1187 expr->type = EXPR_BITFIELD;
1188 expr->bitpos = member->bit_offset;
1189 expr->nrbits = member->fieldwidth;
1190 expr->address = add;
1191 } else {
1192 expr->type = EXPR_PREOP;
1193 expr->op = '*';
1194 expr->unop = add;
1197 expr->ctype = member;
1198 return member;
1201 static struct symbol *evaluate_sizeof(struct expression *expr)
1203 int size;
1205 if (expr->cast_type) {
1206 examine_symbol_type(expr->cast_type);
1207 size = expr->cast_type->bit_size;
1208 } else {
1209 if (!evaluate_expression(expr->cast_expression))
1210 return NULL;
1211 size = expr->cast_expression->ctype->bit_size;
1213 if (size & 7) {
1214 warn(expr->pos, "cannot size expression");
1215 return NULL;
1217 expr->type = EXPR_VALUE;
1218 expr->value = size >> 3;
1219 expr->ctype = size_t_ctype;
1220 return size_t_ctype;
1223 static struct symbol *evaluate_alignof(struct expression *expr)
1225 struct symbol *type = expr->cast_type;
1227 if (!type) {
1228 type = evaluate_expression(expr->cast_expression);
1229 if (!type)
1230 return NULL;
1232 examine_symbol_type(type);
1233 expr->type = EXPR_VALUE;
1234 expr->value = type->ctype.alignment;
1235 expr->ctype = size_t_ctype;
1236 return size_t_ctype;
1239 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1241 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1242 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1243 return clash != 0;
1246 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1248 struct expression *expr;
1249 struct symbol_list *argument_types = fn->arguments;
1250 struct symbol *argtype;
1251 int i = 1;
1253 PREPARE_PTR_LIST(argument_types, argtype);
1254 FOR_EACH_PTR (head, expr) {
1255 struct expression **p = THIS_ADDRESS(expr);
1256 struct symbol *ctype, *target;
1257 ctype = evaluate_expression(expr);
1259 if (!ctype)
1260 return 0;
1262 if (context_clash(f, ctype))
1263 warn(expr->pos, "argument %d used in wrong context", i);
1265 ctype = degenerate(expr);
1267 target = argtype;
1268 if (!target && ctype->bit_size < bits_in_int)
1269 target = &int_ctype;
1270 if (target) {
1271 static char where[30];
1272 examine_symbol_type(target);
1273 sprintf(where, "argument %d", i);
1274 compatible_assignment_types(expr, target, p, ctype, where);
1277 i++;
1278 NEXT_PTR_LIST(argtype);
1279 } END_FOR_EACH_PTR;
1280 FINISH_PTR_LIST(argtype);
1281 return 1;
1284 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1285 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1287 struct expression *entry;
1288 int current = 0;
1289 int max = 0;
1291 FOR_EACH_PTR(expr->expr_list, entry) {
1292 struct expression **p = THIS_ADDRESS(entry);
1294 if (entry->type == EXPR_INDEX) {
1295 current = entry->idx_to;
1296 continue;
1298 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1299 current++;
1300 if (current > max)
1301 max = current;
1302 } END_FOR_EACH_PTR;
1303 return max;
1306 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1307 static int evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1309 if (offset || expression_list_size(expr->expr_list) != 1) {
1310 warn(expr->pos, "unexpected compound initializer");
1311 return 0;
1313 return evaluate_array_initializer(ctype, expr, 0);
1316 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1318 struct expression *entry;
1319 struct symbol *sym;
1321 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1322 FOR_EACH_PTR(expr->expr_list, entry) {
1323 struct expression **p = THIS_ADDRESS(entry);
1325 if (entry->type == EXPR_IDENTIFIER) {
1326 struct ident *ident = entry->expr_ident;
1327 /* We special-case the "already right place" case */
1328 if (sym && sym->ident == ident)
1329 continue;
1330 RESET_PTR_LIST(sym);
1331 for (;;) {
1332 if (!sym) {
1333 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1334 return 0;
1336 if (sym->ident == ident)
1337 break;
1338 NEXT_PTR_LIST(sym);
1340 continue;
1343 if (!sym) {
1344 warn(expr->pos, "too many initializers for struct/union");
1345 return 0;
1348 evaluate_initializer(sym, p, offset + sym->offset);
1350 NEXT_PTR_LIST(sym);
1351 } END_FOR_EACH_PTR;
1352 FINISH_PTR_LIST(sym);
1354 return 0;
1358 * Initializers are kind of like assignments. Except
1359 * they can be a hell of a lot more complex.
1361 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1363 struct expression *expr = *ep;
1366 * Simple non-structure/array initializers are the simple
1367 * case, and look (and parse) largely like assignments.
1369 if (expr->type != EXPR_INITIALIZER) {
1370 int size = 0;
1371 struct symbol *rtype = evaluate_expression(expr);
1372 if (rtype) {
1373 struct expression *pos;
1375 // FIXME! char array[] = "string" special case
1376 // should _not_ degenerate.
1377 rtype = degenerate(expr);
1378 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1379 /* strings are special: char arrays */
1380 if (rtype->type == SYM_ARRAY)
1381 size = get_expression_value(rtype->array_size);
1383 * Don't bother creating a position expression for
1384 * the simple initializer cases that don't need it.
1386 * We need a position if the initializer has a byte
1387 * offset, _or_ if we're initializing a bitfield.
1389 if (offset || ctype->fieldwidth) {
1390 pos = alloc_expression(expr->pos, EXPR_POS);
1391 pos->init_offset = offset;
1392 pos->init_sym = ctype;
1393 pos->init_expr = *ep;
1394 pos->ctype = expr->ctype;
1395 *ep = pos;
1398 return size;
1401 expr->ctype = ctype;
1402 if (ctype->type == SYM_NODE)
1403 ctype = ctype->ctype.base_type;
1405 switch (ctype->type) {
1406 case SYM_ARRAY:
1407 case SYM_PTR:
1408 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1409 case SYM_UNION:
1410 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1411 case SYM_STRUCT:
1412 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1413 default:
1414 return evaluate_scalar_initializer(ctype, expr, offset);
1418 static int get_as(struct symbol *sym)
1420 int as;
1421 unsigned long mod;
1423 if (!sym)
1424 return 0;
1425 as = sym->ctype.as;
1426 mod = sym->ctype.modifiers;
1427 if (sym->type == SYM_NODE) {
1428 sym = sym->ctype.base_type;
1429 as |= sym->ctype.as;
1430 mod |= sym->ctype.modifiers;
1433 * You can always throw a value away by casting to
1434 * "void" - that's an implicit "force". Note that
1435 * the same is _not_ true of "void *".
1437 if (sym == &void_ctype)
1438 return -1;
1441 * At least for now, allow casting to a "unsigned long".
1442 * That's how we do things like pointer arithmetic and
1443 * store pointers to registers.
1445 if (sym == &ulong_ctype)
1446 return -1;
1448 if (sym && sym->type == SYM_PTR) {
1449 sym = sym->ctype.base_type;
1450 as |= sym->ctype.as;
1451 mod |= sym->ctype.modifiers;
1453 if (mod & MOD_FORCE)
1454 return -1;
1455 return as;
1458 static struct symbol *evaluate_cast(struct expression *expr)
1460 struct expression *target = expr->cast_expression;
1461 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1463 expr->ctype = ctype;
1464 expr->cast_type = ctype;
1467 * Special case: a cast can be followed by an
1468 * initializer, in which case we need to pass
1469 * the type value down to that initializer rather
1470 * than trying to evaluate it as an expression
1472 * A more complex case is when the initializer is
1473 * dereferenced as part of a post-fix expression.
1474 * We need to produce an expression that can be dereferenced.
1476 if (target->type == EXPR_INITIALIZER) {
1477 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
1478 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1480 sym->ctype.base_type = ctype;
1481 sym->initializer = expr->cast_expression;
1482 evaluate_symbol(sym);
1484 addr->ctype = NULL; /* Lazy eval */
1485 addr->symbol = sym;
1487 expr->type = EXPR_PREOP;
1488 expr->op = '*';
1489 expr->unop = addr;
1490 expr->ctype = ctype;
1491 return ctype;
1494 evaluate_expression(target);
1495 degenerate(target);
1497 if (!get_as(ctype) && get_as(target->ctype) > 0)
1498 warn(expr->pos, "cast removes address space of expression");
1501 * Casts of constant values are special: they
1502 * can be NULL, and thus need to be simplified
1503 * early.
1505 if (target->type == EXPR_VALUE)
1506 cast_value(expr, ctype, target, target->ctype);
1508 return ctype;
1512 * Evaluate a call expression with a symbol. This
1513 * should expand inline functions, and evaluate
1514 * builtins.
1516 static int evaluate_symbol_call(struct expression *expr)
1518 struct expression *fn = expr->fn;
1519 struct symbol *ctype = fn->ctype;
1521 if (fn->type != EXPR_PREOP)
1522 return 0;
1524 if (ctype->op && ctype->op->evaluate)
1525 return ctype->op->evaluate(expr);
1527 if (ctype->ctype.modifiers & MOD_INLINE) {
1528 int ret;
1529 struct symbol *curr = current_fn;
1530 unsigned long context = current_context;
1531 unsigned long mask = current_contextmask;
1533 current_context |= ctype->ctype.context;
1534 current_contextmask |= ctype->ctype.contextmask;
1535 current_fn = ctype->ctype.base_type;
1536 examine_fn_arguments(current_fn);
1538 ret = inline_function(expr, ctype);
1540 /* restore the old function context */
1541 current_fn = curr;
1542 current_context = context;
1543 current_contextmask = mask;
1544 return ret;
1547 return 0;
1550 static struct symbol *evaluate_call(struct expression *expr)
1552 int args, fnargs;
1553 struct symbol *ctype, *sym;
1554 struct expression *fn = expr->fn;
1555 struct expression_list *arglist = expr->args;
1557 if (!evaluate_expression(fn))
1558 return NULL;
1559 sym = ctype = fn->ctype;
1560 if (ctype->type == SYM_NODE)
1561 ctype = ctype->ctype.base_type;
1562 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1563 ctype = ctype->ctype.base_type;
1564 if (!evaluate_arguments(sym, ctype, arglist))
1565 return NULL;
1566 if (ctype->type != SYM_FN) {
1567 warn(expr->pos, "not a function %s", show_ident(sym->ident));
1568 return NULL;
1570 args = expression_list_size(expr->args);
1571 fnargs = symbol_list_size(ctype->arguments);
1572 if (args < fnargs)
1573 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1574 if (args > fnargs && !ctype->variadic)
1575 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1576 if (sym->type == SYM_NODE) {
1577 if (evaluate_symbol_call(expr))
1578 return expr->ctype;
1580 expr->ctype = ctype->ctype.base_type;
1581 return expr->ctype;
1584 struct symbol *evaluate_expression(struct expression *expr)
1586 if (!expr)
1587 return NULL;
1588 if (expr->ctype)
1589 return expr->ctype;
1591 switch (expr->type) {
1592 case EXPR_VALUE:
1593 warn(expr->pos, "value expression without a type");
1594 return NULL;
1595 case EXPR_STRING:
1596 return evaluate_string(expr);
1597 case EXPR_SYMBOL:
1598 return evaluate_symbol_expression(expr);
1599 case EXPR_BINOP:
1600 if (!evaluate_expression(expr->left))
1601 return NULL;
1602 if (!evaluate_expression(expr->right))
1603 return NULL;
1604 return evaluate_binop(expr);
1605 case EXPR_LOGICAL:
1606 return evaluate_logical(expr);
1607 case EXPR_COMMA:
1608 if (!evaluate_expression(expr->left))
1609 return NULL;
1610 if (!evaluate_expression(expr->right))
1611 return NULL;
1612 return evaluate_comma(expr);
1613 case EXPR_COMPARE:
1614 if (!evaluate_expression(expr->left))
1615 return NULL;
1616 if (!evaluate_expression(expr->right))
1617 return NULL;
1618 return evaluate_compare(expr);
1619 case EXPR_ASSIGNMENT:
1620 if (!evaluate_expression(expr->left))
1621 return NULL;
1622 if (!evaluate_expression(expr->right))
1623 return NULL;
1624 return evaluate_assignment(expr);
1625 case EXPR_PREOP:
1626 if (!evaluate_expression(expr->unop))
1627 return NULL;
1628 return evaluate_preop(expr);
1629 case EXPR_POSTOP:
1630 if (!evaluate_expression(expr->unop))
1631 return NULL;
1632 return evaluate_postop(expr);
1633 case EXPR_CAST:
1634 return evaluate_cast(expr);
1635 case EXPR_SIZEOF:
1636 return evaluate_sizeof(expr);
1637 case EXPR_ALIGNOF:
1638 return evaluate_alignof(expr);
1639 case EXPR_DEREF:
1640 return evaluate_member_dereference(expr);
1641 case EXPR_CALL:
1642 return evaluate_call(expr);
1643 case EXPR_BITFIELD:
1644 warn(expr->pos, "bitfield generated by parser");
1645 return NULL;
1646 case EXPR_CONDITIONAL:
1647 if (!evaluate_conditional(expr->conditional))
1648 return NULL;
1649 if (!evaluate_expression(expr->cond_false))
1650 return NULL;
1651 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1652 return NULL;
1653 return evaluate_conditional_expression(expr);
1654 case EXPR_STATEMENT:
1655 expr->ctype = evaluate_statement(expr->statement);
1656 return expr->ctype;
1658 case EXPR_LABEL:
1659 expr->ctype = &ptr_ctype;
1660 return &ptr_ctype;
1662 case EXPR_TYPE:
1663 /* Evaluate the type of the symbol .. */
1664 evaluate_symbol(expr->symbol);
1665 /* .. but the type of the _expression_ is a "type" */
1666 expr->ctype = &type_ctype;
1667 return &type_ctype;
1669 /* These can not exist as stand-alone expressions */
1670 case EXPR_INITIALIZER:
1671 case EXPR_IDENTIFIER:
1672 case EXPR_INDEX:
1673 case EXPR_POS:
1674 warn(expr->pos, "internal front-end error: initializer in expression");
1675 return NULL;
1677 return NULL;
1680 void check_duplicates(struct symbol *sym)
1682 struct symbol *next = sym;
1684 while ((next = next->same_symbol) != NULL) {
1685 const char *typediff;
1686 evaluate_symbol(next);
1687 typediff = type_difference(sym, next, 0, 0);
1688 if (typediff) {
1689 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1690 show_ident(sym->ident),
1691 input_streams[next->pos.stream].name, next->pos.line, typediff);
1692 return;
1697 struct symbol *evaluate_symbol(struct symbol *sym)
1699 struct symbol *base_type;
1701 if (!sym)
1702 return sym;
1704 sym = examine_symbol_type(sym);
1705 base_type = sym->ctype.base_type;
1706 if (!base_type)
1707 return NULL;
1709 /* Evaluate the initializers */
1710 if (sym->initializer) {
1711 int count = evaluate_initializer(sym, &sym->initializer, 0);
1712 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
1713 int bit_size = count * base_type->ctype.base_type->bit_size;
1714 base_type->array_size = alloc_const_expression(sym->pos, count);
1715 base_type->bit_size = bit_size;
1716 sym->array_size = base_type->array_size;
1717 sym->bit_size = bit_size;
1721 /* And finally, evaluate the body of the symbol too */
1722 if (base_type->type == SYM_FN) {
1723 examine_fn_arguments(base_type);
1724 if (base_type->stmt) {
1725 current_fn = base_type;
1726 current_contextmask = sym->ctype.contextmask;
1727 current_context = sym->ctype.context;
1728 evaluate_statement(base_type->stmt);
1732 return base_type;
1735 struct symbol *evaluate_return_expression(struct statement *stmt)
1737 struct expression *expr = stmt->expression;
1738 struct symbol *ctype, *fntype;
1740 evaluate_expression(expr);
1741 ctype = degenerate(expr);
1742 fntype = current_fn->ctype.base_type;
1743 if (!fntype || fntype == &void_ctype) {
1744 if (expr && ctype != &void_ctype)
1745 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
1746 return NULL;
1749 if (!expr) {
1750 warn(stmt->pos, "return with no return value");
1751 return NULL;
1753 if (!ctype)
1754 return NULL;
1755 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
1756 return NULL;
1759 static void evaluate_if_statement(struct statement *stmt)
1761 struct expression *expr = stmt->if_conditional;
1762 struct symbol *ctype;
1764 if (!expr)
1765 return;
1767 ctype = evaluate_conditional(expr);
1768 if (!ctype)
1769 return;
1771 evaluate_statement(stmt->if_true);
1772 evaluate_statement(stmt->if_false);
1775 struct symbol *evaluate_statement(struct statement *stmt)
1777 if (!stmt)
1778 return NULL;
1780 switch (stmt->type) {
1781 case STMT_RETURN:
1782 return evaluate_return_expression(stmt);
1784 case STMT_EXPRESSION:
1785 evaluate_expression(stmt->expression);
1786 return degenerate(stmt->expression);
1788 case STMT_COMPOUND: {
1789 struct statement *s;
1790 struct symbol *type = NULL;
1791 struct symbol *sym;
1793 /* Evaluate each symbol in the compound statement */
1794 FOR_EACH_PTR(stmt->syms, sym) {
1795 evaluate_symbol(sym);
1796 } END_FOR_EACH_PTR;
1797 evaluate_symbol(stmt->ret);
1800 * Then, evaluate each statement, making the type of the
1801 * compound statement be the type of the last statement
1803 type = NULL;
1804 FOR_EACH_PTR(stmt->stmts, s) {
1805 type = evaluate_statement(s);
1806 } END_FOR_EACH_PTR;
1807 return type;
1809 case STMT_IF:
1810 evaluate_if_statement(stmt);
1811 return NULL;
1812 case STMT_ITERATOR:
1813 evaluate_conditional(stmt->iterator_pre_condition);
1814 evaluate_conditional(stmt->iterator_post_condition);
1815 evaluate_statement(stmt->iterator_pre_statement);
1816 evaluate_statement(stmt->iterator_statement);
1817 evaluate_statement(stmt->iterator_post_statement);
1818 return NULL;
1819 case STMT_SWITCH:
1820 evaluate_expression(stmt->switch_expression);
1821 evaluate_statement(stmt->switch_statement);
1822 return NULL;
1823 case STMT_CASE:
1824 evaluate_expression(stmt->case_expression);
1825 evaluate_expression(stmt->case_to);
1826 evaluate_statement(stmt->case_statement);
1827 return NULL;
1828 case STMT_LABEL:
1829 evaluate_statement(stmt->label_statement);
1830 return NULL;
1831 case STMT_GOTO:
1832 evaluate_expression(stmt->goto_expression);
1833 return NULL;
1834 case STMT_NONE:
1835 break;
1836 case STMT_ASM:
1837 /* FIXME! Do the asm parameter evaluation! */
1838 break;
1840 return NULL;