From 25e74cb385dc353bcf0e56b35a6f1c7c8b13267e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 4 Feb 2016 12:59:06 -0700 Subject: ability to explicitly cast bool to int --- src/analyze.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/analyze.cpp') diff --git a/src/analyze.cpp b/src/analyze.cpp index 38bc656a1f..7e63a43366 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -3610,6 +3610,10 @@ static void eval_const_expr_implicit_cast(CodeGen *g, AstNode *node, AstNode *ex bignum_cast_to_int(&const_val->data.x_bignum, &other_val->data.x_bignum); const_val->ok = true; break; + case CastOpBoolToInt: + bignum_init_unsigned(&const_val->data.x_bignum, other_val->data.x_bool ? 1 : 0); + const_val->ok = true; + break; } } @@ -3643,6 +3647,15 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B return wanted_type; } + // explicit cast from bool to int + if (wanted_type->id == TypeTableEntryIdInt && + actual_type->id == TypeTableEntryIdBool) + { + node->data.fn_call_expr.cast_op = CastOpBoolToInt; + eval_const_expr_implicit_cast(g, node, expr_node); + return wanted_type; + } + // explicit cast from pointer to isize or usize if ((wanted_type == g->builtin_types.entry_isize || wanted_type == g->builtin_types.entry_usize) && actual_type->id == TypeTableEntryIdPointer) -- cgit v1.2.3