| 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 final bool LOG_FAILURES = false; | 9 static final 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 } catch (CancelTypeCheckException e) { | 17 } catch (CancelTypeCheckException 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 interface Type { |
| 28 SourceString get name(); | 28 SourceString get name(); |
| 29 Element get element(); | 29 Element get element(); |
| 30 | |
| 31 /** | |
| 32 * Returns the unaliased type of this type. | |
| 33 * | |
| 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. | |
| 36 * | |
| 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 | |
| 39 * [: Func<int,String> :] is the function type [: (String) -> int :]. | |
| 40 */ | |
| 41 Type unalias(Compiler compiler); | |
| 42 } | 30 } |
| 43 | 31 |
| 44 class TypeVariableType implements Type { | 32 class TypeVariableType implements Type { |
| 45 final TypeVariableElement element; | 33 final TypeVariableElement element; |
| 46 | 34 |
| 47 TypeVariableType(this.element); | 35 TypeVariableType(this.element); |
| 48 | 36 |
| 49 SourceString get name() => element.name; | 37 SourceString get name() => element.name; |
| 50 | 38 |
| 51 Type unalias(Compiler compiler) => this; | 39 toString() => name.slowToString(); |
| 52 | |
| 53 String toString() => name.slowToString(); | |
| 54 } | 40 } |
| 55 | 41 |
| 56 /** | 42 /** |
| 57 * A statement type tracks whether a statement returns or may return. | 43 * A statement type tracks whether a statement returns or may return. |
| 58 */ | 44 */ |
| 59 class StatementType implements Type { | 45 class StatementType implements Type { |
| 60 final String stringName; | 46 final String stringName; |
| 61 Element get element() => null; | 47 Element get element() => null; |
| 62 | 48 |
| 63 SourceString get name() => new SourceString(stringName); | 49 SourceString get name() => new SourceString(stringName); |
| 64 | 50 |
| 65 const StatementType(this.stringName); | 51 const StatementType(this.stringName); |
| 66 | 52 |
| 67 static final RETURNING = const StatementType('<returning>'); | 53 static final RETURNING = const StatementType('<returning>'); |
| 68 static final NOT_RETURNING = const StatementType('<not returning>'); | 54 static final NOT_RETURNING = const StatementType('<not returning>'); |
| 69 static final MAYBE_RETURNING = const StatementType('<maybe returning>'); | 55 static final MAYBE_RETURNING = const StatementType('<maybe returning>'); |
| 70 | 56 |
| 71 /** Combine the information about two control-flow edges that are joined. */ | 57 /** Combine the information about two control-flow edges that are joined. */ |
| 72 StatementType join(StatementType other) { | 58 StatementType join(StatementType other) { |
| 73 return (this === other) ? this : MAYBE_RETURNING; | 59 return (this === other) ? this : MAYBE_RETURNING; |
| 74 } | 60 } |
| 75 | 61 |
| 76 Type unalias(Compiler compiler) => this; | |
| 77 | |
| 78 String toString() => stringName; | 62 String toString() => stringName; |
| 79 } | 63 } |
| 80 | 64 |
| 81 class VoidType implements Type { | 65 class VoidType implements Type { |
| 82 const VoidType(this.element); | 66 const VoidType(this.element); |
| 83 SourceString get name() => element.name; | 67 SourceString get name() => element.name; |
| 84 final VoidElement element; | 68 final VoidElement element; |
| 85 | 69 |
| 86 Type unalias(Compiler compiler) => this; | 70 toString() => name.slowToString(); |
| 87 | |
| 88 String toString() => name.slowToString(); | |
| 89 } | 71 } |
| 90 | 72 |
| 91 class InterfaceType implements Type { | 73 class InterfaceType implements Type { |
| 92 final Element element; | 74 final Element element; |
| 93 final Link<Type> arguments; | 75 final Link<Type> arguments; |
| 94 | 76 |
| 95 const InterfaceType(this.element, | 77 const InterfaceType(this.element, |
| 96 [this.arguments = const EmptyLink<Type>()]); | 78 [this.arguments = const EmptyLink<Type>()]); |
| 97 | 79 |
| 98 SourceString get name() => element.name; | 80 SourceString get name() => element.name; |
| 99 | 81 |
| 100 Type unalias(Compiler compiler) => this; | 82 toString() { |
| 101 | |
| 102 String toString() { | |
| 103 StringBuffer sb = new StringBuffer(); | 83 StringBuffer sb = new StringBuffer(); |
| 104 sb.add(name.slowToString()); | 84 sb.add(name.slowToString()); |
| 105 if (!arguments.isEmpty()) { | 85 if (!arguments.isEmpty()) { |
| 106 sb.add('<'); | 86 sb.add('<'); |
| 107 arguments.printOn(sb, ', '); | 87 arguments.printOn(sb, ', '); |
| 108 sb.add('>'); | 88 sb.add('>'); |
| 109 } | 89 } |
| 110 return sb.toString(); | 90 return sb.toString(); |
| 111 } | 91 } |
| 112 } | 92 } |
| 113 | 93 |
| 114 class FunctionType implements Type { | 94 class FunctionType implements Type { |
| 115 final Element element; | 95 final Element element; |
| 116 final Type returnType; | 96 final Type returnType; |
| 117 final Link<Type> parameterTypes; | 97 final Link<Type> parameterTypes; |
| 118 | 98 |
| 119 const FunctionType(Type this.returnType, Link<Type> this.parameterTypes, | 99 const FunctionType(Type this.returnType, Link<Type> this.parameterTypes, |
| 120 Element this.element); | 100 Element this.element); |
| 121 | 101 |
| 122 Type unalias(Compiler compiler) => this; | 102 toString() { |
| 123 | |
| 124 String toString() { | |
| 125 StringBuffer sb = new StringBuffer(); | 103 StringBuffer sb = new StringBuffer(); |
| 126 bool first = true; | 104 bool first = true; |
| 127 sb.add('('); | 105 sb.add('('); |
| 128 parameterTypes.printOn(sb, ', '); | 106 parameterTypes.printOn(sb, ', '); |
| 129 sb.add(') -> ${returnType}'); | 107 sb.add(') -> ${returnType}'); |
| 130 return sb.toString(); | 108 return sb.toString(); |
| 131 } | 109 } |
| 132 | 110 |
| 133 SourceString get name() => const SourceString('Function'); | 111 SourceString get name() => const SourceString('Function'); |
| 134 | 112 |
| 135 int computeArity() { | 113 int computeArity() { |
| 136 int arity = 0; | 114 int arity = 0; |
| 137 parameterTypes.forEach((_) { arity++; }); | 115 parameterTypes.forEach((_) { arity++; }); |
| 138 return arity; | 116 return arity; |
| 139 } | 117 } |
| 140 } | 118 } |
| 141 | 119 |
| 142 class TypedefType implements Type { | |
| 143 final TypedefElement element; | |
| 144 final Link<Type> typeArguments; | |
| 145 | |
| 146 const TypedefType(this.element, | |
| 147 [this.typeArguments = const EmptyLink<Type>()]); | |
| 148 | |
| 149 SourceString get name() => element.name; | |
| 150 | |
| 151 Type unalias(Compiler compiler) { | |
| 152 // TODO(ahe): This should [ensureResolved]. | |
| 153 compiler.resolveTypedef(element); | |
| 154 return element.alias.unalias(compiler); | |
| 155 } | |
| 156 | |
| 157 String toString() { | |
| 158 StringBuffer sb = new StringBuffer(); | |
| 159 sb.add(name.slowToString()); | |
| 160 if (!typeArguments.isEmpty()) { | |
| 161 sb.add('<'); | |
| 162 typeArguments.printOn(sb, ', '); | |
| 163 sb.add('>'); | |
| 164 } | |
| 165 return sb.toString(); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 class Types { | 120 class Types { |
| 170 final Compiler compiler; | |
| 171 final VoidType voidType; | 121 final VoidType voidType; |
| 172 final InterfaceType dynamicType; | 122 final InterfaceType dynamicType; |
| 173 | 123 |
| 174 Types(Compiler compiler, Element dynamicElement) | 124 Types(Element dynamicElement) |
| 175 : this.with(compiler, dynamicElement, | 125 : this.with(dynamicElement, new LibraryElement(new Script(null, null))); |
| 176 new LibraryElement(new Script(null, null))); | |
| 177 | 126 |
| 178 // TODO(karlklose): should we have a class Void? | 127 // TODO(karlklose): should we have a class Void? |
| 179 Types.with(Compiler this.compiler, | 128 Types.with(Element dynamicElement, LibraryElement library) |
| 180 Element dynamicElement, | |
| 181 LibraryElement library) | |
| 182 : voidType = new VoidType(new VoidElement(library)), | 129 : voidType = new VoidType(new VoidElement(library)), |
| 183 dynamicType = new InterfaceType(dynamicElement); | 130 dynamicType = new InterfaceType(dynamicElement); |
| 184 | 131 |
| 185 /** Returns true if t is a subtype of s */ | 132 /** Returns true if t is a subtype of s */ |
| 186 bool isSubtype(Type t, Type s) { | 133 bool isSubtype(Type t, Type s) { |
| 187 if (t === s || | 134 if (t === s || t === dynamicType || s === dynamicType || |
| 188 t === dynamicType || | 135 // TODO(karlklose): Test for s.element === compiler.objectClass. |
| 189 s === dynamicType || | 136 s.name == const SourceString('Object')) return true; |
| 190 s.element === compiler.objectClass) { | |
| 191 return true; | |
| 192 } | |
| 193 t = t.unalias(compiler); | |
| 194 s = s.unalias(compiler); | |
| 195 | |
| 196 if (t is VoidType) { | 137 if (t is VoidType) { |
| 197 return false; | 138 return false; |
| 198 } else if (t is InterfaceType) { | 139 } else if (t is InterfaceType) { |
| 199 if (s is !InterfaceType) return false; | 140 if (s is !InterfaceType) return false; |
| 200 ClassElement tc = t.element; | 141 ClassElement tc = t.element; |
| 201 if (tc === s.element) return true; | 142 if (tc === s.element) return true; |
| 202 for (Link<Type> supertypes = tc.allSupertypes; | 143 for (Link<Type> supertypes = tc.allSupertypes; |
| 203 supertypes != null && !supertypes.isEmpty(); | 144 supertypes != null && !supertypes.isEmpty(); |
| 204 supertypes = supertypes.tail) { | 145 supertypes = supertypes.tail) { |
| 205 Type supertype = supertypes.head; | 146 Type supertype = supertypes.head; |
| 206 if (supertype.element === s.element) return true; | 147 if (supertype.element === s.element) return true; |
| 207 } | 148 } |
| 208 return false; | 149 return false; |
| 209 } else if (t is FunctionType) { | 150 } else if (t is FunctionType) { |
| 210 if (s.element === compiler.functionClass) return true; | |
| 211 if (s is !FunctionType) return false; | 151 if (s is !FunctionType) return false; |
| 212 FunctionType tf = t; | 152 FunctionType tf = t; |
| 213 FunctionType sf = s; | 153 FunctionType sf = s; |
| 214 Link<Type> tps = tf.parameterTypes; | 154 Link<Type> tps = tf.parameterTypes; |
| 215 Link<Type> sps = sf.parameterTypes; | 155 Link<Type> sps = sf.parameterTypes; |
| 216 while (!tps.isEmpty() && !sps.isEmpty()) { | 156 while (!tps.isEmpty() && !sps.isEmpty()) { |
| 217 if (!isAssignable(tps.head, sps.head)) return false; | 157 if (!isAssignable(tps.head, sps.head)) return false; |
| 218 tps = tps.tail; | 158 tps = tps.tail; |
| 219 sps = sps.tail; | 159 sps = sps.tail; |
| 220 } | 160 } |
| (...skipping 613 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 834 } | 774 } |
| 835 | 775 |
| 836 visitCatchBlock(CatchBlock node) { | 776 visitCatchBlock(CatchBlock node) { |
| 837 return unhandledStatement(); | 777 return unhandledStatement(); |
| 838 } | 778 } |
| 839 | 779 |
| 840 visitTypedef(Typedef node) { | 780 visitTypedef(Typedef node) { |
| 841 return unhandledStatement(); | 781 return unhandledStatement(); |
| 842 } | 782 } |
| 843 } | 783 } |
| OLD | NEW |