[PATCH] shift type fix
[smatch.git] / evaluate.c
blob328617a5e670a6c7d68ead84b99e8eef94138011
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "parse.h"
23 #include "token.h"
24 #include "symbol.h"
25 #include "target.h"
26 #include "expression.h"
28 static struct symbol *current_fn;
29 static int current_context, current_contextmask;
31 static struct symbol *degenerate(struct expression *expr);
33 static struct symbol *evaluate_symbol_expression(struct expression *expr)
35 struct symbol *sym = expr->symbol;
36 struct symbol *base_type;
38 if (!sym) {
39 if (preprocessing) {
40 expr->ctype = &int_ctype;
41 return &int_ctype;
43 warn(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
44 return NULL;
47 examine_symbol_type(sym);
48 if ((sym->ctype.context ^ current_context) & (sym->ctype.contextmask & current_contextmask))
49 warn(expr->pos, "Using symbol '%s' in wrong context", show_ident(expr->symbol_name));
51 base_type = sym->ctype.base_type;
52 if (!base_type) {
53 warn(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
54 return NULL;
57 /* The type of a symbol is the symbol itself! */
58 expr->ctype = sym;
60 /* enum's can be turned into plain values */
61 if (sym->type != SYM_ENUM) {
62 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
63 addr->symbol = sym;
64 addr->symbol_name = expr->symbol_name;
65 addr->ctype = NULL; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
66 expr->type = EXPR_PREOP;
67 expr->op = '*';
68 expr->unop = addr;
69 return sym;
71 expr->type = EXPR_VALUE;
72 expr->value = sym->value;
73 expr->ctype = base_type;
74 return sym;
77 static struct symbol *evaluate_string(struct expression *expr)
79 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
80 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
81 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
82 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
83 unsigned int length = expr->string->length;
85 sym->array_size = alloc_const_expression(expr->pos, length);
86 sym->bit_size = bits_in_char * length;
87 sym->ctype.alignment = 1;
88 sym->ctype.modifiers = MOD_STATIC;
89 sym->ctype.base_type = array;
90 sym->initializer = initstr;
92 initstr->ctype = sym;
93 initstr->string = expr->string;
95 array->array_size = sym->array_size;
96 array->bit_size = bits_in_char * length;
97 array->ctype.alignment = 1;
98 array->ctype.modifiers = MOD_STATIC;
99 array->ctype.base_type = &char_ctype;
101 addr->symbol = sym;
102 addr->ctype = NULL;
104 expr->type = EXPR_PREOP;
105 expr->op = '*';
106 expr->unop = addr;
107 expr->ctype = sym;
108 return sym;
111 static inline struct symbol *integer_promotion(struct symbol *type)
113 unsigned long mod = type->ctype.modifiers;
114 int width;
116 if (type->type == SYM_ENUM)
117 return &int_ctype;
118 else if (type->type == SYM_BITFIELD) {
119 mod = type->ctype.base_type->ctype.modifiers;
120 width = type->fieldwidth;
121 } else if (mod & (MOD_CHAR | MOD_SHORT))
122 width = type->bit_size;
123 else
124 return type;
125 if (mod & MOD_UNSIGNED && width == bits_in_int)
126 return &uint_ctype;
127 return &int_ctype;
131 * integer part of usual arithmetic conversions:
132 * integer promotions are applied
133 * if left and right are identical, we are done
134 * if signedness is the same, convert one with lower rank
135 * unless unsigned argument has rank lower than signed one, convert the
136 * signed one.
137 * if signed argument is bigger than unsigned one, convert the unsigned.
138 * otherwise, convert signed.
140 * Leaving aside the integer promotions, that is equivalent to
141 * if identical, don't convert
142 * if left is bigger than right, convert right
143 * if right is bigger than left, convert right
144 * otherwise, if signedness is the same, convert one with lower rank
145 * otherwise convert the signed one.
147 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
149 unsigned long lmod, rmod;
151 left = integer_promotion(left);
152 right = integer_promotion(right);
154 if (left == right)
155 goto left;
157 if (left->bit_size > right->bit_size)
158 goto left;
160 if (right->bit_size > left->bit_size)
161 goto right;
163 lmod = left->ctype.modifiers;
164 rmod = right->ctype.modifiers;
165 if ((lmod ^ rmod) & MOD_UNSIGNED) {
166 if (lmod & MOD_UNSIGNED)
167 goto left;
168 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
169 goto left;
170 right:
171 left = right;
172 left:
173 return left;
176 static struct expression * cast_to(struct expression *old, struct symbol *type)
178 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
179 expr->ctype = type;
180 expr->cast_type = type;
181 expr->cast_expression = old;
182 return expr;
185 static int is_type_type(struct symbol *type)
187 return (type->ctype.modifiers & MOD_TYPE) != 0;
190 static int is_ptr_type(struct symbol *type)
192 if (type->type == SYM_NODE)
193 type = type->ctype.base_type;
194 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
197 static inline int is_int_type(struct symbol *type)
199 if (type->type == SYM_NODE)
200 type = type->ctype.base_type;
201 return (type->type == SYM_ENUM) ||
202 (type->type == SYM_BITFIELD) ||
203 type->ctype.base_type == &int_type;
206 static struct symbol *bad_expr_type(struct expression *expr)
208 warn(expr->pos, "incompatible types for operation");
209 return NULL;
212 static struct symbol * compatible_integer_binop(struct expression *expr, struct expression **lp, struct expression **rp)
214 struct expression *left = *lp, *right = *rp;
215 struct symbol *ltype = left->ctype, *rtype = right->ctype;
217 if (ltype->type == SYM_NODE)
218 ltype = ltype->ctype.base_type;
219 if (rtype->type == SYM_NODE)
220 rtype = rtype->ctype.base_type;
221 if (is_int_type(ltype) && is_int_type(rtype)) {
222 struct symbol *ctype = bigger_int_type(ltype, rtype);
224 /* Don't bother promoting same-size entities, it only adds clutter */
225 if (ltype->bit_size != ctype->bit_size)
226 *lp = cast_to(left, ctype);
227 if (rtype->bit_size != ctype->bit_size)
228 *rp = cast_to(right, ctype);
229 return ctype;
231 return NULL;
234 static struct symbol *evaluate_int_binop(struct expression *expr)
236 struct symbol *ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
237 if (ctype) {
238 expr->ctype = ctype;
239 return ctype;
241 return bad_expr_type(expr);
244 static inline int lvalue_expression(struct expression *expr)
246 while (expr->type == EXPR_CAST)
247 expr = expr->cast_expression;
248 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
251 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
253 struct symbol *ctype;
254 struct symbol *ptr_type = ptr->ctype;
255 int bit_size;
257 if (ptr_type->type == SYM_NODE)
258 ptr_type = ptr_type->ctype.base_type;
260 if (!is_int_type(i->ctype))
261 return bad_expr_type(expr);
263 ctype = ptr->ctype;
264 examine_symbol_type(ctype);
266 ctype = degenerate(ptr);
267 if (!ctype->ctype.base_type) {
268 warn(expr->pos, "missing type information");
269 return NULL;
272 /* Get the size of whatever the pointer points to */
273 ptr_type = ctype;
274 if (ptr_type->type == SYM_NODE)
275 ptr_type = ptr_type->ctype.base_type;
276 if (ptr_type->type == SYM_PTR)
277 ptr_type = ptr_type->ctype.base_type;
278 bit_size = ptr_type->bit_size;
280 /* Special case: adding zero commonly happens as a result of 'array[0]' */
281 if (i->type == EXPR_VALUE && !i->value) {
282 *expr = *ptr;
283 } else if (bit_size > bits_in_char) {
284 struct expression *add = expr;
285 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
286 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
288 val->ctype = size_t_ctype;
289 val->value = bit_size >> 3;
291 mul->op = '*';
292 mul->ctype = size_t_ctype;
293 mul->left = i;
294 mul->right = val;
296 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
297 add->left = ptr;
298 add->right = mul;
301 expr->ctype = ctype;
302 return ctype;
305 static struct symbol *evaluate_add(struct expression *expr)
307 struct expression *left = expr->left, *right = expr->right;
308 struct symbol *ltype = left->ctype, *rtype = right->ctype;
310 if (is_ptr_type(ltype))
311 return evaluate_ptr_add(expr, left, right);
313 if (is_ptr_type(rtype))
314 return evaluate_ptr_add(expr, right, left);
316 // FIXME! FP promotion
317 return evaluate_int_binop(expr);
320 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
321 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE)
323 const char * type_difference(struct symbol *target, struct symbol *source,
324 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
326 for (;;) {
327 unsigned long mod1, mod2, diff;
328 unsigned long as1, as2;
329 int type1, type2;
330 struct symbol *base1, *base2;
332 if (target == source)
333 break;
334 if (!target || !source)
335 return "different types";
337 * Peel of per-node information.
338 * FIXME! Check alignment and context too here!
340 mod1 = target->ctype.modifiers;
341 as1 = target->ctype.as;
342 mod2 = source->ctype.modifiers;
343 as2 = source->ctype.as;
344 if (target->type == SYM_NODE) {
345 target = target->ctype.base_type;
346 if (!target)
347 return "bad types";
348 if (target->type == SYM_PTR) {
349 mod1 = 0;
350 as1 = 0;
352 mod1 |= target->ctype.modifiers;
353 as1 |= target->ctype.as;
355 if (source->type == SYM_NODE) {
356 source = source->ctype.base_type;
357 if (!source)
358 return "bad types";
359 if (source->type == SYM_PTR) {
360 mod2 = 0;
361 as2 = 0;
363 mod2 |= source->ctype.modifiers;
364 as2 |= source->ctype.as;
367 if (target == source)
368 break;
369 if (!target || !source)
370 return "different types";
372 type1 = target->type;
373 base1 = target->ctype.base_type;
375 type2 = source->type;
376 base2 = source->ctype.base_type;
379 * Pointers to functions compare as the function itself
381 if (type1 == SYM_PTR && base1) {
382 switch (base1->type) {
383 case SYM_FN:
384 type1 = SYM_FN;
385 target = base1;
386 base1 = base1->ctype.base_type;
387 default:
388 /* nothing */;
391 if (type2 == SYM_PTR && base2) {
392 switch (base2->type) {
393 case SYM_FN:
394 type2 = SYM_FN;
395 source = base2;
396 base2 = base2->ctype.base_type;
397 default:
398 /* nothing */;
402 /* Arrays degenerate to pointers for type comparisons */
403 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
404 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
406 if (type1 != type2)
407 return "different base types";
409 /* Must be same address space to be comparable */
410 if (as1 != as2)
411 return "different address spaces";
413 /* Ignore differences in storage types, sign, or addressability */
414 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
415 if (diff) {
416 mod1 &= diff & ~target_mod_ignore;
417 mod2 &= diff & ~source_mod_ignore;
418 if (mod1 | mod2) {
419 if ((mod1 | mod2) & MOD_SIZE)
420 return "different type sizes";
421 return "different modifiers";
425 if (type1 == SYM_FN) {
426 int i;
427 struct symbol *arg1, *arg2;
428 if (base1->variadic != base2->variadic)
429 return "incompatible variadic arguments";
430 PREPARE_PTR_LIST(target->arguments, arg1);
431 PREPARE_PTR_LIST(source->arguments, arg2);
432 i = 1;
433 for (;;) {
434 const char *diff;
435 diff = type_difference(arg1, arg2, 0, 0);
436 if (diff) {
437 static char argdiff[80];
438 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
439 return argdiff;
441 if (!arg1)
442 break;
443 NEXT_PTR_LIST(arg1);
444 NEXT_PTR_LIST(arg2);
445 i++;
447 FINISH_PTR_LIST(arg2);
448 FINISH_PTR_LIST(arg1);
451 target = base1;
452 source = base2;
454 return NULL;
457 static int is_null_ptr(struct expression *expr)
459 if (expr->type != EXPR_VALUE || expr->value)
460 return 0;
461 if (!is_ptr_type(expr->ctype))
462 warn(expr->pos, "Using plain integer as NULL pointer");
463 return 1;
466 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
468 /* NULL expression? Just return the type of the "other side" */
469 if (is_null_ptr(r))
470 return l->ctype;
471 if (is_null_ptr(l))
472 return r->ctype;
473 return NULL;
477 * Ignore differences in "volatile" and "const"ness when
478 * subtracting pointers
480 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
482 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
484 const char *typediff;
485 struct symbol *ctype;
486 struct symbol *ltype, *rtype;
488 ltype = degenerate(l);
489 rtype = degenerate(r);
492 * If it is an integer subtract: the ptr add case will do the
493 * right thing.
495 if (!is_ptr_type(rtype))
496 return evaluate_ptr_add(expr, l, r);
498 ctype = ltype;
499 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
500 if (typediff) {
501 ctype = common_ptr_type(l, r);
502 if (!ctype) {
503 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
504 return NULL;
507 examine_symbol_type(ctype);
509 /* Figure out the base type we point to */
510 if (ctype->type == SYM_NODE)
511 ctype = ctype->ctype.base_type;
512 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
513 warn(expr->pos, "subtraction of functions? Share your drugs");
514 return NULL;
516 ctype = ctype->ctype.base_type;
518 expr->ctype = ssize_t_ctype;
519 if (ctype->bit_size > bits_in_char) {
520 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
521 struct expression *div = expr;
522 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
524 val->ctype = size_t_ctype;
525 val->value = ctype->bit_size >> 3;
527 sub->op = '-';
528 sub->ctype = ssize_t_ctype;
529 sub->left = l;
530 sub->right = r;
532 div->op = '/';
533 div->left = sub;
534 div->right = val;
537 return ssize_t_ctype;
540 static struct symbol *evaluate_sub(struct expression *expr)
542 struct expression *left = expr->left, *right = expr->right;
543 struct symbol *ltype = left->ctype;
545 if (is_ptr_type(ltype))
546 return evaluate_ptr_sub(expr, left, right);
548 // FIXME! FP promotion
549 return evaluate_int_binop(expr);
552 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
554 static struct symbol *evaluate_conditional(struct expression *expr)
556 struct symbol *ctype;
558 if (!expr)
559 return NULL;
561 if (expr->type == EXPR_ASSIGNMENT)
562 warn(expr->pos, "assignment expression in conditional");
564 ctype = evaluate_expression(expr);
565 if (ctype && is_safe_type(ctype))
566 warn(expr->pos, "testing a 'safe expression'");
568 return ctype;
571 static struct symbol *evaluate_logical(struct expression *expr)
573 if (!evaluate_conditional(expr->left))
574 return NULL;
575 if (!evaluate_conditional(expr->right))
576 return NULL;
578 expr->ctype = &bool_ctype;
579 return &bool_ctype;
582 static struct symbol *evaluate_shift(struct expression *expr)
584 struct expression *left = expr->left, *right = expr->right;
585 struct symbol *ltype = left->ctype, *rtype = right->ctype;
587 if (ltype->type == SYM_NODE)
588 ltype = ltype->ctype.base_type;
589 if (rtype->type == SYM_NODE)
590 rtype = rtype->ctype.base_type;
591 if (is_int_type(ltype) && is_int_type(rtype)) {
592 struct symbol *ctype = integer_promotion(ltype);
593 if (ltype->bit_size != ctype->bit_size)
594 expr->left = cast_to(expr->left, ctype);
595 expr->ctype = ctype;
596 ctype = integer_promotion(rtype);
597 if (rtype->bit_size != ctype->bit_size)
598 expr->right = cast_to(expr->right, ctype);
599 return expr->ctype;
601 return bad_expr_type(expr);
604 static struct symbol *evaluate_arithmetic(struct expression *expr)
606 // FIXME! Floating-point promotion!
607 return evaluate_int_binop(expr);
610 static struct symbol *evaluate_binop(struct expression *expr)
612 switch (expr->op) {
613 // addition can take ptr+int, fp and int
614 case '+':
615 return evaluate_add(expr);
617 // subtraction can take ptr-ptr, fp and int
618 case '-':
619 return evaluate_sub(expr);
621 // Arithmetic operations can take fp and int
622 case '*': case '/': case '%':
623 return evaluate_arithmetic(expr);
625 // shifts do integer promotions, but that's it.
626 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
627 return evaluate_shift(expr);
629 // The rest are integer operations (bitops)
630 // '&', '^', '|'
631 default:
632 return evaluate_int_binop(expr);
636 static struct symbol *evaluate_comma(struct expression *expr)
638 expr->ctype = expr->right->ctype;
639 return expr->ctype;
642 static struct symbol *evaluate_compare(struct expression *expr)
644 struct expression *left = expr->left, *right = expr->right;
645 struct symbol *ltype = left->ctype, *rtype = right->ctype;
646 struct symbol *ctype;
648 /* Type types? */
649 if (is_type_type(ltype) && is_type_type(rtype)) {
650 expr->ctype = &bool_ctype;
651 return &bool_ctype;
654 if (is_safe_type(ltype) || is_safe_type(rtype))
655 warn(expr->pos, "testing a 'safe expression'");
657 /* Pointer types? */
658 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
659 expr->ctype = &bool_ctype;
660 // FIXME! Check the types for compatibility
661 return &bool_ctype;
664 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
665 if (ctype) {
666 expr->ctype = &bool_ctype;
667 return &bool_ctype;
670 return bad_expr_type(expr);
673 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
675 return (is_int_type(ltype) && is_int_type(rtype));
679 * FIXME!! This should do casts, array degeneration etc..
681 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
683 struct symbol *ltype = left->ctype, *rtype = right->ctype;
685 if (ltype->type == SYM_NODE)
686 ltype = ltype->ctype.base_type;
688 if (rtype->type == SYM_NODE)
689 rtype = rtype->ctype.base_type;
691 if (ltype->type == SYM_PTR) {
692 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
693 return ltype;
696 if (rtype->type == SYM_PTR) {
697 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
698 return rtype;
700 return NULL;
703 static struct symbol * evaluate_conditional_expression(struct expression *expr)
705 struct expression *cond, *true, *false;
706 struct symbol *ctype, *ltype, *rtype;
707 const char * typediff;
709 ctype = degenerate(expr->conditional);
710 cond = expr->conditional;
712 ltype = ctype;
713 true = cond;
714 if (expr->cond_true) {
715 ltype = degenerate(expr->cond_true);
716 true = expr->cond_true;
719 rtype = degenerate(expr->cond_false);
720 false = expr->cond_false;
722 ctype = ltype;
723 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
724 if (typediff) {
725 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
726 if (!ctype) {
727 ctype = compatible_ptr_type(true, expr->cond_false);
728 if (!ctype) {
729 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
730 return NULL;
735 expr->ctype = ctype;
736 return ctype;
739 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
740 struct expression **rp, struct symbol *source, const char *where)
742 const char *typediff;
743 struct symbol *t;
744 int target_as;
746 /* It's ok if the target is more volatile or const than the source */
747 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
748 if (!typediff)
749 return 1;
751 if (compatible_integer_types(target, source)) {
752 if (target->bit_size != source->bit_size)
753 *rp = cast_to(*rp, target);
754 return 1;
757 /* Pointer destination? */
758 t = target;
759 target_as = t->ctype.as;
760 if (t->type == SYM_NODE) {
761 t = t->ctype.base_type;
762 target_as |= t->ctype.as;
764 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
765 struct expression *right = *rp;
766 struct symbol *s = source;
767 int source_as;
769 // NULL pointer is always ok
770 if (is_null_ptr(right))
771 return 1;
773 /* "void *" matches anything as long as the address space is ok */
774 source_as = s->ctype.as;
775 if (s->type == SYM_NODE) {
776 s = s->ctype.base_type;
777 source_as |= s->ctype.as;
779 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
780 s = s->ctype.base_type;
781 t = t->ctype.base_type;
782 if (s == &void_ctype || t == &void_ctype)
783 return 1;
787 // FIXME!! Cast it?
788 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
789 info(expr->pos, " expected %s", show_typename(target));
790 info(expr->pos, " got %s", show_typename(source));
791 return 0;
795 * FIXME!! This is wrong from a double evaluation standpoint. We can't
796 * just expand the expression twice, that would make any side effects
797 * happen twice too.
799 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
801 int op = expr->op;
802 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
803 static const int op_trans[] = {
804 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
805 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
806 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
807 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
808 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
809 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
810 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
811 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
812 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
813 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
816 subexpr->left = left;
817 subexpr->right = right;
818 subexpr->op = op_trans[op - SPECIAL_BASE];
819 expr->op = '=';
820 expr->right = subexpr;
821 return evaluate_binop(subexpr);
824 static void evaluate_assign_to(struct expression *left, struct symbol *type)
826 if (type->ctype.modifiers & MOD_CONST)
827 warn(left->pos, "assignment to const expression");
828 if (type->type == SYM_NODE)
829 type->ctype.modifiers |= MOD_ASSIGNED;
832 static struct symbol *evaluate_assignment(struct expression *expr)
834 struct expression *left = expr->left, *right = expr->right;
835 struct symbol *ltype, *rtype;
837 ltype = left->ctype;
838 rtype = right->ctype;
839 if (expr->op != '=') {
840 rtype = evaluate_binop_assignment(expr, left, right);
841 if (!rtype)
842 return NULL;
843 right = expr->right;
846 if (!lvalue_expression(left)) {
847 warn(expr->pos, "not an lvalue");
848 return NULL;
851 rtype = degenerate(right);
853 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
854 return NULL;
856 evaluate_assign_to(left, ltype);
858 expr->ctype = ltype;
859 return ltype;
862 static void examine_fn_arguments(struct symbol *fn)
864 struct symbol *s;
866 FOR_EACH_PTR(fn->arguments, s) {
867 struct symbol *arg = evaluate_symbol(s);
868 /* Array/function arguments silently degenerate into pointers */
869 if (arg) {
870 struct symbol *ptr;
871 switch(arg->type) {
872 case SYM_ARRAY:
873 case SYM_FN:
874 ptr = alloc_symbol(s->pos, SYM_PTR);
875 if (arg->type == SYM_ARRAY)
876 ptr->ctype = arg->ctype;
877 else
878 ptr->ctype.base_type = arg;
879 ptr->ctype.as |= s->ctype.as;
880 ptr->ctype.modifiers |= s->ctype.modifiers;
882 s->ctype.base_type = ptr;
883 s->ctype.as = 0;
884 s->ctype.modifiers = 0;
885 examine_symbol_type(s);
886 break;
887 default:
888 /* nothing */
889 break;
892 } END_FOR_EACH_PTR;
895 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
897 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
898 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
899 *newsym = *sym;
900 newsym->ctype.as = as;
901 newsym->ctype.modifiers = mod;
902 sym = newsym;
904 return sym;
907 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
909 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
910 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
912 node->ctype.base_type = ptr;
913 ptr->bit_size = bits_in_pointer;
914 ptr->ctype.alignment = pointer_alignment;
916 node->bit_size = bits_in_pointer;
917 node->ctype.alignment = pointer_alignment;
919 sym->ctype.modifiers |= MOD_ADDRESSABLE;
920 if (sym->ctype.modifiers & MOD_REGISTER) {
921 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
922 sym->ctype.modifiers &= ~MOD_REGISTER;
924 if (sym->type == SYM_NODE) {
925 ptr->ctype.as |= sym->ctype.as;
926 ptr->ctype.modifiers |= sym->ctype.modifiers;
927 sym = sym->ctype.base_type;
929 if (degenerate && sym->type == SYM_ARRAY) {
930 ptr->ctype.as |= sym->ctype.as;
931 ptr->ctype.modifiers |= sym->ctype.modifiers;
932 sym = sym->ctype.base_type;
934 ptr->ctype.base_type = sym;
936 return node;
939 /* Arrays degenerate into pointers on pointer arithmetic */
940 static struct symbol *degenerate(struct expression *expr)
942 struct symbol *ctype, *base;
944 if (!expr)
945 return NULL;
946 ctype = expr->ctype;
947 if (!ctype)
948 return NULL;
949 base = ctype;
950 if (ctype->type == SYM_NODE)
951 base = ctype->ctype.base_type;
953 * Arrays degenerate into pointers to the entries, while
954 * functions degenerate into pointers to themselves
956 switch (base->type) {
957 case SYM_FN:
958 case SYM_ARRAY:
959 if (expr->op != '*' || expr->type != EXPR_PREOP) {
960 warn(expr->pos, "strange non-value function or array");
961 return NULL;
963 *expr = *expr->unop;
964 ctype = create_pointer(expr, ctype, 1);
965 expr->ctype = ctype;
966 default:
967 /* nothing */;
969 return ctype;
972 static struct symbol *evaluate_addressof(struct expression *expr)
974 struct expression *op = expr->unop;
975 struct symbol *ctype;
977 if (op->op != '*' || op->type != EXPR_PREOP) {
978 warn(expr->pos, "not addressable");
979 return NULL;
981 ctype = op->ctype;
982 *expr = *op->unop;
985 * symbol expression evaluation is lazy about the type
986 * of the sub-expression, so we may have to generate
987 * the type here if so..
989 if (!expr->ctype) {
990 ctype = create_pointer(expr, ctype, 0);
991 expr->ctype = ctype;
993 return expr->ctype;
997 static struct symbol *evaluate_dereference(struct expression *expr)
999 struct expression *op = expr->unop;
1000 struct symbol *ctype = op->ctype, *node, *target;
1002 /* Simplify: *&(expr) => (expr) */
1003 if (op->type == EXPR_PREOP && op->op == '&') {
1004 *expr = *op->unop;
1005 return expr->ctype;
1008 /* Dereferencing a node drops all the node information. */
1009 if (ctype->type == SYM_NODE)
1010 ctype = ctype->ctype.base_type;
1012 node = alloc_symbol(expr->pos, SYM_NODE);
1013 target = ctype->ctype.base_type;
1015 switch (ctype->type) {
1016 default:
1017 warn(expr->pos, "cannot derefence this type");
1018 return NULL;
1019 case SYM_PTR:
1020 merge_type(node, ctype);
1021 if (ctype->type != SYM_ARRAY)
1022 break;
1024 * Dereferencing a pointer to an array results in a
1025 * degenerate dereference: the expression becomes
1026 * just a pointer to the entry, and the derefence
1027 * goes away.
1029 *expr = *op;
1031 target = alloc_symbol(expr->pos, SYM_PTR);
1032 target->bit_size = bits_in_pointer;
1033 target->ctype.alignment = pointer_alignment;
1034 merge_type(target, ctype->ctype.base_type);
1035 break;
1037 case SYM_ARRAY:
1039 * When an array is dereferenced, we need to pick
1040 * up the attributes of the original node too..
1042 merge_type(node, op->ctype);
1043 merge_type(node, ctype);
1044 break;
1047 node->bit_size = target->bit_size;
1048 node->array_size = target->array_size;
1050 expr->ctype = node;
1051 return node;
1055 * Unary post-ops: x++ and x--
1057 static struct symbol *evaluate_postop(struct expression *expr)
1059 struct expression *op = expr->unop;
1060 struct symbol *ctype = op->ctype;
1062 if (!lvalue_expression(expr->unop)) {
1063 warn(expr->pos, "need lvalue expression for ++/--");
1064 return NULL;
1067 evaluate_assign_to(op, ctype);
1069 expr->ctype = ctype;
1070 return ctype;
1073 static struct symbol *evaluate_preop(struct expression *expr)
1075 struct symbol *ctype = expr->unop->ctype;
1077 switch (expr->op) {
1078 case '(':
1079 case '+':
1080 *expr = *expr->unop;
1081 return ctype;
1083 case '*':
1084 return evaluate_dereference(expr);
1086 case '&':
1087 return evaluate_addressof(expr);
1089 case SPECIAL_INCREMENT:
1090 case SPECIAL_DECREMENT:
1092 * From a type evaluation standpoint the pre-ops are
1093 * the same as the postops
1095 return evaluate_postop(expr);
1097 case '!':
1098 if (is_safe_type(ctype))
1099 warn(expr->pos, "testing a 'safe expression'");
1100 ctype = &bool_ctype;
1101 break;
1103 default:
1104 break;
1106 expr->ctype = ctype;
1107 return &bool_ctype;
1110 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1112 struct ptr_list *head = (struct ptr_list *)_list;
1113 struct ptr_list *list = head;
1115 if (!head)
1116 return NULL;
1117 do {
1118 int i;
1119 for (i = 0; i < list->nr; i++) {
1120 struct symbol *sym = (struct symbol *) list->list[i];
1121 if (sym->ident) {
1122 if (sym->ident != ident)
1123 continue;
1124 *offset = sym->offset;
1125 return sym;
1126 } else {
1127 struct symbol *ctype = sym->ctype.base_type;
1128 struct symbol *sub;
1129 if (!ctype)
1130 continue;
1131 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1132 continue;
1133 sub = find_identifier(ident, ctype->symbol_list, offset);
1134 if (!sub)
1135 continue;
1136 *offset += sym->offset;
1137 return sub;
1140 } while ((list = list->next) != head);
1141 return NULL;
1144 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1146 struct expression *add;
1148 add = expr;
1149 if (offset) {
1150 /* Create a new add-expression */
1151 add = alloc_expression(expr->pos, EXPR_BINOP);
1152 add->op = '+';
1153 add->left = expr;
1154 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1155 add->right->ctype = &int_ctype;
1156 add->right->value = offset;
1160 * The ctype of the pointer will be lazily evaluated if
1161 * we ever take the address of this member dereference..
1163 add->ctype = NULL;
1164 return add;
1167 /* structure/union dereference */
1168 static struct symbol *evaluate_member_dereference(struct expression *expr)
1170 int offset;
1171 struct symbol *ctype, *member;
1172 struct expression *deref = expr->deref, *add;
1173 struct ident *ident = expr->member;
1174 unsigned int mod;
1175 int address_space;
1177 if (!evaluate_expression(deref))
1178 return NULL;
1179 if (!ident) {
1180 warn(expr->pos, "bad member name");
1181 return NULL;
1184 ctype = deref->ctype;
1185 address_space = ctype->ctype.as;
1186 mod = ctype->ctype.modifiers;
1187 if (ctype->type == SYM_NODE) {
1188 ctype = ctype->ctype.base_type;
1189 address_space |= ctype->ctype.as;
1190 mod |= ctype->ctype.modifiers;
1192 if (!lvalue_expression(deref)) {
1193 warn(deref->pos, "expected lvalue for member dereference");
1194 return NULL;
1196 deref = deref->unop;
1197 expr->deref = deref;
1198 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1199 warn(expr->pos, "expected structure or union");
1200 return NULL;
1202 offset = 0;
1203 member = find_identifier(ident, ctype->symbol_list, &offset);
1204 if (!member) {
1205 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1206 const char *name = "<unnamed>";
1207 int namelen = 9;
1208 if (ctype->ident) {
1209 name = ctype->ident->name;
1210 namelen = ctype->ident->len;
1212 warn(expr->pos, "no member '%s' in %s %.*s",
1213 show_ident(ident), type, namelen, name);
1214 return NULL;
1218 * The member needs to take on the address space and modifiers of
1219 * the "parent" type.
1221 member = convert_to_as_mod(member, address_space, mod);
1222 add = evaluate_offset(deref, offset);
1224 ctype = member->ctype.base_type;
1225 if (ctype->type == SYM_BITFIELD) {
1226 expr->type = EXPR_BITFIELD;
1227 expr->bitpos = member->bit_offset;
1228 expr->nrbits = member->fieldwidth;
1229 expr->address = add;
1230 } else {
1231 expr->type = EXPR_PREOP;
1232 expr->op = '*';
1233 expr->unop = add;
1236 expr->ctype = member;
1237 return member;
1240 static struct symbol *evaluate_sizeof(struct expression *expr)
1242 int size;
1244 if (expr->cast_type) {
1245 examine_symbol_type(expr->cast_type);
1246 size = expr->cast_type->bit_size;
1247 } else {
1248 if (!evaluate_expression(expr->cast_expression))
1249 return NULL;
1250 size = expr->cast_expression->ctype->bit_size;
1252 if (size & 7) {
1253 warn(expr->pos, "cannot size expression");
1254 return NULL;
1256 expr->type = EXPR_VALUE;
1257 expr->value = size >> 3;
1258 expr->ctype = size_t_ctype;
1259 return size_t_ctype;
1262 static struct symbol *evaluate_alignof(struct expression *expr)
1264 struct symbol *type = expr->cast_type;
1266 if (!type) {
1267 type = evaluate_expression(expr->cast_expression);
1268 if (!type)
1269 return NULL;
1271 examine_symbol_type(type);
1272 expr->type = EXPR_VALUE;
1273 expr->value = type->ctype.alignment;
1274 expr->ctype = size_t_ctype;
1275 return size_t_ctype;
1278 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1280 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1281 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1282 return clash != 0;
1285 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1287 struct expression *expr;
1288 struct symbol_list *argument_types = fn->arguments;
1289 struct symbol *argtype;
1290 int i = 1;
1292 PREPARE_PTR_LIST(argument_types, argtype);
1293 FOR_EACH_PTR (head, expr) {
1294 struct expression **p = THIS_ADDRESS(expr);
1295 struct symbol *ctype, *target;
1296 ctype = evaluate_expression(expr);
1298 if (!ctype)
1299 return 0;
1301 if (context_clash(f, ctype))
1302 warn(expr->pos, "argument %d used in wrong context", i);
1304 ctype = degenerate(expr);
1306 target = argtype;
1307 if (!target && ctype->bit_size < bits_in_int)
1308 target = &int_ctype;
1309 if (target) {
1310 static char where[30];
1311 examine_symbol_type(target);
1312 sprintf(where, "argument %d", i);
1313 compatible_assignment_types(expr, target, p, ctype, where);
1316 i++;
1317 NEXT_PTR_LIST(argtype);
1318 } END_FOR_EACH_PTR;
1319 FINISH_PTR_LIST(argtype);
1320 return 1;
1323 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1324 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1326 struct expression *entry;
1327 int current = 0;
1328 int max = 0;
1330 FOR_EACH_PTR(expr->expr_list, entry) {
1331 struct expression **p = THIS_ADDRESS(entry);
1333 if (entry->type == EXPR_INDEX) {
1334 current = entry->idx_to;
1335 continue;
1337 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1338 current++;
1339 if (current > max)
1340 max = current;
1341 } END_FOR_EACH_PTR;
1342 return max;
1345 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1346 static int evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1348 if (offset || expression_list_size(expr->expr_list) != 1) {
1349 warn(expr->pos, "unexpected compound initializer");
1350 return 0;
1352 return evaluate_array_initializer(ctype, expr, 0);
1355 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1357 struct expression *entry;
1358 struct symbol *sym;
1360 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1361 FOR_EACH_PTR(expr->expr_list, entry) {
1362 struct expression **p = THIS_ADDRESS(entry);
1364 if (entry->type == EXPR_IDENTIFIER) {
1365 struct ident *ident = entry->expr_ident;
1366 /* We special-case the "already right place" case */
1367 if (sym && sym->ident == ident)
1368 continue;
1369 RESET_PTR_LIST(sym);
1370 for (;;) {
1371 if (!sym) {
1372 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1373 return 0;
1375 if (sym->ident == ident)
1376 break;
1377 NEXT_PTR_LIST(sym);
1379 continue;
1382 if (!sym) {
1383 warn(expr->pos, "too many initializers for struct/union");
1384 return 0;
1387 evaluate_initializer(sym, p, offset + sym->offset);
1389 NEXT_PTR_LIST(sym);
1390 } END_FOR_EACH_PTR;
1391 FINISH_PTR_LIST(sym);
1393 return 0;
1397 * Initializers are kind of like assignments. Except
1398 * they can be a hell of a lot more complex.
1400 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1402 struct expression *expr = *ep;
1405 * Simple non-structure/array initializers are the simple
1406 * case, and look (and parse) largely like assignments.
1408 if (expr->type != EXPR_INITIALIZER) {
1409 int size = 0;
1410 struct symbol *rtype = evaluate_expression(expr);
1411 if (rtype) {
1412 struct expression *pos;
1414 // FIXME! char array[] = "string" special case
1415 // should _not_ degenerate.
1416 rtype = degenerate(expr);
1417 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1418 /* strings are special: char arrays */
1419 if (rtype->type == SYM_ARRAY)
1420 size = get_expression_value(rtype->array_size);
1422 * Don't bother creating a position expression for
1423 * the simple initializer cases that don't need it.
1425 * We need a position if the initializer has a byte
1426 * offset, _or_ if we're initializing a bitfield.
1428 if (offset || ctype->fieldwidth) {
1429 pos = alloc_expression(expr->pos, EXPR_POS);
1430 pos->init_offset = offset;
1431 pos->init_sym = ctype;
1432 pos->init_expr = *ep;
1433 pos->ctype = expr->ctype;
1434 *ep = pos;
1437 return size;
1440 expr->ctype = ctype;
1441 if (ctype->type == SYM_NODE)
1442 ctype = ctype->ctype.base_type;
1444 switch (ctype->type) {
1445 case SYM_ARRAY:
1446 case SYM_PTR:
1447 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1448 case SYM_UNION:
1449 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1450 case SYM_STRUCT:
1451 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1452 default:
1453 return evaluate_scalar_initializer(ctype, expr, offset);
1457 static int get_as(struct symbol *sym)
1459 int as;
1460 unsigned long mod;
1462 if (!sym)
1463 return 0;
1464 as = sym->ctype.as;
1465 mod = sym->ctype.modifiers;
1466 if (sym->type == SYM_NODE) {
1467 sym = sym->ctype.base_type;
1468 as |= sym->ctype.as;
1469 mod |= sym->ctype.modifiers;
1472 * You can always throw a value away by casting to
1473 * "void" - that's an implicit "force". Note that
1474 * the same is _not_ true of "void *".
1476 if (sym == &void_ctype)
1477 return -1;
1480 * At least for now, allow casting to a "unsigned long".
1481 * That's how we do things like pointer arithmetic and
1482 * store pointers to registers.
1484 if (sym == &ulong_ctype)
1485 return -1;
1487 if (sym && sym->type == SYM_PTR) {
1488 sym = sym->ctype.base_type;
1489 as |= sym->ctype.as;
1490 mod |= sym->ctype.modifiers;
1492 if (mod & MOD_FORCE)
1493 return -1;
1494 return as;
1497 static struct symbol *evaluate_cast(struct expression *expr)
1499 struct expression *target = expr->cast_expression;
1500 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1502 expr->ctype = ctype;
1503 expr->cast_type = ctype;
1506 * Special case: a cast can be followed by an
1507 * initializer, in which case we need to pass
1508 * the type value down to that initializer rather
1509 * than trying to evaluate it as an expression
1511 * A more complex case is when the initializer is
1512 * dereferenced as part of a post-fix expression.
1513 * We need to produce an expression that can be dereferenced.
1515 if (target->type == EXPR_INITIALIZER) {
1516 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
1517 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1519 sym->ctype.base_type = ctype;
1520 sym->initializer = expr->cast_expression;
1521 evaluate_symbol(sym);
1523 addr->ctype = NULL; /* Lazy eval */
1524 addr->symbol = sym;
1526 expr->type = EXPR_PREOP;
1527 expr->op = '*';
1528 expr->unop = addr;
1529 expr->ctype = ctype;
1530 return ctype;
1533 evaluate_expression(target);
1534 degenerate(target);
1536 if (!get_as(ctype) && get_as(target->ctype) > 0)
1537 warn(expr->pos, "cast removes address space of expression");
1540 * Casts of constant values are special: they
1541 * can be NULL, and thus need to be simplified
1542 * early.
1544 if (target->type == EXPR_VALUE)
1545 cast_value(expr, ctype, target, target->ctype);
1547 return ctype;
1551 * Evaluate a call expression with a symbol. This
1552 * should expand inline functions, and evaluate
1553 * builtins.
1555 static int evaluate_symbol_call(struct expression *expr)
1557 struct expression *fn = expr->fn;
1558 struct symbol *ctype = fn->ctype;
1560 if (fn->type != EXPR_PREOP)
1561 return 0;
1563 if (ctype->op && ctype->op->evaluate)
1564 return ctype->op->evaluate(expr);
1566 if (ctype->ctype.modifiers & MOD_INLINE) {
1567 int ret;
1568 struct symbol *curr = current_fn;
1569 unsigned long context = current_context;
1570 unsigned long mask = current_contextmask;
1572 current_context |= ctype->ctype.context;
1573 current_contextmask |= ctype->ctype.contextmask;
1574 current_fn = ctype->ctype.base_type;
1575 examine_fn_arguments(current_fn);
1577 ret = inline_function(expr, ctype);
1579 /* restore the old function context */
1580 current_fn = curr;
1581 current_context = context;
1582 current_contextmask = mask;
1583 return ret;
1586 return 0;
1589 static struct symbol *evaluate_call(struct expression *expr)
1591 int args, fnargs;
1592 struct symbol *ctype, *sym;
1593 struct expression *fn = expr->fn;
1594 struct expression_list *arglist = expr->args;
1596 if (!evaluate_expression(fn))
1597 return NULL;
1598 sym = ctype = fn->ctype;
1599 if (ctype->type == SYM_NODE)
1600 ctype = ctype->ctype.base_type;
1601 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1602 ctype = ctype->ctype.base_type;
1603 if (!evaluate_arguments(sym, ctype, arglist))
1604 return NULL;
1605 if (ctype->type != SYM_FN) {
1606 warn(expr->pos, "not a function %s", show_ident(sym->ident));
1607 return NULL;
1609 args = expression_list_size(expr->args);
1610 fnargs = symbol_list_size(ctype->arguments);
1611 if (args < fnargs)
1612 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1613 if (args > fnargs && !ctype->variadic)
1614 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1615 if (sym->type == SYM_NODE) {
1616 if (evaluate_symbol_call(expr))
1617 return expr->ctype;
1619 expr->ctype = ctype->ctype.base_type;
1620 return expr->ctype;
1623 struct symbol *evaluate_expression(struct expression *expr)
1625 if (!expr)
1626 return NULL;
1627 if (expr->ctype)
1628 return expr->ctype;
1630 switch (expr->type) {
1631 case EXPR_VALUE:
1632 warn(expr->pos, "value expression without a type");
1633 return NULL;
1634 case EXPR_STRING:
1635 return evaluate_string(expr);
1636 case EXPR_SYMBOL:
1637 return evaluate_symbol_expression(expr);
1638 case EXPR_BINOP:
1639 if (!evaluate_expression(expr->left))
1640 return NULL;
1641 if (!evaluate_expression(expr->right))
1642 return NULL;
1643 return evaluate_binop(expr);
1644 case EXPR_LOGICAL:
1645 return evaluate_logical(expr);
1646 case EXPR_COMMA:
1647 if (!evaluate_expression(expr->left))
1648 return NULL;
1649 if (!evaluate_expression(expr->right))
1650 return NULL;
1651 return evaluate_comma(expr);
1652 case EXPR_COMPARE:
1653 if (!evaluate_expression(expr->left))
1654 return NULL;
1655 if (!evaluate_expression(expr->right))
1656 return NULL;
1657 return evaluate_compare(expr);
1658 case EXPR_ASSIGNMENT:
1659 if (!evaluate_expression(expr->left))
1660 return NULL;
1661 if (!evaluate_expression(expr->right))
1662 return NULL;
1663 return evaluate_assignment(expr);
1664 case EXPR_PREOP:
1665 if (!evaluate_expression(expr->unop))
1666 return NULL;
1667 return evaluate_preop(expr);
1668 case EXPR_POSTOP:
1669 if (!evaluate_expression(expr->unop))
1670 return NULL;
1671 return evaluate_postop(expr);
1672 case EXPR_CAST:
1673 return evaluate_cast(expr);
1674 case EXPR_SIZEOF:
1675 return evaluate_sizeof(expr);
1676 case EXPR_ALIGNOF:
1677 return evaluate_alignof(expr);
1678 case EXPR_DEREF:
1679 return evaluate_member_dereference(expr);
1680 case EXPR_CALL:
1681 return evaluate_call(expr);
1682 case EXPR_BITFIELD:
1683 warn(expr->pos, "bitfield generated by parser");
1684 return NULL;
1685 case EXPR_CONDITIONAL:
1686 if (!evaluate_conditional(expr->conditional))
1687 return NULL;
1688 if (!evaluate_expression(expr->cond_false))
1689 return NULL;
1690 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1691 return NULL;
1692 return evaluate_conditional_expression(expr);
1693 case EXPR_STATEMENT:
1694 expr->ctype = evaluate_statement(expr->statement);
1695 return expr->ctype;
1697 case EXPR_LABEL:
1698 expr->ctype = &ptr_ctype;
1699 return &ptr_ctype;
1701 case EXPR_TYPE:
1702 /* Evaluate the type of the symbol .. */
1703 evaluate_symbol(expr->symbol);
1704 /* .. but the type of the _expression_ is a "type" */
1705 expr->ctype = &type_ctype;
1706 return &type_ctype;
1708 /* These can not exist as stand-alone expressions */
1709 case EXPR_INITIALIZER:
1710 case EXPR_IDENTIFIER:
1711 case EXPR_INDEX:
1712 case EXPR_POS:
1713 warn(expr->pos, "internal front-end error: initializer in expression");
1714 return NULL;
1716 return NULL;
1719 void check_duplicates(struct symbol *sym)
1721 struct symbol *next = sym;
1723 while ((next = next->same_symbol) != NULL) {
1724 const char *typediff;
1725 evaluate_symbol(next);
1726 typediff = type_difference(sym, next, 0, 0);
1727 if (typediff) {
1728 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1729 show_ident(sym->ident),
1730 input_streams[next->pos.stream].name, next->pos.line, typediff);
1731 return;
1736 struct symbol *evaluate_symbol(struct symbol *sym)
1738 struct symbol *base_type;
1740 if (!sym)
1741 return sym;
1743 sym = examine_symbol_type(sym);
1744 base_type = sym->ctype.base_type;
1745 if (!base_type)
1746 return NULL;
1748 /* Evaluate the initializers */
1749 if (sym->initializer) {
1750 int count = evaluate_initializer(sym, &sym->initializer, 0);
1751 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
1752 int bit_size = count * base_type->ctype.base_type->bit_size;
1753 base_type->array_size = alloc_const_expression(sym->pos, count);
1754 base_type->bit_size = bit_size;
1755 sym->array_size = base_type->array_size;
1756 sym->bit_size = bit_size;
1760 /* And finally, evaluate the body of the symbol too */
1761 if (base_type->type == SYM_FN) {
1762 examine_fn_arguments(base_type);
1763 if (base_type->stmt) {
1764 current_fn = base_type;
1765 current_contextmask = sym->ctype.contextmask;
1766 current_context = sym->ctype.context;
1767 evaluate_statement(base_type->stmt);
1771 return base_type;
1774 struct symbol *evaluate_return_expression(struct statement *stmt)
1776 struct expression *expr = stmt->expression;
1777 struct symbol *ctype, *fntype;
1779 evaluate_expression(expr);
1780 ctype = degenerate(expr);
1781 fntype = current_fn->ctype.base_type;
1782 if (!fntype || fntype == &void_ctype) {
1783 if (expr && ctype != &void_ctype)
1784 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
1785 return NULL;
1788 if (!expr) {
1789 warn(stmt->pos, "return with no return value");
1790 return NULL;
1792 if (!ctype)
1793 return NULL;
1794 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
1795 return NULL;
1798 static void evaluate_if_statement(struct statement *stmt)
1800 struct expression *expr = stmt->if_conditional;
1801 struct symbol *ctype;
1803 if (!expr)
1804 return;
1806 ctype = evaluate_conditional(expr);
1807 if (!ctype)
1808 return;
1810 evaluate_statement(stmt->if_true);
1811 evaluate_statement(stmt->if_false);
1814 struct symbol *evaluate_statement(struct statement *stmt)
1816 if (!stmt)
1817 return NULL;
1819 switch (stmt->type) {
1820 case STMT_RETURN:
1821 return evaluate_return_expression(stmt);
1823 case STMT_EXPRESSION:
1824 evaluate_expression(stmt->expression);
1825 return degenerate(stmt->expression);
1827 case STMT_COMPOUND: {
1828 struct statement *s;
1829 struct symbol *type = NULL;
1830 struct symbol *sym;
1832 /* Evaluate each symbol in the compound statement */
1833 FOR_EACH_PTR(stmt->syms, sym) {
1834 evaluate_symbol(sym);
1835 } END_FOR_EACH_PTR;
1836 evaluate_symbol(stmt->ret);
1839 * Then, evaluate each statement, making the type of the
1840 * compound statement be the type of the last statement
1842 type = NULL;
1843 FOR_EACH_PTR(stmt->stmts, s) {
1844 type = evaluate_statement(s);
1845 } END_FOR_EACH_PTR;
1846 return type;
1848 case STMT_IF:
1849 evaluate_if_statement(stmt);
1850 return NULL;
1851 case STMT_ITERATOR:
1852 evaluate_conditional(stmt->iterator_pre_condition);
1853 evaluate_conditional(stmt->iterator_post_condition);
1854 evaluate_statement(stmt->iterator_pre_statement);
1855 evaluate_statement(stmt->iterator_statement);
1856 evaluate_statement(stmt->iterator_post_statement);
1857 return NULL;
1858 case STMT_SWITCH:
1859 evaluate_expression(stmt->switch_expression);
1860 evaluate_statement(stmt->switch_statement);
1861 return NULL;
1862 case STMT_CASE:
1863 evaluate_expression(stmt->case_expression);
1864 evaluate_expression(stmt->case_to);
1865 evaluate_statement(stmt->case_statement);
1866 return NULL;
1867 case STMT_LABEL:
1868 evaluate_statement(stmt->label_statement);
1869 return NULL;
1870 case STMT_GOTO:
1871 evaluate_expression(stmt->goto_expression);
1872 return NULL;
1873 case STMT_NONE:
1874 break;
1875 case STMT_ASM:
1876 /* FIXME! Do the asm parameter evaluation! */
1877 break;
1879 return NULL;