2018-02-08 15:55:03 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-02-08 15:55:03 +00:00
|
|
|
/**
|
|
|
|
* Optimiser component that can create new unique names.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/ASTForward.h>
|
2018-10-16 15:01:13 +00:00
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
#include <libyul/YulString.h>
|
|
|
|
|
2018-02-08 15:55:03 +00:00
|
|
|
#include <set>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::yul
|
2018-02-08 15:55:03 +00:00
|
|
|
{
|
2018-12-20 17:55:32 +00:00
|
|
|
struct Dialect;
|
2018-02-08 15:55:03 +00:00
|
|
|
|
2018-10-16 15:01:13 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2018-02-08 15:55:03 +00:00
|
|
|
{
|
2018-10-16 15:01:13 +00:00
|
|
|
public:
|
|
|
|
/// Initialize the name dispenser with all the names used in the given AST.
|
2019-06-19 09:37:22 +00:00
|
|
|
explicit NameDispenser(Dialect const& _dialect, Block const& _ast, std::set<YulString> _reservedNames = {});
|
2018-10-16 15:01:13 +00:00
|
|
|
/// Initialize the name dispenser with the given used names.
|
2018-12-20 17:55:32 +00:00
|
|
|
explicit NameDispenser(Dialect const& _dialect, std::set<YulString> _usedNames);
|
2018-10-16 15:01:13 +00:00
|
|
|
|
2019-03-04 17:03:30 +00:00
|
|
|
/// @returns a currently unused name that should be similar to _nameHint.
|
|
|
|
YulString newName(YulString _nameHint);
|
2018-10-16 15:01:13 +00:00
|
|
|
|
2019-06-19 09:37:22 +00:00
|
|
|
/// Mark @a _name as used, i.e. the dispenser's newName function will not
|
|
|
|
/// return it.
|
|
|
|
void markUsed(YulString _name) { m_usedNames.insert(_name); }
|
|
|
|
|
2020-11-30 11:20:17 +00:00
|
|
|
std::set<YulString> const& usedNames() { return m_usedNames; }
|
|
|
|
|
|
|
|
/// Returns true if `_name` is either used or is a restricted identifier.
|
2019-04-24 11:38:35 +00:00
|
|
|
bool illegalName(YulString _name);
|
2018-10-16 15:01:13 +00:00
|
|
|
|
2020-11-30 11:20:17 +00:00
|
|
|
/// Resets `m_usedNames` with *only* the names that are used in the AST. Also resets value of
|
|
|
|
/// `m_counter` to zero.
|
|
|
|
void reset(Block const& _ast);
|
|
|
|
|
|
|
|
private:
|
2018-12-20 17:55:32 +00:00
|
|
|
Dialect const& m_dialect;
|
2018-10-29 14:12:02 +00:00
|
|
|
std::set<YulString> m_usedNames;
|
2020-11-30 11:20:17 +00:00
|
|
|
std::set<YulString> m_reservedNames;
|
2018-11-07 18:27:15 +00:00
|
|
|
size_t m_counter = 0;
|
2018-02-08 15:55:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|