Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: pkg/analyzer/lib/src/generated/element.dart

Issue 1061043002: Remove union type support from analyzer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/generated/element_resolver.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // This code was auto-generated, is not intended to be edited, and is subject to 5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information. 6 // significant change. Please see the README file for more information.
7 7
8 library engine.element; 8 library engine.element;
9 9
10 import 'dart:collection'; 10 import 'dart:collection';
(...skipping 4799 matching lines...) Expand 10 before | Expand all | Expand 10 after
4810 bool internalIsMoreSpecificThan(DartType type, bool withDynamic, 4810 bool internalIsMoreSpecificThan(DartType type, bool withDynamic,
4811 Set<TypeImpl_TypePair> visitedTypePairs) { 4811 Set<TypeImpl_TypePair> visitedTypePairs) {
4812 // trivial base cases 4812 // trivial base cases
4813 if (type == null) { 4813 if (type == null) {
4814 return false; 4814 return false;
4815 } else if (identical(this, type) || 4815 } else if (identical(this, type) ||
4816 type.isDynamic || 4816 type.isDynamic ||
4817 type.isDartCoreFunction || 4817 type.isDartCoreFunction ||
4818 type.isObject) { 4818 type.isObject) {
4819 return true; 4819 return true;
4820 } else if (type is UnionType) {
4821 return (type as UnionTypeImpl).internalUnionTypeIsLessSpecificThan(
4822 this, withDynamic, visitedTypePairs);
4823 } else if (type is! FunctionType) { 4820 } else if (type is! FunctionType) {
4824 return false; 4821 return false;
4825 } else if (this == type) { 4822 } else if (this == type) {
4826 return true; 4823 return true;
4827 } 4824 }
4828 FunctionType t = this; 4825 FunctionType t = this;
4829 FunctionType s = type as FunctionType; 4826 FunctionType s = type as FunctionType;
4830 List<DartType> tTypes = t.normalParameterTypes; 4827 List<DartType> tTypes = t.normalParameterTypes;
4831 List<DartType> tOpTypes = t.optionalParameterTypes; 4828 List<DartType> tOpTypes = t.optionalParameterTypes;
4832 List<DartType> sTypes = s.normalParameterTypes; 4829 List<DartType> sTypes = s.normalParameterTypes;
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
4928 bool internalIsSubtypeOf( 4925 bool internalIsSubtypeOf(
4929 DartType type, Set<TypeImpl_TypePair> visitedTypePairs) { 4926 DartType type, Set<TypeImpl_TypePair> visitedTypePairs) {
4930 // trivial base cases 4927 // trivial base cases
4931 if (type == null) { 4928 if (type == null) {
4932 return false; 4929 return false;
4933 } else if (identical(this, type) || 4930 } else if (identical(this, type) ||
4934 type.isDynamic || 4931 type.isDynamic ||
4935 type.isDartCoreFunction || 4932 type.isDartCoreFunction ||
4936 type.isObject) { 4933 type.isObject) {
4937 return true; 4934 return true;
4938 } else if (type is UnionType) {
4939 return (type as UnionTypeImpl).internalUnionTypeIsSuperTypeOf(
4940 this, visitedTypePairs);
4941 } else if (type is! FunctionType) { 4935 } else if (type is! FunctionType) {
4942 return false; 4936 return false;
4943 } else if (this == type) { 4937 } else if (this == type) {
4944 return true; 4938 return true;
4945 } 4939 }
4946 FunctionType t = this; 4940 FunctionType t = this;
4947 FunctionType s = type as FunctionType; 4941 FunctionType s = type as FunctionType;
4948 List<DartType> tTypes = t.normalParameterTypes; 4942 List<DartType> tTypes = t.normalParameterTypes;
4949 List<DartType> tOpTypes = t.optionalParameterTypes; 4943 List<DartType> tOpTypes = t.optionalParameterTypes;
4950 List<DartType> sTypes = s.normalParameterTypes; 4944 List<DartType> sTypes = s.normalParameterTypes;
(...skipping 1175 matching lines...) Expand 10 before | Expand all | Expand 10 after
6126 @override 6120 @override
6127 bool internalIsMoreSpecificThan(DartType type, bool withDynamic, 6121 bool internalIsMoreSpecificThan(DartType type, bool withDynamic,
6128 Set<TypeImpl_TypePair> visitedTypePairs) { 6122 Set<TypeImpl_TypePair> visitedTypePairs) {
6129 // 6123 //
6130 // S is dynamic. 6124 // S is dynamic.
6131 // The test to determine whether S is dynamic is done here because dynamic 6125 // The test to determine whether S is dynamic is done here because dynamic
6132 // is not an instance of InterfaceType. 6126 // is not an instance of InterfaceType.
6133 // 6127 //
6134 if (type.isDynamic) { 6128 if (type.isDynamic) {
6135 return true; 6129 return true;
6136 } else if (type is UnionType) {
6137 return (type as UnionTypeImpl).internalUnionTypeIsLessSpecificThan(
6138 this, withDynamic, visitedTypePairs);
6139 } else if (type is! InterfaceType) { 6130 } else if (type is! InterfaceType) {
6140 return false; 6131 return false;
6141 } 6132 }
6142 return _isMoreSpecificThan(type as InterfaceType, 6133 return _isMoreSpecificThan(type as InterfaceType,
6143 new HashSet<ClassElement>(), withDynamic, visitedTypePairs); 6134 new HashSet<ClassElement>(), withDynamic, visitedTypePairs);
6144 } 6135 }
6145 6136
6146 @override 6137 @override
6147 bool internalIsSubtypeOf( 6138 bool internalIsSubtypeOf(
6148 DartType type, Set<TypeImpl_TypePair> visitedTypePairs) { 6139 DartType type, Set<TypeImpl_TypePair> visitedTypePairs) {
6149 // 6140 //
6150 // T is a subtype of S, written T <: S, iff [bottom/dynamic]T << S 6141 // T is a subtype of S, written T <: S, iff [bottom/dynamic]T << S
6151 // 6142 //
6152 if (type.isDynamic) { 6143 if (type.isDynamic) {
6153 return true; 6144 return true;
6154 } else if (type is TypeParameterType) { 6145 } else if (type is TypeParameterType) {
6155 return false; 6146 return false;
6156 } else if (type is UnionType) {
6157 return (type as UnionTypeImpl).internalUnionTypeIsSuperTypeOf(
6158 this, visitedTypePairs);
6159 } else if (type is FunctionType) { 6147 } else if (type is FunctionType) {
6160 // This implementation assumes transitivity 6148 // This implementation assumes transitivity
6161 // for function type subtyping on the RHS, but a literal reading 6149 // for function type subtyping on the RHS, but a literal reading
6162 // of the spec does not specify this. More precisely: 6150 // of the spec does not specify this. More precisely:
6163 // if T <: F1 and F1 <: F2 and F1 and F2 are function types, 6151 // if T <: F1 and F1 <: F2 and F1 and F2 are function types,
6164 // then we assume T <: F2. 6152 // then we assume T <: F2.
6165 // 6153 //
6166 // From the Function Types section of the spec: 6154 // From the Function Types section of the spec:
6167 // 6155 //
6168 // If a type I includes an instance method named call(), and the type of 6156 // If a type I includes an instance method named call(), and the type of
(...skipping 3313 matching lines...) Expand 10 before | Expand all | Expand 10 after
9482 * <i>T</i> may be assigned to a type <i>S</i>, written <i>T</i> &hArr; 9470 * <i>T</i> may be assigned to a type <i>S</i>, written <i>T</i> &hArr;
9483 * <i>S</i>, iff either <i>T</i> <: <i>S</i> or <i>S</i> <: <i>T</i> 9471 * <i>S</i>, iff either <i>T</i> <: <i>S</i> or <i>S</i> <: <i>T</i>
9484 * (Interface Types section of spec). 9472 * (Interface Types section of spec).
9485 * 9473 *
9486 * The given set of [visitedTypePairs] of types (T1, T2), where each pair 9474 * The given set of [visitedTypePairs] of types (T1, T2), where each pair
9487 * indicates that we invoked this method because we are in the process of 9475 * indicates that we invoked this method because we are in the process of
9488 * answering the question of whether T1 is a subtype of T2, is used to prevent 9476 * answering the question of whether T1 is a subtype of T2, is used to prevent
9489 * infinite loops. 9477 * infinite loops.
9490 */ 9478 */
9491 bool isAssignableTo2(DartType type, Set<TypeImpl_TypePair> visitedTypePairs) { 9479 bool isAssignableTo2(DartType type, Set<TypeImpl_TypePair> visitedTypePairs) {
9492 // Strictness matters for union types on the LHS, but not for union types 9480 // We use the language spec definition of [<=>].
9493 // on the RHS. 9481 return isSubtypeOf2(type, visitedTypePairs) ||
9494 if (this is UnionType) { 9482 (type as TypeImpl).isSubtypeOf2(this, visitedTypePairs);
9495 if (AnalysisEngine.instance.strictUnionTypes) {
9496 // *Every* element on the LHS must be assignable to the RHS.
9497 // We recursively fall into the next case when the RHS is also a union:
9498 // the order here is important!
9499 for (DartType left in (this as UnionType).elements) {
9500 // Would have to cast to [TypeImpl] to call the [visitedTypePairs]
9501 // version here.
9502 if (!left.isAssignableTo(type)) {
9503 return false;
9504 }
9505 }
9506 return true;
9507 } else {
9508 // *Some* element on the LHS must be assignable to the RHS.
9509 for (DartType left in (this as UnionType).elements) {
9510 // Would have to cast to [TypeImpl] to call the [visitedTypePairs]
9511 // version here.
9512 if (left.isAssignableTo(type)) {
9513 return true;
9514 }
9515 }
9516 return false;
9517 }
9518 } else if (type is UnionType) {
9519 // The LHS, which is not a union, must be assignable to *some* element
9520 // on the RHS.
9521 for (DartType right in type.elements) {
9522 if (this.isAssignableTo2(right, visitedTypePairs)) {
9523 return true;
9524 }
9525 }
9526 return false;
9527 } else {
9528 // For non union types we use the language spec definition of [<=>].
9529 return isSubtypeOf2(type, visitedTypePairs) ||
9530 (type as TypeImpl).isSubtypeOf2(this, visitedTypePairs);
9531 }
9532 } 9483 }
9533 9484
9534 @override 9485 @override
9535 bool isMoreSpecificThan(DartType type) => 9486 bool isMoreSpecificThan(DartType type) =>
9536 isMoreSpecificThan2(type, false, new HashSet<TypeImpl_TypePair>()); 9487 isMoreSpecificThan2(type, false, new HashSet<TypeImpl_TypePair>());
9537 9488
9538 /** 9489 /**
9539 * Return `true` if this type is more specific than the given [type]. If 9490 * Return `true` if this type is more specific than the given [type]. If
9540 * [withDynamic] is `true`, then "dynamic" should be considered as a subtype 9491 * [withDynamic] is `true`, then "dynamic" should be considered as a subtype
9541 * of any type. 9492 * of any type.
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after
9974 for (int i = 0; i < length; i++) { 9925 for (int i = 0; i < length; i++) {
9975 if (parameterTypes[i] == this) { 9926 if (parameterTypes[i] == this) {
9976 return argumentTypes[i]; 9927 return argumentTypes[i];
9977 } 9928 }
9978 } 9929 }
9979 return this; 9930 return this;
9980 } 9931 }
9981 } 9932 }
9982 9933
9983 /** 9934 /**
9984 * A union of other types. Union types are "flattened" in the sense that a union
9985 * type never contains another union type.
9986 */
9987 abstract class UnionType implements DartType {
9988 /**
9989 * Return an immutable view of the types in this union type.
9990 */
9991 Set<DartType> get elements;
9992 }
9993
9994 /**
9995 * In addition to the methods of the `UnionType` interface we add a factory
9996 * method `union` for building unions.
9997 */
9998 class UnionTypeImpl extends TypeImpl implements UnionType {
9999 /**
10000 * The types in this union.
10001 */
10002 final Set<DartType> _types;
10003
10004 /**
10005 * This constructor should only be called by the `union` factory: it does not
10006 * check that its argument [types] contains no union types.
10007 */
10008 UnionTypeImpl(this._types) : super(null, null);
10009
10010 @override
10011 String get displayName {
10012 StringBuffer buffer = new StringBuffer();
10013 String prefix = "{";
10014 for (DartType t in _types) {
10015 buffer.write(prefix);
10016 buffer.write(t.displayName);
10017 prefix = ",";
10018 }
10019 buffer.write("}");
10020 return buffer.toString();
10021 }
10022
10023 @override
10024 Set<DartType> get elements => _types;
10025
10026 @override
10027 int get hashCode => _types.hashCode;
10028
10029 @override
10030 bool operator ==(Object other) {
10031 if (other == null || other is! UnionType) {
10032 return false;
10033 } else if (identical(this, other)) {
10034 return true;
10035 } else {
10036 return javaSetEquals(_types, (other as UnionType).elements);
10037 }
10038 }
10039
10040 @override
10041 void appendTo(StringBuffer buffer, Set<DartType> visitedTypes) {
10042 if (!visitedTypes.add(this)) {
10043 buffer.write(name == null ? '...' : name);
10044 return;
10045 }
10046 String prefix = "{";
10047 for (DartType type in _types) {
10048 buffer.write(prefix);
10049 (type as TypeImpl).appendTo(buffer, visitedTypes);
10050 prefix = ",";
10051 }
10052 buffer.write("}");
10053 }
10054
10055 @override
10056 bool internalEquals(Object object, Set<ElementPair> visitedElementPairs) =>
10057 this == object;
10058
10059 @override
10060 int internalHashCode(List<DartType> visitedTypes) => hashCode;
10061
10062 @override
10063 bool internalIsMoreSpecificThan(DartType type, bool withDynamic,
10064 Set<TypeImpl_TypePair> visitedTypePairs) {
10065 // What version of subtyping do we want? See discussion below in
10066 // [internalIsSubtypeOf].
10067 if (AnalysisEngine.instance.strictUnionTypes) {
10068 // The less unsound version: all.
10069 for (DartType t in _types) {
10070 if (!(t as TypeImpl).internalIsMoreSpecificThan(
10071 type, withDynamic, visitedTypePairs)) {
10072 return false;
10073 }
10074 }
10075 return true;
10076 } else {
10077 // The more unsound version: any.
10078 for (DartType t in _types) {
10079 if ((t as TypeImpl).internalIsMoreSpecificThan(
10080 type, withDynamic, visitedTypePairs)) {
10081 return true;
10082 }
10083 }
10084 return false;
10085 }
10086 }
10087
10088 @override
10089 bool internalIsSubtypeOf(
10090 DartType type, Set<TypeImpl_TypePair> visitedTypePairs) {
10091 if (AnalysisEngine.instance.strictUnionTypes) {
10092 // The less unsound version: all.
10093 //
10094 // For this version to make sense we also need to redefine assignment
10095 // compatibility [<=>].
10096 // See discussion above.
10097 for (DartType t in _types) {
10098 if (!(t as TypeImpl).internalIsSubtypeOf(type, visitedTypePairs)) {
10099 return false;
10100 }
10101 }
10102 return true;
10103 } else {
10104 // The more unsound version: any.
10105 for (DartType t in _types) {
10106 if ((t as TypeImpl).internalIsSubtypeOf(type, visitedTypePairs)) {
10107 return true;
10108 }
10109 }
10110 return false;
10111 }
10112 }
10113
10114 /**
10115 * The more-specific-than test for union types on the RHS is uniform in
10116 * non-union LHSs. So, other `TypeImpl`s can call this method to implement
10117 * [internalIsMoreSpecificThan] for union types.
10118 */
10119 bool internalUnionTypeIsLessSpecificThan(DartType type, bool withDynamic,
10120 Set<TypeImpl_TypePair> visitedTypePairs) {
10121 // This implementation does not make sense when [type] is a union type,
10122 // at least for the "less unsound" version of [internalIsMoreSpecificThan]
10123 // above.
10124 if (type is UnionType) {
10125 throw new IllegalArgumentException("Only non-union types are supported.");
10126 }
10127 for (DartType t in _types) {
10128 if ((type as TypeImpl).internalIsMoreSpecificThan(
10129 t, withDynamic, visitedTypePairs)) {
10130 return true;
10131 }
10132 }
10133 return false;
10134 }
10135
10136 /**
10137 * The supertype test for union types is uniform in non-union subtypes. So,
10138 * other `TypeImpl`s can call this method to implement `internalIsSubtypeOf`
10139 * for union types.
10140 */
10141 bool internalUnionTypeIsSuperTypeOf(
10142 DartType type, Set<TypeImpl_TypePair> visitedTypePairs) {
10143 // This implementation does not make sense when [type] is a union type,
10144 // at least for the "less unsound" version of [internalIsSubtypeOf] above.
10145 if (type is UnionType) {
10146 throw new IllegalArgumentException("Only non-union types are supported.");
10147 }
10148 for (DartType t in _types) {
10149 if ((type as TypeImpl).internalIsSubtypeOf(t, visitedTypePairs)) {
10150 return true;
10151 }
10152 }
10153 return false;
10154 }
10155
10156 @override
10157 DartType substitute2(
10158 List<DartType> argumentTypes, List<DartType> parameterTypes) {
10159 List<DartType> out = new List<DartType>();
10160 for (DartType t in _types) {
10161 out.add(t.substitute2(argumentTypes, parameterTypes));
10162 }
10163 return union(out);
10164 }
10165
10166 /**
10167 * Return the union of the given [types]. Any unions in the [types] will be
10168 * flattened in the returned union. If there is only one type after flattening
10169 * then it will be returned directly, instead of a singleton union. Nulls are
10170 * discarded, unless all types are null, in which case an exception is raised.
10171 */
10172 static DartType union(List<DartType> types) {
10173 Set<DartType> set = new HashSet<DartType>();
10174 for (DartType t in types) {
10175 if (t is UnionType) {
10176 set.addAll(t.elements);
10177 } else {
10178 if (t != null) {
10179 set.add(t);
10180 }
10181 }
10182 }
10183 if (set.length == 0) {
10184 // TODO(collinsn): better to return [null] here? The use case is e.g.
10185 //
10186 // union(null, null) ==> null;
10187 //
10188 // instead of raising an exception.
10189 throw new IllegalArgumentException("No known use case for empty unions.");
10190 } else if (set.length == 1) {
10191 return set.first;
10192 } else {
10193 return new UnionTypeImpl(set);
10194 }
10195 }
10196 }
10197
10198 /**
10199 * An element included into a library using some URI. 9935 * An element included into a library using some URI.
10200 */ 9936 */
10201 abstract class UriReferencedElement implements Element { 9937 abstract class UriReferencedElement implements Element {
10202 /** 9938 /**
10203 * Return the URI that is used to include this element into the enclosing 9939 * Return the URI that is used to include this element into the enclosing
10204 * library, or `null` if this is the defining compilation unit of a library. 9940 * library, or `null` if this is the defining compilation unit of a library.
10205 */ 9941 */
10206 String get uri; 9942 String get uri;
10207 9943
10208 /** 9944 /**
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
10509 @override 10245 @override
10510 int internalHashCode(List<DartType> visitedTypes) => hashCode; 10246 int internalHashCode(List<DartType> visitedTypes) => hashCode;
10511 10247
10512 @override 10248 @override
10513 bool internalIsMoreSpecificThan(DartType type, bool withDynamic, 10249 bool internalIsMoreSpecificThan(DartType type, bool withDynamic,
10514 Set<TypeImpl_TypePair> visitedTypePairs) => isSubtypeOf(type); 10250 Set<TypeImpl_TypePair> visitedTypePairs) => isSubtypeOf(type);
10515 10251
10516 @override 10252 @override
10517 bool internalIsSubtypeOf( 10253 bool internalIsSubtypeOf(
10518 DartType type, Set<TypeImpl_TypePair> visitedTypePairs) { 10254 DartType type, Set<TypeImpl_TypePair> visitedTypePairs) {
10519 if (type is UnionType) {
10520 return (type as UnionTypeImpl).internalUnionTypeIsSuperTypeOf(
10521 this, visitedTypePairs);
10522 }
10523 // The only subtype relations that pertain to void are therefore: 10255 // The only subtype relations that pertain to void are therefore:
10524 // void <: void (by reflexivity) 10256 // void <: void (by reflexivity)
10525 // bottom <: void (as bottom is a subtype of all types). 10257 // bottom <: void (as bottom is a subtype of all types).
10526 // void <: dynamic (as dynamic is a supertype of all types) 10258 // void <: dynamic (as dynamic is a supertype of all types)
10527 return identical(type, this) || type.isDynamic; 10259 return identical(type, this) || type.isDynamic;
10528 } 10260 }
10529 10261
10530 @override 10262 @override
10531 VoidTypeImpl substitute2( 10263 VoidTypeImpl substitute2(
10532 List<DartType> argumentTypes, List<DartType> parameterTypes) => this; 10264 List<DartType> argumentTypes, List<DartType> parameterTypes) => this;
10533 } 10265 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/generated/element_resolver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698