| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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.constants.expressions; | 5 library dart2js.constants.expressions; |
| 6 | 6 |
| 7 import '../constants/constant_system.dart'; | 7 import '../constants/constant_system.dart'; |
| 8 import '../core_types.dart'; |
| 8 import '../dart2jslib.dart' show assertDebugMode, Compiler; | 9 import '../dart2jslib.dart' show assertDebugMode, Compiler; |
| 9 import '../dart_types.dart'; | 10 import '../dart_types.dart'; |
| 10 import '../elements/elements.dart' show | 11 import '../elements/elements.dart' show |
| 11 ConstructorElement, | 12 ConstructorElement, |
| 12 Element, | 13 Element, |
| 13 FieldElement, | 14 FieldElement, |
| 14 FunctionElement, | 15 FunctionElement, |
| 15 PrefixElement, | 16 PrefixElement, |
| 16 VariableElement; | 17 VariableElement; |
| 17 import '../resolution/operators.dart'; | 18 import '../resolution/operators.dart'; |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 accept(ConstantExpressionVisitor visitor, [context]); | 269 accept(ConstantExpressionVisitor visitor, [context]); |
| 269 | 270 |
| 270 /// Substitute free variables using arguments. | 271 /// Substitute free variables using arguments. |
| 271 ConstantExpression apply(NormalizedArguments arguments) => this; | 272 ConstantExpression apply(NormalizedArguments arguments) => this; |
| 272 | 273 |
| 273 /// Compute the [ConstantValue] for this expression using the [environment] | 274 /// Compute the [ConstantValue] for this expression using the [environment] |
| 274 /// and the [constantSystem]. | 275 /// and the [constantSystem]. |
| 275 ConstantValue evaluate(Environment environment, | 276 ConstantValue evaluate(Environment environment, |
| 276 ConstantSystem constantSystem); | 277 ConstantSystem constantSystem); |
| 277 | 278 |
| 279 /// Returns the type of this constant expression, if it is independent of the |
| 280 /// environment values. |
| 281 DartType getKnownType(CoreTypes coreTypes) => null; |
| 282 |
| 278 String getText() { | 283 String getText() { |
| 279 ConstExpPrinter printer = new ConstExpPrinter(); | 284 ConstExpPrinter printer = new ConstExpPrinter(); |
| 280 accept(printer); | 285 accept(printer); |
| 281 return printer.toString(); | 286 return printer.toString(); |
| 282 } | 287 } |
| 283 | 288 |
| 284 int _computeHashCode(); | 289 int _computeHashCode(); |
| 285 | 290 |
| 286 int get hashCode { | 291 int get hashCode { |
| 287 if (_hashCode == null) { | 292 if (_hashCode == null) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 return constantSystem.createBool(primitiveValue); | 358 return constantSystem.createBool(primitiveValue); |
| 354 } | 359 } |
| 355 | 360 |
| 356 @override | 361 @override |
| 357 int _computeHashCode() => 13 * primitiveValue.hashCode; | 362 int _computeHashCode() => 13 * primitiveValue.hashCode; |
| 358 | 363 |
| 359 @override | 364 @override |
| 360 bool _equals(BoolConstantExpression other) { | 365 bool _equals(BoolConstantExpression other) { |
| 361 return primitiveValue == other.primitiveValue; | 366 return primitiveValue == other.primitiveValue; |
| 362 } | 367 } |
| 368 |
| 369 @override |
| 370 DartType getKnownType(CoreTypes coreTypes) => coreTypes.boolType; |
| 363 } | 371 } |
| 364 | 372 |
| 365 /// Integer literal constant. | 373 /// Integer literal constant. |
| 366 class IntConstantExpression extends PrimitiveConstantExpression { | 374 class IntConstantExpression extends PrimitiveConstantExpression { |
| 367 final int primitiveValue; | 375 final int primitiveValue; |
| 368 | 376 |
| 369 IntConstantExpression(this.primitiveValue); | 377 IntConstantExpression(this.primitiveValue); |
| 370 | 378 |
| 371 ConstantExpressionKind get kind => ConstantExpressionKind.INT; | 379 ConstantExpressionKind get kind => ConstantExpressionKind.INT; |
| 372 | 380 |
| 373 accept(ConstantExpressionVisitor visitor, [context]) { | 381 accept(ConstantExpressionVisitor visitor, [context]) { |
| 374 return visitor.visitInt(this, context); | 382 return visitor.visitInt(this, context); |
| 375 } | 383 } |
| 376 | 384 |
| 377 @override | 385 @override |
| 378 ConstantValue evaluate(Environment environment, | 386 ConstantValue evaluate(Environment environment, |
| 379 ConstantSystem constantSystem) { | 387 ConstantSystem constantSystem) { |
| 380 return constantSystem.createInt(primitiveValue); | 388 return constantSystem.createInt(primitiveValue); |
| 381 } | 389 } |
| 382 | 390 |
| 383 @override | 391 @override |
| 384 int _computeHashCode() => 17 * primitiveValue.hashCode; | 392 int _computeHashCode() => 17 * primitiveValue.hashCode; |
| 385 | 393 |
| 386 @override | 394 @override |
| 387 bool _equals(IntConstantExpression other) { | 395 bool _equals(IntConstantExpression other) { |
| 388 return primitiveValue == other.primitiveValue; | 396 return primitiveValue == other.primitiveValue; |
| 389 } | 397 } |
| 398 |
| 399 @override |
| 400 DartType getKnownType(CoreTypes coreTypes) => coreTypes.intType; |
| 390 } | 401 } |
| 391 | 402 |
| 392 /// Double literal constant. | 403 /// Double literal constant. |
| 393 class DoubleConstantExpression extends PrimitiveConstantExpression { | 404 class DoubleConstantExpression extends PrimitiveConstantExpression { |
| 394 final double primitiveValue; | 405 final double primitiveValue; |
| 395 | 406 |
| 396 DoubleConstantExpression(this.primitiveValue); | 407 DoubleConstantExpression(this.primitiveValue); |
| 397 | 408 |
| 398 ConstantExpressionKind get kind => ConstantExpressionKind.DOUBLE; | 409 ConstantExpressionKind get kind => ConstantExpressionKind.DOUBLE; |
| 399 | 410 |
| 400 accept(ConstantExpressionVisitor visitor, [context]) { | 411 accept(ConstantExpressionVisitor visitor, [context]) { |
| 401 return visitor.visitDouble(this, context); | 412 return visitor.visitDouble(this, context); |
| 402 } | 413 } |
| 403 | 414 |
| 404 @override | 415 @override |
| 405 ConstantValue evaluate(Environment environment, | 416 ConstantValue evaluate(Environment environment, |
| 406 ConstantSystem constantSystem) { | 417 ConstantSystem constantSystem) { |
| 407 return constantSystem.createDouble(primitiveValue); | 418 return constantSystem.createDouble(primitiveValue); |
| 408 } | 419 } |
| 409 | 420 |
| 410 @override | 421 @override |
| 411 int _computeHashCode() => 19 * primitiveValue.hashCode; | 422 int _computeHashCode() => 19 * primitiveValue.hashCode; |
| 412 | 423 |
| 413 @override | 424 @override |
| 414 bool _equals(DoubleConstantExpression other) { | 425 bool _equals(DoubleConstantExpression other) { |
| 415 return primitiveValue == other.primitiveValue; | 426 return primitiveValue == other.primitiveValue; |
| 416 } | 427 } |
| 428 |
| 429 @override |
| 430 DartType getKnownType(CoreTypes coreTypes) => coreTypes.doubleType; |
| 417 } | 431 } |
| 418 | 432 |
| 419 /// String literal constant. | 433 /// String literal constant. |
| 420 class StringConstantExpression extends PrimitiveConstantExpression { | 434 class StringConstantExpression extends PrimitiveConstantExpression { |
| 421 final String primitiveValue; | 435 final String primitiveValue; |
| 422 | 436 |
| 423 StringConstantExpression(this.primitiveValue); | 437 StringConstantExpression(this.primitiveValue); |
| 424 | 438 |
| 425 ConstantExpressionKind get kind => ConstantExpressionKind.STRING; | 439 ConstantExpressionKind get kind => ConstantExpressionKind.STRING; |
| 426 | 440 |
| 427 accept(ConstantExpressionVisitor visitor, [context]) { | 441 accept(ConstantExpressionVisitor visitor, [context]) { |
| 428 return visitor.visitString(this, context); | 442 return visitor.visitString(this, context); |
| 429 } | 443 } |
| 430 | 444 |
| 431 @override | 445 @override |
| 432 ConstantValue evaluate(Environment environment, | 446 ConstantValue evaluate(Environment environment, |
| 433 ConstantSystem constantSystem) { | 447 ConstantSystem constantSystem) { |
| 434 return constantSystem.createString(new DartString.literal(primitiveValue)); | 448 return constantSystem.createString(new DartString.literal(primitiveValue)); |
| 435 } | 449 } |
| 436 | 450 |
| 437 @override | 451 @override |
| 438 int _computeHashCode() => 23 * primitiveValue.hashCode; | 452 int _computeHashCode() => 23 * primitiveValue.hashCode; |
| 439 | 453 |
| 440 @override | 454 @override |
| 441 bool _equals(StringConstantExpression other) { | 455 bool _equals(StringConstantExpression other) { |
| 442 return primitiveValue == other.primitiveValue; | 456 return primitiveValue == other.primitiveValue; |
| 443 } | 457 } |
| 458 |
| 459 @override |
| 460 DartType getKnownType(CoreTypes coreTypes) => coreTypes.stringType; |
| 444 } | 461 } |
| 445 | 462 |
| 446 /// Null literal constant. | 463 /// Null literal constant. |
| 447 class NullConstantExpression extends PrimitiveConstantExpression { | 464 class NullConstantExpression extends PrimitiveConstantExpression { |
| 448 NullConstantExpression(); | 465 NullConstantExpression(); |
| 449 | 466 |
| 450 ConstantExpressionKind get kind => ConstantExpressionKind.NULL; | 467 ConstantExpressionKind get kind => ConstantExpressionKind.NULL; |
| 451 | 468 |
| 452 accept(ConstantExpressionVisitor visitor, [context]) { | 469 accept(ConstantExpressionVisitor visitor, [context]) { |
| 453 return visitor.visitNull(this, context); | 470 return visitor.visitNull(this, context); |
| 454 } | 471 } |
| 455 | 472 |
| 456 @override | 473 @override |
| 457 ConstantValue evaluate(Environment environment, | 474 ConstantValue evaluate(Environment environment, |
| 458 ConstantSystem constantSystem) { | 475 ConstantSystem constantSystem) { |
| 459 return constantSystem.createNull(); | 476 return constantSystem.createNull(); |
| 460 } | 477 } |
| 461 | 478 |
| 462 get primitiveValue => null; | 479 get primitiveValue => null; |
| 463 | 480 |
| 464 @override | 481 @override |
| 465 int _computeHashCode() => 29; | 482 int _computeHashCode() => 29; |
| 466 | 483 |
| 467 @override | 484 @override |
| 468 bool _equals(NullConstantExpression other) => true; | 485 bool _equals(NullConstantExpression other) => true; |
| 486 |
| 487 @override |
| 488 DartType getKnownType(CoreTypes coreTypes) => coreTypes.nullType; |
| 469 } | 489 } |
| 470 | 490 |
| 471 /// Literal list constant. | 491 /// Literal list constant. |
| 472 class ListConstantExpression extends ConstantExpression { | 492 class ListConstantExpression extends ConstantExpression { |
| 473 final InterfaceType type; | 493 final InterfaceType type; |
| 474 final List<ConstantExpression> values; | 494 final List<ConstantExpression> values; |
| 475 | 495 |
| 476 ListConstantExpression(this.type, this.values); | 496 ListConstantExpression(this.type, this.values); |
| 477 | 497 |
| 478 ConstantExpressionKind get kind => ConstantExpressionKind.LIST; | 498 ConstantExpressionKind get kind => ConstantExpressionKind.LIST; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 504 | 524 |
| 505 @override | 525 @override |
| 506 bool _equals(ListConstantExpression other) { | 526 bool _equals(ListConstantExpression other) { |
| 507 if (type != other.type) return false; | 527 if (type != other.type) return false; |
| 508 if (values.length != other.values.length) return false; | 528 if (values.length != other.values.length) return false; |
| 509 for (int i = 0; i < values.length; i++) { | 529 for (int i = 0; i < values.length; i++) { |
| 510 if (values[i] != other.values[i]) return false; | 530 if (values[i] != other.values[i]) return false; |
| 511 } | 531 } |
| 512 return true; | 532 return true; |
| 513 } | 533 } |
| 534 |
| 535 @override |
| 536 DartType getKnownType(CoreTypes coreTypes) => type; |
| 514 } | 537 } |
| 515 | 538 |
| 516 /// Literal map constant. | 539 /// Literal map constant. |
| 517 class MapConstantExpression extends ConstantExpression { | 540 class MapConstantExpression extends ConstantExpression { |
| 518 final InterfaceType type; | 541 final InterfaceType type; |
| 519 final List<ConstantExpression> keys; | 542 final List<ConstantExpression> keys; |
| 520 final List<ConstantExpression> values; | 543 final List<ConstantExpression> values; |
| 521 | 544 |
| 522 MapConstantExpression(this.type, this.keys, this.values); | 545 MapConstantExpression(this.type, this.keys, this.values); |
| 523 | 546 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 @override | 578 @override |
| 556 bool _equals(MapConstantExpression other) { | 579 bool _equals(MapConstantExpression other) { |
| 557 if (type != other.type) return false; | 580 if (type != other.type) return false; |
| 558 if (values.length != other.values.length) return false; | 581 if (values.length != other.values.length) return false; |
| 559 for (int i = 0; i < values.length; i++) { | 582 for (int i = 0; i < values.length; i++) { |
| 560 if (keys[i] != other.keys[i]) return false; | 583 if (keys[i] != other.keys[i]) return false; |
| 561 if (values[i] != other.values[i]) return false; | 584 if (values[i] != other.values[i]) return false; |
| 562 } | 585 } |
| 563 return true; | 586 return true; |
| 564 } | 587 } |
| 588 |
| 589 @override |
| 590 DartType getKnownType(CoreTypes coreTypes) => type; |
| 565 } | 591 } |
| 566 | 592 |
| 567 /// Invocation of a const constructor. | 593 /// Invocation of a const constructor. |
| 568 class ConstructedConstantExpression extends ConstantExpression { | 594 class ConstructedConstantExpression extends ConstantExpression { |
| 569 final InterfaceType type; | 595 final InterfaceType type; |
| 570 final ConstructorElement target; | 596 final ConstructorElement target; |
| 571 final CallStructure callStructure; | 597 final CallStructure callStructure; |
| 572 final List<ConstantExpression> arguments; | 598 final List<ConstantExpression> arguments; |
| 573 | 599 |
| 574 ConstructedConstantExpression( | 600 ConstructedConstantExpression( |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 } | 718 } |
| 693 | 719 |
| 694 @override | 720 @override |
| 695 bool _equals(ConcatenateConstantExpression other) { | 721 bool _equals(ConcatenateConstantExpression other) { |
| 696 if (expressions.length != other.expressions.length) return false; | 722 if (expressions.length != other.expressions.length) return false; |
| 697 for (int i = 0; i < expressions.length; i++) { | 723 for (int i = 0; i < expressions.length; i++) { |
| 698 if (expressions[i] != other.expressions[i]) return false; | 724 if (expressions[i] != other.expressions[i]) return false; |
| 699 } | 725 } |
| 700 return true; | 726 return true; |
| 701 } | 727 } |
| 728 |
| 729 @override |
| 730 DartType getKnownType(CoreTypes coreTypes) => coreTypes.stringType; |
| 702 } | 731 } |
| 703 | 732 |
| 704 /// Symbol literal. | 733 /// Symbol literal. |
| 705 class SymbolConstantExpression extends ConstantExpression { | 734 class SymbolConstantExpression extends ConstantExpression { |
| 706 final String name; | 735 final String name; |
| 707 | 736 |
| 708 SymbolConstantExpression(this.name); | 737 SymbolConstantExpression(this.name); |
| 709 | 738 |
| 710 ConstantExpressionKind get kind => ConstantExpressionKind.SYMBOL; | 739 ConstantExpressionKind get kind => ConstantExpressionKind.SYMBOL; |
| 711 | 740 |
| 712 accept(ConstantExpressionVisitor visitor, [context]) { | 741 accept(ConstantExpressionVisitor visitor, [context]) { |
| 713 return visitor.visitSymbol(this, context); | 742 return visitor.visitSymbol(this, context); |
| 714 } | 743 } |
| 715 | 744 |
| 716 @override | 745 @override |
| 717 int _computeHashCode() => 13 * name.hashCode; | 746 int _computeHashCode() => 13 * name.hashCode; |
| 718 | 747 |
| 719 @override | 748 @override |
| 720 bool _equals(SymbolConstantExpression other) { | 749 bool _equals(SymbolConstantExpression other) { |
| 721 return name == other.name; | 750 return name == other.name; |
| 722 } | 751 } |
| 723 | 752 |
| 724 @override | 753 @override |
| 725 ConstantValue evaluate(Environment environment, | 754 ConstantValue evaluate(Environment environment, |
| 726 ConstantSystem constantSystem) { | 755 ConstantSystem constantSystem) { |
| 727 // TODO(johnniwinther): Implement this. | 756 // TODO(johnniwinther): Implement this. |
| 728 throw new UnsupportedError('SymbolConstantExpression.evaluate'); | 757 throw new UnsupportedError('SymbolConstantExpression.evaluate'); |
| 729 } | 758 } |
| 759 |
| 760 @override |
| 761 DartType getKnownType(CoreTypes coreTypes) => coreTypes.symbolType; |
| 730 } | 762 } |
| 731 | 763 |
| 732 /// Type literal. | 764 /// Type literal. |
| 733 class TypeConstantExpression extends ConstantExpression { | 765 class TypeConstantExpression extends ConstantExpression { |
| 734 /// Either [DynamicType] or a raw [GenericType]. | 766 /// Either [DynamicType] or a raw [GenericType]. |
| 735 final DartType type; | 767 final DartType type; |
| 736 | 768 |
| 737 TypeConstantExpression(this.type) { | 769 TypeConstantExpression(this.type) { |
| 738 assert(type is GenericType || type is DynamicType); | 770 assert(type is GenericType || type is DynamicType); |
| 739 } | 771 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 750 return constantSystem.createType(environment.compiler, type); | 782 return constantSystem.createType(environment.compiler, type); |
| 751 } | 783 } |
| 752 | 784 |
| 753 @override | 785 @override |
| 754 int _computeHashCode() => 13 * type.hashCode; | 786 int _computeHashCode() => 13 * type.hashCode; |
| 755 | 787 |
| 756 @override | 788 @override |
| 757 bool _equals(TypeConstantExpression other) { | 789 bool _equals(TypeConstantExpression other) { |
| 758 return type == other.type; | 790 return type == other.type; |
| 759 } | 791 } |
| 792 |
| 793 @override |
| 794 DartType getKnownType(CoreTypes coreTypes) => coreTypes.typeType; |
| 760 } | 795 } |
| 761 | 796 |
| 762 /// Reference to a constant local, top-level, or static variable. | 797 /// Reference to a constant local, top-level, or static variable. |
| 763 class VariableConstantExpression extends ConstantExpression { | 798 class VariableConstantExpression extends ConstantExpression { |
| 764 final VariableElement element; | 799 final VariableElement element; |
| 765 | 800 |
| 766 VariableConstantExpression(this.element); | 801 VariableConstantExpression(this.element); |
| 767 | 802 |
| 768 ConstantExpressionKind get kind => ConstantExpressionKind.VARIABLE; | 803 ConstantExpressionKind get kind => ConstantExpressionKind.VARIABLE; |
| 769 | 804 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 804 return new FunctionConstantValue(element); | 839 return new FunctionConstantValue(element); |
| 805 } | 840 } |
| 806 | 841 |
| 807 @override | 842 @override |
| 808 int _computeHashCode() => 13 * element.hashCode; | 843 int _computeHashCode() => 13 * element.hashCode; |
| 809 | 844 |
| 810 @override | 845 @override |
| 811 bool _equals(FunctionConstantExpression other) { | 846 bool _equals(FunctionConstantExpression other) { |
| 812 return element == other.element; | 847 return element == other.element; |
| 813 } | 848 } |
| 849 |
| 850 @override |
| 851 DartType getKnownType(CoreTypes coreTypes) => coreTypes.functionType; |
| 814 } | 852 } |
| 815 | 853 |
| 816 /// A constant binary expression like `a * b`. | 854 /// A constant binary expression like `a * b`. |
| 817 class BinaryConstantExpression extends ConstantExpression { | 855 class BinaryConstantExpression extends ConstantExpression { |
| 818 final ConstantExpression left; | 856 final ConstantExpression left; |
| 819 final BinaryOperator operator; | 857 final BinaryOperator operator; |
| 820 final ConstantExpression right; | 858 final ConstantExpression right; |
| 821 | 859 |
| 822 BinaryConstantExpression(this.left, this.operator, this.right) { | 860 BinaryConstantExpression(this.left, this.operator, this.right) { |
| 823 assert(PRECEDENCE_MAP[operator.kind] != null); | 861 assert(PRECEDENCE_MAP[operator.kind] != null); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 837 right.evaluate(environment, constantSystem)); | 875 right.evaluate(environment, constantSystem)); |
| 838 } | 876 } |
| 839 | 877 |
| 840 ConstantExpression apply(NormalizedArguments arguments) { | 878 ConstantExpression apply(NormalizedArguments arguments) { |
| 841 return new BinaryConstantExpression( | 879 return new BinaryConstantExpression( |
| 842 left.apply(arguments), | 880 left.apply(arguments), |
| 843 operator, | 881 operator, |
| 844 right.apply(arguments)); | 882 right.apply(arguments)); |
| 845 } | 883 } |
| 846 | 884 |
| 885 DartType getKnownType(CoreTypes coreTypes) { |
| 886 DartType knownLeftType = left.getKnownType(coreTypes); |
| 887 DartType knownRightType = right.getKnownType(coreTypes); |
| 888 switch (operator.kind) { |
| 889 case BinaryOperatorKind.EQ: |
| 890 case BinaryOperatorKind.NOT_EQ: |
| 891 case BinaryOperatorKind.LOGICAL_AND: |
| 892 case BinaryOperatorKind.LOGICAL_OR: |
| 893 case BinaryOperatorKind.GT: |
| 894 case BinaryOperatorKind.LT: |
| 895 case BinaryOperatorKind.GTEQ: |
| 896 case BinaryOperatorKind.LTEQ: |
| 897 return coreTypes.boolType; |
| 898 case BinaryOperatorKind.ADD: |
| 899 if (knownLeftType == coreTypes.stringType) { |
| 900 assert(knownRightType == coreTypes.stringType); |
| 901 return coreTypes.stringType; |
| 902 } else if (knownLeftType == coreTypes.intType && |
| 903 knownRightType == coreTypes.intType) { |
| 904 return coreTypes.intType; |
| 905 } |
| 906 assert(knownLeftType == coreTypes.doubleType || |
| 907 knownRightType == coreTypes.doubleType); |
| 908 return coreTypes.doubleType; |
| 909 case BinaryOperatorKind.SUB: |
| 910 case BinaryOperatorKind.MUL: |
| 911 case BinaryOperatorKind.MOD: |
| 912 if (knownLeftType == coreTypes.intType && |
| 913 knownRightType == coreTypes.intType) { |
| 914 return coreTypes.intType; |
| 915 } |
| 916 assert(knownLeftType == coreTypes.doubleType || |
| 917 knownRightType == coreTypes.doubleType); |
| 918 return coreTypes.doubleType; |
| 919 case BinaryOperatorKind.DIV: |
| 920 return coreTypes.doubleType; |
| 921 case BinaryOperatorKind.IDIV: |
| 922 return coreTypes.intType; |
| 923 case BinaryOperatorKind.AND: |
| 924 case BinaryOperatorKind.OR: |
| 925 case BinaryOperatorKind.XOR: |
| 926 case BinaryOperatorKind.SHR: |
| 927 case BinaryOperatorKind.SHL: |
| 928 return coreTypes.intType; |
| 929 case BinaryOperatorKind.IF_NULL: |
| 930 case BinaryOperatorKind.INDEX: |
| 931 throw new UnsupportedError( |
| 932 'Unexpected constant binary operator: $operator'); |
| 933 } |
| 934 } |
| 935 |
| 936 |
| 847 int get precedence => PRECEDENCE_MAP[operator.kind]; | 937 int get precedence => PRECEDENCE_MAP[operator.kind]; |
| 848 | 938 |
| 849 @override | 939 @override |
| 850 int _computeHashCode() { | 940 int _computeHashCode() { |
| 851 return 13 * operator.hashCode + | 941 return 13 * operator.hashCode + |
| 852 17 * left.hashCode + | 942 17 * left.hashCode + |
| 853 19 * right.hashCode; | 943 19 * right.hashCode; |
| 854 } | 944 } |
| 855 | 945 |
| 856 @override | 946 @override |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 916 int _computeHashCode() { | 1006 int _computeHashCode() { |
| 917 return 17 * left.hashCode + | 1007 return 17 * left.hashCode + |
| 918 19 * right.hashCode; | 1008 19 * right.hashCode; |
| 919 } | 1009 } |
| 920 | 1010 |
| 921 @override | 1011 @override |
| 922 bool _equals(IdenticalConstantExpression other) { | 1012 bool _equals(IdenticalConstantExpression other) { |
| 923 return left == other.left && | 1013 return left == other.left && |
| 924 right == other.right; | 1014 right == other.right; |
| 925 } | 1015 } |
| 1016 |
| 1017 @override |
| 1018 DartType getKnownType(CoreTypes coreTypes) => coreTypes.boolType; |
| 926 } | 1019 } |
| 927 | 1020 |
| 928 /// A unary constant expression like `-a`. | 1021 /// A unary constant expression like `-a`. |
| 929 class UnaryConstantExpression extends ConstantExpression { | 1022 class UnaryConstantExpression extends ConstantExpression { |
| 930 final UnaryOperator operator; | 1023 final UnaryOperator operator; |
| 931 final ConstantExpression expression; | 1024 final ConstantExpression expression; |
| 932 | 1025 |
| 933 UnaryConstantExpression(this.operator, this.expression) { | 1026 UnaryConstantExpression(this.operator, this.expression) { |
| 934 assert(PRECEDENCE_MAP[operator.kind] != null); | 1027 assert(PRECEDENCE_MAP[operator.kind] != null); |
| 935 } | 1028 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 960 return 13 * operator.hashCode + | 1053 return 13 * operator.hashCode + |
| 961 17 * expression.hashCode; | 1054 17 * expression.hashCode; |
| 962 } | 1055 } |
| 963 | 1056 |
| 964 @override | 1057 @override |
| 965 bool _equals(UnaryConstantExpression other) { | 1058 bool _equals(UnaryConstantExpression other) { |
| 966 return operator == other.operator && | 1059 return operator == other.operator && |
| 967 expression == other.expression; | 1060 expression == other.expression; |
| 968 } | 1061 } |
| 969 | 1062 |
| 1063 @override |
| 1064 DartType getKnownType(CoreTypes coreTypes) { |
| 1065 return expression.getKnownType(coreTypes); |
| 1066 } |
| 1067 |
| 970 static const Map<UnaryOperatorKind, int> PRECEDENCE_MAP = const { | 1068 static const Map<UnaryOperatorKind, int> PRECEDENCE_MAP = const { |
| 971 UnaryOperatorKind.NOT: 14, | 1069 UnaryOperatorKind.NOT: 14, |
| 972 UnaryOperatorKind.COMPLEMENT: 14, | 1070 UnaryOperatorKind.COMPLEMENT: 14, |
| 973 UnaryOperatorKind.NEGATE: 14, | 1071 UnaryOperatorKind.NEGATE: 14, |
| 974 }; | 1072 }; |
| 975 } | 1073 } |
| 976 | 1074 |
| 977 | 1075 |
| 978 /// A string length constant expression like `a.length`. | 1076 /// A string length constant expression like `a.length`. |
| 979 class StringLengthConstantExpression extends ConstantExpression { | 1077 class StringLengthConstantExpression extends ConstantExpression { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1006 | 1104 |
| 1007 @override | 1105 @override |
| 1008 int _computeHashCode() { | 1106 int _computeHashCode() { |
| 1009 return 23 * expression.hashCode; | 1107 return 23 * expression.hashCode; |
| 1010 } | 1108 } |
| 1011 | 1109 |
| 1012 @override | 1110 @override |
| 1013 bool _equals(StringLengthConstantExpression other) { | 1111 bool _equals(StringLengthConstantExpression other) { |
| 1014 return expression == other.expression; | 1112 return expression == other.expression; |
| 1015 } | 1113 } |
| 1114 |
| 1115 @override |
| 1116 DartType getKnownType(CoreTypes coreTypes) => coreTypes.intType; |
| 1016 } | 1117 } |
| 1017 | 1118 |
| 1018 /// A constant conditional expression like `a ? b : c`. | 1119 /// A constant conditional expression like `a ? b : c`. |
| 1019 class ConditionalConstantExpression extends ConstantExpression { | 1120 class ConditionalConstantExpression extends ConstantExpression { |
| 1020 final ConstantExpression condition; | 1121 final ConstantExpression condition; |
| 1021 final ConstantExpression trueExp; | 1122 final ConstantExpression trueExp; |
| 1022 final ConstantExpression falseExp; | 1123 final ConstantExpression falseExp; |
| 1023 | 1124 |
| 1024 ConditionalConstantExpression(this.condition, | 1125 ConditionalConstantExpression(this.condition, |
| 1025 this.trueExp, | 1126 this.trueExp, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1064 ConstantValue falseValue = | 1165 ConstantValue falseValue = |
| 1065 falseExp.evaluate(environment, constantSystem); | 1166 falseExp.evaluate(environment, constantSystem); |
| 1066 if (conditionValue.isTrue) { | 1167 if (conditionValue.isTrue) { |
| 1067 return trueValue; | 1168 return trueValue; |
| 1068 } else if (conditionValue.isFalse) { | 1169 } else if (conditionValue.isFalse) { |
| 1069 return falseValue; | 1170 return falseValue; |
| 1070 } else { | 1171 } else { |
| 1071 return new NonConstantValue(); | 1172 return new NonConstantValue(); |
| 1072 } | 1173 } |
| 1073 } | 1174 } |
| 1175 |
| 1176 @override |
| 1177 DartType getKnownType(CoreTypes coreTypes) { |
| 1178 DartType trueType = trueExp.getKnownType(coreTypes); |
| 1179 DartType falseType = falseExp.getKnownType(coreTypes); |
| 1180 if (trueType == falseType) { |
| 1181 return trueType; |
| 1182 } |
| 1183 return null; |
| 1184 } |
| 1074 } | 1185 } |
| 1075 | 1186 |
| 1076 /// A reference to a position parameter. | 1187 /// A reference to a position parameter. |
| 1077 class PositionalArgumentReference extends ConstantExpression { | 1188 class PositionalArgumentReference extends ConstantExpression { |
| 1078 final int index; | 1189 final int index; |
| 1079 | 1190 |
| 1080 PositionalArgumentReference(this.index); | 1191 PositionalArgumentReference(this.index); |
| 1081 | 1192 |
| 1082 ConstantExpressionKind get kind { | 1193 ConstantExpressionKind get kind { |
| 1083 return ConstantExpressionKind.POSITIONAL_REFERENCE; | 1194 return ConstantExpressionKind.POSITIONAL_REFERENCE; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1196 } else { | 1307 } else { |
| 1197 return defaultConstantValue; | 1308 return defaultConstantValue; |
| 1198 } | 1309 } |
| 1199 } | 1310 } |
| 1200 | 1311 |
| 1201 ConstantExpression apply(NormalizedArguments arguments) { | 1312 ConstantExpression apply(NormalizedArguments arguments) { |
| 1202 return new BoolFromEnvironmentConstantExpression( | 1313 return new BoolFromEnvironmentConstantExpression( |
| 1203 name.apply(arguments), | 1314 name.apply(arguments), |
| 1204 defaultValue != null ? defaultValue.apply(arguments) : null); | 1315 defaultValue != null ? defaultValue.apply(arguments) : null); |
| 1205 } | 1316 } |
| 1317 |
| 1318 @override |
| 1319 DartType getKnownType(CoreTypes coreTypes) => coreTypes.boolType; |
| 1206 } | 1320 } |
| 1207 | 1321 |
| 1208 /// A `const int.fromEnvironment` constant. | 1322 /// A `const int.fromEnvironment` constant. |
| 1209 class IntFromEnvironmentConstantExpression | 1323 class IntFromEnvironmentConstantExpression |
| 1210 extends FromEnvironmentConstantExpression { | 1324 extends FromEnvironmentConstantExpression { |
| 1211 | 1325 |
| 1212 IntFromEnvironmentConstantExpression( | 1326 IntFromEnvironmentConstantExpression( |
| 1213 ConstantExpression name, | 1327 ConstantExpression name, |
| 1214 ConstantExpression defaultValue) | 1328 ConstantExpression defaultValue) |
| 1215 : super(name, defaultValue); | 1329 : super(name, defaultValue); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1249 } else { | 1363 } else { |
| 1250 return constantSystem.createInt(value); | 1364 return constantSystem.createInt(value); |
| 1251 } | 1365 } |
| 1252 } | 1366 } |
| 1253 | 1367 |
| 1254 ConstantExpression apply(NormalizedArguments arguments) { | 1368 ConstantExpression apply(NormalizedArguments arguments) { |
| 1255 return new IntFromEnvironmentConstantExpression( | 1369 return new IntFromEnvironmentConstantExpression( |
| 1256 name.apply(arguments), | 1370 name.apply(arguments), |
| 1257 defaultValue != null ? defaultValue.apply(arguments) : null); | 1371 defaultValue != null ? defaultValue.apply(arguments) : null); |
| 1258 } | 1372 } |
| 1373 |
| 1374 @override |
| 1375 DartType getKnownType(CoreTypes coreTypes) => coreTypes.intType; |
| 1259 } | 1376 } |
| 1260 | 1377 |
| 1261 /// A `const String.fromEnvironment` constant. | 1378 /// A `const String.fromEnvironment` constant. |
| 1262 class StringFromEnvironmentConstantExpression | 1379 class StringFromEnvironmentConstantExpression |
| 1263 extends FromEnvironmentConstantExpression { | 1380 extends FromEnvironmentConstantExpression { |
| 1264 | 1381 |
| 1265 StringFromEnvironmentConstantExpression( | 1382 StringFromEnvironmentConstantExpression( |
| 1266 ConstantExpression name, | 1383 ConstantExpression name, |
| 1267 ConstantExpression defaultValue) | 1384 ConstantExpression defaultValue) |
| 1268 : super(name, defaultValue); | 1385 : super(name, defaultValue); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1298 } else { | 1415 } else { |
| 1299 return constantSystem.createString(new DartString.literal(text)); | 1416 return constantSystem.createString(new DartString.literal(text)); |
| 1300 } | 1417 } |
| 1301 } | 1418 } |
| 1302 | 1419 |
| 1303 ConstantExpression apply(NormalizedArguments arguments) { | 1420 ConstantExpression apply(NormalizedArguments arguments) { |
| 1304 return new StringFromEnvironmentConstantExpression( | 1421 return new StringFromEnvironmentConstantExpression( |
| 1305 name.apply(arguments), | 1422 name.apply(arguments), |
| 1306 defaultValue != null ? defaultValue.apply(arguments) : null); | 1423 defaultValue != null ? defaultValue.apply(arguments) : null); |
| 1307 } | 1424 } |
| 1425 |
| 1426 @override |
| 1427 DartType getKnownType(CoreTypes coreTypes) => coreTypes.stringType; |
| 1308 } | 1428 } |
| 1309 | 1429 |
| 1310 /// A constant expression referenced with a deferred prefix. | 1430 /// A constant expression referenced with a deferred prefix. |
| 1311 /// For example `lib.C`. | 1431 /// For example `lib.C`. |
| 1312 class DeferredConstantExpression extends ConstantExpression { | 1432 class DeferredConstantExpression extends ConstantExpression { |
| 1313 final ConstantExpression expression; | 1433 final ConstantExpression expression; |
| 1314 final PrefixElement prefix; | 1434 final PrefixElement prefix; |
| 1315 | 1435 |
| 1316 DeferredConstantExpression(this.expression, this.prefix); | 1436 DeferredConstantExpression(this.expression, this.prefix); |
| 1317 | 1437 |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1654 visit(exp.name); | 1774 visit(exp.name); |
| 1655 if (exp.defaultValue != null) { | 1775 if (exp.defaultValue != null) { |
| 1656 sb.write(', defaultValue: '); | 1776 sb.write(', defaultValue: '); |
| 1657 visit(exp.defaultValue); | 1777 visit(exp.defaultValue); |
| 1658 } | 1778 } |
| 1659 sb.write(')'); | 1779 sb.write(')'); |
| 1660 } | 1780 } |
| 1661 | 1781 |
| 1662 String toString() => sb.toString(); | 1782 String toString() => sb.toString(); |
| 1663 } | 1783 } |
| OLD | NEW |