If int/long are the same size, an int that overflows into
[smatch.git] / evaluate.c
blob128553bec67449bede25017d66c2d5c220b05c4c
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "parse.h"
23 #include "token.h"
24 #include "symbol.h"
25 #include "target.h"
26 #include "expression.h"
28 static struct symbol *current_fn;
29 static int current_context, current_contextmask;
31 static struct symbol *degenerate(struct expression *expr);
33 static struct symbol *evaluate_symbol_expression(struct expression *expr)
35 struct symbol *sym = expr->symbol;
36 struct symbol *base_type;
38 if (!sym) {
39 if (preprocessing) {
40 expr->ctype = &int_ctype;
41 return &int_ctype;
43 warn(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
44 return NULL;
47 examine_symbol_type(sym);
48 if ((sym->ctype.context ^ current_context) & (sym->ctype.contextmask & current_contextmask))
49 warn(expr->pos, "Using symbol '%s' in wrong context", show_ident(expr->symbol_name));
51 base_type = sym->ctype.base_type;
52 if (!base_type) {
53 warn(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
54 return NULL;
57 /* The type of a symbol is the symbol itself! */
58 expr->ctype = sym;
60 /* enum's can be turned into plain values */
61 if (sym->type != SYM_ENUM) {
62 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
63 addr->symbol = sym;
64 addr->symbol_name = expr->symbol_name;
65 addr->ctype = NULL; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
66 expr->type = EXPR_PREOP;
67 expr->op = '*';
68 expr->unop = addr;
69 return sym;
71 expr->type = EXPR_VALUE;
72 expr->value = sym->value;
73 expr->ctype = base_type;
74 return sym;
77 static struct symbol *evaluate_string(struct expression *expr)
79 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
80 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
81 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
82 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
83 unsigned int length = expr->string->length;
85 sym->array_size = alloc_const_expression(expr->pos, length);
86 sym->bit_size = bits_in_char * length;
87 sym->ctype.alignment = 1;
88 sym->ctype.modifiers = MOD_STATIC;
89 sym->ctype.base_type = array;
90 sym->initializer = initstr;
92 initstr->ctype = sym;
93 initstr->string = expr->string;
95 array->array_size = sym->array_size;
96 array->bit_size = bits_in_char * length;
97 array->ctype.alignment = 1;
98 array->ctype.modifiers = MOD_STATIC;
99 array->ctype.base_type = &char_ctype;
101 addr->symbol = sym;
102 addr->ctype = NULL;
104 expr->type = EXPR_PREOP;
105 expr->op = '*';
106 expr->unop = addr;
107 expr->ctype = sym;
108 return sym;
111 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
113 unsigned long lmod, rmod, mod;
115 if (left == right)
116 goto left;
118 if (left->bit_size > right->bit_size)
119 goto left;
121 if (right->bit_size > left->bit_size)
122 goto right;
124 /* Same size integers - promote to unsigned, promote to long */
125 lmod = left->ctype.modifiers;
126 rmod = right->ctype.modifiers;
127 mod = lmod | rmod;
128 if (mod == lmod)
129 goto left;
130 if (mod == rmod)
131 goto right;
132 return ctype_integer(mod);
134 right:
135 left = right;
136 left:
137 if (left->bit_size < bits_in_int)
138 left = &int_ctype;
139 return left;
142 static struct expression * cast_to(struct expression *old, struct symbol *type)
144 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
145 expr->ctype = type;
146 expr->cast_type = type;
147 expr->cast_expression = old;
148 return expr;
151 static int is_type_type(struct symbol *type)
153 return (type->ctype.modifiers & MOD_TYPE) != 0;
156 static int is_ptr_type(struct symbol *type)
158 if (type->type == SYM_NODE)
159 type = type->ctype.base_type;
160 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
163 static int is_int_type(struct symbol *type)
165 if (type->type == SYM_NODE)
166 type = type->ctype.base_type;
167 return (type->type == SYM_BITFIELD) || type->ctype.base_type == &int_type;
170 static struct symbol *bad_expr_type(struct expression *expr)
172 warn(expr->pos, "incompatible types for operation");
173 return NULL;
176 static struct symbol * compatible_integer_binop(struct expression *expr, struct expression **lp, struct expression **rp)
178 struct expression *left = *lp, *right = *rp;
179 struct symbol *ltype = left->ctype, *rtype = right->ctype;
181 if (ltype->type == SYM_NODE)
182 ltype = ltype->ctype.base_type;
183 if (rtype->type == SYM_NODE)
184 rtype = rtype->ctype.base_type;
185 /* Integer promotion? */
186 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
187 ltype = &int_ctype;
188 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
189 rtype = &int_ctype;
190 if (is_int_type(ltype) && is_int_type(rtype)) {
191 struct symbol *ctype = bigger_int_type(ltype, rtype);
193 /* Don't bother promoting same-size entities, it only adds clutter */
194 if (ltype->bit_size != ctype->bit_size)
195 *lp = cast_to(left, ctype);
196 if (rtype->bit_size != ctype->bit_size)
197 *rp = cast_to(right, ctype);
198 return ctype;
200 return NULL;
203 static struct symbol *evaluate_int_binop(struct expression *expr)
205 struct symbol *ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
206 if (ctype) {
207 expr->ctype = ctype;
208 return ctype;
210 return bad_expr_type(expr);
213 static inline int lvalue_expression(struct expression *expr)
215 while (expr->type == EXPR_CAST)
216 expr = expr->cast_expression;
217 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
220 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
222 struct symbol *ctype;
223 struct symbol *ptr_type = ptr->ctype;
224 struct symbol *i_type = i->ctype;
225 int bit_size;
227 if (i_type->type == SYM_NODE)
228 i_type = i_type->ctype.base_type;
229 if (ptr_type->type == SYM_NODE)
230 ptr_type = ptr_type->ctype.base_type;
232 if (i_type->type == SYM_ENUM)
233 i_type = &int_ctype;
234 if (!is_int_type(i_type))
235 return bad_expr_type(expr);
237 ctype = ptr->ctype;
238 examine_symbol_type(ctype);
240 ctype = degenerate(ptr);
241 if (!ctype->ctype.base_type) {
242 warn(expr->pos, "missing type information");
243 return NULL;
246 /* Get the size of whatever the pointer points to */
247 ptr_type = ctype;
248 if (ptr_type->type == SYM_NODE)
249 ptr_type = ptr_type->ctype.base_type;
250 if (ptr_type->type == SYM_PTR)
251 ptr_type = ptr_type->ctype.base_type;
252 bit_size = ptr_type->bit_size;
254 /* Special case: adding zero commonly happens as a result of 'array[0]' */
255 if (i->type == EXPR_VALUE && !i->value) {
256 *expr = *ptr;
257 } else if (bit_size > bits_in_char) {
258 struct expression *add = expr;
259 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
260 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
262 val->ctype = size_t_ctype;
263 val->value = bit_size >> 3;
265 mul->op = '*';
266 mul->ctype = size_t_ctype;
267 mul->left = i;
268 mul->right = val;
270 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
271 add->left = ptr;
272 add->right = mul;
275 expr->ctype = ctype;
276 return ctype;
279 static struct symbol *evaluate_add(struct expression *expr)
281 struct expression *left = expr->left, *right = expr->right;
282 struct symbol *ltype = left->ctype, *rtype = right->ctype;
284 if (is_ptr_type(ltype))
285 return evaluate_ptr_add(expr, left, right);
287 if (is_ptr_type(rtype))
288 return evaluate_ptr_add(expr, right, left);
290 // FIXME! FP promotion
291 return evaluate_int_binop(expr);
294 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
295 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE)
297 const char * type_difference(struct symbol *target, struct symbol *source,
298 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
300 for (;;) {
301 unsigned long mod1, mod2, diff;
302 unsigned long as1, as2;
303 int type1, type2;
304 struct symbol *base1, *base2;
306 if (target == source)
307 break;
308 if (!target || !source)
309 return "different types";
311 * Peel of per-node information.
312 * FIXME! Check alignment and context too here!
314 mod1 = target->ctype.modifiers;
315 as1 = target->ctype.as;
316 mod2 = source->ctype.modifiers;
317 as2 = source->ctype.as;
318 if (target->type == SYM_NODE) {
319 target = target->ctype.base_type;
320 if (!target)
321 return "bad types";
322 if (target->type == SYM_PTR) {
323 mod1 = 0;
324 as1 = 0;
326 mod1 |= target->ctype.modifiers;
327 as1 |= target->ctype.as;
329 if (source->type == SYM_NODE) {
330 source = source->ctype.base_type;
331 if (!source)
332 return "bad types";
333 if (source->type == SYM_PTR) {
334 mod2 = 0;
335 as2 = 0;
337 mod2 |= source->ctype.modifiers;
338 as2 |= source->ctype.as;
341 if (target == source)
342 break;
343 if (!target || !source)
344 return "different types";
346 type1 = target->type;
347 base1 = target->ctype.base_type;
349 type2 = source->type;
350 base2 = source->ctype.base_type;
353 * Pointers to functions compare as the function itself
355 if (type1 == SYM_PTR && base1) {
356 switch (base1->type) {
357 case SYM_FN:
358 type1 = SYM_FN;
359 target = base1;
360 base1 = base1->ctype.base_type;
361 default:
362 /* nothing */;
365 if (type2 == SYM_PTR && base2) {
366 switch (base2->type) {
367 case SYM_FN:
368 type2 = SYM_FN;
369 source = base2;
370 base2 = base2->ctype.base_type;
371 default:
372 /* nothing */;
376 /* Arrays degenerate to pointers for type comparisons */
377 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
378 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
380 if (type1 != type2)
381 return "different base types";
383 /* Must be same address space to be comparable */
384 if (as1 != as2)
385 return "different address spaces";
387 /* Ignore differences in storage types, sign, or addressability */
388 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
389 if (diff) {
390 mod1 &= diff & ~target_mod_ignore;
391 mod2 &= diff & ~source_mod_ignore;
392 if (mod1 | mod2) {
393 if ((mod1 | mod2) & MOD_SIZE)
394 return "different type sizes";
395 return "different modifiers";
399 if (type1 == SYM_FN) {
400 int i;
401 struct symbol *arg1, *arg2;
402 if (base1->variadic != base2->variadic)
403 return "incompatible variadic arguments";
404 PREPARE_PTR_LIST(target->arguments, arg1);
405 PREPARE_PTR_LIST(source->arguments, arg2);
406 i = 1;
407 for (;;) {
408 const char *diff;
409 diff = type_difference(arg1, arg2, 0, 0);
410 if (diff) {
411 static char argdiff[80];
412 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
413 return argdiff;
415 if (!arg1)
416 break;
417 NEXT_PTR_LIST(arg1);
418 NEXT_PTR_LIST(arg2);
419 i++;
421 FINISH_PTR_LIST(arg2);
422 FINISH_PTR_LIST(arg1);
425 target = base1;
426 source = base2;
428 return NULL;
431 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
433 /* NULL expression? Just return the type of the "other side" */
434 if (r->type == EXPR_VALUE && !r->value)
435 return l->ctype;
436 if (l->type == EXPR_VALUE && !l->value)
437 return r->ctype;
438 return NULL;
442 * Ignore differences in "volatile" and "const"ness when
443 * subtracting pointers
445 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
447 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
449 const char *typediff;
450 struct symbol *ctype;
451 struct symbol *ltype, *rtype;
453 ltype = degenerate(l);
454 rtype = degenerate(r);
457 * If it is an integer subtract: the ptr add case will do the
458 * right thing.
460 if (!is_ptr_type(rtype))
461 return evaluate_ptr_add(expr, l, r);
463 ctype = ltype;
464 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
465 if (typediff) {
466 ctype = common_ptr_type(l, r);
467 if (!ctype) {
468 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
469 return NULL;
472 examine_symbol_type(ctype);
474 /* Figure out the base type we point to */
475 if (ctype->type == SYM_NODE)
476 ctype = ctype->ctype.base_type;
477 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
478 warn(expr->pos, "subtraction of functions? Share your drugs");
479 return NULL;
481 ctype = ctype->ctype.base_type;
483 expr->ctype = ssize_t_ctype;
484 if (ctype->bit_size > bits_in_char) {
485 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
486 struct expression *div = expr;
487 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
489 val->ctype = size_t_ctype;
490 val->value = ctype->bit_size >> 3;
492 sub->op = '-';
493 sub->ctype = ssize_t_ctype;
494 sub->left = l;
495 sub->right = r;
497 div->op = '/';
498 div->left = sub;
499 div->right = val;
502 return ssize_t_ctype;
505 static struct symbol *evaluate_sub(struct expression *expr)
507 struct expression *left = expr->left, *right = expr->right;
508 struct symbol *ltype = left->ctype;
510 if (is_ptr_type(ltype))
511 return evaluate_ptr_sub(expr, left, right);
513 // FIXME! FP promotion
514 return evaluate_int_binop(expr);
517 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
519 static struct symbol *evaluate_conditional(struct expression *expr)
521 struct symbol *ctype;
523 if (!expr)
524 return NULL;
526 if (expr->type == EXPR_ASSIGNMENT)
527 warn(expr->pos, "assignment expression in conditional");
529 ctype = evaluate_expression(expr);
530 if (ctype && is_safe_type(ctype))
531 warn(expr->pos, "testing a 'safe expression'");
533 return ctype;
536 static struct symbol *evaluate_logical(struct expression *expr)
538 if (!evaluate_conditional(expr->left))
539 return NULL;
540 if (!evaluate_conditional(expr->right))
541 return NULL;
543 expr->ctype = &bool_ctype;
544 return &bool_ctype;
547 static struct symbol *evaluate_arithmetic(struct expression *expr)
549 // FIXME! Floating-point promotion!
550 return evaluate_int_binop(expr);
553 static struct symbol *evaluate_binop(struct expression *expr)
555 switch (expr->op) {
556 // addition can take ptr+int, fp and int
557 case '+':
558 return evaluate_add(expr);
560 // subtraction can take ptr-ptr, fp and int
561 case '-':
562 return evaluate_sub(expr);
564 // Arithmetic operations can take fp and int
565 case '*': case '/': case '%':
566 return evaluate_arithmetic(expr);
568 // The rest are integer operations (bitops)
569 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
570 // '&', '^', '|'
571 default:
572 return evaluate_int_binop(expr);
576 static struct symbol *evaluate_comma(struct expression *expr)
578 expr->ctype = expr->right->ctype;
579 return expr->ctype;
582 static struct symbol *evaluate_compare(struct expression *expr)
584 struct expression *left = expr->left, *right = expr->right;
585 struct symbol *ltype = left->ctype, *rtype = right->ctype;
586 struct symbol *ctype;
588 /* Type types? */
589 if (is_type_type(ltype) && is_type_type(rtype)) {
590 expr->ctype = &bool_ctype;
591 return &bool_ctype;
594 if (is_safe_type(ltype) || is_safe_type(rtype))
595 warn(expr->pos, "testing a 'safe expression'");
597 /* Pointer types? */
598 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
599 expr->ctype = &bool_ctype;
600 // FIXME! Check the types for compatibility
601 return &bool_ctype;
604 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
605 if (ctype) {
606 expr->ctype = &bool_ctype;
607 return &bool_ctype;
610 return bad_expr_type(expr);
613 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
615 /* Integer promotion? */
616 if (ltype->type == SYM_NODE)
617 ltype = ltype->ctype.base_type;
618 if (rtype->type == SYM_NODE)
619 rtype = rtype->ctype.base_type;
620 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
621 ltype = &int_ctype;
622 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
623 rtype = &int_ctype;
624 return (is_int_type(ltype) && is_int_type(rtype));
627 static int is_null_ptr(struct expression *expr)
629 return (expr->type == EXPR_VALUE &&
630 expr->value == 0);
634 * FIXME!! This should do casts, array degeneration etc..
636 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
638 struct symbol *ltype = left->ctype, *rtype = right->ctype;
640 if (ltype->type == SYM_NODE)
641 ltype = ltype->ctype.base_type;
643 if (rtype->type == SYM_NODE)
644 rtype = rtype->ctype.base_type;
646 if (ltype->type == SYM_PTR) {
647 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
648 return ltype;
651 if (rtype->type == SYM_PTR) {
652 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
653 return rtype;
655 return NULL;
658 static struct symbol * evaluate_conditional_expression(struct expression *expr)
660 struct expression *cond, *true, *false;
661 struct symbol *ctype, *ltype, *rtype;
662 const char * typediff;
664 ctype = degenerate(expr->conditional);
665 cond = expr->conditional;
667 ltype = ctype;
668 true = cond;
669 if (expr->cond_true) {
670 ltype = degenerate(expr->cond_true);
671 true = expr->cond_true;
674 rtype = degenerate(expr->cond_false);
675 false = expr->cond_false;
677 ctype = ltype;
678 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
679 if (typediff) {
680 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
681 if (!ctype) {
682 ctype = compatible_ptr_type(true, expr->cond_false);
683 if (!ctype) {
684 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
685 return NULL;
690 expr->ctype = ctype;
691 return ctype;
694 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
695 struct expression **rp, struct symbol *source, const char *where)
697 const char *typediff;
698 struct symbol *t;
699 int target_as;
701 /* It's ok if the target is more volatile or const than the source */
702 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
703 if (!typediff)
704 return 1;
706 if (compatible_integer_types(target, source)) {
707 if (target->bit_size != source->bit_size)
708 *rp = cast_to(*rp, target);
709 return 1;
712 /* Pointer destination? */
713 t = target;
714 target_as = t->ctype.as;
715 if (t->type == SYM_NODE) {
716 t = t->ctype.base_type;
717 target_as |= t->ctype.as;
719 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
720 struct expression *right = *rp;
721 struct symbol *s = source;
722 int source_as;
724 // NULL pointer is always ok
725 if (right->type == EXPR_VALUE && !right->value)
726 return 1;
728 /* "void *" matches anything as long as the address space is ok */
729 source_as = s->ctype.as;
730 if (s->type == SYM_NODE) {
731 s = s->ctype.base_type;
732 source_as |= s->ctype.as;
734 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
735 s = s->ctype.base_type;
736 t = t->ctype.base_type;
737 if (s == &void_ctype || t == &void_ctype)
738 return 1;
742 // FIXME!! Cast it?
743 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
744 info(expr->pos, " expected %s", show_typename(target));
745 info(expr->pos, " got %s", show_typename(source));
746 return 0;
750 * FIXME!! This is wrong from a double evaluation standpoint. We can't
751 * just expand the expression twice, that would make any side effects
752 * happen twice too.
754 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
756 int op = expr->op;
757 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
758 static const int op_trans[] = {
759 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
760 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
761 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
762 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
763 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
764 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
765 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
766 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
767 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
768 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
771 subexpr->left = left;
772 subexpr->right = right;
773 subexpr->op = op_trans[op - SPECIAL_BASE];
774 expr->op = '=';
775 expr->right = subexpr;
776 return evaluate_binop(subexpr);
779 static struct symbol *evaluate_assignment(struct expression *expr)
781 struct expression *left = expr->left, *right = expr->right;
782 struct symbol *ltype, *rtype;
784 ltype = left->ctype;
785 rtype = right->ctype;
786 if (expr->op != '=') {
787 rtype = evaluate_binop_assignment(expr, left, right);
788 if (!rtype)
789 return 0;
790 right = expr->right;
793 if (!lvalue_expression(left)) {
794 warn(expr->pos, "not an lvalue");
795 return NULL;
798 rtype = degenerate(right);
800 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
801 return 0;
803 if (ltype->type == SYM_NODE)
804 ltype->ctype.modifiers |= MOD_ASSIGNED;
806 expr->ctype = ltype;
807 return ltype;
810 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
812 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
813 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
814 *newsym = *sym;
815 newsym->ctype.as = as;
816 newsym->ctype.modifiers = mod;
817 sym = newsym;
819 return sym;
822 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
824 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
825 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
827 node->ctype.base_type = ptr;
828 ptr->bit_size = bits_in_pointer;
829 ptr->ctype.alignment = pointer_alignment;
831 node->bit_size = bits_in_pointer;
832 node->ctype.alignment = pointer_alignment;
834 sym->ctype.modifiers |= MOD_ADDRESSABLE;
835 if (sym->ctype.modifiers & MOD_REGISTER) {
836 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
837 sym->ctype.modifiers &= ~MOD_REGISTER;
839 if (sym->type == SYM_NODE) {
840 ptr->ctype.as |= sym->ctype.as;
841 ptr->ctype.modifiers |= sym->ctype.modifiers;
842 sym = sym->ctype.base_type;
844 if (degenerate && sym->type == SYM_ARRAY) {
845 ptr->ctype.as |= sym->ctype.as;
846 ptr->ctype.modifiers |= sym->ctype.modifiers;
847 sym = sym->ctype.base_type;
849 ptr->ctype.base_type = sym;
851 return node;
854 /* Arrays degenerate into pointers on pointer arithmetic */
855 static struct symbol *degenerate(struct expression *expr)
857 struct symbol *ctype, *base;
859 if (!expr)
860 return NULL;
861 ctype = expr->ctype;
862 if (!ctype)
863 return NULL;
864 base = ctype;
865 if (ctype->type == SYM_NODE)
866 base = ctype->ctype.base_type;
868 * Arrays degenerate into pointers to the entries, while
869 * functions degenerate into pointers to themselves
871 switch (base->type) {
872 case SYM_FN:
873 case SYM_ARRAY:
874 if (expr->op != '*' || expr->type != EXPR_PREOP) {
875 warn(expr->pos, "strange non-value function or array");
876 return NULL;
878 *expr = *expr->unop;
879 ctype = create_pointer(expr, ctype, 1);
880 expr->ctype = ctype;
881 default:
882 /* nothing */;
884 return ctype;
887 static struct symbol *evaluate_addressof(struct expression *expr)
889 struct expression *op = expr->unop;
890 struct symbol *ctype;
892 if (op->op != '*' || op->type != EXPR_PREOP) {
893 warn(expr->pos, "not addressable");
894 return NULL;
896 ctype = op->ctype;
897 *expr = *op->unop;
900 * symbol expression evaluation is lazy about the type
901 * of the sub-expression, so we may have to generate
902 * the type here if so..
904 if (!expr->ctype) {
905 ctype = create_pointer(expr, ctype, 0);
906 expr->ctype = ctype;
908 return expr->ctype;
912 static struct symbol *evaluate_dereference(struct expression *expr)
914 struct expression *op = expr->unop;
915 struct symbol *ctype = op->ctype, *node, *target;
917 /* Simplify: *&(expr) => (expr) */
918 if (op->type == EXPR_PREOP && op->op == '&') {
919 *expr = *op->unop;
920 return expr->ctype;
923 /* Dereferencing a node drops all the node information. */
924 if (ctype->type == SYM_NODE)
925 ctype = ctype->ctype.base_type;
927 node = alloc_symbol(expr->pos, SYM_NODE);
928 target = ctype->ctype.base_type;
930 switch (ctype->type) {
931 default:
932 warn(expr->pos, "cannot derefence this type");
933 return NULL;
934 case SYM_PTR:
935 merge_type(node, ctype);
936 if (ctype->type != SYM_ARRAY)
937 break;
939 * Dereferencing a pointer to an array results in a
940 * degenerate dereference: the expression becomes
941 * just a pointer to the entry, and the derefence
942 * goes away.
944 *expr = *op;
946 target = alloc_symbol(expr->pos, SYM_PTR);
947 target->bit_size = bits_in_pointer;
948 target->ctype.alignment = pointer_alignment;
949 merge_type(target, ctype->ctype.base_type);
950 break;
952 case SYM_ARRAY:
954 * When an array is dereferenced, we need to pick
955 * up the attributes of the original node too..
957 merge_type(node, op->ctype);
958 merge_type(node, ctype);
959 break;
962 node->bit_size = target->bit_size;
963 node->array_size = target->array_size;
965 expr->ctype = node;
966 return node;
970 * Unary post-ops: x++ and x--
972 static struct symbol *evaluate_postop(struct expression *expr)
974 struct expression *op = expr->unop;
975 struct symbol *ctype = op->ctype;
977 if (!lvalue_expression(expr->unop)) {
978 warn(expr->pos, "need lvalue expression for ++/--");
979 return NULL;
982 if (ctype->type == SYM_NODE)
983 ctype->ctype.modifiers |= MOD_ASSIGNED;
985 expr->ctype = ctype;
986 return ctype;
989 static struct symbol *evaluate_preop(struct expression *expr)
991 struct symbol *ctype = expr->unop->ctype;
993 switch (expr->op) {
994 case '(':
995 case '+':
996 *expr = *expr->unop;
997 return ctype;
999 case '*':
1000 return evaluate_dereference(expr);
1002 case '&':
1003 return evaluate_addressof(expr);
1005 case SPECIAL_INCREMENT:
1006 case SPECIAL_DECREMENT:
1008 * From a type evaluation standpoint the pre-ops are
1009 * the same as the postops
1011 return evaluate_postop(expr);
1013 case '!':
1014 if (is_safe_type(ctype))
1015 warn(expr->pos, "testing a 'safe expression'");
1016 ctype = &bool_ctype;
1017 break;
1019 default:
1020 break;
1022 expr->ctype = ctype;
1023 return &bool_ctype;
1026 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1028 struct ptr_list *head = (struct ptr_list *)_list;
1029 struct ptr_list *list = head;
1031 if (!head)
1032 return NULL;
1033 do {
1034 int i;
1035 for (i = 0; i < list->nr; i++) {
1036 struct symbol *sym = (struct symbol *) list->list[i];
1037 if (sym->ident) {
1038 if (sym->ident != ident)
1039 continue;
1040 *offset = sym->offset;
1041 return sym;
1042 } else {
1043 struct symbol *ctype = sym->ctype.base_type;
1044 struct symbol *sub;
1045 if (!ctype)
1046 continue;
1047 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1048 continue;
1049 sub = find_identifier(ident, ctype->symbol_list, offset);
1050 if (!sub)
1051 continue;
1052 *offset += sym->offset;
1053 return sub;
1056 } while ((list = list->next) != head);
1057 return NULL;
1060 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1062 struct expression *add;
1064 add = expr;
1065 if (offset) {
1066 /* Create a new add-expression */
1067 add = alloc_expression(expr->pos, EXPR_BINOP);
1068 add->op = '+';
1069 add->left = expr;
1070 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1071 add->right->ctype = &int_ctype;
1072 add->right->value = offset;
1076 * The ctype of the pointer will be lazily evaluated if
1077 * we ever take the address of this member dereference..
1079 add->ctype = NULL;
1080 return add;
1083 /* structure/union dereference */
1084 static struct symbol *evaluate_member_dereference(struct expression *expr)
1086 int offset;
1087 struct symbol *ctype, *member;
1088 struct expression *deref = expr->deref, *add;
1089 struct ident *ident = expr->member;
1090 unsigned int mod;
1091 int address_space;
1093 if (!evaluate_expression(deref))
1094 return NULL;
1095 if (!ident) {
1096 warn(expr->pos, "bad member name");
1097 return NULL;
1100 ctype = deref->ctype;
1101 address_space = ctype->ctype.as;
1102 mod = ctype->ctype.modifiers;
1103 if (ctype->type == SYM_NODE) {
1104 ctype = ctype->ctype.base_type;
1105 address_space |= ctype->ctype.as;
1106 mod |= ctype->ctype.modifiers;
1108 if (!lvalue_expression(deref)) {
1109 warn(deref->pos, "expected lvalue for member dereference");
1110 return NULL;
1112 deref = deref->unop;
1113 expr->deref = deref;
1114 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1115 warn(expr->pos, "expected structure or union");
1116 return NULL;
1118 offset = 0;
1119 member = find_identifier(ident, ctype->symbol_list, &offset);
1120 if (!member) {
1121 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1122 const char *name = "<unnamed>";
1123 int namelen = 9;
1124 if (ctype->ident) {
1125 name = ctype->ident->name;
1126 namelen = ctype->ident->len;
1128 warn(expr->pos, "no member '%s' in %s %.*s",
1129 show_ident(ident), type, namelen, name);
1130 return NULL;
1134 * The member needs to take on the address space and modifiers of
1135 * the "parent" type.
1137 member = convert_to_as_mod(member, address_space, mod);
1138 add = evaluate_offset(deref, offset);
1140 ctype = member->ctype.base_type;
1141 if (ctype->type == SYM_BITFIELD) {
1142 expr->type = EXPR_BITFIELD;
1143 expr->bitpos = member->bit_offset;
1144 expr->nrbits = member->fieldwidth;
1145 expr->address = add;
1146 } else {
1147 expr->type = EXPR_PREOP;
1148 expr->op = '*';
1149 expr->unop = add;
1152 expr->ctype = member;
1153 return member;
1156 static struct symbol *evaluate_sizeof(struct expression *expr)
1158 int size;
1160 if (expr->cast_type) {
1161 examine_symbol_type(expr->cast_type);
1162 size = expr->cast_type->bit_size;
1163 } else {
1164 if (!evaluate_expression(expr->cast_expression))
1165 return 0;
1166 size = expr->cast_expression->ctype->bit_size;
1168 if (size & 7) {
1169 warn(expr->pos, "cannot size expression");
1170 return 0;
1172 expr->type = EXPR_VALUE;
1173 expr->value = size >> 3;
1174 expr->ctype = size_t_ctype;
1175 return size_t_ctype;
1178 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1180 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1181 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1182 return clash != 0;
1185 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1187 struct expression *expr;
1188 struct symbol_list *argument_types = fn->arguments;
1189 struct symbol *argtype;
1190 int i = 1;
1192 PREPARE_PTR_LIST(argument_types, argtype);
1193 FOR_EACH_PTR (head, expr) {
1194 struct expression **p = THIS_ADDRESS(expr);
1195 struct symbol *ctype, *target;
1196 ctype = evaluate_expression(expr);
1198 if (!ctype)
1199 return 0;
1201 if (context_clash(f, ctype))
1202 warn(expr->pos, "argument %d used in wrong context", i);
1204 ctype = degenerate(expr);
1206 target = argtype;
1207 if (!target && ctype->bit_size < bits_in_int)
1208 target = &int_ctype;
1209 if (target) {
1210 static char where[30];
1211 examine_symbol_type(target);
1212 sprintf(where, "argument %d", i);
1213 compatible_assignment_types(expr, target, p, ctype, where);
1216 i++;
1217 NEXT_PTR_LIST(argtype);
1218 } END_FOR_EACH_PTR;
1219 FINISH_PTR_LIST(argtype);
1220 return 1;
1223 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1224 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1226 struct expression *entry;
1227 int current = 0;
1228 int max = 0;
1230 FOR_EACH_PTR(expr->expr_list, entry) {
1231 struct expression **p = THIS_ADDRESS(entry);
1233 if (entry->type == EXPR_INDEX) {
1234 current = entry->idx_to;
1235 continue;
1237 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1238 current++;
1239 if (current > max)
1240 max = current;
1241 } END_FOR_EACH_PTR;
1242 return max;
1245 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1246 static int evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1248 if (offset || expression_list_size(expr->expr_list) != 1) {
1249 warn(expr->pos, "unexpected compound initializer");
1250 return 0;
1252 return evaluate_array_initializer(ctype, expr, 0);
1255 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1257 struct expression *entry;
1258 struct symbol *sym;
1260 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1261 FOR_EACH_PTR(expr->expr_list, entry) {
1262 struct expression **p = THIS_ADDRESS(entry);
1264 if (entry->type == EXPR_IDENTIFIER) {
1265 struct ident *ident = entry->expr_ident;
1266 /* We special-case the "already right place" case */
1267 if (sym && sym->ident == ident)
1268 continue;
1269 RESET_PTR_LIST(sym);
1270 for (;;) {
1271 if (!sym) {
1272 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1273 return 0;
1275 if (sym->ident == ident)
1276 break;
1277 NEXT_PTR_LIST(sym);
1279 continue;
1282 if (!sym) {
1283 warn(expr->pos, "too many initializers for struct/union");
1284 return 0;
1287 evaluate_initializer(sym, p, offset + sym->offset);
1289 NEXT_PTR_LIST(sym);
1290 } END_FOR_EACH_PTR;
1291 FINISH_PTR_LIST(sym);
1293 return 0;
1297 * Initializers are kind of like assignments. Except
1298 * they can be a hell of a lot more complex.
1300 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1302 struct expression *expr = *ep;
1305 * Simple non-structure/array initializers are the simple
1306 * case, and look (and parse) largely like assignments.
1308 if (expr->type != EXPR_INITIALIZER) {
1309 int size = 0;
1310 struct symbol *rtype = evaluate_expression(expr);
1311 if (rtype) {
1312 struct expression *pos;
1314 // FIXME! char array[] = "string" special case
1315 // should _not_ degenerate.
1316 rtype = degenerate(expr);
1317 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1318 /* strings are special: char arrays */
1319 if (rtype->type == SYM_ARRAY)
1320 size = get_expression_value(rtype->array_size);
1322 * Don't bother creating a position expression for
1323 * the simple initializer cases that don't need it.
1325 * We need a position if the initializer has a byte
1326 * offset, _or_ if we're initializing a bitfield.
1328 if (offset || ctype->fieldwidth) {
1329 pos = alloc_expression(expr->pos, EXPR_POS);
1330 pos->init_offset = offset;
1331 pos->init_sym = ctype;
1332 pos->init_expr = *ep;
1333 pos->ctype = expr->ctype;
1334 *ep = pos;
1337 return size;
1340 expr->ctype = ctype;
1341 if (ctype->type == SYM_NODE)
1342 ctype = ctype->ctype.base_type;
1344 switch (ctype->type) {
1345 case SYM_ARRAY:
1346 case SYM_PTR:
1347 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1348 case SYM_UNION:
1349 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1350 case SYM_STRUCT:
1351 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1352 default:
1353 return evaluate_scalar_initializer(ctype, expr, offset);
1357 static int get_as(struct symbol *sym)
1359 int as;
1360 unsigned long mod;
1362 if (!sym)
1363 return 0;
1364 as = sym->ctype.as;
1365 mod = sym->ctype.modifiers;
1366 if (sym->type == SYM_NODE) {
1367 sym = sym->ctype.base_type;
1368 as |= sym->ctype.as;
1369 mod |= sym->ctype.modifiers;
1372 * You can always throw a value away by casting to
1373 * "void" - that's an implicit "force". Note that
1374 * the same is _not_ true of "void *".
1376 if (sym == &void_ctype)
1377 return -1;
1380 * At least for now, allow casting to a "unsigned long".
1381 * That's how we do things like pointer arithmetic and
1382 * store pointers to registers.
1384 if (sym == &ulong_ctype)
1385 return -1;
1387 if (sym && sym->type == SYM_PTR) {
1388 sym = sym->ctype.base_type;
1389 as |= sym->ctype.as;
1390 mod |= sym->ctype.modifiers;
1392 if (mod & MOD_FORCE)
1393 return -1;
1394 return as;
1397 static struct symbol *evaluate_cast(struct expression *expr)
1399 struct expression *target = expr->cast_expression;
1400 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1402 expr->ctype = ctype;
1403 expr->cast_type = ctype;
1406 * Special case: a cast can be followed by an
1407 * initializer, in which case we need to pass
1408 * the type value down to that initializer rather
1409 * than trying to evaluate it as an expression
1411 * A more complex case is when the initializer is
1412 * dereferenced as part of a post-fix expression.
1413 * We need to produce an expression that can be dereferenced.
1415 if (target->type == EXPR_INITIALIZER) {
1416 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
1417 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1419 sym->ctype.base_type = ctype;
1420 sym->initializer = expr->cast_expression;
1421 evaluate_symbol(sym);
1423 addr->ctype = NULL; /* Lazy eval */
1424 addr->symbol = sym;
1426 expr->type = EXPR_PREOP;
1427 expr->op = '*';
1428 expr->unop = addr;
1429 expr->ctype = ctype;
1430 return ctype;
1433 evaluate_expression(target);
1434 degenerate(target);
1436 if (!get_as(ctype) && get_as(target->ctype) > 0)
1437 warn(expr->pos, "cast removes address space of expression");
1440 * Casts of constant values are special: they
1441 * can be NULL, and thus need to be simplified
1442 * early.
1444 if (target->type == EXPR_VALUE)
1445 cast_value(expr, ctype, target, target->ctype);
1447 return ctype;
1451 * Evaluate a call expression with a symbol. This
1452 * should expand inline functions, and evaluate
1453 * builtins.
1455 static int evaluate_symbol_call(struct expression *expr)
1457 struct expression *fn = expr->fn;
1458 struct symbol *ctype = fn->ctype;
1460 if (fn->type != EXPR_PREOP)
1461 return 0;
1463 if (ctype->op && ctype->op->evaluate)
1464 return ctype->op->evaluate(expr);
1466 if (ctype->ctype.modifiers & MOD_INLINE) {
1467 int ret;
1468 struct symbol *curr = current_fn;
1469 unsigned long context = current_context;
1470 unsigned long mask = current_contextmask;
1472 current_context |= ctype->ctype.context;
1473 current_contextmask |= ctype->ctype.contextmask;
1474 current_fn = ctype->ctype.base_type;
1475 ret = inline_function(expr, ctype);
1477 /* restore the old function context */
1478 current_fn = curr;
1479 current_context = context;
1480 current_contextmask = mask;
1481 return ret;
1484 return 0;
1487 static struct symbol *evaluate_call(struct expression *expr)
1489 int args, fnargs;
1490 struct symbol *ctype, *sym;
1491 struct expression *fn = expr->fn;
1492 struct expression_list *arglist = expr->args;
1494 if (!evaluate_expression(fn))
1495 return NULL;
1496 sym = ctype = fn->ctype;
1497 if (ctype->type == SYM_NODE)
1498 ctype = ctype->ctype.base_type;
1499 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1500 ctype = ctype->ctype.base_type;
1501 if (!evaluate_arguments(sym, ctype, arglist))
1502 return NULL;
1503 if (ctype->type != SYM_FN) {
1504 warn(expr->pos, "not a function %s", show_ident(sym->ident));
1505 return NULL;
1507 args = expression_list_size(expr->args);
1508 fnargs = symbol_list_size(ctype->arguments);
1509 if (args < fnargs)
1510 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1511 if (args > fnargs && !ctype->variadic)
1512 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1513 if (sym->type == SYM_NODE) {
1514 if (evaluate_symbol_call(expr))
1515 return expr->ctype;
1517 expr->ctype = ctype->ctype.base_type;
1518 return expr->ctype;
1521 struct symbol *evaluate_expression(struct expression *expr)
1523 if (!expr)
1524 return NULL;
1525 if (expr->ctype)
1526 return expr->ctype;
1528 switch (expr->type) {
1529 case EXPR_VALUE:
1530 warn(expr->pos, "value expression without a type");
1531 return NULL;
1532 case EXPR_STRING:
1533 return evaluate_string(expr);
1534 case EXPR_SYMBOL:
1535 return evaluate_symbol_expression(expr);
1536 case EXPR_BINOP:
1537 if (!evaluate_expression(expr->left))
1538 return NULL;
1539 if (!evaluate_expression(expr->right))
1540 return NULL;
1541 return evaluate_binop(expr);
1542 case EXPR_LOGICAL:
1543 return evaluate_logical(expr);
1544 case EXPR_COMMA:
1545 if (!evaluate_expression(expr->left))
1546 return NULL;
1547 if (!evaluate_expression(expr->right))
1548 return NULL;
1549 return evaluate_comma(expr);
1550 case EXPR_COMPARE:
1551 if (!evaluate_expression(expr->left))
1552 return NULL;
1553 if (!evaluate_expression(expr->right))
1554 return NULL;
1555 return evaluate_compare(expr);
1556 case EXPR_ASSIGNMENT:
1557 if (!evaluate_expression(expr->left))
1558 return NULL;
1559 if (!evaluate_expression(expr->right))
1560 return NULL;
1561 return evaluate_assignment(expr);
1562 case EXPR_PREOP:
1563 if (!evaluate_expression(expr->unop))
1564 return NULL;
1565 return evaluate_preop(expr);
1566 case EXPR_POSTOP:
1567 if (!evaluate_expression(expr->unop))
1568 return NULL;
1569 return evaluate_postop(expr);
1570 case EXPR_CAST:
1571 return evaluate_cast(expr);
1572 case EXPR_SIZEOF:
1573 return evaluate_sizeof(expr);
1574 case EXPR_DEREF:
1575 return evaluate_member_dereference(expr);
1576 case EXPR_CALL:
1577 return evaluate_call(expr);
1578 case EXPR_BITFIELD:
1579 warn(expr->pos, "bitfield generated by parser");
1580 return NULL;
1581 case EXPR_CONDITIONAL:
1582 if (!evaluate_conditional(expr->conditional))
1583 return NULL;
1584 if (!evaluate_expression(expr->cond_false))
1585 return NULL;
1586 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1587 return NULL;
1588 return evaluate_conditional_expression(expr);
1589 case EXPR_STATEMENT:
1590 expr->ctype = evaluate_statement(expr->statement);
1591 return expr->ctype;
1593 case EXPR_LABEL:
1594 expr->ctype = &ptr_ctype;
1595 return &ptr_ctype;
1597 case EXPR_TYPE:
1598 /* Evaluate the type of the symbol .. */
1599 evaluate_symbol(expr->symbol);
1600 /* .. but the type of the _expression_ is a "type" */
1601 expr->ctype = &type_ctype;
1602 return &type_ctype;
1604 /* These can not exist as stand-alone expressions */
1605 case EXPR_INITIALIZER:
1606 case EXPR_IDENTIFIER:
1607 case EXPR_INDEX:
1608 case EXPR_POS:
1609 warn(expr->pos, "internal front-end error: initializer in expression");
1610 return NULL;
1612 return NULL;
1615 void check_duplicates(struct symbol *sym)
1617 struct symbol *next = sym;
1619 while ((next = next->same_symbol) != NULL) {
1620 const char *typediff;
1621 evaluate_symbol(next);
1622 typediff = type_difference(sym, next, 0, 0);
1623 if (typediff) {
1624 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1625 show_ident(sym->ident),
1626 input_streams[next->pos.stream].name, next->pos.line, typediff);
1627 return;
1632 struct symbol *evaluate_symbol(struct symbol *sym)
1634 struct symbol *base_type;
1636 if (!sym)
1637 return sym;
1639 sym = examine_symbol_type(sym);
1640 base_type = sym->ctype.base_type;
1641 if (!base_type)
1642 return NULL;
1644 /* Evaluate the initializers */
1645 if (sym->initializer) {
1646 int count = evaluate_initializer(sym, &sym->initializer, 0);
1647 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
1648 int bit_size = count * base_type->ctype.base_type->bit_size;
1649 base_type->array_size = alloc_const_expression(sym->pos, count);
1650 base_type->bit_size = bit_size;
1651 sym->array_size = base_type->array_size;
1652 sym->bit_size = bit_size;
1656 /* And finally, evaluate the body of the symbol too */
1657 if (base_type->type == SYM_FN) {
1658 struct symbol *s;
1660 FOR_EACH_PTR(base_type->arguments, s) {
1661 evaluate_symbol(s);
1662 } END_FOR_EACH_PTR;
1664 if (base_type->stmt) {
1665 current_fn = base_type;
1666 current_contextmask = sym->ctype.contextmask;
1667 current_context = sym->ctype.context;
1668 evaluate_statement(base_type->stmt);
1672 return base_type;
1675 struct symbol *evaluate_return_expression(struct statement *stmt)
1677 struct expression *expr = stmt->expression;
1678 struct symbol *ctype, *fntype;
1680 evaluate_expression(expr);
1681 ctype = degenerate(expr);
1682 fntype = current_fn->ctype.base_type;
1683 if (!fntype || fntype == &void_ctype) {
1684 if (expr && ctype != &void_ctype)
1685 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
1686 return NULL;
1689 if (!expr) {
1690 warn(stmt->pos, "return with no return value");
1691 return NULL;
1693 if (!ctype)
1694 return NULL;
1695 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
1696 return NULL;
1699 static void evaluate_if_statement(struct statement *stmt)
1701 struct expression *expr = stmt->if_conditional;
1702 struct symbol *ctype;
1704 if (!expr)
1705 return;
1707 ctype = evaluate_conditional(expr);
1708 if (!ctype)
1709 return;
1711 evaluate_statement(stmt->if_true);
1712 evaluate_statement(stmt->if_false);
1715 struct symbol *evaluate_statement(struct statement *stmt)
1717 if (!stmt)
1718 return NULL;
1720 switch (stmt->type) {
1721 case STMT_RETURN:
1722 return evaluate_return_expression(stmt);
1724 case STMT_EXPRESSION:
1725 evaluate_expression(stmt->expression);
1726 return degenerate(stmt->expression);
1728 case STMT_COMPOUND: {
1729 struct statement *s;
1730 struct symbol *type = NULL;
1731 struct symbol *sym;
1733 /* Evaluate each symbol in the compound statement */
1734 FOR_EACH_PTR(stmt->syms, sym) {
1735 evaluate_symbol(sym);
1736 } END_FOR_EACH_PTR;
1737 evaluate_symbol(stmt->ret);
1740 * Then, evaluate each statement, making the type of the
1741 * compound statement be the type of the last statement
1743 type = NULL;
1744 FOR_EACH_PTR(stmt->stmts, s) {
1745 type = evaluate_statement(s);
1746 } END_FOR_EACH_PTR;
1747 return type;
1749 case STMT_IF:
1750 evaluate_if_statement(stmt);
1751 return NULL;
1752 case STMT_ITERATOR:
1753 evaluate_conditional(stmt->iterator_pre_condition);
1754 evaluate_conditional(stmt->iterator_post_condition);
1755 evaluate_statement(stmt->iterator_pre_statement);
1756 evaluate_statement(stmt->iterator_statement);
1757 evaluate_statement(stmt->iterator_post_statement);
1758 return NULL;
1759 case STMT_SWITCH:
1760 evaluate_expression(stmt->switch_expression);
1761 evaluate_statement(stmt->switch_statement);
1762 return NULL;
1763 case STMT_CASE:
1764 evaluate_expression(stmt->case_expression);
1765 evaluate_expression(stmt->case_to);
1766 evaluate_statement(stmt->case_statement);
1767 return NULL;
1768 case STMT_LABEL:
1769 evaluate_statement(stmt->label_statement);
1770 return NULL;
1771 case STMT_GOTO:
1772 evaluate_expression(stmt->goto_expression);
1773 return NULL;
1774 case STMT_NONE:
1775 break;
1776 case STMT_ASM:
1777 /* FIXME! Do the asm parameter evaluation! */
1778 break;
1780 return NULL;