Coverage Report

Created: 2026-10-26 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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 solidity;
27
using namespace solidity::yul;
28
29
void NameDisplacer::operator()(Identifier& _identifier)
30
1.02M
{
31
1.02M
  checkAndReplace(_identifier.name);
32
1.02M
}
33
34
void NameDisplacer::operator()(VariableDeclaration& _varDecl)
35
832k
{
36
832k
  for (NameWithDebugData& var: _varDecl.variables)
37
1.05M
    checkAndReplaceNew(var.name);
38
39
832k
  ASTModifier::operator()(_varDecl);
40
832k
}
41
42
void NameDisplacer::operator()(FunctionDefinition& _function)
43
192k
{
44
  // Should have been done in the block already.
45
192k
  yulAssert(!m_namesToFree.count(_function.name), "");
46
47
192k
  for (auto& param: _function.parameters)
48
341k
    checkAndReplaceNew(param.name);
49
192k
  for (auto& retVar: _function.returnVariables)
50
265k
    checkAndReplaceNew(retVar.name);
51
52
192k
  ASTModifier::operator()(_function);
53
192k
}
54
55
void NameDisplacer::operator()(FunctionCall& _funCall)
56
1.99M
{
57
1.99M
  if (std::holds_alternative<Identifier>(_funCall.functionName))
58
606k
    checkAndReplace(std::get<Identifier>(_funCall.functionName).name);
59
1.99M
  ASTModifier::operator()(_funCall);
60
1.99M
}
61
62
void NameDisplacer::operator()(Block& _block)
63
715k
{
64
  // First replace all the names of function definitions
65
  // because of scoping.
66
715k
  for (auto& st: _block.statements)
67
2.32M
    if (std::holds_alternative<FunctionDefinition>(st))
68
192k
      checkAndReplaceNew(std::get<FunctionDefinition>(st).name);
69
70
715k
  ASTModifier::operator()(_block);
71
715k
}
72
73
void NameDisplacer::checkAndReplaceNew(YulName& _name)
74
1.85M
{
75
1.85M
  yulAssert(!m_translations.count(_name), "");
76
1.85M
  if (m_namesToFree.count(_name))
77
78.1k
    _name = (m_translations[_name] = m_nameDispenser.newName(_name));
78
1.85M
}
79
80
void NameDisplacer::checkAndReplace(YulName& _name) const
81
1.63M
{
82
1.63M
  if (m_translations.count(_name))
83
214k
    _name = m_translations.at(_name);
84
1.63M
}
85