mirror of
https://github.com/luau-lang/luau.git
synced 2025-04-04 10:50:54 +01:00
Enforce exhaustiveness of make
.
This commit is contained in:
parent
26f1d18c81
commit
2ef0ccd5e8
2 changed files with 57 additions and 33 deletions
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include "Luau/Common.h"
|
||||
#include "Luau/Id.h"
|
||||
#include "Luau/Language.h"
|
||||
#include "Luau/UnionFind.h"
|
||||
#include "Luau/VecDeque.h"
|
||||
|
||||
|
@ -23,9 +24,19 @@ struct Analysis final
|
|||
|
||||
using D = typename N::Data;
|
||||
|
||||
D make(const EGraph<L, N>& egraph, const L& enode) const
|
||||
template<typename T>
|
||||
static D fnMake(const N& analysis, const EGraph<L, N>& egraph, const L& enode)
|
||||
{
|
||||
return analysis.make(egraph, enode);
|
||||
return analysis.make(egraph, *enode.template get<T>());
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
D make(const EGraph<L, N>& egraph, const Language<Ts...>& enode) const
|
||||
{
|
||||
using FnMake = D (*)(const N&, const EGraph<L, N>&, const L&);
|
||||
static constexpr FnMake tableMake[sizeof...(Ts)] = {&fnMake<Ts>...};
|
||||
|
||||
return tableMake[enode.index()](analysis, egraph, enode);
|
||||
}
|
||||
|
||||
void join(D& a, const D& b)
|
||||
|
|
|
@ -32,38 +32,51 @@ struct ConstantFold
|
|||
{
|
||||
using Data = std::optional<bool>;
|
||||
|
||||
Data make(const EGraph& egraph, const PropositionalLogic& enode) const
|
||||
Data make(const EGraph& egraph, const Var& var) const
|
||||
{
|
||||
if (enode.get<Var>())
|
||||
return std::nullopt;
|
||||
else if (auto b = enode.get<Bool>())
|
||||
return b->value;
|
||||
else if (auto n = enode.get<Not>())
|
||||
{
|
||||
if (auto data = egraph[n->field<Negated>()].data)
|
||||
return !*data;
|
||||
}
|
||||
else if (auto a = enode.get<And>())
|
||||
{
|
||||
Data left = egraph[a->field<Left>()].data;
|
||||
Data right = egraph[a->field<Right>()].data;
|
||||
if (left && right)
|
||||
return *left && *right;
|
||||
}
|
||||
else if (auto o = enode.get<Or>())
|
||||
{
|
||||
Data left = egraph[o->field<Left>()].data;
|
||||
Data right = egraph[o->field<Right>()].data;
|
||||
if (left && right)
|
||||
return *left && *right;
|
||||
}
|
||||
else if (auto i = enode.get<Implies>())
|
||||
{
|
||||
Data antecedent = egraph[i->field<Antecedent>()].data;
|
||||
Data consequent = egraph[i->field<Consequent>()].data;
|
||||
if (antecedent && consequent)
|
||||
return !*antecedent || *consequent;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Data make(const EGraph& egraph, const Bool& b) const
|
||||
{
|
||||
return b.value;
|
||||
}
|
||||
|
||||
Data make(const EGraph& egraph, const Not& n) const
|
||||
{
|
||||
Data data = egraph[n.field<Negated>()].data;
|
||||
if (data)
|
||||
return !*data;
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Data make(const EGraph& egraph, const And& a) const
|
||||
{
|
||||
Data left = egraph[a.field<Left>()].data;
|
||||
Data right = egraph[a.field<Right>()].data;
|
||||
if (left && right)
|
||||
return *left && *right;
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Data make(const EGraph& egraph, const Or& o) const
|
||||
{
|
||||
Data left = egraph[o.field<Left>()].data;
|
||||
Data right = egraph[o.field<Right>()].data;
|
||||
if (left && right)
|
||||
return *left || *right;
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Data make(const EGraph& egraph, const Implies& i) const
|
||||
{
|
||||
Data antecedent = egraph[i.field<Antecedent>()].data;
|
||||
Data consequent = egraph[i.field<Consequent>()].data;
|
||||
if (antecedent && consequent)
|
||||
return !*antecedent || *consequent;
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue