Coverage Report

Created: 2025-06-24 07:59

/src/solidity/libyul/optimiser/NameDispenser.h
Line
Count
Source (jump to first uncovered line)
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 can create new unique names.
20
 */
21
#pragma once
22
23
#include <libyul/ASTForward.h>
24
25
#include <libyul/YulName.h>
26
27
#include <set>
28
29
namespace solidity::yul
30
{
31
class Dialect;
32
33
/**
34
 * Optimizer component that can be used to generate new names that
35
 * do not conflict with existing names.
36
 *
37
 * Tries to keep names short and appends decimals to disambiguate.
38
 */
39
class NameDispenser
40
{
41
public:
42
  /// Initialize the name dispenser with all the names used in the given AST.
43
  explicit NameDispenser(Dialect const& _dialect, Block const& _ast, std::set<YulName> _reservedNames = {});
44
  /// Initialize the name dispenser with the given used names.
45
  explicit NameDispenser(Dialect const& _dialect, std::set<YulName> _usedNames);
46
47
  /// @returns a currently unused name that should be similar to _nameHint.
48
  YulName newName(YulName _nameHint);
49
50
  /// Mark @a _name as used, i.e. the dispenser's newName function will not
51
  /// return it.
52
630k
  void markUsed(YulName _name) { m_usedNames.insert(_name); }
53
54
0
  std::set<YulName> const& usedNames() { return m_usedNames; }
55
56
  /// Returns true if `_name` is either used or is a restricted identifier.
57
  bool illegalName(YulName _name);
58
59
  /// Resets `m_usedNames` with *only* the names that are used in the AST. Also resets value of
60
  /// `m_counter` to zero.
61
  void reset(Block const& _ast);
62
63
private:
64
  Dialect const& m_dialect;
65
  std::set<YulName> m_usedNames;
66
  std::set<YulName> m_reservedNames;
67
  size_t m_counter = 0;
68
};
69
70
}