Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 class TypeCheckerTask extends CompilerTask { | 5 class TypeCheckerTask extends CompilerTask { |
| 6 TypeCheckerTask(Compiler compiler) : super(compiler); | 6 TypeCheckerTask(Compiler compiler) : super(compiler); |
| 7 String get name => "Type checker"; | 7 String get name => "Type checker"; |
| 8 | 8 |
| 9 static const bool LOG_FAILURES = false; | 9 static const bool LOG_FAILURES = false; |
| 10 | 10 |
| 11 void check(Node tree, TreeElements elements) { | 11 void check(Node tree, TreeElements elements) { |
| 12 measure(() { | 12 measure(() { |
| 13 Visitor visitor = | 13 Visitor visitor = |
| 14 new TypeCheckerVisitor(compiler, elements, compiler.types); | 14 new TypeCheckerVisitor(compiler, elements, compiler.types); |
| 15 try { | 15 try { |
| 16 tree.accept(visitor); | 16 tree.accept(visitor); |
| 17 } on CancelTypeCheckException catch (e) { | 17 } on CancelTypeCheckException catch (e) { |
| 18 if (LOG_FAILURES) { | 18 if (LOG_FAILURES) { |
| 19 // Do not warn about unimplemented features; log message instead. | 19 // Do not warn about unimplemented features; log message instead. |
| 20 compiler.log("'${e.node}': ${e.reason}"); | 20 compiler.log("'${e.node}': ${e.reason}"); |
| 21 } | 21 } |
| 22 } | 22 } |
| 23 }); | 23 }); |
| 24 } | 24 } |
| 25 } | 25 } |
| 26 | 26 |
| 27 interface Type { | 27 abstract class Type implements Hashable { |
| 28 SourceString get name(); | 28 abstract SourceString get name(); |
| 29 Element get element(); | 29 abstract Element get element(); |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * Returns the unaliased type of this type. | 32 * Returns the unaliased type of this type. |
| 33 * | 33 * |
| 34 * The unaliased type of a typedef'd type is the unaliased type to which its | 34 * The unaliased type of a typedef'd type is the unaliased type to which its |
| 35 * name is bound. The unaliased version of any other type is the type itself. | 35 * name is bound. The unaliased version of any other type is the type itself. |
| 36 * | 36 * |
| 37 * For example, the unaliased type of [: typedef A Func<A,B>(B b) :] is the | 37 * For example, the unaliased type of [: typedef A Func<A,B>(B b) :] is the |
| 38 * function type [: (B) -> A :] and the unaliased type of | 38 * function type [: (B) -> A :] and the unaliased type of |
| 39 * [: Func<int,String> :] is the function type [: (String) -> int :]. | 39 * [: Func<int,String> :] is the function type [: (String) -> int :]. |
| 40 */ | 40 */ |
| 41 Type unalias(Compiler compiler); | 41 abstract Type unalias(Compiler compiler); |
| 42 | |
| 43 abstract bool equals(other); | |
| 42 } | 44 } |
| 43 | 45 |
| 44 class TypeVariableType implements Type { | 46 class TypeVariableType implements Type { |
| 45 final TypeVariableElement element; | 47 final TypeVariableElement element; |
| 46 | 48 |
| 47 TypeVariableType(this.element); | 49 TypeVariableType(this.element); |
| 48 | 50 |
| 49 SourceString get name => element.name; | 51 SourceString get name => element.name; |
| 50 | 52 |
| 51 Type unalias(Compiler compiler) => this; | 53 Type unalias(Compiler compiler) => this; |
| 52 | 54 |
| 55 int hashCode() => 17 * element.hashCode(); | |
| 56 | |
| 57 bool equals(other) { | |
| 58 if (other is !TypeVariableType) return false; | |
| 59 return other.element == element; | |
| 60 } | |
| 61 | |
| 53 String toString() => name.slowToString(); | 62 String toString() => name.slowToString(); |
| 54 } | 63 } |
| 55 | 64 |
| 56 /** | 65 /** |
| 57 * A statement type tracks whether a statement returns or may return. | 66 * A statement type tracks whether a statement returns or may return. |
| 58 */ | 67 */ |
| 59 class StatementType implements Type { | 68 class StatementType implements Type { |
| 60 final String stringName; | 69 final String stringName; |
| 61 Element get element => null; | 70 Element get element => null; |
| 62 | 71 |
| 63 SourceString get name => new SourceString(stringName); | 72 SourceString get name => new SourceString(stringName); |
| 64 | 73 |
| 65 const StatementType(this.stringName); | 74 const StatementType(this.stringName); |
| 66 | 75 |
| 67 static const RETURNING = const StatementType('<returning>'); | 76 static const RETURNING = const StatementType('<returning>'); |
| 68 static const NOT_RETURNING = const StatementType('<not returning>'); | 77 static const NOT_RETURNING = const StatementType('<not returning>'); |
| 69 static const MAYBE_RETURNING = const StatementType('<maybe returning>'); | 78 static const MAYBE_RETURNING = const StatementType('<maybe returning>'); |
| 70 | 79 |
| 71 /** Combine the information about two control-flow edges that are joined. */ | 80 /** Combine the information about two control-flow edges that are joined. */ |
| 72 StatementType join(StatementType other) { | 81 StatementType join(StatementType other) { |
| 73 return (this === other) ? this : MAYBE_RETURNING; | 82 return (this === other) ? this : MAYBE_RETURNING; |
| 74 } | 83 } |
| 75 | 84 |
| 76 Type unalias(Compiler compiler) => this; | 85 Type unalias(Compiler compiler) => this; |
| 77 | 86 |
| 87 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.
| |
| 88 | |
| 89 bool equals(other) { | |
| 90 if (other is !StatementType) return false; | |
| 91 return other.stringName == stringName; | |
| 92 } | |
| 93 | |
| 78 String toString() => stringName; | 94 String toString() => stringName; |
| 79 } | 95 } |
| 80 | 96 |
| 81 class VoidType implements Type { | 97 class VoidType implements Type { |
| 82 const VoidType(this.element); | 98 const VoidType(this.element); |
| 83 SourceString get name => element.name; | 99 SourceString get name => element.name; |
| 84 final VoidElement element; | 100 final VoidElement element; |
| 85 | 101 |
| 86 Type unalias(Compiler compiler) => this; | 102 Type unalias(Compiler compiler) => this; |
| 87 | 103 |
| 104 int hashCode() => 1729; | |
| 105 | |
| 106 bool equals(other) => other is VoidType; | |
| 107 | |
| 88 String toString() => name.slowToString(); | 108 String toString() => name.slowToString(); |
| 89 } | 109 } |
| 90 | 110 |
| 91 class InterfaceType implements Type { | 111 class InterfaceType implements Type { |
| 92 final Element element; | 112 final Element element; |
| 93 final Link<Type> arguments; | 113 final Link<Type> arguments; |
| 94 | 114 |
| 95 const InterfaceType(this.element, | 115 const InterfaceType(this.element, |
| 96 [this.arguments = const EmptyLink<Type>()]); | 116 [this.arguments = const EmptyLink<Type>()]); |
| 97 | 117 |
| 98 SourceString get name => element.name; | 118 SourceString get name => element.name; |
| 99 | 119 |
| 100 Type unalias(Compiler compiler) => this; | 120 Type unalias(Compiler compiler) => this; |
| 101 | 121 |
| 102 String toString() { | 122 String toString() { |
| 103 StringBuffer sb = new StringBuffer(); | 123 StringBuffer sb = new StringBuffer(); |
| 104 sb.add(name.slowToString()); | 124 sb.add(name.slowToString()); |
| 105 if (!arguments.isEmpty()) { | 125 if (!arguments.isEmpty()) { |
| 106 sb.add('<'); | 126 sb.add('<'); |
| 107 arguments.printOn(sb, ', '); | 127 arguments.printOn(sb, ', '); |
| 108 sb.add('>'); | 128 sb.add('>'); |
| 109 } | 129 } |
| 110 return sb.toString(); | 130 return sb.toString(); |
| 111 } | 131 } |
| 132 | |
| 133 int hashCode() { | |
| 134 int hash = element.hashCode(); | |
| 135 for (Link<Type> arguments = this.arguments; | |
| 136 !arguments.isEmpty(); | |
| 137 arguments = arguments.tail) { | |
| 138 hash = 17 * hash + 3 * arguments.head.hashCode(); | |
| 139 } | |
| 140 return hash; | |
| 141 } | |
| 142 | |
| 143 bool equals(other) { | |
| 144 if (other is !InterfaceType) return false; | |
| 145 Link<Type> myArguments = arguments; | |
| 146 Link<Type> otherArguments = other.arguments; | |
| 147 while (!myArguments.isEmpty() && !otherArguments.isEmpty()) { | |
| 148 if (myArguments.head != otherArguments.head) { | |
| 149 return false; | |
| 150 } | |
| 151 myArguments = myArguments.tail; | |
| 152 otherArguments = otherArguments.tail; | |
| 153 } | |
| 154 return myArguments.isEmpty() && otherArguments.isEmpty(); | |
| 155 } | |
| 112 } | 156 } |
| 113 | 157 |
| 114 class FunctionType implements Type { | 158 class FunctionType implements Type { |
| 115 final Element element; | 159 final Element element; |
| 116 Type returnType; | 160 Type returnType; |
| 117 Link<Type> parameterTypes; | 161 Link<Type> parameterTypes; |
| 118 | 162 |
| 119 FunctionType(Type this.returnType, Link<Type> this.parameterTypes, | 163 FunctionType(Type this.returnType, Link<Type> this.parameterTypes, |
| 120 Element this.element); | 164 Element this.element); |
| 121 | 165 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 137 parameterTypes.forEach((_) { arity++; }); | 181 parameterTypes.forEach((_) { arity++; }); |
| 138 return arity; | 182 return arity; |
| 139 } | 183 } |
| 140 | 184 |
| 141 void initializeFrom(FunctionType other) { | 185 void initializeFrom(FunctionType other) { |
| 142 assert(returnType === null); | 186 assert(returnType === null); |
| 143 assert(parameterTypes === null); | 187 assert(parameterTypes === null); |
| 144 returnType = other.returnType; | 188 returnType = other.returnType; |
| 145 parameterTypes = other.parameterTypes; | 189 parameterTypes = other.parameterTypes; |
| 146 } | 190 } |
| 191 | |
| 192 int hashCode() { | |
| 193 int hash = 17 * element.hashCode() + 3 * returnType.hashCode(); | |
| 194 for (Link<Type> parameters = parameterTypes; | |
| 195 !parameters.isEmpty(); | |
| 196 parameters = parameters.tail) { | |
| 197 hash = 17 * hash + 3 * parameters.head.hashCode(); | |
| 198 } | |
| 199 return hash; | |
| 200 } | |
| 201 | |
| 202 bool equals(other) { | |
| 203 if (other is !FunctionType) return false; | |
| 204 Link<Type> myParameters = parameterTypes; | |
| 205 Link<Type> otherParameters = other.parameters; | |
| 206 while (!myParameters.isEmpty() && !otherParameters.isEmpty()) { | |
| 207 if (myParameters.head != otherParameters.head) { | |
| 208 return false; | |
| 209 } | |
| 210 myParameters = myParameters.tail; | |
| 211 otherParameters = otherParameters.tail; | |
| 212 } | |
| 213 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.
| |
| 214 } | |
| 147 } | 215 } |
| 148 | 216 |
| 149 class TypedefType implements Type { | 217 class TypedefType implements Type { |
| 150 final TypedefElement element; | 218 final TypedefElement element; |
| 151 final Link<Type> typeArguments; | 219 final Link<Type> typeArguments; |
| 152 | 220 |
| 153 const TypedefType(this.element, | 221 const TypedefType(this.element, |
| 154 [this.typeArguments = const EmptyLink<Type>()]); | 222 [this.typeArguments = const EmptyLink<Type>()]); |
| 155 | 223 |
| 156 SourceString get name => element.name; | 224 SourceString get name => element.name; |
| 157 | 225 |
| 158 Type unalias(Compiler compiler) { | 226 Type unalias(Compiler compiler) { |
| 159 // TODO(ahe): This should be [ensureResolved]. | 227 // TODO(ahe): This should be [ensureResolved]. |
| 160 compiler.resolveTypedef(element); | 228 compiler.resolveTypedef(element); |
| 161 return element.alias.unalias(compiler); | 229 return element.alias.unalias(compiler); |
| 162 } | 230 } |
| 163 | 231 |
| 164 String toString() { | 232 String toString() { |
| 165 StringBuffer sb = new StringBuffer(); | 233 StringBuffer sb = new StringBuffer(); |
| 166 sb.add(name.slowToString()); | 234 sb.add(name.slowToString()); |
| 167 if (!typeArguments.isEmpty()) { | 235 if (!typeArguments.isEmpty()) { |
| 168 sb.add('<'); | 236 sb.add('<'); |
| 169 typeArguments.printOn(sb, ', '); | 237 typeArguments.printOn(sb, ', '); |
| 170 sb.add('>'); | 238 sb.add('>'); |
| 171 } | 239 } |
| 172 return sb.toString(); | 240 return sb.toString(); |
| 173 } | 241 } |
| 242 | |
| 243 int hashCode() => 17 * element.hashCode(); | |
| 244 | |
| 245 bool equals(other) { | |
| 246 if (other is !TypedefType) return false; | |
| 247 return other.element == element; | |
| 248 } | |
| 174 } | 249 } |
| 175 | 250 |
| 176 class Types { | 251 class Types { |
| 177 final Compiler compiler; | 252 final Compiler compiler; |
| 178 // TODO(karlklose): should we have a class Void? | 253 // TODO(karlklose): should we have a class Void? |
| 179 final VoidType voidType; | 254 final VoidType voidType; |
| 180 final InterfaceType dynamicType; | 255 final InterfaceType dynamicType; |
| 181 | 256 |
| 182 Types(Compiler compiler, Element dynamicElement) | 257 Types(Compiler compiler, Element dynamicElement) |
| 183 : this.with(compiler, dynamicElement, | 258 : this.with(compiler, dynamicElement, |
| (...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 843 } | 918 } |
| 844 | 919 |
| 845 visitCatchBlock(CatchBlock node) { | 920 visitCatchBlock(CatchBlock node) { |
| 846 return unhandledStatement(); | 921 return unhandledStatement(); |
| 847 } | 922 } |
| 848 | 923 |
| 849 visitTypedef(Typedef node) { | 924 visitTypedef(Typedef node) { |
| 850 return unhandledStatement(); | 925 return unhandledStatement(); |
| 851 } | 926 } |
| 852 } | 927 } |
| OLD | NEW |