2041 lines
92 KiB
Plaintext
2041 lines
92 KiB
Plaintext
// Expect retcode = 0
|
||
|
||
// sema_expr: expression/statement analysis; state and base accessors stay in sema.spl.
|
||
type .{ .Vec } = @import("../stage2/utils/vec.spl");
|
||
type .{ .ByteView } = @import("../stage2/utils/byte_view.spl");
|
||
type .{ .ByteBuffer } = @import("../stage2/utils/byte_buffer.spl");
|
||
type LangType = @import("./lang_type.spl");
|
||
type .{ .Node, .Ref } = @import("./builtins/spl_ir.spl");
|
||
type .{ .ErrorCode } = @import("./builtins/error_code.spl");
|
||
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");
|
||
type SemaM = @import("./sema.spl");
|
||
type Sema = SemaM.Sema;
|
||
type .{
|
||
.ExprHooks, .IterInfo, .ShapeComp, .ShapeBind, .ContainerLang, .CtFnRef, .DeclKind, .DeclState,
|
||
.Binding, .FnSym, .TypeSym, .GlobalSym, .Decl, .Namespace, .Module, .ImportEdge, .Def,
|
||
.RefInfo, .CallEdge, .Interned, .Target, .GenBind, .GenBindRec, .GenInstance,
|
||
.ShapeParam, .ShapeInstance, .ShapeUse, .ShapeAssocType, .ShapeAdapterResult,
|
||
.LangConform, .ShapeAdapterEval, .ShapeTypeBind, .ShapeState, .ShapeCallResult,
|
||
.ShapeHooks, .HookParamResult, .HookHooks, .CtState
|
||
} = @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); }
|
||
|
||
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,
|
||
};
|
||
}
|
||
|
||
fn main() {}
|
||
|
||
// ---- 表达式 / 语句 ----
|
||
|
||
fn bind_pattern(self: *Sema, ref: Ref, lang: usize) {
|
||
if ref == 0 { ret; }
|
||
var n = Bld.node_ptr(&self.hir, ref);
|
||
if n == null { ret; }
|
||
match n.kind {
|
||
.PatWild => { SemaM.set_tid(self, ref, 0); },
|
||
.PatBind(x) => {
|
||
// 生成实例/匹配臂可能被重复分析;重复 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, true);
|
||
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); },
|
||
.PatTag(x) => {
|
||
var sl = lang;
|
||
if LangType.is_ptr(&self.types, sl) { sl = LangType.elem_of(&self.types, sl); }
|
||
var idx = LangType.member_idx(&self.types, sl, x.name);
|
||
var pl: usize = 0;
|
||
if idx >= 0 { pl = LangType.member_lang(&self.types, sl, idx as usize); }
|
||
if x.sub != 0 { bind_pattern(self, x.sub, pl); }
|
||
SemaM.set_tid(self, ref, sl);
|
||
},
|
||
.PatList(x) => {
|
||
if LangType.is_struct(&self.types, lang) {
|
||
var i: usize = 0;
|
||
while i < x.len {
|
||
bind_pattern(self, x[i],
|
||
LangType.member_lang(&self.types, lang, i));
|
||
i = i + 1;
|
||
}
|
||
} else {
|
||
var i: usize = 0;
|
||
while i < x.len {
|
||
bind_pattern(self, x[i], lang);
|
||
i = i + 1;
|
||
}
|
||
}
|
||
SemaM.set_tid(self, ref, lang);
|
||
},
|
||
.PatTuple(x) => {
|
||
var i: usize = 0;
|
||
while i < x.len {
|
||
bind_pattern(self, x[i],
|
||
LangType.member_lang(&self.types, lang, i));
|
||
i = i + 1;
|
||
}
|
||
SemaM.set_tid(self, ref, lang);
|
||
},
|
||
.PatField(x) => {
|
||
var sl = lang;
|
||
if LangType.is_ptr(&self.types, sl) { sl = LangType.elem_of(&self.types, sl); }
|
||
var idx = LangType.member_idx(&self.types, sl, x.name);
|
||
var fl: usize = 0;
|
||
if idx >= 0 { fl = LangType.member_lang(&self.types, sl, idx as usize); }
|
||
if x.sub != 0 { bind_pattern(self, x.sub, fl); }
|
||
SemaM.set_tid(self, ref, sl);
|
||
},
|
||
.PatFields(xs) => {
|
||
var sl = lang;
|
||
if LangType.is_ptr(&self.types, sl) { sl = LangType.elem_of(&self.types, sl); }
|
||
var i: usize = 0;
|
||
while i < xs.len {
|
||
var fnode = Bld.node_ptr(&self.hir, xs[i]);
|
||
if fnode != null {
|
||
match fnode.kind {
|
||
.PatField(pf) => {
|
||
var idx = LangType.member_idx(&self.types, sl, pf.name);
|
||
var fl: usize = 0;
|
||
if idx >= 0 { fl = LangType.member_lang(&self.types, sl, idx as usize); }
|
||
if pf.sub != 0 { bind_pattern(self, pf.sub, fl); }
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
i = i + 1;
|
||
}
|
||
SemaM.set_tid(self, ref, sl);
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
|
||
fn build_op_call(self: *Sema, sym: usize, args: []Ref, ret_tid: usize): usize {
|
||
var s = SemaM.sym_at(self, sym - 1);
|
||
var callee = Bld.construct(&self.hir, .{ .kind = .FnRef(s.ref) });
|
||
SemaM.grow_tables(self);
|
||
SemaM.set_node_fn(self, callee, sym);
|
||
SemaM.set_tid(self, callee, s.fn_tid);
|
||
var call = Bld.construct(&self.hir, .{ .kind = .Call(.{
|
||
.is_comptime = false,
|
||
.callee = callee,
|
||
.args = args,
|
||
}) });
|
||
SemaM.grow_tables(self);
|
||
SemaM.set_tid(self, call, ret_tid);
|
||
SemaM.record_call(self, sym);
|
||
ret call;
|
||
}
|
||
|
||
type OpImpl = enum { Native: usize, User: usize, Missing };
|
||
|
||
fn op_resolve(self: *Sema, lt: usize, op: SemaOp): usize {
|
||
var sl = SemaOpM.shape_lang(self, op);
|
||
if sl == 0 { ret 0; }
|
||
if self.shape_hooks.op_method == null { ret 0; }
|
||
ret self.shape_hooks.op_method(self as *any, lt, sl, SemaOpM.method_name(op));
|
||
}
|
||
|
||
fn resolve_op(self: *Sema, op: SemaOp, lt: usize): OpImpl {
|
||
if @enum2int(op) == @enum2int(SemaOp.None) { ret .Missing; }
|
||
SemaM.ensure_lang_of(self, lt);
|
||
var kind = LangType.kind_of(&self.types, lt);
|
||
if @enum2int(kind) == @enum2int(LangKind.Type) ||
|
||
@enum2int(kind) == @enum2int(LangKind.Assoc) {
|
||
ret .Native(0);
|
||
}
|
||
// primitive 的操作由编译器声明,直接取 #op 方法,跳过用户级 conformance。
|
||
var repr = LangType.repr_of(&self.types, lt);
|
||
if @enum2int(repr) != @enum2int(ReprKind.None) {
|
||
var msym = SemaM.method_sym(self, lt, SemaOpM.method_name(op));
|
||
if msym == 0 { ret .Missing; }
|
||
ret .User(msym);
|
||
}
|
||
var sym = op_resolve(self, lt, op);
|
||
if sym == 0 { ret .Missing; }
|
||
ret .User(sym);
|
||
}
|
||
|
||
|
||
|
||
fn range_lang(self: *Sema): usize {
|
||
var name = "Range";
|
||
var callee = Bld.construct(&self.hir, .{ .kind = .Global(name) });
|
||
SemaM.grow_tables(self);
|
||
var call_ref = Bld.construct(&self.hir, .{ .kind = .Call(.{
|
||
.is_comptime = true,
|
||
.callee = callee,
|
||
.args = SemaM.empty_list(self),
|
||
}) });
|
||
SemaM.grow_tables(self);
|
||
var cn = Bld.node_ptr(&self.hir, call_ref);
|
||
if cn == null { ret 0; }
|
||
match cn.kind {
|
||
.Call(c) => { ret SemaM.type_gen_lang(self, 0, c, 0); },
|
||
_ => {},
|
||
}
|
||
ret 0;
|
||
}
|
||
|
||
fn dispatch_call_value(self: *Sema, ref: Ref, callee: Ref, args: []Ref, ct: usize): bool {
|
||
var call_shape = SemaOpM.call_shape_lang(self);
|
||
if call_shape == 0 || self.shape_hooks.has_conformance == null { ret false; }
|
||
if !self.shape_hooks.has_conformance(self as *any, ct, call_shape) { ret false; }
|
||
if self.shape_hooks.op_method == null { ret false; }
|
||
var sym = self.shape_hooks.op_method(self as *any, ct, call_shape, "call");
|
||
if sym == 0 { ret false; }
|
||
// 参数 sema 可能追加 FnSym 并让 fn_syms 重分配,先保存返回类型。
|
||
var s = SemaM.sym_at(self, sym - 1);
|
||
var ret_tid = s.ret_tid;
|
||
if LangType.fn_param_count(&self.types, s.fn_tid) < 2 { ret false; }
|
||
var args_t = LangType.fn_param_of(&self.types, s.fn_tid, 1);
|
||
if args_t == 0 { ret false; }
|
||
var arity = LangType.member_count(&self.types, args_t);
|
||
if args.len != arity {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.ArgCount, "callable argument count mismatch", "");
|
||
SemaM.set_tid(self, ref, s.ret_tid);
|
||
ret true;
|
||
}
|
||
var av = Vec.blank(@sizeof(Ref), @alignof(Ref));
|
||
var i: usize = 0;
|
||
while i < args.len {
|
||
var aw = LangType.member_lang(&self.types, args_t, i);
|
||
sema_expr(self, args[i], aw);
|
||
var ar = args[i];
|
||
match Vec.push(&av, &ar as *u8) { .Ok => {}, .Err(error) => {} }
|
||
i = i + 1;
|
||
}
|
||
var tuple_ref = Bld.construct(&self.hir, .{ .kind = .TupleLit((av.data as *Ref)[0..av.len]) });
|
||
SemaM.grow_tables(self);
|
||
SemaM.set_tid(self, tuple_ref, args_t);
|
||
Vec.drop(&av);
|
||
var cav: [2]Ref = .[callee, tuple_ref];
|
||
var call_args = Bld.dup_refs(&self.hir, cav[0..2]);
|
||
var call = build_op_call(self, sym, call_args, ret_tid);
|
||
var cn = Bld.node_ptr(&self.hir, call);
|
||
var np = Bld.node_ptr(&self.hir, ref);
|
||
if cn != null && np != null {
|
||
match cn.kind {
|
||
.Call(cc) => { np.kind = .Call(cc); },
|
||
_ => {},
|
||
}
|
||
}
|
||
SemaM.set_tid(self, ref, ret_tid);
|
||
ret true;
|
||
}
|
||
|
||
// 类型值调用按值处理:variant 字面量仍走 AggLit,其余同类型表达式用 Block 转发。
|
||
fn rewrite_construct_value(self: *Sema, ref: Ref, value: Ref, target: usize) {
|
||
var np = Bld.node_ptr(&self.hir, ref);
|
||
var vn = Bld.node_ptr(&self.hir, value);
|
||
if np == null || vn == null { ret; }
|
||
match vn.kind {
|
||
.AggLit(a) => { np.kind = .AggLit(a); },
|
||
_ => {
|
||
var empty = SemaM.empty_list(self);
|
||
np.kind = .Block(.{ .stmts = empty, .tail = value });
|
||
},
|
||
}
|
||
SemaM.set_tid(self, ref, target);
|
||
}
|
||
|
||
fn construct_type_value(self: *Sema, ref: Ref, callee: Ref, args: []Ref, known: usize): bool {
|
||
var tl = known;
|
||
var cn = Bld.node_ptr(&self.hir, callee);
|
||
if tl == 0 && cn != null {
|
||
match cn.kind {
|
||
// 表达式位置的类型生成器调用要先拿缓存实例,否则 VM 会造出一个
|
||
// 没有方法表的新匿名类型。
|
||
.Call(c) => {
|
||
tl = SemaM.type_gen_lang(self, callee, c, 0);
|
||
if tl != 0 { SemaM.set_tid(self, callee, tl); }
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
if tl == 0 { tl = SemaM.type_value_lang(self, callee); }
|
||
if tl == 0 { ret false; }
|
||
if args.len != 1 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.ArgCount,
|
||
"type constructor expects one argument", "");
|
||
SemaM.set_tid(self, ref, tl);
|
||
ret true;
|
||
}
|
||
sema_expr(self, args[0], tl);
|
||
SemaM.require_type(self, args[0], tl, "constructor");
|
||
rewrite_construct_value(self, ref, args[0], tl);
|
||
ret true;
|
||
}
|
||
|
||
fn make_variant_agg(self: *Sema, target: usize, idx: usize, payload: Ref): Node.Kind {
|
||
var name = LangType.member_name(&self.types, target, idx);
|
||
var fl = Bld.construct(&self.hir, .{ .kind = .FieldLit(.{ .name = name, .value = payload }) });
|
||
SemaM.grow_tables(self);
|
||
var one: [1]Ref = .[fl];
|
||
var fields = Bld.dup_refs(&self.hir, one[0..1]);
|
||
ret .AggLit(.{ .name = "", .fields = fields });
|
||
}
|
||
|
||
fn construct_variant_call(self: *Sema, ref: Ref, callee: Ref, args: []Ref): bool {
|
||
var target = SemaM.tid_of(self, callee);
|
||
var variant = SemaM.node_variant(self, callee);
|
||
if target == 0 || variant == 0 { ret false; }
|
||
var idx = variant - 1;
|
||
var payload_t = LangType.member_lang(&self.types, target, idx);
|
||
var is_unit = payload_t == 0 ||
|
||
@enum2int(LangType.kind_of(&self.types, payload_t)) == @enum2int(LangKind.Unit);
|
||
var payload: Ref = 0;
|
||
if args.len == 0 {
|
||
if !is_unit {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.ArgCount,
|
||
"variant payload missing", "");
|
||
SemaM.set_tid(self, ref, target);
|
||
ret true;
|
||
}
|
||
} else if args.len == 1 {
|
||
sema_expr(self, args[0], payload_t);
|
||
if payload_t != 0 { SemaM.require_type(self, args[0], payload_t, "variant payload"); }
|
||
payload = args[0];
|
||
} else {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.ArgCount,
|
||
"variant payload count mismatch", "");
|
||
SemaM.set_tid(self, ref, target);
|
||
ret true;
|
||
}
|
||
var np = Bld.node_ptr(&self.hir, ref);
|
||
if np != null { np.kind = make_variant_agg(self, target, idx, payload); }
|
||
SemaM.set_tid(self, ref, target);
|
||
ret true;
|
||
}
|
||
|
||
fn dispatch_user_binary(self: *Sema, ref: Ref, x: Node.Kind.Binary, lt: usize, sym: usize) {
|
||
// FnSym 指针不能跨 sema_expr 保存:递归分析可能追加符号并让 fn_syms 重分配。
|
||
var s = SemaM.sym_at(self, sym - 1);
|
||
var fn_tid = s.fn_tid;
|
||
var ret_tid = s.ret_tid;
|
||
var rhs_want = lt;
|
||
if LangType.fn_param_count(&self.types, fn_tid) > 1 {
|
||
rhs_want = LangType.fn_param_of(&self.types, fn_tid, 1);
|
||
}
|
||
sema_expr(self, x.rval, rhs_want);
|
||
if rhs_want != 0 { SemaM.require_type(self, x.rval, rhs_want, "operand"); }
|
||
|
||
var out_tid = ret_tid;
|
||
var av: [2]Ref = .[x.lval, x.rval];
|
||
var args = Bld.dup_refs(&self.hir, av[0..2]);
|
||
var call = build_op_call(self, sym, args, out_tid);
|
||
|
||
match x.op {
|
||
.NotEqual => {
|
||
var np = Bld.node_ptr(&self.hir, ref);
|
||
if np != null {
|
||
np.kind = .Unary(.{ .op = .LogicNot, .val = call });
|
||
}
|
||
SemaM.set_tid(self, ref, LangType.make_bool(&self.types));
|
||
},
|
||
.LessThan, .LessEqual, .GreaterThan, .GreaterEqual => {
|
||
var ord = out_tid;
|
||
var tag = LangType.enum_tag(&self.types, ord);
|
||
if tag == 0 { tag = LangType.make_int(&self.types, 32, true); }
|
||
var ea: [1]Ref = .[call];
|
||
var eargs = Bld.dup_refs(&self.hir, ea[0..1]);
|
||
var e2i = Bld.construct(&self.hir, .{ .kind = .Call(.{
|
||
.is_comptime = false,
|
||
.callee = 0,
|
||
.args = eargs,
|
||
}) });
|
||
SemaM.grow_tables(self);
|
||
SemaM.set_node_builtin(self, e2i, @enum2int(Builtin.Enum2Int) as usize);
|
||
SemaM.set_tid(self, e2i, tag);
|
||
var less = LangType.member_idx(&self.types, ord, "Less");
|
||
var equal = LangType.member_idx(&self.types, ord, "Equal");
|
||
if less < 0 { less = 0; }
|
||
if equal < 0 { equal = 1; }
|
||
var cmp_op: Node.Kind.Op = .LessThan;
|
||
var threshold: usize = 0;
|
||
match x.op {
|
||
.LessThan => { cmp_op = .LessThan; threshold = less as usize + 1; },
|
||
.LessEqual => { cmp_op = .LessThan; threshold = equal as usize + 1; },
|
||
.GreaterThan => { cmp_op = .GreaterThan; threshold = equal as usize; },
|
||
.GreaterEqual => { cmp_op = .GreaterThan; threshold = less as usize; },
|
||
_ => {},
|
||
}
|
||
var lit = Bld.construct(&self.hir, .{ .kind = .LitInt(SemaM.itext(self, threshold)) });
|
||
SemaM.grow_tables(self);
|
||
SemaM.set_tid(self, lit, tag);
|
||
var np = Bld.node_ptr(&self.hir, ref);
|
||
if np != null {
|
||
np.kind = .Binary(.{ .op = cmp_op, .lval = e2i, .rval = lit });
|
||
}
|
||
SemaM.set_tid(self, ref, LangType.make_bool(&self.types));
|
||
},
|
||
_ => {
|
||
var cn = Bld.node_ptr(&self.hir, call);
|
||
var np = Bld.node_ptr(&self.hir, ref);
|
||
if cn != null && np != null {
|
||
match cn.kind {
|
||
.Call(cc) => { np.kind = .Call(cc); },
|
||
_ => {},
|
||
}
|
||
}
|
||
SemaM.set_tid(self, ref, out_tid);
|
||
},
|
||
}
|
||
}
|
||
|
||
fn dispatch_user_unary(self: *Sema, ref: Ref, x: Node.Kind.Unary, vt: usize, sym: usize) {
|
||
var s = SemaM.sym_at(self, sym - 1);
|
||
var ret_tid = s.ret_tid;
|
||
var av: [1]Ref = .[x.val];
|
||
var args = Bld.dup_refs(&self.hir, av[0..1]);
|
||
var call = build_op_call(self, sym, args, ret_tid);
|
||
var cn = Bld.node_ptr(&self.hir, call);
|
||
var np = Bld.node_ptr(&self.hir, ref);
|
||
if cn != null && np != null {
|
||
match cn.kind {
|
||
.Call(cc) => { np.kind = .Call(cc); },
|
||
_ => {},
|
||
}
|
||
}
|
||
SemaM.set_tid(self, ref, ret_tid);
|
||
}
|
||
|
||
fn dispatch_user_index(self: *Sema, ref: Ref, x: Node.Kind.Index, ol: usize): bool {
|
||
var sl = SemaOpM.shape_lang(self, .Index);
|
||
if sl == 0 || self.shape_hooks.op_method == null { ret false; }
|
||
var sym = self.shape_hooks.op_method(self as *any, ol, sl, "index");
|
||
if sym == 0 { ret false; }
|
||
// FnSym 指针不能跨 sema_expr 保存;先取出类型再递归分析下标。
|
||
var s = SemaM.sym_at(self, sym - 1);
|
||
var fn_tid = s.fn_tid;
|
||
var ptr_ty = s.ret_tid;
|
||
if !LangType.is_ptr(&self.types, ptr_ty) { ret false; }
|
||
var item = LangType.elem_of(&self.types, ptr_ty);
|
||
var idx_want = LangType.usize_lang(&self.types);
|
||
if LangType.fn_param_count(&self.types, fn_tid) > 1 {
|
||
idx_want = LangType.fn_param_of(&self.types, fn_tid, 1);
|
||
}
|
||
sema_expr(self, x.idx, idx_want);
|
||
if idx_want != 0 { SemaM.require_type(self, x.idx, idx_want, "index"); }
|
||
var recv_ty = ol;
|
||
if LangType.fn_param_count(&self.types, fn_tid) > 0 {
|
||
recv_ty = LangType.fn_param_of(&self.types, fn_tid, 0);
|
||
}
|
||
var addr = Bld.construct(&self.hir, .{ .kind = .Addr(x.obj) });
|
||
SemaM.grow_tables(self);
|
||
SemaM.set_tid(self, addr, recv_ty);
|
||
var av: [2]Ref = .[addr, x.idx];
|
||
var args = Bld.dup_refs(&self.hir, av[0..2]);
|
||
var call = build_op_call(self, sym, args, ptr_ty);
|
||
var dref = Bld.construct(&self.hir, .{ .kind = .Deref(call) });
|
||
SemaM.grow_tables(self);
|
||
SemaM.set_tid(self, dref, item);
|
||
var np = Bld.node_ptr(&self.hir, ref);
|
||
if np != null { np.kind = .Deref(call); }
|
||
SemaM.set_tid(self, ref, item);
|
||
ret true;
|
||
}
|
||
|
||
fn iter_capture_members(self: *Sema, ref: Ref): Vec {
|
||
var v = Vec.blank(@sizeof(usize), @alignof(usize));
|
||
var n = Bld.node_ptr(&self.hir, ref);
|
||
if n == null { ret v; }
|
||
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, "capture_members") {
|
||
var j: usize = 0;
|
||
while j < x.args.len {
|
||
var mv = SemaM.lit_u64(self, x.args[j]) as usize;
|
||
match Vec.push(&v, &mv as *u8) { .Ok => {}, .Err(error) => {} }
|
||
j = j + 1;
|
||
}
|
||
ret v;
|
||
}
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
i = i + 1;
|
||
}
|
||
ret v;
|
||
}
|
||
|
||
fn bind_iter_captures(self: *Sema, captures: []Ref, members: []Ref, pattern_type: Ref, item: usize) {
|
||
if pattern_type != 0 {
|
||
var pt = SemaM.resolve_type(self, pattern_type);
|
||
if pt != item {
|
||
SemaM.sema_error_code_at(self, pattern_type, ErrorCode.TypeMismatch,
|
||
"for pattern type mismatch", "");
|
||
}
|
||
}
|
||
var i: usize = 0;
|
||
while i < captures.len {
|
||
var cap = captures[i];
|
||
var ct = item;
|
||
if i < members.len {
|
||
var m = members[i];
|
||
if m > 0 { ct = LangType.member_lang(&self.types, item, m - 1); }
|
||
}
|
||
var d = SemaM.node_def(self, cap);
|
||
if d == 0 {
|
||
d = SemaM.new_def(self, ct, true);
|
||
SemaM.set_def(self, cap, d);
|
||
} else {
|
||
SemaM.set_def_tid(self, d, ct);
|
||
}
|
||
SemaM.set_tid(self, cap, ct);
|
||
i = i + 1;
|
||
}
|
||
}
|
||
|
||
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 "";
|
||
}
|
||
|
||
// 把一个已解析的声明绑到表达式节点:`Global` 与 `Type.member` 共用。
|
||
// Fn/Type/Var/Const 的绑定规则只在这里维护,避免两处漂移。
|
||
fn bind_decl_ref(self: *Sema, ref: Ref, di: usize, want: usize) {
|
||
var kind = SemaM.decl_at(self, di).kind;
|
||
SemaM.ensure_decl(self, di);
|
||
if @enum2int(kind) == @enum2int(DeclKind.Fn) {
|
||
SemaM.set_node_fn(self, ref, SemaM.decl_at(self, di).sym);
|
||
SemaM.set_tid(self, ref, SemaM.decl_at(self, di).lang);
|
||
SemaM.record_call(self, SemaM.decl_at(self, di).sym);
|
||
ret;
|
||
}
|
||
if @enum2int(kind) == @enum2int(DeclKind.Type) {
|
||
if self.ct.building || (want != 0 && LangType.is_type(&self.types, want)) {
|
||
// comptime 里类型名是类型值:物化为 ct_handle,供 == 和返回聚合使用。
|
||
var tl = SemaM.decl_at(self, di).lang;
|
||
var dn = Bld.node_ptr(&self.hir, SemaM.decl_at(self, di).node);
|
||
if dn != null {
|
||
match dn.kind {
|
||
.TypeDecl(td) => {
|
||
var rl = SemaM.resolve_type(self, td.ty);
|
||
if rl != 0 { tl = rl; }
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
var h = SemaM.ct_handle(self, tl);
|
||
Bld.node_ptr(&self.hir, ref).kind = .LitInt(SemaM.itext(self, h));
|
||
SemaM.set_tid(self, ref, LangType.make_type(&self.types));
|
||
} else {
|
||
SemaM.set_tid(self, ref, SemaM.decl_at(self, di).lang);
|
||
}
|
||
ret;
|
||
}
|
||
if SemaM.decl_at(self, di).init != 0 {
|
||
SemaM.set_node_initref(self, ref, SemaM.decl_at(self, di).init);
|
||
} else {
|
||
SemaM.set_def(self, ref, SemaM.decl_at(self, di).def);
|
||
}
|
||
SemaM.set_tid(self, ref, SemaM.decl_at(self, di).lang);
|
||
}
|
||
|
||
fn sema_expr(self: *Sema, ref: Ref, want: usize) {
|
||
if ref == 0 { ret; }
|
||
var n = Bld.node_ptr(&self.hir, ref);
|
||
if n == null { ret; }
|
||
var old_tid = SemaM.tid_of(self, ref);
|
||
if old_tid != 0 {
|
||
if want != 0 && want != old_tid {
|
||
// 宏实参可能已按默认 i32 定型;null 也可能先定成 Null 再按比较对象定型。
|
||
if LangType.is_null(&self.types, old_tid) && LangType.accepts_null(&self.types, want) {
|
||
SemaM.set_tid(self, ref, want);
|
||
} else if LangType.is_int(&self.types, want) {
|
||
var relit = false;
|
||
match n.kind {
|
||
.LitInt(_) => { relit = true; },
|
||
.LitChar(_) => { relit = true; },
|
||
_ => {},
|
||
}
|
||
if relit { SemaM.set_tid(self, ref, want); }
|
||
}
|
||
}
|
||
ret;
|
||
}
|
||
match n.kind {
|
||
.AggType(_) => {
|
||
// 表达式位置的聚合类型(如 comptime 体内 `struct{...}`):求值为类型值句柄。
|
||
var lang = SemaM.resolve_type(self, ref);
|
||
if lang != 0 {
|
||
SemaM.set_tid(self, ref, LangType.make_type(&self.types));
|
||
SemaM.set_node_aggval(self, ref, SemaM.ct_handle(self, lang));
|
||
} else {
|
||
SemaM.set_tid(self, ref, want);
|
||
}
|
||
},
|
||
.FnRef(x) => {
|
||
var sym = SemaM.fn_sym_for_ref(self, x);
|
||
if sym == 0 { SemaM.set_tid(self, ref, want); ret; }
|
||
var s = SemaM.sym_at(self, sym - 1);
|
||
SemaM.set_node_fn(self, ref, sym);
|
||
SemaM.set_tid(self, ref, s.fn_tid);
|
||
SemaM.record_call(self, sym);
|
||
},
|
||
.Local(x) => {
|
||
var d = SemaM.node_def(self, x);
|
||
SemaM.set_def(self, ref, d);
|
||
var t = SemaM.def_tid(self, d);
|
||
if t == 0 { t = want; }
|
||
SemaM.set_tid(self, ref, t);
|
||
},
|
||
.Global(x) => {
|
||
var d = SemaM.find_decl(self, x);
|
||
if d == 0 { d = SemaM.find_path_decl(self, x); }
|
||
if d == 0 {
|
||
if SemaM.builtin_tid(self, x) == 0 && SemaM.lookup_type(self, x) == 0 {
|
||
if SemaM.current_fn_is_lambda(self) {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.LambdaCapture,
|
||
"lambda cannot capture or resolve identifier", x);
|
||
} else {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.UndefinedIdentifier, "undefined identifier", x);
|
||
}
|
||
}
|
||
SemaM.set_tid(self, ref, want);
|
||
ret;
|
||
}
|
||
bind_decl_ref(self, ref, d - 1, want);
|
||
},
|
||
.Import(_) => {
|
||
var tgt = SemaM.import_target(self, self.cur_mod, ref);
|
||
if tgt == 0 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.ImportFailed, "cannot import module", "");
|
||
SemaM.set_tid(self, ref, 0);
|
||
ret;
|
||
}
|
||
var tm = tgt - 1;
|
||
SemaM.ensure_module(self, tm);
|
||
SemaM.set_tid(self, ref, SemaM.module_at(self, tm).root_lang);
|
||
},
|
||
.Comptime(x) => {
|
||
var saved_fn = self.cur_fn;
|
||
self.cur_fn = SemaM.ct_root_of(ref);
|
||
sema_expr(self, x, want);
|
||
self.cur_fn = saved_fn;
|
||
var rl = SemaM.tid_of(self, x);
|
||
if !self.ct.building && self.ct.eval != null && rl != 0 {
|
||
self.ct.eval(self as *any, ref);
|
||
}
|
||
if SemaM.tid_of(self, ref) == 0 { SemaM.set_tid(self, ref, rl); }
|
||
},
|
||
.LitInt(_) => {
|
||
// 只对数值目标做上下文化;`f(true)` / `if 1` 不能被 want 改写成合法类型。
|
||
if !LangType.is_int(&self.types, want) && !LangType.is_float(&self.types, want) {
|
||
want = LangType.make_int(&self.types, 32, true);
|
||
}
|
||
SemaM.set_tid(self, ref, want);
|
||
},
|
||
.LitChar(_) => {
|
||
// 字符默认 u8;与整型上下文交互时沿用 LitInt 的重定型规则。
|
||
if !LangType.is_int(&self.types, want) {
|
||
want = LangType.make_int(&self.types, 8, false);
|
||
}
|
||
SemaM.set_tid(self, ref, want);
|
||
},
|
||
.LitFloat(_) => {
|
||
if !LangType.is_float(&self.types, want) {
|
||
want = LangType.make_float(&self.types, 64);
|
||
}
|
||
SemaM.set_tid(self, ref, want);
|
||
},
|
||
.LitBool(_) => {
|
||
SemaM.set_tid(self, ref, LangType.make_bool(&self.types));
|
||
},
|
||
.Undefined => {
|
||
if want == 0 { want = LangType.make_null(&self.types); }
|
||
SemaM.set_tid(self, ref, want);
|
||
},
|
||
.LitStr(_) => {
|
||
var u8l = LangType.make_int(&self.types, 8, false);
|
||
SemaM.set_tid(self, ref, LangType.make_slice(&self.types, u8l));
|
||
},
|
||
.Field(x) => {
|
||
var ol: usize = 0;
|
||
var obj_is_type = false;
|
||
var on = Bld.node_ptr(&self.hir, x.obj);
|
||
if on != null {
|
||
match on.kind {
|
||
.Call(c) => {
|
||
// 直接类型生成器调用:`@vec(i8).with_capacity(...)`。
|
||
// 先按类型求解,避免把它当普通 comptime 值执行。
|
||
ol = SemaM.type_gen_lang(self, x.obj, c, 0);
|
||
if ol != 0 {
|
||
SemaM.set_tid(self, x.obj, ol);
|
||
obj_is_type = true;
|
||
}
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
if ol == 0 {
|
||
sema_expr(self, x.obj, 0);
|
||
ol = SemaM.tid_of(self, x.obj);
|
||
}
|
||
// `@vec(i8).with_capacity(...)`:obj 承载 type 值,先解包到实际 LangType,
|
||
// 否则只能看到 Type 类型本身,查不到匿名 TypeDecl 的关联函数。
|
||
if LangType.is_type(&self.types, ol) {
|
||
var tl = SemaM.type_value_lang(self, x.obj);
|
||
if tl != 0 {
|
||
ol = tl;
|
||
obj_is_type = true;
|
||
}
|
||
}
|
||
var sl = ol;
|
||
if LangType.is_ptr(&self.types, sl) { sl = LangType.elem_of(&self.types, sl); }
|
||
SemaM.ensure_lang_of(self, sl);
|
||
if LangType.is_agg(&self.types, sl) || LangType.is_slice(&self.types, sl) ||
|
||
LangType.is_array(&self.types, sl) ||
|
||
LangType.container_of(&self.types, sl) != 0 {
|
||
var idx = LangType.member_idx(&self.types, sl, x.name);
|
||
if idx >= 0 {
|
||
if LangType.is_enum(&self.types, sl) &&
|
||
(obj_is_type || SemaM.is_type_ref(self, x.obj)) {
|
||
// `Type.Variant`:枚举值(变体字面量)。
|
||
SemaM.set_node_variant(self, ref, (idx as usize) + 1);
|
||
SemaM.set_tid(self, ref, sl);
|
||
ret;
|
||
}
|
||
SemaM.set_tid(self, ref, LangType.member_lang(&self.types, sl, idx as usize));
|
||
ret;
|
||
}
|
||
if (LangType.is_agg(&self.types, sl) ||
|
||
LangType.container_of(&self.types, sl) != 0) && self.in_callee {
|
||
var msym = SemaM.method_sym(self, sl, x.name);
|
||
if msym != 0 {
|
||
var ms = SemaM.sym_at(self, msym - 1);
|
||
SemaM.set_node_fn(self, ref, msym);
|
||
SemaM.set_tid(self, ref, ms.fn_tid);
|
||
SemaM.record_call(self, msym);
|
||
ret;
|
||
}
|
||
}
|
||
}
|
||
// `Type.member`:容器命名空间里的 var/const/fn/type(Zig container-level)。
|
||
if sl != 0 && (obj_is_type || SemaM.is_type_ref(self, x.obj)) {
|
||
var cmod = LangType.container_mod_of(&self.types, sl);
|
||
var cont = LangType.container_of(&self.types, sl);
|
||
var d = SemaM.lookup_decl_in_mod(self, cmod, cont, x.name);
|
||
if d != 0 { bind_decl_ref(self, ref, d - 1, want); ret; }
|
||
}
|
||
if ol != 0 && !LangType.is_type(&self.types, ol) &&
|
||
!LangType.kind_eq(LangType.kind_of(&self.types, ol), .Any) {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.NoField, "no such field", x.name);
|
||
}
|
||
SemaM.set_tid(self, ref, want);
|
||
},
|
||
.AggLit(x) => {
|
||
var sl: usize = 0;
|
||
// `@self { ... }`:以当前聚合匿名类型构造。
|
||
if SemaM.eq(x.name, "self") {
|
||
sl = self.self_lang;
|
||
if sl == 0 { sl = self.current_agg; }
|
||
}
|
||
if sl == 0 && x.name.len != 0 { sl = SemaM.lookup_type(self, x.name); }
|
||
if sl == 0 && want != 0 && LangType.is_agg(&self.types, want) { sl = want; }
|
||
if sl == 0 && want != 0 && LangType.is_slice(&self.types, want) {
|
||
// 切片字面量:字段名到表示槽位由 LangType 的表示层解析。
|
||
var i2: usize = 0;
|
||
while i2 < x.fields.len {
|
||
var fr2 = x.fields[i2];
|
||
var fn3 = Bld.node_ptr(&self.hir, fr2);
|
||
if fn3 != null {
|
||
match fn3.kind {
|
||
.FieldLit(y) => {
|
||
var idx = LangType.repr_field_slot(&self.types, want, y.name);
|
||
if idx >= 0 {
|
||
var ml = LangType.member_lang(&self.types, want, idx as usize);
|
||
sema_expr(self, y.value, ml);
|
||
}
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
i2 = i2 + 1;
|
||
}
|
||
SemaM.set_tid(self, ref, want);
|
||
ret;
|
||
}
|
||
var is_enum = sl != 0 && LangType.is_enum(&self.types, sl);
|
||
if is_enum && x.fields.len != 1 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.NoField,
|
||
"enum literal must have exactly one variant", "");
|
||
}
|
||
var i: usize = 0;
|
||
while i < x.fields.len {
|
||
var fr = x.fields[i];
|
||
var fnode2 = Bld.node_ptr(&self.hir, fr);
|
||
if fnode2 != null {
|
||
match fnode2.kind {
|
||
.FieldLit(y) => {
|
||
var ml: usize = 0;
|
||
var idx: isize = -1;
|
||
if sl != 0 { idx = LangType.member_idx(&self.types, sl, y.name); }
|
||
if sl != 0 && idx < 0 {
|
||
SemaM.sema_error_code_at(self, fr, ErrorCode.NoField,
|
||
"no such field", y.name);
|
||
} else if idx >= 0 {
|
||
ml = LangType.member_lang(&self.types, sl, idx as usize);
|
||
if y.value == 0 {
|
||
if is_enum && ml != 0 &&
|
||
@enum2int(LangType.kind_of(&self.types, ml)) != @enum2int(LangKind.Unit) {
|
||
SemaM.sema_error_code_at(self, fr, ErrorCode.TypeMismatch,
|
||
"variant payload missing", "");
|
||
}
|
||
} else {
|
||
sema_expr(self, y.value, ml);
|
||
var what: []u8 = "field";
|
||
if is_enum { what = "variant payload"; }
|
||
if ml != 0 { SemaM.require_type(self, y.value, ml, what); }
|
||
}
|
||
} else {
|
||
sema_expr(self, y.value, ml);
|
||
}
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
i = i + 1;
|
||
}
|
||
SemaM.set_tid(self, ref, sl);
|
||
},
|
||
.Index(x) => {
|
||
sema_expr(self, x.obj, 0);
|
||
var ol = SemaM.tid_of(self, x.obj);
|
||
// 数组/切片索引保持内建 fast path;Index conformance 只服务用户类型。
|
||
if !LangType.is_array(&self.types, ol) && !LangType.is_slice(&self.types, ol) &&
|
||
dispatch_user_index(self, ref, x, ol) { ret; }
|
||
if LangType.is_struct(&self.types, ol) {
|
||
var iv = SemaM.lit_u64(self, x.idx) as usize;
|
||
sema_expr(self, x.idx, LangType.usize_lang(&self.types));
|
||
SemaM.set_tid(self, ref, LangType.member_lang(&self.types, ol, iv));
|
||
ret;
|
||
}
|
||
var el = LangType.elem_of(&self.types, ol);
|
||
sema_expr(self, x.idx, LangType.usize_lang(&self.types));
|
||
SemaM.set_tid(self, ref, el);
|
||
},
|
||
.TupleLit(x) => {
|
||
var sl: usize = 0;
|
||
if want != 0 && LangType.is_struct(&self.types, want) &&
|
||
LangType.member_count(&self.types, want) == x.len {
|
||
sl = want;
|
||
} else {
|
||
var members = Vec.blank(@sizeof(StructMember), @alignof(StructMember));
|
||
var k: usize = 0;
|
||
while k < x.len {
|
||
var er = x[k];
|
||
var el: usize = 0;
|
||
var tl: usize = 0;
|
||
if SemaM.is_type_ref(self, er) {
|
||
tl = SemaM.type_arg_lang(self, er);
|
||
} else {
|
||
var en = Bld.node_ptr(&self.hir, er);
|
||
if en != null {
|
||
match en.kind {
|
||
.Call(_) => { tl = SemaM.resolve_type(self, er); },
|
||
_ => {},
|
||
}
|
||
}
|
||
}
|
||
if tl != 0 {
|
||
var h = SemaM.ct_handle(self, tl);
|
||
Bld.node_ptr(&self.hir, er).kind = .LitInt(SemaM.itext(self, h));
|
||
SemaM.set_tid(self, er, LangType.make_type(&self.types));
|
||
el = LangType.make_type(&self.types);
|
||
} else {
|
||
sema_expr(self, er, 0);
|
||
el = SemaM.tid_of(self, er);
|
||
}
|
||
var sm = StructMember { .name = "", .lang = el };
|
||
match Vec.push(&members, &sm as *u8) { .Ok => {}, .Err(error) => {} }
|
||
k = k + 1;
|
||
}
|
||
sl = LangType.make_struct(&self.types, members);
|
||
}
|
||
var i: usize = 0;
|
||
while i < x.len {
|
||
var ml = LangType.member_lang(&self.types, sl, i);
|
||
if LangType.is_type(&self.types, ml) && !LangType.is_type(&self.types, SemaM.tid_of(self, x[i])) {
|
||
var tl = SemaM.resolve_type(self, x[i]);
|
||
if tl != 0 {
|
||
var h = SemaM.ct_handle(self, tl);
|
||
Bld.node_ptr(&self.hir, x[i]).kind = .LitInt(SemaM.itext(self, h));
|
||
SemaM.set_tid(self, x[i], LangType.make_type(&self.types));
|
||
}
|
||
}
|
||
if !LangType.is_type(&self.types, SemaM.tid_of(self, x[i])) {
|
||
sema_expr(self, x[i], ml);
|
||
}
|
||
i = i + 1;
|
||
}
|
||
SemaM.set_tid(self, ref, sl);
|
||
},
|
||
.Range(x) => {
|
||
var us = LangType.usize_lang(&self.types);
|
||
sema_expr(self, x.begin, us);
|
||
sema_expr(self, x.end, us);
|
||
SemaM.set_tid(self, ref, range_lang(self));
|
||
},
|
||
.SliceExpr(x) => {
|
||
sema_expr(self, x.obj, 0);
|
||
var el = LangType.elem_of(&self.types, SemaM.tid_of(self, x.obj));
|
||
var sl = LangType.make_slice(&self.types, el);
|
||
var us = LangType.usize_lang(&self.types);
|
||
sema_expr(self, x.begin, us);
|
||
sema_expr(self, x.end, us);
|
||
SemaM.set_tid(self, ref, sl);
|
||
},
|
||
.ArrayLit(x) => {
|
||
var arr: usize = 0;
|
||
var el: usize = 0;
|
||
if x.elem_ty != 0 { el = SemaM.resolve_type(self, x.elem_ty); }
|
||
if el == 0 && want != 0 && LangType.is_array(&self.types, want) {
|
||
arr = want;
|
||
el = LangType.elem_of(&self.types, want);
|
||
} else if el == 0 && want != 0 && LangType.is_slice(&self.types, want) {
|
||
arr = want;
|
||
el = LangType.elem_of(&self.types, want);
|
||
} else {
|
||
arr = LangType.make_array(&self.types, el, x.elems.len);
|
||
}
|
||
var i: usize = 0;
|
||
while i < x.elems.len {
|
||
sema_expr(self, x.elems[i], el);
|
||
i = i + 1;
|
||
}
|
||
SemaM.set_tid(self, ref, arr);
|
||
},
|
||
.Cast(x) => {
|
||
var to = SemaM.resolve_type(self, x.to);
|
||
sema_expr(self, x.value, to);
|
||
SemaM.set_tid(self, ref, to);
|
||
},
|
||
.Call(x) => {
|
||
var fn_tid: usize = 0;
|
||
var ret_tid: usize = 0;
|
||
var bid: Builtin = .None;
|
||
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 {
|
||
match cn.kind {
|
||
.Global(gx) => {
|
||
var vx = gx;
|
||
// `@name` 优先解析内置绑定:避免被同名方法/声明遮蔽(如方法名 assert)。
|
||
var bi = SemaM.find_binding(self, vx);
|
||
var prefer_binding = x.is_comptime && bi != 0;
|
||
var d: usize = 0;
|
||
if !prefer_binding { d = SemaM.find_decl(self, vx); }
|
||
if d == 0 && !prefer_binding { d = SemaM.find_path_decl(self, vx); }
|
||
if d != 0 && @enum2int(SemaM.decl_at(self, d - 1).kind) == @enum2int(DeclKind.Fn) {
|
||
SemaM.ensure_decl(self, d - 1);
|
||
fn_tid = SemaM.decl_at(self, d - 1).lang;
|
||
ret_tid = SemaM.decl_at(self, d - 1).ret_lang;
|
||
SemaM.set_node_fn(self, x.callee, SemaM.decl_at(self, d - 1).sym);
|
||
SemaM.set_tid(self, x.callee, fn_tid);
|
||
SemaM.record_call(self, SemaM.decl_at(self, d - 1).sym);
|
||
} else {
|
||
// 内置绑定表:暴露名 -> Intrinsic / ComptimeFn。
|
||
if bi == 0 { bi = SemaM.find_binding(self, vx); }
|
||
if bi != 0 {
|
||
var b = (self.bindings.data as *Binding)[bi - 1];
|
||
if @enum2int(b.target) == @enum2int(Target.Intrinsic) {
|
||
bid = b.kind;
|
||
if B.is_compile_time(bid) {
|
||
if !x.is_comptime && SemaM.builtin_requires_at(bid) {
|
||
SemaM.sema_error_at(self, ref, "comptime call requires @", vx);
|
||
ret;
|
||
}
|
||
if b.fnref != 0 {
|
||
var db = SemaM.find_decl_by_node(self, b.mod, b.fnref);
|
||
if db != 0 {
|
||
SemaM.ensure_decl(self, db - 1);
|
||
fn_tid = SemaM.decl_at(self, db - 1).lang;
|
||
ret_tid = SemaM.decl_at(self, db - 1).ret_lang;
|
||
}
|
||
}
|
||
SemaM.emit_intrinsic(self, ref, x.args, bid);
|
||
ret;
|
||
} else {
|
||
// 运行期 intrinsic(assert/enum2int):标到节点,留待 lower。
|
||
SemaM.set_node_builtin(self, x.callee, @enum2int(bid) as usize);
|
||
SemaM.set_node_builtin(self, ref, @enum2int(bid) as usize);
|
||
}
|
||
} else if @enum2int(b.target) == @enum2int(Target.Native) {
|
||
// 类型查询在实参静态可解析时仍折成常量;
|
||
// 动态 type 句柄(comptime 参数/reflect 结果)才走宿主桥。
|
||
if SemaM.is_type_query(b.kind) && SemaM.type_builtin(self, ref, x.args, b.kind) != 0 { ret; }
|
||
// 外部实现:只解析符号,调用留给 lower 生成 extern 调用。
|
||
var svn = self.cur_mod;
|
||
if b.mod != svn { SemaM.switch_mod(self, b.mod); }
|
||
SemaM.ensure_namespace(self, b.container);
|
||
if b.mod != svn { SemaM.switch_mod(self, svn); }
|
||
var dn = SemaM.find_decl_by_node(self, b.mod, b.fnref);
|
||
if dn != 0 {
|
||
SemaM.ensure_decl(self, dn - 1);
|
||
fn_tid = SemaM.decl_at(self, dn - 1).lang;
|
||
ret_tid = SemaM.decl_at(self, dn - 1).ret_lang;
|
||
SemaM.set_node_fn(self, x.callee, SemaM.decl_at(self, dn - 1).sym);
|
||
SemaM.set_tid(self, x.callee, fn_tid);
|
||
SemaM.record_call_from(self, SemaM.ct_root_of(ref), SemaM.decl_at(self, dn - 1).sym);
|
||
SemaM.record_call(self, SemaM.decl_at(self, dn - 1).sym);
|
||
native_callee = true;
|
||
}
|
||
} else {
|
||
// ComptimeFn:解析到被调函数体,走 VM 求值。
|
||
var sv2 = self.cur_mod;
|
||
if b.mod != sv2 { SemaM.switch_mod(self, b.mod); }
|
||
SemaM.ensure_namespace(self, b.container);
|
||
if b.mod != sv2 { SemaM.switch_mod(self, sv2); }
|
||
var d2 = SemaM.find_decl_by_node(self, b.mod, b.fnref);
|
||
if d2 != 0 {
|
||
SemaM.ensure_decl(self, d2 - 1);
|
||
fn_tid = SemaM.decl_at(self, d2 - 1).lang;
|
||
ret_tid = SemaM.decl_at(self, d2 - 1).ret_lang;
|
||
SemaM.set_node_fn(self, x.callee, SemaM.decl_at(self, d2 - 1).sym);
|
||
SemaM.set_tid(self, x.callee, fn_tid);
|
||
SemaM.record_call_from(self, SemaM.ct_root_of(ref), SemaM.decl_at(self, d2 - 1).sym);
|
||
SemaM.record_call(self, SemaM.decl_at(self, d2 - 1).sym);
|
||
if !SemaM.has_tag(self, b.fnref, "extern") && !SemaM.has_tag(self, b.fnref, "builtin") {
|
||
is_ct_builtin = true;
|
||
} else {
|
||
native_callee = true;
|
||
}
|
||
}
|
||
}
|
||
} else if x.is_comptime {
|
||
SemaM.sema_error_at(self, ref, "unknown comptime function", vx);
|
||
ret;
|
||
}
|
||
}
|
||
},
|
||
.Field(_) => {
|
||
// 方法调用:先解析 callee(字段可能是方法名)。
|
||
var saved_callee = self.in_callee;
|
||
self.in_callee = true;
|
||
sema_expr(self, x.callee, 0);
|
||
self.in_callee = saved_callee;
|
||
csi = SemaM.node_fn(self, x.callee);
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
// Global 分支在解析 callee 时才写入 node_fn,这里统一回读一次。
|
||
if csi == 0 { csi = SemaM.node_fn(self, x.callee); }
|
||
if csi != 0 {
|
||
var s2 = SemaM.sym_at(self, csi - 1);
|
||
fn_tid = s2.fn_tid;
|
||
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 &&
|
||
!SemaM.has_tag(self, ref, "hook_call") {
|
||
if self.hook_hooks.prepare_params != null {
|
||
// 实例化会改写形参类型,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") {
|
||
var fn_node = Bld.node_ptr(&self.hir, callee_ref);
|
||
var plen: usize = 0;
|
||
if fn_node != null {
|
||
match fn_node.kind { .FnDecl(f) => { plen = f.params.len; }, _ => {} }
|
||
}
|
||
if plen > 0 {
|
||
var fixed = plen - 1;
|
||
if x.args.len < fixed {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.ArgCount, "macro argument count mismatch", "");
|
||
} else {
|
||
var extras = Vec.blank(@sizeof(Ref), @alignof(Ref));
|
||
var ei = fixed;
|
||
while ei < x.args.len {
|
||
var er = x.args[ei];
|
||
sema_expr(self, er, 0);
|
||
var kind = SemaM.macro_arg_kind(self, er);
|
||
var kind_node = Bld.construct(&self.hir, .{ .kind = .LitStr(kind) });
|
||
// 第三个元素是实参的反射类型节点:库宏据此做类型分派,
|
||
// 编译器不再为具体库维护类型词汇表。
|
||
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;
|
||
}
|
||
var ex_slice = Bld.dup_refs(&self.hir, (extras.data as *Ref)[0..extras.len]);
|
||
Vec.drop(&extras);
|
||
var tuple_ref = Bld.construct(&self.hir, .{ .kind = .TupleLit(ex_slice) });
|
||
var hidden = Bld.construct(&self.hir, .{ .kind = .LitInt(SemaM.itext(self, tuple_ref)) });
|
||
SemaM.set_tid(self, hidden, LangType.usize_lang(&self.types));
|
||
var av = Vec.blank(@sizeof(Ref), @alignof(Ref));
|
||
var ai: usize = 0;
|
||
while ai < fixed {
|
||
var ar = x.args[ai];
|
||
match Vec.push(&av, &ar as *u8) { .Ok => {}, .Err(e) => {} }
|
||
ai = ai + 1;
|
||
}
|
||
match Vec.push(&av, &hidden as *u8) { .Ok => {}, .Err(e) => {} }
|
||
var new_args = Bld.dup_refs(&self.hir, (av.data as *Ref)[0..av.len]);
|
||
Vec.drop(&av);
|
||
x.args = new_args;
|
||
var call_node = Bld.node_ptr(&self.hir, ref);
|
||
if call_node != null { call_node.kind = .Call(x); }
|
||
}
|
||
}
|
||
}
|
||
// @ 是源码进入 comptime 的唯一入口;编译器生成的 HIR 必须显式
|
||
// 设置 .is_comptime = true,不能靠 callee 的 comptime 属性旁路。
|
||
if callee_ref != 0 && !x.is_comptime {
|
||
var fnn = Bld.node_ptr(&self.hir, callee_ref);
|
||
if fnn != null {
|
||
match fnn.kind {
|
||
.FnDecl(f) => {
|
||
if f.is_comptime { SemaM.sema_error_at(self, ref, "comptime call requires @", f.name); }
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
}
|
||
if is_ct_builtin && !x.is_comptime {
|
||
SemaM.sema_error_at(self, ref, "comptime call requires @", "");
|
||
}
|
||
if fn_tid == 0 && csi == 0 && @enum2int(bid) == @enum2int(Builtin.None) {
|
||
var gen_tl: usize = 0;
|
||
var ccn = Bld.node_ptr(&self.hir, x.callee);
|
||
if ccn != null {
|
||
match ccn.kind {
|
||
// 类型生成器要先走缓存实例;直接 sema_expr 会把
|
||
// `@Result(...)` 求成一个没有方法表的新匿名类型。
|
||
.Call(c) => {
|
||
gen_tl = SemaM.type_gen_lang(self, x.callee, c, 0);
|
||
if gen_tl != 0 { SemaM.set_tid(self, x.callee, gen_tl); }
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
if gen_tl != 0 {
|
||
if construct_type_value(self, ref, x.callee, x.args, gen_tl) { ret; }
|
||
} else {
|
||
sema_expr(self, x.callee, 0);
|
||
var ct = SemaM.tid_of(self, x.callee);
|
||
if ct != 0 {
|
||
if LangType.kind_eq(LangType.kind_of(&self.types, ct), .Fn) {
|
||
fn_tid = ct;
|
||
ret_tid = LangType.fn_ret_of(&self.types, ct);
|
||
} else if LangType.is_type(&self.types, ct) {
|
||
if construct_type_value(self, ref, x.callee, x.args, 0) { ret; }
|
||
} else if SemaM.node_variant(self, x.callee) != 0 {
|
||
if construct_variant_call(self, ref, x.callee, x.args) { ret; }
|
||
} else if dispatch_call_value(self, ref, x.callee, x.args, ct) {
|
||
ret;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
var param_count: usize = 0;
|
||
var has_signature = false;
|
||
if callee_ref != 0 {
|
||
var fnn = Bld.node_ptr(&self.hir, callee_ref);
|
||
if fnn != null {
|
||
match fnn.kind {
|
||
.FnDecl(f) => { param_count = f.params.len; has_signature = true; },
|
||
_ => {},
|
||
}
|
||
}
|
||
}
|
||
if !has_signature && fn_tid != 0 && LangType.is_fn(&self.types, fn_tid) {
|
||
param_count = LangType.fn_param_count(&self.types, fn_tid);
|
||
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", 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", "");
|
||
}
|
||
var i: usize = 0;
|
||
while i < x.args.len {
|
||
var a = x.args[i];
|
||
var pwant: usize = 0;
|
||
if fn_tid != 0 { pwant = LangType.fn_param_of(&self.types, fn_tid, i); }
|
||
sema_expr(self, a, pwant);
|
||
if pwant != 0 && LangType.is_type(&self.types, pwant) {
|
||
// 静态类型实参折成句柄;动态类型值(如另一次 build 的结果)
|
||
// 必须留给 comptime VM 求值,不能在这里写成 0。
|
||
var h = SemaM.type_arg_handle(self, a);
|
||
if h != 0 {
|
||
var hv = SemaM.ct_handle(self, h);
|
||
Bld.node_ptr(&self.hir, a).kind = .LitInt(SemaM.itext(self, hv));
|
||
SemaM.set_tid(self, a, pwant);
|
||
}
|
||
}
|
||
if pwant != 0 && !SemaM.is_shape_param_at(self, callee_ref, i) {
|
||
SemaM.require_type(self, a, pwant, "argument");
|
||
}
|
||
i = i + 1;
|
||
}
|
||
if callee_ref != 0 && self.shape_hooks.prepare_call != null {
|
||
var sc = ShapeCallResult { .ok = true, .fn_tid = 0, .ret_tid = 0 };
|
||
self.shape_hooks.prepare_call(self as *any, callee_ref, x.callee, x.args, &sc);
|
||
if sc.fn_tid != 0 {
|
||
fn_tid = sc.fn_tid;
|
||
ret_tid = sc.ret_tid;
|
||
}
|
||
}
|
||
// `@` 调用一律是编译期调用:chunk 构建期编进 VM,
|
||
// 其余位置在这里直接求值。native 桥原语同样遵守该规则。
|
||
if x.is_comptime &&
|
||
(native_callee || is_ct_builtin ||
|
||
(fn_tid != 0 && @enum2int(bid) == @enum2int(Builtin.None))) {
|
||
if ret_tid == 0 { ret_tid = LangType.make_unit(&self.types); }
|
||
SemaM.set_tid(self, ref, ret_tid);
|
||
var sym = SemaM.node_fn(self, x.callee);
|
||
if sym != 0 { SemaM.record_call_from(self, SemaM.ct_root_of(ref), sym); }
|
||
if self.ct.building { ret; }
|
||
var sv = self.cur_fn;
|
||
var saved_ref = self.cur_fn_ref;
|
||
var saved_ct_ref = self.ct_call_ref;
|
||
var saved_ct_mod = self.ct_call_mod;
|
||
var saved_macro_ref = self.macro_call_ref;
|
||
var saved_macro_mod = self.macro_call_mod;
|
||
self.cur_fn = SemaM.ct_root_of(ref);
|
||
self.cur_fn_ref = ref;
|
||
self.ct_call_ref = ref;
|
||
self.ct_call_mod = self.cur_mod;
|
||
if is_macro {
|
||
self.macro_call_ref = ref;
|
||
self.macro_call_mod = self.cur_mod;
|
||
}
|
||
if self.ct.eval != null { self.ct.eval(self as *any, ref); }
|
||
self.macro_call_mod = saved_macro_mod;
|
||
self.macro_call_ref = saved_macro_ref;
|
||
self.ct_call_mod = saved_ct_mod;
|
||
self.ct_call_ref = saved_ct_ref;
|
||
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;
|
||
match cn.kind {
|
||
.LitInt(s) => { code = SemaM.parse_u(s); },
|
||
_ => {},
|
||
}
|
||
if code == 0 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.MacroCode, "macro must return a code Ref", "");
|
||
ret;
|
||
}
|
||
var rn = Bld.node_ptr(&self.hir, code);
|
||
if rn == null {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.MacroCode, "macro returned invalid code Ref", "");
|
||
ret;
|
||
}
|
||
cn.kind = rn.kind;
|
||
cn.tags = rn.tags;
|
||
SemaM.set_tid(self, ref, 0);
|
||
sema_expr(self, ref, want);
|
||
ret;
|
||
}
|
||
ret;
|
||
}
|
||
if @enum2int(bid) == @enum2int(Builtin.Enum2Int) {
|
||
var al = SemaM.tid_of(self, x.args[0]);
|
||
SemaM.set_tid(self, ref, LangType.enum_tag(&self.types, al));
|
||
} else if @enum2int(bid) != @enum2int(Builtin.None) {
|
||
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);
|
||
check_assignable(self, x.lval);
|
||
SemaM.mark_assigned(self, x.lval);
|
||
var lt = SemaM.tid_of(self, x.lval);
|
||
if lt == 0 { lt = want; }
|
||
if lt == 0 {
|
||
sema_expr(self, x.rval, want);
|
||
SemaM.set_tid(self, ref, 0);
|
||
ret;
|
||
}
|
||
if SemaM.op_is(x.op, .Assign) {
|
||
sema_expr(self, x.rval, lt);
|
||
if lt != 0 { SemaM.require_type(self, x.rval, lt, "assignment"); }
|
||
SemaM.set_tid(self, ref, lt);
|
||
ret;
|
||
}
|
||
var op = SemaOpM.binary_op(x.op);
|
||
if @enum2int(op) == @enum2int(SemaOp.None) {
|
||
sema_expr(self, x.rval, lt);
|
||
if lt != 0 { SemaM.require_type(self, x.rval, lt, "compound assignment"); }
|
||
SemaM.set_tid(self, ref, lt);
|
||
ret;
|
||
}
|
||
var impl = resolve_op(self, op, lt);
|
||
match impl {
|
||
.Native(sym) => {
|
||
var rwant = lt;
|
||
if sym != 0 {
|
||
var s = SemaM.sym_at(self, sym - 1);
|
||
if LangType.fn_param_count(&self.types, s.fn_tid) > 1 {
|
||
rwant = LangType.fn_param_of(&self.types, s.fn_tid, 1);
|
||
}
|
||
} else if LangType.is_ptr(&self.types, lt) &&
|
||
(SemaM.op_is(x.op, .Add) || SemaM.op_is(x.op, .Sub)) {
|
||
rwant = LangType.usize_lang(&self.types);
|
||
}
|
||
sema_expr(self, x.rval, rwant);
|
||
if rwant != 0 { SemaM.require_type(self, x.rval, rwant, "compound assignment"); }
|
||
SemaM.set_tid(self, ref, lt);
|
||
},
|
||
.User(sym) => {
|
||
var s = SemaM.sym_at(self, sym - 1);
|
||
var fn_tid = s.fn_tid;
|
||
var ret_tid = s.ret_tid;
|
||
var rhs_want = lt;
|
||
if LangType.fn_param_count(&self.types, fn_tid) > 1 {
|
||
rhs_want = LangType.fn_param_of(&self.types, fn_tid, 1);
|
||
}
|
||
sema_expr(self, x.rval, rhs_want);
|
||
if rhs_want != 0 { SemaM.require_type(self, x.rval, rhs_want, "compound assignment"); }
|
||
var av: [2]Ref = .[x.lval, x.rval];
|
||
var args = Bld.dup_refs(&self.hir, av[0..2]);
|
||
var call = build_op_call(self, sym, args, ret_tid);
|
||
SemaM.require_type(self, call, lt, "compound assignment");
|
||
var np = Bld.node_ptr(&self.hir, ref);
|
||
if np != null {
|
||
np.kind = .CompoundAssign(.{ .op = .Assign, .lval = x.lval, .rval = call });
|
||
}
|
||
SemaM.set_tid(self, ref, lt);
|
||
},
|
||
.Missing => {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.OpNotImplemented,
|
||
"type does not implement operator", SemaOpM.symbol_text(op));
|
||
SemaM.set_tid(self, ref, lt);
|
||
},
|
||
}
|
||
},
|
||
.Binary(x) => {
|
||
// 类型值比较:`A == B` / `A != B` 比较底层 lang,编译期折叠为 bool。
|
||
if (SemaM.op_is(x.op, .Equal) || SemaM.op_is(x.op, .NotEqual)) &&
|
||
(SemaM.is_type_value_expr(self, x.lval) || SemaM.is_type_value_expr(self, x.rval)) {
|
||
var ll = SemaM.type_arg_lang(self, x.lval);
|
||
var rl = SemaM.type_arg_lang(self, x.rval);
|
||
// 动态 type 值(comptime 参数/局部)解析出来只是 `type` 本身,
|
||
// 留给 VM 比较 handle;handle 按 lang 去重,身份仍然正确。
|
||
if ll != 0 && rl != 0 &&
|
||
!LangType.is_type(&self.types, ll) && !LangType.is_type(&self.types, rl) {
|
||
var eq = ll == rl;
|
||
if SemaM.op_is(x.op, .NotEqual) { eq = !eq; }
|
||
var np = Bld.node_ptr(&self.hir, ref);
|
||
if np != null {
|
||
if eq { np.kind = .LitBool("true"); } else { np.kind = .LitBool("false"); }
|
||
}
|
||
SemaM.set_tid(self, ref, LangType.make_bool(&self.types));
|
||
ret;
|
||
}
|
||
}
|
||
var opwant = want;
|
||
if SemaM.is_cmp(x.op) { opwant = 0; }
|
||
if SemaM.is_logic(x.op) { opwant = LangType.make_bool(&self.types); }
|
||
sema_expr(self, x.lval, opwant);
|
||
var lt = SemaM.tid_of(self, x.lval);
|
||
if lt == 0 { lt = opwant; }
|
||
// 比较时一侧是无类型 null:按另一侧的可空类型定型。规则只看 LangKind,
|
||
// 不依赖 null 在源码里的具体写法。
|
||
if SemaM.is_cmp(x.op) && LangType.is_null(&self.types, lt) {
|
||
sema_expr(self, x.rval, 0);
|
||
var rt = SemaM.tid_of(self, x.rval);
|
||
if rt != 0 && LangType.accepts_null(&self.types, rt) {
|
||
sema_expr(self, x.lval, rt);
|
||
lt = SemaM.tid_of(self, x.lval);
|
||
}
|
||
}
|
||
if SemaM.is_logic(x.op) {
|
||
SemaM.require_type(self, x.lval, LangType.make_bool(&self.types), "logical operand");
|
||
}
|
||
if lt == 0 {
|
||
sema_expr(self, x.rval, 0);
|
||
if SemaM.is_cmp(x.op) { SemaM.set_tid(self, ref, LangType.make_bool(&self.types)); }
|
||
else { SemaM.set_tid(self, ref, 0); }
|
||
ret;
|
||
}
|
||
if SemaM.is_logic(x.op) {
|
||
sema_expr(self, x.rval, LangType.make_bool(&self.types));
|
||
SemaM.require_type(self, x.rval, LangType.make_bool(&self.types), "logical operand");
|
||
SemaM.set_tid(self, ref, LangType.make_bool(&self.types));
|
||
ret;
|
||
}
|
||
var op = SemaOpM.binary_op(x.op);
|
||
if @enum2int(op) == @enum2int(SemaOp.Reserved) {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.OpNotImplemented,
|
||
"reserved operator", "reserved operator");
|
||
sema_expr(self, x.rval, lt);
|
||
SemaM.set_tid(self, ref, lt);
|
||
ret;
|
||
}
|
||
if @enum2int(op) == @enum2int(SemaOp.None) {
|
||
sema_expr(self, x.rval, lt);
|
||
if lt != 0 { SemaM.require_type(self, x.rval, lt, "operand"); }
|
||
SemaM.set_tid(self, ref, lt);
|
||
ret;
|
||
}
|
||
var impl = resolve_op(self, op, lt);
|
||
match impl {
|
||
.Native(sym) => {
|
||
var rwant = lt;
|
||
var out = lt;
|
||
if sym != 0 {
|
||
var s = SemaM.sym_at(self, sym - 1);
|
||
if LangType.fn_param_count(&self.types, s.fn_tid) > 1 {
|
||
rwant = LangType.fn_param_of(&self.types, s.fn_tid, 1);
|
||
}
|
||
if !SemaM.is_cmp(x.op) && s.ret_tid != 0 { out = s.ret_tid; }
|
||
} else if LangType.is_ptr(&self.types, lt) &&
|
||
(SemaM.op_is(x.op, .Add) || SemaM.op_is(x.op, .Sub)) {
|
||
rwant = LangType.usize_lang(&self.types);
|
||
}
|
||
sema_expr(self, x.rval, rwant);
|
||
if rwant != 0 { SemaM.require_type(self, x.rval, rwant, "operand"); }
|
||
if SemaM.is_cmp(x.op) { out = LangType.make_bool(&self.types); }
|
||
SemaM.set_tid(self, ref, out);
|
||
},
|
||
.User(sym) => {
|
||
dispatch_user_binary(self, ref, x, lt, sym);
|
||
},
|
||
.Missing => {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.OpNotImplemented,
|
||
"type does not implement operator", SemaOpM.symbol_text(op));
|
||
SemaM.set_tid(self, ref, lt);
|
||
},
|
||
}
|
||
},
|
||
.Addr(x) => {
|
||
sema_expr(self, x, 0);
|
||
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) => {
|
||
sema_expr(self, x, 0);
|
||
var ol = SemaM.tid_of(self, x);
|
||
SemaM.set_tid(self, ref, LangType.elem_of(&self.types, ol));
|
||
},
|
||
.Unary(x) => {
|
||
sema_expr(self, x.val, want);
|
||
var vt = SemaM.tid_of(self, x.val);
|
||
if vt == 0 { vt = want; }
|
||
if vt == 0 { SemaM.set_tid(self, ref, want); ret; }
|
||
match x.op {
|
||
.LogicNot => {
|
||
SemaM.require_type(self, x.val, LangType.make_bool(&self.types), "logical operand");
|
||
SemaM.set_tid(self, ref, LangType.make_bool(&self.types));
|
||
},
|
||
.Sub, .BitNot => {
|
||
var op = SemaOpM.unary_op(x.op);
|
||
var impl = resolve_op(self, op, vt);
|
||
match impl {
|
||
.Native(sym) => { SemaM.set_tid(self, ref, vt); },
|
||
.User(sym) => { dispatch_user_unary(self, ref, x, vt, sym); ret; },
|
||
.Missing => {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.OpNotImplemented,
|
||
"type does not implement operator", SemaOpM.symbol_text(op));
|
||
SemaM.set_tid(self, ref, vt);
|
||
},
|
||
}
|
||
},
|
||
_ => { SemaM.set_tid(self, ref, vt); },
|
||
}
|
||
},
|
||
.Block(x) => {
|
||
var i: usize = 0;
|
||
while i < x.stmts.len {
|
||
sema_stmt(self, x.stmts[i], want);
|
||
i = i + 1;
|
||
}
|
||
sema_expr(self, x.tail, want);
|
||
var bt = SemaM.tid_of(self, x.tail);
|
||
if bt == 0 { bt = want; }
|
||
SemaM.set_tid(self, ref, bt);
|
||
},
|
||
.If(x) => {
|
||
var bt = LangType.make_bool(&self.types);
|
||
sema_expr(self, x.cond, bt);
|
||
sema_body(self, x.then, want);
|
||
if x.els != 0 { sema_body(self, x.els, want); }
|
||
SemaM.set_tid(self, ref, want);
|
||
},
|
||
.IfVar(x) => {
|
||
sema_expr(self, x.init, 0);
|
||
var ol = SemaM.tid_of(self, x.init);
|
||
if x.pattern != 0 { bind_pattern(self, x.pattern, ol); }
|
||
sema_body(self, x.then, want);
|
||
if x.els != 0 { sema_body(self, x.els, want); }
|
||
SemaM.set_tid(self, ref, want);
|
||
},
|
||
.Destructure(x) => {
|
||
sema_expr(self, x.init, 0);
|
||
var ol = SemaM.tid_of(self, x.init);
|
||
if x.pattern != 0 { bind_pattern(self, x.pattern, ol); }
|
||
SemaM.set_tid(self, ref, 0);
|
||
},
|
||
.Loop(x) => {
|
||
var rw = self.loop_results.len;
|
||
match Vec.push(&self.loop_results, &want as *u8) { .Ok => {}, .Err(error) => {} }
|
||
sema_body(self, x.body, 0);
|
||
self.loop_results.len = rw;
|
||
SemaM.set_tid(self, ref, want);
|
||
},
|
||
.Iter(x) => {
|
||
var rw = self.loop_results.len;
|
||
match Vec.push(&self.loop_results, &want as *u8) { .Ok => {}, .Err(error) => {} }
|
||
if x.inputs.len != 1 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.ArgCount,
|
||
"for requires one iterator expression", "");
|
||
} else {
|
||
sema_expr(self, x.inputs[0], 0);
|
||
var it_lang = SemaM.tid_of(self, x.inputs[0]);
|
||
var cap_members = iter_capture_members(self, ref);
|
||
defer { Vec.drop(&cap_members); }
|
||
var cmems: []Ref = SemaM.empty_list(self);
|
||
if cap_members.len > 0 {
|
||
cmems = (cap_members.data as *Ref)[0..cap_members.len];
|
||
}
|
||
var iter_lang = SemaOpM.shape_lang(self, .Iter);
|
||
if iter_lang == 0 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.UndefinedIdentifier,
|
||
"iterator shape Iter not found", "Iter");
|
||
} else if self.shape_for_item != null {
|
||
var item = self.shape_for_item(self as *any, it_lang, iter_lang);
|
||
if item != 0 {
|
||
if x.pattern_type != 0 {
|
||
var pt = SemaM.resolve_type(self, x.pattern_type);
|
||
if pt != item {
|
||
SemaM.sema_error_code_at(self, x.pattern_type, ErrorCode.TypeMismatch,
|
||
"for pattern type mismatch", "");
|
||
}
|
||
}
|
||
var next_sym = SemaM.method_sym(self, it_lang, "next");
|
||
if next_sym == 0 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.ShapeMethodMissing,
|
||
"iterator next method is missing", "next");
|
||
} else {
|
||
var ret_lang = SemaM.sym_at(self, next_sym - 1).ret_tid;
|
||
var item_idx = LangType.member_idx(&self.types, ret_lang, "Item");
|
||
var done_idx = LangType.member_idx(&self.types, ret_lang, "Done");
|
||
if item_idx < 0 || done_idx < 0 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.ShapeMismatch,
|
||
"iterator next must return Item/Done enum", "");
|
||
} else {
|
||
bind_iter_captures(self, x.captures, cmems, x.pattern_type, item);
|
||
SemaM.set_iter_info(self, IterInfo {
|
||
.ref = ref,
|
||
.next_sym = next_sym,
|
||
.item_lang = item,
|
||
.item_index = item_idx as usize,
|
||
.done_index = done_idx as usize,
|
||
});
|
||
}
|
||
}
|
||
} else {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.NotIterable,
|
||
"type is not iterable", "");
|
||
}
|
||
}
|
||
}
|
||
sema_body(self, x.body, 0);
|
||
if x.else_body != 0 { sema_body(self, x.else_body, 0); }
|
||
self.loop_results.len = rw;
|
||
SemaM.set_tid(self, ref, want);
|
||
},
|
||
.Try(x) => {
|
||
sema_expr(self, x, 0);
|
||
var sl = SemaM.tid_of(self, x);
|
||
var ok = LangType.enum_ok_index(&self.types, sl);
|
||
if ok < 0 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.TryNoSuccess,
|
||
"try requires exactly one #ok variant", "");
|
||
}
|
||
if LangType.enum_error_count(&self.types, sl) == 0 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.TryNoError,
|
||
"try requires at least one #error variant", "");
|
||
}
|
||
var dst = SemaM.current_ret_lang(self);
|
||
if !SemaM.enum_errors_compatible(self, sl, dst) {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.TryErrorMismatch,
|
||
"try error variants do not match function result", "");
|
||
}
|
||
if ok >= 0 {
|
||
SemaM.set_tid(self, ref, LangType.member_lang(&self.types, sl, ok as usize));
|
||
} else {
|
||
SemaM.set_tid(self, ref, 0);
|
||
}
|
||
},
|
||
.Catch(x) => {
|
||
sema_expr(self, x.value, 0);
|
||
var sl = SemaM.tid_of(self, x.value);
|
||
var ok = LangType.enum_ok_index(&self.types, sl);
|
||
if ok < 0 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.TryNoSuccess,
|
||
"catch requires exactly one #ok variant", "");
|
||
}
|
||
if LangType.enum_error_count(&self.types, sl) == 0 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.TryNoError,
|
||
"catch requires at least one #error variant", "");
|
||
}
|
||
var erridx = SemaM.pattern_error_index(self, x.pattern, sl);
|
||
if erridx < 0 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.CatchPattern,
|
||
"catch pattern must be an #error variant", "");
|
||
}
|
||
if x.pattern != 0 { bind_pattern(self, x.pattern, sl); }
|
||
var ok_lang: usize = 0;
|
||
if ok >= 0 { ok_lang = LangType.member_lang(&self.types, sl, ok as usize); }
|
||
sema_body(self, x.handler, ok_lang);
|
||
var hl: usize = 0;
|
||
var hn = Bld.node_ptr(&self.hir, x.handler);
|
||
if hn != null {
|
||
match hn.kind {
|
||
.Block(b) => { if b.tail != 0 { hl = SemaM.tid_of(self, b.tail); } },
|
||
_ => { hl = SemaM.tid_of(self, x.handler); },
|
||
}
|
||
}
|
||
if hl != 0 && ok_lang != 0 && hl != ok_lang {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.CatchHandlerType,
|
||
"catch handler type must equal #ok payload", "");
|
||
}
|
||
SemaM.set_tid(self, ref, ok_lang);
|
||
},
|
||
.Match(x) => {
|
||
sema_expr(self, x.operand, 0);
|
||
var ol = SemaM.tid_of(self, x.operand);
|
||
var i: usize = 0;
|
||
while i < x.arms.len {
|
||
var ar = x.arms[i];
|
||
var an = Bld.node_ptr(&self.hir, ar);
|
||
if an != null {
|
||
match an.kind {
|
||
.MatchArm(y) => {
|
||
if y.pattern != 0 { bind_pattern(self, y.pattern, ol); }
|
||
sema_body(self, y.body, want);
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
i = i + 1;
|
||
}
|
||
SemaM.set_tid(self, ref, want);
|
||
},
|
||
_ => { SemaM.set_tid(self, ref, want); },
|
||
}
|
||
}
|
||
|
||
// 赋值目标检查:只有 Local / Field / Index / Deref 可写,const 绑定不可写。
|
||
fn check_assignable(self: *Sema, ref: Ref) {
|
||
var n = Bld.node_ptr(&self.hir, ref);
|
||
if n == null { ret; }
|
||
match n.kind {
|
||
.Local(x) => {
|
||
var d = SemaM.node_def(self, x);
|
||
if d != 0 && !SemaM.def_mutable(self, d) {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.AssignToConst,
|
||
"cannot assign to const binding", "");
|
||
}
|
||
},
|
||
.Field(x) => {
|
||
// `S.c`:命名空间全局在 bind_decl_ref 里把 def 挂到 Field 节点上。
|
||
var d = SemaM.node_def(self, ref);
|
||
if d != 0 && !SemaM.def_mutable(self, d) {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.AssignToConst,
|
||
"cannot assign to const binding", "");
|
||
ret;
|
||
}
|
||
// 指针/切片的字段访问是写穿,不要求指针本身是 lvalue。
|
||
var ot = SemaM.tid_of(self, x.obj);
|
||
if LangType.is_ptr(&self.types, ot) || LangType.is_slice(&self.types, ot) { ret; }
|
||
check_assignable(self, x.obj);
|
||
},
|
||
.Index(x) => {
|
||
var ot = SemaM.tid_of(self, x.obj);
|
||
if LangType.is_ptr(&self.types, ot) || LangType.is_slice(&self.types, ot) { ret; }
|
||
check_assignable(self, x.obj);
|
||
},
|
||
.Deref(_) => {},
|
||
.Global(x) => {
|
||
var d = SemaM.node_def(self, ref);
|
||
if d != 0 && !SemaM.def_mutable(self, d) {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.AssignToConst,
|
||
"cannot assign to const binding", "");
|
||
}
|
||
},
|
||
_ => {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.NotAssignable,
|
||
"assignment target is not assignable", "");
|
||
},
|
||
}
|
||
}
|
||
|
||
fn sema_let(self: *Sema, ref: Ref, ret_tid: usize) { var n = Bld.node_ptr(&self.hir, ref);
|
||
if n == null { ret; }
|
||
// 生成方法体可能被重复进入;重复 new_def 会让 Local 读取未初始化槽。
|
||
if SemaM.node_def(self, ref) != 0 { ret; }
|
||
match n.kind {
|
||
.Alloc(x) => {
|
||
var ty: usize = 0;
|
||
if x.ty != 0 {
|
||
ty = SemaM.resolve_decl_type(self, x.ty, x.value);
|
||
if ty != 0 {
|
||
sema_expr(self, x.value, ty);
|
||
SemaM.require_type(self, x.value, ty, "initializer");
|
||
}
|
||
} else if x.value == 0 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.MissingDeclType,
|
||
"declaration requires a type or an initializer", "");
|
||
var d0 = SemaM.new_def(self, 0, !x.is_const);
|
||
SemaM.set_def(self, ref, d0);
|
||
SemaM.set_tid(self, ref, 0);
|
||
ret;
|
||
} else {
|
||
sema_expr(self, x.value, 0);
|
||
ty = SemaM.tid_of(self, x.value);
|
||
if ty == 0 { ty = ret_tid; }
|
||
// 初值是类型值(如 `var v = @vec(u8)`)→ 变量即该类型的实例。
|
||
if LangType.is_type(&self.types, ty) {
|
||
var l = SemaM.ct_lang(self, SemaM.type_handle_arg(self, x.value));
|
||
if l != 0 { ty = l; }
|
||
}
|
||
}
|
||
SemaM.set_tid(self, ref, ty);
|
||
var d = SemaM.new_def(self, ty, !x.is_const);
|
||
SemaM.set_def(self, ref, d);
|
||
SemaM.set_def_init(self, d, x.value);
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
|
||
fn sema_stmt(self: *Sema, ref: Ref, ret_tid: usize) {
|
||
if ref == 0 { ret; }
|
||
var n = Bld.node_ptr(&self.hir, ref);
|
||
if n == null { ret; }
|
||
match n.kind {
|
||
.Return(x) => {
|
||
// 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, rt, "return");
|
||
}
|
||
},
|
||
.Defer(x) => { sema_stmt(self, x, ret_tid); SemaM.set_tid(self, ref, 0); },
|
||
.ErrDefer(x) => {
|
||
if !LangType.enum_has_error(&self.types, SemaM.current_ret_lang(self)) {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.ErrdeferNoError,
|
||
"errdefer requires an #error result", "");
|
||
}
|
||
sema_stmt(self, x, ret_tid);
|
||
SemaM.set_tid(self, ref, 0);
|
||
},
|
||
.Alloc(_) => { sema_let(self, ref, ret_tid); },
|
||
.If(x) => {
|
||
var bt = LangType.make_bool(&self.types);
|
||
sema_expr(self, x.cond, 0);
|
||
SemaM.require_type(self, x.cond, bt, "condition");
|
||
sema_body(self, x.then, ret_tid);
|
||
if x.els != 0 { sema_body(self, x.els, ret_tid); }
|
||
SemaM.set_tid(self, ref, 0);
|
||
},
|
||
.Loop(_) => { sema_expr(self, ref, 0); },
|
||
.Break(x) => {
|
||
var w: usize = 0;
|
||
if self.loop_results.len != 0 {
|
||
w = (self.loop_results.data as *usize)[self.loop_results.len - 1];
|
||
} else {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.BreakOutsideLoop,
|
||
"break outside loop", "");
|
||
}
|
||
if x.value != 0 { sema_expr(self, x.value, w); }
|
||
SemaM.set_tid(self, ref, w);
|
||
},
|
||
.Continue(_) => {
|
||
if self.loop_results.len == 0 {
|
||
SemaM.sema_error_code_at(self, ref, ErrorCode.ContinueOutsideLoop,
|
||
"continue outside loop", "");
|
||
}
|
||
SemaM.set_tid(self, ref, 0);
|
||
},
|
||
.Block(x) => {
|
||
var i: usize = 0;
|
||
while i < x.stmts.len {
|
||
sema_stmt(self, x.stmts[i], ret_tid);
|
||
i = i + 1;
|
||
}
|
||
sema_expr(self, x.tail, ret_tid);
|
||
},
|
||
_ => { sema_expr(self, ref, 0); },
|
||
}
|
||
}
|
||
|
||
// ---- (B) 逐函数体分块分析 ----
|
||
|
||
|
||
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); }
|
||
},
|
||
}
|
||
}
|