Chromium Code Reviews| Index: lib/compiler/implementation/typechecker.dart |
| diff --git a/lib/compiler/implementation/typechecker.dart b/lib/compiler/implementation/typechecker.dart |
| index 1ac592b557d0c985ad4d34f88fe7e73a30751c5a..a10ba93cf4540c74a06b86dfe10b790284afbbc5 100644 |
| --- a/lib/compiler/implementation/typechecker.dart |
| +++ b/lib/compiler/implementation/typechecker.dart |
| @@ -24,9 +24,9 @@ class TypeCheckerTask extends CompilerTask { |
| } |
| } |
| -interface Type { |
| - SourceString get name(); |
| - Element get element(); |
| +abstract class Type implements Hashable { |
| + abstract SourceString get name(); |
| + abstract Element get element(); |
| /** |
| * Returns the unaliased type of this type. |
| @@ -38,7 +38,9 @@ interface Type { |
| * function type [: (B) -> A :] and the unaliased type of |
| * [: Func<int,String> :] is the function type [: (String) -> int :]. |
| */ |
| - Type unalias(Compiler compiler); |
| + abstract Type unalias(Compiler compiler); |
| + |
| + abstract bool equals(other); |
| } |
| class TypeVariableType implements Type { |
| @@ -50,6 +52,13 @@ class TypeVariableType implements Type { |
| Type unalias(Compiler compiler) => this; |
| + int hashCode() => 17 * element.hashCode(); |
| + |
| + bool equals(other) { |
| + if (other is !TypeVariableType) return false; |
| + return other.element == element; |
| + } |
| + |
| String toString() => name.slowToString(); |
| } |
| @@ -75,6 +84,13 @@ class StatementType implements Type { |
| Type unalias(Compiler compiler) => this; |
| + int hashCode() => stringName.hashCode(); |
|
ngeoffray
2012/08/30 11:03:33
I've been advised not to return the hashCode of a
karlklose
2012/08/30 13:04:31
Done, forgot this one.
|
| + |
| + bool equals(other) { |
| + if (other is !StatementType) return false; |
| + return other.stringName == stringName; |
| + } |
| + |
| String toString() => stringName; |
| } |
| @@ -85,6 +101,10 @@ class VoidType implements Type { |
| Type unalias(Compiler compiler) => this; |
| + int hashCode() => 1729; |
| + |
| + bool equals(other) => other is VoidType; |
| + |
| String toString() => name.slowToString(); |
| } |
| @@ -109,6 +129,30 @@ class InterfaceType implements Type { |
| } |
| return sb.toString(); |
| } |
| + |
| + int hashCode() { |
| + int hash = element.hashCode(); |
| + for (Link<Type> arguments = this.arguments; |
| + !arguments.isEmpty(); |
| + arguments = arguments.tail) { |
| + hash = 17 * hash + 3 * arguments.head.hashCode(); |
| + } |
| + return hash; |
| + } |
| + |
| + bool equals(other) { |
| + if (other is !InterfaceType) return false; |
| + Link<Type> myArguments = arguments; |
| + Link<Type> otherArguments = other.arguments; |
| + while (!myArguments.isEmpty() && !otherArguments.isEmpty()) { |
| + if (myArguments.head != otherArguments.head) { |
| + return false; |
| + } |
| + myArguments = myArguments.tail; |
| + otherArguments = otherArguments.tail; |
| + } |
| + return myArguments.isEmpty() && otherArguments.isEmpty(); |
| + } |
| } |
| class FunctionType implements Type { |
| @@ -144,6 +188,30 @@ class FunctionType implements Type { |
| returnType = other.returnType; |
| parameterTypes = other.parameterTypes; |
| } |
| + |
| + int hashCode() { |
| + int hash = 17 * element.hashCode() + 3 * returnType.hashCode(); |
| + for (Link<Type> parameters = parameterTypes; |
| + !parameters.isEmpty(); |
| + parameters = parameters.tail) { |
| + hash = 17 * hash + 3 * parameters.head.hashCode(); |
| + } |
| + return hash; |
| + } |
| + |
| + bool equals(other) { |
| + if (other is !FunctionType) return false; |
| + Link<Type> myParameters = parameterTypes; |
| + Link<Type> otherParameters = other.parameters; |
| + while (!myParameters.isEmpty() && !otherParameters.isEmpty()) { |
| + if (myParameters.head != otherParameters.head) { |
| + return false; |
| + } |
| + myParameters = myParameters.tail; |
| + otherParameters = otherParameters.tail; |
| + } |
| + return myParameters.isEmpty() && otherParameters.isEmpty(); |
|
ngeoffray
2012/08/30 11:03:33
This is the same code than line 145. Add an equals
karlklose
2012/08/30 13:04:31
Done.
|
| + } |
| } |
| class TypedefType implements Type { |
| @@ -171,6 +239,13 @@ class TypedefType implements Type { |
| } |
| return sb.toString(); |
| } |
| + |
| + int hashCode() => 17 * element.hashCode(); |
| + |
| + bool equals(other) { |
| + if (other is !TypedefType) return false; |
| + return other.element == element; |
| + } |
| } |
| class Types { |