aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-10-22 13:14:19 +0100
committergingerBill <bill@gingerbill.org>2021-10-22 13:14:19 +0100
commit48de1a01a9bc5cdc2edda363681455078bae3e52 (patch)
tree646515681912d3d27fa1f682ed60c2ab1d249faf
parent1405420935ea2a343c8bb50ed243a4568aa217c1 (diff)
Correct update propagation of type for ternary if expressions
-rw-r--r--src/check_expr.cpp13
-rw-r--r--src/check_stmt.cpp2
-rw-r--r--src/llvm_backend_const.cpp2
-rw-r--r--src/llvm_backend_expr.cpp28
4 files changed, 30 insertions, 15 deletions
diff --git a/src/check_expr.cpp b/src/check_expr.cpp
index 513144f11..04ba28b60 100644
--- a/src/check_expr.cpp
+++ b/src/check_expr.cpp
@@ -829,7 +829,7 @@ void check_assignment(CheckerContext *c, Operand *operand, Type *type, String co
operand->mode = Addressing_Invalid;
}
-
+ convert_to_typed(c, operand, type);
return;
}
@@ -837,6 +837,8 @@ void check_assignment(CheckerContext *c, Operand *operand, Type *type, String co
if (operand->mode == Addressing_Type && is_type_typeid(type)) {
add_type_info_type(c, operand->type);
add_type_and_value(c->info, operand->expr, Addressing_Value, type, exact_value_typeid(operand->type));
+ } else {
+ convert_to_typed(c, operand, type);
}
} else {
gbString expr_str = expr_to_string(operand->expr);
@@ -3014,11 +3016,15 @@ void update_untyped_expr_type(CheckerContext *c, Ast *e, Type *type, bool final)
if (type != nullptr && type != t_invalid) {
if (e->tav.type == nullptr || e->tav.type == t_invalid) {
add_type_and_value(c->info, e, e->tav.mode, type ? type : e->tav.type, e->tav.value);
+ if (e->kind == Ast_TernaryIfExpr) {
+ goto propagate;
+ }
}
}
return;
}
+propagate:;
switch (e->kind) {
case_ast_node(ue, UnaryExpr, e);
if (old->value.kind != ExactValue_Invalid) {
@@ -6742,13 +6748,14 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
type = y.type;
}
- o->type = type;
+ o->type = x.type;
o->mode = Addressing_Value;
+ o->expr = node;
if (type_hint != nullptr && is_type_untyped(type)) {
if (check_cast_internal(c, &x, type_hint) &&
check_cast_internal(c, &y, type_hint)) {
+ convert_to_typed(c, o, type_hint);
update_untyped_expr_type(c, node, type_hint, !is_type_untyped(type_hint));
- o->type = type_hint;
}
}
case_end;
diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp
index 103ffa071..4dbc76578 100644
--- a/src/check_stmt.cpp
+++ b/src/check_stmt.cpp
@@ -1728,6 +1728,8 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
Entity *e = pt->results->Tuple.variables[i];
Operand *o = &operands[i];
check_assignment(ctx, o, e->type, str_lit("return statement"));
+ convert_to_typed(ctx, o, e->type);
+
if (is_type_untyped(o->type)) {
update_untyped_expr_type(ctx, o->expr, e->type, true);
}
diff --git a/src/llvm_backend_const.cpp b/src/llvm_backend_const.cpp
index 68050e0ce..18b6ee6f6 100644
--- a/src/llvm_backend_const.cpp
+++ b/src/llvm_backend_const.cpp
@@ -329,7 +329,7 @@ LLVMValueRef lb_big_int_to_llvm(lbModule *m, Type *original_type, BigInt const *
debug_print_big_int(a);
gb_printf_err("%s -> %tu\n", type_to_string(original_type), sz);;
}
- GB_ASSERT_MSG(sz >= max_count, "max_count: %tu, sz: %tu, written: %tu", max_count, sz, written);
+ GB_ASSERT_MSG(sz >= max_count, "max_count: %tu, sz: %tu, written: %tu, type %s", max_count, sz, written, type_to_string(original_type));
GB_ASSERT(gb_size_of(rop64) >= sz);
mp_err err = mp_pack(rop, sz, &written,
diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp
index 763afacc2..caeae2fb0 100644
--- a/src/llvm_backend_expr.cpp
+++ b/src/llvm_backend_expr.cpp
@@ -762,6 +762,12 @@ lbValue lb_build_binary_expr(lbProcedure *p, Ast *expr) {
case Token_Xor:
case Token_AndNot: {
Type *type = default_type(tv.type);
+ if (is_type_typed(be->left->tav.type) && is_type_untyped(be->right->tav.type)) {
+ be->right->tav.type = be->left->tav.type;
+ } else if (is_type_untyped(be->left->tav.type) && is_type_typed(be->right->tav.type)) {
+ be->left->tav.type = type_of_expr(be->right);
+ }
+
lbValue left = lb_build_expr(p, be->left);
lbValue right = lb_build_expr(p, be->right);
return lb_emit_arith(p, be->op.kind, left, right, type);
@@ -2247,17 +2253,18 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) {
TokenPos expr_pos = ast_token(expr).pos;
TypeAndValue tv = type_and_value_of_expr(expr);
+ Type *type = type_of_expr(expr);
GB_ASSERT_MSG(tv.mode != Addressing_Invalid, "invalid expression '%s' (tv.mode = %d, tv.type = %s) @ %s\n Current Proc: %.*s : %s", expr_to_string(expr), tv.mode, type_to_string(tv.type), token_pos_to_string(expr_pos), LIT(p->name), type_to_string(p->type));
if (tv.value.kind != ExactValue_Invalid) {
// NOTE(bill): The commented out code below is just for debug purposes only
// GB_ASSERT_MSG(!is_type_untyped(tv.type), "%s @ %s\n%s", type_to_string(tv.type), token_pos_to_string(expr_pos), expr_to_string(expr));
- // if (is_type_untyped(tv.type)) {
+ // if (is_type_untyped(type)) {
// gb_printf_err("%s %s\n", token_pos_to_string(expr_pos), expr_to_string(expr));
// }
// NOTE(bill): Short on constant values
- return lb_const_value(p->module, tv.type, tv.value);
+ return lb_const_value(p->module, type, tv.value);
}
#if 0
@@ -2288,12 +2295,12 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) {
case_ast_node(u, Undef, expr)
lbValue res = {};
- if (is_type_untyped(tv.type)) {
+ if (is_type_untyped(type)) {
res.value = nullptr;
res.type = t_untyped_undef;
} else {
- res.value = LLVMGetUndef(lb_type(m, tv.type));
- res.type = tv.type;
+ res.value = LLVMGetUndef(lb_type(m, type));
+ res.type = type;
}
return res;
case_end;
@@ -2334,7 +2341,7 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) {
TypeAndValue tav = type_and_value_of_expr(expr);
GB_ASSERT(tav.mode == Addressing_Constant);
- return lb_const_value(p->module, tv.type, tv.value);
+ return lb_const_value(p->module, type, tv.value);
case_end;
case_ast_node(se, SelectorCallExpr, expr);
@@ -2407,7 +2414,6 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) {
case_ast_node(ta, TypeAssertion, expr);
TokenPos pos = ast_token(expr).pos;
- Type *type = tv.type;
lbValue e = lb_build_expr(p, ta->expr);
Type *t = type_deref(e.type);
if (is_type_union(t)) {
@@ -2427,16 +2433,16 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) {
lbValue e = lb_build_expr(p, tc->expr);
switch (tc->token.kind) {
case Token_cast:
- return lb_emit_conv(p, e, tv.type);
+ return lb_emit_conv(p, e, type);
case Token_transmute:
- return lb_emit_transmute(p, e, tv.type);
+ return lb_emit_transmute(p, e, type);
}
GB_PANIC("Invalid AST TypeCast");
case_end;
case_ast_node(ac, AutoCast, expr);
lbValue value = lb_build_expr(p, ac->expr);
- return lb_emit_conv(p, value, tv.type);
+ return lb_emit_conv(p, value, type);
case_end;
case_ast_node(ue, UnaryExpr, expr);
@@ -2446,7 +2452,7 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) {
default:
{
lbValue v = lb_build_expr(p, ue->expr);
- return lb_emit_unary_arith(p, ue->op.kind, v, tv.type);
+ return lb_emit_unary_arith(p, ue->op.kind, v, type);
}
}
case_end;