| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 /// Functions for asserting equivalence across serialization. | 5 /// Functions for asserting equivalence across serialization. |
| 6 | 6 |
| 7 library dart2js.serialization.equivalence; | 7 library dart2js.serialization.equivalence; |
| 8 | 8 |
| 9 import '../closure.dart'; | 9 import '../closure.dart'; |
| 10 import '../common/resolution.dart'; | 10 import '../common/resolution.dart'; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 } | 90 } |
| 91 | 91 |
| 92 /// Returns `true` if elements [a] and [b] are equivalent. | 92 /// Returns `true` if elements [a] and [b] are equivalent. |
| 93 bool areElementsEquivalent(Element a, Element b) { | 93 bool areElementsEquivalent(Element a, Element b) { |
| 94 if (identical(a, b)) return true; | 94 if (identical(a, b)) return true; |
| 95 if (a == null || b == null) return false; | 95 if (a == null || b == null) return false; |
| 96 return const ElementIdentityEquivalence().visit(a, b); | 96 return const ElementIdentityEquivalence().visit(a, b); |
| 97 } | 97 } |
| 98 | 98 |
| 99 /// Returns `true` if types [a] and [b] are equivalent. | 99 /// Returns `true` if types [a] and [b] are equivalent. |
| 100 bool areTypesEquivalent(DartType a, DartType b) { | 100 bool areTypesEquivalent(ResolutionDartType a, ResolutionDartType b) { |
| 101 if (identical(a, b)) return true; | 101 if (identical(a, b)) return true; |
| 102 if (a == null || b == null) return false; | 102 if (a == null || b == null) return false; |
| 103 return const TypeEquivalence().visit(a, b); | 103 return const TypeEquivalence().visit(a, b); |
| 104 } | 104 } |
| 105 | 105 |
| 106 /// Returns `true` if constants [exp1] and [exp2] are equivalent. | 106 /// Returns `true` if constants [exp1] and [exp2] are equivalent. |
| 107 bool areConstantsEquivalent(ConstantExpression exp1, ConstantExpression exp2) { | 107 bool areConstantsEquivalent(ConstantExpression exp1, ConstantExpression exp2) { |
| 108 if (identical(exp1, exp2)) return true; | 108 if (identical(exp1, exp2)) return true; |
| 109 if (exp1 == null || exp2 == null) return false; | 109 if (exp1 == null || exp2 == null) return false; |
| 110 return const ConstantEquivalence().visit(exp1, exp2); | 110 return const ConstantEquivalence().visit(exp1, exp2); |
| 111 } | 111 } |
| 112 | 112 |
| 113 /// Returns `true` if constant values [value1] and [value2] are equivalent. | 113 /// Returns `true` if constant values [value1] and [value2] are equivalent. |
| 114 bool areConstantValuesEquivalent(ConstantValue value1, ConstantValue value2) { | 114 bool areConstantValuesEquivalent(ConstantValue value1, ConstantValue value2) { |
| 115 if (identical(value1, value2)) return true; | 115 if (identical(value1, value2)) return true; |
| 116 if (value1 == null || value2 == null) return false; | 116 if (value1 == null || value2 == null) return false; |
| 117 return const ConstantValueEquivalence().visit(value1, value2); | 117 return const ConstantValueEquivalence().visit(value1, value2); |
| 118 } | 118 } |
| 119 | 119 |
| 120 /// Returns `true` if the lists of elements, [a] and [b], are equivalent. | 120 /// Returns `true` if the lists of elements, [a] and [b], are equivalent. |
| 121 bool areElementListsEquivalent(List<Element> a, List<Element> b) { | 121 bool areElementListsEquivalent(List<Element> a, List<Element> b) { |
| 122 return areListsEquivalent(a, b, areElementsEquivalent); | 122 return areListsEquivalent(a, b, areElementsEquivalent); |
| 123 } | 123 } |
| 124 | 124 |
| 125 /// Returns `true` if the lists of types, [a] and [b], are equivalent. | 125 /// Returns `true` if the lists of types, [a] and [b], are equivalent. |
| 126 bool areTypeListsEquivalent(List<DartType> a, List<DartType> b) { | 126 bool areTypeListsEquivalent( |
| 127 List<ResolutionDartType> a, List<ResolutionDartType> b) { |
| 127 return areListsEquivalent(a, b, areTypesEquivalent); | 128 return areListsEquivalent(a, b, areTypesEquivalent); |
| 128 } | 129 } |
| 129 | 130 |
| 130 /// Returns `true` if the lists of constants, [a] and [b], are equivalent. | 131 /// Returns `true` if the lists of constants, [a] and [b], are equivalent. |
| 131 bool areConstantListsEquivalent( | 132 bool areConstantListsEquivalent( |
| 132 List<ConstantExpression> a, List<ConstantExpression> b) { | 133 List<ConstantExpression> a, List<ConstantExpression> b) { |
| 133 return areListsEquivalent(a, b, areConstantsEquivalent); | 134 return areListsEquivalent(a, b, areConstantsEquivalent); |
| 134 } | 135 } |
| 135 | 136 |
| 136 /// Returns `true` if the lists of constant values, [a] and [b], are equivalent. | 137 /// Returns `true` if the lists of constant values, [a] and [b], are equivalent. |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 bool valueEquivalence(a, b) = equality]) { | 353 bool valueEquivalence(a, b) = equality]) { |
| 353 return areMapsEquivalent(map1, map2, keyEquivalence, valueEquivalence); | 354 return areMapsEquivalent(map1, map2, keyEquivalence, valueEquivalence); |
| 354 } | 355 } |
| 355 | 356 |
| 356 bool testElements(Object object1, Object object2, String property, | 357 bool testElements(Object object1, Object object2, String property, |
| 357 Element element1, Element element2) { | 358 Element element1, Element element2) { |
| 358 return areElementsEquivalent(element1, element2); | 359 return areElementsEquivalent(element1, element2); |
| 359 } | 360 } |
| 360 | 361 |
| 361 bool testTypes(Object object1, Object object2, String property, | 362 bool testTypes(Object object1, Object object2, String property, |
| 362 DartType type1, DartType type2) { | 363 ResolutionDartType type1, ResolutionDartType type2) { |
| 363 return areTypesEquivalent(type1, type2); | 364 return areTypesEquivalent(type1, type2); |
| 364 } | 365 } |
| 365 | 366 |
| 366 bool testConstants(Object object1, Object object2, String property, | 367 bool testConstants(Object object1, Object object2, String property, |
| 367 ConstantExpression exp1, ConstantExpression exp2) { | 368 ConstantExpression exp1, ConstantExpression exp2) { |
| 368 return areConstantsEquivalent(exp1, exp2); | 369 return areConstantsEquivalent(exp1, exp2); |
| 369 } | 370 } |
| 370 | 371 |
| 371 bool testConstantValues(Object object1, Object object2, String property, | 372 bool testConstantValues(Object object1, Object object2, String property, |
| 372 ConstantValue value1, ConstantValue value2) { | 373 ConstantValue value1, ConstantValue value2) { |
| 373 return areConstantValuesEquivalent(value1, value2); | 374 return areConstantValuesEquivalent(value1, value2); |
| 374 } | 375 } |
| 375 | 376 |
| 376 bool testTypeLists(Object object1, Object object2, String property, | 377 bool testTypeLists(Object object1, Object object2, String property, |
| 377 List<DartType> list1, List<DartType> list2) { | 378 List<ResolutionDartType> list1, List<ResolutionDartType> list2) { |
| 378 return areTypeListsEquivalent(list1, list2); | 379 return areTypeListsEquivalent(list1, list2); |
| 379 } | 380 } |
| 380 | 381 |
| 381 bool testConstantLists(Object object1, Object object2, String property, | 382 bool testConstantLists(Object object1, Object object2, String property, |
| 382 List<ConstantExpression> list1, List<ConstantExpression> list2) { | 383 List<ConstantExpression> list1, List<ConstantExpression> list2) { |
| 383 return areConstantListsEquivalent(list1, list2); | 384 return areConstantListsEquivalent(list1, list2); |
| 384 } | 385 } |
| 385 | 386 |
| 386 bool testConstantValueLists(Object object1, Object object2, String property, | 387 bool testConstantValueLists(Object object1, Object object2, String property, |
| 387 List<ConstantValue> list1, List<ConstantValue> list2) { | 388 List<ConstantValue> list1, List<ConstantValue> list2) { |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 585 } | 586 } |
| 586 | 587 |
| 587 @override | 588 @override |
| 588 bool visitWarnOnUseElement( | 589 bool visitWarnOnUseElement( |
| 589 WarnOnUseElement element1, WarnOnUseElement element2) { | 590 WarnOnUseElement element1, WarnOnUseElement element2) { |
| 590 return strategy.testElements(element1, element2, 'wrappedElement', | 591 return strategy.testElements(element1, element2, 'wrappedElement', |
| 591 element1.wrappedElement, element2.wrappedElement); | 592 element1.wrappedElement, element2.wrappedElement); |
| 592 } | 593 } |
| 593 } | 594 } |
| 594 | 595 |
| 595 /// Visitor that checks for equivalence of [DartType]s. | 596 /// Visitor that checks for equivalence of [ResolutionDartType]s. |
| 596 class TypeEquivalence implements DartTypeVisitor<bool, DartType> { | 597 class TypeEquivalence implements DartTypeVisitor<bool, ResolutionDartType> { |
| 597 final TestStrategy strategy; | 598 final TestStrategy strategy; |
| 598 | 599 |
| 599 const TypeEquivalence([this.strategy = const TestStrategy()]); | 600 const TypeEquivalence([this.strategy = const TestStrategy()]); |
| 600 | 601 |
| 601 bool visit(DartType type1, DartType type2) { | 602 bool visit(ResolutionDartType type1, ResolutionDartType type2) { |
| 602 return strategy.test(type1, type2, 'kind', type1.kind, type2.kind) && | 603 return strategy.test(type1, type2, 'kind', type1.kind, type2.kind) && |
| 603 type1.accept(this, type2); | 604 type1.accept(this, type2); |
| 604 } | 605 } |
| 605 | 606 |
| 606 @override | 607 @override |
| 607 bool visitDynamicType(DynamicType type, DynamicType other) => true; | 608 bool visitDynamicType( |
| 609 ResolutionDynamicType type, ResolutionDynamicType other) => |
| 610 true; |
| 608 | 611 |
| 609 @override | 612 @override |
| 610 bool visitFunctionType(FunctionType type, FunctionType other) { | 613 bool visitFunctionType( |
| 614 ResolutionFunctionType type, ResolutionFunctionType other) { |
| 611 return strategy.testTypeLists(type, other, 'parameterTypes', | 615 return strategy.testTypeLists(type, other, 'parameterTypes', |
| 612 type.parameterTypes, other.parameterTypes) && | 616 type.parameterTypes, other.parameterTypes) && |
| 613 strategy.testTypeLists(type, other, 'optionalParameterTypes', | 617 strategy.testTypeLists(type, other, 'optionalParameterTypes', |
| 614 type.optionalParameterTypes, other.optionalParameterTypes) && | 618 type.optionalParameterTypes, other.optionalParameterTypes) && |
| 615 strategy.testTypeLists(type, other, 'namedParameterTypes', | 619 strategy.testTypeLists(type, other, 'namedParameterTypes', |
| 616 type.namedParameterTypes, other.namedParameterTypes) && | 620 type.namedParameterTypes, other.namedParameterTypes) && |
| 617 strategy.testLists(type, other, 'namedParameters', type.namedParameters, | 621 strategy.testLists(type, other, 'namedParameters', type.namedParameters, |
| 618 other.namedParameters); | 622 other.namedParameters); |
| 619 } | 623 } |
| 620 | 624 |
| 621 bool visitGenericType(GenericType type, GenericType other) { | 625 bool visitGenericType(GenericType type, GenericType other) { |
| 622 return strategy.testElements( | 626 return strategy.testElements( |
| 623 type, other, 'element', type.element, other.element) && | 627 type, other, 'element', type.element, other.element) && |
| 624 strategy.testTypeLists(type, other, 'typeArguments', type.typeArguments, | 628 strategy.testTypeLists(type, other, 'typeArguments', type.typeArguments, |
| 625 other.typeArguments); | 629 other.typeArguments); |
| 626 } | 630 } |
| 627 | 631 |
| 628 @override | 632 @override |
| 629 bool visitMalformedType(MalformedType type, MalformedType other) => true; | 633 bool visitMalformedType(MalformedType type, MalformedType other) => true; |
| 630 | 634 |
| 631 @override | 635 @override |
| 632 bool visitTypeVariableType(TypeVariableType type, TypeVariableType other) { | 636 bool visitTypeVariableType( |
| 637 ResolutionTypeVariableType type, ResolutionTypeVariableType other) { |
| 633 return strategy.testElements( | 638 return strategy.testElements( |
| 634 type, other, 'element', type.element, other.element) && | 639 type, other, 'element', type.element, other.element) && |
| 635 strategy.test(type, other, 'is MethodTypeVariableType', | 640 strategy.test(type, other, 'is MethodTypeVariableType', |
| 636 type is MethodTypeVariableType, other is MethodTypeVariableType); | 641 type is MethodTypeVariableType, other is MethodTypeVariableType); |
| 637 } | 642 } |
| 638 | 643 |
| 639 @override | 644 @override |
| 640 bool visitVoidType(VoidType type, VoidType argument) => true; | 645 bool visitVoidType(ResolutionVoidType type, ResolutionVoidType argument) => |
| 646 true; |
| 641 | 647 |
| 642 @override | 648 @override |
| 643 bool visitInterfaceType(InterfaceType type, InterfaceType other) { | 649 bool visitInterfaceType( |
| 650 ResolutionInterfaceType type, ResolutionInterfaceType other) { |
| 644 return visitGenericType(type, other); | 651 return visitGenericType(type, other); |
| 645 } | 652 } |
| 646 | 653 |
| 647 @override | 654 @override |
| 648 bool visitTypedefType(TypedefType type, TypedefType other) { | 655 bool visitTypedefType( |
| 656 ResolutionTypedefType type, ResolutionTypedefType other) { |
| 649 return visitGenericType(type, other); | 657 return visitGenericType(type, other); |
| 650 } | 658 } |
| 651 } | 659 } |
| 652 | 660 |
| 653 /// Visitor that checks for structural equivalence of [ConstantExpression]s. | 661 /// Visitor that checks for structural equivalence of [ConstantExpression]s. |
| 654 class ConstantEquivalence | 662 class ConstantEquivalence |
| 655 implements ConstantExpressionVisitor<bool, ConstantExpression> { | 663 implements ConstantExpressionVisitor<bool, ConstantExpression> { |
| 656 final TestStrategy strategy; | 664 final TestStrategy strategy; |
| 657 | 665 |
| 658 const ConstantEquivalence([this.strategy = const TestStrategy()]); | 666 const ConstantEquivalence([this.strategy = const TestStrategy()]); |
| (...skipping 1370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2029 } | 2037 } |
| 2030 | 2038 |
| 2031 bool areMetadataAnnotationsEquivalent( | 2039 bool areMetadataAnnotationsEquivalent( |
| 2032 MetadataAnnotation metadata1, MetadataAnnotation metadata2) { | 2040 MetadataAnnotation metadata1, MetadataAnnotation metadata2) { |
| 2033 if (metadata1 == metadata2) return true; | 2041 if (metadata1 == metadata2) return true; |
| 2034 if (metadata1 == null || metadata2 == null) return false; | 2042 if (metadata1 == null || metadata2 == null) return false; |
| 2035 return areElementsEquivalent( | 2043 return areElementsEquivalent( |
| 2036 metadata1.annotatedElement, metadata2.annotatedElement) && | 2044 metadata1.annotatedElement, metadata2.annotatedElement) && |
| 2037 areConstantsEquivalent(metadata1.constant, metadata2.constant); | 2045 areConstantsEquivalent(metadata1.constant, metadata2.constant); |
| 2038 } | 2046 } |
| OLD | NEW |