Skip to content

Commit

Permalink
Finding new todos
Browse files Browse the repository at this point in the history
- support Transient location in ArrayType and ArrayUtils.
- support location in derivation of Mapping. (mapping might be in a
  storage struct or a transient struct)
  • Loading branch information
Amxx committed Mar 23, 2024
1 parent ac87eea commit ba8e5e7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
31 changes: 18 additions & 13 deletions contracts/example.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
pragma solidity >0.8.25;

contract Test {
struct MyStruct { uint256 value; }
struct MyStruct {
uint256 v;
// mapping(address => uint256) m; // ERROR
}

uint256 transient v_t;
uint256 v_s;
Expand All @@ -16,18 +19,20 @@ contract Test {
mapping(address => mapping(uint256 => uint256)) m2_s;

function set(uint256 value) public {
v_t = value;
v_s = value;
// a_t.push(value); // Error: Member "push" is not available in uint256[] transient pointer outside of storage.
a_s.push(value);
a_t[0] = value;
a_s[0] = value;
s_t.value = value;
s_s.value = value;
m_t[msg.sender] = value;
m_s[msg.sender] = value;
m2_t[msg.sender][0] = value;
m2_s[msg.sender][0] = value;
// v_t = value;
// v_s = value;
// a_t.push(value);
// a_s.push(value);
// a_t[0] = value;
// a_s[0] = value;
// s_t.v = value;
// s_s.v = value;
// s_t.m[msg.sender] = value;
// s_s.m[msg.sender] = value;
// m_t[msg.sender] = value;
// m_s[msg.sender] = value;
// m2_t[msg.sender][0] = value;
// m2_s[msg.sender][0] = value;

// MyStruct transient s;
// assembly { s.slot := 0 } // Error: The suffix ".slot" is not supported by this variable or type.
Expand Down
8 changes: 4 additions & 4 deletions libsolidity/ast/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1922,7 +1922,7 @@ MemberList::MemberMap ArrayType::nativeMembers(ASTNode const*) const
if (!isString())
{
members.emplace_back("length", TypeProvider::uint256());
if (isDynamicallySized() && location() == DataLocation::Storage)
if (isDynamicallySized() && (location() == DataLocation::Storage || location() == DataLocation::Transient))
{
Type const* thisAsPointer = TypeProvider::withLocation(this, location(), true);
members.emplace_back("push", TypeProvider::function(
Expand Down Expand Up @@ -1953,15 +1953,15 @@ MemberList::MemberMap ArrayType::nativeMembers(ASTNode const*) const

Type const* ArrayType::encodingType() const
{
if (location() == DataLocation::Storage)
if (location() == DataLocation::Storage || location() == DataLocation::Transient)
return TypeProvider::uint256();
else
return TypeProvider::withLocation(this, DataLocation::Memory, true);
}

Type const* ArrayType::decodingType() const
{
if (location() == DataLocation::Storage)
if (location() == DataLocation::Storage || location() == DataLocation::Transient)
return TypeProvider::uint256();
else
return this;
Expand All @@ -1983,7 +1983,7 @@ TypeResult ArrayType::interfaceType(bool _inLibrary) const
solAssert(!baseInterfaceType.message().empty(), "Expected detailed error message!");
result = baseInterfaceType;
}
else if (_inLibrary && location() == DataLocation::Storage)
else if (_inLibrary && location() == DataLocation::Storage || location() == DataLocation::Transient)
result = this;
else if (m_arrayKind != ArrayKind::Ordinary)
result = TypeProvider::withLocation(this, DataLocation::Memory, true);
Expand Down
16 changes: 12 additions & 4 deletions libsolidity/codegen/ArrayUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ using namespace solidity::evmasm;
using namespace solidity::frontend;
using namespace solidity::langutil;

// [Amxx] TODO: transient version
void ArrayUtils::copyArrayToStorage(ArrayType const& _targetType, ArrayType const& _sourceType) const
{
// this copies source to target and also clears target if it was larger
Expand Down Expand Up @@ -601,6 +602,7 @@ void ArrayUtils::clearArray(ArrayType const& _typeIn) const
);
}

// [Amxx] TODO: transient?
void ArrayUtils::clearDynamicArray(ArrayType const& _type) const
{
solAssert(_type.location() == DataLocation::Storage, "");
Expand Down Expand Up @@ -640,6 +642,7 @@ void ArrayUtils::clearDynamicArray(ArrayType const& _type) const
m_context << Instruction::POP;
}

// [Amxx] TODO: transient?
void ArrayUtils::resizeDynamicArray(ArrayType const& _typeIn) const
{
Type const* type = &_typeIn;
Expand Down Expand Up @@ -790,6 +793,7 @@ void ArrayUtils::resizeDynamicArray(ArrayType const& _typeIn) const
);
}

// [Amxx] TODO: transient?
void ArrayUtils::incrementDynamicArraySize(ArrayType const& _type) const
{
solAssert(_type.location() == DataLocation::Storage, "");
Expand Down Expand Up @@ -834,6 +838,7 @@ void ArrayUtils::incrementDynamicArraySize(ArrayType const& _type) const
})", {"ref"});
}

// [Amxx] TODO: transient?
void ArrayUtils::popStorageArrayElement(ArrayType const& _type) const
{
solAssert(_type.location() == DataLocation::Storage, "");
Expand Down Expand Up @@ -965,10 +970,13 @@ void ArrayUtils::clearStorageLoop(Type const* _type) const
);
}

// [Amxx] TODO: transient?
void ArrayUtils::convertLengthToSize(ArrayType const& _arrayType, bool _pad) const
{
if (_arrayType.location() == DataLocation::Storage)
switch (_arrayType.location())
{
case DataLocation::Storage:
case DataLocation::Transient:
if (_arrayType.baseType()->storageSize() <= 1)
{
unsigned baseBytes = _arrayType.baseType()->storageBytes();
Expand All @@ -984,9 +992,8 @@ void ArrayUtils::convertLengthToSize(ArrayType const& _arrayType, bool _pad) con
}
else
m_context << _arrayType.baseType()->storageSize() << Instruction::MUL;
}
else
{
break;
default:
if (!_arrayType.isByteArrayOrString())
{
if (_arrayType.location() == DataLocation::Memory)
Expand All @@ -999,6 +1006,7 @@ void ArrayUtils::convertLengthToSize(ArrayType const& _arrayType, bool _pad) con
m_context << u256(31) << Instruction::ADD
<< u256(32) << Instruction::DUP1
<< Instruction::SWAP2 << Instruction::DIV << Instruction::MUL;
break;
}
}

Expand Down

0 comments on commit ba8e5e7

Please sign in to comment.