Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(126)

Side by Side Diff: pkg/kernel/lib/transformations/closure/rewriter.dart

Issue 2981603002: Convert closures in all initializers, and share the context between them. (Closed)
Patch Set: Review comments. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 _insertStatement(new ExpressionStatement( 78 _insertStatement(new ExpressionStatement(
79 new VectorSet(new VariableGet(contextDeclaration), 0, accessParent))); 79 new VectorSet(new VariableGet(contextDeclaration), 0, accessParent)));
80 } 80 }
81 } 81 }
82 82
83 void insertExtendContext(VectorSet extender) { 83 void insertExtendContext(VectorSet extender) {
84 _insertStatement(new ExpressionStatement(extender)); 84 _insertStatement(new ExpressionStatement(extender));
85 } 85 }
86 } 86 }
87 87
88 /// Creates and updates the context as [Let] bindings around the initializer 88 class InitializerListRewriter extends AstRewriter {
89 /// expression. 89 final Constructor parentConstructor;
90 abstract class InitializerRewriter extends AstRewriter { 90 final List<Initializer> prefix = [];
91 final Expression initializingExpression;
92 91
93 InitializerRewriter(this.initializingExpression); 92 InitializerListRewriter(this.parentConstructor);
94 93
95 @override 94 @override
96 BlockRewriter forNestedBlock(Block block) { 95 BlockRewriter forNestedBlock(Block block) {
97 return new BlockRewriter(block); 96 return new BlockRewriter(block);
98 } 97 }
99 98
100 @override 99 @override
101 void insertContextDeclaration(Expression accessParent) { 100 void insertContextDeclaration(Expression accessParent) {
102 _createDeclaration(); 101 _createDeclaration();
103 var oldParent = initializingExpression.parent; 102 var init = new LocalInitializer(contextDeclaration);
104 Let binding = new Let(contextDeclaration, initializingExpression); 103 init.parent = parentConstructor;
105 binding.parent = oldParent; 104 prefix.add(init);
106 setInitializerExpression(binding);
107 } 105 }
108 106
109 @override 107 @override
110 void insertExtendContext(VectorSet extender) { 108 void insertExtendContext(VectorSet extender) {
111 Let parent = initializingExpression.parent; 109 var init = new LocalInitializer(
112 Let binding = new Let(new VariableDeclaration(null, initializer: extender), 110 new VariableDeclaration(null, initializer: extender));
113 initializingExpression); 111 init.parent = parentConstructor;
114 parent.body = binding; 112 prefix.add(init);
115 binding.parent = parent;
116 }
117
118 void setInitializerExpression(Expression expression);
119 }
120
121 class FieldInitializerRewriter extends InitializerRewriter {
122 FieldInitializerRewriter(Expression initializingExpression)
123 : super(initializingExpression) {
124 assert(initializingExpression.parent is FieldInitializer);
125 }
126
127 void setInitializerExpression(Expression expression) {
128 (expression.parent as FieldInitializer).value = expression;
129 } 113 }
130 } 114 }
131
132 class LocalInitializerRewriter extends InitializerRewriter {
133 LocalInitializerRewriter(Expression initializingExpression)
134 : super(initializingExpression) {
135 // The initializer is up two levels because the variable declaration node is
136 // in between.
137 assert(initializingExpression.parent.parent is LocalInitializer);
138 }
139
140 void setInitializerExpression(Expression expression) {
141 (expression.parent as VariableDeclaration).initializer = expression;
142 }
143 }
OLDNEW
« no previous file with comments | « pkg/kernel/lib/transformations/closure/info.dart ('k') | pkg/kernel/test/closures_initializers/suite.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698