OLD | NEW |
---|---|
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'package:kernel/ast.dart' as ir; | 5 import 'package:kernel/ast.dart' as ir; |
6 | 6 |
7 import '../closure.dart'; | 7 import '../closure.dart'; |
8 import '../elements/entities.dart'; | 8 import '../elements/entities.dart'; |
9 import '../kernel/element_map.dart'; | 9 import '../kernel/element_map.dart'; |
10 import 'closure.dart'; | 10 import 'closure.dart'; |
11 | 11 |
12 /// This builder walks the code to determine what variables are captured/free at | 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 | 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. | 14 /// about how a particular variable is being used at any point in the code. |
15 class ClosureScopeBuilder extends ir.Visitor { | 15 class ClosureScopeBuilder extends ir.Visitor { |
16 /// A map of each visited call node with the associated information about what | 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 | 17 /// variables are captured/used. Each ir.Node key corresponds to a scope that |
18 /// was encountered while visiting a closure (initially called through | 18 /// was encountered while visiting a closure (initially called through |
19 /// [translateLazyIntializer] or [translateConstructorOrProcedure]). | 19 /// [translateLazyIntializer] or [translateConstructorOrProcedure]). |
20 Map<ir.Node, ClosureScope> _closureInfoMap = <ir.Node, ClosureScope>{}; | 20 Map<ir.Node, ClosureScope> _closureInfoMap = <ir.Node, ClosureScope>{}; |
21 | 21 |
22 /// Map entities to their corresponding scope information (such as what | |
23 /// variables are captured/used). The distinction between this map and | |
24 /// [_closureInfoMap] is that [_closureInfoMap] stores this data for closures | |
25 /// specifically, whereas [_scopeInfoMap] stores this information for entities | |
26 /// that are *not* closures (this information is used by the locals handler). | |
27 /// The union of these two maps represents all the scopes encountered. | |
28 Map<Entity, ScopeInfo> _scopeInfoMap = <Entity, ScopeInfo>{}; | |
sra1
2017/07/12 23:57:41
final, don't initialize.
Emily Fortuna
2017/07/13 21:03:39
Done.
| |
29 | |
22 /// A map of the nodes that we have flagged as necessary to generate closure | 30 /// 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 | 31 /// classes for in a later stage. We map that node to information ascertained |
24 /// about variable usage in the surrounding scope. | 32 /// about variable usage in the surrounding scope. |
25 Map<ir.TreeNode /* ir.Field | ir.FunctionNode */, ScopeInfo> | 33 Map<ir.TreeNode /* ir.Field | ir.FunctionNode */, ScopeInfo> |
26 _closuresToGenerate = <ir.TreeNode, ScopeInfo>{}; | 34 _closuresToGenerate = <ir.TreeNode, ScopeInfo>{}; |
27 | 35 |
28 /// The local variables that have been declared in the current scope. | 36 /// The local variables that have been declared in the current scope. |
29 List<ir.VariableDeclaration> _scopeVariables; | 37 List<ir.VariableDeclaration> _scopeVariables; |
30 | 38 |
31 /// Pointer to the context in which this closure is executed. | 39 /// Pointer to the context in which this closure is executed. |
(...skipping 22 matching lines...) Expand all Loading... | |
54 bool _inTry = false; | 62 bool _inTry = false; |
55 | 63 |
56 /// Lookup the local entity that corresponds to a kernel variable declaration. | 64 /// Lookup the local entity that corresponds to a kernel variable declaration. |
57 final KernelToLocalsMap _localsMap; | 65 final KernelToLocalsMap _localsMap; |
58 | 66 |
59 /// The current scope we are in. | 67 /// The current scope we are in. |
60 KernelScopeInfo _currentScopeInfo; | 68 KernelScopeInfo _currentScopeInfo; |
61 | 69 |
62 final KernelToElementMap _kernelToElementMap; | 70 final KernelToElementMap _kernelToElementMap; |
63 | 71 |
64 ClosureScopeBuilder(this._closureInfoMap, this._closuresToGenerate, | 72 ClosureScopeBuilder(this._closureInfoMap, this._scopeInfoMap, |
65 this._localsMap, this._kernelToElementMap); | 73 this._closuresToGenerate, this._localsMap, this._kernelToElementMap); |
66 | 74 |
67 /// Update the [ClosureScope] object corresponding to | 75 /// Update the [ClosureScope] object corresponding to |
68 /// this node if any variables are captured. | 76 /// this node if any variables are captured. |
69 void attachCapturedScopeVariables(ir.Node node) { | 77 void attachCapturedScopeVariables(ir.Node node) { |
70 Set<Local> capturedVariablesForScope = new Set<Local>(); | 78 Set<Local> capturedVariablesForScope = new Set<Local>(); |
71 | 79 |
72 for (ir.VariableDeclaration variable in _scopeVariables) { | 80 for (ir.VariableDeclaration variable in _scopeVariables) { |
73 // No need to box non-assignable elements. | 81 // No need to box non-assignable elements. |
74 if (variable.isFinal || variable.isConst) continue; | 82 if (variable.isFinal || variable.isConst) continue; |
75 if (!_mutatedVariables.contains(variable)) continue; | 83 if (!_mutatedVariables.contains(variable)) continue; |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 | 213 |
206 void visitInvokable(ir.TreeNode node) { | 214 void visitInvokable(ir.TreeNode node) { |
207 bool oldIsInsideClosure = _isInsideClosure; | 215 bool oldIsInsideClosure = _isInsideClosure; |
208 ir.Node oldExecutableContext = _executableContext; | 216 ir.Node oldExecutableContext = _executableContext; |
209 KernelScopeInfo oldScopeInfo = _currentScopeInfo; | 217 KernelScopeInfo oldScopeInfo = _currentScopeInfo; |
210 | 218 |
211 // _outermostNode is only null the first time we enter the body of the | 219 // _outermostNode is only null the first time we enter the body of the |
212 // field, constructor, or method that is being analyzed. | 220 // field, constructor, or method that is being analyzed. |
213 _isInsideClosure = _outermostNode != null; | 221 _isInsideClosure = _outermostNode != null; |
214 _executableContext = node; | 222 _executableContext = node; |
215 if (!_isInsideClosure) { | 223 _currentScopeInfo = new KernelScopeInfo(_nodeToThisLocal(node)); |
224 if (_isInsideClosure) { | |
225 _closuresToGenerate[node] = _currentScopeInfo; | |
226 } else { | |
216 _outermostNode = node; | 227 _outermostNode = node; |
228 Entity entity; | |
229 ir.TreeNode tempNode = node; | |
230 if (tempNode is ir.Member) { | |
231 entity = _kernelToElementMap.getMember(tempNode); | |
Johnni Winther
2017/07/13 08:56:14
Maybe the visitor should have the member entity as
Emily Fortuna
2017/07/13 21:03:39
done.
| |
232 } else { | |
233 entity = _kernelToElementMap.getLocalFunction(tempNode); | |
Johnni Winther
2017/07/13 08:56:14
Can this ever happen? We always start visiting on
Emily Fortuna
2017/07/13 21:03:39
You're right it won't. Took it out.
| |
234 } | |
235 _scopeInfoMap[entity] = _currentScopeInfo; | |
217 } | 236 } |
218 _currentScopeInfo = new KernelScopeInfo(_nodeToThisLocal(node)); | |
219 _closuresToGenerate[node] = _currentScopeInfo; | |
220 | 237 |
221 enterNewScope(node, () { | 238 enterNewScope(node, () { |
222 node.visitChildren(this); | 239 node.visitChildren(this); |
223 }); | 240 }); |
224 | 241 |
225 KernelScopeInfo savedScopeInfo = _currentScopeInfo; | 242 KernelScopeInfo savedScopeInfo = _currentScopeInfo; |
226 bool savedIsInsideClosure = _isInsideClosure; | 243 bool savedIsInsideClosure = _isInsideClosure; |
227 | 244 |
228 // Restore old values. | 245 // Restore old values. |
229 _isInsideClosure = oldIsInsideClosure; | 246 _isInsideClosure = oldIsInsideClosure; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
285 nodeToConvert.isInstanceMember)) { | 302 nodeToConvert.isInstanceMember)) { |
286 return new ThisLocal(_kernelToElementMap.getConstructor(nodeToConvert)); | 303 return new ThisLocal(_kernelToElementMap.getConstructor(nodeToConvert)); |
287 } else if (nodeToConvert is ir.Procedure && | 304 } else if (nodeToConvert is ir.Procedure && |
288 nodeToConvert.isInstanceMember) { | 305 nodeToConvert.isInstanceMember) { |
289 return new ThisLocal(_kernelToElementMap.getMethod(nodeToConvert)); | 306 return new ThisLocal(_kernelToElementMap.getMethod(nodeToConvert)); |
290 } | 307 } |
291 } | 308 } |
292 return null; | 309 return null; |
293 } | 310 } |
294 } | 311 } |
OLD | NEW |