refactor(ir): add CallPlan + CallResolver.plan(c); resultType delegates (A3.2 convergence step 2)
Introduce CallPlan — the single classification record for a call: kind (14 variants), return_type, a Target union (builtin/func/named/protocol_method/ foreign_method/constructed/none), variant tag, and the prepends_receiver / prepends_ctx / expands_defaults properties the selected dispatch implies. Move call recognition into CallResolver.plan(c) (branch order preserved exactly) and reimplement resultType(c) as plan(c).return_type — the typing consumer converges onto the plan first. lowerCall is untouched; routing it through plan(c) is sub-step 3. 10 plan-object tests assert kind/target/variant + receiver/ctx/default properties for every pinned call form: builtin/reflection, lazy + resolved direct fn (incl. default-arg expansion + __sx_ctx prepend), closure / default-conv vs C-conv fn-pointer, protocol dispatch, struct/UFCS #compiler method, foreign instance vs static, qualified + dot-shorthand enum construction, namespace fn, and the unresolved fallthrough. Widen for the new collaborator only: resolveVariantIndex -> pub (plan resolves the variant tag); Scope/Binding + init/deinit/put -> pub (so unit tests can stand up a lexical scope for closure/fn-ptr callees without a full lowering). zig build, zig build test, and tests/run_examples.sh (357/0) all green; no behavior change.
This commit is contained in:
@@ -1,7 +1,15 @@
|
||||
// Tests for calls.zig — focused on the call-result-typing paths CallResolver
|
||||
// owns that need no lexical scope / fn registration: builtin and reflection
|
||||
// builtin classification, and the unresolved fallthrough. Reached via the
|
||||
// public `Lowering.inferExprType` delegation.
|
||||
// Tests for calls.zig.
|
||||
//
|
||||
// Two layers:
|
||||
// 1. Result-type delegation reached via the public `Lowering.inferExprType`
|
||||
// (builtin / reflection classification, cast, dot-shorthand fallthrough) —
|
||||
// these need no lexical scope / fn registration.
|
||||
// 2. The `CallPlan` object built by `CallResolver.plan` — its selected
|
||||
// kind / target / variant and the receiver / `__sx_ctx` / default-arg
|
||||
// properties, across every call form pinned by A3.2 sub-step 1
|
||||
// (direct / UFCS / protocol / closure / fn-pointer / foreign / enum /
|
||||
// namespace). `resultType` is just `plan(c).return_type`, so these also
|
||||
// lock the typing the regression suite relies on.
|
||||
|
||||
const std = @import("std");
|
||||
const ast = @import("../ast.zig");
|
||||
@@ -9,12 +17,49 @@ const Node = ast.Node;
|
||||
|
||||
const ir_mod = @import("ir.zig");
|
||||
const TypeId = ir_mod.TypeId;
|
||||
const FuncId = ir_mod.FuncId;
|
||||
const Ref = ir_mod.Ref;
|
||||
const Lowering = ir_mod.Lowering;
|
||||
const CallResolver = ir_mod.CallResolver;
|
||||
const CallPlan = ir_mod.CallPlan;
|
||||
|
||||
const lower = @import("lower.zig");
|
||||
const Scope = lower.Scope;
|
||||
const Binding = lower.Binding;
|
||||
const BuiltinId = @import("inst.zig").BuiltinId;
|
||||
|
||||
fn node(data: ast.Node.Data) Node {
|
||||
return .{ .span = .{ .start = 0, .end = 0 }, .data = data };
|
||||
}
|
||||
|
||||
// ── AST builders (heap-allocated so the call graph outlives one statement) ──
|
||||
|
||||
fn mk(alloc: std.mem.Allocator, data: ast.Node.Data) *Node {
|
||||
const n = alloc.create(Node) catch unreachable;
|
||||
n.* = .{ .span = .{ .start = 0, .end = 0 }, .data = data };
|
||||
return n;
|
||||
}
|
||||
fn ident(alloc: std.mem.Allocator, name: []const u8) *Node {
|
||||
return mk(alloc, .{ .identifier = .{ .name = name } });
|
||||
}
|
||||
fn typeExpr(alloc: std.mem.Allocator, name: []const u8) *Node {
|
||||
return mk(alloc, .{ .type_expr = .{ .name = name } });
|
||||
}
|
||||
fn intLit(alloc: std.mem.Allocator, v: i64) *Node {
|
||||
return mk(alloc, .{ .int_literal = .{ .value = v } });
|
||||
}
|
||||
fn emptyBody(alloc: std.mem.Allocator) *Node {
|
||||
return mk(alloc, .{ .block = .{ .stmts = &.{} } });
|
||||
}
|
||||
fn fieldAccess(alloc: std.mem.Allocator, obj: *Node, field: []const u8) *Node {
|
||||
return mk(alloc, .{ .field_access = .{ .object = obj, .field = field } });
|
||||
}
|
||||
fn callNode(alloc: std.mem.Allocator, callee: *Node, args: []const *Node) *Node {
|
||||
return mk(alloc, .{ .call = .{ .callee = callee, .args = args } });
|
||||
}
|
||||
|
||||
// ── Layer 1: result-type delegation (no scope / registration needed) ────────
|
||||
|
||||
test "calls: builtin and reflection result types, unknown fallthrough" {
|
||||
const alloc = std.testing.allocator;
|
||||
var module = ir_mod.Module.init(alloc);
|
||||
@@ -94,3 +139,303 @@ test "calls: dot-shorthand enum construction types as the target type" {
|
||||
l.target_type = .s32;
|
||||
try std.testing.expectEqual(TypeId.s32, l.inferExprType(&enum_call));
|
||||
}
|
||||
|
||||
// ── Layer 2: the CallPlan object (kind / target / variant / properties) ─────
|
||||
|
||||
test "plan: builtin and reflection carry kind + target" {
|
||||
const alloc = std.testing.allocator;
|
||||
var module = ir_mod.Module.init(alloc);
|
||||
defer module.deinit();
|
||||
var l = Lowering.init(&module);
|
||||
const cr = CallResolver{ .l = &l };
|
||||
|
||||
var arg = node(.{ .int_literal = .{ .value = 1 } });
|
||||
var args = [_]*Node{&arg};
|
||||
|
||||
var so_callee = node(.{ .identifier = .{ .name = "size_of" } });
|
||||
var so_call = node(.{ .call = .{ .callee = &so_callee, .args = &args } });
|
||||
const so = cr.plan(&so_call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.builtin, so.kind);
|
||||
try std.testing.expectEqual(BuiltinId.size_of, so.target.builtin);
|
||||
try std.testing.expectEqual(TypeId.s64, so.return_type);
|
||||
|
||||
var tn_callee = node(.{ .identifier = .{ .name = "type_name" } });
|
||||
var tn_call = node(.{ .call = .{ .callee = &tn_callee, .args = &args } });
|
||||
const tn = cr.plan(&tn_call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.reflection, tn.kind);
|
||||
try std.testing.expectEqualStrings("type_name", tn.target.named);
|
||||
try std.testing.expectEqual(TypeId.string, tn.return_type);
|
||||
}
|
||||
|
||||
test "plan: unresolved bare callee" {
|
||||
const alloc = std.testing.allocator;
|
||||
var module = ir_mod.Module.init(alloc);
|
||||
defer module.deinit();
|
||||
var l = Lowering.init(&module);
|
||||
const cr = CallResolver{ .l = &l };
|
||||
|
||||
var callee = node(.{ .identifier = .{ .name = "nope" } });
|
||||
var call = node(.{ .call = .{ .callee = &callee, .args = &.{} } });
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.unresolved, p.kind);
|
||||
try std.testing.expectEqual(TypeId.unresolved, p.return_type);
|
||||
}
|
||||
|
||||
test "plan: lazy free fn classifies as direct_fn and flags default-arg expansion" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
var module = ir_mod.Module.init(alloc);
|
||||
defer module.deinit();
|
||||
var l = Lowering.init(&module);
|
||||
const cr = CallResolver{ .l = &l };
|
||||
|
||||
// greet :: (a: s64, b: s64 = 0) -> s64 — registered but NOT lowered, so
|
||||
// it resolves through the AST (lazy) arm and `b`'s default is splice-able.
|
||||
const params = [_]ast.Param{
|
||||
.{ .name = "a", .name_span = .{ .start = 0, .end = 0 }, .type_expr = typeExpr(alloc, "s64") },
|
||||
.{ .name = "b", .name_span = .{ .start = 0, .end = 0 }, .type_expr = typeExpr(alloc, "s64"), .default_expr = intLit(alloc, 0) },
|
||||
};
|
||||
const fd = ast.FnDecl{ .name = "greet", .params = ¶ms, .return_type = typeExpr(alloc, "s64"), .body = emptyBody(alloc) };
|
||||
l.program_index.fn_ast_map.put("greet", &fd) catch unreachable;
|
||||
|
||||
// greet(1) — omits `b`, so its default is spliced in.
|
||||
{
|
||||
const one = [_]*Node{intLit(alloc, 1)};
|
||||
const call = callNode(alloc, ident(alloc, "greet"), &one);
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.direct_fn, p.kind);
|
||||
try std.testing.expectEqualStrings("greet", p.target.named);
|
||||
try std.testing.expectEqual(TypeId.s64, p.return_type);
|
||||
try std.testing.expect(p.expands_defaults);
|
||||
try std.testing.expect(!p.prepends_receiver);
|
||||
}
|
||||
// greet(1, 2) — all args supplied, no expansion.
|
||||
{
|
||||
const two = [_]*Node{ intLit(alloc, 1), intLit(alloc, 2) };
|
||||
const call = callNode(alloc, ident(alloc, "greet"), &two);
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.direct_fn, p.kind);
|
||||
try std.testing.expect(!p.expands_defaults);
|
||||
}
|
||||
}
|
||||
|
||||
test "plan: resolved free fn carries func target + __sx_ctx prepend" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
var module = ir_mod.Module.init(alloc);
|
||||
defer module.deinit();
|
||||
var l = Lowering.init(&module);
|
||||
const cr = CallResolver{ .l = &l };
|
||||
|
||||
// noop :: () { } — lowered, so it resolves to a concrete FuncId.
|
||||
const fd = ast.FnDecl{ .name = "noop", .params = &.{}, .return_type = null, .body = emptyBody(alloc) };
|
||||
l.lowerFunction(&fd, "noop", false);
|
||||
const fid = l.resolveFuncByName("noop").?;
|
||||
// Stamp the implicit-ctx flag the way the implicit-Context machinery would.
|
||||
module.functions.items[@intFromEnum(fid)].has_implicit_ctx = true;
|
||||
|
||||
var callee = node(.{ .identifier = .{ .name = "noop" } });
|
||||
var call = node(.{ .call = .{ .callee = &callee, .args = &.{} } });
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.direct_fn, p.kind);
|
||||
try std.testing.expectEqual(fid, p.target.func);
|
||||
try std.testing.expectEqual(TypeId.void, p.return_type);
|
||||
try std.testing.expect(p.prepends_ctx);
|
||||
}
|
||||
|
||||
test "plan: closure and fn-pointer callees, __sx_ctx by calling convention" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
var module = ir_mod.Module.init(alloc);
|
||||
defer module.deinit();
|
||||
var l = Lowering.init(&module);
|
||||
l.implicit_ctx_enabled = true;
|
||||
const cr = CallResolver{ .l = &l };
|
||||
|
||||
var scope = Scope.init(alloc, null);
|
||||
defer scope.deinit();
|
||||
l.scope = &scope;
|
||||
|
||||
// cb : Closure() -> bool — sx-side closure, carries ctx at slot 0.
|
||||
const closure_ty = module.types.closureType(&.{}, .bool);
|
||||
scope.put("cb", .{ .ref = Ref.none, .ty = closure_ty, .is_alloca = false });
|
||||
{
|
||||
const call = callNode(alloc, ident(alloc, "cb"), &.{});
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.closure, p.kind);
|
||||
try std.testing.expectEqualStrings("cb", p.target.named);
|
||||
try std.testing.expectEqual(TypeId.bool, p.return_type);
|
||||
try std.testing.expect(p.prepends_ctx);
|
||||
}
|
||||
|
||||
// fp : () -> s32 (default conv) — sx fn-pointer, carries ctx.
|
||||
const fp_ty = module.types.functionType(&.{}, .s32);
|
||||
scope.put("fp", .{ .ref = Ref.none, .ty = fp_ty, .is_alloca = false });
|
||||
{
|
||||
const call = callNode(alloc, ident(alloc, "fp"), &.{});
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.fn_pointer, p.kind);
|
||||
try std.testing.expectEqual(TypeId.s32, p.return_type);
|
||||
try std.testing.expect(p.prepends_ctx);
|
||||
}
|
||||
|
||||
// cfp : () -> s32 (C conv) — C fn-pointer, NO implicit ctx.
|
||||
const cfp_ty = module.types.functionTypeCC(&.{}, .s32, .c);
|
||||
scope.put("cfp", .{ .ref = Ref.none, .ty = cfp_ty, .is_alloca = false });
|
||||
{
|
||||
const call = callNode(alloc, ident(alloc, "cfp"), &.{});
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.fn_pointer, p.kind);
|
||||
try std.testing.expect(!p.prepends_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
test "plan: protocol dispatch selects method index + prepends receiver" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
var module = ir_mod.Module.init(alloc);
|
||||
defer module.deinit();
|
||||
var l = Lowering.init(&module);
|
||||
const cr = CallResolver{ .l = &l };
|
||||
|
||||
// Drawable :: protocol { measure :: () -> s64; draw :: () -> bool; }
|
||||
const methods = [_]ast.ProtocolMethodDecl{
|
||||
.{ .name = "measure", .params = &.{}, .param_names = &.{}, .return_type = typeExpr(alloc, "s64"), .default_body = null },
|
||||
.{ .name = "draw", .params = &.{}, .param_names = &.{}, .return_type = typeExpr(alloc, "bool"), .default_body = null },
|
||||
};
|
||||
const pd = ast.ProtocolDecl{ .name = "Drawable", .methods = &methods };
|
||||
l.registerProtocolDecl(&pd);
|
||||
|
||||
// A receiver typed as the protocol: `cast(Drawable, _)`.
|
||||
const recv = callNode(alloc, ident(alloc, "cast"), &[_]*Node{ typeExpr(alloc, "Drawable"), intLit(alloc, 0) });
|
||||
const call = callNode(alloc, fieldAccess(alloc, recv, "draw"), &.{});
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.protocol_dispatch, p.kind);
|
||||
try std.testing.expectEqual(@as(u32, 1), p.target.protocol_method);
|
||||
try std.testing.expectEqual(TypeId.bool, p.return_type);
|
||||
try std.testing.expect(p.prepends_receiver);
|
||||
}
|
||||
|
||||
test "plan: struct (UFCS) method via #compiler dispatch + prepends receiver" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
var module = ir_mod.Module.init(alloc);
|
||||
defer module.deinit();
|
||||
var l = Lowering.init(&module);
|
||||
const cr = CallResolver{ .l = &l };
|
||||
|
||||
// struct Point, with a `#compiler` method Point.scale(self) -> s64.
|
||||
_ = module.types.intern(.{ .@"struct" = .{ .name = module.types.internString("Point"), .fields = &.{} } });
|
||||
const self_param = ast.Param{ .name = "self", .name_span = .{ .start = 0, .end = 0 }, .type_expr = typeExpr(alloc, "Point") };
|
||||
const params = [_]ast.Param{self_param};
|
||||
const compiler_body = mk(alloc, .{ .compiler_expr = {} });
|
||||
const method_fd = ast.FnDecl{ .name = "Point.scale", .params = ¶ms, .return_type = typeExpr(alloc, "s64"), .body = compiler_body };
|
||||
l.program_index.fn_ast_map.put("Point.scale", &method_fd) catch unreachable;
|
||||
|
||||
const recv = callNode(alloc, ident(alloc, "cast"), &[_]*Node{ typeExpr(alloc, "Point"), intLit(alloc, 0) });
|
||||
const call = callNode(alloc, fieldAccess(alloc, recv, "scale"), &.{});
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.struct_method, p.kind);
|
||||
try std.testing.expectEqualStrings("Point.scale", p.target.named);
|
||||
try std.testing.expectEqual(TypeId.s64, p.return_type);
|
||||
try std.testing.expect(p.prepends_receiver);
|
||||
}
|
||||
|
||||
test "plan: foreign-class instance vs static dispatch" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
var module = ir_mod.Module.init(alloc);
|
||||
defer module.deinit();
|
||||
var l = Lowering.init(&module);
|
||||
const cr = CallResolver{ .l = &l };
|
||||
|
||||
const members = [_]ast.ForeignClassMember{
|
||||
.{ .method = .{ .name = "length", .params = &.{}, .param_names = &.{}, .return_type = typeExpr(alloc, "s64"), .is_static = false } },
|
||||
.{ .method = .{ .name = "stringWithUTF8String", .params = &.{}, .param_names = &.{}, .return_type = typeExpr(alloc, "s64"), .is_static = true } },
|
||||
};
|
||||
var fcd = ast.ForeignClassDecl{ .name = "NSString", .foreign_path = "NSString", .runtime = .objc_class, .members = &members };
|
||||
l.program_index.foreign_class_map.put("NSString", &fcd) catch unreachable;
|
||||
_ = module.types.intern(.{ .@"struct" = .{ .name = module.types.internString("NSString"), .fields = &.{} } });
|
||||
|
||||
// Instance: `cast(NSString, _).length` — receiver prepended.
|
||||
{
|
||||
const recv = callNode(alloc, ident(alloc, "cast"), &[_]*Node{ typeExpr(alloc, "NSString"), intLit(alloc, 0) });
|
||||
const call = callNode(alloc, fieldAccess(alloc, recv, "length"), &.{});
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.foreign_instance, p.kind);
|
||||
try std.testing.expectEqualStrings("length", p.target.foreign_method.name);
|
||||
try std.testing.expect(!p.target.foreign_method.is_static);
|
||||
try std.testing.expectEqual(TypeId.s64, p.return_type);
|
||||
try std.testing.expect(p.prepends_receiver);
|
||||
}
|
||||
// Static: `NSString.stringWithUTF8String(...)` — no receiver.
|
||||
{
|
||||
const call = callNode(alloc, fieldAccess(alloc, ident(alloc, "NSString"), "stringWithUTF8String"), &.{});
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.foreign_static, p.kind);
|
||||
try std.testing.expectEqualStrings("stringWithUTF8String", p.target.foreign_method.name);
|
||||
try std.testing.expect(p.target.foreign_method.is_static);
|
||||
try std.testing.expect(!p.prepends_receiver);
|
||||
}
|
||||
}
|
||||
|
||||
test "plan: enum construction (qualified + dot-shorthand) carries variant tag" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
var module = ir_mod.Module.init(alloc);
|
||||
defer module.deinit();
|
||||
var l = Lowering.init(&module);
|
||||
const cr = CallResolver{ .l = &l };
|
||||
|
||||
const red = module.types.internString("Red");
|
||||
const green = module.types.internString("Green");
|
||||
const variants = [_]@TypeOf(red){ red, green };
|
||||
const color = module.types.intern(.{ .@"enum" = .{ .name = module.types.internString("Color"), .variants = &variants } });
|
||||
|
||||
// Qualified: `Color.Green`.
|
||||
{
|
||||
const call = callNode(alloc, fieldAccess(alloc, typeExpr(alloc, "Color"), "Green"), &.{});
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.enum_construct, p.kind);
|
||||
try std.testing.expectEqual(color, p.target.constructed);
|
||||
try std.testing.expectEqual(@as(?u32, 1), p.variant);
|
||||
try std.testing.expectEqual(color, p.return_type);
|
||||
}
|
||||
// Dot-shorthand: `.Green` with the union as the target type.
|
||||
{
|
||||
l.target_type = color;
|
||||
const call = callNode(alloc, mk(alloc, .{ .enum_literal = .{ .name = "Green" } }), &.{});
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.enum_shorthand, p.kind);
|
||||
try std.testing.expectEqual(color, p.target.constructed);
|
||||
try std.testing.expectEqual(@as(?u32, 1), p.variant);
|
||||
try std.testing.expectEqual(color, p.return_type);
|
||||
}
|
||||
}
|
||||
|
||||
test "plan: qualified namespace function" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
var module = ir_mod.Module.init(alloc);
|
||||
defer module.deinit();
|
||||
var l = Lowering.init(&module);
|
||||
const cr = CallResolver{ .l = &l };
|
||||
|
||||
// mathlib.square :: () -> s64 — registered under its qualified name, lazy.
|
||||
const fd = ast.FnDecl{ .name = "mathlib.square", .params = &.{}, .return_type = typeExpr(alloc, "s64"), .body = emptyBody(alloc) };
|
||||
l.program_index.fn_ast_map.put("mathlib.square", &fd) catch unreachable;
|
||||
|
||||
const call = callNode(alloc, fieldAccess(alloc, ident(alloc, "mathlib"), "square"), &.{});
|
||||
const p = cr.plan(&call.data.call);
|
||||
try std.testing.expectEqual(CallPlan.Kind.namespace_fn, p.kind);
|
||||
try std.testing.expectEqualStrings("mathlib.square", p.target.named);
|
||||
try std.testing.expectEqual(TypeId.s64, p.return_type);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user