/src/solidity/libyul/optimiser/VarDeclInitializer.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 | | #include <libyul/optimiser/VarDeclInitializer.h> |
20 | | #include <libyul/AST.h> |
21 | | |
22 | | #include <libsolutil/CommonData.h> |
23 | | #include <libsolutil/Visitor.h> |
24 | | #include <libyul/Dialect.h> |
25 | | |
26 | | using namespace solidity; |
27 | | using namespace solidity::yul; |
28 | | |
29 | | void VarDeclInitializer::operator()(Block& _block) |
30 | 855k | { |
31 | 855k | ASTModifier::operator()(_block); |
32 | | |
33 | 855k | using OptionalStatements = std::optional<std::vector<Statement>>; |
34 | 855k | util::GenericVisitor visitor{ |
35 | 855k | util::VisitorFallback<OptionalStatements>{}, |
36 | 855k | [this](VariableDeclaration& _varDecl) -> OptionalStatements |
37 | 855k | { |
38 | 303k | if (_varDecl.value) |
39 | 278k | return {}; |
40 | | |
41 | 25.7k | if (_varDecl.variables.size() == 1) |
42 | 10.1k | { |
43 | 10.1k | _varDecl.value = std::make_unique<Expression>(m_dialect.zeroLiteral()); |
44 | 10.1k | return {}; |
45 | 10.1k | } |
46 | 15.5k | else |
47 | 15.5k | { |
48 | 15.5k | OptionalStatements ret{std::vector<Statement>{}}; |
49 | 15.5k | for (auto& var: _varDecl.variables) |
50 | 44.7k | { |
51 | 44.7k | std::unique_ptr<Expression> expr = std::make_unique<Expression>(m_dialect.zeroLiteral()); |
52 | 44.7k | ret->emplace_back(VariableDeclaration{std::move(_varDecl.debugData), {std::move(var)}, std::move(expr)}); |
53 | 44.7k | } |
54 | 15.5k | return ret; |
55 | 15.5k | } |
56 | 25.7k | } |
57 | 855k | }; |
58 | | |
59 | 1.94M | util::iterateReplacing(_block.statements, [&](auto&& _statement) { return std::visit(visitor, _statement); }); |
60 | 855k | } |