-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathNameSimplifier.cpp
125 lines (105 loc) · 3.79 KB
/
NameSimplifier.cpp
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
/*
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/>.
*/
#include <libyul/optimiser/NameSimplifier.h>
#include <libyul/optimiser/NameCollector.h>
#include <libyul/AST.h>
#include <libyul/Dialect.h>
#include <libyul/YulName.h>
#include <libyul/optimiser/NameDispenser.h>
#include <libyul/optimiser/OptimizerUtilities.h>
#include <libsolutil/CommonData.h>
#include <regex>
using namespace solidity::yul;
NameSimplifier::NameSimplifier(OptimiserStepContext& _context, Block const& _ast):
m_context(_context)
{
for (YulName name: _context.reservedIdentifiers)
m_translations[name] = name;
for (YulName const& name: NameCollector(_ast).names())
findSimplification(name);
}
void NameSimplifier::operator()(FunctionDefinition& _funDef)
{
translate(_funDef.name);
renameVariables(_funDef.parameters);
renameVariables(_funDef.returnVariables);
ASTModifier::operator()(_funDef);
}
void NameSimplifier::operator()(VariableDeclaration& _varDecl)
{
renameVariables(_varDecl.variables);
ASTModifier::operator()(_varDecl);
}
void NameSimplifier::renameVariables(std::vector<NameWithDebugData>& _variables)
{
for (NameWithDebugData& typedName: _variables)
translate(typedName.name);
}
void NameSimplifier::operator()(Identifier& _identifier)
{
translate(_identifier.name);
}
void NameSimplifier::operator()(FunctionCall& _funCall)
{
// The visitor on its own does not visit the function name.
if (!isBuiltinFunctionCall(_funCall))
{
yulAssert(std::holds_alternative<Identifier>(_funCall.functionName));
(*this)(std::get<Identifier>(_funCall.functionName));
}
ASTModifier::operator()(_funCall);
}
void NameSimplifier::findSimplification(YulName const& _name)
{
if (m_translations.count(_name))
return;
std::string name = _name.str();
static auto replacements = std::vector<std::pair<std::regex, std::string>>{
{std::regex("_\\$|\\$_"), "_"}, // remove type mangling delimiters
{std::regex("_[0-9]+([^0-9a-fA-Fx])"), "$1"}, // removes AST IDs that are not hex.
{std::regex("_[0-9]+$"), ""}, // removes AST IDs that are not hex.
{std::regex("_t_"), "_"}, // remove type prefixes
{std::regex("__"), "_"},
{std::regex("(abi_..code.*)_to_.*"), "$1"}, // removes _to... for abi functions
{std::regex("(stringliteral_?[0-9a-f][0-9a-f][0-9a-f][0-9a-f])[0-9a-f]*"), "$1"}, // shorten string literal
{std::regex("tuple_"), ""},
{std::regex("_memory_ptr"), ""},
{std::regex("_calldata_ptr"), "_calldata"},
{std::regex("_fromStack"), ""},
{std::regex("_storage_storage"), "_storage"},
{std::regex("(storage.*)_?storage"), "$1"},
{std::regex("_memory_memory"), "_memory"},
{std::regex("_contract\\$_([^_]*)_?"), "$1_"},
{std::regex("index_access_(t_)?array"), "index_access"},
{std::regex("[0-9]*_$"), ""}
};
for (auto const& [pattern, substitute]: replacements)
{
std::string candidate = regex_replace(name, pattern, substitute);
if (!candidate.empty() && !m_context.dispenser.illegalName(YulName(candidate)))
name = candidate;
}
if (name != _name.str())
{
YulName newName{name};
m_context.dispenser.markUsed(newName);
m_translations[_name] = std::move(newName);
}
}
void NameSimplifier::translate(YulName& _name)
{
auto it = m_translations.find(_name);
if (it != m_translations.end())
_name = it->second;
}