Browse Source

Merge pull request #1444 from LianaHus/sol_externalTypes

Solidity: Convert contract types to addresses N2
cl-refactor
chriseth 10 years ago
parent
commit
e700708492
  1. 16
      libsolidity/AST.cpp
  2. 17
      libsolidity/Types.cpp
  3. 2
      libsolidity/Types.h
  4. 14
      test/SolidityNameAndTypeResolution.cpp

16
libsolidity/AST.cpp

@ -337,8 +337,14 @@ void FunctionDefinition::checkTypeRequirements()
{
if (!var->getType()->canLiveOutsideStorage())
BOOST_THROW_EXCEPTION(var->createTypeError("Type is required to live outside storage."));
if (!(var->getType()->externalType()) && getVisibility() >= Visibility::Public)
BOOST_THROW_EXCEPTION(var->createTypeError("Internal type is not allowed for function with external visibility"));
if (getVisibility() >= Visibility::Public && !(var->getType()->externalType()))
{
// todo delete when will be implemented arrays as parameter type in internal functions
if (getVisibility() == Visibility::Public && var->getType()->getCategory() == Type::Category::Array)
BOOST_THROW_EXCEPTION(var->createTypeError("Arrays only implemented for external functions."));
else
BOOST_THROW_EXCEPTION(var->createTypeError("Internal type is not allowed for public and external functions."));
}
}
for (ASTPointer<ModifierInvocation> const& modifier: m_functionModifiers)
modifier->checkTypeRequirements(isConstructor() ?
@ -379,7 +385,11 @@ void VariableDeclaration::checkTypeRequirements()
m_value->expectType(*m_type);
if (m_isStateVariable && !m_type->externalType() && getVisibility() >= Visibility::Public)
BOOST_THROW_EXCEPTION(createTypeError("Internal type is not allowed for state variables."));
} else
if (!FunctionType(*this).externalType())
BOOST_THROW_EXCEPTION(createTypeError("Internal type is not allowed for public state variables."));
}
else
{
// no type declared and no previous assignment, infer the type
m_value->checkTypeRequirements();

17
libsolidity/Types.cpp

@ -1103,11 +1103,18 @@ TypePointer FunctionType::externalType() const
TypePointers paramTypes;
TypePointers retParamTypes;
for (auto it = m_parameterTypes.cbegin(); it != m_parameterTypes.cend(); ++it)
paramTypes.push_back((*it)->externalType());
for (auto it = m_returnParameterTypes.cbegin(); it != m_returnParameterTypes.cend(); ++it)
retParamTypes.push_back((*it)->externalType());
for (auto type: m_parameterTypes)
{
if (!type->externalType())
return TypePointer();
paramTypes.push_back(type->externalType());
}
for (auto type: m_returnParameterTypes)
{
if (!type->externalType())
return TypePointer();
retParamTypes.push_back(type->externalType());
}
return make_shared<FunctionType>(paramTypes, retParamTypes, m_location, m_arbitraryParameters);
}

2
libsolidity/Types.h

@ -512,6 +512,8 @@ public:
virtual Category getCategory() const override { return Category::Function; }
/// @returns TypePointer of a new FunctionType object. All input/return parameters are an appropriate external types of input/return parameters of current function.
/// Returns an empty shared pointer if one of the input/return parameters does not have an externaltype.
virtual TypePointer externalType() const override;
explicit FunctionType(FunctionDefinition const& _function, bool _isInternal = true);

14
test/SolidityNameAndTypeResolution.cpp

@ -460,7 +460,7 @@ BOOST_AUTO_TEST_CASE(function_external_types)
uint a;
}
contract Test {
function boo(uint arg2, bool arg3, bytes8 arg4, bool[2] pairs, uint[] dynamic, C carg) external returns (uint ret) {
function boo(uint arg2, bool arg3, bytes8 arg4, bool[2] pairs, uint[] dynamic, C carg, address[] addresses) external returns (uint ret) {
ret = 5;
}
})";
@ -471,7 +471,7 @@ BOOST_AUTO_TEST_CASE(function_external_types)
auto functions = contract->getDefinedFunctions();
if (functions.empty())
continue;
BOOST_CHECK_EQUAL("boo(uint256,bool,bytes8,bool[2],uint256[],address)", functions[0]->externalSignature());
BOOST_CHECK_EQUAL("boo(uint256,bool,bytes8,bool[2],uint256[],address,address[])", functions[0]->externalSignature());
}
}
@ -503,6 +503,16 @@ BOOST_AUTO_TEST_CASE(function_external_call_not_allowed_conversion)
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
}
// todo delete when implemented
BOOST_AUTO_TEST_CASE(arrays_in_internal_functions)
{
char const* text = R"(
contract Test {
function foo(address[] addresses) {}
})";
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
}
BOOST_AUTO_TEST_CASE(function_internal_allowed_conversion)
{
char const* text = R"(

Loading…
Cancel
Save