feature. See also
. The project being documented here (as the example) is the Zig library itself.
Builder.binConstAssumeCapacity
fn binConstAssumeCapacity(
self: *Builder,
tag: Constant.Tag,
lhs: Constant,
rhs: Constant,
) Constant
File
Code
fn binConstAssumeCapacity(
self: *Builder,
tag: Constant.Tag,
lhs: Constant,
rhs: Constant,
) Constant {
switch (tag) {
.add,
.@"add nsw",
.@"add nuw",
.sub,
.@"sub nsw",
.@"sub nuw",
.shl,
.xor,
=> {},
else => unreachable,
}
const Key = struct { tag: Constant.Tag, extra: Constant.Binary };
const Adapter = struct {
builder: *const Builder,
pub fn hash(_: @This(), key: Key) u32 {
return @truncate(std.hash.Wyhash.hash(
std.hash.int(@backingInt(key.tag)),
std.mem.asBytes(&key.extra),
));
}
pub fn eql(ctx: @This(), lhs_key: Key, _: void, rhs_index: usize) bool {
if (lhs_key.tag != ctx.builder.constant_items.items(.tag)[rhs_index]) return false;
const rhs_data = ctx.builder.constant_items.items(.data)[rhs_index];
const rhs_extra = ctx.builder.constantExtraData(Constant.Binary, rhs_data);
return std.meta.eql(lhs_key.extra, rhs_extra);
}
};
const data = Key{ .tag = tag, .extra = .{ .lhs = lhs, .rhs = rhs } };
const gop = self.constant_map.getOrPutAssumeCapacityAdapted(data, Adapter{ .builder = self });
if (!gop.found_existing) {
gop.key_ptr.* = {};
gop.value_ptr.* = {};
self.constant_items.appendAssumeCapacity(.{
.tag = tag,
.data = self.addConstantExtraAssumeCapacity(data.extra),
});
}
return @fromBackingInt(@intCast(gop.index));
}