| 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 CapturedScope 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 CapturedScopeBuilder 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 final Map<ir.Node, ClosureScope> _scopesCapturedInClosureMap; | 20 final Map<ir.Node, CapturedScope> _scopesCapturedInClosureMap; |
| 21 | 21 |
| 22 /// Map entities to their corresponding scope information (such as what | 22 /// Map entities to their corresponding scope information (such as what |
| 23 /// variables are captured/used). | 23 /// variables are captured/used). |
| 24 final Map<Entity, ScopeInfo> _scopeInfoMap; | 24 final Map<Entity, ScopeInfo> _scopeInfoMap; |
| 25 | 25 |
| 26 /// 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 |
| 27 /// 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 |
| 28 /// about variable usage in the surrounding scope. | 28 /// about variable usage in the surrounding scope. |
| 29 final Map<ir.TreeNode /* ir.Field | ir.FunctionNode */, ScopeInfo> | 29 final Map<ir.TreeNode /* ir.Field | ir.FunctionNode */, ScopeInfo> |
| 30 _closuresToGenerate; | 30 _closuresToGenerate; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 | 62 |
| 63 /// The current scope we are in. | 63 /// The current scope we are in. |
| 64 KernelScopeInfo _currentScopeInfo; | 64 KernelScopeInfo _currentScopeInfo; |
| 65 | 65 |
| 66 final KernelToElementMap _kernelToElementMap; | 66 final KernelToElementMap _kernelToElementMap; |
| 67 | 67 |
| 68 /// The original entity from which we start the tree-walk to find closure and | 68 /// The original entity from which we start the tree-walk to find closure and |
| 69 /// scope information. | 69 /// scope information. |
| 70 final Entity _originalEntity; | 70 final Entity _originalEntity; |
| 71 | 71 |
| 72 ClosureScopeBuilder( | 72 CapturedScopeBuilder( |
| 73 this._scopesCapturedInClosureMap, | 73 this._scopesCapturedInClosureMap, |
| 74 this._scopeInfoMap, | 74 this._scopeInfoMap, |
| 75 this._originalEntity, | 75 this._originalEntity, |
| 76 this._closuresToGenerate, | 76 this._closuresToGenerate, |
| 77 this._localsMap, | 77 this._localsMap, |
| 78 this._kernelToElementMap); | 78 this._kernelToElementMap); |
| 79 | 79 |
| 80 /// Update the [ClosureScope] object corresponding to | 80 /// Update the [CapturedScope] object corresponding to |
| 81 /// this node if any variables are captured. | 81 /// this node if any variables are captured. |
| 82 void attachCapturedScopeVariables(ir.Node node) { | 82 void attachCapturedScopeVariables(ir.Node node) { |
| 83 Set<Local> capturedVariablesForScope = new Set<Local>(); | 83 Set<Local> capturedVariablesForScope = new Set<Local>(); |
| 84 | 84 |
| 85 for (ir.VariableDeclaration variable in _scopeVariables) { | 85 for (ir.VariableDeclaration variable in _scopeVariables) { |
| 86 // No need to box non-assignable elements. | 86 // No need to box non-assignable elements. |
| 87 if (variable.isFinal || variable.isConst) continue; | 87 if (variable.isFinal || variable.isConst) continue; |
| 88 if (!_mutatedVariables.contains(variable)) continue; | 88 if (!_mutatedVariables.contains(variable)) continue; |
| 89 if (_capturedVariables.contains(variable)) { | 89 if (_capturedVariables.contains(variable)) { |
| 90 capturedVariablesForScope.add(_localsMap.getLocal(variable)); | 90 capturedVariablesForScope.add(_localsMap.getLocal(variable)); |
| 91 } | 91 } |
| 92 } | 92 } |
| 93 if (!capturedVariablesForScope.isEmpty) { | 93 if (!capturedVariablesForScope.isEmpty) { |
| 94 ThisLocal thisLocal = null; | 94 ThisLocal thisLocal = null; |
| 95 if (node is ir.Member && node.isInstanceMember) { | 95 if (node is ir.Member && node.isInstanceMember) { |
| 96 if (node is ir.Procedure) { | 96 if (node is ir.Procedure) { |
| 97 thisLocal = new ThisLocal(_kernelToElementMap.getMethod(node)); | 97 thisLocal = new ThisLocal(_kernelToElementMap.getMethod(node)); |
| 98 } else if (node is ir.Field) { | 98 } else if (node is ir.Field) { |
| 99 thisLocal = new ThisLocal(_kernelToElementMap.getField(node)); | 99 thisLocal = new ThisLocal(_kernelToElementMap.getField(node)); |
| 100 } | 100 } |
| 101 } else if (node is ir.Constructor) { | 101 } else if (node is ir.Constructor) { |
| 102 thisLocal = new ThisLocal(_kernelToElementMap.getConstructor(node)); | 102 thisLocal = new ThisLocal(_kernelToElementMap.getConstructor(node)); |
| 103 } | 103 } |
| 104 | 104 |
| 105 assert(_scopeInfoMap[_nodeToEntity(node)] != null); | 105 assert(_scopeInfoMap[_nodeToEntity(node)] != null); |
| 106 _scopesCapturedInClosureMap[node] = new KernelClosureScope( | 106 _scopesCapturedInClosureMap[node] = new KernelCapturedScope( |
| 107 capturedVariablesForScope, | 107 capturedVariablesForScope, |
| 108 _nodeToEntity(_executableContext), | 108 _nodeToEntity(_executableContext), |
| 109 thisLocal); | 109 thisLocal); |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 | 112 |
| 113 Entity _nodeToEntity(ir.Node node) { | 113 Entity _nodeToEntity(ir.Node node) { |
| 114 if (node is ir.Member) { | 114 if (node is ir.Member) { |
| 115 return _kernelToElementMap.getMember(node); | 115 return _kernelToElementMap.getMember(node); |
| 116 } else { | 116 } else { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 // 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. |
| 209 for (ir.VariableDeclaration variable in node.variables) { | 209 for (ir.VariableDeclaration variable in node.variables) { |
| 210 // Non-mutated variables should not be boxed. The _mutatedVariables set | 210 // Non-mutated variables should not be boxed. The _mutatedVariables set |
| 211 // gets cleared when `enterNewScope` returns, so check it here. | 211 // gets cleared when `enterNewScope` returns, so check it here. |
| 212 if (_capturedVariables.contains(variable) && | 212 if (_capturedVariables.contains(variable) && |
| 213 _mutatedVariables.contains(variable)) { | 213 _mutatedVariables.contains(variable)) { |
| 214 boxedLoopVariables.add(_localsMap.getLocal(variable)); | 214 boxedLoopVariables.add(_localsMap.getLocal(variable)); |
| 215 } | 215 } |
| 216 } | 216 } |
| 217 }); | 217 }); |
| 218 KernelClosureScope scope = _scopesCapturedInClosureMap[node]; | 218 KernelCapturedScope scope = _scopesCapturedInClosureMap[node]; |
| 219 if (scope == null) return; | 219 if (scope == null) return; |
| 220 _scopesCapturedInClosureMap[node] = new KernelLoopClosureScope( | 220 _scopesCapturedInClosureMap[node] = new KernelCapturedLoopScope( |
| 221 scope.boxedVariables, | 221 scope.boxedVariables, |
| 222 boxedLoopVariables, | 222 boxedLoopVariables, |
| 223 scope.context, | 223 scope.context, |
| 224 scope.thisLocal); | 224 scope.thisLocal); |
| 225 } | 225 } |
| 226 | 226 |
| 227 void visitInvokable(ir.TreeNode node) { | 227 void visitInvokable(ir.TreeNode node) { |
| 228 bool oldIsInsideClosure = _isInsideClosure; | 228 bool oldIsInsideClosure = _isInsideClosure; |
| 229 ir.Node oldExecutableContext = _executableContext; | 229 ir.Node oldExecutableContext = _executableContext; |
| 230 KernelScopeInfo oldScopeInfo = _currentScopeInfo; | 230 KernelScopeInfo oldScopeInfo = _currentScopeInfo; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 nodeToConvert.isInstanceMember)) { | 308 nodeToConvert.isInstanceMember)) { |
| 309 return new ThisLocal(_kernelToElementMap.getConstructor(nodeToConvert)); | 309 return new ThisLocal(_kernelToElementMap.getConstructor(nodeToConvert)); |
| 310 } else if (nodeToConvert is ir.Procedure && | 310 } else if (nodeToConvert is ir.Procedure && |
| 311 nodeToConvert.isInstanceMember) { | 311 nodeToConvert.isInstanceMember) { |
| 312 return new ThisLocal(_kernelToElementMap.getMethod(nodeToConvert)); | 312 return new ThisLocal(_kernelToElementMap.getMethod(nodeToConvert)); |
| 313 } | 313 } |
| 314 } | 314 } |
| 315 return null; | 315 return null; |
| 316 } | 316 } |
| 317 } | 317 } |
| OLD | NEW |