solidity/libyul/optimiser/UnusedFunctionsCommon.cpp

76 lines
2.4 KiB
C++
Raw Normal View History

/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libyul/optimiser/UnusedFunctionsCommon.h>
#include <libyul/Dialect.h>
#include <liblangutil/SourceLocation.h>
#include <libsolutil/CommonData.h>
using namespace solidity;
using namespace solidity::util;
using namespace solidity::yul;
using namespace solidity::yul::unusedFunctionsCommon;
FunctionDefinition unusedFunctionsCommon::createLinkingFunction(
FunctionDefinition const& _original,
std::pair<std::vector<bool>, std::vector<bool>> const& _usedParametersAndReturns,
YulString const& _originalFunctionName,
YulString const& _linkingFunctionName,
NameDispenser& _nameDispenser
)
{
auto generateTypedName = [&](TypedName t)
{
return TypedName{
2021-04-27 14:53:04 +00:00
t.debugData,
_nameDispenser.newName(t.name),
t.type
};
};
FunctionDefinition linkingFunction{
2021-04-27 14:53:04 +00:00
_original.debugData,
_linkingFunctionName,
util::applyMap(_original.parameters, generateTypedName),
util::applyMap(_original.returnVariables, generateTypedName),
2021-04-27 14:53:04 +00:00
{_original.debugData, {}} // body
};
2021-04-27 14:53:04 +00:00
FunctionCall call{_original.debugData, Identifier{_original.debugData, _originalFunctionName}, {}};
for (auto const& p: filter(linkingFunction.parameters, _usedParametersAndReturns.first))
2021-04-27 14:53:04 +00:00
call.arguments.emplace_back(Identifier{_original.debugData, p.name});
2021-04-27 14:53:04 +00:00
Assignment assignment{_original.debugData, {}, nullptr};
for (auto const& r: filter(linkingFunction.returnVariables, _usedParametersAndReturns.second))
2021-04-27 14:53:04 +00:00
assignment.variableNames.emplace_back(Identifier{_original.debugData, r.name});
if (assignment.variableNames.empty())
2021-04-27 14:53:04 +00:00
linkingFunction.body.statements.emplace_back(ExpressionStatement{_original.debugData, std::move(call)});
else
{
assignment.value = std::make_unique<Expression>(std::move(call));
linkingFunction.body.statements.emplace_back(std::move(assignment));
}
return linkingFunction;
}