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 AsyncMarker, |
9 Arguments, | 10 Arguments, |
10 Block, | 11 Block, |
11 Catch, | 12 Catch, |
12 Class, | 13 Class, |
13 ClosureCreation, | 14 ClosureCreation, |
14 Constructor, | 15 Constructor, |
15 DartType, | 16 DartType, |
| 17 DoStatement, |
16 EmptyStatement, | 18 EmptyStatement, |
17 Expression, | 19 Expression, |
18 ExpressionStatement, | 20 ExpressionStatement, |
19 Field, | 21 Field, |
20 ForInStatement, | 22 ForInStatement, |
21 ForStatement, | 23 ForStatement, |
22 FunctionDeclaration, | 24 FunctionDeclaration, |
23 FunctionExpression, | 25 FunctionExpression, |
24 FunctionNode, | 26 FunctionNode, |
25 FunctionType, | 27 FunctionType, |
(...skipping 19 matching lines...) Expand all Loading... |
45 ThisExpression, | 47 ThisExpression, |
46 Transformer, | 48 Transformer, |
47 TreeNode, | 49 TreeNode, |
48 TypeParameter, | 50 TypeParameter, |
49 TypeParameterType, | 51 TypeParameterType, |
50 VariableDeclaration, | 52 VariableDeclaration, |
51 VariableGet, | 53 VariableGet, |
52 VariableSet, | 54 VariableSet, |
53 VectorCreation, | 55 VectorCreation, |
54 VectorType, | 56 VectorType, |
| 57 WhileStatement, |
55 transformList; | 58 transformList; |
56 | 59 |
57 import '../../frontend/accessors.dart' show VariableAccessor; | 60 import '../../frontend/accessors.dart' show VariableAccessor; |
58 | 61 |
59 import '../../clone.dart' show CloneVisitor; | 62 import '../../clone.dart' show CloneVisitor; |
60 | 63 |
61 import '../../core_types.dart' show CoreTypes; | 64 import '../../core_types.dart' show CoreTypes; |
62 | 65 |
63 import '../../type_algebra.dart' show substitute; | 66 import '../../type_algebra.dart' show substitute; |
64 | 67 |
65 import 'clone_without_body.dart' show CloneWithoutBody; | 68 import 'clone_without_body.dart' show CloneWithoutBody; |
66 | 69 |
67 import 'context.dart' show Context, NoContext, LocalContext; | 70 import 'context.dart' show Context, NoContext, LocalContext; |
68 | 71 |
69 import 'info.dart' show ClosureInfo; | 72 import 'info.dart' show ClosureInfo; |
70 | 73 |
71 import 'rewriter.dart' show AstRewriter, BlockRewriter, InitializerListRewriter; | 74 import 'rewriter.dart' show AstRewriter, BlockRewriter, InitializerListRewriter; |
72 | 75 |
| 76 bool isLoop(TreeNode node) { |
| 77 return node is WhileStatement || |
| 78 node is DoStatement || |
| 79 node is ForStatement || |
| 80 node is ForInStatement; |
| 81 } |
| 82 |
73 class ClosureConverter extends Transformer { | 83 class ClosureConverter extends Transformer { |
74 final CoreTypes coreTypes; | 84 final CoreTypes coreTypes; |
75 | 85 |
76 final Set<VariableDeclaration> capturedVariables; | 86 final Set<VariableDeclaration> capturedVariables; |
77 | 87 |
78 // This map pairs variables that are captured with flags indicating whether | 88 // This map pairs variables that are captured with flags indicating whether |
79 // they are used inside or outside an initializer. See | 89 // they are used inside or outside an initializer. See |
80 // [ClosureInfo.parameterUses]. | 90 // [ClosureInfo.parameterUses]. |
81 final Map<VariableDeclaration, int> parameterUses; | 91 final Map<VariableDeclaration, int> parameterUses; |
82 | 92 |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 TreeNode visitField(Field node) { | 350 TreeNode visitField(Field node) { |
341 currentMember = node; | 351 currentMember = node; |
342 context = new NoContext(this); | 352 context = new NoContext(this); |
343 node = super.visitField(node); | 353 node = super.visitField(node); |
344 context = null; | 354 context = null; |
345 currentMember = null; | 355 currentMember = null; |
346 return node; | 356 return node; |
347 } | 357 } |
348 | 358 |
349 Expression handleLocalFunction(FunctionNode function) { | 359 Expression handleLocalFunction(FunctionNode function) { |
| 360 if (function.asyncMarker == AsyncMarker.SyncYielding) { |
| 361 function.transformChildren(this); |
| 362 return new FunctionExpression(function); |
| 363 } |
350 FunctionNode enclosingFunction = currentFunction; | 364 FunctionNode enclosingFunction = currentFunction; |
351 Map<TypeParameter, DartType> enclosingTypeSubstitution = typeSubstitution; | 365 Map<TypeParameter, DartType> enclosingTypeSubstitution = typeSubstitution; |
352 currentFunction = function; | 366 currentFunction = function; |
353 Statement body = function.body; | 367 Statement body = function.body; |
354 assert(body != null); | 368 assert(body != null); |
355 | 369 |
356 rewriter = makeRewriterForBody(function); | 370 rewriter = makeRewriterForBody(function); |
357 | 371 |
358 VariableDeclaration contextVariable = | 372 VariableDeclaration contextVariable = |
359 new VariableDeclaration("#contextParameter", type: const VectorType()); | 373 new VariableDeclaration("#contextParameter", type: const VectorType()); |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 .forEach(extendContextConditionally(inInitializer: false)); | 561 .forEach(extendContextConditionally(inInitializer: false)); |
548 assert(node.body != null); | 562 assert(node.body != null); |
549 node.body = node.body.accept(this); | 563 node.body = node.body.accept(this); |
550 node.body.parent = node; | 564 node.body.parent = node; |
551 return node; | 565 return node; |
552 } | 566 } |
553 | 567 |
554 TreeNode visitBlock(Block node) { | 568 TreeNode visitBlock(Block node) { |
555 return saveContext(() { | 569 return saveContext(() { |
556 BlockRewriter blockRewriter = rewriter = rewriter.forNestedBlock(node); | 570 BlockRewriter blockRewriter = rewriter = rewriter.forNestedBlock(node); |
| 571 if (node.parent is Statement && |
| 572 isLoop(node.parent) && |
| 573 context is! NoContext) { |
| 574 context = context.toNestedContext(); |
| 575 } |
557 blockRewriter.transformStatements(this); | 576 blockRewriter.transformStatements(this); |
558 return node; | 577 return node; |
559 }); | 578 }); |
560 } | 579 } |
561 | 580 |
562 TreeNode visitVariableDeclaration(VariableDeclaration node) { | 581 TreeNode visitVariableDeclaration(VariableDeclaration node) { |
563 node.transformChildren(this); | 582 node.transformChildren(this); |
564 | 583 |
565 if (!capturedVariables.contains(node)) return node; | 584 if (!capturedVariables.contains(node)) return node; |
566 if (node.initializer == null && node.parent is FunctionNode) { | 585 if (node.initializer == null && node.parent is FunctionNode) { |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 copy.function.body.parent = copy.function; | 839 copy.function.body.parent = copy.function; |
821 return copy; | 840 return copy; |
822 } | 841 } |
823 | 842 |
824 void addGetterForwarder(Name name, Procedure getter) { | 843 void addGetterForwarder(Name name, Procedure getter) { |
825 assert(getter.isGetter); | 844 assert(getter.isGetter); |
826 newClassMembers | 845 newClassMembers |
827 .add(copyWithBody(getter, forwardToThisProperty(getter))..name = name); | 846 .add(copyWithBody(getter, forwardToThisProperty(getter))..name = name); |
828 } | 847 } |
829 } | 848 } |
OLD | NEW |