solidity/libyul/optimiser/NameDispenser.cpp

53 lines
1.5 KiB
C++
Raw Normal View History

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/>.
*/
/**
* 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
#include <libyul/optimiser/NameCollector.h>
#include <libyul/AsmData.h>
2018-12-20 17:55:32 +00:00
#include <libyul/Dialect.h>
2018-02-08 15:55:03 +00:00
using namespace std;
using namespace dev;
using namespace yul;
2018-02-08 15:55:03 +00:00
2018-12-20 17:55:32 +00:00
NameDispenser::NameDispenser(Dialect const& _dialect, Block const& _ast):
NameDispenser(_dialect, NameCollector(_ast).names())
{
}
2018-12-20 17:55:32 +00:00
NameDispenser::NameDispenser(Dialect const& _dialect, set<YulString> _usedNames):
m_dialect(_dialect),
m_usedNames(std::move(_usedNames))
{
}
YulString NameDispenser::newName(YulString _nameHint)
2018-02-08 15:55:03 +00:00
{
YulString name = _nameHint;
2018-12-20 17:55:32 +00:00
while (name.empty() || m_usedNames.count(name) || m_dialect.builtin(name))
2018-02-08 15:55:03 +00:00
{
m_counter++;
name = YulString(_nameHint.str() + "_" + to_string(m_counter));
2018-02-08 15:55:03 +00:00
}
m_usedNames.emplace(name);
2018-02-08 15:55:03 +00:00
return name;
}