spl 编写编译器基础框架 ai修复出现的stage2和stage3问题并提供新语法
This commit is contained in:
1 parent
126b979f72
commit
50ee126d44
87 files changed
+3168
-394
No files matched your search
@@ -1,6 +1,8 @@
|
||||
#!builtin
|
||||
#!role(builtins)
|
||||
|
||||
type meta = @import("meta");
|
||||
|
||||
// splc3 的预置头:把编译器底层能力以 `#builtin` 声明暴露给用户/comptime 代码。
|
||||
// `#builtin(<id>)` 的 id 必须命中 stage3/intrinsic.spl 的 intrinsic_of_id。
|
||||
|
||||
@@ -27,26 +29,25 @@ type Node = @import("./spl_ir.spl").Node;
|
||||
#builtin(construct) #extern(builtin) comptime fn construct(n: Node): Ref;
|
||||
|
||||
// receiver hook:@Self 只挂在方法的第一个参数上。
|
||||
// 参数类型是 *T 时取地址,是 T 时按值传递;已显式传 &x 的调用不再二次取地址。
|
||||
// 参数类型是 *T 时取地址,是 T 时按值传递;implicit 本身已是指向接收者的指针时直接复用。
|
||||
#hook(param)
|
||||
macro fn Self(callee: Ref, value: Ref): Ref {
|
||||
if value == 0 { ret 0; }
|
||||
|
||||
var vn = @inspect(value);
|
||||
match vn.kind {
|
||||
.Addr(_) => { ret value; },
|
||||
_ => {},
|
||||
macro fn Self(callee: Ref, value: Ref, implicit: Ref): Ref {
|
||||
if implicit == 0 {
|
||||
if value != 0 { ret value; }
|
||||
ret 0;
|
||||
}
|
||||
|
||||
var cn = @inspect(callee);
|
||||
var cn = @meta.inspect(callee);
|
||||
match cn.kind {
|
||||
.FnDecl(f) => {
|
||||
var pn = @inspect(f.params[0]);
|
||||
var pn = @meta.inspect(f.params[0]);
|
||||
match pn.kind {
|
||||
.ParamDecl(p) => {
|
||||
var tn = @inspect(p.ty);
|
||||
var tn = @meta.inspect(p.ty);
|
||||
match tn.kind {
|
||||
.PtrType(_) => { ret @construct(.{ .kind = .Addr(value) }); },
|
||||
.PtrType(_) => {
|
||||
if @ptr_ref(implicit) { ret implicit; }
|
||||
ret @meta.construct(.{ .kind = .Addr(implicit) });
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
},
|
||||
@@ -55,5 +56,39 @@ macro fn Self(callee: Ref, value: Ref): Ref {
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
ret value;
|
||||
ret implicit;
|
||||
}
|
||||
|
||||
// implicit 已是 `*T` 形式(局部变量或参数)时不再二次取地址。
|
||||
comptime fn ptr_ref(r: Ref): bool {
|
||||
var n = @meta.inspect(r);
|
||||
var decl: Ref = 0;
|
||||
match n.kind {
|
||||
.Addr(_) => { ret true; },
|
||||
.Local(p) => { decl = p; },
|
||||
_ => {},
|
||||
}
|
||||
if decl == 0 { ret false; }
|
||||
var dn = @meta.inspect(decl);
|
||||
var ty: Ref = 0;
|
||||
match dn.kind {
|
||||
.ParamDecl(p) => { ty = p.ty; },
|
||||
.VarDecl(v) => { ty = v.ty; },
|
||||
_ => {},
|
||||
}
|
||||
if ty == 0 { ret false; }
|
||||
var tn = @meta.inspect(ty);
|
||||
match tn.kind {
|
||||
.PtrType(_) => { ret true; },
|
||||
.ShapeUse(x) => {
|
||||
var sn = @meta.inspect(x.shapes[0]);
|
||||
match sn.kind { .PtrType(_) => { ret true; }, _ => {} }
|
||||
},
|
||||
.ShapeNoDefault(xs) => {
|
||||
var sn = @meta.inspect(xs[0]);
|
||||
match sn.kind { .PtrType(_) => { ret true; }, _ => {} }
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
ret false;
|
||||
}
|
||||
Reference in new issue
Block a user