Merge pull request #309 from chriseth/fix_cross_contract_enum_access

Add structs and enums to contract types.
This commit is contained in:
chriseth 2015-12-21 15:37:24 +01:00
commit 6f78998aaf
4 changed files with 59 additions and 14 deletions

View File

@ -1042,12 +1042,14 @@ MemberList::MemberMap ContractType::nativeMembers(ContractDefinition const*) con
} }
} }
else else
{
for (auto const& it: m_contract.interfaceFunctions()) for (auto const& it: m_contract.interfaceFunctions())
members.push_back(MemberList::Member( members.push_back(MemberList::Member(
it.second->declaration().name(), it.second->declaration().name(),
it.second->asMemberFunction(m_contract.isLibrary()), it.second->asMemberFunction(m_contract.isLibrary()),
&it.second->declaration() &it.second->declaration()
)); ));
}
return members; return members;
} }
@ -1858,6 +1860,12 @@ MemberList::MemberMap TypeType::nativeMembers(ContractDefinition const* _current
if (m_actualType->category() == Category::Contract) if (m_actualType->category() == Category::Contract)
{ {
ContractDefinition const& contract = dynamic_cast<ContractType const&>(*m_actualType).contractDefinition(); ContractDefinition const& contract = dynamic_cast<ContractType const&>(*m_actualType).contractDefinition();
bool isBase = false;
if (_currentScope != nullptr)
{
auto const& currentBases = _currentScope->annotation().linearizedBaseContracts;
isBase = (find(currentBases.begin(), currentBases.end(), &contract) != currentBases.end());
}
if (contract.isLibrary()) if (contract.isLibrary())
for (auto const& it: contract.interfaceFunctions()) for (auto const& it: contract.interfaceFunctions())
members.push_back(MemberList::Member( members.push_back(MemberList::Member(
@ -1865,14 +1873,19 @@ MemberList::MemberMap TypeType::nativeMembers(ContractDefinition const* _current
it.second->asMemberFunction(true), // use callcode it.second->asMemberFunction(true), // use callcode
&it.second->declaration() &it.second->declaration()
)); ));
else if (_currentScope != nullptr) if (isBase)
{ {
auto const& currentBases = _currentScope->annotation().linearizedBaseContracts; // We are accessing the type of a base contract, so add all public and protected
if (find(currentBases.begin(), currentBases.end(), &contract) != currentBases.end()) // members. Note that this does not add inherited functions on purpose.
// We are accessing the type of a base contract, so add all public and protected for (Declaration const* decl: contract.inheritableMembers())
// members. Note that this does not add inherited functions on purpose. members.push_back(MemberList::Member(decl->name(), decl->type(), decl));
for (Declaration const* decl: contract.inheritableMembers()) }
members.push_back(MemberList::Member(decl->name(), decl->type(), decl)); else
{
for (auto const& stru: contract.definedStructs())
members.push_back(MemberList::Member(stru->name(), stru->type(), stru));
for (auto const& enu: contract.definedEnums())
members.push_back(MemberList::Member(enu->name(), enu->type(), enu));
} }
} }
else if (m_actualType->category() == Category::Enum) else if (m_actualType->category() == Category::Enum)

View File

@ -913,14 +913,16 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
if (dynamic_cast<ContractType const*>(type.actualType().get())) if (dynamic_cast<ContractType const*>(type.actualType().get()))
{ {
auto const& funType = dynamic_cast<FunctionType const&>(*_memberAccess.annotation().type); if (auto funType = dynamic_cast<FunctionType const*>(_memberAccess.annotation().type.get()))
if (funType.location() != FunctionType::Location::Internal)
m_context << funType.externalIdentifier();
else
{ {
auto const* function = dynamic_cast<FunctionDefinition const*>(_memberAccess.annotation().referencedDeclaration); if (funType->location() != FunctionType::Location::Internal)
solAssert(!!function, "Function not found in member access"); m_context << funType->externalIdentifier();
m_context << m_context.functionEntryLabel(*function).pushTag(); else
{
auto const* function = dynamic_cast<FunctionDefinition const*>(_memberAccess.annotation().referencedDeclaration);
solAssert(!!function, "Function not found in member access");
m_context << m_context.functionEntryLabel(*function).pushTag();
}
} }
} }
else if (auto enumType = dynamic_cast<EnumType const*>(type.actualType().get())) else if (auto enumType = dynamic_cast<EnumType const*>(type.actualType().get()))

View File

@ -5377,6 +5377,21 @@ BOOST_AUTO_TEST_CASE(library_stray_values)
BOOST_CHECK(callContractFunction("f(uint256)", u256(33)) == encodeArgs(u256(42))); BOOST_CHECK(callContractFunction("f(uint256)", u256(33)) == encodeArgs(u256(42)));
} }
BOOST_AUTO_TEST_CASE(cross_contract_types)
{
char const* sourceCode = R"(
contract Lib { struct S {uint a; uint b; } }
contract Test {
function f() returns (uint r) {
var x = Lib.S({a: 2, b: 3});
r = x.b;
}
}
)";
compileAndRun(sourceCode, 0, "Test");
BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(3)));
}
BOOST_AUTO_TEST_CASE(simple_throw) BOOST_AUTO_TEST_CASE(simple_throw)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(

View File

@ -1348,6 +1348,21 @@ BOOST_AUTO_TEST_CASE(enum_member_access)
BOOST_CHECK(success(text)); BOOST_CHECK(success(text));
} }
BOOST_AUTO_TEST_CASE(enum_member_access_accross_contracts)
{
char const* text = R"(
contract Interface {
enum MyEnum { One, Two }
}
contract Impl {
function test() returns (Interface.MyEnum) {
return Interface.MyEnum.One;
}
}
)";
BOOST_CHECK(success(text));
}
BOOST_AUTO_TEST_CASE(enum_invalid_member_access) BOOST_AUTO_TEST_CASE(enum_invalid_member_access)
{ {
char const* text = R"( char const* text = R"(