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 /// A library to help transform compounds and null-aware accessors into | 5 /// A library to help transform compounds and null-aware accessors into |
6 /// let expressions. | 6 /// let expressions. |
7 library kernel.frontend.accessors; | 7 library kernel.frontend.accessors; |
8 | 8 |
9 import '../ast.dart'; | 9 import '../ast.dart'; |
10 | 10 |
11 abstract class Accessor { | 11 abstract class Accessor { |
12 // [builtBinary] and [builtGetter] capture the inner nodes. Used by | |
13 // dart2js+rasta for determining how subexpressions map to legacy dart2js Ast | |
14 // nodes. This will be removed once dart2js type analysis (aka inference) is | |
15 // reimplemented on kernel. | |
16 Expression builtBinary; | |
17 Expression builtGetter; | |
asgerf
2016/11/24 09:44:33
LGTM
sra1
2016/11/24 23:53:52
Acknowledged.
| |
18 | |
12 Expression buildSimpleRead() { | 19 Expression buildSimpleRead() { |
13 return _finish(_makeSimpleRead()); | 20 return _finish(_makeSimpleRead()); |
14 } | 21 } |
15 | 22 |
16 /// Returns an assignment to the accessor. | 23 /// Returns an assignment to the accessor. |
17 /// | 24 /// |
18 /// The returned expression evaluates to the assigned value, unless | 25 /// The returned expression evaluates to the assigned value, unless |
19 /// [voidContext] is true, in which case it may evaluate to anything. | 26 /// [voidContext] is true, in which case it may evaluate to anything. |
20 Expression buildAssignment(Expression value, {bool voidContext: false}) { | 27 Expression buildAssignment(Expression value, {bool voidContext: false}) { |
21 return _finish(_makeSimpleWrite(value, voidContext)); | 28 return _finish(_makeSimpleWrite(value, voidContext)); |
22 } | 29 } |
23 | 30 |
24 Expression buildNullAwareAssignment(Expression value, DartType type, | 31 Expression buildNullAwareAssignment(Expression value, DartType type, |
25 {bool voidContext: false}) { | 32 {bool voidContext: false}) { |
26 if (voidContext) { | 33 if (voidContext) { |
27 return _finish(new ConditionalExpression(buildIsNull(_makeRead()), | 34 return _finish(new ConditionalExpression(buildIsNull(_makeRead()), |
28 _makeWrite(value, voidContext), new NullLiteral(), type)); | 35 _makeWrite(value, voidContext), new NullLiteral(), type)); |
29 } | 36 } |
30 var tmp = new VariableDeclaration.forValue(_makeRead()); | 37 var tmp = new VariableDeclaration.forValue(_makeRead()); |
31 return _finish(makeLet( | 38 return _finish(makeLet( |
32 tmp, | 39 tmp, |
33 new ConditionalExpression(buildIsNull(new VariableGet(tmp)), | 40 new ConditionalExpression(buildIsNull(new VariableGet(tmp)), |
34 _makeWrite(value, voidContext), new VariableGet(tmp), type))); | 41 _makeWrite(value, voidContext), new VariableGet(tmp), type))); |
35 } | 42 } |
36 | 43 |
37 Expression buildCompoundAssignment(Name binaryOperator, Expression value, | 44 Expression buildCompoundAssignment(Name binaryOperator, Expression value, |
38 {bool voidContext: false, Procedure interfaceTarget}) { | 45 {bool voidContext: false, Procedure interfaceTarget}) { |
39 return _finish(_makeWrite( | 46 return _finish(_makeWrite( |
40 makeBinary(_makeRead(), binaryOperator, interfaceTarget, value), | 47 builtBinary = |
48 makeBinary(_makeRead(), binaryOperator, interfaceTarget, value), | |
41 voidContext)); | 49 voidContext)); |
42 } | 50 } |
43 | 51 |
44 Expression buildPrefixIncrement(Name binaryOperator, | 52 Expression buildPrefixIncrement(Name binaryOperator, |
45 {bool voidContext: false, Procedure interfaceTarget}) { | 53 {bool voidContext: false, Procedure interfaceTarget}) { |
46 return buildCompoundAssignment(binaryOperator, new IntLiteral(1), | 54 return buildCompoundAssignment(binaryOperator, new IntLiteral(1), |
47 voidContext: voidContext, interfaceTarget: interfaceTarget); | 55 voidContext: voidContext, interfaceTarget: interfaceTarget); |
48 } | 56 } |
49 | 57 |
50 Expression buildPostfixIncrement(Name binaryOperator, | 58 Expression buildPostfixIncrement(Name binaryOperator, |
51 {bool voidContext: false, Procedure interfaceTarget}) { | 59 {bool voidContext: false, Procedure interfaceTarget}) { |
52 if (voidContext) { | 60 if (voidContext) { |
53 return buildPrefixIncrement(binaryOperator, | 61 return buildPrefixIncrement(binaryOperator, |
54 voidContext: true, interfaceTarget: interfaceTarget); | 62 voidContext: true, interfaceTarget: interfaceTarget); |
55 } | 63 } |
56 var value = new VariableDeclaration.forValue(_makeRead()); | 64 var value = new VariableDeclaration.forValue(_makeRead()); |
57 valueAccess() => new VariableGet(value); | 65 valueAccess() => new VariableGet(value); |
58 var dummy = new VariableDeclaration.forValue(_makeWrite( | 66 var dummy = new VariableDeclaration.forValue(_makeWrite( |
59 makeBinary( | 67 builtBinary = makeBinary( |
60 valueAccess(), binaryOperator, interfaceTarget, new IntLiteral(1)), | 68 valueAccess(), binaryOperator, interfaceTarget, new IntLiteral(1)), |
61 true)); | 69 true)); |
62 return _finish(makeLet(value, makeLet(dummy, valueAccess()))); | 70 return _finish(makeLet(value, makeLet(dummy, valueAccess()))); |
63 } | 71 } |
64 | 72 |
65 Expression _makeSimpleRead() => _makeRead(); | 73 Expression _makeSimpleRead() => _makeRead(); |
66 | 74 |
67 Expression _makeSimpleWrite(Expression value, bool voidContext) { | 75 Expression _makeSimpleWrite(Expression value, bool voidContext) { |
68 return _makeWrite(value, voidContext); | 76 return _makeWrite(value, voidContext); |
69 } | 77 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 _makeSimpleRead() => new PropertyGet(receiver, name, getter); | 123 _makeSimpleRead() => new PropertyGet(receiver, name, getter); |
116 _makeSimpleWrite(Expression value, bool voidContext) { | 124 _makeSimpleWrite(Expression value, bool voidContext) { |
117 return new PropertySet(receiver, name, value, setter); | 125 return new PropertySet(receiver, name, value, setter); |
118 } | 126 } |
119 | 127 |
120 receiverAccess() { | 128 receiverAccess() { |
121 _receiverVariable ??= new VariableDeclaration.forValue(receiver); | 129 _receiverVariable ??= new VariableDeclaration.forValue(receiver); |
122 return new VariableGet(_receiverVariable); | 130 return new VariableGet(_receiverVariable); |
123 } | 131 } |
124 | 132 |
125 _makeRead() => new PropertyGet(receiverAccess(), name, getter); | 133 _makeRead() => builtGetter = new PropertyGet(receiverAccess(), name, getter); |
126 | 134 |
127 _makeWrite(Expression value, bool voidContext) { | 135 _makeWrite(Expression value, bool voidContext) { |
128 return new PropertySet(receiverAccess(), name, value, setter); | 136 return new PropertySet(receiverAccess(), name, value, setter); |
129 } | 137 } |
130 | 138 |
131 _finish(Expression body) => makeLet(_receiverVariable, body); | 139 _finish(Expression body) => makeLet(_receiverVariable, body); |
132 } | 140 } |
133 | 141 |
134 /// Special case of [PropertyAccessor] to avoid creating an indirect access to | 142 /// Special case of [PropertyAccessor] to avoid creating an indirect access to |
135 /// 'this'. | 143 /// 'this'. |
136 class ThisPropertyAccessor extends Accessor { | 144 class ThisPropertyAccessor extends Accessor { |
137 Name name; | 145 Name name; |
138 Member getter, setter; | 146 Member getter, setter; |
139 | 147 |
140 ThisPropertyAccessor(this.name, this.getter, this.setter); | 148 ThisPropertyAccessor(this.name, this.getter, this.setter); |
141 | 149 |
142 _makeRead() => new PropertyGet(new ThisExpression(), name, getter); | 150 _makeRead() => builtGetter = new PropertyGet(new ThisExpression(), name, gette r); |
asgerf
2016/11/24 09:44:33
Long line (please use dartfmt in pkg/kernel)
sra1
2016/11/24 23:53:52
Done.
| |
143 | 151 |
144 _makeWrite(Expression value, bool voidContext) { | 152 _makeWrite(Expression value, bool voidContext) { |
145 return new PropertySet(new ThisExpression(), name, value, setter); | 153 return new PropertySet(new ThisExpression(), name, value, setter); |
146 } | 154 } |
147 } | 155 } |
148 | 156 |
149 class NullAwarePropertyAccessor extends Accessor { | 157 class NullAwarePropertyAccessor extends Accessor { |
150 VariableDeclaration receiver; | 158 VariableDeclaration receiver; |
151 Name name; | 159 Name name; |
152 Member getter, setter; | 160 Member getter, setter; |
153 DartType type; | 161 DartType type; |
154 | 162 |
155 NullAwarePropertyAccessor( | 163 NullAwarePropertyAccessor( |
156 Expression receiver, this.name, this.getter, this.setter, this.type) | 164 Expression receiver, this.name, this.getter, this.setter, this.type) |
157 : this.receiver = makeOrReuseVariable(receiver); | 165 : this.receiver = makeOrReuseVariable(receiver); |
158 | 166 |
159 receiverAccess() => new VariableGet(receiver); | 167 receiverAccess() => new VariableGet(receiver); |
160 | 168 |
161 _makeRead() => new PropertyGet(receiverAccess(), name, getter); | 169 _makeRead() => builtGetter = new PropertyGet(receiverAccess(), name, getter); |
162 | 170 |
163 _makeWrite(Expression value, bool voidContext) { | 171 _makeWrite(Expression value, bool voidContext) { |
164 return new PropertySet(receiverAccess(), name, value, setter); | 172 return new PropertySet(receiverAccess(), name, value, setter); |
165 } | 173 } |
166 | 174 |
167 _finish(Expression body) => makeLet( | 175 _finish(Expression body) => makeLet( |
168 receiver, | 176 receiver, |
169 new ConditionalExpression( | 177 new ConditionalExpression( |
170 buildIsNull(receiverAccess()), new NullLiteral(), body, type)); | 178 buildIsNull(receiverAccess()), new NullLiteral(), body, type)); |
171 } | 179 } |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
405 | 413 |
406 VariableDeclaration makeOrReuseVariable(Expression value) { | 414 VariableDeclaration makeOrReuseVariable(Expression value) { |
407 // TODO: Devise a way to remember if a variable declaration was reused | 415 // TODO: Devise a way to remember if a variable declaration was reused |
408 // or is fresh (hence needs a let binding). | 416 // or is fresh (hence needs a let binding). |
409 return new VariableDeclaration.forValue(value); | 417 return new VariableDeclaration.forValue(value); |
410 } | 418 } |
411 | 419 |
412 Expression wrapInvalid(Expression e) { | 420 Expression wrapInvalid(Expression e) { |
413 return new Let(new VariableDeclaration.forValue(e), new InvalidExpression()); | 421 return new Let(new VariableDeclaration.forValue(e), new InvalidExpression()); |
414 } | 422 } |
OLD | NEW |