Implement readFromMemory/writeToMemory for ptrLikeOptional

This commit is contained in:
DerryAlex 2023-03-14 19:08:56 +08:00 committed by GitHub
parent 9ecdcb8e30
commit d6e48abde8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1365,6 +1365,17 @@ pub const Value = extern union {
if (val.isDeclRef()) return error.ReinterpretDeclRef;
return val.writeToMemory(Type.usize, mod, buffer);
},
.Optional => {
assert(ty.isPtrLikeOptional());
var buf: Type.Payload.ElemType = undefined;
const child = ty.optionalChild(&buf);
const opt_val = val.optionalValue();
if (opt_val) |some| {
return some.writeToMemory(child, mod, buffer);
} else {
return writeToMemory(Value.zero, Type.usize, mod, buffer);
}
},
else => @panic("TODO implement writeToMemory for more types"),
}
}
@ -1471,6 +1482,17 @@ pub const Value = extern union {
if (val.isDeclRef()) return error.ReinterpretDeclRef;
return val.writeToPackedMemory(Type.usize, mod, buffer, bit_offset);
},
.Optional => {
assert(ty.isPtrLikeOptional());
var buf: Type.Payload.ElemType = undefined;
const child = ty.optionalChild(&buf);
const opt_val = val.optionalValue();
if (opt_val) |some| {
return some.writeToPackedMemory(child, mod, buffer, bit_offset);
} else {
return writeToPackedMemory(Value.zero, Type.usize, mod, buffer, bit_offset);
}
},
else => @panic("TODO implement writeToPackedMemory for more types"),
}
}
@ -1579,6 +1601,12 @@ pub const Value = extern union {
assert(!ty.isSlice()); // No well defined layout.
return readFromMemory(Type.usize, mod, buffer, arena);
},
.Optional => {
assert(ty.isPtrLikeOptional());
var buf: Type.Payload.ElemType = undefined;
const child = ty.optionalChild(&buf);
return readFromMemory(child, mod, buffer, arena);
},
else => @panic("TODO implement readFromMemory for more types"),
}
}
@ -1670,6 +1698,12 @@ pub const Value = extern union {
assert(!ty.isSlice()); // No well defined layout.
return readFromPackedMemory(Type.usize, mod, buffer, bit_offset, arena);
},
.Optional => {
assert(ty.isPtrLikeOptional());
var buf: Type.Payload.ElemType = undefined;
const child = ty.optionalChild(&buf);
return readFromPackedMemory(child, mod, buffer, bit_offset, arena);
},
else => @panic("TODO implement readFromPackedMemory for more types"),
}
}