Sema: implement readFromMemory for arrays

This commit is contained in:
Andrew Kelley 2022-02-20 00:08:05 -07:00
parent bfff8544e1
commit 01638c250f

View File

@ -1090,7 +1090,12 @@ pub const Value = extern union {
}
}
pub fn readFromMemory(ty: Type, target: Target, buffer: []const u8, arena: Allocator) !Value {
pub fn readFromMemory(
ty: Type,
target: Target,
buffer: []const u8,
arena: Allocator,
) Allocator.Error!Value {
switch (ty.zigTypeTag()) {
.Int => {
const int_info = ty.intInfo(target);
@ -1111,6 +1116,17 @@ pub const Value = extern union {
128 => return Value.Tag.float_128.create(arena, floatReadFromMemory(f128, target, buffer)),
else => unreachable,
},
.Array => {
const elem_ty = ty.childType();
const elem_size = elem_ty.abiSize(target);
const elems = try arena.alloc(Value, @intCast(usize, ty.arrayLen()));
var offset: usize = 0;
for (elems) |*elem| {
elem.* = try readFromMemory(elem_ty, target, buffer[offset..], arena);
offset += elem_size;
}
return Tag.array.create(arena, elems);
},
else => @panic("TODO implement readFromMemory for more types"),
}
}