authorgravatar for 58830309+g-w1@users.noreply.github.comg-w1 <58830309+g-w1@users.noreply.github.com> 2020-12-28 08:42:22-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-12-28 15:42:22+02:00
logc234761eddc2fe932046767756b9e6df82a73b65
tree959a1873c66019485873a425fd5a915b490871ed
parentfffb0904f898d47800aa418085dc6064d02c32fe
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

stage2: make Alloc(Inferred) have mutabality info (#7570)


3 files changed, 26 insertions(+), 6 deletions(-)

src/astgen.zig+3-3
......@@ -616,11 +616,11 @@ fn varDecl(
616616 .Keyword_var => {
617617 const var_data: struct { result_loc: ResultLoc, alloc: *zir.Inst } = if (node.getTypeNode()) |type_node| a: {
618618 const type_inst = try typeExpr(mod, scope, type_node);
619 const alloc = try addZIRUnOp(mod, scope, name_src, .alloc, type_inst);
619 const alloc = try addZIRUnOp(mod, scope, name_src, .alloc_mut, type_inst);
620620 break :a .{ .alloc = alloc, .result_loc = .{ .ptr = alloc } };
621621 } else a: {
622 const alloc = try addZIRNoOp(mod, scope, name_src, .alloc_inferred);
623 break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc.castTag(.alloc_inferred).? } };
622 const alloc = try addZIRNoOp(mod, scope, name_src, .alloc_inferred_mut);
623 break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc.castTag(.alloc_inferred_mut).? } };
624624 };
625625 const init_inst = try expr(mod, scope, var_data.result_loc, init_node);
626626 const sub_scope = try block_arena.create(Scope.LocalPtr);
src/zir.zig+10-2
......@@ -41,8 +41,12 @@ pub const Inst = struct {
4141 /// Allocates stack local memory. Its lifetime ends when the block ends that contains
4242 /// this instruction. The operand is the type of the allocated object.
4343 alloc,
44 /// Same as `alloc` except mutable.
45 alloc_mut,
4446 /// Same as `alloc` except the type is inferred.
4547 alloc_inferred,
48 /// Same as `alloc_inferred` except mutable.
49 alloc_inferred_mut,
4650 /// Create an `anyframe->T`.
4751 anyframe_type,
4852 /// Array concatenation. `a ++ b`
......@@ -289,16 +293,19 @@ pub const Inst = struct {
289293
290294 pub fn Type(tag: Tag) type {
291295 return switch (tag) {
296 .alloc_inferred,
297 .alloc_inferred_mut,
292298 .breakpoint,
293299 .dbg_stmt,
294300 .returnvoid,
295 .alloc_inferred,
296301 .ret_ptr,
297302 .ret_type,
298303 .unreach_nocheck,
299304 .@"unreachable",
300305 => NoOp,
301306
307 .alloc,
308 .alloc_mut,
302309 .boolnot,
303310 .compileerror,
304311 .deref,
......@@ -307,7 +314,6 @@ pub const Inst = struct {
307314 .isnonnull,
308315 .iserr,
309316 .ptrtoint,
310 .alloc,
311317 .ensure_result_used,
312318 .ensure_result_non_error,
313319 .ensure_indexable,
......@@ -419,7 +425,9 @@ pub const Inst = struct {
419425 .add,
420426 .addwrap,
421427 .alloc,
428 .alloc_mut,
422429 .alloc_inferred,
430 .alloc_inferred_mut,
423431 .array_cat,
424432 .array_mul,
425433 .array_type,
src/zir_sema.zig+13-1
......@@ -27,7 +27,9 @@ const Decl = Module.Decl;
2727pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!*Inst {
2828 switch (old_inst.tag) {
2929 .alloc => return analyzeInstAlloc(mod, scope, old_inst.castTag(.alloc).?),
30 .alloc_mut => return analyzeInstAllocMut(mod, scope, old_inst.castTag(.alloc_mut).?),
3031 .alloc_inferred => return analyzeInstAllocInferred(mod, scope, old_inst.castTag(.alloc_inferred).?),
32 .alloc_inferred_mut => return analyzeInstAllocInferredMut(mod, scope, old_inst.castTag(.alloc_inferred_mut).?),
3133 .arg => return analyzeInstArg(mod, scope, old_inst.castTag(.arg).?),
3234 .bitcast_ref => return analyzeInstBitCastRef(mod, scope, old_inst.castTag(.bitcast_ref).?),
3335 .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)
410412
411413fn analyzeInstAlloc(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
412414 const var_type = try resolveType(mod, scope, inst.positionals.operand);
413 // TODO this should happen only for var allocs
415 const ptr_type = try mod.simplePtrType(scope, inst.base.src, var_type, true, .One);
416 const b = try mod.requireRuntimeBlock(scope, inst.base.src);
417 return mod.addNoOp(b, inst.base.src, ptr_type, .alloc);
418}
419
420fn analyzeInstAllocMut(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
421 const var_type = try resolveType(mod, scope, inst.positionals.operand);
414422 if (!var_type.isValidVarType(false)) {
415423 return mod.fail(scope, inst.base.src, "variable of type '{}' must be const or comptime", .{var_type});
416424 }
......@@ -423,6 +431,10 @@ fn analyzeInstAllocInferred(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) I
423431 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstAllocInferred", .{});
424432}
425433
434fn analyzeInstAllocInferredMut(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerError!*Inst {
435 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstAllocInferredMut", .{});
436}
437
426438fn analyzeInstStore(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
427439 const ptr = try resolveInst(mod, scope, inst.positionals.lhs);
428440 const value = try resolveInst(mod, scope, inst.positionals.rhs);