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

+249 -20
View File
@@ -11,6 +11,7 @@ type Bld = @import("./spl_ir_builder.spl");
type .{ .Builtin } = @import("./intrinsic.spl");
type B = @import("./intrinsic.spl");
type SemaOpM = @import("./sema_op.spl");
type ReflectType = @import("./reflect_type.spl");
type .{ .SemaOp } = @import("./sema_op.spl");
type .{ .LangKind, .ReprKind } = @import("./lang_type.spl");
type .{ .StructMember } = @import("./lang_type.spl");
@@ -26,6 +27,94 @@ type .{
} = @import("./sema.spl");
type tid = usize;
fn expr_is_type_value(self: *Sema, ref: Ref): bool {
var n = Bld.node_ptr(&self.hir, ref);
if n == null { ret false; }
match n.kind {
.Field(_), .Local(_), .LitInt(_), .LitFloat(_), .LitBool(_), .LitChar(_),
.LitStr(_), .Index(_), .Addr(_), .Deref(_), .AggLit(_), .ArrayLit(_),
.TupleLit(_), .Binary(_), .Unary(_), .Cast(_), .SliceExpr(_), .Range(_),
.Try(_), .Catch(_), .IsNonNull(_), .OptionalPayload(_), .IsTag(_),
.TagPayload(_), .Match(_), .Block(_), .If(_), .IfVar(_), .Loop(_),
.Iter(_), .Return(_), .Break(_), .Continue(_), .Defer(_), .ErrDefer(_) => { ret false; },
.NamedType(_), .AggType(_), .ShapeApply(_), .ShapeUse(_), .ShapeNoDefault(_),
.Type, .SelfType, .ArrayType(_), .SliceType(_), .PtrType(_), .FnType(_),
.TupleType(_), .IntType(_), .FloatType(_), .Bool => { ret true; },
.Call(x) => {
var cs = SemaM.node_fn(self, x.callee);
if cs == 0 {
var cnn = Bld.node_ptr(&self.hir, x.callee);
if cnn != null {
match cnn.kind {
.Global(g) => {
var d = SemaM.find_decl(self, g);
if d != 0 {
SemaM.ensure_decl(self, d - 1);
cs = SemaM.decl_at(self, d - 1).sym;
}
},
_ => {},
}
}
}
if cs != 0 {
var fr = SemaM.sym_at(self, cs - 1).ref;
if fr != 0 && SemaM.type_is_generator(self, fr) { ret true; }
}
ret false;
},
.Global(_) => {},
_ => { ret false; },
}
var t = SemaM.tid_of(self, ref);
if t == 0 { t = SemaM.resolve_type(self, ref); }
ret t != 0 && LangType.is_type(&self.types, t);
}
fn append_tag(self: *Sema, ref: Ref, name: []u8, args: []Ref) {
var n = Bld.node_ptr(&self.hir, ref);
if n == null { ret; }
var tag = Bld.construct(&self.hir, .{ .kind = .Tag(.{
.is_at = false,
.name = name,
.args = Bld.dup_refs(&self.hir, args),
}) });
var buf = Vec.blank(@sizeof(Ref), @alignof(Ref));
defer { Vec.drop(&buf); }
var i: usize = 0;
while i < n.tags.len {
var t = n.tags[i];
match Vec.push(&buf, &t as *u8) { .Ok => {}, .Err(error) => { ret; } }
i = i + 1;
}
match Vec.push(&buf, &tag as *u8) { .Ok => {}, .Err(error) => { ret; } }
n.tags = Bld.dup_refs(&self.hir, (buf.data as *Ref)[0..buf.len]);
}
fn find_tag(self: *Sema, ref: Ref, name: []u8): Ref {
var n = Bld.node_ptr(&self.hir, ref);
if n == null { ret 0; }
var i: usize = 0;
while i < n.tags.len {
var tn = Bld.node_ptr(&self.hir, n.tags[i]);
if tn != null {
match tn.kind {
.Tag(x) => { if SemaM.eq(x.name, name) { ret n.tags[i]; } },
_ => {},
}
}
i = i + 1;
}
ret 0;
}
// 记录 hook 注入前的 implicit 与显式实参;实例化重解析时据此重新执行 hook。
fn set_param_src(self: *Sema, ref: Ref, implicit: Ref, args: []Ref) {
var tuple = Bld.construct(&self.hir, .{ .kind = .TupleLit(Bld.dup_refs(&self.hir, args)) });
var src: [2]Ref = .[implicit, tuple];
append_tag(self, ref, "param_src", src[0..2]);
}
fn hook_sema_expr(user: *any, ref: Ref, want: usize) { sema_expr(user as *Sema, ref, want); }
fn hook_sema_body(user: *any, body: usize, ret_tid: usize) { sema_body(user as *Sema, body, ret_tid); }
fn hook_iter_capture_members(user: *any, ref: Ref): Vec { ret iter_capture_members(user as *Sema, ref); }
@@ -34,6 +123,7 @@ fn hooks(): ExprHooks {
ret ExprHooks {
.sema_expr = hook_sema_expr,
.sema_body = hook_sema_body,
.patch_body = patch_body,
.iter_capture_members = hook_iter_capture_members,
};
}
@@ -49,8 +139,13 @@ fn bind_pattern(self: *Sema, ref: Ref, lang: usize) {
match n.kind {
.PatWild => { SemaM.set_tid(self, ref, 0); },
.PatBind(x) => {
var d = SemaM.new_def(self, lang);
SemaM.set_def(self, ref, d);
// 生成实例/匹配臂可能被重复分析;重复 new_def 会让 arm body
// 的 Local 指向旧 def,而 pattern bind 写新 def 的槽。
var d = SemaM.node_def(self, ref);
if d == 0 {
d = SemaM.new_def(self, lang);
SemaM.set_def(self, ref, d);
}
SemaM.set_tid(self, ref, lang);
},
.PatLit(x) => { sema_expr(self, x, lang); SemaM.set_tid(self, ref, lang); },
@@ -476,6 +571,33 @@ fn bind_iter_captures(self: *Sema, captures: []Ref, members: []Ref, pattern_type
}
}
fn callee_label(self: *Sema, callee: Ref): []u8 {
var n = Bld.node_ptr(&self.hir, callee);
if n == null { ret ""; }
match n.kind {
.Global(g) => { ret g; },
.Field(f) => {
var on = Bld.node_ptr(&self.hir, f.obj);
if on != null {
match on.kind {
.Global(g) => {
var b = ByteBuffer.new();
defer { ByteBuffer.drop(&b); }
ByteBuffer.append(&b, g);
ByteBuffer.append(&b, ".");
ByteBuffer.append(&b, f.name);
ret Bld.dup_bytes(&self.hir, ByteBuffer.as_slice(b));
},
_ => {},
}
}
ret f.name;
},
_ => {},
}
ret "";
}
fn sema_expr(self: *Sema, ref: Ref, want: usize) {
if ref == 0 { ret; }
var n = Bld.node_ptr(&self.hir, ref);
@@ -872,6 +994,14 @@ fn sema_expr(self: *Sema, ref: Ref, want: usize) {
var is_ct_builtin = false;
var native_callee = false;
var callee_ref: usize = 0;
var implicit0: Ref = 0;
var cn0 = Bld.node_ptr(&self.hir, x.callee);
if cn0 != null {
match cn0.kind {
.Field(f) => { if !expr_is_type_value(self, f.obj) { implicit0 = f.obj; } },
_ => {},
}
}
var csi = SemaM.node_fn(self, x.callee);
var cn = Bld.node_ptr(&self.hir, x.callee);
if cn != null {
@@ -984,29 +1114,64 @@ fn sema_expr(self: *Sema, ref: Ref, want: usize) {
ret_tid = s2.ret_tid;
callee_ref = s2.ref;
}
// 用户类型生成器在表达式位置也按类型求解;普通 comptime 值调用
// 的形参检查会把 shape 约束参数误当成运行期 shape 值。
if fn_tid != 0 && LangType.is_type(&self.types, ret_tid) &&
callee_ref != 0 && SemaM.type_is_generator(self, callee_ref) {
var tl = SemaM.type_gen_lang(self, ref, x, 0);
SemaM.set_tid(self, ref, tl);
ret;
}
var is_macro = callee_ref != 0 && SemaM.has_tag(self, callee_ref, "macro") &&
!SemaM.has_tag(self, ref, "adapter_call") &&
!SemaM.has_tag(self, ref, "hook_call");
if is_macro && !x.is_comptime {
SemaM.sema_error_code_at(self, ref, ErrorCode.MacroCode, "macro call requires @", "");
}
if callee_ref != 0 && !is_macro && !x.is_comptime &&
if callee_ref != 0 && !is_macro &&
!SemaM.has_tag(self, ref, "hook_call") {
var rr = HookParamResult { .args = x.args, .changed = false };
SemaM.insert_method_receiver(self, callee_ref, x.callee, x.args, &rr);
if rr.changed {
x.args = rr.args;
var call_node = Bld.node_ptr(&self.hir, ref);
if call_node != null { call_node.kind = .Call(x); }
}
if self.hook_hooks.prepare_params != null {
var hr = HookParamResult { .args = x.args, .changed = false };
self.hook_hooks.prepare_params(self as *any, callee_ref, x.args, &hr);
// 实例化会改写形参类型,hook 注入结果必须按当前类型重算;
// param_src 保存注入前的 implicit 和显式实参,保证重算幂等。
var src = find_tag(self, ref, "param_src");
var use_implicit = implicit0;
var use_args = x.args;
var rerun = false;
if src != 0 {
var sn = Bld.node_ptr(&self.hir, src);
if sn != null {
match sn.kind {
.Tag(t) => {
if t.args.len == 2 {
use_implicit = t.args[0];
var an = Bld.node_ptr(&self.hir, t.args[1]);
if an != null {
match an.kind {
.TupleLit(xs) => { use_args = xs; },
_ => {},
}
}
rerun = true;
}
},
_ => {},
}
}
}
var hr = HookParamResult { .args = use_args, .changed = false };
self.hook_hooks.prepare_params(self as *any, callee_ref, use_implicit, use_args, &hr);
if hr.changed {
x.args = hr.args;
} else if rerun {
x.args = use_args;
}
if rerun || hr.changed {
var call_node2 = Bld.node_ptr(&self.hir, ref);
if call_node2 != null { call_node2.kind = .Call(x); }
}
if !rerun && hr.changed {
set_param_src(self, ref, implicit0, use_args);
}
}
}
if is_macro && SemaM.has_tag(self, callee_ref, "variadic") {
@@ -1027,8 +1192,13 @@ fn sema_expr(self: *Sema, ref: Ref, want: usize) {
sema_expr(self, er, 0);
var kind = SemaM.macro_arg_kind(self, er);
var kind_node = Bld.construct(&self.hir, .{ .kind = .LitStr(kind) });
var pair_args: [2]Ref = .[er, kind_node];
var pair = Bld.construct(&self.hir, .{ .kind = .TupleLit(pair_args[0..2]) });
// 第三个元素是实参的反射类型节点:库宏据此做类型分派,
// 编译器不再为具体库维护类型词汇表。
var ty_ref: Ref = 0;
var tl = SemaM.tid_of(self, er);
if tl != 0 { ty_ref = ReflectType.lang_to_reflect_hir(self, tl); }
var pair_args: [3]Ref = .[er, kind_node, ty_ref];
var pair = Bld.construct(&self.hir, .{ .kind = .TupleLit(pair_args[0..3]) });
match Vec.push(&extras, &pair as *u8) { .Ok => {}, .Err(e) => {} }
ei = ei + 1;
}
@@ -1118,7 +1288,8 @@ fn sema_expr(self: *Sema, ref: Ref, want: usize) {
has_signature = true;
}
if !has_signature && @enum2int(bid) == @enum2int(Builtin.None) && !native_callee {
SemaM.sema_error_code_at(self, ref, ErrorCode.NotCallable, "callee is not a function", "");
SemaM.sema_error_code_at(self, ref, ErrorCode.NotCallable,
"callee is not a function", callee_label(self, x.callee));
} else if has_signature && x.args.len != param_count {
SemaM.sema_error_code_at(self, ref, ErrorCode.ArgCount, "argument count mismatch", "");
}
@@ -1183,6 +1354,8 @@ fn sema_expr(self: *Sema, ref: Ref, want: usize) {
self.cur_fn_ref = saved_ref;
self.cur_fn = sv;
if is_macro {
// 宏已通过 @meta.error 报过诊断:不要再叠加“返回值无效”错误。
if self.had_error { ret; }
var cn = Bld.node_ptr(&self.hir, ref);
if cn == null { ret; }
var code: Ref = 0;
@@ -1214,6 +1387,15 @@ fn sema_expr(self: *Sema, ref: Ref, want: usize) {
SemaM.set_tid(self, ref, LangType.make_unit(&self.types));
} else { SemaM.set_tid(self, ref, ret_tid); }
},
.ShapeCall(x) => {
if self.shape_hooks.apply_expr == null {
SemaM.sema_error_code(self, ErrorCode.ShapeParamNotShape,
"shape apply hook missing", x.adapter);
ret;
}
var t = self.shape_hooks.apply_expr(self as *any, ref, x.adapter, x.shapes, x.args);
if t != 0 { SemaM.set_tid(self, ref, t); }
},
.CompoundAssign(x) => {
sema_expr(self, x.lval, want);
SemaM.mark_assigned(self, x.lval);
@@ -1369,8 +1551,20 @@ fn sema_expr(self: *Sema, ref: Ref, want: usize) {
},
.Addr(x) => {
sema_expr(self, x, 0);
var t = want;
if t == 0 { t = LangType.make_ptr(&self.types, SemaM.tid_of(self, x)); }
var ot = SemaM.tid_of(self, x);
// want 只有在其 pointee 就是操作数类型、或操作数仍是 shape/关联类型时才采用;
// 否则 `&a` 会被 shape 的 `*W` 期望类型覆盖掉真实类型 *A。
var shape_like = false;
if self.shape_hooks.is_shape_lang != null {
shape_like = self.shape_hooks.is_shape_lang(self as *any, ot);
}
if LangType.is_assoc(&self.types, ot) { shape_like = true; }
var t: usize = 0;
if want != 0 && LangType.is_ptr(&self.types, want) {
var we = LangType.elem_of(&self.types, want);
if ot == 0 || we == ot || shape_like { t = want; }
}
if t == 0 { t = LangType.make_ptr(&self.types, ot); }
SemaM.set_tid(self, ref, t);
},
.Deref(x) => {
@@ -1623,9 +1817,12 @@ fn sema_stmt(self: *Sema, ref: Ref, ret_tid: usize) {
if n == null { ret; }
match n.kind {
.Return(x) => {
sema_expr(self, x, ret_tid);
// catch handler 的 tail 是 #ok payload,但里面的 ret 仍然返回外层函数。
var rt = self.ret_override;
if rt == 0 { rt = ret_tid; }
sema_expr(self, x, rt);
if x != 0 && !SemaM.current_fn_is_macro(self) {
SemaM.require_type(self, x, ret_tid, "return");
SemaM.require_type(self, x, rt, "return");
}
},
.Defer(x) => { sema_stmt(self, x, ret_tid); SemaM.set_tid(self, ref, 0); },
@@ -1664,7 +1861,7 @@ fn sema_stmt(self: *Sema, ref: Ref, ret_tid: usize) {
}
sema_expr(self, x.tail, ret_tid);
},
_ => { sema_expr(self, ref, ret_tid); },
_ => { sema_expr(self, ref, 0); },
}
}
@@ -1675,3 +1872,35 @@ fn sema_body(self: *Sema, body: usize, ret_tid: usize) {
if body == 0 { ret; }
sema_stmt(self, body, ret_tid);
}
fn in_patch_roots(self: *Sema, ref: Ref): bool {
var i: usize = 0;
while i < self.shape_state.patch_roots.len {
if (self.shape_state.patch_roots.data as *Ref)[i] == ref { ret true; }
i = i + 1;
}
ret false;
}
// 实例化 patch:只重解析依赖 shape 形参的语句,其余节点保留模板分析结果。
fn patch_body(user: *any, body: usize, ret_tid: usize) {
var self = user as *Sema;
if body == 0 { ret; }
var n = Bld.node_ptr(&self.hir, body);
if n == null { ret; }
match n.kind {
.Block(b) => {
var i: usize = 0;
while i < b.stmts.len {
if in_patch_roots(self, b.stmts[i]) { sema_stmt(self, b.stmts[i], ret_tid); }
i = i + 1;
}
if b.tail != 0 && in_patch_roots(self, b.tail) {
sema_expr(self, b.tail, ret_tid);
}
},
_ => {
if in_patch_roots(self, body) { sema_stmt(self, body, ret_tid); }
},
}
}