[PATCH] lazy-copy macro expansion in pre-processing
[smatch.git] / evaluate.c
bloba75ce30e59547647b6310aad373772a31d2dcb1a
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 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1247 struct expression *entry;
1248 struct symbol *sym;
1250 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1251 FOR_EACH_PTR(expr->expr_list, entry) {
1252 struct expression **p = THIS_ADDRESS(entry);
1254 if (entry->type == EXPR_IDENTIFIER) {
1255 struct ident *ident = entry->expr_ident;
1256 /* We special-case the "already right place" case */
1257 if (sym && sym->ident == ident)
1258 continue;
1259 RESET_PTR_LIST(sym);
1260 for (;;) {
1261 if (!sym) {
1262 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1263 return 0;
1265 if (sym->ident == ident)
1266 break;
1267 NEXT_PTR_LIST(sym);
1269 continue;
1272 if (!sym) {
1273 warn(expr->pos, "too many initializers for struct/union");
1274 return 0;
1277 evaluate_initializer(sym, p, offset + sym->offset);
1279 NEXT_PTR_LIST(sym);
1280 } END_FOR_EACH_PTR;
1281 FINISH_PTR_LIST(sym);
1283 return 0;
1287 * Initializers are kind of like assignments. Except
1288 * they can be a hell of a lot more complex.
1290 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1292 struct expression *expr = *ep;
1295 * Simple non-structure/array initializers are the simple
1296 * case, and look (and parse) largely like assignments.
1298 if (expr->type != EXPR_INITIALIZER) {
1299 int size = 0;
1300 struct symbol *rtype = evaluate_expression(expr);
1301 if (rtype) {
1302 struct expression *pos;
1304 // FIXME! char array[] = "string" special case
1305 // should _not_ degenerate.
1306 rtype = degenerate(expr);
1307 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1308 /* strings are special: char arrays */
1309 if (rtype->type == SYM_ARRAY)
1310 size = get_expression_value(rtype->array_size);
1312 * Don't bother creating a position expression for
1313 * the simple initializer cases that don't need it.
1315 * We need a position if the initializer has a byte
1316 * offset, _or_ if we're initializing a bitfield.
1318 if (offset || ctype->fieldwidth) {
1319 pos = alloc_expression(expr->pos, EXPR_POS);
1320 pos->init_offset = offset;
1321 pos->init_sym = ctype;
1322 pos->init_expr = *ep;
1323 pos->ctype = expr->ctype;
1324 *ep = pos;
1327 return size;
1330 expr->ctype = ctype;
1331 if (ctype->type == SYM_NODE)
1332 ctype = ctype->ctype.base_type;
1334 switch (ctype->type) {
1335 case SYM_ARRAY:
1336 case SYM_PTR:
1337 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1338 case SYM_UNION:
1339 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1340 case SYM_STRUCT:
1341 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1342 default:
1343 break;
1345 warn(expr->pos, "unexpected compound initializer");
1346 return 0;
1349 static int get_as(struct symbol *sym)
1351 int as;
1352 unsigned long mod;
1354 if (!sym)
1355 return 0;
1356 as = sym->ctype.as;
1357 mod = sym->ctype.modifiers;
1358 if (sym->type == SYM_NODE) {
1359 sym = sym->ctype.base_type;
1360 as |= sym->ctype.as;
1361 mod |= sym->ctype.modifiers;
1364 * You can always throw a value away by casting to
1365 * "void" - that's an implicit "force". Note that
1366 * the same is _not_ true of "void *".
1368 if (sym == &void_ctype)
1369 return -1;
1372 * At least for now, allow casting to a "unsigned long".
1373 * That's how we do things like pointer arithmetic and
1374 * store pointers to registers.
1376 if (sym == &ulong_ctype)
1377 return -1;
1379 if (sym && sym->type == SYM_PTR) {
1380 sym = sym->ctype.base_type;
1381 as |= sym->ctype.as;
1382 mod |= sym->ctype.modifiers;
1384 if (mod & MOD_FORCE)
1385 return -1;
1386 return as;
1389 static struct symbol *evaluate_cast(struct expression *expr)
1391 struct expression *target = expr->cast_expression;
1392 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1394 expr->ctype = ctype;
1395 expr->cast_type = ctype;
1398 * Special case: a cast can be followed by an
1399 * initializer, in which case we need to pass
1400 * the type value down to that initializer rather
1401 * than trying to evaluate it as an expression
1403 * A more complex case is when the initializer is
1404 * dereferenced as part of a post-fix expression.
1405 * We need to produce an expression that can be dereferenced.
1407 if (target->type == EXPR_INITIALIZER) {
1408 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
1409 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1411 sym->ctype.base_type = ctype;
1412 sym->initializer = expr->cast_expression;
1413 evaluate_symbol(sym);
1415 addr->ctype = NULL; /* Lazy eval */
1416 addr->symbol = sym;
1418 expr->type = EXPR_PREOP;
1419 expr->op = '*';
1420 expr->unop = addr;
1421 expr->ctype = ctype;
1422 return ctype;
1425 evaluate_expression(target);
1426 degenerate(target);
1428 if (!get_as(ctype) && get_as(target->ctype) > 0)
1429 warn(expr->pos, "cast removes address space of expression");
1432 * Casts of constant values are special: they
1433 * can be NULL, and thus need to be simplified
1434 * early.
1436 if (target->type == EXPR_VALUE)
1437 cast_value(expr, ctype, target, target->ctype);
1439 return ctype;
1443 * Evaluate a call expression with a symbol. This
1444 * should expand inline functions, and evaluate
1445 * builtins.
1447 static int evaluate_symbol_call(struct expression *expr)
1449 struct expression *fn = expr->fn;
1450 struct symbol *ctype = fn->ctype;
1452 if (fn->type != EXPR_PREOP)
1453 return 0;
1455 if (ctype->op && ctype->op->evaluate)
1456 return ctype->op->evaluate(expr);
1458 if (ctype->ctype.modifiers & MOD_INLINE) {
1459 int ret;
1460 struct symbol *curr = current_fn;
1461 unsigned long context = current_context;
1462 unsigned long mask = current_contextmask;
1464 current_context |= ctype->ctype.context;
1465 current_contextmask |= ctype->ctype.contextmask;
1466 current_fn = ctype->ctype.base_type;
1467 ret = inline_function(expr, ctype);
1469 /* restore the old function context */
1470 current_fn = curr;
1471 current_context = context;
1472 current_contextmask = mask;
1473 return ret;
1476 return 0;
1479 static struct symbol *evaluate_call(struct expression *expr)
1481 int args, fnargs;
1482 struct symbol *ctype, *sym;
1483 struct expression *fn = expr->fn;
1484 struct expression_list *arglist = expr->args;
1486 if (!evaluate_expression(fn))
1487 return NULL;
1488 sym = ctype = fn->ctype;
1489 if (ctype->type == SYM_NODE)
1490 ctype = ctype->ctype.base_type;
1491 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1492 ctype = ctype->ctype.base_type;
1493 if (!evaluate_arguments(sym, ctype, arglist))
1494 return NULL;
1495 if (ctype->type != SYM_FN) {
1496 warn(expr->pos, "not a function %s", show_ident(sym->ident));
1497 return NULL;
1499 args = expression_list_size(expr->args);
1500 fnargs = symbol_list_size(ctype->arguments);
1501 if (args < fnargs)
1502 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1503 if (args > fnargs && !ctype->variadic)
1504 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1505 if (sym->type == SYM_NODE) {
1506 if (evaluate_symbol_call(expr))
1507 return expr->ctype;
1509 expr->ctype = ctype->ctype.base_type;
1510 return expr->ctype;
1513 struct symbol *evaluate_expression(struct expression *expr)
1515 if (!expr)
1516 return NULL;
1517 if (expr->ctype)
1518 return expr->ctype;
1520 switch (expr->type) {
1521 case EXPR_VALUE:
1522 warn(expr->pos, "value expression without a type");
1523 return NULL;
1524 case EXPR_STRING:
1525 return evaluate_string(expr);
1526 case EXPR_SYMBOL:
1527 return evaluate_symbol_expression(expr);
1528 case EXPR_BINOP:
1529 if (!evaluate_expression(expr->left))
1530 return NULL;
1531 if (!evaluate_expression(expr->right))
1532 return NULL;
1533 return evaluate_binop(expr);
1534 case EXPR_LOGICAL:
1535 return evaluate_logical(expr);
1536 case EXPR_COMMA:
1537 if (!evaluate_expression(expr->left))
1538 return NULL;
1539 if (!evaluate_expression(expr->right))
1540 return NULL;
1541 return evaluate_comma(expr);
1542 case EXPR_COMPARE:
1543 if (!evaluate_expression(expr->left))
1544 return NULL;
1545 if (!evaluate_expression(expr->right))
1546 return NULL;
1547 return evaluate_compare(expr);
1548 case EXPR_ASSIGNMENT:
1549 if (!evaluate_expression(expr->left))
1550 return NULL;
1551 if (!evaluate_expression(expr->right))
1552 return NULL;
1553 return evaluate_assignment(expr);
1554 case EXPR_PREOP:
1555 if (!evaluate_expression(expr->unop))
1556 return NULL;
1557 return evaluate_preop(expr);
1558 case EXPR_POSTOP:
1559 if (!evaluate_expression(expr->unop))
1560 return NULL;
1561 return evaluate_postop(expr);
1562 case EXPR_CAST:
1563 return evaluate_cast(expr);
1564 case EXPR_SIZEOF:
1565 return evaluate_sizeof(expr);
1566 case EXPR_DEREF:
1567 return evaluate_member_dereference(expr);
1568 case EXPR_CALL:
1569 return evaluate_call(expr);
1570 case EXPR_BITFIELD:
1571 warn(expr->pos, "bitfield generated by parser");
1572 return NULL;
1573 case EXPR_CONDITIONAL:
1574 if (!evaluate_conditional(expr->conditional))
1575 return NULL;
1576 if (!evaluate_expression(expr->cond_false))
1577 return NULL;
1578 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1579 return NULL;
1580 return evaluate_conditional_expression(expr);
1581 case EXPR_STATEMENT:
1582 expr->ctype = evaluate_statement(expr->statement);
1583 return expr->ctype;
1585 case EXPR_LABEL:
1586 expr->ctype = &ptr_ctype;
1587 return &ptr_ctype;
1589 case EXPR_TYPE:
1590 /* Evaluate the type of the symbol .. */
1591 evaluate_symbol(expr->symbol);
1592 /* .. but the type of the _expression_ is a "type" */
1593 expr->ctype = &type_ctype;
1594 return &type_ctype;
1596 /* These can not exist as stand-alone expressions */
1597 case EXPR_INITIALIZER:
1598 case EXPR_IDENTIFIER:
1599 case EXPR_INDEX:
1600 case EXPR_POS:
1601 warn(expr->pos, "internal front-end error: initializer in expression");
1602 return NULL;
1604 return NULL;
1607 void check_duplicates(struct symbol *sym)
1609 struct symbol *next = sym;
1611 while ((next = next->same_symbol) != NULL) {
1612 const char *typediff;
1613 evaluate_symbol(next);
1614 typediff = type_difference(sym, next, 0, 0);
1615 if (typediff) {
1616 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1617 show_ident(sym->ident),
1618 input_streams[next->pos.stream].name, next->pos.line, typediff);
1619 return;
1624 struct symbol *evaluate_symbol(struct symbol *sym)
1626 struct symbol *base_type;
1628 if (!sym)
1629 return sym;
1631 sym = examine_symbol_type(sym);
1632 base_type = sym->ctype.base_type;
1633 if (!base_type)
1634 return NULL;
1636 /* Evaluate the initializers */
1637 if (sym->initializer) {
1638 int count = evaluate_initializer(sym, &sym->initializer, 0);
1639 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
1640 int bit_size = count * base_type->ctype.base_type->bit_size;
1641 base_type->array_size = alloc_const_expression(sym->pos, count);
1642 base_type->bit_size = bit_size;
1643 sym->array_size = base_type->array_size;
1644 sym->bit_size = bit_size;
1648 /* And finally, evaluate the body of the symbol too */
1649 if (base_type->type == SYM_FN) {
1650 struct symbol *s;
1652 FOR_EACH_PTR(base_type->arguments, s) {
1653 evaluate_symbol(s);
1654 } END_FOR_EACH_PTR;
1656 if (base_type->stmt) {
1657 current_fn = base_type;
1658 current_contextmask = sym->ctype.contextmask;
1659 current_context = sym->ctype.context;
1660 evaluate_statement(base_type->stmt);
1664 return base_type;
1667 struct symbol *evaluate_return_expression(struct statement *stmt)
1669 struct expression *expr = stmt->expression;
1670 struct symbol *ctype, *fntype;
1672 fntype = current_fn->ctype.base_type;
1673 if (!fntype || fntype == &void_ctype) {
1674 if (expr)
1675 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
1676 return NULL;
1679 if (!expr) {
1680 warn(stmt->pos, "return with no return value");
1681 return NULL;
1683 ctype = evaluate_expression(expr);
1684 if (!ctype)
1685 return NULL;
1686 ctype = degenerate(expr);
1687 expr->ctype = ctype;
1688 compatible_assignment_types(expr, fntype, &expr, ctype, "return expression");
1689 stmt->expression = expr;
1690 return NULL;
1693 static void evaluate_if_statement(struct statement *stmt)
1695 struct expression *expr = stmt->if_conditional;
1696 struct symbol *ctype;
1698 if (!expr)
1699 return;
1701 ctype = evaluate_conditional(expr);
1702 if (!ctype)
1703 return;
1705 evaluate_statement(stmt->if_true);
1706 evaluate_statement(stmt->if_false);
1709 struct symbol *evaluate_statement(struct statement *stmt)
1711 if (!stmt)
1712 return NULL;
1714 switch (stmt->type) {
1715 case STMT_RETURN:
1716 return evaluate_return_expression(stmt);
1718 case STMT_EXPRESSION:
1719 evaluate_expression(stmt->expression);
1720 return degenerate(stmt->expression);
1722 case STMT_COMPOUND: {
1723 struct statement *s;
1724 struct symbol *type = NULL;
1725 struct symbol *sym;
1727 /* Evaluate each symbol in the compound statement */
1728 FOR_EACH_PTR(stmt->syms, sym) {
1729 evaluate_symbol(sym);
1730 } END_FOR_EACH_PTR;
1731 evaluate_symbol(stmt->ret);
1734 * Then, evaluate each statement, making the type of the
1735 * compound statement be the type of the last statement
1737 type = NULL;
1738 FOR_EACH_PTR(stmt->stmts, s) {
1739 type = evaluate_statement(s);
1740 } END_FOR_EACH_PTR;
1741 return type;
1743 case STMT_IF:
1744 evaluate_if_statement(stmt);
1745 return NULL;
1746 case STMT_ITERATOR:
1747 evaluate_conditional(stmt->iterator_pre_condition);
1748 evaluate_conditional(stmt->iterator_post_condition);
1749 evaluate_statement(stmt->iterator_pre_statement);
1750 evaluate_statement(stmt->iterator_statement);
1751 evaluate_statement(stmt->iterator_post_statement);
1752 return NULL;
1753 case STMT_SWITCH:
1754 evaluate_expression(stmt->switch_expression);
1755 evaluate_statement(stmt->switch_statement);
1756 return NULL;
1757 case STMT_CASE:
1758 evaluate_expression(stmt->case_expression);
1759 evaluate_expression(stmt->case_to);
1760 evaluate_statement(stmt->case_statement);
1761 return NULL;
1762 case STMT_LABEL:
1763 evaluate_statement(stmt->label_statement);
1764 return NULL;
1765 case STMT_GOTO:
1766 evaluate_expression(stmt->goto_expression);
1767 return NULL;
1768 case STMT_NONE:
1769 break;
1770 case STMT_ASM:
1771 /* FIXME! Do the asm parameter evaluation! */
1772 break;
1774 return NULL;