[PATCH] better recovery from type errors in EXPR_COMMA
[smatch.git] / evaluate.c
blob6d86335d30e87e4e5dfb02722d66d8fb4e2a1e4f
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 = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
66 expr->type = EXPR_PREOP;
67 expr->op = '*';
68 expr->unop = addr;
69 return sym;
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 = &lazy_ptr_ctype;
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_float_type(struct symbol *type)
199 if (type->type == SYM_NODE)
200 type = type->ctype.base_type;
201 return type->ctype.base_type == &fp_type;
204 static inline int is_byte_type(struct symbol *type)
206 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
209 static inline int is_string_type(struct symbol *type)
211 if (type->type == SYM_NODE)
212 type = type->ctype.base_type;
213 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
216 static struct symbol *bad_expr_type(struct expression *expr)
218 warn(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
219 switch (expr->type) {
220 case EXPR_BINOP:
221 case EXPR_COMPARE:
222 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
223 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
224 break;
225 case EXPR_PREOP:
226 case EXPR_POSTOP:
227 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
228 break;
229 default:
230 break;
233 return NULL;
236 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
238 struct expression *left = *lp, *right = *rp;
239 struct symbol *ltype = left->ctype, *rtype = right->ctype;
241 if (ltype->type == SYM_NODE)
242 ltype = ltype->ctype.base_type;
243 if (rtype->type == SYM_NODE)
244 rtype = rtype->ctype.base_type;
245 if (is_float_type(ltype)) {
246 if (is_int_type(rtype))
247 goto Left;
248 if (is_float_type(rtype)) {
249 unsigned long lmod = ltype->ctype.modifiers;
250 unsigned long rmod = rtype->ctype.modifiers;
251 lmod &= MOD_LONG | MOD_LONGLONG;
252 rmod &= MOD_LONG | MOD_LONGLONG;
253 if (lmod == rmod)
254 return ltype;
255 if (lmod & ~rmod)
256 goto Left;
257 else
258 goto Right;
260 return NULL;
262 if (!is_float_type(rtype) || !is_int_type(ltype))
263 return NULL;
264 Right:
265 *lp = cast_to(left, rtype);
266 return rtype;
267 Left:
268 *rp = cast_to(right, ltype);
269 return ltype;
272 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
274 struct expression *left = *lp, *right = *rp;
275 struct symbol *ltype = left->ctype, *rtype = right->ctype;
277 if (ltype->type == SYM_NODE)
278 ltype = ltype->ctype.base_type;
279 if (rtype->type == SYM_NODE)
280 rtype = rtype->ctype.base_type;
281 if (is_int_type(ltype) && is_int_type(rtype)) {
282 struct symbol *ctype = bigger_int_type(ltype, rtype);
284 /* Don't bother promoting same-size entities, it only adds clutter */
285 if (ltype->bit_size != ctype->bit_size)
286 *lp = cast_to(left, ctype);
287 if (rtype->bit_size != ctype->bit_size)
288 *rp = cast_to(right, ctype);
289 return ctype;
291 return NULL;
294 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
296 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
297 if (!ctype && float_ok)
298 ctype = compatible_float_binop(&expr->left, &expr->right);
299 if (ctype) {
300 expr->ctype = ctype;
301 return ctype;
303 return bad_expr_type(expr);
306 static inline int lvalue_expression(struct expression *expr)
308 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
311 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
313 struct symbol *ctype;
314 struct symbol *ptr_type = ptr->ctype;
315 int bit_size;
317 if (ptr_type->type == SYM_NODE)
318 ptr_type = ptr_type->ctype.base_type;
320 if (!is_int_type(i->ctype))
321 return bad_expr_type(expr);
323 ctype = ptr->ctype;
324 examine_symbol_type(ctype);
326 ctype = degenerate(ptr);
327 if (!ctype->ctype.base_type) {
328 warn(expr->pos, "missing type information");
329 return NULL;
332 /* Get the size of whatever the pointer points to */
333 ptr_type = ctype;
334 if (ptr_type->type == SYM_NODE)
335 ptr_type = ptr_type->ctype.base_type;
336 if (ptr_type->type == SYM_PTR)
337 ptr_type = ptr_type->ctype.base_type;
338 bit_size = ptr_type->bit_size;
340 /* Special case: adding zero commonly happens as a result of 'array[0]' */
341 if (i->type == EXPR_VALUE && !i->value) {
342 *expr = *ptr;
343 } else if (bit_size > bits_in_char) {
344 struct expression *add = expr;
345 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
346 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
348 val->ctype = size_t_ctype;
349 val->value = bit_size >> 3;
351 mul->op = '*';
352 mul->ctype = size_t_ctype;
353 mul->left = i;
354 mul->right = val;
356 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
357 add->left = ptr;
358 add->right = mul;
361 expr->ctype = ctype;
362 return ctype;
365 static struct symbol *evaluate_add(struct expression *expr)
367 struct expression *left = expr->left, *right = expr->right;
368 struct symbol *ltype = left->ctype, *rtype = right->ctype;
370 if (is_ptr_type(ltype))
371 return evaluate_ptr_add(expr, left, right);
373 if (is_ptr_type(rtype))
374 return evaluate_ptr_add(expr, right, left);
376 return evaluate_arith(expr, 1);
379 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
380 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED)
382 const char * type_difference(struct symbol *target, struct symbol *source,
383 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
385 for (;;) {
386 unsigned long mod1, mod2, diff;
387 unsigned long as1, as2;
388 int type1, type2;
389 struct symbol *base1, *base2;
391 if (target == source)
392 break;
393 if (!target || !source)
394 return "different types";
396 * Peel of per-node information.
397 * FIXME! Check alignment and context too here!
399 mod1 = target->ctype.modifiers;
400 as1 = target->ctype.as;
401 mod2 = source->ctype.modifiers;
402 as2 = source->ctype.as;
403 if (target->type == SYM_NODE) {
404 target = target->ctype.base_type;
405 if (!target)
406 return "bad types";
407 if (target->type == SYM_PTR) {
408 mod1 = 0;
409 as1 = 0;
411 mod1 |= target->ctype.modifiers;
412 as1 |= target->ctype.as;
414 if (source->type == SYM_NODE) {
415 source = source->ctype.base_type;
416 if (!source)
417 return "bad types";
418 if (source->type == SYM_PTR) {
419 mod2 = 0;
420 as2 = 0;
422 mod2 |= source->ctype.modifiers;
423 as2 |= source->ctype.as;
426 if (target == source)
427 break;
428 if (!target || !source)
429 return "different types";
431 type1 = target->type;
432 base1 = target->ctype.base_type;
434 type2 = source->type;
435 base2 = source->ctype.base_type;
438 * Pointers to functions compare as the function itself
440 if (type1 == SYM_PTR && base1) {
441 switch (base1->type) {
442 case SYM_FN:
443 type1 = SYM_FN;
444 target = base1;
445 base1 = base1->ctype.base_type;
446 default:
447 /* nothing */;
450 if (type2 == SYM_PTR && base2) {
451 switch (base2->type) {
452 case SYM_FN:
453 type2 = SYM_FN;
454 source = base2;
455 base2 = base2->ctype.base_type;
456 default:
457 /* nothing */;
461 /* Arrays degenerate to pointers for type comparisons */
462 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
463 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
465 if (type1 != type2)
466 return "different base types";
468 /* Must be same address space to be comparable */
469 if (as1 != as2)
470 return "different address spaces";
472 /* Ignore differences in storage types, sign, or addressability */
473 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
474 if (diff) {
475 mod1 &= diff & ~target_mod_ignore;
476 mod2 &= diff & ~source_mod_ignore;
477 if (mod1 | mod2) {
478 if ((mod1 | mod2) & MOD_SIZE)
479 return "different type sizes";
480 return "different modifiers";
484 if (type1 == SYM_FN) {
485 int i;
486 struct symbol *arg1, *arg2;
487 if (base1->variadic != base2->variadic)
488 return "incompatible variadic arguments";
489 PREPARE_PTR_LIST(target->arguments, arg1);
490 PREPARE_PTR_LIST(source->arguments, arg2);
491 i = 1;
492 for (;;) {
493 const char *diff;
494 diff = type_difference(arg1, arg2, 0, 0);
495 if (diff) {
496 static char argdiff[80];
497 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
498 return argdiff;
500 if (!arg1)
501 break;
502 NEXT_PTR_LIST(arg1);
503 NEXT_PTR_LIST(arg2);
504 i++;
506 FINISH_PTR_LIST(arg2);
507 FINISH_PTR_LIST(arg1);
510 target = base1;
511 source = base2;
513 return NULL;
516 static int is_null_ptr(struct expression *expr)
518 if (expr->type != EXPR_VALUE || expr->value)
519 return 0;
520 if (!is_ptr_type(expr->ctype))
521 warn(expr->pos, "Using plain integer as NULL pointer");
522 return 1;
525 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
527 /* NULL expression? Just return the type of the "other side" */
528 if (is_null_ptr(r))
529 return l->ctype;
530 if (is_null_ptr(l))
531 return r->ctype;
532 return NULL;
536 * Ignore differences in "volatile" and "const"ness when
537 * subtracting pointers
539 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
541 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
543 const char *typediff;
544 struct symbol *ctype;
545 struct symbol *ltype, *rtype;
547 ltype = degenerate(l);
548 rtype = degenerate(r);
551 * If it is an integer subtract: the ptr add case will do the
552 * right thing.
554 if (!is_ptr_type(rtype))
555 return evaluate_ptr_add(expr, l, r);
557 ctype = ltype;
558 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
559 if (typediff) {
560 ctype = common_ptr_type(l, r);
561 if (!ctype) {
562 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
563 return NULL;
566 examine_symbol_type(ctype);
568 /* Figure out the base type we point to */
569 if (ctype->type == SYM_NODE)
570 ctype = ctype->ctype.base_type;
571 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
572 warn(expr->pos, "subtraction of functions? Share your drugs");
573 return NULL;
575 ctype = ctype->ctype.base_type;
577 expr->ctype = ssize_t_ctype;
578 if (ctype->bit_size > bits_in_char) {
579 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
580 struct expression *div = expr;
581 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
583 val->ctype = size_t_ctype;
584 val->value = ctype->bit_size >> 3;
586 sub->op = '-';
587 sub->ctype = ssize_t_ctype;
588 sub->left = l;
589 sub->right = r;
591 div->op = '/';
592 div->left = sub;
593 div->right = val;
596 return ssize_t_ctype;
599 static struct symbol *evaluate_sub(struct expression *expr)
601 struct expression *left = expr->left, *right = expr->right;
602 struct symbol *ltype = left->ctype;
604 if (is_ptr_type(ltype))
605 return evaluate_ptr_sub(expr, left, right);
607 return evaluate_arith(expr, 1);
610 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
612 static struct symbol *evaluate_conditional(struct expression **p)
614 struct symbol *ctype;
615 struct expression *expr = *p;
617 if (!expr)
618 return NULL;
620 if (expr->type == EXPR_ASSIGNMENT)
621 warn(expr->pos, "assignment expression in conditional");
623 ctype = evaluate_expression(expr);
624 if (ctype) {
625 if (is_safe_type(ctype))
626 warn(expr->pos, "testing a 'safe expression'");
627 if (is_float_type(ctype)) {
628 struct expression *comp;
630 * It's easier to handle here, rather than deal with
631 * FP all over the place. Floating point in boolean
632 * context is rare enough (and very often wrong),
633 * so price of explicit comparison with appropriate
634 * FP zero is not too high. And it simplifies things
635 * elsewhere.
637 comp = alloc_expression(expr->pos, EXPR_BINOP);
638 comp->op = SPECIAL_NOTEQUAL;
639 comp->left = expr;
640 comp->right = alloc_expression(expr->pos, EXPR_FVALUE);
641 comp->right->ctype = comp->left->ctype;
642 comp->right->fvalue = 0;
643 ctype = comp->ctype = &bool_ctype;
644 *p = comp;
648 return ctype;
651 static struct symbol *evaluate_logical(struct expression *expr)
653 if (!evaluate_conditional(&expr->left))
654 return NULL;
655 if (!evaluate_conditional(&expr->right))
656 return NULL;
658 expr->ctype = &bool_ctype;
659 return &bool_ctype;
662 static struct symbol *evaluate_shift(struct expression *expr)
664 struct expression *left = expr->left, *right = expr->right;
665 struct symbol *ltype = left->ctype, *rtype = right->ctype;
667 if (ltype->type == SYM_NODE)
668 ltype = ltype->ctype.base_type;
669 if (rtype->type == SYM_NODE)
670 rtype = rtype->ctype.base_type;
671 if (is_int_type(ltype) && is_int_type(rtype)) {
672 struct symbol *ctype = integer_promotion(ltype);
673 if (ltype->bit_size != ctype->bit_size)
674 expr->left = cast_to(expr->left, ctype);
675 expr->ctype = ctype;
676 ctype = integer_promotion(rtype);
677 if (rtype->bit_size != ctype->bit_size)
678 expr->right = cast_to(expr->right, ctype);
679 return expr->ctype;
681 return bad_expr_type(expr);
684 static struct symbol *evaluate_binop(struct expression *expr)
686 switch (expr->op) {
687 // addition can take ptr+int, fp and int
688 case '+':
689 return evaluate_add(expr);
691 // subtraction can take ptr-ptr, fp and int
692 case '-':
693 return evaluate_sub(expr);
695 // Arithmetic operations can take fp and int
696 case '*': case '/':
697 return evaluate_arith(expr, 1);
699 // shifts do integer promotions, but that's it.
700 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
701 return evaluate_shift(expr);
703 // The rest are integer operations
704 // '%', '&', '^', '|'
705 default:
706 return evaluate_arith(expr, 0);
710 static struct symbol *evaluate_comma(struct expression *expr)
712 expr->ctype = expr->right->ctype;
713 return expr->ctype;
716 static int modify_for_unsigned(int op)
718 if (op == '<')
719 op = SPECIAL_UNSIGNED_LT;
720 else if (op == '>')
721 op = SPECIAL_UNSIGNED_GT;
722 else if (op == SPECIAL_LTE)
723 op = SPECIAL_UNSIGNED_LTE;
724 else if (op == SPECIAL_GTE)
725 op = SPECIAL_UNSIGNED_GTE;
726 return op;
729 static struct symbol *evaluate_compare(struct expression *expr)
731 struct expression *left = expr->left, *right = expr->right;
732 struct symbol *ltype = left->ctype, *rtype = right->ctype;
733 struct symbol *ctype;
735 /* Type types? */
736 if (is_type_type(ltype) && is_type_type(rtype)) {
737 expr->ctype = &bool_ctype;
738 return &bool_ctype;
741 if (is_safe_type(ltype) || is_safe_type(rtype))
742 warn(expr->pos, "testing a 'safe expression'");
744 /* Pointer types? */
745 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
746 expr->ctype = &bool_ctype;
747 // FIXME! Check the types for compatibility
748 return &bool_ctype;
751 ctype = compatible_integer_binop(&expr->left, &expr->right);
752 if (ctype) {
753 if (ctype->ctype.modifiers & MOD_UNSIGNED)
754 expr->op = modify_for_unsigned(expr->op);
755 expr->ctype = &bool_ctype;
756 return &bool_ctype;
758 ctype = compatible_float_binop(&expr->left, &expr->right);
759 if (ctype) {
760 expr->ctype = &bool_ctype;
761 return &bool_ctype;
764 return bad_expr_type(expr);
768 * FIXME!! This should do casts, array degeneration etc..
770 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
772 struct symbol *ltype = left->ctype, *rtype = right->ctype;
774 if (ltype->type == SYM_NODE)
775 ltype = ltype->ctype.base_type;
777 if (rtype->type == SYM_NODE)
778 rtype = rtype->ctype.base_type;
780 if (ltype->type == SYM_PTR) {
781 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
782 return ltype;
785 if (rtype->type == SYM_PTR) {
786 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
787 return rtype;
789 return NULL;
792 static struct symbol * evaluate_conditional_expression(struct expression *expr)
794 struct expression *cond, *true, *false;
795 struct symbol *ctype, *ltype, *rtype;
796 const char * typediff;
798 ctype = degenerate(expr->conditional);
799 cond = expr->conditional;
801 ltype = ctype;
802 true = cond;
803 if (expr->cond_true) {
804 ltype = degenerate(expr->cond_true);
805 true = expr->cond_true;
808 rtype = degenerate(expr->cond_false);
809 false = expr->cond_false;
811 ctype = ltype;
812 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
813 if (!typediff)
814 goto out;
816 ctype = compatible_integer_binop(&true, &expr->cond_false);
817 if (ctype)
818 goto out;
819 ctype = compatible_ptr_type(true, expr->cond_false);
820 if (ctype)
821 goto out;
822 ctype = compatible_float_binop(&true, &expr->cond_false);
823 if (ctype)
824 goto out;
825 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
826 return NULL;
828 out:
829 expr->ctype = ctype;
830 return ctype;
833 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
834 struct expression **rp, struct symbol *source, const char *where)
836 const char *typediff;
837 struct symbol *t;
838 int target_as;
840 /* It's ok if the target is more volatile or const than the source */
841 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
842 if (!typediff)
843 return 1;
845 if (is_int_type(target)) {
846 if (is_int_type(source)) {
847 if (target->bit_size != source->bit_size)
848 goto Cast;
849 return 1;
851 if (is_float_type(source))
852 goto Cast;
853 } else if (is_float_type(target)) {
854 if (is_int_type(source))
855 goto Cast;
856 if (is_float_type(source)) {
857 if (target->bit_size != source->bit_size)
858 goto Cast;
859 return 1;
863 /* Pointer destination? */
864 t = target;
865 target_as = t->ctype.as;
866 if (t->type == SYM_NODE) {
867 t = t->ctype.base_type;
868 target_as |= t->ctype.as;
870 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
871 struct expression *right = *rp;
872 struct symbol *s = source;
873 int source_as;
875 // NULL pointer is always ok
876 if (is_null_ptr(right))
877 return 1;
879 /* "void *" matches anything as long as the address space is ok */
880 source_as = s->ctype.as;
881 if (s->type == SYM_NODE) {
882 s = s->ctype.base_type;
883 source_as |= s->ctype.as;
885 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
886 s = s->ctype.base_type;
887 t = t->ctype.base_type;
888 if (s == &void_ctype || t == &void_ctype)
889 return 1;
893 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
894 info(expr->pos, " expected %s", show_typename(target));
895 info(expr->pos, " got %s", show_typename(source));
896 *rp = cast_to(*rp, target);
897 return 0;
898 Cast:
899 *rp = cast_to(*rp, target);
900 return 1;
904 * FIXME!! This is wrong from a double evaluation standpoint. We can't
905 * just expand the expression twice, that would make any side effects
906 * happen twice too.
908 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
910 int op = expr->op;
911 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
912 static const int op_trans[] = {
913 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
914 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
915 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
916 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
917 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
918 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
919 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
920 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
921 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
922 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
924 struct expression *e0, *e1, *e2, *e3, *e4, *e5;
925 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
926 struct symbol *ltype = left->ctype;
927 struct expression *addr;
928 struct symbol *lptype;
930 if (left->type == EXPR_BITFIELD)
931 addr = left->address;
932 else
933 addr = left->unop;
935 lptype = addr->ctype;
937 a->ctype.base_type = lptype;
938 a->bit_size = lptype->bit_size;
939 a->array_size = lptype->array_size;
941 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
942 e0->symbol = a;
943 e0->ctype = &lazy_ptr_ctype;
945 e1 = alloc_expression(expr->pos, EXPR_PREOP);
946 e1->unop = e0;
947 e1->op = '*';
948 e1->ctype = lptype;
950 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
951 e2->left = e1;
952 e2->right = addr;
953 e2->op = '=';
954 e2->ctype = lptype;
956 /* we can't cannibalize left, unfortunately */
957 e3 = alloc_expression(expr->pos, left->type);
958 *e3 = *left;
959 if (e3->type == EXPR_BITFIELD)
960 e3->address = e1;
961 else
962 e3->unop = e1;
964 e4 = alloc_expression(expr->pos, EXPR_BINOP);
965 e4->op = subexpr->op = op_trans[op - SPECIAL_BASE];
966 e4->left = e3;
967 e4->right = right;
968 /* will calculate type later */
970 e5 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
971 e5->left = e3; /* we can share that one */
972 e5->right = e4;
973 e5->op = '=';
974 e5->ctype = ltype;
976 expr->type = EXPR_COMMA;
977 expr->left = e2;
978 expr->right = e5;
979 expr->ctype = ltype;
981 return evaluate_binop(e4);
984 static void evaluate_assign_to(struct expression *left, struct symbol *type)
986 if (type->ctype.modifiers & MOD_CONST)
987 warn(left->pos, "assignment to const expression");
988 if (type->type == SYM_NODE)
989 type->ctype.modifiers |= MOD_ASSIGNED;
992 static struct symbol *evaluate_assignment(struct expression *expr)
994 struct expression *left = expr->left, *right = expr->right;
995 struct expression *where = expr;
996 struct symbol *ltype, *rtype;
998 if (!lvalue_expression(left)) {
999 warn(expr->pos, "not an lvalue");
1000 return NULL;
1003 ltype = left->ctype;
1005 if (expr->op != '=') {
1006 if (!evaluate_binop_assignment(expr, left, right))
1007 return NULL;
1008 where = expr->right; /* expr is EXPR_COMMA now */
1009 left = where->left;
1010 right = where->right;
1013 rtype = degenerate(right);
1015 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment"))
1016 return NULL;
1018 evaluate_assign_to(left, ltype);
1020 expr->ctype = ltype;
1021 return ltype;
1024 static void examine_fn_arguments(struct symbol *fn)
1026 struct symbol *s;
1028 FOR_EACH_PTR(fn->arguments, s) {
1029 struct symbol *arg = evaluate_symbol(s);
1030 /* Array/function arguments silently degenerate into pointers */
1031 if (arg) {
1032 struct symbol *ptr;
1033 switch(arg->type) {
1034 case SYM_ARRAY:
1035 case SYM_FN:
1036 ptr = alloc_symbol(s->pos, SYM_PTR);
1037 if (arg->type == SYM_ARRAY)
1038 ptr->ctype = arg->ctype;
1039 else
1040 ptr->ctype.base_type = arg;
1041 ptr->ctype.as |= s->ctype.as;
1042 ptr->ctype.modifiers |= s->ctype.modifiers;
1044 s->ctype.base_type = ptr;
1045 s->ctype.as = 0;
1046 s->ctype.modifiers = 0;
1047 examine_symbol_type(s);
1048 break;
1049 default:
1050 /* nothing */
1051 break;
1054 } END_FOR_EACH_PTR;
1057 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1059 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1060 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1061 *newsym = *sym;
1062 newsym->ctype.as = as;
1063 newsym->ctype.modifiers = mod;
1064 sym = newsym;
1066 return sym;
1069 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1071 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1072 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1074 node->ctype.base_type = ptr;
1075 ptr->bit_size = bits_in_pointer;
1076 ptr->ctype.alignment = pointer_alignment;
1078 node->bit_size = bits_in_pointer;
1079 node->ctype.alignment = pointer_alignment;
1081 access_symbol(sym);
1082 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1083 if (sym->ctype.modifiers & MOD_REGISTER) {
1084 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1085 sym->ctype.modifiers &= ~MOD_REGISTER;
1087 if (sym->type == SYM_NODE) {
1088 ptr->ctype.as |= sym->ctype.as;
1089 ptr->ctype.modifiers |= sym->ctype.modifiers;
1090 sym = sym->ctype.base_type;
1092 if (degenerate && sym->type == SYM_ARRAY) {
1093 ptr->ctype.as |= sym->ctype.as;
1094 ptr->ctype.modifiers |= sym->ctype.modifiers;
1095 sym = sym->ctype.base_type;
1097 ptr->ctype.base_type = sym;
1099 return node;
1102 /* Arrays degenerate into pointers on pointer arithmetic */
1103 static struct symbol *degenerate(struct expression *expr)
1105 struct symbol *ctype, *base;
1107 if (!expr)
1108 return NULL;
1109 ctype = expr->ctype;
1110 if (!ctype)
1111 return NULL;
1112 base = ctype;
1113 if (ctype->type == SYM_NODE)
1114 base = ctype->ctype.base_type;
1116 * Arrays degenerate into pointers to the entries, while
1117 * functions degenerate into pointers to themselves.
1118 * If array was part of non-lvalue compound, we create a copy
1119 * of that compound first and then act as if we were dealing with
1120 * the corresponding field in there.
1122 switch (base->type) {
1123 case SYM_ARRAY:
1124 if (expr->type == EXPR_SLICE) {
1125 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1126 struct expression *e0, *e1, *e2, *e3, *e4;
1128 a->ctype.base_type = expr->base->ctype;
1129 a->bit_size = expr->base->ctype->bit_size;
1130 a->array_size = expr->base->ctype->array_size;
1132 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1133 e0->symbol = a;
1134 e0->ctype = &lazy_ptr_ctype;
1136 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1137 e1->unop = e0;
1138 e1->op = '*';
1139 e1->ctype = expr->base->ctype; /* XXX */
1141 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1142 e2->left = e1;
1143 e2->right = expr->base;
1144 e2->op = '=';
1145 e2->ctype = expr->base->ctype;
1147 if (expr->r_bitpos) {
1148 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1149 e3->op = '+';
1150 e3->left = e0;
1151 e3->right = alloc_const_expression(expr->pos,
1152 expr->r_bitpos >> 3);
1153 e3->ctype = &lazy_ptr_ctype;
1154 } else {
1155 e3 = e0;
1158 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1159 e4->left = e2;
1160 e4->right = e3;
1161 e4->ctype = &lazy_ptr_ctype;
1163 expr->unop = e4;
1164 expr->type = EXPR_PREOP;
1165 expr->op = '*';
1167 case SYM_FN:
1168 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1169 warn(expr->pos, "strange non-value function or array");
1170 return NULL;
1172 *expr = *expr->unop;
1173 ctype = create_pointer(expr, ctype, 1);
1174 expr->ctype = ctype;
1175 default:
1176 /* nothing */;
1178 return ctype;
1181 static struct symbol *evaluate_addressof(struct expression *expr)
1183 struct expression *op = expr->unop;
1184 struct symbol *ctype;
1186 if (op->op != '*' || op->type != EXPR_PREOP) {
1187 warn(expr->pos, "not addressable");
1188 return NULL;
1190 ctype = op->ctype;
1191 *expr = *op->unop;
1194 * symbol expression evaluation is lazy about the type
1195 * of the sub-expression, so we may have to generate
1196 * the type here if so..
1198 if (expr->ctype == &lazy_ptr_ctype) {
1199 ctype = create_pointer(expr, ctype, 0);
1200 expr->ctype = ctype;
1202 return expr->ctype;
1206 static struct symbol *evaluate_dereference(struct expression *expr)
1208 struct expression *op = expr->unop;
1209 struct symbol *ctype = op->ctype, *node, *target;
1211 /* Simplify: *&(expr) => (expr) */
1212 if (op->type == EXPR_PREOP && op->op == '&') {
1213 *expr = *op->unop;
1214 return expr->ctype;
1217 /* Dereferencing a node drops all the node information. */
1218 if (ctype->type == SYM_NODE)
1219 ctype = ctype->ctype.base_type;
1221 node = alloc_symbol(expr->pos, SYM_NODE);
1222 target = ctype->ctype.base_type;
1224 switch (ctype->type) {
1225 default:
1226 warn(expr->pos, "cannot derefence this type");
1227 return NULL;
1228 case SYM_PTR:
1229 merge_type(node, ctype);
1230 if (ctype->type != SYM_ARRAY)
1231 break;
1233 * Dereferencing a pointer to an array results in a
1234 * degenerate dereference: the expression becomes
1235 * just a pointer to the entry, and the derefence
1236 * goes away.
1238 *expr = *op;
1240 target = alloc_symbol(expr->pos, SYM_PTR);
1241 target->bit_size = bits_in_pointer;
1242 target->ctype.alignment = pointer_alignment;
1243 merge_type(target, ctype->ctype.base_type);
1244 break;
1246 case SYM_ARRAY:
1248 * When an array is dereferenced, we need to pick
1249 * up the attributes of the original node too..
1251 merge_type(node, op->ctype);
1252 merge_type(node, ctype);
1253 break;
1256 node->bit_size = target->bit_size;
1257 node->array_size = target->array_size;
1259 expr->ctype = node;
1260 return node;
1264 * Unary post-ops: x++ and x--
1266 static struct symbol *evaluate_postop(struct expression *expr)
1268 struct expression *op = expr->unop;
1269 struct symbol *ctype = op->ctype;
1271 if (!lvalue_expression(expr->unop)) {
1272 warn(expr->pos, "need lvalue expression for ++/--");
1273 return NULL;
1276 evaluate_assign_to(op, ctype);
1278 expr->ctype = ctype;
1279 return ctype;
1282 static struct symbol *evaluate_sign(struct expression *expr)
1284 struct symbol *ctype = expr->unop->ctype;
1285 if (is_int_type(ctype)) {
1286 struct symbol *rtype = rtype = integer_promotion(ctype);
1287 if (rtype->bit_size != ctype->bit_size)
1288 expr->unop = cast_to(expr->unop, rtype);
1289 ctype = rtype;
1290 } else if (is_float_type(ctype) && expr->op != '%') {
1291 /* no conversions needed */
1292 } else {
1293 return bad_expr_type(expr);
1295 if (expr->op == '+')
1296 *expr = *expr->unop;
1297 expr->ctype = ctype;
1298 return ctype;
1301 static struct symbol *evaluate_preop(struct expression *expr)
1303 struct symbol *ctype = expr->unop->ctype;
1305 switch (expr->op) {
1306 case '(':
1307 *expr = *expr->unop;
1308 return ctype;
1310 case '+':
1311 case '-':
1312 case '~':
1313 return evaluate_sign(expr);
1315 case '*':
1316 return evaluate_dereference(expr);
1318 case '&':
1319 return evaluate_addressof(expr);
1321 case SPECIAL_INCREMENT:
1322 case SPECIAL_DECREMENT:
1324 * From a type evaluation standpoint the pre-ops are
1325 * the same as the postops
1327 return evaluate_postop(expr);
1329 case '!':
1330 if (is_safe_type(ctype))
1331 warn(expr->pos, "testing a 'safe expression'");
1332 if (is_float_type(ctype)) {
1333 struct expression *arg = expr->unop;
1334 expr->type = EXPR_BINOP;
1335 expr->op = SPECIAL_EQUAL;
1336 expr->left = arg;
1337 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1338 expr->right->ctype = ctype;
1339 expr->right->fvalue = 0;
1341 ctype = &bool_ctype;
1342 break;
1344 default:
1345 break;
1347 expr->ctype = ctype;
1348 return &bool_ctype;
1351 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1353 struct ptr_list *head = (struct ptr_list *)_list;
1354 struct ptr_list *list = head;
1356 if (!head)
1357 return NULL;
1358 do {
1359 int i;
1360 for (i = 0; i < list->nr; i++) {
1361 struct symbol *sym = (struct symbol *) list->list[i];
1362 if (sym->ident) {
1363 if (sym->ident != ident)
1364 continue;
1365 *offset = sym->offset;
1366 return sym;
1367 } else {
1368 struct symbol *ctype = sym->ctype.base_type;
1369 struct symbol *sub;
1370 if (!ctype)
1371 continue;
1372 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1373 continue;
1374 sub = find_identifier(ident, ctype->symbol_list, offset);
1375 if (!sub)
1376 continue;
1377 *offset += sym->offset;
1378 return sub;
1381 } while ((list = list->next) != head);
1382 return NULL;
1385 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1387 struct expression *add;
1390 * Create a new add-expression
1392 * NOTE! Even if we just add zero, we need a new node
1393 * for the member pointer, since it has a different
1394 * type than the original pointer. We could make that
1395 * be just a cast, but the fact is, a node is a node,
1396 * so we might as well just do the "add zero" here.
1398 add = alloc_expression(expr->pos, EXPR_BINOP);
1399 add->op = '+';
1400 add->left = expr;
1401 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1402 add->right->ctype = &int_ctype;
1403 add->right->value = offset;
1406 * The ctype of the pointer will be lazily evaluated if
1407 * we ever take the address of this member dereference..
1409 add->ctype = &lazy_ptr_ctype;
1410 return add;
1413 /* structure/union dereference */
1414 static struct symbol *evaluate_member_dereference(struct expression *expr)
1416 int offset;
1417 struct symbol *ctype, *member;
1418 struct expression *deref = expr->deref, *add;
1419 struct ident *ident = expr->member;
1420 unsigned int mod;
1421 int address_space;
1423 if (!evaluate_expression(deref))
1424 return NULL;
1425 if (!ident) {
1426 warn(expr->pos, "bad member name");
1427 return NULL;
1430 ctype = deref->ctype;
1431 address_space = ctype->ctype.as;
1432 mod = ctype->ctype.modifiers;
1433 if (ctype->type == SYM_NODE) {
1434 ctype = ctype->ctype.base_type;
1435 address_space |= ctype->ctype.as;
1436 mod |= ctype->ctype.modifiers;
1438 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1439 warn(expr->pos, "expected structure or union");
1440 return NULL;
1442 offset = 0;
1443 member = find_identifier(ident, ctype->symbol_list, &offset);
1444 if (!member) {
1445 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1446 const char *name = "<unnamed>";
1447 int namelen = 9;
1448 if (ctype->ident) {
1449 name = ctype->ident->name;
1450 namelen = ctype->ident->len;
1452 warn(expr->pos, "no member '%s' in %s %.*s",
1453 show_ident(ident), type, namelen, name);
1454 return NULL;
1458 * The member needs to take on the address space and modifiers of
1459 * the "parent" type.
1461 member = convert_to_as_mod(member, address_space, mod);
1462 ctype = member->ctype.base_type;
1464 if (!lvalue_expression(deref)) {
1465 if (deref->type != EXPR_SLICE) {
1466 expr->base = deref;
1467 expr->r_bitpos = 0;
1468 } else {
1469 expr->base = deref->base;
1470 expr->r_bitpos = deref->r_bitpos;
1472 expr->r_bitpos += offset << 3;
1473 expr->type = EXPR_SLICE;
1474 if (ctype->type == SYM_BITFIELD) {
1475 expr->r_bitpos += member->bit_offset;
1476 expr->r_nrbits = member->fieldwidth;
1477 } else {
1478 expr->r_nrbits = member->bit_size;
1480 expr->ctype = member;
1481 return member;
1484 deref = deref->unop;
1485 expr->deref = deref;
1487 add = evaluate_offset(deref, offset);
1488 if (ctype->type == SYM_BITFIELD) {
1489 expr->type = EXPR_BITFIELD;
1490 expr->bitpos = member->bit_offset;
1491 expr->nrbits = member->fieldwidth;
1492 expr->address = add;
1493 } else {
1494 expr->type = EXPR_PREOP;
1495 expr->op = '*';
1496 expr->unop = add;
1499 expr->ctype = member;
1500 return member;
1503 static struct symbol *evaluate_cast(struct expression *);
1505 static struct symbol *evaluate_sizeof(struct expression *expr)
1507 int size;
1509 if (expr->cast_type) {
1510 if (expr->cast_expression) {
1511 struct symbol *sym = evaluate_cast(expr);
1512 size = sym->bit_size;
1513 } else {
1514 examine_symbol_type(expr->cast_type);
1515 size = expr->cast_type->bit_size;
1517 } else {
1518 if (!evaluate_expression(expr->cast_expression))
1519 return NULL;
1520 size = expr->cast_expression->ctype->bit_size;
1522 if (size & 7)
1523 warn(expr->pos, "cannot size expression");
1524 expr->type = EXPR_VALUE;
1525 expr->value = size >> 3;
1526 expr->ctype = size_t_ctype;
1527 return size_t_ctype;
1530 static struct symbol *evaluate_alignof(struct expression *expr)
1532 struct symbol *type = expr->cast_type;
1534 if (!type) {
1535 type = evaluate_expression(expr->cast_expression);
1536 if (!type)
1537 return NULL;
1539 examine_symbol_type(type);
1540 expr->type = EXPR_VALUE;
1541 expr->value = type->ctype.alignment;
1542 expr->ctype = size_t_ctype;
1543 return size_t_ctype;
1546 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1548 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1549 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1550 return clash != 0;
1553 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1555 struct expression *expr;
1556 struct symbol_list *argument_types = fn->arguments;
1557 struct symbol *argtype;
1558 int i = 1;
1560 PREPARE_PTR_LIST(argument_types, argtype);
1561 FOR_EACH_PTR (head, expr) {
1562 struct expression **p = THIS_ADDRESS(expr);
1563 struct symbol *ctype, *target;
1564 ctype = evaluate_expression(expr);
1566 if (!ctype)
1567 return 0;
1569 if (context_clash(f, ctype))
1570 warn(expr->pos, "argument %d used in wrong context", i);
1572 ctype = degenerate(expr);
1574 target = argtype;
1575 if (!target && ctype->bit_size < bits_in_int)
1576 target = &int_ctype;
1577 if (target) {
1578 static char where[30];
1579 examine_symbol_type(target);
1580 sprintf(where, "argument %d", i);
1581 compatible_assignment_types(expr, target, p, ctype, where);
1584 i++;
1585 NEXT_PTR_LIST(argtype);
1586 } END_FOR_EACH_PTR;
1587 FINISH_PTR_LIST(argtype);
1588 return 1;
1591 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1592 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1594 struct expression *entry;
1595 int current = 0;
1596 int max = 0;
1597 int accept_string = is_byte_type(ctype);
1599 FOR_EACH_PTR(expr->expr_list, entry) {
1600 struct expression **p = THIS_ADDRESS(entry);
1601 struct symbol *sym;
1602 int entries;
1604 if (entry->type == EXPR_INDEX) {
1605 current = entry->idx_to;
1606 continue;
1608 if (accept_string && entry->type == EXPR_STRING) {
1609 sym = evaluate_expression(entry);
1610 entries = get_expression_value(sym->array_size);
1611 } else {
1612 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1613 entries = 1;
1615 current += entries;
1616 if (current > max)
1617 max = current;
1618 } END_FOR_EACH_PTR;
1619 return max;
1622 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1623 static int evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1625 if (offset || expression_list_size(expr->expr_list) != 1) {
1626 warn(expr->pos, "unexpected compound initializer");
1627 return 0;
1629 return evaluate_array_initializer(ctype, expr, 0);
1632 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1634 struct expression *entry;
1635 struct symbol *sym;
1637 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1638 FOR_EACH_PTR(expr->expr_list, entry) {
1639 struct expression **p = THIS_ADDRESS(entry);
1641 if (entry->type == EXPR_IDENTIFIER) {
1642 struct ident *ident = entry->expr_ident;
1643 /* We special-case the "already right place" case */
1644 if (sym && sym->ident == ident)
1645 continue;
1646 RESET_PTR_LIST(sym);
1647 for (;;) {
1648 if (!sym) {
1649 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1650 return 0;
1652 if (sym->ident == ident)
1653 break;
1654 NEXT_PTR_LIST(sym);
1656 continue;
1659 if (!sym) {
1660 warn(expr->pos, "too many initializers for struct/union");
1661 return 0;
1664 evaluate_initializer(sym, p, offset + sym->offset);
1666 NEXT_PTR_LIST(sym);
1667 } END_FOR_EACH_PTR;
1668 FINISH_PTR_LIST(sym);
1670 return 0;
1674 * Initializers are kind of like assignments. Except
1675 * they can be a hell of a lot more complex.
1677 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1679 struct expression *expr = *ep;
1682 * Simple non-structure/array initializers are the simple
1683 * case, and look (and parse) largely like assignments.
1685 if (expr->type != EXPR_INITIALIZER) {
1686 int size = 0, is_string = expr->type == EXPR_STRING;
1687 struct symbol *rtype = evaluate_expression(expr);
1688 if (rtype) {
1689 struct expression *pos;
1692 * Special case:
1693 * char array[] = "string"
1694 * should _not_ degenerate.
1696 if (is_string && is_string_type(ctype)) {
1697 struct expression *array_size = ctype->array_size;
1698 if (!array_size)
1699 array_size = ctype->array_size = rtype->array_size;
1700 size = get_expression_value(array_size);
1701 } else {
1702 rtype = degenerate(expr);
1703 size = 1;
1705 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1708 * Don't bother creating a position expression for
1709 * the simple initializer cases that don't need it.
1711 * We need a position if the initializer has a byte
1712 * offset, _or_ if we're initializing a bitfield.
1714 if (offset || ctype->fieldwidth) {
1715 pos = alloc_expression(expr->pos, EXPR_POS);
1716 pos->init_offset = offset;
1717 pos->init_sym = ctype;
1718 pos->init_expr = *ep;
1719 pos->ctype = expr->ctype;
1720 *ep = pos;
1723 return size;
1726 expr->ctype = ctype;
1727 if (ctype->type == SYM_NODE)
1728 ctype = ctype->ctype.base_type;
1730 switch (ctype->type) {
1731 case SYM_ARRAY:
1732 case SYM_PTR:
1733 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1734 case SYM_UNION:
1735 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1736 case SYM_STRUCT:
1737 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1738 default:
1739 return evaluate_scalar_initializer(ctype, expr, offset);
1743 static int get_as(struct symbol *sym)
1745 int as;
1746 unsigned long mod;
1748 if (!sym)
1749 return 0;
1750 as = sym->ctype.as;
1751 mod = sym->ctype.modifiers;
1752 if (sym->type == SYM_NODE) {
1753 sym = sym->ctype.base_type;
1754 as |= sym->ctype.as;
1755 mod |= sym->ctype.modifiers;
1759 * At least for now, allow casting to a "unsigned long".
1760 * That's how we do things like pointer arithmetic and
1761 * store pointers to registers.
1763 if (sym == &ulong_ctype)
1764 return -1;
1766 if (sym && sym->type == SYM_PTR) {
1767 sym = sym->ctype.base_type;
1768 as |= sym->ctype.as;
1769 mod |= sym->ctype.modifiers;
1771 if (mod & MOD_FORCE)
1772 return -1;
1773 return as;
1776 static struct symbol *evaluate_cast(struct expression *expr)
1778 struct expression *target = expr->cast_expression;
1779 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1780 enum type type;
1782 expr->ctype = ctype;
1783 expr->cast_type = ctype;
1786 * Special case: a cast can be followed by an
1787 * initializer, in which case we need to pass
1788 * the type value down to that initializer rather
1789 * than trying to evaluate it as an expression
1791 * A more complex case is when the initializer is
1792 * dereferenced as part of a post-fix expression.
1793 * We need to produce an expression that can be dereferenced.
1795 if (target->type == EXPR_INITIALIZER) {
1796 struct symbol *sym = expr->cast_type;
1797 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1799 sym->initializer = expr->cast_expression;
1800 evaluate_symbol(sym);
1802 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
1803 addr->symbol = sym;
1805 expr->type = EXPR_PREOP;
1806 expr->op = '*';
1807 expr->unop = addr;
1808 expr->ctype = sym;
1810 return sym;
1813 evaluate_expression(target);
1814 degenerate(target);
1817 * You can always throw a value away by casting to
1818 * "void" - that's an implicit "force". Note that
1819 * the same is _not_ true of "void *".
1821 if (ctype == &void_ctype)
1822 goto out;
1824 type = ctype->type;
1825 if (type == SYM_NODE) {
1826 type = ctype->ctype.base_type->type;
1827 if (ctype->ctype.base_type == &void_ctype)
1828 goto out;
1830 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
1831 warn(expr->pos, "cast to non-scalar");
1833 if (!target->ctype) {
1834 warn(expr->pos, "cast from unknown type");
1835 goto out;
1838 type = target->ctype->type;
1839 if (type == SYM_NODE)
1840 type = target->ctype->ctype.base_type->type;
1841 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
1842 warn(expr->pos, "cast from non-scalar");
1844 if (!get_as(ctype) && get_as(target->ctype) > 0)
1845 warn(expr->pos, "cast removes address space of expression");
1848 * Casts of constant values are special: they
1849 * can be NULL, and thus need to be simplified
1850 * early.
1852 if (target->type == EXPR_VALUE)
1853 cast_value(expr, ctype, target, target->ctype);
1855 out:
1856 return ctype;
1860 * Evaluate a call expression with a symbol. This
1861 * should expand inline functions, and evaluate
1862 * builtins.
1864 static int evaluate_symbol_call(struct expression *expr)
1866 struct expression *fn = expr->fn;
1867 struct symbol *ctype = fn->ctype;
1869 if (fn->type != EXPR_PREOP)
1870 return 0;
1872 if (ctype->op && ctype->op->evaluate)
1873 return ctype->op->evaluate(expr);
1875 if (ctype->ctype.modifiers & MOD_INLINE) {
1876 int ret;
1877 struct symbol *curr = current_fn;
1878 unsigned long context = current_context;
1879 unsigned long mask = current_contextmask;
1881 current_context |= ctype->ctype.context;
1882 current_contextmask |= ctype->ctype.contextmask;
1883 current_fn = ctype->ctype.base_type;
1884 examine_fn_arguments(current_fn);
1886 ret = inline_function(expr, ctype);
1888 /* restore the old function context */
1889 current_fn = curr;
1890 current_context = context;
1891 current_contextmask = mask;
1892 return ret;
1895 return 0;
1898 static struct symbol *evaluate_call(struct expression *expr)
1900 int args, fnargs;
1901 struct symbol *ctype, *sym;
1902 struct expression *fn = expr->fn;
1903 struct expression_list *arglist = expr->args;
1905 if (!evaluate_expression(fn))
1906 return NULL;
1907 sym = ctype = fn->ctype;
1908 if (ctype->type == SYM_NODE)
1909 ctype = ctype->ctype.base_type;
1910 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1911 ctype = ctype->ctype.base_type;
1912 if (!evaluate_arguments(sym, ctype, arglist))
1913 return NULL;
1914 if (ctype->type != SYM_FN) {
1915 warn(expr->pos, "not a function %s", show_ident(sym->ident));
1916 return NULL;
1918 args = expression_list_size(expr->args);
1919 fnargs = symbol_list_size(ctype->arguments);
1920 if (args < fnargs)
1921 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1922 if (args > fnargs && !ctype->variadic)
1923 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1924 if (sym->type == SYM_NODE) {
1925 if (evaluate_symbol_call(expr))
1926 return expr->ctype;
1928 expr->ctype = ctype->ctype.base_type;
1929 return expr->ctype;
1932 struct symbol *evaluate_expression(struct expression *expr)
1934 if (!expr)
1935 return NULL;
1936 if (expr->ctype)
1937 return expr->ctype;
1939 switch (expr->type) {
1940 case EXPR_VALUE:
1941 case EXPR_FVALUE:
1942 warn(expr->pos, "value expression without a type");
1943 return NULL;
1944 case EXPR_STRING:
1945 return evaluate_string(expr);
1946 case EXPR_SYMBOL:
1947 return evaluate_symbol_expression(expr);
1948 case EXPR_BINOP:
1949 if (!evaluate_expression(expr->left))
1950 return NULL;
1951 if (!evaluate_expression(expr->right))
1952 return NULL;
1953 return evaluate_binop(expr);
1954 case EXPR_LOGICAL:
1955 return evaluate_logical(expr);
1956 case EXPR_COMMA:
1957 evaluate_expression(expr->left);
1958 if (!evaluate_expression(expr->right))
1959 return NULL;
1960 return evaluate_comma(expr);
1961 case EXPR_COMPARE:
1962 if (!evaluate_expression(expr->left))
1963 return NULL;
1964 if (!evaluate_expression(expr->right))
1965 return NULL;
1966 return evaluate_compare(expr);
1967 case EXPR_ASSIGNMENT:
1968 if (!evaluate_expression(expr->left))
1969 return NULL;
1970 if (!evaluate_expression(expr->right))
1971 return NULL;
1972 return evaluate_assignment(expr);
1973 case EXPR_PREOP:
1974 if (!evaluate_expression(expr->unop))
1975 return NULL;
1976 return evaluate_preop(expr);
1977 case EXPR_POSTOP:
1978 if (!evaluate_expression(expr->unop))
1979 return NULL;
1980 return evaluate_postop(expr);
1981 case EXPR_CAST:
1982 return evaluate_cast(expr);
1983 case EXPR_SIZEOF:
1984 return evaluate_sizeof(expr);
1985 case EXPR_ALIGNOF:
1986 return evaluate_alignof(expr);
1987 case EXPR_DEREF:
1988 return evaluate_member_dereference(expr);
1989 case EXPR_CALL:
1990 return evaluate_call(expr);
1991 case EXPR_BITFIELD:
1992 warn(expr->pos, "bitfield generated by parser");
1993 return NULL;
1994 case EXPR_SELECT:
1995 case EXPR_CONDITIONAL:
1996 if (!evaluate_conditional(&expr->conditional))
1997 return NULL;
1998 if (!evaluate_expression(expr->cond_false))
1999 return NULL;
2000 if (expr->cond_true && !evaluate_expression(expr->cond_true))
2001 return NULL;
2002 return evaluate_conditional_expression(expr);
2003 case EXPR_STATEMENT:
2004 expr->ctype = evaluate_statement(expr->statement);
2005 return expr->ctype;
2007 case EXPR_LABEL:
2008 expr->ctype = &ptr_ctype;
2009 return &ptr_ctype;
2011 case EXPR_TYPE:
2012 /* Evaluate the type of the symbol .. */
2013 evaluate_symbol(expr->symbol);
2014 /* .. but the type of the _expression_ is a "type" */
2015 expr->ctype = &type_ctype;
2016 return &type_ctype;
2018 /* These can not exist as stand-alone expressions */
2019 case EXPR_INITIALIZER:
2020 case EXPR_IDENTIFIER:
2021 case EXPR_INDEX:
2022 case EXPR_POS:
2023 warn(expr->pos, "internal front-end error: initializer in expression");
2024 return NULL;
2025 case EXPR_SLICE:
2026 warn(expr->pos, "internal front-end error: SLICE re-evaluated");
2027 return NULL;
2029 return NULL;
2032 void check_duplicates(struct symbol *sym)
2034 struct symbol *next = sym;
2036 while ((next = next->same_symbol) != NULL) {
2037 const char *typediff;
2038 evaluate_symbol(next);
2039 typediff = type_difference(sym, next, 0, 0);
2040 if (typediff) {
2041 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2042 show_ident(sym->ident),
2043 input_streams[next->pos.stream].name, next->pos.line, typediff);
2044 return;
2049 struct symbol *evaluate_symbol(struct symbol *sym)
2051 struct symbol *base_type;
2053 if (!sym)
2054 return sym;
2056 sym = examine_symbol_type(sym);
2057 base_type = sym->ctype.base_type;
2058 if (!base_type)
2059 return NULL;
2061 /* Evaluate the initializers */
2062 if (sym->initializer) {
2063 int count = evaluate_initializer(sym, &sym->initializer, 0);
2064 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
2065 int bit_size = count * base_type->ctype.base_type->bit_size;
2066 base_type->array_size = alloc_const_expression(sym->pos, count);
2067 base_type->bit_size = bit_size;
2068 sym->array_size = base_type->array_size;
2069 sym->bit_size = bit_size;
2073 /* And finally, evaluate the body of the symbol too */
2074 if (base_type->type == SYM_FN) {
2075 examine_fn_arguments(base_type);
2076 if (base_type->stmt) {
2077 current_fn = base_type;
2078 current_contextmask = sym->ctype.contextmask;
2079 current_context = sym->ctype.context;
2080 evaluate_statement(base_type->stmt);
2084 return base_type;
2087 struct symbol *evaluate_return_expression(struct statement *stmt)
2089 struct expression *expr = stmt->expression;
2090 struct symbol *ctype, *fntype;
2092 evaluate_expression(expr);
2093 ctype = degenerate(expr);
2094 fntype = current_fn->ctype.base_type;
2095 if (!fntype || fntype == &void_ctype) {
2096 if (expr && ctype != &void_ctype)
2097 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2098 return NULL;
2101 if (!expr) {
2102 warn(stmt->pos, "return with no return value");
2103 return NULL;
2105 if (!ctype)
2106 return NULL;
2107 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
2108 return NULL;
2111 static void evaluate_if_statement(struct statement *stmt)
2113 struct symbol *ctype;
2115 if (!stmt->if_conditional)
2116 return;
2118 evaluate_conditional(&stmt->if_conditional);
2119 evaluate_statement(stmt->if_true);
2120 evaluate_statement(stmt->if_false);
2123 static void evaluate_iterator(struct statement *stmt)
2125 struct expression **pre = &stmt->iterator_pre_condition;
2126 struct expression **post = &stmt->iterator_post_condition;
2127 if (*pre == *post) {
2128 evaluate_conditional(pre);
2129 *post = *pre;
2130 } else {
2131 evaluate_conditional(pre);
2132 evaluate_conditional(post);
2134 evaluate_statement(stmt->iterator_pre_statement);
2135 evaluate_statement(stmt->iterator_statement);
2136 evaluate_statement(stmt->iterator_post_statement);
2139 struct symbol *evaluate_statement(struct statement *stmt)
2141 if (!stmt)
2142 return NULL;
2144 switch (stmt->type) {
2145 case STMT_RETURN:
2146 return evaluate_return_expression(stmt);
2148 case STMT_EXPRESSION:
2149 if (!evaluate_expression(stmt->expression))
2150 return NULL;
2151 return degenerate(stmt->expression);
2153 case STMT_COMPOUND: {
2154 struct statement *s;
2155 struct symbol *type = NULL;
2156 struct symbol *sym;
2158 /* Evaluate each symbol in the compound statement */
2159 FOR_EACH_PTR(stmt->syms, sym) {
2160 evaluate_symbol(sym);
2161 } END_FOR_EACH_PTR;
2162 evaluate_symbol(stmt->ret);
2165 * Then, evaluate each statement, making the type of the
2166 * compound statement be the type of the last statement
2168 type = NULL;
2169 FOR_EACH_PTR(stmt->stmts, s) {
2170 type = evaluate_statement(s);
2171 } END_FOR_EACH_PTR;
2172 if (!type)
2173 type = &void_ctype;
2174 return type;
2176 case STMT_IF:
2177 evaluate_if_statement(stmt);
2178 return NULL;
2179 case STMT_ITERATOR:
2180 evaluate_iterator(stmt);
2181 return NULL;
2182 case STMT_SWITCH:
2183 evaluate_expression(stmt->switch_expression);
2184 evaluate_statement(stmt->switch_statement);
2185 return NULL;
2186 case STMT_CASE:
2187 evaluate_expression(stmt->case_expression);
2188 evaluate_expression(stmt->case_to);
2189 evaluate_statement(stmt->case_statement);
2190 return NULL;
2191 case STMT_LABEL:
2192 return evaluate_statement(stmt->label_statement);
2193 case STMT_GOTO:
2194 evaluate_expression(stmt->goto_expression);
2195 return NULL;
2196 case STMT_NONE:
2197 break;
2198 case STMT_ASM:
2199 /* FIXME! Do the asm parameter evaluation! */
2200 break;
2202 return NULL;