| 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 part of dart2js; | 5 part of dart2js; |
| 6 | 6 |
| 7 class TypeCheckerTask extends CompilerTask { | 7 class TypeCheckerTask extends CompilerTask { |
| 8 TypeCheckerTask(Compiler compiler) : super(compiler); | 8 TypeCheckerTask(Compiler compiler) : super(compiler); |
| 9 String get name => "Type checker"; | 9 String get name => "Type checker"; |
| 10 | 10 |
| 11 static const bool LOG_FAILURES = false; | 11 static const bool LOG_FAILURES = false; |
| 12 | 12 |
| 13 void check(Node tree, TreeElements elements) { | 13 void check(Node tree, TreeElements elements) { |
| 14 measure(() { | 14 measure(() { |
| 15 Visitor visitor = | 15 Visitor visitor = |
| 16 new TypeCheckerVisitor(compiler, elements, compiler.types); | 16 new TypeCheckerVisitor(compiler, elements, compiler.types); |
| 17 try { | 17 try { |
| 18 tree.accept(visitor); | 18 tree.accept(visitor); |
| 19 } on CancelTypeCheckException catch (e) { | 19 } on CancelTypeCheckException catch (e) { |
| 20 if (LOG_FAILURES) { | 20 if (LOG_FAILURES) { |
| 21 // Do not warn about unimplemented features; log message instead. | 21 // Do not warn about unimplemented features; log message instead. |
| 22 compiler.log("'${e.node}': ${e.reason}"); | 22 compiler.log("'${e.node}': ${e.reason}"); |
| 23 } | 23 } |
| 24 } | 24 } |
| 25 }); | 25 }); |
| 26 } | 26 } |
| 27 } | 27 } |
| 28 | 28 |
| 29 class TypeKind { |
| 30 final String id; |
| 31 |
| 32 const TypeKind(String this.id); |
| 33 |
| 34 static const TypeKind FUNCTION = const TypeKind('function'); |
| 35 static const TypeKind INTERFACE = const TypeKind('interface'); |
| 36 static const TypeKind STATEMENT = const TypeKind('statement'); |
| 37 static const TypeKind TYPEDEF = const TypeKind('typedef'); |
| 38 static const TypeKind TYPE_VARIABLE = const TypeKind('type variable'); |
| 39 static const TypeKind VOID = const TypeKind('void'); |
| 40 |
| 41 String toString() => id; |
| 42 } |
| 43 |
| 29 abstract class DartType { | 44 abstract class DartType { |
| 30 abstract SourceString get name; | 45 abstract SourceString get name; |
| 31 | 46 |
| 47 abstract TypeKind get kind; |
| 48 |
| 32 /** | 49 /** |
| 33 * Returns the [Element] which declared this type. | 50 * Returns the [Element] which declared this type. |
| 34 * | 51 * |
| 35 * This can be [ClassElement] for classes, [TypedefElement] for typedefs, | 52 * This can be [ClassElement] for classes, [TypedefElement] for typedefs, |
| 36 * [TypeVariableElement] for type variables and [FunctionElement] for | 53 * [TypeVariableElement] for type variables and [FunctionElement] for |
| 37 * function types. | 54 * function types. |
| 38 * | 55 * |
| 39 * Invariant: [element] must be a declaration element. | 56 * Invariant: [element] must be a declaration element. |
| 40 */ | 57 */ |
| 41 abstract Element get element; | 58 abstract Element get element; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 53 abstract DartType unalias(Compiler compiler); | 70 abstract DartType unalias(Compiler compiler); |
| 54 | 71 |
| 55 abstract bool operator ==(other); | 72 abstract bool operator ==(other); |
| 56 } | 73 } |
| 57 | 74 |
| 58 class TypeVariableType implements DartType { | 75 class TypeVariableType implements DartType { |
| 59 final TypeVariableElement element; | 76 final TypeVariableElement element; |
| 60 | 77 |
| 61 TypeVariableType(this.element); | 78 TypeVariableType(this.element); |
| 62 | 79 |
| 80 TypeKind get kind => TypeKind.TYPE_VARIABLE; |
| 81 |
| 63 SourceString get name => element.name; | 82 SourceString get name => element.name; |
| 64 | 83 |
| 65 DartType unalias(Compiler compiler) => this; | 84 DartType unalias(Compiler compiler) => this; |
| 66 | 85 |
| 67 int get hashCode => 17 * element.hashCode; | 86 int get hashCode => 17 * element.hashCode; |
| 68 | 87 |
| 69 bool operator ==(other) { | 88 bool operator ==(other) { |
| 70 if (other is !TypeVariableType) return false; | 89 if (other is !TypeVariableType) return false; |
| 71 return identical(other.element, element); | 90 return identical(other.element, element); |
| 72 } | 91 } |
| 73 | 92 |
| 74 String toString() => name.slowToString(); | 93 String toString() => name.slowToString(); |
| 75 } | 94 } |
| 76 | 95 |
| 77 /** | 96 /** |
| 78 * A statement type tracks whether a statement returns or may return. | 97 * A statement type tracks whether a statement returns or may return. |
| 79 */ | 98 */ |
| 80 class StatementType implements DartType { | 99 class StatementType implements DartType { |
| 81 final String stringName; | 100 final String stringName; |
| 101 |
| 82 Element get element => null; | 102 Element get element => null; |
| 83 | 103 |
| 104 TypeKind get kind => TypeKind.STATEMENT; |
| 105 |
| 84 SourceString get name => new SourceString(stringName); | 106 SourceString get name => new SourceString(stringName); |
| 85 | 107 |
| 86 const StatementType(this.stringName); | 108 const StatementType(this.stringName); |
| 87 | 109 |
| 88 static const RETURNING = const StatementType('<returning>'); | 110 static const RETURNING = const StatementType('<returning>'); |
| 89 static const NOT_RETURNING = const StatementType('<not returning>'); | 111 static const NOT_RETURNING = const StatementType('<not returning>'); |
| 90 static const MAYBE_RETURNING = const StatementType('<maybe returning>'); | 112 static const MAYBE_RETURNING = const StatementType('<maybe returning>'); |
| 91 | 113 |
| 92 /** Combine the information about two control-flow edges that are joined. */ | 114 /** Combine the information about two control-flow edges that are joined. */ |
| 93 StatementType join(StatementType other) { | 115 StatementType join(StatementType other) { |
| 94 return (identical(this, other)) ? this : MAYBE_RETURNING; | 116 return (identical(this, other)) ? this : MAYBE_RETURNING; |
| 95 } | 117 } |
| 96 | 118 |
| 97 DartType unalias(Compiler compiler) => this; | 119 DartType unalias(Compiler compiler) => this; |
| 98 | 120 |
| 99 int get hashCode => 17 * stringName.hashCode; | 121 int get hashCode => 17 * stringName.hashCode; |
| 100 | 122 |
| 101 bool operator ==(other) { | 123 bool operator ==(other) { |
| 102 if (other is !StatementType) return false; | 124 if (other is !StatementType) return false; |
| 103 return other.stringName == stringName; | 125 return other.stringName == stringName; |
| 104 } | 126 } |
| 105 | 127 |
| 106 String toString() => stringName; | 128 String toString() => stringName; |
| 107 } | 129 } |
| 108 | 130 |
| 109 class VoidType implements DartType { | 131 class VoidType implements DartType { |
| 110 const VoidType(this.element); | 132 const VoidType(this.element); |
| 133 |
| 134 TypeKind get kind => TypeKind.VOID; |
| 135 |
| 111 SourceString get name => element.name; | 136 SourceString get name => element.name; |
| 137 |
| 112 final VoidElement element; | 138 final VoidElement element; |
| 113 | 139 |
| 114 DartType unalias(Compiler compiler) => this; | 140 DartType unalias(Compiler compiler) => this; |
| 115 | 141 |
| 116 int get hashCode => 1729; | 142 int get hashCode => 1729; |
| 117 | 143 |
| 118 bool operator ==(other) => other is VoidType; | 144 bool operator ==(other) => other is VoidType; |
| 119 | 145 |
| 120 String toString() => name.slowToString(); | 146 String toString() => name.slowToString(); |
| 121 } | 147 } |
| 122 | 148 |
| 123 class InterfaceType implements DartType { | 149 class InterfaceType implements DartType { |
| 124 final Element element; | 150 final Element element; |
| 125 final Link<DartType> arguments; | 151 final Link<DartType> arguments; |
| 126 | 152 |
| 127 const InterfaceType(this.element, | 153 const InterfaceType(this.element, |
| 128 [this.arguments = const Link<DartType>()]); | 154 [this.arguments = const Link<DartType>()]); |
| 129 | 155 |
| 156 TypeKind get kind => TypeKind.INTERFACE; |
| 157 |
| 130 SourceString get name => element.name; | 158 SourceString get name => element.name; |
| 131 | 159 |
| 132 DartType unalias(Compiler compiler) => this; | 160 DartType unalias(Compiler compiler) => this; |
| 133 | 161 |
| 134 String toString() { | 162 String toString() { |
| 135 StringBuffer sb = new StringBuffer(); | 163 StringBuffer sb = new StringBuffer(); |
| 136 sb.add(name.slowToString()); | 164 sb.add(name.slowToString()); |
| 137 if (!arguments.isEmpty) { | 165 if (!arguments.isEmpty) { |
| 138 sb.add('<'); | 166 sb.add('<'); |
| 139 arguments.printOn(sb, ', '); | 167 arguments.printOn(sb, ', '); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 163 class FunctionType implements DartType { | 191 class FunctionType implements DartType { |
| 164 final Element element; | 192 final Element element; |
| 165 DartType returnType; | 193 DartType returnType; |
| 166 Link<DartType> parameterTypes; | 194 Link<DartType> parameterTypes; |
| 167 | 195 |
| 168 FunctionType(DartType this.returnType, Link<DartType> this.parameterTypes, | 196 FunctionType(DartType this.returnType, Link<DartType> this.parameterTypes, |
| 169 Element this.element) { | 197 Element this.element) { |
| 170 assert(element == null || invariant(element, element.isDeclaration)); | 198 assert(element == null || invariant(element, element.isDeclaration)); |
| 171 } | 199 } |
| 172 | 200 |
| 201 TypeKind get kind => TypeKind.FUNCTION; |
| 202 |
| 173 DartType unalias(Compiler compiler) => this; | 203 DartType unalias(Compiler compiler) => this; |
| 174 | 204 |
| 175 String toString() { | 205 String toString() { |
| 176 StringBuffer sb = new StringBuffer(); | 206 StringBuffer sb = new StringBuffer(); |
| 177 bool first = true; | 207 bool first = true; |
| 178 sb.add('('); | 208 sb.add('('); |
| 179 parameterTypes.printOn(sb, ', '); | 209 parameterTypes.printOn(sb, ', '); |
| 180 sb.add(') -> ${returnType}'); | 210 sb.add(') -> ${returnType}'); |
| 181 return sb.toString(); | 211 return sb.toString(); |
| 182 } | 212 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 213 } | 243 } |
| 214 } | 244 } |
| 215 | 245 |
| 216 class TypedefType implements DartType { | 246 class TypedefType implements DartType { |
| 217 final TypedefElement element; | 247 final TypedefElement element; |
| 218 final Link<DartType> typeArguments; | 248 final Link<DartType> typeArguments; |
| 219 | 249 |
| 220 const TypedefType(this.element, | 250 const TypedefType(this.element, |
| 221 [this.typeArguments = const Link<DartType>()]); | 251 [this.typeArguments = const Link<DartType>()]); |
| 222 | 252 |
| 253 TypeKind get kind => TypeKind.TYPEDEF; |
| 254 |
| 223 SourceString get name => element.name; | 255 SourceString get name => element.name; |
| 224 | 256 |
| 225 DartType unalias(Compiler compiler) { | 257 DartType unalias(Compiler compiler) { |
| 226 // TODO(ahe): This should be [ensureResolved]. | 258 // TODO(ahe): This should be [ensureResolved]. |
| 227 compiler.resolveTypedef(element); | 259 compiler.resolveTypedef(element); |
| 228 return element.alias.unalias(compiler); | 260 return element.alias.unalias(compiler); |
| 229 } | 261 } |
| 230 | 262 |
| 231 String toString() { | 263 String toString() { |
| 232 StringBuffer sb = new StringBuffer(); | 264 StringBuffer sb = new StringBuffer(); |
| (...skipping 756 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 989 } | 1021 } |
| 990 | 1022 |
| 991 DartType visitStringNode(StringNode node) { | 1023 DartType visitStringNode(StringNode node) { |
| 992 compiler.unimplemented('visitNode', node: node); | 1024 compiler.unimplemented('visitNode', node: node); |
| 993 } | 1025 } |
| 994 | 1026 |
| 995 DartType visitLibraryDependency(LibraryDependency node) { | 1027 DartType visitLibraryDependency(LibraryDependency node) { |
| 996 compiler.unimplemented('visitNode', node: node); | 1028 compiler.unimplemented('visitNode', node: node); |
| 997 } | 1029 } |
| 998 } | 1030 } |
| OLD | NEW |