Skip to content

Commit

Permalink
Merge pull request #20424 from mlugg/the-great-decl-split
Browse files Browse the repository at this point in the history
Dismantle Decl a little bit more
  • Loading branch information
mlugg authored Jun 26, 2024
2 parents 3e9ab6a + 4cb5318 commit a016ca6
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 138 deletions.
14 changes: 7 additions & 7 deletions lib/std/zig/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4368,7 +4368,7 @@ fn fnDecl(
decl_inst,
std.zig.hashSrc(tree.getNodeSource(decl_node)),
.{ .named = fn_name_token },
decl_gz.decl_line - gz.decl_line,
decl_gz.decl_line,
is_pub,
is_export,
doc_comment_index,
Expand Down Expand Up @@ -4529,7 +4529,7 @@ fn globalVarDecl(
decl_inst,
std.zig.hashSrc(tree.getNodeSource(node)),
.{ .named = name_token },
block_scope.decl_line - gz.decl_line,
block_scope.decl_line,
is_pub,
is_export,
doc_comment_index,
Expand Down Expand Up @@ -4579,7 +4579,7 @@ fn comptimeDecl(
decl_inst,
std.zig.hashSrc(tree.getNodeSource(node)),
.@"comptime",
decl_block.decl_line - gz.decl_line,
decl_block.decl_line,
false,
false,
.empty,
Expand Down Expand Up @@ -4629,7 +4629,7 @@ fn usingnamespaceDecl(
decl_inst,
std.zig.hashSrc(tree.getNodeSource(node)),
.@"usingnamespace",
decl_block.decl_line - gz.decl_line,
decl_block.decl_line,
is_pub,
false,
.empty,
Expand Down Expand Up @@ -4818,7 +4818,7 @@ fn testDecl(
decl_inst,
std.zig.hashSrc(tree.getNodeSource(node)),
test_name,
decl_block.decl_line - gz.decl_line,
decl_block.decl_line,
false,
false,
.empty,
Expand Down Expand Up @@ -13861,7 +13861,7 @@ fn setDeclaration(
decl_inst: Zir.Inst.Index,
src_hash: std.zig.SrcHash,
name: DeclarationName,
line_offset: u32,
src_line: u32,
is_pub: bool,
is_export: bool,
doc_comment: Zir.NullTerminatedString,
Expand Down Expand Up @@ -13913,7 +13913,7 @@ fn setDeclaration(
.@"comptime" => .@"comptime",
.@"usingnamespace" => .@"usingnamespace",
},
.line_offset = line_offset,
.src_line = src_line,
.flags = .{
.value_body_len = @intCast(value_len),
.is_pub = is_pub,
Expand Down
4 changes: 1 addition & 3 deletions lib/std/zig/Zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2598,9 +2598,7 @@ pub const Inst = struct {
src_hash_3: u32,
/// The name of this `Decl`. Also indicates whether it is a test, comptime block, etc.
name: Name,
/// This Decl's line number relative to that of its parent.
/// TODO: column must be encoded similarly to respect non-formatted code!
line_offset: u32,
src_line: u32,
flags: Flags,

pub const Flags = packed struct(u32) {
Expand Down
2 changes: 1 addition & 1 deletion src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3494,7 +3494,7 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: std.Progress.Node) !vo
.{@errorName(err)},
));
decl.analysis = .codegen_failure;
try module.retryable_failures.append(gpa, InternPool.Depender.wrap(.{ .decl = decl_index }));
try module.retryable_failures.append(gpa, InternPool.AnalSubject.wrap(.{ .decl = decl_index }));
};
},
.analyze_mod => |pkg| {
Expand Down
51 changes: 25 additions & 26 deletions src/InternPool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace_name_deps: std.AutoArrayHashMapUnmanaged(NamespaceNameKey, DepEntry.In
/// Given a `Depender`, points to an entry in `dep_entries` whose `depender`
/// matches. The `next_dependee` field can be used to iterate all such entries
/// and remove them from the corresponding lists.
first_dependency: std.AutoArrayHashMapUnmanaged(Depender, DepEntry.Index) = .{},
first_dependency: std.AutoArrayHashMapUnmanaged(AnalSubject, DepEntry.Index) = .{},

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

/// Reperesents the "source" of a dependency edge, i.e. either a Decl or a
/// runtime function (represented as an InternPool index).
/// MSB is 0 for a Decl, 1 for a function.
pub const Depender = enum(u32) {
_,
/// Analysis Subject. Represents a single entity which undergoes semantic analysis.
/// This is either a `Decl` (in future `Cau`) or a runtime function.
/// The LSB is used as a tag bit.
/// This is the "source" of an incremental dependency edge.
pub const AnalSubject = packed struct(u32) {
kind: enum(u1) { decl, func },
index: u31,
pub const Unwrapped = union(enum) {
decl: DeclIndex,
func: InternPool.Index,
};
pub fn unwrap(dep: Depender) Unwrapped {
const tag: u1 = @truncate(@intFromEnum(dep) >> 31);
const val: u31 = @truncate(@intFromEnum(dep));
return switch (tag) {
0 => .{ .decl = @enumFromInt(val) },
1 => .{ .func = @enumFromInt(val) },
pub fn unwrap(as: AnalSubject) Unwrapped {
return switch (as.kind) {
.decl => .{ .decl = @enumFromInt(as.index) },
.func => .{ .func = @enumFromInt(as.index) },
};
}
pub fn wrap(raw: Unwrapped) Depender {
return @enumFromInt(switch (raw) {
.decl => |decl| @intFromEnum(decl),
.func => |func| (1 << 31) | @intFromEnum(func),
});
pub fn wrap(raw: Unwrapped) AnalSubject {
return switch (raw) {
.decl => |decl| .{ .kind = .decl, .index = @intCast(@intFromEnum(decl)) },
.func => |func| .{ .kind = .func, .index = @intCast(@intFromEnum(func)) },
};
}
pub fn toOptional(dep: Depender) Optional {
return @enumFromInt(@intFromEnum(dep));
pub fn toOptional(as: AnalSubject) Optional {
return @enumFromInt(@as(u32, @bitCast(as)));
}
pub const Optional = enum(u32) {
none = std.math.maxInt(u32),
_,
pub fn unwrap(opt: Optional) ?Depender {
pub fn unwrap(opt: Optional) ?AnalSubject {
return switch (opt) {
.none => null,
_ => @enumFromInt(@intFromEnum(opt)),
_ => @bitCast(@intFromEnum(opt)),
};
}
};
Expand All @@ -178,7 +178,7 @@ pub const Dependee = union(enum) {
namespace_name: NamespaceNameKey,
};

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

while (opt_idx.unwrap()) |idx| {
Expand Down Expand Up @@ -207,7 +207,7 @@ pub fn removeDependenciesForDepender(ip: *InternPool, gpa: Allocator, depender:
pub const DependencyIterator = struct {
ip: *const InternPool,
next_entry: DepEntry.Index.Optional,
pub fn next(it: *DependencyIterator) ?Depender {
pub fn next(it: *DependencyIterator) ?AnalSubject {
const idx = it.next_entry.unwrap() orelse return null;
const entry = it.ip.dep_entries.items[@intFromEnum(idx)];
it.next_entry = entry.next;
Expand Down Expand Up @@ -236,7 +236,7 @@ pub fn dependencyIterator(ip: *const InternPool, dependee: Dependee) DependencyI
};
}

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

0 comments on commit a016ca6

Please sign in to comment.