refactor(ir): extract protocol/impl lookup into protocols.zig (A4.2 step 2)

Move the pure protocol/impl conformance lookups into one module,
src/ir/protocols.zig, behind a *Lowering facade (ProtocolResolver), mirroring
GenericResolver / CallResolver. Per PLAN-ARCH A4.2 ("move pure lookup first;
keep emission in Lowering"), this increment moves only the read-only queries:
- getProtocolInfo (is a type a registered protocol + its method table),
- hasImplPlain (have the (protocol, type) thunks been materialized),
- packArgConformsTo (impl-declaration-level conformance for ..xs: P).

Registration (registerProtocolDecl / registerImplBlock / registerParamImpl) and
all IR emission (createProtocolThunk / buildProtocolValue / tryUserConversion /
getOrCreateThunks) stay in Lowering for the later increments. The state maps
(protocol_thunk_map / param_impl_map on Lowering, protocol_decl_map /
protocol_ast_map in ProgramIndex) stay put; the facade reads them via self.l.* —
no map migration.

Lowering keeps getProtocolInfo as a thin pub wrapper (~9 callers incl.
calls.zig); hasImplPlain + packArgConformsTo are deleted (no fallback), their 3
call sites (computeHasImpl x2, the pack-conformance check x1) routed through
self.protocolResolver(). formatTypeName widened to pub (the lookups use it);
protocolResolver() accessor added.

protocols.test.zig (wired into the barrel) drives ProtocolResolver directly:
getProtocolInfo (registered vs builtin/plain-struct + wrapper delegation),
hasImplPlain (thunk-map materialization), packArgConformsTo (non-parameterised
requires <ty>.<m> in fn_ast_map; trivially-true for an erased protocol value;
false for unknown protocol).

zig build, zig build test, tests/run_examples.sh (357/0) all green — no .ir
snapshot churn; the 0410/0411/0412 rejection anchors still pass.
This commit is contained in:
agra
2026-06-02 21:56:03 +03:00
parent df386a422e
commit 81d332dfb0
4 changed files with 208 additions and 55 deletions

85
src/ir/protocols.zig Normal file
View File

@@ -0,0 +1,85 @@
const std = @import("std");
const types = @import("types.zig");
const lower = @import("lower.zig");
const program_index_mod = @import("program_index.zig");
const TypeId = types.TypeId;
const Lowering = lower.Lowering;
const ProtocolDeclInfo = program_index_mod.ProtocolDeclInfo;
/// Protocol / impl LOOKUP (architecture phase A4.2, first increment), extracted
/// from `Lowering`. Owns the read-only conformance queries:
/// - `getProtocolInfo` — is a type a registered protocol, and its method table,
/// - `hasImplPlain` — has a (protocol, type) pair had its thunks materialized,
/// - `packArgConformsTo` — does a type conform to a protocol at the
/// impl-declaration level (for protocol-pack `..xs: P` elements).
///
/// A `*Lowering` facade (Principle 5, like `GenericResolver` / `CallResolver`):
/// these read the protocol/impl registries (`protocol_decl_map` /
/// `protocol_ast_map` in `ProgramIndex`; `protocol_thunk_map` / `param_impl_map`
/// on `Lowering`) plus the type table, so it borrows `*Lowering` rather than
/// re-threading every map. Registration (`register*`) and IR emission
/// (`createProtocolThunk` / `buildProtocolValue` / `tryUserConversion`) stay in
/// `Lowering` for the later A4.2 increments — this step moves only pure lookup.
pub const ProtocolResolver = struct {
l: *Lowering,
/// If `ty` is a registered protocol struct, return its decl info (method
/// table); else null.
pub fn getProtocolInfo(self: ProtocolResolver, ty: TypeId) ?ProtocolDeclInfo {
if (ty.isBuiltin()) return null;
const info = self.l.module.types.get(ty);
if (info != .@"struct") return null;
const name = self.l.module.types.getString(info.@"struct".name);
return self.l.program_index.protocol_decl_map.get(name);
}
/// Have the thunks for (protocol `p_name`, concrete `ty`) been materialized?
/// `protocol_thunk_map` is populated lazily when a protocol VALUE is created
/// with `xx`, so this answers "has erasure already happened for this pair".
pub fn hasImplPlain(self: ProtocolResolver, p_name: []const u8, ty: TypeId) bool {
const ty_name = self.l.formatTypeName(ty);
const thunk_key = std.fmt.allocPrint(self.l.alloc, "{s}\x00{s}", .{ p_name, ty_name }) catch return false;
return self.l.protocol_thunk_map.contains(thunk_key);
}
/// Does `ty` conform to protocol `p_name` (under SOME type-args for a
/// parameterised protocol)? Used to check protocol-pack elements
/// (`..xs: P`), where each element's protocol type-args are inferred from
/// its impl rather than written out.
///
/// Conformance is queried at the IMPL-DECLARATION level (not via
/// `protocol_thunk_map`, which is only populated lazily when a protocol
/// VALUE is created with `xx`):
/// - Parameterised `P`: any `param_impl_map` key `P\x00<args>\x00<mangle(ty)>`.
/// - Non-parameterised `P`: every required (non-default) method `m` is
/// registered as `<ty>.<m>` in `fn_ast_map` (how `registerImplBlock`
/// records a non-parameterised impl).
/// An arg already of the protocol's own (erased) type trivially conforms.
pub fn packArgConformsTo(self: ProtocolResolver, p_name: []const u8, ty: TypeId) bool {
// Arg already erased to the protocol struct itself (e.g. `xx a`).
if (!ty.isBuiltin()) {
const info = self.l.module.types.get(ty);
if (info == .@"struct" and info.@"struct".is_protocol and
std.mem.eql(u8, self.l.module.types.getString(info.@"struct".name), p_name)) return true;
}
const pd = self.l.program_index.protocol_ast_map.get(p_name) orelse return false;
if (pd.type_params.len > 0) {
const prefix = std.fmt.allocPrint(self.l.alloc, "{s}\x00", .{p_name}) catch return false;
const suffix = std.fmt.allocPrint(self.l.alloc, "\x00{s}", .{self.l.mangleTypeName(ty)}) catch return false;
var it = self.l.param_impl_map.keyIterator();
while (it.next()) |k| {
if (std.mem.startsWith(u8, k.*, prefix) and std.mem.endsWith(u8, k.*, suffix)) return true;
}
return false;
}
// Non-parameterised: require each non-default method as `<ty>.<m>`.
const ty_name = self.l.formatTypeName(ty);
for (pd.methods) |m| {
if (m.default_body != null) continue;
const q = std.fmt.allocPrint(self.l.alloc, "{s}.{s}", .{ ty_name, m.name }) catch return false;
if (!self.l.program_index.fn_ast_map.contains(q)) return false;
}
return true;
}
};