Merge pull request #7778 from ethereum/generic_visitor_cpp17

Rewrite GenericVisitor
This commit is contained in:
chriseth 2019-11-26 16:09:38 +01:00 committed by GitHub
commit 40b4a876eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 59 additions and 123 deletions

View File

@ -20,109 +20,41 @@
#pragma once
#include <functional>
#include <boost/variant/static_visitor.hpp>
namespace dev
{
/// Generic visitor used as follows:
/// std::visit(GenericVisitor<Class1, Class2>(
/// [](Class1& _c) { _c.f(); },
/// [](Class2& _c) { _c.g(); }
/// ), variant);
/// This one does not have a fallback and will fail at
/// compile-time if you do not specify all variants.
/**
* Generic visitor used as follows:
* std::visit(GenericVisitor{
* [](Class1& _c) { _c.f(); },
* [](Class2& _c) { _c.g(); }
* }, variant);
* This one does not have a fallback and will fail at
* compile-time if you do not specify all variants.
*
* Fallback with no return (it will not fail if you do not specify all variants):
* std::visit(GenericVisitor{
* VisitorFallback<>{},
* [](Class1& _c) { _c.f(); },
* [](Class2& _c) { _c.g(); }
* }, variant);
*
* Fallback with return type R (the fallback returns `R{}`:
* std::visit(GenericVisitor{
* VisitorFallback<R>{},
* [](Class1& _c) { _c.f(); },
* [](Class2& _c) { _c.g(); }
* }, variant);
*/
template <class...>
struct GenericVisitor{};
template <typename...> struct VisitorFallback;
template <class Visitable, class... Others>
struct GenericVisitor<Visitable, Others...>: public GenericVisitor<Others...>
{
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))
{}
template <typename R>
struct VisitorFallback<R> { template<typename T> R operator()(T&&) const { return {}; } };
void operator()(Visitable& _v) const { m_visitor(_v); }
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<>
struct VisitorFallback<> { template<typename T> void operator()(T&&) const {} };
template <typename... Visitors> struct GenericVisitor: Visitors... { using Visitors::operator()...; };
template <typename... Visitors> GenericVisitor(Visitors...) -> GenericVisitor<Visitors...>;
}

View File

@ -146,7 +146,7 @@ bool AsmAnalyzer::operator()(Identifier const& _identifier)
solAssert(!_identifier.name.empty(), "");
size_t numErrorsBefore = m_errorReporter.errors().size();
bool success = true;
if (m_currentScope->lookup(_identifier.name, Scope::Visitor(
if (m_currentScope->lookup(_identifier.name, GenericVisitor{
[&](Scope::Variable const& _var)
{
if (!m_activeVariables.count(&_var))
@ -171,7 +171,7 @@ bool AsmAnalyzer::operator()(Identifier const& _identifier)
);
success = false;
}
)))
}))
{
}
else
@ -341,7 +341,7 @@ bool AsmAnalyzer::operator()(FunctionCall const& _funCall)
if (f->literalArguments)
needsLiteralArguments = true;
}
else if (!m_currentScope->lookup(_funCall.functionName.name, Scope::Visitor(
else if (!m_currentScope->lookup(_funCall.functionName.name, GenericVisitor{
[&](Scope::Variable const&)
{
m_errorReporter.typeError(
@ -364,7 +364,7 @@ bool AsmAnalyzer::operator()(FunctionCall const& _funCall)
parameters = _fun.arguments.size();
returns = _fun.returns.size();
}
)))
}))
{
m_errorReporter.declarationError(_funCall.functionName.location, "Function not found.");
success = false;

View File

@ -48,8 +48,6 @@ struct Scope
};
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 registerLabel(YulString _name);

View File

@ -82,14 +82,14 @@ void VariableReferenceCounter::operator()(Block const& _block)
void VariableReferenceCounter::increaseRefIfFound(YulString _variableName)
{
m_scope->lookup(_variableName, Scope::Visitor(
m_scope->lookup(_variableName, GenericVisitor{
[=](Scope::Variable const& _var)
{
++m_context.variableReferences[&_var];
},
[=](Scope::Label const&) { },
[=](Scope::Function const&) { }
));
});
}
CodeTransform::CodeTransform(
@ -302,11 +302,11 @@ void CodeTransform::operator()(FunctionCall const& _call)
}
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::Label&) { solAssert(false, "Expected function name."); },
[&](Scope::Function& _function) { function = &_function; }
)), "Function name not found.");
}), "Function name not found.");
solAssert(function, "");
solAssert(function->arguments.size() == _call.arguments.size(), "");
for (auto const& arg: _call.arguments | boost::adaptors::reversed)
@ -363,7 +363,7 @@ void CodeTransform::operator()(Identifier const& _identifier)
m_assembly.setSourceLocation(_identifier.location);
// First search internals, then externals.
solAssert(m_scope, "");
if (m_scope->lookup(_identifier.name, Scope::NonconstVisitor(
if (m_scope->lookup(_identifier.name, GenericVisitor{
[=](Scope::Variable& _var)
{
// 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.");
}
)))
}))
{
return;
}
@ -682,14 +682,14 @@ void CodeTransform::operator()(Block const& _block)
AbstractAssembly::LabelID CodeTransform::labelFromIdentifier(Identifier const& _identifier)
{
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::Label& _label)
{
label = labelID(_label);
},
[=](Scope::Function&) { solAssert(false, "Expected label"); }
)))
}))
{
solAssert(false, "Identifier not found.");
}

