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.converter; | 5 library kernel.transformations.closure.converter; |
6 | 6 |
7 import '../../ast.dart' | 7 import '../../ast.dart' |
8 show | 8 show |
9 Arguments, | 9 Arguments, |
10 Block, | 10 Block, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 StringLiteral, | 45 StringLiteral, |
46 Supertype, | 46 Supertype, |
47 ThisExpression, | 47 ThisExpression, |
48 Transformer, | 48 Transformer, |
49 TreeNode, | 49 TreeNode, |
50 TypeParameter, | 50 TypeParameter, |
51 TypeParameterType, | 51 TypeParameterType, |
52 VariableDeclaration, | 52 VariableDeclaration, |
53 VariableGet, | 53 VariableGet, |
54 VariableSet, | 54 VariableSet, |
| 55 VectorType, |
55 transformList; | 56 transformList; |
56 | 57 |
57 import '../../frontend/accessors.dart' show VariableAccessor; | 58 import '../../frontend/accessors.dart' show VariableAccessor; |
58 | 59 |
59 import '../../clone.dart' show CloneVisitor; | 60 import '../../clone.dart' show CloneVisitor; |
60 | 61 |
61 import '../../core_types.dart' show CoreTypes; | 62 import '../../core_types.dart' show CoreTypes; |
62 | 63 |
63 import '../../type_algebra.dart' show substitute; | 64 import '../../type_algebra.dart' show substitute; |
64 | 65 |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 FunctionNode enclosingFunction = currentFunction; | 257 FunctionNode enclosingFunction = currentFunction; |
257 Map<TypeParameter, DartType> enclosingTypeSubstitution = typeSubstitution; | 258 Map<TypeParameter, DartType> enclosingTypeSubstitution = typeSubstitution; |
258 currentFunction = function; | 259 currentFunction = function; |
259 Statement body = function.body; | 260 Statement body = function.body; |
260 assert(body != null); | 261 assert(body != null); |
261 | 262 |
262 rewriter = makeRewriterForBody(function); | 263 rewriter = makeRewriterForBody(function); |
263 | 264 |
264 VariableDeclaration contextVariable = new VariableDeclaration( | 265 VariableDeclaration contextVariable = new VariableDeclaration( |
265 "#contextParameter", | 266 "#contextParameter", |
266 type: contextClass.rawType, | 267 type: const VectorType(), |
267 isFinal: true); | 268 isFinal: true); |
268 Context parent = context; | 269 Context parent = context; |
269 context = context.toNestedContext(new VariableAccessor(contextVariable)); | 270 context = context.toNestedContext(new VariableAccessor(contextVariable)); |
270 | 271 |
271 Set<TypeParameter> captured = capturedTypeVariables[currentFunction]; | 272 Set<TypeParameter> captured = capturedTypeVariables[currentFunction]; |
272 if (captured != null) { | 273 if (captured != null) { |
273 typeSubstitution = copyTypeVariables(captured); | 274 typeSubstitution = copyTypeVariables(captured); |
274 } else { | 275 } else { |
275 typeSubstitution = const <TypeParameter, DartType>{}; | 276 typeSubstitution = const <TypeParameter, DartType>{}; |
276 } | 277 } |
(...skipping 30 matching lines...) Expand all Loading... |
307 | 308 |
308 TreeNode visitFunctionExpression(FunctionExpression node) { | 309 TreeNode visitFunctionExpression(FunctionExpression node) { |
309 return saveContext(() { | 310 return saveContext(() { |
310 return handleLocalFunction(node.function); | 311 return handleLocalFunction(node.function); |
311 }); | 312 }); |
312 } | 313 } |
313 | 314 |
314 /// Add a new class to the current library that looks like this: | 315 /// Add a new class to the current library that looks like this: |
315 /// | 316 /// |
316 /// class Closure#0 extends core::Object implements core::Function { | 317 /// class Closure#0 extends core::Object implements core::Function { |
317 /// field _in::Context context; | 318 /// field Vector context; |
318 /// constructor •(final _in::Context #t1) → dynamic | 319 /// constructor •(final Vector #t1) → dynamic |
319 /// : self::Closure 0::context = #t1 | 320 /// : self::Closure#0::context = #t1 |
320 /// ; | 321 /// ; |
321 /// method call(/* The parameters of [function] */) → dynamic { | 322 /// method call(/* The parameters of [function] */) → dynamic { |
322 /// /// #t2 is [contextVariable]. | 323 /// /// #t2 is [contextVariable]. |
323 /// final _in::Context #t2 = this.{self::Closure#0::context}; | 324 /// final Vector #t2 = this.{self::Closure#0::context}; |
324 /// /* The body of [function]. */ | 325 /// /* The body of [function]. */ |
325 /// } | 326 /// } |
326 /// } | 327 /// } |
327 /// | 328 /// |
328 /// Returns a constructor call to invoke the above constructor. | 329 /// Returns a constructor call to invoke the above constructor. |
329 /// | 330 /// |
330 /// TODO(ahe): We shouldn't create a class for each closure. Instead we turn | 331 /// TODO(ahe): We shouldn't create a class for each closure. Instead we turn |
331 /// [function] into a top-level function and use the Dart VM's mechnism for | 332 /// [function] into a top-level function and use the Dart VM's mechnism for |
332 /// closures. | 333 /// closures. |
333 Expression addClosure( | 334 Expression addClosure( |
334 FunctionNode function, | 335 FunctionNode function, |
335 VariableDeclaration contextVariable, | 336 VariableDeclaration contextVariable, |
336 Expression accessContext, | 337 Expression accessContext, |
337 Map<TypeParameter, DartType> substitution, | 338 Map<TypeParameter, DartType> substitution, |
338 Map<TypeParameter, DartType> enclosingTypeSubstitution) { | 339 Map<TypeParameter, DartType> enclosingTypeSubstitution) { |
339 Field contextField = new Field( | 340 Field contextField = new Field( |
340 // TODO(ahe): Rename to #context. | 341 // TODO(ahe): Rename to #context. |
341 new Name("context"), | 342 new Name("context"), |
342 type: contextClass.rawType, | 343 type: const VectorType(), |
343 fileUri: currentFileUri); | 344 fileUri: currentFileUri); |
344 Class closureClass = createClosureClass(function, | 345 Class closureClass = createClosureClass(function, |
345 fields: [contextField], substitution: substitution); | 346 fields: [contextField], substitution: substitution); |
346 closureClass.addMember(new Procedure( | 347 closureClass.addMember(new Procedure( |
347 new Name("call"), ProcedureKind.Method, function, | 348 new Name("call"), ProcedureKind.Method, function, |
348 fileUri: currentFileUri)); | 349 fileUri: currentFileUri)); |
349 newLibraryMembers.add(closureClass); | 350 newLibraryMembers.add(closureClass); |
350 Statement note = new ExpressionStatement( | 351 Statement note = new ExpressionStatement( |
351 new StringLiteral("This is a temporary solution. " | 352 new StringLiteral("This is a temporary solution. " |
352 "In the VM, this will become an additional parameter.")); | 353 "In the VM, this will become an additional parameter.")); |
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
833 // TODO(ahe): Remove this method when we don't generate closure classes | 834 // TODO(ahe): Remove this method when we don't generate closure classes |
834 // anymore. | 835 // anymore. |
835 void addClosureClassNote(Class closureClass) { | 836 void addClosureClassNote(Class closureClass) { |
836 closureClass.addMember(new Field(new Name("note"), | 837 closureClass.addMember(new Field(new Name("note"), |
837 type: coreTypes.stringClass.rawType, | 838 type: coreTypes.stringClass.rawType, |
838 initializer: new StringLiteral( | 839 initializer: new StringLiteral( |
839 "This is temporary. The VM doesn't need closure classes."), | 840 "This is temporary. The VM doesn't need closure classes."), |
840 fileUri: currentFileUri)); | 841 fileUri: currentFileUri)); |
841 } | 842 } |
842 } | 843 } |
OLD | NEW |