/src/solidity/libyul/optimiser/UnusedFunctionsCommon.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 | | #include <libyul/optimiser/UnusedFunctionsCommon.h> |
20 | | |
21 | | #include <libyul/Dialect.h> |
22 | | |
23 | | #include <libsolutil/CommonData.h> |
24 | | |
25 | | using namespace solidity; |
26 | | using namespace solidity::util; |
27 | | using namespace solidity::yul; |
28 | | using namespace solidity::yul::unusedFunctionsCommon; |
29 | | |
30 | | FunctionDefinition unusedFunctionsCommon::createLinkingFunction( |
31 | | FunctionDefinition const& _original, |
32 | | std::pair<std::vector<bool>, std::vector<bool>> const& _usedParametersAndReturns, |
33 | | YulString const& _originalFunctionName, |
34 | | YulString const& _linkingFunctionName, |
35 | | NameDispenser& _nameDispenser |
36 | | ) |
37 | 0 | { |
38 | 0 | auto generateTypedName = [&](TypedName t) |
39 | 0 | { |
40 | 0 | return TypedName{ |
41 | 0 | t.debugData, |
42 | 0 | _nameDispenser.newName(t.name), |
43 | 0 | t.type |
44 | 0 | }; |
45 | 0 | }; |
46 | |
|
47 | 0 | FunctionDefinition linkingFunction{ |
48 | 0 | _original.debugData, |
49 | 0 | _linkingFunctionName, |
50 | 0 | util::applyMap(_original.parameters, generateTypedName), |
51 | 0 | util::applyMap(_original.returnVariables, generateTypedName), |
52 | 0 | {_original.debugData, {}} // body |
53 | 0 | }; |
54 | |
|
55 | 0 | FunctionCall call{_original.debugData, Identifier{_original.debugData, _originalFunctionName}, {}}; |
56 | 0 | for (auto const& p: filter(linkingFunction.parameters, _usedParametersAndReturns.first)) |
57 | 0 | call.arguments.emplace_back(Identifier{_original.debugData, p.name}); |
58 | |
|
59 | 0 | Assignment assignment{_original.debugData, {}, nullptr}; |
60 | |
|
61 | 0 | for (auto const& r: filter(linkingFunction.returnVariables, _usedParametersAndReturns.second)) |
62 | 0 | assignment.variableNames.emplace_back(Identifier{_original.debugData, r.name}); |
63 | |
|
64 | 0 | if (assignment.variableNames.empty()) |
65 | 0 | linkingFunction.body.statements.emplace_back(ExpressionStatement{_original.debugData, std::move(call)}); |
66 | 0 | else |
67 | 0 | { |
68 | 0 | assignment.value = std::make_unique<Expression>(std::move(call)); |
69 | 0 | linkingFunction.body.statements.emplace_back(std::move(assignment)); |
70 | 0 | } |
71 | |
|
72 | 0 | return linkingFunction; |
73 | 0 | } |