Skip to content

Commit

Permalink
Changes ContractCompiler::superContract into superContractRange, whic…
Browse files Browse the repository at this point in the history
…h also finds super contracts in the case of cross contract lookup.
  • Loading branch information
ekpyron committed May 14, 2018
1 parent b08370f commit 952bb6f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
29 changes: 16 additions & 13 deletions libsolidity/codegen/CompilerContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,20 @@ FunctionDefinition const& CompilerContext::resolveVirtualFunction(FunctionDefini
if (scope->isLibrary())
return _function;
solAssert(!m_inheritanceHierarchy.empty(), "No inheritance hierarchy set.");
return resolveVirtualFunction(_function, m_inheritanceHierarchy.begin());
return resolveVirtualFunction(_function, {m_inheritanceHierarchy.begin(), m_inheritanceHierarchy.end()});
}

FunctionDefinition const& CompilerContext::superFunction(FunctionDefinition const& _function, ContractDefinition const& _base)
{
solAssert(!m_inheritanceHierarchy.empty(), "No inheritance hierarchy set.");
return resolveVirtualFunction(_function, superContract(_base));
return resolveVirtualFunction(_function, superContractRange(_base));
}

FunctionDefinition const* CompilerContext::nextConstructor(ContractDefinition const& _contract) const
{
vector<ContractDefinition const*>::const_iterator it = superContract(_contract);
for (; it != m_inheritanceHierarchy.end(); ++it)
if ((*it)->constructor())
return (*it)->constructor();
for (auto const& base: superContractRange(_contract))
if (base->constructor())
return base->constructor();

return nullptr;
}
Expand Down Expand Up @@ -377,14 +376,13 @@ void CompilerContext::appendInlineAssembly(

FunctionDefinition const& CompilerContext::resolveVirtualFunction(
FunctionDefinition const& _function,
vector<ContractDefinition const*>::const_iterator _searchStart
boost::iterator_range<vector<ContractDefinition const*>::const_iterator> _searchRange
)
{
string name = _function.name();
FunctionType functionType(_function);
auto it = _searchStart;
for (; it != m_inheritanceHierarchy.end(); ++it)
for (FunctionDefinition const* function: (*it)->definedFunctions())
for (auto contract: _searchRange)
for (FunctionDefinition const* function: contract->definedFunctions())
if (
function->name() == name &&
!function->isConstructor() &&
Expand All @@ -395,12 +393,17 @@ FunctionDefinition const& CompilerContext::resolveVirtualFunction(
return _function; // not reached
}

vector<ContractDefinition const*>::const_iterator CompilerContext::superContract(ContractDefinition const& _contract) const
boost::iterator_range<vector<ContractDefinition const*>::const_iterator> CompilerContext::superContractRange(ContractDefinition const &_contract) const
{
solAssert(!m_inheritanceHierarchy.empty(), "No inheritance hierarchy set.");
auto it = find(m_inheritanceHierarchy.begin(), m_inheritanceHierarchy.end(), &_contract);
solAssert(it != m_inheritanceHierarchy.end(), "Base not found in inheritance hierarchy.");
return ++it;
if (it != m_inheritanceHierarchy.end())
return {++it, m_inheritanceHierarchy.end()};

auto const& baseContracts = _contract.annotation().linearizedBaseContracts;
it = find(baseContracts.begin(), baseContracts.end(), &_contract);
solAssert(it != baseContracts.end(), "Base not found in base contracts.");
return {++it, baseContracts.end()};
}

void CompilerContext::updateSourceLocation()
Expand Down
10 changes: 6 additions & 4 deletions libsolidity/codegen/CompilerContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

#include <libdevcore/Common.h>

#include <boost/range/iterator_range.hpp>

#include <ostream>
#include <stack>
#include <queue>
Expand Down Expand Up @@ -252,14 +254,14 @@ class CompilerContext
};

private:
/// Searches the inheritance hierarchy towards the base starting from @a _searchStart and returns
/// Searches the inheritance hierarchy towards the base within @a _searchRange and returns
/// the first function definition that is overwritten by _function.
FunctionDefinition const& resolveVirtualFunction(
FunctionDefinition const& _function,
std::vector<ContractDefinition const*>::const_iterator _searchStart
boost::iterator_range<std::vector<ContractDefinition const*>::const_iterator> _searchRange
);
/// @returns an iterator to the contract directly above the given contract.
std::vector<ContractDefinition const*>::const_iterator superContract(const ContractDefinition &_contract) const;
/// @returns an iterator range from the contract directly above the given contract to the end of the inheritance hierarchy.
boost::iterator_range<std::vector<ContractDefinition const*>::const_iterator> superContractRange(const ContractDefinition &_contract) const;
/// Updates source location set in the assembly.
void updateSourceLocation();

Expand Down

0 comments on commit 952bb6f

Please sign in to comment.