@@ -3138,6 +3138,29 @@ static IrInstruction *ir_gen_value(IrBuilder *irb, Scope *scope, AstNode *node,
3138
3138
return ir_lval_wrap(irb, scope, value, lval);
3139
3139
}
3140
3140
3141
+ static IrInstruction *ir_gen_ptr(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval,
3142
+ IrInstruction *result_loc, IrInstruction *ptr)
3143
+ {
3144
+ switch (lval) {
3145
+ case LValPtr:
3146
+ assert(result_loc == nullptr);
3147
+ return ptr;
3148
+ case LValNone:
3149
+ return ir_build_load_ptr(irb, scope, node, ptr, result_loc);
3150
+ case LValErrorUnion: {
3151
+ // ptr points to an error union;
3152
+ // result_loc points to the result payload
3153
+ // must return the error code
3154
+ IrInstruction *payload_ptr = ir_build_unwrap_err_payload(irb, scope, node, ptr, false);
3155
+ ir_build_load_ptr(irb, scope, node, payload_ptr, result_loc);
3156
+ return ir_build_unwrap_err_code(irb, scope, node, ptr);
3157
+ }
3158
+ case LValOptional:
3159
+ zig_panic("TODO");
3160
+ }
3161
+ zig_unreachable();
3162
+ }
3163
+
3141
3164
static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval,
3142
3165
IrInstruction *result_loc)
3143
3166
{
@@ -3735,10 +3758,7 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
3735
3758
ZigVar *var = find_variable(irb->codegen, scope, variable_name, &crossed_fndef_scope);
3736
3759
if (var) {
3737
3760
IrInstruction *var_ptr = ir_build_var_ptr_x(irb, scope, node, var, crossed_fndef_scope);
3738
- if (lval == LValPtr)
3739
- return var_ptr;
3740
- IrInstruction *loaded = ir_build_load_ptr(irb, scope, node, var_ptr, result_loc);
3741
- return ir_lval_wrap(irb, scope, loaded, lval);
3761
+ return ir_gen_ptr(irb, scope, node, lval, result_loc, var_ptr);
3742
3762
}
3743
3763
3744
3764
Tld *tld = find_decl(irb->codegen, scope, variable_name);
0 commit comments