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

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

Issue 10832136: Skeleton typedef type implementation (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Backend updated Created 8 years, 4 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: lib/compiler/implementation/typechecker.dart
diff --git a/lib/compiler/implementation/typechecker.dart b/lib/compiler/implementation/typechecker.dart
index 8918e9a7a5fed736cfd0fb888b00e08241e9b47a..38b49d254afe1fd613d2d34686ceb6feed15b874 100644
--- a/lib/compiler/implementation/typechecker.dart
+++ b/lib/compiler/implementation/typechecker.dart
@@ -27,6 +27,18 @@ class TypeCheckerTask extends CompilerTask {
interface Type {
SourceString get name();
Element get element();
+
+ /**
+ * Returns the unaliased type of this type.
+ *
+ * The unaliased type of a typedef'd type is the unaliased type to which its
+ * name is bound. The unaliased version of any other type is the type itself.
+ *
+ * For example, the unaliased type of [: typedef A Func<A,B>(B b) :] is the
+ * function type [: (B) -> A :] and the unaliased type of
+ * [: Func<int,String> :] is the function type [: (String) -> int :].
+ */
+ Type unalias(Compiler compiler);
ngeoffray 2012/08/16 12:45:44 Why having an 'unalias' function and not a isFunct
ahe 2012/08/16 13:22:58 I was thinking the same. Clearly, unalias is the
Johnni Winther 2012/08/17 11:15:19 The convenience method would have to have the sign
ahe 2012/08/21 14:28:39 You can answer the question "is function?" without
Johnni Winther 2012/08/22 07:47:58 No. That is why we need Compiler.
ahe 2012/08/22 07:50:19 You don't need the compiler because the only Typed
}
class TypeVariableType implements Type {
@@ -36,7 +48,9 @@ class TypeVariableType implements Type {
SourceString get name() => element.name;
- toString() => name.slowToString();
+ Type unalias(Compiler compiler) => this;
+
+ String toString() => name.slowToString();
}
/**
@@ -59,6 +73,8 @@ class StatementType implements Type {
return (this === other) ? this : MAYBE_RETURNING;
}
+ Type unalias(Compiler compiler) => this;
+
String toString() => stringName;
}
@@ -67,7 +83,9 @@ class VoidType implements Type {
SourceString get name() => element.name;
final VoidElement element;
- toString() => name.slowToString();
+ Type unalias(Compiler compiler) => this;
+
+ String toString() => name.slowToString();
}
class InterfaceType implements Type {
@@ -79,7 +97,9 @@ class InterfaceType implements Type {
SourceString get name() => element.name;
- toString() {
+ Type unalias(Compiler compiler) => this;
+
+ String toString() {
StringBuffer sb = new StringBuffer();
sb.add(name.slowToString());
if (!arguments.isEmpty()) {
@@ -99,7 +119,9 @@ class FunctionType implements Type {
FunctionType(Type this.returnType, Link<Type> this.parameterTypes,
Element this.element);
- toString() {
+ Type unalias(Compiler compiler) => this;
+
+ String toString() {
StringBuffer sb = new StringBuffer();
bool first = true;
sb.add('(');
@@ -124,23 +146,60 @@ class FunctionType implements Type {
}
}
+class TypedefType implements Type {
+ final TypedefElement element;
+ final Link<Type> typeArguments;
+
+ const TypedefType(this.element,
+ [this.typeArguments = const EmptyLink<Type>()]);
+
+ SourceString get name() => element.name;
+
+ Type unalias(Compiler compiler) {
+ // TODO(ahe): This should [ensureResolved].
ngeoffray 2012/08/16 12:45:44 should *call*
Johnni Winther 2012/08/17 11:15:19 Done.
+ compiler.resolveTypedef(element);
+ return element.alias.unalias(compiler);
+ }
+
+ String toString() {
+ StringBuffer sb = new StringBuffer();
+ sb.add(name.slowToString());
+ if (!typeArguments.isEmpty()) {
+ sb.add('<');
+ typeArguments.printOn(sb, ', ');
+ sb.add('>');
+ }
+ return sb.toString();
+ }
+}
+
class Types {
+ final Compiler compiler;
final VoidType voidType;
final InterfaceType dynamicType;
- Types(Element dynamicElement)
- : this.with(dynamicElement, new LibraryElement(new Script(null, null)));
+ Types(Compiler compiler, Element dynamicElement)
+ : this.with(compiler, dynamicElement,
+ new LibraryElement(new Script(null, null)));
// TODO(karlklose): should we have a class Void?
ahe 2012/08/16 13:22:58 This TODO really belongs on line 178.
Johnni Winther 2012/08/17 11:15:19 Done.
- Types.with(Element dynamicElement, LibraryElement library)
+ Types.with(Compiler this.compiler,
+ Element dynamicElement,
+ LibraryElement library)
: voidType = new VoidType(new VoidElement(library)),
dynamicType = new InterfaceType(dynamicElement);
/** Returns true if t is a subtype of s */
bool isSubtype(Type t, Type s) {
- if (t === s || t === dynamicType || s === dynamicType ||
- // TODO(karlklose): Test for s.element === compiler.objectClass.
- s.name == const SourceString('Object')) return true;
+ if (t === s ||
+ t === dynamicType ||
+ s === dynamicType ||
+ s.element === compiler.objectClass) {
+ return true;
+ }
+ t = t.unalias(compiler);
+ s = s.unalias(compiler);
+
if (t is VoidType) {
return false;
} else if (t is InterfaceType) {
@@ -155,6 +214,7 @@ class Types {
}
return false;
} else if (t is FunctionType) {
+ if (s.element === compiler.functionClass) return true;
if (s is !FunctionType) return false;
FunctionType tf = t;
FunctionType sf = s;

Powered by Google App Engine
This is Rietveld 408576698