From 2505b8ed9007a9c4c1423d3ad460d7ed8f81b8ef Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 16 Mar 2015 16:15:13 +0100 Subject: [PATCH] Provide access to storage offsets via contract type. --- libsolidity/Compiler.cpp | 17 +++-------------- libsolidity/Types.cpp | 26 +++++++++++++++++++++++--- libsolidity/Types.h | 4 ++++ 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/libsolidity/Compiler.cpp b/libsolidity/Compiler.cpp index db61cc4a1..8e2634499 100644 --- a/libsolidity/Compiler.cpp +++ b/libsolidity/Compiler.cpp @@ -20,12 +20,12 @@ * Solidity compiler. */ +#include #include #include #include #include #include -#include #include #include @@ -274,19 +274,8 @@ void Compiler::appendReturnValuePacker(TypePointers const& _typeParameters) void Compiler::registerStateVariables(ContractDefinition const& _contract) { - vector variables; - for (ContractDefinition const* contract: boost::adaptors::reverse(_contract.getLinearizedBaseContracts())) - for (ASTPointer 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); + for (auto const& var: ContractType(_contract).getStateVariables()) + m_context.addStateVariable(*get<0>(var), get<1>(var), get<2>(var)); } void Compiler::initializeStateVariables(ContractDefinition const& _contract) diff --git a/libsolidity/Types.cpp b/libsolidity/Types.cpp index 04f86b922..ed3cb8fd1 100644 --- a/libsolidity/Types.cpp +++ b/libsolidity/Types.cpp @@ -20,14 +20,14 @@ * Solidity data types */ +#include +#include +#include #include #include #include -#include #include -#include - using namespace std; namespace dev @@ -812,6 +812,26 @@ u256 ContractType::getFunctionIdentifier(string const& _functionName) const return Invalid256; } +vector> ContractType::getStateVariables() const +{ + vector variables; + for (ContractDefinition const* contract: boost::adaptors::reverse(m_contract.getLinearizedBaseContracts())) + for (ASTPointer 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> 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 { return _operator == Token::Delete ? make_shared() : TypePointer(); diff --git a/libsolidity/Types.h b/libsolidity/Types.h index e7601fde8..c7dbcdeb9 100644 --- a/libsolidity/Types.h +++ b/libsolidity/Types.h @@ -403,6 +403,10 @@ public: /// not exist. 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> getStateVariables() const; + private: ContractDefinition const& m_contract; /// If true, it is the "super" type of the current contract, i.e. it contains only inherited