Browse Source

Merge pull request #2338 from chriseth/sol_memoryStructs

Memory structs.
cl-refactor
chriseth 9 years ago
parent
commit
86850c3c89
  1. 162
      libsolidity/AST.cpp
  2. 6
      libsolidity/AST.h
  3. 55
      libsolidity/CompilerUtils.cpp
  4. 79
      libsolidity/ExpressionCompiler.cpp
  5. 74
      libsolidity/LValue.cpp
  6. 2
      libsolidity/LValue.h
  7. 59
      libsolidity/Types.cpp
  8. 9
      libsolidity/Types.h
  9. 195
      test/libsolidity/SolidityEndToEndTest.cpp
  10. 41
      test/libsolidity/SolidityNameAndTypeResolution.cpp

162
libsolidity/AST.cpp

@ -802,12 +802,11 @@ void FunctionCall::checkTypeRequirements(TypePointers const*)
m_expression->checkTypeRequirements(isPositionalCall ? &argumentTypes : nullptr);
Type const* expressionType = m_expression->getType().get();
TypePointer const& expressionType = m_expression->getType();
FunctionTypePointer functionType;
if (isTypeConversion())
{
TypeType const& type = dynamic_cast<TypeType const&>(*expressionType);
//@todo for structs, we have to check the number of arguments to be equal to the
// number of non-mapping members
if (m_arguments.size() != 1)
BOOST_THROW_EXCEPTION(createTypeError("Exactly one argument expected for explicit type conversion."));
if (!isPositionalCall)
@ -815,87 +814,106 @@ void FunctionCall::checkTypeRequirements(TypePointers const*)
if (!m_arguments.front()->getType()->isExplicitlyConvertibleTo(*type.getActualType()))
BOOST_THROW_EXCEPTION(createTypeError("Explicit type conversion not allowed."));
m_type = type.getActualType();
return;
}
else if (FunctionType const* functionType = dynamic_cast<FunctionType const*>(expressionType))
if (isStructConstructorCall())
{
//@todo would be nice to create a struct type from the arguments
// and then ask if that is implicitly convertible to the struct represented by the
// function parameters
TypePointers const& parameterTypes = functionType->getParameterTypes();
if (!functionType->takesArbitraryParameters() && parameterTypes.size() != m_arguments.size())
BOOST_THROW_EXCEPTION(createTypeError("Wrong argument count for function call."));
TypeType const& type = dynamic_cast<TypeType const&>(*expressionType);
auto const& structType = dynamic_cast<StructType const&>(*type.getActualType());
functionType = structType.constructorType();
}
else
functionType = dynamic_pointer_cast<FunctionType const>(expressionType);
if (isPositionalCall)
{
// call by positional arguments
for (size_t i = 0; i < m_arguments.size(); ++i)
if (
!functionType->takesArbitraryParameters() &&
!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[i])
)
BOOST_THROW_EXCEPTION(m_arguments[i]->createTypeError(
"Invalid type for argument in function call. "
"Invalid implicit conversion from " +
m_arguments[i]->getType()->toString() +
" to " +
parameterTypes[i]->toString() +
" requested."
));
}
else
{
// call by named arguments
if (functionType->takesArbitraryParameters())
BOOST_THROW_EXCEPTION(createTypeError("Named arguments cannnot be used for functions "
"that take arbitrary parameters."));
auto const& parameterNames = functionType->getParameterNames();
if (parameterNames.size() != m_names.size())
BOOST_THROW_EXCEPTION(createTypeError("Some argument names are missing."));
// check duplicate names
for (size_t i = 0; i < m_names.size(); i++)
for (size_t j = i + 1; j < m_names.size(); j++)
if (*m_names[i] == *m_names[j])
BOOST_THROW_EXCEPTION(m_arguments[i]->createTypeError("Duplicate named argument."));
for (size_t i = 0; i < m_names.size(); i++) {
bool found = false;
for (size_t j = 0; j < parameterNames.size(); j++) {
if (parameterNames[j] == *m_names[i]) {
// check type convertible
if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[j]))
BOOST_THROW_EXCEPTION(m_arguments[i]->createTypeError(
"Invalid type for argument in function call. "
"Invalid implicit conversion from " +
m_arguments[i]->getType()->toString() +
" to " +
parameterTypes[i]->toString() +
" requested."
));
found = true;
break;
}
if (!functionType)
BOOST_THROW_EXCEPTION(createTypeError("Type is not callable."));
//@todo would be nice to create a struct type from the arguments
// and then ask if that is implicitly convertible to the struct represented by the
// function parameters
TypePointers const& parameterTypes = functionType->getParameterTypes();
if (!functionType->takesArbitraryParameters() && parameterTypes.size() != m_arguments.size())
BOOST_THROW_EXCEPTION(createTypeError("Wrong argument count for function call."));
if (isPositionalCall)
{
// call by positional arguments
for (size_t i = 0; i < m_arguments.size(); ++i)
if (
!functionType->takesArbitraryParameters() &&
!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[i])
)
BOOST_THROW_EXCEPTION(m_arguments[i]->createTypeError(
"Invalid type for argument in function call. "
"Invalid implicit conversion from " +
m_arguments[i]->getType()->toString() +
" to " +
parameterTypes[i]->toString() +
" requested."
));
}
else
{
// call by named arguments
if (functionType->takesArbitraryParameters())
BOOST_THROW_EXCEPTION(createTypeError(
"Named arguments cannnot be used for functions that take arbitrary parameters."
));
auto const& parameterNames = functionType->getParameterNames();
if (parameterNames.size() != m_names.size())
BOOST_THROW_EXCEPTION(createTypeError("Some argument names are missing."));
// check duplicate names
for (size_t i = 0; i < m_names.size(); i++)
for (size_t j = i + 1; j < m_names.size(); j++)
if (*m_names[i] == *m_names[j])
BOOST_THROW_EXCEPTION(m_arguments[i]->createTypeError("Duplicate named argument."));
for (size_t i = 0; i < m_names.size(); i++) {
bool found = false;
for (size_t j = 0; j < parameterNames.size(); j++) {
if (parameterNames[j] == *m_names[i]) {
// check type convertible
if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[j]))
BOOST_THROW_EXCEPTION(m_arguments[i]->createTypeError(
"Invalid type for argument in function call. "
"Invalid implicit conversion from " +
m_arguments[i]->getType()->toString() +
" to " +
parameterTypes[i]->toString() +
" requested."
));
found = true;
break;
}
if (!found)
BOOST_THROW_EXCEPTION(createTypeError("Named argument does not match function declaration."));
}
if (!found)
BOOST_THROW_EXCEPTION(createTypeError("Named argument does not match function declaration."));
}
// @todo actually the return type should be an anonymous struct,
// but we change it to the type of the first return value until we have structs
if (functionType->getReturnParameterTypes().empty())
m_type = make_shared<VoidType>();
else
m_type = functionType->getReturnParameterTypes().front();
}
// @todo actually the return type should be an anonymous struct,
// but we change it to the type of the first return value until we have anonymous
// structs and tuples
if (functionType->getReturnParameterTypes().empty())
m_type = make_shared<VoidType>();
else
BOOST_THROW_EXCEPTION(createTypeError("Type is not callable."));
m_type = functionType->getReturnParameterTypes().front();
}
bool FunctionCall::isTypeConversion() const
{
return m_expression->getType()->getCategory() == Type::Category::TypeType;
return m_expression->getType()->getCategory() == Type::Category::TypeType && !isStructConstructorCall();
}
bool FunctionCall::isStructConstructorCall() const
{
if (auto const* type = dynamic_cast<TypeType const*>(m_expression->getType().get()))
return type->getActualType()->getCategory() == Type::Category::Struct;
else
return false;
}
void NewExpression::checkTypeRequirements(TypePointers const*)

