| 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 abstract class DartType implements Hashable { | 27 interface DartType { |
| 28 abstract SourceString get name(); | 28 SourceString get name(); |
| 29 abstract Element get element(); | 29 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 abstract DartType unalias(Compiler compiler); | 41 DartType unalias(Compiler compiler); |
| 42 | |
| 43 abstract bool equals(other); | |
| 44 } | 42 } |
| 45 | 43 |
| 46 class TypeVariableType implements DartType { | 44 class TypeVariableType implements DartType { |
| 47 final TypeVariableElement element; | 45 final TypeVariableElement element; |
| 48 | 46 |
| 49 TypeVariableType(this.element); | 47 TypeVariableType(this.element); |
| 50 | 48 |
| 51 SourceString get name => element.name; | 49 SourceString get name => element.name; |
| 52 | 50 |
| 53 DartType unalias(Compiler compiler) => this; | 51 DartType unalias(Compiler compiler) => this; |
| 54 | 52 |
| 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 | |
| 62 String toString() => name.slowToString(); | 53 String toString() => name.slowToString(); |
| 63 } | 54 } |
| 64 | 55 |
| 65 /** | 56 /** |
| 66 * A statement type tracks whether a statement returns or may return. | 57 * A statement type tracks whether a statement returns or may return. |
| 67 */ | 58 */ |
| 68 class StatementType implements DartType { | 59 class StatementType implements DartType { |
| 69 final String stringName; | 60 final String stringName; |
| 70 Element get element => null; | 61 Element get element => null; |
| 71 | 62 |
| 72 SourceString get name => new SourceString(stringName); | 63 SourceString get name => new SourceString(stringName); |
| 73 | 64 |
| 74 const StatementType(this.stringName); | 65 const StatementType(this.stringName); |
| 75 | 66 |
| 76 static const RETURNING = const StatementType('<returning>'); | 67 static const RETURNING = const StatementType('<returning>'); |
| 77 static const NOT_RETURNING = const StatementType('<not returning>'); | 68 static const NOT_RETURNING = const StatementType('<not returning>'); |
| 78 static const MAYBE_RETURNING = const StatementType('<maybe returning>'); | 69 static const MAYBE_RETURNING = const StatementType('<maybe returning>'); |
| 79 | 70 |
| 80 /** Combine the information about two control-flow edges that are joined. */ | 71 /** Combine the information about two control-flow edges that are joined. */ |
| 81 StatementType join(StatementType other) { | 72 StatementType join(StatementType other) { |
| 82 return (this === other) ? this : MAYBE_RETURNING; | 73 return (this === other) ? this : MAYBE_RETURNING; |
| 83 } | 74 } |
| 84 | 75 |
| 85 DartType unalias(Compiler compiler) => this; | 76 DartType unalias(Compiler compiler) => this; |
| 86 | 77 |
| 87 int hashCode() => 17 * stringName.hashCode(); | |
| 88 | |
| 89 bool equals(other) { | |
| 90 if (other is !StatementType) return false; | |
| 91 return other.stringName == stringName; | |
| 92 } | |
| 93 | |
| 94 String toString() => stringName; | 78 String toString() => stringName; |
| 95 } | 79 } |
| 96 | 80 |
| 97 class VoidType implements DartType { | 81 class VoidType implements DartType { |
| 98 const VoidType(this.element); | 82 const VoidType(this.element); |
| 99 SourceString get name => element.name; | 83 SourceString get name => element.name; |
| 100 final VoidElement element; | 84 final VoidElement element; |
| 101 | 85 |
| 102 DartType unalias(Compiler compiler) => this; | 86 DartType unalias(Compiler compiler) => this; |
| 103 | 87 |
| 104 int hashCode() => 1729; | |
| 105 | |
| 106 bool equals(other) => other is VoidType; | |
| 107 | |
| 108 String toString() => name.slowToString(); | 88 String toString() => name.slowToString(); |
| 109 } | 89 } |
| 110 | 90 |
| 111 class InterfaceType implements DartType { | 91 class InterfaceType implements DartType { |
| 112 final Element element; | 92 final Element element; |
| 113 final Link<DartType> arguments; | 93 final Link<DartType> arguments; |
| 114 | 94 |
| 115 const InterfaceType(this.element, | 95 const InterfaceType(this.element, |
| 116 [this.arguments = const EmptyLink<DartType>()]); | 96 [this.arguments = const EmptyLink<DartType>()]); |
| 117 | 97 |
| 118 SourceString get name => element.name; | 98 SourceString get name => element.name; |
| 119 | 99 |
| 120 DartType unalias(Compiler compiler) => this; | 100 DartType unalias(Compiler compiler) => this; |
| 121 | 101 |
| 122 String toString() { | 102 String toString() { |
| 123 StringBuffer sb = new StringBuffer(); | 103 StringBuffer sb = new StringBuffer(); |
| 124 sb.add(name.slowToString()); | 104 sb.add(name.slowToString()); |
| 125 if (!arguments.isEmpty()) { | 105 if (!arguments.isEmpty()) { |
| 126 sb.add('<'); | 106 sb.add('<'); |
| 127 arguments.printOn(sb, ', '); | 107 arguments.printOn(sb, ', '); |
| 128 sb.add('>'); | 108 sb.add('>'); |
| 129 } | 109 } |
| 130 return sb.toString(); | 110 return sb.toString(); |
| 131 } | 111 } |
| 132 | |
| 133 int hashCode() { | |
| 134 int hash = element.hashCode(); | |
| 135 for (Link<DartType> arguments = this.arguments; | |
| 136 !arguments.isEmpty(); | |
| 137 arguments = arguments.tail) { | |
| 138 int argumentHash = arguments.head != null ? arguments.head.hashCode() : 0; | |
| 139 hash = 17 * hash + 3 * argumentHash; | |
| 140 } | |
| 141 return hash; | |
| 142 } | |
| 143 | |
| 144 bool equals(other) { | |
| 145 if (other is !InterfaceType) return false; | |
| 146 return arguments == other.arguments; | |
| 147 } | |
| 148 } | 112 } |
| 149 | 113 |
| 150 class FunctionType implements DartType { | 114 class FunctionType implements DartType { |
| 151 final Element element; | 115 final Element element; |
| 152 DartType returnType; | 116 DartType returnType; |
| 153 Link<DartType> parameterTypes; | 117 Link<DartType> parameterTypes; |
| 154 | 118 |
| 155 FunctionType(DartType this.returnType, Link<DartType> this.parameterTypes, | 119 FunctionType(DartType this.returnType, Link<DartType> this.parameterTypes, |
| 156 Element this.element); | 120 Element this.element); |
| 157 | 121 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 173 parameterTypes.forEach((_) { arity++; }); | 137 parameterTypes.forEach((_) { arity++; }); |
| 174 return arity; | 138 return arity; |
| 175 } | 139 } |
| 176 | 140 |
| 177 void initializeFrom(FunctionType other) { | 141 void initializeFrom(FunctionType other) { |
| 178 assert(returnType === null); | 142 assert(returnType === null); |
| 179 assert(parameterTypes === null); | 143 assert(parameterTypes === null); |
| 180 returnType = other.returnType; | 144 returnType = other.returnType; |
| 181 parameterTypes = other.parameterTypes; | 145 parameterTypes = other.parameterTypes; |
| 182 } | 146 } |
| 183 | |
| 184 int hashCode() { | |
| 185 int hash = 17 * element.hashCode() + 3 * returnType.hashCode(); | |
| 186 for (Link<DartType> parameters = parameterTypes; | |
| 187 !parameters.isEmpty(); | |
| 188 parameters = parameters.tail) { | |
| 189 hash = 17 * hash + 3 * parameters.head.hashCode(); | |
| 190 } | |
| 191 return hash; | |
| 192 } | |
| 193 | |
| 194 bool equals(other) { | |
| 195 if (other is !FunctionType) return false; | |
| 196 return returnType == other.returnType | |
| 197 && parameterTypes == other.parameterTypes; | |
| 198 } | |
| 199 } | 147 } |
| 200 | 148 |
| 201 class TypedefType implements DartType { | 149 class TypedefType implements DartType { |
| 202 final TypedefElement element; | 150 final TypedefElement element; |
| 203 final Link<DartType> typeArguments; | 151 final Link<DartType> typeArguments; |
| 204 | 152 |
| 205 const TypedefType(this.element, | 153 const TypedefType(this.element, |
| 206 [this.typeArguments = const EmptyLink<DartType>()]); | 154 [this.typeArguments = const EmptyLink<DartType>()]); |
| 207 | 155 |
| 208 SourceString get name => element.name; | 156 SourceString get name => element.name; |
| 209 | 157 |
| 210 DartType unalias(Compiler compiler) { | 158 DartType unalias(Compiler compiler) { |
| 211 // TODO(ahe): This should be [ensureResolved]. | 159 // TODO(ahe): This should be [ensureResolved]. |
| 212 compiler.resolveTypedef(element); | 160 compiler.resolveTypedef(element); |
| 213 return element.alias.unalias(compiler); | 161 return element.alias.unalias(compiler); |
| 214 } | 162 } |
| 215 | 163 |
| 216 String toString() { | 164 String toString() { |
| 217 StringBuffer sb = new StringBuffer(); | 165 StringBuffer sb = new StringBuffer(); |
| 218 sb.add(name.slowToString()); | 166 sb.add(name.slowToString()); |
| 219 if (!typeArguments.isEmpty()) { | 167 if (!typeArguments.isEmpty()) { |
| 220 sb.add('<'); | 168 sb.add('<'); |
| 221 typeArguments.printOn(sb, ', '); | 169 typeArguments.printOn(sb, ', '); |
| 222 sb.add('>'); | 170 sb.add('>'); |
| 223 } | 171 } |
| 224 return sb.toString(); | 172 return sb.toString(); |
| 225 } | 173 } |
| 226 | |
| 227 int hashCode() => 17 * element.hashCode(); | |
| 228 | |
| 229 bool equals(other) { | |
| 230 if (other is !TypedefType) return false; | |
| 231 return other.element == element; | |
| 232 } | |
| 233 } | 174 } |
| 234 | 175 |
| 235 class Types { | 176 class Types { |
| 236 final Compiler compiler; | 177 final Compiler compiler; |
| 237 // TODO(karlklose): should we have a class Void? | 178 // TODO(karlklose): should we have a class Void? |
| 238 final VoidType voidType; | 179 final VoidType voidType; |
| 239 final InterfaceType dynamicType; | 180 final InterfaceType dynamicType; |
| 240 | 181 |
| 241 Types(Compiler compiler, Element dynamicElement) | 182 Types(Compiler compiler, Element dynamicElement) |
| 242 : this.with(compiler, dynamicElement, | 183 : this.with(compiler, dynamicElement, |
| (...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 903 } | 844 } |
| 904 | 845 |
| 905 visitCatchBlock(CatchBlock node) { | 846 visitCatchBlock(CatchBlock node) { |
| 906 return unhandledStatement(); | 847 return unhandledStatement(); |
| 907 } | 848 } |
| 908 | 849 |
| 909 visitTypedef(Typedef node) { | 850 visitTypedef(Typedef node) { |
| 910 return unhandledStatement(); | 851 return unhandledStatement(); |
| 911 } | 852 } |
| 912 } | 853 } |
| OLD | NEW |