From a4e19f94f10445273b977e21e721d236a37c5cf4 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 31 Jan 2016 21:22:05 -0700 Subject: support casting between floats --- src/analyze.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'src/analyze.cpp') diff --git a/src/analyze.cpp b/src/analyze.cpp index 785bad9ff2..8942746e2d 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1681,6 +1681,14 @@ static bool types_match_with_implicit_cast(CodeGen *g, TypeTableEntry *expected_ return true; } + // implicit float widening conversion + if (expected_type->id == TypeTableEntryIdFloat && + actual_type->id == TypeTableEntryIdFloat && + expected_type->size_in_bits >= actual_type->size_in_bits) + { + return true; + } + // implicit constant sized array to unknown size array conversion if (expected_type->id == TypeTableEntryIdStruct && expected_type->data.structure.is_unknown_size_array && @@ -3366,7 +3374,7 @@ static void eval_const_expr_implicit_cast(CodeGen *g, AstNode *node, AstNode *ex case CastOpNoCast: zig_unreachable(); case CastOpNoop: - case CastOpIntWidenOrShorten: + case CastOpWidenOrShorten: case CastOpPointerReinterpret: *const_val = *other_val; break; @@ -3477,11 +3485,13 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B return wanted_type; } - // explicit cast from any int to any other int - if (wanted_type->id == TypeTableEntryIdInt && - actual_type->id == TypeTableEntryIdInt) + // explicit widening or shortening cast + if ((wanted_type->id == TypeTableEntryIdInt && + actual_type->id == TypeTableEntryIdInt) || + (wanted_type->id == TypeTableEntryIdFloat && + actual_type->id == TypeTableEntryIdFloat)) { - node->data.fn_call_expr.cast_op = CastOpIntWidenOrShorten; + node->data.fn_call_expr.cast_op = CastOpWidenOrShorten; eval_const_expr_implicit_cast(g, node, expr_node); return wanted_type; } -- cgit v1.2.3