mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #1630 from ethereum/function-to-address
Explicit external function type to address conversion
This commit is contained in:
commit
5225a5bb5e
@ -1,5 +1,8 @@
|
|||||||
### 0.4.10 (unreleased)
|
### 0.4.10 (unreleased)
|
||||||
|
|
||||||
|
Features:
|
||||||
|
* Type system: Support explicit conversion of external function to address.
|
||||||
|
|
||||||
### 0.4.9 (2017-01-31)
|
### 0.4.9 (2017-01-31)
|
||||||
|
|
||||||
Features:
|
Features:
|
||||||
|
@ -2158,6 +2158,17 @@ bool FunctionType::operator==(Type const& _other) const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool FunctionType::isExplicitlyConvertibleTo(Type const& _convertTo) const
|
||||||
|
{
|
||||||
|
if (m_location == Location::External && _convertTo.category() == Category::Integer)
|
||||||
|
{
|
||||||
|
IntegerType const& convertTo = dynamic_cast<IntegerType const&>(_convertTo);
|
||||||
|
if (convertTo.isAddress())
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return _convertTo.category() == category();
|
||||||
|
}
|
||||||
|
|
||||||
TypePointer FunctionType::unaryOperatorResult(Token::Value _operator) const
|
TypePointer FunctionType::unaryOperatorResult(Token::Value _operator) const
|
||||||
{
|
{
|
||||||
if (_operator == Token::Value::Delete)
|
if (_operator == Token::Value::Delete)
|
||||||
|
@ -922,6 +922,7 @@ public:
|
|||||||
|
|
||||||
virtual std::string identifier() const override;
|
virtual std::string identifier() const override;
|
||||||
virtual bool operator==(Type const& _other) const override;
|
virtual bool operator==(Type const& _other) const override;
|
||||||
|
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override;
|
||||||
virtual TypePointer unaryOperatorResult(Token::Value _operator) const override;
|
virtual TypePointer unaryOperatorResult(Token::Value _operator) const override;
|
||||||
virtual std::string canonicalName(bool /*_addDataLocation*/) const override;
|
virtual std::string canonicalName(bool /*_addDataLocation*/) const override;
|
||||||
virtual std::string toString(bool _short) const override;
|
virtual std::string toString(bool _short) const override;
|
||||||
|
@ -787,6 +787,20 @@ void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetTyp
|
|||||||
if (_cleanupNeeded)
|
if (_cleanupNeeded)
|
||||||
m_context << Instruction::ISZERO << Instruction::ISZERO;
|
m_context << Instruction::ISZERO << Instruction::ISZERO;
|
||||||
break;
|
break;
|
||||||
|
case Type::Category::Function:
|
||||||
|
{
|
||||||
|
if (targetTypeCategory == Type::Category::Integer)
|
||||||
|
{
|
||||||
|
IntegerType const& targetType = dynamic_cast<IntegerType const&>(_targetType);
|
||||||
|
solAssert(targetType.isAddress(), "Function type can only be converted to address.");
|
||||||
|
FunctionType const& typeOnStack = dynamic_cast<FunctionType const&>(_typeOnStack);
|
||||||
|
solAssert(typeOnStack.location() == FunctionType::Location::External, "Only external function type can be converted.");
|
||||||
|
|
||||||
|
// stack: <address> <function_id>
|
||||||
|
m_context << Instruction::POP;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
// All other types should not be convertible to non-equal types.
|
// All other types should not be convertible to non-equal types.
|
||||||
solAssert(_typeOnStack == _targetType, "Invalid type conversion requested.");
|
solAssert(_typeOnStack == _targetType, "Invalid type conversion requested.");
|
||||||
|
@ -42,7 +42,7 @@ class StackHeightChecker
|
|||||||
public:
|
public:
|
||||||
StackHeightChecker(CompilerContext const& _context):
|
StackHeightChecker(CompilerContext const& _context):
|
||||||
m_context(_context), stackHeight(m_context.stackHeight()) {}
|
m_context(_context), stackHeight(m_context.stackHeight()) {}
|
||||||
void check() { solAssert(m_context.stackHeight() == stackHeight, "I sense a disturbance in the stack."); }
|
void check() { solAssert(m_context.stackHeight() == stackHeight, std::string("I sense a disturbance in the stack: ") + std::to_string(m_context.stackHeight()) + " vs " + std::to_string(stackHeight)); }
|
||||||
private:
|
private:
|
||||||
CompilerContext const& m_context;
|
CompilerContext const& m_context;
|
||||||
unsigned stackHeight;
|
unsigned stackHeight;
|
||||||
|
@ -8462,6 +8462,25 @@ BOOST_AUTO_TEST_CASE(function_array_cross_calls)
|
|||||||
BOOST_CHECK(callContractFunction("test()") == encodeArgs(u256(5), u256(6), u256(7)));
|
BOOST_CHECK(callContractFunction("test()") == encodeArgs(u256(5), u256(6), u256(7)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(external_function_to_address)
|
||||||
|
{
|
||||||
|
char const* sourceCode = R"(
|
||||||
|
contract C {
|
||||||
|
function f() returns (bool) {
|
||||||
|
return address(this.f) == address(this);
|
||||||
|
}
|
||||||
|
function g(function() external cb) returns (address) {
|
||||||
|
return address(cb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
compileAndRun(sourceCode, 0, "C");
|
||||||
|
BOOST_CHECK(callContractFunction("f()") == encodeArgs(true));
|
||||||
|
BOOST_CHECK(callContractFunction("g(function)", fromHex("00000000000000000000000000000000000004226121ff00000000000000000")) == encodeArgs(u160(0x42)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(copy_internal_function_array_to_storage)
|
BOOST_AUTO_TEST_CASE(copy_internal_function_array_to_storage)
|
||||||
{
|
{
|
||||||
char const* sourceCode = R"(
|
char const* sourceCode = R"(
|
||||||
|
@ -4725,6 +4725,42 @@ BOOST_AUTO_TEST_CASE(delete_external_function_type_invalid)
|
|||||||
CHECK_ERROR(text, TypeError, "");
|
CHECK_ERROR(text, TypeError, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(external_function_type_to_address)
|
||||||
|
{
|
||||||
|
char const* text = R"(
|
||||||
|
contract C {
|
||||||
|
function f() returns (address) {
|
||||||
|
return address(this.f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
CHECK_SUCCESS(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(internal_function_type_to_address)
|
||||||
|
{
|
||||||
|
char const* text = R"(
|
||||||
|
contract C {
|
||||||
|
function f() returns (address) {
|
||||||
|
return address(f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
CHECK_ERROR(text, TypeError, "Explicit type conversion not allowed");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(external_function_type_to_uint)
|
||||||
|
{
|
||||||
|
char const* text = R"(
|
||||||
|
contract C {
|
||||||
|
function f() returns (uint) {
|
||||||
|
return uint(this.f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
CHECK_ERROR(text, TypeError, "Explicit type conversion not allowed");
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(invalid_fixed_point_literal)
|
BOOST_AUTO_TEST_CASE(invalid_fixed_point_literal)
|
||||||
{
|
{
|
||||||
char const* text = R"(
|
char const* text = R"(
|
||||||
|
Loading…
Reference in New Issue
Block a user