mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Scoper: Walks Yul AST and assigns scopes
This is taken from DataFlowAnalyzer and will be used in the optimizer step MemoryLoadResolver.
This commit is contained in:
parent
8e9a5a02c2
commit
c9ed116621
@ -160,6 +160,8 @@ add_library(yul
|
|||||||
optimiser/RedundantAssignEliminator.h
|
optimiser/RedundantAssignEliminator.h
|
||||||
optimiser/Rematerialiser.cpp
|
optimiser/Rematerialiser.cpp
|
||||||
optimiser/Rematerialiser.h
|
optimiser/Rematerialiser.h
|
||||||
|
optimiser/Scoper.h
|
||||||
|
optimiser/Scoper.cpp
|
||||||
optimiser/SSAReverser.cpp
|
optimiser/SSAReverser.cpp
|
||||||
optimiser/SSAReverser.h
|
optimiser/SSAReverser.h
|
||||||
optimiser/SSATransform.cpp
|
optimiser/SSATransform.cpp
|
||||||
|
|||||||
88
libyul/optimiser/Scoper.cpp
Normal file
88
libyul/optimiser/Scoper.cpp
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
// SPDX-License-Identifier: GPL-3.0
|
||||||
|
|
||||||
|
#include <libyul/optimiser/Scoper.h>
|
||||||
|
|
||||||
|
#include <libyul/AST.h>
|
||||||
|
#include <libyul/Exceptions.h>
|
||||||
|
|
||||||
|
#include <libsolutil/CommonData.h>
|
||||||
|
|
||||||
|
#include <boost/range/adaptor/reversed.hpp>
|
||||||
|
|
||||||
|
#include <set>
|
||||||
|
|
||||||
|
using namespace solidity::yul;
|
||||||
|
using namespace solidity::util;
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void Scoper::operator()(VariableDeclaration& _varDecl)
|
||||||
|
{
|
||||||
|
set<YulString> names;
|
||||||
|
for (auto const& var: _varDecl.variables)
|
||||||
|
names.emplace(var.name);
|
||||||
|
m_variableScopes.back().variables += names;
|
||||||
|
|
||||||
|
ASTModifier::operator()(_varDecl);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Scoper::operator()(FunctionDefinition& _fun)
|
||||||
|
{
|
||||||
|
pushScope(true);
|
||||||
|
for (auto const& parameter: _fun.parameters)
|
||||||
|
m_variableScopes.back().variables.emplace(parameter.name);
|
||||||
|
for (auto const& var: _fun.returnVariables)
|
||||||
|
{
|
||||||
|
m_variableScopes.back().variables.emplace(var.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTModifier::operator()(_fun);
|
||||||
|
|
||||||
|
popScope();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Scoper::operator()(Block& _block)
|
||||||
|
{
|
||||||
|
size_t numScopes = m_variableScopes.size();
|
||||||
|
pushScope(false);
|
||||||
|
ASTModifier::operator()(_block);
|
||||||
|
popScope();
|
||||||
|
yulAssert(numScopes == m_variableScopes.size(), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Scoper::pushScope(bool _functionScope)
|
||||||
|
{
|
||||||
|
m_variableScopes.emplace_back(_functionScope);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Scoper::popScope()
|
||||||
|
{
|
||||||
|
m_variableScopes.pop_back();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Scoper::inScope(YulString _variableName) const
|
||||||
|
{
|
||||||
|
for (auto const& scope: m_variableScopes | boost::adaptors::reversed)
|
||||||
|
{
|
||||||
|
if (scope.variables.count(_variableName))
|
||||||
|
return true;
|
||||||
|
if (scope.isFunction)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
56
libyul/optimiser/Scoper.h
Normal file
56
libyul/optimiser/Scoper.h
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
// SPDX-License-Identifier: GPL-3.0
|
||||||
|
/**
|
||||||
|
* Base class that assigns scope to Yul AST
|
||||||
|
*/
|
||||||
|
#include <libyul/optimiser/ASTWalker.h>
|
||||||
|
|
||||||
|
#include <libyul/AST.h>
|
||||||
|
|
||||||
|
namespace solidity::yul
|
||||||
|
{
|
||||||
|
|
||||||
|
class Scoper: public ASTModifier
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using ASTModifier::operator();
|
||||||
|
|
||||||
|
void operator()(VariableDeclaration& _varDecl) override;
|
||||||
|
void operator()(FunctionDefinition& _fun) override;
|
||||||
|
void operator()(Block& _block) override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/// Returns true iff the variable is in scope.
|
||||||
|
bool inScope(YulString _variableName) const;
|
||||||
|
|
||||||
|
/// Creates a new inner scope.
|
||||||
|
void pushScope(bool _functionScope);
|
||||||
|
/// Removes the innermost scope and clears all variables in it.
|
||||||
|
virtual void popScope();
|
||||||
|
|
||||||
|
struct Scope
|
||||||
|
{
|
||||||
|
explicit Scope(bool _isFunction): isFunction(_isFunction) {}
|
||||||
|
std::set<YulString> variables;
|
||||||
|
bool isFunction;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<Scope> m_variableScopes;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user