Allow simplification patterns to signal failure

This commit is contained in:
Alex Beregszaszi 2019-02-25 11:31:35 +00:00 committed by Mathias Baumann
parent c7824932a4
commit f95388011b
4 changed files with 19 additions and 2 deletions

View File

@ -203,6 +203,7 @@ ExpressionClasses::Id ExpressionClasses::tryToSimplify(Expression const& _expr)
//cout << "with rule " << match->first.toString() << endl; //cout << "with rule " << match->first.toString() << endl;
//ExpressionTemplate t(match->second()); //ExpressionTemplate t(match->second());
//cout << "to " << match->second().toString() << endl; //cout << "to " << match->second().toString() << endl;
return rebuildExpression(ExpressionTemplate(match->action(), _expr.item->location())); return rebuildExpression(ExpressionTemplate(match->action(), _expr.item->location()));
} }

View File

@ -36,9 +36,22 @@ namespace solidity
template <class Pattern> template <class Pattern>
struct SimplificationRule struct SimplificationRule
{ {
SimplificationRule(
Pattern _pattern,
std::function<Pattern()> _action,
bool _removesNonConstants,
std::function<bool()> _feasible = {}
):
pattern(std::move(_pattern)),
action(std::move(_action)),
removesNonConstants(_removesNonConstants),
feasible(std::move(_feasible))
{}
Pattern pattern; Pattern pattern;
std::function<Pattern()> action; std::function<Pattern()> action;
bool removesNonConstants; bool removesNonConstants;
std::function<bool()> feasible;
}; };
} }

View File

@ -51,7 +51,9 @@ SimplificationRule<Pattern> const* Rules::findFirstMatch(
for (auto const& rule: m_rules[uint8_t(_expr.item->instruction())]) for (auto const& rule: m_rules[uint8_t(_expr.item->instruction())])
{ {
if (rule.pattern.matches(_expr, _classes)) if (rule.pattern.matches(_expr, _classes))
if (!rule.feasible || rule.feasible())
return &rule; return &rule;
resetMatchGroups(); resetMatchGroups();
} }
return nullptr; return nullptr;

View File

@ -51,6 +51,7 @@ SimplificationRule<Pattern> const* SimplificationRules::findFirstMatch(
{ {
rules.resetMatchGroups(); rules.resetMatchGroups();
if (rule.pattern.matches(_expr, _dialect, _ssaValues)) if (rule.pattern.matches(_expr, _dialect, _ssaValues))
if (!rule.feasible || rule.feasible())
return &rule; return &rule;
} }
return nullptr; return nullptr;