1 /*
2 	This file is part of solidity.
3 
4 	solidity is free software: you can redistribute it and/or modify
5 	it under the terms of the GNU General Public License as published by
6 	the Free Software Foundation, either version 3 of the License, or
7 	(at your option) any later version.
8 
9 	solidity is distributed in the hope that it will be useful,
10 	but WITHOUT ANY WARRANTY; without even the implied warranty of
11 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 	GNU General Public License for more details.
13 
14 	You should have received a copy of the GNU General Public License
15 	along with solidity.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 // SPDX-License-Identifier: GPL-3.0
18 /**
19  * Creates an independent copy of an AST, renaming identifiers to be unique.
20  */
21 
22 #pragma once
23 
24 #include <libyul/ASTForward.h>
25 
26 #include <libyul/YulString.h>
27 
28 #include <memory>
29 #include <optional>
30 #include <set>
31 #include <vector>
32 #include <map>
33 
34 namespace solidity::yul
35 {
36 
37 class ExpressionCopier
38 {
39 public:
40 	virtual ~ExpressionCopier() = default;
41 	virtual Expression operator()(Literal const& _literal) = 0;
42 	virtual Expression operator()(Identifier const& _identifier) = 0;
43 	virtual Expression operator()(FunctionCall const&) = 0;
44 };
45 
46 class StatementCopier
47 {
48 public:
49 	virtual ~StatementCopier() = default;
50 	virtual Statement operator()(ExpressionStatement const& _statement) = 0;
51 	virtual Statement operator()(Assignment const& _assignment) = 0;
52 	virtual Statement operator()(VariableDeclaration const& _varDecl) = 0;
53 	virtual Statement operator()(If const& _if) = 0;
54 	virtual Statement operator()(Switch const& _switch) = 0;
55 	virtual Statement operator()(FunctionDefinition const&) = 0;
56 	virtual Statement operator()(ForLoop const&) = 0;
57 	virtual Statement operator()(Break const&) = 0;
58 	virtual Statement operator()(Continue const&) = 0;
59 	virtual Statement operator()(Leave const&) = 0;
60 	virtual Statement operator()(Block const& _block) = 0;
61 };
62 
63 /**
64  * Creates a copy of a Yul AST potentially replacing identifier names.
65  * Base class to be extended.
66  */
67 class ASTCopier: public ExpressionCopier, public StatementCopier
68 {
69 public:
70 	~ASTCopier() override = default;
71 	Expression operator()(Literal const& _literal) override;
72 	Expression operator()(Identifier const& _identifier) override;
73 	Expression operator()(FunctionCall const&) override;
74 	Statement operator()(ExpressionStatement const& _statement) override;
75 	Statement operator()(Assignment const& _assignment) override;
76 	Statement operator()(VariableDeclaration const& _varDecl) override;
77 	Statement operator()(If const& _if) override;
78 	Statement operator()(Switch const& _switch) override;
79 	Statement operator()(FunctionDefinition const&) override;
80 	Statement operator()(ForLoop const&) override;
81 	Statement operator()(Break const&) override;
82 	Statement operator()(Continue const&) override;
83 	Statement operator()(Leave const&) override;
84 	Statement operator()(Block const& _block) override;
85 
86 	virtual Expression translate(Expression const& _expression);
87 	virtual Statement translate(Statement const& _statement);
88 
89 	Block translate(Block const& _block);
90 protected:
91 	template <typename T>
92 	std::vector<T> translateVector(std::vector<T> const& _values);
93 
94 	template <typename T>
translate(std::unique_ptr<T> const & _v)95 	std::unique_ptr<T> translate(std::unique_ptr<T> const& _v)
96 	{
97 		return _v ? std::make_unique<T>(translate(*_v)) : nullptr;
98 	}
99 
100 	Case translate(Case const& _case);
101 	virtual Identifier translate(Identifier const& _identifier);
102 	Literal translate(Literal const& _literal);
103 	TypedName translate(TypedName const& _typedName);
104 
enterScope(Block const &)105 	virtual void enterScope(Block const&) { }
leaveScope(Block const &)106 	virtual void leaveScope(Block const&) { }
enterFunction(FunctionDefinition const &)107 	virtual void enterFunction(FunctionDefinition const&) { }
leaveFunction(FunctionDefinition const &)108 	virtual void leaveFunction(FunctionDefinition const&) { }
translateIdentifier(YulString _name)109 	virtual YulString translateIdentifier(YulString _name) { return _name; }
110 };
111 
112 template <typename T>
translateVector(std::vector<T> const & _values)113 std::vector<T> ASTCopier::translateVector(std::vector<T> const& _values)
114 {
115 	std::vector<T> translated;
116 	for (auto const& v: _values)
117 		translated.emplace_back(translate(v));
118 	return translated;
119 }
120 
121 /// Helper class that creates a copy of the function definition, replacing the names of the variable
122 /// declarations with new names.
123 class FunctionCopier: public ASTCopier
124 {
125 public:
FunctionCopier(std::map<YulString,YulString> const & _translations)126 	FunctionCopier(
127 		std::map<YulString, YulString> const& _translations
128 	):
129 		m_translations(_translations)
130 	{}
131 	using ASTCopier::operator();
132 	YulString translateIdentifier(YulString _name) override;
133 private:
134 	/// A mapping between old and new names. We replace the names of variable declarations contained
135 	/// in the mapping with their new names.
136 	std::map<YulString, YulString> const& m_translations;
137 };
138 
139 }
140