2019-03-18 10:21:41 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity 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.
|
|
|
|
|
|
|
|
solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2019-03-18 10:21:41 +00:00
|
|
|
/**
|
|
|
|
* Component that translates Solidity code into Yul at statement level and below.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <libsolidity/ast/ASTVisitor.h>
|
2019-04-30 16:32:56 +00:00
|
|
|
#include <libsolidity/codegen/ir/IRLValue.h>
|
2020-02-06 13:05:25 +00:00
|
|
|
#include <libsolidity/codegen/ir/IRVariable.h>
|
2019-03-18 10:21:41 +00:00
|
|
|
|
2020-02-10 10:58:36 +00:00
|
|
|
#include <functional>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::frontend
|
2019-03-18 10:21:41 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
class IRGenerationContext;
|
|
|
|
class YulUtilFunctions;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component that translates Solidity's AST into Yul at statement level and below.
|
|
|
|
* It is an AST visitor that appends to an internal string buffer.
|
|
|
|
*/
|
|
|
|
class IRGeneratorForStatements: public ASTConstVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IRGeneratorForStatements(IRGenerationContext& _context, YulUtilFunctions& _utils):
|
|
|
|
m_context(_context),
|
|
|
|
m_utils(_utils)
|
|
|
|
{}
|
|
|
|
|
2019-04-30 16:32:56 +00:00
|
|
|
std::string code() const;
|
2019-03-18 10:21:41 +00:00
|
|
|
|
2020-09-11 10:02:10 +00:00
|
|
|
/// Generate the code for the statements in the block;
|
|
|
|
void generate(Block const& _block);
|
|
|
|
|
2019-05-20 10:01:47 +00:00
|
|
|
/// Generates code to initialize the given state variable.
|
|
|
|
void initializeStateVar(VariableDeclaration const& _varDecl);
|
2020-03-02 20:42:46 +00:00
|
|
|
/// Generates code to initialize the given local variable.
|
|
|
|
void initializeLocalVar(VariableDeclaration const& _varDecl);
|
2019-05-20 10:01:47 +00:00
|
|
|
|
2020-04-06 15:26:59 +00:00
|
|
|
/// Calculates expression's value and returns variable where it was stored
|
|
|
|
IRVariable evaluateExpression(Expression const& _expression, Type const& _to);
|
|
|
|
|
2020-04-20 21:05:14 +00:00
|
|
|
/// @returns the name of a function that computes the value of the given constant
|
|
|
|
/// and also generates the function.
|
|
|
|
std::string constantValueFunction(VariableDeclaration const& _constant);
|
|
|
|
|
2019-05-02 19:22:23 +00:00
|
|
|
void endVisit(VariableDeclarationStatement const& _variableDeclaration) override;
|
2020-02-13 19:42:16 +00:00
|
|
|
bool visit(Conditional const& _conditional) override;
|
2019-03-18 10:21:41 +00:00
|
|
|
bool visit(Assignment const& _assignment) override;
|
2019-05-02 16:28:33 +00:00
|
|
|
bool visit(TupleExpression const& _tuple) override;
|
2019-05-09 09:09:28 +00:00
|
|
|
bool visit(IfStatement const& _ifStatement) override;
|
2019-04-24 22:01:13 +00:00
|
|
|
bool visit(ForStatement const& _forStatement) override;
|
2019-05-08 11:27:36 +00:00
|
|
|
bool visit(WhileStatement const& _whileStatement) override;
|
2019-04-24 22:01:13 +00:00
|
|
|
bool visit(Continue const& _continueStatement) override;
|
|
|
|
bool visit(Break const& _breakStatement) override;
|
2019-05-02 19:22:23 +00:00
|
|
|
void endVisit(Return const& _return) override;
|
2019-05-02 12:25:15 +00:00
|
|
|
void endVisit(UnaryOperation const& _unaryOperation) override;
|
2019-05-02 16:09:19 +00:00
|
|
|
bool visit(BinaryOperation const& _binOp) override;
|
2020-05-19 19:50:22 +00:00
|
|
|
bool visit(FunctionCall const& _funCall) override;
|
2019-05-02 19:22:23 +00:00
|
|
|
void endVisit(FunctionCall const& _funCall) override;
|
2020-01-23 09:53:04 +00:00
|
|
|
void endVisit(FunctionCallOptions const& _funCallOptions) override;
|
2019-05-02 19:17:06 +00:00
|
|
|
void endVisit(MemberAccess const& _memberAccess) override;
|
2019-04-11 10:58:27 +00:00
|
|
|
bool visit(InlineAssembly const& _inlineAsm) override;
|
2019-05-02 10:48:34 +00:00
|
|
|
void endVisit(IndexAccess const& _indexAccess) override;
|
2019-09-03 16:30:00 +00:00
|
|
|
void endVisit(IndexRangeAccess const& _indexRangeAccess) override;
|
2019-05-02 19:17:06 +00:00
|
|
|
void endVisit(Identifier const& _identifier) override;
|
2019-04-02 10:37:48 +00:00
|
|
|
bool visit(Literal const& _literal) override;
|
2019-03-18 10:21:41 +00:00
|
|
|
|
2020-02-10 10:58:36 +00:00
|
|
|
bool visit(TryStatement const& _tryStatement) override;
|
|
|
|
bool visit(TryCatchClause const& _tryCatchClause) override;
|
|
|
|
|
2019-03-18 10:21:41 +00:00
|
|
|
private:
|
2020-02-10 10:58:36 +00:00
|
|
|
/// Handles all catch cases of a try statement, except the success-case.
|
|
|
|
void handleCatch(TryStatement const& _tryStatement);
|
|
|
|
void handleCatchStructuredAndFallback(
|
|
|
|
TryCatchClause const& _structured,
|
|
|
|
TryCatchClause const* _fallback
|
|
|
|
);
|
|
|
|
void handleCatchFallback(TryCatchClause const& _fallback);
|
|
|
|
|
|
|
|
/// Generates code to rethrow an exception.
|
|
|
|
void rethrow();
|
|
|
|
|
2020-04-16 12:21:12 +00:00
|
|
|
void handleVariableReference(
|
|
|
|
VariableDeclaration const& _variable,
|
|
|
|
Expression const& _referencingExpression
|
|
|
|
);
|
|
|
|
|
2019-05-06 17:48:31 +00:00
|
|
|
/// Appends code to call an external function with the given arguments.
|
|
|
|
/// All involved expressions have already been visited.
|
|
|
|
void appendExternalFunctionCall(
|
|
|
|
FunctionCall const& _functionCall,
|
|
|
|
std::vector<ASTPointer<Expression const>> const& _arguments
|
|
|
|
);
|
|
|
|
|
2020-05-04 10:50:31 +00:00
|
|
|
/// Appends code for .call / .delegatecall / .staticcall.
|
|
|
|
/// All involved expressions have already been visited.
|
|
|
|
void appendBareCall(
|
|
|
|
FunctionCall const& _functionCall,
|
|
|
|
std::vector<ASTPointer<Expression const>> const& _arguments
|
|
|
|
);
|
|
|
|
|
2020-02-10 10:44:52 +00:00
|
|
|
/// @returns code that evaluates to the first unused memory slot (which does not have to
|
|
|
|
/// be empty).
|
|
|
|
static std::string freeMemory();
|
2019-05-06 17:48:31 +00:00
|
|
|
|
2020-02-06 13:05:25 +00:00
|
|
|
/// Generates the required conversion code and @returns an IRVariable referring to the value of @a _variable
|
|
|
|
/// converted to type @a _to.
|
|
|
|
IRVariable convert(IRVariable const& _variable, Type const& _to);
|
|
|
|
|
2019-04-15 16:10:36 +00:00
|
|
|
/// @returns a Yul expression representing the current value of @a _expression,
|
|
|
|
/// converted to type @a _to if it does not yet have that type.
|
2020-05-05 11:06:01 +00:00
|
|
|
/// If @a _forceCleanup is set to true, it also cleans the value, in case it already has type @a _to.
|
|
|
|
std::string expressionAsType(Expression const& _expression, Type const& _to, bool _forceCleanup = false);
|
2020-02-06 13:05:25 +00:00
|
|
|
|
|
|
|
/// @returns an output stream that can be used to define @a _var using a function call or
|
|
|
|
/// single stack slot expression.
|
|
|
|
std::ostream& define(IRVariable const& _var);
|
|
|
|
/// Defines @a _var using the value of @a _value while performing type conversions, if required.
|
|
|
|
void define(IRVariable const& _var, IRVariable const& _value) { declareAssign(_var, _value, true); }
|
|
|
|
/// Assigns @a _var to the value of @a _value while performing type conversions, if required.
|
|
|
|
void assign(IRVariable const& _var, IRVariable const& _value) { declareAssign(_var, _value, false); }
|
|
|
|
/// Declares variable @a _var.
|
|
|
|
void declare(IRVariable const& _var);
|
|
|
|
|
|
|
|
void declareAssign(IRVariable const& _var, IRVariable const& _value, bool _define);
|
2019-04-15 16:10:36 +00:00
|
|
|
|
2020-03-02 20:42:46 +00:00
|
|
|
/// @returns an IRVariable with the zero
|
|
|
|
/// value of @a _type.
|
|
|
|
/// @param _splitFunctionTypes if false, returns two zeroes
|
|
|
|
IRVariable zeroValue(Type const& _type, bool _splitFunctionTypes = true);
|
|
|
|
|
2019-05-02 16:09:19 +00:00
|
|
|
void appendAndOrOperatorCode(BinaryOperation const& _binOp);
|
2019-05-13 13:26:55 +00:00
|
|
|
void appendSimpleUnaryOperation(UnaryOperation const& _operation, Expression const& _expr);
|
2019-05-02 16:09:19 +00:00
|
|
|
|
2019-05-23 18:17:20 +00:00
|
|
|
/// @returns code to perform the given binary operation in the given type on the two values.
|
|
|
|
std::string binaryOperation(
|
|
|
|
langutil::Token _op,
|
|
|
|
Type const& _type,
|
|
|
|
std::string const& _left,
|
|
|
|
std::string const& _right
|
|
|
|
);
|
|
|
|
|
2020-04-20 21:16:42 +00:00
|
|
|
/// @returns code to perform the given shift operation.
|
|
|
|
/// The operation itself will be performed in the type of the value,
|
|
|
|
/// while the amount to shift can have its own type.
|
|
|
|
std::string shiftOperation(langutil::Token _op, IRVariable const& _value, IRVariable const& _shiftAmount);
|
|
|
|
|
2020-02-06 13:05:25 +00:00
|
|
|
/// Assigns the value of @a _value to the lvalue @a _lvalue.
|
|
|
|
void writeToLValue(IRLValue const& _lvalue, IRVariable const& _value);
|
|
|
|
/// @returns a fresh IR variable containing the value of the lvalue @a _lvalue.
|
|
|
|
IRVariable readFromLValue(IRLValue const& _lvalue);
|
|
|
|
|
2020-04-09 10:48:57 +00:00
|
|
|
/// Stores the given @a _lvalue in m_currentLValue, if it will be written to (willBeWrittenTo). Otherwise
|
2020-02-06 13:05:25 +00:00
|
|
|
/// defines the expression @a _expression by reading the value from @a _lvalue.
|
|
|
|
void setLValue(Expression const& _expression, IRLValue _lvalue);
|
2019-05-08 11:27:36 +00:00
|
|
|
void generateLoop(
|
|
|
|
Statement const& _body,
|
|
|
|
Expression const* _conditionExpression,
|
|
|
|
Statement const* _initExpression = nullptr,
|
|
|
|
ExpressionStatement const* _loopExpression = nullptr,
|
|
|
|
bool _isDoWhile = false
|
|
|
|
);
|
2019-04-30 16:32:56 +00:00
|
|
|
|
2019-05-06 09:09:50 +00:00
|
|
|
static Type const& type(Expression const& _expression);
|
|
|
|
|
2020-09-11 09:03:22 +00:00
|
|
|
void setLocation(ASTNode const& _node);
|
|
|
|
|
2020-10-27 16:14:58 +00:00
|
|
|
std::string linkerSymbol(ContractDefinition const& _library) const;
|
|
|
|
|
2019-03-18 10:21:41 +00:00
|
|
|
std::ostringstream m_code;
|
|
|
|
IRGenerationContext& m_context;
|
|
|
|
YulUtilFunctions& m_utils;
|
2020-02-06 13:05:25 +00:00
|
|
|
std::optional<IRLValue> m_currentLValue;
|
2020-09-11 09:03:22 +00:00
|
|
|
langutil::SourceLocation m_currentLocation;
|
2019-03-18 10:21:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|