Coverage Report

Created: 2022-08-24 06:43

/src/solidity/libyul/optimiser/ForLoopConditionIntoBody.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/ForLoopConditionIntoBody.h>
20
#include <libyul/optimiser/OptimiserStep.h>
21
#include <libyul/AST.h>
22
23
#include <libsolutil/CommonData.h>
24
25
using namespace std;
26
using namespace solidity;
27
using namespace solidity::yul;
28
29
void ForLoopConditionIntoBody::run(OptimiserStepContext& _context, Block& _ast)
30
31.0k
{
31
31.0k
  ForLoopConditionIntoBody{_context.dialect}(_ast);
32
31.0k
}
33
34
void ForLoopConditionIntoBody::operator()(ForLoop& _forLoop)
35
66.9k
{
36
66.9k
  if (
37
66.9k
    m_dialect.booleanNegationFunction() &&
38
66.9k
    !holds_alternative<Literal>(*_forLoop.condition) &&
39
66.9k
    !holds_alternative<Identifier>(*_forLoop.condition)
40
66.9k
  )
41
5.52k
  {
42
5.52k
    shared_ptr<DebugData const> debugData = debugDataOf(*_forLoop.condition);
43
44
5.52k
    _forLoop.body.statements.emplace(
45
5.52k
      begin(_forLoop.body.statements),
46
5.52k
      If {
47
5.52k
        debugData,
48
5.52k
        make_unique<Expression>(
49
5.52k
          FunctionCall {
50
5.52k
            debugData,
51
5.52k
            {debugData, m_dialect.booleanNegationFunction()->name},
52
5.52k
            util::make_vector<Expression>(std::move(*_forLoop.condition))
53
5.52k
          }
54
5.52k
        ),
55
5.52k
        Block {debugData, util::make_vector<Statement>(Break{{}})}
56
5.52k
      }
57
5.52k
    );
58
5.52k
    _forLoop.condition = make_unique<Expression>(
59
5.52k
      Literal {
60
5.52k
        debugData,
61
5.52k
        LiteralKind::Boolean,
62
5.52k
        "true"_yulstring,
63
5.52k
        m_dialect.boolType
64
5.52k
      }
65
5.52k
    );
66
5.52k
  }
67
66.9k
  ASTModifier::operator()(_forLoop);
68
66.9k
}
69