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

Unified Diff: pkg/compiler/lib/src/dart_types.dart

Issue 1976213002: Adjusts dart2js backend to handle method type arguments. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Made MethodTypeVariableType malformed, eliminated malformMethodTypeVariableType Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: pkg/compiler/lib/src/dart_types.dart
diff --git a/pkg/compiler/lib/src/dart_types.dart b/pkg/compiler/lib/src/dart_types.dart
index 685077d0b6380f328b351f118f8e5157f45b29d2..f6c830a4773f8b12ba4dcf329f19f2ae6ad1e133 100644
--- a/pkg/compiler/lib/src/dart_types.dart
+++ b/pkg/compiler/lib/src/dart_types.dart
@@ -10,7 +10,7 @@ import 'common/resolution.dart' show Resolution;
import 'common.dart';
import 'core_types.dart';
import 'elements/elements.dart';
-import 'elements/modelx.dart' show TypeDeclarationElementX;
+import 'elements/modelx.dart' show TypeDeclarationElementX, ErroneousElementX;
import 'ordered_typeset.dart' show OrderedTypeSet;
import 'util/util.dart' show equalElements;
@@ -124,7 +124,7 @@ abstract class DartType {
bool get isTypeVariable => kind == TypeKind.TYPE_VARIABLE;
/// Is [: true :] if this type is a malformed type.
- bool get isMalformed => kind == TypeKind.MALFORMED_TYPE;
+ bool get isMalformed => false;
/// Is `true` if this type is declared by an enum.
bool get isEnumType => false;
@@ -164,6 +164,15 @@ abstract class DartType {
type.accept(visitor, argument);
}
}
+
+ /// Returns a [DartType] which corresponds to [this] except that each
+ /// contained [MethodTypeVariableType] is replaced by a [DynamicType].
+ /// GENERIC_METHODS: Temporary, only used with '--generic-method-syntax'.
+ DartType get dynamifyMethodTypeVariableType => this;
+
+ /// Returns true iff [this] is or contains a [MethodTypeVariableType].
+ /// GENERIC_METHODS: Temporary, only used with '--generic-method-syntax'
+ bool get containsMethodTypeVariableType => false;
}
/**
@@ -234,6 +243,32 @@ class TypeVariableType extends DartType {
String toString() => name;
}
+/// Provides a thin model of method type variables: They are treated as if
+/// their value were `dynamic` when used in a type annotation, and as a
+/// malformed type when used in an `as` or `is` expression.
+class MethodTypeVariableType extends TypeVariableType {
+ MethodTypeVariableType(TypeVariableElement element) : super(element);
+
+ @override
+ bool get treatAsDynamic => true;
+
+ @override
+ bool get isMalformed => true;
+
+ @override
+ DartType get dynamifyMethodTypeVariableType => const DynamicType();
+
+ @override
+ get containsMethodTypeVariableType => true;
+
+ ErroneousElement get toMalformedElement =>
Johnni Winther 2016/05/23 12:50:41 Remove this (unused)
eernst 2016/05/23 14:30:24 (OK, unused after fixing constructor.dart:739), do
+ new ErroneousElementX(
+ MessageKind.TYPE_VARIABLE_FROM_METHOD_NOT_REIFIED,
+ const {},
+ element.name,
+ element.typeDeclaration);
+}
+
/// Internal type representing the result of analyzing a statement.
class StatementType extends DartType {
Element get element => null;
@@ -313,6 +348,9 @@ class MalformedType extends DartType {
// Malformed types are treated as dynamic.
bool get treatAsDynamic => true;
+ @override
+ bool get isMalformed => true;
+
accept(DartTypeVisitor visitor, var argument) {
return visitor.visitMalformedType(this, argument);
}
@@ -341,9 +379,13 @@ abstract class GenericType extends DartType {
final TypeDeclarationElement element;
final List<DartType> typeArguments;
- GenericType(TypeDeclarationElement element, this.typeArguments,
+ GenericType(TypeDeclarationElement element, List<DartType> typeArguments,
{bool checkTypeArgumentCount: true})
- : this.element = element {
+ : this.element = element,
+ this.typeArguments = typeArguments,
+ this.containsMethodTypeVariableType =
+ typeArguments.any(_typeContainsMethodTypeVariableType)
+ {
assert(invariant(CURRENT_ELEMENT_SPANNABLE, element != null,
message: "Missing element for generic type."));
assert(invariant(element, () {
@@ -405,6 +447,17 @@ abstract class GenericType extends DartType {
return sb.toString();
}
+ @override
+ final bool containsMethodTypeVariableType;
+
+ @override
+ DartType get dynamifyMethodTypeVariableType {
+ if (!containsMethodTypeVariableType) return this;
+ List<DartType> newTypeArguments = typeArguments.map(
+ (DartType type) => type.dynamifyMethodTypeVariableType).toList();
+ return createInstantiation(newTypeArguments);
+ }
+
int get hashCode {
int hash = element.hashCode;
for (DartType argument in typeArguments) {
@@ -586,11 +639,21 @@ class FunctionType extends DartType {
}
FunctionType.internal(FunctionTypedElement this.element,
- [DartType this.returnType = const DynamicType(),
- this.parameterTypes = const <DartType>[],
- this.optionalParameterTypes = const <DartType>[],
- this.namedParameters = const <String>[],
- this.namedParameterTypes = const <DartType>[]]) {
+ [DartType returnType = const DynamicType(),
+ List<DartType> parameterTypes = const <DartType>[],
+ List<DartType> optionalParameterTypes = const <DartType>[],
+ List<String> namedParameters = const <String>[],
+ List<DartType> namedParameterTypes = const <DartType>[]])
+ : this.returnType = returnType,
+ this.parameterTypes = parameterTypes,
+ this.optionalParameterTypes = optionalParameterTypes,
+ this.namedParameters = namedParameters,
+ this.namedParameterTypes = namedParameterTypes,
+ this.containsMethodTypeVariableType =
+ returnType.containsMethodTypeVariableType ||
+ parameterTypes.any(_typeContainsMethodTypeVariableType) ||
+ optionalParameterTypes.any(_typeContainsMethodTypeVariableType) ||
+ namedParameterTypes.any(_typeContainsMethodTypeVariableType) {
assert(invariant(
CURRENT_ELEMENT_SPANNABLE, element == null || element.isDeclaration));
// Assert that optional and named parameters are not used at the same time.
@@ -718,6 +781,28 @@ class FunctionType extends DartType {
int computeArity() => parameterTypes.length;
+ @override
+ DartType get dynamifyMethodTypeVariableType {
+ if (!containsMethodTypeVariableType) return this;
+ DartType eraseIt(DartType type) => type.dynamifyMethodTypeVariableType;
+ DartType newReturnType = returnType.dynamifyMethodTypeVariableType;
+ List<DartType> newParameterTypes = parameterTypes.map(eraseIt).toList();
+ List<DartType> newOptionalParameterTypes =
+ optionalParameterTypes.map(eraseIt).toList();
+ List<DartType> newNamedParameterTypes =
+ namedParameterTypes.map(eraseIt).toList();
+ return new FunctionType.internal(
+ element,
+ newReturnType,
+ newParameterTypes,
+ newOptionalParameterTypes,
+ namedParameters,
+ newNamedParameterTypes);
+ }
+
+ @override
+ final bool containsMethodTypeVariableType;
+
int get hashCode {
int hash = 3 * returnType.hashCode;
for (DartType parameter in parameterTypes) {
@@ -745,6 +830,9 @@ class FunctionType extends DartType {
}
}
+bool _typeContainsMethodTypeVariableType(DartType type) =>
+ type.containsMethodTypeVariableType;
+
class TypedefType extends GenericType {
DartType _unaliased;
@@ -1347,6 +1435,15 @@ class Types implements DartTypes {
static ClassElement getClassContext(DartType type) {
TypeVariableType typeVariable = type.typeVariableOccurrence;
if (typeVariable == null) return null;
+ // GENERIC_METHODS: When generic method support is complete enough to
+ // include a runtime value for method type variables this must be updated.
+ // For full support the global assumption that all type variables are
+ // declared by the same enclosing class will not hold: Both an enclosing
+ // method and an enclosing class may define type variables, so the return
+ // type cannot be [ClassElement] and the caller must be prepared to look in
+ // two locations, not one. Currently we ignore method type variables by
+ // returning in the next statement.
+ if (typeVariable.element.typeDeclaration is! ClassElement) return null;
return typeVariable.element.typeDeclaration;
}

Powered by Google App Engine
This is Rietveld 408576698