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