Coverage Report

Created: 2022-08-24 06:55

/src/solidity/libyul/optimiser/VarDeclInitializer.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
#include <libyul/optimiser/VarDeclInitializer.h>
20
#include <libyul/AST.h>
21
22
#include <libsolutil/CommonData.h>
23
#include <libsolutil/Visitor.h>
24
#include <libyul/Dialect.h>
25
26
using namespace std;
27
using namespace solidity;
28
using namespace solidity::yul;
29
30
void VarDeclInitializer::operator()(Block& _block)
31
0
{
32
0
  ASTModifier::operator()(_block);
33
34
0
  using OptionalStatements = std::optional<vector<Statement>>;
35
0
  util::GenericVisitor visitor{
36
0
    util::VisitorFallback<OptionalStatements>{},
37
0
    [this](VariableDeclaration& _varDecl) -> OptionalStatements
38
0
    {
39
0
      if (_varDecl.value)
40
0
        return {};
41
42
0
      if (_varDecl.variables.size() == 1)
43
0
      {
44
0
        _varDecl.value = make_unique<Expression>(m_dialect.zeroLiteralForType(_varDecl.variables.front().type));
45
0
        return {};
46
0
      }
47
0
      else
48
0
      {
49
0
        OptionalStatements ret{vector<Statement>{}};
50
0
        for (auto& var: _varDecl.variables)
51
0
        {
52
0
          unique_ptr<Expression> expr = make_unique<Expression >(m_dialect.zeroLiteralForType(var.type));
53
0
          ret->emplace_back(VariableDeclaration{std::move(_varDecl.debugData), {std::move(var)}, std::move(expr)});
54
0
        }
55
0
        return ret;
56
0
      }
57
0
    }
58
0
  };
59
60
0
  util::iterateReplacing(_block.statements, [&](auto&& _statement) { return std::visit(visitor, _statement); });
61
0
}