-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathASTCopier.h
140 lines (121 loc) · 4.59 KB
/
ASTCopier.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Creates an independent copy of an AST, renaming identifiers to be unique.
*/
#pragma once
#include <libyul/ASTForward.h>
#include <libyul/YulName.h>
#include <memory>
#include <optional>
#include <set>
#include <vector>
#include <map>
namespace solidity::yul
{
class ExpressionCopier
{
public:
virtual ~ExpressionCopier() = default;
virtual Expression operator()(Literal const& _literal) = 0;
virtual Expression operator()(Identifier const& _identifier) = 0;
virtual Expression operator()(FunctionCall const&) = 0;
};
class StatementCopier
{
public:
virtual ~StatementCopier() = default;
virtual Statement operator()(ExpressionStatement const& _statement) = 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;
virtual Statement operator()(Leave 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:
~ASTCopier() override = default;
Expression operator()(Literal const& _literal) override;
Expression operator()(Identifier const& _identifier) override;
Expression operator()(FunctionCall const&) override;
Statement operator()(ExpressionStatement const& _statement) 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()(Leave const&) override;
Statement operator()(Block const& _block) override;
virtual Expression translate(Expression const& _expression);
virtual Statement translate(Statement const& _statement);
Block translate(Block const& _block);
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)
{
return _v ? std::make_unique<T>(translate(*_v)) : nullptr;
}
Case translate(Case const& _case);
virtual Identifier translate(Identifier const& _identifier);
Literal translate(Literal const& _literal);
FunctionName translate(FunctionName const& _functionName);
NameWithDebugData translate(NameWithDebugData const& _typedName);
virtual void enterScope(Block const&) { }
virtual void leaveScope(Block const&) { }
virtual void enterFunction(FunctionDefinition const&) { }
virtual void leaveFunction(FunctionDefinition const&) { }
virtual YulName translateIdentifier(YulName _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;
}
/// Helper class that creates a copy of the function definition, replacing the names of the variable
/// declarations with new names.
class FunctionCopier: public ASTCopier
{
public:
FunctionCopier(
std::map<YulName, YulName> const& _translations
):
m_translations(_translations)
{}
using ASTCopier::operator();
YulName translateIdentifier(YulName _name) override;
private:
/// A mapping between old and new names. We replace the names of variable declarations contained
/// in the mapping with their new names.
std::map<YulName, YulName> const& m_translations;
};
}