[PATCH] enum handling
[smatch.git] / evaluate.c
blob9eac898209adfa7f35f6a2818fecab346a70319c
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 warning(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 warning(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 warning(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 /* enums 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 = &lazy_ptr_ctype; /* 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;
70 } else if (base_type->bit_size < bits_in_int) {
71 /* ugly - we need to force sizeof for these guys */
72 struct expression *e = alloc_expression(expr->pos, EXPR_VALUE);
73 e->value = sym->value;
74 e->ctype = base_type;
75 expr->type = EXPR_PREOP;
76 expr->op = '+';
77 expr->unop = e;
78 } else {
79 expr->type = EXPR_VALUE;
80 expr->value = sym->value;
82 expr->ctype = base_type;
83 return base_type;
86 static struct symbol *evaluate_string(struct expression *expr)
88 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
89 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
90 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
91 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
92 unsigned int length = expr->string->length;
94 sym->array_size = alloc_const_expression(expr->pos, length);
95 sym->bit_size = bits_in_char * length;
96 sym->ctype.alignment = 1;
97 sym->ctype.modifiers = MOD_STATIC;
98 sym->ctype.base_type = array;
99 sym->initializer = initstr;
101 initstr->ctype = sym;
102 initstr->string = expr->string;
104 array->array_size = sym->array_size;
105 array->bit_size = bits_in_char * length;
106 array->ctype.alignment = 1;
107 array->ctype.modifiers = MOD_STATIC;
108 array->ctype.base_type = &char_ctype;
110 addr->symbol = sym;
111 addr->ctype = &lazy_ptr_ctype;
113 expr->type = EXPR_PREOP;
114 expr->op = '*';
115 expr->unop = addr;
116 expr->ctype = sym;
117 return sym;
120 static inline struct symbol *integer_promotion(struct symbol *type)
122 unsigned long mod = type->ctype.modifiers;
123 int width;
125 if (type->type == SYM_ENUM) {
126 type = type->ctype.base_type;
127 mod = type->ctype.modifiers;
129 if (type->type == SYM_BITFIELD) {
130 mod = type->ctype.base_type->ctype.modifiers;
131 width = type->fieldwidth;
132 } else if (mod & (MOD_CHAR | MOD_SHORT))
133 width = type->bit_size;
134 else
135 return type;
136 if (mod & MOD_UNSIGNED && width == bits_in_int)
137 return &uint_ctype;
138 return &int_ctype;
142 * integer part of usual arithmetic conversions:
143 * integer promotions are applied
144 * if left and right are identical, we are done
145 * if signedness is the same, convert one with lower rank
146 * unless unsigned argument has rank lower than signed one, convert the
147 * signed one.
148 * if signed argument is bigger than unsigned one, convert the unsigned.
149 * otherwise, convert signed.
151 * Leaving aside the integer promotions, that is equivalent to
152 * if identical, don't convert
153 * if left is bigger than right, convert right
154 * if right is bigger than left, convert right
155 * otherwise, if signedness is the same, convert one with lower rank
156 * otherwise convert the signed one.
158 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
160 unsigned long lmod, rmod;
162 left = integer_promotion(left);
163 right = integer_promotion(right);
165 if (left == right)
166 goto left;
168 if (left->bit_size > right->bit_size)
169 goto left;
171 if (right->bit_size > left->bit_size)
172 goto right;
174 lmod = left->ctype.modifiers;
175 rmod = right->ctype.modifiers;
176 if ((lmod ^ rmod) & MOD_UNSIGNED) {
177 if (lmod & MOD_UNSIGNED)
178 goto left;
179 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
180 goto left;
181 right:
182 left = right;
183 left:
184 return left;
187 static struct expression * cast_to(struct expression *old, struct symbol *type)
189 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
190 expr->ctype = type;
191 expr->cast_type = type;
192 expr->cast_expression = old;
193 return expr;
196 static int is_type_type(struct symbol *type)
198 return (type->ctype.modifiers & MOD_TYPE) != 0;
201 static int is_ptr_type(struct symbol *type)
203 if (type->type == SYM_NODE)
204 type = type->ctype.base_type;
205 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
208 static inline int is_float_type(struct symbol *type)
210 if (type->type == SYM_NODE)
211 type = type->ctype.base_type;
212 return type->ctype.base_type == &fp_type;
215 static inline int is_byte_type(struct symbol *type)
217 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
220 static inline int is_string_type(struct symbol *type)
222 if (type->type == SYM_NODE)
223 type = type->ctype.base_type;
224 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
227 static struct symbol *bad_expr_type(struct expression *expr)
229 warning(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
230 switch (expr->type) {
231 case EXPR_BINOP:
232 case EXPR_COMPARE:
233 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
234 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
235 break;
236 case EXPR_PREOP:
237 case EXPR_POSTOP:
238 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
239 break;
240 default:
241 break;
244 return NULL;
247 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
249 struct expression *left = *lp, *right = *rp;
250 struct symbol *ltype = left->ctype, *rtype = right->ctype;
252 if (ltype->type == SYM_NODE)
253 ltype = ltype->ctype.base_type;
254 if (rtype->type == SYM_NODE)
255 rtype = rtype->ctype.base_type;
256 if (is_float_type(ltype)) {
257 if (is_int_type(rtype))
258 goto Left;
259 if (is_float_type(rtype)) {
260 unsigned long lmod = ltype->ctype.modifiers;
261 unsigned long rmod = rtype->ctype.modifiers;
262 lmod &= MOD_LONG | MOD_LONGLONG;
263 rmod &= MOD_LONG | MOD_LONGLONG;
264 if (lmod == rmod)
265 return ltype;
266 if (lmod & ~rmod)
267 goto Left;
268 else
269 goto Right;
271 return NULL;
273 if (!is_float_type(rtype) || !is_int_type(ltype))
274 return NULL;
275 Right:
276 *lp = cast_to(left, rtype);
277 return rtype;
278 Left:
279 *rp = cast_to(right, ltype);
280 return ltype;
283 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
285 struct expression *left = *lp, *right = *rp;
286 struct symbol *ltype = left->ctype, *rtype = right->ctype;
288 if (ltype->type == SYM_NODE)
289 ltype = ltype->ctype.base_type;
290 if (rtype->type == SYM_NODE)
291 rtype = rtype->ctype.base_type;
292 if (is_int_type(ltype) && is_int_type(rtype)) {
293 struct symbol *ctype = bigger_int_type(ltype, rtype);
295 /* Don't bother promoting same-size entities, it only adds clutter */
296 if (ltype->bit_size != ctype->bit_size)
297 *lp = cast_to(left, ctype);
298 if (rtype->bit_size != ctype->bit_size)
299 *rp = cast_to(right, ctype);
300 return ctype;
302 return NULL;
305 static int restricted_value(struct expression *v, struct symbol *type)
307 if (v->type != EXPR_VALUE)
308 return 1;
309 if (v->value != 0)
310 return 1;
311 return 0;
314 static int restricted_binop(int op, struct symbol *type)
316 switch (op) {
317 case '&':
318 case '|':
319 case '^':
320 case '?':
321 case SPECIAL_EQUAL:
322 case SPECIAL_NOTEQUAL:
323 return 0;
324 default:
325 return 1;
329 static int restricted_unop(int op, struct symbol *type)
331 if (op == '~' && type->bit_size >= bits_in_int)
332 return 0;
333 if (op == '+')
334 return 0;
335 return 1;
338 static struct symbol *compatible_restricted_binop(int op, struct expression **lp, struct expression **rp)
340 struct expression *left = *lp, *right = *rp;
341 struct symbol *ltype = left->ctype, *rtype = right->ctype;
342 struct symbol *type = NULL;
344 if (ltype->type == SYM_NODE)
345 ltype = ltype->ctype.base_type;
346 if (ltype->type == SYM_ENUM)
347 ltype = ltype->ctype.base_type;
348 if (rtype->type == SYM_NODE)
349 rtype = rtype->ctype.base_type;
350 if (rtype->type == SYM_ENUM)
351 rtype = rtype->ctype.base_type;
352 if (is_restricted_type(ltype)) {
353 if (is_restricted_type(rtype)) {
354 if (ltype == rtype)
355 type = ltype;
356 } else {
357 if (!restricted_value(right, ltype))
358 type = ltype;
360 } else if (is_restricted_type(rtype)) {
361 if (!restricted_value(left, rtype))
362 type = rtype;
364 if (!type)
365 return NULL;
366 if (restricted_binop(op, type))
367 return NULL;
368 return type;
371 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
373 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
374 if (!ctype && float_ok)
375 ctype = compatible_float_binop(&expr->left, &expr->right);
376 if (!ctype)
377 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
378 if (ctype) {
379 expr->ctype = ctype;
380 return ctype;
382 return bad_expr_type(expr);
385 static inline int lvalue_expression(struct expression *expr)
387 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
390 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
392 struct symbol *ctype;
393 struct symbol *ptr_type = ptr->ctype;
394 int bit_size;
396 if (ptr_type->type == SYM_NODE)
397 ptr_type = ptr_type->ctype.base_type;
399 if (!is_int_type(i->ctype))
400 return bad_expr_type(expr);
402 ctype = ptr->ctype;
403 examine_symbol_type(ctype);
405 ctype = degenerate(ptr);
406 if (!ctype->ctype.base_type) {
407 warning(expr->pos, "missing type information");
408 return NULL;
411 /* Get the size of whatever the pointer points to */
412 ptr_type = ctype;
413 if (ptr_type->type == SYM_NODE)
414 ptr_type = ptr_type->ctype.base_type;
415 if (ptr_type->type == SYM_PTR)
416 ptr_type = ptr_type->ctype.base_type;
417 bit_size = ptr_type->bit_size;
419 /* Special case: adding zero commonly happens as a result of 'array[0]' */
420 if (i->type == EXPR_VALUE && !i->value) {
421 *expr = *ptr;
422 } else if (bit_size > bits_in_char) {
423 struct expression *add = expr;
424 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
425 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
427 val->ctype = size_t_ctype;
428 val->value = bit_size >> 3;
430 mul->op = '*';
431 mul->ctype = size_t_ctype;
432 mul->left = i;
433 mul->right = val;
435 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
436 add->left = ptr;
437 add->right = mul;
440 expr->ctype = ctype;
441 return ctype;
444 static struct symbol *evaluate_add(struct expression *expr)
446 struct expression *left = expr->left, *right = expr->right;
447 struct symbol *ltype = left->ctype, *rtype = right->ctype;
449 if (is_ptr_type(ltype))
450 return evaluate_ptr_add(expr, left, right);
452 if (is_ptr_type(rtype))
453 return evaluate_ptr_add(expr, right, left);
455 return evaluate_arith(expr, 1);
458 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
459 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
460 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
461 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED)
463 const char * type_difference(struct symbol *target, struct symbol *source,
464 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
466 for (;;) {
467 unsigned long mod1, mod2, diff;
468 unsigned long as1, as2;
469 int type1, type2;
470 struct symbol *base1, *base2;
472 if (target == source)
473 break;
474 if (!target || !source)
475 return "different types";
477 * Peel of per-node information.
478 * FIXME! Check alignment and context too here!
480 mod1 = target->ctype.modifiers;
481 as1 = target->ctype.as;
482 mod2 = source->ctype.modifiers;
483 as2 = source->ctype.as;
484 if (target->type == SYM_NODE) {
485 target = target->ctype.base_type;
486 if (!target)
487 return "bad types";
488 if (target->type == SYM_PTR) {
489 mod1 = 0;
490 as1 = 0;
492 mod1 |= target->ctype.modifiers;
493 as1 |= target->ctype.as;
495 if (source->type == SYM_NODE) {
496 source = source->ctype.base_type;
497 if (!source)
498 return "bad types";
499 if (source->type == SYM_PTR) {
500 mod2 = 0;
501 as2 = 0;
503 mod2 |= source->ctype.modifiers;
504 as2 |= source->ctype.as;
506 if (target->type == SYM_ENUM) {
507 target = target->ctype.base_type;
508 if (!target)
509 return "bad types";
511 if (source->type == SYM_ENUM) {
512 source = source->ctype.base_type;
513 if (!source)
514 return "bad types";
517 if (target == source)
518 break;
519 if (!target || !source)
520 return "different types";
522 type1 = target->type;
523 base1 = target->ctype.base_type;
525 type2 = source->type;
526 base2 = source->ctype.base_type;
529 * Pointers to functions compare as the function itself
531 if (type1 == SYM_PTR && base1) {
532 switch (base1->type) {
533 case SYM_FN:
534 type1 = SYM_FN;
535 target = base1;
536 base1 = base1->ctype.base_type;
537 default:
538 /* nothing */;
541 if (type2 == SYM_PTR && base2) {
542 switch (base2->type) {
543 case SYM_FN:
544 type2 = SYM_FN;
545 source = base2;
546 base2 = base2->ctype.base_type;
547 default:
548 /* nothing */;
552 /* Arrays degenerate to pointers for type comparisons */
553 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
554 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
556 if (type1 != type2 || type1 == SYM_RESTRICT)
557 return "different base types";
559 /* Must be same address space to be comparable */
560 if (as1 != as2)
561 return "different address spaces";
563 /* Ignore differences in storage types or addressability */
564 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
565 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
566 if (diff) {
567 if (diff & MOD_SIZE)
568 return "different type sizes";
569 if (diff & ~MOD_SIGNEDNESS)
570 return "different modifiers";
572 /* Differs in signedness only.. */
573 if (Wtypesign) {
575 * Warn if both are explicitly signed ("unsigned" is obvously
576 * always explicit, and since we know one of them has to be
577 * unsigned, we check if the signed one was explicit).
579 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
580 return "different explicit signedness";
583 * "char" matches both "unsigned char" and "signed char",
584 * so if the explicit test didn't trigger, then we should
585 * not warn about a char.
587 if (!(mod1 & MOD_CHAR))
588 return "different signedness";
592 if (type1 == SYM_FN) {
593 int i;
594 struct symbol *arg1, *arg2;
595 if (base1->variadic != base2->variadic)
596 return "incompatible variadic arguments";
597 PREPARE_PTR_LIST(target->arguments, arg1);
598 PREPARE_PTR_LIST(source->arguments, arg2);
599 i = 1;
600 for (;;) {
601 const char *diff;
602 diff = type_difference(arg1, arg2, 0, 0);
603 if (diff) {
604 static char argdiff[80];
605 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
606 return argdiff;
608 if (!arg1)
609 break;
610 NEXT_PTR_LIST(arg1);
611 NEXT_PTR_LIST(arg2);
612 i++;
614 FINISH_PTR_LIST(arg2);
615 FINISH_PTR_LIST(arg1);
618 target = base1;
619 source = base2;
621 return NULL;
624 static int is_null_ptr(struct expression *expr)
626 if (expr->type != EXPR_VALUE || expr->value)
627 return 0;
628 if (!is_ptr_type(expr->ctype))
629 warning(expr->pos, "Using plain integer as NULL pointer");
630 return 1;
633 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
635 /* NULL expression? Just return the type of the "other side" */
636 if (is_null_ptr(r))
637 return l->ctype;
638 if (is_null_ptr(l))
639 return r->ctype;
640 return NULL;
644 * Ignore differences in "volatile" and "const"ness when
645 * subtracting pointers
647 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
649 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
651 const char *typediff;
652 struct symbol *ctype;
653 struct symbol *ltype, *rtype;
655 ltype = degenerate(l);
656 rtype = degenerate(r);
659 * If it is an integer subtract: the ptr add case will do the
660 * right thing.
662 if (!is_ptr_type(rtype))
663 return evaluate_ptr_add(expr, l, r);
665 ctype = ltype;
666 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
667 if (typediff) {
668 ctype = common_ptr_type(l, r);
669 if (!ctype) {
670 warning(expr->pos, "subtraction of different types can't work (%s)", typediff);
671 return NULL;
674 examine_symbol_type(ctype);
676 /* Figure out the base type we point to */
677 if (ctype->type == SYM_NODE)
678 ctype = ctype->ctype.base_type;
679 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
680 warning(expr->pos, "subtraction of functions? Share your drugs");
681 return NULL;
683 ctype = ctype->ctype.base_type;
685 expr->ctype = ssize_t_ctype;
686 if (ctype->bit_size > bits_in_char) {
687 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
688 struct expression *div = expr;
689 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
691 val->ctype = size_t_ctype;
692 val->value = ctype->bit_size >> 3;
694 sub->op = '-';
695 sub->ctype = ssize_t_ctype;
696 sub->left = l;
697 sub->right = r;
699 div->op = '/';
700 div->left = sub;
701 div->right = val;
704 return ssize_t_ctype;
707 static struct symbol *evaluate_sub(struct expression *expr)
709 struct expression *left = expr->left, *right = expr->right;
710 struct symbol *ltype = left->ctype;
712 if (is_ptr_type(ltype))
713 return evaluate_ptr_sub(expr, left, right);
715 return evaluate_arith(expr, 1);
718 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
720 static struct symbol *evaluate_conditional(struct expression **p)
722 struct symbol *ctype;
723 struct expression *expr = *p;
725 if (!expr)
726 return NULL;
728 if (expr->type == EXPR_ASSIGNMENT)
729 warning(expr->pos, "assignment expression in conditional");
731 ctype = evaluate_expression(expr);
732 if (ctype) {
733 if (is_safe_type(ctype))
734 warning(expr->pos, "testing a 'safe expression'");
735 if (is_float_type(ctype)) {
736 struct expression *comp;
738 * It's easier to handle here, rather than deal with
739 * FP all over the place. Floating point in boolean
740 * context is rare enough (and very often wrong),
741 * so price of explicit comparison with appropriate
742 * FP zero is not too high. And it simplifies things
743 * elsewhere.
745 comp = alloc_expression(expr->pos, EXPR_BINOP);
746 comp->op = SPECIAL_NOTEQUAL;
747 comp->left = expr;
748 comp->right = alloc_expression(expr->pos, EXPR_FVALUE);
749 comp->right->ctype = comp->left->ctype;
750 comp->right->fvalue = 0;
751 ctype = comp->ctype = &bool_ctype;
752 *p = comp;
756 return ctype;
759 static struct symbol *evaluate_logical(struct expression *expr)
761 if (!evaluate_conditional(&expr->left))
762 return NULL;
763 if (!evaluate_conditional(&expr->right))
764 return NULL;
766 expr->ctype = &bool_ctype;
767 return &bool_ctype;
770 static struct symbol *evaluate_shift(struct expression *expr)
772 struct expression *left = expr->left, *right = expr->right;
773 struct symbol *ltype = left->ctype, *rtype = right->ctype;
775 if (ltype->type == SYM_NODE)
776 ltype = ltype->ctype.base_type;
777 if (rtype->type == SYM_NODE)
778 rtype = rtype->ctype.base_type;
779 if (is_int_type(ltype) && is_int_type(rtype)) {
780 struct symbol *ctype = integer_promotion(ltype);
781 if (ltype->bit_size != ctype->bit_size)
782 expr->left = cast_to(expr->left, ctype);
783 expr->ctype = ctype;
784 ctype = integer_promotion(rtype);
785 if (rtype->bit_size != ctype->bit_size)
786 expr->right = cast_to(expr->right, ctype);
787 return expr->ctype;
789 return bad_expr_type(expr);
792 static struct symbol *evaluate_binop(struct expression *expr)
794 switch (expr->op) {
795 // addition can take ptr+int, fp and int
796 case '+':
797 return evaluate_add(expr);
799 // subtraction can take ptr-ptr, fp and int
800 case '-':
801 return evaluate_sub(expr);
803 // Arithmetic operations can take fp and int
804 case '*': case '/':
805 return evaluate_arith(expr, 1);
807 // shifts do integer promotions, but that's it.
808 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
809 return evaluate_shift(expr);
811 // The rest are integer operations
812 // '%', '&', '^', '|'
813 default:
814 return evaluate_arith(expr, 0);
818 static struct symbol *evaluate_comma(struct expression *expr)
820 expr->ctype = expr->right->ctype;
821 return expr->ctype;
824 static int modify_for_unsigned(int op)
826 if (op == '<')
827 op = SPECIAL_UNSIGNED_LT;
828 else if (op == '>')
829 op = SPECIAL_UNSIGNED_GT;
830 else if (op == SPECIAL_LTE)
831 op = SPECIAL_UNSIGNED_LTE;
832 else if (op == SPECIAL_GTE)
833 op = SPECIAL_UNSIGNED_GTE;
834 return op;
837 static struct symbol *evaluate_compare(struct expression *expr)
839 struct expression *left = expr->left, *right = expr->right;
840 struct symbol *ltype = left->ctype, *rtype = right->ctype;
841 struct symbol *ctype;
843 /* Type types? */
844 if (is_type_type(ltype) && is_type_type(rtype))
845 goto OK;
847 if (is_safe_type(ltype) || is_safe_type(rtype))
848 warning(expr->pos, "testing a 'safe expression'");
850 /* Pointer types? */
851 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
852 // FIXME! Check the types for compatibility
853 goto OK;
856 ctype = compatible_integer_binop(&expr->left, &expr->right);
857 if (ctype) {
858 if (ctype->ctype.modifiers & MOD_UNSIGNED)
859 expr->op = modify_for_unsigned(expr->op);
860 goto OK;
863 ctype = compatible_float_binop(&expr->left, &expr->right);
864 if (ctype)
865 goto OK;
867 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
868 if (ctype)
869 goto OK;
871 bad_expr_type(expr);
874 expr->ctype = &bool_ctype;
875 return &bool_ctype;
879 * FIXME!! This should do casts, array degeneration etc..
881 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
883 struct symbol *ltype = left->ctype, *rtype = right->ctype;
885 if (ltype->type == SYM_NODE)
886 ltype = ltype->ctype.base_type;
888 if (rtype->type == SYM_NODE)
889 rtype = rtype->ctype.base_type;
891 if (ltype->type == SYM_PTR) {
892 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
893 return ltype;
896 if (rtype->type == SYM_PTR) {
897 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
898 return rtype;
900 return NULL;
903 static struct symbol * evaluate_conditional_expression(struct expression *expr)
905 struct expression *cond, *false;
906 struct expression **true_p;
907 struct symbol *ctype, *ltype, *rtype;
908 const char * typediff;
910 ctype = degenerate(expr->conditional);
911 cond = expr->conditional;
913 ltype = ctype;
914 true_p = &expr->conditional;
915 if (expr->cond_true) {
916 ltype = degenerate(expr->cond_true);
917 true_p = &expr->cond_true;
920 rtype = degenerate(expr->cond_false);
921 false = expr->cond_false;
923 ctype = ltype;
924 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
925 if (!typediff)
926 goto out;
928 ctype = compatible_integer_binop(true_p, &expr->cond_false);
929 if (ctype)
930 goto out;
931 ctype = compatible_ptr_type(*true_p, expr->cond_false);
932 if (ctype)
933 goto out;
934 ctype = compatible_float_binop(true_p, &expr->cond_false);
935 if (ctype)
936 goto out;
937 ctype = compatible_restricted_binop('?', true_p, &expr->cond_false);
938 if (ctype)
939 goto out;
940 warning(expr->pos, "incompatible types in conditional expression (%s)", typediff);
941 return NULL;
943 out:
944 expr->ctype = ctype;
945 return ctype;
948 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
949 struct expression **rp, struct symbol *source, const char *where)
951 const char *typediff;
952 struct symbol *t;
953 int target_as;
955 /* It's ok if the target is more volatile or const than the source */
956 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
957 if (!typediff)
958 return 1;
960 if (is_int_type(target)) {
961 if (is_int_type(source)) {
962 if (target->bit_size != source->bit_size)
963 goto Cast;
964 return 1;
966 if (is_float_type(source))
967 goto Cast;
968 } else if (is_float_type(target)) {
969 if (is_int_type(source))
970 goto Cast;
971 if (is_float_type(source)) {
972 if (target->bit_size != source->bit_size)
973 goto Cast;
974 return 1;
978 if (is_restricted_type(target) && !restricted_value(*rp, target))
979 return 1;
981 /* Pointer destination? */
982 t = target;
983 target_as = t->ctype.as;
984 if (t->type == SYM_NODE) {
985 t = t->ctype.base_type;
986 target_as |= t->ctype.as;
988 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
989 struct expression *right = *rp;
990 struct symbol *s = source;
991 int source_as;
993 // NULL pointer is always ok
994 if (is_null_ptr(right))
995 return 1;
997 /* "void *" matches anything as long as the address space is ok */
998 source_as = s->ctype.as;
999 if (s->type == SYM_NODE) {
1000 s = s->ctype.base_type;
1001 source_as |= s->ctype.as;
1003 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1004 s = s->ctype.base_type;
1005 t = t->ctype.base_type;
1006 if (s == &void_ctype || t == &void_ctype)
1007 return 1;
1011 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1012 info(expr->pos, " expected %s", show_typename(target));
1013 info(expr->pos, " got %s", show_typename(source));
1014 *rp = cast_to(*rp, target);
1015 return 0;
1016 Cast:
1017 *rp = cast_to(*rp, target);
1018 return 1;
1022 * FIXME!! This is wrong from a double evaluation standpoint. We can't
1023 * just expand the expression twice, that would make any side effects
1024 * happen twice too.
1026 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
1028 int op = expr->op;
1029 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
1030 static const int op_trans[] = {
1031 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
1032 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
1033 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
1034 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
1035 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
1036 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
1037 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
1038 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
1039 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
1040 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
1042 struct expression *e0, *e1, *e2, *e3, *e4, *e5;
1043 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1044 struct symbol *ltype = left->ctype;
1045 struct expression *addr;
1046 struct symbol *lptype;
1048 if (left->type == EXPR_BITFIELD)
1049 addr = left->address;
1050 else
1051 addr = left->unop;
1053 lptype = addr->ctype;
1055 a->ctype.base_type = lptype;
1056 a->bit_size = lptype->bit_size;
1057 a->array_size = lptype->array_size;
1059 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1060 e0->symbol = a;
1061 e0->ctype = &lazy_ptr_ctype;
1063 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1064 e1->unop = e0;
1065 e1->op = '*';
1066 e1->ctype = lptype;
1068 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1069 e2->left = e1;
1070 e2->right = addr;
1071 e2->op = '=';
1072 e2->ctype = lptype;
1074 /* we can't cannibalize left, unfortunately */
1075 e3 = alloc_expression(expr->pos, left->type);
1076 *e3 = *left;
1077 if (e3->type == EXPR_BITFIELD)
1078 e3->address = e1;
1079 else
1080 e3->unop = e1;
1082 e4 = alloc_expression(expr->pos, EXPR_BINOP);
1083 e4->op = subexpr->op = op_trans[op - SPECIAL_BASE];
1084 e4->left = e3;
1085 e4->right = right;
1086 /* will calculate type later */
1088 e5 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1089 e5->left = e3; /* we can share that one */
1090 e5->right = e4;
1091 e5->op = '=';
1092 e5->ctype = ltype;
1094 expr->type = EXPR_COMMA;
1095 expr->left = e2;
1096 expr->right = e5;
1097 expr->ctype = ltype;
1099 return evaluate_binop(e4);
1102 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1104 if (type->ctype.modifiers & MOD_CONST)
1105 warning(left->pos, "assignment to const expression");
1106 if (type->type == SYM_NODE)
1107 type->ctype.modifiers |= MOD_ASSIGNED;
1110 static struct symbol *evaluate_assignment(struct expression *expr)
1112 struct expression *left = expr->left, *right = expr->right;
1113 struct expression *where = expr;
1114 struct symbol *ltype, *rtype;
1116 if (!lvalue_expression(left)) {
1117 warning(expr->pos, "not an lvalue");
1118 return NULL;
1121 ltype = left->ctype;
1123 if (expr->op != '=') {
1124 if (!evaluate_binop_assignment(expr, left, right))
1125 return NULL;
1126 where = expr->right; /* expr is EXPR_COMMA now */
1127 left = where->left;
1128 right = where->right;
1131 rtype = degenerate(right);
1133 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment"))
1134 return NULL;
1136 evaluate_assign_to(left, ltype);
1138 expr->ctype = ltype;
1139 return ltype;
1142 static void examine_fn_arguments(struct symbol *fn)
1144 struct symbol *s;
1146 FOR_EACH_PTR(fn->arguments, s) {
1147 struct symbol *arg = evaluate_symbol(s);
1148 /* Array/function arguments silently degenerate into pointers */
1149 if (arg) {
1150 struct symbol *ptr;
1151 switch(arg->type) {
1152 case SYM_ARRAY:
1153 case SYM_FN:
1154 ptr = alloc_symbol(s->pos, SYM_PTR);
1155 if (arg->type == SYM_ARRAY)
1156 ptr->ctype = arg->ctype;
1157 else
1158 ptr->ctype.base_type = arg;
1159 ptr->ctype.as |= s->ctype.as;
1160 ptr->ctype.modifiers |= s->ctype.modifiers;
1162 s->ctype.base_type = ptr;
1163 s->ctype.as = 0;
1164 s->ctype.modifiers = 0;
1165 examine_symbol_type(s);
1166 break;
1167 default:
1168 /* nothing */
1169 break;
1172 } END_FOR_EACH_PTR(s);
1175 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1177 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1178 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1179 *newsym = *sym;
1180 newsym->ctype.as = as;
1181 newsym->ctype.modifiers = mod;
1182 sym = newsym;
1184 return sym;
1187 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1189 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1190 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1192 node->ctype.base_type = ptr;
1193 ptr->bit_size = bits_in_pointer;
1194 ptr->ctype.alignment = pointer_alignment;
1196 node->bit_size = bits_in_pointer;
1197 node->ctype.alignment = pointer_alignment;
1199 access_symbol(sym);
1200 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1201 if (sym->ctype.modifiers & MOD_REGISTER) {
1202 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1203 sym->ctype.modifiers &= ~MOD_REGISTER;
1205 if (sym->type == SYM_NODE) {
1206 ptr->ctype.as |= sym->ctype.as;
1207 ptr->ctype.modifiers |= sym->ctype.modifiers;
1208 sym = sym->ctype.base_type;
1210 if (degenerate && sym->type == SYM_ARRAY) {
1211 ptr->ctype.as |= sym->ctype.as;
1212 ptr->ctype.modifiers |= sym->ctype.modifiers;
1213 sym = sym->ctype.base_type;
1215 ptr->ctype.base_type = sym;
1217 return node;
1220 /* Arrays degenerate into pointers on pointer arithmetic */
1221 static struct symbol *degenerate(struct expression *expr)
1223 struct symbol *ctype, *base;
1225 if (!expr)
1226 return NULL;
1227 ctype = expr->ctype;
1228 if (!ctype)
1229 return NULL;
1230 base = ctype;
1231 if (ctype->type == SYM_NODE)
1232 base = ctype->ctype.base_type;
1234 * Arrays degenerate into pointers to the entries, while
1235 * functions degenerate into pointers to themselves.
1236 * If array was part of non-lvalue compound, we create a copy
1237 * of that compound first and then act as if we were dealing with
1238 * the corresponding field in there.
1240 switch (base->type) {
1241 case SYM_ARRAY:
1242 if (expr->type == EXPR_SLICE) {
1243 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1244 struct expression *e0, *e1, *e2, *e3, *e4;
1246 a->ctype.base_type = expr->base->ctype;
1247 a->bit_size = expr->base->ctype->bit_size;
1248 a->array_size = expr->base->ctype->array_size;
1250 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1251 e0->symbol = a;
1252 e0->ctype = &lazy_ptr_ctype;
1254 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1255 e1->unop = e0;
1256 e1->op = '*';
1257 e1->ctype = expr->base->ctype; /* XXX */
1259 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1260 e2->left = e1;
1261 e2->right = expr->base;
1262 e2->op = '=';
1263 e2->ctype = expr->base->ctype;
1265 if (expr->r_bitpos) {
1266 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1267 e3->op = '+';
1268 e3->left = e0;
1269 e3->right = alloc_const_expression(expr->pos,
1270 expr->r_bitpos >> 3);
1271 e3->ctype = &lazy_ptr_ctype;
1272 } else {
1273 e3 = e0;
1276 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1277 e4->left = e2;
1278 e4->right = e3;
1279 e4->ctype = &lazy_ptr_ctype;
1281 expr->unop = e4;
1282 expr->type = EXPR_PREOP;
1283 expr->op = '*';
1285 case SYM_FN:
1286 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1287 warning(expr->pos, "strange non-value function or array");
1288 return NULL;
1290 *expr = *expr->unop;
1291 ctype = create_pointer(expr, ctype, 1);
1292 expr->ctype = ctype;
1293 default:
1294 /* nothing */;
1296 return ctype;
1299 static struct symbol *evaluate_addressof(struct expression *expr)
1301 struct expression *op = expr->unop;
1302 struct symbol *ctype;
1304 if (op->op != '*' || op->type != EXPR_PREOP) {
1305 warning(expr->pos, "not addressable");
1306 return NULL;
1308 ctype = op->ctype;
1309 *expr = *op->unop;
1312 * symbol expression evaluation is lazy about the type
1313 * of the sub-expression, so we may have to generate
1314 * the type here if so..
1316 if (expr->ctype == &lazy_ptr_ctype) {
1317 ctype = create_pointer(expr, ctype, 0);
1318 expr->ctype = ctype;
1320 return expr->ctype;
1324 static struct symbol *evaluate_dereference(struct expression *expr)
1326 struct expression *op = expr->unop;
1327 struct symbol *ctype = op->ctype, *node, *target;
1329 /* Simplify: *&(expr) => (expr) */
1330 if (op->type == EXPR_PREOP && op->op == '&') {
1331 *expr = *op->unop;
1332 return expr->ctype;
1335 /* Dereferencing a node drops all the node information. */
1336 if (ctype->type == SYM_NODE)
1337 ctype = ctype->ctype.base_type;
1339 node = alloc_symbol(expr->pos, SYM_NODE);
1340 target = ctype->ctype.base_type;
1342 switch (ctype->type) {
1343 default:
1344 warning(expr->pos, "cannot derefence this type");
1345 return NULL;
1346 case SYM_PTR:
1347 merge_type(node, ctype);
1348 if (ctype->type != SYM_ARRAY)
1349 break;
1351 * Dereferencing a pointer to an array results in a
1352 * degenerate dereference: the expression becomes
1353 * just a pointer to the entry, and the derefence
1354 * goes away.
1356 *expr = *op;
1358 target = alloc_symbol(expr->pos, SYM_PTR);
1359 target->bit_size = bits_in_pointer;
1360 target->ctype.alignment = pointer_alignment;
1361 merge_type(target, ctype->ctype.base_type);
1362 break;
1364 case SYM_ARRAY:
1365 if (!lvalue_expression(op)) {
1366 warning(op->pos, "non-lvalue array??");
1367 return NULL;
1370 /* Do the implied "addressof" on the array */
1371 *op = *op->unop;
1374 * When an array is dereferenced, we need to pick
1375 * up the attributes of the original node too..
1377 merge_type(node, op->ctype);
1378 merge_type(node, ctype);
1379 break;
1382 node->bit_size = target->bit_size;
1383 node->array_size = target->array_size;
1385 expr->ctype = node;
1386 return node;
1390 * Unary post-ops: x++ and x--
1392 static struct symbol *evaluate_postop(struct expression *expr)
1394 struct expression *op = expr->unop;
1395 struct symbol *ctype = op->ctype;
1397 if (!lvalue_expression(expr->unop)) {
1398 warning(expr->pos, "need lvalue expression for ++/--");
1399 return NULL;
1401 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1402 warning(expr->pos, "bad operation on restricted");
1403 return NULL;
1406 evaluate_assign_to(op, ctype);
1408 expr->ctype = ctype;
1409 return ctype;
1412 static struct symbol *evaluate_sign(struct expression *expr)
1414 struct symbol *ctype = expr->unop->ctype;
1415 if (is_int_type(ctype)) {
1416 struct symbol *rtype = rtype = integer_promotion(ctype);
1417 if (rtype->bit_size != ctype->bit_size)
1418 expr->unop = cast_to(expr->unop, rtype);
1419 ctype = rtype;
1420 } else if (is_float_type(ctype) && expr->op != '~') {
1421 /* no conversions needed */
1422 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1423 /* no conversions needed */
1424 } else {
1425 return bad_expr_type(expr);
1427 if (expr->op == '+')
1428 *expr = *expr->unop;
1429 expr->ctype = ctype;
1430 return ctype;
1433 static struct symbol *evaluate_preop(struct expression *expr)
1435 struct symbol *ctype = expr->unop->ctype;
1437 switch (expr->op) {
1438 case '(':
1439 *expr = *expr->unop;
1440 return ctype;
1442 case '+':
1443 case '-':
1444 case '~':
1445 return evaluate_sign(expr);
1447 case '*':
1448 return evaluate_dereference(expr);
1450 case '&':
1451 return evaluate_addressof(expr);
1453 case SPECIAL_INCREMENT:
1454 case SPECIAL_DECREMENT:
1456 * From a type evaluation standpoint the pre-ops are
1457 * the same as the postops
1459 return evaluate_postop(expr);
1461 case '!':
1462 if (is_safe_type(ctype))
1463 warning(expr->pos, "testing a 'safe expression'");
1464 if (is_float_type(ctype)) {
1465 struct expression *arg = expr->unop;
1466 expr->type = EXPR_BINOP;
1467 expr->op = SPECIAL_EQUAL;
1468 expr->left = arg;
1469 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1470 expr->right->ctype = ctype;
1471 expr->right->fvalue = 0;
1473 ctype = &bool_ctype;
1474 break;
1476 default:
1477 break;
1479 expr->ctype = ctype;
1480 return &bool_ctype;
1483 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1485 struct ptr_list *head = (struct ptr_list *)_list;
1486 struct ptr_list *list = head;
1488 if (!head)
1489 return NULL;
1490 do {
1491 int i;
1492 for (i = 0; i < list->nr; i++) {
1493 struct symbol *sym = (struct symbol *) list->list[i];
1494 if (sym->ident) {
1495 if (sym->ident != ident)
1496 continue;
1497 *offset = sym->offset;
1498 return sym;
1499 } else {
1500 struct symbol *ctype = sym->ctype.base_type;
1501 struct symbol *sub;
1502 if (!ctype)
1503 continue;
1504 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1505 continue;
1506 sub = find_identifier(ident, ctype->symbol_list, offset);
1507 if (!sub)
1508 continue;
1509 *offset += sym->offset;
1510 return sub;
1513 } while ((list = list->next) != head);
1514 return NULL;
1517 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1519 struct expression *add;
1522 * Create a new add-expression
1524 * NOTE! Even if we just add zero, we need a new node
1525 * for the member pointer, since it has a different
1526 * type than the original pointer. We could make that
1527 * be just a cast, but the fact is, a node is a node,
1528 * so we might as well just do the "add zero" here.
1530 add = alloc_expression(expr->pos, EXPR_BINOP);
1531 add->op = '+';
1532 add->left = expr;
1533 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1534 add->right->ctype = &int_ctype;
1535 add->right->value = offset;
1538 * The ctype of the pointer will be lazily evaluated if
1539 * we ever take the address of this member dereference..
1541 add->ctype = &lazy_ptr_ctype;
1542 return add;
1545 /* structure/union dereference */
1546 static struct symbol *evaluate_member_dereference(struct expression *expr)
1548 int offset;
1549 struct symbol *ctype, *member;
1550 struct expression *deref = expr->deref, *add;
1551 struct ident *ident = expr->member;
1552 unsigned int mod;
1553 int address_space;
1555 if (!evaluate_expression(deref))
1556 return NULL;
1557 if (!ident) {
1558 warning(expr->pos, "bad member name");
1559 return NULL;
1562 ctype = deref->ctype;
1563 address_space = ctype->ctype.as;
1564 mod = ctype->ctype.modifiers;
1565 if (ctype->type == SYM_NODE) {
1566 ctype = ctype->ctype.base_type;
1567 address_space |= ctype->ctype.as;
1568 mod |= ctype->ctype.modifiers;
1570 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1571 warning(expr->pos, "expected structure or union");
1572 return NULL;
1574 offset = 0;
1575 member = find_identifier(ident, ctype->symbol_list, &offset);
1576 if (!member) {
1577 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1578 const char *name = "<unnamed>";
1579 int namelen = 9;
1580 if (ctype->ident) {
1581 name = ctype->ident->name;
1582 namelen = ctype->ident->len;
1584 warning(expr->pos, "no member '%s' in %s %.*s",
1585 show_ident(ident), type, namelen, name);
1586 return NULL;
1590 * The member needs to take on the address space and modifiers of
1591 * the "parent" type.
1593 member = convert_to_as_mod(member, address_space, mod);
1594 ctype = member->ctype.base_type;
1596 if (!lvalue_expression(deref)) {
1597 if (deref->type != EXPR_SLICE) {
1598 expr->base = deref;
1599 expr->r_bitpos = 0;
1600 } else {
1601 expr->base = deref->base;
1602 expr->r_bitpos = deref->r_bitpos;
1604 expr->r_bitpos += offset << 3;
1605 expr->type = EXPR_SLICE;
1606 if (ctype->type == SYM_BITFIELD) {
1607 expr->r_bitpos += member->bit_offset;
1608 expr->r_nrbits = member->fieldwidth;
1609 } else {
1610 expr->r_nrbits = member->bit_size;
1612 expr->ctype = member;
1613 return member;
1616 deref = deref->unop;
1617 expr->deref = deref;
1619 add = evaluate_offset(deref, offset);
1620 if (ctype->type == SYM_BITFIELD) {
1621 expr->type = EXPR_BITFIELD;
1622 expr->bitpos = member->bit_offset;
1623 expr->nrbits = member->fieldwidth;
1624 expr->address = add;
1625 } else {
1626 expr->type = EXPR_PREOP;
1627 expr->op = '*';
1628 expr->unop = add;
1631 expr->ctype = member;
1632 return member;
1635 static int is_promoted(struct expression *expr)
1637 while (1) {
1638 switch (expr->type) {
1639 case EXPR_BINOP:
1640 case EXPR_SELECT:
1641 case EXPR_CONDITIONAL:
1642 return 1;
1643 case EXPR_COMMA:
1644 expr = expr->right;
1645 continue;
1646 case EXPR_PREOP:
1647 switch (expr->op) {
1648 case '(':
1649 expr = expr->unop;
1650 continue;
1651 case '+':
1652 case '-':
1653 case '~':
1654 return 1;
1655 default:
1656 return 0;
1658 default:
1659 return 0;
1665 static struct symbol *evaluate_cast(struct expression *);
1667 static struct symbol *evaluate_sizeof(struct expression *expr)
1669 struct expression *what = expr->cast_expression;
1670 int size;
1672 if (expr->cast_type) {
1673 if (what) {
1674 struct symbol *sym = evaluate_cast(expr);
1675 size = sym->bit_size;
1676 } else {
1677 examine_symbol_type(expr->cast_type);
1678 size = expr->cast_type->bit_size;
1680 } else {
1681 if (!evaluate_expression(what))
1682 return NULL;
1683 size = what->ctype->bit_size;
1684 if (is_restricted_type(what->ctype)) {
1685 if (size < bits_in_int && is_promoted(what))
1686 size = bits_in_int;
1688 if (is_bitfield_type(what->ctype))
1689 warning(expr->pos, "sizeof applied to bitfield type");
1691 if (size & 7)
1692 warning(expr->pos, "cannot size expression");
1693 expr->type = EXPR_VALUE;
1694 expr->value = size >> 3;
1695 expr->ctype = size_t_ctype;
1696 return size_t_ctype;
1699 static struct symbol *evaluate_alignof(struct expression *expr)
1701 struct symbol *type = expr->cast_type;
1703 if (!type) {
1704 type = evaluate_expression(expr->cast_expression);
1705 if (!type)
1706 return NULL;
1708 if (is_bitfield_type(type))
1709 warning(expr->pos, "alignof applied to bitfield type");
1710 examine_symbol_type(type);
1711 expr->type = EXPR_VALUE;
1712 expr->value = type->ctype.alignment;
1713 expr->ctype = size_t_ctype;
1714 return size_t_ctype;
1717 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1719 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1720 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1721 return clash != 0;
1724 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1726 struct expression *expr;
1727 struct symbol_list *argument_types = fn->arguments;
1728 struct symbol *argtype;
1729 int i = 1;
1731 PREPARE_PTR_LIST(argument_types, argtype);
1732 FOR_EACH_PTR (head, expr) {
1733 struct expression **p = THIS_ADDRESS(expr);
1734 struct symbol *ctype, *target;
1735 ctype = evaluate_expression(expr);
1737 if (!ctype)
1738 return 0;
1740 if (context_clash(f, ctype))
1741 warning(expr->pos, "argument %d used in wrong context", i);
1743 ctype = degenerate(expr);
1745 target = argtype;
1746 if (!target && ctype->bit_size < bits_in_int)
1747 target = &int_ctype;
1748 if (target) {
1749 static char where[30];
1750 examine_symbol_type(target);
1751 sprintf(where, "argument %d", i);
1752 compatible_assignment_types(expr, target, p, ctype, where);
1755 i++;
1756 NEXT_PTR_LIST(argtype);
1757 } END_FOR_EACH_PTR(expr);
1758 FINISH_PTR_LIST(argtype);
1759 return 1;
1762 static int evaluate_initializer(struct symbol *ctype, struct expression **ep);
1764 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1766 struct expression *entry = *ep;
1767 struct expression **parent, *reuse = NULL;
1768 unsigned long offset;
1769 struct symbol *sym;
1770 unsigned long from, to;
1771 int accept_string = is_byte_type(ctype);
1773 from = current;
1774 to = from+1;
1775 parent = ep;
1776 if (entry->type == EXPR_INDEX) {
1777 from = entry->idx_from;
1778 to = entry->idx_to+1;
1779 parent = &entry->idx_expression;
1780 reuse = entry;
1781 entry = entry->idx_expression;
1784 offset = from * (ctype->bit_size>>3);
1785 if (offset) {
1786 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1787 reuse->type = EXPR_POS;
1788 reuse->ctype = ctype;
1789 reuse->init_offset = offset;
1790 reuse->init_nr = to - from;
1791 reuse->init_expr = entry;
1792 parent = &reuse->init_expr;
1793 entry = reuse;
1795 *ep = entry;
1797 if (accept_string && entry->type == EXPR_STRING) {
1798 sym = evaluate_expression(entry);
1799 to = from + get_expression_value(sym->array_size);
1800 } else {
1801 evaluate_initializer(ctype, parent);
1803 return to;
1806 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1808 struct expression *entry;
1809 int current = 0;
1810 int max = 0;
1812 FOR_EACH_PTR(expr->expr_list, entry) {
1813 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1814 if (current > max)
1815 max = current;
1816 } END_FOR_EACH_PTR(entry);
1817 return max;
1820 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1821 static int evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1823 if (expression_list_size(expr->expr_list) != 1) {
1824 warning(expr->pos, "unexpected compound initializer");
1825 return 0;
1827 return evaluate_array_initializer(ctype, expr);
1830 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1832 struct symbol *sym;
1834 FOR_EACH_PTR(ctype->symbol_list, sym) {
1835 if (sym->ident == ident)
1836 return sym;
1837 } END_FOR_EACH_PTR(sym);
1838 return NULL;
1841 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1843 struct expression *entry = *ep;
1844 struct expression **parent;
1845 struct expression *reuse = NULL;
1846 unsigned long offset;
1848 if (!sym) {
1849 error(entry->pos, "unknown named initializer");
1850 return -1;
1853 if (entry->type == EXPR_IDENTIFIER) {
1854 reuse = entry;
1855 entry = entry->ident_expression;
1858 parent = ep;
1859 offset = sym->offset;
1860 if (offset) {
1861 if (!reuse)
1862 reuse = alloc_expression(entry->pos, EXPR_POS);
1863 reuse->type = EXPR_POS;
1864 reuse->ctype = ctype;
1865 reuse->init_offset = offset;
1866 reuse->init_nr = 1;
1867 reuse->init_expr = entry;
1868 parent = &reuse->init_expr;
1869 entry = reuse;
1871 *ep = entry;
1872 evaluate_initializer(sym, parent);
1873 return 0;
1876 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1878 struct expression *entry;
1879 struct symbol *sym;
1881 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1882 FOR_EACH_PTR(expr->expr_list, entry) {
1883 if (entry->type == EXPR_IDENTIFIER) {
1884 struct ident *ident = entry->expr_ident;
1885 /* We special-case the "already right place" case */
1886 if (!sym || sym->ident != ident) {
1887 RESET_PTR_LIST(sym);
1888 for (;;) {
1889 if (!sym)
1890 break;
1891 if (sym->ident == ident)
1892 break;
1893 NEXT_PTR_LIST(sym);
1897 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1898 return 0;
1899 NEXT_PTR_LIST(sym);
1900 } END_FOR_EACH_PTR(entry);
1901 FINISH_PTR_LIST(sym);
1903 return 0;
1907 * Initializers are kind of like assignments. Except
1908 * they can be a hell of a lot more complex.
1910 static int evaluate_initializer(struct symbol *ctype, struct expression **ep)
1912 struct expression *expr = *ep;
1915 * Simple non-structure/array initializers are the simple
1916 * case, and look (and parse) largely like assignments.
1918 switch (expr->type) {
1919 default: {
1920 int size = 0, is_string = expr->type == EXPR_STRING;
1921 struct symbol *rtype = evaluate_expression(expr);
1922 if (rtype) {
1924 * Special case:
1925 * char array[] = "string"
1926 * should _not_ degenerate.
1928 if (is_string && is_string_type(ctype)) {
1929 struct expression *array_size = ctype->array_size;
1930 if (!array_size)
1931 array_size = ctype->array_size = rtype->array_size;
1932 size = get_expression_value(array_size);
1933 } else {
1934 rtype = degenerate(expr);
1935 size = 1;
1937 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1939 return size;
1942 case EXPR_INITIALIZER:
1943 expr->ctype = ctype;
1944 if (ctype->type == SYM_NODE)
1945 ctype = ctype->ctype.base_type;
1947 switch (ctype->type) {
1948 case SYM_ARRAY:
1949 case SYM_PTR:
1950 return evaluate_array_initializer(ctype->ctype.base_type, expr);
1951 case SYM_UNION:
1952 return evaluate_struct_or_union_initializer(ctype, expr, 0);
1953 case SYM_STRUCT:
1954 return evaluate_struct_or_union_initializer(ctype, expr, 1);
1955 default:
1956 return evaluate_scalar_initializer(ctype, expr);
1959 case EXPR_IDENTIFIER:
1960 if (ctype->type == SYM_NODE)
1961 ctype = ctype->ctype.base_type;
1962 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
1963 error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
1964 show_symbol(ctype);
1965 return 0;
1967 return evaluate_one_struct_initializer(ctype, ep,
1968 find_struct_ident(ctype, expr->expr_ident));
1970 case EXPR_INDEX:
1971 if (ctype->type == SYM_NODE)
1972 ctype = ctype->ctype.base_type;
1973 if (ctype->type != SYM_ARRAY) {
1974 error(expr->pos, "expected array");
1975 return 0;
1977 return evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
1979 case EXPR_POS:
1981 * An EXPR_POS expression has already been evaluated, and we don't
1982 * need to do anything more
1984 return 0;
1988 static int get_as(struct symbol *sym)
1990 int as;
1991 unsigned long mod;
1993 if (!sym)
1994 return 0;
1995 as = sym->ctype.as;
1996 mod = sym->ctype.modifiers;
1997 if (sym->type == SYM_NODE) {
1998 sym = sym->ctype.base_type;
1999 as |= sym->ctype.as;
2000 mod |= sym->ctype.modifiers;
2004 * At least for now, allow casting to a "unsigned long".
2005 * That's how we do things like pointer arithmetic and
2006 * store pointers to registers.
2008 if (sym == &ulong_ctype)
2009 return -1;
2011 if (sym && sym->type == SYM_PTR) {
2012 sym = sym->ctype.base_type;
2013 as |= sym->ctype.as;
2014 mod |= sym->ctype.modifiers;
2016 if (mod & MOD_FORCE)
2017 return -1;
2018 return as;
2021 static struct symbol *evaluate_cast(struct expression *expr)
2023 struct expression *target = expr->cast_expression;
2024 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2025 enum type type;
2027 if (!target)
2028 return NULL;
2030 expr->ctype = ctype;
2031 expr->cast_type = ctype;
2034 * Special case: a cast can be followed by an
2035 * initializer, in which case we need to pass
2036 * the type value down to that initializer rather
2037 * than trying to evaluate it as an expression
2039 * A more complex case is when the initializer is
2040 * dereferenced as part of a post-fix expression.
2041 * We need to produce an expression that can be dereferenced.
2043 if (target->type == EXPR_INITIALIZER) {
2044 struct symbol *sym = expr->cast_type;
2045 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2047 sym->initializer = expr->cast_expression;
2048 evaluate_symbol(sym);
2050 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2051 addr->symbol = sym;
2053 expr->type = EXPR_PREOP;
2054 expr->op = '*';
2055 expr->unop = addr;
2056 expr->ctype = sym;
2058 return sym;
2061 evaluate_expression(target);
2062 degenerate(target);
2065 * You can always throw a value away by casting to
2066 * "void" - that's an implicit "force". Note that
2067 * the same is _not_ true of "void *".
2069 if (ctype == &void_ctype)
2070 goto out;
2072 type = ctype->type;
2073 if (type == SYM_NODE) {
2074 type = ctype->ctype.base_type->type;
2075 if (ctype->ctype.base_type == &void_ctype)
2076 goto out;
2078 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2079 warning(expr->pos, "cast to non-scalar");
2081 if (!target->ctype) {
2082 warning(expr->pos, "cast from unknown type");
2083 goto out;
2086 type = target->ctype->type;
2087 if (type == SYM_NODE)
2088 type = target->ctype->ctype.base_type->type;
2089 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2090 warning(expr->pos, "cast from non-scalar");
2092 if (!get_as(ctype) && get_as(target->ctype) > 0)
2093 warning(expr->pos, "cast removes address space of expression");
2095 if (!(ctype->ctype.modifiers & MOD_FORCE)) {
2096 struct symbol *t1 = ctype, *t2 = target->ctype;
2097 if (t1->type == SYM_NODE)
2098 t1 = t1->ctype.base_type;
2099 if (t2->type == SYM_NODE)
2100 t2 = t2->ctype.base_type;
2101 if (t1 != t2) {
2102 if (t1->type == SYM_RESTRICT)
2103 warning(expr->pos, "cast to restricted type");
2104 if (t2->type == SYM_RESTRICT)
2105 warning(expr->pos, "cast from restricted type");
2110 * Casts of constant values are special: they
2111 * can be NULL, and thus need to be simplified
2112 * early.
2114 if (target->type == EXPR_VALUE)
2115 cast_value(expr, ctype, target, target->ctype);
2117 out:
2118 return ctype;
2122 * Evaluate a call expression with a symbol. This
2123 * should expand inline functions, and evaluate
2124 * builtins.
2126 static int evaluate_symbol_call(struct expression *expr)
2128 struct expression *fn = expr->fn;
2129 struct symbol *ctype = fn->ctype;
2131 if (fn->type != EXPR_PREOP)
2132 return 0;
2134 if (ctype->op && ctype->op->evaluate)
2135 return ctype->op->evaluate(expr);
2137 if (ctype->ctype.modifiers & MOD_INLINE) {
2138 int ret;
2139 struct symbol *curr = current_fn;
2140 unsigned long context = current_context;
2141 unsigned long mask = current_contextmask;
2143 current_context |= ctype->ctype.context;
2144 current_contextmask |= ctype->ctype.contextmask;
2145 current_fn = ctype->ctype.base_type;
2146 examine_fn_arguments(current_fn);
2148 ret = inline_function(expr, ctype);
2150 /* restore the old function context */
2151 current_fn = curr;
2152 current_context = context;
2153 current_contextmask = mask;
2154 return ret;
2157 return 0;
2160 static struct symbol *evaluate_call(struct expression *expr)
2162 int args, fnargs;
2163 struct symbol *ctype, *sym;
2164 struct expression *fn = expr->fn;
2165 struct expression_list *arglist = expr->args;
2167 if (!evaluate_expression(fn))
2168 return NULL;
2169 sym = ctype = fn->ctype;
2170 if (ctype->type == SYM_NODE)
2171 ctype = ctype->ctype.base_type;
2172 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2173 ctype = ctype->ctype.base_type;
2174 if (!evaluate_arguments(sym, ctype, arglist))
2175 return NULL;
2176 if (ctype->type != SYM_FN) {
2177 warning(expr->pos, "not a function %s", show_ident(sym->ident));
2178 return NULL;
2180 args = expression_list_size(expr->args);
2181 fnargs = symbol_list_size(ctype->arguments);
2182 if (args < fnargs)
2183 warning(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2184 if (args > fnargs && !ctype->variadic)
2185 warning(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2186 if (sym->type == SYM_NODE) {
2187 if (evaluate_symbol_call(expr))
2188 return expr->ctype;
2190 expr->ctype = ctype->ctype.base_type;
2191 return expr->ctype;
2194 struct symbol *evaluate_expression(struct expression *expr)
2196 if (!expr)
2197 return NULL;
2198 if (expr->ctype)
2199 return expr->ctype;
2201 switch (expr->type) {
2202 case EXPR_VALUE:
2203 case EXPR_FVALUE:
2204 warning(expr->pos, "value expression without a type");
2205 return NULL;
2206 case EXPR_STRING:
2207 return evaluate_string(expr);
2208 case EXPR_SYMBOL:
2209 return evaluate_symbol_expression(expr);
2210 case EXPR_BINOP:
2211 if (!evaluate_expression(expr->left))
2212 return NULL;
2213 if (!evaluate_expression(expr->right))
2214 return NULL;
2215 return evaluate_binop(expr);
2216 case EXPR_LOGICAL:
2217 return evaluate_logical(expr);
2218 case EXPR_COMMA:
2219 evaluate_expression(expr->left);
2220 if (!evaluate_expression(expr->right))
2221 return NULL;
2222 return evaluate_comma(expr);
2223 case EXPR_COMPARE:
2224 if (!evaluate_expression(expr->left))
2225 return NULL;
2226 if (!evaluate_expression(expr->right))
2227 return NULL;
2228 return evaluate_compare(expr);
2229 case EXPR_ASSIGNMENT:
2230 if (!evaluate_expression(expr->left))
2231 return NULL;
2232 if (!evaluate_expression(expr->right))
2233 return NULL;
2234 return evaluate_assignment(expr);
2235 case EXPR_PREOP:
2236 if (!evaluate_expression(expr->unop))
2237 return NULL;
2238 return evaluate_preop(expr);
2239 case EXPR_POSTOP:
2240 if (!evaluate_expression(expr->unop))
2241 return NULL;
2242 return evaluate_postop(expr);
2243 case EXPR_CAST:
2244 return evaluate_cast(expr);
2245 case EXPR_SIZEOF:
2246 return evaluate_sizeof(expr);
2247 case EXPR_ALIGNOF:
2248 return evaluate_alignof(expr);
2249 case EXPR_DEREF:
2250 return evaluate_member_dereference(expr);
2251 case EXPR_CALL:
2252 return evaluate_call(expr);
2253 case EXPR_BITFIELD:
2254 warning(expr->pos, "bitfield generated by parser");
2255 return NULL;
2256 case EXPR_SELECT:
2257 case EXPR_CONDITIONAL:
2258 if (!evaluate_conditional(&expr->conditional))
2259 return NULL;
2260 if (!evaluate_expression(expr->cond_false))
2261 return NULL;
2262 if (expr->cond_true && !evaluate_expression(expr->cond_true))
2263 return NULL;
2264 return evaluate_conditional_expression(expr);
2265 case EXPR_STATEMENT:
2266 expr->ctype = evaluate_statement(expr->statement);
2267 return expr->ctype;
2269 case EXPR_LABEL:
2270 expr->ctype = &ptr_ctype;
2271 return &ptr_ctype;
2273 case EXPR_TYPE:
2274 /* Evaluate the type of the symbol .. */
2275 evaluate_symbol(expr->symbol);
2276 /* .. but the type of the _expression_ is a "type" */
2277 expr->ctype = &type_ctype;
2278 return &type_ctype;
2280 /* These can not exist as stand-alone expressions */
2281 case EXPR_INITIALIZER:
2282 case EXPR_IDENTIFIER:
2283 case EXPR_INDEX:
2284 case EXPR_POS:
2285 warning(expr->pos, "internal front-end error: initializer in expression");
2286 return NULL;
2287 case EXPR_SLICE:
2288 warning(expr->pos, "internal front-end error: SLICE re-evaluated");
2289 return NULL;
2291 return NULL;
2294 void check_duplicates(struct symbol *sym)
2296 struct symbol *next = sym;
2298 while ((next = next->same_symbol) != NULL) {
2299 const char *typediff;
2300 evaluate_symbol(next);
2301 typediff = type_difference(sym, next, 0, 0);
2302 if (typediff) {
2303 warning(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2304 show_ident(sym->ident),
2305 input_streams[next->pos.stream].name, next->pos.line, typediff);
2306 return;
2311 struct symbol *evaluate_symbol(struct symbol *sym)
2313 struct symbol *base_type;
2315 if (!sym)
2316 return sym;
2318 sym = examine_symbol_type(sym);
2319 base_type = sym->ctype.base_type;
2320 if (!base_type)
2321 return NULL;
2323 /* Evaluate the initializers */
2324 if (sym->initializer) {
2325 int count = evaluate_initializer(sym, &sym->initializer);
2326 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
2327 int bit_size = count * base_type->ctype.base_type->bit_size;
2328 base_type->array_size = alloc_const_expression(sym->pos, count);
2329 base_type->bit_size = bit_size;
2330 sym->array_size = base_type->array_size;
2331 sym->bit_size = bit_size;
2335 /* And finally, evaluate the body of the symbol too */
2336 if (base_type->type == SYM_FN) {
2337 struct symbol *curr = current_fn;
2338 unsigned long context = current_context;
2339 unsigned long mask = current_contextmask;
2341 current_fn = base_type;
2342 current_contextmask = sym->ctype.contextmask;
2343 current_context = sym->ctype.context;
2345 examine_fn_arguments(base_type);
2346 if (!base_type->stmt && base_type->inline_stmt)
2347 uninline(sym);
2348 if (base_type->stmt)
2349 evaluate_statement(base_type->stmt);
2351 current_fn = curr;
2352 current_contextmask = mask;
2353 current_context = context;
2356 return base_type;
2359 struct symbol *evaluate_return_expression(struct statement *stmt)
2361 struct expression *expr = stmt->expression;
2362 struct symbol *ctype, *fntype;
2364 evaluate_expression(expr);
2365 ctype = degenerate(expr);
2366 fntype = current_fn->ctype.base_type;
2367 if (!fntype || fntype == &void_ctype) {
2368 if (expr && ctype != &void_ctype)
2369 warning(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2370 return NULL;
2373 if (!expr) {
2374 warning(stmt->pos, "return with no return value");
2375 return NULL;
2377 if (!ctype)
2378 return NULL;
2379 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
2380 return NULL;
2383 static void evaluate_if_statement(struct statement *stmt)
2385 if (!stmt->if_conditional)
2386 return;
2388 evaluate_conditional(&stmt->if_conditional);
2389 evaluate_statement(stmt->if_true);
2390 evaluate_statement(stmt->if_false);
2393 static void evaluate_iterator(struct statement *stmt)
2395 struct expression **pre = &stmt->iterator_pre_condition;
2396 struct expression **post = &stmt->iterator_post_condition;
2397 if (*pre == *post) {
2398 evaluate_conditional(pre);
2399 *post = *pre;
2400 } else {
2401 evaluate_conditional(pre);
2402 evaluate_conditional(post);
2404 evaluate_statement(stmt->iterator_pre_statement);
2405 evaluate_statement(stmt->iterator_statement);
2406 evaluate_statement(stmt->iterator_post_statement);
2409 struct symbol *evaluate_statement(struct statement *stmt)
2411 if (!stmt)
2412 return NULL;
2414 switch (stmt->type) {
2415 case STMT_RETURN:
2416 return evaluate_return_expression(stmt);
2418 case STMT_EXPRESSION:
2419 if (!evaluate_expression(stmt->expression))
2420 return NULL;
2421 return degenerate(stmt->expression);
2423 case STMT_COMPOUND: {
2424 struct statement *s;
2425 struct symbol *type = NULL;
2426 struct symbol *sym;
2428 /* Evaluate each symbol in the compound statement */
2429 FOR_EACH_PTR(stmt->syms, sym) {
2430 evaluate_symbol(sym);
2431 } END_FOR_EACH_PTR(sym);
2432 evaluate_symbol(stmt->ret);
2435 * Then, evaluate each statement, making the type of the
2436 * compound statement be the type of the last statement
2438 type = NULL;
2439 FOR_EACH_PTR(stmt->stmts, s) {
2440 type = evaluate_statement(s);
2441 } END_FOR_EACH_PTR(s);
2442 if (!type)
2443 type = &void_ctype;
2444 return type;
2446 case STMT_IF:
2447 evaluate_if_statement(stmt);
2448 return NULL;
2449 case STMT_ITERATOR:
2450 evaluate_iterator(stmt);
2451 return NULL;
2452 case STMT_SWITCH:
2453 evaluate_expression(stmt->switch_expression);
2454 evaluate_statement(stmt->switch_statement);
2455 return NULL;
2456 case STMT_CASE:
2457 evaluate_expression(stmt->case_expression);
2458 evaluate_expression(stmt->case_to);
2459 evaluate_statement(stmt->case_statement);
2460 return NULL;
2461 case STMT_LABEL:
2462 return evaluate_statement(stmt->label_statement);
2463 case STMT_GOTO:
2464 evaluate_expression(stmt->goto_expression);
2465 return NULL;
2466 case STMT_NONE:
2467 break;
2468 case STMT_ASM:
2469 /* FIXME! Do the asm parameter evaluation! */
2470 break;
2472 return NULL;