mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
State variables.
This commit is contained in:
parent
4c8e670530
commit
64a4d77c8b
8
AST.h
8
AST.h
@ -116,9 +116,9 @@ public:
|
||||
|
||||
virtual void accept(ASTVisitor& _visitor) override;
|
||||
|
||||
std::vector<ASTPointer<StructDefinition>> const& getDefinedStructs() { return m_definedStructs; }
|
||||
std::vector<ASTPointer<VariableDeclaration>> const& getStateVariables() { return m_stateVariables; }
|
||||
std::vector<ASTPointer<FunctionDefinition>> const& getDefinedFunctions() { return m_definedFunctions; }
|
||||
std::vector<ASTPointer<StructDefinition>> const& getDefinedStructs() const { return m_definedStructs; }
|
||||
std::vector<ASTPointer<VariableDeclaration>> const& getStateVariables() const { return m_stateVariables; }
|
||||
std::vector<ASTPointer<FunctionDefinition>> const& getDefinedFunctions() const { return m_definedFunctions; }
|
||||
|
||||
private:
|
||||
std::vector<ASTPointer<StructDefinition>> m_definedStructs;
|
||||
@ -135,6 +135,8 @@ public:
|
||||
Declaration(_location, _name), m_members(_members) {}
|
||||
virtual void accept(ASTVisitor& _visitor) override;
|
||||
|
||||
std::vector<ASTPointer<VariableDeclaration>> const& getMembers() const { return m_members; }
|
||||
|
||||
private:
|
||||
std::vector<ASTPointer<VariableDeclaration>> m_members;
|
||||
};
|
||||
|
19
Compiler.cpp
19
Compiler.cpp
@ -21,6 +21,8 @@
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <libevmcore/Instruction.h>
|
||||
#include <libevmcore/Assembly.h>
|
||||
#include <libsolidity/AST.h>
|
||||
#include <libsolidity/Compiler.h>
|
||||
#include <libsolidity/ExpressionCompiler.h>
|
||||
@ -42,10 +44,12 @@ void Compiler::compileContract(ContractDefinition& _contract)
|
||||
m_context = CompilerContext(); // clear it just in case
|
||||
|
||||
//@todo constructor
|
||||
//@todo register state variables
|
||||
|
||||
for (ASTPointer<FunctionDefinition> const& function: _contract.getDefinedFunctions())
|
||||
m_context.addFunction(*function);
|
||||
//@todo sort them?
|
||||
for (ASTPointer<VariableDeclaration> const& variable: _contract.getStateVariables())
|
||||
m_context.addStateVariable(*variable);
|
||||
|
||||
appendFunctionSelector(_contract.getDefinedFunctions());
|
||||
for (ASTPointer<FunctionDefinition> const& function: _contract.getDefinedFunctions())
|
||||
@ -122,7 +126,7 @@ void Compiler::appendCalldataUnpacker(FunctionDefinition const& _function)
|
||||
if (numBytes == 0)
|
||||
BOOST_THROW_EXCEPTION(CompilerError()
|
||||
<< errinfo_sourceLocation(var->getLocation())
|
||||
<< errinfo_comment("Type not yet supported."));
|
||||
<< errinfo_comment("Type " + var->getType()->toString() + " not yet supported."));
|
||||
if (numBytes == 32)
|
||||
m_context << u256(dataOffset) << eth::Instruction::CALLDATALOAD;
|
||||
else
|
||||
@ -139,11 +143,12 @@ void Compiler::appendReturnValuePacker(FunctionDefinition const& _function)
|
||||
vector<ASTPointer<VariableDeclaration>> const& parameters = _function.getReturnParameters();
|
||||
for (unsigned i = 0; i < parameters.size(); ++i)
|
||||
{
|
||||
unsigned numBytes = parameters[i]->getType()->getCalldataEncodedSize();
|
||||
Type const& paramType = *parameters[i]->getType();
|
||||
unsigned numBytes = paramType.getCalldataEncodedSize();
|
||||
if (numBytes == 0)
|
||||
BOOST_THROW_EXCEPTION(CompilerError()
|
||||
<< errinfo_sourceLocation(parameters[i]->getLocation())
|
||||
<< errinfo_comment("Type not yet supported."));
|
||||
<< errinfo_comment("Type " + paramType.toString() + " not yet supported."));
|
||||
m_context << eth::dupInstruction(parameters.size() - i);
|
||||
if (numBytes != 32)
|
||||
m_context << (u256(1) << ((32 - numBytes) * 8)) << eth::Instruction::MUL;
|
||||
@ -272,7 +277,8 @@ bool Compiler::visit(Return& _return)
|
||||
ExpressionCompiler::compileExpression(m_context, *expression);
|
||||
VariableDeclaration const& firstVariable = *_return.getFunctionReturnParameters().getParameters().front();
|
||||
ExpressionCompiler::appendTypeConversion(m_context, *expression->getType(), *firstVariable.getType());
|
||||
int stackPosition = m_context.getStackPositionOfVariable(firstVariable);
|
||||
|
||||
unsigned stackPosition = m_context.baseToCurrentStackOffset(m_context.getBaseStackOffsetOfVariable(firstVariable));
|
||||
m_context << eth::swapInstruction(stackPosition) << eth::Instruction::POP;
|
||||
}
|
||||
m_context.appendJumpTo(m_returnTag);
|
||||
@ -287,7 +293,8 @@ bool Compiler::visit(VariableDefinition& _variableDefinition)
|
||||
ExpressionCompiler::appendTypeConversion(m_context,
|
||||
*expression->getType(),
|
||||
*_variableDefinition.getDeclaration().getType());
|
||||
int stackPosition = m_context.getStackPositionOfVariable(_variableDefinition.getDeclaration());
|
||||
unsigned baseStackOffset = m_context.getBaseStackOffsetOfVariable(_variableDefinition.getDeclaration());
|
||||
unsigned stackPosition = m_context.baseToCurrentStackOffset(baseStackOffset);
|
||||
m_context << eth::swapInstruction(stackPosition) << eth::Instruction::POP;
|
||||
}
|
||||
return false;
|
||||
|
@ -30,6 +30,12 @@ using namespace std;
|
||||
namespace dev {
|
||||
namespace solidity {
|
||||
|
||||
void CompilerContext::addStateVariable(VariableDeclaration const& _declaration)
|
||||
{
|
||||
m_stateVariables[&_declaration] = m_stateVariablesSize;
|
||||
m_stateVariablesSize += _declaration.getType()->getStorageSize();
|
||||
}
|
||||
|
||||
void CompilerContext::initializeLocalVariables(unsigned _numVariables)
|
||||
{
|
||||
if (_numVariables > 0)
|
||||
@ -41,12 +47,9 @@ void CompilerContext::initializeLocalVariables(unsigned _numVariables)
|
||||
}
|
||||
}
|
||||
|
||||
int CompilerContext::getStackPositionOfVariable(Declaration const& _declaration)
|
||||
bool CompilerContext::isLocalVariable(Declaration const* _declaration) const
|
||||
{
|
||||
auto res = find(begin(m_localVariables), end(m_localVariables), &_declaration);
|
||||
if (asserts(res != m_localVariables.end()))
|
||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Variable not found on stack."));
|
||||
return end(m_localVariables) - res - 1 + m_asm.deposit();
|
||||
return std::find(m_localVariables.begin(), m_localVariables.end(), _declaration) != m_localVariables.end();
|
||||
}
|
||||
|
||||
eth::AssemblyItem CompilerContext::getFunctionEntryLabel(FunctionDefinition const& _function) const
|
||||
@ -57,5 +60,28 @@ eth::AssemblyItem CompilerContext::getFunctionEntryLabel(FunctionDefinition cons
|
||||
return res->second.tag();
|
||||
}
|
||||
|
||||
unsigned CompilerContext::getBaseStackOffsetOfVariable(Declaration const& _declaration) const
|
||||
{
|
||||
auto res = find(begin(m_localVariables), end(m_localVariables), &_declaration);
|
||||
if (asserts(res != m_localVariables.end()))
|
||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Variable not found on stack."));
|
||||
return unsigned(end(m_localVariables) - res - 1);
|
||||
}
|
||||
|
||||
unsigned CompilerContext::baseToCurrentStackOffset(unsigned _baseOffset) const
|
||||
{
|
||||
return _baseOffset + m_asm.deposit();
|
||||
}
|
||||
|
||||
u256 CompilerContext::getStorageLocationOfVariable(const Declaration& _declaration) const
|
||||
{
|
||||
auto it = m_stateVariables.find(&_declaration);
|
||||
if (it == m_stateVariables.end())
|
||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Variable not found in storage."));
|
||||
return it->second;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -38,19 +38,28 @@ namespace solidity {
|
||||
class CompilerContext
|
||||
{
|
||||
public:
|
||||
CompilerContext() {}
|
||||
CompilerContext(): m_stateVariablesSize(0) {}
|
||||
|
||||
void addStateVariable(VariableDeclaration const& _declaration);
|
||||
void startNewFunction() { m_localVariables.clear(); m_asm.setDeposit(0); }
|
||||
void initializeLocalVariables(unsigned _numVariables);
|
||||
void addVariable(VariableDeclaration const& _declaration) { m_localVariables.push_back(&_declaration); }
|
||||
/// Returns the distance of the given local variable from the top of the stack.
|
||||
int getStackPositionOfVariable(Declaration const& _declaration);
|
||||
|
||||
void addFunction(FunctionDefinition const& _function) { m_functionEntryLabels.insert(std::make_pair(&_function, m_asm.newTag())); }
|
||||
eth::AssemblyItem getFunctionEntryLabel(FunctionDefinition const& _function) const;
|
||||
|
||||
void adjustStackOffset(int _adjustment) { m_asm.adjustDeposit(_adjustment); }
|
||||
|
||||
bool isFunctionDefinition(Declaration const* _declaration) const { return m_functionEntryLabels.count(_declaration); }
|
||||
bool isLocalVariable(Declaration const* _declaration) const;
|
||||
bool isStateVariable(Declaration const* _declaration) const { return m_stateVariables.count(_declaration); }
|
||||
|
||||
eth::AssemblyItem getFunctionEntryLabel(FunctionDefinition const& _function) const;
|
||||
/// Returns the distance of the given local variable from the top of the local variable stack.
|
||||
unsigned getBaseStackOffsetOfVariable(Declaration const& _declaration) const;
|
||||
/// If supplied by a value returned by @ref getBaseStackOffsetOfVariable(variable), returns
|
||||
/// the distance of that variable from the current top of the stack.
|
||||
unsigned baseToCurrentStackOffset(unsigned _baseOffset) const;
|
||||
u256 getStorageLocationOfVariable(Declaration const& _declaration) const;
|
||||
|
||||
/// Appends a JUMPI instruction to a new tag and @returns the tag
|
||||
eth::AssemblyItem appendConditionalJump() { return m_asm.appendJumpI().tag(); }
|
||||
/// Appends a JUMPI instruction to @a _tag
|
||||
@ -79,10 +88,14 @@ public:
|
||||
private:
|
||||
eth::Assembly m_asm;
|
||||
|
||||
/// Size of the state variables, offset of next variable to be added.
|
||||
u256 m_stateVariablesSize;
|
||||
/// Storage offsets of state variables
|
||||
std::map<Declaration const*, u256> m_stateVariables;
|
||||
/// Offsets of local variables on the stack.
|
||||
std::vector<Declaration const*> m_localVariables;
|
||||
/// Labels pointing to the entry points of funcitons.
|
||||
std::map<FunctionDefinition const*, eth::AssemblyItem> m_functionEntryLabels;
|
||||
std::map<Declaration const*, eth::AssemblyItem> m_functionEntryLabels;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -46,12 +46,12 @@ void ExpressionCompiler::appendTypeConversion(CompilerContext& _context,
|
||||
|
||||
bool ExpressionCompiler::visit(Assignment& _assignment)
|
||||
{
|
||||
m_currentLValue = nullptr;
|
||||
|
||||
Expression& rightHandSide = _assignment.getRightHandSide();
|
||||
rightHandSide.accept(*this);
|
||||
Type const& resultType = *_assignment.getType();
|
||||
appendTypeConversion(*rightHandSide.getType(), resultType);
|
||||
|
||||
m_currentLValue.reset();
|
||||
_assignment.getLeftHandSide().accept(*this);
|
||||
|
||||
Token::Value op = _assignment.getAssignmentOperator();
|
||||
@ -99,10 +99,7 @@ void ExpressionCompiler::endVisit(UnaryOperation& _unaryOperation)
|
||||
m_context << eth::Instruction::ADD;
|
||||
else
|
||||
m_context << eth::Instruction::SWAP1 << eth::Instruction::SUB; // @todo avoid the swap
|
||||
if (_unaryOperation.isPrefixOperation())
|
||||
storeInLValue(_unaryOperation);
|
||||
else
|
||||
moveToLValue(_unaryOperation);
|
||||
storeInLValue(_unaryOperation, !_unaryOperation.isPrefixOperation());
|
||||
break;
|
||||
case Token::ADD: // +
|
||||
// unary add, so basically no-op
|
||||
@ -164,9 +161,13 @@ bool ExpressionCompiler::visit(FunctionCall& _functionCall)
|
||||
{
|
||||
// Calling convention: Caller pushes return address and arguments
|
||||
// Callee removes them and pushes return values
|
||||
m_currentLValue = nullptr;
|
||||
m_currentLValue.reset();
|
||||
_functionCall.getExpression().accept(*this);
|
||||
FunctionDefinition const& function = dynamic_cast<FunctionDefinition&>(*m_currentLValue);
|
||||
if (asserts(m_currentLValue.isInCode()))
|
||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Code reference expected."));
|
||||
eth::AssemblyItem functionTag(eth::PushTag, m_currentLValue.location);
|
||||
|
||||
FunctionDefinition const& function = dynamic_cast<FunctionType const&>(*_functionCall.getExpression().getType()).getFunction();
|
||||
|
||||
eth::AssemblyItem returnLabel = m_context.pushNewTag();
|
||||
std::vector<ASTPointer<Expression>> const& arguments = _functionCall.getArguments();
|
||||
@ -179,7 +180,7 @@ bool ExpressionCompiler::visit(FunctionCall& _functionCall)
|
||||
*function.getParameters()[i]->getType());
|
||||
}
|
||||
|
||||
m_context.appendJumpTo(m_context.getFunctionEntryLabel(function));
|
||||
m_context.appendJumpTo(functionTag);
|
||||
m_context << returnLabel;
|
||||
|
||||
// callee adds return parameters, but removes arguments and return label
|
||||
@ -205,24 +206,20 @@ void ExpressionCompiler::endVisit(IndexAccess&)
|
||||
|
||||
void ExpressionCompiler::endVisit(Identifier& _identifier)
|
||||
{
|
||||
m_currentLValue = _identifier.getReferencedDeclaration();
|
||||
switch (_identifier.getType()->getCategory())
|
||||
{
|
||||
case Type::Category::BOOL:
|
||||
case Type::Category::INTEGER:
|
||||
case Type::Category::REAL:
|
||||
{
|
||||
//@todo we also have to check where to retrieve them from once we add storage variables
|
||||
unsigned stackPos = stackPositionOfLValue();
|
||||
if (stackPos >= 15) //@todo correct this by fetching earlier or moving to memory
|
||||
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_identifier.getLocation())
|
||||
<< errinfo_comment("Stack too deep."));
|
||||
m_context << eth::dupInstruction(stackPos + 1);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
Declaration const* declaration = _identifier.getReferencedDeclaration();
|
||||
if (m_context.isLocalVariable(declaration))
|
||||
m_currentLValue = LValueLocation(LValueLocation::STACK,
|
||||
m_context.getBaseStackOffsetOfVariable(*declaration));
|
||||
else if (m_context.isStateVariable(declaration))
|
||||
m_currentLValue = LValueLocation(LValueLocation::STORAGE,
|
||||
m_context.getStorageLocationOfVariable(*declaration));
|
||||
else if (m_context.isFunctionDefinition(declaration))
|
||||
m_currentLValue = LValueLocation(LValueLocation::CODE,
|
||||
m_context.getFunctionEntryLabel(dynamic_cast<FunctionDefinition const&>(*declaration)).data());
|
||||
else
|
||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Identifier type not supported or identifier not found."));
|
||||
|
||||
retrieveLValueValue(_identifier);
|
||||
}
|
||||
|
||||
void ExpressionCompiler::endVisit(Literal& _literal)
|
||||
@ -388,31 +385,65 @@ void ExpressionCompiler::appendHighBitsCleanup(IntegerType const& _typeOnStack)
|
||||
m_context << ((u256(1) << _typeOnStack.getNumBits()) - 1) << eth::Instruction::AND;
|
||||
}
|
||||
|
||||
void ExpressionCompiler::storeInLValue(Expression const& _expression)
|
||||
void ExpressionCompiler::retrieveLValueValue(Expression const& _expression)
|
||||
{
|
||||
moveToLValue(_expression);
|
||||
unsigned stackPos = stackPositionOfLValue();
|
||||
if (stackPos > 16)
|
||||
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_expression.getLocation())
|
||||
<< errinfo_comment("Stack too deep."));
|
||||
m_context << eth::dupInstruction(stackPos + 1);
|
||||
switch (m_currentLValue.locationType)
|
||||
{
|
||||
case LValueLocation::CODE:
|
||||
// not stored on the stack
|
||||
break;
|
||||
case LValueLocation::STACK:
|
||||
{
|
||||
unsigned stackPos = m_context.baseToCurrentStackOffset(unsigned(m_currentLValue.location));
|
||||
if (stackPos >= 15) //@todo correct this by fetching earlier or moving to memory
|
||||
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_expression.getLocation())
|
||||
<< errinfo_comment("Stack too deep."));
|
||||
m_context << eth::dupInstruction(stackPos + 1);
|
||||
break;
|
||||
}
|
||||
case LValueLocation::STORAGE:
|
||||
m_context << m_currentLValue.location << eth::Instruction::SLOAD;
|
||||
break;
|
||||
case LValueLocation::MEMORY:
|
||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Location type not yet implemented."));
|
||||
break;
|
||||
default:
|
||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unsupported location type."));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ExpressionCompiler::moveToLValue(Expression const& _expression)
|
||||
void ExpressionCompiler::storeInLValue(Expression const& _expression, bool _move)
|
||||
{
|
||||
unsigned stackPos = stackPositionOfLValue();
|
||||
if (stackPos > 16)
|
||||
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_expression.getLocation())
|
||||
<< errinfo_comment("Stack too deep."));
|
||||
else if (stackPos > 0)
|
||||
m_context << eth::swapInstruction(stackPos) << eth::Instruction::POP;
|
||||
}
|
||||
|
||||
unsigned ExpressionCompiler::stackPositionOfLValue() const
|
||||
{
|
||||
if (asserts(m_currentLValue))
|
||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("LValue not available on request."));
|
||||
return m_context.getStackPositionOfVariable(*m_currentLValue);
|
||||
switch (m_currentLValue.locationType)
|
||||
{
|
||||
case LValueLocation::STACK:
|
||||
{
|
||||
unsigned stackPos = m_context.baseToCurrentStackOffset(unsigned(m_currentLValue.location));
|
||||
if (stackPos > 16)
|
||||
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_expression.getLocation())
|
||||
<< errinfo_comment("Stack too deep."));
|
||||
else if (stackPos > 0)
|
||||
m_context << eth::swapInstruction(stackPos) << eth::Instruction::POP;
|
||||
if (!_move)
|
||||
retrieveLValueValue(_expression);
|
||||
break;
|
||||
}
|
||||
case LValueLocation::STORAGE:
|
||||
if (!_move)
|
||||
m_context << eth::Instruction::DUP1;
|
||||
m_context << m_currentLValue.location << eth::Instruction::SSTORE;
|
||||
break;
|
||||
case LValueLocation::CODE:
|
||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Location type does not support assignment."));
|
||||
break;
|
||||
case LValueLocation::MEMORY:
|
||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Location type not yet implemented."));
|
||||
break;
|
||||
default:
|
||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unsupported location type."));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,18 +20,25 @@
|
||||
* Solidity AST to EVM bytecode compiler for expressions.
|
||||
*/
|
||||
|
||||
#include <libdevcore/Common.h>
|
||||
#include <libsolidity/ASTVisitor.h>
|
||||
|
||||
namespace dev {
|
||||
namespace eth
|
||||
{
|
||||
class AssemblyItem; // forward
|
||||
}
|
||||
namespace solidity {
|
||||
|
||||
class CompilerContext; // forward
|
||||
class Type; // forward
|
||||
class IntegerType; // forward
|
||||
|
||||
/// Compiler for expressions, i.e. converts an AST tree whose root is an Expression into a stream
|
||||
/// of EVM instructions. It needs a compiler context that is the same for the whole compilation
|
||||
/// unit.
|
||||
/**
|
||||
* Compiler for expressions, i.e. converts an AST tree whose root is an Expression into a stream
|
||||
* of EVM instructions. It needs a compiler context that is the same for the whole compilation
|
||||
* unit.
|
||||
*/
|
||||
class ExpressionCompiler: private ASTVisitor
|
||||
{
|
||||
public:
|
||||
@ -42,7 +49,7 @@ public:
|
||||
static void appendTypeConversion(CompilerContext& _context, Type const& _typeOnStack, Type const& _targetType);
|
||||
|
||||
private:
|
||||
ExpressionCompiler(CompilerContext& _compilerContext): m_currentLValue(nullptr), m_context(_compilerContext) {}
|
||||
ExpressionCompiler(CompilerContext& _compilerContext): m_context(_compilerContext) {}
|
||||
|
||||
virtual bool visit(Assignment& _assignment) override;
|
||||
virtual void endVisit(UnaryOperation& _unaryOperation) override;
|
||||
@ -72,15 +79,36 @@ private:
|
||||
//// Appends code that cleans higher-order bits for integer types.
|
||||
void appendHighBitsCleanup(IntegerType const& _typeOnStack);
|
||||
|
||||
/// Stores the value on top of the stack in the current lvalue and copies that value to the
|
||||
/// top of the stack again
|
||||
void storeInLValue(Expression const& _expression);
|
||||
/// The same as storeInLValue but do not again retrieve the value to the top of the stack.
|
||||
void moveToLValue(Expression const& _expression);
|
||||
/// Returns the position of @a m_currentLValue in the stack, where 0 is the top of the stack.
|
||||
unsigned stackPositionOfLValue() const;
|
||||
/// Copies the value of the current lvalue to the top of the stack.
|
||||
void retrieveLValueValue(Expression const& _expression);
|
||||
/// Stores the value on top of the stack in the current lvalue. Removes it from the stack if
|
||||
/// @a _move is true.
|
||||
void storeInLValue(Expression const& _expression, bool _move = false);
|
||||
|
||||
Declaration* m_currentLValue;
|
||||
/**
|
||||
* Location of an lvalue, either in code (for a function) on the stack, in the storage or memory.
|
||||
*/
|
||||
struct LValueLocation
|
||||
{
|
||||
enum LocationType { INVALID, CODE, STACK, MEMORY, STORAGE };
|
||||
|
||||
LValueLocation() { reset(); }
|
||||
LValueLocation(LocationType _type, u256 const& _location): locationType(_type), location(_location) {}
|
||||
void reset() { locationType = INVALID; location = 0; }
|
||||
bool isValid() const { return locationType != INVALID; }
|
||||
bool isInCode() const { return locationType == CODE; }
|
||||
bool isInOnStack() const { return locationType == STACK; }
|
||||
bool isInMemory() const { return locationType == MEMORY; }
|
||||
bool isInStorage() const { return locationType == STORAGE; }
|
||||
|
||||
LocationType locationType;
|
||||
/// Depending on the type, this is the id of a tag (code), the base offset of a stack
|
||||
/// variable (@see CompilerContext::getBaseStackOffsetOfVariable) or the offset in
|
||||
/// storage or memory.
|
||||
u256 location;
|
||||
};
|
||||
|
||||
LValueLocation m_currentLValue;
|
||||
CompilerContext& m_context;
|
||||
};
|
||||
|
||||
|
16
Types.cpp
16
Types.cpp
@ -207,6 +207,14 @@ bool ContractType::operator==(Type const& _other) const
|
||||
return other.m_contract == m_contract;
|
||||
}
|
||||
|
||||
u256 ContractType::getStorageSize() const
|
||||
{
|
||||
u256 size = 0;
|
||||
for (ASTPointer<VariableDeclaration> const& variable: m_contract.getStateVariables())
|
||||
size += variable->getType()->getStorageSize();
|
||||
return max<u256>(1, size);
|
||||
}
|
||||
|
||||
bool StructType::operator==(Type const& _other) const
|
||||
{
|
||||
if (_other.getCategory() != getCategory())
|
||||
@ -215,6 +223,14 @@ bool StructType::operator==(Type const& _other) const
|
||||
return other.m_struct == m_struct;
|
||||
}
|
||||
|
||||
u256 StructType::getStorageSize() const
|
||||
{
|
||||
u256 size = 0;
|
||||
for (ASTPointer<VariableDeclaration> const& variable: m_struct.getMembers())
|
||||
size += variable->getType()->getStorageSize();
|
||||
return max<u256>(1, size);
|
||||
}
|
||||
|
||||
bool FunctionType::operator==(Type const& _other) const
|
||||
{
|
||||
if (_other.getCategory() != getCategory())
|
||||
|
16
Types.h
16
Types.h
@ -75,6 +75,9 @@ public:
|
||||
/// @returns number of bytes used by this type when encoded for CALL, or 0 if the encoding
|
||||
/// is not a simple big-endian encoding or the type cannot be stored on the stack.
|
||||
virtual unsigned getCalldataEncodedSize() const { return 0; }
|
||||
/// @returns number of bytes required to hold this value in storage.
|
||||
/// For dynamically "allocated" types, it returns the size of the statically allocated head,
|
||||
virtual u256 getStorageSize() const { return 1; }
|
||||
|
||||
virtual std::string toString() const = 0;
|
||||
virtual u256 literalValue(Literal const&) const
|
||||
@ -157,7 +160,7 @@ public:
|
||||
ContractType(ContractDefinition const& _contract): m_contract(_contract) {}
|
||||
|
||||
virtual bool operator==(Type const& _other) const override;
|
||||
|
||||
virtual u256 getStorageSize() const;
|
||||
virtual std::string toString() const override { return "contract{...}"; }
|
||||
|
||||
private:
|
||||
@ -178,7 +181,7 @@ public:
|
||||
}
|
||||
|
||||
virtual bool operator==(Type const& _other) const override;
|
||||
|
||||
virtual u256 getStorageSize() const;
|
||||
virtual std::string toString() const override { return "struct{...}"; }
|
||||
|
||||
private:
|
||||
@ -196,9 +199,9 @@ public:
|
||||
|
||||
FunctionDefinition const& getFunction() const { return m_function; }
|
||||
|
||||
virtual std::string toString() const override { return "function(...)returns(...)"; }
|
||||
|
||||
virtual bool operator==(Type const& _other) const override;
|
||||
virtual std::string toString() const override { return "function(...)returns(...)"; }
|
||||
virtual u256 getStorageSize() const { BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage size of non-storable function type requested.")); return 1; }
|
||||
|
||||
private:
|
||||
FunctionDefinition const& m_function;
|
||||
@ -212,9 +215,9 @@ class MappingType: public Type
|
||||
public:
|
||||
virtual Category getCategory() const override { return Category::MAPPING; }
|
||||
MappingType() {}
|
||||
virtual std::string toString() const override { return "mapping(...=>...)"; }
|
||||
|
||||
virtual bool operator==(Type const& _other) const override;
|
||||
virtual std::string toString() const override { return "mapping(...=>...)"; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<Type const> m_keyType;
|
||||
@ -232,6 +235,7 @@ public:
|
||||
VoidType() {}
|
||||
|
||||
virtual std::string toString() const override { return "void"; }
|
||||
virtual u256 getStorageSize() const { BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage size of non-storable void type requested.")); return 1; }
|
||||
};
|
||||
|
||||
/**
|
||||
@ -247,7 +251,7 @@ public:
|
||||
std::shared_ptr<Type const> const& getActualType() const { return m_actualType; }
|
||||
|
||||
virtual bool operator==(Type const& _other) const override;
|
||||
|
||||
virtual u256 getStorageSize() const { BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage size of non-storable type type requested.")); return 1; }
|
||||
virtual std::string toString() const override { return "type(" + m_actualType->toString() + ")"; }
|
||||
|
||||
private:
|
||||
|
Loading…
Reference in New Issue
Block a user