| 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 // Note: copied from package:kernel at revision 7346348. | 5 // Note: copied from package:kernel at revision 7346348. |
| 6 | 6 |
| 7 /// A library to help transform compounds and null-aware accessors into | 7 /// A library to help transform compounds and null-aware accessors into |
| 8 /// let expressions. | 8 /// let expressions. |
| 9 library kernel.frontend.accessors; | 9 library kernel.frontend.accessors; |
| 10 | 10 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 } | 52 } |
| 53 var tmp = new VariableDeclaration.forValue(_makeRead()); | 53 var tmp = new VariableDeclaration.forValue(_makeRead()); |
| 54 return _finish(makeLet( | 54 return _finish(makeLet( |
| 55 tmp, | 55 tmp, |
| 56 new ConditionalExpression(buildIsNull(new VariableGet(tmp)), | 56 new ConditionalExpression(buildIsNull(new VariableGet(tmp)), |
| 57 _makeWrite(value, false), new VariableGet(tmp), type))); | 57 _makeWrite(value, false), new VariableGet(tmp), type))); |
| 58 } | 58 } |
| 59 | 59 |
| 60 /// Returns an [Expression] representing a compound assignment (e.g. `+=`) | 60 /// Returns an [Expression] representing a compound assignment (e.g. `+=`) |
| 61 /// with the accessor on the LHS and [value] on the RHS. | 61 /// with the accessor on the LHS and [value] on the RHS. |
| 62 Expression buildCompoundAssignment(Name binaryOperator, Expression value, | 62 Expression buildCompoundAssignment( |
| 63 Name binaryOperator, Expression value, int charOffset, |
| 63 {bool voidContext: false, Procedure interfaceTarget}) { | 64 {bool voidContext: false, Procedure interfaceTarget}) { |
| 64 return _finish(_makeWrite( | 65 return _finish(_makeWrite( |
| 65 makeBinary(_makeRead(), binaryOperator, interfaceTarget, value), | 66 makeBinary( |
| 67 _makeRead(), binaryOperator, interfaceTarget, value, charOffset), |
| 66 voidContext)); | 68 voidContext)); |
| 67 } | 69 } |
| 68 | 70 |
| 69 /// Returns an [Expression] representing a pre-increment or pre-decrement | 71 /// Returns an [Expression] representing a pre-increment or pre-decrement |
| 70 /// of the accessor. | 72 /// of the accessor. |
| 71 Expression buildPrefixIncrement(Name binaryOperator, | 73 Expression buildPrefixIncrement(Name binaryOperator, int charOffset, |
| 72 {bool voidContext: false, Procedure interfaceTarget}) { | 74 {bool voidContext: false, Procedure interfaceTarget}) { |
| 73 return buildCompoundAssignment(binaryOperator, new IntLiteral(1), | 75 return buildCompoundAssignment( |
| 76 binaryOperator, new IntLiteral(1), charOffset, |
| 74 voidContext: voidContext, interfaceTarget: interfaceTarget); | 77 voidContext: voidContext, interfaceTarget: interfaceTarget); |
| 75 } | 78 } |
| 76 | 79 |
| 77 /// Returns an [Expression] representing a post-increment or post-decrement | 80 /// Returns an [Expression] representing a post-increment or post-decrement |
| 78 /// of the accessor. | 81 /// of the accessor. |
| 79 Expression buildPostfixIncrement(Name binaryOperator, | 82 Expression buildPostfixIncrement(Name binaryOperator, int charOffset, |
| 80 {bool voidContext: false, Procedure interfaceTarget}) { | 83 {bool voidContext: false, Procedure interfaceTarget}) { |
| 81 if (voidContext) { | 84 if (voidContext) { |
| 82 return buildPrefixIncrement(binaryOperator, | 85 return buildPrefixIncrement(binaryOperator, charOffset, |
| 83 voidContext: true, interfaceTarget: interfaceTarget); | 86 voidContext: true, interfaceTarget: interfaceTarget); |
| 84 } | 87 } |
| 85 var value = new VariableDeclaration.forValue(_makeRead()); | 88 var value = new VariableDeclaration.forValue(_makeRead()); |
| 86 valueAccess() => new VariableGet(value); | 89 valueAccess() => new VariableGet(value); |
| 87 var dummy = new VariableDeclaration.forValue(_makeWrite( | 90 var dummy = new VariableDeclaration.forValue(_makeWrite( |
| 88 makeBinary( | 91 makeBinary(valueAccess(), binaryOperator, interfaceTarget, |
| 89 valueAccess(), binaryOperator, interfaceTarget, new IntLiteral(1)), | 92 new IntLiteral(1), charOffset), |
| 90 true)); | 93 true)); |
| 91 return _finish(makeLet(value, makeLet(dummy, valueAccess()))); | 94 return _finish(makeLet(value, makeLet(dummy, valueAccess()))); |
| 92 } | 95 } |
| 93 | 96 |
| 94 Expression _makeSimpleRead() => _makeRead(); | 97 Expression _makeSimpleRead() => _makeRead(); |
| 95 | 98 |
| 96 Expression _makeSimpleWrite(Expression value, bool voidContext) { | 99 Expression _makeSimpleWrite(Expression value, bool voidContext) { |
| 97 return _makeWrite(value, voidContext); | 100 return _makeWrite(value, voidContext); |
| 98 } | 101 } |
| 99 | 102 |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 | 245 |
| 243 final Name _indexGet = new Name('[]'); | 246 final Name _indexGet = new Name('[]'); |
| 244 final Name _indexSet = new Name('[]='); | 247 final Name _indexSet = new Name('[]='); |
| 245 | 248 |
| 246 class IndexAccessor extends Accessor { | 249 class IndexAccessor extends Accessor { |
| 247 Expression receiver; | 250 Expression receiver; |
| 248 Expression index; | 251 Expression index; |
| 249 VariableDeclaration receiverVariable; | 252 VariableDeclaration receiverVariable; |
| 250 VariableDeclaration indexVariable; | 253 VariableDeclaration indexVariable; |
| 251 Procedure getter, setter; | 254 Procedure getter, setter; |
| 255 int charOffset; |
| 252 | 256 |
| 253 static Accessor make(Expression receiver, Expression index, Procedure getter, | 257 static Accessor make(Expression receiver, Expression index, Procedure getter, |
| 254 Procedure setter) { | 258 Procedure setter, int charOffset) { |
| 255 if (receiver is ThisExpression) { | 259 if (receiver is ThisExpression) { |
| 256 return new ThisIndexAccessor(index, getter, setter); | 260 return new ThisIndexAccessor(index, getter, setter); |
| 257 } else { | 261 } else { |
| 258 return new IndexAccessor.internal(receiver, index, getter, setter); | 262 return new IndexAccessor.internal( |
| 263 receiver, index, getter, setter, charOffset); |
| 259 } | 264 } |
| 260 } | 265 } |
| 261 | 266 |
| 262 IndexAccessor.internal(this.receiver, this.index, this.getter, this.setter); | 267 IndexAccessor.internal( |
| 268 this.receiver, this.index, this.getter, this.setter, this.charOffset); |
| 263 | 269 |
| 264 _makeSimpleRead() => new MethodInvocation( | 270 _makeSimpleRead() => new MethodInvocation( |
| 265 receiver, _indexGet, new Arguments(<Expression>[index]), getter); | 271 receiver, _indexGet, new Arguments(<Expression>[index]), getter) |
| 272 ..fileOffset = charOffset; |
| 266 | 273 |
| 267 _makeSimpleWrite(Expression value, bool voidContext) { | 274 _makeSimpleWrite(Expression value, bool voidContext) { |
| 268 if (!voidContext) return _makeWriteAndReturn(value); | 275 if (!voidContext) return _makeWriteAndReturn(value); |
| 269 return new MethodInvocation( | 276 return new MethodInvocation( |
| 270 receiver, _indexSet, new Arguments(<Expression>[index, value]), setter); | 277 receiver, _indexSet, new Arguments(<Expression>[index, value]), setter) |
| 278 ..fileOffset = charOffset; |
| 271 } | 279 } |
| 272 | 280 |
| 273 receiverAccess() { | 281 receiverAccess() { |
| 274 // We cannot reuse the receiver if it is a variable since it might be | 282 // We cannot reuse the receiver if it is a variable since it might be |
| 275 // reassigned in the index expression. | 283 // reassigned in the index expression. |
| 276 receiverVariable ??= new VariableDeclaration.forValue(receiver); | 284 receiverVariable ??= new VariableDeclaration.forValue(receiver); |
| 277 return new VariableGet(receiverVariable); | 285 return new VariableGet(receiverVariable)..fileOffset = charOffset; |
| 278 } | 286 } |
| 279 | 287 |
| 280 indexAccess() { | 288 indexAccess() { |
| 281 indexVariable ??= new VariableDeclaration.forValue(index); | 289 indexVariable ??= new VariableDeclaration.forValue(index); |
| 282 return new VariableGet(indexVariable); | 290 return new VariableGet(indexVariable)..fileOffset = charOffset; |
| 283 } | 291 } |
| 284 | 292 |
| 285 _makeRead() { | 293 _makeRead() { |
| 286 return new MethodInvocation(receiverAccess(), _indexGet, | 294 return new MethodInvocation( |
| 287 new Arguments(<Expression>[indexAccess()]), getter); | 295 receiverAccess(), |
| 296 _indexGet, |
| 297 new Arguments(<Expression>[indexAccess()]), |
| 298 getter)..fileOffset = charOffset; |
| 288 } | 299 } |
| 289 | 300 |
| 290 _makeWrite(Expression value, bool voidContext) { | 301 _makeWrite(Expression value, bool voidContext) { |
| 291 if (!voidContext) return _makeWriteAndReturn(value); | 302 if (!voidContext) return _makeWriteAndReturn(value); |
| 292 return new MethodInvocation(receiverAccess(), _indexSet, | 303 return new MethodInvocation( |
| 293 new Arguments(<Expression>[indexAccess(), value]), setter); | 304 receiverAccess(), |
| 305 _indexSet, |
| 306 new Arguments(<Expression>[indexAccess(), value]), |
| 307 setter)..fileOffset = charOffset; |
| 294 } | 308 } |
| 295 | 309 |
| 296 _makeWriteAndReturn(Expression value) { | 310 _makeWriteAndReturn(Expression value) { |
| 297 // The call to []= does not return the value like direct-style assignments | 311 // The call to []= does not return the value like direct-style assignments |
| 298 // do. We need to bind the value in a let. | 312 // do. We need to bind the value in a let. |
| 299 var valueVariable = new VariableDeclaration.forValue(value); | 313 var valueVariable = new VariableDeclaration.forValue(value); |
| 300 var dummy = new VariableDeclaration.forValue(new MethodInvocation( | 314 var dummy = new VariableDeclaration.forValue(new MethodInvocation( |
| 301 receiverAccess(), | 315 receiverAccess(), |
| 302 _indexSet, | 316 _indexSet, |
| 303 new Arguments( | 317 new Arguments( |
| 304 <Expression>[indexAccess(), new VariableGet(valueVariable)]), | 318 <Expression>[indexAccess(), new VariableGet(valueVariable)]), |
| 305 setter)); | 319 setter)..fileOffset = charOffset); |
| 306 return makeLet( | 320 return makeLet( |
| 307 valueVariable, makeLet(dummy, new VariableGet(valueVariable))); | 321 valueVariable, makeLet(dummy, new VariableGet(valueVariable))); |
| 308 } | 322 } |
| 309 | 323 |
| 310 Expression _finish(Expression body) { | 324 Expression _finish(Expression body) { |
| 311 return makeLet(receiverVariable, makeLet(indexVariable, body)); | 325 return makeLet(receiverVariable, makeLet(indexVariable, body)); |
| 312 } | 326 } |
| 313 } | 327 } |
| 314 | 328 |
| 315 /// Special case of [IndexAccessor] to avoid creating an indirect access to | 329 /// Special case of [IndexAccessor] to avoid creating an indirect access to |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 | 456 |
| 443 Expression _finish(Expression body) => makeLet(value, body); | 457 Expression _finish(Expression body) => makeLet(value, body); |
| 444 } | 458 } |
| 445 | 459 |
| 446 Expression makeLet(VariableDeclaration variable, Expression body) { | 460 Expression makeLet(VariableDeclaration variable, Expression body) { |
| 447 if (variable == null) return body; | 461 if (variable == null) return body; |
| 448 return new Let(variable, body); | 462 return new Let(variable, body); |
| 449 } | 463 } |
| 450 | 464 |
| 451 Expression makeBinary(Expression left, Name operator, Procedure interfaceTarget, | 465 Expression makeBinary(Expression left, Name operator, Procedure interfaceTarget, |
| 452 Expression right) { | 466 Expression right, int charOffset) { |
| 453 return new MethodInvocation( | 467 return new MethodInvocation( |
| 454 left, operator, new Arguments(<Expression>[right]), interfaceTarget); | 468 left, operator, new Arguments(<Expression>[right]), interfaceTarget) |
| 469 ..fileOffset = charOffset; |
| 455 } | 470 } |
| 456 | 471 |
| 457 final Name _equalOperator = new Name('=='); | 472 final Name _equalOperator = new Name('=='); |
| 458 | 473 |
| 459 Expression buildIsNull(Expression value) { | 474 Expression buildIsNull(Expression value) { |
| 460 return makeBinary(value, _equalOperator, null, new NullLiteral()); | 475 return makeBinary( |
| 476 value, _equalOperator, null, new NullLiteral(), TreeNode.noOffset); |
| 461 } | 477 } |
| 462 | 478 |
| 463 VariableDeclaration makeOrReuseVariable(Expression value) { | 479 VariableDeclaration makeOrReuseVariable(Expression value) { |
| 464 // TODO: Devise a way to remember if a variable declaration was reused | 480 // TODO: Devise a way to remember if a variable declaration was reused |
| 465 // or is fresh (hence needs a let binding). | 481 // or is fresh (hence needs a let binding). |
| 466 return new VariableDeclaration.forValue(value); | 482 return new VariableDeclaration.forValue(value); |
| 467 } | 483 } |
| 468 | 484 |
| 469 Expression wrapInvalid(Expression e) { | 485 Expression wrapInvalid(Expression e) { |
| 470 return new Let(new VariableDeclaration.forValue(e), new InvalidExpression()); | 486 return new Let(new VariableDeclaration.forValue(e), new InvalidExpression()); |
| 471 } | 487 } |
| OLD | NEW |