const Scratch = struct astgen: *AstGenscratch_top: u32fn init(astgen: *AstGen) Scratchfn reset(s: *Scratch) voidfn addSlice(s: *Scratch, len: u32) Allocator.Error!Slicefn addOptionalSlice(s: *Scratch, present: bool, len: u32) Allocator.Error!?Slicefn appendBodyWithFixups(s: *Scratch, body: []const Zir.Inst.Index) Allocator.Error!u32fn all(s: *Scratch) SliceReturns the slice containing all data added to this Scratch.
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943const Scratch = struct {
astgen: *AstGen,
scratch_top: u32,
fn init(astgen: *AstGen) Scratch {
return .{
.astgen = astgen,
.scratch_top = @intCast(astgen.scratch.items.len),
};
}
fn reset(s: *Scratch) void {
s.astgen.scratch.shrinkRetainingCapacity(s.scratch_top);
s.* = undefined;
}
fn addSlice(s: *Scratch, len: u32) Allocator.Error!Slice {
const start: u32 = @intCast(s.astgen.scratch.items.len);
try s.astgen.scratch.resize(s.astgen.gpa, start + len);
return .{ .start = start, .len = len };
}
fn addOptionalSlice(s: *Scratch, present: bool, len: u32) Allocator.Error!?Slice {
if (!present) return null;
return try addSlice(s, len);
}
fn appendBodyWithFixups(s: *Scratch, body: []const Zir.Inst.Index) Allocator.Error!u32 {
const len = countBodyLenAfterFixups(s.astgen, body);
try s.astgen.scratch.ensureUnusedCapacity(s.astgen.gpa, len);
appendBodyWithFixupsArrayList(s.astgen, &s.astgen.scratch, body);
return len;
}
/// Returns the slice containing all data added to this `Scratch`.
fn all(s: *Scratch) Slice {
const len = s.astgen.scratch.items.len - s.scratch_top;
return .{ .start = s.scratch_top, .len = @intCast(len) };
}
const Slice = struct {
start: u32,
len: u32,
fn get(s: Slice, astgen: *AstGen) []u32 {
return astgen.scratch.items[s.start..][0..s.len];
}
};
}