diff options
| author | g-w1 <58830309+g-w1@users.noreply.github.com> | 2020-12-28 08:42:22 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-28 15:42:22 +0200 |
| commit | c234761eddc2fe932046767756b9e6df82a73b65 (patch) | |
| tree | 959a1873c66019485873a425fd5a915b490871ed | |
| parent | fffb0904f898d47800aa418085dc6064d02c32fe (diff) | |
| download | zig-c234761eddc2fe932046767756b9e6df82a73b65.tar.gz zig-c234761eddc2fe932046767756b9e6df82a73b65.zip | |
stage2: make Alloc(Inferred) have mutabality info (#7570)
| -rw-r--r-- | src/astgen.zig | 6 | ||||
| -rw-r--r-- | src/zir.zig | 12 | ||||
| -rw-r--r-- | src/zir_sema.zig | 14 |
3 files changed, 26 insertions, 6 deletions
diff --git a/src/astgen.zig b/src/astgen.zig index 3db3f3654d..100b2b9d02 100644 --- a/src/astgen.zig +++ b/src/astgen.zig @@ -616,11 +616,11 @@ fn varDecl( .Keyword_var => { const var_data: struct { result_loc: ResultLoc, alloc: *zir.Inst } = if (node.getTypeNode()) |type_node| a: { const type_inst = try typeExpr(mod, scope, type_node); - const alloc = try addZIRUnOp(mod, scope, name_src, .alloc, type_inst); + const alloc = try addZIRUnOp(mod, scope, name_src, .alloc_mut, type_inst); break :a .{ .alloc = alloc, .result_loc = .{ .ptr = alloc } }; } else a: { - const alloc = try addZIRNoOp(mod, scope, name_src, .alloc_inferred); - break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc.castTag(.alloc_inferred).? } }; + const alloc = try addZIRNoOp(mod, scope, name_src, .alloc_inferred_mut); + break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc.castTag(.alloc_inferred_mut).? } }; }; const init_inst = try expr(mod, scope, var_data.result_loc, init_node); const sub_scope = try block_arena.create(Scope.LocalPtr); diff --git a/src/zir.zig b/src/zir.zig index 73530ec86d..10f64be609 100644 --- a/src/zir.zig +++ b/src/zir.zig @@ -41,8 +41,12 @@ pub const Inst = struct { /// Allocates stack local memory. Its lifetime ends when the block ends that contains /// this instruction. The operand is the type of the allocated object. alloc, + /// Same as `alloc` except mutable. + alloc_mut, /// Same as `alloc` except the type is inferred. alloc_inferred, + /// Same as `alloc_inferred` except mutable. + alloc_inferred_mut, /// Create an `anyframe->T`. anyframe_type, /// Array concatenation. `a ++ b` @@ -289,16 +293,19 @@ pub const Inst = struct { pub fn Type(tag: Tag) type { return switch (tag) { + .alloc_inferred, + .alloc_inferred_mut, .breakpoint, .dbg_stmt, .returnvoid, - .alloc_inferred, .ret_ptr, .ret_type, .unreach_nocheck, .@"unreachable", => NoOp, + .alloc, + .alloc_mut, .boolnot, .compileerror, .deref, @@ -307,7 +314,6 @@ pub const Inst = struct { .isnonnull, .iserr, .ptrtoint, - .alloc, .ensure_result_used, .ensure_result_non_error, .ensure_indexable, @@ -419,7 +425,9 @@ pub const Inst = struct { .add, .addwrap, .alloc, + .alloc_mut, .alloc_inferred, + .alloc_inferred_mut, .array_cat, .array_mul, .array_type, diff --git a/src/zir_sema.zig b/src/zir_sema.zig index 8e6632ee84..f3dae8f5b1 100644 --- a/src/zir_sema.zig +++ b/src/zir_sema.zig @@ -27,7 +27,9 @@ const Decl = Module.Decl; pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!*Inst { switch (old_inst.tag) { .alloc => return analyzeInstAlloc(mod, scope, old_inst.castTag(.alloc).?), + .alloc_mut => return analyzeInstAllocMut(mod, scope, old_inst.castTag(.alloc_mut).?), .alloc_inferred => return analyzeInstAllocInferred(mod, scope, old_inst.castTag(.alloc_inferred).?), + .alloc_inferred_mut => return analyzeInstAllocInferredMut(mod, scope, old_inst.castTag(.alloc_inferred_mut).?), .arg => return analyzeInstArg(mod, scope, old_inst.castTag(.arg).?), .bitcast_ref => return analyzeInstBitCastRef(mod, scope, old_inst.castTag(.bitcast_ref).?), .bitcast_result_ptr => return analyzeInstBitCastResultPtr(mod, scope, old_inst.castTag(.bitcast_result_ptr).?), @@ -410,7 +412,13 @@ fn analyzeInstEnsureIndexable(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) fn analyzeInstAlloc(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst { const var_type = try resolveType(mod, scope, inst.positionals.operand); - // TODO this should happen only for var allocs + const ptr_type = try mod.simplePtrType(scope, inst.base.src, var_type, true, .One); + const b = try mod.requireRuntimeBlock(scope, inst.base.src); + return mod.addNoOp(b, inst.base.src, ptr_type, .alloc); +} + +fn analyzeInstAllocMut(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst { + const var_type = try resolveType(mod, scope, inst.positionals.operand); if (!var_type.isValidVarType(false)) { return mod.fail(scope, inst.base.src, "variable of type '{}' must be const or comptime", .{var_type}); } @@ -423,6 +431,10 @@ fn analyzeInstAllocInferred(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) I return mod.fail(scope, inst.base.src, "TODO implement analyzeInstAllocInferred", .{}); } +fn analyzeInstAllocInferredMut(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerError!*Inst { + return mod.fail(scope, inst.base.src, "TODO implement analyzeInstAllocInferredMut", .{}); +} + fn analyzeInstStore(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst { const ptr = try resolveInst(mod, scope, inst.positionals.lhs); const value = try resolveInst(mod, scope, inst.positionals.rhs); |
