mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Rewrite GenericVisitor
This commit is contained in:
parent
41177bc75c
commit
2bfa3a7c97
@ -20,109 +20,41 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
#include <boost/variant/static_visitor.hpp>
|
|
||||||
|
|
||||||
namespace dev
|
namespace dev
|
||||||
{
|
{
|
||||||
|
|
||||||
/// Generic visitor used as follows:
|
/**
|
||||||
/// std::visit(GenericVisitor<Class1, Class2>(
|
* Generic visitor used as follows:
|
||||||
/// [](Class1& _c) { _c.f(); },
|
* std::visit(GenericVisitor{
|
||||||
/// [](Class2& _c) { _c.g(); }
|
* [](Class1& _c) { _c.f(); },
|
||||||
/// ), variant);
|
* [](Class2& _c) { _c.g(); }
|
||||||
/// This one does not have a fallback and will fail at
|
* }, variant);
|
||||||
/// compile-time if you do not specify all variants.
|
* This one does not have a fallback and will fail at
|
||||||
|
* compile-time if you do not specify all variants.
|
||||||
|
*
|
||||||
|
* Fallback with no return:
|
||||||
|
* std::visit(GenericVisitor{
|
||||||
|
* VisitorFallback<>{},
|
||||||
|
* [](Class1& _c) { _c.f(); },
|
||||||
|
* [](Class2& _c) { _c.g(); }
|
||||||
|
* }, variant);
|
||||||
|
*
|
||||||
|
* Fallback with return type R:
|
||||||
|
* std::visit(GenericVisitor{
|
||||||
|
* VisitorFallback<R>{},
|
||||||
|
* [](Class1& _c) { _c.f(); },
|
||||||
|
* [](Class2& _c) { _c.g(); }
|
||||||
|
* }, variant);
|
||||||
|
*/
|
||||||
|
|
||||||
template <class...>
|
template <typename...> struct VisitorFallback;
|
||||||
struct GenericVisitor{};
|
|
||||||
|
|
||||||
template <class Visitable, class... Others>
|
template <typename R>
|
||||||
struct GenericVisitor<Visitable, Others...>: public GenericVisitor<Others...>
|
struct VisitorFallback<R> { template<typename T> R operator()(T&&) const { return {}; } };
|
||||||
{
|
|
||||||
using GenericVisitor<Others...>::operator ();
|
|
||||||
explicit GenericVisitor(
|
|
||||||
std::function<void(Visitable&)> _visitor,
|
|
||||||
std::function<void(Others&)>... _otherVisitors
|
|
||||||
):
|
|
||||||
GenericVisitor<Others...>(std::move(_otherVisitors)...),
|
|
||||||
m_visitor(std::move(_visitor))
|
|
||||||
{}
|
|
||||||
|
|
||||||
void operator()(Visitable& _v) const { m_visitor(_v); }
|
template<>
|
||||||
|
struct VisitorFallback<> { template<typename T> void operator()(T&&) const {} };
|
||||||
std::function<void(Visitable&)> m_visitor;
|
|
||||||
};
|
|
||||||
template <>
|
|
||||||
struct GenericVisitor<>: public boost::static_visitor<> {
|
|
||||||
void operator()() const {}
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Generic visitor with fallback:
|
|
||||||
/// std::visit(GenericFallbackVisitor<Class1, Class2>(
|
|
||||||
/// [](Class1& _c) { _c.f(); },
|
|
||||||
/// [](Class2& _c) { _c.g(); }
|
|
||||||
/// ), variant);
|
|
||||||
/// This one DOES have a fallback and will NOT fail at
|
|
||||||
/// compile-time if you do not specify all variants.
|
|
||||||
|
|
||||||
template <class...>
|
|
||||||
struct GenericFallbackVisitor{};
|
|
||||||
|
|
||||||
template <class Visitable, class... Others>
|
|
||||||
struct GenericFallbackVisitor<Visitable, Others...>: public GenericFallbackVisitor<Others...>
|
|
||||||
{
|
|
||||||
explicit GenericFallbackVisitor(
|
|
||||||
std::function<void(Visitable&)> _visitor,
|
|
||||||
std::function<void(Others&)>... _otherVisitors
|
|
||||||
):
|
|
||||||
GenericFallbackVisitor<Others...>(std::move(_otherVisitors)...),
|
|
||||||
m_visitor(std::move(_visitor))
|
|
||||||
{}
|
|
||||||
|
|
||||||
using GenericFallbackVisitor<Others...>::operator ();
|
|
||||||
void operator()(Visitable& _v) const { m_visitor(_v); }
|
|
||||||
|
|
||||||
std::function<void(Visitable&)> m_visitor;
|
|
||||||
};
|
|
||||||
template <>
|
|
||||||
struct GenericFallbackVisitor<>: public boost::static_visitor<> {
|
|
||||||
template <class T>
|
|
||||||
void operator()(T&) const { }
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Generic visitor with fallback that can return a value:
|
|
||||||
/// std::visit(GenericFallbackReturnsVisitor<ReturnType, Class1, Class2>(
|
|
||||||
/// [](Class1& _c) { return _c.f(); },
|
|
||||||
/// [](Class2& _c) { return _c.g(); }
|
|
||||||
/// ), variant);
|
|
||||||
/// This one DOES have a fallback and will NOT fail at
|
|
||||||
/// compile-time if you do not specify all variants.
|
|
||||||
/// The fallback {}-constructs the return value.
|
|
||||||
|
|
||||||
template <class R, class...>
|
|
||||||
struct GenericFallbackReturnsVisitor{};
|
|
||||||
|
|
||||||
template <class R, class Visitable, class... Others>
|
|
||||||
struct GenericFallbackReturnsVisitor<R, Visitable, Others...>: public GenericFallbackReturnsVisitor<R, Others...>
|
|
||||||
{
|
|
||||||
explicit GenericFallbackReturnsVisitor(
|
|
||||||
std::function<R(Visitable&)> _visitor,
|
|
||||||
std::function<R(Others&)>... _otherVisitors
|
|
||||||
):
|
|
||||||
GenericFallbackReturnsVisitor<R, Others...>(std::move(_otherVisitors)...),
|
|
||||||
m_visitor(std::move(_visitor))
|
|
||||||
{}
|
|
||||||
|
|
||||||
using GenericFallbackReturnsVisitor<R, Others...>::operator ();
|
|
||||||
R operator()(Visitable& _v) const { return m_visitor(_v); }
|
|
||||||
|
|
||||||
std::function<R(Visitable&)> m_visitor;
|
|
||||||
};
|
|
||||||
template <class R>
|
|
||||||
struct GenericFallbackReturnsVisitor<R>: public boost::static_visitor<R> {
|
|
||||||
template <class T>
|
|
||||||
R operator()(T&) const { return {}; }
|
|
||||||
};
|
|
||||||
|
|
||||||
|
template <typename... Visitors> struct GenericVisitor: Visitors... { using Visitors::operator()...; };
|
||||||
|
template <typename... Visitors> GenericVisitor(Visitors...) -> GenericVisitor<Visitors...>;
|
||||||
}
|
}
|
||||||
|
@ -146,7 +146,7 @@ bool AsmAnalyzer::operator()(Identifier const& _identifier)
|
|||||||
solAssert(!_identifier.name.empty(), "");
|
solAssert(!_identifier.name.empty(), "");
|
||||||
size_t numErrorsBefore = m_errorReporter.errors().size();
|
size_t numErrorsBefore = m_errorReporter.errors().size();
|
||||||
bool success = true;
|
bool success = true;
|
||||||
if (m_currentScope->lookup(_identifier.name, Scope::Visitor(
|
if (m_currentScope->lookup(_identifier.name, GenericVisitor{
|
||||||
[&](Scope::Variable const& _var)
|
[&](Scope::Variable const& _var)
|
||||||
{
|
{
|
||||||
if (!m_activeVariables.count(&_var))
|
if (!m_activeVariables.count(&_var))
|
||||||
@ -171,7 +171,7 @@ bool AsmAnalyzer::operator()(Identifier const& _identifier)
|
|||||||
);
|
);
|
||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
)))
|
}))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -341,7 +341,7 @@ bool AsmAnalyzer::operator()(FunctionCall const& _funCall)
|
|||||||
if (f->literalArguments)
|
if (f->literalArguments)
|
||||||
needsLiteralArguments = true;
|
needsLiteralArguments = true;
|
||||||
}
|
}
|
||||||
else if (!m_currentScope->lookup(_funCall.functionName.name, Scope::Visitor(
|
else if (!m_currentScope->lookup(_funCall.functionName.name, GenericVisitor{
|
||||||
[&](Scope::Variable const&)
|
[&](Scope::Variable const&)
|
||||||
{
|
{
|
||||||
m_errorReporter.typeError(
|
m_errorReporter.typeError(
|
||||||
@ -364,7 +364,7 @@ bool AsmAnalyzer::operator()(FunctionCall const& _funCall)
|
|||||||
parameters = _fun.arguments.size();
|
parameters = _fun.arguments.size();
|
||||||
returns = _fun.returns.size();
|
returns = _fun.returns.size();
|
||||||
}
|
}
|
||||||
)))
|
}))
|
||||||
{
|
{
|
||||||
m_errorReporter.declarationError(_funCall.functionName.location, "Function not found.");
|
m_errorReporter.declarationError(_funCall.functionName.location, "Function not found.");
|
||||||
success = false;
|
success = false;
|
||||||
|
@ -48,8 +48,6 @@ struct Scope
|
|||||||
};
|
};
|
||||||
|
|
||||||
using Identifier = std::variant<Variable, Label, Function>;
|
using Identifier = std::variant<Variable, Label, Function>;
|
||||||
using Visitor = dev::GenericVisitor<Variable const, Label const, Function const>;
|
|
||||||
using NonconstVisitor = dev::GenericVisitor<Variable, Label, Function>;
|
|
||||||
|
|
||||||
bool registerVariable(YulString _name, YulType const& _type);
|
bool registerVariable(YulString _name, YulType const& _type);
|
||||||
bool registerLabel(YulString _name);
|
bool registerLabel(YulString _name);
|
||||||
|
@ -82,14 +82,14 @@ void VariableReferenceCounter::operator()(Block const& _block)
|
|||||||
|
|
||||||
void VariableReferenceCounter::increaseRefIfFound(YulString _variableName)
|
void VariableReferenceCounter::increaseRefIfFound(YulString _variableName)
|
||||||
{
|
{
|
||||||
m_scope->lookup(_variableName, Scope::Visitor(
|
m_scope->lookup(_variableName, GenericVisitor{
|
||||||
[=](Scope::Variable const& _var)
|
[=](Scope::Variable const& _var)
|
||||||
{
|
{
|
||||||
++m_context.variableReferences[&_var];
|
++m_context.variableReferences[&_var];
|
||||||
},
|
},
|
||||||
[=](Scope::Label const&) { },
|
[=](Scope::Label const&) { },
|
||||||
[=](Scope::Function const&) { }
|
[=](Scope::Function const&) { }
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
CodeTransform::CodeTransform(
|
CodeTransform::CodeTransform(
|
||||||
@ -302,11 +302,11 @@ void CodeTransform::operator()(FunctionCall const& _call)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Scope::Function* function = nullptr;
|
Scope::Function* function = nullptr;
|
||||||
solAssert(m_scope->lookup(_call.functionName.name, Scope::NonconstVisitor(
|
solAssert(m_scope->lookup(_call.functionName.name, GenericVisitor{
|
||||||
[=](Scope::Variable&) { solAssert(false, "Expected function name."); },
|
[=](Scope::Variable&) { solAssert(false, "Expected function name."); },
|
||||||
[=](Scope::Label&) { solAssert(false, "Expected function name."); },
|
[=](Scope::Label&) { solAssert(false, "Expected function name."); },
|
||||||
[&](Scope::Function& _function) { function = &_function; }
|
[&](Scope::Function& _function) { function = &_function; }
|
||||||
)), "Function name not found.");
|
}), "Function name not found.");
|
||||||
solAssert(function, "");
|
solAssert(function, "");
|
||||||
solAssert(function->arguments.size() == _call.arguments.size(), "");
|
solAssert(function->arguments.size() == _call.arguments.size(), "");
|
||||||
for (auto const& arg: _call.arguments | boost::adaptors::reversed)
|
for (auto const& arg: _call.arguments | boost::adaptors::reversed)
|
||||||
@ -363,7 +363,7 @@ void CodeTransform::operator()(Identifier const& _identifier)
|
|||||||
m_assembly.setSourceLocation(_identifier.location);
|
m_assembly.setSourceLocation(_identifier.location);
|
||||||
// First search internals, then externals.
|
// First search internals, then externals.
|
||||||
solAssert(m_scope, "");
|
solAssert(m_scope, "");
|
||||||
if (m_scope->lookup(_identifier.name, Scope::NonconstVisitor(
|
if (m_scope->lookup(_identifier.name, GenericVisitor{
|
||||||
[=](Scope::Variable& _var)
|
[=](Scope::Variable& _var)
|
||||||
{
|
{
|
||||||
// TODO: opportunity for optimization: Do not DUP if this is the last reference
|
// TODO: opportunity for optimization: Do not DUP if this is the last reference
|
||||||
@ -383,7 +383,7 @@ void CodeTransform::operator()(Identifier const& _identifier)
|
|||||||
{
|
{
|
||||||
solAssert(false, "Function not removed during desugaring.");
|
solAssert(false, "Function not removed during desugaring.");
|
||||||
}
|
}
|
||||||
)))
|
}))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -682,14 +682,14 @@ void CodeTransform::operator()(Block const& _block)
|
|||||||
AbstractAssembly::LabelID CodeTransform::labelFromIdentifier(Identifier const& _identifier)
|
AbstractAssembly::LabelID CodeTransform::labelFromIdentifier(Identifier const& _identifier)
|
||||||
{
|
{
|
||||||
AbstractAssembly::LabelID label = AbstractAssembly::LabelID(-1);
|
AbstractAssembly::LabelID label = AbstractAssembly::LabelID(-1);
|
||||||
if (!m_scope->lookup(_identifier.name, Scope::NonconstVisitor(
|
if (!m_scope->lookup(_identifier.name, GenericVisitor{
|
||||||
[=](Scope::Variable&) { solAssert(false, "Expected label"); },
|
[=](Scope::Variable&) { solAssert(false, "Expected label"); },
|
||||||
[&](Scope::Label& _label)
|
[&](Scope::Label& _label)
|
||||||
{
|
{
|
||||||
label = labelID(_label);
|
label = labelID(_label);
|
||||||
},
|
},
|
||||||
[=](Scope::Function&) { solAssert(false, "Expected label"); }
|
[=](Scope::Function&) { solAssert(false, "Expected label"); }
|
||||||
)))
|
}))
|
||||||
{
|
{
|
||||||
solAssert(false, "Identifier not found.");
|
solAssert(false, "Identifier not found.");
|
||||||
}
|
}
|
||||||
|
@ -180,7 +180,8 @@ void ControlFlowSimplifier::visit(Statement& _st)
|
|||||||
|
|
||||||
void ControlFlowSimplifier::simplify(std::vector<yul::Statement>& _statements)
|
void ControlFlowSimplifier::simplify(std::vector<yul::Statement>& _statements)
|
||||||
{
|
{
|
||||||
GenericFallbackReturnsVisitor<OptionalStatements, If, Switch> const visitor(
|
GenericVisitor visitor{
|
||||||
|
VisitorFallback<OptionalStatements>{},
|
||||||
[&](If& _ifStmt) -> OptionalStatements {
|
[&](If& _ifStmt) -> OptionalStatements {
|
||||||
if (_ifStmt.body.statements.empty() && m_dialect.discardFunction())
|
if (_ifStmt.body.statements.empty() && m_dialect.discardFunction())
|
||||||
{
|
{
|
||||||
@ -205,8 +206,7 @@ void ControlFlowSimplifier::simplify(std::vector<yul::Statement>& _statements)
|
|||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
);
|
};
|
||||||
|
|
||||||
iterateReplacing(
|
iterateReplacing(
|
||||||
_statements,
|
_statements,
|
||||||
[&](Statement& _stmt) -> OptionalStatements
|
[&](Statement& _stmt) -> OptionalStatements
|
||||||
|
@ -157,17 +157,19 @@ void InlineModifier::operator()(Block& _block)
|
|||||||
std::optional<vector<Statement>> InlineModifier::tryInlineStatement(Statement& _statement)
|
std::optional<vector<Statement>> InlineModifier::tryInlineStatement(Statement& _statement)
|
||||||
{
|
{
|
||||||
// Only inline for expression statements, assignments and variable declarations.
|
// Only inline for expression statements, assignments and variable declarations.
|
||||||
Expression* e = std::visit(GenericFallbackReturnsVisitor<Expression*, ExpressionStatement, Assignment, VariableDeclaration>(
|
Expression* e = std::visit(GenericVisitor{
|
||||||
|
VisitorFallback<Expression*>{},
|
||||||
[](ExpressionStatement& _s) { return &_s.expression; },
|
[](ExpressionStatement& _s) { return &_s.expression; },
|
||||||
[](Assignment& _s) { return _s.value.get(); },
|
[](Assignment& _s) { return _s.value.get(); },
|
||||||
[](VariableDeclaration& _s) { return _s.value.get(); }
|
[](VariableDeclaration& _s) { return _s.value.get(); }
|
||||||
), _statement);
|
}, _statement);
|
||||||
if (e)
|
if (e)
|
||||||
{
|
{
|
||||||
// Only inline direct function calls.
|
// Only inline direct function calls.
|
||||||
FunctionCall* funCall = std::visit(GenericFallbackReturnsVisitor<FunctionCall*, FunctionCall&>(
|
FunctionCall* funCall = std::visit(GenericVisitor{
|
||||||
|
VisitorFallback<FunctionCall*>{},
|
||||||
[](FunctionCall& _e) { return &_e; }
|
[](FunctionCall& _e) { return &_e; }
|
||||||
), *e);
|
}, *e);
|
||||||
if (funCall && m_driver.shallInline(*funCall, m_currentFunction))
|
if (funCall && m_driver.shallInline(*funCall, m_currentFunction))
|
||||||
return performInline(_statement, *funCall);
|
return performInline(_statement, *funCall);
|
||||||
}
|
}
|
||||||
@ -205,7 +207,8 @@ vector<Statement> InlineModifier::performInline(Statement& _statement, FunctionC
|
|||||||
Statement newBody = BodyCopier(m_nameDispenser, variableReplacements)(function->body);
|
Statement newBody = BodyCopier(m_nameDispenser, variableReplacements)(function->body);
|
||||||
newStatements += std::move(std::get<Block>(newBody).statements);
|
newStatements += std::move(std::get<Block>(newBody).statements);
|
||||||
|
|
||||||
std::visit(GenericFallbackVisitor<Assignment, VariableDeclaration>{
|
std::visit(GenericVisitor{
|
||||||
|
VisitorFallback<>{},
|
||||||
[&](Assignment& _assignment)
|
[&](Assignment& _assignment)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < _assignment.variableNames.size(); ++i)
|
for (size_t i = 0; i < _assignment.variableNames.size(); ++i)
|
||||||
|
@ -71,7 +71,8 @@ void StructuralSimplifier::operator()(Block& _block)
|
|||||||
|
|
||||||
void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements)
|
void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements)
|
||||||
{
|
{
|
||||||
GenericFallbackReturnsVisitor<OptionalStatements, If, Switch, ForLoop> const visitor(
|
GenericVisitor visitor{
|
||||||
|
VisitorFallback<OptionalStatements>{},
|
||||||
[&](If& _ifStmt) -> OptionalStatements {
|
[&](If& _ifStmt) -> OptionalStatements {
|
||||||
if (expressionAlwaysTrue(*_ifStmt.condition))
|
if (expressionAlwaysTrue(*_ifStmt.condition))
|
||||||
return {std::move(_ifStmt.body.statements)};
|
return {std::move(_ifStmt.body.statements)};
|
||||||
@ -89,7 +90,7 @@ void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements)
|
|||||||
return {std::move(_forLoop.pre.statements)};
|
return {std::move(_forLoop.pre.statements)};
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
);
|
};
|
||||||
|
|
||||||
iterateReplacing(
|
iterateReplacing(
|
||||||
_statements,
|
_statements,
|
||||||
|
@ -30,7 +30,8 @@ void VarDeclInitializer::operator()(Block& _block)
|
|||||||
ASTModifier::operator()(_block);
|
ASTModifier::operator()(_block);
|
||||||
|
|
||||||
using OptionalStatements = std::optional<vector<Statement>>;
|
using OptionalStatements = std::optional<vector<Statement>>;
|
||||||
GenericFallbackReturnsVisitor<OptionalStatements, VariableDeclaration> visitor{
|
GenericVisitor visitor{
|
||||||
|
VisitorFallback<OptionalStatements>{},
|
||||||
[](VariableDeclaration& _varDecl) -> OptionalStatements
|
[](VariableDeclaration& _varDecl) -> OptionalStatements
|
||||||
{
|
{
|
||||||
if (_varDecl.value)
|
if (_varDecl.value)
|
||||||
@ -51,5 +52,6 @@ void VarDeclInitializer::operator()(Block& _block)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
iterateReplacing(_block.statements, [&](auto&& _statement) { return std::visit(visitor, _statement); });
|
iterateReplacing(_block.statements, [&](auto&& _statement) { return std::visit(visitor, _statement); });
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user