| 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.constants.values; | 5 library dart2js.constants.values; |
| 6 | 6 |
| 7 import '../common.dart'; | 7 import '../common.dart'; |
| 8 import '../core_types.dart'; | 8 import '../core_types.dart'; |
| 9 import '../dart_types.dart'; | 9 import '../dart_types.dart'; |
| 10 import '../elements/elements.dart' | 10 import '../elements/elements.dart' show Entity; |
| 11 show FieldElement, FunctionElement, PrefixElement; | 11 import '../elements/entities.dart'; |
| 12 import '../tree/dartstring.dart'; | 12 import '../tree/dartstring.dart'; |
| 13 import '../util/util.dart' show Hashing; | 13 import '../util/util.dart' show Hashing; |
| 14 | 14 |
| 15 enum ConstantValueKind { | 15 enum ConstantValueKind { |
| 16 FUNCTION, | 16 FUNCTION, |
| 17 NULL, | 17 NULL, |
| 18 INT, | 18 INT, |
| 19 DOUBLE, | 19 DOUBLE, |
| 20 BOOL, | 20 BOOL, |
| 21 STRING, | 21 STRING, |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 | 105 |
| 106 String toString() { | 106 String toString() { |
| 107 assertDebugMode("Use ConstantValue.toDartText() or " | 107 assertDebugMode("Use ConstantValue.toDartText() or " |
| 108 "ConstantValue.toStructuredText() " | 108 "ConstantValue.toStructuredText() " |
| 109 "instead of ConstantValue.toString()."); | 109 "instead of ConstantValue.toString()."); |
| 110 return toStructuredText(); | 110 return toStructuredText(); |
| 111 } | 111 } |
| 112 } | 112 } |
| 113 | 113 |
| 114 class FunctionConstantValue extends ConstantValue { | 114 class FunctionConstantValue extends ConstantValue { |
| 115 FunctionElement element; | 115 final FunctionEntity element; |
| 116 // TODO(johnniwinther): Should the type be derived from [element]. |
| 117 final FunctionType type; |
| 116 | 118 |
| 117 FunctionConstantValue(this.element) { | 119 FunctionConstantValue(this.element, this.type); |
| 118 assert(element.type != null); | |
| 119 } | |
| 120 | 120 |
| 121 bool get isFunction => true; | 121 bool get isFunction => true; |
| 122 | 122 |
| 123 bool operator ==(var other) { | 123 bool operator ==(var other) { |
| 124 if (other is! FunctionConstantValue) return false; | 124 if (other is! FunctionConstantValue) return false; |
| 125 return identical(other.element, element); | 125 return identical(other.element, element); |
| 126 } | 126 } |
| 127 | 127 |
| 128 List<ConstantValue> getDependencies() => const <ConstantValue>[]; | 128 List<ConstantValue> getDependencies() => const <ConstantValue>[]; |
| 129 | 129 |
| 130 DartString toDartString() { | 130 DartString toDartString() { |
| 131 return new DartString.literal(element.name); | 131 return new DartString.literal(element.name); |
| 132 } | 132 } |
| 133 | 133 |
| 134 DartType getType(CommonElements types) => element.type; | 134 DartType getType(CommonElements types) => type; |
| 135 | 135 |
| 136 int get hashCode => (17 * element.hashCode) & 0x7fffffff; | 136 int get hashCode => (17 * element.hashCode) & 0x7fffffff; |
| 137 | 137 |
| 138 accept(ConstantValueVisitor visitor, arg) => visitor.visitFunction(this, arg); | 138 accept(ConstantValueVisitor visitor, arg) => visitor.visitFunction(this, arg); |
| 139 | 139 |
| 140 ConstantValueKind get kind => ConstantValueKind.FUNCTION; | 140 ConstantValueKind get kind => ConstantValueKind.FUNCTION; |
| 141 | 141 |
| 142 String toDartText() { | 142 String toDartText() { |
| 143 if (element.isStatic) { | 143 if (element.enclosingClass != null) { |
| 144 return '${element.enclosingClass.name}.${element.name}'; | 144 return '${element.enclosingClass.name}.${element.name}'; |
| 145 } else { | 145 } else { |
| 146 return '${element.name}'; | 146 return '${element.name}'; |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 | 149 |
| 150 String toStructuredText() { | 150 String toStructuredText() { |
| 151 return 'FunctionConstant(${toDartText()})'; | 151 return 'FunctionConstant(${toDartText()})'; |
| 152 } | 152 } |
| 153 } | 153 } |
| (...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 ConstantValueKind get kind => ConstantValueKind.SYNTHETIC; | 694 ConstantValueKind get kind => ConstantValueKind.SYNTHETIC; |
| 695 | 695 |
| 696 String toDartText() => 'synthetic($valueKind, $payload)'; | 696 String toDartText() => 'synthetic($valueKind, $payload)'; |
| 697 | 697 |
| 698 String toStructuredText() => 'SyntheticConstant($valueKind, $payload)'; | 698 String toStructuredText() => 'SyntheticConstant($valueKind, $payload)'; |
| 699 } | 699 } |
| 700 | 700 |
| 701 class ConstructedConstantValue extends ObjectConstantValue { | 701 class ConstructedConstantValue extends ObjectConstantValue { |
| 702 // TODO(johnniwinther): Make [fields] private to avoid misuse of the map | 702 // TODO(johnniwinther): Make [fields] private to avoid misuse of the map |
| 703 // ordering and mutability. | 703 // ordering and mutability. |
| 704 final Map<FieldElement, ConstantValue> fields; | 704 final Map<FieldEntity, ConstantValue> fields; |
| 705 final int hashCode; | 705 final int hashCode; |
| 706 | 706 |
| 707 ConstructedConstantValue( | 707 ConstructedConstantValue( |
| 708 InterfaceType type, Map<FieldElement, ConstantValue> fields) | 708 InterfaceType type, Map<FieldEntity, ConstantValue> fields) |
| 709 : this.fields = fields, | 709 : this.fields = fields, |
| 710 hashCode = Hashing.unorderedMapHash(fields, Hashing.objectHash(type)), | 710 hashCode = Hashing.unorderedMapHash(fields, Hashing.objectHash(type)), |
| 711 super(type) { | 711 super(type) { |
| 712 assert(type != null); | 712 assert(type != null); |
| 713 assert(!fields.containsValue(null)); | 713 assert(!fields.containsValue(null)); |
| 714 } | 714 } |
| 715 | 715 |
| 716 bool get isConstructedObject => true; | 716 bool get isConstructedObject => true; |
| 717 | 717 |
| 718 bool operator ==(var otherVar) { | 718 bool operator ==(var otherVar) { |
| 719 if (identical(this, otherVar)) return true; | 719 if (identical(this, otherVar)) return true; |
| 720 if (otherVar is! ConstructedConstantValue) return false; | 720 if (otherVar is! ConstructedConstantValue) return false; |
| 721 ConstructedConstantValue other = otherVar; | 721 ConstructedConstantValue other = otherVar; |
| 722 if (hashCode != other.hashCode) return false; | 722 if (hashCode != other.hashCode) return false; |
| 723 if (type != other.type) return false; | 723 if (type != other.type) return false; |
| 724 if (fields.length != other.fields.length) return false; | 724 if (fields.length != other.fields.length) return false; |
| 725 for (FieldElement field in fields.keys) { | 725 for (FieldEntity field in fields.keys) { |
| 726 if (fields[field] != other.fields[field]) return false; | 726 if (fields[field] != other.fields[field]) return false; |
| 727 } | 727 } |
| 728 return true; | 728 return true; |
| 729 } | 729 } |
| 730 | 730 |
| 731 List<ConstantValue> getDependencies() => fields.values.toList(); | 731 List<ConstantValue> getDependencies() => fields.values.toList(); |
| 732 | 732 |
| 733 accept(ConstantValueVisitor visitor, arg) { | 733 accept(ConstantValueVisitor visitor, arg) { |
| 734 return visitor.visitConstructed(this, arg); | 734 return visitor.visitConstructed(this, arg); |
| 735 } | 735 } |
| 736 | 736 |
| 737 ConstantValueKind get kind => ConstantValueKind.CONSTRUCTED; | 737 ConstantValueKind get kind => ConstantValueKind.CONSTRUCTED; |
| 738 | 738 |
| 739 String toDartText() { | 739 String toDartText() { |
| 740 StringBuffer sb = new StringBuffer(); | 740 StringBuffer sb = new StringBuffer(); |
| 741 sb.write(type.name); | 741 sb.write(type.name); |
| 742 _unparseTypeArguments(sb); | 742 _unparseTypeArguments(sb); |
| 743 sb.write('('); | 743 sb.write('('); |
| 744 int i = 0; | 744 int i = 0; |
| 745 fields.forEach((FieldElement field, ConstantValue value) { | 745 fields.forEach((FieldEntity field, ConstantValue value) { |
| 746 if (i > 0) sb.write(','); | 746 if (i > 0) sb.write(','); |
| 747 sb.write(field.name); | 747 sb.write(field.name); |
| 748 sb.write('='); | 748 sb.write('='); |
| 749 sb.write(value.toDartText()); | 749 sb.write(value.toDartText()); |
| 750 i++; | 750 i++; |
| 751 }); | 751 }); |
| 752 sb.write(')'); | 752 sb.write(')'); |
| 753 return sb.toString(); | 753 return sb.toString(); |
| 754 } | 754 } |
| 755 | 755 |
| 756 String toStructuredText() { | 756 String toStructuredText() { |
| 757 StringBuffer sb = new StringBuffer(); | 757 StringBuffer sb = new StringBuffer(); |
| 758 sb.write('ConstructedConstant('); | 758 sb.write('ConstructedConstant('); |
| 759 sb.write(type); | 759 sb.write(type); |
| 760 sb.write('('); | 760 sb.write('('); |
| 761 int i = 0; | 761 int i = 0; |
| 762 fields.forEach((FieldElement field, ConstantValue value) { | 762 fields.forEach((FieldEntity field, ConstantValue value) { |
| 763 if (i > 0) sb.write(','); | 763 if (i > 0) sb.write(','); |
| 764 sb.write(field.name); | 764 sb.write(field.name); |
| 765 sb.write('='); | 765 sb.write('='); |
| 766 sb.write(value.toStructuredText()); | 766 sb.write(value.toStructuredText()); |
| 767 i++; | 767 i++; |
| 768 }); | 768 }); |
| 769 sb.write('))'); | 769 sb.write('))'); |
| 770 return sb.toString(); | 770 return sb.toString(); |
| 771 } | 771 } |
| 772 } | 772 } |
| 773 | 773 |
| 774 /// A reference to a constant in another output unit. | 774 /// A reference to a constant in another output unit. |
| 775 /// Used for referring to deferred constants. | 775 /// Used for referring to deferred constants. |
| 776 class DeferredConstantValue extends ConstantValue { | 776 class DeferredConstantValue extends ConstantValue { |
| 777 DeferredConstantValue(this.referenced, this.prefix); | 777 DeferredConstantValue(this.referenced, this.prefix); |
| 778 | 778 |
| 779 final ConstantValue referenced; | 779 final ConstantValue referenced; |
| 780 final PrefixElement prefix; | 780 final Entity prefix; |
| 781 | 781 |
| 782 bool get isReference => true; | 782 bool get isReference => true; |
| 783 | 783 |
| 784 bool operator ==(other) { | 784 bool operator ==(other) { |
| 785 return other is DeferredConstantValue && | 785 return other is DeferredConstantValue && |
| 786 referenced == other.referenced && | 786 referenced == other.referenced && |
| 787 prefix == other.prefix; | 787 prefix == other.prefix; |
| 788 } | 788 } |
| 789 | 789 |
| 790 get hashCode => (referenced.hashCode * 17 + prefix.hashCode) & 0x3fffffff; | 790 get hashCode => (referenced.hashCode * 17 + prefix.hashCode) & 0x3fffffff; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 822 DartType getType(CommonElements types) => const DynamicType(); | 822 DartType getType(CommonElements types) => const DynamicType(); |
| 823 | 823 |
| 824 ConstantValueKind get kind => ConstantValueKind.NON_CONSTANT; | 824 ConstantValueKind get kind => ConstantValueKind.NON_CONSTANT; |
| 825 | 825 |
| 826 @override | 826 @override |
| 827 String toStructuredText() => 'NonConstant'; | 827 String toStructuredText() => 'NonConstant'; |
| 828 | 828 |
| 829 @override | 829 @override |
| 830 String toDartText() => '>>non-constant<<'; | 830 String toDartText() => '>>non-constant<<'; |
| 831 } | 831 } |
| OLD | NEW |