Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Analysis/src/AstUtils.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
3
#include "Luau/Ast.h"
4
#include "Luau/Type.h"
5
6
namespace Luau
7
{
8
9
struct AstExprTableFinder : AstVisitor
10
{
11
NotNull<DenseHashSet<TypeId>> result;
12
NotNull<const DenseHashMap<const AstExpr*, TypeId>> astTypes;
13
14
explicit AstExprTableFinder(NotNull<DenseHashSet<TypeId>> result, NotNull<const DenseHashMap<const AstExpr*, TypeId>> astTypes)
15
: result(result)
16
, astTypes(astTypes)
17
{
18
}
19
20
bool visit(AstExpr* expr) override
21
{
22
return false;
23
}
24
25
bool visit(AstExprTable* tbl) override
26
{
27
const TypeId* ty = astTypes->find(tbl);
28
LUAU_ASSERT(ty);
29
if (ty)
30
result->insert(*ty);
31
32
return true;
33
}
34
};
35
36
void findUniqueTypes(NotNull<DenseHashSet<TypeId>> uniqueTypes, AstExpr* expr, NotNull<const DenseHashMap<const AstExpr*, TypeId>> astTypes)
37
{
38
AstExprTableFinder finder{uniqueTypes, astTypes};
39
expr->visit(&finder);
40
}
41
42
template<typename Iter>
43
void findUniqueTypes(
44
NotNull<DenseHashSet<TypeId>> uniqueTypes,
45
Iter startIt,
46
Iter endIt,
47
NotNull<const DenseHashMap<const AstExpr*, TypeId>> astTypes
48
)
49
{
50
while (startIt != endIt)
51
{
52
AstExpr* expr = *startIt;
53
if (expr->is<AstExprTable>())
54
findUniqueTypes(uniqueTypes, expr, astTypes);
55
++startIt;
56
}
57
}
58
59
60
void findUniqueTypes(
61
NotNull<DenseHashSet<TypeId>> uniqueTypes,
62
AstArray<AstExpr*> exprs,
63
NotNull<const DenseHashMap<const AstExpr*, TypeId>> astTypes
64
)
65
{
66
findUniqueTypes(uniqueTypes, exprs.begin(), exprs.end(), astTypes);
67
}
68
69
void findUniqueTypes(
70
NotNull<DenseHashSet<TypeId>> uniqueTypes,
71
const std::vector<AstExpr*>& exprs,
72
NotNull<const DenseHashMap<const AstExpr*, TypeId>> astTypes
73
)
74
{
75
findUniqueTypes(uniqueTypes, exprs.begin(), exprs.end(), astTypes);
76
}
77
78
} // namespace Luau
79
80