Coverage Report

Created: 2022-08-24 06:43

/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
870k
{
32
870k
  checkAndReplace(_identifier.name);
33
870k
}
34
35
void NameDisplacer::operator()(VariableDeclaration& _varDecl)
36
141k
{
37
141k
  for (TypedName& var: _varDecl.variables)
38
145k
    checkAndReplaceNew(var.name);
39
40
141k
  ASTModifier::operator()(_varDecl);
41
141k
}
42
43
void NameDisplacer::operator()(FunctionDefinition& _function)
44
122k
{
45
  // Should have been done in the block already.
46
122k
  yulAssert(!m_namesToFree.count(_function.name), "");
47
48
122k
  for (auto& param: _function.parameters)
49
147k
    checkAndReplaceNew(param.name);
50
122k
  for (auto& retVar: _function.returnVariables)
51
96.3k
    checkAndReplaceNew(retVar.name);
52
53
122k
  ASTModifier::operator()(_function);
54
122k
}
55
56
void NameDisplacer::operator()(FunctionCall& _funCall)
57
939k
{
58
939k
  checkAndReplace(_funCall.functionName.name);
59
939k
  ASTModifier::operator()(_funCall);
60
939k
}
61
62
void NameDisplacer::operator()(Block& _block)
63
337k
{
64
  // First replace all the names of function definitions
65
  // because of scoping.
66
337k
  for (auto& st: _block.statements)
67
924k
    if (holds_alternative<FunctionDefinition>(st))
68
122k
      checkAndReplaceNew(std::get<FunctionDefinition>(st).name);
69
70
337k
  ASTModifier::operator()(_block);
71
337k
}
72
73
void NameDisplacer::checkAndReplaceNew(YulString& _name)
74
511k
{
75
511k
  yulAssert(!m_translations.count(_name), "");
76
511k
  if (m_namesToFree.count(_name))
77
3.24k
    _name = (m_translations[_name] = m_nameDispenser.newName(_name));
78
511k
}
79
80
void NameDisplacer::checkAndReplace(YulString& _name) const
81
1.81M
{
82
1.81M
  if (m_translations.count(_name))
83
3.25k
    _name = m_translations.at(_name);
84
1.81M
}
85