Files
spl/stage3/hook.spl
T

346 lines
12 KiB
Plaintext

// hook 挂载层:decorator / param / impl 的解析、签名校验和调用。
// sema 只提供声明分析、名字解析和 comptime 求值;这里不实现语言类型规则。
type .{ .Vec } = @import("../stage2/utils/vec.spl");
type Bld = @import("./spl_ir_builder.spl");
type .{ .Node, .Ref } = @import("./builtins/spl_ir.spl");
type .{ .ErrorCode } = @import("./builtins/error_code.spl");
type SemaM = @import("./sema.spl");
type Sema = SemaM.Sema;
type HookHooks = SemaM.HookHooks;
type HookParamResult = SemaM.HookParamResult;
type LangType = @import("./lang_type.spl");
type HookKind = enum { Decorator, Param };
type DeclCat = enum { Invalid, Fn, Type, Value };
fn decl_cat(k: Node.Kind): DeclCat {
match k {
.FnDecl(_) => { ret .Fn; },
.TypeDecl(_) => { ret .Type; },
.VarDecl(_), .ConstDecl(_) => { ret .Value; },
_ => {},
}
ret .Invalid;
}
fn ref_value(self: *Sema, r: Ref): Ref {
var v = Bld.construct(&self.hir, .{ .kind = .LitInt(SemaM.itext(self, r)) });
SemaM.set_tid(self, v, LangType.usize_lang(&self.types));
ret v;
}
fn param_type(self: *Sema, p: Ref): usize {
var pn = Bld.node_ptr(&self.hir, p);
if pn == null { ret 0; }
match pn.kind {
.ParamDecl(x) => { ret SemaM.resolve_type(self, x.ty); },
_ => {},
}
ret 0;
}
fn is_ref_type(self: *Sema, ty: usize): bool {
ret ty != 0 && ty == LangType.usize_lang(&self.types);
}
fn is_type_type(self: *Sema, ty: usize): bool {
ret ty != 0 && LangType.is_type(&self.types, ty);
}
fn hook_error(self: *Sema, kind: HookKind, node: Ref, name: []u8, msg: []u8) {
match kind {
.Decorator => { SemaM.sema_error_code_at(self, node, ErrorCode.Decorator, msg, name); },
.Param => { SemaM.sema_error_code_at(self, node, ErrorCode.ParamHook, msg, name); },
}
}
fn check_hook(self: *Sema, hd: usize, kind: HookKind, at: Ref): bool {
SemaM.ensure_decl(self, hd);
var d = SemaM.decl_at(self, hd);
var name = d.name;
var hnode = d.node;
var hmod = d.mod;
var n = Bld.node_ptr(&self.hir, hnode);
if n == null { ret false; }
var saved = self.cur_mod;
if hmod != saved { SemaM.switch_mod(self, hmod); }
match n.kind {
.FnDecl(f) => {
if !SemaM.has_tag(self, hnode, "macro") {
hook_error(self, kind, at, name, "hook must be macro fn");
if hmod != saved { SemaM.switch_mod(self, saved); }
ret false;
}
var ok = true;
if f.ret_ty != 0 && !is_ref_type(self, SemaM.resolve_type(self, f.ret_ty)) {
ok = false;
}
match kind {
.Decorator => {
if f.params.len < 1 || !is_ref_type(self, param_type(self, f.params[0])) {
ok = false;
}
},
.Param => {
if f.params.len < 2 ||
!is_ref_type(self, param_type(self, f.params[0])) ||
!is_ref_type(self, param_type(self, f.params[1])) {
ok = false;
}
},
}
if !ok { hook_error(self, kind, at, name, "hook signature mismatch"); }
if hmod != saved { SemaM.switch_mod(self, saved); }
ret ok;
},
_ => {},
}
if hmod != saved { SemaM.switch_mod(self, saved); }
hook_error(self, kind, at, name, "hook is not a function");
ret false;
}
// 编译器生成的 hook 调用:保留 LitInt(返回 Ref),不按用户宏展开。
fn run_hook(self: *Sema, hook_decl: usize, args: []Ref): Ref {
SemaM.ensure_decl(self, hook_decl);
var d = SemaM.decl_at(self, hook_decl);
var name = d.name;
var hmod = d.mod;
var sym = d.sym;
var lang = d.lang;
var saved_mod = self.cur_mod;
if hmod != saved_mod { SemaM.switch_mod(self, hmod); }
var callee = Bld.construct(&self.hir, .{ .kind = .Global(name) });
SemaM.set_node_fn(self, callee, sym);
SemaM.set_tid(self, callee, lang);
var call = Bld.construct(&self.hir, .{ .kind = .Call(.{
.is_comptime = true,
.callee = callee,
.args = args,
}) });
var none: [1]Ref = .[0];
var tag = Bld.construct(&self.hir, .{ .kind = .Tag(.{
.is_at = false,
.name = "hook_call",
.args = none[0..0],
}) });
var tags: [1]Ref = .[tag];
var cn = Bld.node_ptr(&self.hir, call);
if cn != null { cn.tags = Bld.dup_refs(&self.hir, tags[0..1]); }
SemaM.sema_expr(self, call, 0);
var out: Ref = 0;
var rn = Bld.node_ptr(&self.hir, call);
if rn != null {
match rn.kind {
.LitInt(s) => { out = SemaM.parse_u(s); },
_ => {},
}
}
if hmod != saved_mod { SemaM.switch_mod(self, saved_mod); }
ret out;
}
fn run_hook_tagged(self: *Sema, hd: usize, fixed: []Ref, extra: []Ref): Ref {
if extra.len == 0 { ret run_hook(self, hd, fixed); }
var buf = Vec.blank(@sizeof(Ref), @alignof(Ref));
defer { Vec.drop(&buf); }
var i: usize = 0;
while i < fixed.len {
var a = fixed[i];
match Vec.push(&buf, &a as *u8) { .Ok => {}, .Err(error) => { ret 0; } }
i = i + 1;
}
i = 0;
while i < extra.len {
var a = extra[i];
match Vec.push(&buf, &a as *u8) { .Ok => {}, .Err(error) => { ret 0; } }
i = i + 1;
}
ret run_hook(self, hd, (buf.data as *Ref)[0..buf.len]);
}
fn apply_decorators(user: *any, di: usize) {
var self = user as *Sema;
var node = SemaM.decl_at(self, di).node;
var n = Bld.node_ptr(&self.hir, node);
if n == null { ret; }
var input_cat = decl_cat(n.kind);
var tags = n.tags;
var i: usize = tags.len;
while i > 0 {
i = i - 1;
var tn = Bld.node_ptr(&self.hir, tags[i]);
if tn == null { continue; }
match tn.kind {
.Tag(x) => {
if !x.is_at { continue; }
var hd = SemaM.find_decl(self, x.name);
if hd == 0 {
SemaM.sema_error_code_at(self, node, ErrorCode.Decorator, "decorator not found", x.name);
continue;
}
if !check_hook(self, hd - 1, .Decorator, node) { continue; }
var args: [1]Ref = .[ref_value(self, node)];
var code = run_hook_tagged(self, hd - 1, args[0..1], x.args);
if code == 0 {
SemaM.sema_error_code_at(self, node, ErrorCode.Decorator,
"decorator returned no declaration", x.name);
continue;
}
var rn = Bld.node_ptr(&self.hir, code);
if rn == null || @enum2int(decl_cat(rn.kind)) != @enum2int(input_cat) {
SemaM.sema_error_code_at(self, node, ErrorCode.Decorator,
"decorator returned wrong declaration category", x.name);
continue;
}
node = code;
SemaM.decl_at(self, di).node = node;
},
_ => {},
}
}
var nn = Bld.node_ptr(&self.hir, node);
if nn == null { ret; }
match nn.kind {
.FnDecl(f) => {
SemaM.decl_at(self, di).kind = .Fn;
SemaM.decl_at(self, di).name = f.name;
},
.TypeDecl(t) => {
SemaM.decl_at(self, di).kind = .Type;
SemaM.decl_at(self, di).name = t.name;
},
.VarDecl(v) => {
SemaM.decl_at(self, di).kind = .Var;
SemaM.decl_at(self, di).name = v.name;
},
.ConstDecl(c) => {
SemaM.decl_at(self, di).kind = .Const;
SemaM.decl_at(self, di).name = c.name;
},
_ => {},
}
}
fn prepare_params(user: *any, callee_ref: Ref, args: []Ref, out: *HookParamResult) {
var self = user as *Sema;
out.args = args;
out.changed = false;
var fnp = Bld.node_ptr(&self.hir, callee_ref);
if fnp == null { ret; }
match fnp.kind {
.FnDecl(f) => {
var any_hook = false;
var si: usize = 0;
while si < f.params.len {
var sn = Bld.node_ptr(&self.hir, f.params[si]);
if sn != null {
var ti2: usize = 0;
while ti2 < sn.tags.len {
var tn2 = Bld.node_ptr(&self.hir, sn.tags[ti2]);
if tn2 != null {
match tn2.kind { .Tag(x) => { if x.is_at { any_hook = true; } }, _ => {} }
}
ti2 = ti2 + 1;
}
}
si = si + 1;
}
if !any_hook { ret; }
var buf = Vec.blank(@sizeof(Ref), @alignof(Ref));
defer { Vec.drop(&buf); }
var changed = false;
var i: usize = 0;
while i < f.params.len {
var pn = Bld.node_ptr(&self.hir, f.params[i]);
if pn == null { break; }
var has_any = false;
var ti: usize = 0;
while ti < pn.tags.len {
var tn = Bld.node_ptr(&self.hir, pn.tags[ti]);
if tn != null {
match tn.kind {
.Tag(x) => { if x.is_at { has_any = true; } },
_ => {},
}
}
ti = ti + 1;
}
if i >= args.len && !has_any { break; }
var value: Ref = 0;
if i < args.len { value = args[i]; }
var hi = pn.tags.len;
while hi > 0 {
hi = hi - 1;
var tn = Bld.node_ptr(&self.hir, pn.tags[hi]);
if tn == null { continue; }
match tn.kind {
.Tag(x) => {
if !x.is_at { continue; }
var hd = SemaM.find_decl(self, x.name);
if hd == 0 {
SemaM.sema_error_code_at(self, f.params[i], ErrorCode.ParamHook,
"param hook not found", x.name);
continue;
}
if !check_hook(self, hd - 1, .Param, f.params[i]) { continue; }
var hargs: [2]Ref = .[ref_value(self, callee_ref), ref_value(self, value)];
var code = run_hook_tagged(self, hd - 1, hargs[0..2], x.args);
if code == 0 {
SemaM.sema_error_code_at(self, f.params[i], ErrorCode.ParamHook,
"param hook returned no value", x.name);
continue;
}
value = code;
changed = true;
},
_ => {},
}
}
if i >= args.len && value == 0 {
SemaM.sema_error_code_at(self, f.params[i], ErrorCode.ParamHook,
"missing param has no hook value", "");
break;
}
if value == 0 { break; }
match Vec.push(&buf, &value as *u8) {
.Ok => {},
.Err(error) => { ret; },
}
i = i + 1;
}
if changed {
out.args = Bld.dup_refs(&self.hir, (buf.data as *Ref)[0..buf.len]);
out.changed = true;
}
},
_ => {},
}
}
fn is_impl(user: *any, node: Ref): bool {
var self = user as *Sema;
var n = Bld.node_ptr(&self.hir, node);
if n == null { ret false; }
match n.kind {
.FnDecl(f) => {
if !SemaM.has_tag(self, node, "macro") || f.params.len < 2 { ret false; }
ret is_type_type(self, param_type(self, f.params[0])) &&
is_type_type(self, param_type(self, f.params[1]));
},
_ => {},
}
ret false;
}
fn hooks(): HookHooks {
ret HookHooks {
.apply_decorators = apply_decorators,
.prepare_params = prepare_params,
.is_impl = is_impl,
};
}
fn main() {}