| 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 final Map<ir.Node, ClosureScope> _scopesCapturedInClosureMap; |
| 21 |
| 22 /// Map entities to their corresponding scope information (such as what |
| 23 /// variables are captured/used). |
| 24 final Map<Entity, ScopeInfo> _scopeInfoMap; |
| 21 | 25 |
| 22 /// A map of the nodes that we have flagged as necessary to generate closure | 26 /// 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 | 27 /// classes for in a later stage. We map that node to information ascertained |
| 24 /// about variable usage in the surrounding scope. | 28 /// about variable usage in the surrounding scope. |
| 25 Map<ir.TreeNode /* ir.Field | ir.FunctionNode */, ScopeInfo> | 29 final Map<ir.TreeNode /* ir.Field | ir.FunctionNode */, ScopeInfo> |
| 26 _closuresToGenerate = <ir.TreeNode, ScopeInfo>{}; | 30 _closuresToGenerate; |
| 27 | 31 |
| 28 /// The local variables that have been declared in the current scope. | 32 /// The local variables that have been declared in the current scope. |
| 29 List<ir.VariableDeclaration> _scopeVariables; | 33 List<ir.VariableDeclaration> _scopeVariables; |
| 30 | 34 |
| 31 /// Pointer to the context in which this closure is executed. | 35 /// Pointer to the context in which this closure is executed. |
| 32 /// For example, in the expression `var foo = () => 3 + i;`, the executable | 36 /// 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`. | 37 /// context as we walk the nodes in that expression is the ir.Field `foo`. |
| 34 ir.Node _executableContext; | 38 ir.Node _executableContext; |
| 35 | 39 |
| 36 /// A flag to indicate if we are currently inside a closure. | 40 /// A flag to indicate if we are currently inside a closure. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 54 bool _inTry = false; | 58 bool _inTry = false; |
| 55 | 59 |
| 56 /// Lookup the local entity that corresponds to a kernel variable declaration. | 60 /// Lookup the local entity that corresponds to a kernel variable declaration. |
| 57 final KernelToLocalsMap _localsMap; | 61 final KernelToLocalsMap _localsMap; |
| 58 | 62 |
| 59 /// The current scope we are in. | 63 /// The current scope we are in. |
| 60 KernelScopeInfo _currentScopeInfo; | 64 KernelScopeInfo _currentScopeInfo; |
| 61 | 65 |
| 62 final KernelToElementMap _kernelToElementMap; | 66 final KernelToElementMap _kernelToElementMap; |
| 63 | 67 |
| 64 ClosureScopeBuilder(this._closureInfoMap, this._closuresToGenerate, | 68 /// The original entity from which we start the tree-walk to find closure and |
| 65 this._localsMap, this._kernelToElementMap); | 69 /// scope information. |
| 70 final Entity _originalEntity; |
| 71 |
| 72 ClosureScopeBuilder( |
| 73 this._scopesCapturedInClosureMap, |
| 74 this._scopeInfoMap, |
| 75 this._originalEntity, |
| 76 this._closuresToGenerate, |
| 77 this._localsMap, |
| 78 this._kernelToElementMap); |
| 66 | 79 |
| 67 /// Update the [ClosureScope] object corresponding to | 80 /// Update the [ClosureScope] object corresponding to |
| 68 /// this node if any variables are captured. | 81 /// this node if any variables are captured. |
| 69 void attachCapturedScopeVariables(ir.Node node) { | 82 void attachCapturedScopeVariables(ir.Node node) { |
| 70 Set<Local> capturedVariablesForScope = new Set<Local>(); | 83 Set<Local> capturedVariablesForScope = new Set<Local>(); |
| 71 | 84 |
| 72 for (ir.VariableDeclaration variable in _scopeVariables) { | 85 for (ir.VariableDeclaration variable in _scopeVariables) { |
| 73 // No need to box non-assignable elements. | 86 // No need to box non-assignable elements. |
| 74 if (variable.isFinal || variable.isConst) continue; | 87 if (variable.isFinal || variable.isConst) continue; |
| 75 if (!_mutatedVariables.contains(variable)) continue; | 88 if (!_mutatedVariables.contains(variable)) continue; |
| 76 if (_capturedVariables.contains(variable)) { | 89 if (_capturedVariables.contains(variable)) { |
| 77 capturedVariablesForScope.add(_localsMap.getLocal(variable)); | 90 capturedVariablesForScope.add(_localsMap.getLocal(variable)); |
| 78 } | 91 } |
| 79 } | 92 } |
| 80 if (!capturedVariablesForScope.isEmpty) { | 93 if (!capturedVariablesForScope.isEmpty) { |
| 81 ThisLocal thisLocal = null; | 94 ThisLocal thisLocal = null; |
| 82 if (node is ir.Member && node.isInstanceMember) { | 95 if (node is ir.Member && node.isInstanceMember) { |
| 83 if (node is ir.Procedure) { | 96 if (node is ir.Procedure) { |
| 84 thisLocal = new ThisLocal(_kernelToElementMap.getMethod(node)); | 97 thisLocal = new ThisLocal(_kernelToElementMap.getMethod(node)); |
| 85 } else if (node is ir.Field) { | 98 } else if (node is ir.Field) { |
| 86 thisLocal = new ThisLocal(_kernelToElementMap.getField(node)); | 99 thisLocal = new ThisLocal(_kernelToElementMap.getField(node)); |
| 87 } | 100 } |
| 88 } else if (node is ir.Constructor) { | 101 } else if (node is ir.Constructor) { |
| 89 thisLocal = new ThisLocal(_kernelToElementMap.getConstructor(node)); | 102 thisLocal = new ThisLocal(_kernelToElementMap.getConstructor(node)); |
| 90 } | 103 } |
| 91 | 104 |
| 92 Entity context; | 105 assert(_scopeInfoMap[_nodeToEntity(node)] != null); |
| 93 if (_executableContext is ir.Member) { | 106 _scopesCapturedInClosureMap[node] = new KernelClosureScope( |
| 94 context = _kernelToElementMap.getMember(_executableContext); | 107 capturedVariablesForScope, |
| 95 } else { | 108 _nodeToEntity(_executableContext), |
| 96 context = _kernelToElementMap.getLocalFunction(_executableContext); | 109 thisLocal); |
| 97 } | |
| 98 _closureInfoMap[node] = | |
| 99 new KernelClosureScope(capturedVariablesForScope, context, thisLocal); | |
| 100 } | 110 } |
| 101 } | 111 } |
| 102 | 112 |
| 113 Entity _nodeToEntity(ir.Node node) { |
| 114 if (node is ir.Member) { |
| 115 return _kernelToElementMap.getMember(node); |
| 116 } else { |
| 117 return _kernelToElementMap.getLocalFunction(node); |
| 118 } |
| 119 } |
| 120 |
| 103 /// Perform book-keeping with the current set of local variables that have | 121 /// Perform book-keeping with the current set of local variables that have |
| 104 /// been seen thus far before entering this new scope. | 122 /// been seen thus far before entering this new scope. |
| 105 void enterNewScope(ir.Node node, Function visitNewScope) { | 123 void enterNewScope(ir.Node node, Function visitNewScope) { |
| 106 List<ir.VariableDeclaration> oldScopeVariables = _scopeVariables; | 124 List<ir.VariableDeclaration> oldScopeVariables = _scopeVariables; |
| 107 _scopeVariables = <ir.VariableDeclaration>[]; | 125 _scopeVariables = <ir.VariableDeclaration>[]; |
| 108 visitNewScope(); | 126 visitNewScope(); |
| 109 attachCapturedScopeVariables(node); | 127 attachCapturedScopeVariables(node); |
| 110 _mutatedVariables.removeAll(_scopeVariables); | 128 _mutatedVariables.removeAll(_scopeVariables); |
| 111 _scopeVariables = oldScopeVariables; | 129 _scopeVariables = oldScopeVariables; |
| 112 } | 130 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 // See if we have declared loop variables that need to be boxed. | 208 // See if we have declared loop variables that need to be boxed. |
| 191 for (ir.VariableDeclaration variable in node.variables) { | 209 for (ir.VariableDeclaration variable in node.variables) { |
| 192 // Non-mutated variables should not be boxed. The _mutatedVariables set | 210 // Non-mutated variables should not be boxed. The _mutatedVariables set |
| 193 // gets cleared when `enterNewScope` returns, so check it here. | 211 // gets cleared when `enterNewScope` returns, so check it here. |
| 194 if (_capturedVariables.contains(variable) && | 212 if (_capturedVariables.contains(variable) && |
| 195 _mutatedVariables.contains(variable)) { | 213 _mutatedVariables.contains(variable)) { |
| 196 boxedLoopVariables.add(_localsMap.getLocal(variable)); | 214 boxedLoopVariables.add(_localsMap.getLocal(variable)); |
| 197 } | 215 } |
| 198 } | 216 } |
| 199 }); | 217 }); |
| 200 KernelClosureScope scope = _closureInfoMap[node]; | 218 KernelClosureScope scope = _scopesCapturedInClosureMap[node]; |
| 201 if (scope == null) return; | 219 if (scope == null) return; |
| 202 _closureInfoMap[node] = new KernelLoopClosureScope(scope.boxedVariables, | 220 _scopesCapturedInClosureMap[node] = new KernelLoopClosureScope( |
| 203 boxedLoopVariables, scope.context, scope.thisLocal); | 221 scope.boxedVariables, |
| 222 boxedLoopVariables, |
| 223 scope.context, |
| 224 scope.thisLocal); |
| 204 } | 225 } |
| 205 | 226 |
| 206 void visitInvokable(ir.TreeNode node) { | 227 void visitInvokable(ir.TreeNode node) { |
| 207 bool oldIsInsideClosure = _isInsideClosure; | 228 bool oldIsInsideClosure = _isInsideClosure; |
| 208 ir.Node oldExecutableContext = _executableContext; | 229 ir.Node oldExecutableContext = _executableContext; |
| 209 KernelScopeInfo oldScopeInfo = _currentScopeInfo; | 230 KernelScopeInfo oldScopeInfo = _currentScopeInfo; |
| 210 | 231 |
| 211 // _outermostNode is only null the first time we enter the body of the | 232 // _outermostNode is only null the first time we enter the body of the |
| 212 // field, constructor, or method that is being analyzed. | 233 // field, constructor, or method that is being analyzed. |
| 213 _isInsideClosure = _outermostNode != null; | 234 _isInsideClosure = _outermostNode != null; |
| 214 _executableContext = node; | 235 _executableContext = node; |
| 215 if (!_isInsideClosure) { | 236 _currentScopeInfo = new KernelScopeInfo(_nodeToThisLocal(node)); |
| 237 if (_isInsideClosure) { |
| 238 _closuresToGenerate[node] = _currentScopeInfo; |
| 239 } else { |
| 216 _outermostNode = node; | 240 _outermostNode = node; |
| 241 _scopeInfoMap[_originalEntity] = _currentScopeInfo; |
| 217 } | 242 } |
| 218 _currentScopeInfo = new KernelScopeInfo(_nodeToThisLocal(node)); | |
| 219 _closuresToGenerate[node] = _currentScopeInfo; | |
| 220 | 243 |
| 221 enterNewScope(node, () { | 244 enterNewScope(node, () { |
| 222 node.visitChildren(this); | 245 node.visitChildren(this); |
| 223 }); | 246 }); |
| 224 | 247 |
| 225 KernelScopeInfo savedScopeInfo = _currentScopeInfo; | 248 KernelScopeInfo savedScopeInfo = _currentScopeInfo; |
| 226 bool savedIsInsideClosure = _isInsideClosure; | 249 bool savedIsInsideClosure = _isInsideClosure; |
| 227 | 250 |
| 228 // Restore old values. | 251 // Restore old values. |
| 229 _isInsideClosure = oldIsInsideClosure; | 252 _isInsideClosure = oldIsInsideClosure; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 nodeToConvert.isInstanceMember)) { | 308 nodeToConvert.isInstanceMember)) { |
| 286 return new ThisLocal(_kernelToElementMap.getConstructor(nodeToConvert)); | 309 return new ThisLocal(_kernelToElementMap.getConstructor(nodeToConvert)); |
| 287 } else if (nodeToConvert is ir.Procedure && | 310 } else if (nodeToConvert is ir.Procedure && |
| 288 nodeToConvert.isInstanceMember) { | 311 nodeToConvert.isInstanceMember) { |
| 289 return new ThisLocal(_kernelToElementMap.getMethod(nodeToConvert)); | 312 return new ThisLocal(_kernelToElementMap.getMethod(nodeToConvert)); |
| 290 } | 313 } |
| 291 } | 314 } |
| 292 return null; | 315 return null; |
| 293 } | 316 } |
| 294 } | 317 } |
| OLD | NEW |