@@ -3832,7 +3832,9 @@ static IrInstruction *ir_gen_array_access(IrBuilder *irb, Scope *scope, AstNode
3832
3832
return ir_build_load_result(irb, scope, node, ptr_instruction, result_loc);
3833
3833
}
3834
3834
3835
- static IrInstruction *ir_gen_field_access(IrBuilder *irb, Scope *scope, AstNode *node) {
3835
+ static IrInstruction *ir_gen_field_access(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval,
3836
+ IrInstruction *result_loc)
3837
+ {
3836
3838
assert(node->type == NodeTypeFieldAccessExpr);
3837
3839
3838
3840
AstNode *container_ref_node = node->data.field_access_expr.struct_expr;
@@ -3842,7 +3844,8 @@ static IrInstruction *ir_gen_field_access(IrBuilder *irb, Scope *scope, AstNode
3842
3844
if (container_ref_instruction == irb->codegen->invalid_instruction)
3843
3845
return container_ref_instruction;
3844
3846
3845
- return ir_build_field_ptr(irb, scope, node, container_ref_instruction, nullptr, field_name);
3847
+ IrInstruction *field_ptr = ir_build_field_ptr(irb, scope, node, container_ref_instruction, nullptr, field_name);
3848
+ return ir_gen_ptr(irb, scope, node, lval, result_loc, field_ptr);
3846
3849
}
3847
3850
3848
3851
static IrInstruction *ir_gen_overflow_op(IrBuilder *irb, Scope *scope, AstNode *node, IrOverflowOp op) {
@@ -5856,7 +5859,7 @@ static IrInstruction *ir_gen_asm_expr(IrBuilder *irb, Scope *scope, AstNode *nod
5856
5859
return ir_build_asm(irb, scope, node, input_list, output_types, output_vars, return_count, is_volatile);
5857
5860
}
5858
5861
5859
- static IrInstruction *ir_gen_if_optional_expr(IrBuilder *irb, Scope *scope, AstNode *node,
5862
+ static IrInstruction *ir_gen_if_optional_expr(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval,
5860
5863
IrInstruction *result_loc)
5861
5864
{
5862
5865
assert(node->type == NodeTypeIfOptional);
@@ -5897,20 +5900,17 @@ static IrInstruction *ir_gen_if_optional_expr(IrBuilder *irb, Scope *scope, AstN
5897
5900
ZigVar *var = ir_create_var(irb, node, subexpr_scope,
5898
5901
var_symbol, is_const, is_const, is_shadowable, is_comptime);
5899
5902
5900
- IrInstruction *payload_alloca = ir_build_alloca_src(irb, scope, node, nullptr, nullptr, "optional_payload");
5901
- IrInstruction *var_ptr_value = ir_build_unwrap_maybe(irb, subexpr_scope, node, maybe_val_ptr, false);
5902
- IrInstruction *var_value = var_is_ptr ? var_ptr_value : ir_build_load_result(irb, subexpr_scope, node,
5903
- var_ptr_value, payload_alloca);
5904
- ir_build_store_result(irb, subexpr_scope, node, payload_alloca, var_value);
5905
- ir_build_var_decl_src(irb, subexpr_scope, node, var, var_type, nullptr, payload_alloca);
5903
+ IrInstruction *payload_ptr = ir_build_unwrap_maybe(irb, subexpr_scope, node, maybe_val_ptr, false);
5904
+ IrInstruction *var_ptr = var_is_ptr ?
5905
+ ir_build_ref(irb, subexpr_scope, node, payload_ptr, true, false) : payload_ptr;
5906
+ ir_build_var_decl_src(irb, subexpr_scope, node, var, var_type, nullptr, var_ptr);
5906
5907
var_scope = var->child_scope;
5907
5908
} else {
5908
5909
var_scope = subexpr_scope;
5909
5910
}
5910
5911
IrInstruction *then_expr_result = ir_gen_node(irb, then_node, var_scope, LValNone, result_loc);
5911
5912
if (then_expr_result == irb->codegen->invalid_instruction)
5912
5913
return then_expr_result;
5913
- IrBasicBlock *after_then_block = irb->current_basic_block;
5914
5914
if (!instr_is_unreachable(then_expr_result))
5915
5915
ir_mark_gen(ir_build_br(irb, scope, node, endif_block, is_comptime));
5916
5916
@@ -5923,19 +5923,11 @@ static IrInstruction *ir_gen_if_optional_expr(IrBuilder *irb, Scope *scope, AstN
5923
5923
} else {
5924
5924
else_expr_result = ir_build_const_void(irb, scope, node);
5925
5925
}
5926
- IrBasicBlock *after_else_block = irb->current_basic_block;
5927
5926
if (!instr_is_unreachable(else_expr_result))
5928
5927
ir_mark_gen(ir_build_br(irb, scope, node, endif_block, is_comptime));
5929
5928
5930
5929
ir_set_cursor_at_end_and_append_block(irb, endif_block);
5931
- IrInstruction **incoming_values = allocate<IrInstruction *>(2);
5932
- incoming_values[0] = then_expr_result;
5933
- incoming_values[1] = else_expr_result;
5934
- IrBasicBlock **incoming_blocks = allocate<IrBasicBlock *>(2);
5935
- incoming_blocks[0] = after_then_block;
5936
- incoming_blocks[1] = after_else_block;
5937
-
5938
- return ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values);
5930
+ return ir_gen_lval_ptr(irb, scope, node, lval, result_loc);
5939
5931
}
5940
5932
5941
5933
static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *node, IrInstruction *result_loc) {
@@ -7276,15 +7268,7 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
7276
7268
case NodeTypeReturnExpr:
7277
7269
return ir_gen_return(irb, scope, node, lval, result_loc);
7278
7270
case NodeTypeFieldAccessExpr:
7279
- {
7280
- IrInstruction *ptr_instruction = ir_gen_field_access(irb, scope, node);
7281
- if (ptr_instruction == irb->codegen->invalid_instruction)
7282
- return ptr_instruction;
7283
- if (lval == LValPtr)
7284
- return ptr_instruction;
7285
-
7286
- return ir_build_load_ptr(irb, scope, node, ptr_instruction, nullptr);
7287
- }
7271
+ return ir_gen_field_access(irb, scope, node, lval, result_loc);
7288
7272
case NodeTypePtrDeref: {
7289
7273
AstNode *expr_node = node->data.ptr_deref_expr.target;
7290
7274
IrInstruction *value = ir_gen_node(irb, expr_node, scope, lval, nullptr);
@@ -7325,7 +7309,7 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
7325
7309
case NodeTypeIfErrorExpr:
7326
7310
return ir_lval_wrap(irb, scope, ir_gen_if_err_expr(irb, scope, node, result_loc), lval);
7327
7311
case NodeTypeIfOptional:
7328
- return ir_lval_wrap(irb, scope, ir_gen_if_optional_expr(irb, scope, node, result_loc), lval );
7312
+ return ir_gen_if_optional_expr(irb, scope, node, lval, result_loc );
7329
7313
case NodeTypeSwitchExpr:
7330
7314
return ir_lval_wrap(irb, scope, ir_gen_switch_expr(irb, scope, node, result_loc), lval);
7331
7315
case NodeTypeCompTime:
0 commit comments