Coverage Report

Created: 2025-09-08 08:10

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