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 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 } | 58 } |
59 | 59 |
60 String toString() => stringName; | 60 String toString() => stringName; |
61 } | 61 } |
62 | 62 |
63 class InterfaceType implements Type { | 63 class InterfaceType implements Type { |
64 final SourceString name; | 64 final SourceString name; |
65 final Element element; | 65 final Element element; |
66 final Link<Type> arguments; | 66 final Link<Type> arguments; |
67 | 67 |
68 const InterfaceType(this.name, this.element, this.arguments); | 68 const InterfaceType(this.name, this.element, |
| 69 [this.arguments = const EmptyLink<Type>()]); |
69 | 70 |
70 toString() { | 71 toString() { |
71 StringBuffer sb = new StringBuffer(); | 72 StringBuffer sb = new StringBuffer(); |
72 sb.add(name.slowToString()); | 73 sb.add(name.slowToString()); |
73 if (!arguments.isEmpty()) { | 74 if (!arguments.isEmpty()) { |
74 sb.add('<'); | 75 sb.add('<'); |
75 arguments.printOn(sb); | 76 arguments.printOn(sb); |
76 sb.add('>'); | 77 sb.add('>'); |
77 } | 78 } |
78 return sb.toString(); | 79 return sb.toString(); |
79 } | 80 } |
80 } | 81 } |
81 | 82 |
82 // TODO(karlklose): merge into InterfaceType as a named constructor. | |
83 class SimpleType extends InterfaceType { | |
84 const SimpleType(SourceString name, Element element) | |
85 : super(name, element, const EmptyLink<Type>()); | |
86 String toString() => name.slowToString(); | |
87 } | |
88 | |
89 class FunctionType implements Type { | 83 class FunctionType implements Type { |
90 final Element element; | 84 final Element element; |
91 final Type returnType; | 85 final Type returnType; |
92 final Link<Type> parameterTypes; | 86 final Link<Type> parameterTypes; |
93 | 87 |
94 const FunctionType(Type this.returnType, Link<Type> this.parameterTypes, | 88 const FunctionType(Type this.returnType, Link<Type> this.parameterTypes, |
95 Element this.element); | 89 Element this.element); |
96 | 90 |
97 toString() { | 91 toString() { |
98 StringBuffer sb = new StringBuffer(); | 92 StringBuffer sb = new StringBuffer(); |
(...skipping 10 matching lines...) Expand all Loading... |
109 class Types { | 103 class Types { |
110 static final VOID = const SourceString('void'); | 104 static final VOID = const SourceString('void'); |
111 static final INT = const SourceString('int'); | 105 static final INT = const SourceString('int'); |
112 static final DOUBLE = const SourceString('double'); | 106 static final DOUBLE = const SourceString('double'); |
113 static final DYNAMIC = const SourceString('Dynamic'); | 107 static final DYNAMIC = const SourceString('Dynamic'); |
114 static final STRING = const SourceString('String'); | 108 static final STRING = const SourceString('String'); |
115 static final BOOL = const SourceString('bool'); | 109 static final BOOL = const SourceString('bool'); |
116 static final OBJECT = const SourceString('Object'); | 110 static final OBJECT = const SourceString('Object'); |
117 static final LIST = const SourceString('List'); | 111 static final LIST = const SourceString('List'); |
118 | 112 |
119 final SimpleType voidType; | 113 final InterfaceType voidType; |
120 final SimpleType dynamicType; | 114 final InterfaceType dynamicType; |
121 | 115 |
122 Types() : this.with(new LibraryElement(new Script(null, null))); | 116 Types() : this.with(new LibraryElement(new Script(null, null))); |
123 | 117 |
124 Types.with(LibraryElement library) | 118 Types.with(LibraryElement library) |
125 : voidType = new SimpleType(VOID, new ClassElement(VOID, library)), | 119 : voidType = new InterfaceType(VOID, new ClassElement(VOID, library)), |
126 dynamicType = new SimpleType(DYNAMIC, new ClassElement(DYNAMIC, library)); | 120 dynamicType = new InterfaceType(DYNAMIC, new ClassElement(DYNAMIC, library
)); |
127 | 121 |
128 Type lookup(SourceString s) { | 122 Type lookup(SourceString s) { |
129 if (VOID == s) { | 123 if (VOID == s) { |
130 return voidType; | 124 return voidType; |
131 } else if (DYNAMIC == s || s.stringValue === 'var') { | 125 } else if (DYNAMIC == s || s.stringValue === 'var') { |
132 return dynamicType; | 126 return dynamicType; |
133 } | 127 } |
134 return null; | 128 return null; |
135 } | 129 } |
136 | 130 |
137 /** Returns true if t is a subtype of s */ | 131 /** Returns true if t is a subtype of s */ |
138 bool isSubtype(Type t, Type s) { | 132 bool isSubtype(Type t, Type s) { |
139 if (t === s || t === dynamicType || s === dynamicType || | 133 if (t === s || t === dynamicType || s === dynamicType || |
140 s.name == OBJECT) return true; | 134 s.name == OBJECT) return true; |
141 if (t is SimpleType) { | 135 if (t is InterfaceType) { |
142 if (s is !SimpleType) return false; | 136 if (s is !InterfaceType) return false; |
143 ClassElement tc = t.element; | 137 ClassElement tc = t.element; |
144 for (Link<Type> supertypes = tc.allSupertypes; | 138 for (Link<Type> supertypes = tc.allSupertypes; |
145 supertypes != null && !supertypes.isEmpty(); | 139 supertypes != null && !supertypes.isEmpty(); |
146 supertypes = supertypes.tail) { | 140 supertypes = supertypes.tail) { |
147 Type supertype = supertypes.head; | 141 Type supertype = supertypes.head; |
148 if (supertype.element === s.element) return true; | 142 if (supertype.element === s.element) return true; |
149 } | 143 } |
150 return false; | 144 return false; |
151 } else if (t is FunctionType) { | 145 } else if (t is FunctionType) { |
152 if (s is !FunctionType) return false; | 146 if (s is !FunctionType) return false; |
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
749 } | 743 } |
750 | 744 |
751 visitCatchBlock(CatchBlock node) { | 745 visitCatchBlock(CatchBlock node) { |
752 fail(node); | 746 fail(node); |
753 } | 747 } |
754 | 748 |
755 visitTypedef(Typedef node) { | 749 visitTypedef(Typedef node) { |
756 fail(node); | 750 fail(node); |
757 } | 751 } |
758 } | 752 } |
OLD | NEW |