/src/solidity/libyul/optimiser/FunctionGrouper.cpp
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 | | * Optimiser component that changes the code of a block so that all non-function definition |
20 | | * statements are moved to a block of their own followed by all function definitions. |
21 | | */ |
22 | | |
23 | | #include <libyul/optimiser/FunctionGrouper.h> |
24 | | |
25 | | #include <libyul/AST.h> |
26 | | |
27 | | using namespace std; |
28 | | using namespace solidity; |
29 | | using namespace solidity::yul; |
30 | | |
31 | | |
32 | | void FunctionGrouper::operator()(Block& _block) |
33 | 0 | { |
34 | 0 | if (alreadyGrouped(_block)) |
35 | 0 | return; |
36 | | |
37 | 0 | vector<Statement> reordered; |
38 | 0 | reordered.emplace_back(Block{_block.debugData, {}}); |
39 | |
|
40 | 0 | for (auto&& statement: _block.statements) |
41 | 0 | { |
42 | 0 | if (holds_alternative<FunctionDefinition>(statement)) |
43 | 0 | reordered.emplace_back(std::move(statement)); |
44 | 0 | else |
45 | 0 | std::get<Block>(reordered.front()).statements.emplace_back(std::move(statement)); |
46 | 0 | } |
47 | 0 | _block.statements = std::move(reordered); |
48 | 0 | } |
49 | | |
50 | | bool FunctionGrouper::alreadyGrouped(Block const& _block) |
51 | 0 | { |
52 | 0 | if (_block.statements.empty()) |
53 | 0 | return false; |
54 | 0 | if (!holds_alternative<Block>(_block.statements.front())) |
55 | 0 | return false; |
56 | 0 | for (size_t i = 1; i < _block.statements.size(); ++i) |
57 | 0 | if (!holds_alternative<FunctionDefinition>(_block.statements.at(i))) |
58 | 0 | return false; |
59 | 0 | return true; |
60 | 0 | } |