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

Unified Diff: sdk/lib/_internal/compiler/implementation/typechecker.dart

Issue 11567009: Implement InterfaceType.asInstanceOf. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years 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
« no previous file with comments | « no previous file | tests/compiler/dart2js/type_substitution_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/compiler/implementation/typechecker.dart
diff --git a/sdk/lib/_internal/compiler/implementation/typechecker.dart b/sdk/lib/_internal/compiler/implementation/typechecker.dart
index 990dc32de9c144a80983f75fd3044ec1f195085c..6007cc1c75933cc7f7852f2f96e34ffa59500b5f 100644
--- a/sdk/lib/_internal/compiler/implementation/typechecker.dart
+++ b/sdk/lib/_internal/compiler/implementation/typechecker.dart
@@ -240,48 +240,6 @@ class VoidType extends DartType {
String toString() => name.slowToString();
}
-/**
- * Helper method for performing substitution of a linked list of types.
- *
- * If no types are changed by the substitution, the [types] is returned instead
- * of a newly created linked list.
- */
-Link<DartType> substTypes(Link<DartType> types,
- Link<DartType> arguments, Link<DartType> parameters) {
- bool changed = false;
- var builder = new LinkBuilder<DartType>();
- Link<DartType> typeLink = types;
- while (!typeLink.isEmpty) {
- var argument = typeLink.head.subst(arguments, parameters);
- if (!changed && !identical(argument, typeLink.head)) {
- changed = true;
- }
- builder.addLast(argument);
- typeLink = typeLink.tail;
- }
- if (changed) {
- // Create a new link only if necessary.
- return builder.toLink();
- }
- return types;
-}
-
-/**
- * Combine error messages in a malformed type to a single message string.
- */
-String fetchReasonsFromMalformedType(DartType type) {
- // TODO(johnniwinther): Figure out how to produce good error message in face
- // of multiple errors, and how to ensure non-localized error messages.
- var reasons = new List<String>();
- type.forEachMalformedType((MalformedType malformedType) {
- ErroneousElement error = malformedType.element;
- Message message = error.messageKind.message(error.messageArguments);
- reasons.add(message.toString());
- return true;
- });
- return Strings.join(reasons, ', ');
-}
-
class MalformedType extends DartType {
final ErroneousElement element;
@@ -379,7 +337,7 @@ class InterfaceType extends DartType {
return this;
}
Link<DartType> newTypeArguments =
- substTypes(typeArguments, arguments, parameters);
+ Types.substTypes(typeArguments, arguments, parameters);
if (!identical(typeArguments, newTypeArguments)) {
// Create a new type only if necessary.
return new InterfaceType(element, newTypeArguments);
@@ -396,6 +354,24 @@ class InterfaceType extends DartType {
return true;
}
+ /**
+ * Returns the type as an instance of class [other], if possible, null
+ * otherwise.
+ */
+ DartType asInstanceOf(ClassElement other) {
+ if (element == other) return this;
+ for (InterfaceType supertype in element.allSupertypes) {
+ ClassElement superclass = supertype.element;
+ if (superclass == other) {
+ Link<DartType> arguments = Types.substTypes(supertype.typeArguments,
+ typeArguments,
+ element.typeVariables);
+ return new InterfaceType(superclass, arguments);
+ }
+ }
+ return null;
+ }
+
DartType unalias(Compiler compiler) => this;
String toString() {
@@ -456,7 +432,8 @@ class FunctionType extends DartType {
}
var newReturnType = returnType.subst(arguments, parameters);
bool changed = !identical(newReturnType, returnType);
- var newParameterTypes = substTypes(parameterTypes, arguments, parameters);
+ var newParameterTypes = Types.substTypes(parameterTypes, arguments,
+ parameters);
if (!changed && !identical(parameterTypes, newParameterTypes)) {
changed = true;
}
@@ -546,8 +523,8 @@ class TypedefType extends DartType {
// Return fast on empty substitutions.
return this;
}
- Link<DartType> newTypeArguments =
- substTypes(typeArguments, arguments, parameters);
+ Link<DartType> newTypeArguments = Types.substTypes(typeArguments, arguments,
+ parameters);
if (!identical(typeArguments, newTypeArguments)) {
// Create a new type only if necessary.
return new TypedefType(element, newTypeArguments);
@@ -676,6 +653,50 @@ class Types {
bool isAssignable(DartType r, DartType s) {
return isSubtype(r, s) || isSubtype(s, r);
}
+
+
+ /**
+ * Helper method for performing substitution of a linked list of types.
+ *
+ * If no types are changed by the substitution, the [types] is returned
+ * instead of a newly created linked list.
+ */
+ static Link<DartType> substTypes(Link<DartType> types,
ahe 2012/12/14 08:00:19 Was this method changed? If not, why was it moved
karlklose 2013/01/08 14:01:43 It is unchanged. I moved this method from a top-le
+ Link<DartType> arguments,
+ Link<DartType> parameters) {
+ bool changed = false;
+ var builder = new LinkBuilder<DartType>();
+ Link<DartType> typeLink = types;
+ while (!typeLink.isEmpty) {
+ var argument = typeLink.head.subst(arguments, parameters);
+ if (!changed && !identical(argument, typeLink.head)) {
+ changed = true;
+ }
+ builder.addLast(argument);
+ typeLink = typeLink.tail;
+ }
+ if (changed) {
+ // Create a new link only if necessary.
+ return builder.toLink();
+ }
+ return types;
+ }
+
+ /**
+ * Combine error messages in a malformed type to a single message string.
+ */
+ static String fetchReasonsFromMalformedType(DartType type) {
ahe 2012/12/14 08:00:19 Was this method changed?
karlklose 2013/01/08 14:01:43 Not changed. See above for the reason to move it.
+ // TODO(johnniwinther): Figure out how to produce good error message in face
+ // of multiple errors, and how to ensure non-localized error messages.
+ var reasons = new List<String>();
+ type.forEachMalformedType((MalformedType malformedType) {
+ ErroneousElement error = malformedType.element;
+ Message message = error.messageKind.message(error.messageArguments);
+ reasons.add(message.toString());
+ return true;
+ });
+ return Strings.join(reasons, ', ');
+ }
}
class CancelTypeCheckException {
« no previous file with comments | « no previous file | tests/compiler/dart2js/type_substitution_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698