/src/solidity/libyul/optimiser/VarNameCleaner.h
Line | Count | Source (jump to first uncovered line) |
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 | | #pragma once |
20 | | |
21 | | |
22 | | #include <libyul/ASTForward.h> |
23 | | #include <libyul/optimiser/ASTWalker.h> |
24 | | #include <libyul/YulString.h> |
25 | | #include <libyul/optimiser/OptimiserStep.h> |
26 | | |
27 | | #include <map> |
28 | | #include <set> |
29 | | #include <string> |
30 | | |
31 | | namespace solidity::yul |
32 | | { |
33 | | |
34 | | struct Dialect; |
35 | | |
36 | | /** |
37 | | * Pass to normalize identifier suffixes. |
38 | | * |
39 | | * That is, for each function scope, nested suffixes get flattened and all suffixes |
40 | | * renumbered by their base name. |
41 | | * Function names are not modified. |
42 | | * |
43 | | * NOTE: This step destroys the promise of the Disambiguator and thus cannot |
44 | | * be used in the main loop of the optimizer without running the disambiguator again. |
45 | | * Because of that, it is not included in the step list of the Optimizer Suite. |
46 | | * |
47 | | * Prerequisites: Disambiguator, FunctionHoister, FunctionGrouper |
48 | | */ |
49 | | class VarNameCleaner: public ASTModifier |
50 | | { |
51 | | public: |
52 | | static constexpr char const* name{"VarNameCleaner"}; |
53 | | static void run(OptimiserStepContext& _context, Block& _ast) |
54 | 0 | { |
55 | 0 | VarNameCleaner{_ast, _context.dialect, _context.reservedIdentifiers}(_ast); |
56 | 0 | } |
57 | | |
58 | | using ASTModifier::operator(); |
59 | | void operator()(VariableDeclaration& _varDecl) override; |
60 | | void operator()(Identifier& _identifier) override; |
61 | | void operator()(FunctionDefinition& _funDef) override; |
62 | | |
63 | | private: |
64 | | VarNameCleaner( |
65 | | Block const& _ast, |
66 | | Dialect const& _dialect, |
67 | | std::set<YulString> _namesToKeep = {} |
68 | | ); |
69 | | |
70 | | /// Tries to rename a list of variables. |
71 | | void renameVariables(std::vector<TypedName>& _variables); |
72 | | |
73 | | /// @returns suffix-stripped name, if a suffix was detected, none otherwise. |
74 | | YulString stripSuffix(YulString const& _name) const; |
75 | | |
76 | | /// Looks out for a "clean name" the given @p name could be trimmed down to. |
77 | | /// @returns a trimmed down and "clean name" in case it found one, none otherwise. |
78 | | YulString findCleanName(YulString const& name) const; |
79 | | |
80 | | /// Tests whether a given name was already used within this pass |
81 | | /// or was set to be kept. |
82 | | bool isUsedName(YulString const& _name) const; |
83 | | |
84 | | Dialect const& m_dialect; |
85 | | |
86 | | /// These names will not be modified. |
87 | | std::set<YulString> m_namesToKeep; |
88 | | |
89 | | /// Set of names that are in use. |
90 | | std::set<YulString> m_usedNames; |
91 | | |
92 | | /// Maps old to new names. |
93 | | std::map<YulString, YulString> m_translatedNames; |
94 | | |
95 | | /// Whether the traverse is inside a function definition. |
96 | | /// Used to assert that a function definition cannot be inside another. |
97 | | bool m_insideFunction = false; |
98 | | }; |
99 | | |
100 | | } |