Coverage Report

Created: 2022-08-24 06:55

/src/solidity/libyul/optimiser/NameDisplacer.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 renames identifiers to free up certain names.
20
 */
21
22
#include <libyul/optimiser/NameDisplacer.h>
23
24
#include <libyul/AST.h>
25
26
using namespace std;
27
using namespace solidity;
28
using namespace solidity::yul;
29
30
void NameDisplacer::operator()(Identifier& _identifier)
31
2.60M
{
32
2.60M
  checkAndReplace(_identifier.name);
33
2.60M
}
34
35
void NameDisplacer::operator()(VariableDeclaration& _varDecl)
36
862k
{
37
862k
  for (TypedName& var: _varDecl.variables)
38
979k
    checkAndReplaceNew(var.name);
39
40
862k
  ASTModifier::operator()(_varDecl);
41
862k
}
42
43
void NameDisplacer::operator()(FunctionDefinition& _function)
44
467k
{
45
  // Should have been done in the block already.
46
467k
  yulAssert(!m_namesToFree.count(_function.name), "");
47
48
467k
  for (auto& param: _function.parameters)
49
335k
    checkAndReplaceNew(param.name);
50
467k
  for (auto& retVar: _function.returnVariables)
51
266k
    checkAndReplaceNew(retVar.name);
52
53
467k
  ASTModifier::operator()(_function);
54
467k
}
55
56
void NameDisplacer::operator()(FunctionCall& _funCall)
57
4.76M
{
58
4.76M
  checkAndReplace(_funCall.functionName.name);
59
4.76M
  ASTModifier::operator()(_funCall);
60
4.76M
}
61
62
void NameDisplacer::operator()(Block& _block)
63
2.24M
{
64
  // First replace all the names of function definitions
65
  // because of scoping.
66
2.24M
  for (auto& st: _block.statements)
67
5.02M
    if (holds_alternative<FunctionDefinition>(st))
68
467k
      checkAndReplaceNew(std::get<FunctionDefinition>(st).name);
69
70
2.24M
  ASTModifier::operator()(_block);
71
2.24M
}
72
73
void NameDisplacer::checkAndReplaceNew(YulString& _name)
74
2.04M
{
75
2.04M
  yulAssert(!m_translations.count(_name), "");
76
2.04M
  if (m_namesToFree.count(_name))
77
64.9k
    _name = (m_translations[_name] = m_nameDispenser.newName(_name));
78
2.04M
}
79
80
void NameDisplacer::checkAndReplace(YulString& _name) const
81
7.36M
{
82
7.36M
  if (m_translations.count(_name))
83
247k
    _name = m_translations.at(_name);
84
7.36M
}
85