// HIR 子树深拷贝。同一 old Ref 在本次克隆中只产生一个 new Ref, // 以保持 Local 指向的 Param/Var 与声明本身同一身份。 type .{ .Vec } = @import("../stage2/utils/vec.spl"); type .{ .Arena } = @import("../stage2/utils/arena.spl"); type Bld = @import("./spl_ir_builder.spl"); type .{ .Node, .Ref } = @import("./builtins/spl_ir.spl"); type CloneState = @self; olds: Vec, news: Vec, fn new(): CloneState { ret CloneState { .olds = Vec.blank(@sizeof(Ref), @alignof(Ref)), .news = Vec.blank(@sizeof(Ref), @alignof(Ref)), }; } fn drop(self: *CloneState) { Vec.drop(&self.olds); Vec.drop(&self.news); } // 深拷贝子引用列表:construct 只浅拷 Ref,实例化需要独立子树时用这里。 fn clone_refs(b: *Bld.HirNodeContainer, s: *CloneState, xs: []Ref): []Ref { if xs.len == 0 { ret xs; } var p = Arena.alloc_ok(&b.lists, xs.len * @sizeof(Ref), @alignof(Ref)); var dst = p as *Ref; var i: usize = 0; while i < xs.len { dst[i] = clone_ref(b, s, xs.ptr[i]); i = i + 1; } ret dst[0..xs.len]; } fn reset(s: *CloneState) { s.olds.len = 0; s.news.len = 0; } fn lookup(s: *CloneState, r: Ref): Ref { var i: usize = 0; while i < s.olds.len { if (s.olds.data as *Ref)[i] == r { ret (s.news.data as *Ref)[i]; } i = i + 1; } ret 0; } fn record(s: *CloneState, old: Ref, new: Ref) { match Vec.push(&s.olds, &old as *u8) { .Ok => {}, .Err(e) => {} } match Vec.push(&s.news, &new as *u8) { .Ok => {}, .Err(e) => {} } } // 深拷贝一棵子树:同一 old Ref 在本次 clone 中只产生一个 new Ref, // 使 Local 指向的 Param/Var 与声明本身保持同一身份。 fn clone_ref(b: *Bld.HirNodeContainer, s: *CloneState, r: Ref): Ref { if r == 0 { ret 0; } var old_new = lookup(s, r); if old_new != 0 { ret old_new; } var n = Bld.inspect(b, r); var k = n.kind; match n.kind { .Tag(x) => { k = .Tag(.{ .name = x.name, .args = clone_refs(b, s, x.args), .is_at = x.is_at }); }, .TopBlock(x) => { k = .TopBlock(clone_ref(b, s, x)); }, .FnDecl(x) => { k = .FnDecl(.{ .is_comptime = x.is_comptime, .injected = clone_refs(b, s, x.injected), .name = x.name, .params = clone_refs(b, s, x.params), .ret_ty = clone_ref(b, s, x.ret_ty), .body = clone_ref(b, s, x.body) }); }, .ParamDecl(x) => { k = .ParamDecl(.{ .is_comptime = x.is_comptime, .by_ref = x.by_ref, .name = x.name, .ty = clone_ref(b, s, x.ty) }); }, .TypeDecl(x) => { k = .TypeDecl(.{ .name = x.name, .ty = clone_ref(b, s, x.ty) }); }, .AssocDecl(x) => { k = .AssocDecl(.{ .name = x.name }); }, .FieldDecl(x) => { k = .FieldDecl(.{ .name = x.name, .ty = clone_ref(b, s, x.ty) }); }, .VarDecl(x) => { k = .VarDecl(.{ .is_comptime = x.is_comptime, .name = x.name, .ty = clone_ref(b, s, x.ty), .init = clone_ref(b, s, x.init) }); }, .ConstDecl(x) => { k = .ConstDecl(.{ .is_comptime = x.is_comptime, .name = x.name, .ty = clone_ref(b, s, x.ty), .init = clone_ref(b, s, x.init) }); }, .Alloc(x) => { k = .Alloc(.{ .ty = clone_ref(b, s, x.ty), .value = clone_ref(b, s, x.value) }); }, .Block(x) => { k = .Block(.{ .stmts = clone_refs(b, s, x.stmts), .tail = clone_ref(b, s, x.tail) }); }, .If(x) => { k = .If(.{ .cond = clone_ref(b, s, x.cond), .then = clone_ref(b, s, x.then), .els = clone_ref(b, s, x.els), .ret_ty = clone_ref(b, s, x.ret_ty) }); }, .IfVar(x) => { k = .IfVar(.{ .pattern = clone_ref(b, s, x.pattern), .init = clone_ref(b, s, x.init), .then = clone_ref(b, s, x.then), .els = clone_ref(b, s, x.els) }); }, .Destructure(x) => { k = .Destructure(.{ .pattern = clone_ref(b, s, x.pattern), .init = clone_ref(b, s, x.init) }); }, .Loop(x) => { k = .Loop(.{ .label = clone_ref(b, s, x.label), .body = clone_ref(b, s, x.body), .ret_ty = clone_ref(b, s, x.ret_ty) }); }, .Iter(x) => { k = .Iter(.{ .label = clone_ref(b, s, x.label), .inputs = clone_refs(b, s, x.inputs), .captures = clone_refs(b, s, x.captures), .pattern_type = clone_ref(b, s, x.pattern_type), .body = clone_ref(b, s, x.body), .else_body = clone_ref(b, s, x.else_body) }); }, .Break(x) => { k = .Break(.{ .label = clone_ref(b, s, x.label), .value = clone_ref(b, s, x.value) }); }, .Continue(x) => { k = .Continue(.{ .label = clone_ref(b, s, x.label) }); }, .Return(x) => { k = .Return(clone_ref(b, s, x)); }, .Defer(x) => { k = .Defer(clone_ref(b, s, x)); }, .ErrDefer(x) => { k = .ErrDefer(clone_ref(b, s, x)); }, .Try(x) => { k = .Try(clone_ref(b, s, x)); }, .Catch(x) => { k = .Catch(.{ .value = clone_ref(b, s, x.value), .pattern = clone_ref(b, s, x.pattern), .handler = clone_ref(b, s, x.handler) }); }, .IsNonNull(x) => { k = .IsNonNull(clone_ref(b, s, x)); }, .OptionalPayload(x) => { k = .OptionalPayload(clone_ref(b, s, x)); }, .IsTag(x) => { k = .IsTag(.{ .value = clone_ref(b, s, x.value), .name = x.name }); }, .TagPayload(x) => { k = .TagPayload(clone_ref(b, s, x)); }, .Match(x) => { k = .Match(.{ .operand = clone_ref(b, s, x.operand), .arms = clone_refs(b, s, x.arms) }); }, .MatchArm(x) => { k = .MatchArm(.{ .pattern = clone_ref(b, s, x.pattern), .body = clone_ref(b, s, x.body) }); }, .PatLit(x) => { k = .PatLit(clone_ref(b, s, x)); }, .PatTag(x) => { k = .PatTag(.{ .name = x.name, .sub = clone_ref(b, s, x.sub) }); }, .PatList(xs) => { k = .PatList(clone_refs(b, s, xs)); }, .PatTuple(xs) => { k = .PatTuple(clone_refs(b, s, xs)); }, .PatField(x) => { k = .PatField(.{ .name = x.name, .sub = clone_ref(b, s, x.sub) }); }, .AggType(x) => { k = .AggType(.{ .kind = x.kind, .conforms = clone_refs(b, s, x.conforms), .fields = clone_refs(b, s, x.fields), .decls = clone_refs(b, s, x.decls), .injected = clone_refs(b, s, x.injected) }); }, .ShapeApply(x) => { k = .ShapeApply(.{ .base = clone_ref(b, s, x.base), .binds = clone_refs(b, s, x.binds) }); }, .ShapeUse(x) => { k = .ShapeUse(.{ .adapter = x.adapter, .shapes = clone_refs(b, s, x.shapes) }); }, .AssocBind(x) => { k = .AssocBind(.{ .name = x.name, .ty = clone_ref(b, s, x.ty) }); }, .ArrayType(x) => { k = .ArrayType(.{ .len = clone_ref(b, s, x.len), .elem = clone_ref(b, s, x.elem) }); }, .SliceType(x) => { k = .SliceType(clone_ref(b, s, x)); }, .PtrType(x) => { k = .PtrType(clone_ref(b, s, x)); }, .FnType(x) => { k = .FnType(.{ .params = clone_refs(b, s, x.params), .ret_ty = clone_ref(b, s, x.ret_ty) }); }, .TupleType(xs) => { k = .TupleType(clone_refs(b, s, xs)); }, .Field(x) => { k = .Field(.{ .obj = clone_ref(b, s, x.obj), .name = x.name }); }, .Index(x) => { k = .Index(.{ .obj = clone_ref(b, s, x.obj), .idx = clone_ref(b, s, x.idx) }); }, .Range(x) => { k = .Range(.{ .begin = clone_ref(b, s, x.begin), .end = clone_ref(b, s, x.end) }); }, .SliceExpr(x) => { k = .SliceExpr(.{ .obj = clone_ref(b, s, x.obj), .begin = clone_ref(b, s, x.begin), .end = clone_ref(b, s, x.end) }); }, .Cast(x) => { k = .Cast(.{ .to = clone_ref(b, s, x.to), .value = clone_ref(b, s, x.value) }); }, .Local(x) => { k = .Local(clone_ref(b, s, x)); }, .Addr(x) => { k = .Addr(clone_ref(b, s, x)); }, .Deref(x) => { k = .Deref(clone_ref(b, s, x)); }, .FieldLit(x) => { k = .FieldLit(.{ .name = x.name, .value = clone_ref(b, s, x.value) }); }, .AggLit(x) => { k = .AggLit(.{ .name = x.name, .fields = clone_refs(b, s, x.fields) }); }, .ArrayLit(x) => { k = .ArrayLit(.{ .elem_ty = clone_ref(b, s, x.elem_ty), .elems = clone_refs(b, s, x.elems) }); }, .TupleLit(xs) => { k = .TupleLit(clone_refs(b, s, xs)); }, .CompoundAssign(x) => { k = .CompoundAssign(.{ .op = x.op, .lval = clone_ref(b, s, x.lval), .rval = clone_ref(b, s, x.rval) }); }, .Binary(x) => { k = .Binary(.{ .op = x.op, .lval = clone_ref(b, s, x.lval), .rval = clone_ref(b, s, x.rval) }); }, .Unary(x) => { k = .Unary(.{ .op = x.op, .val = clone_ref(b, s, x.val) }); }, .Call(x) => { k = .Call(.{ .is_comptime = x.is_comptime, .callee = clone_ref(b, s, x.callee), .args = clone_refs(b, s, x.args) }); }, .Comptime(x) => { k = .Comptime(clone_ref(b, s, x)); }, _ => {}, } var out = Bld.construct(b, .{ .kind = k, .tags = clone_refs(b, s, n.tags) }); record(s, r, out); ret out; } fn main() { test_clone(); } // 深拷贝:克隆出的子树句柄全新,结构与载荷保持不变。 fn test_clone() { var ir = Bld.new(); defer { Bld.drop(&ir); } var st = new(); defer { drop(&st); } @assert(clone_ref(&ir, &st, 0) == 0, "clone ref 0"); var a = Bld.construct(&ir, .{ .kind = .LitInt("1") }); var b = Bld.construct(&ir, .{ .kind = .LitInt("2") }); var bin = Bld.construct(&ir, .{ .kind = .Binary(.{ .op = .Add, .lval = a, .rval = b }) }); var c = clone_ref(&ir, &st, bin); @assert(c != bin, "clone root distinct"); @assert(clone_ref(&ir, &st, bin) == c, "same old maps to same new"); match Bld.inspect(&ir, c).kind { .Binary(x) => { @assert(x.lval != a && x.rval != b, "children cloned"); match Bld.inspect(&ir, x.lval).kind { .LitInt(s) => { @assert(s.len == 1 && s[0] == '1', "lval text"); }, _ => { @assert(false, "lval kind"); }, } match Bld.inspect(&ir, x.rval).kind { .LitInt(s) => { @assert(s.len == 1 && s[0] == '2', "rval text"); }, _ => { @assert(false, "rval kind"); }, } }, _ => { @assert(false, "clone kind"); }, } reset(&st); var c2 = clone_ref(&ir, &st, bin); @assert(c2 != c, "reset drops old mapping"); }