2014-10-30 00:20:32 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2014-10-30 00:20:32 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2014-10-30 00:20:32 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2014-10-30 00:20:32 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2014-10-30 00:20:32 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
2015-01-09 06:39:30 +00:00
|
|
|
* @author Gav Wood <g@ethdev.com>
|
2014-10-30 00:20:32 +00:00
|
|
|
* @date 2014
|
|
|
|
* Solidity AST to EVM bytecode compiler for expressions.
|
|
|
|
*/
|
|
|
|
|
2018-12-12 15:47:05 +00:00
|
|
|
#pragma once
|
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/ast/ASTVisitor.h>
|
|
|
|
#include <libsolidity/codegen/LValue.h>
|
2019-09-18 14:44:36 +00:00
|
|
|
#include <libsolidity/interface/DebugSettings.h>
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
2018-12-17 16:06:11 +00:00
|
|
|
#include <liblangutil/SourceLocation.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Common.h>
|
2018-12-17 16:06:11 +00:00
|
|
|
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::evmasm
|
2014-11-07 01:06:37 +00:00
|
|
|
{
|
|
|
|
class AssemblyItem; // forward
|
|
|
|
}
|
2019-12-11 16:31:36 +00:00
|
|
|
|
|
|
|
namespace solidity::frontend {
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2014-12-04 18:38:24 +00:00
|
|
|
// forward declarations
|
|
|
|
class CompilerContext;
|
2015-06-15 10:10:41 +00:00
|
|
|
class CompilerUtils;
|
2014-12-04 18:38:24 +00:00
|
|
|
class Type;
|
|
|
|
class IntegerType;
|
2015-02-20 14:52:30 +00:00
|
|
|
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.
|
|
|
|
*/
|
2014-12-06 01:19:10 +00:00
|
|
|
class ExpressionCompiler: private ASTConstVisitor
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-09-18 14:44:36 +00:00
|
|
|
ExpressionCompiler(
|
|
|
|
CompilerContext& _compilerContext,
|
|
|
|
bool _optimiseOrderLiterals
|
|
|
|
):
|
|
|
|
m_optimiseOrderLiterals(_optimiseOrderLiterals),
|
|
|
|
m_context(_compilerContext)
|
|
|
|
{}
|
2015-02-25 14:14:22 +00:00
|
|
|
|
|
|
|
/// 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);
|
|
|
|
|
2015-08-21 15:57:57 +00:00
|
|
|
/// Appends code for a Constant State Variable accessor function
|
2019-02-13 15:22:42 +00:00
|
|
|
void appendConstStateVariableAccessor(VariableDeclaration const& _varDecl);
|
2015-08-21 15:57:57 +00:00
|
|
|
|
2015-02-25 14:14:22 +00:00
|
|
|
private:
|
2018-11-16 01:09:04 +00:00
|
|
|
bool visit(Conditional const& _condition) override;
|
|
|
|
bool visit(Assignment const& _assignment) override;
|
|
|
|
bool visit(TupleExpression const& _tuple) override;
|
|
|
|
bool visit(UnaryOperation const& _unaryOperation) override;
|
|
|
|
bool visit(BinaryOperation const& _binaryOperation) override;
|
|
|
|
bool visit(FunctionCall const& _functionCall) override;
|
2020-01-22 14:42:50 +00:00
|
|
|
bool visit(FunctionCallOptions const& _functionCallOptions) override;
|
2018-11-16 01:09:04 +00:00
|
|
|
bool visit(NewExpression const& _newExpression) override;
|
|
|
|
bool visit(MemberAccess const& _memberAccess) override;
|
|
|
|
bool visit(IndexAccess const& _indexAccess) override;
|
2019-09-03 16:30:00 +00:00
|
|
|
bool visit(IndexRangeAccess const& _indexAccess) override;
|
2018-11-16 01:09:04 +00:00
|
|
|
void endVisit(Identifier const& _identifier) override;
|
|
|
|
void endVisit(Literal const& _literal) override;
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
///@{
|
|
|
|
///@name Append code for various operator types
|
2014-12-06 00:06:24 +00:00
|
|
|
void appendAndOrOperatorCode(BinaryOperation const& _binaryOperation);
|
2018-10-22 14:48:21 +00:00
|
|
|
void appendCompareOperatorCode(Token _operator, Type const& _type);
|
|
|
|
void appendOrdinaryBinaryOperatorCode(Token _operator, Type const& _type);
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
void appendArithmeticOperatorCode(Token _operator, Type const& _type);
|
|
|
|
void appendBitOperatorCode(Token _operator);
|
|
|
|
void appendShiftOperatorCode(Token _operator, Type const& _valueType, Type const& _shiftAmountType);
|
2020-07-23 14:16:39 +00:00
|
|
|
void appendExpOperatorCode(Type const& _valueType, Type const& _exponentType);
|
2014-10-30 00:20:32 +00:00
|
|
|
/// @}
|
|
|
|
|
2014-12-10 22:01:40 +00:00
|
|
|
/// Appends code to call a function of the given type with the given arguments.
|
2019-09-05 18:02:58 +00:00
|
|
|
/// @param _tryCall if true, this is the external call of a try statement. In that case,
|
|
|
|
/// returns success flag on top of stack and does not revert on failure.
|
2015-06-05 22:57:51 +00:00
|
|
|
void appendExternalFunctionCall(
|
|
|
|
FunctionType const& _functionType,
|
2019-09-05 18:02:58 +00:00
|
|
|
std::vector<ASTPointer<Expression const>> const& _arguments,
|
|
|
|
bool _tryCall
|
2015-06-05 22:57:51 +00:00
|
|
|
);
|
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
|
|
|
|
2016-10-21 18:16:21 +00:00
|
|
|
/// Appends code for a variable that might be a constant or not
|
|
|
|
void appendVariable(VariableDeclaration const& _variable, Expression const& _expression);
|
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.
|
2020-02-17 17:44:39 +00:00
|
|
|
template <class LValueType, class... Arguments>
|
|
|
|
void setLValue(Expression const& _expression, Arguments const&... _arguments);
|
2014-11-07 01:06:37 +00:00
|
|
|
|
2016-12-06 22:45:17 +00:00
|
|
|
/// @returns true if the operator applied to the given type requires a cleanup prior to the
|
|
|
|
/// operation.
|
2018-10-22 14:48:21 +00:00
|
|
|
static bool cleanupNeededForOp(Type::Category _type, Token _op);
|
2016-12-06 22:45:17 +00:00
|
|
|
|
2019-09-05 11:00:07 +00:00
|
|
|
void acceptAndConvert(Expression const& _expression, Type const& _type, bool _cleanupNeeded = false);
|
|
|
|
|
2015-06-15 10:10:41 +00:00
|
|
|
/// @returns the CompilerUtils object containing the current context.
|
|
|
|
CompilerUtils utils();
|
|
|
|
|
2017-07-17 11:12:00 +00:00
|
|
|
bool m_optimiseOrderLiterals;
|
2014-10-30 00:20:32 +00:00
|
|
|
CompilerContext& m_context;
|
2015-02-25 14:14:22 +00:00
|
|
|
std::unique_ptr<LValue> m_currentLValue;
|
2015-06-15 10:10:41 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
};
|
|
|
|
|
2020-02-17 17:44:39 +00:00
|
|
|
template <class LValueType, class... Arguments>
|
|
|
|
void ExpressionCompiler::setLValue(Expression const& _expression, Arguments const&... _arguments)
|
2015-02-25 14:55:42 +00:00
|
|
|
{
|
2015-02-25 15:00:23 +00:00
|
|
|
solAssert(!m_currentLValue, "Current LValue not reset before trying to set new one.");
|
2020-02-17 17:44:39 +00:00
|
|
|
std::unique_ptr<LValueType> lvalue = std::make_unique<LValueType>(m_context, _arguments...);
|
2020-04-09 10:48:57 +00:00
|
|
|
if (_expression.annotation().willBeWrittenTo)
|
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
|
|
|
}
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
}
|