/src/solidity/libyul/backends/evm/VariableReferenceCounter.h
Line | Count | Source |
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 | | * Counts the number of references to a variable. |
20 | | */ |
21 | | #pragma once |
22 | | |
23 | | #include <libyul/optimiser/ASTWalker.h> |
24 | | #include <libyul/Scope.h> |
25 | | |
26 | | namespace solidity::yul |
27 | | { |
28 | | struct AsmAnalysisInfo; |
29 | | |
30 | | /** |
31 | | * Counts the number of references to a variable. This includes actual (read) references |
32 | | * but also assignments to the variable. It does not include the declaration itself or |
33 | | * function parameters, but it does include function return parameters. |
34 | | * |
35 | | * This component can handle multiple variables of the same name. |
36 | | * |
37 | | * Can only be applied to strict assembly. |
38 | | */ |
39 | | struct VariableReferenceCounter: public yul::ASTWalker |
40 | | { |
41 | | public: |
42 | | static std::map<Scope::Variable const*, unsigned> run(AsmAnalysisInfo const& _assemblyInfo, Block const& _block) |
43 | 24.3k | { |
44 | 24.3k | VariableReferenceCounter variableReferenceCounter(_assemblyInfo); |
45 | 24.3k | variableReferenceCounter(_block); |
46 | 24.3k | return std::move(variableReferenceCounter.m_variableReferences); |
47 | 24.3k | } |
48 | | |
49 | | protected: |
50 | | void operator()(Block const& _block) override; |
51 | | void operator()(Identifier const& _identifier) override; |
52 | | void operator()(FunctionDefinition const&) override; |
53 | | void operator()(ForLoop const&) override; |
54 | | |
55 | | private: |
56 | | explicit VariableReferenceCounter( |
57 | | AsmAnalysisInfo const& _assemblyInfo |
58 | | ): m_info(_assemblyInfo) |
59 | 24.3k | {} |
60 | | |
61 | | void increaseRefIfFound(YulName _variableName); |
62 | | |
63 | | AsmAnalysisInfo const& m_info; |
64 | | Scope* m_scope = nullptr; |
65 | | std::map<Scope::Variable const*, unsigned> m_variableReferences; |
66 | | }; |
67 | | |
68 | | } |