Coverage Report

Created: 2022-08-24 06:38

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