6
libsolidity/AST.h

@ -1136,9 +1136,11 @@ public:
std::vector<ASTPointer<Expression const>> getArguments() const { return {m_arguments.begin(), m_arguments.end()}; }
std::vector<ASTPointer<ASTString>> const& getNames() const { return m_names; }
/// Returns true if this is not an actual function call, but an explicit type conversion
/// or constructor call.
/// @returns true if this is not an actual function call, but an explicit type conversion.
/// Returns false for struct constructor calls.
bool isTypeConversion() const;
/// @return true if this is a constructor call for a struct, i.e. StructName(...).
bool isStructConstructorCall() const;
private:
ASTPointer<Expression> m_expression;

55
libsolidity/CompilerUtils.cpp

@ -436,19 +436,54 @@ void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetTyp
}
case Type::Category::Struct:
{
//@todo we can probably use some of the code for arrays here.
solAssert(targetTypeCategory == stackTypeCategory, "");
auto& targetType = dynamic_cast<StructType const&>(_targetType);
auto& stackType = dynamic_cast<StructType const&>(_typeOnStack);
auto& typeOnStack = dynamic_cast<StructType const&>(_typeOnStack);
solAssert(
targetType.location() == DataLocation::Storage &&
stackType.location() == DataLocation::Storage,
"Non-storage structs not yet implemented."
);
solAssert(
targetType.isPointer(),
"Type conversion to non-pointer struct requested."
);
targetType.location() != DataLocation::CallData &&
typeOnStack.location() != DataLocation::CallData
, "");
switch (targetType.location())
{
case DataLocation::Storage:
// Other cases are done explicitly in LValue::storeValue, and only possible by assignment.
solAssert(
targetType.isPointer() &&
typeOnStack.location() == DataLocation::Storage,
"Invalid conversion to storage type."
);
break;
case DataLocation::Memory:
// Copy the array to a free position in memory, unless it is already in memory.
if (typeOnStack.location() != DataLocation::Memory)
{
solAssert(typeOnStack.location() == DataLocation::Storage, "");
// stack: <source ref> <source byte offset>
m_context << eth::Instruction::POP;
m_context << typeOnStack.memorySize();
allocateMemory();
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2;
// stack: <memory ptr> <source ref> <memory ptr>
for (auto const& member: typeOnStack.getMembers())
{
if (!member.type->canLiveOutsideStorage())
continue;
pair<u256, unsigned> const& offsets = typeOnStack.getStorageOffsetsOfMember(member.name);
m_context << offsets.first << eth::Instruction::DUP3 << eth::Instruction::ADD;
m_context << u256(offsets.second);
StorageItem(m_context, *member.type).retrieveValue(SourceLocation(), true);
TypePointer targetMemberType = targetType.getMemberType(member.name);
solAssert(!!targetMemberType, "Member not found in target type.");
convertType(*member.type, *targetMemberType, true);
storeInMemoryDynamic(*targetMemberType, true);
}
m_context << eth::Instruction::POP << eth::Instruction::POP;
}
break;
case DataLocation::CallData:
solAssert(false, "Invalid type conversion target location CallData.");
break;
}
break;
}
default:

