diff options
author | Christian Parpart <christian@parpart.family> | 2018-10-15 18:30:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-15 18:30:00 +0800 |
commit | b2b845d6def4d28215c5d591589555bd8f4ea6ab (patch) | |
tree | a8501e929c94a7d36e69a6350e98c68556fe9038 /libyul/optimiser/ASTCopier.h | |
parent | b965fd6e17f77e94afeb070a27182251b85b8ab3 (diff) | |
parent | 1304361b9c48438d5c55903492b5f11c3dac73e5 (diff) | |
download | dexon-solidity-b2b845d6def4d28215c5d591589555bd8f4ea6ab.tar.gz dexon-solidity-b2b845d6def4d28215c5d591589555bd8f4ea6ab.tar.zst dexon-solidity-b2b845d6def4d28215c5d591589555bd8f4ea6ab.zip |
Merge pull request #5220 from ethereum/libjulia-to-libyul
Renames `libjulia` directory to `libyul` & namespace `dev::julia` to `dev::yul`
Diffstat (limited to 'libyul/optimiser/ASTCopier.h')
-rw-r--r-- | libyul/optimiser/ASTCopier.h | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/libyul/optimiser/ASTCopier.h b/libyul/optimiser/ASTCopier.h new file mode 100644 index 00000000..13369cef --- /dev/null +++ b/libyul/optimiser/ASTCopier.h @@ -0,0 +1,124 @@ +/* + 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/ASTDataForward.h> + +#include <boost/variant.hpp> +#include <boost/optional.hpp> + +#include <vector> +#include <set> +#include <memory> + +namespace dev +{ +namespace yul +{ + +class ExpressionCopier: public boost::static_visitor<Expression> +{ +public: + virtual ~ExpressionCopier() = default; + 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; + 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()(Block const& _block) = 0; +}; + +/** + * Creates a copy of a Yul AST potentially replacing identifier names. + * Base class to be extended. + */ +class ASTCopier: public ExpressionCopier, public StatementCopier +{ +public: + virtual ~ASTCopier() = default; + virtual Expression operator()(Literal const& _literal) override; + virtual Statement operator()(Instruction const& _instruction) override; + virtual Expression operator()(Identifier const& _identifier) override; + virtual Expression operator()(FunctionalInstruction const& _instr) override; + virtual Expression operator()(FunctionCall const&) override; + virtual Statement operator()(ExpressionStatement const& _statement) override; + virtual Statement operator()(Label const& _label) override; + virtual Statement operator()(StackAssignment const& _assignment) override; + virtual Statement operator()(Assignment const& _assignment) override; + virtual Statement operator()(VariableDeclaration const& _varDecl) override; + virtual Statement operator()(If const& _if) override; + virtual Statement operator()(Switch const& _switch) override; + virtual Statement operator()(FunctionDefinition const&) override; + virtual Statement operator()(ForLoop const&) override; + virtual Statement operator()(Block const& _block) override; + + virtual Expression translate(Expression const& _expression); + virtual Statement translate(Statement const& _statement); + +protected: + template <typename T> + std::vector<T> translateVector(std::vector<T> const& _values); + + template <typename T> + std::shared_ptr<T> translate(std::shared_ptr<T> const& _v) + { + return _v ? std::make_shared<T>(translate(*_v)) : nullptr; + } + Block translate(Block const& _block); + Case translate(Case const& _case); + Identifier translate(Identifier const& _identifier); + Literal translate(Literal const& _literal); + TypedName translate(TypedName const& _typedName); + + virtual void enterScope(Block const&) { } + virtual void leaveScope(Block const&) { } + virtual void enterFunction(FunctionDefinition const&) { } + virtual void leaveFunction(FunctionDefinition const&) { } + virtual std::string translateIdentifier(std::string const& _name) { return _name; } +}; + +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; +} + + +} +} |