solidity/libsolidity/ExpressionCompiler.h

138 lines
5.3 KiB
C
Raw Normal View History

/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Christian <c@ethdev.com>
2015-01-09 06:39:30 +00:00
* @author Gav Wood <g@ethdev.com>
* @date 2014
* Solidity AST to EVM bytecode compiler for expressions.
*/
2014-12-10 22:01:40 +00:00
#include <functional>
2015-02-12 17:38:07 +00:00
#include <memory>
2014-11-10 16:31:09 +00:00
#include <boost/noncopyable.hpp>
2014-11-07 01:06:37 +00:00
#include <libdevcore/Common.h>
#include <libevmasm/SourceLocation.h>
#include <libsolidity/ASTVisitor.h>
2015-02-25 14:14:22 +00:00
#include <libsolidity/LValue.h>
#include <libsolidity/Utils.h>
namespace dev {
2014-11-07 01:06:37 +00:00
namespace eth
{
class AssemblyItem; // forward
}
namespace solidity {
2014-12-04 18:38:24 +00:00
// forward declarations
class CompilerContext;
class CompilerUtils;
2014-12-04 18:38:24 +00:00
class Type;
class IntegerType;
class ArrayType;
2014-10-30 11:42:04 +00:00
2014-11-07 01:06:37 +00:00
/**
* 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 ASTConstVisitor
{
public:
/// Appends code for a State Variable accessor function
static void appendStateVariableAccessor(CompilerContext& _context, VariableDeclaration const& _varDecl, bool _optimize = false);
explicit ExpressionCompiler(CompilerContext& _compilerContext, bool _optimize = false):
2015-02-25 14:14:22 +00:00
m_optimize(_optimize), m_context(_compilerContext) {}
/// Compile the given @a _expression and leave its value on the stack.
void compile(Expression const& _expression);
/// Appends code to set a state variable to its initial value/expression.
void appendStateVariableInitialization(VariableDeclaration const& _varDecl);
/// Appends code for a State Variable accessor function
void appendStateVariableAccessor(VariableDeclaration const& _varDecl);
/// Appends code for a Constant State Variable accessor function
void appendConstStateVariableAccessor(const VariableDeclaration& _varDecl);
2015-02-25 14:14:22 +00:00
private:
2014-12-06 00:06:24 +00:00
virtual bool visit(Assignment const& _assignment) override;
2014-12-19 10:31:17 +00:00
virtual bool visit(UnaryOperation const& _unaryOperation) override;
2014-12-06 00:06:24 +00:00
virtual bool visit(BinaryOperation const& _binaryOperation) override;
virtual bool visit(FunctionCall const& _functionCall) override;
2014-12-12 15:49:26 +00:00
virtual bool visit(NewExpression const& _newExpression) override;
2014-12-06 00:06:24 +00:00
virtual void endVisit(MemberAccess const& _memberAccess) override;
virtual bool visit(IndexAccess const& _indexAccess) override;
virtual void endVisit(Identifier const& _identifier) override;
virtual void endVisit(Literal const& _literal) override;
///@{
///@name Append code for various operator types
2014-12-06 00:06:24 +00:00
void appendAndOrOperatorCode(BinaryOperation const& _binaryOperation);
void appendCompareOperatorCode(Token::Value _operator, Type const& _type);
void appendOrdinaryBinaryOperatorCode(Token::Value _operator, Type const& _type);
void appendArithmeticOperatorCode(Token::Value _operator, Type const& _type);
void appendBitOperatorCode(Token::Value _operator);
void appendShiftOperatorCode(Token::Value _operator);
/// @}
2014-12-10 22:01:40 +00:00
/// Appends code to call a function of the given type with the given arguments.
void appendExternalFunctionCall(
FunctionType const& _functionType,
std::vector<ASTPointer<Expression const>> const& _arguments
);
2015-02-10 16:53:43 +00:00
/// Appends code that evaluates a single expression and moves the result to memory. The memory offset is
/// expected to be on the stack and is updated by this call.
void appendExpressionCopyToMemory(Type const& _expectedType, Expression const& _expression);
2014-12-10 22:01:40 +00:00
2015-02-25 14:14:22 +00:00
/// Sets the current LValue to a new one (of the appropriate type) from the given declaration.
/// Also retrieves the value if it was not requested by @a _expression.
void setLValueFromDeclaration(Declaration const& _declaration, Expression const& _expression);
/// Sets the current LValue to a StorageItem holding the type of @a _expression. The reference is assumed
/// to be on the stack.
/// Also retrieves the value if it was not requested by @a _expression.
void setLValueToStorageItem(Expression const& _expression);
2015-02-25 14:55:42 +00:00
/// Sets the current LValue to a new LValue constructed from the arguments.
/// Also retrieves the value if it was not requested by @a _expression.
template <class _LValueType, class... _Arguments>
void setLValue(Expression const& _expression, _Arguments const&... _arguments);
2014-11-07 01:06:37 +00:00
/// @returns the CompilerUtils object containing the current context.
CompilerUtils utils();
bool m_optimize;
CompilerContext& m_context;
2015-02-25 14:14:22 +00:00
std::unique_ptr<LValue> m_currentLValue;
};
2015-02-25 14:55:42 +00:00
template <class _LValueType, class... _Arguments>
void ExpressionCompiler::setLValue(Expression const& _expression, _Arguments const&... _arguments)
{
2015-02-25 15:00:23 +00:00
solAssert(!m_currentLValue, "Current LValue not reset before trying to set new one.");
2015-02-25 14:55:42 +00:00
std::unique_ptr<_LValueType> lvalue(new _LValueType(m_context, _arguments...));
if (_expression.annotation().lValueRequested)
2015-02-25 14:55:42 +00:00
m_currentLValue = move(lvalue);
else
2015-08-31 16:44:29 +00:00
lvalue->retrieveValue(_expression.location(), true);
2015-02-25 14:55:42 +00:00
}
}
}