79
libsolidity/ExpressionCompiler.cpp

@ -155,8 +155,6 @@ bool ExpressionCompiler::visit(Assignment const& _assignment)
TypePointer type = _assignment.getRightHandSide().getType();
if (!_assignment.getType()->dataStoredIn(DataLocation::Storage))
{
//@todo we should delay conversion here if RHS is not in memory, LHS is a MemoryItem
// and not dynamically-sized.
utils().convertType(*type, *_assignment.getType());
type = _assignment.getType();
}
@ -315,38 +313,66 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
using Location = FunctionType::Location;
if (_functionCall.isTypeConversion())
{
//@todo struct construction
solAssert(_functionCall.getArguments().size() == 1, "");
solAssert(_functionCall.getNames().empty(), "");
Expression const& firstArgument = *_functionCall.getArguments().front();
firstArgument.accept(*this);
utils().convertType(*firstArgument.getType(), *_functionCall.getType());
return false;
}
FunctionTypePointer functionType;
if (_functionCall.isStructConstructorCall())
{
auto const& type = dynamic_cast<TypeType const&>(*_functionCall.getExpression().getType());
auto const& structType = dynamic_cast<StructType const&>(*type.getActualType());
functionType = structType.constructorType();
}
else
functionType = dynamic_pointer_cast<FunctionType const>(_functionCall.getExpression().getType());
TypePointers const& parameterTypes = functionType->getParameterTypes();
vector<ASTPointer<Expression const>> const& callArguments = _functionCall.getArguments();
vector<ASTPointer<ASTString>> const& callArgumentNames = _functionCall.getNames();
if (!functionType->takesArbitraryParameters())
solAssert(callArguments.size() == parameterTypes.size(), "");
vector<ASTPointer<Expression const>> arguments;
if (callArgumentNames.empty())
// normal arguments
arguments = callArguments;
else
// named arguments
for (auto const& parameterName: functionType->getParameterNames())
{
bool found = false;
for (size_t j = 0; j < callArgumentNames.size() && !found; j++)
if ((found = (parameterName == *callArgumentNames[j])))
// we found the actual parameter position
arguments.push_back(callArguments[j]);
solAssert(found, "");
}
if (_functionCall.isStructConstructorCall())
{
FunctionType const& function = dynamic_cast<FunctionType const&>(*_functionCall.getExpression().getType());
TypePointers const& parameterTypes = function.getParameterTypes();
vector<ASTPointer<Expression const>> const& callArguments = _functionCall.getArguments();
vector<ASTPointer<ASTString>> const& callArgumentNames = _functionCall.getNames();
if (!function.takesArbitraryParameters())
solAssert(callArguments.size() == parameterTypes.size(), "");
vector<ASTPointer<Expression const>> arguments;
if (callArgumentNames.empty())
// normal arguments
arguments = callArguments;
else
// named arguments
for (auto const& parameterName: function.getParameterNames())
{
bool found = false;
for (size_t j = 0; j < callArgumentNames.size() && !found; j++)
if ((found = (parameterName == *callArgumentNames[j])))
// we found the actual parameter position
arguments.push_back(callArguments[j]);
solAssert(found, "");
}
TypeType const& type = dynamic_cast<TypeType const&>(*_functionCall.getExpression().getType());
auto const& structType = dynamic_cast<StructType const&>(*type.getActualType());
m_context << u256(max(32u, structType.getCalldataEncodedSize(true)));
utils().allocateMemory();
m_context << eth::Instruction::DUP1;
for (unsigned i = 0; i < arguments.size(); ++i)
{
arguments[i]->accept(*this);
utils().convertType(*arguments[i]->getType(), *functionType->getParameterTypes()[i]);
utils().storeInMemoryDynamic(*functionType->getParameterTypes()[i]);
}
m_context << eth::Instruction::POP;
}
else
{
FunctionType const& function = *functionType;
switch (function.getLocation())
{
case Location::Internal:
@ -691,7 +717,8 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
}
case DataLocation::Memory:
{
solAssert(false, "Member access for memory structs not yet implemented.");
m_context << type.memoryOffsetOfMember(member) << eth::Instruction::ADD;
setLValue<MemoryItem>(_memberAccess, *_memberAccess.getType());
break;
}
default:

74
libsolidity/LValue.cpp

@ -47,6 +47,7 @@ void StackVariable::retrieveValue(SourceLocation const& _location, bool) const
errinfo_sourceLocation(_location) <<
errinfo_comment("Stack too deep, try removing local variables.")
);
solAssert(stackPos + 1 >= m_size, "Size and stack pos mismatch.");
for (unsigned i = 0; i < m_size; ++i)
m_context << eth::dupInstruction(stackPos + 1);
}
@ -175,6 +176,7 @@ void StorageItem::retrieveValue(SourceLocation const&, bool _remove) const
void StorageItem::storeValue(Type const& _sourceType, SourceLocation const& _location, bool _move) const
{
CompilerUtils utils(m_context);
// stack: value storage_key storage_offset
if (m_dataType.isValueType())
{
@ -238,52 +240,66 @@ void StorageItem::storeValue(Type const& _sourceType, SourceLocation const& _loc
dynamic_cast<ArrayType const&>(m_dataType),
dynamic_cast<ArrayType const&>(_sourceType));
if (_move)
CompilerUtils(m_context).popStackElement(_sourceType);
utils.popStackElement(m_dataType);
}
else if (m_dataType.getCategory() == Type::Category::Struct)
{
// stack layout: source_ref source_offset target_ref target_offset
// stack layout: source_ref [source_offset] target_ref target_offset
// note that we have structs, so offsets should be zero and are ignored
auto const& structType = dynamic_cast<StructType const&>(m_dataType);
auto const& sourceType = dynamic_cast<StructType const&>(_sourceType);
solAssert(
structType.structDefinition() ==
dynamic_cast<StructType const&>(_sourceType).structDefinition(),
structType.structDefinition() == sourceType.structDefinition(),
"Struct assignment with conversion."
);
solAssert(sourceType.location() != DataLocation::CallData, "Structs in calldata not supported.");
for (auto const& member: structType.getMembers())
{
// assign each member that is not a mapping
TypePointer const& memberType = member.type;
if (memberType->getCategory() == Type::Category::Mapping)
continue;
TypePointer sourceMemberType = sourceType.getMemberType(member.name);
if (sourceType.location() == DataLocation::Storage)
{
// stack layout: source_ref source_offset target_ref target_offset
pair<u256, unsigned> const& offsets = sourceType.getStorageOffsetsOfMember(member.name);
m_context << offsets.first << eth::Instruction::DUP5 << eth::Instruction::ADD;
m_context << u256(offsets.second);
// stack: source_ref source_off target_ref target_off source_member_ref source_member_off
StorageItem(m_context, *sourceMemberType).retrieveValue(_location, true);
// stack: source_ref source_off target_ref target_off source_value...
}
else
{
solAssert(sourceType.location() == DataLocation::Memory, "");
// stack layout: source_ref target_ref target_offset
TypePointer sourceMemberType = sourceType.getMemberType(member.name);
m_context << sourceType.memoryOffsetOfMember(member.name);
m_context << eth::Instruction::DUP4 << eth::Instruction::ADD;
MemoryItem(m_context, *sourceMemberType).retrieveValue(_location, true);
// stack layout: source_ref target_ref target_offset source_value...
}
unsigned stackSize = sourceMemberType->getSizeOnStack();
pair<u256, unsigned> const& offsets = structType.getStorageOffsetsOfMember(member.name);
m_context
<< offsets.first << u256(offsets.second)
<< eth::Instruction::DUP6 << eth::Instruction::DUP3
<< eth::Instruction::ADD << eth::Instruction::DUP2;
// stack: source_ref source_off target_ref target_off member_slot_offset member_byte_offset source_member_ref source_member_off
StorageItem(m_context, *memberType).retrieveValue(_location, true);
// stack: source_ref source_off target_ref target_off member_offset source_value...
solAssert(
4 + memberType->getSizeOnStack() <= 16,
"Stack too deep, try removing local varibales."
);
m_context
<< eth::dupInstruction(4 + memberType->getSizeOnStack())
<< eth::dupInstruction(3 + memberType->getSizeOnStack()) << eth::Instruction::ADD
<< eth::dupInstruction(2 + memberType->getSizeOnStack());
// stack: source_ref source_off target_ref target_off member_slot_offset member_byte_offset source_value... target_member_ref target_member_byte_off
StorageItem(m_context, *memberType).storeValue(*memberType, _location, true);
m_context << eth::Instruction::POP << eth::Instruction::POP;
m_context << eth::dupInstruction(2 + stackSize) << offsets.first << eth::Instruction::ADD;
m_context << u256(offsets.second);
// stack: source_ref [source_off] target_ref target_off source_value... target_member_ref target_member_byte_off
StorageItem(m_context, *memberType).storeValue(*sourceMemberType, _location, true);
}
// stack layout: source_ref [source_offset] target_ref target_offset
unsigned sourceStackSize = sourceType.getSizeOnStack();
if (_move)
m_context
<< eth::Instruction::POP << eth::Instruction::POP
<< eth::Instruction::POP << eth::Instruction::POP;
else
m_context
<< eth::Instruction::SWAP2 << eth::Instruction::POP
<< eth::Instruction::SWAP2 << eth::Instruction::POP;
utils.popStackSlots(2 + sourceType.getSizeOnStack());
else if (sourceType.getSizeOnStack() >= 1)
{
// remove the source ref
solAssert(sourceStackSize <= 2, "Invalid stack size.");
m_context << eth::swapInstruction(sourceStackSize);
if (sourceStackSize == 2)
m_context << eth::Instruction::POP;
m_context << eth::Instruction::SWAP2 << eth::Instruction::POP;
}
}
else
BOOST_THROW_EXCEPTION(

2
libsolidity/LValue.h

@ -103,7 +103,7 @@ private:
class MemoryItem: public LValue
{
public:
MemoryItem(CompilerContext& _compilerContext, Type const& _type, bool _padded);
MemoryItem(CompilerContext& _compilerContext, Type const& _type, bool _padded = true);
virtual unsigned sizeOnStack() const override { return 1; }
virtual void retrieveValue(SourceLocation const& _location, bool _remove = false) const override;
virtual void storeValue(

59
libsolidity/Types.cpp

@ -30,11 +30,8 @@
#include <libsolidity/AST.h>
using namespace std;
namespace dev
{
namespace solidity
{
using namespace dev;
using namespace dev::solidity;
void StorageOffsets::computeOffsets(TypePointers const& _types)
{
@ -999,6 +996,15 @@ unsigned StructType::getCalldataEncodedSize(bool _padded) const
return size;
}
u256 StructType::memorySize() const
{
u256 size;
for (auto const& member: getMembers())
if (member.type->canLiveOutsideStorage())
size += member.type->memoryHeadSize();
return size;
}
u256 StructType::getStorageSize() const
{
return max<u256>(1, getMembers().getStorageSize());
@ -1012,6 +1018,14 @@ bool StructType::canLiveOutsideStorage() const
return true;
}
unsigned StructType::getSizeOnStack() const
{
if (location() == DataLocation::Storage)
return 2; // slot and offset
else
return 1;
}
string StructType::toString(bool _short) const
{
string ret = "struct " + m_struct.getName();
@ -1047,6 +1061,26 @@ TypePointer StructType::copyForLocation(DataLocation _location, bool _isPointer)
return copy;
}
FunctionTypePointer StructType::constructorType() const
{
TypePointers paramTypes;
strings paramNames;
for (auto const& member: getMembers())
{
if (!member.type->canLiveOutsideStorage())
continue;
paramNames.push_back(member.name);
paramTypes.push_back(copyForLocationIfReference(DataLocation::Memory, member.type));
}
return make_shared<FunctionType>(
paramTypes,
TypePointers{copyForLocation(DataLocation::Memory, false)},
paramNames,
strings(),
FunctionType::Location::Internal
);
}
pair<u256, unsigned> const& StructType::getStorageOffsetsOfMember(string const& _name) const
{
auto const* offsets = getMembers().getMemberStorageOffset(_name);
@ -1054,6 +1088,18 @@ pair<u256, unsigned> const& StructType::getStorageOffsetsOfMember(string const&
return *offsets;
}
u256 StructType::memoryOffsetOfMember(string const& _name) const
{
u256 offset;
for (auto const& member: getMembers())
if (member.name == _name)
return offset;
else
offset += member.type->memoryHeadSize();
solAssert(false, "Member not found in struct.");
return 0;
}
TypePointer EnumType::unaryOperatorResult(Token::Value _operator) const
{
return _operator == Token::Delete ? make_shared<VoidType>() : TypePointer();
@ -1663,6 +1709,3 @@ string MagicType::toString(bool) const
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown kind of magic."));
}
}
}
}

9
libsolidity/Types.h

@ -543,21 +543,26 @@ class StructType: public ReferenceType
public:
virtual Category getCategory() const override { return Category::Struct; }
explicit StructType(StructDefinition const& _struct):
//@todo only storage until we have non-storage structs
ReferenceType(DataLocation::Storage), m_struct(_struct) {}
virtual bool isImplicitlyConvertibleTo(const Type& _convertTo) const override;
virtual bool operator==(Type const& _other) const override;
virtual unsigned getCalldataEncodedSize(bool _padded) const override;
u256 memorySize() const;
virtual u256 getStorageSize() const override;
virtual bool canLiveOutsideStorage() const override;
virtual unsigned getSizeOnStack() const override { return 2; }
virtual unsigned getSizeOnStack() const override;
virtual std::string toString(bool _short) const override;
virtual MemberList const& getMembers() const override;
TypePointer copyForLocation(DataLocation _location, bool _isPointer) const override;
/// @returns a function that peforms the type conversion between a list of struct members
/// and a memory struct of this type.
FunctionTypePointer constructorType() const;
std::pair<u256, unsigned> const& getStorageOffsetsOfMember(std::string const& _name) const;
u256 memoryOffsetOfMember(std::string const& _name) const;
StructDefinition const& structDefinition() const { return m_struct; }

195
test/libsolidity/SolidityEndToEndTest.cpp

@ -4806,6 +4806,201 @@ BOOST_AUTO_TEST_CASE(memory_arrays_dynamic_index_access_write)
BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0x20), u256(4), data));
}
BOOST_AUTO_TEST_CASE(memory_structs_read_write)
{
char const* sourceCode = R"(
contract Test {
struct S { uint8 x; uint16 y; uint z; uint8[2] a; }
S[5] data;
function testInit() returns (uint8 x, uint16 y, uint z, uint8 a, bool flag) {
S[2] memory d;
x = d[0].x;
y = d[0].y;
z = d[0].z;
a = d[0].a[1];
flag = true;
}
function testCopyRead() returns (uint8 x, uint16 y, uint z, uint8 a) {
data[2].x = 1;
data[2].y = 2;
data[2].z = 3;
data[2].a[1] = 4;
S memory s = data[2];
x = s.x;
y = s.y;
z = s.z;
a = s.a[1];
}
function testAssign() returns (uint8 x, uint16 y, uint z, uint8 a) {
S memory s;
s.x = 1;
s.y = 2;
s.z = 3;
s.a[1] = 4;
x = s.x;
y = s.y;
z = s.z;
a = s.a[1];
}
}
)";
compileAndRun(sourceCode, 0, "Test");
BOOST_CHECK(callContractFunction("testInit()") == encodeArgs(u256(0), u256(0), u256(0), u256(0), true));
BOOST_CHECK(callContractFunction("testCopyRead()") == encodeArgs(u256(1), u256(2), u256(3), u256(4)));
BOOST_CHECK(callContractFunction("testAssign()") == encodeArgs(u256(1), u256(2), u256(3), u256(4)));
}
BOOST_AUTO_TEST_CASE(memory_structs_as_function_args)
{
char const* sourceCode = R"(
contract Test {
struct S { uint8 x; uint16 y; uint z; }
function test() returns (uint x, uint y, uint z) {
S memory data = combine(1, 2, 3);
x = extract(data, 0);
y = extract(data, 1);
z = extract(data, 2);
}
function extract(S s, uint which) internal returns (uint x) {
if (which == 0) return s.x;
else if (which == 1) return s.y;
else return s.z;
}
function combine(uint8 x, uint16 y, uint z) internal returns (S s) {
s.x = x;
s.y = y;
s.z = z;
}
}
)";
compileAndRun(sourceCode, 0, "Test");
BOOST_CHECK(callContractFunction("test()") == encodeArgs(u256(1), u256(2), u256(3)));
}
BOOST_AUTO_TEST_CASE(memory_structs_nested)
{
char const* sourceCode = R"(
contract Test {
struct S { uint8 x; uint16 y; uint z; }
struct X { uint8 x; S s; }
function test() returns (uint a, uint x, uint y, uint z) {
X memory d = combine(1, 2, 3, 4);
a = extract(d, 0);
x = extract(d, 1);
y = extract(d, 2);
z = extract(d, 3);
}
function extract(X s, uint which) internal returns (uint x) {
if (which == 0) return s.x;
else if (which == 1) return s.s.x;
else if (which == 2) return s.s.y;
else return s.s.z;
}
function combine(uint8 a, uint8 x, uint16 y, uint z) internal returns (X s) {
s.x = a;
s.s.x = x;
s.s.y = y;
s.s.z = z;
}
}
)";
compileAndRun(sourceCode, 0, "Test");
BOOST_CHECK(callContractFunction("test()") == encodeArgs(u256(1), u256(2), u256(3), u256(4)));
}
BOOST_AUTO_TEST_CASE(memory_structs_nested_load)
{
char const* sourceCode = R"(
contract Test {
struct S { uint8 x; uint16 y; uint z; }
struct X { uint8 x; S s; uint8[2] a; }
X m_x;
function load() returns (uint a, uint x, uint y, uint z, uint a1, uint a2) {
m_x.x = 1;
m_x.s.x = 2;
m_x.s.y = 3;
m_x.s.z = 4;
m_x.a[0] = 5;
m_x.a[1] = 6;
X memory d = m_x;
a = d.x;
x = d.s.x;
y = d.s.y;
z = d.s.z;
a1 = d.a[0];
a2 = d.a[1];
}
function store() returns (uint a, uint x, uint y, uint z, uint a1, uint a2) {
X memory d;
d.x = 1;
d.s.x = 2;
d.s.y = 3;
d.s.z = 4;
d.a[0] = 5;
d.a[1] = 6;
m_x = d;
a = m_x.x;
x = m_x.s.x;
y = m_x.s.y;
z = m_x.s.z;
a1 = m_x.a[0];
a2 = m_x.a[1];
}
}
)";
compileAndRun(sourceCode, 0, "Test");
auto out = encodeArgs(u256(1), u256(2), u256(3), u256(4), u256(5), u256(6));
BOOST_CHECK(callContractFunction("load()") == out);
BOOST_CHECK(callContractFunction("store()") == out);
}
BOOST_AUTO_TEST_CASE(struct_constructor_nested)
{
char const* sourceCode = R"(
contract C {
struct X { uint x1; uint x2; }
struct S { uint s1; uint[3] s2; X s3; }
S s;
function C() {
uint[3] memory s2;
s2[1] = 9;
s = S(1, s2, X(4, 5));
}
function get() returns (uint s1, uint[3] s2, uint x1, uint x2)
{
s1 = s.s1;
s2 = s.s2;
x1 = s.s3.x1;
x2 = s.s3.x2;
}
}
)";
compileAndRun(sourceCode, 0, "C");
auto out = encodeArgs(u256(1), u256(0), u256(9), u256(0), u256(4), u256(5));
BOOST_CHECK(callContractFunction("get()") == out);
}
BOOST_AUTO_TEST_CASE(struct_named_constructor)
{
char const* sourceCode = R"(
contract C {
struct S { uint a; bool x; }
S public s;
function C() {
s = S({a: 1, x: true});
}
}
)";
compileAndRun(sourceCode, 0, "C");
BOOST_CHECK(callContractFunction("s()") == encodeArgs(u256(1), true));
}
BOOST_AUTO_TEST_SUITE_END()
}

41
test/libsolidity/SolidityNameAndTypeResolution.cpp

@ -2056,6 +2056,47 @@ BOOST_AUTO_TEST_CASE(memory_arrays_not_resizeable)
BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
}
BOOST_AUTO_TEST_CASE(struct_constructor)
{
char const* sourceCode = R"(
contract C {
struct S { uint a; bool x; }
function f() {
S memory s = S(1, true);
}
}
)";
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
}
BOOST_AUTO_TEST_CASE(struct_constructor_nested)
{
char const* sourceCode = R"(
contract C {
struct X { uint x1; uint x2; }
struct S { uint s1; uint[3] s2; X s3; }
function f() {
uint[3] memory s2;
S memory s = S(1, s2, X(4, 5));
}
}
)";
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
}
BOOST_AUTO_TEST_CASE(struct_named_constructor)
{
char const* sourceCode = R"(
contract C {
struct S { uint a; bool x; }
function f() {
S memory s = S({a: 1, x: true});
}
}
)";
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
}
BOOST_AUTO_TEST_SUITE_END()
}

Loading…
Cancel
Save