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