Browse Source

Correct update propagation of type for ternary if expressions

gingerBill 3 years ago
parent
commit
48de1a01a9
4 changed files with 30 additions and 15 deletions
  1. 10 3
      src/check_expr.cpp
  2. 2 0
      src/check_stmt.cpp
  3. 1 1
      src/llvm_backend_const.cpp
  4. 17 11
      src/llvm_backend_expr.cpp

+ 10 - 3
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;

+ 2 - 0
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);
 				}

+ 1 - 1
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,

+ 17 - 11
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;