diff options
| author | Veikka Tuominen <git@vexu.eu> | 2022-06-11 11:33:09 +0300 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2022-06-11 23:49:33 +0300 |
| commit | 35c7e376b89f41d8260b70e770e03ef6af68849d (patch) | |
| tree | 88734375267629dc08d0c33bb43fe399461a4d07 /src | |
| parent | 0333ff4476d0132a2397122dcab964de7fc0f2d3 (diff) | |
| download | zig-35c7e376b89f41d8260b70e770e03ef6af68849d.tar.gz zig-35c7e376b89f41d8260b70e770e03ef6af68849d.zip | |
stage2: improve anon name strategy for local variables
Diffstat (limited to 'src')
| -rw-r--r-- | src/AstGen.zig | 7 | ||||
| -rw-r--r-- | src/Sema.zig | 49 | ||||
| -rw-r--r-- | src/Zir.zig | 2 |
3 files changed, 47 insertions, 11 deletions
diff --git a/src/AstGen.zig b/src/AstGen.zig index 3f22146f0e..078523831c 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -2753,7 +2753,10 @@ fn varDecl( const result_loc: ResultLoc = if (type_node != 0) .{ .ty = try typeExpr(gz, scope, type_node), } else .none; + const prev_anon_name_strategy = gz.anon_name_strategy; + gz.anon_name_strategy = .dbg_var; const init_inst = try reachableExpr(gz, scope, result_loc, var_decl.ast.init_node, node); + gz.anon_name_strategy = prev_anon_name_strategy; try gz.addDbgVar(.dbg_var_val, ident_name, init_inst); @@ -2777,6 +2780,7 @@ fn varDecl( var init_scope = gz.makeSubBlock(scope); // we may add more instructions to gz before stacking init_scope init_scope.instructions_top = GenZir.unstacked_top; + init_scope.anon_name_strategy = .dbg_var; defer init_scope.unstack(); var resolve_inferred_alloc: Zir.Inst.Ref = .none; @@ -2956,7 +2960,10 @@ fn varDecl( resolve_inferred_alloc = alloc; break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc } }; }; + const prev_anon_name_strategy = gz.anon_name_strategy; + gz.anon_name_strategy = .dbg_var; _ = try reachableExprComptime(gz, scope, var_data.result_loc, var_decl.ast.init_node, node, is_comptime); + gz.anon_name_strategy = prev_anon_name_strategy; if (resolve_inferred_alloc != .none) { _ = try gz.addUnNode(.resolve_inferred_alloc, resolve_inferred_alloc, node); } diff --git a/src/Sema.zig b/src/Sema.zig index 9d09324193..f0eb67ec26 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -914,9 +914,9 @@ fn analyzeBodyInner( // zig fmt: off .variable => try sema.zirVarExtended( block, extended), .struct_decl => try sema.zirStructDecl( block, extended, inst), - .enum_decl => try sema.zirEnumDecl( block, extended), + .enum_decl => try sema.zirEnumDecl( block, extended, inst), .union_decl => try sema.zirUnionDecl( block, extended, inst), - .opaque_decl => try sema.zirOpaqueDecl( block, extended), + .opaque_decl => try sema.zirOpaqueDecl( block, extended, inst), .this => try sema.zirThis( block, extended), .ret_addr => try sema.zirRetAddr( block, extended), .builtin_src => try sema.zirBuiltinSrc( block, extended), @@ -2101,7 +2101,7 @@ fn zirStructDecl( const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{ .ty = Type.type, .val = struct_val, - }, small.name_strategy, "struct"); + }, small.name_strategy, "struct", inst); const new_decl = mod.declPtr(new_decl_index); new_decl.owns_tv = true; errdefer mod.abortAnonDecl(new_decl_index); @@ -2133,6 +2133,7 @@ fn createAnonymousDeclTypeNamed( typed_value: TypedValue, name_strategy: Zir.Inst.NameStrategy, anon_prefix: []const u8, + inst: ?Zir.Inst.Index, ) !Decl.Index { const mod = sema.mod; const namespace = block.namespace; @@ -2152,11 +2153,13 @@ fn createAnonymousDeclTypeNamed( const name = try std.fmt.allocPrintZ(sema.gpa, "{s}__{s}_{d}", .{ src_decl.name, anon_prefix, @enumToInt(new_decl_index), }); + errdefer sema.gpa.free(name); try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, namespace, typed_value, name); return new_decl_index; }, .parent => { const name = try sema.gpa.dupeZ(u8, mem.sliceTo(sema.mod.declPtr(block.src_decl).name, 0)); + errdefer sema.gpa.free(name); try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, namespace, typed_value, name); return new_decl_index; }, @@ -2188,9 +2191,31 @@ fn createAnonymousDeclTypeNamed( try buf.appendSlice(")"); const name = try buf.toOwnedSliceSentinel(0); + errdefer sema.gpa.free(name); try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, namespace, typed_value, name); return new_decl_index; }, + .dbg_var => { + const ref = Zir.indexToRef(inst.?); + const zir_tags = sema.code.instructions.items(.tag); + const zir_data = sema.code.instructions.items(.data); + var i = inst.?; + while (i < zir_tags.len) : (i += 1) switch (zir_tags[i]) { + .dbg_var_ptr, .dbg_var_val => { + if (zir_data[i].str_op.operand != ref) continue; + + const name = try std.fmt.allocPrintZ(sema.gpa, "{s}.{s}", .{ + src_decl.name, zir_data[i].str_op.getStr(sema.code), + }); + errdefer sema.gpa.free(name); + + try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, namespace, typed_value, name); + return new_decl_index; + }, + else => {}, + }; + return sema.createAnonymousDeclTypeNamed(block, typed_value, .anon, anon_prefix, null); + }, } } @@ -2198,6 +2223,7 @@ fn zirEnumDecl( sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData, + inst: Zir.Inst.Index, ) CompileError!Air.Inst.Ref { const tracy = trace(@src()); defer tracy.end(); @@ -2252,7 +2278,7 @@ fn zirEnumDecl( const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{ .ty = Type.type, .val = enum_val, - }, small.name_strategy, "enum"); + }, small.name_strategy, "enum", inst); const new_decl = mod.declPtr(new_decl_index); new_decl.owns_tv = true; errdefer mod.abortAnonDecl(new_decl_index); @@ -2472,7 +2498,7 @@ fn zirUnionDecl( const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{ .ty = Type.type, .val = union_val, - }, small.name_strategy, "union"); + }, small.name_strategy, "union", inst); const new_decl = mod.declPtr(new_decl_index); new_decl.owns_tv = true; errdefer mod.abortAnonDecl(new_decl_index); @@ -2504,6 +2530,7 @@ fn zirOpaqueDecl( sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData, + inst: Zir.Inst.Index, ) CompileError!Air.Inst.Ref { const tracy = trace(@src()); defer tracy.end(); @@ -2540,7 +2567,7 @@ fn zirOpaqueDecl( const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{ .ty = Type.type, .val = opaque_val, - }, small.name_strategy, "opaque"); + }, small.name_strategy, "opaque", inst); const new_decl = mod.declPtr(new_decl_index); new_decl.owns_tv = true; errdefer mod.abortAnonDecl(new_decl_index); @@ -2589,7 +2616,7 @@ fn zirErrorSetDecl( const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{ .ty = Type.type, .val = error_set_val, - }, name_strategy, "error"); + }, name_strategy, "error", inst); const new_decl = mod.declPtr(new_decl_index); new_decl.owns_tv = true; errdefer mod.abortAnonDecl(new_decl_index); @@ -14632,7 +14659,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{ .ty = Type.type, .val = enum_val, - }, .anon, "enum"); + }, .anon, "enum", null); const new_decl = mod.declPtr(new_decl_index); new_decl.owns_tv = true; errdefer mod.abortAnonDecl(new_decl_index); @@ -14722,7 +14749,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{ .ty = Type.type, .val = opaque_val, - }, .anon, "opaque"); + }, .anon, "opaque", null); const new_decl = mod.declPtr(new_decl_index); new_decl.owns_tv = true; errdefer mod.abortAnonDecl(new_decl_index); @@ -14773,7 +14800,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{ .ty = Type.type, .val = new_union_val, - }, .anon, "union"); + }, .anon, "union", null); const new_decl = mod.declPtr(new_decl_index); new_decl.owns_tv = true; errdefer mod.abortAnonDecl(new_decl_index); @@ -14941,7 +14968,7 @@ fn reifyStruct( const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{ .ty = Type.type, .val = new_struct_val, - }, .anon, "struct"); + }, .anon, "struct", null); const new_decl = mod.declPtr(new_decl_index); new_decl.owns_tv = true; errdefer mod.abortAnonDecl(new_decl_index); diff --git a/src/Zir.zig b/src/Zir.zig index 1ca31755f7..b43b775dfa 100644 --- a/src/Zir.zig +++ b/src/Zir.zig @@ -3156,6 +3156,8 @@ pub const Inst = struct { /// Create an anonymous name for this declaration. /// Like this: "ParentDeclName_struct_69" anon, + /// Use the name specified in the next `dbg_var_{val,ptr}` instruction. + dbg_var, }; /// Trailing: |
