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 '../diagnostics/invariant.dart' show |
11 show ClassElement, | 11 DEBUG_MODE; |
12 Element, | 12 import '../elements/elements.dart' show |
13 FieldElement, | 13 ClassElement, |
14 FunctionElement, | 14 Element, |
15 PrefixElement; | 15 FieldElement, |
| 16 FunctionElement, |
| 17 PrefixElement; |
16 import '../tree/tree.dart' hide unparse; | 18 import '../tree/tree.dart' hide unparse; |
17 import '../util/util.dart' show Hashing; | 19 import '../util/util.dart' show |
| 20 Hashing; |
18 | 21 |
19 abstract class ConstantValueVisitor<R, A> { | 22 abstract class ConstantValueVisitor<R, A> { |
20 const ConstantValueVisitor(); | 23 const ConstantValueVisitor(); |
21 | 24 |
22 R visitFunction(FunctionConstantValue constant, A arg); | 25 R visitFunction(FunctionConstantValue constant, A arg); |
23 R visitNull(NullConstantValue constant, A arg); | 26 R visitNull(NullConstantValue constant, A arg); |
24 R visitInt(IntConstantValue constant, A arg); | 27 R visitInt(IntConstantValue constant, A arg); |
25 R visitDouble(DoubleConstantValue constant, A arg); | 28 R visitDouble(DoubleConstantValue constant, A arg); |
26 R visitBool(BoolConstantValue constant, A arg); | 29 R visitBool(BoolConstantValue constant, A arg); |
27 R visitString(StringConstantValue constant, A arg); | 30 R visitString(StringConstantValue constant, A arg); |
28 R visitList(ListConstantValue constant, A arg); | 31 R visitList(ListConstantValue constant, A arg); |
29 R visitMap(MapConstantValue constant, A arg); | 32 R visitMap(MapConstantValue constant, A arg); |
30 R visitConstructed(ConstructedConstantValue constant, A arg); | 33 R visitConstructed(ConstructedConstantValue constant, A arg); |
31 R visitType(TypeConstantValue constant, A arg); | 34 R visitType(TypeConstantValue constant, A arg); |
32 R visitInterceptor(InterceptorConstantValue constant, A arg); | 35 R visitInterceptor(InterceptorConstantValue constant, A arg); |
33 R visitSynthetic(SyntheticConstantValue constant, A arg); | 36 R visitSynthetic(SyntheticConstantValue constant, A arg); |
34 R visitDeferred(DeferredConstantValue constant, A arg); | 37 R visitDeferred(DeferredConstantValue constant, A arg); |
| 38 R visitNonConstant(NonConstantValue constant, A arg); |
35 } | 39 } |
36 | 40 |
37 abstract class ConstantValue { | 41 abstract class ConstantValue { |
38 const ConstantValue(); | 42 const ConstantValue(); |
39 | 43 |
40 /// `true` if this is a valid constant value. | 44 /// `true` if this is a valid constant value. |
41 bool get isConstant => true; | 45 bool get isConstant => true; |
42 | 46 |
43 bool get isNull => false; | 47 bool get isNull => false; |
44 bool get isBool => false; | 48 bool get isBool => false; |
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 | 630 |
627 String unparse() => 'synthetic($kind, $payload)'; | 631 String unparse() => 'synthetic($kind, $payload)'; |
628 | 632 |
629 String toStructuredString() => 'SyntheticConstant($kind, $payload)'; | 633 String toStructuredString() => 'SyntheticConstant($kind, $payload)'; |
630 } | 634 } |
631 | 635 |
632 class ConstructedConstantValue extends ObjectConstantValue { | 636 class ConstructedConstantValue extends ObjectConstantValue { |
633 final Map<FieldElement, ConstantValue> fields; | 637 final Map<FieldElement, ConstantValue> fields; |
634 final int hashCode; | 638 final int hashCode; |
635 | 639 |
636 ConstructedConstantValue(InterfaceType type, | 640 factory ConstructedConstantValue( |
637 Map<FieldElement, ConstantValue> fields) | 641 InterfaceType type, |
638 : this.fields = fields, | 642 Map<FieldElement, ConstantValue> fields) { |
639 hashCode = Hashing.mapHash(fields, Hashing.objectHash(type)), | 643 fields = normalizeFields(type, fields); |
640 super(type) { | 644 int hashCode = Hashing.mapHash(fields, Hashing.objectHash(type)); |
| 645 return new ConstructedConstantValue._(type, fields, hashCode); |
| 646 } |
| 647 |
| 648 ConstructedConstantValue._(InterfaceType type, this.fields, this.hashCode) |
| 649 : super(type) { |
641 assert(type != null); | 650 assert(type != null); |
642 assert(!fields.containsValue(null)); | 651 assert(invariant(NO_LOCATION_SPANNABLE, !fields.containsValue(null), |
| 652 message: () { |
| 653 DEBUG_MODE = true; |
| 654 return "Null field value for constructed constant of type '$type' " |
| 655 "with fields ${fields}."; |
| 656 })); |
| 657 } |
| 658 |
| 659 static Map<FieldElement, ConstantValue> normalizeFields( |
| 660 InterfaceType type, Map<FieldElement, ConstantValue> fields) { |
| 661 Map<FieldElement, ConstantValue> normalizedFields = |
| 662 <FieldElement, ConstantValue>{}; |
| 663 type.element.implementation.forEachInstanceField( |
| 664 (ClassElement enclosingClass, FieldElement field) { |
| 665 assert(invariant(NO_LOCATION_SPANNABLE, fields.containsKey(field), |
| 666 message: () { |
| 667 DEBUG_MODE = true; |
| 668 return "No field value provided for $field in constructed constant " |
| 669 "of type '$type' with fields ${fields}."; |
| 670 })); |
| 671 normalizedFields[field] = fields[field]; |
| 672 }, includeSuperAndInjectedMembers: true); |
| 673 return normalizedFields; |
643 } | 674 } |
644 | 675 |
645 bool get isConstructedObject => true; | 676 bool get isConstructedObject => true; |
646 | 677 |
647 bool operator ==(var otherVar) { | 678 bool operator ==(var otherVar) { |
648 if (otherVar is !ConstructedConstantValue) return false; | 679 if (otherVar is !ConstructedConstantValue) return false; |
649 ConstructedConstantValue other = otherVar; | 680 ConstructedConstantValue other = otherVar; |
650 if (hashCode != other.hashCode) return false; | 681 if (hashCode != other.hashCode) return false; |
651 if (type != other.type) return false; | 682 if (type != other.type) return false; |
652 if (fields.length != other.fields.length) return false; | 683 if (fields.length != other.fields.length) return false; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 } | 758 } |
728 | 759 |
729 /// A constant value resulting from a non constant or erroneous constant | 760 /// A constant value resulting from a non constant or erroneous constant |
730 /// expression. | 761 /// expression. |
731 // TODO(johnniwinther): Expand this to contain the error kind. | 762 // TODO(johnniwinther): Expand this to contain the error kind. |
732 class NonConstantValue extends ConstantValue { | 763 class NonConstantValue extends ConstantValue { |
733 bool get isConstant => false; | 764 bool get isConstant => false; |
734 | 765 |
735 @override | 766 @override |
736 accept(ConstantValueVisitor visitor, arg) { | 767 accept(ConstantValueVisitor visitor, arg) { |
737 // TODO(johnniwinther): Should this be part of the visiting? | 768 return visitor.visitNonConstant(this, arg); |
738 } | 769 } |
739 | 770 |
740 @override | 771 @override |
741 List<ConstantValue> getDependencies() => const <ConstantValue>[]; | 772 List<ConstantValue> getDependencies() => const <ConstantValue>[]; |
742 | 773 |
743 @override | 774 @override |
744 DartType getType(CoreTypes types) => const DynamicType(); | 775 DartType getType(CoreTypes types) => const DynamicType(); |
745 | 776 |
746 @override | 777 @override |
747 String toStructuredString() => 'NonConstant'; | 778 String toStructuredString() => 'NonConstant'; |
748 | 779 |
749 @override | 780 @override |
750 String unparse() => '>>non-constant<<'; | 781 String unparse() => '>>non-constant<<'; |
751 } | 782 } |
OLD | NEW |