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

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 = <ir.VariableDeclaration>[];
108 visitNewScope();
109 attachCapturedScopeVariables(node);
110 _mutatedVariables.removeAll(_scopeVariables);
111 _scopeVariables = oldScopeVariables;
112 }
113
114 @override
115 void defaultNode(ir.Node node) {
116 node.visitChildren(this);
117 }
118
119 @override
120 visitTryCatch(ir.TryCatch node) {
121 bool oldInTry = _inTry;
122 _inTry = true;
123 node.visitChildren(this);
124 _inTry = oldInTry;
125 }
126
127 @override
128 visitTryFinally(ir.TryFinally node) {
129 bool oldInTry = _inTry;
130 _inTry = true;
131 node.visitChildren(this);
132 _inTry = oldInTry;
133 }
134
135 @override
136 visitVariableGet(ir.VariableGet node) {
137 _markVariableAsUsed(node.variable);
138 }
139
140 @override
141 visitVariableSet(ir.VariableSet node) {
142 _mutatedVariables.add(node.variable);
143 _markVariableAsUsed(node.variable);
144 node.visitChildren(this);
145 }
146
147 /// Add this variable to the set of free variables if appropriate and add to
148 /// the tally of variables used in try or sync blocks.
149 void _markVariableAsUsed(ir.VariableDeclaration variable) {
150 if (_isInsideClosure && !_inCurrentContext(variable)) {
151 // If the element is not declared in the current function and the element
152 // is not the closure itself we need to mark the element as free variable.
153 // Note that the check on [insideClosure] is not just an
154 // optimization: factories have type parameters as function
155 // parameters, and type parameters are declared in the class, not
156 // the factory.
157 _currentScopeInfo.freeVariables.add(variable);
158 }
159 if (_inTry) {
160 _currentScopeInfo.localsUsedInTryOrSync
161 .add(_localsMap.getLocal(variable));
162 }
163 }
164
165 @override
166 void visitForStatement(ir.ForStatement node) {
167 List<Local> boxedLoopVariables = <Local>[];
168 enterNewScope(node, () {
169 // First visit initialized variables and update steps so we can easily
170 // check if a loop variable was captured in one of these subexpressions.
171 node.variables
172 .forEach((ir.VariableDeclaration variable) => variable.accept(this));
173 node.updates
174 .forEach((ir.Expression expression) => expression.accept(this));
175
176 // Loop variables that have not been captured yet can safely be flagged as
177 // non-mutated, because no nested function can observe the mutation.
178 for (ir.VariableDeclaration variable in node.variables) {
179 if (!_capturedVariables.contains(variable)) {
180 _mutatedVariables.remove(variable);
181 }
182 }
183
184 // Visit condition and body.
185 // This must happen after the above, so any loop variables mutated in the
186 // condition or body are indeed flagged as mutated.
187 if (node.condition != null) node.condition.accept(this);
188 node.body.accept(this);
189
190 // See if we have declared loop variables that need to be boxed.
191 for (ir.VariableDeclaration variable in node.variables) {
192 // Non-mutated variables should not be boxed. The _mutatedVariables set
193 // gets cleared when `enterNewScope` returns, so check it here.
194 if (_capturedVariables.contains(variable) &&
195 _mutatedVariables.contains(variable)) {
196 boxedLoopVariables.add(_localsMap.getLocal(variable));
197 }
198 }
199 });
200 KernelClosureScope scope = _closureInfoMap[node];
201 if (scope == null) return;
202 _closureInfoMap[node] = new KernelLoopClosureScope(scope.boxedVariables,
203 boxedLoopVariables, scope.context, scope.thisLocal);
204 }
205
206 void visitInvokable(ir.TreeNode node) {
207 bool oldIsInsideClosure = _isInsideClosure;
208 ir.Node oldExecutableContext = _executableContext;
209 KernelScopeInfo oldScopeInfo = _currentScopeInfo;
210
211 // _outermostNode is only null the first time we enter the body of the
212 // field, constructor, or method that is being analyzed.
213 _isInsideClosure = _outermostNode != null;
214 _executableContext = node;
215 if (!_isInsideClosure) {
216 _outermostNode = node;
217 }
218 _currentScopeInfo = new KernelScopeInfo(_nodeToThisLocal(node));
219 _closuresToGenerate[node] = _currentScopeInfo;
220
221 enterNewScope(node, () {
222 node.visitChildren(this);
223 });
224
225 KernelScopeInfo savedScopeInfo = _currentScopeInfo;
226 bool savedIsInsideClosure = _isInsideClosure;
227
228 // Restore old values.
229 _isInsideClosure = oldIsInsideClosure;
230 _currentScopeInfo = oldScopeInfo;
231 _executableContext = oldExecutableContext;
232
233 // Mark all free variables as captured and expect to encounter them in the
234 // outer function.
235 Iterable<ir.VariableDeclaration> freeVariables =
236 savedScopeInfo.freeVariables;
237 assert(freeVariables.isEmpty || savedIsInsideClosure);
238 for (ir.VariableDeclaration freeVariable in freeVariables) {
239 assert(!_capturedVariables.contains(freeVariable));
240 _capturedVariables.add(freeVariable);
241 _markVariableAsUsed(freeVariable);
242 }
243 }
244
245 /// Return true if [variable]'s context is the same as the current executable
246 /// context.
247 bool _inCurrentContext(ir.VariableDeclaration variable) {
248 ir.TreeNode node = variable;
249 while (node != _outermostNode && node != _executableContext) {
250 node = node.parent;
251 }
252 return node == _executableContext;
253 }
254
255 void translateLazyInitializer(ir.Field field) {
256 visitInvokable(field);
257 }
258
259 void translateConstructorOrProcedure(ir.Node constructorOrProcedure) {
260 constructorOrProcedure.accept(this);
261 }
262
263 void visitFunctionNode(ir.FunctionNode functionNode) {
264 visitInvokable(functionNode);
265 }
266
267 /// If [node] is an instance member return the corresponding `this` reference.
268 /// If not, return null.
269 Entity _nodeToThisLocal(
270 ir.TreeNode
271 /*ir.Field|ir.FunctionNode|ir.Constructor|ir.Procedure*/ node) {
272 ir.Node nodeToConvert = node;
273 if (nodeToConvert is ir.Field) {
274 if (!nodeToConvert.isInstanceMember) return null;
275 return new ThisLocal(_kernelToElementMap.getField(nodeToConvert));
276 } else {
277 if (nodeToConvert is ir.FunctionNode) {
278 // Step up one node higher to find the corresponding entity for this
279 // node.
280 nodeToConvert = node.parent;
281 }
282 if (nodeToConvert is ir.Constructor ||
283 (nodeToConvert is ir.Procedure &&
284 nodeToConvert.kind == ir.ProcedureKind.Factory &&
285 nodeToConvert.isInstanceMember)) {
286 return new ThisLocal(_kernelToElementMap.getConstructor(nodeToConvert));
287 } else if (nodeToConvert is ir.Procedure &&
288 nodeToConvert.isInstanceMember) {
289 return new ThisLocal(_kernelToElementMap.getMethod(nodeToConvert));
290 }
291 }
292 return null;
293 }
294 }
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