solidity/libyul/optimiser/ASTCopier.h

129 lines
4.4 KiB
C
Raw Normal View History

2017-11-28 11:45:37 +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/>.
*/
/**
* Creates an independent copy of an AST, renaming identifiers to be unique.
*/
#pragma once
#include <libyul/AsmDataForward.h>
2017-11-28 11:45:37 +00:00
#include <libyul/YulString.h>
2017-11-28 11:45:37 +00:00
#include <boost/variant.hpp>
2017-11-28 14:00:23 +00:00
#include <memory>
#include <optional>
#include <set>
#include <vector>
2017-11-28 11:45:37 +00:00
namespace yul
2017-11-28 11:45:37 +00:00
{
2017-12-08 13:01:22 +00:00
class ExpressionCopier: public boost::static_visitor<Expression>
{
public:
virtual ~ExpressionCopier() = default;
2017-12-08 13:01:22 +00:00
virtual Expression operator()(Literal const& _literal) = 0;
virtual Expression operator()(Identifier const& _identifier) = 0;
virtual Expression operator()(FunctionalInstruction const& _instr) = 0;
virtual Expression operator()(FunctionCall const&) = 0;
};
class StatementCopier: public boost::static_visitor<Statement>
{
public:
virtual ~StatementCopier() = default;
2017-12-08 13:01:22 +00:00
virtual Statement operator()(ExpressionStatement const& _statement) = 0;
virtual Statement operator()(Instruction const& _instruction) = 0;
virtual Statement operator()(Label const& _label) = 0;
virtual Statement operator()(StackAssignment const& _assignment) = 0;
virtual Statement operator()(Assignment const& _assignment) = 0;
virtual Statement operator()(VariableDeclaration const& _varDecl) = 0;
virtual Statement operator()(If const& _if) = 0;
virtual Statement operator()(Switch const& _switch) = 0;
virtual Statement operator()(FunctionDefinition const&) = 0;
virtual Statement operator()(ForLoop const&) = 0;
virtual Statement operator()(Break const&) = 0;
virtual Statement operator()(Continue const&) = 0;
2017-12-08 13:01:22 +00:00
virtual Statement operator()(Block const& _block) = 0;
};
2017-11-28 11:45:37 +00:00
/**
2018-06-18 21:26:31 +00:00
* Creates a copy of a Yul AST potentially replacing identifier names.
2017-11-28 11:45:37 +00:00
* Base class to be extended.
*/
2017-12-08 13:01:22 +00:00
class ASTCopier: public ExpressionCopier, public StatementCopier
2017-11-28 11:45:37 +00:00
{
public:
virtual ~ASTCopier() = default;
Expression operator()(Literal const& _literal) override;
Statement operator()(Instruction const& _instruction) override;
Expression operator()(Identifier const& _identifier) override;
Expression operator()(FunctionalInstruction const& _instr) override;
Expression operator()(FunctionCall const&) override;
Statement operator()(ExpressionStatement const& _statement) override;
Statement operator()(Label const& _label) override;
Statement operator()(StackAssignment const& _assignment) override;
Statement operator()(Assignment const& _assignment) override;
Statement operator()(VariableDeclaration const& _varDecl) override;
Statement operator()(If const& _if) override;
Statement operator()(Switch const& _switch) override;
Statement operator()(FunctionDefinition const&) override;
Statement operator()(ForLoop const&) override;
Statement operator()(Break const&) override;
Statement operator()(Continue const&) override;
Statement operator()(Block const& _block) override;
2017-11-28 11:45:37 +00:00
2017-12-08 13:01:22 +00:00
virtual Expression translate(Expression const& _expression);
2017-12-05 19:06:34 +00:00
virtual Statement translate(Statement const& _statement);
2017-11-28 11:45:37 +00:00
protected:
template <typename T>
std::vector<T> translateVector(std::vector<T> const& _values);
template <typename T>
std::unique_ptr<T> translate(std::unique_ptr<T> const& _v)
2017-11-28 11:45:37 +00:00
{
return _v ? std::make_unique<T>(translate(*_v)) : nullptr;
2017-11-28 11:45:37 +00:00
}
2017-11-28 11:45:37 +00:00
Block translate(Block const& _block);
Case translate(Case const& _case);
2019-04-11 10:58:27 +00:00
virtual Identifier translate(Identifier const& _identifier);
2017-11-28 11:45:37 +00:00
Literal translate(Literal const& _literal);
TypedName translate(TypedName const& _typedName);
2017-11-28 14:00:23 +00:00
virtual void enterScope(Block const&) { }
virtual void leaveScope(Block const&) { }
virtual void enterFunction(FunctionDefinition const&) { }
virtual void leaveFunction(FunctionDefinition const&) { }
virtual YulString translateIdentifier(YulString _name) { return _name; }
2017-11-28 11:45:37 +00:00
};
template <typename T>
std::vector<T> ASTCopier::translateVector(std::vector<T> const& _values)
{
std::vector<T> translated;
for (auto const& v: _values)
translated.emplace_back(translate(v));
return translated;
}
}