View File

@ -180,7 +180,8 @@ void ControlFlowSimplifier::visit(Statement& _st)
void ControlFlowSimplifier::simplify(std::vector<yul::Statement>& _statements)
{
GenericFallbackReturnsVisitor<OptionalStatements, If, Switch> const visitor(
GenericVisitor visitor{
VisitorFallback<OptionalStatements>{},
[&](If& _ifStmt) -> OptionalStatements {
if (_ifStmt.body.statements.empty() && m_dialect.discardFunction())
{
@ -205,8 +206,7 @@ void ControlFlowSimplifier::simplify(std::vector<yul::Statement>& _statements)
return {};
}
);
};
iterateReplacing(
_statements,
[&](Statement& _stmt) -> OptionalStatements

View File

@ -157,17 +157,19 @@ void InlineModifier::operator()(Block& _block)
std::optional<vector<Statement>> InlineModifier::tryInlineStatement(Statement& _statement)
{
// 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; },
[](Assignment& _s) { return _s.value.get(); },
[](VariableDeclaration& _s) { return _s.value.get(); }
), _statement);
}, _statement);
if (e)
{
// Only inline direct function calls.
FunctionCall* funCall = std::visit(GenericFallbackReturnsVisitor<FunctionCall*, FunctionCall&>(
FunctionCall* funCall = std::visit(GenericVisitor{
VisitorFallback<FunctionCall*>{},
[](FunctionCall& _e) { return &_e; }
), *e);
}, *e);
if (funCall && m_driver.shallInline(*funCall, m_currentFunction))
return performInline(_statement, *funCall);
}
@ -205,7 +207,8 @@ vector<Statement> InlineModifier::performInline(Statement& _statement, FunctionC
Statement newBody = BodyCopier(m_nameDispenser, variableReplacements)(function->body);
newStatements += std::move(std::get<Block>(newBody).statements);
std::visit(GenericFallbackVisitor<Assignment, VariableDeclaration>{
std::visit(GenericVisitor{
VisitorFallback<>{},
[&](Assignment& _assignment)
{
for (size_t i = 0; i < _assignment.variableNames.size(); ++i)

View File

@ -71,7 +71,8 @@ void StructuralSimplifier::operator()(Block& _block)
void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements)
{
GenericFallbackReturnsVisitor<OptionalStatements, If, Switch, ForLoop> const visitor(
GenericVisitor visitor{
VisitorFallback<OptionalStatements>{},
[&](If& _ifStmt) -> OptionalStatements {
if (expressionAlwaysTrue(*_ifStmt.condition))
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 {};
}
);
};
iterateReplacing(
_statements,

View File

@ -30,7 +30,8 @@ void VarDeclInitializer::operator()(Block& _block)
ASTModifier::operator()(_block);
using OptionalStatements = std::optional<vector<Statement>>;
GenericFallbackReturnsVisitor<OptionalStatements, VariableDeclaration> visitor{
GenericVisitor visitor{
VisitorFallback<OptionalStatements>{},
[](VariableDeclaration& _varDecl) -> OptionalStatements
{
if (_varDecl.value)
@ -51,5 +52,6 @@ void VarDeclInitializer::operator()(Block& _block)
}
}
};
iterateReplacing(_block.statements, [&](auto&& _statement) { return std::visit(visitor, _statement); });
}