Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(372)

Side by Side Diff: pkg/compiler/lib/src/js_model/closure_visitors.dart

Issue 2964783002: Reapply "Added for-loop variable tracking and regular closures/initializers captured variable track… (Closed)
Patch Set: . Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'package:kernel/ast.dart' as ir;
6
7 import '../closure.dart';
8 import '../elements/entities.dart';
9 import 'closure.dart';
10 import '../kernel/element_map.dart';
11
12 /// This builder walks the code to determine what variables are captured/free at
13 /// various points to build ClosureScope that can respond to queries
14 /// about how a particular variable is being used at any point in the code.
15 class ClosureScopeBuilder extends ir.Visitor {
16 /// A map of each visited call node with the associated information about what
17 /// variables are captured/used. Each ir.Node key corresponds to a scope that
18 /// was encountered while visiting a closure (initially called through
19 /// [translateLazyIntializer] or [translateConstructorOrProcedure]).
20 Map<ir.Node, ClosureScope> _closureInfoMap = <ir.Node, ClosureScope>{};
21
22 /// A map of the nodes that we have flagged as necessary to generate closure
23 /// classes for in a later stage. We map that node to information ascertained
24 /// about variable usage in the surrounding scope.
25 Map<ir.Node /* ir.Field | ir.FunctionNode */, ScopeInfo> _closuresToGenerate =
26 <ir.Node, ScopeInfo>{};
27
28 /// The local variables that have been declared in the current scope.
29 List<ir.VariableDeclaration> _scopeVariables;
30
31 /// Pointer to the context in which this closure is executed.
32 /// For example, in the expression `var foo = () => 3 + i;`, the executable
33 /// context as we walk the nodes in that expression is the ir.Field `foo`.
34 ir.Node _executableContext;
35
36 /// A flag to indicate if we are currently inside a closure.
37 bool _isInsideClosure = false;
38
39 /// Pointer to the original node where this closure builder started.
40 ir.Node _outermostNode;
41
42 /// Keep track of the mutated local variables so that we don't need to box
43 /// non-mutated variables.
44 Set<ir.VariableDeclaration> _mutatedVariables =
45 new Set<ir.VariableDeclaration>();
46
47 /// The set of variables that are accessed in some form, whether they are
48 /// mutated or not.
49 Set<ir.VariableDeclaration> _capturedVariables =
50 new Set<ir.VariableDeclaration>();
51
52 /// If true, the visitor is currently traversing some nodes that are inside a
53 /// try block.
54 bool _inTry = false;
55
56 /// Lookup the local entity that corresponds to a kernel variable declaration.
57 final KernelToLocalsMap _localsMap;
58
59 /// The current scope we are in.
60 KernelScopeInfo _currentScopeInfo;
61
62 final KernelToElementMap _kernelToElementMap;
63
64 ClosureScopeBuilder(this._closureInfoMap, this._closuresToGenerate,
65 this._localsMap, this._kernelToElementMap);
66
67 /// Update the [ClosureScope] object corresponding to
68 /// this node if any variables are captured.
69 void attachCapturedScopeVariables(ir.Node node) {
70 Set<Local> capturedVariablesForScope = new Set<Local>();
71
72 for (ir.VariableDeclaration variable in _scopeVariables) {
73 // No need to box non-assignable elements.
74 if (variable.isFinal || variable.isConst) continue;
75 if (!_mutatedVariables.contains(variable)) continue;
76 if (_capturedVariables.contains(variable)) {
77 capturedVariablesForScope.add(_localsMap.getLocal(variable));
78 }
79 }
80 if (!capturedVariablesForScope.isEmpty) {
81 ThisLocal thisLocal = null;
82 if (node is ir.Member && node.isInstanceMember) {
83 if (node is ir.Procedure) {
84 thisLocal = new ThisLocal(_kernelToElementMap.getMethod(node));
85 } else if (node is ir.Field) {
86 thisLocal = new ThisLocal(_kernelToElementMap.getField(node));
87 }
88 } else if (node is ir.Constructor) {
89 thisLocal = new ThisLocal(_kernelToElementMap.getConstructor(node));
90 }
91
92 Entity context;
93 if (_executableContext is ir.Member) {
94 context = _kernelToElementMap.getMember(_executableContext);
95 } else {
96 context = _kernelToElementMap.getLocalFunction(_executableContext);
97 }
98 _closureInfoMap[node] =
99 new KernelClosureScope(capturedVariablesForScope, context, thisLocal);
100 }
101 }
102
103 /// Perform book-keeping with the current set of local variables that have
104 /// been seen thus far before entering this new scope.
105 void enterNewScope(ir.Node node, Function visitNewScope) {
106 List<ir.VariableDeclaration> oldScopeVariables = _scopeVariables;
107 _scopeVariables = <
108 ir.VariableDeclaration>[]; // TODO: need to construct a new _curentScope Info as we... enter a new scope! or do we do it earlier because the for loop cal ls this method too.
Siggi Cherem (dart-lang) 2017/07/06 19:15:32 nit: 80 col :)
Emily Fortuna 2017/07/06 19:29:41 oh geez. sorry. I clearly didn't take out all my d
109 visitNewScope();
110 attachCapturedScopeVariables(node);
111 _mutatedVariables.removeAll(_scopeVariables);
112 _scopeVariables = oldScopeVariables;
113 }
114
115 @override
116 void defaultNode(ir.Node node) {
117 node.visitChildren(this);
118 }
119
120 @override
121 visitTryCatch(ir.TryCatch node) {
122 bool oldInTry = _inTry;
123 _inTry = true;
124 node.visitChildren(this);
125 _inTry = oldInTry;
126 }
127
128 @override
129 visitTryFinally(ir.TryFinally node) {
130 bool oldInTry = _inTry;
131 _inTry = true;
132 node.visitChildren(this);
133 _inTry = oldInTry;
134 }
135
136 @override
137 visitVariableGet(ir.VariableGet node) {
138 _markVariableAsUsed(node.variable);
139 }
140
141 @override
142 visitVariableSet(ir.VariableSet node) {
143 _mutatedVariables.add(node.variable);
144 _markVariableAsUsed(node.variable);
145 node.visitChildren(this);
146 }
147
148 /// Add this variable to the set of free variables if appropriate and add to
149 /// the tally of variables used in try or sync blocks.
150 void _markVariableAsUsed(ir.VariableDeclaration variable) {
151 if (_isInsideClosure && !_inCurrentContext(variable)) {
152 // If the element is not declared in the current function and the element
153 // is not the closure itself we need to mark the element as free variable.
154 // Note that the check on [insideClosure] is not just an
155 // optimization: factories have type parameters as function
156 // parameters, and type parameters are declared in the class, not
157 // the factory.
158 _currentScopeInfo.freeVariables.add(variable);
159 print('we add variable $variable');
Siggi Cherem (dart-lang) 2017/07/06 19:15:32 delete
Emily Fortuna 2017/07/06 19:29:40 Done.
160 }
161 if (_inTry) {
162 _currentScopeInfo.localsUsedInTryOrSync
163 .add(_localsMap.getLocal(variable));
164 }
165 }
166
167 @override
168 void visitForStatement(ir.ForStatement node) {
169 List<Local> boxedLoopVariables = <Local>[];
170 enterNewScope(node, () {
171 // First visit initialized variables and update steps so we can easily
172 // check if a loop variable was captured in one of these subexpressions.
173 node.variables
174 .forEach((ir.VariableDeclaration variable) => variable.accept(this));
175 node.updates
176 .forEach((ir.Expression expression) => expression.accept(this));
177
178 // Loop variables that have not been captured yet can safely be flagged as
179 // non-mutated, because no nested function can observe the mutation.
180 for (ir.VariableDeclaration variable in node.variables) {
181 if (!_capturedVariables.contains(variable)) {
182 _mutatedVariables.remove(variable);
183 }
184 }
185
186 // Visit condition and body.
187 // This must happen after the above, so any loop variables mutated in the
188 // condition or body are indeed flagged as mutated.
189 if (node.condition != null) node.condition.accept(this);
190 node.body.accept(this);
191
192 // See if we have declared loop variables that need to be boxed.
193 for (ir.VariableDeclaration variable in node.variables) {
194 // Non-mutated variables should not be boxed. The _mutatedVariables set
195 // gets cleared when `enterNewScope` returns, so check it here.
196 if (_capturedVariables.contains(variable) &&
197 _mutatedVariables.contains(variable)) {
198 boxedLoopVariables.add(_localsMap.getLocal(variable));
199 }
200 }
201 });
202 KernelClosureScope scope = _closureInfoMap[node];
203 if (scope == null) return;
204 _closureInfoMap[node] = new KernelLoopClosureScope(scope.boxedVariables,
205 boxedLoopVariables, scope.context, scope.thisLocal);
206 }
207
208 void visitInvokable(ir.TreeNode node) {
209 bool oldIsInsideClosure = _isInsideClosure;
210 ir.Node oldExecutableContext = _executableContext;
211 KernelScopeInfo oldScopeInfo = _currentScopeInfo;
212
213 // _outermostNode is only null the first time we enter the body of the
214 // field, constructor, or method that is being analyzed.
215 _isInsideClosure = _outermostNode != null;
216 _executableContext = node;
217 if (!_isInsideClosure) {
218 _outermostNode = node;
219 }
220 _currentScopeInfo = new KernelScopeInfo(_nodeToThisLocal(node));
221 _closuresToGenerate[node] = _currentScopeInfo;
222
223 enterNewScope(node, () {
224 node.visitChildren(this);
225 });
226
227 KernelScopeInfo savedScopeInfo = _currentScopeInfo;
228 bool savedIsInsideClosure = _isInsideClosure;
229
230 // Restore old values.
231 _isInsideClosure = oldIsInsideClosure;
232 _currentScopeInfo = oldScopeInfo;
233 _executableContext = oldExecutableContext;
234
235 // Mark all free variables as captured and expect to encounter them in the
236 // outer function.
237 Iterable<ir.VariableDeclaration> freeVariables =
238 savedScopeInfo.freeVariables;
239 print(
Siggi Cherem (dart-lang) 2017/07/06 19:15:32 ditto
Emily Fortuna 2017/07/06 19:29:41 Done.
240 'emtpy? ${freeVariables.isEmpty} ${freeVariables} in closure? ${savedIsI nsideClosure} $savedScopeInfo');
241 assert(freeVariables.isEmpty || savedIsInsideClosure);
242 for (ir.VariableDeclaration freeVariable in freeVariables) {
243 assert(!_capturedVariables.contains(freeVariable));
244 _capturedVariables.add(freeVariable);
245 _markVariableAsUsed(freeVariable);
246 }
247 }
248
249 /// Return true if [variable]'s context is the same as the current executable
250 /// context.
251 bool _inCurrentContext(ir.VariableDeclaration variable) {
252 ir.TreeNode node = variable;
253 while (node.parent != null) {
Siggi Cherem (dart-lang) 2017/07/06 19:15:31 did you find cases where the node is not under the
Emily Fortuna 2017/07/06 19:29:41 Done.
254 if (node == _executableContext) return true;
255 node = node.parent;
256 }
257 return node == _executableContext;
258 /*while (node != _outermostNode) {
Siggi Cherem (dart-lang) 2017/07/06 19:15:32 delete?
Emily Fortuna 2017/07/06 19:29:41 Done.
259 if (node == _executableContext) return true;
260 node = node.parent;
261 }
262 return node == _executableContext;*/
263 }
264
265 void translateLazyInitializer(ir.Field field) {
266 visitInvokable(field);
267 }
268
269 void translateConstructorOrProcedure(ir.Node constructorOrProcedure) {
270 constructorOrProcedure.accept(this);
271 }
272
273 void visitFunctionNode(ir.FunctionNode functionNode) {
274 visitInvokable(functionNode);
275 }
276
277 /// If [node] is an instance member return the corresponding `this` reference.
278 /// If not, return null.
279 Entity _nodeToThisLocal(
280 ir.TreeNode /* ir.Field | ir.FunctionNode | ir.Constructor | ir.Procedure */ node) {
281 ir.Node nodeToConvert = node;
282 if (nodeToConvert is ir.Field && nodeToConvert.isInstanceMember) {
Siggi Cherem (dart-lang) 2017/07/06 19:15:32 Maybe split in two conditions, so you can return q
Emily Fortuna 2017/07/06 19:29:41 Done.
283 return new ThisLocal(_kernelToElementMap.getField(nodeToConvert));
284 } else {
285 if (nodeToConvert is ir.FunctionNode) {
286 // Step up one node higher to find the corresponding entity for this
287 // node.
288 nodeToConvert = node.parent;
289 }
290 if (nodeToConvert is ir.Constructor ||
291 (nodeToConvert is ir.Procedure &&
292 nodeToConvert.kind == ir.ProcedureKind.Factory &&
293 nodeToConvert.isInstanceMember)) {
294 return new ThisLocal(_kernelToElementMap.getConstructor(nodeToConvert));
295 } else if (nodeToConvert is ir.Procedure &&
296 nodeToConvert.isInstanceMember) {
297 return new ThisLocal(_kernelToElementMap.getMethod(nodeToConvert));
298 } else {
299 return null;
300 }
301 }
Siggi Cherem (dart-lang) 2017/07/06 19:15:31 if we have coverage above for all the situations,
Emily Fortuna 2017/07/06 19:29:41 there's an else inside that other else, so it real
302 }
303 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/js_model/closure.dart ('k') | pkg/compiler/lib/src/js_model/js_strategy.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698