Browse Source

Provide access to storage offsets via contract type.

cl-refactor
chriseth 10 years ago
parent
commit
2505b8ed90
  1. 17
      libsolidity/Compiler.cpp
  2. 26
      libsolidity/Types.cpp
  3. 4
      libsolidity/Types.h

17
libsolidity/Compiler.cpp

@ -20,12 +20,12 @@
* Solidity compiler. * Solidity compiler.
*/ */
#include <libsolidity/Compiler.h>
#include <algorithm> #include <algorithm>
#include <boost/range/adaptor/reversed.hpp> #include <boost/range/adaptor/reversed.hpp>
#include <libevmcore/Instruction.h> #include <libevmcore/Instruction.h>
#include <libevmcore/Assembly.h> #include <libevmcore/Assembly.h>
#include <libsolidity/AST.h> #include <libsolidity/AST.h>
#include <libsolidity/Compiler.h>
#include <libsolidity/ExpressionCompiler.h> #include <libsolidity/ExpressionCompiler.h>
#include <libsolidity/CompilerUtils.h> #include <libsolidity/CompilerUtils.h>
@ -274,19 +274,8 @@ void Compiler::appendReturnValuePacker(TypePointers const& _typeParameters)
void Compiler::registerStateVariables(ContractDefinition const& _contract) void Compiler::registerStateVariables(ContractDefinition const& _contract)
{ {
vector<VariableDeclaration const*> variables; for (auto const& var: ContractType(_contract).getStateVariables())
for (ContractDefinition const* contract: boost::adaptors::reverse(_contract.getLinearizedBaseContracts())) m_context.addStateVariable(*get<0>(var), get<1>(var), get<2>(var));
for (ASTPointer<VariableDeclaration> const& variable: contract->getStateVariables())
if (!variable->isConstant())
variables.push_back(variable.get());
TypePointers types;
for (auto variable: variables)
types.push_back(variable->getType());
StorageOffsets offsets;
offsets.computeOffsets(types);
for (size_t index = 0; index < variables.size(); ++index)
if (auto const* offset = offsets.getOffset(index))
m_context.addStateVariable(*variables[index], offset->first, offset->second);
} }
void Compiler::initializeStateVariables(ContractDefinition const& _contract) void Compiler::initializeStateVariables(ContractDefinition const& _contract)

26
libsolidity/Types.cpp

@ -20,14 +20,14 @@
* Solidity data types * Solidity data types
*/ */
#include <libsolidity/Types.h>
#include <limits>
#include <boost/range/adaptor/reversed.hpp>
#include <libdevcore/CommonIO.h> #include <libdevcore/CommonIO.h>
#include <libdevcore/CommonData.h> #include <libdevcore/CommonData.h>
#include <libsolidity/Utils.h> #include <libsolidity/Utils.h>
#include <libsolidity/Types.h>
#include <libsolidity/AST.h> #include <libsolidity/AST.h>
#include <limits>
using namespace std; using namespace std;
namespace dev namespace dev
@ -812,6 +812,26 @@ u256 ContractType::getFunctionIdentifier(string const& _functionName) const
return Invalid256; return Invalid256;
} }
vector<tuple<VariableDeclaration const*, u256, unsigned>> ContractType::getStateVariables() const
{
vector<VariableDeclaration const*> variables;
for (ContractDefinition const* contract: boost::adaptors::reverse(m_contract.getLinearizedBaseContracts()))
for (ASTPointer<VariableDeclaration> const& variable: contract->getStateVariables())
if (!variable->isConstant())
variables.push_back(variable.get());
TypePointers types;
for (auto variable: variables)
types.push_back(variable->getType());
StorageOffsets offsets;
offsets.computeOffsets(types);
vector<tuple<VariableDeclaration const*, u256, unsigned>> variablesAndOffsets;
for (size_t index = 0; index < variables.size(); ++index)
if (auto const* offset = offsets.getOffset(index))
variablesAndOffsets.push_back(make_tuple(variables[index], offset->first, offset->second));
return variablesAndOffsets;
}
TypePointer StructType::unaryOperatorResult(Token::Value _operator) const TypePointer StructType::unaryOperatorResult(Token::Value _operator) const
{ {
return _operator == Token::Delete ? make_shared<VoidType>() : TypePointer(); return _operator == Token::Delete ? make_shared<VoidType>() : TypePointer();

4
libsolidity/Types.h

@ -403,6 +403,10 @@ public:
/// not exist. /// not exist.
u256 getFunctionIdentifier(std::string const& _functionName) const; u256 getFunctionIdentifier(std::string const& _functionName) const;
/// @returns a list of all state variables (including inherited) of the contract and their
/// offsets in storage.
std::vector<std::tuple<VariableDeclaration const*, u256, unsigned>> getStateVariables() const;
private: private:
ContractDefinition const& m_contract; ContractDefinition const& m_contract;
/// If true, it is the "super" type of the current contract, i.e. it contains only inherited /// If true, it is the "super" type of the current contract, i.e. it contains only inherited

Loading…
Cancel
Save