From a3a20f09e27cf92493bdb92f6a96c29a8ab538d1 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 15 Aug 2021 16:55:30 +0100 Subject: [PATCH] `or_return` built-in procedure --- src/check_builtin.cpp | 122 ++++++++++++++++++++++++++++++++-- src/checker_builtin_procs.hpp | 2 + src/llvm_backend_proc.cpp | 2 + src/llvm_backend_utility.cpp | 42 ++++++++++++ 4 files changed, 161 insertions(+), 7 deletions(-) diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 846bea296..77c4ee453 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -48,6 +48,17 @@ BuiltinTypeIsProc *builtin_type_is_procs[BuiltinProc__type_simple_boolean_end - }; +void check_or_else_right_type(CheckerContext *c, Ast *expr, String const &name, Type *right_type) { + if (right_type == nullptr) { + return; + } + if (!is_type_boolean(right_type) && !type_has_nil(right_type)) { + gbString str = type_to_string(right_type); + error(expr, "'%.*s' expects an \"optional ok\" like value, or an n-valued expression where the last value is either a boolean or can be compared against 'nil', got %s", LIT(name), str); + gb_string_free(str); + } +} + void check_or_else_split_types(CheckerContext *c, Operand *x, String const &name, Type **left_type_, Type **right_type_) { Type *left_type = nullptr; Type *right_type = nullptr; @@ -70,15 +81,11 @@ void check_or_else_split_types(CheckerContext *c, Operand *x, String const &name if (left_type_) *left_type_ = left_type; if (right_type_) *right_type_ = right_type; - if (!is_type_boolean(right_type)) { - gbString str = type_to_string(right_type); - error(x->expr, "'%.*s' expects an \"optional ok\" like value, got %s", LIT(name), str); - gb_string_free(str); - } + check_or_else_right_type(c, x->expr, name, right_type); } -void check_try_expr_no_value_error(CheckerContext *c, String const &name, Operand const &x, Type *type_hint) { +void check_or_else_expr_no_value_error(CheckerContext *c, String const &name, Operand const &x, Type *type_hint) { // TODO(bill): better error message gbString t = type_to_string(x.type); error(x.expr, "'%.*s' does not return a value, value is of type %s", LIT(name), t); @@ -108,6 +115,33 @@ void check_try_expr_no_value_error(CheckerContext *c, String const &name, Operan } +void check_or_return_split_types(CheckerContext *c, Operand *x, String const &name, Type **left_type_, Type **right_type_) { + Type *left_type = nullptr; + Type *right_type = nullptr; + if (x->type->kind == Type_Tuple) { + auto const &vars = x->type->Tuple.variables; + auto lhs = array_slice(vars, 0, vars.count-1); + auto rhs = vars[vars.count-1]; + if (lhs.count == 1) { + left_type = lhs[0]->type; + } else if (lhs.count != 0) { + left_type = alloc_type_tuple(); + left_type->Tuple.variables = array_make_from_ptr(lhs.data, lhs.count, lhs.count); + } + + right_type = rhs->type; + } else { + check_promote_optional_ok(c, x, &left_type, &right_type); + } + + if (left_type_) *left_type_ = left_type; + if (right_type_) *right_type_ = right_type; + + check_or_else_right_type(c, x->expr, name, right_type); +} + + + bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) { ast_node(ce, CallExpr, call); if (ce->inlining != ProcInlining_none) { @@ -146,6 +180,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 break; case BuiltinProc_or_else: + case BuiltinProc_or_return: // NOTE(bill): The arguments may be multi-expr break; @@ -1830,7 +1865,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 if (left_type != nullptr) { check_assignment(c, &y, left_type, builtin_name); } else { - check_try_expr_no_value_error(c, builtin_name, x, type_hint); + check_or_else_expr_no_value_error(c, builtin_name, x, type_hint); } if (left_type == nullptr) { @@ -1841,6 +1876,79 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 return true; } + case BuiltinProc_or_return: { + GB_ASSERT(ce->args.count == 1); + Ast *arg = ce->args[0]; + + Operand x = {}; + check_multi_expr_with_type_hint(c, &x, arg, type_hint); + if (x.mode == Addressing_Invalid) { + operand->mode = Addressing_Value; + operand->type = t_invalid; + return false; + } + + Type *left_type = nullptr; + Type *right_type = nullptr; + check_or_return_split_types(c, &x, builtin_name, &left_type, &right_type); + add_type_and_value(&c->checker->info, arg, x.mode, x.type, x.value); + + if (right_type == nullptr) { + check_or_else_expr_no_value_error(c, builtin_name, x, type_hint); + } else { + Type *proc_type = base_type(c->curr_proc_sig); + GB_ASSERT(proc_type->kind == Type_Proc); + Type *result_type = proc_type->Proc.results; + if (result_type == nullptr) { + error(call, "'%.*s' requires the current procedure to have at least one return value", LIT(builtin_name)); + } else { + GB_ASSERT(result_type->kind == Type_Tuple); + + auto const &vars = result_type->Tuple.variables; + Type *end_type = vars[vars.count-1]->type; + + if (vars.count > 1) { + if (!proc_type->Proc.has_named_results) { + error(call, "'%.*s' within a procedure with more than 1 return value requires that the return values are named, allowing for early return", LIT(builtin_name)); + } + } + + Operand rhs = {}; + rhs.type = right_type; + rhs.mode = Addressing_Value; + + // TODO(bill): better error message + if (!check_is_assignable_to(c, &rhs, end_type)) { + gbString a = type_to_string(right_type); + gbString b = type_to_string(end_type); + gbString ret_type = type_to_string(result_type); + error(call, "Cannot assign end value of type '%s' to '%s' in '%.*s'", a, b, LIT(builtin_name)); + if (vars.count == 1) { + error_line("\tProcedure return value type: %s\n", ret_type); + } else { + error_line("\tProcedure return value types: (%s)\n", ret_type); + } + gb_string_free(ret_type); + gb_string_free(b); + gb_string_free(a); + } + } + } + + operand->type = left_type; + if (left_type != nullptr) { + operand->mode = Addressing_Value; + } else { + operand->mode = Addressing_NoValue; + } + + if (c->curr_proc_sig == nullptr) { + error(call, "'%.*s' can only be used within a procedure", LIT(builtin_name)); + } + + return true; + } + case BuiltinProc_simd_vector: { Operand x = {}; Operand y = {}; diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index a25657abe..bd105f0d7 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -34,6 +34,7 @@ enum BuiltinProcId { BuiltinProc_soa_unzip, BuiltinProc_or_else, + BuiltinProc_or_return, BuiltinProc_DIRECTIVE, // NOTE(bill): This is used for specialized hash-prefixed procedures @@ -266,6 +267,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("soa_unzip"), 1, false, Expr_Expr, BuiltinProcPkg_builtin}, {STR_LIT("or_else"), 2, false, Expr_Expr, BuiltinProcPkg_builtin}, + {STR_LIT("or_return"), 1, false, Expr_Expr, BuiltinProcPkg_builtin}, {STR_LIT(""), 0, true, Expr_Expr, BuiltinProcPkg_builtin}, // DIRECTIVE diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 5b922de4b..b3178e008 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -1254,6 +1254,8 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, case BuiltinProc_or_else: return lb_emit_or_else(p, ce->args[0], ce->args[1], tv); + case BuiltinProc_or_return: + return lb_emit_or_return(p, ce->args[0], tv); // "Intrinsics" diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index 5fc5389c9..406b4fc2e 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -364,6 +364,48 @@ lbValue lb_emit_or_else(lbProcedure *p, Ast *arg, Ast *else_expr, TypeAndValue c return res; } +void lb_build_return_stmt(lbProcedure *p, Slice const &return_results); +void lb_build_return_stmt_internal(lbProcedure *p, lbValue const &res); + +lbValue lb_emit_or_return(lbProcedure *p, Ast *arg, TypeAndValue const &tv) { + lbValue lhs = {}; + lbValue rhs = {}; + lb_emit_try_lhs_rhs(p, arg, tv, &lhs, &rhs); + + lbBlock *return_block = lb_create_block(p, "or_return.return"); + lbBlock *continue_block = lb_create_block(p, "or_return.continue"); + + lb_emit_if(p, lb_emit_try_has_value(p, rhs), continue_block, return_block); + lb_start_block(p, return_block); + { + Type *proc_type = base_type(p->type); + Type *results = proc_type->Proc.results; + GB_ASSERT(results != nullptr && results->kind == Type_Tuple); + TypeTuple *tuple = &results->Tuple; + + GB_ASSERT(tuple->variables.count != 0); + + Entity *end_entity = tuple->variables[tuple->variables.count-1]; + rhs = lb_emit_conv(p, rhs, end_entity->type); + if (p->type->Proc.has_named_results) { + GB_ASSERT(end_entity->token.string.len != 0); + + // NOTE(bill): store the named values before returning + lbValue found = map_must_get(&p->module->values, hash_entity(end_entity)); + lb_emit_store(p, found, rhs); + + lb_build_return_stmt(p, {}); + } else { + GB_ASSERT(tuple->variables.count == 1); + lb_build_return_stmt_internal(p, rhs); + } + } + lb_start_block(p, continue_block); + if (tv.type != nullptr) { + return lb_emit_conv(p, lhs, tv.type); + } + return {}; +} void lb_emit_increment(lbProcedure *p, lbValue addr) {