Coverage Report

Created: 2025-06-24 07:59

/src/solidity/libyul/optimiser/FunctionHoister.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
 * Optimiser component that changes the code so that it consists of a block starting with
20
 * a single block followed only by function definitions and with no functions defined
21
 * anywhere else.
22
 */
23
24
#include <libyul/optimiser/FunctionHoister.h>
25
#include <libyul/optimiser/OptimizerUtilities.h>
26
#include <libyul/AST.h>
27
28
#include <libsolutil/CommonData.h>
29
30
using namespace solidity;
31
using namespace solidity::yul;
32
33
168k
FunctionHoister::FunctionHoister() = default;
34
168k
FunctionHoister::~FunctionHoister() = default;
35
36
void FunctionHoister::operator()(Block& _block)
37
1.67M
{
38
1.67M
  bool topLevel = m_isTopLevel;
39
1.67M
  m_isTopLevel = false;
40
1.67M
  for (auto&& statement: _block.statements)
41
4.10M
  {
42
4.10M
    std::visit(*this, statement);
43
4.10M
    if (std::holds_alternative<FunctionDefinition>(statement))
44
548k
    {
45
548k
      m_functions.emplace_back(std::move(statement));
46
548k
      statement = Block{_block.debugData, {}};
47
548k
    }
48
4.10M
  }
49
1.67M
  removeEmptyBlocks(_block);
50
1.67M
  if (topLevel)
51
168k
    _block.statements += std::move(m_functions);
52
1.67M
}