Lines Matching refs:expr

50 static struct symbol *degenerate(struct expression *expr);
53 static inline int valid_expr_type(struct expression *expr)
55 return expr && valid_type(expr->ctype);
58 static inline int valid_subexpr_type(struct expression *expr)
60 return valid_expr_type(expr->left)
61 && valid_expr_type(expr->right);
78 static struct symbol *evaluate_symbol_expression(struct expression *expr)
81 struct symbol *sym = expr->symbol;
85 expression_error(expr, "undefined identifier '%s'", show_ident(expr->symbol_name));
93 expression_error(expr, "identifier '%s' has no type", show_ident(expr->symbol_name));
97 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
99 addr->symbol_name = expr->symbol_name;
101 addr->flags = expr->flags;
102 expr->type = EXPR_PREOP;
103 expr->op = '*';
104 expr->unop = addr;
105 expr->flags = CEF_NONE;
108 expr->ctype = sym;
112 static struct symbol *evaluate_string(struct expression *expr)
114 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
115 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
116 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
117 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
118 unsigned int length = expr->string->length;
119 struct symbol *char_type = expr->wide ? wchar_ctype : &char_ctype;
121 sym->array_size = alloc_const_expression(expr->pos, length);
132 initstr->string = expr->string;
146 expr->type = EXPR_PREOP;
147 expr->op = '*';
148 expr->unop = addr;
149 expr->ctype = sym;
248 static int is_same_type(struct expression *expr, struct symbol *new)
250 struct symbol *old = expr->ctype;
281 warning(expr->pos, "implicit cast %s nocast type", tofrom);
308 static int cast_flags(struct expression *expr, struct expression *target);
309 static struct symbol *cast_to_bool(struct expression *expr);
317 struct expression *expr;
324 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
325 expr->ctype = type;
326 expr->cast_type = type;
327 expr->cast_expression = old;
328 expr->flags = cast_flags(expr, old);
331 cast_to_bool(expr);
333 return expr;
390 static struct symbol *bad_expr_type(struct expression *expr)
392 switch (expr->type) {
395 if (!valid_subexpr_type(expr))
397 sparse_error(expr->pos, "incompatible types for operation (%s):", show_special(expr->op));
398 info(expr->pos, " %s", show_typename(expr->left->ctype));
399 info(expr->pos, " %s", show_typename(expr->right->ctype));
403 if (!valid_expr_type(expr->unop))
405 sparse_error(expr->pos, "incompatible type for operation (%s):", show_special(expr->op));
406 info(expr->pos, " %s", show_typename(expr->unop->ctype));
412 expr->flags = CEF_NONE;
413 return expr->ctype = &bad_ctype;
508 static inline void unrestrict(struct expression *expr,
514 warning(expr->pos, "%s degrades to integer",
560 static inline int lvalue_expression(struct expression *expr)
562 return expr->type == EXPR_PREOP && expr->op == '*';
565 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *itype)
567 struct expression *index = expr->right;
571 classify_type(degenerate(expr->left), &ctype);
578 if ((expr->left->flags & CEF_ADDR) && (expr->right->flags & CEF_ICE))
579 expr->flags = CEF_ADDR;
582 expression_error(expr, "missing type information");
586 expression_error(expr, "arithmetics on pointers to functions");
595 expr->ctype = ctype;
601 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
613 expr->right = val;
621 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
622 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
634 expr->right = index;
800 static void bad_null(struct expression *expr)
803 warning(expr->pos, "Using plain integer as NULL pointer");
814 static struct symbol *evaluate_ptr_sub(struct expression *expr)
818 struct expression *l = expr->left;
819 struct expression *r = expr->right;
831 expression_error(expr, "subtraction of different types can't work (%s)", typediff);
834 expression_error(expr, "subtraction of functions? Share your drugs");
838 expr->ctype = ssize_t_ctype;
840 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
841 struct expression *div = expr;
842 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
850 warning(expr->pos, "potentially expensive pointer subtraction");
851 info(expr->pos, " '%s' has a non-power-of-2 size: %lu", show_typename(lbase), value);
870 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
874 if (!expr)
877 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
878 warning(expr->pos, "assignment expression in conditional");
880 ctype = evaluate_expression(expr);
884 warning(expr->pos, "testing a 'safe expression'");
887 warning(expr->pos, "the address of %s will always evaluate as true", "a function");
890 warning(expr->pos, "the address of %s will always evaluate as true", "an array");
892 sparse_error(expr->pos, "non-scalar type in conditional:");
893 info(expr->pos, " %s", show_typename(ctype));
897 ctype = degenerate(expr);
901 static struct symbol *evaluate_logical(struct expression *expr)
903 if (!evaluate_conditional(expr->left, 0))
905 if (!evaluate_conditional(expr->right, 0))
909 expr->ctype = &int_ctype;
910 expr->flags = expr->left->flags & expr->right->flags;
911 expr->flags &= ~(CEF_CONST_MASK | CEF_ADDR);
915 static struct symbol *evaluate_binop(struct expression *expr)
918 int lclass = classify_type(expr->left->ctype, &ltype);
919 int rclass = classify_type(expr->right->ctype, &rtype);
920 int op = expr->op;
924 expr->flags = expr->left->flags & expr->right->flags;
925 expr->flags &= ~CEF_CONST_MASK;
932 return bad_expr_type(expr);
938 unrestrict(expr->left, lclass, &ltype);
939 unrestrict(expr->right, rclass, &rtype);
944 const unsigned left_not = expr->left->type == EXPR_PREOP
945 && expr->left->op == '!';
946 const unsigned right_not = expr->right->type == EXPR_PREOP
947 && expr->right->op == '!';
949 warning(expr->pos, "dubious: %sx %c %sy",
954 ltype = usual_conversions(op, expr->left, expr->right,
959 expr->left = cast_to(expr->left, ltype);
960 expr->right = cast_to(expr->right, rtype);
961 expr->ctype = ctype;
967 unrestrict(expr->right, rclass, &rtype);
968 return evaluate_ptr_add(expr, rtype);
973 struct expression *index = expr->left;
975 expr->left = expr->right;
976 expr->right = index;
977 return evaluate_ptr_add(expr, ltype);
981 if (lclass & rclass & TYPE_PTR && expr->op == '-')
982 return evaluate_ptr_sub(expr);
984 return bad_expr_type(expr);
987 static struct symbol *evaluate_comma(struct expression *expr)
989 expr->ctype = unqualify_type(degenerate(expr->right));
990 if (expr->ctype == &null_ctype)
991 expr->ctype = &ptr_ctype;
992 expr->flags &= expr->left->flags & expr->right->flags;
993 return expr->ctype;
1024 static struct symbol *evaluate_compare(struct expression *expr)
1026 struct expression *left = expr->left, *right = expr->right;
1039 expr->flags = CEF_SET_ICE;
1044 warning(expr->pos, "testing a 'safe expression'");
1046 expr->flags = left->flags & right->flags & ~CEF_CONST_MASK & ~CEF_ADDR;
1050 ctype = usual_conversions(expr->op, expr->left, expr->right,
1052 expr->left = cast_to(expr->left, ctype);
1053 expr->right = cast_to(expr->right, ctype);
1055 expr->op = modify_for_unsigned(expr->op);
1061 return bad_expr_type(expr);
1064 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1072 int positive = expr->op == SPECIAL_EQUAL;
1073 expr->type = EXPR_VALUE;
1074 expr->value = positive;
1078 expr->left = cast_to(left, rtype);
1082 expr->right = cast_to(right, ltype);
1088 return bad_expr_type(expr);
1089 expr->op = modify_for_unsigned(expr->op);
1095 if (expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) {
1098 expr->right = cast_to(right, ltype);
1102 expr->left = cast_to(left, rtype);
1114 expression_error(expr, "incompatible types in comparison expression (%s):", typediff);
1115 info(expr->pos, " %s", show_typename(ltype));
1116 info(expr->pos, " %s", show_typename(rtype));
1121 expr->ctype = &int_ctype;
1132 static struct symbol *evaluate_conditional_expression(struct expression *expr)
1140 if (!evaluate_conditional(expr->conditional, 0))
1142 if (!evaluate_expression(expr->cond_false))
1145 ctype = degenerate(expr->conditional);
1146 rtype = degenerate(expr->cond_false);
1148 cond = &expr->conditional;
1150 if (expr->cond_true) {
1151 if (!evaluate_expression(expr->cond_true))
1153 ltype = degenerate(expr->cond_true);
1154 cond = &expr->cond_true;
1157 expr->flags = (expr->conditional->flags & (*cond)->flags &
1158 expr->cond_false->flags & ~CEF_CONST_MASK);
1171 if (expr->conditional->flags & (CEF_ACE | CEF_ADDR)) {
1172 int is_true = expr_truth_value(expr->conditional);
1173 struct expression *arg = is_true ? *cond : expr->cond_false;
1174 expr->flags = arg->flags & ~CEF_CONST_MASK;
1180 ctype = usual_conversions('?', *cond, expr->cond_false,
1183 expr->cond_false = cast_to(expr->cond_false, ctype);
1189 int is_null2 = is_null_pointer_constant(expr->cond_false);
1193 expr->cond_false = cast_to(expr->cond_false, &ptr_ctype);
1206 bad_null(expr->cond_false);
1207 expr->cond_false = cast_to(expr->cond_false, ltype);
1254 expression_error(expr, "incompatible types in conditional expression (%s):", typediff);
1255 info(expr->pos, " %s", show_typename(ltype));
1256 info(expr->pos, " %s", show_typename(rtype));
1261 switch (expr_truth_value(expr->conditional)) {
1262 case 1: expr->ctype = ltype;
1264 case 0: expr->ctype = rtype;
1272 expr->ctype = ctype;
1283 expr->cond_false = cast_to(expr->cond_false, ctype);
1296 static int evaluate_assign_op(struct expression *expr)
1298 struct symbol *target = expr->left->ctype;
1299 struct symbol *source = expr->right->ctype;
1303 int op = expr->op;
1307 expression_error(expr, "invalid assignment");
1312 warning(expr->pos, "bad assignment (%s) to %s",
1314 expr->right = cast_to(expr->right, target);
1320 if (!restricted_value(expr->right, t))
1324 unrestrict(expr->left, tclass, &t);
1327 unrestrict(expr->right, sclass, &s);
1329 expr->right = cast_to(expr->right, source);
1333 expr->right = cast_to(expr->right, &uint_ctype);
1341 warning(expr->pos, "invalid assignment: %s", show_special(op));
1342 info(expr->pos, " left side has type %s", show_typename(t));
1343 info(expr->pos, " right side has type %s", show_typename(s));
1344 expr->right = cast_to(expr->right, target);
1349 unrestrict(expr->right, sclass, &s);
1350 evaluate_ptr_add(expr, s);
1353 expression_error(expr, "invalid pointer assignment");
1357 expression_error(expr, "invalid assignment");
1361 target = usual_conversions(op, expr->left, expr->right,
1364 expr->right = cast_to(expr->right, target);
1490 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1497 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1498 info(expr->pos, " expected %s", show_typename(target));
1499 info(expr->pos, " got %s", show_typename(source));
1524 static int compatible_argument_type(struct expression *expr, struct symbol *target,
1530 return compatible_assignment_types(expr, target, rp, where);
1533 static void mark_addressable(struct expression *expr)
1535 while (expr->type == EXPR_BINOP && expr->op == '+')
1536 expr = expr->left;
1537 if (expr->type == EXPR_SYMBOL) {
1538 struct symbol *sym = expr->symbol;
1543 static void mark_assigned(struct expression *expr)
1547 if (!expr)
1549 switch (expr->type) {
1551 sym = expr->symbol;
1560 mark_assigned(expr->left);
1561 mark_assigned(expr->right);
1565 mark_assigned(expr->cast_expression);
1568 mark_assigned(expr->base);
1585 static struct symbol *evaluate_assignment(struct expression *expr)
1587 struct expression *left = expr->left;
1591 expression_error(expr, "not an lvalue");
1597 if (expr->op != '=') {
1598 if (!evaluate_assign_op(expr))
1601 if (!compatible_assignment_types(expr, ltype, &expr->right, "assignment"))
1607 expr->ctype = ltype;
1660 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1662 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1663 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1674 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1693 static struct symbol *degenerate(struct expression *expr)
1697 if (!expr)
1699 ctype = expr->ctype;
1714 if (expr->type == EXPR_SLICE) {
1715 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1718 a->ctype.base_type = expr->base->ctype;
1719 a->bit_size = expr->base->ctype->bit_size;
1720 a->array_size = expr->base->ctype->array_size;
1722 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1726 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1729 e1->ctype = expr->base->ctype; /* XXX */
1731 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1733 e2->right = expr->base;
1735 e2->ctype = expr->base->ctype;
1737 if (expr->r_bitpos) {
1738 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1741 e3->right = alloc_const_expression(expr->pos,
1742 bits_to_bytes(expr->r_bitpos));
1748 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1753 expr->unop = e4;
1754 expr->type = EXPR_PREOP;
1755 expr->op = '*';
1758 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1759 expression_error(expr, "strange non-value function or array");
1763 sparse_error(expr->pos, "taking the address of built-in function '%s'", show_ident(ctype->ident));
1764 *expr = *expr->unop;
1765 ctype = create_pointer(expr, ctype, 1);
1766 expr->ctype = ctype;
1767 mark_addressable(expr);
1774 static struct symbol *evaluate_addressof(struct expression *expr)
1776 struct expression *op = expr->unop;
1780 expression_error(expr, "not addressable");
1785 sparse_error(expr->pos, "taking the address of built-in function '%s'", show_ident(ctype->ident));
1786 *expr = *op->unop;
1788 mark_addressable(expr);
1795 if (expr->ctype == &lazy_ptr_ctype) {
1796 ctype = create_pointer(expr, ctype, 0);
1797 expr->ctype = ctype;
1799 return expr->ctype;
1803 static struct symbol *evaluate_dereference(struct expression *expr)
1805 struct expression *op = expr->unop;
1808 /* Simplify: *&(expr) => (expr) */
1810 *expr = *op->unop;
1811 expr->flags = CEF_NONE;
1812 return expr->ctype;
1825 expression_error(expr, "cannot dereference this type");
1828 *expr = *op;
1829 return expr->ctype;
1832 node = alloc_symbol(expr->pos, SYM_NODE);
1850 node = alloc_symbol(expr->pos, SYM_NODE);
1859 expr->ctype = node;
1866 static struct symbol *evaluate_postop(struct expression *expr)
1868 struct expression *op = expr->unop;
1874 expression_error(expr, "need scalar for ++/--");
1877 if (!lvalue_expression(expr->unop)) {
1878 expression_error(expr, "need lvalue expression for ++/--");
1882 unrestrict(expr, class, &ctype);
1894 expr->op_value = multiply;
1895 expr->ctype = ctype;
1899 expression_error(expr, "bad argument type for ++/--");
1903 static struct symbol *evaluate_sign(struct expression *expr)
1905 struct symbol *ctype = expr->unop->ctype;
1907 unsigned char flags = expr->unop->flags & ~CEF_CONST_MASK;
1911 return bad_expr_type(expr);
1917 expr->unop = cast_to(expr->unop, ctype);
1918 } else if (expr->op != '~') {
1921 return bad_expr_type(expr);
1923 if (expr->op == '+')
1924 *expr = *expr->unop;
1925 expr->flags = flags;
1926 expr->ctype = ctype;
1929 if (restricted_unop(expr->op, &ctype))
1930 unrestrict(expr, class, &ctype);
1934 static struct symbol *evaluate_preop(struct expression *expr)
1936 struct symbol *ctype = expr->unop->ctype;
1938 switch (expr->op) {
1940 *expr = *expr->unop;
1946 return evaluate_sign(expr);
1949 return evaluate_dereference(expr);
1952 return evaluate_addressof(expr);
1960 return evaluate_postop(expr);
1963 ctype = degenerate(expr->unop);
1964 expr->flags = expr->unop->flags & ~CEF_CONST_MASK;
1969 expr->flags &= ~CEF_ADDR;
1972 warning(expr->pos, "testing a 'safe expression'");
1974 struct expression *arg = expr->unop;
1975 expr->type = EXPR_COMPARE;
1976 expr->op = SPECIAL_EQUAL;
1977 expr->left = arg;
1978 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1979 expr->right->ctype = ctype;
1980 expr->right->fvalue = 0;
1982 warning(expr->pos, "%s degrades to integer",
1992 expr->ctype = ctype;
2030 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
2043 add = alloc_expression(expr->pos, EXPR_BINOP);
2045 add->left = expr;
2046 add->right = alloc_expression(expr->pos, EXPR_VALUE);
2059 add->flags = expr->flags;
2065 static struct symbol *evaluate_member_dereference(struct expression *expr)
2069 struct expression *deref = expr->deref, *add;
2070 struct ident *ident = expr->member;
2077 expression_error(expr, "bad member name");
2091 expression_error(expr, "expected structure or union");
2105 expression_error(expr, "no member '%s' in %s %.*s",
2108 expression_error(expr, "using member '%s' in "
2123 expr->base = deref;
2124 expr->r_bitpos = 0;
2126 expr->base = deref->base;
2127 expr->r_bitpos = deref->r_bitpos;
2129 expr->r_bitpos += bytes_to_bits(offset);
2130 expr->type = EXPR_SLICE;
2131 expr->r_bitpos += member->bit_offset;
2132 expr->ctype = member;
2137 expr->deref = deref;
2140 expr->type = EXPR_PREOP;
2141 expr->op = '*';
2142 expr->unop = add;
2144 expr->ctype = member;
2148 static int is_promoted(struct expression *expr)
2151 switch (expr->type) {
2157 expr = expr->right;
2160 switch (expr->op) {
2162 expr = expr->unop;
2178 static struct symbol *evaluate_type_information(struct expression *expr)
2180 struct symbol *sym = expr->cast_type;
2182 sym = evaluate_expression(expr->cast_expression);
2190 if (sym->bit_size < bits_in_int && is_promoted(expr))
2198 expression_error(expr, "trying to examine bitfield type");
2204 static struct symbol *evaluate_sizeof(struct expression *expr)
2209 type = evaluate_type_information(expr);
2217 warning(expr->pos, "expression using sizeof(void)");
2223 warning(expr->pos, "expression using sizeof _Bool");
2229 warning(expr->pos, "expression using sizeof on a function");
2234 warning(expr->pos, "using sizeof on a flexible structure");
2246 base = alloc_expression(expr->pos, EXPR_SIZEOF);
2251 base = alloc_expression(expr->pos, EXPR_VALUE);
2255 size = alloc_expression(expr->pos, EXPR_CAST);
2260 expr->left = size;
2261 expr->right = base;
2262 expr->type = EXPR_BINOP;
2263 expr->op = '*';
2264 return expr->ctype = size_t_ctype;
2269 expression_error(expr, "cannot size expression");
2271 expr->type = EXPR_VALUE;
2272 expr->value = bits_to_bytes(size);
2273 expr->taint = 0;
2274 expr->ctype = size_t_ctype;
2278 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
2283 type = evaluate_type_information(expr);
2299 expression_error(expr, "expected pointer expression");
2305 expr->type = EXPR_VALUE;
2306 expr->value = bits_to_bytes(size);
2307 expr->taint = 0;
2308 expr->ctype = size_t_ctype;
2312 static struct symbol *evaluate_alignof(struct expression *expr)
2316 type = evaluate_type_information(expr);
2320 expr->type = EXPR_VALUE;
2321 expr->value = type->ctype.alignment;
2322 expr->taint = 0;
2323 expr->ctype = size_t_ctype;
2329 struct expression *expr;
2334 FOR_EACH_PTR (head, expr) {
2335 struct expression **p = THIS_ADDRESS(expr);
2337 ctype = evaluate_expression(expr);
2347 *p = cast_to(expr, integer_promotion(type));
2350 *p = cast_to(expr, &double_ctype);
2352 if (expr->ctype == &null_ctype)
2353 *p = cast_to(expr, &ptr_ctype);
2355 degenerate(expr);
2361 compatible_argument_type(expr, target, p, where);
2366 } END_FOR_EACH_PTR(expr);
2595 static void handle_list_initializer(struct expression *expr,
2601 if (expr->zero_init)
2602 free_ptr_list(&expr->expr_list);
2604 FOR_EACH_PTR(expr->expr_list, e) {
2680 expr->ctype = ctype;
2854 static struct symbol *cast_to_bool(struct expression *expr)
2856 struct expression *old = expr->cast_expression;
2865 zero = alloc_const_expression(expr->pos, 0);
2868 expr->op = SPECIAL_NOTEQUAL;
2869 ctype = usual_conversions(expr->op, old, zero,
2871 expr->type = EXPR_COMPARE;
2872 expr->left = cast_to(old, ctype);
2873 expr->right = cast_to(zero, ctype);
2875 return expr->ctype;
2878 static int cast_flags(struct expression *expr, struct expression *old)
2884 class = classify_type(expr->ctype, &t);
2951 static struct symbol *evaluate_compound_literal(struct expression *expr, struct expression *source)
2953 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2954 struct symbol *sym = expr->cast_type;
2964 expr->type = EXPR_PREOP;
2965 expr->op = '*';
2966 expr->deref = addr;
2967 expr->ctype = sym;
2971 static struct symbol *evaluate_cast(struct expression *expr)
2973 struct expression *source = expr->cast_expression;
2995 return evaluate_compound_literal(expr, source);
2997 ctype = examine_symbol_type(expr->cast_type);
2999 expr->ctype = ctype;
3000 expr->cast_type = ctype;
3007 expr->flags = cast_flags(expr, source);
3019 expression_error(expr, "cast from unknown type");
3024 if (expr->type == EXPR_FORCE_CAST)
3037 warning(expr->pos, "cast to union type");
3047 evaluate_compound_literal(expr, init);
3051 warning(expr->pos, "cast to non-scalar");
3055 warning(expr->pos, "cast from non-scalar");
3063 warning(expr->pos, "cast to %s",
3068 warning(expr->pos, "%s degrades to integer",
3071 warning(expr->pos, "cast from %s",
3092 warning(expr->pos, "cast removes address space '%s' of expression", show_as(sas));
3094 warning(expr->pos, "cast between address spaces (%s -> %s)", show_as(sas), show_as(tas));
3097 warning(expr->pos,
3105 expr->type = EXPR_VALUE;
3106 expr->ctype = &null_ctype;
3107 expr->value = 0;
3108 return expr->ctype;
3114 cast_to_bool(expr);
3127 warning(expr->pos, "cast to %s", show_typename(ctype));
3131 warning(expr->pos, "cast from %s", show_typename(source->ctype));
3144 static int evaluate_symbol_call(struct expression *expr)
3146 struct expression *fn = expr->fn;
3153 return ctype->op->evaluate(expr);
3158 static struct symbol *evaluate_call(struct expression *expr)
3162 struct expression *fn = expr->fn;
3163 struct expression_list *arglist = expr->args;
3179 expression_error(expr, "not a function %s",
3191 if (!sym->op->args(expr))
3196 args = expression_list_size(expr->args);
3199 expression_error(expr,
3205 expression_error(expr,
3209 expr->ctype = ctype->ctype.base_type;
3211 if (evaluate_symbol_call(expr))
3212 return expr->ctype;
3214 return expr->ctype;
3217 static struct symbol *evaluate_offsetof(struct expression *expr)
3219 struct expression *e = expr->down;
3220 struct symbol *ctype = expr->in;
3223 if (expr->op == '.') {
3227 expression_error(expr, "expected structure or union");
3233 expression_error(expr, "expected structure or union");
3237 field = find_identifier(expr->ident, ctype->symbol_list, &offset);
3239 expression_error(expr, "unknown member");
3243 expr->type = EXPR_VALUE;
3244 expr->flags = CEF_SET_ICE;
3245 expr->value = offset;
3246 expr->taint = 0;
3247 expr->ctype = size_t_ctype;
3250 expression_error(expr, "expected structure or union");
3256 expression_error(expr, "expected array");
3260 if (!expr->index) {
3261 expr->type = EXPR_VALUE;
3262 expr->flags = CEF_SET_ICE;
3263 expr->value = 0;
3264 expr->taint = 0;
3265 expr->ctype = size_t_ctype;
3267 struct expression *idx = expr->index, *m;
3273 expression_error(expr, "non-integer index");
3280 m = alloc_const_expression(expr->pos,
3284 expr->type = EXPR_BINOP;
3285 expr->left = idx;
3286 expr->right = m;
3287 expr->op = '*';
3288 expr->ctype = size_t_ctype;
3289 expr->flags = m->flags & idx->flags & ~CEF_CONST_MASK;
3294 *copy = *expr;
3299 expr->type = EXPR_BINOP;
3300 expr->flags = e->flags & copy->flags & ~CEF_CONST_MASK;
3301 expr->op = '+';
3302 expr->ctype = size_t_ctype;
3303 expr->left = copy;
3304 expr->right = e;
3332 static struct symbol *evaluate_generic_selection(struct expression *expr)
3339 if (!evaluate_expression(expr->control))
3341 if (!(ctrl = degenerate(expr->control)))
3346 for (map = expr->map; map; map = map->next) {
3372 res = map->expr;
3375 res = expr->def;
3377 sparse_error(expr->pos, "no generic selection for '%s'", show_typename(ctrl));
3382 *expr = *res;
3383 return evaluate_expression(expr);
3386 struct symbol *evaluate_expression(struct expression *expr)
3388 if (!expr)
3390 if (expr->ctype)
3391 return expr->ctype;
3393 switch (expr->type) {
3396 expression_error(expr, "value expression without a type");
3399 return evaluate_string(expr);
3401 return evaluate_symbol_expression(expr);
3403 evaluate_expression(expr->left);
3404 evaluate_expression(expr->right);
3405 if (!valid_subexpr_type(expr))
3407 return evaluate_binop(expr);
3409 return evaluate_logical(expr);
3411 evaluate_expression(expr->left);
3412 if (!evaluate_expression(expr->right))
3414 return evaluate_comma(expr);
3416 evaluate_expression(expr->left);
3417 evaluate_expression(expr->right);
3418 if (!valid_subexpr_type(expr))
3420 return evaluate_compare(expr);
3422 evaluate_expression(expr->left);
3423 evaluate_expression(expr->right);
3424 if (!valid_subexpr_type(expr))
3426 return evaluate_assignment(expr);
3428 if (!evaluate_expression(expr->unop))
3430 return evaluate_preop(expr);
3432 if (!evaluate_expression(expr->unop))
3434 return evaluate_postop(expr);
3438 return evaluate_cast(expr);
3440 return evaluate_sizeof(expr);
3442 return evaluate_ptrsizeof(expr);
3444 return evaluate_alignof(expr);
3446 return evaluate_member_dereference(expr);
3448 return evaluate_call(expr);
3451 return evaluate_conditional_expression(expr);
3453 expr->ctype = evaluate_statement(expr->statement);
3454 return expr->ctype;
3457 expr->ctype = &ptr_ctype;
3458 check_label_declaration(expr->pos, expr->label_symbol);
3463 evaluate_symbol(expr->symbol);
3465 expr->ctype = &type_ctype;
3469 return evaluate_offsetof(expr);
3472 return evaluate_generic_selection(expr);
3479 expression_error(expr, "internal front-end error: initializer in expression");
3482 expression_error(expr, "internal front-end error: SLICE re-evaluated");
3582 struct expression *expr = stmt->expression;
3585 evaluate_expression(expr);
3589 if (expr && expr->ctype && !is_void_type(expr->ctype))
3590 expression_error(expr, "return expression in %s function", rettype?"void":"typeless");
3591 if (expr && Wreturn_void)
3596 if (!expr) {
3600 if (!expr->ctype)
3602 compatible_assignment_types(expr, rettype, &stmt->expression, "return expression");
3702 struct expression *expr = op->constraint;
3703 const char *constraint = expr->string->data;
3706 expression_error(expr, "output constraint is not an assignment constraint (\"%s\")", constraint);
3711 struct expression *expr = op->constraint;
3712 const char *constraint = expr->string->data;
3715 expression_error(expr, "input constraint with assignment (\"%s\")", constraint);
3721 struct expression *expr = op->expr;
3725 addr = alloc_expression(expr->pos, EXPR_PREOP);
3727 addr->unop = expr;
3730 op->expr = addr;
3732 evaluate_expression(op->expr);
3733 degenerate(op->expr);
3739 struct expression *expr;
3756 expr = op->expr;
3757 if (!evaluate_expression(expr))
3759 if (!lvalue_expression(expr))
3760 warning(expr->pos, "asm output is not an lvalue");
3761 evaluate_assign_to(expr, expr->ctype);
3775 if (!evaluate_expression(op->expr))
3780 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
3781 if (!expr) {
3785 if (expr->type == EXPR_STRING)
3787 expression_error(expr, "asm clobber is not a string");
3788 } END_FOR_EACH_PTR(expr);