@@ -68,7 +68,6 @@ struct DataFlowGraph
68
68
DenseHashMap<const AstExpr*, const Def*> compoundAssignDefs{nullptr };
69
69
70
70
DenseHashMap<const AstExpr*, const RefinementKey*> astRefinementKeys{nullptr };
71
-
72
71
friend struct DataFlowGraphBuilder ;
73
72
};
74
73
@@ -83,6 +82,7 @@ struct DfgScope
83
82
84
83
DfgScope* parent;
85
84
ScopeType scopeType;
85
+ Location location;
86
86
87
87
using Bindings = DenseHashMap<Symbol, const Def*>;
88
88
using Props = DenseHashMap<const Def*, std::unordered_map<std::string, const Def*>>;
@@ -105,10 +105,44 @@ struct DataFlowResult
105
105
const RefinementKey* parent = nullptr ;
106
106
};
107
107
108
+ using ScopeStack = std::vector<DfgScope*>;
109
+
108
110
struct DataFlowGraphBuilder
109
111
{
110
112
static DataFlowGraph build (AstStatBlock* root, NotNull<struct InternalErrorReporter > handle);
111
113
114
+ /* *
115
+ * This method is identical to the build method above, but returns a pair of dfg, scopes as the data flow graph
116
+ * here is intended to live on the module between runs of typechecking. Before, the DFG only needed to live as
117
+ * long as the typecheck, but in a world with incremental typechecking, we need the information on the dfg to incrementally
118
+ * typecheck small fragments of code.
119
+ * @param block - pointer to the ast to build the dfg for
120
+ * @param handle - for raising internal errors while building the dfg
121
+ */
122
+ static std::pair<std::shared_ptr<DataFlowGraph>, std::vector<std::unique_ptr<DfgScope>>> buildShared (
123
+ AstStatBlock* block,
124
+ NotNull<InternalErrorReporter> handle
125
+ );
126
+
127
+ /* *
128
+ * Takes a stale graph along with a list of scopes, a small fragment of the ast, and a cursor position
129
+ * and constructs the DataFlowGraph for just that fragment. This method will fabricate defs in the final
130
+ * DFG for things that have been referenced and exist in the stale dfg.
131
+ * For example, the fragment local z = x + y will populate defs for x and y from the stale graph.
132
+ * @param staleGraph - the old DFG
133
+ * @param scopes - the old DfgScopes in the graph
134
+ * @param fragment - the Ast Fragment to re-build the root for
135
+ * @param cursorPos - the current location of the cursor - used to determine which scope we are currently in
136
+ * @param handle - for internal compiler errors
137
+ */
138
+ static DataFlowGraph updateGraph (
139
+ const DataFlowGraph& staleGraph,
140
+ const std::vector<std::unique_ptr<DfgScope>>& scopes,
141
+ AstStatBlock* fragment,
142
+ const Position& cursorPos,
143
+ NotNull<InternalErrorReporter> handle
144
+ );
145
+
112
146
private:
113
147
DataFlowGraphBuilder () = default ;
114
148
@@ -120,10 +154,15 @@ struct DataFlowGraphBuilder
120
154
NotNull<RefinementKeyArena> keyArena{&graph.keyArena };
121
155
122
156
struct InternalErrorReporter * handle = nullptr ;
123
- DfgScope* moduleScope = nullptr ;
124
157
158
+ // / The arena owning all of the scope allocations for the dataflow graph being built.
125
159
std::vector<std::unique_ptr<DfgScope>> scopes;
126
160
161
+ // / A stack of scopes used by the visitor to see where we are.
162
+ ScopeStack scopeStack;
163
+
164
+ DfgScope* currentScope ();
165
+
127
166
struct FunctionCapture
128
167
{
129
168
std::vector<DefId> captureDefs;
@@ -134,81 +173,81 @@ struct DataFlowGraphBuilder
134
173
DenseHashMap<Symbol, FunctionCapture> captures{Symbol{}};
135
174
void resolveCaptures ();
136
175
137
- DfgScope* childScope (DfgScope* scope , DfgScope::ScopeType scopeType = DfgScope::Linear);
176
+ DfgScope* makeChildScope (Location loc , DfgScope::ScopeType scopeType = DfgScope::Linear);
138
177
139
178
void join (DfgScope* p, DfgScope* a, DfgScope* b);
140
179
void joinBindings (DfgScope* p, const DfgScope& a, const DfgScope& b);
141
180
void joinProps (DfgScope* p, const DfgScope& a, const DfgScope& b);
142
181
143
- DefId lookup (DfgScope* scope, Symbol symbol);
144
- DefId lookup (DfgScope* scope, DefId def, const std::string& key);
145
-
146
- ControlFlow visit (DfgScope* scope, AstStatBlock* b);
147
- ControlFlow visitBlockWithoutChildScope (DfgScope* scope, AstStatBlock* b);
148
-
149
- ControlFlow visit (DfgScope* scope, AstStat* s);
150
- ControlFlow visit (DfgScope* scope, AstStatIf* i);
151
- ControlFlow visit (DfgScope* scope, AstStatWhile* w);
152
- ControlFlow visit (DfgScope* scope, AstStatRepeat* r);
153
- ControlFlow visit (DfgScope* scope, AstStatBreak* b);
154
- ControlFlow visit (DfgScope* scope, AstStatContinue* c);
155
- ControlFlow visit (DfgScope* scope, AstStatReturn* r);
156
- ControlFlow visit (DfgScope* scope, AstStatExpr* e);
157
- ControlFlow visit (DfgScope* scope, AstStatLocal* l);
158
- ControlFlow visit (DfgScope* scope, AstStatFor* f);
159
- ControlFlow visit (DfgScope* scope, AstStatForIn* f);
160
- ControlFlow visit (DfgScope* scope, AstStatAssign* a);
161
- ControlFlow visit (DfgScope* scope, AstStatCompoundAssign* c);
162
- ControlFlow visit (DfgScope* scope, AstStatFunction* f);
163
- ControlFlow visit (DfgScope* scope, AstStatLocalFunction* l);
164
- ControlFlow visit (DfgScope* scope, AstStatTypeAlias* t);
165
- ControlFlow visit (DfgScope* scope, AstStatTypeFunction* f);
166
- ControlFlow visit (DfgScope* scope, AstStatDeclareGlobal* d);
167
- ControlFlow visit (DfgScope* scope, AstStatDeclareFunction* d);
168
- ControlFlow visit (DfgScope* scope, AstStatDeclareClass* d);
169
- ControlFlow visit (DfgScope* scope, AstStatError* error);
170
-
171
- DataFlowResult visitExpr (DfgScope* scope, AstExpr* e);
172
- DataFlowResult visitExpr (DfgScope* scope, AstExprGroup* group);
173
- DataFlowResult visitExpr (DfgScope* scope, AstExprLocal* l);
174
- DataFlowResult visitExpr (DfgScope* scope, AstExprGlobal* g);
175
- DataFlowResult visitExpr (DfgScope* scope, AstExprCall* c);
176
- DataFlowResult visitExpr (DfgScope* scope, AstExprIndexName* i);
177
- DataFlowResult visitExpr (DfgScope* scope, AstExprIndexExpr* i);
178
- DataFlowResult visitExpr (DfgScope* scope, AstExprFunction* f);
179
- DataFlowResult visitExpr (DfgScope* scope, AstExprTable* t);
180
- DataFlowResult visitExpr (DfgScope* scope, AstExprUnary* u);
181
- DataFlowResult visitExpr (DfgScope* scope, AstExprBinary* b);
182
- DataFlowResult visitExpr (DfgScope* scope, AstExprTypeAssertion* t);
183
- DataFlowResult visitExpr (DfgScope* scope, AstExprIfElse* i);
184
- DataFlowResult visitExpr (DfgScope* scope, AstExprInterpString* i);
185
- DataFlowResult visitExpr (DfgScope* scope, AstExprError* error);
186
-
187
- void visitLValue (DfgScope* scope, AstExpr* e, DefId incomingDef);
188
- DefId visitLValue (DfgScope* scope, AstExprLocal* l, DefId incomingDef);
189
- DefId visitLValue (DfgScope* scope, AstExprGlobal* g, DefId incomingDef);
190
- DefId visitLValue (DfgScope* scope, AstExprIndexName* i, DefId incomingDef);
191
- DefId visitLValue (DfgScope* scope, AstExprIndexExpr* i, DefId incomingDef);
192
- DefId visitLValue (DfgScope* scope, AstExprError* e, DefId incomingDef);
193
-
194
- void visitType (DfgScope* scope, AstType* t);
195
- void visitType (DfgScope* scope, AstTypeReference* r);
196
- void visitType (DfgScope* scope, AstTypeTable* t);
197
- void visitType (DfgScope* scope, AstTypeFunction* f);
198
- void visitType (DfgScope* scope, AstTypeTypeof* t);
199
- void visitType (DfgScope* scope, AstTypeUnion* u);
200
- void visitType (DfgScope* scope, AstTypeIntersection* i);
201
- void visitType (DfgScope* scope, AstTypeError* error);
202
-
203
- void visitTypePack (DfgScope* scope, AstTypePack* p);
204
- void visitTypePack (DfgScope* scope, AstTypePackExplicit* e);
205
- void visitTypePack (DfgScope* scope, AstTypePackVariadic* v);
206
- void visitTypePack (DfgScope* scope, AstTypePackGeneric* g);
207
-
208
- void visitTypeList (DfgScope* scope, AstTypeList l);
209
-
210
- void visitGenerics (DfgScope* scope, AstArray<AstGenericType> g);
211
- void visitGenericPacks (DfgScope* scope, AstArray<AstGenericTypePack> g);
182
+ DefId lookup (Symbol symbol);
183
+ DefId lookup (DefId def, const std::string& key);
184
+
185
+ ControlFlow visit (AstStatBlock* b);
186
+ ControlFlow visitBlockWithoutChildScope (AstStatBlock* b);
187
+
188
+ ControlFlow visit (AstStat* s);
189
+ ControlFlow visit (AstStatIf* i);
190
+ ControlFlow visit (AstStatWhile* w);
191
+ ControlFlow visit (AstStatRepeat* r);
192
+ ControlFlow visit (AstStatBreak* b);
193
+ ControlFlow visit (AstStatContinue* c);
194
+ ControlFlow visit (AstStatReturn* r);
195
+ ControlFlow visit (AstStatExpr* e);
196
+ ControlFlow visit (AstStatLocal* l);
197
+ ControlFlow visit (AstStatFor* f);
198
+ ControlFlow visit (AstStatForIn* f);
199
+ ControlFlow visit (AstStatAssign* a);
200
+ ControlFlow visit (AstStatCompoundAssign* c);
201
+ ControlFlow visit (AstStatFunction* f);
202
+ ControlFlow visit (AstStatLocalFunction* l);
203
+ ControlFlow visit (AstStatTypeAlias* t);
204
+ ControlFlow visit (AstStatTypeFunction* f);
205
+ ControlFlow visit (AstStatDeclareGlobal* d);
206
+ ControlFlow visit (AstStatDeclareFunction* d);
207
+ ControlFlow visit (AstStatDeclareClass* d);
208
+ ControlFlow visit (AstStatError* error);
209
+
210
+ DataFlowResult visitExpr (AstExpr* e);
211
+ DataFlowResult visitExpr (AstExprGroup* group);
212
+ DataFlowResult visitExpr (AstExprLocal* l);
213
+ DataFlowResult visitExpr (AstExprGlobal* g);
214
+ DataFlowResult visitExpr (AstExprCall* c);
215
+ DataFlowResult visitExpr (AstExprIndexName* i);
216
+ DataFlowResult visitExpr (AstExprIndexExpr* i);
217
+ DataFlowResult visitExpr (AstExprFunction* f);
218
+ DataFlowResult visitExpr (AstExprTable* t);
219
+ DataFlowResult visitExpr (AstExprUnary* u);
220
+ DataFlowResult visitExpr (AstExprBinary* b);
221
+ DataFlowResult visitExpr (AstExprTypeAssertion* t);
222
+ DataFlowResult visitExpr (AstExprIfElse* i);
223
+ DataFlowResult visitExpr (AstExprInterpString* i);
224
+ DataFlowResult visitExpr (AstExprError* error);
225
+
226
+ void visitLValue (AstExpr* e, DefId incomingDef);
227
+ DefId visitLValue (AstExprLocal* l, DefId incomingDef);
228
+ DefId visitLValue (AstExprGlobal* g, DefId incomingDef);
229
+ DefId visitLValue (AstExprIndexName* i, DefId incomingDef);
230
+ DefId visitLValue (AstExprIndexExpr* i, DefId incomingDef);
231
+ DefId visitLValue (AstExprError* e, DefId incomingDef);
232
+
233
+ void visitType (AstType* t);
234
+ void visitType (AstTypeReference* r);
235
+ void visitType (AstTypeTable* t);
236
+ void visitType (AstTypeFunction* f);
237
+ void visitType (AstTypeTypeof* t);
238
+ void visitType (AstTypeUnion* u);
239
+ void visitType (AstTypeIntersection* i);
240
+ void visitType (AstTypeError* error);
241
+
242
+ void visitTypePack (AstTypePack* p);
243
+ void visitTypePack (AstTypePackExplicit* e);
244
+ void visitTypePack (AstTypePackVariadic* v);
245
+ void visitTypePack (AstTypePackGeneric* g);
246
+
247
+ void visitTypeList (AstTypeList l);
248
+
249
+ void visitGenerics (AstArray<AstGenericType> g);
250
+ void visitGenericPacks (AstArray<AstGenericTypePack> g);
212
251
};
213
252
214
253
} // namespace Luau
0 commit comments