luau/Analysis/src/Def.cpp

53 lines
1.3 KiB
C++
Raw Normal View History

2022-10-21 18:33:43 +01:00
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#include "Luau/Def.h"
2023-10-20 21:36:26 +01:00
#include "Luau/Common.h"
2023-11-10 18:05:48 +00:00
#include "Luau/DenseHash.h"
#include <deque>
2022-10-21 18:33:43 +01:00
namespace Luau
{
2023-10-20 21:36:26 +01:00
bool containsSubscriptedDefinition(DefId def)
2022-10-21 18:33:43 +01:00
{
2023-10-20 21:36:26 +01:00
if (auto cell = get<Cell>(def))
return cell->subscripted;
2023-11-10 18:05:48 +00:00
else if (auto phi = get<Phi>(def))
{
std::deque<DefId> queue(begin(phi->operands), end(phi->operands));
DenseHashSet<const Def*> seen{nullptr};
while (!queue.empty())
{
DefId next = queue.front();
queue.pop_front();
LUAU_ASSERT(!seen.find(next));
if (seen.find(next))
continue;
seen.insert(next);
2023-10-20 21:36:26 +01:00
2023-11-10 18:05:48 +00:00
if (auto cell_ = get<Cell>(next); cell_ && cell_->subscripted)
return true;
else if (auto phi_ = get<Phi>(next))
queue.insert(queue.end(), phi_->operands.begin(), phi_->operands.end());
}
}
2023-10-20 21:36:26 +01:00
return false;
}
DefId DefArena::freshCell(bool subscripted)
{
return NotNull{allocator.allocate(Def{Cell{subscripted}})};
2022-10-21 18:33:43 +01:00
}
2023-11-10 18:05:48 +00:00
DefId DefArena::phi(DefId a, DefId b)
{
if (a == b)
return a;
else
return NotNull{allocator.allocate(Def{Phi{{a, b}}})};
}
2022-10-21 18:33:43 +01:00
} // namespace Luau