spl 编写编译器基础框架 ai修复出现的stage2和stage3问题并提供新语法

This commit is contained in:
zzy committed 2026-11-07 21:52:02 +08:00
1 parent 126b979f72
commit 50ee126d44
87 files changed
+3168 -394

No files matched your search

+29
View File
@@ -1,6 +1,35 @@
// Expect retcode = 0
type meta = @import("meta");
type .{ .Ref } = meta;
#hook(param)
macro fn Self(callee: Ref, value: Ref, implicit: Ref): Ref {
if implicit == 0 {
if value != 0 { ret value; }
ret 0;
}
var cn = @meta.inspect(callee);
match cn.kind {
.FnDecl(f) => {
var pn = @meta.inspect(f.params[0]);
match pn.kind {
.ParamDecl(p) => {
var tn = @meta.inspect(p.ty);
match tn.kind {
.PtrType(_) => { ret @meta.construct(.{ .kind = .Addr(implicit) }); },
_ => {},
}
},
_ => {},
}
},
_ => {},
}
ret implicit;
}
type Pair = struct {
a: i32,
+3 -2
View File
@@ -7,8 +7,9 @@ type .{ .Ref } = meta;
type .{ .Map } = @import("../support/generic_bind_fixture.spl");
#hook(param)
macro fn Self(callee: Ref, value: Ref): Ref {
ret @meta.construct(.{ .kind = .Addr(value) });
macro fn Self(callee: Ref, value: Ref, implicit: Ref): Ref {
if implicit == 0 { ret value; }
ret @meta.construct(.{ .kind = .Addr(implicit) });
}
fn main() {
+3 -2
View File
@@ -7,8 +7,9 @@ type .{ .Ref } = meta;
type .{ .Map } = @import("../support/generic_bind_fixture.spl");
#hook(param)
macro fn Self(callee: Ref, value: Ref): Ref {
ret @meta.construct(.{ .kind = .Addr(value) });
macro fn Self(callee: Ref, value: Ref, implicit: Ref): Ref {
if implicit == 0 { ret value; }
ret @meta.construct(.{ .kind = .Addr(implicit) });
}
fn main() {
+1 -1
View File
@@ -4,7 +4,7 @@ type meta = @import("meta");
type .{ .Node, .Ref } = @import("spl_ir");
#hook(param)
macro fn unused(callee: Ref, value: Ref): Ref {
macro fn unused(callee: Ref, value: Ref, implicit: Ref): Ref {
ret @meta.construct(.{ .kind = .LitInt("9") });
}
+29
View File
@@ -1,6 +1,35 @@
// Expect retcode = 0
type meta = @import("meta");
type .{ .Ref } = meta;
#hook(param)
macro fn Self(callee: Ref, value: Ref, implicit: Ref): Ref {
if implicit == 0 {
if value != 0 { ret value; }
ret 0;
}
var cn = @meta.inspect(callee);
match cn.kind {
.FnDecl(f) => {
var pn = @meta.inspect(f.params[0]);
match pn.kind {
.ParamDecl(p) => {
var tn = @meta.inspect(p.ty);
match tn.kind {
.PtrType(_) => { ret @meta.construct(.{ .kind = .Addr(implicit) }); },
_ => {},
}
},
_ => {},
}
},
_ => {},
}
ret implicit;
}
type Pair = struct { a: i32, b: u8 };
type Inner = struct { x: i16, y: bool };
+29
View File
@@ -1,6 +1,35 @@
// Expect retcode = 0
type meta = @import("meta");
type .{ .Ref } = meta;
#hook(param)
macro fn Self(callee: Ref, value: Ref, implicit: Ref): Ref {
if implicit == 0 {
if value != 0 { ret value; }
ret 0;
}
var cn = @meta.inspect(callee);
match cn.kind {
.FnDecl(f) => {
var pn = @meta.inspect(f.params[0]);
match pn.kind {
.ParamDecl(p) => {
var tn = @meta.inspect(p.ty);
match tn.kind {
.PtrType(_) => { ret @meta.construct(.{ .kind = .Addr(implicit) }); },
_ => {},
}
},
_ => {},
}
},
_ => {},
}
ret implicit;
}
type Pair = struct { a: i32, b: u8 };
type Arr3 = [3]u16;
+27
View File
@@ -1,6 +1,33 @@
// Expect retcode = 0
type meta = @import("meta");
#hook(param)
macro fn Self(callee: Ref, value: Ref, implicit: Ref): Ref {
if implicit == 0 {
if value != 0 { ret value; }
ret 0;
}
var cn = @meta.inspect(callee);
match cn.kind {
.FnDecl(f) => {
var pn = @meta.inspect(f.params[0]);
match pn.kind {
.ParamDecl(p) => {
var tn = @meta.inspect(p.ty);
match tn.kind {
.PtrType(_) => { ret @meta.construct(.{ .kind = .Addr(implicit) }); },
_ => {},
}
},
_ => {},
}
},
_ => {},
}
ret implicit;
}
type .{ .Node, .Ref } = @import("spl_ir");
type VoidT = void;
+27
View File
@@ -1,6 +1,33 @@
// Expect retcode = 0
type meta = @import("meta");
#hook(param)
macro fn Self(callee: Ref, value: Ref, implicit: Ref): Ref {
if implicit == 0 {
if value != 0 { ret value; }
ret 0;
}
var cn = @meta.inspect(callee);
match cn.kind {
.FnDecl(f) => {
var pn = @meta.inspect(f.params[0]);
match pn.kind {
.ParamDecl(p) => {
var tn = @meta.inspect(p.ty);
match tn.kind {
.PtrType(_) => { ret @meta.construct(.{ .kind = .Addr(implicit) }); },
_ => {},
}
},
_ => {},
}
},
_ => {},
}
ret implicit;
}
type .{ .Node, .Ref } = @import("spl_ir");
comptime fn tags_roundtrip(): usize {
@@ -1,6 +1,33 @@
// Expect retcode = 0
type meta = @import("meta");
#hook(param)
macro fn Self(callee: Ref, value: Ref, implicit: Ref): Ref {
if implicit == 0 {
if value != 0 { ret value; }
ret 0;
}
var cn = @meta.inspect(callee);
match cn.kind {
.FnDecl(f) => {
var pn = @meta.inspect(f.params[0]);
match pn.kind {
.ParamDecl(p) => {
var tn = @meta.inspect(p.ty);
match tn.kind {
.PtrType(_) => { ret @meta.construct(.{ .kind = .Addr(implicit) }); },
_ => {},
}
},
_ => {},
}
},
_ => {},
}
ret implicit;
}
type .{ .Node, .Ref } = @import("spl_ir");
comptime fn int_node(bits: usize, signed: bool): Ref {
+27
View File
@@ -1,6 +1,33 @@
// Expect retcode = 0
type meta = @import("meta");
#hook(param)
macro fn Self(callee: Ref, value: Ref, implicit: Ref): Ref {
if implicit == 0 {
if value != 0 { ret value; }
ret 0;
}
var cn = @meta.inspect(callee);
match cn.kind {
.FnDecl(f) => {
var pn = @meta.inspect(f.params[0]);
match pn.kind {
.ParamDecl(p) => {
var tn = @meta.inspect(p.ty);
match tn.kind {
.PtrType(_) => { ret @meta.construct(.{ .kind = .Addr(implicit) }); },
_ => {},
}
},
_ => {},
}
},
_ => {},
}
ret implicit;
}
type .{ .method, .type_name } = @import("./meta_utils.spl");
type .{ .Node, .Ref } = @import("spl_ir");
+27
View File
@@ -1,6 +1,33 @@
// Expect retcode = 0
type meta = @import("meta");
#hook(param)
macro fn Self(callee: Ref, value: Ref, implicit: Ref): Ref {
if implicit == 0 {
if value != 0 { ret value; }
ret 0;
}
var cn = @meta.inspect(callee);
match cn.kind {
.FnDecl(f) => {
var pn = @meta.inspect(f.params[0]);
match pn.kind {
.ParamDecl(p) => {
var tn = @meta.inspect(p.ty);
match tn.kind {
.PtrType(_) => { ret @meta.construct(.{ .kind = .Addr(implicit) }); },
_ => {},
}
},
_ => {},
}
},
_ => {},
}
ret implicit;
}
type .{ .Node, .Ref } = @import("spl_ir");
comptime fn int_ref(): Ref {
+3 -6
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .identity } = @import("./support/shape_identity.spl");
type Cal = shape {
fn call(a: i32, b: i32): i32;
}
@@ -16,12 +18,7 @@ type Sub = struct {
}
}
#hook(adapter)
macro fn static(called_type: type, shape_type: type): Ref {
ret 0;
}
fn Cal1(c: @static[Cal], a: i32, b: i32): i32 {
fn Cal1(c: @identity[Cal], a: i32, b: i32): i32 {
ret c.call(a, b);
}
+2 -5
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .static } = @import("./support/shape_identity.spl");
type Cal = shape {
fn call(a: i32, b: i32): i32;
}
@@ -16,11 +18,6 @@ type Sub = struct {
}
}
#hook(adapter)
macro fn static(called_type: type, shape_type: type): Ref {
ret 0;
}
fn Both(a: @static[Cal], b: @static[Cal], x: i32, y: i32): i32 {
ret a.call(x, y) + b.call(x, y);
}
+2 -5
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .wrap } = @import("./support/shape_identity.spl");
type Cal = shape {
fn call(a: i32, b: i32): i32;
}
@@ -16,11 +18,6 @@ type Sub = struct {
}
}
#hook(adapter)
macro fn wrap(called_type: type, shape_type: type): Ref {
ret 0;
}
fn Use(c: @wrap[Cal], a: i32, b: i32): i32 {
ret c.call(a, b);
}
+2 -5
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .dyn } = @import("./support/shape_identity.spl");
type .{ .Ref } = @import("spl_ir");
type Cal = shape {
@@ -14,11 +16,6 @@ type Sub = struct {
fn call(a: i32, b: i32): i32 { ret a - b; }
};
#hook(adapter)
macro fn dyn(called_type: type, shape_type: type): Ref {
ret 0;
}
fn Use(c: @dyn[Cal], a: i32, b: i32): i32 {
ret c.call(a, b);
}
+2 -5
View File
@@ -1,5 +1,7 @@
// Expect compile_error E0305
type .{ .bad_adapter } = @import("./support/shape_identity.spl");
type Cal = shape {
fn call(a: i32, b: i32): i32;
fn tag(): i32;
@@ -14,11 +16,6 @@ type Wrong = struct {
fn tag(): i32 { ret 1; }
}
#hook(adapter)
macro fn bad_adapter(called_type: type, shape_type: type): Ref {
ret 0;
}
fn UseMissing(c: @bad_adapter[Cal], a: i32, b: i32): i32 {
ret c.call(a, b) + c.tag();
}
+2 -5
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .static } = @import("./support/shape_identity.spl");
type SelfShape = shape {
fn value(self: *@self): i32;
fn same(self: *@self): *@self;
@@ -17,11 +19,6 @@ type Node = struct: SelfShape {
}
}
#hook(adapter)
macro fn static(called_type: type, shape_type: type): Ref {
ret 0;
}
fn use_self(n: @static[SelfShape]): i32 {
var p = n.same(&n);
ret p.value(p);
+2 -5
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .static } = @import("./support/shape_identity.spl");
comptime fn Step(T: type): type {
ret enum {
Item: T,
@@ -26,11 +28,6 @@ type Counter = struct: Iter[Item = i32] {
}
}
#hook(adapter)
macro fn static(called_type: type, shape_type: type): Ref {
ret 0;
}
fn first(it: @static[Iter[Item = i32]]): i32 {
var s = it.next(&it);
match s {
+2 -5
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .static } = @import("./support/shape_identity.spl");
comptime fn Step(T: type): type {
ret enum {
Item: T,
@@ -34,11 +36,6 @@ type CursorIter = struct: Iter[Item = i32] {
}
}
#hook(adapter)
macro fn static(called_type: type, shape_type: type): Ref {
ret 0;
}
fn first(it: @static[Iter[Item = i32]]): i32 {
var s = it.next(&it);
match s {
+2 -5
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .static } = @import("./support/shape_identity.spl");
comptime fn Step(T: type): type {
ret enum {
Item: T,
@@ -34,11 +36,6 @@ type Counter = struct: Iter[Item = i32] + Tag {
}
}
#hook(adapter)
macro fn static(called_type: type, shape_type: type): Ref {
ret 0;
}
fn use_both(it: @static[Iter[Item = i32] + Tag]): i32 {
var s = it.next(&it);
match s {
+2 -10
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .sa_adapter, .sb_adapter } = @import("./support/shape_identity.spl");
type .{ .Ref } = @import("spl_ir");
type SA = shape {
@@ -18,16 +20,6 @@ type B = struct {
fn b(): i32 { ret 22; }
};
#hook(adapter)
macro fn sa_adapter(called_type: type, shape_type: type): Ref {
ret 0;
}
#hook(adapter)
macro fn sb_adapter(called_type: type, shape_type: type): Ref {
ret 0;
}
fn use_a(x: @sa_adapter[SA]): i32 {
ret x.a();
}
+2 -5
View File
@@ -1,5 +1,7 @@
// Expect compile_error E0317
type .{ .alloc } = @import("./support/shape_identity.spl");
type .{ .Ref } = @import("spl_ir");
type SA = shape {
@@ -10,11 +12,6 @@ type Bad = struct {
fn b(): i32 { ret 1; }
};
#hook(adapter)
macro fn alloc(called_type: type, shape_type: type): Ref {
ret 0;
}
fn use_a(x: @alloc[SA]): i32 {
ret x.a();
}
+2 -5
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .static } = @import("./support/shape_identity.spl");
type ValueShape = shape {
fn value(self: *@self): i32;
}
@@ -12,11 +14,6 @@ type Node = struct {
}
}
#hook(adapter)
macro fn static(called_type: type, shape_type: type): Ref {
ret 0;
}
fn read(n: @static[ValueShape]): i32 {
ret n.value(&n);
}
+2 -5
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .static } = @import("./support/shape_identity.spl");
type Sink = shape {
type Item;
fn put(self: *@self, value: Item): Item;
@@ -14,11 +16,6 @@ type IntSink = struct: Sink[Item = i32] {
}
}
#hook(adapter)
macro fn static(called_type: type, shape_type: type): Ref {
ret 0;
}
fn add(s: @static[Sink[Item = i32]], value: i32): i32 {
ret s.put(&s, value);
}
+2 -5
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .static } = @import("./support/shape_identity.spl");
type Base = shape {
fn base(self: *@self): i32;
}
@@ -20,11 +22,6 @@ type Impl = struct: Derived {
}
}
#hook(adapter)
macro fn static(called_type: type, shape_type: type): Ref {
ret 0;
}
fn use(d: @static[Derived]): i32 {
ret d.base(&d) + d.extra(&d);
}
+1 -1
View File
@@ -22,5 +22,5 @@ fn main(): void {
@assert(c.value_copy() == 1, "self value");
c.set(7);
@assert(c.get() == 7, "self ref set");
@assert(c.get(&c) == 7, "explicit receiver");
@assert(c.get() == 7, "implicit receiver");
}
+1 -1
View File
@@ -7,7 +7,7 @@ comptime fn Pair(T: type): type {
a: T,
b: T,
fn add(lhs: @self, rhs: @self): @self {
fn add(@Self lhs: @self, rhs: @self): @self {
ret .{ .a = lhs.a + rhs.a, .b = lhs.b + rhs.b };
}
};
+1 -1
View File
@@ -6,7 +6,7 @@ comptime fn NumBox(T: type): type {
ret struct: Add[Rhs = T, Output = @self] {
v: T,
fn add(lhs: @self, rhs: T): @self {
fn add(@Self lhs: @self, rhs: T): @self {
ret .{ .v = lhs.v + rhs };
}
};
+1 -1
View File
@@ -4,7 +4,7 @@ type meta = @import("meta");
type .{ .Node, .Ref } = @import("spl_ir");
#hook(param)
macro fn replace(callee: Ref, value: Ref): Ref {
macro fn replace(callee: Ref, value: Ref, implicit: Ref): Ref {
ret @meta.construct(.{ .kind = .LitInt("7") });
}
+2 -2
View File
@@ -4,12 +4,12 @@ type meta = @import("meta");
type .{ .Node, .Ref } = @import("spl_ir");
#hook(param)
macro fn seven(callee: Ref, value: Ref): Ref {
macro fn seven(callee: Ref, value: Ref, implicit: Ref): Ref {
ret @meta.construct(.{ .kind = .LitInt("7") });
}
#hook(param)
macro fn nine(callee: Ref, value: Ref): Ref {
macro fn nine(callee: Ref, value: Ref, implicit: Ref): Ref {
ret @meta.construct(.{ .kind = .LitInt("9") });
}
+2 -2
View File
@@ -4,7 +4,7 @@ type meta = @import("meta");
type .{ .Node, .Ref } = @import("spl_ir");
#hook(param)
macro fn plus_one(callee: Ref, value: Ref): Ref {
macro fn plus_one(callee: Ref, value: Ref, implicit: Ref): Ref {
var one = @meta.construct(.{ .kind = .LitInt("1") });
var sum = @meta.construct(.{
.kind = .Binary(.{ .op = .Add, .lval = value, .rval = one }),
@@ -13,7 +13,7 @@ macro fn plus_one(callee: Ref, value: Ref): Ref {
}
#hook(param)
macro fn times_two(callee: Ref, value: Ref): Ref {
macro fn times_two(callee: Ref, value: Ref, implicit: Ref): Ref {
var two = @meta.construct(.{ .kind = .LitInt("2") });
var product = @meta.construct(.{
.kind = .Binary(.{ .op = .Mul, .lval = value, .rval = two }),
+1 -1
View File
@@ -4,7 +4,7 @@ type meta = @import("meta");
type .{ .Node, .Ref } = @import("spl_ir");
#hook(param)
macro fn check_callee(callee: Ref, value: Ref): Ref {
macro fn check_callee(callee: Ref, value: Ref, implicit: Ref): Ref {
var n = @meta.inspect(callee);
match n.kind {
.FnDecl(f) => {
+1 -1
View File
@@ -4,7 +4,7 @@ type meta = @import("meta");
type .{ .Node, .Ref } = @import("spl_ir");
#hook(param)
macro fn default_seven(callee: Ref, value: Ref): Ref {
macro fn default_seven(callee: Ref, value: Ref, implicit: Ref): Ref {
if value != 0 { ret value; }
ret @meta.construct(.{ .kind = .LitInt("7") });
}
+2 -5
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .identity } = @import("./support/shape_identity.spl");
type .{ .Ref } = @import("spl_ir");
type Cal = shape {
@@ -10,11 +12,6 @@ type A = struct {
fn call(): i32 { ret 5; }
}
#hook(adapter)
macro fn identity(called_type: type, shape_type: type): Ref {
ret 0;
}
fn use(c: @identity[Cal]): i32 {
ret c.call();
}
+5 -3
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .identity } = @import("./support/shape_identity.spl");
type .{ .Ref } = @import("spl_ir");
type Cal = shape {
@@ -12,11 +14,11 @@ type A = struct {
// kind 只是声明信息;位置和签名才是 impl hook 的权威。
#hook(param)
macro fn identity(called_type: type, shape_type: type): Ref {
ret 0;
macro fn impl_identity(called_type: type, shape_type: type): Ref {
ret @identity(called_type, shape_type);
}
fn use(c: @identity[Cal]): i32 {
fn use(c: @impl_identity[Cal]): i32 {
ret c.call();
}
+5 -3
View File
@@ -1,5 +1,7 @@
// Expect retcode = 0
type .{ .identity } = @import("./support/shape_identity.spl");
type .{ .Ref } = @import("spl_ir");
type Cal = shape {
@@ -11,11 +13,11 @@ type A = struct {
}
// 没有 #hook tag;位置和签名仍应决定它是 impl hook。
macro fn identity(called_type: type, shape_type: type): Ref {
ret 0;
macro fn impl_identity(called_type: type, shape_type: type): Ref {
ret @identity(called_type, shape_type);
}
fn use(c: @identity[Cal]): i32 {
fn use(c: @impl_identity[Cal]): i32 {
ret c.call();
}
@@ -0,0 +1,17 @@
// Expect compile_error E0106
// Expect location = 9:21
comptime fn Bad(T: type): type {
ret struct {
v: T,
fn broken(self: @self): T {
ret self.missing;
}
};
}
fn main() {
var b: @Bad(i32) = .{ .v = 1 };
var x = b.broken();
}
@@ -0,0 +1,17 @@
// Expect compile_error E0105 E0106
// Expect location = 9:26
comptime fn Bad(T: type): type {
ret struct {
v: T,
fn broken(self: @self): usize {
ret self.nope();
}
};
}
fn main() {
var b: @Bad(i32) = .{ .v = 1 };
var x = b.broken();
}
@@ -0,0 +1,57 @@
// Expect retcode = 0
#extern(vm) fn vm_alloc(sz: usize): *any;
type AllocError = enum {
OutOfMemory,
};
comptime fn Result(T: type, E: type): type {
ret enum {
#ok
Ok: T,
#error
Err: E,
};
}
type Alloc = struct {
fn alloc(@Self self: *@self, size: usize): @Result(*any, AllocError) {
var p = vm_alloc(size);
if p == null {
ret .(.Err(.OutOfMemory));
}
ret .(.Ok(p));
}
};
comptime fn Box(T: type): type {
ret struct {
data: *T,
len: usize,
cap: usize,
fn make(alloc: Alloc, n: usize): @self {
var v: @self = .{ .data = null, .len = 0, .cap = n };
var p: *any = null;
match alloc.alloc(@sizeof(T) * n) {
.Ok(raw) => { p = raw; },
.Err(e) => { panic("alloc failed"); },
}
v.data = p as *T;
ret v;
}
fn push(@Self self: *@self, v: T) {
self.data[self.len] = v;
self.len = self.len + 1;
}
};
}
fn main() {
var b = @Box(i32).make(Alloc{}, 4);
b.push(7);
@assert(b.data[0] == 7, "value");
}
@@ -0,0 +1,60 @@
// Expect retcode = 0
#extern(vm) fn vm_alloc(sz: usize): *any;
type AllocError = enum {
OutOfMemory,
};
comptime fn Result(T: type, E: type): type {
ret enum {
#ok
Ok: T,
#error
Err: E,
};
}
type Alloc = struct {
fn alloc(@Self self: *@self, size: usize): @Result(*any, AllocError) {
var p = vm_alloc(size);
if p == null {
ret .(.Err(.OutOfMemory));
}
ret .(.Ok(p));
}
};
fn take_alloc(r: @Result(*any, AllocError)): *any {
match r {
.Ok(p) => { ret p; },
.Err(e) => { panic("alloc failed"); ret null; },
}
}
comptime fn Box(T: type): type {
ret struct {
data: *T,
len: usize,
cap: usize,
fn make(alloc: Alloc, n: usize): @self {
var v: @self = .{ .data = null, .len = 0, .cap = n };
var p = take_alloc(alloc.alloc(@sizeof(T) * n));
v.data = p as *T;
ret v;
}
fn push(@Self self: *@self, v: T) {
self.data[self.len] = v;
self.len = self.len + 1;
}
};
}
fn main() {
var b = @Box(i32).make(Alloc{}, 4);
b.push(7);
@assert(b.data[0] == 7, "value");
}
+110
View File
@@ -0,0 +1,110 @@
// Expect retcode = 0
#extern(vm) fn vm_alloc(sz: usize): *any;
#extern(vm) fn vm_free(p: *any);
type AllocError = enum { OutOfMemory };
comptime fn Result(T: type, E: type): type {
ret enum {
#ok
Ok: T,
#error
Err: E,
};
}
type Alloc = struct {
fn alloc(@Self self: *@self, size: usize): @Result(*any, AllocError) {
var p = vm_alloc(size);
if p == null { ret .(.Err(.OutOfMemory)); }
ret .(.Ok(p));
}
fn dealloc(@Self self: *@self, ptr: *any): void {
vm_free(ptr);
}
};
comptime fn RawVec(T: type): type {
ret struct {
data: *T,
length: usize,
cap: usize,
fn empty(): @self {
ret .{ .data = null, .length = 0, .cap = 0 };
}
fn len(@Self self: *@self): usize {
ret self.length;
}
fn capacity(@Self self: *@self): usize {
ret self.cap;
}
fn push_unchecked(@Self self: *@self, v: T) {
self.data[self.length] = v;
self.length = self.length + 1;
}
};
}
fn grow_capacity(current: usize, min_cap: usize, need: usize): usize {
var cap = current;
if cap < min_cap { cap = min_cap; }
while cap < need { cap = cap * 2; }
ret cap;
}
comptime fn Grow(T: type, A: type): type {
ret struct {
fn reserve(base: *@RawVec(T), alloc: A, additional: usize) {
var need = base.len() + additional;
if need <= base.capacity() { ret; }
var new_cap = grow_capacity(base.capacity(), 8, need);
var p: *u8 = null;
match alloc.alloc(@sizeof(T) * new_cap) {
.Ok(raw) => { p = raw as *u8; },
.Err(e) => { panic("alloc failed"); },
}
base.data = p as *T;
base.cap = new_cap;
}
};
}
comptime fn VecOwned(T: type, A: type): type {
ret struct {
base: @RawVec(T),
alloc: A,
fn with_capacity(alloc: A, n: usize): @self {
var v: @self = .{ .base = @RawVec(T).empty(), .alloc = alloc };
if n == 0 { ret v; }
@Grow(T, A).reserve(&v.base, v.alloc, n);
ret v;
}
fn push(@Self self: *@self, v: T) {
if self.base.len() + 1 > self.base.cap {
@Grow(T, A).reserve(&self.base, self.alloc, 1);
}
@RawVec(T).push_unchecked(&self.base, v);
}
fn get(@Self self: *@self, i: usize): T {
ret self.base.data[i];
}
};
}
fn main() {
var v = @VecOwned([]u8, Alloc).with_capacity(Alloc{}, 3);
v.push("aa");
v.push("bb");
v.push("cc");
@assert(v.get(0).len() == 2, "aa");
@assert(v.get(2)[0] == 'c', "cc");
}
@@ -0,0 +1,47 @@
// Expect retcode = 0
#extern(vm) fn vm_alloc(sz: usize): *any;
#extern(vm) fn vm_free(p: *any);
type Allocator = shape {
fn alloc(@Self self: *@self, size: usize): *any;
fn dealloc(@Self self: *@self, ptr: *any);
};
type VmAlloc = struct: Allocator {
fn alloc(@Self self: *@self, size: usize): *any {
ret vm_alloc(size);
}
fn dealloc(@Self self: *@self, ptr: *any) {
vm_free(ptr);
}
};
comptime fn Box(T: type, A: Allocator): type {
ret struct {
p: *T,
alloc: A,
fn make(alloc: A): @self {
var b: @self = .{ .p = null, .alloc = alloc };
b.p = alloc.alloc(@sizeof(T)) as *T;
ret b;
}
fn set(@Self self: *@self, v: T) {
self.p[0] = v;
}
fn get(@Self self: *@self): T {
ret self.p[0];
}
};
}
fn main() {
@assert(@sizeof(@Box(i32, VmAlloc)) == @sizeof(*i32), "zst alloc field");
var b = @Box(i32, VmAlloc).make(VmAlloc{});
b.set(42);
@assert(b.get() == 42, "bound ok");
}
@@ -0,0 +1,44 @@
// Expect retcode = 0
type Sink = shape {
type Item;
fn put(@Self self: *@self, v: Item);
fn total(@Self self: *@self): i32;
};
type IntSink = struct: Sink[Item = i32] {
sum: i32,
fn put(@Self self: *@self, v: i32) {
self.sum = self.sum + v;
}
fn total(@Self self: *@self): i32 {
ret self.sum;
}
};
comptime fn Writer(S: Sink[Item = i32]): type {
ret struct {
sink: S,
fn make(sink: S): @self {
ret .{ .sink = sink };
}
fn add(@Self self: *@self, v: i32) {
self.sink.put(v);
}
fn total(@Self self: *@self): i32 {
ret self.sink.total();
}
};
}
fn main() {
var w = @Writer(IntSink).make(IntSink{ .sum = 0 });
w.add(3);
w.add(4);
@assert(w.total() == 7, "assoc bound");
}
@@ -0,0 +1,46 @@
// Expect retcode = 0
type Allocator = shape {
fn alloc(@Self self: *@self, size: usize): *any;
};
type Logger = shape {
fn log(@Self self: *@self, v: i32);
};
type Both = struct: Allocator + Logger {
mark: i32,
fn alloc(@Self self: *@self, size: usize): *any {
ret null;
}
fn log(@Self self: *@self, v: i32) {
self.mark = v;
}
};
comptime fn Use(A: Allocator + Logger): type {
ret struct {
a: A,
fn make(a: A): @self {
ret .{ .a = a };
}
fn run(@Self self: *@self, v: i32) {
self.a.log(v);
self.a.alloc(4);
}
fn mark(@Self self: *@self): i32 {
ret self.a.mark;
}
};
}
fn main() {
var u = @Use(Both).make(Both{ .mark = 0 });
u.run(9);
@assert(u.mark() == 9, "composed bound");
}
@@ -0,0 +1,38 @@
// Expect retcode = 0
type .{ .static } = @import("./support/shape_identity.spl");
type .{ .Ref } = @import("spl_ir");
type Allocator = shape {
fn alloc(@Self self: *@self, size: usize): *any;
};
type VmAlloc = struct: Allocator {
fn alloc(@Self self: *@self, size: usize): *any {
ret null;
}
};
comptime fn Box(T: type, A: @static[Allocator]): type {
ret struct {
p: *T,
alloc: A,
fn make(alloc: A): @self {
var b: @self = .{ .p = null, .alloc = alloc };
b.p = alloc.alloc(@sizeof(T)) as *T;
ret b;
}
fn ptr(@Self self: *@self): *T {
ret self.p;
}
};
}
fn main() {
var b = @Box(i32, VmAlloc).make(VmAlloc{});
@assert(@sizeof(@Box(i32, VmAlloc)) == @sizeof(*i32), "static hook zst");
@assert(b.ptr() == b.p, "static hook bound");
}
@@ -0,0 +1,19 @@
// Expect compile_error E0317
// Expect location = 18:31
type Allocator = shape {
fn alloc(@Self self: *@self, size: usize): *any;
};
type Bad = struct {};
comptime fn Box(T: type, A: Allocator): type {
ret struct {
p: *T,
alloc: A,
};
}
fn main() {
@assert(@sizeof(@Box(i32, Bad)) > 0, "size");
}
@@ -0,0 +1,23 @@
// Expect compile_error E0305
// Expect location = 22:31
type Allocator = shape {
fn alloc(@Self self: *@self, size: usize): *any;
};
type Bad = struct {
fn alloc(@Self self: *@self, wrong: i32): *any {
ret null;
}
};
comptime fn Box(T: type, A: Allocator): type {
ret struct {
p: *T,
alloc: A,
};
}
fn main() {
@assert(@sizeof(@Box(i32, Bad)) > 0, "size");
}
@@ -0,0 +1,57 @@
// Expect retcode = 0
type Allocator = shape {
fn alloc(@Self self: *@self, size: usize): *any;
};
type VmA = struct: Allocator {
pad: u8,
fn alloc(@Self self: *@self, size: usize): *any {
ret null;
}
};
type VmB = struct: Allocator {
pad: u32,
fn alloc(@Self self: *@self, size: usize): *any {
ret null;
}
};
comptime fn Inner(A: Allocator): type {
ret struct {
a: A,
fn make(a: A): @self {
ret .{ .a = a };
}
fn tag(@Self self: *@self): usize {
ret @sizeof(A);
}
};
}
comptime fn Outer(A: Allocator): type {
ret struct {
inner: @Inner(A),
fn make(a: A): @self {
ret .{ .inner = @Inner(A).make(a) };
}
fn tag(@Self self: *@self): usize {
ret self.inner.tag();
}
};
}
fn main() {
var a = @Outer(VmA).make(VmA{ .pad = 1 });
var b = @Outer(VmB).make(VmB{ .pad = 2 });
@assert(a.tag() == @sizeof(VmA), "instance a");
@assert(b.tag() == @sizeof(VmB), "instance b");
@assert(@sizeof(VmA) != @sizeof(VmB), "distinct instances");
}
@@ -0,0 +1,14 @@
// Expect compile_error E0308
// Expect location = 12:8
type Allocator = shape {
fn alloc(@Self self: *@self, size: usize): *any;
};
type Logger = shape {
fn log(@Self self: *@self, v: i32);
};
fn bad(x: Allocator + Logger) {}
fn main() {}
+78 -89
View File
@@ -9,64 +9,55 @@
type meta = @import("meta");
type .{ .Ref } = meta;
#hook(param)
macro fn Self(callee: Ref, value: Ref): Ref {
if value == 0 {
ret 0;
}
var vn = @meta.inspect(value);
match vn.kind {
.Addr(_) => { ret value; },
_ => {},
}
ret @meta.construct(.{ .kind = .Addr(value) });
}
type .{ .Self } = @import("./support/self_hook.spl");
type Layout = struct {
size: usize,
align: usize,
};
type Allocator = shape {
fn alloc(@Self self: *@self, layout: Layout): *u8;
fn dealloc(@Self self: *@self, ptr: *u8, layout: Layout): void;
fn realloc(@Self self: *@self, ptr: *u8, old: Layout, new: Layout): *u8;
type AllocError = enum {
OutOfMemory,
InvalidLayout,
};
fn vm_alloc_raw(layout: Layout): *u8 {
comptime fn Result(T: type, E: type): type {
ret enum {
#ok
Ok: T,
#error
Err: E,
};
}
type Allocator = shape {
fn alloc(@Self self: *@self, layout: Layout): @Result(*any, AllocError);
fn dealloc(@Self self: *@self, ptr: *any, layout: Layout);
};
fn vm_alloc_raw(layout: Layout): @Result(*any, AllocError) {
var p = vm_alloc(layout.size);
@assert(p != null, "alloc failed");
ret p as *u8;
if p == null {
ret .(.Err(AllocError.OutOfMemory));
}
ret .(.Ok(p));
}
fn vm_free_raw(ptr: *u8): void {
vm_free(ptr as *any);
}
fn vm_realloc_raw(ptr: *u8, old: Layout, new: Layout): *u8 {
if ptr == null {
ret vm_alloc_raw(new);
fn alloc_value(r: @Result(*any, AllocError)): *any {
match r {
.Ok(p) => { ret p; },
.Err(e) => { panic("allocation failed"); ret null; },
}
if new.size == 0 {
vm_free_raw(ptr);
ret null;
}
var p = vm_realloc(ptr as *any, new.size);
@assert(p != null, "realloc failed");
ret p as *u8;
}
type VmAlloc = struct: Allocator {
fn alloc(@Self self: *@self, layout: Layout): *u8 {
fn alloc(@Self self: *@self, layout: Layout): @Result(*any, AllocError) {
ret vm_alloc_raw(layout);
}
fn dealloc(@Self self: *@self, ptr: *u8, layout: Layout): void {
vm_free_raw(ptr);
}
fn realloc(@Self self: *@self, ptr: *u8, old: Layout, new: Layout): *u8 {
ret vm_realloc_raw(ptr, old, new);
fn dealloc(@Self self: *@self, ptr: *any, layout: Layout): void {
vm_free(ptr);
}
};
@@ -74,16 +65,12 @@ type VmAlloc = struct: Allocator {
type TaggedAlloc = struct: Allocator {
tag: u8,
fn alloc(@Self self: *@self, layout: Layout): *u8 {
fn alloc(@Self self: *@self, layout: Layout): @Result(*any, AllocError) {
ret vm_alloc_raw(layout);
}
fn dealloc(@Self self: *@self, ptr: *u8, layout: Layout): void {
vm_free_raw(ptr);
}
fn realloc(@Self self: *@self, ptr: *u8, old: Layout, new: Layout): *u8 {
ret vm_realloc_raw(ptr, old, new);
fn dealloc(@Self self: *@self, ptr: *any, layout: Layout): void {
vm_free(ptr);
}
};
@@ -121,49 +108,49 @@ comptime fn RawVec(T: type): type {
ret .{ ._ptr = null, ._len = 0, ._cap = 0 };
}
fn len(self: @self): usize {
fn len(@Self self: @self): usize {
ret self._len;
}
fn capacity(self: @self): usize {
fn capacity(@Self self: @self): usize {
ret self._cap;
}
fn as_ptr(self: @self): *T {
fn as_ptr(@Self self: @self): *T {
ret self._ptr;
}
fn as_slice(self: @self): []T {
fn as_slice(@Self self: @self): []T {
ret @Slice(T).from_raw_parts(self._ptr, self._len);
}
fn get(self: @self, i: usize): T {
fn get(@Self self: @self, i: usize): T {
@assert(i < self._len, "index out of range");
ret self._ptr[i];
}
fn at(self: @self, i: usize): *T {
fn at(@Self self: @self, i: usize): *T {
@assert(i < self._len, "index out of range");
ret self._ptr + i;
}
fn set(self: *@self, i: usize, v: T): void {
fn set(@Self self: *@self, i: usize, v: T): void {
@assert(i < self._len, "index out of range");
self._ptr[i] = v;
}
fn push_unchecked(self: *@self, v: T): void {
fn push_unchecked(@Self self: *@self, v: T): void {
self._ptr[self._len] = v;
self._len = self._len + 1;
}
fn pop_unchecked(self: *@self): T {
fn pop_unchecked(@Self self: *@self): T {
@assert(self._len > 0, "pop from empty");
self._len = self._len - 1;
ret self._ptr[self._len];
}
fn insert_unchecked(self: *@self, i: usize, v: T): void {
fn insert_unchecked(@Self self: *@self, i: usize, v: T): void {
var j = self._len;
while j > i {
self._ptr[j] = self._ptr[j - 1];
@@ -173,7 +160,7 @@ comptime fn RawVec(T: type): type {
self._len = self._len + 1;
}
fn remove_unchecked(self: *@self, i: usize): T {
fn remove_unchecked(@Self self: *@self, i: usize): T {
var v = self._ptr[i];
var j = i;
while j + 1 < self._len {
@@ -184,17 +171,17 @@ comptime fn RawVec(T: type): type {
ret v;
}
fn clear(self: *@self): void {
fn clear(@Self self: *@self): void {
self._len = 0;
}
fn truncate(self: *@self, n: usize): void {
fn truncate(@Self self: *@self, n: usize): void {
if n < self._len {
self._len = n;
}
}
fn reset(self: *@self): void {
fn reset(@Self self: *@self): void {
self._ptr = null;
self._len = 0;
self._cap = 0;
@@ -212,13 +199,15 @@ comptime fn Grow(T: type, A: type): type {
var new_cap = grow_capacity(base._cap, 8, need);
var old_layout = layout_of(@sizeof(T), @alignof(T), base._cap);
var new_layout = layout_of(@sizeof(T), @alignof(T), new_cap);
var p: *u8 = null;
if base._cap == 0 {
p = alloc.alloc(new_layout);
} else {
p = alloc.realloc(base._ptr as *u8, old_layout, new_layout);
var p = alloc_value(alloc.alloc(new_layout));
if base._cap != 0 {
var i: usize = 0;
while i < base._len * @sizeof(T) {
(p as *u8)[i] = (base._ptr as *u8)[i];
i = i + 1;
}
alloc.dealloc(base._ptr as *any, old_layout);
}
@assert(p != null, "grow failed");
base._ptr = p as *T;
base._cap = new_cap;
}
@@ -241,33 +230,33 @@ comptime fn Vec(T: type): type {
}
var g: VmAlloc = .{};
var lay = layout_of(@sizeof(T), @alignof(T), n);
var p = g.alloc(lay);
var p = alloc_value(g.alloc(lay));
v.base._ptr = p as *T;
v.base._cap = n;
ret v;
}
fn len(self: @self): usize {
fn len(@Self self: @self): usize {
ret self.base.len();
}
fn capacity(self: @self): usize {
fn capacity(@Self self: @self): usize {
ret self.base.capacity();
}
fn as_ptr(self: @self): *T {
fn as_ptr(@Self self: @self): *T {
ret self.base.as_ptr();
}
fn as_slice(self: @self): []T {
fn as_slice(@Self self: @self): []T {
ret self.base.as_slice();
}
fn get(self: @self, i: usize): T {
fn get(@Self self: @self, i: usize): T {
ret self.base.get(i);
}
fn at(self: @self, i: usize): *T {
fn at(@Self self: @self, i: usize): *T {
ret self.base.at(i);
}
@@ -353,7 +342,7 @@ comptime fn Vec(T: type): type {
fn drop(@Self self: *@self): void {
if self.base._cap != 0 {
var g: VmAlloc = .{};
g.dealloc(self.base._ptr as *u8, layout_of(@sizeof(T), @alignof(T), self.base._cap));
g.dealloc(self.base._ptr as *any, layout_of(@sizeof(T), @alignof(T), self.base._cap));
}
@RawVec(T).reset(&self.base);
}
@@ -382,29 +371,29 @@ comptime fn VecOwned(T: type, A: type): type {
ret v;
}
var lay = layout_of(@sizeof(T), @alignof(T), n);
var p = alloc.alloc(lay);
var p = alloc_value(alloc.alloc(lay));
v.base._ptr = p as *T;
v.base._cap = n;
ret v;
}
fn len(self: @self): usize {
fn len(@Self self: @self): usize {
ret self.base.len();
}
fn capacity(self: @self): usize {
fn capacity(@Self self: @self): usize {
ret self.base.capacity();
}
fn as_slice(self: @self): []T {
fn as_slice(@Self self: @self): []T {
ret self.base.as_slice();
}
fn get(self: @self, i: usize): T {
fn get(@Self self: @self, i: usize): T {
ret self.base.get(i);
}
fn at(self: @self, i: usize): *T {
fn at(@Self self: @self, i: usize): *T {
ret self.base.at(i);
}
@@ -485,7 +474,7 @@ comptime fn VecOwned(T: type, A: type): type {
fn drop(@Self self: *@self): void {
if self.base._cap != 0 {
self.alloc.dealloc(self.base._ptr as *u8, layout_of(@sizeof(T), @alignof(T), self.base._cap));
self.alloc.dealloc(self.base._ptr as *any, layout_of(@sizeof(T), @alignof(T), self.base._cap));
}
@RawVec(T).reset(&self.base);
}
@@ -513,29 +502,29 @@ comptime fn VecParam(T: type, A: type): type {
ret v;
}
var lay = layout_of(@sizeof(T), @alignof(T), n);
var p = alloc.alloc(lay);
var p = alloc_value(alloc.alloc(lay));
v.base._ptr = p as *T;
v.base._cap = n;
ret v;
}
fn len(self: @self): usize {
fn len(@Self self: @self): usize {
ret self.base.len();
}
fn capacity(self: @self): usize {
fn capacity(@Self self: @self): usize {
ret self.base.capacity();
}
fn as_slice(self: @self): []T {
fn as_slice(@Self self: @self): []T {
ret self.base.as_slice();
}
fn get(self: @self, i: usize): T {
fn get(@Self self: @self, i: usize): T {
ret self.base.get(i);
}
fn at(self: @self, i: usize): *T {
fn at(@Self self: @self, i: usize): *T {
ret self.base.at(i);
}
@@ -616,7 +605,7 @@ comptime fn VecParam(T: type, A: type): type {
fn drop(@Self self: *@self, alloc: A): void {
if self.base._cap != 0 {
alloc.dealloc(self.base._ptr as *u8, layout_of(@sizeof(T), @alignof(T), self.base._cap));
alloc.dealloc(self.base._ptr as *any, layout_of(@sizeof(T), @alignof(T), self.base._cap));
}
@RawVec(T).reset(&self.base);
}
+3 -2
View File
@@ -4,8 +4,9 @@ type meta = @import("meta");
type .{ .Ref } = meta;
#hook(param)
macro fn Self(callee: Ref, value: Ref): Ref {
ret @meta.construct(.{ .kind = .Addr(value) });
macro fn Self(callee: Ref, value: Ref, implicit: Ref): Ref {
if implicit == 0 { ret value; }
ret @meta.construct(.{ .kind = .Addr(implicit) });
}
type Counter = struct {
+1 -1
View File
@@ -4,7 +4,7 @@ type meta = @import("meta");
type .{ .Ref } = meta;
#hook(param)
macro fn default(callee: Ref, value: Ref, fallback: i32): Ref {
macro fn default(callee: Ref, value: Ref, implicit: Ref, fallback: i32): Ref {
if value != 0 { ret value; }
if fallback != 42 { @meta.error(.DefaultMismatch); ret 0; }
ret @meta.construct(.{ .kind = .LitInt("42") });