spl 编写编译器基础框架 ai修复出现的stage2和stage3问题并提供新语法
This commit is contained in:
1 parent
126b979f72
commit
50ee126d44
87 files changed
+3168
-394
No files matched your search
+111
-19
@@ -75,6 +75,11 @@ type FnSym = struct {
|
||||
op: usize, // SemaOp 值;0 = 不是语言操作
|
||||
}
|
||||
|
||||
type ReflectSrc = struct {
|
||||
copy: Ref, // @meta.reflect 生成的方法签名副本
|
||||
src: Ref, // 原始 FnDecl 节点,用于反查 FnSym
|
||||
}
|
||||
|
||||
type TypeSym = struct {
|
||||
name: []u8,
|
||||
lang: usize,
|
||||
@@ -142,6 +147,7 @@ type GenInstance = struct { fn_ref: Ref, args: Vec, lang: usize };
|
||||
type ShapeParam = struct {
|
||||
fn_ref: Ref,
|
||||
param_index: usize,
|
||||
has_adapter: bool,
|
||||
adapter_decl: usize,
|
||||
shape_lang: usize,
|
||||
shape_ref: Ref, // ShapeApply / NamedType 使用点
|
||||
@@ -157,6 +163,7 @@ type ShapeInstance = struct {
|
||||
// shape 使用点的解析结果:`@adapter[Shape]` 里 adapter、shape 和绑定。
|
||||
type ShapeUse = struct {
|
||||
is_shape: bool,
|
||||
has_adapter: bool,
|
||||
adapter_decl: usize,
|
||||
shape_lang: usize,
|
||||
shape_ref: Ref,
|
||||
@@ -191,6 +198,12 @@ type ShapeAdapterResult = struct {
|
||||
adapter_mod: usize, // adapter FnDecl 所属模块
|
||||
};
|
||||
|
||||
// 类型生成器形参的 shape 约束求值结果:lang 是最终绑定进生成器的类型。
|
||||
type ShapeArgBind = struct {
|
||||
ok: bool,
|
||||
lang: usize,
|
||||
};
|
||||
|
||||
// comptime VM 内 IR 函数引用 -> Sema FnSym 的映射,用于把函数值还原为符号。
|
||||
type CtFnRef = struct { ref: Ref, sym: usize };
|
||||
|
||||
@@ -228,6 +241,9 @@ type ShapeState = struct {
|
||||
type_binds: Vec, // ShapeTypeBind,实例形参 ref -> lang
|
||||
assoc_types: Vec, // ShapeAssocType,shape 关联类型占位缓存
|
||||
composed: Vec, // ShapeComp,A + B 的临时组合
|
||||
patch_roots: Vec, // Ref,实例化时只需重解析的语句根
|
||||
patching: bool,
|
||||
patch_decl: usize, // 正在 patch 的 decl 下标;嵌套分析其他函数时必须走完整 sema
|
||||
};
|
||||
|
||||
// 一次 shape 调用点处理的结果;fn_tid/ret_tid 为实例化后的真实签名。
|
||||
@@ -245,6 +261,9 @@ type ShapeHooks = struct {
|
||||
check_conforms: fn(*any, Ref, usize): void,
|
||||
op_method: fn(*any, usize, usize, []u8): usize,
|
||||
has_conformance: fn(*any, usize, usize): bool,
|
||||
is_shape_lang: fn(*any, usize): bool,
|
||||
bind_type_arg: fn(*any, Ref, usize, *ShapeArgBind): void,
|
||||
apply_expr: fn(*any, Ref, []u8, []Ref, []Ref): usize,
|
||||
};
|
||||
|
||||
// decorator/param/impl 的挂载与调用由 hook 模块负责;sema 只提供
|
||||
@@ -256,13 +275,14 @@ type HookParamResult = struct {
|
||||
|
||||
type HookHooks = struct {
|
||||
apply_decorators: fn(*any, usize): void,
|
||||
prepare_params: fn(*any, Ref, []Ref, *HookParamResult): void,
|
||||
prepare_params: fn(*any, Ref, Ref, []Ref, *HookParamResult): void,
|
||||
is_impl: fn(*any, Ref): bool,
|
||||
};
|
||||
|
||||
type ExprHooks = struct {
|
||||
sema_expr: fn(*any, Ref, usize): void,
|
||||
sema_body: fn(*any, usize, usize): void,
|
||||
patch_body: fn(*any, usize, usize): void,
|
||||
iter_capture_members: fn(*any, Ref): Vec,
|
||||
};
|
||||
|
||||
@@ -288,6 +308,7 @@ type TypeHooks = struct {
|
||||
resolve_param: fn(*any, Ref): usize,
|
||||
type_gen: fn(*any, Ref, Node.Kind.Call, usize): usize,
|
||||
instantiate_generator: fn(*any, Ref, Node.Kind.Call, usize, usize): usize,
|
||||
type_is_generator: fn(*any, Ref): bool,
|
||||
type_arg_lang: fn(*any, Ref): usize,
|
||||
type_value_lang: fn(*any, Ref): usize,
|
||||
type_handle_arg: fn(*any, usize): usize,
|
||||
@@ -306,7 +327,6 @@ type BindingHooks = struct {
|
||||
builtin_tag_id: fn(*any, Ref): Builtin,
|
||||
has_tag: fn(*any, Ref, []u8): bool,
|
||||
has_builtin_tag: fn(*any, Ref): bool,
|
||||
insert_method_receiver: fn(*any, Ref, Ref, []Ref, *HookParamResult): void,
|
||||
collect_builtins: fn(*any): void,
|
||||
list_has_tag: fn(*any, []Ref, []u8): bool,
|
||||
raw_tag: fn(*any, Ref): []u8,
|
||||
@@ -328,6 +348,8 @@ hir: Bld.HirNodeContainer,
|
||||
mods: Vec, // Module
|
||||
cur_mod: usize,
|
||||
refs: Vec, // RefInfo,按 Ref 下标;0 号为哨兵
|
||||
reflect_stack: Vec, // 正在反射的 lang;互递归类型用名字截断
|
||||
reflect_srcs: Vec, // ReflectSrc:反射方法副本 -> 原始 FnDecl
|
||||
types: LangType,
|
||||
defs: Vec, // Def
|
||||
fn_syms: Vec, // FnSym(函数记录,惰性登记)
|
||||
@@ -338,6 +360,7 @@ def_global: Vec, // per def id -> gidx+1(0 = 非全局)
|
||||
loop_results: Vec, // 循环结果 lang 栈(供 break 值)
|
||||
current_agg: usize, // 当前所在聚合 lang(`@self`)
|
||||
self_lang: usize, // 非 0:当前实例化目标的 lang;`@self` 优先解析到它
|
||||
ret_override: usize, // 非 0:catch handler 内的 ret 使用外层函数返回类型
|
||||
lang_attach_depth: usize,
|
||||
|
||||
current_container: Ref, // 当前名称空间容器 ref(名字解析用)
|
||||
@@ -383,21 +406,32 @@ type_hooks: TypeHooks,
|
||||
binding_hooks: BindingHooks,
|
||||
dump_hooks: DumpHooks,
|
||||
|
||||
fn node_loc_of(self: *Sema, ref: Ref, line: *usize, col: *usize): bool {
|
||||
if ref == 0 { ret false; }
|
||||
var n = Bld.node_ptr(&self.hir, ref);
|
||||
if n == null || n.loc.line == 0 { ret false; }
|
||||
line.* = n.loc.line;
|
||||
col.* = n.loc.col;
|
||||
ret true;
|
||||
}
|
||||
|
||||
fn append_error_loc(self: *Sema, b: *ByteBuffer, ref: Ref) {
|
||||
var use = ref;
|
||||
if use == 0 { use = self.macro_call_ref; }
|
||||
if use == 0 { use = self.ct_call_ref; }
|
||||
if use == 0 { use = self.cur_fn_ref; }
|
||||
var line: usize = 0;
|
||||
var col: usize = 0;
|
||||
var has = false;
|
||||
if use != 0 {
|
||||
var n = Bld.node_ptr(&self.hir, use);
|
||||
if n != null && n.loc.line != 0 {
|
||||
line = n.loc.line;
|
||||
col = n.loc.col;
|
||||
has = true;
|
||||
}
|
||||
var has = node_loc_of(self, use, &line, &col);
|
||||
// 生成/合成节点常常保留 ref 但 loc 为 0;继续向调用点/当前函数回退。
|
||||
if !has {
|
||||
use = self.macro_call_ref;
|
||||
has = node_loc_of(self, use, &line, &col);
|
||||
}
|
||||
if !has {
|
||||
use = self.ct_call_ref;
|
||||
has = node_loc_of(self, use, &line, &col);
|
||||
}
|
||||
if !has {
|
||||
use = self.cur_fn_ref;
|
||||
has = node_loc_of(self, use, &line, &col);
|
||||
}
|
||||
var path_mod = self.cur_mod;
|
||||
if use != 0 {
|
||||
@@ -748,6 +782,17 @@ fn record_ref_mods(self: *Sema, m: usize, refs: []Ref) {
|
||||
}
|
||||
}
|
||||
|
||||
// 克隆产生的新节点继承生成器声明模块,避免分析期 cur_mod 切换后路径漂移。
|
||||
fn record_new_ref_mods(self: *Sema, m: usize, start: usize) {
|
||||
var i = start + 1;
|
||||
var n = Bld.count(&self.hir);
|
||||
while i <= n {
|
||||
var ri = ref_info(self, i);
|
||||
if ri != null { ri.mod = m + 1; }
|
||||
i = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn new(hir: Bld.HirNodeContainer, path: ByteView, root: Ref, refs: []Ref): Sema {
|
||||
// 模块路径由 Sema 自己持有,driver 的临时 ModEntry 可以提前释放。
|
||||
var stable_path = ByteView.from_slice(Bld.dup_bytes(&hir, ByteView.as_slice(path)));
|
||||
@@ -760,6 +805,8 @@ fn new(hir: Bld.HirNodeContainer, path: ByteView, root: Ref, refs: []Ref): Sema
|
||||
.mods = Vec.blank(@sizeof(Module), @alignof(Module)),
|
||||
.cur_mod = 0,
|
||||
.refs = Vec.blank(@sizeof(RefInfo), @alignof(RefInfo)),
|
||||
.reflect_stack = Vec.blank(@sizeof(usize), @alignof(usize)),
|
||||
.reflect_srcs = Vec.blank(@sizeof(ReflectSrc), @alignof(ReflectSrc)),
|
||||
.types = tt,
|
||||
.defs = Vec.blank(@sizeof(Def), @alignof(Def)),
|
||||
.fn_syms = Vec.blank(@sizeof(FnSym), @alignof(FnSym)),
|
||||
@@ -770,6 +817,7 @@ fn new(hir: Bld.HirNodeContainer, path: ByteView, root: Ref, refs: []Ref): Sema
|
||||
.loop_results = Vec.blank(@sizeof(usize), @alignof(usize)),
|
||||
.current_agg = 0,
|
||||
.self_lang = 0,
|
||||
.ret_override = 0,
|
||||
.lang_attach_depth = 0,
|
||||
.current_container = 0,
|
||||
.in_callee = false,
|
||||
@@ -800,11 +848,12 @@ fn new(hir: Bld.HirNodeContainer, path: ByteView, root: Ref, refs: []Ref): Sema
|
||||
.gen_method_binds = Vec.blank(@sizeof(GenBindRec), @alignof(GenBindRec)),
|
||||
.gen_instances = Vec.blank(@sizeof(GenInstance), @alignof(GenInstance)),
|
||||
.iter_infos = Vec.blank(@sizeof(IterInfo), @alignof(IterInfo)),
|
||||
.expr_hooks = ExprHooks { .sema_expr = null, .sema_body = null, .iter_capture_members = null },
|
||||
.expr_hooks = ExprHooks { .sema_expr = null, .sema_body = null, .patch_body = null, .iter_capture_members = null },
|
||||
.decl_hooks = DeclHooks { .ensure_decl = null, .ensure_module = null, .analyze_fn_body = null },
|
||||
.lang_hooks = LangHooks { .record_conforms = null, .copy_conforms = null, .conform_binds = null, .method_sym = null },
|
||||
.type_hooks = TypeHooks {
|
||||
.resolve_type = null, .resolve_param = null, .type_gen = null,
|
||||
.type_is_generator = null,
|
||||
.type_arg_lang = null, .type_value_lang = null, .type_handle_arg = null,
|
||||
.type_arg_handle = null, .type_query_int = null,
|
||||
.type_builtin = null, .emit_intrinsic = null, .builtin_tid = null,
|
||||
@@ -812,7 +861,7 @@ fn new(hir: Bld.HirNodeContainer, path: ByteView, root: Ref, refs: []Ref): Sema
|
||||
},
|
||||
.binding_hooks = BindingHooks {
|
||||
.find_binding = null, .builtin_tag_id = null, .has_tag = null,
|
||||
.has_builtin_tag = null, .insert_method_receiver = null,
|
||||
.has_builtin_tag = null,
|
||||
.collect_builtins = null, .list_has_tag = null, .raw_tag = null, .tag_arg = null,
|
||||
},
|
||||
.dump_hooks = DumpHooks { .dump = null },
|
||||
@@ -823,6 +872,9 @@ fn new(hir: Bld.HirNodeContainer, path: ByteView, root: Ref, refs: []Ref): Sema
|
||||
.type_binds = Vec.blank(@sizeof(ShapeTypeBind), @alignof(ShapeTypeBind)),
|
||||
.assoc_types = Vec.blank(@sizeof(ShapeAssocType), @alignof(ShapeAssocType)),
|
||||
.composed = Vec.blank(@sizeof(ShapeComp), @alignof(ShapeComp)),
|
||||
.patch_roots = Vec.blank(@sizeof(Ref), @alignof(Ref)),
|
||||
.patching = false,
|
||||
.patch_decl = 0,
|
||||
},
|
||||
.shape_hooks = ShapeHooks {
|
||||
.bind_params = null,
|
||||
@@ -831,6 +883,9 @@ fn new(hir: Bld.HirNodeContainer, path: ByteView, root: Ref, refs: []Ref): Sema
|
||||
.check_conforms = null,
|
||||
.op_method = null,
|
||||
.has_conformance = null,
|
||||
.is_shape_lang = null,
|
||||
.bind_type_arg = null,
|
||||
.apply_expr = null,
|
||||
},
|
||||
.shape_for_item = null,
|
||||
.hook_hooks = HookHooks {
|
||||
@@ -914,8 +969,11 @@ fn drop(self: *Sema) {
|
||||
ci = ci + 1;
|
||||
}
|
||||
Vec.drop(&self.shape_state.composed);
|
||||
Vec.drop(&self.shape_state.patch_roots);
|
||||
Vec.drop(&self.ct.fn_refs);
|
||||
Vec.drop(&self.refs);
|
||||
Vec.drop(&self.reflect_stack);
|
||||
Vec.drop(&self.reflect_srcs);
|
||||
var mi: usize = 0;
|
||||
while mi < self.mods.len {
|
||||
var mm = (self.mods.data as *Module) + mi;
|
||||
@@ -1216,17 +1274,28 @@ fn scan_namespace_into(self: *Sema, ns_i: usize) {
|
||||
}
|
||||
|
||||
fn ensure_namespace(self: *Sema, container: Ref): usize {
|
||||
// 容器名空间属于容器声明的模块,不能跟着调用方 cur_mod 走;否则同一个
|
||||
// 生成 struct 会在调用方模块里重复注册同名 Decl,签名里的模块私有类型
|
||||
// 也会在错误模块里解析。
|
||||
var ns_mod = self.cur_mod;
|
||||
if container != 0 {
|
||||
var ci = ref_info(self, container);
|
||||
if ci != null && ci.mod != 0 { ns_mod = ci.mod - 1; }
|
||||
}
|
||||
var i: usize = 0;
|
||||
while i < self.namespaces.len {
|
||||
var ns0 = (self.namespaces.data as *Namespace)[i];
|
||||
if ns0.container == container && ns0.mod == self.cur_mod { ret i + 1; }
|
||||
if ns0.container == container && ns0.mod == ns_mod { ret i + 1; }
|
||||
i = i + 1;
|
||||
}
|
||||
var ns = Namespace { .mod = self.cur_mod, .container = container,
|
||||
var saved = self.cur_mod;
|
||||
if ns_mod != saved { switch_mod(self, ns_mod); }
|
||||
var ns = Namespace { .mod = ns_mod, .container = container,
|
||||
.entries = Vec.blank(@sizeof(NameEntry), @alignof(NameEntry)) };
|
||||
match Vec.push(&self.namespaces, &ns as *u8) { .Ok => {}, .Err(error) => {} }
|
||||
var idx = self.namespaces.len - 1;
|
||||
scan_namespace_into(self, idx + 1);
|
||||
if ns_mod != saved { switch_mod(self, saved); }
|
||||
ret idx + 1;
|
||||
}
|
||||
|
||||
@@ -1302,7 +1371,6 @@ fn find_binding(self: *Sema, s: []u8): usize { if self.binding_hooks.find_bindin
|
||||
fn builtin_tag_id(self: *Sema, fnref: Ref): Builtin { if self.binding_hooks.builtin_tag_id == null { ret .None; } ret self.binding_hooks.builtin_tag_id(self as *any, fnref); }
|
||||
fn has_tag(self: *Sema, fnref: Ref, name: []u8): bool { if self.binding_hooks.has_tag == null { ret false; } ret self.binding_hooks.has_tag(self as *any, fnref, name); }
|
||||
fn has_builtin_tag(self: *Sema, fnref: Ref): bool { if self.binding_hooks.has_builtin_tag == null { ret false; } ret self.binding_hooks.has_builtin_tag(self as *any, fnref); }
|
||||
fn insert_method_receiver(self: *Sema, callee_ref: Ref, callee: Ref, args: []Ref, out: *HookParamResult) { if self.binding_hooks.insert_method_receiver != null { self.binding_hooks.insert_method_receiver(self as *any, callee_ref, callee, args, out); } }
|
||||
fn collect_builtins(self: *Sema) { if self.binding_hooks.collect_builtins != null { self.binding_hooks.collect_builtins(self as *any); } }
|
||||
fn list_has_tag(self: *Sema, l: []Ref, tag: []u8): bool { if self.binding_hooks.list_has_tag == null { ret false; } ret self.binding_hooks.list_has_tag(self as *any, l, tag); }
|
||||
fn builtin_tag_raw(self: *Sema, fnref: Ref): []u8 {
|
||||
@@ -1339,6 +1407,7 @@ fn ct_lang(self: *Sema, h: tid): usize {
|
||||
fn resolve_type(self: *Sema, ref: Ref): usize { if self.type_hooks.resolve_type == null { ret 0; } ret self.type_hooks.resolve_type(self as *any, ref); }
|
||||
fn resolve_param(self: *Sema, ref: Ref): usize { if self.type_hooks.resolve_param == null { ret 0; } ret self.type_hooks.resolve_param(self as *any, ref); }
|
||||
fn type_gen_lang(self: *Sema, ref: Ref, x: Node.Kind.Call, self_lang: usize): usize { if self.type_hooks.type_gen == null { ret 0; } ret self.type_hooks.type_gen(self as *any, ref, x, self_lang); }
|
||||
fn type_is_generator(self: *Sema, fn_ref: Ref): bool { if self.type_hooks.type_is_generator == null { ret false; } ret self.type_hooks.type_is_generator(self as *any, fn_ref); }
|
||||
fn type_arg_lang(self: *Sema, ref: Ref): usize { if self.type_hooks.type_arg_lang == null { ret 0; } ret self.type_hooks.type_arg_lang(self as *any, ref); }
|
||||
fn type_value_lang(self: *Sema, ref: Ref): usize { if self.type_hooks.type_value_lang == null { ret 0; } ret self.type_hooks.type_value_lang(self as *any, ref); }
|
||||
fn type_handle_arg(self: *Sema, a0: usize): usize { if self.type_hooks.type_handle_arg == null { ret 0; } ret self.type_hooks.type_handle_arg(self as *any, a0); }
|
||||
@@ -1454,6 +1523,25 @@ fn fn_sym_for_ref(self: *Sema, fn_ref: Ref): usize {
|
||||
if sym_at(self, i).ref == fn_ref { ret i + 1; }
|
||||
i = i + 1;
|
||||
}
|
||||
// @meta.reflect 产出的是方法签名副本;反查原始 FnDecl 的符号。
|
||||
var src = reflect_src_of(self, fn_ref);
|
||||
if src != 0 && src != fn_ref { ret fn_sym_for_ref(self, src); }
|
||||
ret 0;
|
||||
}
|
||||
|
||||
fn record_reflect_src(self: *Sema, copy: Ref, src: Ref) {
|
||||
if copy == 0 || src == 0 { ret; }
|
||||
var e = ReflectSrc { .copy = copy, .src = src };
|
||||
match Vec.push(&self.reflect_srcs, &e as *u8) { .Ok => {}, .Err(error) => {} }
|
||||
}
|
||||
|
||||
fn reflect_src_of(self: *Sema, r: Ref): Ref {
|
||||
var i: usize = 0;
|
||||
while i < self.reflect_srcs.len {
|
||||
var e = (self.reflect_srcs.data as *ReflectSrc)[i];
|
||||
if e.copy == r { ret e.src; }
|
||||
i = i + 1;
|
||||
}
|
||||
ret 0;
|
||||
}
|
||||
|
||||
@@ -1615,7 +1703,7 @@ fn is_type_value_expr(self: *Sema, ref: Ref): bool {
|
||||
match n.kind {
|
||||
.IntType(_), .FloatType(_), .Bool, .Unit, .Any, .Type, .Never,
|
||||
.PtrType(_), .SliceType(_), .ArrayType(_), .FnType(_), .TupleType(_),
|
||||
.NamedType(_), .AggType(_), .ShapeApply(_), .ShapeUse(_) => { ret true; },
|
||||
.NamedType(_), .AggType(_), .ShapeApply(_), .ShapeUse(_), .ShapeNoDefault(_) => { ret true; },
|
||||
.Global(x) => {
|
||||
var d = find_decl(self, x);
|
||||
ret d != 0 && @enum2int(decl_at(self, d - 1).kind) == @enum2int(DeclKind.Type);
|
||||
@@ -1801,6 +1889,10 @@ fn sema_body(self: *Sema, body: usize, ret_tid: usize) {
|
||||
if self.expr_hooks.sema_body != null { self.expr_hooks.sema_body(self as *any, body, ret_tid); }
|
||||
}
|
||||
|
||||
fn patch_body(self: *Sema, body: usize, ret_tid: usize) {
|
||||
if self.expr_hooks.patch_body != null { self.expr_hooks.patch_body(self as *any, body, ret_tid); }
|
||||
}
|
||||
|
||||
fn iter_capture_members(self: *Sema, ref: Ref): Vec {
|
||||
if self.expr_hooks.iter_capture_members == null { ret Vec.blank(@sizeof(usize), @alignof(usize)); }
|
||||
ret self.expr_hooks.iter_capture_members(self as *any, ref);
|
||||
|
||||
Reference in new issue
Block a user