Coverage Report

Created: 2025-09-08 08:10

/src/solidity/libyul/optimiser/NameDisplacer.h
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
#pragma once
23
24
#include <libyul/optimiser/ASTWalker.h>
25
#include <libyul/optimiser/NameDispenser.h>
26
27
#include <set>
28
#include <map>
29
30
namespace solidity::yul
31
{
32
class Dialect;
33
34
/**
35
 * Optimiser component that renames identifiers to free up certain names.
36
 *
37
 * Only replaces names that have been defined inside the code. If the code uses
38
 * names to be freed but does not define them, they remain unchanged.
39
 *
40
 * Prerequisites: Disambiguator
41
 */
42
class NameDisplacer: public ASTModifier
43
{
44
public:
45
  explicit NameDisplacer(
46
    NameDispenser& _dispenser,
47
    std::set<YulName> const& _namesToFree
48
  ):
49
    m_nameDispenser(_dispenser),
50
    m_namesToFree(_namesToFree)
51
65.2k
  {
52
65.2k
    for (YulName n: _namesToFree)
53
51.2k
      m_nameDispenser.markUsed(n);
54
65.2k
  }
55
56
  using ASTModifier::operator();
57
  void operator()(Identifier& _identifier) override;
58
  void operator()(VariableDeclaration& _varDecl) override;
59
  void operator()(FunctionDefinition& _function) override;
60
  void operator()(FunctionCall& _funCall) override;
61
  void operator()(Block& _block) override;
62
63
65.1k
  std::map<YulName, YulName> const& translations() const { return m_translations; }
64
65
protected:
66
  /// Check if the newly introduced identifier @a _name has to be replaced.
67
  void checkAndReplaceNew(YulName& _name);
68
  /// Replace the identifier @a _name if it is in the translation map.
69
  void checkAndReplace(YulName& _name) const;
70
71
  NameDispenser& m_nameDispenser;
72
  std::set<YulName> const& m_namesToFree;
73
  std::map<YulName, YulName> m_translations;
74
};
75
76
}