spl 编写编译器基础框架 ai修复出现的stage2和stage3问题并提供新语法
This commit is contained in:
1 parent
126b979f72
commit
50ee126d44
87 files changed
+3168
-394
No files matched your search
+495
-28
@@ -16,13 +16,15 @@ type ShapeInstance = SemaM.ShapeInstance;
|
||||
type ShapeUse = SemaM.ShapeUse;
|
||||
type ShapeAdapterResult = SemaM.ShapeAdapterResult;
|
||||
type ShapeAdapterEval = SemaM.ShapeAdapterEval;
|
||||
type ShapeArgBind = SemaM.ShapeArgBind;
|
||||
type ShapeTypeBind = SemaM.ShapeTypeBind;
|
||||
type CtFnRef = SemaM.CtFnRef;
|
||||
type Decl = SemaM.Decl;
|
||||
type ShapeCallResult = SemaM.ShapeCallResult;
|
||||
type ShapeHooks = SemaM.ShapeHooks;
|
||||
type ShapeComp = SemaM.ShapeComp;
|
||||
type ShapeBind = SemaM.ShapeBind;
|
||||
type ShapeComp = SemaM.ShapeComp;
|
||||
type RefInfo = SemaM.RefInfo;
|
||||
|
||||
fn shape_bind_find(binds: *Vec, name: []u8): usize {
|
||||
var i: usize = 0;
|
||||
@@ -55,6 +57,7 @@ fn shape_use_shape_count(self: *Sema, ref: Ref): usize {
|
||||
if n == null { ret 0; }
|
||||
match n.kind {
|
||||
.ShapeUse(x) => { ret x.shapes.len; },
|
||||
.ShapeNoDefault(xs) => { ret xs.len; },
|
||||
_ => {},
|
||||
}
|
||||
ret 1;
|
||||
@@ -68,6 +71,10 @@ fn shape_use_shape_at(self: *Sema, ref: Ref, i: usize): Ref {
|
||||
if i < x.shapes.len { ret x.shapes[i]; }
|
||||
ret 0;
|
||||
},
|
||||
.ShapeNoDefault(xs) => {
|
||||
if i < xs.len { ret xs[i]; }
|
||||
ret 0;
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
if i == 0 { ret ref; }
|
||||
@@ -338,13 +345,15 @@ fn compose_shapes(self: *Sema, use_ref: Ref): usize {
|
||||
}
|
||||
|
||||
fn shape_use_of(self: *Sema, ty_ref: Ref): ShapeUse {
|
||||
var out = ShapeUse { .is_shape = false, .adapter_decl = 0, .shape_lang = 0, .shape_ref = 0 };
|
||||
var out = ShapeUse { .is_shape = false, .has_adapter = false, .adapter_decl = 0, .shape_lang = 0, .shape_ref = 0 };
|
||||
if ty_ref == 0 { ret out; }
|
||||
var n = Bld.node_ptr(&self.hir, ty_ref);
|
||||
if n == null { ret out; }
|
||||
var adapter_name: []u8 = "";
|
||||
var no_default = false;
|
||||
match n.kind {
|
||||
.ShapeUse(x) => { adapter_name = x.adapter; },
|
||||
.ShapeNoDefault(_) => { no_default = true; },
|
||||
_ => {
|
||||
var old = at_tag_name(self, ty_ref);
|
||||
if old.len != 0 {
|
||||
@@ -355,9 +364,16 @@ fn shape_use_of(self: *Sema, ty_ref: Ref): ShapeUse {
|
||||
},
|
||||
}
|
||||
if shape_use_shape_count(self, ty_ref) == 0 { ret out; }
|
||||
// 空 adapter 的普通 shape 列表是类型参数上的纯编译期约束;
|
||||
// @[Shape] 是无默认 adapter 的运行期 shape 参数。
|
||||
if adapter_name.len == 0 && !no_default { ret out; }
|
||||
var first = shape_use_shape_at(self, ty_ref, 0);
|
||||
var shape_lang = SemaM.type_arg_lang(self, first);
|
||||
if shape_lang != 0 && !is_shape_lang(self, shape_lang) {
|
||||
var base_shape = shape_lang;
|
||||
if LangType.is_ptr(&self.types, base_shape) {
|
||||
base_shape = LangType.elem_of(&self.types, base_shape);
|
||||
}
|
||||
if shape_lang != 0 && !is_shape_lang(self, base_shape) {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeParamNotShape, "shape parameter type is not a shape", adapter_name);
|
||||
ret out;
|
||||
}
|
||||
@@ -367,15 +383,35 @@ fn shape_use_of(self: *Sema, ty_ref: Ref): ShapeUse {
|
||||
if composed == 0 { ret out; }
|
||||
shape_lang = composed;
|
||||
}
|
||||
if no_default {
|
||||
out = ShapeUse { .is_shape = true, .has_adapter = false, .adapter_decl = 0,
|
||||
.shape_lang = shape_lang, .shape_ref = ty_ref };
|
||||
ret out;
|
||||
}
|
||||
var d = find_shape_adapter(self, adapter_name);
|
||||
if d == 0 { ret out; }
|
||||
out = ShapeUse { .is_shape = true, .adapter_decl = d - 1,
|
||||
out = ShapeUse { .is_shape = true, .has_adapter = true, .adapter_decl = d - 1,
|
||||
.shape_lang = shape_lang, .shape_ref = ty_ref };
|
||||
ret out;
|
||||
}
|
||||
|
||||
// 声明期:把每个 shape 形参的 adapter/shape 固定下来;原函数体按 shape 做类型检查。
|
||||
// 空 adapter 的 shape 列表是类型参数约束,不是运行期 shape 参数。
|
||||
fn is_empty_shape_use(self: *Sema, ref: Ref): bool {
|
||||
var n = Bld.node_ptr(&self.hir, ref);
|
||||
if n == null { ret false; }
|
||||
match n.kind {
|
||||
.ShapeUse(x) => { ret x.adapter.len == 0; },
|
||||
.ShapeNoDefault(_) => { ret false; },
|
||||
_ => {},
|
||||
}
|
||||
ret false;
|
||||
}
|
||||
|
||||
// 声明期:把每个运行期 shape 形参的 adapter/shape 固定下来;原函数体按 shape 做类型检查。
|
||||
fn bind_shape_params(self: *Sema, fn_ref: Ref, params: []Ref) {
|
||||
// 类型生成器的形参是类型/编译期值,不是运行期 shape 参数;
|
||||
// 其约束由 sema_type.instantiate_generator 统一处理。
|
||||
if SemaM.type_is_generator(self, fn_ref) { ret; }
|
||||
var j: usize = 0;
|
||||
while j < params.len {
|
||||
var pn = Bld.node_ptr(&self.hir, params[j]);
|
||||
@@ -387,11 +423,15 @@ fn bind_shape_params(self: *Sema, fn_ref: Ref, params: []Ref) {
|
||||
SemaM.set_tid(self, p.ty, su.shape_lang);
|
||||
if shape_param_at(self, fn_ref, j) == 0 {
|
||||
var sp = ShapeParam { .fn_ref = fn_ref, .param_index = j,
|
||||
.has_adapter = su.has_adapter,
|
||||
.adapter_decl = su.adapter_decl,
|
||||
.shape_lang = su.shape_lang,
|
||||
.shape_ref = su.shape_ref };
|
||||
match Vec.push(&self.shape_state.params, &sp as *u8) { .Ok => {}, .Err(e) => {} }
|
||||
}
|
||||
} else if is_empty_shape_use(self, p.ty) {
|
||||
SemaM.sema_error_code_at(self, p.ty, ErrorCode.ShapeParamNotShape,
|
||||
"shape list is only valid on a type parameter", "");
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
@@ -461,8 +501,7 @@ fn shape_adapter_eval_type(self: *Sema, adi: usize, arg_lang: usize, shape_lang:
|
||||
_ => {},
|
||||
}
|
||||
if adapter_ref == 0 {
|
||||
// Ref 0 表示 identity:不生成 wrapper,装箱类型就是实参类型。
|
||||
out.lang = arg_lang;
|
||||
SemaM.sema_error_code(self, ErrorCode.ImplHookBadReturn, "shape adapter returned Ref 0", "");
|
||||
ret out;
|
||||
}
|
||||
// 非 0 的包装函数必须由本次 hook 执行生成;旧节点或任意小整数 Ref 不是 wrapper。
|
||||
@@ -512,21 +551,51 @@ fn shape_adapter_eval_type(self: *Sema, adi: usize, arg_lang: usize, shape_lang:
|
||||
}
|
||||
|
||||
// 适配器返回 `(type, wrapper)`:执行 comptime 调用,读取真实结果。
|
||||
fn adapter_eval_for(self: *Sema, adi: usize, arg_lang: usize,
|
||||
shape_lang: usize): ShapeAdapterEval {
|
||||
var out = ShapeAdapterEval { .lang = 0, .wrapper_sym = 0, .adapter_ref = 0 };
|
||||
var saved = self.cur_mod;
|
||||
var amod = SemaM.decl_at(self, adi).mod;
|
||||
if amod != saved { SemaM.switch_mod(self, amod); }
|
||||
SemaM.ensure_decl(self, adi);
|
||||
out = shape_adapter_eval_type(self, adi, arg_lang, shape_lang);
|
||||
if amod != saved { SemaM.switch_mod(self, saved); }
|
||||
ret out;
|
||||
}
|
||||
|
||||
fn shape_adapter_result(self: *Sema, sp_i: usize, arg_lang: usize): ShapeAdapterResult {
|
||||
var out = ShapeAdapterResult { .lang = 0, .wrapper_sym = 0, .adapter_ref = 0, .adapter_mod = 0 };
|
||||
if sp_i == 0 || sp_i > self.shape_state.params.len { ret out; }
|
||||
var sp = (self.shape_state.params.data as *ShapeParam) + (sp_i - 1);
|
||||
var saved = self.cur_mod;
|
||||
var adi = sp.adapter_decl;
|
||||
var amod = SemaM.decl_at(self, adi).mod;
|
||||
if amod != saved { SemaM.switch_mod(self, amod); }
|
||||
SemaM.ensure_decl(self, adi);
|
||||
var ev = shape_adapter_eval_type(self, adi, arg_lang, sp.shape_lang);
|
||||
var ev = adapter_eval_for(self, adi, arg_lang, sp.shape_lang);
|
||||
out = ShapeAdapterResult { .lang = ev.lang, .wrapper_sym = ev.wrapper_sym, .adapter_ref = ev.adapter_ref, .adapter_mod = amod };
|
||||
if amod != saved { SemaM.switch_mod(self, saved); }
|
||||
ret out;
|
||||
}
|
||||
|
||||
// 类型生成器形参的约束入口:普通 shape / ShapeApply / 组合列表 / adapter 形式
|
||||
// 都走这里,避免在 sema_type 里散落 shape 规则。
|
||||
fn bind_type_arg(user: *any, ty_ref: Ref, arg_lang: usize, out: *ShapeArgBind) {
|
||||
var self = user as *Sema;
|
||||
out.ok = false;
|
||||
out.lang = 0;
|
||||
if ty_ref == 0 || arg_lang == 0 { ret; }
|
||||
var adapter_name: []u8 = "";
|
||||
var n = Bld.node_ptr(&self.hir, ty_ref);
|
||||
if n != null {
|
||||
match n.kind {
|
||||
.ShapeUse(x) => { adapter_name = x.adapter; },
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
if !shape_conforms_use(self, ty_ref, arg_lang) { ret; }
|
||||
// 类型参数只存在于编译期;adapter 是运行期 shape 参数的概念,
|
||||
// 这里只做 shape 约束匹配,不执行转换。
|
||||
out.ok = true;
|
||||
out.lang = arg_lang;
|
||||
}
|
||||
|
||||
// shape 类型与目标方法的签名匹配;关联类型从 binds 取,不做推断。
|
||||
fn shape_type_match(self: *Sema, a: usize, b: usize, shape_lang: usize, value_lang: usize,
|
||||
binds: *Vec, reported: *bool): bool {
|
||||
@@ -658,21 +727,45 @@ fn shape_conforms_use(self: *Sema, use_ref: Ref, value_lang: usize): bool {
|
||||
// 目标类型是否具备 shape 声明里的方法名与签名;方法体在实例化后继续分析。
|
||||
fn shape_conforms(self: *Sema, shape_ref: Ref, value_lang: usize): bool {
|
||||
var shape_lang = SemaM.resolve_type(self, shape_ref);
|
||||
var want_ptr = false;
|
||||
if LangType.is_ptr(&self.types, shape_lang) {
|
||||
var inner = LangType.elem_of(&self.types, shape_lang);
|
||||
if is_shape_lang(self, inner) {
|
||||
shape_lang = inner;
|
||||
want_ptr = true;
|
||||
}
|
||||
}
|
||||
if shape_lang == 0 || !is_shape_lang(self, shape_lang) {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeParamNotShape,
|
||||
"shape parameter type is not a shape", "");
|
||||
ret false;
|
||||
}
|
||||
var vl = value_lang;
|
||||
if want_ptr {
|
||||
// `*Shape` 要求实参也是指针;指针深度必须匹配。
|
||||
if !LangType.is_ptr(&self.types, vl) {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeMismatch,
|
||||
"pointer shape requires a pointer argument", "");
|
||||
ret false;
|
||||
}
|
||||
vl = LangType.elem_of(&self.types, vl);
|
||||
}
|
||||
var binds = Vec.blank(@sizeof(ShapeBind), @alignof(ShapeBind));
|
||||
defer { Vec.drop(&binds); }
|
||||
if !shape_apply_binds(self, shape_ref, &binds) { ret false; }
|
||||
if !shape_marker_binds(self, value_lang, shape_lang, &binds) { ret false; }
|
||||
var inner_ref = shape_ref;
|
||||
var srn = Bld.node_ptr(&self.hir, shape_ref);
|
||||
if srn != null {
|
||||
match srn.kind { .PtrType(x) => { inner_ref = x; }, _ => {} }
|
||||
}
|
||||
if !shape_apply_binds(self, inner_ref, &binds) { ret false; }
|
||||
if !shape_marker_binds(self, vl, shape_lang, &binds) { ret false; }
|
||||
if !shape_assoc_names_bound(self, shape_lang, &binds) { ret false; }
|
||||
ret shape_conforms_with(self, shape_lang, value_lang, &binds);
|
||||
ret shape_conforms_with(self, shape_lang, vl, &binds);
|
||||
}
|
||||
|
||||
fn shape_conforms_with(self: *Sema, shape_lang: usize, value_lang: usize,
|
||||
binds: *Vec): bool {
|
||||
var vl = value_lang;
|
||||
var cont = LangType.container_of(&self.types, shape_lang);
|
||||
if cont == 0 { ret true; }
|
||||
var n = Bld.node_ptr(&self.hir, cont);
|
||||
@@ -695,18 +788,18 @@ fn shape_conforms_with(self: *Sema, shape_lang: usize, value_lang: usize,
|
||||
}
|
||||
var want_tid = SemaM.sym_at(self, want - 1).fn_tid;
|
||||
var reported = false;
|
||||
var got = SemaM.method_sym(self, value_lang, f.name);
|
||||
var got = SemaM.method_sym(self, vl, f.name);
|
||||
var got_tid: usize = 0;
|
||||
if got != 0 { got_tid = SemaM.sym_at(self, got - 1).fn_tid; }
|
||||
var matched = false;
|
||||
if got != 0 {
|
||||
matched = shape_type_match(self, want_tid,
|
||||
SemaM.sym_at(self, got - 1).fn_tid,
|
||||
shape_lang, value_lang,
|
||||
shape_lang, vl,
|
||||
binds, &reported);
|
||||
} else {
|
||||
// dyn 表允许用同名字段承载方法类型。
|
||||
var field_idx = LangType.member_idx(&self.types, value_lang, f.name);
|
||||
var field_idx = LangType.member_idx(&self.types, vl, f.name);
|
||||
if field_idx < 0 {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeMethodMissing,
|
||||
"shape method is missing", f.name);
|
||||
@@ -714,15 +807,20 @@ fn shape_conforms_with(self: *Sema, shape_lang: usize, value_lang: usize,
|
||||
break;
|
||||
}
|
||||
matched = shape_type_match(self, want_tid,
|
||||
LangType.member_lang(&self.types, value_lang,
|
||||
LangType.member_lang(&self.types, vl,
|
||||
field_idx as usize),
|
||||
shape_lang, value_lang,
|
||||
shape_lang, vl,
|
||||
binds, &reported);
|
||||
}
|
||||
if !matched {
|
||||
if !reported {
|
||||
var db = ByteBuffer.new();
|
||||
defer { ByteBuffer.drop(&db); }
|
||||
SemaM.append_type_name(self, &db, vl);
|
||||
ByteBuffer.append(&db, " vs ");
|
||||
SemaM.append_type_name(self, &db, value_lang);
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeMismatch,
|
||||
"shape method signature mismatch", f.name);
|
||||
"shape method signature mismatch", ByteBuffer.as_slice(db));
|
||||
}
|
||||
ok = false;
|
||||
break;
|
||||
@@ -736,12 +834,12 @@ fn shape_conforms_with(self: *Sema, shape_lang: usize, value_lang: usize,
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
if ok && value_lang != shape_lang {
|
||||
if ok && vl != shape_lang {
|
||||
match n.kind {
|
||||
.AggType(a) => {
|
||||
var pi: usize = 0;
|
||||
while pi < a.conforms.len {
|
||||
if !shape_conforms(self, a.conforms[pi], value_lang) {
|
||||
if !shape_conforms(self, a.conforms[pi], vl) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
@@ -800,7 +898,15 @@ fn check_conforms(user: *any, agg_ref: Ref, value_lang: usize) {
|
||||
}
|
||||
|
||||
fn shape_type_node(self: *Sema, lang: usize): usize {
|
||||
var r = Bld.construct(&self.hir, .{ .kind = .NamedType("__shape_ty") });
|
||||
var r: Ref = 0;
|
||||
// hook 只能 inspect 语法节点;指针 shape 实例必须露出 PtrType,
|
||||
// 否则 @Self 看不到形参已经是指针并会再次取地址。
|
||||
if LangType.is_ptr(&self.types, lang) {
|
||||
var inner = shape_type_node(self, LangType.elem_of(&self.types, lang));
|
||||
r = Bld.construct(&self.hir, .{ .kind = .PtrType(inner) });
|
||||
} else {
|
||||
r = Bld.construct(&self.hir, .{ .kind = .NamedType("__shape_ty") });
|
||||
}
|
||||
SemaM.grow_tables(self);
|
||||
SemaM.set_tid(self, r, lang);
|
||||
ret r;
|
||||
@@ -877,7 +983,218 @@ fn insert_shape_box(self: *Sema, arg: usize, ret_lang: usize, wrapper_sym: usize
|
||||
ret call;
|
||||
}
|
||||
|
||||
fn shape_instance_matches(self: *Sema, si: *ShapeInstance, fn_ref: Ref, args: []usize): bool {
|
||||
// 实例去重 key:模板 + adapter 转换结果类型。
|
||||
// 暂不做结构去重;Shape 依赖节点在实例化时重解析,key 未来需要纳入解析结果时改这里。
|
||||
// ---- 实例化依赖 patch ----
|
||||
|
||||
fn copy_clone_refinfo(self: *Sema, st: *NodeClone.CloneState) {
|
||||
SemaM.grow_tables(self);
|
||||
var i: usize = 0;
|
||||
while i < st.olds.len {
|
||||
var o = (st.olds.data as *Ref)[i];
|
||||
var nw = (st.news.data as *Ref)[i];
|
||||
if o != 0 && o < self.refs.len && nw < self.refs.len {
|
||||
var src = (self.refs.data as *RefInfo) + o;
|
||||
var dst = (self.refs.data as *RefInfo) + nw;
|
||||
dst.* = src.*;
|
||||
// 模板 Def 属于模板函数的槽位;实例必须重新分配。
|
||||
dst.def = 0;
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn lang_shape_dep(self: *Sema, t: usize, depth: usize): bool {
|
||||
if t == 0 || depth > 4 { ret false; }
|
||||
if is_shape_lang(self, t) || LangType.is_assoc(&self.types, t) { ret true; }
|
||||
var k = LangType.kind_of(&self.types, t);
|
||||
match k {
|
||||
.Ptr, .Slice => { ret lang_shape_dep(self, LangType.elem_of(&self.types, t), depth + 1); },
|
||||
.Array => { ret lang_shape_dep(self, LangType.elem_of(&self.types, t), depth + 1); },
|
||||
.Fn => {
|
||||
var i: usize = 0;
|
||||
while i < LangType.fn_param_count(&self.types, t) {
|
||||
if lang_shape_dep(self, LangType.fn_param_of(&self.types, t, i), depth + 1) { ret true; }
|
||||
i = i + 1;
|
||||
}
|
||||
ret lang_shape_dep(self, LangType.fn_ret_of(&self.types, t), depth + 1);
|
||||
},
|
||||
.Struct, .Union, .Enum => {
|
||||
var i: usize = 0;
|
||||
while i < LangType.member_count(&self.types, t) {
|
||||
if lang_shape_dep(self, LangType.member_lang(&self.types, t, i), depth + 1) { ret true; }
|
||||
i = i + 1;
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
ret false;
|
||||
}
|
||||
|
||||
fn any_shape_dep(self: *Sema, xs: []Ref): bool {
|
||||
var i: usize = 0;
|
||||
while i < xs.len {
|
||||
if ref_shape_dep(self, xs[i]) { ret true; }
|
||||
i = i + 1;
|
||||
}
|
||||
ret false;
|
||||
}
|
||||
|
||||
// 节点是否依赖 shape 形参:直接 tid 是 shape/关联类型,或子树里有这种节点。
|
||||
fn ref_shape_dep(self: *Sema, ref: Ref): bool {
|
||||
if ref == 0 { ret false; }
|
||||
if lang_shape_dep(self, SemaM.tid_of(self, ref), 0) { ret true; }
|
||||
var n = Bld.node_ptr(&self.hir, ref);
|
||||
if n == null { ret false; }
|
||||
// def 已清空:声明和任何局部引用都必须重新分析,保证实例槽位一致。
|
||||
match n.kind {
|
||||
.Local(_), .Alloc(_), .VarDecl(_), .ConstDecl(_) => { ret true; },
|
||||
_ => {},
|
||||
}
|
||||
match n.kind {
|
||||
.Block(x) => { ret any_shape_dep(self, x.stmts) || ref_shape_dep(self, x.tail); },
|
||||
.If(x) => { ret ref_shape_dep(self, x.cond) || ref_shape_dep(self, x.then) ||
|
||||
ref_shape_dep(self, x.els); },
|
||||
.IfVar(x) => { ret ref_shape_dep(self, x.init) || ref_shape_dep(self, x.then) ||
|
||||
ref_shape_dep(self, x.els); },
|
||||
.Destructure(x) => { ret ref_shape_dep(self, x.init); },
|
||||
.Loop(x) => { ret ref_shape_dep(self, x.body); },
|
||||
.Iter(x) => { ret any_shape_dep(self, x.inputs) || any_shape_dep(self, x.captures) ||
|
||||
ref_shape_dep(self, x.body) || ref_shape_dep(self, x.else_body); },
|
||||
.Break(x) => { ret ref_shape_dep(self, x.value); },
|
||||
.Return(x) => { ret ref_shape_dep(self, x); },
|
||||
.Defer(x) => { ret ref_shape_dep(self, x); },
|
||||
.ErrDefer(x) => { ret ref_shape_dep(self, x); },
|
||||
.Try(x) => { ret ref_shape_dep(self, x); },
|
||||
.Catch(x) => { ret ref_shape_dep(self, x.value) || ref_shape_dep(self, x.handler); },
|
||||
.IsNonNull(x) => { ret ref_shape_dep(self, x); },
|
||||
.OptionalPayload(x) => { ret ref_shape_dep(self, x); },
|
||||
.IsTag(x) => { ret ref_shape_dep(self, x.value); },
|
||||
.TagPayload(x) => { ret ref_shape_dep(self, x); },
|
||||
.Match(x) => { ret ref_shape_dep(self, x.operand) || any_shape_dep(self, x.arms); },
|
||||
.MatchArm(x) => { ret ref_shape_dep(self, x.pattern) || ref_shape_dep(self, x.body); },
|
||||
.PatLit(x) => { ret ref_shape_dep(self, x); },
|
||||
.PatTag(x) => { ret ref_shape_dep(self, x.sub); },
|
||||
.PatList(xs) => { ret any_shape_dep(self, xs); },
|
||||
.PatTuple(xs) => { ret any_shape_dep(self, xs); },
|
||||
.PatField(x) => { ret ref_shape_dep(self, x.sub); },
|
||||
.Alloc(x) => { ret ref_shape_dep(self, x.ty) || ref_shape_dep(self, x.value); },
|
||||
.VarDecl(x) => { ret ref_shape_dep(self, x.ty) || ref_shape_dep(self, x.init); },
|
||||
.ConstDecl(x) => { ret ref_shape_dep(self, x.ty) || ref_shape_dep(self, x.init); },
|
||||
.Field(x) => { ret ref_shape_dep(self, x.obj); },
|
||||
.Index(x) => { ret ref_shape_dep(self, x.obj) || ref_shape_dep(self, x.idx); },
|
||||
.Range(x) => { ret ref_shape_dep(self, x.begin) || ref_shape_dep(self, x.end); },
|
||||
.SliceExpr(x) => { ret ref_shape_dep(self, x.obj) || ref_shape_dep(self, x.begin) ||
|
||||
ref_shape_dep(self, x.end); },
|
||||
.Cast(x) => { ret ref_shape_dep(self, x.value); },
|
||||
.Addr(x) => { ret ref_shape_dep(self, x); },
|
||||
.Deref(x) => { ret ref_shape_dep(self, x); },
|
||||
.FieldLit(x) => { ret ref_shape_dep(self, x.value); },
|
||||
.AggLit(x) => { ret any_shape_dep(self, x.fields); },
|
||||
.ArrayLit(x) => { ret any_shape_dep(self, x.elems); },
|
||||
.TupleLit(xs) => { ret any_shape_dep(self, xs); },
|
||||
.CompoundAssign(x) => { ret ref_shape_dep(self, x.lval) || ref_shape_dep(self, x.rval); },
|
||||
.Binary(x) => { ret ref_shape_dep(self, x.lval) || ref_shape_dep(self, x.rval); },
|
||||
.Unary(x) => { ret ref_shape_dep(self, x.val); },
|
||||
.Call(x) => { ret ref_shape_dep(self, x.callee) || any_shape_dep(self, x.args); },
|
||||
.Comptime(x) => { ret ref_shape_dep(self, x); },
|
||||
_ => {},
|
||||
}
|
||||
ret false;
|
||||
}
|
||||
|
||||
// 依赖根只需要重解析到语句级;其他模板语句直接复用已分析结果。
|
||||
fn collect_patch_roots(self: *Sema, body: Ref) {
|
||||
self.shape_state.patch_roots.len = 0;
|
||||
var n = Bld.node_ptr(&self.hir, body);
|
||||
if n == null { ret; }
|
||||
match n.kind {
|
||||
.Block(x) => {
|
||||
var i: usize = 0;
|
||||
while i < x.stmts.len {
|
||||
if ref_shape_dep(self, x.stmts[i]) {
|
||||
var s = x.stmts[i];
|
||||
match Vec.push(&self.shape_state.patch_roots, &s as *u8) { .Ok => {}, .Err(e) => {} }
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
if x.tail != 0 && ref_shape_dep(self, x.tail) {
|
||||
var t = x.tail;
|
||||
match Vec.push(&self.shape_state.patch_roots, &t as *u8) { .Ok => {}, .Err(e) => {} }
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
if ref_shape_dep(self, body) {
|
||||
match Vec.push(&self.shape_state.patch_roots, &body as *u8) { .Ok => {}, .Err(e) => {} }
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// 只清掉依赖 shape 的节点;非依赖子树保留模板分析结果,sema 会在这些节点提前返回。
|
||||
fn reset_patch_defs(self: *Sema, ref: Ref) {
|
||||
if ref == 0 { ret; }
|
||||
var n = Bld.node_ptr(&self.hir, ref);
|
||||
if n == null { ret; }
|
||||
if !ref_shape_dep(self, ref) { ret; }
|
||||
var ri = SemaM.ref_info(self, ref);
|
||||
if ri != null {
|
||||
ri.tid = 0;
|
||||
ri.def = 0;
|
||||
ri.fn_sym = 0;
|
||||
ri.builtin = 0;
|
||||
ri.variant = 0;
|
||||
ri.initref = 0;
|
||||
ri.aggval = 0;
|
||||
}
|
||||
match n.kind {
|
||||
.Block(x) => {
|
||||
var i: usize = 0;
|
||||
while i < x.stmts.len { reset_patch_defs(self, x.stmts[i]); i = i + 1; }
|
||||
reset_patch_defs(self, x.tail);
|
||||
},
|
||||
.If(x) => { reset_patch_defs(self, x.cond); reset_patch_defs(self, x.then); reset_patch_defs(self, x.els); },
|
||||
.IfVar(x) => { reset_patch_defs(self, x.pattern); reset_patch_defs(self, x.init); reset_patch_defs(self, x.then); reset_patch_defs(self, x.els); },
|
||||
.Destructure(x) => { reset_patch_defs(self, x.init); },
|
||||
.Loop(x) => { reset_patch_defs(self, x.body); },
|
||||
.Iter(x) => { var i: usize = 0; while i < x.inputs.len { reset_patch_defs(self, x.inputs[i]); i = i + 1; }
|
||||
reset_patch_defs(self, x.body); reset_patch_defs(self, x.else_body); },
|
||||
.Break(x) => { reset_patch_defs(self, x.value); },
|
||||
.Alloc(x) => { reset_patch_defs(self, x.ty); reset_patch_defs(self, x.value); },
|
||||
.VarDecl(x) => { reset_patch_defs(self, x.ty); reset_patch_defs(self, x.init); },
|
||||
.ConstDecl(x) => { reset_patch_defs(self, x.ty); reset_patch_defs(self, x.init); },
|
||||
.Return(x) => { reset_patch_defs(self, x); },
|
||||
.Defer(x) => { reset_patch_defs(self, x); },
|
||||
.ErrDefer(x) => { reset_patch_defs(self, x); },
|
||||
.Try(x) => { reset_patch_defs(self, x); },
|
||||
.Catch(x) => { reset_patch_defs(self, x.value); reset_patch_defs(self, x.handler); },
|
||||
.Match(x) => { reset_patch_defs(self, x.operand); var i: usize = 0; while i < x.arms.len { reset_patch_defs(self, x.arms[i]); i = i + 1; } },
|
||||
.MatchArm(x) => { reset_patch_defs(self, x.pattern); reset_patch_defs(self, x.body); },
|
||||
.PatLit(x) => { reset_patch_defs(self, x); },
|
||||
.PatTag(x) => { reset_patch_defs(self, x.sub); },
|
||||
.PatList(xs) => { var i: usize = 0; while i < xs.len { reset_patch_defs(self, xs[i]); i = i + 1; } },
|
||||
.PatTuple(xs) => { var i: usize = 0; while i < xs.len { reset_patch_defs(self, xs[i]); i = i + 1; } },
|
||||
.PatField(x) => { reset_patch_defs(self, x.sub); },
|
||||
.Field(x) => { reset_patch_defs(self, x.obj); },
|
||||
.Index(x) => { reset_patch_defs(self, x.obj); reset_patch_defs(self, x.idx); },
|
||||
.SliceExpr(x) => { reset_patch_defs(self, x.obj); reset_patch_defs(self, x.begin); reset_patch_defs(self, x.end); },
|
||||
.Cast(x) => { reset_patch_defs(self, x.value); },
|
||||
.Addr(x) => { reset_patch_defs(self, x); },
|
||||
.Deref(x) => { reset_patch_defs(self, x); },
|
||||
.FieldLit(x) => { reset_patch_defs(self, x.value); },
|
||||
.AggLit(x) => { var i: usize = 0; while i < x.fields.len { reset_patch_defs(self, x.fields[i]); i = i + 1; } },
|
||||
.ArrayLit(x) => { var i: usize = 0; while i < x.elems.len { reset_patch_defs(self, x.elems[i]); i = i + 1; } },
|
||||
.TupleLit(xs) => { var i: usize = 0; while i < xs.len { reset_patch_defs(self, xs[i]); i = i + 1; } },
|
||||
.CompoundAssign(x) => { reset_patch_defs(self, x.lval); reset_patch_defs(self, x.rval); },
|
||||
.Binary(x) => { reset_patch_defs(self, x.lval); reset_patch_defs(self, x.rval); },
|
||||
.Unary(x) => { reset_patch_defs(self, x.val); },
|
||||
.Call(x) => { reset_patch_defs(self, x.callee); var i: usize = 0; while i < x.args.len { reset_patch_defs(self, x.args[i]); i = i + 1; } },
|
||||
.Comptime(x) => { reset_patch_defs(self, x); },
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
||||
fn shape_instance_key_eq(self: *Sema, si: *ShapeInstance, fn_ref: Ref, args: []usize): bool {
|
||||
if si.fn_ref != fn_ref || si.args.len != args.len { ret false; }
|
||||
var i: usize = 0;
|
||||
while i < args.len {
|
||||
@@ -891,7 +1208,7 @@ fn shape_instance_for(self: *Sema, fn_ref: Ref, args: []usize): usize {
|
||||
var i: usize = 0;
|
||||
while i < self.shape_state.instances.len {
|
||||
var si = (self.shape_state.instances.data as *ShapeInstance) + i;
|
||||
if shape_instance_matches(self, si, fn_ref, args) { ret si.sym; }
|
||||
if shape_instance_key_eq(self, si, fn_ref, args) { ret si.sym; }
|
||||
i = i + 1;
|
||||
}
|
||||
var orig = SemaM.fn_sym_for_ref(self, fn_ref);
|
||||
@@ -906,8 +1223,9 @@ fn shape_instance_for(self: *Sema, fn_ref: Ref, args: []usize): usize {
|
||||
SemaM.analyze_fn_body(self, od - 1);
|
||||
var clone_state = NodeClone.new();
|
||||
var clone = NodeClone.clone_ref(&self.hir, &clone_state, fn_ref);
|
||||
NodeClone.drop(&clone_state);
|
||||
SemaM.grow_tables(self);
|
||||
copy_clone_refinfo(self, &clone_state);
|
||||
NodeClone.drop(&clone_state);
|
||||
var nm = shape_instance_name(self);
|
||||
var cn = Bld.node_ptr(&self.hir, clone);
|
||||
if cn != null {
|
||||
@@ -957,7 +1275,23 @@ fn shape_instance_for(self: *Sema, fn_ref: Ref, args: []usize): usize {
|
||||
var di = self.decls.len;
|
||||
match Vec.push(&self.decls, &d as *u8) { .Ok => {}, .Err(e) => {} }
|
||||
SemaM.ensure_decl(self, di);
|
||||
// 只重解析依赖 shape 形参的语句;其余模板节点复用克隆过来的分析结果。
|
||||
var body_ref: Ref = 0;
|
||||
var cbn = Bld.node_ptr(&self.hir, clone);
|
||||
if cbn != null {
|
||||
match cbn.kind { .FnDecl(f) => { body_ref = f.body; }, _ => {} }
|
||||
}
|
||||
collect_patch_roots(self, body_ref);
|
||||
var ri: usize = 0;
|
||||
while ri < self.shape_state.patch_roots.len {
|
||||
reset_patch_defs(self, (self.shape_state.patch_roots.data as *Ref)[ri]);
|
||||
ri = ri + 1;
|
||||
}
|
||||
self.shape_state.patching = true;
|
||||
self.shape_state.patch_decl = di;
|
||||
SemaM.analyze_fn_body(self, di);
|
||||
self.shape_state.patching = false;
|
||||
self.shape_state.patch_decl = 0;
|
||||
var inst = SemaM.decl_at(self, di).sym;
|
||||
if m != saved { SemaM.switch_mod(self, saved); }
|
||||
if inst != 0 {
|
||||
@@ -1021,8 +1355,33 @@ fn prepare_call(user: *any, callee_ref: Ref, callee: Ref, args: []usize,
|
||||
if al == 0 {
|
||||
shape_ok = false;
|
||||
need_report = true;
|
||||
} else if !shape_conforms_use(self, sp.shape_ref, al) {
|
||||
} else if is_untyped_literal(self, a) {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeMismatch,
|
||||
"shape argument literal requires `as`", "");
|
||||
shape_ok = false;
|
||||
} else if !sp.has_adapter {
|
||||
// @[Shape]:没有默认 adapter;直接 conforming 允许。
|
||||
if !shape_conforms_use(self, sp.shape_ref, al) {
|
||||
shape_ok = false;
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeMismatch,
|
||||
"shape argument does not conform; add @adapter[Shape](...)",
|
||||
"");
|
||||
} else {
|
||||
match Vec.push(&shape_args, &al as *u8) {
|
||||
.Ok => {},
|
||||
.Err(e) => { shape_ok = false; },
|
||||
}
|
||||
}
|
||||
} else if SemaM.has_tag(self, a, "shape_applied") {
|
||||
// 显式 @adapter[Shape](arg) 已经完成转换,不再套默认 adapter。
|
||||
if !shape_conforms_use(self, sp.shape_ref, al) {
|
||||
shape_ok = false;
|
||||
} else {
|
||||
match Vec.push(&shape_args, &al as *u8) {
|
||||
.Ok => {},
|
||||
.Err(e) => { shape_ok = false; },
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var sr = shape_adapter_result(self, pi + 1, al);
|
||||
if sr.lang == 0 || !shape_conforms_use(self, sp.shape_ref, sr.lang) {
|
||||
@@ -1059,6 +1418,107 @@ fn prepare_call(user: *any, callee_ref: Ref, callee: Ref, args: []usize,
|
||||
}
|
||||
}
|
||||
|
||||
// 裸数字字面量没有稳定类型;shape 参数必须先 `as` 出具体类型。
|
||||
fn is_untyped_literal(self: *Sema, ref: Ref): bool {
|
||||
var n = Bld.node_ptr(&self.hir, ref);
|
||||
if n == null { ret false; }
|
||||
match n.kind {
|
||||
.LitInt(_), .LitFloat(_) => { ret true; },
|
||||
_ => {},
|
||||
}
|
||||
ret false;
|
||||
}
|
||||
|
||||
fn apply_expr_hook(user: *any, ref: Ref, adapter: []u8, shapes: []Ref, args: []Ref): usize {
|
||||
ret shape_apply_expr(user as *Sema, ref, adapter, shapes, args);
|
||||
}
|
||||
|
||||
// `@adapter[Shape](arg)`:解析 shape、执行 adapter、把 arg 替换成转换函数调用。
|
||||
fn shape_apply_expr(self: *Sema, ref: Ref, adapter: []u8, shapes: []Ref, args: []Ref): usize {
|
||||
if shapes.len == 0 {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeParamNotShape,
|
||||
"shape application requires a shape", adapter);
|
||||
ret 0;
|
||||
}
|
||||
var shape_lang = SemaM.resolve_type(self, shapes[0]);
|
||||
if shape_lang == 0 { ret 0; }
|
||||
if shapes.len > 1 {
|
||||
var use_ref = Bld.construct(&self.hir, .{ .kind = .ShapeUse(.{
|
||||
.adapter = "",
|
||||
.shapes = shapes,
|
||||
}) });
|
||||
shape_lang = compose_shapes(self, use_ref);
|
||||
if shape_lang == 0 { ret 0; }
|
||||
}
|
||||
if !is_shape_lang(self, shape_lang) {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeParamNotShape,
|
||||
"shape application target is not a shape", adapter);
|
||||
ret 0;
|
||||
}
|
||||
var adi = find_shape_adapter(self, adapter);
|
||||
if adi == 0 { ret 0; }
|
||||
if args.len != 1 {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeMismatch,
|
||||
"shape application expects one argument", adapter);
|
||||
ret 0;
|
||||
}
|
||||
var value = args[0];
|
||||
SemaM.sema_expr(self, value, 0);
|
||||
if is_untyped_literal(self, value) {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeMismatch,
|
||||
"shape application literal requires `as`", adapter);
|
||||
ret 0;
|
||||
}
|
||||
var arg_lang = SemaM.tid_of(self, value);
|
||||
if arg_lang == 0 {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeMismatch,
|
||||
"shape application argument has no type", adapter);
|
||||
ret 0;
|
||||
}
|
||||
var ev = adapter_eval_for(self, adi - 1, arg_lang, shape_lang);
|
||||
if ev.adapter_ref == 0 || ev.lang == 0 { ret 0; }
|
||||
var binds = Vec.blank(@sizeof(ShapeBind), @alignof(ShapeBind));
|
||||
defer { Vec.drop(&binds); }
|
||||
if !shape_conforms_with(self, shape_lang, ev.lang, &binds) {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeMismatch,
|
||||
"shape application result does not conform", adapter);
|
||||
ret 0;
|
||||
}
|
||||
var amod = SemaM.decl_at(self, adi - 1).mod;
|
||||
var sym = ensure_adapter_fn(self, ev.adapter_ref, amod);
|
||||
if sym == 0 { ret 0; }
|
||||
var ws = SemaM.sym_at(self, sym - 1);
|
||||
var callee = Bld.construct(&self.hir, .{ .kind = .Global(ws.name) });
|
||||
SemaM.set_node_fn(self, callee, sym);
|
||||
SemaM.set_tid(self, callee, ws.fn_tid);
|
||||
var av = Vec.blank(@sizeof(usize), @alignof(usize));
|
||||
defer { Vec.drop(&av); }
|
||||
match Vec.push(&av, &value as *u8) { .Ok => {}, .Err(e) => { ret 0; } }
|
||||
var call = Bld.construct(&self.hir, .{ .kind = .Call(.{
|
||||
.is_comptime = false,
|
||||
.callee = callee,
|
||||
.args = (av.data as *Ref)[0..av.len],
|
||||
}) });
|
||||
SemaM.sema_expr(self, call, 0);
|
||||
var t = SemaM.tid_of(self, call);
|
||||
if t == 0 { ret 0; }
|
||||
var empty: [1]Ref = .[0];
|
||||
var tag = Bld.construct(&self.hir, .{ .kind = .Tag(.{
|
||||
.is_at = false,
|
||||
.name = "shape_applied",
|
||||
.args = empty[0..0],
|
||||
}) });
|
||||
var p = Bld.node_ptr(&self.hir, ref);
|
||||
var rn = Bld.node_ptr(&self.hir, call);
|
||||
if p != null && rn != null {
|
||||
p.kind = rn.kind;
|
||||
p.tags = rn.tags;
|
||||
var tags: [1]Ref = .[tag];
|
||||
p.tags = Bld.dup_refs(&self.hir, tags[0..1]);
|
||||
}
|
||||
ret t;
|
||||
}
|
||||
|
||||
fn op_method(user: *any, value_lang: usize, shape_lang: usize, method: []u8): usize {
|
||||
var self = user as *Sema;
|
||||
if !shape_marker_has(self, value_lang, shape_lang) { ret 0; }
|
||||
@@ -1074,6 +1534,10 @@ fn marker_has_hook(user: *any, value_lang: usize, shape_lang: usize): bool {
|
||||
ret shape_marker_has(user as *Sema, value_lang, shape_lang);
|
||||
}
|
||||
|
||||
fn shape_lang_hook(user: *any, lang: usize): bool {
|
||||
ret is_shape_lang(user as *Sema, lang);
|
||||
}
|
||||
|
||||
fn hooks(): ShapeHooks {
|
||||
ret ShapeHooks {
|
||||
.bind_params = bind_params,
|
||||
@@ -1082,6 +1546,9 @@ fn hooks(): ShapeHooks {
|
||||
.check_conforms = check_conforms,
|
||||
.op_method = op_method,
|
||||
.has_conformance = marker_has_hook,
|
||||
.is_shape_lang = shape_lang_hook,
|
||||
.bind_type_arg = bind_type_arg,
|
||||
.apply_expr = apply_expr_hook,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user