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.
|
|
|
|
*/
|
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/NameDispenser.h>
|
2018-02-08 15:55:03 +00:00
|
|
|
|
2018-10-16 15:01:13 +00:00
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
2020-07-06 17:25:12 +00:00
|
|
|
#include <libyul/optimiser/OptimizerUtilities.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2018-12-20 17:55:32 +00:00
|
|
|
#include <libyul/Dialect.h>
|
2020-07-06 17:25:12 +00:00
|
|
|
#include <libyul/YulString.h>
|
2019-04-24 11:38:35 +00:00
|
|
|
|
2020-07-06 15:59:32 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2018-10-16 15:01:13 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
using namespace solidity::util;
|
2018-02-08 15:55:03 +00:00
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
NameDispenser::NameDispenser(Dialect const& _dialect, Block const& _ast, std::set<YulString> _reservedNames):
|
2020-11-30 11:20:17 +00:00
|
|
|
NameDispenser(_dialect, NameCollector(_ast).names() + _reservedNames)
|
2018-10-16 15:01:13 +00:00
|
|
|
{
|
2022-08-23 17:28:45 +00:00
|
|
|
m_reservedNames = std::move(_reservedNames);
|
2018-10-16 15:01:13 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
NameDispenser::NameDispenser(Dialect const& _dialect, std::set<YulString> _usedNames):
|
2018-12-20 17:55:32 +00:00
|
|
|
m_dialect(_dialect),
|
2018-10-16 15:01:13 +00:00
|
|
|
m_usedNames(std::move(_usedNames))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-04 17:03:30 +00:00
|
|
|
YulString NameDispenser::newName(YulString _nameHint)
|
2018-02-08 15:55:03 +00:00
|
|
|
{
|
2018-10-29 14:12:02 +00:00
|
|
|
YulString name = _nameHint;
|
2019-04-24 11:38:35 +00:00
|
|
|
while (illegalName(name))
|
2018-02-08 15:55:03 +00:00
|
|
|
{
|
2018-11-07 18:27:15 +00:00
|
|
|
m_counter++;
|
2023-08-23 02:03:44 +00:00
|
|
|
name = YulString(_nameHint.str() + "_" + std::to_string(m_counter));
|
2018-02-08 15:55:03 +00:00
|
|
|
}
|
2018-10-29 14:12:02 +00:00
|
|
|
m_usedNames.emplace(name);
|
2018-02-08 15:55:03 +00:00
|
|
|
return name;
|
|
|
|
}
|
2019-04-24 11:38:35 +00:00
|
|
|
|
|
|
|
bool NameDispenser::illegalName(YulString _name)
|
|
|
|
{
|
2020-07-06 17:25:12 +00:00
|
|
|
return isRestrictedIdentifier(m_dialect, _name) || m_usedNames.count(_name);
|
2019-04-24 11:38:35 +00:00
|
|
|
}
|
2020-11-30 11:20:17 +00:00
|
|
|
|
|
|
|
void NameDispenser::reset(Block const& _ast)
|
|
|
|
{
|
|
|
|
m_usedNames = NameCollector(_ast).names() + m_reservedNames;
|
|
|
|
m_counter = 0;
|
|
|
|
}
|