aboutsummaryrefslogtreecommitdiffstats
path: root/libyul/optimiser/NameDispenser.h
blob: 7311440b4d2ae75f9fc03d5f58a8d500de671ead (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
    This file is part of solidity.

    solidity is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    solidity is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with solidity.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * Optimiser component that can create new unique names.
 */
#pragma once

#include <libyul/ASTDataForward.h>

#include <libyul/YulString.h>

#include <set>

namespace dev
{
namespace yul
{

/**
 * Optimizer component that can be used to generate new names that
 * do not conflict with existing names.
 *
 * Tries to keep names short and appends decimals to disambiguate.
 */
class NameDispenser
{
public:
    /// Initialize the name dispenser with all the names used in the given AST.
    explicit NameDispenser(Block const& _ast);
    /// Initialize the name dispenser with the given used names.
    explicit NameDispenser(std::set<YulString> _usedNames);

    /// @returns a currently unused name that should be similar to _nameHint
    /// and prefixed by _context if present.
    /// If the resulting name would be too long, trims the context at the end
    /// and the name hint at the start.
    YulString newName(YulString _nameHint, YulString _context = {});

private:
    YulString newNameInternal(YulString _nameHint);

    std::set<YulString> m_usedNames;
    size_t m_counter = 0;
};

}
}