OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of dart2js; | 5 part of dart2js; |
6 | 6 |
7 const DART_CONSTANT_SYSTEM = const DartConstantSystem(); | 7 const DART_CONSTANT_SYSTEM = const DartConstantSystem(); |
8 | 8 |
9 class BitNotOperation implements UnaryOperation { | 9 class BitNotOperation implements UnaryOperation { |
10 final String name = '~'; | 10 final String name = '~'; |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 BoolConstantValue fold(ConstantValue left, ConstantValue right) { | 311 BoolConstantValue fold(ConstantValue left, ConstantValue right) { |
312 // In order to preserve runtime semantics which says that NaN !== NaN don't | 312 // In order to preserve runtime semantics which says that NaN !== NaN don't |
313 // constant fold NaN === NaN. Otherwise the output depends on inlined | 313 // constant fold NaN === NaN. Otherwise the output depends on inlined |
314 // variables and other optimizations. | 314 // variables and other optimizations. |
315 if (left.isNaN && right.isNaN) return null; | 315 if (left.isNaN && right.isNaN) return null; |
316 return DART_CONSTANT_SYSTEM.createBool(left == right); | 316 return DART_CONSTANT_SYSTEM.createBool(left == right); |
317 } | 317 } |
318 apply(left, right) => identical(left, right); | 318 apply(left, right) => identical(left, right); |
319 } | 319 } |
320 | 320 |
| 321 abstract class CodeUnitAtOperation implements BinaryOperation { |
| 322 final String name = 'charCodeAt'; |
| 323 const CodeUnitAtOperation(); |
| 324 apply(left, right) => left.codeUnitAt(right); |
| 325 } |
| 326 |
| 327 class CodeUnitAtConstantOperation extends CodeUnitAtOperation { |
| 328 const CodeUnitAtConstantOperation(); |
| 329 ConstantValue fold(ConstantValue left, ConstantValue right) { |
| 330 // 'a'.codeUnitAt(0) is not a constant expression. |
| 331 return null; |
| 332 } |
| 333 } |
| 334 |
| 335 class CodeUnitAtRuntimeOperation extends CodeUnitAtOperation { |
| 336 const CodeUnitAtRuntimeOperation(); |
| 337 IntConstantValue fold(ConstantValue left, ConstantValue right) { |
| 338 if (left.isString && right.isInt) { |
| 339 StringConstantValue stringConstant = left; |
| 340 IntConstantValue indexConstant = right; |
| 341 DartString dartString = stringConstant.primitiveValue; |
| 342 int index = indexConstant.primitiveValue; |
| 343 if (index < 0 || index >= dartString.length) return null; |
| 344 String string = dartString.slowToString(); |
| 345 int value = string.codeUnitAt(index); |
| 346 return DART_CONSTANT_SYSTEM.createInt(value); |
| 347 } |
| 348 return null; |
| 349 } |
| 350 } |
| 351 |
321 /** | 352 /** |
322 * A constant system implementing the Dart semantics. This system relies on | 353 * A constant system implementing the Dart semantics. This system relies on |
323 * the underlying runtime-system. That is, if dart2js is run in an environment | 354 * the underlying runtime-system. That is, if dart2js is run in an environment |
324 * that doesn't correctly implement Dart's semantics this constant system will | 355 * that doesn't correctly implement Dart's semantics this constant system will |
325 * not return the correct values. | 356 * not return the correct values. |
326 */ | 357 */ |
327 class DartConstantSystem extends ConstantSystem { | 358 class DartConstantSystem extends ConstantSystem { |
328 final add = const AddOperation(); | 359 final add = const AddOperation(); |
329 final bitAnd = const BitAndOperation(); | 360 final bitAnd = const BitAndOperation(); |
330 final bitNot = const BitNotOperation(); | 361 final bitNot = const BitNotOperation(); |
331 final bitOr = const BitOrOperation(); | 362 final bitOr = const BitOrOperation(); |
332 final bitXor = const BitXorOperation(); | 363 final bitXor = const BitXorOperation(); |
333 final booleanAnd = const BooleanAndOperation(); | 364 final booleanAnd = const BooleanAndOperation(); |
334 final booleanOr = const BooleanOrOperation(); | 365 final booleanOr = const BooleanOrOperation(); |
335 final divide = const DivideOperation(); | 366 final divide = const DivideOperation(); |
336 final equal = const EqualsOperation(); | 367 final equal = const EqualsOperation(); |
337 final greaterEqual = const GreaterEqualOperation(); | 368 final greaterEqual = const GreaterEqualOperation(); |
338 final greater = const GreaterOperation(); | 369 final greater = const GreaterOperation(); |
339 final identity = const IdentityOperation(); | 370 final identity = const IdentityOperation(); |
340 final lessEqual = const LessEqualOperation(); | 371 final lessEqual = const LessEqualOperation(); |
341 final less = const LessOperation(); | 372 final less = const LessOperation(); |
342 final modulo = const ModuloOperation(); | 373 final modulo = const ModuloOperation(); |
343 final multiply = const MultiplyOperation(); | 374 final multiply = const MultiplyOperation(); |
344 final negate = const NegateOperation(); | 375 final negate = const NegateOperation(); |
345 final not = const NotOperation(); | 376 final not = const NotOperation(); |
346 final shiftLeft = const ShiftLeftOperation(); | 377 final shiftLeft = const ShiftLeftOperation(); |
347 final shiftRight = const ShiftRightOperation(); | 378 final shiftRight = const ShiftRightOperation(); |
348 final subtract = const SubtractOperation(); | 379 final subtract = const SubtractOperation(); |
349 final truncatingDivide = const TruncatingDivideOperation(); | 380 final truncatingDivide = const TruncatingDivideOperation(); |
| 381 final codeUnitAt = const CodeUnitAtConstantOperation(); |
350 | 382 |
351 const DartConstantSystem(); | 383 const DartConstantSystem(); |
352 | 384 |
353 IntConstantValue createInt(int i) => new IntConstantValue(i); | 385 IntConstantValue createInt(int i) => new IntConstantValue(i); |
354 DoubleConstantValue createDouble(double d) => new DoubleConstantValue(d); | 386 DoubleConstantValue createDouble(double d) => new DoubleConstantValue(d); |
355 StringConstantValue createString(DartString string) { | 387 StringConstantValue createString(DartString string) { |
356 return new StringConstantValue(string); | 388 return new StringConstantValue(string); |
357 } | 389 } |
358 BoolConstantValue createBool(bool value) => new BoolConstantValue(value); | 390 BoolConstantValue createBool(bool value) => new BoolConstantValue(value); |
359 NullConstantValue createNull() => new NullConstantValue(); | 391 NullConstantValue createNull() => new NullConstantValue(); |
360 MapConstantValue createMap(Compiler compiler, | 392 MapConstantValue createMap(Compiler compiler, |
361 InterfaceType type, | 393 InterfaceType type, |
362 List<ConstantValue> keys, | 394 List<ConstantValue> keys, |
363 List<ConstantValue> values) { | 395 List<ConstantValue> values) { |
364 return new MapConstantValue(type, keys, values); | 396 return new MapConstantValue(type, keys, values); |
365 } | 397 } |
366 | 398 |
367 bool isInt(ConstantValue constant) => constant.isInt; | 399 bool isInt(ConstantValue constant) => constant.isInt; |
368 bool isDouble(ConstantValue constant) => constant.isDouble; | 400 bool isDouble(ConstantValue constant) => constant.isDouble; |
369 bool isString(ConstantValue constant) => constant.isString; | 401 bool isString(ConstantValue constant) => constant.isString; |
370 bool isBool(ConstantValue constant) => constant.isBool; | 402 bool isBool(ConstantValue constant) => constant.isBool; |
371 bool isNull(ConstantValue constant) => constant.isNull; | 403 bool isNull(ConstantValue constant) => constant.isNull; |
372 | 404 |
373 bool isSubtype(Compiler compiler, DartType s, DartType t) { | 405 bool isSubtype(Compiler compiler, DartType s, DartType t) { |
374 return compiler.types.isSubtype(s, t); | 406 return compiler.types.isSubtype(s, t); |
375 } | 407 } |
376 } | 408 } |
OLD | NEW |