| 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 library dart2js.constant_system.dart; | 5 library dart2js.constant_system.dart; |
| 6 | 6 |
| 7 import 'compiler.dart' show Compiler; | 7 import 'compiler.dart' show Compiler; |
| 8 import 'constants/constant_system.dart'; | 8 import 'constants/constant_system.dart'; |
| 9 import 'constants/values.dart'; | 9 import 'constants/values.dart'; |
| 10 import 'dart_types.dart'; | 10 import 'dart_types.dart'; |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 final String name = '??'; | 341 final String name = '??'; |
| 342 const IfNullOperation(); | 342 const IfNullOperation(); |
| 343 ConstantValue fold(ConstantValue left, ConstantValue right) { | 343 ConstantValue fold(ConstantValue left, ConstantValue right) { |
| 344 if (left.isNull) return right; | 344 if (left.isNull) return right; |
| 345 return left; | 345 return left; |
| 346 } | 346 } |
| 347 | 347 |
| 348 apply(left, right) => left ?? right; | 348 apply(left, right) => left ?? right; |
| 349 } | 349 } |
| 350 | 350 |
| 351 class CodeUnitAtOperation implements BinaryOperation { | 351 abstract class CodeUnitAtOperation implements BinaryOperation { |
| 352 String get name => 'charCodeAt'; | 352 final String name = 'charCodeAt'; |
| 353 const CodeUnitAtOperation(); | 353 const CodeUnitAtOperation(); |
| 354 ConstantValue fold(ConstantValue left, ConstantValue right) => null; | |
| 355 apply(left, right) => left.codeUnitAt(right); | 354 apply(left, right) => left.codeUnitAt(right); |
| 356 } | 355 } |
| 357 | 356 |
| 357 class CodeUnitAtConstantOperation extends CodeUnitAtOperation { |
| 358 const CodeUnitAtConstantOperation(); |
| 359 ConstantValue fold(ConstantValue left, ConstantValue right) { |
| 360 // 'a'.codeUnitAt(0) is not a constant expression. |
| 361 return null; |
| 362 } |
| 363 } |
| 364 |
| 358 class CodeUnitAtRuntimeOperation extends CodeUnitAtOperation { | 365 class CodeUnitAtRuntimeOperation extends CodeUnitAtOperation { |
| 359 const CodeUnitAtRuntimeOperation(); | 366 const CodeUnitAtRuntimeOperation(); |
| 360 IntConstantValue fold(ConstantValue left, ConstantValue right) { | 367 IntConstantValue fold(ConstantValue left, ConstantValue right) { |
| 361 if (left.isString && right.isInt) { | 368 if (left.isString && right.isInt) { |
| 362 StringConstantValue stringConstant = left; | 369 StringConstantValue stringConstant = left; |
| 363 IntConstantValue indexConstant = right; | 370 IntConstantValue indexConstant = right; |
| 364 DartString dartString = stringConstant.primitiveValue; | 371 DartString dartString = stringConstant.primitiveValue; |
| 365 int index = indexConstant.primitiveValue; | 372 int index = indexConstant.primitiveValue; |
| 366 if (index < 0 || index >= dartString.length) return null; | 373 if (index < 0 || index >= dartString.length) return null; |
| 367 String string = dartString.slowToString(); | 374 String string = dartString.slowToString(); |
| 368 int value = string.codeUnitAt(index); | 375 int value = string.codeUnitAt(index); |
| 369 return DART_CONSTANT_SYSTEM.createInt(value); | 376 return DART_CONSTANT_SYSTEM.createInt(value); |
| 370 } | 377 } |
| 371 return null; | 378 return null; |
| 372 } | 379 } |
| 373 } | 380 } |
| 374 | 381 |
| 375 class UnfoldedUnaryOperation implements UnaryOperation { | |
| 376 final String name; | |
| 377 const UnfoldedUnaryOperation(this.name); | |
| 378 ConstantValue fold(ConstantValue constant) { | |
| 379 return null; | |
| 380 } | |
| 381 } | |
| 382 | |
| 383 /** | 382 /** |
| 384 * A constant system implementing the Dart semantics. This system relies on | 383 * A constant system implementing the Dart semantics. This system relies on |
| 385 * the underlying runtime-system. That is, if dart2js is run in an environment | 384 * the underlying runtime-system. That is, if dart2js is run in an environment |
| 386 * that doesn't correctly implement Dart's semantics this constant system will | 385 * that doesn't correctly implement Dart's semantics this constant system will |
| 387 * not return the correct values. | 386 * not return the correct values. |
| 388 */ | 387 */ |
| 389 class DartConstantSystem extends ConstantSystem { | 388 class DartConstantSystem extends ConstantSystem { |
| 390 final add = const AddOperation(); | 389 final add = const AddOperation(); |
| 391 final bitAnd = const BitAndOperation(); | 390 final bitAnd = const BitAndOperation(); |
| 392 final bitNot = const BitNotOperation(); | 391 final bitNot = const BitNotOperation(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 403 final lessEqual = const LessEqualOperation(); | 402 final lessEqual = const LessEqualOperation(); |
| 404 final less = const LessOperation(); | 403 final less = const LessOperation(); |
| 405 final modulo = const ModuloOperation(); | 404 final modulo = const ModuloOperation(); |
| 406 final multiply = const MultiplyOperation(); | 405 final multiply = const MultiplyOperation(); |
| 407 final negate = const NegateOperation(); | 406 final negate = const NegateOperation(); |
| 408 final not = const NotOperation(); | 407 final not = const NotOperation(); |
| 409 final shiftLeft = const ShiftLeftOperation(); | 408 final shiftLeft = const ShiftLeftOperation(); |
| 410 final shiftRight = const ShiftRightOperation(); | 409 final shiftRight = const ShiftRightOperation(); |
| 411 final subtract = const SubtractOperation(); | 410 final subtract = const SubtractOperation(); |
| 412 final truncatingDivide = const TruncatingDivideOperation(); | 411 final truncatingDivide = const TruncatingDivideOperation(); |
| 413 final codeUnitAt = const CodeUnitAtOperation(); | 412 final codeUnitAt = const CodeUnitAtConstantOperation(); |
| 414 final round = const UnfoldedUnaryOperation('round'); | |
| 415 | 413 |
| 416 const DartConstantSystem(); | 414 const DartConstantSystem(); |
| 417 | 415 |
| 418 @override | 416 @override |
| 419 IntConstantValue createInt(int i) => new IntConstantValue(i); | 417 IntConstantValue createInt(int i) => new IntConstantValue(i); |
| 420 | 418 |
| 421 @override | 419 @override |
| 422 DoubleConstantValue createDouble(double d) => new DoubleConstantValue(d); | 420 DoubleConstantValue createDouble(double d) => new DoubleConstantValue(d); |
| 423 | 421 |
| 424 @override | 422 @override |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 bool isInt(ConstantValue constant) => constant.isInt; | 460 bool isInt(ConstantValue constant) => constant.isInt; |
| 463 bool isDouble(ConstantValue constant) => constant.isDouble; | 461 bool isDouble(ConstantValue constant) => constant.isDouble; |
| 464 bool isString(ConstantValue constant) => constant.isString; | 462 bool isString(ConstantValue constant) => constant.isString; |
| 465 bool isBool(ConstantValue constant) => constant.isBool; | 463 bool isBool(ConstantValue constant) => constant.isBool; |
| 466 bool isNull(ConstantValue constant) => constant.isNull; | 464 bool isNull(ConstantValue constant) => constant.isNull; |
| 467 | 465 |
| 468 bool isSubtype(DartTypes types, DartType s, DartType t) { | 466 bool isSubtype(DartTypes types, DartType s, DartType t) { |
| 469 return types.isSubtype(s, t); | 467 return types.isSubtype(s, t); |
| 470 } | 468 } |
| 471 } | 469 } |
| OLD | NEW |