Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Analysis/include/Luau/DataFlowGraph.h
2727 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#pragma once
3
4
// Do not include LValue. It should never be used here.
5
#include "Luau/Ast.h"
6
#include "Luau/ControlFlow.h"
7
#include "Luau/DenseHash.h"
8
#include "Luau/Def.h"
9
#include "Luau/NotNull.h"
10
#include "Luau/Symbol.h"
11
#include "Luau/TypedAllocator.h"
12
13
#include <unordered_map>
14
15
namespace Luau
16
{
17
18
struct RefinementKey
19
{
20
const RefinementKey* parent = nullptr;
21
DefId def;
22
std::optional<std::string> propName;
23
};
24
25
struct RefinementKeyArena
26
{
27
TypedAllocator<RefinementKey> allocator;
28
29
const RefinementKey* leaf(DefId def);
30
const RefinementKey* node(const RefinementKey* parent, DefId def, const std::string& propName);
31
};
32
33
struct DataFlowGraph
34
{
35
DataFlowGraph(DataFlowGraph&&) = default;
36
DataFlowGraph& operator=(DataFlowGraph&&) = default;
37
38
DefId getDef(const AstExpr* expr) const;
39
// Look up the definition optionally, knowing it may not be present.
40
std::optional<DefId> getDefOptional(const AstExpr* expr) const;
41
42
DefId getDef(const AstLocal* local) const;
43
44
DefId getDef(const AstStatDeclareGlobal* global) const;
45
DefId getDef(const AstStatDeclareFunction* func) const;
46
47
const RefinementKey* getRefinementKey(const AstExpr* expr) const;
48
49
std::optional<Symbol> getSymbolFromDef(const Def* def) const;
50
51
private:
52
DataFlowGraph(NotNull<DefArena> defArena, NotNull<RefinementKeyArena> keyArena);
53
54
DataFlowGraph(const DataFlowGraph&) = delete;
55
DataFlowGraph& operator=(const DataFlowGraph&) = delete;
56
57
NotNull<DefArena> defArena;
58
NotNull<RefinementKeyArena> keyArena;
59
60
DenseHashMap<const AstExpr*, const Def*> astDefs{nullptr};
61
62
// Sometimes we don't have the AstExprLocal* but we have AstLocal*, and sometimes we need to extract that DefId.
63
DenseHashMap<const AstLocal*, const Def*> localDefs{nullptr};
64
65
// There's no AstStatDeclaration, and it feels useless to introduce it just to enforce an invariant in one place.
66
// All keys in this maps are really only statements that ambiently declares a symbol.
67
DenseHashMap<const AstStat*, const Def*> declaredDefs{nullptr};
68
DenseHashMap<const Def*, Symbol> defToSymbol{nullptr};
69
70
DenseHashMap<const AstExpr*, const RefinementKey*> astRefinementKeys{nullptr};
71
friend struct DataFlowGraphBuilder;
72
};
73
74
struct DfgScope
75
{
76
enum ScopeType
77
{
78
Linear,
79
Loop,
80
Function,
81
};
82
83
DfgScope* parent;
84
ScopeType scopeType;
85
86
using Bindings = DenseHashMap<Symbol, const Def*>;
87
using Props = DenseHashMap<const Def*, std::unordered_map<std::string, const Def*>>;
88
89
Bindings bindings{Symbol{}};
90
Props props{nullptr};
91
92
std::optional<DefId> lookup(Symbol symbol) const;
93
std::optional<DefId> lookup(DefId def, const std::string& key) const;
94
95
void inherit(const DfgScope* childScope);
96
};
97
98
struct DataFlowResult
99
{
100
DefId def;
101
const RefinementKey* parent = nullptr;
102
};
103
104
using ScopeStack = std::vector<DfgScope*>;
105
106
struct DataFlowGraphBuilder
107
{
108
static DataFlowGraph build(
109
AstStatBlock* block,
110
NotNull<DefArena> defArena,
111
NotNull<RefinementKeyArena> keyArena,
112
NotNull<struct InternalErrorReporter> handle
113
);
114
115
static DataFlowGraph empty(NotNull<DefArena> defArena, NotNull<RefinementKeyArena> keyArena);
116
117
private:
118
DataFlowGraphBuilder(NotNull<DefArena> defArena, NotNull<RefinementKeyArena> keyArena);
119
120
DataFlowGraphBuilder(const DataFlowGraphBuilder&) = delete;
121
DataFlowGraphBuilder& operator=(const DataFlowGraphBuilder&) = delete;
122
123
DataFlowGraph graph;
124
NotNull<DefArena> defArena;
125
NotNull<RefinementKeyArena> keyArena;
126
127
struct InternalErrorReporter* handle = nullptr;
128
129
/// The arena owning all of the scope allocations for the dataflow graph being built.
130
std::vector<std::unique_ptr<DfgScope>> scopes;
131
132
/// A stack of scopes used by the visitor to see where we are.
133
ScopeStack scopeStack;
134
NotNull<DfgScope> currentScope();
135
136
struct FunctionCapture
137
{
138
std::vector<DefId> captureDefs;
139
std::vector<DefId> allVersions;
140
size_t versionOffset = 0;
141
};
142
143
DenseHashMap<Symbol, FunctionCapture> captures{Symbol{}};
144
void resolveCaptures();
145
146
DfgScope* makeChildScope(DfgScope::ScopeType scopeType = DfgScope::Linear);
147
148
void join(DfgScope* p, DfgScope* a, DfgScope* b);
149
void joinBindings(DfgScope* p, const DfgScope& a, const DfgScope& b);
150
void joinProps(DfgScope* p, const DfgScope& a, const DfgScope& b);
151
152
DefId lookup(Symbol symbol, Location location);
153
DefId lookup(DefId def, const std::string& key, Location location);
154
155
ControlFlow visit(AstStatBlock* b);
156
ControlFlow visitBlockWithoutChildScope(AstStatBlock* b);
157
158
ControlFlow visit(AstStat* s);
159
ControlFlow visit(AstStatIf* i);
160
ControlFlow visit(AstStatWhile* w);
161
ControlFlow visit(AstStatRepeat* r);
162
ControlFlow visit(AstStatBreak* b);
163
ControlFlow visit(AstStatContinue* c);
164
ControlFlow visit(AstStatReturn* r);
165
ControlFlow visit(AstStatExpr* e);
166
ControlFlow visit(AstStatLocal* l);
167
ControlFlow visit(AstStatFor* f);
168
ControlFlow visit(AstStatForIn* f);
169
ControlFlow visit(AstStatAssign* a);
170
ControlFlow visit(AstStatCompoundAssign* c);
171
ControlFlow visit(AstStatFunction* f);
172
ControlFlow visit(AstStatLocalFunction* l);
173
ControlFlow visit(AstStatTypeAlias* t);
174
ControlFlow visit(AstStatTypeFunction* f);
175
ControlFlow visit(AstStatDeclareGlobal* d);
176
ControlFlow visit(AstStatDeclareFunction* d);
177
ControlFlow visit(AstStatDeclareExternType* d);
178
ControlFlow visit(AstStatError* error);
179
180
DataFlowResult visitExpr(AstExpr* e);
181
DataFlowResult visitExpr(AstExprGroup* group);
182
DataFlowResult visitExpr(AstExprLocal* l);
183
DataFlowResult visitExpr(AstExprGlobal* g);
184
DataFlowResult visitExpr(AstExprCall* c);
185
DataFlowResult visitExpr(AstExprIndexName* i);
186
DataFlowResult visitExpr(AstExprIndexExpr* i);
187
188
DataFlowResult visitExpr(AstExprFunction* f);
189
DataFlowResult visitFunction(AstExprFunction* f, NotNull<DfgScope> signatureScope);
190
191
DataFlowResult visitExpr(AstExprTable* t);
192
DataFlowResult visitExpr(AstExprUnary* u);
193
DataFlowResult visitExpr(AstExprBinary* b);
194
DataFlowResult visitExpr(AstExprTypeAssertion* t);
195
DataFlowResult visitExpr(AstExprIfElse* i);
196
DataFlowResult visitExpr(AstExprInterpString* i);
197
DataFlowResult visitExpr(AstExprInstantiate* i);
198
DataFlowResult visitExpr(AstExprError* error);
199
200
void visitLValue(AstExpr* e, DefId incomingDef);
201
DefId visitLValue(AstExprLocal* l, DefId incomingDef);
202
DefId visitLValue(AstExprGlobal* g, DefId incomingDef);
203
DefId visitLValue(AstExprIndexName* i, DefId incomingDef);
204
DefId visitLValue(AstExprIndexExpr* i, DefId incomingDef);
205
DefId visitLValue(AstExprError* e, DefId incomingDef);
206
207
void visitType(AstType* t);
208
void visitType(AstTypeReference* r);
209
void visitType(AstTypeTable* t);
210
void visitType(AstTypeFunction* f);
211
void visitType(AstTypeTypeof* t);
212
void visitType(AstTypeUnion* u);
213
void visitType(AstTypeIntersection* i);
214
void visitType(AstTypeError* error);
215
216
void visitTypePack(AstTypePack* p);
217
void visitTypePack(AstTypePackExplicit* e);
218
void visitTypePack(AstTypePackVariadic* v);
219
void visitTypePack(AstTypePackGeneric* g);
220
221
void visitTypeList(AstTypeList l);
222
223
void visitGenerics(AstArray<AstGenericType*> g);
224
void visitGenericPacks(AstArray<AstGenericTypePack*> g);
225
};
226
227
} // namespace Luau
228
229