OLD | NEW |
---|---|
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 library kernel.transformations.closure.info; | 5 library kernel.transformations.closure.info; |
6 | 6 |
7 import '../../ast.dart' | 7 import '../../ast.dart' |
8 show | 8 show |
9 Class, | 9 Class, |
10 Constructor, | 10 Constructor, |
(...skipping 12 matching lines...) Expand all Loading... | |
23 | 23 |
24 import '../../visitor.dart' show RecursiveVisitor; | 24 import '../../visitor.dart' show RecursiveVisitor; |
25 | 25 |
26 class ClosureInfo extends RecursiveVisitor { | 26 class ClosureInfo extends RecursiveVisitor { |
27 FunctionNode currentFunction; | 27 FunctionNode currentFunction; |
28 final Map<VariableDeclaration, FunctionNode> function = | 28 final Map<VariableDeclaration, FunctionNode> function = |
29 <VariableDeclaration, FunctionNode>{}; | 29 <VariableDeclaration, FunctionNode>{}; |
30 | 30 |
31 final Set<VariableDeclaration> variables = new Set<VariableDeclaration>(); | 31 final Set<VariableDeclaration> variables = new Set<VariableDeclaration>(); |
32 | 32 |
33 /// Map from functions to set of type variables captured within them. | |
34 /// | |
karlklose
2017/07/25 09:38:23
Remove empty comment line.
sjindel
2017/07/25 13:11:48
Done.
| |
33 final Map<FunctionNode, Set<TypeParameter>> typeVariables = | 35 final Map<FunctionNode, Set<TypeParameter>> typeVariables = |
34 <FunctionNode, Set<TypeParameter>>{}; | 36 <FunctionNode, Set<TypeParameter>>{}; |
35 | 37 |
36 /// Map from members to synthetic variables for accessing `this` in a local | 38 /// Map from members to synthetic variables for accessing `this` in a local |
37 /// function. | 39 /// function. |
38 final Map<FunctionNode, VariableDeclaration> thisAccess = | 40 final Map<FunctionNode, VariableDeclaration> thisAccess = |
39 <FunctionNode, VariableDeclaration>{}; | 41 <FunctionNode, VariableDeclaration>{}; |
40 | 42 |
41 final Set<String> currentMemberLocalNames = new Set<String>(); | 43 final Set<String> currentMemberLocalNames = new Set<String>(); |
42 | 44 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 saveCurrentFunction(() { | 161 saveCurrentFunction(() { |
160 currentFunction = node; | 162 currentFunction = node; |
161 node.visitChildren(this); | 163 node.visitChildren(this); |
162 }); | 164 }); |
163 | 165 |
164 Set<TypeParameter> capturedTypeVariables = typeVariables[node]; | 166 Set<TypeParameter> capturedTypeVariables = typeVariables[node]; |
165 if (capturedTypeVariables != null && !isOuterMostContext) { | 167 if (capturedTypeVariables != null && !isOuterMostContext) { |
166 // Propagate captured type variables to enclosing function. | 168 // Propagate captured type variables to enclosing function. |
167 typeVariables | 169 typeVariables |
168 .putIfAbsent(currentFunction, () => new Set<TypeParameter>()) | 170 .putIfAbsent(currentFunction, () => new Set<TypeParameter>()) |
169 .addAll(capturedTypeVariables); | 171 .addAll( |
172 capturedTypeVariables.where((t) => t.parent != currentFunction)); | |
karlklose
2017/07/25 09:38:23
Consider adding a comment here to explain when 't.
sjindel
2017/07/25 13:11:48
Done.
Dmitry Stefantsov
2017/07/26 09:12:09
Karl may correct me, but I think using square brac
karlklose
2017/07/26 10:36:30
Yes, you should use `` for code fragments. Note th
| |
170 } | 173 } |
171 } | 174 } |
172 | 175 |
173 visitVariableDeclaration(VariableDeclaration node) { | 176 visitVariableDeclaration(VariableDeclaration node) { |
174 function[node] = currentFunction; | 177 function[node] = currentFunction; |
175 node.visitChildren(this); | 178 node.visitChildren(this); |
176 } | 179 } |
177 | 180 |
178 visitVariableGet(VariableGet node) { | 181 visitVariableGet(VariableGet node) { |
179 if (function[node.variable] != currentFunction) { | 182 if (function[node.variable] != currentFunction) { |
180 variables.add(node.variable); | 183 variables.add(node.variable); |
181 } | 184 } |
182 node.visitChildren(this); | 185 node.visitChildren(this); |
183 } | 186 } |
184 | 187 |
185 visitVariableSet(VariableSet node) { | 188 visitVariableSet(VariableSet node) { |
186 if (function[node.variable] != currentFunction) { | 189 if (function[node.variable] != currentFunction) { |
187 variables.add(node.variable); | 190 variables.add(node.variable); |
188 } | 191 } |
189 node.visitChildren(this); | 192 node.visitChildren(this); |
190 } | 193 } |
191 | 194 |
192 visitTypeParameterType(TypeParameterType node) { | 195 visitTypeParameterType(TypeParameterType node) { |
193 if (!isOuterMostContext) { | 196 if (!isOuterMostContext && node.parameter.parent != currentFunction) { |
194 typeVariables | 197 typeVariables |
195 .putIfAbsent(currentFunction, () => new Set<TypeParameter>()) | 198 .putIfAbsent(currentFunction, () => new Set<TypeParameter>()) |
196 .add(node.parameter); | 199 .add(node.parameter); |
197 } | 200 } |
198 } | 201 } |
199 | 202 |
200 visitThisExpression(ThisExpression node) { | 203 visitThisExpression(ThisExpression node) { |
201 if (!isOuterMostContext) { | 204 if (!isOuterMostContext) { |
202 thisAccess.putIfAbsent( | 205 thisAccess.putIfAbsent( |
203 currentMemberFunction, () => new VariableDeclaration("#self")); | 206 currentMemberFunction, () => new VariableDeclaration("#self")); |
204 } | 207 } |
205 } | 208 } |
206 | 209 |
207 saveCurrentFunction(void f()) { | 210 saveCurrentFunction(void f()) { |
208 var saved = currentFunction; | 211 var saved = currentFunction; |
209 try { | 212 try { |
210 f(); | 213 f(); |
211 } finally { | 214 } finally { |
212 currentFunction = saved; | 215 currentFunction = saved; |
213 } | 216 } |
214 } | 217 } |
215 } | 218 } |
OLD | NEW |