mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[Sol->Yul] Implementing deleting storage structs
This commit is contained in:
parent
3ede1f1bca
commit
700390a68b
@ -1147,6 +1147,45 @@ string YulUtilFunctions::clearStorageArrayFunction(ArrayType const& _type)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string YulUtilFunctions::clearStorageStructFunction(StructType const& _type)
|
||||||
|
{
|
||||||
|
solAssert(_type.location() == DataLocation::Storage, "");
|
||||||
|
|
||||||
|
string functionName = "clear_struct_storage_" + _type.identifier();
|
||||||
|
|
||||||
|
return m_functionCollector.createFunction(functionName, [&] {
|
||||||
|
MemberList::MemberMap structMembers = _type.nativeMembers(nullptr);
|
||||||
|
vector<map<string, string>> memberSetValues;
|
||||||
|
|
||||||
|
for (auto const& member: structMembers)
|
||||||
|
{
|
||||||
|
auto const& [memberSlotDiff, memberStorageOffset] = _type.storageOffsetsOfMember(member.name);
|
||||||
|
|
||||||
|
memberSetValues.emplace_back().emplace("clearMember", Whiskers(R"(
|
||||||
|
<setZero>(add(slot, <memberSlotDiff>), <memberStorageOffset>)
|
||||||
|
)")
|
||||||
|
("setZero", storageSetToZeroFunction(*member.type))
|
||||||
|
("memberSlotDiff", memberSlotDiff.str())
|
||||||
|
("memberStorageOffset", to_string(memberStorageOffset))
|
||||||
|
.render()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Whiskers(R"(
|
||||||
|
function <functionName>(slot) {
|
||||||
|
<#member>
|
||||||
|
<clearMember>
|
||||||
|
</member>
|
||||||
|
}
|
||||||
|
)")
|
||||||
|
("functionName", functionName)
|
||||||
|
("allocStruct", allocateMemoryStructFunction(_type))
|
||||||
|
("storageSize", _type.storageSize().str())
|
||||||
|
("member", memberSetValues)
|
||||||
|
.render();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
string YulUtilFunctions::arrayConvertLengthToSize(ArrayType const& _type)
|
string YulUtilFunctions::arrayConvertLengthToSize(ArrayType const& _type)
|
||||||
{
|
{
|
||||||
string functionName = "array_convert_length_to_size_" + _type.identifier();
|
string functionName = "array_convert_length_to_size_" + _type.identifier();
|
||||||
@ -2741,12 +2780,23 @@ string YulUtilFunctions::storageSetToZeroFunction(Type const& _type)
|
|||||||
else if (_type.category() == Type::Category::Array)
|
else if (_type.category() == Type::Category::Array)
|
||||||
return Whiskers(R"(
|
return Whiskers(R"(
|
||||||
function <functionName>(slot, offset) {
|
function <functionName>(slot, offset) {
|
||||||
|
if iszero(eq(offset, 0)) { panic() }
|
||||||
<clearArray>(slot)
|
<clearArray>(slot)
|
||||||
}
|
}
|
||||||
)")
|
)")
|
||||||
("functionName", functionName)
|
("functionName", functionName)
|
||||||
("clearArray", clearStorageArrayFunction(dynamic_cast<ArrayType const&>(_type)))
|
("clearArray", clearStorageArrayFunction(dynamic_cast<ArrayType const&>(_type)))
|
||||||
.render();
|
.render();
|
||||||
|
else if (_type.category() == Type::Category::Struct)
|
||||||
|
return Whiskers(R"(
|
||||||
|
function <functionName>(slot, offset) {
|
||||||
|
if iszero(eq(offset, 0)) { panic() }
|
||||||
|
<clearStruct>(slot)
|
||||||
|
}
|
||||||
|
)")
|
||||||
|
("functionName", functionName)
|
||||||
|
("clearStruct", clearStorageStructFunction(dynamic_cast<StructType const&>(_type)))
|
||||||
|
.render();
|
||||||
else
|
else
|
||||||
solUnimplemented("setToZero for type " + _type.identifier() + " not yet implemented!");
|
solUnimplemented("setToZero for type " + _type.identifier() + " not yet implemented!");
|
||||||
});
|
});
|
||||||
|
@ -427,6 +427,10 @@ private:
|
|||||||
/// signature: (slot, offset)
|
/// signature: (slot, offset)
|
||||||
std::string partialClearStorageSlotFunction();
|
std::string partialClearStorageSlotFunction();
|
||||||
|
|
||||||
|
/// @returns the name of a function that will clear the given storage struct
|
||||||
|
/// signature: (slot) ->
|
||||||
|
std::string clearStorageStructFunction(StructType const& _type);
|
||||||
|
|
||||||
langutil::EVMVersion m_evmVersion;
|
langutil::EVMVersion m_evmVersion;
|
||||||
RevertStrings m_revertStrings;
|
RevertStrings m_revertStrings;
|
||||||
MultiUseYulFunctionCollector& m_functionCollector;
|
MultiUseYulFunctionCollector& m_functionCollector;
|
||||||
|
Loading…
Reference in New Issue
Block a user