Coverage Report

Created: 2022-08-24 06:52

/src/solidity/libyul/optimiser/OptimizerUtilities.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
 * Some useful snippets for the optimiser.
20
 */
21
22
#include <libyul/optimiser/OptimizerUtilities.h>
23
24
#include <libyul/backends/evm/EVMDialect.h>
25
26
#include <libyul/Dialect.h>
27
#include <libyul/AST.h>
28
29
#include <liblangutil/Token.h>
30
#include <libsolutil/CommonData.h>
31
32
#include <range/v3/action/remove_if.hpp>
33
34
using namespace std;
35
using namespace solidity;
36
using namespace solidity::langutil;
37
using namespace solidity::util;
38
using namespace solidity::yul;
39
40
void yul::removeEmptyBlocks(Block& _block)
41
0
{
42
0
  auto isEmptyBlock = [](Statement const& _st) -> bool {
43
0
    return holds_alternative<Block>(_st) && std::get<Block>(_st).statements.empty();
44
0
  };
45
0
  ranges::actions::remove_if(_block.statements, isEmptyBlock);
46
0
}
47
48
bool yul::isRestrictedIdentifier(Dialect const& _dialect, YulString const& _identifier)
49
0
{
50
0
  return _identifier.empty() || TokenTraits::isYulKeyword(_identifier.str()) || _dialect.reservedIdentifier(_identifier);
51
0
}
52
53
optional<evmasm::Instruction> yul::toEVMInstruction(Dialect const& _dialect, YulString const& _name)
54
0
{
55
0
  if (auto const* dialect = dynamic_cast<EVMDialect const*>(&_dialect))
56
0
    if (BuiltinFunctionForEVM const* builtin = dialect->builtin(_name))
57
0
      return builtin->instruction;
58
0
  return nullopt;
59
0
}
60
61
void StatementRemover::operator()(Block& _block)
62
0
{
63
0
  util::iterateReplacing(
64
0
    _block.statements,
65
0
    [&](Statement& _statement) -> std::optional<vector<Statement>>
66
0
    {
67
0
      if (m_toRemove.count(&_statement))
68
0
        return {vector<Statement>{}};
69
0
      else
70
0
        return nullopt;
71
0
    }
72
0
  );
73
0
  ASTModifier::operator()(_block);
74
0
}