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
8 changed files with 59 additions and 123 deletions
+4 -4
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;
-2
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);
+8 -8
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.");
}
+3 -3
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
+8 -5
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)
+3 -2
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,
+3 -1
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); });
}