-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Expand file tree
/
Copy pathVarNameCleaner.h
More file actions
103 lines (80 loc) · 3.08 KB
/
VarNameCleaner.h
File metadata and controls
103 lines (80 loc) · 3.08 KB
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
/*
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
#pragma once
#include <libyul/ASTForward.h>
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/YulName.h>
#include <libyul/optimiser/OptimiserStep.h>
#include <map>
#include <set>
#include <string>
namespace solidity::yul
{
class Dialect;
/**
* Pass to normalize identifier suffixes.
*
* That is, for each function scope, nested suffixes get flattened and all suffixes
* renumbered by their base name.
* Function names are not modified.
*
* NOTE: This step destroys the promise of the Disambiguator and thus cannot
* be used in the main loop of the optimizer without running the disambiguator again.
* Because of that, it is not included in the step list of the Optimizer Suite.
*
* Prerequisites: Disambiguator, FunctionHoister, FunctionGrouper
*/
class VarNameCleaner: public ASTModifier
{
public:
static constexpr char const* name{"VarNameCleaner"};
static void run(OptimiserStepContext& _context, Block& _ast)
{
VarNameCleaner{_ast, _context.dialect, _context.reservedIdentifiers}(_ast);
}
using ASTModifier::operator();
void operator()(VariableDeclaration& _varDecl) override;
void operator()(Identifier& _identifier) override;
void operator()(FunctionDefinition& _funDef) override;
private:
VarNameCleaner(
Block const& _ast,
Dialect const& _dialect,
std::set<YulName> _namesToKeep = {}
);
/// Tries to rename a list of variables.
void renameVariables(std::vector<NameWithDebugData>& _variables);
/// @returns suffix-stripped name, if a suffix was detected, none otherwise.
YulName stripSuffix(YulName const& _name) const;
/// Looks out for a "clean name" the given @p name could be trimmed down to.
/// @returns a trimmed down and "clean name" in case it found one, none otherwise.
YulName findCleanName(YulName const& name);
/// Tests whether a given name was already used within this pass
/// or was set to be kept.
bool isUsedName(YulName const& _name) const;
Dialect const& m_dialect;
/// These names will not be modified.
std::set<YulName> m_namesToKeep;
/// Set of names that are in use.
std::set<YulName> m_usedNames;
/// Next suffix to try per stripped base name, avoids O(n²) probing.
mutable std::map<YulName, size_t> m_nextSuffix;
/// Maps old to new names.
std::map<YulName, YulName> m_translatedNames;
/// Whether the traverse is inside a function definition.
/// Used to assert that a function definition cannot be inside another.
bool m_insideFunction = false;
};
}