diff options
| author | gingerBill <bill@gingerbill.org> | 2020-03-24 14:31:09 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2020-03-24 14:31:09 +0000 |
| commit | 1e375ba8de75f2649cd66089b87d273b749c575f (patch) | |
| tree | 61d05c65cbb2c36c780d96a77188572dc76165c6 /src | |
| parent | 0730e01b24bfba4bd91f224dd4ecbeef23bd6c1c (diff) | |
| parent | 5cbb266ef5666c1df5d25d3afe23e4a777abd22b (diff) | |
Merge branch 'master' into llvm-integration
Diffstat (limited to 'src')
| -rw-r--r-- | src/check_stmt.cpp | 8 | ||||
| -rw-r--r-- | src/entity.cpp | 2 | ||||
| -rw-r--r-- | src/ir.cpp | 35 | ||||
| -rw-r--r-- | src/types.cpp | 1 |
4 files changed, 27 insertions, 19 deletions
diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 67172d951..e9b6869c8 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -1155,10 +1155,12 @@ void check_type_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) { } } + bool is_reference = false; + if (is_ptr && cc->list.count == 1 && case_type != nullptr) { - case_type = alloc_type_pointer(case_type); + is_reference = true; } if (cc->list.count > 1) { @@ -1173,7 +1175,9 @@ void check_type_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) { { Entity *tag_var = alloc_entity_variable(ctx->scope, lhs->Ident.token, case_type, EntityState_Resolved); tag_var->flags |= EntityFlag_Used; - tag_var->flags |= EntityFlag_Value; + if (!is_reference) { + tag_var->flags |= EntityFlag_Value; + } add_entity(ctx->checker, ctx->scope, lhs, tag_var); add_entity_use(ctx, lhs, tag_var); add_implicit_entity(ctx, stmt, tag_var); diff --git a/src/entity.cpp b/src/entity.cpp index dbe4f926c..a64e767d6 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -32,7 +32,7 @@ String const entity_strings[] = { #undef ENTITY_KIND }; -enum EntityFlag { +enum EntityFlag : u32 { EntityFlag_Visited = 1<<0, EntityFlag_Used = 1<<1, EntityFlag_Using = 1<<2, diff --git a/src/ir.cpp b/src/ir.cpp index e938049c4..f51538a2e 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -9530,13 +9530,16 @@ void ir_build_range_tuple(irProcedure *proc, Ast *expr, Type *val0_type, Type *v void ir_store_type_case_implicit(irProcedure *proc, Ast *clause, irValue *value) { Entity *e = implicit_entity_of_node(clause); GB_ASSERT(e != nullptr); -#if 1 - irValue *x = ir_add_local(proc, e, nullptr, false); - ir_emit_store(proc, x, value); -#else - irValue *x = ir_address_from_load_or_generate_local(proc, value); - ir_module_add_value(proc->module, e, x); -#endif + + if (e->flags & EntityFlag_Value) { + // by value + irValue *x = ir_add_local(proc, e, nullptr, false); + GB_ASSERT(are_types_identical(ir_type(value), e->type)); + ir_emit_store(proc, x, value); + } else { + // by reference + ir_module_add_value(proc->module, e, value); + } } void ir_type_case_body(irProcedure *proc, Ast *label, Ast *clause, irBlock *body, irBlock *done) { @@ -10477,14 +10480,8 @@ void ir_build_stmt_internal(irProcedure *proc, Ast *node) { ir_start_block(proc, body); // bool any_or_not_ptr = is_type_any(type_deref(parent_type)) || !is_parent_ptr; - bool any_or_not_ptr = !is_parent_ptr; if (cc->list.count == 1) { - Type *ct = case_entity->type; - if (any_or_not_ptr) { - ct = alloc_type_pointer(ct); - } - GB_ASSERT_MSG(is_type_pointer(ct), "%s", type_to_string(ct)); irValue *data = nullptr; if (switch_kind == TypeSwitch_Union) { data = union_data; @@ -10492,9 +10489,17 @@ void ir_build_stmt_internal(irProcedure *proc, Ast *node) { irValue *any_data = ir_emit_load(proc, ir_emit_struct_ep(proc, parent_ptr, 0)); data = any_data; } - value = ir_emit_conv(proc, data, ct); - if (any_or_not_ptr) { + Type *ct = case_entity->type; + Type *ct_ptr = alloc_type_pointer(ct); + + + value = ir_emit_conv(proc, data, ct_ptr); + + if (case_entity->flags & EntityFlag_Value) { + // by value value = ir_emit_load(proc, value); + } else { + // by reference } } diff --git a/src/types.cpp b/src/types.cpp index 4c7f07cc7..86be0d38f 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -3490,4 +3490,3 @@ gbString type_to_string(Type *type) { return write_type_to_string(gb_string_make(heap_allocator(), ""), type); } - |