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 library kernel.transformations.closure.rewriter; | 5 library kernel.transformations.closure.rewriter; |
6 | 6 |
7 import '../../ast.dart'; | 7 import '../../ast.dart'; |
8 import 'converter.dart' show ClosureConverter; | 8 import 'converter.dart' show ClosureConverter; |
9 | 9 |
10 /// Used by the [Context] to initialize and update the context variable | 10 /// Used by the [Context] to initialize and update the context variable |
11 /// used to capture the variables closed over by functions. | 11 /// used to capture the variables closed over by functions. |
12 abstract class AstRewriter { | 12 abstract class AstRewriter { |
13 /// The declared variable that holds the context. | 13 /// The declared variable that holds the context. |
14 VariableDeclaration contextDeclaration; | 14 VariableDeclaration contextDeclaration; |
15 | 15 |
16 /// Expression that is used to initialize the vector representing the context. | 16 /// Expression that is used to initialize the vector representing the context. |
17 /// It's [length] field is modified by the [extend] operation | 17 /// It's [length] field is modified by the [extend] operation |
18 VectorCreation vectorCreation; | 18 VectorCreation vectorCreation; |
19 | 19 |
20 /// Creates a new [AstRewriter] for a (nested) [Block]. | 20 /// Creates a new [AstRewriter] for a (nested) [Block]. |
21 BlockRewriter forNestedBlock(Block block); | 21 BlockRewriter forNestedBlock(Block block); |
22 | 22 |
23 /// Inserts an allocation of a context and initializes [contextDeclaration] | 23 /// Inserts an allocation of a context and initializes [contextDeclaration] |
24 /// and [vectorCreation]. | 24 /// and [vectorCreation]. |
25 void insertContextDeclaration(Expression accessParent); | 25 void insertContextDeclaration(Expression accessParent); |
26 | 26 |
27 /// Inserts an expression or statement that extends the context. | 27 /// Inserts an expression or statement that extends the context. |
28 void insertExtendContext(VectorSet extender); | 28 void insertExtendContext(VectorSet extender); |
29 | 29 |
| 30 /// Inserts an expression that sets a parameter to NULL, so we don't have |
| 31 /// unnecessary references to it. |
| 32 void insertZeroOutParameter(VariableDeclaration parameter); |
| 33 |
30 void _createDeclaration() { | 34 void _createDeclaration() { |
31 assert(contextDeclaration == null && vectorCreation == null); | 35 assert(contextDeclaration == null && vectorCreation == null); |
32 | 36 |
33 // Context size is set to 2 initially, because the 0-th element of it holds | 37 // Context size is set to 2 initially, because the 0-th element of it holds |
34 // the vector of type arguments that the VM creates, and the 1-st element | 38 // the vector of type arguments that the VM creates, and the 1-st element |
35 // works as a link to the parent context. | 39 // works as a link to the parent context. |
36 vectorCreation = new VectorCreation(2); | 40 vectorCreation = new VectorCreation(2); |
37 contextDeclaration = new VariableDeclaration.forValue(vectorCreation, | 41 contextDeclaration = new VariableDeclaration.forValue(vectorCreation, |
38 type: const DynamicType()); | 42 type: const DynamicType()); |
39 contextDeclaration.name = "#context"; | 43 contextDeclaration.name = "#context"; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 if (accessParent is! NullLiteral) { | 81 if (accessParent is! NullLiteral) { |
78 // Index 1 of a context always points to the parent. | 82 // Index 1 of a context always points to the parent. |
79 _insertStatement(new ExpressionStatement( | 83 _insertStatement(new ExpressionStatement( |
80 new VectorSet(new VariableGet(contextDeclaration), 1, accessParent))); | 84 new VectorSet(new VariableGet(contextDeclaration), 1, accessParent))); |
81 } | 85 } |
82 } | 86 } |
83 | 87 |
84 void insertExtendContext(VectorSet extender) { | 88 void insertExtendContext(VectorSet extender) { |
85 _insertStatement(new ExpressionStatement(extender)); | 89 _insertStatement(new ExpressionStatement(extender)); |
86 } | 90 } |
| 91 |
| 92 void insertZeroOutParameter(VariableDeclaration parameter) { |
| 93 _insertStatement( |
| 94 new ExpressionStatement(new VariableSet(parameter, new NullLiteral()))); |
| 95 } |
87 } | 96 } |
88 | 97 |
89 class InitializerListRewriter extends AstRewriter { | 98 class InitializerListRewriter extends AstRewriter { |
90 final Constructor parentConstructor; | 99 final Constructor parentConstructor; |
91 final List<Initializer> prefix = []; | 100 final List<Initializer> prefix = []; |
92 | 101 |
93 InitializerListRewriter(this.parentConstructor); | 102 InitializerListRewriter(this.parentConstructor); |
94 | 103 |
95 @override | 104 @override |
96 BlockRewriter forNestedBlock(Block block) { | 105 BlockRewriter forNestedBlock(Block block) { |
97 return new BlockRewriter(block); | 106 return new BlockRewriter(block); |
98 } | 107 } |
99 | 108 |
100 @override | 109 @override |
101 void insertContextDeclaration(Expression accessParent) { | 110 void insertContextDeclaration(Expression accessParent) { |
102 _createDeclaration(); | 111 _createDeclaration(); |
103 var init = new LocalInitializer(contextDeclaration); | 112 var init = new LocalInitializer(contextDeclaration); |
104 init.parent = parentConstructor; | 113 init.parent = parentConstructor; |
105 prefix.add(init); | 114 prefix.add(init); |
106 } | 115 } |
107 | 116 |
108 @override | 117 @override |
109 void insertExtendContext(VectorSet extender) { | 118 void insertExtendContext(VectorSet extender) { |
110 var init = new LocalInitializer( | 119 var init = new LocalInitializer( |
111 new VariableDeclaration(null, initializer: extender)); | 120 new VariableDeclaration(null, initializer: extender)); |
112 init.parent = parentConstructor; | 121 init.parent = parentConstructor; |
113 prefix.add(init); | 122 prefix.add(init); |
114 } | 123 } |
| 124 |
| 125 @override |
| 126 void insertZeroOutParameter(VariableDeclaration parameter) { |
| 127 var init = new LocalInitializer(new VariableDeclaration(null, |
| 128 initializer: new VariableSet(parameter, new NullLiteral()))); |
| 129 init.parent = parentConstructor; |
| 130 prefix.add(init); |
| 131 } |
115 } | 132 } |
OLD | NEW |