2019-01-21 07:13:31 +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
|
2019-01-21 07:13:31 +00:00
|
|
|
|
|
|
|
#include <libyul/optimiser/VarNameCleaner.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>
|
2019-01-21 07:13:31 +00:00
|
|
|
#include <libyul/Dialect.h>
|
2020-07-06 15:59:32 +00:00
|
|
|
|
2019-01-21 07:13:31 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
|
|
|
#include <climits>
|
|
|
|
#include <iterator>
|
|
|
|
#include <string>
|
|
|
|
#include <regex>
|
2021-09-16 14:33:28 +00:00
|
|
|
#include <limits>
|
2019-01-21 07:13:31 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity::yul;
|
2019-01-21 07:13:31 +00:00
|
|
|
|
|
|
|
VarNameCleaner::VarNameCleaner(
|
|
|
|
Block const& _ast,
|
|
|
|
Dialect const& _dialect,
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<YulString> _namesToKeep
|
2019-01-21 07:13:31 +00:00
|
|
|
):
|
|
|
|
m_dialect{_dialect},
|
2020-06-09 20:17:50 +00:00
|
|
|
m_namesToKeep{std::move(_namesToKeep)},
|
2019-01-21 07:13:31 +00:00
|
|
|
m_translatedNames{}
|
|
|
|
{
|
|
|
|
for (auto const& statement: _ast.statements)
|
2023-08-23 02:03:44 +00:00
|
|
|
if (std::holds_alternative<FunctionDefinition>(statement))
|
2020-06-09 20:17:50 +00:00
|
|
|
m_namesToKeep.insert(std::get<FunctionDefinition>(statement).name);
|
|
|
|
m_usedNames = m_namesToKeep;
|
2019-01-21 07:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VarNameCleaner::operator()(FunctionDefinition& _funDef)
|
|
|
|
{
|
|
|
|
yulAssert(!m_insideFunction, "");
|
|
|
|
m_insideFunction = true;
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<YulString> globalUsedNames = std::move(m_usedNames);
|
2020-06-09 20:17:50 +00:00
|
|
|
m_usedNames = m_namesToKeep;
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, YulString> globalTranslatedNames;
|
2019-01-21 07:13:31 +00:00
|
|
|
swap(globalTranslatedNames, m_translatedNames);
|
|
|
|
|
|
|
|
renameVariables(_funDef.parameters);
|
|
|
|
renameVariables(_funDef.returnVariables);
|
|
|
|
ASTModifier::operator()(_funDef);
|
|
|
|
|
|
|
|
swap(globalUsedNames, m_usedNames);
|
|
|
|
swap(globalTranslatedNames, m_translatedNames);
|
|
|
|
|
|
|
|
m_insideFunction = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VarNameCleaner::operator()(VariableDeclaration& _varDecl)
|
|
|
|
{
|
|
|
|
renameVariables(_varDecl.variables);
|
|
|
|
ASTModifier::operator()(_varDecl);
|
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
void VarNameCleaner::renameVariables(std::vector<TypedName>& _variables)
|
2019-01-21 07:13:31 +00:00
|
|
|
{
|
|
|
|
for (TypedName& typedName: _variables)
|
|
|
|
{
|
|
|
|
auto newName = findCleanName(typedName.name);
|
|
|
|
if (newName != typedName.name)
|
|
|
|
{
|
|
|
|
m_translatedNames[typedName.name] = newName;
|
|
|
|
typedName.name = newName;
|
|
|
|
}
|
|
|
|
m_usedNames.insert(typedName.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VarNameCleaner::operator()(Identifier& _identifier)
|
|
|
|
{
|
|
|
|
auto name = m_translatedNames.find(_identifier.name);
|
|
|
|
if (name != m_translatedNames.end())
|
|
|
|
_identifier.name = name->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
YulString VarNameCleaner::findCleanName(YulString const& _name) const
|
|
|
|
{
|
|
|
|
auto newName = stripSuffix(_name);
|
2019-04-24 11:38:35 +00:00
|
|
|
if (!isUsedName(newName))
|
2019-01-21 07:13:31 +00:00
|
|
|
return newName;
|
|
|
|
|
|
|
|
// create new name with suffix (by finding a free identifier)
|
2023-08-23 02:03:44 +00:00
|
|
|
for (size_t i = 1; i < std::numeric_limits<decltype(i)>::max(); ++i)
|
2019-01-21 07:13:31 +00:00
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
YulString newNameSuffixed = YulString{newName.str() + "_" + std::to_string(i)};
|
2019-01-21 07:13:31 +00:00
|
|
|
if (!isUsedName(newNameSuffixed))
|
|
|
|
return newNameSuffixed;
|
|
|
|
}
|
|
|
|
yulAssert(false, "Exhausted by attempting to find an available suffix.");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VarNameCleaner::isUsedName(YulString const& _name) const
|
|
|
|
{
|
2020-07-06 17:25:12 +00:00
|
|
|
return isRestrictedIdentifier(m_dialect, _name) || m_usedNames.count(_name);
|
2019-01-21 07:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
YulString VarNameCleaner::stripSuffix(YulString const& _name) const
|
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
static std::regex const suffixRegex("(_+[0-9]+)+$");
|
2019-01-21 07:13:31 +00:00
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::smatch suffixMatch;
|
2019-01-21 07:13:31 +00:00
|
|
|
if (regex_search(_name.str(), suffixMatch, suffixRegex))
|
|
|
|
return {YulString{suffixMatch.prefix().str()}};
|
|
|
|
return _name;
|
|
|
|
}
|