mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Introduce LiteralRematerializer and thus simplify StructuralSimplifier.
This commit is contained in:
@@ -61,29 +61,7 @@ OptionalStatements replaceConstArgSwitch(Switch& _switchStmt, u256 const& _const
|
||||
|
||||
void StructuralSimplifier::operator()(Block& _block)
|
||||
{
|
||||
pushScope(false);
|
||||
simplify(_block.statements);
|
||||
popScope();
|
||||
}
|
||||
|
||||
boost::optional<dev::u256> StructuralSimplifier::hasLiteralValue(Expression const& _expression) const
|
||||
{
|
||||
Expression const* expr = &_expression;
|
||||
|
||||
if (expr->type() == typeid(Identifier))
|
||||
{
|
||||
Identifier const& ident = boost::get<Identifier>(*expr);
|
||||
if (m_value.count(ident.name))
|
||||
expr = m_value.at(ident.name);
|
||||
}
|
||||
|
||||
if (expr && expr->type() == typeid(Literal))
|
||||
{
|
||||
Literal const& literal = boost::get<Literal>(*expr);
|
||||
return valueOfLiteral(literal);
|
||||
}
|
||||
|
||||
return boost::optional<u256>();
|
||||
}
|
||||
|
||||
void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements)
|
||||
@@ -124,34 +102,24 @@ void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements)
|
||||
|
||||
bool StructuralSimplifier::expressionAlwaysTrue(Expression const& _expression)
|
||||
{
|
||||
return boost::apply_visitor(GenericFallbackReturnsVisitor<bool, Identifier const, Literal const>(
|
||||
[&](Identifier const& _identifier) -> bool {
|
||||
if (auto expr = m_value[_identifier.name])
|
||||
return expressionAlwaysTrue(*expr);
|
||||
return false;
|
||||
},
|
||||
[](Literal const& _literal) -> bool {
|
||||
return
|
||||
(_literal.kind == LiteralKind::Boolean && _literal.value == "true"_yulstring) ||
|
||||
(_literal.kind == LiteralKind::Number && valueOfNumberLiteral(_literal) != u256(0))
|
||||
;
|
||||
}
|
||||
), _expression);
|
||||
if (boost::optional<u256> value = hasLiteralValue(_expression))
|
||||
return *value != 0;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool StructuralSimplifier::expressionAlwaysFalse(Expression const& _expression)
|
||||
{
|
||||
return boost::apply_visitor(GenericFallbackReturnsVisitor<bool, Identifier const, Literal const>(
|
||||
[&](Identifier const& _identifier) -> bool {
|
||||
if (auto expr = m_value[_identifier.name])
|
||||
return expressionAlwaysFalse(*expr);
|
||||
return false;
|
||||
},
|
||||
[](Literal const& _literal) -> bool {
|
||||
return
|
||||
(_literal.kind == LiteralKind::Boolean && _literal.value == "false"_yulstring) ||
|
||||
(_literal.kind == LiteralKind::Number && valueOfNumberLiteral(_literal) == u256(0))
|
||||
;
|
||||
}
|
||||
), _expression);
|
||||
if (boost::optional<u256> value = hasLiteralValue(_expression))
|
||||
return *value == 0;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
boost::optional<dev::u256> StructuralSimplifier::hasLiteralValue(Expression const& _expression) const
|
||||
{
|
||||
if (_expression.type() == typeid(Literal))
|
||||
return valueOfLiteral(boost::get<Literal>(_expression));
|
||||
else
|
||||
return boost::optional<u256>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user