refactor(ir): factor protocol/impl planning into ProtocolResolver (A4.2 planning increment)
Factor the lookup/planning half of the protocol emission functions into protocols.zig, keeping IR emission in Lowering (PLAN-ARCH A4.2 final increment): - protocolMethodInfos(proto) — the dispatch method table = which methods getOrCreateThunks must thunk. getOrCreateThunks now does PLANNING via this + EMISSION (createProtocolThunk loop) in Lowering. - findVisibleImpls(entries, out) — moved verbatim (pure BFS over the import graph; the cross-module visibility selection behind the 0410 path). tryUserConversion calls it via the resolver. - matchPackImpl(src_ty, pack_key) -> ?PackImplMatch — the pure pack-impl matching loop (prefix + return match) + convert-method find, returning the matched entry + convert fd + src params/ret. tryPackImplMatch consumes it; the binding + monomorphise + call emission stays in Lowering. Emission untouched: createProtocolThunk, buildProtocolValue, and the monomorphise+call tails of tryUserConversion / tryPackImplMatch remain in Lowering. The reentrancy guard, key-build, and the Into no-visible / duplicate / recursive diagnostics stay in tryUserConversion (byte-for-byte). lower.zig net -94 lines. No new pub exposure (uses the existing ParamImplEntry / PackParamImplEntry / formatTypeName surface). protocols.test.zig +3: protocolMethodInfos (method table + null-for-unknown, no silent empty default); findVisibleImpls (falls open with no graph; filters to here + transitive imports); matchPackImpl (selects on prefix+return; null for non-closure source / unknown key). zig build, zig build test, tests/run_examples.sh (357/0) all green — no .ir churn; the 0410/0411/0412 diagnostics are byte-for-byte preserved.
This commit is contained in:
@@ -93,6 +93,138 @@ pub const ProtocolResolver = struct {
|
||||
return true;
|
||||
}
|
||||
|
||||
// ── Thunk / impl PLANNING (lookup only; emission stays in Lowering) ──
|
||||
|
||||
/// The dispatch method table for protocol `proto_name` — i.e. exactly which
|
||||
/// methods `getOrCreateThunks` must materialize a thunk for. Null if the
|
||||
/// name isn't a registered (non-parameterised) protocol.
|
||||
pub fn protocolMethodInfos(self: ProtocolResolver, proto_name: []const u8) ?[]const ProtocolMethodInfo {
|
||||
const pd = self.l.program_index.protocol_decl_map.get(proto_name) orelse return null;
|
||||
return pd.methods;
|
||||
}
|
||||
|
||||
/// Filter parameterised-impl `entries` to those reachable from the current
|
||||
/// source file (the file itself + everything it transitively imports). The
|
||||
/// cross-module visibility selection behind the `0410` path. Falls open
|
||||
/// (all entries) when the source-file context or import graph isn't wired
|
||||
/// (e.g. comptime callers). Appends the visible subset to `out`.
|
||||
pub fn findVisibleImpls(self: ProtocolResolver, entries: []const Lowering.ParamImplEntry, out: *std.ArrayList(Lowering.ParamImplEntry)) void {
|
||||
const here = self.l.current_source_file orelse {
|
||||
out.appendSlice(self.l.alloc, entries) catch {};
|
||||
return;
|
||||
};
|
||||
const graph = self.l.program_index.import_graph orelse {
|
||||
out.appendSlice(self.l.alloc, entries) catch {};
|
||||
return;
|
||||
};
|
||||
|
||||
// BFS over the import graph to compute the visible set.
|
||||
var visible = std.StringHashMap(void).init(self.l.alloc);
|
||||
defer visible.deinit();
|
||||
visible.put(here, {}) catch {};
|
||||
var queue = std.ArrayList([]const u8).empty;
|
||||
defer queue.deinit(self.l.alloc);
|
||||
queue.append(self.l.alloc, here) catch {};
|
||||
var head: usize = 0;
|
||||
while (head < queue.items.len) : (head += 1) {
|
||||
const node = queue.items[head];
|
||||
const direct = graph.get(node) orelse continue;
|
||||
var it = direct.iterator();
|
||||
while (it.next()) |kv| {
|
||||
const next = kv.key_ptr.*;
|
||||
if (visible.contains(next)) continue;
|
||||
visible.put(next, {}) catch {};
|
||||
queue.append(self.l.alloc, next) catch {};
|
||||
}
|
||||
}
|
||||
|
||||
for (entries) |e| {
|
||||
if (visible.contains(e.defining_module)) {
|
||||
out.append(self.l.alloc, e) catch {};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A pack-impl selected for a concrete source closure/function: the matched
|
||||
/// entry plus its `convert` method. Pure SELECTION — binding + monomorphise
|
||||
/// + emission stay in `Lowering.tryPackImplMatch`.
|
||||
pub const PackImplMatch = struct {
|
||||
entry: Lowering.PackParamImplEntry,
|
||||
convert_fd: *const ast.FnDecl,
|
||||
/// The source closure/function's param + return types — the binding
|
||||
/// step (in `Lowering`) reads these to bind the pack-var tail + ret-var.
|
||||
src_params: []const TypeId,
|
||||
src_ret: TypeId,
|
||||
};
|
||||
|
||||
/// Among the pack impls under `pack_key`, find the first whose fixed prefix
|
||||
/// matches `src_ty`'s leading params (and whose return matches, unless the
|
||||
/// impl's return is a generic var). Returns the matched entry + its
|
||||
/// `convert` method, or null when nothing matches. No emission.
|
||||
pub fn matchPackImpl(self: ProtocolResolver, src_ty: TypeId, pack_key: []const u8) ?PackImplMatch {
|
||||
const pack_entries = self.l.param_impl_pack_map.get(pack_key) orelse return null;
|
||||
if (pack_entries.items.len == 0) return null;
|
||||
const table = &self.l.module.types;
|
||||
// Source must itself be a closure/function the pack can match.
|
||||
const src_info = table.get(src_ty);
|
||||
if (src_info != .closure and src_info != .function) return null;
|
||||
|
||||
const src_params: []const TypeId = switch (src_info) {
|
||||
.closure => |c| c.params,
|
||||
.function => |f| f.params,
|
||||
else => unreachable,
|
||||
};
|
||||
const src_ret: TypeId = switch (src_info) {
|
||||
.closure => |c| c.ret,
|
||||
.function => |f| f.ret,
|
||||
else => unreachable,
|
||||
};
|
||||
|
||||
// Find pack impls whose fixed prefix matches src's leading params.
|
||||
var matched_idx: ?usize = null;
|
||||
for (pack_entries.items, 0..) |entry, i| {
|
||||
const ent_info = table.get(entry.source_pack_ty);
|
||||
// Pack impls always wear a closure (resolveClosureType routes
|
||||
// both Closure and the future Fn pack forms through
|
||||
// closureTypePack); a function-typed pack impl is not produced
|
||||
// by current parser shapes.
|
||||
if (ent_info != .closure) continue;
|
||||
const ent_ci = ent_info.closure;
|
||||
const pack_start = ent_ci.pack_start orelse continue;
|
||||
// Fixed prefix must fit within the source's params.
|
||||
if (pack_start > src_params.len) continue;
|
||||
var prefix_ok = true;
|
||||
var i_fix: u32 = 0;
|
||||
while (i_fix < pack_start) : (i_fix += 1) {
|
||||
if (ent_ci.params[i_fix] != src_params[i_fix]) {
|
||||
prefix_ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!prefix_ok) continue;
|
||||
// Return type: if the impl's return is a generic var
|
||||
// (ret_var_name set), any source return binds; otherwise it
|
||||
// must equal the source's return exactly.
|
||||
if (entry.ret_var_name == null and ent_ci.ret != src_ret) continue;
|
||||
// First match wins for v1; concrete-wins-over-pack already
|
||||
// happened by the caller checking concrete first. Multiple
|
||||
// overlapping pack impls would be a separate diagnostic
|
||||
// (deferred — same module duplicates are caught at registration).
|
||||
matched_idx = i;
|
||||
break;
|
||||
}
|
||||
const idx = matched_idx orelse return null;
|
||||
const entry = pack_entries.items[idx];
|
||||
|
||||
// Find the `convert` method.
|
||||
for (entry.methods) |m| {
|
||||
if (std.mem.eql(u8, m.name, "convert")) {
|
||||
return .{ .entry = entry, .convert_fd = m, .src_params = src_params, .src_ret = src_ret };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ── Registration ────────────────────────────────────────────────────
|
||||
|
||||
pub fn registerProtocolDecl(self: ProtocolResolver, pd: *const ast.ProtocolDecl) void {
|
||||
|
||||
Reference in New Issue
Block a user