/src/solidity/libyul/optimiser/ASTWalker.cpp
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 | | * Generic AST walker. |
20 | | */ |
21 | | |
22 | | #include <libyul/optimiser/ASTWalker.h> |
23 | | |
24 | | #include <libyul/AST.h> |
25 | | |
26 | | #include <range/v3/view/reverse.hpp> |
27 | | |
28 | | using namespace std; |
29 | | using namespace solidity; |
30 | | using namespace solidity::yul; |
31 | | using namespace solidity::util; |
32 | | |
33 | | void ASTWalker::operator()(FunctionCall const& _funCall) |
34 | 1.00G | { |
35 | | // Does not visit _funCall.functionName on purpose |
36 | 1.00G | walkVector(_funCall.arguments | ranges::views::reverse); |
37 | 1.00G | } |
38 | | |
39 | | void ASTWalker::operator()(ExpressionStatement const& _statement) |
40 | 383M | { |
41 | 383M | visit(_statement.expression); |
42 | 383M | } |
43 | | |
44 | | void ASTWalker::operator()(Assignment const& _assignment) |
45 | 119M | { |
46 | 119M | for (auto const& name: _assignment.variableNames) |
47 | 119M | (*this)(name); |
48 | 119M | visit(*_assignment.value); |
49 | 119M | } |
50 | | |
51 | | void ASTWalker::operator()(VariableDeclaration const& _varDecl) |
52 | 1.98G | { |
53 | 1.98G | if (_varDecl.value) |
54 | 1.98G | visit(*_varDecl.value); |
55 | 1.98G | } |
56 | | |
57 | | void ASTWalker::operator()(If const& _if) |
58 | 102M | { |
59 | 102M | visit(*_if.condition); |
60 | 102M | (*this)(_if.body); |
61 | 102M | } |
62 | | |
63 | | void ASTWalker::operator()(Switch const& _switch) |
64 | 6.41M | { |
65 | 6.41M | visit(*_switch.expression); |
66 | 6.41M | for (auto const& _case: _switch.cases) |
67 | 19.8M | { |
68 | 19.8M | if (_case.value) |
69 | 15.2M | (*this)(*_case.value); |
70 | 19.8M | (*this)(_case.body); |
71 | 19.8M | } |
72 | 6.41M | } |
73 | | |
74 | | void ASTWalker::operator()(FunctionDefinition const& _fun) |
75 | 57.3M | { |
76 | 57.3M | (*this)(_fun.body); |
77 | 57.3M | } |
78 | | |
79 | | void ASTWalker::operator()(ForLoop const& _for) |
80 | 64.8M | { |
81 | 64.8M | (*this)(_for.pre); |
82 | 64.8M | visit(*_for.condition); |
83 | 64.8M | (*this)(_for.body); |
84 | 64.8M | (*this)(_for.post); |
85 | 64.8M | } |
86 | | |
87 | | void ASTWalker::operator()(Block const& _block) |
88 | 555M | { |
89 | 555M | walkVector(_block.statements); |
90 | 555M | } |
91 | | |
92 | | void ASTWalker::visit(Statement const& _st) |
93 | 3.05G | { |
94 | 3.05G | std::visit(*this, _st); |
95 | 3.05G | } |
96 | | |
97 | | void ASTWalker::visit(Expression const& _e) |
98 | 5.25G | { |
99 | 5.25G | std::visit(*this, _e); |
100 | 5.25G | } |
101 | | |
102 | | void ASTModifier::operator()(FunctionCall& _funCall) |
103 | 468M | { |
104 | | // Does not visit _funCall.functionName on purpose |
105 | 468M | walkVector(_funCall.arguments | ranges::views::reverse); |
106 | 468M | } |
107 | | |
108 | | void ASTModifier::operator()(ExpressionStatement& _statement) |
109 | 218M | { |
110 | 218M | visit(_statement.expression); |
111 | 218M | } |
112 | | |
113 | | void ASTModifier::operator()(Assignment& _assignment) |
114 | 46.3M | { |
115 | 46.3M | for (auto& name: _assignment.variableNames) |
116 | 46.3M | (*this)(name); |
117 | 46.3M | visit(*_assignment.value); |
118 | 46.3M | } |
119 | | |
120 | | void ASTModifier::operator()(VariableDeclaration& _varDecl) |
121 | 761M | { |
122 | 761M | if (_varDecl.value) |
123 | 761M | visit(*_varDecl.value); |
124 | 761M | } |
125 | | |
126 | | void ASTModifier::operator()(If& _if) |
127 | 48.6M | { |
128 | 48.6M | visit(*_if.condition); |
129 | 48.6M | (*this)(_if.body); |
130 | 48.6M | } |
131 | | |
132 | | void ASTModifier::operator()(Switch& _switch) |
133 | 3.16M | { |
134 | 3.16M | visit(*_switch.expression); |
135 | 3.16M | for (auto& _case: _switch.cases) |
136 | 9.93M | { |
137 | 9.93M | if (_case.value) |
138 | 7.70M | (*this)(*_case.value); |
139 | 9.93M | (*this)(_case.body); |
140 | 9.93M | } |
141 | 3.16M | } |
142 | | |
143 | | void ASTModifier::operator()(FunctionDefinition& _fun) |
144 | 47.0M | { |
145 | 47.0M | (*this)(_fun.body); |
146 | 47.0M | } |
147 | | |
148 | | void ASTModifier::operator()(ForLoop& _for) |
149 | 23.9M | { |
150 | 23.9M | (*this)(_for.pre); |
151 | 23.9M | visit(*_for.condition); |
152 | 23.9M | (*this)(_for.post); |
153 | 23.9M | (*this)(_for.body); |
154 | 23.9M | } |
155 | | |
156 | | void ASTModifier::operator()(Break&) |
157 | 15.2M | { |
158 | 15.2M | } |
159 | | |
160 | | void ASTModifier::operator()(Continue&) |
161 | 1.78M | { |
162 | 1.78M | } |
163 | | |
164 | | void ASTModifier::operator()(Leave&) |
165 | 6.62M | { |
166 | 6.62M | } |
167 | | |
168 | | void ASTModifier::operator()(Block& _block) |
169 | 164M | { |
170 | 164M | walkVector(_block.statements); |
171 | 164M | } |
172 | | |
173 | | void ASTModifier::visit(Statement& _st) |
174 | 1.45G | { |
175 | 1.45G | std::visit(*this, _st); |
176 | 1.45G | } |
177 | | |
178 | | void ASTModifier::visit(Expression& _e) |
179 | 2.35G | { |
180 | 2.35G | std::visit(*this, _e); |
181 | 2.35G | } |