Skip to content

Commit a016ca6

Browse files
authored
Merge pull request #20424 from mlugg/the-great-decl-split
Dismantle Decl a little bit more
2 parents 3e9ab6a + 4cb5318 commit a016ca6

File tree

11 files changed

+161
-138
lines changed

11 files changed

+161
-138
lines changed

lib/std/zig/AstGen.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4368,7 +4368,7 @@ fn fnDecl(
43684368
decl_inst,
43694369
std.zig.hashSrc(tree.getNodeSource(decl_node)),
43704370
.{ .named = fn_name_token },
4371-
decl_gz.decl_line - gz.decl_line,
4371+
decl_gz.decl_line,
43724372
is_pub,
43734373
is_export,
43744374
doc_comment_index,
@@ -4529,7 +4529,7 @@ fn globalVarDecl(
45294529
decl_inst,
45304530
std.zig.hashSrc(tree.getNodeSource(node)),
45314531
.{ .named = name_token },
4532-
block_scope.decl_line - gz.decl_line,
4532+
block_scope.decl_line,
45334533
is_pub,
45344534
is_export,
45354535
doc_comment_index,
@@ -4579,7 +4579,7 @@ fn comptimeDecl(
45794579
decl_inst,
45804580
std.zig.hashSrc(tree.getNodeSource(node)),
45814581
.@"comptime",
4582-
decl_block.decl_line - gz.decl_line,
4582+
decl_block.decl_line,
45834583
false,
45844584
false,
45854585
.empty,
@@ -4629,7 +4629,7 @@ fn usingnamespaceDecl(
46294629
decl_inst,
46304630
std.zig.hashSrc(tree.getNodeSource(node)),
46314631
.@"usingnamespace",
4632-
decl_block.decl_line - gz.decl_line,
4632+
decl_block.decl_line,
46334633
is_pub,
46344634
false,
46354635
.empty,
@@ -4818,7 +4818,7 @@ fn testDecl(
48184818
decl_inst,
48194819
std.zig.hashSrc(tree.getNodeSource(node)),
48204820
test_name,
4821-
decl_block.decl_line - gz.decl_line,
4821+
decl_block.decl_line,
48224822
false,
48234823
false,
48244824
.empty,
@@ -13861,7 +13861,7 @@ fn setDeclaration(
1386113861
decl_inst: Zir.Inst.Index,
1386213862
src_hash: std.zig.SrcHash,
1386313863
name: DeclarationName,
13864-
line_offset: u32,
13864+
src_line: u32,
1386513865
is_pub: bool,
1386613866
is_export: bool,
1386713867
doc_comment: Zir.NullTerminatedString,
@@ -13913,7 +13913,7 @@ fn setDeclaration(
1391313913
.@"comptime" => .@"comptime",
1391413914
.@"usingnamespace" => .@"usingnamespace",
1391513915
},
13916-
.line_offset = line_offset,
13916+
.src_line = src_line,
1391713917
.flags = .{
1391813918
.value_body_len = @intCast(value_len),
1391913919
.is_pub = is_pub,

lib/std/zig/Zir.zig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,9 +2598,7 @@ pub const Inst = struct {
25982598
src_hash_3: u32,
25992599
/// The name of this `Decl`. Also indicates whether it is a test, comptime block, etc.
26002600
name: Name,
2601-
/// This Decl's line number relative to that of its parent.
2602-
/// TODO: column must be encoded similarly to respect non-formatted code!
2603-
line_offset: u32,
2601+
src_line: u32,
26042602
flags: Flags,
26052603

26062604
pub const Flags = packed struct(u32) {

src/Compilation.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3494,7 +3494,7 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: std.Progress.Node) !vo
34943494
.{@errorName(err)},
34953495
));
34963496
decl.analysis = .codegen_failure;
3497-
try module.retryable_failures.append(gpa, InternPool.Depender.wrap(.{ .decl = decl_index }));
3497+
try module.retryable_failures.append(gpa, InternPool.AnalSubject.wrap(.{ .decl = decl_index }));
34983498
};
34993499
},
35003500
.analyze_mod => |pkg| {

src/InternPool.zig

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ namespace_name_deps: std.AutoArrayHashMapUnmanaged(NamespaceNameKey, DepEntry.In
8181
/// Given a `Depender`, points to an entry in `dep_entries` whose `depender`
8282
/// matches. The `next_dependee` field can be used to iterate all such entries
8383
/// and remove them from the corresponding lists.
84-
first_dependency: std.AutoArrayHashMapUnmanaged(Depender, DepEntry.Index) = .{},
84+
first_dependency: std.AutoArrayHashMapUnmanaged(AnalSubject, DepEntry.Index) = .{},
8585

8686
/// Stores dependency information. The hashmaps declared above are used to look
8787
/// up entries in this list as required. This is not stored in `extra` so that
@@ -132,39 +132,39 @@ pub fn trackZir(ip: *InternPool, gpa: Allocator, file: *Module.File, inst: Zir.I
132132
return @enumFromInt(gop.index);
133133
}
134134

135-
/// Reperesents the "source" of a dependency edge, i.e. either a Decl or a
136-
/// runtime function (represented as an InternPool index).
137-
/// MSB is 0 for a Decl, 1 for a function.
138-
pub const Depender = enum(u32) {
139-
_,
135+
/// Analysis Subject. Represents a single entity which undergoes semantic analysis.
136+
/// This is either a `Decl` (in future `Cau`) or a runtime function.
137+
/// The LSB is used as a tag bit.
138+
/// This is the "source" of an incremental dependency edge.
139+
pub const AnalSubject = packed struct(u32) {
140+
kind: enum(u1) { decl, func },
141+
index: u31,
140142
pub const Unwrapped = union(enum) {
141143
decl: DeclIndex,
142144
func: InternPool.Index,
143145
};
144-
pub fn unwrap(dep: Depender) Unwrapped {
145-
const tag: u1 = @truncate(@intFromEnum(dep) >> 31);
146-
const val: u31 = @truncate(@intFromEnum(dep));
147-
return switch (tag) {
148-
0 => .{ .decl = @enumFromInt(val) },
149-
1 => .{ .func = @enumFromInt(val) },
146+
pub fn unwrap(as: AnalSubject) Unwrapped {
147+
return switch (as.kind) {
148+
.decl => .{ .decl = @enumFromInt(as.index) },
149+
.func => .{ .func = @enumFromInt(as.index) },
150150
};
151151
}
152-
pub fn wrap(raw: Unwrapped) Depender {
153-
return @enumFromInt(switch (raw) {
154-
.decl => |decl| @intFromEnum(decl),
155-
.func => |func| (1 << 31) | @intFromEnum(func),
156-
});
152+
pub fn wrap(raw: Unwrapped) AnalSubject {
153+
return switch (raw) {
154+
.decl => |decl| .{ .kind = .decl, .index = @intCast(@intFromEnum(decl)) },
155+
.func => |func| .{ .kind = .func, .index = @intCast(@intFromEnum(func)) },
156+
};
157157
}
158-
pub fn toOptional(dep: Depender) Optional {
159-
return @enumFromInt(@intFromEnum(dep));
158+
pub fn toOptional(as: AnalSubject) Optional {
159+
return @enumFromInt(@as(u32, @bitCast(as)));
160160
}
161161
pub const Optional = enum(u32) {
162162
none = std.math.maxInt(u32),
163163
_,
164-
pub fn unwrap(opt: Optional) ?Depender {
164+
pub fn unwrap(opt: Optional) ?AnalSubject {
165165
return switch (opt) {
166166
.none => null,
167-
_ => @enumFromInt(@intFromEnum(opt)),
167+
_ => @bitCast(@intFromEnum(opt)),
168168
};
169169
}
170170
};
@@ -178,7 +178,7 @@ pub const Dependee = union(enum) {
178178
namespace_name: NamespaceNameKey,
179179
};
180180

181-
pub fn removeDependenciesForDepender(ip: *InternPool, gpa: Allocator, depender: Depender) void {
181+
pub fn removeDependenciesForDepender(ip: *InternPool, gpa: Allocator, depender: AnalSubject) void {
182182
var opt_idx = (ip.first_dependency.fetchSwapRemove(depender) orelse return).value.toOptional();
183183

184184
while (opt_idx.unwrap()) |idx| {
@@ -207,7 +207,7 @@ pub fn removeDependenciesForDepender(ip: *InternPool, gpa: Allocator, depender:
207207
pub const DependencyIterator = struct {
208208
ip: *const InternPool,
209209
next_entry: DepEntry.Index.Optional,
210-
pub fn next(it: *DependencyIterator) ?Depender {
210+
pub fn next(it: *DependencyIterator) ?AnalSubject {
211211
const idx = it.next_entry.unwrap() orelse return null;
212212
const entry = it.ip.dep_entries.items[@intFromEnum(idx)];
213213
it.next_entry = entry.next;
@@ -236,7 +236,7 @@ pub fn dependencyIterator(ip: *const InternPool, dependee: Dependee) DependencyI
236236
};
237237
}
238238

239-
pub fn addDependency(ip: *InternPool, gpa: Allocator, depender: Depender, dependee: Dependee) Allocator.Error!void {
239+
pub fn addDependency(ip: *InternPool, gpa: Allocator, depender: AnalSubject, dependee: Dependee) Allocator.Error!void {
240240
const first_depender_dep: DepEntry.Index.Optional = if (ip.first_dependency.get(depender)) |idx| dep: {
241241
// The entry already exists, so there is capacity to overwrite it later.
242242
break :dep idx.toOptional();
@@ -300,7 +300,7 @@ pub const DepEntry = extern struct {
300300
/// the first and only entry in one of `intern_pool.*_deps`, and does not
301301
/// appear in any list by `first_dependency`, but is not in
302302
/// `free_dep_entries` since `*_deps` stores a reference to it.
303-
depender: Depender.Optional,
303+
depender: AnalSubject.Optional,
304304
/// Index into `dep_entries` forming a doubly linked list of all dependencies on this dependee.
305305
/// Used to iterate all dependers for a given dependee during an update.
306306
/// null if this is the end of the list.
@@ -6958,7 +6958,6 @@ fn finishFuncInstance(
69586958
const decl_index = try ip.createDecl(gpa, .{
69596959
.name = undefined,
69606960
.src_namespace = fn_owner_decl.src_namespace,
6961-
.src_line = fn_owner_decl.src_line,
69626961
.has_tv = true,
69636962
.owns_tv = true,
69646963
.val = @import("Value.zig").fromInterned(func_index),

0 commit comments

Comments
 (0)