4 * Copyright (C) 2003 Transmeta Corp.
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
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
;
40 expr
->ctype
= &int_ctype
;
43 warn(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
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
;
53 warn(expr
->pos
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
57 /* The type of a symbol is the symbol itself! */
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
);
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
;
71 expr
->type
= EXPR_VALUE
;
72 expr
->value
= sym
->value
;
73 expr
->ctype
= base_type
;
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
;
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
;
104 expr
->type
= EXPR_PREOP
;
111 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
113 unsigned long lmod
, rmod
, mod
;
118 if (left
->bit_size
> right
->bit_size
)
121 if (right
->bit_size
> left
->bit_size
)
124 /* Same size integers - promote to unsigned, promote to long */
125 lmod
= left
->ctype
.modifiers
;
126 rmod
= right
->ctype
.modifiers
;
132 return ctype_integer(mod
);
137 if (left
->bit_size
< bits_in_int
)
142 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
144 struct expression
*expr
= alloc_expression(old
->pos
, EXPR_CAST
);
146 expr
->cast_type
= type
;
147 expr
->cast_expression
= old
;
151 static int is_type_type(struct symbol
*type
)
153 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
156 static int is_ptr_type(struct symbol
*type
)
158 if (type
->type
== SYM_NODE
)
159 type
= type
->ctype
.base_type
;
160 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
163 static int is_int_type(struct symbol
*type
)
165 if (type
->type
== SYM_NODE
)
166 type
= type
->ctype
.base_type
;
167 return (type
->type
== SYM_BITFIELD
) || type
->ctype
.base_type
== &int_type
;
170 static struct symbol
*bad_expr_type(struct expression
*expr
)
172 warn(expr
->pos
, "incompatible types for operation");
176 static struct symbol
* compatible_integer_binop(struct expression
*expr
, struct expression
**lp
, struct expression
**rp
)
178 struct expression
*left
= *lp
, *right
= *rp
;
179 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
181 if (ltype
->type
== SYM_NODE
)
182 ltype
= ltype
->ctype
.base_type
;
183 if (rtype
->type
== SYM_NODE
)
184 rtype
= rtype
->ctype
.base_type
;
185 /* Integer promotion? */
186 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
188 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
190 if (is_int_type(ltype
) && is_int_type(rtype
)) {
191 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
193 /* Don't bother promoting same-size entities, it only adds clutter */
194 if (ltype
->bit_size
!= ctype
->bit_size
)
195 *lp
= cast_to(left
, ctype
);
196 if (rtype
->bit_size
!= ctype
->bit_size
)
197 *rp
= cast_to(right
, ctype
);
203 static struct symbol
*evaluate_int_binop(struct expression
*expr
)
205 struct symbol
*ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
210 return bad_expr_type(expr
);
213 static inline int lvalue_expression(struct expression
*expr
)
215 while (expr
->type
== EXPR_CAST
)
216 expr
= expr
->cast_expression
;
217 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
220 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
222 struct symbol
*ctype
;
223 struct symbol
*ptr_type
= ptr
->ctype
;
224 struct symbol
*i_type
= i
->ctype
;
227 if (i_type
->type
== SYM_NODE
)
228 i_type
= i_type
->ctype
.base_type
;
229 if (ptr_type
->type
== SYM_NODE
)
230 ptr_type
= ptr_type
->ctype
.base_type
;
232 if (i_type
->type
== SYM_ENUM
)
234 if (!is_int_type(i_type
))
235 return bad_expr_type(expr
);
238 examine_symbol_type(ctype
);
240 ctype
= degenerate(ptr
);
241 if (!ctype
->ctype
.base_type
) {
242 warn(expr
->pos
, "missing type information");
246 /* Get the size of whatever the pointer points to */
248 if (ptr_type
->type
== SYM_NODE
)
249 ptr_type
= ptr_type
->ctype
.base_type
;
250 if (ptr_type
->type
== SYM_PTR
)
251 ptr_type
= ptr_type
->ctype
.base_type
;
252 bit_size
= ptr_type
->bit_size
;
254 /* Special case: adding zero commonly happens as a result of 'array[0]' */
255 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
257 } else if (bit_size
> bits_in_char
) {
258 struct expression
*add
= expr
;
259 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
260 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
262 val
->ctype
= size_t_ctype
;
263 val
->value
= bit_size
>> 3;
266 mul
->ctype
= size_t_ctype
;
270 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
279 static struct symbol
*evaluate_add(struct expression
*expr
)
281 struct expression
*left
= expr
->left
, *right
= expr
->right
;
282 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
284 if (is_ptr_type(ltype
))
285 return evaluate_ptr_add(expr
, left
, right
);
287 if (is_ptr_type(rtype
))
288 return evaluate_ptr_add(expr
, right
, left
);
290 // FIXME! FP promotion
291 return evaluate_int_binop(expr
);
294 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
295 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE)
297 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
298 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
301 unsigned long mod1
, mod2
, diff
;
302 unsigned long as1
, as2
;
304 struct symbol
*base1
, *base2
;
306 if (target
== source
)
308 if (!target
|| !source
)
309 return "different types";
311 * Peel of per-node information.
312 * FIXME! Check alignment and context too here!
314 mod1
= target
->ctype
.modifiers
;
315 as1
= target
->ctype
.as
;
316 mod2
= source
->ctype
.modifiers
;
317 as2
= source
->ctype
.as
;
318 if (target
->type
== SYM_NODE
) {
319 target
= target
->ctype
.base_type
;
322 if (target
->type
== SYM_PTR
) {
326 mod1
|= target
->ctype
.modifiers
;
327 as1
|= target
->ctype
.as
;
329 if (source
->type
== SYM_NODE
) {
330 source
= source
->ctype
.base_type
;
333 if (source
->type
== SYM_PTR
) {
337 mod2
|= source
->ctype
.modifiers
;
338 as2
|= source
->ctype
.as
;
341 if (target
== source
)
343 if (!target
|| !source
)
344 return "different types";
346 type1
= target
->type
;
347 base1
= target
->ctype
.base_type
;
349 type2
= source
->type
;
350 base2
= source
->ctype
.base_type
;
353 * Pointers to functions compare as the function itself
355 if (type1
== SYM_PTR
&& base1
) {
356 switch (base1
->type
) {
360 base1
= base1
->ctype
.base_type
;
365 if (type2
== SYM_PTR
&& base2
) {
366 switch (base2
->type
) {
370 base2
= base2
->ctype
.base_type
;
376 /* Arrays degenerate to pointers for type comparisons */
377 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
378 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
381 return "different base types";
383 /* Must be same address space to be comparable */
385 return "different address spaces";
387 /* Ignore differences in storage types, sign, or addressability */
388 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
390 mod1
&= diff
& ~target_mod_ignore
;
391 mod2
&= diff
& ~source_mod_ignore
;
393 if ((mod1
| mod2
) & MOD_SIZE
)
394 return "different type sizes";
395 return "different modifiers";
399 if (type1
== SYM_FN
) {
401 struct symbol
*arg1
, *arg2
;
402 if (base1
->variadic
!= base2
->variadic
)
403 return "incompatible variadic arguments";
404 PREPARE_PTR_LIST(target
->arguments
, arg1
);
405 PREPARE_PTR_LIST(source
->arguments
, arg2
);
409 diff
= type_difference(arg1
, arg2
, 0, 0);
411 static char argdiff
[80];
412 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
421 FINISH_PTR_LIST(arg2
);
422 FINISH_PTR_LIST(arg1
);
431 static int is_null_ptr(struct expression
*expr
)
433 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
435 if (!is_ptr_type(expr
->ctype
))
436 warn(expr
->pos
, "Using plain integer as NULL pointer");
440 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
442 /* NULL expression? Just return the type of the "other side" */
451 * Ignore differences in "volatile" and "const"ness when
452 * subtracting pointers
454 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
456 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
458 const char *typediff
;
459 struct symbol
*ctype
;
460 struct symbol
*ltype
, *rtype
;
462 ltype
= degenerate(l
);
463 rtype
= degenerate(r
);
466 * If it is an integer subtract: the ptr add case will do the
469 if (!is_ptr_type(rtype
))
470 return evaluate_ptr_add(expr
, l
, r
);
473 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
475 ctype
= common_ptr_type(l
, r
);
477 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
481 examine_symbol_type(ctype
);
483 /* Figure out the base type we point to */
484 if (ctype
->type
== SYM_NODE
)
485 ctype
= ctype
->ctype
.base_type
;
486 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
487 warn(expr
->pos
, "subtraction of functions? Share your drugs");
490 ctype
= ctype
->ctype
.base_type
;
492 expr
->ctype
= ssize_t_ctype
;
493 if (ctype
->bit_size
> bits_in_char
) {
494 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
495 struct expression
*div
= expr
;
496 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
498 val
->ctype
= size_t_ctype
;
499 val
->value
= ctype
->bit_size
>> 3;
502 sub
->ctype
= ssize_t_ctype
;
511 return ssize_t_ctype
;
514 static struct symbol
*evaluate_sub(struct expression
*expr
)
516 struct expression
*left
= expr
->left
, *right
= expr
->right
;
517 struct symbol
*ltype
= left
->ctype
;
519 if (is_ptr_type(ltype
))
520 return evaluate_ptr_sub(expr
, left
, right
);
522 // FIXME! FP promotion
523 return evaluate_int_binop(expr
);
526 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
528 static struct symbol
*evaluate_conditional(struct expression
*expr
)
530 struct symbol
*ctype
;
535 if (expr
->type
== EXPR_ASSIGNMENT
)
536 warn(expr
->pos
, "assignment expression in conditional");
538 ctype
= evaluate_expression(expr
);
539 if (ctype
&& is_safe_type(ctype
))
540 warn(expr
->pos
, "testing a 'safe expression'");
545 static struct symbol
*evaluate_logical(struct expression
*expr
)
547 if (!evaluate_conditional(expr
->left
))
549 if (!evaluate_conditional(expr
->right
))
552 expr
->ctype
= &bool_ctype
;
556 static struct symbol
*evaluate_arithmetic(struct expression
*expr
)
558 // FIXME! Floating-point promotion!
559 return evaluate_int_binop(expr
);
562 static struct symbol
*evaluate_binop(struct expression
*expr
)
565 // addition can take ptr+int, fp and int
567 return evaluate_add(expr
);
569 // subtraction can take ptr-ptr, fp and int
571 return evaluate_sub(expr
);
573 // Arithmetic operations can take fp and int
574 case '*': case '/': case '%':
575 return evaluate_arithmetic(expr
);
577 // The rest are integer operations (bitops)
578 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
581 return evaluate_int_binop(expr
);
585 static struct symbol
*evaluate_comma(struct expression
*expr
)
587 expr
->ctype
= expr
->right
->ctype
;
591 static struct symbol
*evaluate_compare(struct expression
*expr
)
593 struct expression
*left
= expr
->left
, *right
= expr
->right
;
594 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
595 struct symbol
*ctype
;
598 if (is_type_type(ltype
) && is_type_type(rtype
)) {
599 expr
->ctype
= &bool_ctype
;
603 if (is_safe_type(ltype
) || is_safe_type(rtype
))
604 warn(expr
->pos
, "testing a 'safe expression'");
607 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
608 expr
->ctype
= &bool_ctype
;
609 // FIXME! Check the types for compatibility
613 ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
615 expr
->ctype
= &bool_ctype
;
619 return bad_expr_type(expr
);
622 static int compatible_integer_types(struct symbol
*ltype
, struct symbol
*rtype
)
624 /* Integer promotion? */
625 if (ltype
->type
== SYM_NODE
)
626 ltype
= ltype
->ctype
.base_type
;
627 if (rtype
->type
== SYM_NODE
)
628 rtype
= rtype
->ctype
.base_type
;
629 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
631 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
633 return (is_int_type(ltype
) && is_int_type(rtype
));
637 * FIXME!! This should do casts, array degeneration etc..
639 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
641 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
643 if (ltype
->type
== SYM_NODE
)
644 ltype
= ltype
->ctype
.base_type
;
646 if (rtype
->type
== SYM_NODE
)
647 rtype
= rtype
->ctype
.base_type
;
649 if (ltype
->type
== SYM_PTR
) {
650 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
654 if (rtype
->type
== SYM_PTR
) {
655 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
661 static struct symbol
* evaluate_conditional_expression(struct expression
*expr
)
663 struct expression
*cond
, *true, *false;
664 struct symbol
*ctype
, *ltype
, *rtype
;
665 const char * typediff
;
667 ctype
= degenerate(expr
->conditional
);
668 cond
= expr
->conditional
;
672 if (expr
->cond_true
) {
673 ltype
= degenerate(expr
->cond_true
);
674 true = expr
->cond_true
;
677 rtype
= degenerate(expr
->cond_false
);
678 false = expr
->cond_false
;
681 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
683 ctype
= compatible_integer_binop(expr
, &true, &expr
->cond_false
);
685 ctype
= compatible_ptr_type(true, expr
->cond_false
);
687 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
697 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
698 struct expression
**rp
, struct symbol
*source
, const char *where
)
700 const char *typediff
;
704 /* It's ok if the target is more volatile or const than the source */
705 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
709 if (compatible_integer_types(target
, source
)) {
710 if (target
->bit_size
!= source
->bit_size
)
711 *rp
= cast_to(*rp
, target
);
715 /* Pointer destination? */
717 target_as
= t
->ctype
.as
;
718 if (t
->type
== SYM_NODE
) {
719 t
= t
->ctype
.base_type
;
720 target_as
|= t
->ctype
.as
;
722 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
723 struct expression
*right
= *rp
;
724 struct symbol
*s
= source
;
727 // NULL pointer is always ok
728 if (is_null_ptr(right
))
731 /* "void *" matches anything as long as the address space is ok */
732 source_as
= s
->ctype
.as
;
733 if (s
->type
== SYM_NODE
) {
734 s
= s
->ctype
.base_type
;
735 source_as
|= s
->ctype
.as
;
737 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
738 s
= s
->ctype
.base_type
;
739 t
= t
->ctype
.base_type
;
740 if (s
== &void_ctype
|| t
== &void_ctype
)
746 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
747 info(expr
->pos
, " expected %s", show_typename(target
));
748 info(expr
->pos
, " got %s", show_typename(source
));
753 * FIXME!! This is wrong from a double evaluation standpoint. We can't
754 * just expand the expression twice, that would make any side effects
757 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
760 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
761 static const int op_trans
[] = {
762 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
763 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
764 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
765 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
766 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
767 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
768 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
769 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
770 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
771 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
774 subexpr
->left
= left
;
775 subexpr
->right
= right
;
776 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
778 expr
->right
= subexpr
;
779 return evaluate_binop(subexpr
);
782 static struct symbol
*evaluate_assignment(struct expression
*expr
)
784 struct expression
*left
= expr
->left
, *right
= expr
->right
;
785 struct symbol
*ltype
, *rtype
;
788 rtype
= right
->ctype
;
789 if (expr
->op
!= '=') {
790 rtype
= evaluate_binop_assignment(expr
, left
, right
);
796 if (!lvalue_expression(left
)) {
797 warn(expr
->pos
, "not an lvalue");
801 rtype
= degenerate(right
);
803 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
806 if (ltype
->type
== SYM_NODE
)
807 ltype
->ctype
.modifiers
|= MOD_ASSIGNED
;
813 static void examine_fn_arguments(struct symbol
*fn
)
817 FOR_EACH_PTR(fn
->arguments
, s
) {
818 struct symbol
*arg
= evaluate_symbol(s
);
819 /* Array/function arguments silently degenerate into pointers */
825 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
826 if (arg
->type
== SYM_ARRAY
)
827 ptr
->ctype
= arg
->ctype
;
829 ptr
->ctype
.base_type
= arg
;
830 ptr
->ctype
.as
|= s
->ctype
.as
;
831 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
833 s
->ctype
.base_type
= ptr
;
835 s
->ctype
.modifiers
= 0;
836 examine_symbol_type(s
);
846 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
848 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
849 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
851 newsym
->ctype
.as
= as
;
852 newsym
->ctype
.modifiers
= mod
;
858 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
860 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
861 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
863 node
->ctype
.base_type
= ptr
;
864 ptr
->bit_size
= bits_in_pointer
;
865 ptr
->ctype
.alignment
= pointer_alignment
;
867 node
->bit_size
= bits_in_pointer
;
868 node
->ctype
.alignment
= pointer_alignment
;
870 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
871 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
872 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
873 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
875 if (sym
->type
== SYM_NODE
) {
876 ptr
->ctype
.as
|= sym
->ctype
.as
;
877 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
878 sym
= sym
->ctype
.base_type
;
880 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
881 ptr
->ctype
.as
|= sym
->ctype
.as
;
882 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
883 sym
= sym
->ctype
.base_type
;
885 ptr
->ctype
.base_type
= sym
;
890 /* Arrays degenerate into pointers on pointer arithmetic */
891 static struct symbol
*degenerate(struct expression
*expr
)
893 struct symbol
*ctype
, *base
;
901 if (ctype
->type
== SYM_NODE
)
902 base
= ctype
->ctype
.base_type
;
904 * Arrays degenerate into pointers to the entries, while
905 * functions degenerate into pointers to themselves
907 switch (base
->type
) {
910 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
911 warn(expr
->pos
, "strange non-value function or array");
915 ctype
= create_pointer(expr
, ctype
, 1);
923 static struct symbol
*evaluate_addressof(struct expression
*expr
)
925 struct expression
*op
= expr
->unop
;
926 struct symbol
*ctype
;
928 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
929 warn(expr
->pos
, "not addressable");
936 * symbol expression evaluation is lazy about the type
937 * of the sub-expression, so we may have to generate
938 * the type here if so..
941 ctype
= create_pointer(expr
, ctype
, 0);
948 static struct symbol
*evaluate_dereference(struct expression
*expr
)
950 struct expression
*op
= expr
->unop
;
951 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
953 /* Simplify: *&(expr) => (expr) */
954 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
959 /* Dereferencing a node drops all the node information. */
960 if (ctype
->type
== SYM_NODE
)
961 ctype
= ctype
->ctype
.base_type
;
963 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
964 target
= ctype
->ctype
.base_type
;
966 switch (ctype
->type
) {
968 warn(expr
->pos
, "cannot derefence this type");
971 merge_type(node
, ctype
);
972 if (ctype
->type
!= SYM_ARRAY
)
975 * Dereferencing a pointer to an array results in a
976 * degenerate dereference: the expression becomes
977 * just a pointer to the entry, and the derefence
982 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
983 target
->bit_size
= bits_in_pointer
;
984 target
->ctype
.alignment
= pointer_alignment
;
985 merge_type(target
, ctype
->ctype
.base_type
);
990 * When an array is dereferenced, we need to pick
991 * up the attributes of the original node too..
993 merge_type(node
, op
->ctype
);
994 merge_type(node
, ctype
);
998 node
->bit_size
= target
->bit_size
;
999 node
->array_size
= target
->array_size
;
1006 * Unary post-ops: x++ and x--
1008 static struct symbol
*evaluate_postop(struct expression
*expr
)
1010 struct expression
*op
= expr
->unop
;
1011 struct symbol
*ctype
= op
->ctype
;
1013 if (!lvalue_expression(expr
->unop
)) {
1014 warn(expr
->pos
, "need lvalue expression for ++/--");
1018 if (ctype
->type
== SYM_NODE
)
1019 ctype
->ctype
.modifiers
|= MOD_ASSIGNED
;
1021 expr
->ctype
= ctype
;
1025 static struct symbol
*evaluate_preop(struct expression
*expr
)
1027 struct symbol
*ctype
= expr
->unop
->ctype
;
1032 *expr
= *expr
->unop
;
1036 return evaluate_dereference(expr
);
1039 return evaluate_addressof(expr
);
1041 case SPECIAL_INCREMENT
:
1042 case SPECIAL_DECREMENT
:
1044 * From a type evaluation standpoint the pre-ops are
1045 * the same as the postops
1047 return evaluate_postop(expr
);
1050 if (is_safe_type(ctype
))
1051 warn(expr
->pos
, "testing a 'safe expression'");
1052 ctype
= &bool_ctype
;
1058 expr
->ctype
= ctype
;
1062 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1064 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1065 struct ptr_list
*list
= head
;
1071 for (i
= 0; i
< list
->nr
; i
++) {
1072 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1074 if (sym
->ident
!= ident
)
1076 *offset
= sym
->offset
;
1079 struct symbol
*ctype
= sym
->ctype
.base_type
;
1083 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1085 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1088 *offset
+= sym
->offset
;
1092 } while ((list
= list
->next
) != head
);
1096 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1098 struct expression
*add
;
1102 /* Create a new add-expression */
1103 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1106 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1107 add
->right
->ctype
= &int_ctype
;
1108 add
->right
->value
= offset
;
1112 * The ctype of the pointer will be lazily evaluated if
1113 * we ever take the address of this member dereference..
1119 /* structure/union dereference */
1120 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1123 struct symbol
*ctype
, *member
;
1124 struct expression
*deref
= expr
->deref
, *add
;
1125 struct ident
*ident
= expr
->member
;
1129 if (!evaluate_expression(deref
))
1132 warn(expr
->pos
, "bad member name");
1136 ctype
= deref
->ctype
;
1137 address_space
= ctype
->ctype
.as
;
1138 mod
= ctype
->ctype
.modifiers
;
1139 if (ctype
->type
== SYM_NODE
) {
1140 ctype
= ctype
->ctype
.base_type
;
1141 address_space
|= ctype
->ctype
.as
;
1142 mod
|= ctype
->ctype
.modifiers
;
1144 if (!lvalue_expression(deref
)) {
1145 warn(deref
->pos
, "expected lvalue for member dereference");
1148 deref
= deref
->unop
;
1149 expr
->deref
= deref
;
1150 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1151 warn(expr
->pos
, "expected structure or union");
1155 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1157 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1158 const char *name
= "<unnamed>";
1161 name
= ctype
->ident
->name
;
1162 namelen
= ctype
->ident
->len
;
1164 warn(expr
->pos
, "no member '%s' in %s %.*s",
1165 show_ident(ident
), type
, namelen
, name
);
1170 * The member needs to take on the address space and modifiers of
1171 * the "parent" type.
1173 member
= convert_to_as_mod(member
, address_space
, mod
);
1174 add
= evaluate_offset(deref
, offset
);
1176 ctype
= member
->ctype
.base_type
;
1177 if (ctype
->type
== SYM_BITFIELD
) {
1178 expr
->type
= EXPR_BITFIELD
;
1179 expr
->bitpos
= member
->bit_offset
;
1180 expr
->nrbits
= member
->fieldwidth
;
1181 expr
->address
= add
;
1183 expr
->type
= EXPR_PREOP
;
1188 expr
->ctype
= member
;
1192 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1196 if (expr
->cast_type
) {
1197 examine_symbol_type(expr
->cast_type
);
1198 size
= expr
->cast_type
->bit_size
;
1200 if (!evaluate_expression(expr
->cast_expression
))
1202 size
= expr
->cast_expression
->ctype
->bit_size
;
1205 warn(expr
->pos
, "cannot size expression");
1208 expr
->type
= EXPR_VALUE
;
1209 expr
->value
= size
>> 3;
1210 expr
->ctype
= size_t_ctype
;
1211 return size_t_ctype
;
1214 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1216 struct symbol
*type
= expr
->cast_type
;
1219 type
= evaluate_expression(expr
->cast_expression
);
1223 examine_symbol_type(type
);
1224 expr
->type
= EXPR_VALUE
;
1225 expr
->value
= type
->ctype
.alignment
;
1226 expr
->ctype
= size_t_ctype
;
1227 return size_t_ctype
;
1230 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1232 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1233 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1237 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1239 struct expression
*expr
;
1240 struct symbol_list
*argument_types
= fn
->arguments
;
1241 struct symbol
*argtype
;
1244 PREPARE_PTR_LIST(argument_types
, argtype
);
1245 FOR_EACH_PTR (head
, expr
) {
1246 struct expression
**p
= THIS_ADDRESS(expr
);
1247 struct symbol
*ctype
, *target
;
1248 ctype
= evaluate_expression(expr
);
1253 if (context_clash(f
, ctype
))
1254 warn(expr
->pos
, "argument %d used in wrong context", i
);
1256 ctype
= degenerate(expr
);
1259 if (!target
&& ctype
->bit_size
< bits_in_int
)
1260 target
= &int_ctype
;
1262 static char where
[30];
1263 examine_symbol_type(target
);
1264 sprintf(where
, "argument %d", i
);
1265 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1269 NEXT_PTR_LIST(argtype
);
1271 FINISH_PTR_LIST(argtype
);
1275 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
);
1276 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1278 struct expression
*entry
;
1282 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1283 struct expression
**p
= THIS_ADDRESS(entry
);
1285 if (entry
->type
== EXPR_INDEX
) {
1286 current
= entry
->idx_to
;
1289 evaluate_initializer(ctype
, p
, offset
+ current
*(ctype
->bit_size
>>3));
1297 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1298 static int evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1300 if (offset
|| expression_list_size(expr
->expr_list
) != 1) {
1301 warn(expr
->pos
, "unexpected compound initializer");
1304 return evaluate_array_initializer(ctype
, expr
, 0);
1307 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
, unsigned long offset
)
1309 struct expression
*entry
;
1312 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1313 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1314 struct expression
**p
= THIS_ADDRESS(entry
);
1316 if (entry
->type
== EXPR_IDENTIFIER
) {
1317 struct ident
*ident
= entry
->expr_ident
;
1318 /* We special-case the "already right place" case */
1319 if (sym
&& sym
->ident
== ident
)
1321 RESET_PTR_LIST(sym
);
1324 warn(entry
->pos
, "unknown named initializer '%s'", show_ident(ident
));
1327 if (sym
->ident
== ident
)
1335 warn(expr
->pos
, "too many initializers for struct/union");
1339 evaluate_initializer(sym
, p
, offset
+ sym
->offset
);
1343 FINISH_PTR_LIST(sym
);
1349 * Initializers are kind of like assignments. Except
1350 * they can be a hell of a lot more complex.
1352 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
)
1354 struct expression
*expr
= *ep
;
1357 * Simple non-structure/array initializers are the simple
1358 * case, and look (and parse) largely like assignments.
1360 if (expr
->type
!= EXPR_INITIALIZER
) {
1362 struct symbol
*rtype
= evaluate_expression(expr
);
1364 struct expression
*pos
;
1366 // FIXME! char array[] = "string" special case
1367 // should _not_ degenerate.
1368 rtype
= degenerate(expr
);
1369 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1370 /* strings are special: char arrays */
1371 if (rtype
->type
== SYM_ARRAY
)
1372 size
= get_expression_value(rtype
->array_size
);
1374 * Don't bother creating a position expression for
1375 * the simple initializer cases that don't need it.
1377 * We need a position if the initializer has a byte
1378 * offset, _or_ if we're initializing a bitfield.
1380 if (offset
|| ctype
->fieldwidth
) {
1381 pos
= alloc_expression(expr
->pos
, EXPR_POS
);
1382 pos
->init_offset
= offset
;
1383 pos
->init_sym
= ctype
;
1384 pos
->init_expr
= *ep
;
1385 pos
->ctype
= expr
->ctype
;
1392 expr
->ctype
= ctype
;
1393 if (ctype
->type
== SYM_NODE
)
1394 ctype
= ctype
->ctype
.base_type
;
1396 switch (ctype
->type
) {
1399 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
, offset
);
1401 return evaluate_struct_or_union_initializer(ctype
, expr
, 0, offset
);
1403 return evaluate_struct_or_union_initializer(ctype
, expr
, 1, offset
);
1405 return evaluate_scalar_initializer(ctype
, expr
, offset
);
1409 static int get_as(struct symbol
*sym
)
1417 mod
= sym
->ctype
.modifiers
;
1418 if (sym
->type
== SYM_NODE
) {
1419 sym
= sym
->ctype
.base_type
;
1420 as
|= sym
->ctype
.as
;
1421 mod
|= sym
->ctype
.modifiers
;
1424 * You can always throw a value away by casting to
1425 * "void" - that's an implicit "force". Note that
1426 * the same is _not_ true of "void *".
1428 if (sym
== &void_ctype
)
1432 * At least for now, allow casting to a "unsigned long".
1433 * That's how we do things like pointer arithmetic and
1434 * store pointers to registers.
1436 if (sym
== &ulong_ctype
)
1439 if (sym
&& sym
->type
== SYM_PTR
) {
1440 sym
= sym
->ctype
.base_type
;
1441 as
|= sym
->ctype
.as
;
1442 mod
|= sym
->ctype
.modifiers
;
1444 if (mod
& MOD_FORCE
)
1449 static struct symbol
*evaluate_cast(struct expression
*expr
)
1451 struct expression
*target
= expr
->cast_expression
;
1452 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1454 expr
->ctype
= ctype
;
1455 expr
->cast_type
= ctype
;
1458 * Special case: a cast can be followed by an
1459 * initializer, in which case we need to pass
1460 * the type value down to that initializer rather
1461 * than trying to evaluate it as an expression
1463 * A more complex case is when the initializer is
1464 * dereferenced as part of a post-fix expression.
1465 * We need to produce an expression that can be dereferenced.
1467 if (target
->type
== EXPR_INITIALIZER
) {
1468 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1469 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1471 sym
->ctype
.base_type
= ctype
;
1472 sym
->initializer
= expr
->cast_expression
;
1473 evaluate_symbol(sym
);
1475 addr
->ctype
= NULL
; /* Lazy eval */
1478 expr
->type
= EXPR_PREOP
;
1481 expr
->ctype
= ctype
;
1485 evaluate_expression(target
);
1488 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
1489 warn(expr
->pos
, "cast removes address space of expression");
1492 * Casts of constant values are special: they
1493 * can be NULL, and thus need to be simplified
1496 if (target
->type
== EXPR_VALUE
)
1497 cast_value(expr
, ctype
, target
, target
->ctype
);
1503 * Evaluate a call expression with a symbol. This
1504 * should expand inline functions, and evaluate
1507 static int evaluate_symbol_call(struct expression
*expr
)
1509 struct expression
*fn
= expr
->fn
;
1510 struct symbol
*ctype
= fn
->ctype
;
1512 if (fn
->type
!= EXPR_PREOP
)
1515 if (ctype
->op
&& ctype
->op
->evaluate
)
1516 return ctype
->op
->evaluate(expr
);
1518 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
1520 struct symbol
*curr
= current_fn
;
1521 unsigned long context
= current_context
;
1522 unsigned long mask
= current_contextmask
;
1524 current_context
|= ctype
->ctype
.context
;
1525 current_contextmask
|= ctype
->ctype
.contextmask
;
1526 current_fn
= ctype
->ctype
.base_type
;
1527 examine_fn_arguments(current_fn
);
1529 ret
= inline_function(expr
, ctype
);
1531 /* restore the old function context */
1533 current_context
= context
;
1534 current_contextmask
= mask
;
1541 static struct symbol
*evaluate_call(struct expression
*expr
)
1544 struct symbol
*ctype
, *sym
;
1545 struct expression
*fn
= expr
->fn
;
1546 struct expression_list
*arglist
= expr
->args
;
1548 if (!evaluate_expression(fn
))
1550 sym
= ctype
= fn
->ctype
;
1551 if (ctype
->type
== SYM_NODE
)
1552 ctype
= ctype
->ctype
.base_type
;
1553 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1554 ctype
= ctype
->ctype
.base_type
;
1555 if (!evaluate_arguments(sym
, ctype
, arglist
))
1557 if (ctype
->type
!= SYM_FN
) {
1558 warn(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
1561 args
= expression_list_size(expr
->args
);
1562 fnargs
= symbol_list_size(ctype
->arguments
);
1564 warn(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
1565 if (args
> fnargs
&& !ctype
->variadic
)
1566 warn(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
1567 if (sym
->type
== SYM_NODE
) {
1568 if (evaluate_symbol_call(expr
))
1571 expr
->ctype
= ctype
->ctype
.base_type
;
1575 struct symbol
*evaluate_expression(struct expression
*expr
)
1582 switch (expr
->type
) {
1584 warn(expr
->pos
, "value expression without a type");
1587 return evaluate_string(expr
);
1589 return evaluate_symbol_expression(expr
);
1591 if (!evaluate_expression(expr
->left
))
1593 if (!evaluate_expression(expr
->right
))
1595 return evaluate_binop(expr
);
1597 return evaluate_logical(expr
);
1599 if (!evaluate_expression(expr
->left
))
1601 if (!evaluate_expression(expr
->right
))
1603 return evaluate_comma(expr
);
1605 if (!evaluate_expression(expr
->left
))
1607 if (!evaluate_expression(expr
->right
))
1609 return evaluate_compare(expr
);
1610 case EXPR_ASSIGNMENT
:
1611 if (!evaluate_expression(expr
->left
))
1613 if (!evaluate_expression(expr
->right
))
1615 return evaluate_assignment(expr
);
1617 if (!evaluate_expression(expr
->unop
))
1619 return evaluate_preop(expr
);
1621 if (!evaluate_expression(expr
->unop
))
1623 return evaluate_postop(expr
);
1625 return evaluate_cast(expr
);
1627 return evaluate_sizeof(expr
);
1629 return evaluate_alignof(expr
);
1631 return evaluate_member_dereference(expr
);
1633 return evaluate_call(expr
);
1635 warn(expr
->pos
, "bitfield generated by parser");
1637 case EXPR_CONDITIONAL
:
1638 if (!evaluate_conditional(expr
->conditional
))
1640 if (!evaluate_expression(expr
->cond_false
))
1642 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1644 return evaluate_conditional_expression(expr
);
1645 case EXPR_STATEMENT
:
1646 expr
->ctype
= evaluate_statement(expr
->statement
);
1650 expr
->ctype
= &ptr_ctype
;
1654 /* Evaluate the type of the symbol .. */
1655 evaluate_symbol(expr
->symbol
);
1656 /* .. but the type of the _expression_ is a "type" */
1657 expr
->ctype
= &type_ctype
;
1660 /* These can not exist as stand-alone expressions */
1661 case EXPR_INITIALIZER
:
1662 case EXPR_IDENTIFIER
:
1665 warn(expr
->pos
, "internal front-end error: initializer in expression");
1671 void check_duplicates(struct symbol
*sym
)
1673 struct symbol
*next
= sym
;
1675 while ((next
= next
->same_symbol
) != NULL
) {
1676 const char *typediff
;
1677 evaluate_symbol(next
);
1678 typediff
= type_difference(sym
, next
, 0, 0);
1680 warn(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1681 show_ident(sym
->ident
),
1682 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
1688 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1690 struct symbol
*base_type
;
1695 sym
= examine_symbol_type(sym
);
1696 base_type
= sym
->ctype
.base_type
;
1700 /* Evaluate the initializers */
1701 if (sym
->initializer
) {
1702 int count
= evaluate_initializer(sym
, &sym
->initializer
, 0);
1703 if (base_type
->type
== SYM_ARRAY
&& !base_type
->array_size
) {
1704 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1705 base_type
->array_size
= alloc_const_expression(sym
->pos
, count
);
1706 base_type
->bit_size
= bit_size
;
1707 sym
->array_size
= base_type
->array_size
;
1708 sym
->bit_size
= bit_size
;
1712 /* And finally, evaluate the body of the symbol too */
1713 if (base_type
->type
== SYM_FN
) {
1714 examine_fn_arguments(base_type
);
1715 if (base_type
->stmt
) {
1716 current_fn
= base_type
;
1717 current_contextmask
= sym
->ctype
.contextmask
;
1718 current_context
= sym
->ctype
.context
;
1719 evaluate_statement(base_type
->stmt
);
1726 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1728 struct expression
*expr
= stmt
->expression
;
1729 struct symbol
*ctype
, *fntype
;
1731 evaluate_expression(expr
);
1732 ctype
= degenerate(expr
);
1733 fntype
= current_fn
->ctype
.base_type
;
1734 if (!fntype
|| fntype
== &void_ctype
) {
1735 if (expr
&& ctype
!= &void_ctype
)
1736 warn(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
1741 warn(stmt
->pos
, "return with no return value");
1746 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression");
1750 static void evaluate_if_statement(struct statement
*stmt
)
1752 struct expression
*expr
= stmt
->if_conditional
;
1753 struct symbol
*ctype
;
1758 ctype
= evaluate_conditional(expr
);
1762 evaluate_statement(stmt
->if_true
);
1763 evaluate_statement(stmt
->if_false
);
1766 struct symbol
*evaluate_statement(struct statement
*stmt
)
1771 switch (stmt
->type
) {
1773 return evaluate_return_expression(stmt
);
1775 case STMT_EXPRESSION
:
1776 evaluate_expression(stmt
->expression
);
1777 return degenerate(stmt
->expression
);
1779 case STMT_COMPOUND
: {
1780 struct statement
*s
;
1781 struct symbol
*type
= NULL
;
1784 /* Evaluate each symbol in the compound statement */
1785 FOR_EACH_PTR(stmt
->syms
, sym
) {
1786 evaluate_symbol(sym
);
1788 evaluate_symbol(stmt
->ret
);
1791 * Then, evaluate each statement, making the type of the
1792 * compound statement be the type of the last statement
1795 FOR_EACH_PTR(stmt
->stmts
, s
) {
1796 type
= evaluate_statement(s
);
1801 evaluate_if_statement(stmt
);
1804 evaluate_conditional(stmt
->iterator_pre_condition
);
1805 evaluate_conditional(stmt
->iterator_post_condition
);
1806 evaluate_statement(stmt
->iterator_pre_statement
);
1807 evaluate_statement(stmt
->iterator_statement
);
1808 evaluate_statement(stmt
->iterator_post_statement
);
1811 evaluate_expression(stmt
->switch_expression
);
1812 evaluate_statement(stmt
->switch_statement
);
1815 evaluate_expression(stmt
->case_expression
);
1816 evaluate_expression(stmt
->case_to
);
1817 evaluate_statement(stmt
->case_statement
);
1820 evaluate_statement(stmt
->label_statement
);
1823 evaluate_expression(stmt
->goto_expression
);
1828 /* FIXME! Do the asm parameter evaluation! */