Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Analysis/src/Def.cpp
2725 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#include "Luau/Def.h"
3
4
#include "Luau/Common.h"
5
6
#include <algorithm>
7
8
namespace Luau
9
{
10
11
bool containsSubscriptedDefinition(DefId def)
12
{
13
if (auto cell = get<Cell>(def))
14
return cell->subscripted;
15
else if (auto phi = get<Phi>(def))
16
return std::any_of(phi->operands.begin(), phi->operands.end(), containsSubscriptedDefinition);
17
else
18
return false;
19
}
20
21
void collectOperands(DefId def, std::vector<DefId>* operands)
22
{
23
LUAU_ASSERT(operands);
24
if (std::find(operands->begin(), operands->end(), def) != operands->end())
25
return;
26
else if (get<Cell>(def))
27
operands->push_back(def);
28
else if (auto phi = get<Phi>(def))
29
{
30
// A trivial phi node has no operands to populate, so we push this definition in directly.
31
if (phi->operands.empty())
32
return operands->push_back(def);
33
34
for (const Def* operand : phi->operands)
35
collectOperands(NotNull{operand}, operands);
36
}
37
}
38
39
DefId DefArena::freshCell(Symbol sym, Location location, bool subscripted)
40
{
41
return NotNull{allocator.allocate(Def{Cell{subscripted}, sym, location})};
42
}
43
44
DefId DefArena::phi(DefId a, DefId b)
45
{
46
return phi({a, b});
47
}
48
49
DefId DefArena::phi(const std::vector<DefId>& defs)
50
{
51
std::vector<DefId> operands;
52
for (DefId operand : defs)
53
collectOperands(operand, &operands);
54
55
// There's no need to allocate a Phi node for a singleton set.
56
if (operands.size() == 1)
57
return operands[0];
58
else
59
return NotNull{allocator.allocate(Def{Phi{std::move(operands)}})};
60
}
61
62
} // namespace Luau
63
64