stage3 ai编写新语法语义 同时提供fflush
This commit is contained in:
1 parent
bb8761d392
commit
126b979f72
170 files changed
+10729
-3175
No files matched your search
+525
-23
@@ -21,6 +21,140 @@ type CtFnRef = SemaM.CtFnRef;
|
||||
type Decl = SemaM.Decl;
|
||||
type ShapeCallResult = SemaM.ShapeCallResult;
|
||||
type ShapeHooks = SemaM.ShapeHooks;
|
||||
type ShapeComp = SemaM.ShapeComp;
|
||||
type ShapeBind = SemaM.ShapeBind;
|
||||
|
||||
fn shape_bind_find(binds: *Vec, name: []u8): usize {
|
||||
var i: usize = 0;
|
||||
while i < binds.len {
|
||||
var b = (binds.data as *ShapeBind) + i;
|
||||
if SemaM.eq(b.name, name) { ret b.lang; }
|
||||
i = i + 1;
|
||||
}
|
||||
ret 0;
|
||||
}
|
||||
|
||||
fn shape_bind_put(self: *Sema, binds: *Vec, name: []u8, lang: usize): bool {
|
||||
var old = shape_bind_find(binds, name);
|
||||
if old != 0 {
|
||||
if old == lang { ret true; }
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeAssocMismatch,
|
||||
"associated type binding mismatch", name);
|
||||
ret false;
|
||||
}
|
||||
var b = ShapeBind { .name = name, .lang = lang };
|
||||
match Vec.push(binds, &b as *u8) {
|
||||
.Ok => { ret true; },
|
||||
.Err(error) => { ret false; },
|
||||
}
|
||||
ret false;
|
||||
}
|
||||
|
||||
fn shape_use_shape_count(self: *Sema, ref: Ref): usize {
|
||||
var n = Bld.node_ptr(&self.hir, ref);
|
||||
if n == null { ret 0; }
|
||||
match n.kind {
|
||||
.ShapeUse(x) => { ret x.shapes.len; },
|
||||
_ => {},
|
||||
}
|
||||
ret 1;
|
||||
}
|
||||
|
||||
fn shape_use_shape_at(self: *Sema, ref: Ref, i: usize): Ref {
|
||||
var n = Bld.node_ptr(&self.hir, ref);
|
||||
if n == null { ret 0; }
|
||||
match n.kind {
|
||||
.ShapeUse(x) => {
|
||||
if i < x.shapes.len { ret x.shapes[i]; }
|
||||
ret 0;
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
if i == 0 { ret ref; }
|
||||
ret 0;
|
||||
}
|
||||
|
||||
fn shape_apply_binds(self: *Sema, shape_ref: Ref, binds: *Vec): bool {
|
||||
var n = Bld.node_ptr(&self.hir, shape_ref);
|
||||
if n == null { ret true; }
|
||||
match n.kind {
|
||||
.ShapeApply(x) => {
|
||||
var i: usize = 0;
|
||||
while i < x.binds.len {
|
||||
var bn = Bld.node_ptr(&self.hir, x.binds[i]);
|
||||
if bn != null {
|
||||
match bn.kind {
|
||||
.AssocBind(b) => {
|
||||
var l = SemaM.resolve_type(self, b.ty);
|
||||
if !shape_bind_put(self, binds, b.name, l) { ret false; }
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
ret true;
|
||||
}
|
||||
|
||||
// shape 声明上的 marker 绑定作为默认值;使用点显式绑定优先后合并。
|
||||
fn shape_marker_binds(self: *Sema, value_lang: usize, shape_lang: usize, binds: *Vec): bool {
|
||||
if SemaM.lang_conform_binds(self, value_lang, shape_lang, binds) { ret true; }
|
||||
var cont = LangType.container_of(&self.types, value_lang);
|
||||
if cont == 0 { ret true; }
|
||||
var n = Bld.node_ptr(&self.hir, cont);
|
||||
if n == null { ret true; }
|
||||
var ok = true;
|
||||
match n.kind {
|
||||
.AggType(a) => {
|
||||
var i: usize = 0;
|
||||
while i < a.conforms.len {
|
||||
var cref = a.conforms[i];
|
||||
var sl = SemaM.resolve_type(self, cref);
|
||||
if sl == shape_lang {
|
||||
if !shape_apply_binds(self, cref, binds) { ok = false; }
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
ret ok;
|
||||
}
|
||||
|
||||
fn shape_assoc_names_bound(self: *Sema, shape_lang: usize, binds: *Vec): bool {
|
||||
var cont = LangType.container_of(&self.types, shape_lang);
|
||||
if cont == 0 { ret true; }
|
||||
var n = Bld.node_ptr(&self.hir, cont);
|
||||
if n == null { ret true; }
|
||||
var ok = true;
|
||||
match n.kind {
|
||||
.AggType(a) => {
|
||||
var i: usize = 0;
|
||||
while i < a.decls.len {
|
||||
var dn = Bld.node_ptr(&self.hir, a.decls[i]);
|
||||
if dn != null {
|
||||
match dn.kind {
|
||||
.AssocDecl(x) => {
|
||||
if shape_bind_find(binds, x.name) == 0 {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeAssocUnbound,
|
||||
"associated type is not bound", x.name);
|
||||
ok = false;
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
ret ok;
|
||||
}
|
||||
|
||||
// ---- shape:声明期绑定 adapter、调用点生成实例 ----
|
||||
|
||||
fn shape_param_at(self: *Sema, fn_ref: Ref, param_index: usize): usize {
|
||||
@@ -56,7 +190,7 @@ fn is_shape_lang(self: *Sema, lang: usize): bool {
|
||||
ret is_shape;
|
||||
}
|
||||
|
||||
// 找到该 shape 参数显式挂载的 adapter 名,例如 `@alloc Allocator` 的 `alloc`。
|
||||
// 找到该 shape 参数显式挂载的 adapter 名,例如 `@alloc[Allocator]` 的 `alloc`。
|
||||
fn at_tag_name(self: *Sema, ref: Ref): []u8 {
|
||||
var n = Bld.node_ptr(&self.hir, ref);
|
||||
if n == null { ret ""; }
|
||||
@@ -74,7 +208,7 @@ fn at_tag_name(self: *Sema, ref: Ref): []u8 {
|
||||
ret "";
|
||||
}
|
||||
|
||||
// `@name Shape` 只按 name 查找 impl hook;不再扫描模块里的唯一候选。
|
||||
// `@name[Shape]` 只按 name 查找 adapter hook;不再扫描模块里的唯一候选。
|
||||
fn find_shape_adapter(self: *Sema, name: []u8): usize {
|
||||
var d = SemaM.find_decl(self, name);
|
||||
if d == 0 {
|
||||
@@ -108,24 +242,135 @@ fn find_shape_adapter(self: *Sema, name: []u8): usize {
|
||||
ret d;
|
||||
}
|
||||
|
||||
// `@call Shape` 是带挂载项的 type_expr;这里解析显式 adapter 名和 shape 类型。
|
||||
// `@name[Shape]` 是 type-only shape 使用点;这里解析显式 adapter 名和 shape 类型。
|
||||
// 组合 shape 的同名方法必须兼容;不同签名直接报 E0320。
|
||||
fn compose_conflicts(self: *Sema, use_ref: Ref): bool {
|
||||
var count = shape_use_shape_count(self, use_ref);
|
||||
var i: usize = 0;
|
||||
while i < count {
|
||||
var si = SemaM.resolve_type(self, shape_use_shape_at(self, use_ref, i));
|
||||
var ci = LangType.container_of(&self.types, si);
|
||||
var ni = Bld.node_ptr(&self.hir, ci);
|
||||
if ni != null {
|
||||
var j: usize = i + 1;
|
||||
while j < count {
|
||||
var sj = SemaM.resolve_type(self, shape_use_shape_at(self, use_ref, j));
|
||||
var cj = LangType.container_of(&self.types, sj);
|
||||
var nj = Bld.node_ptr(&self.hir, cj);
|
||||
if nj != null {
|
||||
match ni.kind {
|
||||
.AggType(ai) => {
|
||||
var k: usize = 0;
|
||||
while k < ai.decls.len {
|
||||
var dn = Bld.node_ptr(&self.hir, ai.decls[k]);
|
||||
if dn != null {
|
||||
match dn.kind {
|
||||
.FnDecl(f) => {
|
||||
var other = SemaM.method_sym(self, sj, f.name);
|
||||
var own = SemaM.method_sym(self, si, f.name);
|
||||
if other != 0 && own != 0 {
|
||||
var binds = Vec.blank(@sizeof(ShapeBind), @alignof(ShapeBind));
|
||||
var reported = false;
|
||||
var same = shape_type_match(self,
|
||||
SemaM.sym_at(self, own - 1).fn_tid,
|
||||
SemaM.sym_at(self, other - 1).fn_tid,
|
||||
si, sj, &binds, &reported);
|
||||
Vec.drop(&binds);
|
||||
if !same {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeMethodConflict,
|
||||
"shape method conflict", f.name);
|
||||
ret true;
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
k = k + 1;
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
j = j + 1;
|
||||
}
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
ret false;
|
||||
}
|
||||
|
||||
// `A + B` 在原始函数体里需要一个合并方法集的临时 shape;实例化后仍按真实类型替换。
|
||||
fn compose_shapes(self: *Sema, use_ref: Ref): usize {
|
||||
var count = shape_use_shape_count(self, use_ref);
|
||||
if count <= 1 { ret 0; }
|
||||
if compose_conflicts(self, use_ref) { ret 0; }
|
||||
var parts = Vec.blank(@sizeof(usize), @alignof(usize));
|
||||
var i: usize = 0;
|
||||
while i < count {
|
||||
var sr = shape_use_shape_at(self, use_ref, i);
|
||||
var sl = SemaM.resolve_type(self, sr);
|
||||
match Vec.push(&parts, &sl as *u8) {
|
||||
.Ok => {},
|
||||
.Err(error) => { Vec.drop(&parts); ret 0; },
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
var empty: [1]Ref = .[0];
|
||||
var agg = Bld.construct(&self.hir, .{ .kind = .AggType(.{
|
||||
.kind = .Shape,
|
||||
.conforms = empty[0..0],
|
||||
.fields = empty[0..0],
|
||||
.decls = empty[0..0],
|
||||
.injected = empty[0..0],
|
||||
}) });
|
||||
if agg == 0 { Vec.drop(&parts); ret 0; }
|
||||
self.shape_state.seq = self.shape_state.seq + 1;
|
||||
var lang = LangType.declare_struct(&self.types);
|
||||
LangType.set_container(&self.types, lang, self.cur_mod, agg);
|
||||
LangType.set_name(&self.types, lang, "__shape_comp");
|
||||
var rec = ShapeComp { .lang = lang, .parts = parts };
|
||||
match Vec.push(&self.shape_state.composed, &rec as *u8) {
|
||||
.Ok => {},
|
||||
.Err(error) => { Vec.drop(&parts); ret 0; },
|
||||
}
|
||||
ret lang;
|
||||
}
|
||||
|
||||
fn shape_use_of(self: *Sema, ty_ref: Ref): ShapeUse {
|
||||
var out = ShapeUse { .is_shape = false, .adapter_decl = 0, .shape_lang = 0 };
|
||||
var out = ShapeUse { .is_shape = 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; }
|
||||
if n.tags.len == 0 { ret out; }
|
||||
var adapter_name = at_tag_name(self, ty_ref);
|
||||
if adapter_name.len == 0 { ret out; }
|
||||
var shape_lang = SemaM.type_arg_lang(self, ty_ref);
|
||||
var adapter_name: []u8 = "";
|
||||
match n.kind {
|
||||
.ShapeUse(x) => { adapter_name = x.adapter; },
|
||||
_ => {
|
||||
var old = at_tag_name(self, ty_ref);
|
||||
if old.len != 0 {
|
||||
SemaM.sema_error_code(self, ErrorCode.Syntax,
|
||||
"shape use requires @adapter[Shape]", old);
|
||||
}
|
||||
ret out;
|
||||
},
|
||||
}
|
||||
if shape_use_shape_count(self, ty_ref) == 0 { 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) {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeParamNotShape, "shape parameter type is not a shape", adapter_name);
|
||||
ret out;
|
||||
}
|
||||
if shape_lang == 0 { ret out; }
|
||||
if shape_use_shape_count(self, ty_ref) > 1 {
|
||||
var composed = compose_shapes(self, ty_ref);
|
||||
if composed == 0 { ret out; }
|
||||
shape_lang = composed;
|
||||
}
|
||||
var d = find_shape_adapter(self, adapter_name);
|
||||
if d == 0 { ret out; }
|
||||
out = ShapeUse { .is_shape = true, .adapter_decl = d - 1, .shape_lang = shape_lang };
|
||||
out = ShapeUse { .is_shape = true, .adapter_decl = d - 1,
|
||||
.shape_lang = shape_lang, .shape_ref = ty_ref };
|
||||
ret out;
|
||||
}
|
||||
|
||||
@@ -143,7 +388,8 @@ fn bind_shape_params(self: *Sema, fn_ref: Ref, params: []Ref) {
|
||||
if shape_param_at(self, fn_ref, j) == 0 {
|
||||
var sp = ShapeParam { .fn_ref = fn_ref, .param_index = j,
|
||||
.adapter_decl = su.adapter_decl,
|
||||
.shape_lang = su.shape_lang };
|
||||
.shape_lang = su.shape_lang,
|
||||
.shape_ref = su.shape_ref };
|
||||
match Vec.push(&self.shape_state.params, &sp as *u8) { .Ok => {}, .Err(e) => {} }
|
||||
}
|
||||
}
|
||||
@@ -281,12 +527,157 @@ fn shape_adapter_result(self: *Sema, sp_i: usize, arg_lang: usize): ShapeAdapter
|
||||
ret out;
|
||||
}
|
||||
|
||||
// 目标类型是否具备 shape 声明里的方法名与 fn 类型;方法体在实例化后继续分析。
|
||||
fn shape_conforms(self: *Sema, shape_lang: usize, value_lang: usize): bool {
|
||||
// shape 类型与目标方法的签名匹配;关联类型从 binds 取,不做推断。
|
||||
fn shape_type_match(self: *Sema, a: usize, b: usize, shape_lang: usize, value_lang: usize,
|
||||
binds: *Vec, reported: *bool): bool {
|
||||
if a == 0 || b == 0 { ret a == b; }
|
||||
if LangType.is_assoc(&self.types, a) {
|
||||
var name = LangType.name_of(&self.types, a);
|
||||
var bound = shape_bind_find(binds, name);
|
||||
if bound == 0 {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeAssocUnbound,
|
||||
"associated type is not bound", name);
|
||||
reported.* = true;
|
||||
ret false;
|
||||
}
|
||||
if bound == b { ret true; }
|
||||
if shape_type_match(self, bound, b, shape_lang, value_lang, binds, reported) { ret true; }
|
||||
if !reported.* {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeAssocMismatch,
|
||||
"associated type does not match", name);
|
||||
reported.* = true;
|
||||
}
|
||||
ret false;
|
||||
}
|
||||
if a == shape_lang { ret b == value_lang; }
|
||||
var ka = LangType.kind_of(&self.types, a);
|
||||
var kb = LangType.kind_of(&self.types, b);
|
||||
if !LangType.kind_eq(ka, kb) { ret false; }
|
||||
match ka {
|
||||
.Ptr, .Slice => {
|
||||
ret shape_type_match(self, LangType.elem_of(&self.types, a),
|
||||
LangType.elem_of(&self.types, b),
|
||||
shape_lang, value_lang, binds, reported);
|
||||
},
|
||||
.Array => {
|
||||
if LangType.array_len_of(&self.types, a) != LangType.array_len_of(&self.types, b) {
|
||||
ret false;
|
||||
}
|
||||
ret shape_type_match(self, LangType.elem_of(&self.types, a),
|
||||
LangType.elem_of(&self.types, b),
|
||||
shape_lang, value_lang, binds, reported);
|
||||
},
|
||||
.Fn => {
|
||||
if LangType.fn_param_count(&self.types, a) != LangType.fn_param_count(&self.types, b) {
|
||||
ret false;
|
||||
}
|
||||
var i: usize = 0;
|
||||
while i < LangType.fn_param_count(&self.types, a) {
|
||||
if !shape_type_match(self, LangType.fn_param_of(&self.types, a, i),
|
||||
LangType.fn_param_of(&self.types, b, i),
|
||||
shape_lang, value_lang, binds, reported) {
|
||||
ret false;
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
ret shape_type_match(self, LangType.fn_ret_of(&self.types, a),
|
||||
LangType.fn_ret_of(&self.types, b),
|
||||
shape_lang, value_lang, binds, reported);
|
||||
},
|
||||
.Struct, .Union, .Enum => {
|
||||
if LangType.member_count(&self.types, a) != LangType.member_count(&self.types, b) {
|
||||
ret false;
|
||||
}
|
||||
var i: usize = 0;
|
||||
while i < LangType.member_count(&self.types, a) {
|
||||
if !SemaM.eq(LangType.member_name(&self.types, a, i),
|
||||
LangType.member_name(&self.types, b, i)) {
|
||||
ret false;
|
||||
}
|
||||
if !shape_type_match(self, LangType.member_lang(&self.types, a, i),
|
||||
LangType.member_lang(&self.types, b, i),
|
||||
shape_lang, value_lang, binds, reported) {
|
||||
ret false;
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
if LangType.kind_eq(ka, .Enum) {
|
||||
if LangType.enum_tag(&self.types, a) != LangType.enum_tag(&self.types, b) { ret false; }
|
||||
if LangType.enum_ok_index(&self.types, a) != LangType.enum_ok_index(&self.types, b) { ret false; }
|
||||
if LangType.enum_error_count(&self.types, a) != LangType.enum_error_count(&self.types, b) { ret false; }
|
||||
}
|
||||
ret true;
|
||||
},
|
||||
.Int, .Float => { ret LangType.ir_of(&self.types, a) == LangType.ir_of(&self.types, b); },
|
||||
.Shape => { ret a == b; },
|
||||
.Bool, .Unit, .Any, .Type, .Never, .Error => { ret true; },
|
||||
.Assoc => { ret false; },
|
||||
}
|
||||
ret false;
|
||||
}
|
||||
|
||||
fn shape_marker_has(self: *Sema, value_lang: usize, shape_lang: usize): bool {
|
||||
var cont = LangType.container_of(&self.types, value_lang);
|
||||
if cont == 0 { ret false; }
|
||||
var n = Bld.node_ptr(&self.hir, cont);
|
||||
if n == null { ret false; }
|
||||
match n.kind {
|
||||
.AggType(a) => {
|
||||
var i: usize = 0;
|
||||
while i < a.conforms.len {
|
||||
if SemaM.resolve_type(self, a.conforms[i]) == shape_lang { ret true; }
|
||||
i = i + 1;
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
ret false;
|
||||
}
|
||||
|
||||
fn shape_conforms_use(self: *Sema, use_ref: Ref, value_lang: usize): bool {
|
||||
var count = shape_use_shape_count(self, use_ref);
|
||||
var i: usize = 0;
|
||||
while i < count {
|
||||
var sref = shape_use_shape_at(self, use_ref, i);
|
||||
var sn = Bld.node_ptr(&self.hir, sref);
|
||||
var simple = false;
|
||||
if sn != null {
|
||||
match sn.kind { .NamedType(_) => { simple = true; }, _ => {} }
|
||||
}
|
||||
// 类型声明上的 marker 已经完整检查过;无名绑定时使用点不重复检查。
|
||||
if simple && shape_marker_has(self, value_lang, SemaM.resolve_type(self, sref)) {
|
||||
i = i + 1;
|
||||
continue;
|
||||
}
|
||||
if !shape_conforms(self, sref, value_lang) { ret false; }
|
||||
i = i + 1;
|
||||
}
|
||||
ret true;
|
||||
}
|
||||
|
||||
// 目标类型是否具备 shape 声明里的方法名与签名;方法体在实例化后继续分析。
|
||||
fn shape_conforms(self: *Sema, shape_ref: Ref, value_lang: usize): bool {
|
||||
var shape_lang = SemaM.resolve_type(self, shape_ref);
|
||||
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 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; }
|
||||
if !shape_assoc_names_bound(self, shape_lang, &binds) { ret false; }
|
||||
ret shape_conforms_with(self, shape_lang, value_lang, &binds);
|
||||
}
|
||||
|
||||
fn shape_conforms_with(self: *Sema, shape_lang: usize, value_lang: usize,
|
||||
binds: *Vec): bool {
|
||||
var cont = LangType.container_of(&self.types, shape_lang);
|
||||
if cont == 0 { ret true; }
|
||||
var n = Bld.node_ptr(&self.hir, cont);
|
||||
if n == null { ret true; }
|
||||
var ok = true;
|
||||
match n.kind {
|
||||
.AggType(a) => {
|
||||
var i: usize = 0;
|
||||
@@ -296,15 +687,45 @@ fn shape_conforms(self: *Sema, shape_lang: usize, value_lang: usize): bool {
|
||||
match dn.kind {
|
||||
.FnDecl(f) => {
|
||||
var want = SemaM.method_sym(self, shape_lang, f.name);
|
||||
if want == 0 { ret false; }
|
||||
if want == 0 {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeMethodMissing,
|
||||
"shape method declaration is missing", f.name);
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
var want_tid = SemaM.sym_at(self, want - 1).fn_tid;
|
||||
var reported = false;
|
||||
var got = SemaM.method_sym(self, value_lang, f.name);
|
||||
if got != 0 && SemaM.sym_at(self, got - 1).fn_tid == want_tid { i = i + 1; continue; }
|
||||
// dyn 表可以是同名字段:字段类型必须正好是该 shape 方法类型。
|
||||
var field_idx = LangType.member_idx(&self.types, value_lang, f.name);
|
||||
if field_idx < 0 { ret false; }
|
||||
if LangType.member_lang(&self.types, value_lang, field_idx as usize) != want_tid {
|
||||
ret false;
|
||||
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,
|
||||
binds, &reported);
|
||||
} else {
|
||||
// dyn 表允许用同名字段承载方法类型。
|
||||
var field_idx = LangType.member_idx(&self.types, value_lang, f.name);
|
||||
if field_idx < 0 {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeMethodMissing,
|
||||
"shape method is missing", f.name);
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
matched = shape_type_match(self, want_tid,
|
||||
LangType.member_lang(&self.types, value_lang,
|
||||
field_idx as usize),
|
||||
shape_lang, value_lang,
|
||||
binds, &reported);
|
||||
}
|
||||
if !matched {
|
||||
if !reported {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeMismatch,
|
||||
"shape method signature mismatch", f.name);
|
||||
}
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
@@ -315,7 +736,67 @@ fn shape_conforms(self: *Sema, shape_lang: usize, value_lang: usize): bool {
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
ret true;
|
||||
if ok && value_lang != 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) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
pi = pi + 1;
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
ret ok;
|
||||
}
|
||||
|
||||
fn for_item_hook(): fn(*any, usize, usize): usize {
|
||||
ret for_item;
|
||||
}
|
||||
|
||||
fn for_item(user: *any, value_lang: usize, shape_lang: usize): usize {
|
||||
var self = user as *Sema;
|
||||
var binds = Vec.blank(@sizeof(ShapeBind), @alignof(ShapeBind));
|
||||
if !shape_marker_binds(self, value_lang, shape_lang, &binds) {
|
||||
Vec.drop(&binds);
|
||||
ret 0;
|
||||
}
|
||||
if !shape_assoc_names_bound(self, shape_lang, &binds) {
|
||||
Vec.drop(&binds);
|
||||
ret 0;
|
||||
}
|
||||
var ok = shape_conforms_with(self, shape_lang, value_lang, &binds);
|
||||
var item: usize = 0;
|
||||
if ok { item = shape_bind_find(&binds, "Item"); }
|
||||
Vec.drop(&binds);
|
||||
if !ok { ret 0; }
|
||||
if item == 0 {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeAssocUnbound,
|
||||
"iterator Item is not bound", "Item");
|
||||
}
|
||||
ret item;
|
||||
}
|
||||
|
||||
fn check_conforms(user: *any, agg_ref: Ref, value_lang: usize) {
|
||||
var self = user as *Sema;
|
||||
var n = Bld.node_ptr(&self.hir, agg_ref);
|
||||
if n == null { ret; }
|
||||
match n.kind {
|
||||
.AggType(a) => {
|
||||
// shape 继承在具体类型 conformance 时展开,shape 自身不按结构实现父 shape。
|
||||
if SemaM.agg_is_shape(a.kind) { ret; }
|
||||
var i: usize = 0;
|
||||
while i < a.conforms.len {
|
||||
if !shape_conforms(self, a.conforms[i], value_lang) { ret; }
|
||||
i = i + 1;
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
||||
fn shape_type_node(self: *Sema, lang: usize): usize {
|
||||
@@ -526,22 +1007,25 @@ fn prepare_call(user: *any, callee_ref: Ref, callee: Ref, args: []usize,
|
||||
var shape_args = Vec.blank(@sizeof(usize), @alignof(usize));
|
||||
defer { Vec.drop(&shape_args); }
|
||||
var shape_ok = true;
|
||||
var need_report = false;
|
||||
var pi: usize = 0;
|
||||
while pi < self.shape_state.params.len {
|
||||
var sp = (self.shape_state.params.data as *ShapeParam) + pi;
|
||||
if sp.fn_ref == callee_ref {
|
||||
if sp.param_index >= args.len {
|
||||
shape_ok = false;
|
||||
need_report = true;
|
||||
} else {
|
||||
var a = args[sp.param_index];
|
||||
var al = SemaM.tid_of(self, a);
|
||||
if al == 0 {
|
||||
shape_ok = false;
|
||||
} else if !shape_conforms(self, sp.shape_lang, al) {
|
||||
need_report = true;
|
||||
} else if !shape_conforms_use(self, sp.shape_ref, al) {
|
||||
shape_ok = false;
|
||||
} else {
|
||||
var sr = shape_adapter_result(self, pi + 1, al);
|
||||
if sr.lang == 0 || !shape_conforms(self, sp.shape_lang, sr.lang) {
|
||||
if sr.lang == 0 || !shape_conforms_use(self, sp.shape_ref, sr.lang) {
|
||||
shape_ok = false;
|
||||
} else {
|
||||
var boxed = insert_shape_box(self, a, sr.lang, sr.wrapper_sym,
|
||||
@@ -557,7 +1041,7 @@ fn prepare_call(user: *any, callee_ref: Ref, callee: Ref, args: []usize,
|
||||
}
|
||||
pi = pi + 1;
|
||||
}
|
||||
if !shape_ok {
|
||||
if !shape_ok && need_report {
|
||||
SemaM.sema_error_code(self, ErrorCode.ShapeMismatch, "shape argument does not conform", "");
|
||||
}
|
||||
out.ok = shape_ok;
|
||||
@@ -575,11 +1059,29 @@ fn prepare_call(user: *any, callee_ref: Ref, callee: Ref, args: []usize,
|
||||
}
|
||||
}
|
||||
|
||||
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; }
|
||||
var binds = Vec.blank(@sizeof(ShapeBind), @alignof(ShapeBind));
|
||||
defer { Vec.drop(&binds); }
|
||||
if !shape_marker_binds(self, value_lang, shape_lang, &binds) { ret 0; }
|
||||
if !shape_assoc_names_bound(self, shape_lang, &binds) { ret 0; }
|
||||
if !shape_conforms_with(self, shape_lang, value_lang, &binds) { ret 0; }
|
||||
ret SemaM.method_sym(self, value_lang, method);
|
||||
}
|
||||
|
||||
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 hooks(): ShapeHooks {
|
||||
ret ShapeHooks {
|
||||
.bind_params = bind_params,
|
||||
.type_bind_of = type_bind_of,
|
||||
.prepare_call = prepare_call,
|
||||
.check_conforms = check_conforms,
|
||||
.op_method = op_method,
|
||||
.has_conformance = marker_has_hook,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user