Coverage Report

Created: 2025-06-24 07:59

/src/solidity/libyul/optimiser/UnusedFunctionsCommon.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/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
  YulName const& _originalFunctionName,
34
  YulName const& _linkingFunctionName,
35
  NameDispenser& _nameDispenser
36
)
37
59.9k
{
38
59.9k
  auto generateTypedName = [&](NameWithDebugData t)
39
296k
  {
40
296k
    return NameWithDebugData{
41
296k
      t.debugData,
42
296k
      _nameDispenser.newName(t.name)
43
296k
    };
44
296k
  };
45
46
59.9k
  FunctionDefinition linkingFunction{
47
59.9k
    _original.debugData,
48
59.9k
    _linkingFunctionName,
49
59.9k
    util::applyMap(_original.parameters, generateTypedName),
50
59.9k
    util::applyMap(_original.returnVariables, generateTypedName),
51
59.9k
    {_original.debugData, {}} // body
52
59.9k
  };
53
54
59.9k
  FunctionCall call{_original.debugData, Identifier{_original.debugData, _originalFunctionName}, {}};
55
59.9k
  for (auto const& p: filter(linkingFunction.parameters, _usedParametersAndReturns.first))
56
16.4k
    call.arguments.emplace_back(Identifier{_original.debugData, p.name});
57
58
59.9k
  Assignment assignment{_original.debugData, {}, nullptr};
59
60
59.9k
  for (auto const& r: filter(linkingFunction.returnVariables, _usedParametersAndReturns.second))
61
6.89k
    assignment.variableNames.emplace_back(Identifier{_original.debugData, r.name});
62
63
59.9k
  if (assignment.variableNames.empty())
64
53.5k
    linkingFunction.body.statements.emplace_back(ExpressionStatement{_original.debugData, std::move(call)});
65
6.38k
  else
66
6.38k
  {
67
6.38k
    assignment.value = std::make_unique<Expression>(std::move(call));
68
6.38k
    linkingFunction.body.statements.emplace_back(std::move(assignment));
69
6.38k
  }
70
71
59.9k
  return linkingFunction;
72
59.9k
}